Search in sources :

Example 11 with LoadBalancerSimulator

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

the class SimpleLoadBalancerDelayTest method loadBalancerQuarantineReQuarantineTest.

/**
 * Client is quarantined again after evicted, with longer backoff time
 * @throws Exception
 */
@Test(groups = { "small", "back-end" })
public void loadBalancerQuarantineReQuarantineTest() 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, 3000l, 90l, 3150l, 3100l, 3800l, 3150l, 90l, 80l, 90l, 60l, 65l, 80l, 20l));
    delayMaps.put("test.qa2.com:2345", Arrays.asList(90l, 90l, 60l, 90l, 50l, 80l, 60l, 90l, 90l, 90l, 60l, 90l, 60l, 65l, 180l, 90l, 120l, 60l));
    LoadBalancerSimulator.TimedValueGenerator<String, Long> delayGenerator = new DelayValueGenerator<>(delayMaps, DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS);
    // 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);
    // 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
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 60);
    // 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);
    // Evicted from quarantine finally. The point number should be none zero
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1) > 0);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // uri1 should be quarantined again
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 0);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 3);
    printStates(loadBalancerSimulator);
    // uri1 should be evicted again
    assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1) > 0);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 4);
    printStates(loadBalancerSimulator);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 100);
    _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 12 with LoadBalancerSimulator

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

the class SimpleLoadBalancerDelayTest method testLoadBalancerWithFastRecoveryAndSlowstartWithDegrading.

@Test(groups = { "small", "back-end" })
public void testLoadBalancerWithFastRecoveryAndSlowstartWithDegrading() 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");
    // constant delay generator
    Map<String, List<Long>> delayMaps = new HashMap<>();
    delayMaps.put("test.qa1.com:1234", Arrays.asList(80L, 80L, 60L, 80L, 50L, 80L, 80L, 80L, 60L, 80L, 60L, 80L, 80L, 80L, 60L, 80L, 50L, 60L, 80L, 60L, 80L, 80L, 80L, 60L, 80L, 60L, 80L, 80L, 60L, 80L, 50L, 80L, 80L, 80L, 60L, 80L, 60L, 80L, 80L, 60L, 80L, 50L, 80L, 80L, 80L, 60L, 80L, 60L));
    delayMaps.put("test.qa2.com:2345", Arrays.asList(80L, 80L, 60L, 80L, 50L, 80L, 80L, 80L, 60L, 80L, 60L, 80L, 80L, 80L, 60L, 80L, 50L, 60L, 80L, 60L, 80L, 80L, 3080L, 3060L, 89L, 60L, 3080L, 3080L, 3000L, 3000L, 3000L, 3000L, 3080L, 4060L, 3080L, 4080L, 4060L, 80L, 80L, 60L, 60L, 60L));
    LoadBalancerSimulator.TimedValueGenerator<String, Long> delayGenerator = new DelayValueGenerator<>(delayMaps, 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, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1000, 1000, 1000));
    Map<String, Object> transportClientProperties = Collections.singletonMap("DelayGenerator", delayGenerator);
    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);
    assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2) >= 8);
    printStates(loadBalancerSimulator);
    // Got traffic, computedDrapRate recovered.
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // degrading again with high latency: kicked out from recoveryMap
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2) < 32);
    printStates(loadBalancerSimulator);
}
Also used : 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) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 13 with LoadBalancerSimulator

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

the class SimpleLoadBalancerDelayTest method testLoadBalancerWithFastRecoveryAndSlowstart.

@Test(groups = { "small", "back-end" })
public void testLoadBalancerWithFastRecoveryAndSlowstart() 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");
    // constant delay generator
    LoadBalancerSimulator.TimedValueGenerator<String, Long> delayGenerator = (uri, time, unit) -> 100l;
    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, 20, 1, 1, 1, 1, 1, 1, 1, 1, 10));
    Map<String, Object> transportClientProperties = Collections.singletonMap("DelayGenerator", delayGenerator);
    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);
    assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2) >= 4);
    printStates(loadBalancerSimulator);
    // only one possible recovery, points increasing by recoveryMap
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 8);
    assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2) >= 64);
    printStates(loadBalancerSimulator);
    // fully recovered
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 6);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    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) 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 14 with LoadBalancerSimulator

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

the class SimpleLoadBalancerDelayTest method loadBalancerQuarantineMixTest.

/**
 * When quarantine is full, the rest of the clients should degrading just as no quarantine presents
 * @throws Exception
 */
@Test(groups = { "small", "back-end" })
public void loadBalancerQuarantineMixTest() throws Exception {
    String uri1 = "test.qa1.com:1234";
    String uri2 = "test.qa2.com:2345";
    String uri3 = "test.qa3.com:6789";
    List<String> uris = Arrays.asList(uri1, uri2, uri3);
    // 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, 3090l, 3075l, 90l, 80l, 75l, 90l, 80l, 20l, 60l, 85l, 60l));
    delayMaps.put("test.qa2.com:2345", Arrays.asList(90l, 90l, 75l, 90l, 50l, 80l, 90l, 90l, 75l, 90l, 75l, 90l, 50l, 60l, 20l, 80l, 60l, 20l));
    delayMaps.put("test.qa3.com:6789", Arrays.asList(80l, 3000l, 3000l, 3000l, 3000l, 90l, 75l, 90l, 80l, 75l, 90l, 80l, 800l, 50l, 60l, 85l, 50l));
    LoadBalancerSimulator.TimedValueGenerator<String, Long> delayGenerator = new DelayValueGenerator<>(delayMaps, DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS);
    // 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);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri3), 100);
    // wait for 2 intervals due to call dropping involved
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // the points for uri1/uri3 should be 60
    // Also if the loadbalancing strategy changed, the numbers could be lower
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 60);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri3), 60);
    // continue the simulation
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // the points for uri3 should be 0 as it is now in quarantine, uri1 should not be 0
    assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1) > 0);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri3), 0);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // uri1 points in minimal (1)
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 1);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // uri 3 evicted, uri 1 in quarantine
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 0);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri3), 1);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 8);
    printStates(loadBalancerSimulator);
    // uri1/uri3 should fully recovered by now
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 100);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri3), 100);
    _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 15 with LoadBalancerSimulator

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

the class SimpleLoadBalancerDelayTest method loadBalancerQuarantineMaxNumTest.

/**
 * Quarantine with CAP: no more client can be added when CAP reached
 * @throws Exception
 */
@Test(groups = { "small", "back-end" })
public void loadBalancerQuarantineMaxNumTest() throws Exception {
    String uri1 = "test.qa1.com:1234";
    String uri2 = "test.qa2.com:2345";
    String uri3 = "test.qa3.com:6789";
    List<String> uris = Arrays.asList(uri1, uri2, uri3);
    // 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, 90l, 75l, 90l, 80l, 75l, 90l, 80l));
    delayMaps.put("test.qa2.com:2345", Arrays.asList(90l, 90l, 75l, 90l, 50l, 80l, 90l, 90l, 75l, 90l, 75l, 90l));
    delayMaps.put("test.qa3.com:6789", Arrays.asList(80l, 3000l, 3000l, 3000l, 3000l, 90l, 75l, 90l, 80l, 75l, 90l, 80l));
    LoadBalancerSimulator.TimedValueGenerator<String, Long> delayGenerator = new DelayValueGenerator<>(delayMaps, DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS);
    // 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);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri3), 100);
    // wait for 2 intervals due to call dropping involved
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // the points for uri1/uri3 should be 60
    // Also if the loadbalancing strategy changed, the numbers could be lower
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 60);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri3), 60);
    // continue the simulation
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    // the points for uri3 should be 0 as it is now in quarantine, uri1 should not be 0
    assertTrue(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1) > 0);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri3), 0);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 2);
    printStates(loadBalancerSimulator);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri3), 0);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri2), 100);
    loadBalancerSimulator.runWait(DegraderLoadBalancerStrategyConfig.DEFAULT_UPDATE_INTERVAL_MS * 9);
    printStates(loadBalancerSimulator);
    // uri1/uri3 should fully recovered by now
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri1), 100);
    assertEquals(loadBalancerSimulator.getPoint("foo", DefaultPartitionAccessor.DEFAULT_PARTITION_ID, uri3), 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)

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