com.netflix.loadbalancer.AvailabilityFilteringRule Java Examples

The following examples show how to use com.netflix.loadbalancer.AvailabilityFilteringRule. 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: LBBuilderTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildWithDiscoveryEnabledNIWSServerList() {
    IRule rule = new AvailabilityFilteringRule();
    ServerList<DiscoveryEnabledServer> list = new DiscoveryEnabledNIWSServerList("dummy:7001");
    ServerListFilter<DiscoveryEnabledServer> filter = new ZoneAffinityServerListFilter<>();
    ZoneAwareLoadBalancer<DiscoveryEnabledServer> lb = LoadBalancerBuilder.<DiscoveryEnabledServer>newBuilder()
            .withDynamicServerList(list)
            .withRule(rule)
            .withServerListFilter(filter)
            .buildDynamicServerListLoadBalancer();
    assertNotNull(lb);
    assertEquals(Lists.newArrayList(expected), lb.getAllServers());
    assertSame(filter, lb.getFilter());
    assertSame(list, lb.getServerListImpl());
    Server server = lb.chooseServer();
    // make sure load balancer does not recreate the server instance
    assertTrue(server instanceof DiscoveryEnabledServer);
}
 
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: RetryTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Before
public void beforeTest() {
    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon.NFLoadBalancerClassName", BaseLoadBalancer.class.getName());
    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon.client.NFLoadBalancerPingClassName", DummyPing.class.getName());

    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon.ReadTimeout", "1000");
    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon." + CommonClientConfigKey.ConnectTimeout, "500");
    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon." + CommonClientConfigKey.OkToRetryOnAllOperations, "true");

    client = (RestClient) ClientFactory.getNamedClient("RetryTest");

    lb = (BaseLoadBalancer) client.getLoadBalancer();
    lb.setServersList(Lists.newArrayList(localServer));
    
    httpClient = NFHttpClientFactory.getNamedNFHttpClient("RetryTest");
    connectionPoolManager = (MonitoredConnectionManager) httpClient.getConnectionManager(); 
    
    client.setMaxAutoRetries(0);
    client.setMaxAutoRetriesNextServer(0);
    client.setOkToRetryOnAllOperations(false);
    lb.setServersList(Lists.newArrayList(localServer));
    // reset the server index
    lb.setRule(new AvailabilityFilteringRule());
    lb.getLoadBalancerStats().getSingleServerStat(localServer).clearSuccessiveConnectionFailureCount();
}
 
Example #4
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreamWithLoadBalancer() throws Exception {
    // NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
    // IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
    IClientConfig config = IClientConfig.Builder.newBuilder().withRetryOnAllOperations(true)
            .withMaxAutoRetries(1)
            .withMaxAutoRetriesNextServer(3)
            .build();
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ServerSentEvent> lbObservables = (LoadBalancingHttpClient<ByteBuf, ServerSentEvent>) RibbonTransport.newSSEClient(lb, config);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/personStream");
    List<Person> result = Lists.newArrayList();
    Server goodServer = new Server("localhost:" + port);
    Server badServer = new Server("localhost:12245");
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    lb.setServersList(servers);
    result = getPersonListFromResponse(lbObservables.submit(request, null, null));
    assertEquals(EmbeddedResources.entityStream, result);
}
 
Example #5
Source File: ListenerTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testSuccessExecutionOnAbosoluteURI() throws IOException {
    MockWebServer server = new MockWebServer();
    String content = "OK";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json")
            .setBody(content));
    server.play();

    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "2000")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://localhost:" + server.getPort() + "/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + server.getPort());
    List<Server> servers = Lists.newArrayList(goodServer, badServer);

    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.ConnectTimeout, 500);
    TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig);
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    HttpClientResponse<ByteBuf> response = client.submit(request, null, overrideConfig).toBlocking().last();
    assertEquals(200, response.getStatus().code());
    assertEquals(1, listener.executionStartCounter.get());
    assertEquals(1, listener.startWithServerCounter.get());
    assertEquals(0, listener.exceptionWithServerCounter.get());
    assertEquals(0, listener.executionFailedCounter.get());
    assertEquals(1, listener.executionSuccessCounter.get());
    assertEquals(500, listener.getContext().getClientProperty(CommonClientConfigKey.ConnectTimeout).intValue());
    assertTrue(listener.isContextChecked());
    assertTrue(listener.isCheckExecutionInfo());
    assertSame(response, listener.getResponse());
}
 
Example #6
Source File: LBBuilderTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildStaticServerListLoadBalancer() {
    List<Server> list = Lists.newArrayList(expected, expected);
    IRule rule = new AvailabilityFilteringRule();
    IClientConfig clientConfig = IClientConfig.Builder.newBuilder()
            .withDefaultValues()
            .withMaxAutoRetriesNextServer(3).build();

    assertEquals(3, clientConfig.get(Keys.MaxAutoRetriesNextServer).intValue());
    BaseLoadBalancer lb = LoadBalancerBuilder.newBuilder()
            .withRule(rule)
            .buildFixedServerListLoadBalancer(list);
    assertEquals(list, lb.getAllServers());
    assertSame(rule, lb.getRule());
}
 
Example #7
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableWithMultipleServers() throws Exception {
    IClientConfig config = DefaultClientConfigImpl
            .getClientConfigWithDefaultValues()
            .withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config,
            new NettyHttpLoadBalancerErrorHandler(1, 3, true));
    Person person = getPersonObservable(lbObservables.submit(request)).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    ServerStats stats = lbObservables.getServerStats(badServer);
    // two requests to bad server because retry same server is set to 1
    assertEquals(4, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(4, stats.getSuccessiveConnectionFailureCount());
    
    stats = lbObservables.getServerStats(goodServer);
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());

    person = getPersonObservable(lbObservables.submit(request)).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    HttpClientListener listener = lbObservables.getListener();
    assertEquals(1, listener.getPoolReuse());
}
 
Example #8
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableWithMultipleServersWithOverrideRxConfig() throws Exception {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config,
            new NettyHttpLoadBalancerErrorHandler(1, 3, true));
    HttpClientConfig rxconfig = HttpClientConfig.Builder.newDefaultConfig();
    Person person = getPersonObservable(lbObservables.submit(request, rxconfig)).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    ServerStats stats = lbObservables.getServerStats(badServer);
    // two requests to bad server because retry same server is set to 1
    assertEquals(4, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(4, stats.getSuccessiveConnectionFailureCount());
    
    stats = lbObservables.getServerStats(goodServer);
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
    
    final HttpClientListener listener = lbObservables.getListener();
    assertEquals(1, listener.getConnectionCount());
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 1;
        }
    });
}
 
Example #9
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableWithRetrySameServer() throws Exception {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, goodServer);
    
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config,
            new NettyHttpLoadBalancerErrorHandler(1, 0, true));

    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    assertNull(observer.obj);
    assertTrue(observer.error instanceof ClientException);

    ServerStats stats = lbObservables.getServerStats(badServer);
    
    // two requests to bad server because retry same server is set to 1
    assertEquals(2, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    
    stats = lbObservables.getServerStats(goodServer);
    assertEquals(0, stats.getTotalRequestsCount());
}
 
Example #10
Source File: ListenerTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailedExecutionForAbsoluteURI() {
    IClientConfig config = DefaultClientConfigImpl
            .getClientConfigWithDefaultValues()
            .withProperty(CommonClientConfigKey.ConnectTimeout, "100")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://xyz.unknowhost.xyz/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server badServer2 = new Server("localhost:34567");
    List<Server> servers = Lists.newArrayList(badServer, badServer2);

    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
    TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig);
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    try {
        client.submit(request, null, overrideConfig).toBlocking().last();
        fail("Exception expected");
    } catch(Exception e) {
        assertNotNull(e);
    }
    assertEquals(1, listener.executionStartCounter.get());
    assertEquals(2, listener.startWithServerCounter.get());
    assertEquals(2, listener.exceptionWithServerCounter.get());
    assertEquals(1, listener.executionFailedCounter.get());
    assertTrue(listener.isContextChecked());
    assertTrue(listener.isCheckExecutionInfo());
    assertTrue(listener.getFinalThrowable() instanceof ClientException);
}
 
Example #11
Source File: ListenerTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailedExecution() {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues()
            .withProperty(CommonClientConfigKey.ConnectTimeout, "100")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);

    System.out.println(config);

    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer  = new Server("localhost:12345");
    Server badServer2 = new Server("localhost:34567");
    List<Server> servers = Lists.newArrayList(badServer, badServer2);

    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
    TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig);
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    try {
        client.submit(request, null, overrideConfig).toBlocking().last();
        fail("Exception expected");
    } catch(Exception e) {
        assertNotNull(e);
    }
    assertEquals(1, listener.executionStartCounter.get());
    assertEquals(4, listener.startWithServerCounter.get());
    assertEquals(4, listener.exceptionWithServerCounter.get());
    assertEquals(1, listener.executionFailedCounter.get());
    assertTrue(listener.isContextChecked());
    assertTrue(listener.isCheckExecutionInfo());
    assertNotNull(listener.getFinalThrowable());
    listener.getFinalThrowable().printStackTrace();
    assertTrue(listener.getFinalThrowable() instanceof ClientException);
    assertEquals(100, listener.getContext().getClientProperty(CommonClientConfigKey.ConnectTimeout).intValue());
}
 
Example #12
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableWithMultipleServersFailed() throws Exception {        
    IClientConfig config = IClientConfig.Builder.newBuilder()
            .withDefaultValues()
            .withRetryOnAllOperations(true)
            .withMaxAutoRetries(1)
            .withMaxAutoRetriesNextServer(3)
            .withConnectTimeout(100)
            .build();
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());        
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config);
    Server badServer = new Server("localhost:12345");
    Server badServer1 = new Server("localhost:12346");
    Server badServer2 = new Server("localhost:12347");

    List<Server> servers = Lists.newArrayList(badServer, badServer1, badServer2);
    lb.setServersList(servers);
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    assertNull(observer.obj);
    observer.error.printStackTrace();
    assertTrue(observer.error instanceof ClientException);
    
    ServerStats stats = lbObservables.getServerStats(badServer);
    // two requests to bad server because retry same server is set to 1
    assertEquals(2, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(2, stats.getSuccessiveConnectionFailureCount());
}
 
Example #13
Source File: SayHelloConfiguration.java    From spring-boot-inside with MIT License 4 votes vote down vote up
@Bean
public IRule ribbonRule(IClientConfig config) {
  return new AvailabilityFilteringRule();
}
 
Example #14
Source File: ListenerTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testAbortedExecutionOnServer() {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "100")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server badServer2 = new Server("localhost:34567");
    List<Server> servers = Lists.newArrayList(badServer, badServer2);
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
    TestExecutionListener listener = new TestExecutionListener(request, overrideConfig) {
        @Override
        public void onStartWithServer(ExecutionContext context, ExecutionInfo info) {
            throw new AbortExecutionException("exit now");
        }
    };
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> ref = new AtomicReference<Throwable>();
    client.submit(request, null, overrideConfig).subscribe(new Action1<HttpClientResponse<ByteBuf>>() {
        @Override
        public void call(HttpClientResponse<ByteBuf> byteBufHttpClientResponse) {
        }
    }, new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {
            ref.set(throwable);
            latch.countDown();
        }
    });
    try {
        latch.await(500, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertTrue(ref.get() instanceof AbortExecutionException);
}
 
Example #15
Source File: ListenerTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testAbortedExecution() {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "100")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server badServer2 = new Server("localhost:34567");
    List<Server> servers = Lists.newArrayList(badServer, badServer2);
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
    TestExecutionListener listener = new TestExecutionListener(request, overrideConfig) {
        @Override
        public void onExecutionStart(ExecutionContext context) {
            throw new AbortExecutionException("exit now");
        }
    };
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> ref = new AtomicReference<Throwable>();
    client.submit(request, null, overrideConfig).subscribe(new Action1<HttpClientResponse<ByteBuf>>() {
        @Override
        public void call(HttpClientResponse<ByteBuf> byteBufHttpClientResponse) {
        }
    }, new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {
            ref.set(throwable);
            latch.countDown();
        }
    });
    try {
        latch.await(500, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertTrue(ref.get() instanceof AbortExecutionException);
}
 
Example #16
Source File: ListenerTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testSuccessExecution() throws IOException {
    MockWebServer server = new MockWebServer();
    String content = "OK";
    server.enqueue(new MockResponse()
            .setResponseCode(200)
            .setHeader("Content-type", "application/json")
            .setBody(content));
    server.play();

    IClientConfig config = DefaultClientConfigImpl
            .getClientConfigWithDefaultValues()
            .withProperty(CommonClientConfigKey.ConnectTimeout, "2000")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);

    System.out.println(config);

    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer  = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + server.getPort());
    List<Server> servers = Lists.newArrayList(goodServer, badServer);

    BaseLoadBalancer lb = LoadBalancerBuilder.newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);

    IClientConfig overrideConfig = DefaultClientConfigImpl
            .getEmptyConfig()
            .set(CommonClientConfigKey.ConnectTimeout, 500);

    TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<>(request, overrideConfig);
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    HttpClientResponse<ByteBuf> response = client.submit(request, null, overrideConfig).toBlocking().last();

    System.out.println(listener);

    assertEquals(200, response.getStatus().code());
    assertEquals(1, listener.executionStartCounter.get());
    assertEquals(3, listener.startWithServerCounter.get());
    assertEquals(2, listener.exceptionWithServerCounter.get());
    assertEquals(0, listener.executionFailedCounter.get());
    assertEquals(1, listener.executionSuccessCounter.get());
    assertEquals(500, listener.getContext().getClientProperty(CommonClientConfigKey.ConnectTimeout).intValue());
    assertTrue(listener.isContextChecked());
    assertTrue(listener.isCheckExecutionInfo());
    assertSame(response, listener.getResponse());
}
 
Example #17
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadBalancingPostWithNoRetrySameServer() throws Exception {
    MockWebServer server = new MockWebServer();
    String content = "{\"name\": \"ribbon\", \"age\": 2}";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json")
            .setBody(content));       
    server.play();

    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues()
            .set(CommonClientConfigKey.ReadTimeout, 100);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/postTimeout")
            .withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null))
            .withHeader("Content-type", "application/json");
    NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(0, 3, true);
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
    Server goodServer = new Server("localhost:" + server.getPort());
    Server badServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    lb.setServersList(servers);
    RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) {
        @Override
        public boolean isRetriableException(Throwable e, boolean sameServer) {
            return true;
        }
    };
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    server.shutdown();
    assertEquals("ribbon", observer.obj.name);
    assertEquals(2, observer.obj.age);
    ServerStats stats = lbObservables.getServerStats(badServer);
    assertEquals(2, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(2, stats.getSuccessiveConnectionFailureCount());
    
    stats = lbObservables.getServerStats(goodServer);
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
}
 
Example #18
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadBalancingPostWithReadTimeout() throws Exception {
    MockWebServer server = new MockWebServer();
    String content = "{\"name\": \"ribbon\", \"age\": 2}";
    server.enqueue(new MockResponse()
            .setResponseCode(200)
            .setHeader("Content-type", "application/json")
            .setBody(content));       
    server.play();

    IClientConfig config = DefaultClientConfigImpl
            .getClientConfigWithDefaultValues()
            .set(CommonClientConfigKey.ReadTimeout, 100);
    
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/postTimeout")
            .withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null))
            .withHeader("Content-type", "application/json");
    
    NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
    Server goodServer = new Server("localhost:" + server.getPort());
    Server badServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    lb.setServersList(servers);
    RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) {
        @Override
        public boolean isRetriableException(Throwable e, boolean sameServer) {
            return true;
        }
    };
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    assertEquals("ribbon", observer.obj.name);
    assertEquals(2, observer.obj.age);
    ServerStats stats = lbObservables.getServerStats(badServer);
    server.shutdown();
    assertEquals(4, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(4, stats.getSuccessiveConnectionFailureCount());
    
    stats = lbObservables.getServerStats(goodServer);
    // two requests to bad server because retry same server is set to 1
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
}
 
Example #19
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadBalancingWithTwoServers() throws Exception {
    MockWebServer server = new MockWebServer();
    String content = "{\"name\": \"ribbon\", \"age\": 2}";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json")
            .setBody(content));       
    server.play();

    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues();
            
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/person")
            .withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null))
            .withHeader("Content-type", "application/json");
    NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
    HttpClientListener externalListener = HttpClientListener.newHttpListener("external");
    lbObservables.subscribe(externalListener);
    Server server1 = new Server("localhost:" + server.getPort());
    Server server2 = new Server("localhost:" + port);
    
    lb.setServersList(Lists.newArrayList(server1, server2));
    RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) {
        @Override
        public boolean isRetriableException(Throwable e, boolean sameServer) {
            return true;
        }
    };
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    assertEquals("ribbon", observer.obj.name);
    assertEquals(EmbeddedResources.defaultPerson.age, observer.obj.age);
    
    observer = new ObserverWithLatch<Person>();
    observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    assertEquals("ribbon", observer.obj.name);
    assertEquals(2, observer.obj.age);
    
    ServerStats stats = lbObservables.getServerStats(server1);
    server.shutdown();
    // assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    
    stats = lbObservables.getServerStats(server2);
    // two requests to bad server because retry same server is set to 1
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
    final HttpClientListener listener = lbObservables.getListener();
    assertEquals(2, listener.getPoolAcquires());
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 2;
        }
    });
    assertEquals(2, listener.getConnectionCount());
    assertEquals(0, listener.getPoolReuse());
    assertEquals(2, externalListener.getPoolAcquires());
}
 
Example #20
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadBalancingObservablesWithReadTimeout() throws Exception {
    NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
    MockWebServer server = new MockWebServer();
    String content = "{\"name\": \"ribbon\", \"age\": 2}";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json")
            .setBody(content));       
    server.play();

    IClientConfig config = DefaultClientConfigImpl
            .getClientConfigWithDefaultValues()
            .set(CommonClientConfigKey.ReadTimeout, 100);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/readTimeout");
    
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
    
    Server goodServer = new Server("localhost:" + server.getPort());
    Server badServer  = new Server("localhost:" + port);
    lb.setServersList(Lists.newArrayList(goodServer, badServer, badServer, goodServer));
    
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    assertEquals("ribbon", observer.obj.name);
    assertEquals(2, observer.obj.age);
    ServerStats stats = lbObservables.getServerStats(badServer);
    server.shutdown();
    
    final HttpClientListener listener = lbObservables.getListener();
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 5;
        }
    });
    assertEquals(0, listener.getPoolReuse());
    
    // two requests to bad server because retry same server is set to 1
    assertEquals(4, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(4, stats.getSuccessiveConnectionFailureCount());
    
    stats = lbObservables.getServerStats(goodServer);
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
}
 
Example #21
Source File: DefaultNIWSServerListFilterTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testZoneAffinityEnabled() throws Exception {
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest1.ribbon.DeploymentContextBasedVipAddresses", "l10nservicegeneral.cloud.netflix.net:7001");
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest1.ribbon.NFLoadBalancerClassName", DynamicServerListLoadBalancer.class.getName());
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest1.ribbon.NIWSServerListClassName", DiscoveryEnabledNIWSServerList.class.getName());

    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest1.ribbon.EnableZoneAffinity", "true");
    DynamicServerListLoadBalancer lb = (DynamicServerListLoadBalancer) ClientFactory.getNamedLoadBalancer("DefaultNIWSServerListFilterTest1");
    assertTrue(lb.getRule() instanceof AvailabilityFilteringRule);
    ZoneAffinityServerListFilter filter = (ZoneAffinityServerListFilter) lb.getFilter();
    LoadBalancerStats loadBalancerStats = lb.getLoadBalancerStats();
    List<DiscoveryEnabledServer> servers = new ArrayList<DiscoveryEnabledServer>();        
    servers.add(createServer(1, "a"));
    servers.add(createServer(2, "a"));
    servers.add(createServer(3, "a"));
    servers.add(createServer(4, "a"));
    servers.add(createServer(1, "b"));
    servers.add(createServer(2, "b"));
    servers.add(createServer(3, "b"));
    servers.add(createServer(1, "c"));
    servers.add(createServer(2, "c"));
    servers.add(createServer(3, "c"));
    servers.add(createServer(4, "c"));
    servers.add(createServer(5, "c"));
    List<DiscoveryEnabledServer> filtered = filter.getFilteredListOfServers(servers);
    List<DiscoveryEnabledServer> expected = new ArrayList<DiscoveryEnabledServer>();
    expected.add(createServer(1, "c"));
    expected.add(createServer(2, "c"));
    expected.add(createServer(3, "c"));
    expected.add(createServer(4, "c"));
    expected.add(createServer(5, "c"));
    assertEquals(expected, filtered);
    lb.setServersList(filtered);        
    for (int i = 1; i <= 4; i++) {            
        loadBalancerStats.incrementActiveRequestsCount(createServer(i, "c"));
    }
    filtered = filter.getFilteredListOfServers(servers);
    assertEquals(servers, filtered);

}
 
Example #22
Source File: RibbonServiceConfiguration.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Bean
public IRule iRule(IClientConfig iClientConfig) {
    return new AvailabilityFilteringRule();
}
 
Example #23
Source File: RibbonConfiguration.java    From blog_demos with Apache License 2.0 2 votes vote down vote up
/**
 * 轮询规则
 * @param config
 * @return
 */
@Bean
public IRule ribbonRule(IClientConfig config){
    return new AvailabilityFilteringRule();
}
 
Example #24
Source File: RibbonConfiguration.java    From tutorials with MIT License 2 votes vote down vote up
/**
 * AvailabilityFilteringRule will use Ribbon’s built-in circuit breaker functionality to filter out any servers in an “open-circuit” state:
 * if a ping fails to connect to a given server, or if it gets a read failure for the server, Ribbon will consider that server “dead” until it begins to respond normally.
 *
 * @param  config Client configuration
 * @return The Load Balancer rule
 */
@Bean
public IRule ribbonRule(IClientConfig config) {
    return new AvailabilityFilteringRule();
}