use of com.netflix.discovery.CacheRefreshedEvent in project ribbon by Netflix.
the class EurekaNotificationServerListUpdater method start.
@Override
public synchronized void start(final UpdateAction updateAction) {
if (isActive.compareAndSet(false, true)) {
this.updateListener = new EurekaEventListener() {
@Override
public void onEvent(EurekaEvent event) {
if (event instanceof CacheRefreshedEvent) {
if (!updateQueued.compareAndSet(false, true)) {
// if an update is already queued
logger.info("an update action is already queued, returning as no-op");
return;
}
if (!refreshExecutor.isShutdown()) {
try {
refreshExecutor.submit(new Runnable() {
@Override
public void run() {
try {
updateAction.doUpdate();
lastUpdated.set(System.currentTimeMillis());
} catch (Exception e) {
logger.warn("Failed to update serverList", e);
} finally {
updateQueued.set(false);
}
}
});
// fire and forget
} catch (Exception e) {
logger.warn("Error submitting update task to executor, skipping one round of updates", e);
// if submit fails, need to reset updateQueued to false
updateQueued.set(false);
}
} else {
logger.debug("stopping EurekaNotificationServerListUpdater, as refreshExecutor has been shut down");
stop();
}
}
}
};
if (eurekaClient == null) {
eurekaClient = eurekaClientProvider.get();
}
if (eurekaClient != null) {
eurekaClient.registerEventListener(updateListener);
} else {
logger.error("Failed to register an updateListener to eureka client, eureka client is null");
throw new IllegalStateException("Failed to start the updater, unable to register the update listener due to eureka client being null.");
}
} else {
logger.info("Update listener already registered, no-op");
}
}
use of com.netflix.discovery.CacheRefreshedEvent in project ribbon by Netflix.
the class EurekaDynamicServerListLoadBalancerTest method testLoadBalancerHappyCase.
@Test
public void testLoadBalancerHappyCase() throws Exception {
Assert.assertNotEquals("the two test server list counts should be different", secondServerListSize, initialServerListSize);
DynamicServerListLoadBalancer<DiscoveryEnabledServer> lb = null;
try {
Capture<EurekaEventListener> eventListenerCapture = new Capture<EurekaEventListener>();
eurekaClientMock.registerEventListener(EasyMock.capture(eventListenerCapture));
PowerMock.replay(DiscoveryClient.class);
PowerMock.replay(eurekaClientMock);
// actual testing
// initial creation and loading of the first serverlist
lb = new DynamicServerListLoadBalancer<DiscoveryEnabledServer>(config, new AvailabilityFilteringRule(), new DummyPing(), new DiscoveryEnabledNIWSServerList(vipAddress, eurekaClientProvider), new ZoneAffinityServerListFilter<DiscoveryEnabledServer>(), new EurekaNotificationServerListUpdater(eurekaClientProvider));
Assert.assertEquals(initialServerListSize, lb.getServerCount(false));
// trigger an eureka CacheRefreshEvent
eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
Assert.assertTrue(verifyFinalServerListCount(secondServerListSize, lb));
} finally {
if (lb != null) {
lb.shutdown();
PowerMock.verify(eurekaClientMock);
PowerMock.verify(DiscoveryClient.class);
}
}
}
use of com.netflix.discovery.CacheRefreshedEvent in project ribbon by Netflix.
the class EurekaNotificationServerListUpdaterTest method testTaskAlreadyQueued.
@Test
public void testTaskAlreadyQueued() throws Exception {
EurekaNotificationServerListUpdater serverListUpdater = new EurekaNotificationServerListUpdater(new Provider<EurekaClient>() {
@Override
public EurekaClient get() {
return eurekaClientMock;
}
}, testExecutor);
try {
Capture<EurekaEventListener> eventListenerCapture = new Capture<EurekaEventListener>();
eurekaClientMock.registerEventListener(EasyMock.capture(eventListenerCapture));
EasyMock.replay(eurekaClientMock);
final CountDownLatch countDownLatch = new CountDownLatch(1);
serverListUpdater.start(new ServerListUpdater.UpdateAction() {
@Override
public void doUpdate() {
if (countDownLatch.getCount() == 0) {
Assert.fail("should only countdown once");
}
countDownLatch.countDown();
}
});
eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
Assert.assertTrue(countDownLatch.await(2, TimeUnit.SECONDS));
// sleep a bit more
Thread.sleep(100);
Assert.assertFalse(serverListUpdater.updateQueued.get());
} finally {
serverListUpdater.stop();
EasyMock.verify(eurekaClientMock);
}
}
Aggregations