use of com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor.DEFAULT_PARTITION_ID in project rest.li by linkedin.
the class DegraderLoadBalancerTest method testDegraderLoadBalancerHandlingExceptionInUpdate.
@Test(groups = { "small", "back-end" }, retryAnalyzer = SingleRetry.class)
public void testDegraderLoadBalancerHandlingExceptionInUpdate() {
Map<String, Object> myMap = lbDefaultConfig();
Long timeInterval = 5000L;
TestClock clock = new TestClock();
myMap.put(PropertyKeys.CLOCK, clock);
myMap.put(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_UPDATE_INTERVAL_MS, timeInterval);
Map<String, String> degraderProperties = degraderDefaultConfig();
degraderProperties.put(PropertyKeys.DEGRADER_HIGH_ERROR_RATE, "0.5");
degraderProperties.put(PropertyKeys.DEGRADER_LOW_ERROR_RATE, "0.2");
DegraderImpl.Config degraderConfig = DegraderConfigFactory.toDegraderConfig(degraderProperties);
final List<DegraderTrackerClient> clients = createTrackerClient(3, clock, degraderConfig);
DegraderLoadBalancerStrategyConfig unbrokenConfig = DegraderLoadBalancerStrategyConfig.createHttpConfigFromMap(myMap);
DegraderLoadBalancerStrategyConfig brokenConfig = new MockDegraderLoadBalancerStrategyConfig(unbrokenConfig);
URI uri4 = URI.create("http://test.linkedin.com:10010/abc4");
// this client will throw exception when getDegraderControl is called hence triggering a failed state update
BrokenTrackerClient brokenClient = new BrokenTrackerClient(uri4, getDefaultPartitionData(1d), new TestLoadBalancerClient(uri4), clock, null);
clients.add(brokenClient);
// test DegraderLoadBalancerStrategyV3 when the strategy is LOAD_BALANCE
DegraderLoadBalancerStrategyV3 strategyV3 = new DegraderLoadBalancerStrategyV3(brokenConfig, "testStrategyV3", null, DEGRADER_STATE_LISTENER_FACTORIES);
DegraderLoadBalancerStrategyAdapter strategyAdapterV3 = new DegraderLoadBalancerStrategyAdapter(strategyV3);
// simulate 100 threads trying to get client at the same time. Make sure that they won't be blocked if an exception
// occurs during updateState()
runMultiThreadedTest(strategyAdapterV3, clients, 100, true);
PartitionDegraderLoadBalancerState stateV3 = strategyV3.getState().getPartitionState(0);
// only one exception would occur and other thread would succeed in initializing immediately after
assertTrue(stateV3.isInitialized());
assertEquals(stateV3.getStrategy(), PartitionDegraderLoadBalancerState.Strategy.CALL_DROPPING);
brokenClient.reset();
// test DegraderLoadBalancerStrategy when the strategy is CALL_DROPPING. We have to make some prepare the
// environment by simulating lots of high latency calls to the tracker client
int numberOfCallsPerClient = 10;
List<CallCompletion> callCompletions = new ArrayList<>();
for (DegraderTrackerClient client : clients) {
for (int i = 0; i < numberOfCallsPerClient; i++) {
callCompletions.add(client.getCallTracker().startCall());
}
}
clock.addMs(brokenConfig.getUpdateIntervalMs() - 1000);
for (CallCompletion cc : callCompletions) {
for (int i = 0; i < numberOfCallsPerClient; i++) {
cc.endCall();
}
}
clock.addMs(1000);
Map<DegraderTrackerClient, TrackerClientMetrics> beforeStateUpdate = getTrackerClientMetrics(clients);
// no side-effects on tracker clients when update fails
Map<DegraderTrackerClient, TrackerClientMetrics> afterFailedV2StateUpdate = getTrackerClientMetrics(clients);
for (DegraderTrackerClient client : clients) {
assertEquals(beforeStateUpdate.get(client), afterFailedV2StateUpdate.get(client));
}
runMultiThreadedTest(strategyAdapterV3, clients, 100, true);
stateV3 = strategyV3.getState().getPartitionState(0);
// no side-effects on state when update fails
assertEquals(stateV3.getStrategy(), PartitionDegraderLoadBalancerState.Strategy.CALL_DROPPING);
// no side-effects on tracker clients when update fails
Map<DegraderTrackerClient, TrackerClientMetrics> afterFailedV3StateUpdate = getTrackerClientMetrics(clients);
for (DegraderTrackerClient client : clients) {
assertEquals(beforeStateUpdate.get(client), afterFailedV3StateUpdate.get(client));
}
brokenClient.reset();
// reset metrics on tracker client's degrader control
for (DegraderTrackerClient client : clients) {
TrackerClientMetrics originalMetrics = beforeStateUpdate.get(client);
DegraderControl degraderControl = client.getDegraderControl(DEFAULT_PARTITION_ID);
degraderControl.setOverrideDropRate(originalMetrics._overrideDropRate);
degraderControl.setMaxDropRate(originalMetrics._maxDropRate);
degraderControl.setOverrideMinCallCount(originalMetrics._overrideMinCallCount);
}
callCompletions.clear();
for (DegraderTrackerClient client : clients) {
for (int i = 0; i < numberOfCallsPerClient; i++) {
callCompletions.add(client.getCallTracker().startCall());
}
}
clock.addMs(brokenConfig.getUpdateIntervalMs() - 1000);
for (CallCompletion cc : callCompletions) {
for (int i = 0; i < numberOfCallsPerClient; i++) {
cc.endCall();
}
}
clock.addMs(1000);
strategyV3.setConfig(unbrokenConfig);
beforeStateUpdate = getTrackerClientMetrics(clients);
runMultiThreadedTest(strategyAdapterV3, clients, 100, false);
stateV3 = strategyV3.getState().getPartitionState(0);
// This time update should succeed, and both state and trackerclients are updated
Map<DegraderTrackerClient, TrackerClientMetrics> afterV3StateUpdate = getTrackerClientMetrics(clients);
for (DegraderTrackerClient client : clients) {
assertNotEquals(beforeStateUpdate.get(client), afterV3StateUpdate.get(client));
}
assertEquals(stateV3.getStrategy(), PartitionDegraderLoadBalancerState.Strategy.LOAD_BALANCE);
}
use of com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor.DEFAULT_PARTITION_ID in project rest.li by linkedin.
the class DegraderLoadBalancerTest method testWeightedBalancingWithDeadClient.
@Test(groups = { "small", "back-end" })
public void testWeightedBalancingWithDeadClient() throws URISyntaxException {
Map<String, Object> myMap = lbDefaultConfig();
myMap.put(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_UPDATE_INTERVAL_MS, 5000L);
myMap.put(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_MAX_CLUSTER_LATENCY_WITHOUT_DEGRADING, 100.0);
// this test expected the dead tracker client to not recover through the
// getTrackerClient mechanism. It only recovered through explicit calls to client1/client2.
// While we have fixed this problem, keeping this testcase to show how we can completely disable
// a tracker client through the getTrackerClient method.
myMap.put(PropertyKeys.HTTP_LB_INITIAL_RECOVERY_LEVEL, 0.0);
DegraderLoadBalancerStrategyV3 strategy = getStrategy(myMap);
List<DegraderTrackerClient> clients = new ArrayList<>();
URI uri1 = URI.create("http://test.linkedin.com:3242/fdsaf");
URI uri2 = URI.create("http://test.linkedin.com:3243/fdsaf");
TestClock clock1 = new TestClock();
TestClock clock2 = new TestClock();
DegraderTrackerClient client1 = getClient(uri1, clock1);
DegraderTrackerClient client2 = getClient(uri2, clock2);
clients.add(client1);
clients.add(client2);
// force client2 to be disabled
DegraderControl dcClient2Default = client2.getDegraderControl(DEFAULT_PARTITION_ID);
dcClient2Default.setMinCallCount(1);
dcClient2Default.setOverrideMinCallCount(1);
dcClient2Default.setMaxDropRate(1d);
dcClient2Default.setUpStep(1d);
dcClient2Default.setHighErrorRate(0);
CallCompletion cc = client2.getCallTracker().startCall();
clock2.addMs(10000);
cc.endCallWithError();
clock1.addMs(15000);
clock2.addMs(5000);
System.err.println(dcClient2Default.getCurrentComputedDropRate());
System.err.println(dcClient2Default.getCurrentComputedDropRate());
// now verify that we only get client1
for (int i = 0; i < 1000; ++i) {
assertEquals(getTrackerClient(strategy, null, new RequestContext(), 0, clients), client1);
}
// now force client1 to be disabled
DegraderControl dcClient1Default = client1.getDegraderControl(DEFAULT_PARTITION_ID);
dcClient1Default.setMinCallCount(1);
dcClient1Default.setOverrideMinCallCount(1);
dcClient1Default.setMaxDropRate(1d);
dcClient1Default.setUpStep(1d);
dcClient1Default.setHighErrorRate(0);
cc = client1.getCallTracker().startCall();
clock1.addMs(10000);
cc.endCallWithError();
clock1.addMs(5000);
// now verify that we never get a client back
for (int i = 0; i < 1000; ++i) {
assertNull(getTrackerClient(strategy, null, new RequestContext(), 1, clients));
}
// now enable client1 and client2
clock1.addMs(15000);
clock2.addMs(15000);
client1.getCallTracker().startCall().endCall();
client2.getCallTracker().startCall().endCall();
clock1.addMs(5000);
clock2.addMs(5000);
// now verify that we get client 1 or 2
for (int i = 0; i < 1000; ++i) {
assertTrue(clients.contains(getTrackerClient(strategy, null, new RequestContext(), 2, clients)));
}
}
use of com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor.DEFAULT_PARTITION_ID in project rest.li by linkedin.
the class DegraderLoadBalancerTest method DegraderLoadBalancerQuarantineTest.
/**
* DegraderLoadBalancerQuarantineTest
*/
@Test(groups = { "small", "back-end" })
public void DegraderLoadBalancerQuarantineTest() {
DegraderLoadBalancerStrategyConfig config = new DegraderLoadBalancerStrategyConfig(1000);
TestClock clock = new TestClock();
DegraderImpl.Config degraderConfig = DegraderConfigFactory.toDegraderConfig(Collections.emptyMap());
List<DegraderTrackerClient> trackerClients = createTrackerClient(3, clock, degraderConfig);
DegraderTrackerClientUpdater degraderTrackerClientUpdater = new DegraderTrackerClientUpdater(trackerClients.get(0), DEFAULT_PARTITION_ID);
LoadBalancerQuarantine quarantine = new LoadBalancerQuarantine(degraderTrackerClientUpdater.getTrackerClient(), config, "abc0");
TransportHealthCheck healthCheck = (TransportHealthCheck) quarantine.getHealthCheckClient();
RestRequest restRequest = healthCheck.getRestRequest();
Assert.assertTrue(restRequest.getURI().equals(URI.create("http://test.linkedin.com:10010/abc0")));
Assert.assertTrue(restRequest.getMethod().equals("OPTIONS"));
DegraderLoadBalancerStrategyConfig config1 = new DegraderLoadBalancerStrategyConfig(1000, DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_ONLY_AT_INTERVAL, 100, null, Collections.<String, Object>emptyMap(), DegraderLoadBalancerStrategyConfig.DEFAULT_CLOCK, DegraderLoadBalancerStrategyConfig.DEFAULT_INITIAL_RECOVERY_LEVEL, DegraderLoadBalancerStrategyConfig.DEFAULT_RAMP_FACTOR, DegraderLoadBalancerStrategyConfig.DEFAULT_HIGH_WATER_MARK, DegraderLoadBalancerStrategyConfig.DEFAULT_LOW_WATER_MARK, DegraderLoadBalancerStrategyConfig.DEFAULT_GLOBAL_STEP_UP, DegraderLoadBalancerStrategyConfig.DEFAULT_GLOBAL_STEP_DOWN, DegraderLoadBalancerStrategyConfig.DEFAULT_CLUSTER_MIN_CALL_COUNT_HIGH_WATER_MARK, DegraderLoadBalancerStrategyConfig.DEFAULT_CLUSTER_MIN_CALL_COUNT_LOW_WATER_MARK, DegraderLoadBalancerStrategyConfig.DEFAULT_HASHRING_POINT_CLEANUP_RATE, null, DegraderLoadBalancerStrategyConfig.DEFAULT_NUM_PROBES, DegraderLoadBalancerStrategyConfig.DEFAULT_POINTS_PER_HOST, DegraderLoadBalancerStrategyConfig.DEFAULT_BOUNDED_LOAD_BALANCING_FACTOR, null, DegraderLoadBalancerStrategyConfig.DEFAULT_QUARANTINE_MAXPERCENT, null, null, "GET", "/test/admin", DegraderImpl.DEFAULT_LOW_LATENCY, null, DegraderLoadBalancerStrategyConfig.DEFAULT_LOW_EVENT_EMITTING_INTERVAL, DegraderLoadBalancerStrategyConfig.DEFAULT_HIGH_EVENT_EMITTING_INTERVAL, DegraderLoadBalancerStrategyConfig.DEFAULT_CLUSTER_NAME);
DegraderTrackerClientUpdater updater1 = new DegraderTrackerClientUpdater(trackerClients.get(1), DEFAULT_PARTITION_ID);
quarantine = new LoadBalancerQuarantine(updater1.getTrackerClient(), config1, "abc0");
healthCheck = (TransportHealthCheck) quarantine.getHealthCheckClient();
restRequest = healthCheck.getRestRequest();
Assert.assertTrue(restRequest.getURI().equals(URI.create("http://test.linkedin.com:10010/test/admin")));
Assert.assertTrue(restRequest.getMethod().equals("GET"));
DegraderLoadBalancerStrategyConfig config2 = new DegraderLoadBalancerStrategyConfig(1000, DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_ONLY_AT_INTERVAL, 100, null, Collections.<String, Object>emptyMap(), DegraderLoadBalancerStrategyConfig.DEFAULT_CLOCK, DegraderLoadBalancerStrategyConfig.DEFAULT_INITIAL_RECOVERY_LEVEL, DegraderLoadBalancerStrategyConfig.DEFAULT_RAMP_FACTOR, DegraderLoadBalancerStrategyConfig.DEFAULT_HIGH_WATER_MARK, DegraderLoadBalancerStrategyConfig.DEFAULT_LOW_WATER_MARK, DegraderLoadBalancerStrategyConfig.DEFAULT_GLOBAL_STEP_UP, DegraderLoadBalancerStrategyConfig.DEFAULT_GLOBAL_STEP_DOWN, DegraderLoadBalancerStrategyConfig.DEFAULT_CLUSTER_MIN_CALL_COUNT_HIGH_WATER_MARK, DegraderLoadBalancerStrategyConfig.DEFAULT_CLUSTER_MIN_CALL_COUNT_LOW_WATER_MARK, DegraderLoadBalancerStrategyConfig.DEFAULT_HASHRING_POINT_CLEANUP_RATE, null, DegraderLoadBalancerStrategyConfig.DEFAULT_NUM_PROBES, DegraderLoadBalancerStrategyConfig.DEFAULT_POINTS_PER_HOST, DegraderLoadBalancerStrategyConfig.DEFAULT_BOUNDED_LOAD_BALANCING_FACTOR, null, DegraderLoadBalancerStrategyConfig.DEFAULT_QUARANTINE_MAXPERCENT, null, null, "OPTIONS", null, DegraderImpl.DEFAULT_LOW_LATENCY, null, DegraderLoadBalancerStrategyConfig.DEFAULT_LOW_EVENT_EMITTING_INTERVAL, DegraderLoadBalancerStrategyConfig.DEFAULT_HIGH_EVENT_EMITTING_INTERVAL, DegraderLoadBalancerStrategyConfig.DEFAULT_CLUSTER_NAME);
DegraderTrackerClientUpdater updater2 = new DegraderTrackerClientUpdater(trackerClients.get(2), DEFAULT_PARTITION_ID);
quarantine = new LoadBalancerQuarantine(updater2.getTrackerClient(), config2, "abc0");
healthCheck = (TransportHealthCheck) quarantine.getHealthCheckClient();
restRequest = healthCheck.getRestRequest();
Assert.assertTrue(restRequest.getURI().equals(URI.create("http://test.linkedin.com:10010/abc2")));
Assert.assertTrue(restRequest.getMethod().equals("OPTIONS"));
}
use of com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor.DEFAULT_PARTITION_ID 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);
}
}
use of com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor.DEFAULT_PARTITION_ID 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);
}
Aggregations