Search in sources :

Example 56 with Server

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

the class NettyClientTest method testLoadBalancingObservablesWithReadTimeout.

@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());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) 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) MockWebServer(com.google.mockwebserver.MockWebServer) IClientConfig(com.netflix.client.config.IClientConfig) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Person(com.netflix.ribbon.test.resources.EmbeddedResources.Person) Test(org.junit.Test)

Example 57 with Server

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

the class NettyClientTest method testLoadBalancerThrottle.

@Test
public void testLoadBalancerThrottle() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/throttle");
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(IClientConfigKey.Keys.MaxAutoRetriesNextServer, 1).set(IClientConfigKey.Keys.OkToRetryOnAllOperations, true);
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config);
    Server server = new Server(host, port);
    lb.setServersList(Lists.newArrayList(server, server, server));
    Observable<HttpClientResponse<ByteBuf>> response = lbObservables.submit(request);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    response.subscribe(new Action1<HttpClientResponse<ByteBuf>>() {

        @Override
        public void call(HttpClientResponse<ByteBuf> t1) {
            System.err.println("Get response: " + t1.getStatus().code());
            latch.countDown();
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable t1) {
            error.set(t1);
            latch.countDown();
        }
    }, new Action0() {

        @Override
        public void call() {
            Thread.dumpStack();
            latch.countDown();
        }
    });
    latch.await();
    assertTrue(error.get() instanceof ClientException);
    ClientException ce = (ClientException) error.get();
    assertTrue(ce.toString(), ce.getErrorType() == ClientException.ErrorType.NUMBEROF_RETRIES_NEXTSERVER_EXCEEDED);
    assertEquals(2, lbObservables.getServerStats(server).getSuccessiveConnectionFailureCount());
}
Also used : Action0(rx.functions.Action0) HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) CountDownLatch(java.util.concurrent.CountDownLatch) DummyPing(com.netflix.loadbalancer.DummyPing) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) IClientConfig(com.netflix.client.config.IClientConfig) ClientException(com.netflix.client.ClientException) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Test(org.junit.Test)

Example 58 with Server

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

the class NettyClientTest method testStreamWithLoadBalancer.

@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);
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) ServerSentEvent(io.reactivex.netty.protocol.text.sse.ServerSentEvent) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) ByteBuf(io.netty.buffer.ByteBuf) 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)

Example 59 with Server

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

the class ServerListRefreshTest method testServerListRefresh.

/**
     * This test ensures that when server list is refreshed in the load balancer, the set of servers
     * which equals to (oldList - newList) should be removed from the map of cached RxClient. Any server
     * that is not part of oldList should stay in the map.
     *
     * @throws IOException
     */
@Test
public void testServerListRefresh() throws IOException {
    String content = "Hello world";
    MockWebServer server1 = new MockWebServer();
    MockWebServer server2 = new MockWebServer();
    MockWebServer server3 = new MockWebServer();
    MockResponse mockResponse = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content);
    server1.enqueue(mockResponse);
    server2.enqueue(mockResponse);
    server3.enqueue(mockResponse);
    server1.play();
    server2.play();
    server3.play();
    try {
        BaseLoadBalancer lb = new BaseLoadBalancer();
        List<Server> initialList = Lists.newArrayList(new Server("localhost", server1.getPort()), new Server("localhost", server2.getPort()));
        lb.setServersList(initialList);
        LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb);
        HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/");
        client.submit(request).toBlocking().last();
        client.submit(request).toBlocking().last();
        HttpClientRequest<ByteBuf> request2 = HttpClientRequest.createGet("http://localhost:" + server3.getPort());
        client.submit(request2).toBlocking().last();
        Set<Server> cachedServers = client.getRxClients().keySet();
        assertEquals(Sets.newHashSet(new Server("localhost", server1.getPort()), new Server("localhost", server2.getPort()), new Server("localhost", server3.getPort())), cachedServers);
        List<Server> newList = Lists.newArrayList(new Server("localhost", server1.getPort()), new Server("localhost", 99999));
        lb.setServersList(newList);
        cachedServers = client.getRxClients().keySet();
        assertEquals(Sets.newHashSet(new Server("localhost", server1.getPort()), new Server("localhost", server3.getPort())), cachedServers);
    } finally {
        server1.shutdown();
        server2.shutdown();
        server3.shutdown();
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) MockWebServer(com.google.mockwebserver.MockWebServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 60 with Server

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

the class DiscoveryLoadBalancerTest method testLoadBalancer.

@Test
public void testLoadBalancer() {
    IClientConfig config = IClientConfig.Builder.newBuilder().withDefaultValues().withDeploymentContextBasedVipAddresses(getVipAddress()).build().set(IClientConfigKey.Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(config);
    LoadBalancerContext lbContext = client.getLoadBalancerContext();
    List<Server> serverList = lbContext.getLoadBalancer().getAllServers();
    assertEquals(getMockServerList(), serverList);
}
Also used : LoadBalancerContext(com.netflix.loadbalancer.LoadBalancerContext) Server(com.netflix.loadbalancer.Server) DiscoveryEnabledNIWSServerList(com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList) IClientConfig(com.netflix.client.config.IClientConfig) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test) MockedDiscoveryServerListTest(com.netflix.ribbon.testutils.MockedDiscoveryServerListTest)

Aggregations

Server (com.netflix.loadbalancer.Server)72 Test (org.junit.Test)56 ByteBuf (io.netty.buffer.ByteBuf)26 MockWebServer (com.google.mockwebserver.MockWebServer)25 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)24 IClientConfig (com.netflix.client.config.IClientConfig)20 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)19 DummyPing (com.netflix.loadbalancer.DummyPing)18 HttpServer (com.sun.net.httpserver.HttpServer)18 ArrayList (java.util.ArrayList)13 Person (com.netflix.ribbon.test.resources.EmbeddedResources.Person)12 URI (java.net.URI)11 ServerStats (com.netflix.loadbalancer.ServerStats)10 ClientException (com.netflix.client.ClientException)9 HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)9 ExecutionListener (com.netflix.loadbalancer.reactive.ExecutionListener)8 MockResponse (com.google.mockwebserver.MockResponse)7 AbortExecutionException (com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 DynamicServerListLoadBalancer (com.netflix.loadbalancer.DynamicServerListLoadBalancer)5