com.netflix.loadbalancer.ServerListUpdater Java Examples

The following examples show how to use com.netflix.loadbalancer.ServerListUpdater. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EurekaNotificationServerListUpdaterTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testFailIfDiscoveryIsNotAvailable() {
    EurekaNotificationServerListUpdater serverListUpdater = new EurekaNotificationServerListUpdater(
            new Provider<EurekaClient>() {
                @Override
                public EurekaClient get() {
                    return null;
                }
            },
            testExecutor
    );

    serverListUpdater.start(new ServerListUpdater.UpdateAction() {
        @Override
        public void doUpdate() {
            Assert.fail("Should not reach here");
        }
    });
}
 
Example #2
Source File: LBBuilderTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildWithDiscoveryEnabledNIWSServerListAndUpdater() {
    IRule rule = new AvailabilityFilteringRule();
    ServerList<DiscoveryEnabledServer> list = new DiscoveryEnabledNIWSServerList("dummy:7001");
    ServerListFilter<DiscoveryEnabledServer> filter = new ZoneAffinityServerListFilter<>();
    ServerListUpdater updater = new PollingServerListUpdater();
    ZoneAwareLoadBalancer<DiscoveryEnabledServer> lb = LoadBalancerBuilder.<DiscoveryEnabledServer>newBuilder()
            .withDynamicServerList(list)
            .withRule(rule)
            .withServerListFilter(filter)
            .withServerListUpdater(updater)
            .buildDynamicServerListLoadBalancerWithUpdater();
    assertNotNull(lb);
    assertEquals(Lists.newArrayList(expected), lb.getAllServers());
    assertSame(filter, lb.getFilter());
    assertSame(list, lb.getServerListImpl());
    assertSame(updater, lb.getServerListUpdater());
    Server server = lb.chooseServer();
    // make sure load balancer does not recreate the server instance
    assertTrue(server instanceof DiscoveryEnabledServer);
}
 
Example #3
Source File: RuleBaseConfig.java    From spring-cloud-ribbon-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * The load balancer definition.
 *
 * @param config            the client config.
 * @param serverList        the server list.
 * @param serverListFilter  the server list filter.
 * @param rule              the load balancing rule.
 * @param ping              the ping strategy.
 * @param serverListUpdater the server list updater.
 * @return The Dynamic Server List Load Balancer.
 */
@Bean
@ConditionalOnMissingBean
public ILoadBalancer loadBalancer(IClientConfig config,
                                  ServerList<Server> serverList,
                                  ServerListFilter<Server> serverListFilter,
                                  IRule rule, IPing ping,
                                  ServerListUpdater serverListUpdater) {
    log.debug("dynamic server list load balancer enabled.");
    return new DynamicServerListLoadBalancer<>(config, rule, ping, serverList,
            serverListFilter, serverListUpdater);
}
 
Example #4
Source File: EurekaNotificationServerListUpdaterTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testEurekaClientUnregister() {
    ThreadPoolExecutor executorMock = EasyMock.createMock(ThreadPoolExecutor.class);
    EasyMock.expect(executorMock.isShutdown()).andReturn(Boolean.TRUE);

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

    try {
        Capture<EurekaEventListener> registeredListener = new Capture<EurekaEventListener>();
        eurekaClientMock.registerEventListener(EasyMock.capture(registeredListener));

        EasyMock.replay(eurekaClientMock);
        EasyMock.replay(executorMock);

        serverListUpdater.start(new ServerListUpdater.UpdateAction() {
            @Override
            public void doUpdate() {
                Assert.fail("should not reach here");
            }
        });

        registeredListener.getValue().onEvent(new CacheRefreshedEvent());
        
    } finally {
        EasyMock.verify(executorMock);
        EasyMock.verify(eurekaClientMock);
    }

}
 
Example #5
Source File: ServiceCombRibbonClientConfiguration.java    From spring-cloud-huawei with Apache License 2.0 4 votes vote down vote up
@Bean
public ServerListUpdater serviceCombServerListUpdater(ServiceCombEventBus eventBus) {
  return new ServiceCombServerListUpdater(eventBus);
}
 
Example #6
Source File: CustomIloadBalancer.java    From spring-cloud-formula with Apache License 2.0 4 votes vote down vote up
public CustomIloadBalancer(IClientConfig config, IRule rule, IPing ping, ServerList serverList,
                           ServerListFilter serverListFilter, ServerListUpdater serverListUpdater) {
    super(config, rule, ping, serverList,
            serverListFilter, serverListUpdater);
}
 
Example #7
Source File: EurekaNotificationServerListUpdaterTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@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);
    }
}
 
Example #8
Source File: EurekaNotificationServerListUpdaterTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@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());

    Assert.assertTrue(updateCountLatch.await(2, TimeUnit.SECONDS));  // latch is for both

    serverListUpdater1.stop();
    serverListUpdater2.stop();

    EasyMock.verify(eurekaClientMock);
    EasyMock.verify(eurekaClientMock2);
}
 
Example #9
Source File: EurekaNotificationServerListUpdaterTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@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));
        Thread.sleep(100);  // sleep a bit more

        Assert.assertFalse(serverListUpdater.updateQueued.get());
    } finally {
        serverListUpdater.stop();

        EasyMock.verify(eurekaClientMock);
    }
}
 
Example #10
Source File: EurekaNotificationServerListUpdaterTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@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);
    }

}