Search in sources :

Example 1 with DarkClusterStrategyName

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

the class DarkClustersConverter method toProperties.

@SuppressWarnings("unchecked")
public static Map<String, Object> toProperties(DarkClusterConfigMap config) {
    if (config == null) {
        return Collections.emptyMap();
    } else {
        Map<String, Object> darkProps = new HashMap<>();
        for (Map.Entry<String, DarkClusterConfig> entry : config.entrySet()) {
            String darkClusterName = entry.getKey();
            DarkClusterConfig darkClusterConfig = entry.getValue();
            Map<String, Object> prop = new HashMap<>();
            if (darkClusterConfig.hasMultiplier()) {
                prop.put(PropertyKeys.DARK_CLUSTER_MULTIPLIER, darkClusterConfig.getMultiplier().toString());
            }
            if (darkClusterConfig.hasDispatcherOutboundTargetRate()) {
                prop.put(PropertyKeys.DARK_CLUSTER_OUTBOUND_TARGET_RATE, darkClusterConfig.getDispatcherOutboundTargetRate().toString());
            }
            if (darkClusterConfig.hasDispatcherMaxRequestsToBuffer()) {
                prop.put(PropertyKeys.DARK_CLUSTER_MAX_REQUESTS_TO_BUFFER, darkClusterConfig.getDispatcherMaxRequestsToBuffer().toString());
            }
            if (darkClusterConfig.hasDispatcherBufferedRequestExpiryInSeconds()) {
                prop.put(PropertyKeys.DARK_CLUSTER_BUFFERED_REQUEST_EXPIRY_IN_SECONDS, darkClusterConfig.getDispatcherBufferedRequestExpiryInSeconds().toString());
            }
            if (darkClusterConfig.hasDarkClusterStrategyPrioritizedList()) {
                DarkClusterStrategyNameArray strategyNameArray = darkClusterConfig.getDarkClusterStrategyPrioritizedList();
                List<String> strategyList = new ArrayList<>();
                for (DarkClusterStrategyName type : strategyNameArray) {
                    strategyList.add(type.toString());
                }
                prop.put(PropertyKeys.DARK_CLUSTER_STRATEGY_LIST, strategyList);
            }
            if (darkClusterConfig.hasTransportClientProperties()) {
                prop.put(PropertyKeys.DARK_CLUSTER_TRANSPORT_CLIENT_PROPERTIES, TransportClientPropertiesConverter.toProperties(darkClusterConfig.getTransportClientProperties()));
            }
            darkProps.put(darkClusterName, prop);
        }
        return darkProps;
    }
}
Also used : HashMap(java.util.HashMap) DarkClusterStrategyNameArray(com.linkedin.d2.DarkClusterStrategyNameArray) DarkClusterStrategyName(com.linkedin.d2.DarkClusterStrategyName) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) ArrayList(java.util.ArrayList) DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with DarkClusterStrategyName

use of com.linkedin.d2.DarkClusterStrategyName 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 3 with DarkClusterStrategyName

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

the class DarkClustersConverterTest method testBadStrategies.

@Test
public void testBadStrategies() {
    Map<String, Object> props = new HashMap<>();
    List<String> myStrategyList = new ArrayList<>();
    myStrategyList.add("RELATIVE_TRAFFIC");
    myStrategyList.add("BLAH_BLAH");
    Map<String, Object> darkClusterMap = new HashMap<>();
    darkClusterMap.put(PropertyKeys.DARK_CLUSTER_STRATEGY_LIST, myStrategyList);
    props.put(DARK_CLUSTER_KEY, darkClusterMap);
    DarkClusterConfigMap configMap = DarkClustersConverter.toConfig(props);
    DarkClusterStrategyNameArray strategyList = configMap.get(DARK_CLUSTER_KEY).getDarkClusterStrategyPrioritizedList();
    Assert.assertEquals(strategyList.get(0), DarkClusterStrategyName.RELATIVE_TRAFFIC, "first strategy should be RELATIVE_TRAFFIC");
    // the bad strategy BLAH_BLAH gets converted to unknown on access
    Assert.assertEquals(strategyList.get(1), DarkClusterStrategyName.$UNKNOWN, "second strategy should be unknown");
}
Also used : DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) HashMap(java.util.HashMap) DarkClusterStrategyNameArray(com.linkedin.d2.DarkClusterStrategyNameArray) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Aggregations

DarkClusterStrategyNameArray (com.linkedin.d2.DarkClusterStrategyNameArray)3 DarkClusterConfigMap (com.linkedin.d2.DarkClusterConfigMap)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 DarkClusterConfig (com.linkedin.d2.DarkClusterConfig)1 DarkClusterStrategyName (com.linkedin.d2.DarkClusterStrategyName)1 BaseDarkClusterDispatcher (com.linkedin.darkcluster.api.BaseDarkClusterDispatcher)1 NoOpDarkClusterStrategy (com.linkedin.darkcluster.api.NoOpDarkClusterStrategy)1 ConstantQpsRateLimiter (com.linkedin.r2.transport.http.client.ConstantQpsRateLimiter)1 Map (java.util.Map)1 Test (org.testng.annotations.Test)1