Search in sources :

Example 91 with TrackerClient

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

the class DegraderLoadBalancerTest method getTrackerClientMetrics.

private static Map<TrackerClient, TrackerClientMetrics> getTrackerClientMetrics(List<TrackerClient> clients) {
    Map<TrackerClient, TrackerClientMetrics> map = new HashMap<TrackerClient, TrackerClientMetrics>();
    for (TrackerClient client : clients) {
        DegraderControl degraderControl = client.getDegraderControl(DEFAULT_PARTITION_ID);
        map.put(client, new TrackerClientMetrics(degraderControl.getOverrideDropRate(), degraderControl.getMaxDropRate(), degraderControl.getOverrideMinCallCount()));
    }
    return map;
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) HashMap(java.util.HashMap) DegraderControl(com.linkedin.util.degrader.DegraderControl)

Example 92 with TrackerClient

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

the class DegraderLoadBalancerTest method clusterRecovery1TC.

/**
   * helper method to test DegraderLoadBalancerStrategy recovery with 1 TrackerClient.
   *
   * We want to test DegraderV2 and V3 with 2 different strategies : LoadBalacing and Call Dropping.
   * So this method needs to able to handle all 4 permutations.
   *
   * @param myMap
   * @param clock
   * @param stepsToFullRecovery
   * @param timeInterval
   * @param strategy
   */
public void clusterRecovery1TC(Map<String, Object> myMap, TestClock clock, int stepsToFullRecovery, Long timeInterval, DegraderLoadBalancerStrategyAdapter strategy, DegraderLoadBalancerStrategyV2_1.DegraderLoadBalancerState.Strategy strategyV2, DegraderLoadBalancerStrategyV3.PartitionDegraderLoadBalancerState.Strategy strategyV3) {
    final int NUM_CHECKS = 5;
    final Long TIME_INTERVAL = timeInterval;
    int localStepsToFullRecovery = stepsToFullRecovery;
    DegraderLoadBalancerStrategyConfig config = DegraderLoadBalancerStrategyConfig.createHttpConfigFromMap(myMap);
    List<TrackerClient> clients = new ArrayList<TrackerClient>();
    URI uri1 = URI.create("http://test.linkedin.com:3242/fdsaf");
    URIRequest request = new URIRequest(uri1);
    TrackerClient client1 = new TrackerClient(uri1, getDefaultPartitionData(1d), new TestLoadBalancerClient(uri1), clock, null);
    clients.add(client1);
    // force client1 to be disabled
    DegraderControl dcClient1Default = client1.getDegraderControl(DEFAULT_PARTITION_ID);
    dcClient1Default.setOverrideMinCallCount(5);
    dcClient1Default.setMinCallCount(5);
    dcClient1Default.setMaxDropRate(1d);
    dcClient1Default.setUpStep(1.0d);
    List<CallCompletion> ccList = new ArrayList<CallCompletion>();
    CallCompletion cc;
    for (int j = 0; j < NUM_CHECKS; j++) {
        cc = client1.getCallTracker().startCall();
        ccList.add(cc);
    }
    // add high latency and errors to shut off traffic to this tracker client.
    // note: the default values for highError and lowError in the degrader are 1.1,
    // which means we don't use errorRates when deciding when to lb/degrade.
    // In addition, because we changed to use the
    clock.addMs(3500);
    //for (int j = 0; j < NUM_CHECKS; j++)
    for (Iterator<CallCompletion> iter = ccList.listIterator(); iter.hasNext(); ) {
        cc = iter.next();
        cc.endCallWithError();
        iter.remove();
    }
    // go to next time interval.
    clock.addMs(TIME_INTERVAL);
    Assert.assertEquals(dcClient1Default.getCurrentComputedDropRate(), 1.0);
    // trigger a state update
    TrackerClient resultTC = getTrackerClient(strategy, request, new RequestContext(), 1, clients);
    if (config.getInitialRecoveryLevel() < 0.01) {
        //the returned TrackerClient should be null
        assertNull(resultTC, "expected null trackerclient");
        // tracker client, so it's time to try it out. We need to enter this code at least once.
        do {
            // go to next time interval.
            clock.addMs(TIME_INTERVAL);
            // try adjusting the hash ring on this updateState
            if (strategyV3 != null) {
                strategy.setStrategyV3(DEFAULT_PARTITION_ID, strategyV3);
            } else if (strategyV2 != null) {
                strategy.setStrategyV2(strategyV2);
            } else {
                fail("should set strategy (either LoadBalance or Degrader");
            }
            resultTC = getTrackerClient(strategy, request, new RequestContext(), 1, clients);
            localStepsToFullRecovery--;
        } while (localStepsToFullRecovery > 0);
    }
    assertNotNull(resultTC, "expected non-null trackerclient");
    // make calls to the tracker client to verify that it's on the road to healthy status.
    for (int j = 0; j < NUM_CHECKS; j++) {
        cc = resultTC.getCallTracker().startCall();
        ccList.add(cc);
    }
    clock.addMs(10);
    for (Iterator<CallCompletion> iter = ccList.listIterator(); iter.hasNext(); ) {
        cc = iter.next();
        cc.endCall();
        iter.remove();
    }
    // go to next time interval.
    clock.addMs(TIME_INTERVAL);
    Assert.assertTrue(dcClient1Default.getCurrentComputedDropRate() < 1d);
    resultTC = getTrackerClient(strategy, request, new RequestContext(), 1, clients);
    assertNotNull(resultTC, "expected non-null trackerclient");
}
Also used : ArrayList(java.util.ArrayList) URIRequest(com.linkedin.d2.balancer.util.URIRequest) DegraderControl(com.linkedin.util.degrader.DegraderControl) URI(java.net.URI) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) CallCompletion(com.linkedin.util.degrader.CallCompletion) AtomicLong(java.util.concurrent.atomic.AtomicLong) RequestContext(com.linkedin.r2.message.RequestContext)

Example 93 with TrackerClient

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

the class LoadBalancerStrategyBenchmark method createDegraderTrackerClients.

private static Map<URI, TrackerClient> createDegraderTrackerClients(int numHosts) {
    Map<URI, TrackerClient> trackerClients = new HashMap<>();
    for (int i = 0; i < numHosts; i++) {
        URI uri = URI.create(URI_PREFIX + i + URI_SUFFIX);
        trackerClients.put(uri, new DegraderTrackerClientImpl(uri, DEFAULT_PARTITION_DATA_MAP, new BaseTransportTestClient(), CLOCK, null));
    }
    return trackerClients;
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) HashMap(java.util.HashMap) DegraderTrackerClientImpl(com.linkedin.d2.balancer.clients.DegraderTrackerClientImpl) URI(java.net.URI)

Example 94 with TrackerClient

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

the class TrackerClientMockHelper method mockTrackerClients.

/**
 * Mock a list of {@link TrackerClient} for testing
 *
 * @param numTrackerClients The number of {@link TrackerClient} to be mocked
 * @param callCountList The call count that each host receives
 * @param outstandingCallCountList The outstanding call count that each host receives
 * @param latencyList The latency of each host
 * @param outstandingLatencyList The outstanding latency of each host
 * @param errorCountList The error count of each host
 * @return A list of mocked {@link TrackerClient}
 */
public static List<TrackerClient> mockTrackerClients(int numTrackerClients, List<Integer> callCountList, List<Integer> outstandingCallCountList, List<Long> latencyList, List<Long> outstandingLatencyList, List<Integer> errorCountList, boolean doNotSlowStart, List<Boolean> doNotLoadBalance) {
    List<TrackerClient> trackerClients = new ArrayList<>();
    for (int index = 0; index < numTrackerClients; index++) {
        URI uri = URI.create("URI/" + index);
        TrackerClient trackerClient = Mockito.mock(TrackerClient.class);
        CallTracker callTracker = Mockito.mock(CallTracker.class);
        LongStats longStats = new LongStats(callCountList.get(index), latencyList.get(index), 0, 0, 0, 0, 0, 0, 0);
        Map<ErrorType, Integer> errorTypeCounts = new HashMap<>();
        errorTypeCounts.put(ErrorType.SERVER_ERROR, errorCountList.get(index));
        CallTrackerImpl.CallTrackerStats callStats = new CallTrackerImpl.CallTrackerStats(RelativeLoadBalancerStrategyFactory.DEFAULT_UPDATE_INTERVAL_MS, 0, RelativeLoadBalancerStrategyFactory.DEFAULT_UPDATE_INTERVAL_MS, callCountList.get(index), 0, 0, errorCountList.get(index), errorCountList.get(index), 1, RelativeLoadBalancerStrategyFactory.DEFAULT_UPDATE_INTERVAL_MS - outstandingLatencyList.get(index), outstandingCallCountList.get(index), longStats, errorTypeCounts, errorTypeCounts);
        Mockito.when(trackerClient.getCallTracker()).thenReturn(callTracker);
        Mockito.when(callTracker.getCallStats()).thenReturn(callStats);
        Mockito.when(trackerClient.getUri()).thenReturn(uri);
        Mockito.when(trackerClient.getPartitionWeight(anyInt())).thenReturn(1.0);
        Mockito.when(trackerClient.getSubsetWeight(anyInt())).thenReturn(1.0);
        Mockito.when(trackerClient.doNotSlowStart()).thenReturn(doNotSlowStart);
        Mockito.when(trackerClient.doNotLoadBalance()).thenReturn(doNotLoadBalance.get(index));
        trackerClients.add(trackerClient);
    }
    return trackerClients;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LongStats(com.linkedin.common.stats.LongStats) URI(java.net.URI) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) ErrorType(com.linkedin.util.degrader.ErrorType) CallTrackerImpl(com.linkedin.util.degrader.CallTrackerImpl) CallTracker(com.linkedin.util.degrader.CallTracker)

Example 95 with TrackerClient

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

the class TrackerClientMockHelper method mockTrackerClients.

/**
 * Mock a list of {@link TrackerClient} without call stats
 *
 * @param numTrackerClients The number of hosts to be mocked
 * @return A list of mocked {@link TrackerClient}
 */
public static List<TrackerClient> mockTrackerClients(int numTrackerClients) {
    List<TrackerClient> trackerClients = new ArrayList<>();
    for (int index = 0; index < numTrackerClients; index++) {
        URI uri = URI.create("URI/" + index);
        TrackerClient trackerClient = Mockito.mock(TrackerClient.class);
        Mockito.when(trackerClient.getCallTracker()).thenReturn(new CallTrackerImpl(RelativeLoadBalancerStrategyFactory.DEFAULT_UPDATE_INTERVAL_MS));
        Mockito.when(trackerClient.getUri()).thenReturn(uri);
        Mockito.when(trackerClient.getPartitionWeight(anyInt())).thenReturn(1.0);
        Mockito.when(trackerClient.getSubsetWeight(anyInt())).thenReturn(1.0);
        trackerClients.add(trackerClient);
    }
    return trackerClients;
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) CallTrackerImpl(com.linkedin.util.degrader.CallTrackerImpl) ArrayList(java.util.ArrayList) URI(java.net.URI)

Aggregations

TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)117 URI (java.net.URI)83 Test (org.testng.annotations.Test)58 HashMap (java.util.HashMap)50 ArrayList (java.util.ArrayList)49 RequestContext (com.linkedin.r2.message.RequestContext)39 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)30 DegraderTrackerClient (com.linkedin.d2.balancer.clients.DegraderTrackerClient)26 Map (java.util.Map)25 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)22 DegraderTrackerClientImpl (com.linkedin.d2.balancer.clients.DegraderTrackerClientImpl)18 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)18 D2RelativeStrategyProperties (com.linkedin.d2.D2RelativeStrategyProperties)17 DegraderTrackerClientTest (com.linkedin.d2.balancer.clients.DegraderTrackerClientTest)17 NullStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback)15 DegraderLoadBalancerTest (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)14 URIRequest (com.linkedin.d2.balancer.util.URIRequest)14 DegraderControl (com.linkedin.util.degrader.DegraderControl)14 HashSet (java.util.HashSet)13 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)12