Search in sources :

Example 21 with SettableClock

use of com.linkedin.util.clock.SettableClock 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 22 with SettableClock

use of com.linkedin.util.clock.SettableClock in project rest.li by linkedin.

the class DegraderLoadBalancerTest method testBadTrackerClients.

@Test(groups = { "small", "back-end" })
public void testBadTrackerClients() throws URISyntaxException {
    DegraderLoadBalancerStrategyV3 strategy = getStrategy();
    // test null twice (first time will have no state)
    for (int i = 0; i < 2; ++i) {
        assertNull(getTrackerClient(strategy, null, new RequestContext(), 0, null));
    }
    strategy = getStrategy();
    // test empty twice (first time will have no state)
    for (int i = 0; i < 2; ++i) {
        assertNull(getTrackerClient(strategy, null, new RequestContext(), 0, new ArrayList<>()));
    }
    // test same cluster generation id but different client lists
    strategy = getStrategy();
    List<DegraderTrackerClient> clients1 = new ArrayList<>();
    SettableClock clock1 = new SettableClock();
    SettableClock clock2 = new SettableClock();
    List<DegraderTrackerClient> clients2 = new ArrayList<>();
    SettableClock clock3 = new SettableClock();
    SettableClock clock4 = new SettableClock();
    clients1.add(getClient(URI.create("http://test.linkedin.com:3242/fdsaf"), clock1));
    clients1.add(getClient(URI.create("http://test.linkedin.com:3243/fdsaf"), clock2));
    clients2.add(getClient(URI.create("http://asdbasdf.com:3242/fdsaf"), clock3));
    clients2.add(getClient(URI.create("ftp://example.com:21/what"), clock4));
    // same cluster generation id but different tracker clients
    assertNotNull(getTrackerClient(strategy, null, new RequestContext(), 0, clients2));
    assertNull(getTrackerClient(strategy, null, new RequestContext(), 0, clients1));
    // now trigger an update by cluster generation id
    assertNotNull(getTrackerClient(strategy, null, new RequestContext(), 1, clients1));
    assertNull(getTrackerClient(strategy, null, new RequestContext(), 1, clients2));
}
Also used : DegraderTrackerClient(com.linkedin.d2.balancer.clients.DegraderTrackerClient) ArrayList(java.util.ArrayList) SettableClock(com.linkedin.util.clock.SettableClock) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test) DegraderTrackerClientTest(com.linkedin.d2.balancer.clients.DegraderTrackerClientTest)

Aggregations

SettableClock (com.linkedin.util.clock.SettableClock)22 Test (org.testng.annotations.Test)19 RequestContext (com.linkedin.r2.message.RequestContext)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 URI (java.net.URI)9 RestRequest (com.linkedin.r2.message.rest.RestRequest)8 HashMap (java.util.HashMap)8 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)6 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)6 DegraderControl (com.linkedin.util.degrader.DegraderControl)6 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)5 ArrayList (java.util.ArrayList)5 Callback (com.linkedin.common.callback.Callback)4 FutureCallback (com.linkedin.common.callback.FutureCallback)4 LongTracking (com.linkedin.common.stats.LongTracking)4 ByteString (com.linkedin.data.ByteString)4 RestResponse (com.linkedin.r2.message.rest.RestResponse)4 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)4 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)4 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)4