protected void registerInstanceLocallyWithLeaseDurationInSecs(InstanceInfo remoteInstance, int leaseDurationInSecs) { registry.register(remoteInstance, leaseDurationInSecs, false); registeredApps.add(new Pair<String, String>(LOCAL_REGION_APP_NAME, remoteInstance.getId())); }
@Override public void execute() { System.out.println(String.format("isLeaseExpirationEnabled=%s, getNumOfRenewsInLastMin=%d, " + "getNumOfRenewsPerMinThreshold=%s, instanceNumberOfMYLOCALAPP=%d", registry.isLeaseExpirationEnabled(), registry.getNumOfRenewsInLastMin(), registry.getNumOfRenewsPerMinThreshold(), registry.getApplication(LOCAL_REGION_APP_NAME).getInstances().size())); } })
case Heartbeat: InstanceStatus overriddenStatus = overriddenInstanceStatusMap.get(id); infoFromRegistry = getInstanceByAppAndId(appName, id, false); node.heartbeat(appName, id, infoFromRegistry, overriddenStatus, false); break; break; case StatusUpdate: infoFromRegistry = getInstanceByAppAndId(appName, id, false); node.statusUpdate(appName, id, newStatus, infoFromRegistry); break; case DeleteStatusOverride: infoFromRegistry = getInstanceByAppAndId(appName, id, false); node.deleteStatusOverride(appName, id, infoFromRegistry); break;
@Override public boolean isLeaseExpirationEnabled() { if (!isSelfPreservationModeEnabled()) { // The self preservation mode is disabled, hence allowing the instances to expire. return true; } return numberOfRenewsPerMinThreshold > 0 && getNumOfRenewsInLastMin() > numberOfRenewsPerMinThreshold; }
@Override public void execute() { System.out.println("checking on 80s"); Preconditions.checkState(Boolean.TRUE.equals(registry.isLeaseExpirationEnabled()), "Lease expiration should be enabled"); Preconditions.checkState(registry.getNumOfRenewsInLastMin() == 90, "Renewals in last min should be 90"); Preconditions.checkState(registry.getApplication(LOCAL_REGION_APP_NAME).getInstances().size() == 45, "There should be 45 instances in application - MYLOCAPP"); } }),
boolean statusResult = registry.statusUpdate(LOCAL_REGION_APP_NAME, seed.getId(), InstanceStatus.OUT_OF_SERVICE, "0", false); assertThat("Couldn't override instance status", statusResult, is(true)); verifyLocalInstanceStatus(seed.getId(), InstanceStatus.OUT_OF_SERVICE); InstanceInfo registeredInstance = registry.getInstanceByAppAndId(seed.getAppName(), seed.getId()); registeredInstance.setStatusWithoutDirty(InstanceStatus.UP); verifyLocalInstanceStatus(seed.getId(), InstanceStatus.UP); registry.renew(seed.getAppName(), seed.getId(), false); verifyLocalInstanceStatus(seed.getId(), InstanceStatus.OUT_OF_SERVICE); statusResult = registry.deleteStatusOverride(LOCAL_REGION_APP_NAME, seed.getId(), InstanceStatus.DOWN, "0", false); assertThat("Couldn't remove status override", statusResult, is(true)); verifyLocalInstanceStatus(seed.getId(), InstanceStatus.DOWN); registry.register(myInstance3, 10000000, false); verifyLocalInstanceStatus(seed.getId(), InstanceStatus.UP);
for (InstanceInfo instance : app.getInstances()) { try { if (isRegisterable(instance)) { register(instance, instance.getLeaseInfo().getDurationInSecs(), true); count++;
@Test public void testStatusOverrideDeleteIsAppliedToRegistry() throws Exception { // Override instance status registry.register(testInstanceInfo, false); registry.statusUpdate(testInstanceInfo.getAppName(), testInstanceInfo.getId(), InstanceStatus.OUT_OF_SERVICE, "0", false); assertThat(testInstanceInfo.getStatus(), is(equalTo(InstanceStatus.OUT_OF_SERVICE))); // Remove the override Response response = instanceResource.deleteStatusUpdate("false", null, "0"); assertThat(response.getStatus(), is(equalTo(200))); assertThat(testInstanceInfo.getStatus(), is(equalTo(InstanceStatus.UNKNOWN))); }
@RequestMapping(value = "/lastn", method = RequestMethod.GET) public String lastn(HttpServletRequest request, Map<String, Object> model) { populateBase(request, model); PeerAwareInstanceRegistryImpl registry = (PeerAwareInstanceRegistryImpl) getRegistry(); ArrayList<Map<String, Object>> lastNCanceled = new ArrayList<>(); List<Pair<Long, String>> list = registry.getLastNCanceledInstances(); for (Pair<Long, String> entry : list) { lastNCanceled.add(registeredInstance(entry.second(), entry.first())); } model.put("lastNCanceled", lastNCanceled); list = registry.getLastNRegisteredInstances(); ArrayList<Map<String, Object>> lastNRegistered = new ArrayList<>(); for (Pair<Long, String> entry : list) { lastNRegistered.add(registeredInstance(entry.second(), entry.first())); } model.put("lastNRegistered", lastNRegistered); return "eureka/lastn"; }
@Test public void testStatusOverrideSetAndRemoval() throws Exception { InstanceInfo seed = createLocalInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME); seed.setLastDirtyTimestamp(100l); // Regular registration first InstanceInfo myInstance1 = new InstanceInfo(seed); registerInstanceLocally(myInstance1); verifyLocalInstanceStatus(myInstance1.getId(), InstanceStatus.UP); // Override status boolean statusResult = registry.statusUpdate(LOCAL_REGION_APP_NAME, seed.getId(), InstanceStatus.OUT_OF_SERVICE, "0", false); assertThat("Couldn't override instance status", statusResult, is(true)); verifyLocalInstanceStatus(seed.getId(), InstanceStatus.OUT_OF_SERVICE); // Register again with status UP to verify that the override is still in place even if the dirtytimestamp is higher InstanceInfo myInstance2 = new InstanceInfo(seed); // clone to avoid object state in this test myInstance2.setLastDirtyTimestamp(200l); // use a later 'client side' dirty timestamp registry.register(myInstance2, 10000000, false); verifyLocalInstanceStatus(seed.getId(), InstanceStatus.OUT_OF_SERVICE); // Now remove override statusResult = registry.deleteStatusOverride(LOCAL_REGION_APP_NAME, seed.getId(), InstanceStatus.DOWN, "0", false); assertThat("Couldn't remove status override", statusResult, is(true)); verifyLocalInstanceStatus(seed.getId(), InstanceStatus.DOWN); // Register again with status UP after the override deletion, keeping myInstance2's dirtyTimestamp (== no client side change) InstanceInfo myInstance3 = new InstanceInfo(seed); // clone to avoid object state in this test myInstance3.setLastDirtyTimestamp(200l); // use a later 'client side' dirty timestamp registry.register(myInstance3, 10000000, false); verifyLocalInstanceStatus(seed.getId(), InstanceStatus.UP); }
public boolean shouldAllowAccess() { return shouldAllowAccess(true); }
public MockServer(String appName, PeerEurekaNodes peerEurekaNodes) throws Exception { ApplicationInfoManager infoManager = new ApplicationInfoManager(new MyDataCenterInstanceConfig()); DefaultEurekaServerConfig serverConfig = Mockito.spy(new DefaultEurekaServerConfig()); DefaultEurekaClientConfig clientConfig = new DefaultEurekaClientConfig(); ServerCodecs serverCodecs = new DefaultServerCodecs(serverConfig); EurekaClient eurekaClient = Mockito.mock(EurekaClient.class); Mockito.doReturn("true").when(serverConfig).getExperimental("registry.registration.ignoreIfDirtyTimestampIsOlder"); this.registry = new PeerAwareInstanceRegistryImpl(serverConfig, clientConfig, serverCodecs, eurekaClient); this.registry.init(peerEurekaNodes); this.applicationResource = new ApplicationResource(appName, serverConfig, registry); EurekaServerContext serverContext = Mockito.mock(EurekaServerContext.class); Mockito.when(serverContext.getServerConfig()).thenReturn(serverConfig); Mockito.when(serverContext.getRegistry()).thenReturn(registry); this.replicationResource = new PeerReplicationResource(serverContext); } }
@Override public void execute() { System.out.println("checking on 120s"); System.out.println("getNumOfRenewsPerMinThreshold=" + registry.getNumOfRenewsPerMinThreshold()); Preconditions.checkState(registry.getNumOfRenewsPerMinThreshold() == 256, "NumOfRenewsPerMinThreshold should be updated to 256"); Preconditions.checkState(registry.getApplication(LOCAL_REGION_APP_NAME).getInstances().size() == 45, "There should be 45 instances in application - MYLOCAPP"); } })
protected void verifyLocalInstanceStatus(String id, InstanceInfo.InstanceStatus status) { InstanceInfo instanceInfo = registry.getApplication(LOCAL_REGION_APP_NAME).getByInstanceId(id); assertThat("InstanceInfo with id " + id + " not found", instanceInfo, is(notNullValue())); assertThat("Invalid InstanceInfo state", instanceInfo.getStatus(), is(equalTo(status))); }
/** * Checks if the number of renewals is lesser than threshold. * * @return 0 if the renewals are greater than threshold, 1 otherwise. */ @com.netflix.servo.annotations.Monitor(name = "isBelowRenewThreshold", description = "0 = false, 1 = true", type = com.netflix.servo.annotations.DataSourceType.GAUGE) @Override public int isBelowRenewThresold() { if ((getNumOfRenewsInLastMin() <= numberOfRenewsPerMinThreshold) && ((this.startupTime > 0) && (System.currentTimeMillis() > this.startupTime + (serverConfig.getWaitTimeInMsWhenSyncEmpty())))) { return 1; } else { return 0; } }
@After public void tearDown() throws Exception { for (Pair<String, String> registeredApp : registeredApps) { System.out.println("Canceling application: " + registeredApp.first() + " from local registry."); registry.cancel(registeredApp.first(), registeredApp.second(), false); } serverContext.shutdown(); mockRemoteEurekaServer.stop(); remoteRegionApps.clear(); remoteRegionAppsDelta.clear(); ConfigurationManager.getConfigInstance().clearProperty("eureka.remoteRegionUrls"); ConfigurationManager.getConfigInstance().clearProperty("eureka.deltaRetentionTimerIntervalInMs"); }
/** * Gets the list of all {@link Applications} from the registry in sorted * lexical order of {@link Application#getName()}. * * @return the list of {@link Applications} in lexical order. */ @Override public List<Application> getSortedApplications() { List<Application> apps = new ArrayList<Application>(getApplications().getRegisteredApplications()); Collections.sort(apps, APP_COMPARATOR); return apps; }
@Override public void execute() { System.out.println("checking on 40s"); Preconditions.checkState(Boolean.FALSE.equals(registry.isLeaseExpirationEnabled()), "Lease expiration should be disabled"); Preconditions.checkState(registry.getNumOfRenewsInLastMin() == 0, "Renewals in last min should be 0"); Preconditions.checkState(registry.getApplication(LOCAL_REGION_APP_NAME).getInstances().size() == 50, "There should be 50 instances in application - MYLOCAPP"); } }),
for (InstanceInfo instance : app.getInstances()) { try { if (isRegisterable(instance)) { register(instance, instance.getLeaseInfo().getDurationInSecs(), true); count++;
@Test public void testStatusOverrideDeleteIsAppliedToRegistryAndProvidedStatusIsSet() throws Exception { // Override instance status registry.register(testInstanceInfo, false); registry.statusUpdate(testInstanceInfo.getAppName(), testInstanceInfo.getId(), InstanceStatus.OUT_OF_SERVICE, "0", false); assertThat(testInstanceInfo.getStatus(), is(equalTo(InstanceStatus.OUT_OF_SERVICE))); // Remove the override Response response = instanceResource.deleteStatusUpdate("false", "DOWN", "0"); assertThat(response.getStatus(), is(equalTo(200))); assertThat(testInstanceInfo.getStatus(), is(equalTo(InstanceStatus.DOWN))); } }