/** * Shuffles the provided instances so that they will not always be returned * in the same order. * * @param filterUpInstances * whether to return only UP instances */ public void shuffleInstances(boolean filterUpInstances) { shuffleInstances(filterUpInstances, false, null, null, null); }
/** * Shuffles a whole region so that the instances will not always be returned * in the same order. * * @param remoteRegionsRegistry * the map of remote region names to their registries * @param clientConfig * the {@link EurekaClientConfig}, whose settings will be used to * determine whether to filter to only UP instances * @param instanceRegionChecker * the instance region checker */ public void shuffleAndIndexInstances(Map<String, Applications> remoteRegionsRegistry, EurekaClientConfig clientConfig, InstanceRegionChecker instanceRegionChecker) { shuffleInstances(clientConfig.shouldFilterOnlyUpInstances(), true, remoteRegionsRegistry, clientConfig, instanceRegionChecker); }
/** * Gets the <em>applications</em> after filtering the applications for * instances with only UP states and shuffling them. * * <p> * The filtering depends on the option specified by the configuration * {@link EurekaClientConfig#shouldFilterOnlyUpInstances()}. Shuffling helps * in randomizing the applications list there by avoiding the same instances * receiving traffic during start ups. * </p> * * @param apps * The applications that needs to be filtered and shuffled. * @return The applications after the filter and the shuffle. */ private Applications filterAndShuffle(Applications apps) { if (apps != null) { if (isFetchingRemoteRegionRegistries()) { Map<String, Applications> remoteRegionVsApps = new ConcurrentHashMap<String, Applications>(); apps.shuffleAndIndexInstances(remoteRegionVsApps, clientConfig, instanceRegionChecker); for (Applications applications : remoteRegionVsApps.values()) { applications.shuffleInstances(clientConfig.shouldFilterOnlyUpInstances()); } this.remoteRegionVsApps = remoteRegionVsApps; } else { apps.shuffleInstances(clientConfig.shouldFilterOnlyUpInstances()); } } return apps; }
Applications applications = response.getEntity(); if (applications != null) { applications.shuffleInstances(true); // filter out non-UP instances List<InstanceInfo> validInstanceInfos = applications.getInstancesByVirtualHostName(vipAddress); for (InstanceInfo instanceInfo : validInstanceInfos) {
public Applications toApplications() { Map<String, Application> appsByName = new HashMap<>(); Iterator<InstanceInfo> it = serviceIterator(); while (it.hasNext()) { InstanceInfo instanceInfo = it.next(); Application instanceApp = appsByName.get(instanceInfo.getAppName()); if (instanceApp == null) { instanceApp = new Application(instanceInfo.getAppName()); appsByName.put(instanceInfo.getAppName(), instanceApp); } instanceApp.addInstance(instanceInfo); } // Do not pass application list to the constructor, as it does not initialize properly Applications // data structure. Applications applications = new Applications(); for (Application app : appsByName.values()) { applications.addApplication(app); } applications.shuffleInstances(false); applications.setAppsHashCode(applications.getReconcileHashCode()); applications.setVersion(1L); return applications; }
getApplications().shuffleInstances(clientConfig.shouldFilterOnlyUpInstances()); applications.shuffleInstances(clientConfig.shouldFilterOnlyUpInstances());
/** * Test that instancesMap in Application and shuffleVirtualHostNameMap in * Applications are correctly updated when the last instance is removed from * an application and shuffleInstances has been run. */ @Test public void shuffleVirtualHostNameMapLastInstanceTest() { DataCenterInfo myDCI = new DataCenterInfo() { public DataCenterInfo.Name getName() { return DataCenterInfo.Name.MyOwn; } }; InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder().setAppName("test") .setVIPAddress("test.testname:1").setDataCenterInfo(myDCI).setHostName("test.hostname").build(); Application application = new Application("TestApp"); application.addInstance(instanceInfo); Applications applications = new Applications(); applications.addApplication(application); applications.shuffleInstances(true); List<InstanceInfo> testApp = applications.getInstancesByVirtualHostName("test.testname:1"); assertEquals(Iterables.getOnlyElement(testApp), application.getByInstanceId("test.hostname")); application.removeInstance(instanceInfo); assertEquals(0, applications.size()); applications.shuffleInstances(true); testApp = applications.getInstancesByVirtualHostName("test.testname:1"); assertTrue(testApp.isEmpty()); assertNull(application.getByInstanceId("test.hostname")); }
@Test public void testInstanceFiltering() { DataCenterInfo myDCI = new DataCenterInfo() { public DataCenterInfo.Name getName() { return DataCenterInfo.Name.MyOwn; } }; InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder() .setAppName("test") .setVIPAddress("test.testname:1") .setSecureVIPAddress("securetest.testname:7102") .setDataCenterInfo(myDCI) .setHostName("test.hostname") .setStatus(InstanceStatus.DOWN) .build(); Application application = new Application("TestApp"); application.addInstance(instanceInfo); Applications applications = new Applications(); applications.addApplication(application); applications.shuffleInstances(true); assertNotNull(applications.getRegisteredApplications("TestApp").getByInstanceId("test.hostname")); assertTrue(applications.getInstancesBySecureVirtualHostName("securetest.testname:7102").isEmpty()); assertTrue(applications.getInstancesBySecureVirtualHostName("test.testname:1").isEmpty()); }
assertEquals(1, applications.size()); applications.shuffleInstances(true); List<InstanceInfo> testApp = applications.getInstancesByVirtualHostName("test.testname:1"); assertEquals(0, applications.size()); applications.shuffleInstances(true); testApp = applications.getInstancesBySecureVirtualHostName("securetest.testname:7102"); assertTrue(testApp.isEmpty());
/** * Shuffles the provided instances so that they will not always be returned * in the same order. * * @param filterUpInstances * whether to return only UP instances */ public void shuffleInstances(boolean filterUpInstances) { shuffleInstances(filterUpInstances, false, null, null, null); }
/** * Shuffles a whole region so that the instances will not always be returned * in the same order. * * @param remoteRegionsRegistry * the map of remote region names to their registries * @param clientConfig * the {@link EurekaClientConfig}, whose settings will be used to * determine whether to filter to only UP instances * @param instanceRegionChecker * the instance region checker */ public void shuffleAndIndexInstances(Map<String, Applications> remoteRegionsRegistry, EurekaClientConfig clientConfig, InstanceRegionChecker instanceRegionChecker) { shuffleInstances(clientConfig.shouldFilterOnlyUpInstances(), true, remoteRegionsRegistry, clientConfig, instanceRegionChecker); }
/** * Gets the <em>applications</em> after filtering the applications for * instances with only UP states and shuffling them. * * <p> * The filtering depends on the option specified by the configuration * {@link EurekaClientConfig#shouldFilterOnlyUpInstances()}. Shuffling helps * in randomizing the applications list there by avoiding the same instances * receiving traffic during start ups. * </p> * * @param apps * The applications that needs to be filtered and shuffled. * @return The applications after the filter and the shuffle. */ private Applications filterAndShuffle(Applications apps) { if (apps != null) { if (isFetchingRemoteRegionRegistries()) { Map<String, Applications> remoteRegionVsApps = new ConcurrentHashMap<String, Applications>(); apps.shuffleAndIndexInstances(remoteRegionVsApps, clientConfig, instanceRegionChecker); for (Applications applications : remoteRegionVsApps.values()) { applications.shuffleInstances(clientConfig.shouldFilterOnlyUpInstances()); } this.remoteRegionVsApps = remoteRegionVsApps; } else { apps.shuffleInstances(clientConfig.shouldFilterOnlyUpInstances()); } } return apps; }
Applications applications = response.getEntity(); if (applications != null) { applications.shuffleInstances(true); // filter out non-UP instances List<InstanceInfo> validInstanceInfos = applications.getInstancesByVirtualHostName(vipAddress); for (InstanceInfo instanceInfo : validInstanceInfos) {
public Applications toApplications() { Map<String, Application> appsByName = new HashMap<>(); Iterator<InstanceInfo> it = serviceIterator(); while (it.hasNext()) { InstanceInfo instanceInfo = it.next(); Application instanceApp = appsByName.get(instanceInfo.getAppName()); if (instanceApp == null) { instanceApp = new Application(instanceInfo.getAppName()); appsByName.put(instanceInfo.getAppName(), instanceApp); } instanceApp.addInstance(instanceInfo); } // Do not pass application list to the constructor, as it does not initialize properly Applications // data structure. Applications applications = new Applications(); for (Application app : appsByName.values()) { applications.addApplication(app); } applications.shuffleInstances(false); applications.setAppsHashCode(applications.getReconcileHashCode()); applications.setVersion(1L); return applications; }
getApplications().shuffleInstances(clientConfig.shouldFilterOnlyUpInstances()); applications.shuffleInstances(clientConfig.shouldFilterOnlyUpInstances());