Search in sources :

Example 16 with com.linkedin.d2.loadBalancerStrategyType

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

the class TestLoadBalancerStrategy method testPartitionWeightChange.

@Test(dataProvider = "strategy")
public void testPartitionWeightChange(loadBalancerStrategyType type) {
    double weight = 0.5;
    Map<Integer, PartitionData> partitionDataMap = new HashMap<>();
    partitionDataMap.put(LoadBalancerStrategyTestRunner.DEFAULT_PARTITION_ID, new PartitionData(weight));
    LoadBalancerStrategyTestRunner testRunner = new LoadBalancerStrategyTestRunnerBuilder(type, DEFAULT_SERVICE_NAME, DEFAULT_NUM_HOSTS).setConstantRequestCount(DEFAULT_REQUESTS_PER_INTERVAL).addPartitionDataMap(0, partitionDataMap).setNumIntervals(3).setConstantLatency(Arrays.asList(HEALTHY_HOST_CONSTANT_LATENCY, HEALTHY_HOST_CONSTANT_LATENCY, HEALTHY_HOST_CONSTANT_LATENCY, HEALTHY_HOST_CONSTANT_LATENCY, HEALTHY_HOST_CONSTANT_LATENCY)).build();
    testRunner.runWait();
    Map<URI, Integer> pointsMap = testRunner.getPoints();
    assertEquals(pointsMap.get(testRunner.getUri(0)).intValue(), (int) (weight * HEALTHY_POINTS));
    assertEquals(pointsMap.get(testRunner.getUri(1)).intValue(), HEALTHY_POINTS);
    assertEquals(pointsMap.get(testRunner.getUri(2)).intValue(), HEALTHY_POINTS);
    assertEquals(pointsMap.get(testRunner.getUri(3)).intValue(), HEALTHY_POINTS);
    assertEquals(pointsMap.get(testRunner.getUri(4)).intValue(), HEALTHY_POINTS);
}
Also used : LoadBalancerStrategyTestRunnerBuilder(com.linkedin.d2.balancer.strategies.framework.LoadBalancerStrategyTestRunnerBuilder) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) HashMap(java.util.HashMap) LoadBalancerStrategyTestRunner(com.linkedin.d2.balancer.strategies.framework.LoadBalancerStrategyTestRunner) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 17 with com.linkedin.d2.loadBalancerStrategyType

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

the class DarkClusterStrategyFactoryImpl method createStrategy.

/**
 * In the future, additional strategies can be added, and the logic here can choose the appropriate one based on the config values.
 */
private DarkClusterStrategy createStrategy(String darkClusterName, DarkClusterConfig darkClusterConfig) {
    if (darkClusterConfig.hasDarkClusterStrategyPrioritizedList()) {
        DarkClusterStrategyNameArray strategyList = darkClusterConfig.getDarkClusterStrategyPrioritizedList();
        for (com.linkedin.d2.DarkClusterStrategyName darkClusterStrategyName : strategyList) {
            switch(darkClusterStrategyName) {
                case RELATIVE_TRAFFIC:
                    if (RelativeTrafficMultiplierDarkClusterStrategy.isValidConfig(darkClusterConfig)) {
                        BaseDarkClusterDispatcher baseDarkClusterDispatcher = new BaseDarkClusterDispatcherImpl(darkClusterName, _darkClusterDispatcher, _notifier, _verifierManager);
                        return new RelativeTrafficMultiplierDarkClusterStrategy(_sourceClusterName, darkClusterName, darkClusterConfig.getMultiplier(), baseDarkClusterDispatcher, _notifier, _facilities.getClusterInfoProvider(), _random);
                    }
                    break;
                case IDENTICAL_TRAFFIC:
                    if (IdenticalTrafficMultiplierDarkClusterStrategy.isValidConfig(darkClusterConfig)) {
                        BaseDarkClusterDispatcher baseDarkClusterDispatcher = new BaseDarkClusterDispatcherImpl(darkClusterName, _darkClusterDispatcher, _notifier, _verifierManager);
                        return new IdenticalTrafficMultiplierDarkClusterStrategy(_sourceClusterName, darkClusterName, darkClusterConfig.getMultiplier(), baseDarkClusterDispatcher, _notifier, _facilities.getClusterInfoProvider(), _random);
                    }
                    break;
                case CONSTANT_QPS:
                    if (_rateLimiterSupplier == null) {
                        LOG.error("Dark Cluster {} configured to use CONSTANT_QPS strategy, but no rate limiter provided during instantiation. " + "No Dark Cluster strategy will be used!", darkClusterName);
                        break;
                    }
                    if (ConstantQpsDarkClusterStrategy.isValidConfig(darkClusterConfig)) {
                        BaseDarkClusterDispatcher baseDarkClusterDispatcher = new BaseDarkClusterDispatcherImpl(darkClusterName, _darkClusterDispatcher, _notifier, _verifierManager);
                        ConstantQpsRateLimiter rateLimiter = _rateLimiterSupplier.get();
                        rateLimiter.setBufferCapacity(darkClusterConfig.getDispatcherMaxRequestsToBuffer());
                        rateLimiter.setBufferTtl(darkClusterConfig.getDispatcherBufferedRequestExpiryInSeconds(), ChronoUnit.SECONDS);
                        return new ConstantQpsDarkClusterStrategy(_sourceClusterName, darkClusterName, darkClusterConfig.getDispatcherOutboundTargetRate(), baseDarkClusterDispatcher, _notifier, _facilities.getClusterInfoProvider(), rateLimiter);
                    }
                    break;
                default:
                    break;
            }
        }
    }
    return new NoOpDarkClusterStrategy();
}
Also used : ConstantQpsRateLimiter(com.linkedin.r2.transport.http.client.ConstantQpsRateLimiter) DarkClusterStrategyNameArray(com.linkedin.d2.DarkClusterStrategyNameArray) BaseDarkClusterDispatcher(com.linkedin.darkcluster.api.BaseDarkClusterDispatcher) NoOpDarkClusterStrategy(com.linkedin.darkcluster.api.NoOpDarkClusterStrategy)

Example 18 with com.linkedin.d2.loadBalancerStrategyType

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

the class PartitionPropertiesConverterTest method testHashMD5PartitionProperties.

@Test
public void testHashMD5PartitionProperties() {
    final String partitionKeyRegex = "/foo/bar/(\\d+)";
    final int partitionCount = 8;
    final HashBasedPartitionProperties.HashAlgorithm hashAlgorithm = HashBasedPartitionProperties.HashAlgorithm.MD5;
    PartitionProperties partitionProperties = new HashBasedPartitionProperties(partitionKeyRegex, partitionCount, hashAlgorithm);
    D2ClusterPartitionConfiguration.PartitionTypeSpecificData data = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
    data.setHashAlgorithm(com.linkedin.d2.HashAlgorithm.MD5);
    D2ClusterPartitionConfiguration partitionConfig = new D2ClusterPartitionConfiguration().setType(PartitionTypeEnum.HASH).setPartitionKeyRegex(partitionKeyRegex).setPartitionCount(partitionCount).setPartitionTypeSpecificData(data);
    Assert.assertEquals(PartitionPropertiesConverter.toProperties(partitionConfig), partitionProperties);
    Assert.assertEquals(PartitionPropertiesConverter.toConfig(partitionProperties), partitionConfig);
}
Also used : PartitionProperties(com.linkedin.d2.balancer.properties.PartitionProperties) RangedPartitionProperties(com.linkedin.d2.RangedPartitionProperties) NullPartitionProperties(com.linkedin.d2.balancer.properties.NullPartitionProperties) HashBasedPartitionProperties(com.linkedin.d2.balancer.properties.HashBasedPartitionProperties) RangeBasedPartitionProperties(com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties) HashBasedPartitionProperties(com.linkedin.d2.balancer.properties.HashBasedPartitionProperties) D2ClusterPartitionConfiguration(com.linkedin.d2.D2ClusterPartitionConfiguration) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)15 HashMap (java.util.HashMap)14 LoadBalancerStrategyTestRunner (com.linkedin.d2.balancer.strategies.framework.LoadBalancerStrategyTestRunner)13 LoadBalancerStrategyTestRunnerBuilder (com.linkedin.d2.balancer.strategies.framework.LoadBalancerStrategyTestRunnerBuilder)13 D2RelativeStrategyProperties (com.linkedin.d2.D2RelativeStrategyProperties)11 URI (java.net.URI)6 D2QuarantineProperties (com.linkedin.d2.D2QuarantineProperties)4 D2ClusterPartitionConfiguration (com.linkedin.d2.D2ClusterPartitionConfiguration)2 RangedPartitionProperties (com.linkedin.d2.RangedPartitionProperties)2 HashBasedPartitionProperties (com.linkedin.d2.balancer.properties.HashBasedPartitionProperties)2 NullPartitionProperties (com.linkedin.d2.balancer.properties.NullPartitionProperties)2 PartitionProperties (com.linkedin.d2.balancer.properties.PartitionProperties)2 RangeBasedPartitionProperties (com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties)2 ArrayList (java.util.ArrayList)2 DarkClusterStrategyNameArray (com.linkedin.d2.DarkClusterStrategyNameArray)1 HttpStatusCodeRange (com.linkedin.d2.HttpStatusCodeRange)1 HttpStatusCodeRangeArray (com.linkedin.d2.HttpStatusCodeRangeArray)1 LoadBalancerState (com.linkedin.d2.balancer.LoadBalancerState)1 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)1 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)1