Search in sources :

Example 6 with SettableClock

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

the class TestServerRetryTracker method setUp.

@BeforeMethod
public void setUp() {
    _clock = new SettableClock();
    _serverRetryTracker = new ServerRetryTracker(ServerRetryFilter.DEFAULT_RETRY_LIMIT, ServerRetryFilter.DEFAULT_AGGREGATED_INTERVAL_NUM, ServerRetryFilter.DEFAULT_MAX_REQUEST_RETRY_RATIO, ServerRetryFilter.DEFAULT_UPDATE_INTERVAL_MS, _clock);
}
Also used : SettableClock(com.linkedin.util.clock.SettableClock) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 7 with SettableClock

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

the class TestAsyncPoolStatsTracker method testMinimumSamplingPeriod.

/**
 * Tests sampled values are the same when #getStats() are called within the same
 * sampling period. Also tests the samplers are correctly updated when #getStats
 * are called in successive sampling periods.
 */
@Test
public void testMinimumSamplingPeriod() {
    SettableClock clock = new SettableClock();
    AsyncPoolStatsTracker tracker = new AsyncPoolStatsTracker(() -> LIFECYCLE_STATS, () -> MAX_SIZE, () -> MIN_SIZE, () -> _poolSize, () -> _checkedOut, () -> IDLE_SIZE, clock, new LongTracking());
    // Samples the max values
    tracker.sampleMaxPoolSize();
    tracker.sampleMaxCheckedOut();
    tracker.sampleMaxWaitTime(WAIT_TIME);
    Assert.assertEquals(tracker.getStats().getSampleMaxPoolSize(), POOL_SIZE);
    Assert.assertEquals(tracker.getStats().getSampleMaxCheckedOut(), CHECKED_OUT);
    Assert.assertEquals(tracker.getStats().getSampleMaxWaitTime(), WAIT_TIME);
    // Without incrementing time we should still be getting the old sampled values
    _poolSize = POOL_SIZE + 10;
    tracker.sampleMaxPoolSize();
    _checkedOut = CHECKED_OUT + 10;
    tracker.sampleMaxCheckedOut();
    tracker.sampleMaxWaitTime(WAIT_TIME + 100);
    Assert.assertEquals(tracker.getStats().getSampleMaxPoolSize(), POOL_SIZE);
    Assert.assertEquals(tracker.getStats().getSampleMaxCheckedOut(), CHECKED_OUT);
    Assert.assertEquals(tracker.getStats().getSampleMaxWaitTime(), WAIT_TIME);
    // After incrementing time we should be getting the new sampled values
    clock.addDuration(SAMPLING_DURATION_INCREMENT);
    Assert.assertEquals(tracker.getStats().getSampleMaxPoolSize(), POOL_SIZE + 10);
    Assert.assertEquals(tracker.getStats().getSampleMaxCheckedOut(), CHECKED_OUT + 10);
    Assert.assertEquals(tracker.getStats().getSampleMaxWaitTime(), WAIT_TIME + 100);
}
Also used : LongTracking(com.linkedin.common.stats.LongTracking) SettableClock(com.linkedin.util.clock.SettableClock) AsyncPoolStatsTracker(com.linkedin.r2.transport.http.client.AsyncPoolStatsTracker) Test(org.testng.annotations.Test)

Example 8 with SettableClock

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

the class TestAsyncPoolStatsTracker method testSamplers.

@Test
public void testSamplers() {
    SettableClock clock = new SettableClock();
    AsyncPoolStatsTracker tracker = new AsyncPoolStatsTracker(() -> LIFECYCLE_STATS, () -> MAX_SIZE, () -> MIN_SIZE, () -> _poolSize, () -> _checkedOut, () -> IDLE_SIZE, clock, new LongTracking());
    // Samples the max values
    tracker.sampleMaxPoolSize();
    tracker.sampleMaxCheckedOut();
    Assert.assertEquals(tracker.getStats().getSampleMaxPoolSize(), POOL_SIZE);
    Assert.assertEquals(tracker.getStats().getSampleMaxCheckedOut(), CHECKED_OUT);
    // Samples at smaller values compared the old samples
    _poolSize = POOL_SIZE - 10;
    _checkedOut = CHECKED_OUT - 10;
    tracker.sampleMaxPoolSize();
    tracker.sampleMaxCheckedOut();
    clock.addDuration(SAMPLING_DURATION_INCREMENT);
    Assert.assertEquals(tracker.getStats().getSampleMaxPoolSize(), POOL_SIZE);
    Assert.assertEquals(tracker.getStats().getSampleMaxCheckedOut(), CHECKED_OUT);
    // Samples the max pool size at POOL_SIZE + 10
    _poolSize = POOL_SIZE + 10;
    _checkedOut = CHECKED_OUT + 10;
    tracker.sampleMaxPoolSize();
    tracker.sampleMaxCheckedOut();
    clock.addDuration(SAMPLING_DURATION_INCREMENT);
    Assert.assertEquals(tracker.getStats().getSampleMaxCheckedOut(), CHECKED_OUT + 10);
    Assert.assertEquals(tracker.getStats().getSampleMaxPoolSize(), POOL_SIZE + 10);
}
Also used : LongTracking(com.linkedin.common.stats.LongTracking) SettableClock(com.linkedin.util.clock.SettableClock) AsyncPoolStatsTracker(com.linkedin.r2.transport.http.client.AsyncPoolStatsTracker) Test(org.testng.annotations.Test)

Example 9 with SettableClock

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

the class DegraderTrackerClientTest method testDoNotSlowStartWhenFalse.

@Test
public void testDoNotSlowStartWhenFalse() {
    Map<Integer, PartitionData> partitionDataMap = createDefaultPartitionData(1d);
    DegraderImpl.Config config = new DegraderImpl.Config();
    double initialDropRate = 0.99d;
    config.setInitialDropRate(initialDropRate);
    DegraderTrackerClient client = new DegraderTrackerClientImpl(URI.create("http://test.qa.com:1234/foo"), partitionDataMap, new TestClient(), new SettableClock(), config, DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS, TrackerClientImpl.DEFAULT_ERROR_STATUS_PATTERN, false);
    DegraderControl degraderControl = client.getDegraderControl(DefaultPartitionAccessor.DEFAULT_PARTITION_ID);
    Assert.assertEquals(degraderControl.getInitialDropRate(), initialDropRate, "Initial drop rate in config should not have been overridden by doNotSlowStart uri property.");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) DegraderLoadBalancerStrategyConfig(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyConfig) DegraderImpl(com.linkedin.util.degrader.DegraderImpl) DegraderControl(com.linkedin.util.degrader.DegraderControl) SettableClock(com.linkedin.util.clock.SettableClock) Test(org.testng.annotations.Test)

Example 10 with SettableClock

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

the class DegraderTrackerClientTest method testCallTrackingRestRequest.

@Test
public void testCallTrackingRestRequest() throws Exception {
    URI uri = URI.create("http://test.qa.com:1234/foo");
    SettableClock clock = new SettableClock();
    AtomicInteger action = new AtomicInteger(0);
    TransportClient tc = new TransportClient() {

        @Override
        public void restRequest(RestRequest request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<RestResponse> callback) {
            clock.addDuration(5);
            switch(action.get()) {
                // success
                case 0:
                    callback.onResponse(TransportResponseImpl.success(RestResponseFactory.noResponse()));
                    break;
                // fail with rest exception
                case 1:
                    callback.onResponse(TransportResponseImpl.error(RestException.forError(500, "rest exception")));
                    break;
                // fail with timeout exception
                case 2:
                    callback.onResponse(TransportResponseImpl.error(new RemoteInvocationException(new TimeoutException())));
                    break;
                // fail with other exception
                default:
                    callback.onResponse(TransportResponseImpl.error(new RuntimeException()));
                    break;
            }
        }

        @Override
        public void shutdown(Callback<None> callback) {
        }
    };
    DegraderTrackerClientImpl client = (DegraderTrackerClientImpl) createTrackerClient(tc, clock, uri);
    CallTracker callTracker = client.getCallTracker();
    CallTracker.CallStats stats;
    DegraderControl degraderControl = client.getDegraderControl(DefaultPartitionAccessor.DEFAULT_PARTITION_ID);
    client.restRequest(new RestRequestBuilder(uri).build(), new RequestContext(), new HashMap<>(), new TestTransportCallback<>());
    clock.addDuration(5000);
    stats = callTracker.getCallStats();
    Assert.assertEquals(stats.getCallCount(), 1);
    Assert.assertEquals(stats.getErrorCount(), 0);
    Assert.assertEquals(stats.getCallCountTotal(), 1);
    Assert.assertEquals(stats.getErrorCountTotal(), 0);
    Assert.assertEquals(degraderControl.getCurrentComputedDropRate(), 0.0, 0.001);
    action.set(1);
    client.restRequest(new RestRequestBuilder(uri).build(), new RequestContext(), new HashMap<>(), new TestTransportCallback<>());
    clock.addDuration(5000);
    stats = callTracker.getCallStats();
    Assert.assertEquals(stats.getCallCount(), 1);
    Assert.assertEquals(stats.getErrorCount(), 1);
    Assert.assertEquals(stats.getCallCountTotal(), 2);
    Assert.assertEquals(stats.getErrorCountTotal(), 1);
    Assert.assertEquals(degraderControl.getCurrentComputedDropRate(), 0.2, 0.001);
    action.set(2);
    client.restRequest(new RestRequestBuilder(uri).build(), new RequestContext(), new HashMap<>(), new TestTransportCallback<>());
    clock.addDuration(5000);
    stats = callTracker.getCallStats();
    Assert.assertEquals(stats.getCallCount(), 1);
    Assert.assertEquals(stats.getErrorCount(), 1);
    Assert.assertEquals(stats.getCallCountTotal(), 3);
    Assert.assertEquals(stats.getErrorCountTotal(), 2);
    Assert.assertEquals(degraderControl.getCurrentComputedDropRate(), 0.4, 0.001);
    action.set(3);
    client.restRequest(new RestRequestBuilder(uri).build(), new RequestContext(), new HashMap<>(), new TestTransportCallback<>());
    clock.addDuration(5000);
    stats = callTracker.getCallStats();
    Assert.assertEquals(stats.getCallCount(), 1);
    Assert.assertEquals(stats.getErrorCount(), 1);
    Assert.assertEquals(stats.getCallCountTotal(), 4);
    Assert.assertEquals(stats.getErrorCountTotal(), 3);
    Assert.assertEquals(degraderControl.getCurrentComputedDropRate(), 0.2, 0.001);
}
Also used : TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) DegraderControl(com.linkedin.util.degrader.DegraderControl) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SettableClock(com.linkedin.util.clock.SettableClock) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException) CallTracker(com.linkedin.util.degrader.CallTracker) Test(org.testng.annotations.Test)

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