Search in sources :

Example 11 with RetryClient

use of com.linkedin.d2.balancer.clients.RetryClient in project rest.li by linkedin.

the class RetryClientTest method testStreamException.

@Test
public void testStreamException() throws Exception {
    SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/bad"), HttpClientFactory.UNLIMITED_CLIENT_REQUEST_RETRY_RATIO);
    DynamicClient dynamicClient = new DynamicClient(balancer, null);
    RetryClient client = new RetryClient(dynamicClient, balancer, D2ClientConfig.DEFAULT_RETRY_LIMIT, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, SystemClock.instance(), true, true);
    URI uri = URI.create("d2://retryService?arg1=empty&arg2=empty");
    StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.emptyStream());
    DegraderTrackerClientTest.TestCallback<StreamResponse> streamCallback = new DegraderTrackerClientTest.TestCallback<>();
    RequestContext context = new RequestContext();
    KeyMapper.TargetHostHints.setRequestContextTargetHost(context, URI.create("http://test.linkedin.com/bad"));
    client.streamRequest(streamRequest, context, streamCallback);
    assertNull(streamCallback.t);
    assertNotNull(streamCallback.e);
    assertTrue(streamCallback.e.getMessage().contains("exception happens"), streamCallback.e.getMessage());
}
Also used : SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 12 with RetryClient

use of com.linkedin.d2.balancer.clients.RetryClient in project rest.li by linkedin.

the class RetryClientTest method testRestRetryUnlimitedClientRetryRatio.

@Test
public void testRestRetryUnlimitedClientRetryRatio() throws Exception {
    SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/good"), HttpClientFactory.UNLIMITED_CLIENT_REQUEST_RETRY_RATIO);
    ClockedExecutor clock = new ClockedExecutor();
    DynamicClient dynamicClient = new DynamicClient(balancer, null);
    RetryClient client = new RetryClient(dynamicClient, balancer, D2ClientConfig.DEFAULT_RETRY_LIMIT, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, clock, true, false);
    URI uri = URI.create("d2://retryService?arg1=empty&arg2=empty");
    RestRequest restRequest = new RestRequestBuilder(uri).build();
    clock.scheduleWithFixedDelay(() -> {
        DegraderTrackerClientTest.TestCallback<RestResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
        client.restRequest(restRequest, restCallback);
        // This request will be retried and route to the good host
        assertNull(restCallback.e);
        assertNotNull(restCallback.t);
    }, 0, 100, TimeUnit.MILLISECONDS);
    clock.runFor(RetryClient.DEFAULT_UPDATE_INTERVAL_MS * 2);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ClockedExecutor(com.linkedin.test.util.ClockedExecutor) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 13 with RetryClient

use of com.linkedin.d2.balancer.clients.RetryClient in project rest.li by linkedin.

the class RetryClientTest method testRestRetryExceedsClientRetryRatio.

// Known to be flaky in CI
@Test(retryAnalyzer = SingleRetry.class)
public void testRestRetryExceedsClientRetryRatio() throws Exception {
    SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/good"), HttpClientFactory.DEFAULT_MAX_CLIENT_REQUEST_RETRY_RATIO);
    SettableClock clock = new SettableClock();
    DynamicClient dynamicClient = new DynamicClient(balancer, null);
    RetryClient client = new RetryClient(dynamicClient, balancer, D2ClientConfig.DEFAULT_RETRY_LIMIT, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, clock, true, false);
    URI uri1 = URI.create("d2://retryService1?arg1=empty&arg2=empty");
    RestRequest restRequest1 = new RestRequestBuilder(uri1).build();
    URI uri2 = URI.create("d2://retryService2?arg1=empty&arg2=empty");
    RestRequest restRequest2 = new RestRequestBuilder(uri2).build();
    // This request will be retried and route to the good host
    DegraderTrackerClientTest.TestCallback<RestResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
    client.restRequest(restRequest1, restCallback);
    assertNull(restCallback.e);
    assertNotNull(restCallback.t);
    // This request will not be retried because the retry ratio is exceeded
    clock.addDuration(RetryClient.DEFAULT_UPDATE_INTERVAL_MS);
    restCallback = new DegraderTrackerClientTest.TestCallback<>();
    client.restRequest(restRequest1, restCallback);
    assertNull(restCallback.t);
    assertNotNull(restCallback.e);
    assertTrue(restCallback.e.getMessage().contains("Data not available"));
    // If the client sends request to a different service endpoint, the retry ratio should not interfere
    restCallback = new DegraderTrackerClientTest.TestCallback<>();
    client.restRequest(restRequest2, restCallback);
    assertNull(restCallback.e);
    assertNotNull(restCallback.t);
    // After 5s interval, retry counter is reset and this request will be retried again
    clock.addDuration(RetryClient.DEFAULT_UPDATE_INTERVAL_MS * RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM);
    restCallback = new DegraderTrackerClientTest.TestCallback<>();
    client.restRequest(restRequest1, restCallback);
    assertNull(restCallback.e);
    assertNotNull(restCallback.t);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) RestResponse(com.linkedin.r2.message.rest.RestResponse) SettableClock(com.linkedin.util.clock.SettableClock) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 14 with RetryClient

use of com.linkedin.d2.balancer.clients.RetryClient in project rest.li by linkedin.

the class RetryClientTest method testRestRetry.

@Test
public void testRestRetry() throws Exception {
    SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/good"), HttpClientFactory.UNLIMITED_CLIENT_REQUEST_RETRY_RATIO);
    DynamicClient dynamicClient = new DynamicClient(balancer, null);
    RetryClient client = new RetryClient(dynamicClient, balancer, D2ClientConfig.DEFAULT_RETRY_LIMIT, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, SystemClock.instance(), true, false);
    URI uri = URI.create("d2://retryService?arg1arg2");
    RestRequest restRequest = new RestRequestBuilder(uri).setEntity(CONTENT).build();
    DegraderTrackerClientTest.TestCallback<RestResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
    client.restRequest(restRequest, restCallback);
    assertNull(restCallback.e);
    assertNotNull(restCallback.t);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 15 with RetryClient

use of com.linkedin.d2.balancer.clients.RetryClient in project rest.li by linkedin.

the class RetryClientTest method testRestException.

@Test
public void testRestException() throws Exception {
    SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/bad"), HttpClientFactory.UNLIMITED_CLIENT_REQUEST_RETRY_RATIO);
    DynamicClient dynamicClient = new DynamicClient(balancer, null);
    RetryClient client = new RetryClient(dynamicClient, balancer, D2ClientConfig.DEFAULT_RETRY_LIMIT, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, SystemClock.instance(), true, false);
    URI uri = URI.create("d2://retryService?arg1=empty&arg2=empty");
    RestRequest restRequest = new RestRequestBuilder(uri).build();
    DegraderTrackerClientTest.TestCallback<RestResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
    RequestContext context = new RequestContext();
    KeyMapper.TargetHostHints.setRequestContextTargetHost(context, URI.create("http://test.linkedin.com/bad"));
    client.restRequest(restRequest, context, restCallback);
    assertNull(restCallback.t);
    assertNotNull(restCallback.e);
    assertTrue(restCallback.e.getMessage().contains("exception happens"));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) Test(org.testng.annotations.Test)

Aggregations

SimpleLoadBalancer (com.linkedin.d2.balancer.simple.SimpleLoadBalancer)14 URI (java.net.URI)14 Test (org.testng.annotations.Test)14 RestRequest (com.linkedin.r2.message.rest.RestRequest)9 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)9 RestResponse (com.linkedin.r2.message.rest.RestResponse)9 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)5 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)5 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)5 RequestContext (com.linkedin.r2.message.RequestContext)2 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)2 FutureCallback (com.linkedin.common.callback.FutureCallback)1 BackupRequestsClient (com.linkedin.d2.balancer.clients.BackupRequestsClient)1 DynamicClient (com.linkedin.d2.balancer.clients.DynamicClient)1 RequestTimeoutClient (com.linkedin.d2.balancer.clients.RequestTimeoutClient)1 RetryClient (com.linkedin.d2.balancer.clients.RetryClient)1 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)1 LoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory)1 RandomLoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory)1 RelativeLoadBalancerStrategy (com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy)1