Search in sources :

Example 1 with CacheRefreshedEvent

use of com.netflix.discovery.CacheRefreshedEvent in project eureka by Netflix.

the class DiscoveryClientResource method awaitCacheUpdate.

public boolean awaitCacheUpdate(long timeout, TimeUnit unit) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    Object eventListener = new Object() {

        @Subscribe
        public void consume(CacheRefreshedEvent event) {
            latch.countDown();
        }
    };
    try {
        getEventBus().registerSubscriber(eventListener);
    } catch (InvalidSubscriberException e) {
        throw new IllegalStateException("Unexpected error during subscriber registration", e);
    }
    try {
        return latch.await(timeout, unit);
    } finally {
        getEventBus().unregisterSubscriber(eventListener);
    }
}
Also used : InvalidSubscriberException(com.netflix.eventbus.spi.InvalidSubscriberException) CountDownLatch(java.util.concurrent.CountDownLatch) CacheRefreshedEvent(com.netflix.discovery.CacheRefreshedEvent)

Example 2 with CacheRefreshedEvent

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");
    }
}
Also used : EurekaEvent(com.netflix.discovery.EurekaEvent) EurekaEventListener(com.netflix.discovery.EurekaEventListener) CacheRefreshedEvent(com.netflix.discovery.CacheRefreshedEvent)

Example 3 with CacheRefreshedEvent

use of com.netflix.discovery.CacheRefreshedEvent in project ribbon by Netflix.

the class EurekaNotificationServerListUpdaterTest method testUpdating.

@Test
public void testUpdating() 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 AtomicBoolean firstTime = new AtomicBoolean(false);
        final CountDownLatch firstLatch = new CountDownLatch(1);
        final CountDownLatch secondLatch = new CountDownLatch(1);
        serverListUpdater.start(new ServerListUpdater.UpdateAction() {

            @Override
            public void doUpdate() {
                if (firstTime.compareAndSet(false, true)) {
                    firstLatch.countDown();
                } else {
                    secondLatch.countDown();
                }
            }
        });
        eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
        Assert.assertTrue(firstLatch.await(2, TimeUnit.SECONDS));
        // wait a bit for the updateQueued flag to be reset
        for (int i = 1; i < 10; i++) {
            if (serverListUpdater.updateQueued.get()) {
                Thread.sleep(i * 100);
            } else {
                break;
            }
        }
        eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
        Assert.assertTrue(secondLatch.await(2, TimeUnit.SECONDS));
    } finally {
        serverListUpdater.stop();
        EasyMock.verify(eurekaClientMock);
    }
}
Also used : EurekaClient(com.netflix.discovery.EurekaClient) EurekaEventListener(com.netflix.discovery.EurekaEventListener) CountDownLatch(java.util.concurrent.CountDownLatch) Capture(org.easymock.Capture) ServerListUpdater(com.netflix.loadbalancer.ServerListUpdater) CacheRefreshedEvent(com.netflix.discovery.CacheRefreshedEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 4 with CacheRefreshedEvent

use of com.netflix.discovery.CacheRefreshedEvent in project ribbon by Netflix.

the class EurekaNotificationServerListUpdaterTest method testSubmitExceptionClearQueued.

@Test
public void testSubmitExceptionClearQueued() {
    ThreadPoolExecutor executorMock = EasyMock.createMock(ThreadPoolExecutor.class);
    EasyMock.expect(executorMock.submit(EasyMock.isA(Runnable.class))).andThrow(new RejectedExecutionException("test exception"));
    EasyMock.expect(executorMock.isShutdown()).andReturn(Boolean.FALSE);
    EurekaNotificationServerListUpdater serverListUpdater = new EurekaNotificationServerListUpdater(new Provider<EurekaClient>() {

        @Override
        public EurekaClient get() {
            return eurekaClientMock;
        }
    }, executorMock);
    try {
        Capture<EurekaEventListener> eventListenerCapture = new Capture<EurekaEventListener>();
        eurekaClientMock.registerEventListener(EasyMock.capture(eventListenerCapture));
        EasyMock.replay(eurekaClientMock);
        EasyMock.replay(executorMock);
        serverListUpdater.start(new ServerListUpdater.UpdateAction() {

            @Override
            public void doUpdate() {
                Assert.fail("should not reach here");
            }
        });
        eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
        Assert.assertFalse(serverListUpdater.updateQueued.get());
    } finally {
        serverListUpdater.stop();
        EasyMock.verify(executorMock);
        EasyMock.verify(eurekaClientMock);
    }
}
Also used : EurekaClient(com.netflix.discovery.EurekaClient) EurekaEventListener(com.netflix.discovery.EurekaEventListener) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Capture(org.easymock.Capture) ServerListUpdater(com.netflix.loadbalancer.ServerListUpdater) CacheRefreshedEvent(com.netflix.discovery.CacheRefreshedEvent) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Example 5 with CacheRefreshedEvent

use of com.netflix.discovery.CacheRefreshedEvent in project ribbon by Netflix.

the class EurekaNotificationServerListUpdaterTest method testStopWithCommonExecutor.

@Test
public void testStopWithCommonExecutor() throws Exception {
    EurekaNotificationServerListUpdater serverListUpdater1 = new EurekaNotificationServerListUpdater(new Provider<EurekaClient>() {

        @Override
        public EurekaClient get() {
            return eurekaClientMock;
        }
    }, testExecutor);
    EurekaNotificationServerListUpdater serverListUpdater2 = new EurekaNotificationServerListUpdater(new Provider<EurekaClient>() {

        @Override
        public EurekaClient get() {
            return eurekaClientMock2;
        }
    }, testExecutor);
    Capture<EurekaEventListener> eventListenerCapture = new Capture<EurekaEventListener>();
    eurekaClientMock.registerEventListener(EasyMock.capture(eventListenerCapture));
    Capture<EurekaEventListener> eventListenerCapture2 = new Capture<EurekaEventListener>();
    eurekaClientMock2.registerEventListener(EasyMock.capture(eventListenerCapture2));
    EasyMock.replay(eurekaClientMock);
    EasyMock.replay(eurekaClientMock2);
    final CountDownLatch updateCountLatch = new CountDownLatch(2);
    serverListUpdater1.start(new ServerListUpdater.UpdateAction() {

        @Override
        public void doUpdate() {
            updateCountLatch.countDown();
        }
    });
    serverListUpdater2.start(new ServerListUpdater.UpdateAction() {

        @Override
        public void doUpdate() {
            updateCountLatch.countDown();
        }
    });
    eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
    eventListenerCapture2.getValue().onEvent(new CacheRefreshedEvent());
    // latch is for both
    Assert.assertTrue(updateCountLatch.await(2, TimeUnit.SECONDS));
    serverListUpdater1.stop();
    serverListUpdater2.stop();
    EasyMock.verify(eurekaClientMock);
    EasyMock.verify(eurekaClientMock2);
}
Also used : EurekaClient(com.netflix.discovery.EurekaClient) EurekaEventListener(com.netflix.discovery.EurekaEventListener) CountDownLatch(java.util.concurrent.CountDownLatch) Capture(org.easymock.Capture) ServerListUpdater(com.netflix.loadbalancer.ServerListUpdater) CacheRefreshedEvent(com.netflix.discovery.CacheRefreshedEvent) Test(org.junit.Test)

Aggregations

CacheRefreshedEvent (com.netflix.discovery.CacheRefreshedEvent)8 EurekaEventListener (com.netflix.discovery.EurekaEventListener)7 Capture (org.easymock.Capture)6 Test (org.junit.Test)6 EurekaClient (com.netflix.discovery.EurekaClient)5 ServerListUpdater (com.netflix.loadbalancer.ServerListUpdater)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 EurekaEvent (com.netflix.discovery.EurekaEvent)1 InvalidSubscriberException (com.netflix.eventbus.spi.InvalidSubscriberException)1 DiscoveryEnabledNIWSServerList (com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList)1 DiscoveryEnabledServer (com.netflix.niws.loadbalancer.DiscoveryEnabledServer)1 EurekaNotificationServerListUpdater (com.netflix.niws.loadbalancer.EurekaNotificationServerListUpdater)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1