Search in sources :

Example 11 with BaseLoadBalancer

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

the class ListenerTest method testAbortedExecution.

@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);
}
Also used : Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) AbortExecutionException(com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException) ByteBuf(io.netty.buffer.ByteBuf) ExecutionListener(com.netflix.loadbalancer.reactive.ExecutionListener) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionContext(com.netflix.loadbalancer.reactive.ExecutionContext) DummyPing(com.netflix.loadbalancer.DummyPing) IClientConfig(com.netflix.client.config.IClientConfig) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Test(org.junit.Test)

Example 12 with BaseLoadBalancer

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

the class ListenerTest method testSuccessExecutionOnAbosoluteURI.

@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());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) ByteBuf(io.netty.buffer.ByteBuf) ExecutionListener(com.netflix.loadbalancer.reactive.ExecutionListener) DummyPing(com.netflix.loadbalancer.DummyPing) MockWebServer(com.google.mockwebserver.MockWebServer) IClientConfig(com.netflix.client.config.IClientConfig) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Test(org.junit.Test)

Example 13 with BaseLoadBalancer

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

the class ListenerTest method testFailedExecution.

@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());
}
Also used : Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) ByteBuf(io.netty.buffer.ByteBuf) ClientException(com.netflix.client.ClientException) AbortExecutionException(com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException) IOException(java.io.IOException) ExecutionListener(com.netflix.loadbalancer.reactive.ExecutionListener) DummyPing(com.netflix.loadbalancer.DummyPing) IClientConfig(com.netflix.client.config.IClientConfig) ClientException(com.netflix.client.ClientException) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Test(org.junit.Test)

Example 14 with BaseLoadBalancer

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

the class RestClient method shutdown.

public void shutdown() {
    ILoadBalancer lb = this.getLoadBalancer();
    if (lb instanceof BaseLoadBalancer) {
        ((BaseLoadBalancer) lb).shutdown();
    }
    NFHttpClientFactory.shutdownNFHttpClient(restClientName);
}
Also used : ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer)

Example 15 with BaseLoadBalancer

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

the class RestClientTest method testExecuteWithLB.

@Test
public void testExecuteWithLB() throws Exception {
    ConfigurationManager.getConfigInstance().setProperty("allservices.ribbon." + CommonClientConfigKey.ReadTimeout, "10000");
    ConfigurationManager.getConfigInstance().setProperty("allservices.ribbon." + CommonClientConfigKey.FollowRedirects, "true");
    RestClient client = (RestClient) ClientFactory.getNamedClient("allservices");
    BaseLoadBalancer lb = new BaseLoadBalancer();
    Server[] servers = new Server[] { new Server("localhost", server.getServerPort()) };
    lb.addServers(Arrays.asList(servers));
    client.setLoadBalancer(lb);
    Set<URI> expected = new HashSet<URI>();
    expected.add(new URI(server.getServerPath("/")));
    Set<URI> result = new HashSet<URI>();
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
    for (int i = 0; i < 5; i++) {
        HttpResponse response = client.executeWithLoadBalancer(request);
        assertStatusIsOk(response.getStatus());
        assertTrue(response.isSuccess());
        String content = response.getEntity(String.class);
        response.close();
        assertFalse(content.isEmpty());
        result.add(response.getRequestedURI());
    }
    assertEquals(expected, result);
    request = HttpRequest.newBuilder().uri(server.getServerURI()).build();
    HttpResponse response = client.executeWithLoadBalancer(request);
    assertEquals(200, response.getStatus());
}
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) HashSet(java.util.HashSet) 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