Search in sources :

Example 16 with D2RelativeStrategyProperties

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

the class StateUpdaterTest method testExecutorSchedule.

@Test
public void testExecutorSchedule() throws InterruptedException {
    setup(new D2RelativeStrategyProperties(), new ConcurrentHashMap<>());
    List<TrackerClient> trackerClients = TrackerClientMockHelper.mockTrackerClients(2, Arrays.asList(20, 20), Arrays.asList(10, 10), Arrays.asList(200L, 200L), Arrays.asList(100L, 100L), Arrays.asList(0, 0));
    PartitionState existingState = new PartitionStateTestDataBuilder().setTrackerClientStateMap(trackerClients, Arrays.asList(1.0, 1.0), Arrays.asList(TrackerClientState.HealthState.HEALTHY, TrackerClientState.HealthState.HEALTHY), Arrays.asList(30, 30)).build();
    ConcurrentMap<Integer, PartitionState> stateMap = new ConcurrentHashMap<>();
    stateMap.put(DEFAULT_PARTITION_ID, existingState);
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    D2RelativeStrategyProperties relativeStrategyProperties = RelativeLoadBalancerStrategyFactory.putDefaultValues(new D2RelativeStrategyProperties());
    _stateUpdater = new StateUpdater(relativeStrategyProperties, _quarantineManager, executorService, stateMap, Collections.emptyList(), SERVICE_NAME);
    // In 6 seconds, the update should be executed twice
    CountDownLatch countDownLatch = new CountDownLatch(2);
    Mockito.doAnswer(new ExecutionCountDown<>(countDownLatch)).when(_quarantineManager).updateQuarantineState(any(), any(), anyLong());
    if (!countDownLatch.await(6, TimeUnit.SECONDS)) {
        fail("scheduled update failed to finish within 6 seconds");
    }
    Mockito.verify(_quarantineManager, Mockito.atLeast(2)).updateQuarantineState(any(), any(), anyLong());
    assertEquals(_stateUpdater.getPointsMap(DEFAULT_PARTITION_ID).get(trackerClients.get(0).getUri()).intValue(), HEALTHY_POINTS);
    assertEquals(_stateUpdater.getPointsMap(DEFAULT_PARTITION_ID).get(trackerClients.get(1).getUri()).intValue(), HEALTHY_POINTS);
    executorService.shutdown();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) Test(org.testng.annotations.Test)

Example 17 with D2RelativeStrategyProperties

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

the class StateUpdaterTest method testExecutorScheduleWithError.

@Test
public void testExecutorScheduleWithError() throws InterruptedException {
    D2RelativeStrategyProperties relativeStrategyProperties = new D2RelativeStrategyProperties().setInitialHealthScore(0.01).setUpdateIntervalMs(10);
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("D2 TestExecutor"));
    setup(relativeStrategyProperties, new ConcurrentHashMap<>(), executor);
    List<TrackerClient> trackerClients = TrackerClientMockHelper.mockTrackerClients(2, Arrays.asList(20, 20), Arrays.asList(10, 10), Arrays.asList(200L, 200L), Arrays.asList(100L, 100L), Arrays.asList(0, 0));
    _stateUpdater.updateState(new HashSet<>(trackerClients), 0, DEFAULT_CLUSTER_GENERATION_ID, false);
    Mockito.doThrow(new NullPointerException()).when(_quarantineManager).updateQuarantineState(Mockito.any(PartitionState.class), Mockito.any(PartitionState.class), anyLong());
    assertEquals(_stateUpdater.getPointsMap(0).get(trackerClients.get(0).getUri()).intValue(), 1);
    Thread.sleep(21);
    assertEquals(_stateUpdater.getPointsMap(0).get(trackerClients.get(0).getUri()).intValue(), 1, "The points did not change due to the failure in each interval execution");
    // Verify that the quarantine manager is invoked in each interval, so the tasks are not cancelled
    Mockito.verify(_quarantineManager, Mockito.atLeast(3)).updateQuarantineState(Mockito.any(PartitionState.class), Mockito.any(PartitionState.class), anyLong());
    executor.shutdown();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) Test(org.testng.annotations.Test)

Example 18 with D2RelativeStrategyProperties

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

the class StateUpdaterTest method testClusterGenerationIdChange.

// Known to be flaky in CI
@Test(retryAnalyzer = ThreeRetries.class)
public void testClusterGenerationIdChange() throws InterruptedException {
    PartitionState state = new PartitionStateTestDataBuilder().setClusterGenerationId(DEFAULT_CLUSTER_GENERATION_ID).build();
    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));
    ConcurrentMap<Integer, PartitionState> partitionLoadBalancerStateMap = new ConcurrentHashMap<>();
    partitionLoadBalancerStateMap.put(DEFAULT_PARTITION_ID, state);
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    D2RelativeStrategyProperties relativeStrategyProperties = RelativeLoadBalancerStrategyFactory.putDefaultValues(new D2RelativeStrategyProperties());
    _stateUpdater = new StateUpdater(relativeStrategyProperties, _quarantineManager, executorService, partitionLoadBalancerStateMap, Collections.emptyList(), SERVICE_NAME);
    // update will be scheduled twice, once from interval update, once from cluster change
    CountDownLatch countDownLatch = new CountDownLatch(2);
    Mockito.doAnswer(new ExecutionCountDown<>(countDownLatch)).when(_quarantineManager).updateQuarantineState(any(), any(), anyLong());
    // Cluster generation id changed from 0 to 1
    _stateUpdater.updateState(new HashSet<>(trackerClients), DEFAULT_PARTITION_ID, 1, false);
    if (!countDownLatch.await(5, TimeUnit.SECONDS)) {
        fail("cluster update failed to finish within 5 seconds");
    }
    assertEquals(_stateUpdater.getPointsMap(DEFAULT_PARTITION_ID).size(), 2);
    executorService.shutdown();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) Test(org.testng.annotations.Test)

Example 19 with D2RelativeStrategyProperties

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

the class StateUpdaterTest method testForceUpdate.

// Known to be flaky in CI
@Test(retryAnalyzer = ThreeRetries.class)
public void testForceUpdate() throws InterruptedException {
    PartitionState state = new PartitionStateTestDataBuilder().setClusterGenerationId(DEFAULT_CLUSTER_GENERATION_ID).build();
    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));
    ConcurrentMap<Integer, PartitionState> partitionLoadBalancerStateMap = new ConcurrentHashMap<>();
    partitionLoadBalancerStateMap.put(DEFAULT_PARTITION_ID, state);
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    D2RelativeStrategyProperties relativeStrategyProperties = RelativeLoadBalancerStrategyFactory.putDefaultValues(new D2RelativeStrategyProperties());
    _stateUpdater = new StateUpdater(relativeStrategyProperties, _quarantineManager, executorService, partitionLoadBalancerStateMap, Collections.emptyList(), SERVICE_NAME);
    // update will be scheduled three times, once from interval update, once from cluster change, once from force update
    CountDownLatch countDownLatch = new CountDownLatch(3);
    Mockito.doAnswer(new ExecutionCountDown<>(countDownLatch)).when(_quarantineManager).updateQuarantineState(any(), any(), anyLong());
    // Cluster generation id changed from 0 to 1
    _stateUpdater.updateState(new HashSet<>(trackerClients), DEFAULT_PARTITION_ID, 1, false);
    _stateUpdater.updateState(new HashSet<>(trackerClients), DEFAULT_PARTITION_ID, 1, true);
    if (!countDownLatch.await(5, TimeUnit.SECONDS)) {
        fail("cluster update failed to finish within 5 seconds");
    }
    assertEquals(_stateUpdater.getPointsMap(DEFAULT_PARTITION_ID).size(), 2);
    executorService.shutdown();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) Test(org.testng.annotations.Test)

Example 20 with D2RelativeStrategyProperties

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

the class LoadBalancerStrategyBenchmark method buildRelativeLoadBalancerStrategy.

private static RelativeLoadBalancerStrategy buildRelativeLoadBalancerStrategy() {
    D2RelativeStrategyProperties relativeStrategyProperties = new D2RelativeStrategyProperties();
    ServiceProperties serviceProperties = new ServiceProperties(DUMMY_SERVICE_NAME, DEFAULT_CLUSTER_NAME, DEFAULT_PATH, DEFAULT_STRATEGY_LIST, null, null, null, null, null, null, null, RelativeStrategyPropertiesConverter.toMap(relativeStrategyProperties));
    return new RelativeLoadBalancerStrategyFactory(EXECUTOR_SERVICE, null, new ArrayList<>(), null, SystemClock.instance()).newLoadBalancer(serviceProperties);
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) RelativeLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategyFactory) ArrayList(java.util.ArrayList) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties)

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