Search in sources :

Example 1 with ConstantResponseTimeDistribution

use of com.linkedin.d2.backuprequests.ConstantResponseTimeDistribution in project rest.li by linkedin.

the class TestBackupRequestsClient method testStatsConsumerLatencyUpdate.

// @Test - Disabled due to flakiness. See SI-3077 to track and resolve this.
public void testStatsConsumerLatencyUpdate() throws Exception {
    AtomicReference<ServiceProperties> serviceProperties = new AtomicReference<>();
    TestBackupRequestsStrategyStatsConsumer statsConsumer = new TestBackupRequestsStrategyStatsConsumer();
    serviceProperties.set(createServiceProperties(null));
    BackupRequestsClient client = createClient(serviceProperties::get, statsConsumer, new ConstantResponseTimeDistribution(1, TimeUnit.NANOSECONDS), false);
    URI uri = URI.create("d2://testService");
    RestRequest restRequest = new RestRequestBuilder(uri).setEntity(CONTENT).build();
    RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(R2Constants.OPERATION, "get");
    Future<RestResponse> response = client.restRequest(restRequest, requestContext);
    assertEquals(response.get().getStatus(), 200);
    List<StatsConsumerEvent> events = statsConsumer.getEvents();
    assertEquals(events.size(), 0);
    for (int i = 0; i < Short.MAX_VALUE * 4; i++) {
        requestContext = new RequestContext();
        requestContext.putLocalAttr(R2Constants.OPERATION, "get");
        response = client.restRequest(restRequest, requestContext);
        assertEquals(response.get().getStatus(), 200);
    }
    assertEquals(statsConsumer.getLatencyWithBackup().size(), 0);
    assertEquals(statsConsumer.getLatencyWithoutBackup().size(), 0);
    serviceProperties.set(createServiceProperties(Arrays.asList(createBackupRequestsConfiguration(5, "get"))));
    while (statsConsumer.getLatencyWithoutBackup().size() < 1) {
        requestContext = new RequestContext();
        requestContext.putLocalAttr(R2Constants.OPERATION, "get");
        response = client.restRequest(restRequest, requestContext);
        assertEquals(response.get().getStatus(), 200);
    }
    assertEquals(statsConsumer.getLatencyWithoutBackup().size(), 1);
    assertEquals(statsConsumer.getLatencyWithBackup().size(), 1);
    // allowing 1% imprecision
    long expected = statsConsumer.getLatencyWithoutBackup().get(0).getTotalCount();
    long actual = statsConsumer.getLatencyWithBackup().get(0).getTotalCount();
    assertTrue(actual > expected * .99 && actual < expected * 1.01, "Expected: " + expected + "+-" + (expected * .01) + ", but actual: " + actual);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(java.net.URI) ConstantResponseTimeDistribution(com.linkedin.d2.backuprequests.ConstantResponseTimeDistribution) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext)

Example 2 with ConstantResponseTimeDistribution

use of com.linkedin.d2.backuprequests.ConstantResponseTimeDistribution in project rest.li by linkedin.

the class TestBackupRequestsClient method testD2ServiceUnavailable.

@Test(dataProvider = "isD2Async")
public void testD2ServiceUnavailable(boolean isD2Async) throws Exception {
    LoadBalancer loadBalancer = new TestLoadBalancer(new ConstantResponseTimeDistribution(1, TimeUnit.NANOSECONDS), null, new ServiceUnavailableException("", ""));
    TestBackupRequestsStrategyStatsConsumer statsConsumer = new TestBackupRequestsStrategyStatsConsumer();
    BackupRequestsClient client = createClient(statsConsumer, loadBalancer, isD2Async);
    URI uri = URI.create("d2://testService");
    RestRequest restRequest = new RestRequestBuilder(uri).setEntity(CONTENT).build();
    RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(R2Constants.OPERATION, "get");
    Future<RestResponse> response = client.restRequest(restRequest, requestContext);
    assertEquals(response.get().getStatus(), 200, "If D2 call fails, we should fallback to request without backup");
    List<StatsConsumerEvent> events = statsConsumer.getEvents();
    assertEquals(events.size(), 0);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) LoadBalancer(com.linkedin.d2.balancer.LoadBalancer) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) URI(java.net.URI) ConstantResponseTimeDistribution(com.linkedin.d2.backuprequests.ConstantResponseTimeDistribution) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

ConstantResponseTimeDistribution (com.linkedin.d2.backuprequests.ConstantResponseTimeDistribution)2 RequestContext (com.linkedin.r2.message.RequestContext)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 URI (java.net.URI)2 LoadBalancer (com.linkedin.d2.balancer.LoadBalancer)1 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)1 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)1 SimpleLoadBalancer (com.linkedin.d2.balancer.simple.SimpleLoadBalancer)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AfterTest (org.testng.annotations.AfterTest)1 BeforeTest (org.testng.annotations.BeforeTest)1 Test (org.testng.annotations.Test)1