Search in sources :

Example 1 with LoadBalancerSimulator

use of com.linkedin.d2.balancer.simple.LoadBalancerSimulator in project rest.li by linkedin.

the class SimpleLoadBalancerDelayTest method loadBalancerD2MonitorWithQuarantineTest.

@Test(groups = { "small", "back-end" })
public void loadBalancerD2MonitorWithQuarantineTest() throws Exception {
    String uri1 = "test.qa1.com:1234";
    String uri2 = "test.qa2.com:2345";
    List<String> uris = Arrays.asList(uri1, uri2);
    URI uriU1 = new URI(uri1);
    URI uriU2 = new URI(uri2);
    // Construct the delay patterns: for each URI there is a list of delays for each interval
    Map<String, List<Long>> delayMaps = new HashMap<>();
    delayMaps.put("test.qa1.com:1234", Arrays.asList(80l, 30l, 30l, 30l, 30l, 80l, 30l, 30l, 30l, 30l, 30l, 80l, 60l, 80l, 80l, 60l, 80l, 80l, 60l, 80l, 80l));
    delayMaps.put("test.qa2.com:2345", Arrays.asList(80l, 80l, 30l, 30l, 30l, 80l, 30l, 30l, 3060l, 4080l, 3050l, 3080l, 4080l, 4080l, 4080l, 4080l, 60l, 80l, 60l, 80l, 80l));
    LoadBalancerSimulator.TimedValueGenerator<String, Long> delayGenerator = new DelayValueGenerator<>(delayMaps, DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS);
    Map<String, Object> strategyProperties = DegraderLoadBalancerTest.lbDefaultConfig();
    // setting the event emitting interval to 40s
    strategyProperties.put(PropertyKeys.HTTP_LB_HIGH_EVENT_EMITTING_INTERVAL, "40000");
    strategyProperties.put(PropertyKeys.HTTP_LB_QUARANTINE_MAX_PERCENT, 0.05);
    strategyProperties.put(PropertyKeys.HTTP_LB_HASH_CONFIG, HASH_CONFIG_MAP);
    // Construct the QPS generator
    LoadBalancerSimulator.QPSGenerator qpsGenerator = new ConstantQPSGenerator(2000);
    // Create the simulator
    LoadBalancerSimulator loadBalancerSimulator = LoadBalancerSimulationBuilder.build("cluster-1", "foo", uris, strategyProperties, null, null, delayGenerator, qpsGenerator);
    URI expectedUri1 = LoadBalancerSimulationBuilder.getExpectedUri("test.qa1.com:1234", "foo");
    // Start the simulation
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS);
    printStates(loadBalancerSimulator);
    // the points for uri1 should be 100
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 100);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    List<D2Monitor> d2Monitors = _d2MonitorMap.get("foo");
    // For healthy state, there is no emission yet.
    assertTrue(d2Monitors == null || d2Monitors.isEmpty());
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    d2Monitors = _d2MonitorMap.get("foo");
    // For healthy state, there is no emission yet.
    assertTrue(d2Monitors == null || d2Monitors.isEmpty());
    printStates(loadBalancerSimulator);
    // wait for 3 intervals due to call dropping involved
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 6);
    d2Monitors = _d2MonitorMap.get("foo");
    assertTrue(d2Monitors != null);
    // the first emitting
    assertFalse(d2Monitors.isEmpty());
    D2Monitor d2Monitor = d2Monitors.get(0);
    assertTrue(d2Monitor.getClusterStats().getClusterCallCount() > 0);
    assertTrue(d2Monitor.getClusterStats().getClusterDropLevel() < 0.00001);
    assertEquals(d2Monitor.getIntervalMs(), 40000);
    printStates(loadBalancerSimulator);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 100);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 8);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 100);
    assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2) < 10);
    d2Monitors = _d2MonitorMap.get("foo");
    assertTrue(d2Monitors != null);
    // the first emitting
    assertFalse(d2Monitors.isEmpty());
    d2Monitor = d2Monitors.get(0);
    assertTrue(d2Monitor.getClusterStats().getClusterCallCount() > 0);
    List<D2Monitor.UriInfo> uriList = d2Monitor.getUriList();
    assertFalse(uriList.isEmpty());
    D2Monitor.UriInfo goodUri = uriList.get(0).getCurrentAvgLatency() < uriList.get(0).getCurrentAvgLatency() ? uriList.get(0) : uriList.get(1);
    D2Monitor.UriInfo badUri = uriList.get(0).getCurrentAvgLatency() >= uriList.get(0).getCurrentAvgLatency() ? uriList.get(0) : uriList.get(1);
    assertTrue(goodUri.getCurrentAvgLatency() <= 80);
    assertTrue(goodUri.getCurrentCallCount() > 1900);
    assertTrue(badUri.getCurrentCallCount() == 0);
    assertTrue(badUri.getQuarantineDuration() > 0);
    assertEquals(d2Monitor.getIntervalMs(), 40000);
    printStates(loadBalancerSimulator);
    // Done. Shutdown the simulation
    loadBalancerSimulator.shutdown();
}
Also used : HashMap(java.util.HashMap) URI(java.net.URI) ArrayList(java.util.ArrayList) List(java.util.List) D2Monitor(com.linkedin.d2.balancer.event.D2Monitor) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 2 with LoadBalancerSimulator

use of com.linkedin.d2.balancer.simple.LoadBalancerSimulator in project rest.li by linkedin.

the class SimpleLoadBalancerDelayTest method loadBalancerQuarantineDefaultDegraderTest.

/**
 * Test to verify quarantine works with the default degrader step up/down
 * @throws Exception
 */
@Test(groups = { "small", "back-end" })
public void loadBalancerQuarantineDefaultDegraderTest() throws Exception {
    String uri1 = "test.qa1.com:1234";
    String uri2 = "test.qa2.com:2345";
    List<String> uris = Arrays.asList(uri1, uri2);
    // Construct the delay patterns: for each URI there is a list of delays for each interval
    Map<String, List<Long>> delayMaps = new HashMap<>();
    delayMaps.put("test.qa1.com:1234", Arrays.asList(80l, 3000l, 3000l, 3000l, 70l, 3000l, 3200l, 3400l, 80l, 60l, 80l, 10l, 60l, 80l, 80l, 60l, 80l, 80l, 60l, 80l, 80l, 60l, 80l, 80l, 60l, 80l, 80l, 60l, 80l, 80l, 60l, 80l, 80l, 60l, 80l, 80l));
    delayMaps.put("test.qa2.com:2345", Arrays.asList(80l, 80l, 60l, 80l, 50l, 80l, 80l, 80l, 60l, 80l, 60l, 80l, 60l, 20l, 35l, 60l, 28l, 32l, 64l, 60l, 80l, 80l, 60l, 80l, 80l, 60l, 80l, 80l, 60l, 80l, 80l, 60l, 80l, 80l, 60l, 80l, 80l));
    LoadBalancerSimulator.TimedValueGenerator<String, Long> delayGenerator = new DelayValueGenerator<>(delayMaps, DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS);
    // Construct the QPS generator
    LoadBalancerSimulator.QPSGenerator qpsGenerator = new ConstantQPSGenerator(1000);
    // use the default up/down steps for degrading/recovering
    Map<String, String> degraderProperties = DegraderLoadBalancerTest.degraderDefaultConfig();
    degraderProperties.put(PropertyKeys.DEGRADER_DOWN_STEP, "0.05");
    degraderProperties.put(PropertyKeys.DEGRADER_UP_STEP, "0.2");
    // Create the simulator
    LoadBalancerSimulator loadBalancerSimulator = LoadBalancerSimulationBuilder.build("cluster-1", "foo", uris, lbStrategyPropertiesWithQuarantine(), null, degraderProperties, delayGenerator, qpsGenerator);
    URI expectedUri1 = LoadBalancerSimulationBuilder.getExpectedUri("test.qa1.com:1234", "foo");
    // Start the simulation
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS);
    printStates(loadBalancerSimulator);
    // the points for uri1 should be 100
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 100);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    // wait for 2 intervals due to call dropping involved
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // the points for uri1 should be 80
    // Also if the loadbalancing strategy changed, the numbers could be lower
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 80);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1) > 30);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // even recover a little, still not in quarantine yet
    assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1) > 10);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 0);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 24);
    printStates(loadBalancerSimulator);
    // fully recovery needs much longer time
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 100);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    // the points for uri3 should be around 60, recovering
    _log.info(loadBalancerSimulator.getClockedExecutor().toString());
    // Done. Shutdown the simulation
    loadBalancerSimulator.shutdown();
}
Also used : HashMap(java.util.HashMap) URI(java.net.URI) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 3 with LoadBalancerSimulator

use of com.linkedin.d2.balancer.simple.LoadBalancerSimulator in project rest.li by linkedin.

the class SimpleLoadBalancerDelayTest method loadBalancerQuarantineLongIntervalTest.

/**
 * Quaratine with long checking interval
 * @throws Exception
 */
@Test(groups = { "small", "back-end" })
public void loadBalancerQuarantineLongIntervalTest() throws Exception {
    String uri1 = "test.qa1.com:1234";
    String uri2 = "test.qa2.com:2345";
    List<String> uris = Arrays.asList(uri1, uri2);
    // Construct the delay patterns: for each URI there is a list of delays for each interval
    Map<String, List<Long>> delayMaps = new HashMap<>();
    delayMaps.put("test.qa1.com:1234", Arrays.asList(80l, 3000l, 3000l, 3000l, 3000l, 60l, 65l, 60l, 80l, 65l, 60l, 80l));
    delayMaps.put("test.qa2.com:2345", Arrays.asList(60l, 60l, 65l, 60l, 50l, 80l, 60l, 60l, 65l, 60l, 65l, 60l));
    LoadBalancerSimulator.TimedValueGenerator<String, Long> delayGenerator = new DelayValueGenerator<>(delayMaps, DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS);
    // check interval is 1000ms, so 10 checks will span across 2 check intervals
    // strategyProperties.put(PropertyKeys.HTTP_LB_QUARANTINE_CHECK_INTERVAL, "1000");
    // Construct the QPS generator
    LoadBalancerSimulator.QPSGenerator qpsGenerator = new ConstantQPSGenerator(1000);
    // Create the simulator
    LoadBalancerSimulator loadBalancerSimulator = LoadBalancerSimulationBuilder.build("cluster-1", "foo", uris, lbStrategyPropertiesWithQuarantine(), null, null, delayGenerator, qpsGenerator);
    URI expectedUri1 = LoadBalancerSimulationBuilder.getExpectedUri("test.qa1.com:1234", "foo");
    // Start the simulation
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS);
    printStates(loadBalancerSimulator);
    // the points for uri1 should be 100
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 100);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    // assertTrue(loadBalancerSimulator.getCountPercent(expectedUri1) <= 0.55);
    // assertTrue(loadBalancerSimulator.getCountPercent(expectedUri1) >= 0.45);
    // wait for 2 intervals due to call dropping involved
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // the points for uri1 should be 60
    // Also if the loadbalancing strategy changed, the numbers could be lower
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 60);
    // assertTrue(loadBalancerSimulator.getCountPercent(expectedUri1) <= 0.65);
    // assertTrue(loadBalancerSimulator.getCountPercent(expectedUri1) >= 0.25);
    // continue the simulation
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // the points for uri1 should be 0 as it is now in quarantine
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 0);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 0);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 7);
    printStates(loadBalancerSimulator);
    // uri1 should fully recovered by now
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 100);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    // the points for uri3 should be around 60, recovering
    _log.info(loadBalancerSimulator.getClockedExecutor().toString());
    // Done. Shutdown the simulation
    loadBalancerSimulator.shutdown();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) URI(java.net.URI) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 4 with LoadBalancerSimulator

use of com.linkedin.d2.balancer.simple.LoadBalancerSimulator in project rest.li by linkedin.

the class SimpleLoadBalancerDelayTest method testLoadBalancerWithSlowStartClient.

@Test(groups = { "small", "back-end" })
public void testLoadBalancerWithSlowStartClient() throws Exception {
    // Generate service, cluster and uri properties for d2
    URI uri1 = URI.create("http://test.qa1.com:1234");
    URI uri2 = URI.create("http://test.qa2.com:2345");
    URI uri3 = URI.create("http://test.qa3.com:6789");
    String clusterName = "cluster-2";
    Map<Integer, PartitionData> partitionData = new HashMap<>(1);
    partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
    Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<>(3);
    uriData.put(uri1, partitionData);
    uriData.put(uri2, partitionData);
    uriData.put(uri3, partitionData);
    ClusterProperties clusterProperties = new ClusterProperties(clusterName);
    List<String> prioritizedSchemes = Collections.singletonList("http");
    // enable multi-probe consistent hashing
    Map<String, Object> lbStrategyProperties = new HashMap<>();
    lbStrategyProperties.put(PropertyKeys.HTTP_LB_CONSISTENT_HASH_ALGORITHM, DelegatingRingFactory.MULTI_PROBE_CONSISTENT_HASH);
    lbStrategyProperties.put(PropertyKeys.HTTP_LB_HASH_CONFIG, HASH_CONFIG_MAP);
    // set initial drop rate and slow start threshold
    Map<String, String> degraderProperties = DegraderLoadBalancerTest.degraderDefaultConfig();
    degraderProperties.put(PropertyKeys.DEGRADER_INITIAL_DROP_RATE, "0.99");
    degraderProperties.put(PropertyKeys.DEGRADER_SLOW_START_THRESHOLD, "0.1");
    // constant delay generator
    LoadBalancerSimulator.TimedValueGenerator<String, Long> delayGenerator = (uri, time, unit) -> 100l;
    // constant QPS generator
    LoadBalancerSimulator.QPSGenerator qpsGenerator = () -> 1000;
    Map<String, Object> transportClientProperties = Collections.singletonMap("DelayGenerator", delayGenerator);
    ServiceProperties serviceProperties = new ServiceProperties("foo", clusterName, "/foo", Arrays.asList("degraderV3"), lbStrategyProperties, transportClientProperties, degraderProperties, prioritizedSchemes, null);
    UriProperties uriProperties = new UriProperties(clusterName, uriData);
    // pass all the info to the simulator
    LoadBalancerSimulator loadBalancerSimulator = new LoadBalancerSimulator(serviceProperties, clusterProperties, uriProperties, delayGenerator, qpsGenerator, null);
    // Start the simulation, wait for 10 UPDATE_INTERVALS to make sure all uris are fully ramped.
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 20);
    printStates(loadBalancerSimulator);
    URI uri4 = URI.create("http://test.qa4.com:9876");
    uriData.put(uri4, partitionData);
    uriProperties = new UriProperties(clusterName, uriData);
    loadBalancerSimulator.updateUriProperties(uriProperties);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS);
    printStates(loadBalancerSimulator);
    // Create the delay generator for the uris
    URI expectedUri4 = URI.create("http://test.qa4.com:9876/foo");
    loadBalancerSimulator.getCountPercent(expectedUri4);
    // the points for uri4 should be 1 and call count percentage is 0.3%.
    double callCountPercent = loadBalancerSimulator.getCountPercent(expectedUri4);
    assertTrue(callCountPercent <= 0.006, "expected percentage is less than 0.006, actual is " + callCountPercent);
    // wait for 2 intervals due to call dropping
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // the points for uri4 should be 4 and call count percentage is 1.3%
    callCountPercent = loadBalancerSimulator.getCountPercent(expectedUri4);
    assertTrue(callCountPercent <= 0.02, "expected percentage is less than 0.02, actual is " + callCountPercent);
    // wait for 2 intervals due to call dropping
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // the points for uri4 should be 16 and call count percentage is 5%
    callCountPercent = loadBalancerSimulator.getCountPercent(expectedUri4);
    assertTrue(callCountPercent <= 0.07, "expected percentage is less than 0.07, actual is " + callCountPercent);
    assertTrue(callCountPercent >= 0.03, "expected percentage is larger than 0.03, actual is " + callCountPercent);
    // wait for 2 intervals due to call dropping
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // the points for uri4 should be 56 and call count percentage is 16%
    callCountPercent = loadBalancerSimulator.getCountPercent(expectedUri4);
    assertTrue(callCountPercent <= 0.18, "expected percentage is less than 0.18, actual is " + callCountPercent);
    assertTrue(callCountPercent >= 0.12, "expected percentage is larger than 0.12, actual is " + callCountPercent);
    // wait for 2 intervals due to call dropping
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // the points for uri4 should be 96 and call count percentage is 24%
    callCountPercent = loadBalancerSimulator.getCountPercent(expectedUri4);
    assertTrue(callCountPercent <= 0.28, "expected percentage is less than 0.26, actual is " + callCountPercent);
    assertTrue(callCountPercent >= 0.20, "expected percentage is larger than 0.22, actual is " + callCountPercent);
}
Also used : DegraderLoadBalancerStrategyConfig(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyConfig) DefaultPartitionAccessor(com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) ArrayList(java.util.ArrayList) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest) Map(java.util.Map) DelegatingRingFactory(com.linkedin.d2.balancer.strategies.DelegatingRingFactory) URI(java.net.URI) Assert.assertFalse(org.testng.Assert.assertFalse) D2Monitor(com.linkedin.d2.balancer.event.D2Monitor) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) DegraderLoadBalancerStrategyV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyV3) EventEmitter(com.linkedin.d2.balancer.event.EventEmitter) PropertyKeys(com.linkedin.d2.balancer.properties.PropertyKeys) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Assert.assertTrue(org.testng.Assert.assertTrue) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) Collections(java.util.Collections) HashMap(java.util.HashMap) URI(java.net.URI) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 5 with LoadBalancerSimulator

use of com.linkedin.d2.balancer.simple.LoadBalancerSimulator in project rest.li by linkedin.

the class SimpleLoadBalancerDelayTest method testLoadBalancerWithFastRecoveryAndSlowstartWithErrors.

@Test(groups = { "small", "back-end" })
public void testLoadBalancerWithFastRecoveryAndSlowstartWithErrors() throws Exception {
    // Generate service, cluster and uri properties for d2
    URI uri1 = URI.create("http://test.qa1.com:1234");
    URI uri2 = URI.create("http://test.qa2.com:2345");
    String clusterName = "cluster-2";
    Map<Integer, PartitionData> partitionData = new HashMap<>(1);
    partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
    Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<>(1);
    uriData.put(uri1, partitionData);
    ClusterProperties clusterProperties = new ClusterProperties(clusterName);
    List<String> prioritizedSchemes = Collections.singletonList("http");
    // set initial drop rate and slow start threshold
    Map<String, String> degraderProperties = DegraderLoadBalancerTest.degraderDefaultConfig();
    degraderProperties.put(PropertyKeys.DEGRADER_INITIAL_DROP_RATE, "0.99");
    degraderProperties.put(PropertyKeys.DEGRADER_SLOW_START_THRESHOLD, "0.1");
    degraderProperties.put(PropertyKeys.DEGRADER_MIN_CALL_COUNT, "1");
    degraderProperties.put(PropertyKeys.DEGRADER_DOWN_STEP, "0.3");
    degraderProperties.put(PropertyKeys.DEGRADER_HIGH_ERROR_RATE, "0.1");
    degraderProperties.put(PropertyKeys.DEGRADER_LOW_ERROR_RATE, "0.01");
    // constant delay generator
    LoadBalancerSimulator.TimedValueGenerator<String, Long> delayGenerator = (uri, time, unit) -> 100l;
    Map<String, List<String>> returnMaps = new HashMap<>();
    returnMaps.put("test.qa1.com:1234", Collections.singletonList(null));
    returnMaps.put("test.qa2.com:2345", Collections.singletonList("simulated error"));
    LoadBalancerSimulator.TimedValueGenerator<String, String> errorGenerator = new DelayValueGenerator<>(returnMaps, DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS);
    LoadBalancerSimulator.QPSGenerator qpsGenerator = new QPSValueGenerator(Arrays.asList(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 100));
    Map<String, Object> transportClientProperties = new HashMap<>();
    transportClientProperties.put("DelayGenerator", delayGenerator);
    transportClientProperties.put("ErrorGenerator", errorGenerator);
    ServiceProperties serviceProperties = new ServiceProperties("foo", clusterName, "/foo", Arrays.asList("degraderV3"), lbStrategyPropertiesWithSlowstart(), transportClientProperties, degraderProperties, prioritizedSchemes, null);
    UriProperties uriProperties = new UriProperties(clusterName, uriData);
    // pass all the info to the simulator
    LoadBalancerSimulator loadBalancerSimulator = new LoadBalancerSimulator(serviceProperties, clusterProperties, uriProperties, delayGenerator, qpsGenerator, null);
    // Start the simulation: wait for uri to its full points
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 5);
    // assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1) >= 4);
    _log.info("Points is " + loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1));
    printStates(loadBalancerSimulator);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 5);
    // assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 100);
    printStates(loadBalancerSimulator);
    // Adding uri2
    uriData.put(uri2, partitionData);
    uriProperties = new UriProperties(clusterName, uriData);
    loadBalancerSimulator.updateUriProperties(uriProperties);
    // no traffic to uri2, even though the points are increasing
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 10);
    printStates(loadBalancerSimulator);
    assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2) >= 4);
    // Getting traffic for uri2 -- kicked out due to errors
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 4);
    printStates(loadBalancerSimulator);
    // fully degraded due to errors
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 6);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 1);
    printStates(loadBalancerSimulator);
}
Also used : DegraderLoadBalancerStrategyConfig(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyConfig) DefaultPartitionAccessor(com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) ArrayList(java.util.ArrayList) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest) Map(java.util.Map) DelegatingRingFactory(com.linkedin.d2.balancer.strategies.DelegatingRingFactory) URI(java.net.URI) Assert.assertFalse(org.testng.Assert.assertFalse) D2Monitor(com.linkedin.d2.balancer.event.D2Monitor) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) DegraderLoadBalancerStrategyV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyV3) EventEmitter(com.linkedin.d2.balancer.event.EventEmitter) PropertyKeys(com.linkedin.d2.balancer.properties.PropertyKeys) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Assert.assertTrue(org.testng.Assert.assertTrue) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) Collections(java.util.Collections) HashMap(java.util.HashMap) URI(java.net.URI) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) ArrayList(java.util.ArrayList) List(java.util.List) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Aggregations

DegraderLoadBalancerTest (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)18 URI (java.net.URI)18 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)18 List (java.util.List)18 Test (org.testng.annotations.Test)18 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)7 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)7 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)7 Map (java.util.Map)7 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)6 D2Monitor (com.linkedin.d2.balancer.event.D2Monitor)5 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)3 EventEmitter (com.linkedin.d2.balancer.event.EventEmitter)3 PropertyKeys (com.linkedin.d2.balancer.properties.PropertyKeys)3 DelegatingRingFactory (com.linkedin.d2.balancer.strategies.DelegatingRingFactory)3 DegraderLoadBalancerStrategyConfig (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyConfig)3 DegraderLoadBalancerStrategyV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyV3)3 DefaultPartitionAccessor (com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor)3 Arrays (java.util.Arrays)3