Search in sources :

Example 1 with BaseLoadBalancer

use of com.netflix.loadbalancer.BaseLoadBalancer in project ribbon by Netflix.

the class LBBuilderTest method testBuildStaticServerListLoadBalancer.

@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());
}
Also used : Server(com.netflix.loadbalancer.Server) IClientConfig(com.netflix.client.config.IClientConfig) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) IRule(com.netflix.loadbalancer.IRule) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with BaseLoadBalancer

use of com.netflix.loadbalancer.BaseLoadBalancer in project ribbon by Netflix.

the class RetryTest method beforeTest.

@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();
}
Also used : DummyPing(com.netflix.loadbalancer.DummyPing) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Before(org.junit.Before)

Example 3 with BaseLoadBalancer

use of com.netflix.loadbalancer.BaseLoadBalancer in project ribbon by Netflix.

the class RestClientTest method testSecureClient2.

@Test
public void testSecureClient2() throws Exception {
    ConfigurationManager.getConfigInstance().setProperty("test3.ribbon." + CommonClientConfigKey.IsSecure, "true");
    ConfigurationManager.getConfigInstance().setProperty("test3.ribbon." + CommonClientConfigKey.TrustStore, secureServer.getTrustStore().getAbsolutePath());
    ConfigurationManager.getConfigInstance().setProperty("test3.ribbon." + CommonClientConfigKey.TrustStorePassword, SecureGetTest.PASSWORD);
    RestClient client = (RestClient) ClientFactory.getNamedClient("test3");
    BaseLoadBalancer lb = new BaseLoadBalancer();
    Server[] servers = new Server[] { new Server("localhost", secureServer.getServerPort()) };
    lb.addServers(Arrays.asList(servers));
    client.setLoadBalancer(lb);
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
    HttpResponse response = client.executeWithLoadBalancer(request);
    assertStatusIsOk(response.getStatus());
    assertEquals(secureServer.getServerPath("/"), response.getRequestedURI().toString());
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) Server(com.netflix.loadbalancer.Server) MockHttpServer(com.netflix.client.testutil.MockHttpServer) HttpResponse(com.netflix.client.http.HttpResponse) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) URI(java.net.URI) Test(org.junit.Test)

Example 4 with BaseLoadBalancer

use of com.netflix.loadbalancer.BaseLoadBalancer in project ribbon by Netflix.

the class NettyClientTest method testObservableWithRetrySameServer.

@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());
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) ByteBuf(io.netty.buffer.ByteBuf) ServerStats(com.netflix.loadbalancer.ServerStats) DummyPing(com.netflix.loadbalancer.DummyPing) IClientConfig(com.netflix.client.config.IClientConfig) ClientException(com.netflix.client.ClientException) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Person(com.netflix.ribbon.test.resources.EmbeddedResources.Person) Test(org.junit.Test)

Example 5 with BaseLoadBalancer

use of com.netflix.loadbalancer.BaseLoadBalancer in project ribbon by Netflix.

the class NettyClientTest method testObservableWithMultipleServersWithOverrideRxConfig.

@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;
        }
    });
}
Also used : HttpClientConfig(io.reactivex.netty.protocol.http.client.HttpClient.HttpClientConfig) HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) ByteBuf(io.netty.buffer.ByteBuf) HttpClientListener(io.reactivex.netty.servo.http.HttpClientListener) ServerStats(com.netflix.loadbalancer.ServerStats) DummyPing(com.netflix.loadbalancer.DummyPing) IClientConfig(com.netflix.client.config.IClientConfig) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Person(com.netflix.ribbon.test.resources.EmbeddedResources.Person) Test(org.junit.Test)

Aggregations

BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)26 Server (com.netflix.loadbalancer.Server)24 Test (org.junit.Test)23 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)19 ByteBuf (io.netty.buffer.ByteBuf)19 MockWebServer (com.google.mockwebserver.MockWebServer)18 IClientConfig (com.netflix.client.config.IClientConfig)18 DummyPing (com.netflix.loadbalancer.DummyPing)18 HttpServer (com.sun.net.httpserver.HttpServer)10 Person (com.netflix.ribbon.test.resources.EmbeddedResources.Person)9 ServerStats (com.netflix.loadbalancer.ServerStats)8 MockResponse (com.google.mockwebserver.MockResponse)7 ExecutionListener (com.netflix.loadbalancer.reactive.ExecutionListener)7 ClientException (com.netflix.client.ClientException)6 AbortExecutionException (com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException)5 HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)4 HttpClientListener (io.reactivex.netty.servo.http.HttpClientListener)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 RequestSpecificRetryHandler (com.netflix.client.RequestSpecificRetryHandler)3 RetryHandler (com.netflix.client.RetryHandler)3