Search in sources :

Example 21 with D2RelativeStrategyProperties

use of com.linkedin.d2.D2RelativeStrategyProperties in project rest.li by linkedin.

the class StateUpdaterTest method testInitializePartition.

@Test(dataProvider = "partitionId")
public void testInitializePartition(int partitionId) {
    setup(new D2RelativeStrategyProperties(), new ConcurrentHashMap<>());
    List<TrackerClient> trackerClients = TrackerClientMockHelper.mockTrackerClients(2, Arrays.asList(20, 20), Arrays.asList(10, 10), Arrays.asList(200L, 500L), Arrays.asList(100L, 200L), Arrays.asList(0, 0));
    assertTrue(_stateUpdater.getPointsMap(partitionId).isEmpty(), "There should be no state before initialization");
    _stateUpdater.updateState(new HashSet<>(trackerClients), partitionId, DEFAULT_CLUSTER_GENERATION_ID, false);
    assertEquals(_stateUpdater.getPointsMap(partitionId).get(trackerClients.get(0).getUri()).intValue(), HEALTHY_POINTS);
    assertEquals(_stateUpdater.getPointsMap(partitionId).get(trackerClients.get(1).getUri()).intValue(), HEALTHY_POINTS);
    assertEquals(_stateUpdater.getFirstValidPartitionId(), partitionId);
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) Test(org.testng.annotations.Test)

Example 22 with D2RelativeStrategyProperties

use of com.linkedin.d2.D2RelativeStrategyProperties in project rest.li by linkedin.

the class StateUpdaterTest method testHealthScoreDrop.

@Test(dataProvider = "trackerClients")
public void testHealthScoreDrop(List<TrackerClient> trackerClients, double highLatencyFactor, double highErrorRate, boolean expectToDropHealthScore) {
    PartitionState state = new PartitionStateTestDataBuilder().setClusterGenerationId(DEFAULT_CLUSTER_GENERATION_ID).setTrackerClientStateMap(trackerClients, Arrays.asList(StateUpdater.MAX_HEALTH_SCORE, StateUpdater.MAX_HEALTH_SCORE, StateUpdater.MAX_HEALTH_SCORE), Arrays.asList(TrackerClientState.HealthState.HEALTHY, TrackerClientState.HealthState.HEALTHY, TrackerClientState.HealthState.HEALTHY), Arrays.asList(30, 30, 30)).build();
    ConcurrentMap<Integer, PartitionState> partitionLoadBalancerStateMap = new ConcurrentHashMap<>();
    partitionLoadBalancerStateMap.put(DEFAULT_PARTITION_ID, state);
    setup(new D2RelativeStrategyProperties().setRelativeLatencyHighThresholdFactor(highLatencyFactor).setHighErrorRate(highErrorRate), partitionLoadBalancerStateMap);
    _stateUpdater.updateState();
    Map<URI, Integer> pointsMap = _stateUpdater.getPointsMap(DEFAULT_PARTITION_ID);
    if (!expectToDropHealthScore) {
        assertEquals(pointsMap.get(trackerClients.get(0).getUri()).intValue(), HEALTHY_POINTS);
        assertEquals(pointsMap.get(trackerClients.get(1).getUri()).intValue(), HEALTHY_POINTS);
        assertEquals(pointsMap.get(trackerClients.get(2).getUri()).intValue(), HEALTHY_POINTS);
    } else {
        assertEquals(pointsMap.get(trackerClients.get(0).getUri()).intValue(), (int) (HEALTHY_POINTS - RelativeLoadBalancerStrategyFactory.DEFAULT_DOWN_STEP * RelativeLoadBalancerStrategyFactory.DEFAULT_POINTS_PER_WEIGHT));
        assertEquals(pointsMap.get(trackerClients.get(1).getUri()).intValue(), HEALTHY_POINTS);
        assertEquals(pointsMap.get(trackerClients.get(2).getUri()).intValue(), HEALTHY_POINTS);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URI(java.net.URI) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) Test(org.testng.annotations.Test)

Example 23 with D2RelativeStrategyProperties

use of com.linkedin.d2.D2RelativeStrategyProperties in project rest.li by linkedin.

the class StateUpdaterTest method testClusterUrisChange.

@Test
public void testClusterUrisChange() {
    List<TrackerClient> trackerClients = TrackerClientMockHelper.mockTrackerClients(3, Arrays.asList(20, 20, 20), Arrays.asList(10, 10, 10), Arrays.asList(200L, 220L, 1000L), Arrays.asList(100L, 110L, 500L), Arrays.asList(0, 0, 0));
    PartitionState state = new PartitionStateTestDataBuilder().setClusterGenerationId(DEFAULT_CLUSTER_GENERATION_ID).setTrackerClientStateMap(trackerClients, Arrays.asList(StateUpdater.MAX_HEALTH_SCORE, StateUpdater.MAX_HEALTH_SCORE, StateUpdater.MAX_HEALTH_SCORE), Arrays.asList(TrackerClientState.HealthState.HEALTHY, TrackerClientState.HealthState.HEALTHY, TrackerClientState.HealthState.HEALTHY), Arrays.asList(30, 30, 30)).build();
    ConcurrentMap<Integer, PartitionState> partitionLoadBalancerStateMap = new ConcurrentHashMap<>();
    partitionLoadBalancerStateMap.put(DEFAULT_PARTITION_ID, state);
    setup(new D2RelativeStrategyProperties(), partitionLoadBalancerStateMap);
    // New tracker clients set only contains 2 out of 3 tracker clients from the old state
    Set<TrackerClient> newTrackerClientSet = new HashSet<>();
    newTrackerClientSet.add(trackerClients.get(0));
    newTrackerClientSet.add(trackerClients.get(1));
    _stateUpdater.updateStateForPartition(newTrackerClientSet, DEFAULT_PARTITION_ID, state, 1L, false);
    Map<URI, Integer> pointsMap = _stateUpdater.getPointsMap(DEFAULT_PARTITION_ID);
    assertEquals(pointsMap.size(), 2, "There should only be 2 uris after cluster id change");
    assertEquals(pointsMap.get(trackerClients.get(0).getUri()).intValue(), HEALTHY_POINTS);
    assertEquals(pointsMap.get(trackerClients.get(1).getUri()).intValue(), HEALTHY_POINTS);
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URI(java.net.URI) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 24 with D2RelativeStrategyProperties

use of com.linkedin.d2.D2RelativeStrategyProperties in project rest.li by linkedin.

the class LoadBalancerStrategyTestRunnerBuilder method buildRelativeStrategy.

private LoadBalancerStrategyTestRunner buildRelativeStrategy() {
    if (_serviceProperties == null) {
        setRelativeLoadBalancerStrategies(new D2RelativeStrategyProperties());
    }
    _strategy = new RelativeLoadBalancerStrategyFactory(_clockedExecutor, null, new ArrayList<>(), null, _clockedExecutor).newLoadBalancer(_serviceProperties);
    _transportClients = _uris.stream().map(uri -> new MockTransportClient(_clockedExecutor, _latencyManager, _errorCountManager, uri, INTERVAL_IN_MILLIS, _currentErrorCountMap, _lastRequestCountMap, _callCountMap, _latencySumMap)).collect(Collectors.toList());
    Map<URI, TrackerClient> trackerClientMap = _transportClients.stream().map(transportClient -> {
        // If partition map is not specified, by default we only support one partition
        Map<Integer, PartitionData> partitionDataMap = _partitionDataMap.getOrDefault(transportClient.getUri(), DEFAULT_PARTITION_DATA_MAP);
        return new TrackerClientImpl(transportClient.getUri(), partitionDataMap, transportClient, _clockedExecutor, INTERVAL_IN_MILLIS, (status) -> status >= 500 && status <= 599);
    }).collect(Collectors.toMap(TrackerClient::getUri, trackerClient -> trackerClient));
    return buildInternal(trackerClientMap);
}
Also used : Arrays(java.util.Arrays) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) Callback(com.linkedin.common.callback.Callback) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) HashMap(java.util.HashMap) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestResponse(com.linkedin.r2.message.rest.RestResponse) ArrayList(java.util.ArrayList) ClockedExecutor(com.linkedin.test.util.ClockedExecutor) Map(java.util.Map) DegraderConfigFactory(com.linkedin.d2.balancer.strategies.degrader.DegraderConfigFactory) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) URI(java.net.URI) HttpStatus(org.eclipse.jetty.http.HttpStatus) RelativeLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategyFactory) RestException(com.linkedin.r2.message.rest.RestException) RestRequest(com.linkedin.r2.message.rest.RestRequest) com.linkedin.d2.loadBalancerStrategyType(com.linkedin.d2.loadBalancerStrategyType) Set(java.util.Set) DegraderTrackerClientImpl(com.linkedin.d2.balancer.clients.DegraderTrackerClientImpl) TrackerClientImpl(com.linkedin.d2.balancer.clients.TrackerClientImpl) PropertyKeys(com.linkedin.d2.balancer.properties.PropertyKeys) Collectors(java.util.stream.Collectors) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) TransportResponseImpl(com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl) RequestContext(com.linkedin.r2.message.RequestContext) None(com.linkedin.common.util.None) RelativeStrategyPropertiesConverter(com.linkedin.d2.balancer.config.RelativeStrategyPropertiesConverter) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) RelativeLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategyFactory) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map) DegraderTrackerClientImpl(com.linkedin.d2.balancer.clients.DegraderTrackerClientImpl) TrackerClientImpl(com.linkedin.d2.balancer.clients.TrackerClientImpl) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties)

Example 25 with D2RelativeStrategyProperties

use of com.linkedin.d2.D2RelativeStrategyProperties in project rest.li by linkedin.

the class StateUpdaterTest method testUpdateTrackerClientWithDoNotLoadBalance.

@Test
public void testUpdateTrackerClientWithDoNotLoadBalance() {
    final boolean doNotLoadBalance = true;
    List<TrackerClient> trackerClients = TrackerClientMockHelper.mockTrackerClients(3, Arrays.asList(20, 1, 20), Arrays.asList(0, 0, 0), Arrays.asList(1000L, 5000L, 100000L), Arrays.asList(0L, 0L, 0L), Arrays.asList(0, 0, 10), Arrays.asList(false, false, doNotLoadBalance));
    PartitionState state = new PartitionStateTestDataBuilder().setClusterGenerationId(DEFAULT_CLUSTER_GENERATION_ID).setTrackerClientStateMap(trackerClients, Arrays.asList(1.0, 1.0, 1.0), Arrays.asList(TrackerClientState.HealthState.HEALTHY, TrackerClientState.HealthState.HEALTHY, TrackerClientState.HealthState.HEALTHY), Arrays.asList(30, 30, 30)).build();
    ConcurrentMap<Integer, PartitionState> partitionLoadBalancerStateMap = new ConcurrentHashMap<>();
    partitionLoadBalancerStateMap.put(DEFAULT_PARTITION_ID, state);
    setup(new D2RelativeStrategyProperties(), partitionLoadBalancerStateMap);
    _stateUpdater.updateState();
    Map<URI, Integer> pointsMap = _stateUpdater.getPointsMap(DEFAULT_PARTITION_ID);
    assertEquals(pointsMap.get(trackerClients.get(0).getUri()).intValue(), HEALTHY_POINTS, "Healthy client should not have health score reduced.");
    assertEquals(pointsMap.get(trackerClients.get(1).getUri()).intValue(), (int) (HEALTHY_POINTS - RelativeLoadBalancerStrategyFactory.DEFAULT_DOWN_STEP * RelativeLoadBalancerStrategyFactory.DEFAULT_POINTS_PER_WEIGHT), "This should be considered unhealthy because its latency exceeds the threshold " + "(the client with load balancing disabled should not affect the average latency calculation).");
    assertEquals(pointsMap.get(trackerClients.get(2).getUri()).intValue(), HEALTHY_POINTS, "The client with load balancing disabled " + "should not have health score reduced.");
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URI(java.net.URI) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) Test(org.testng.annotations.Test)

Aggregations

D2RelativeStrategyProperties (com.linkedin.d2.D2RelativeStrategyProperties)37 Test (org.testng.annotations.Test)27 LoadBalancerStrategyTestRunnerBuilder (com.linkedin.d2.balancer.strategies.framework.LoadBalancerStrategyTestRunnerBuilder)17 HashMap (java.util.HashMap)15 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)13 LoadBalancerStrategyTestRunner (com.linkedin.d2.balancer.strategies.framework.LoadBalancerStrategyTestRunner)12 URI (java.net.URI)11 ArrayList (java.util.ArrayList)9 D2QuarantineProperties (com.linkedin.d2.D2QuarantineProperties)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 HttpStatusCodeRange (com.linkedin.d2.HttpStatusCodeRange)5 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)5 D2RingProperties (com.linkedin.d2.D2RingProperties)4 HttpStatusCodeRangeArray (com.linkedin.d2.HttpStatusCodeRangeArray)4 LatencyCorrelation (com.linkedin.d2.balancer.strategies.framework.LatencyCorrelation)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Map (java.util.Map)3 ConsistentHashAlgorithm (com.linkedin.d2.ConsistentHashAlgorithm)2 HashConfig (com.linkedin.d2.HashConfig)2 HashMethod (com.linkedin.d2.HashMethod)2