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();
}
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();
}
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();
}
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();
}
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);
}
Aggregations