Search in sources :

Example 21 with DarkClusterConfig

use of com.linkedin.d2.DarkClusterConfig 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 22 with DarkClusterConfig

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

the class MockClusterInfoProvider method addDarkClusterConfig.

/**
 * add the ability to add a dark cluster to a source cluster's darkClusterConfigMap
 */
void addDarkClusterConfig(String sourceClusterName, String darkClusterName, DarkClusterConfig darkClusterConfig) {
    DarkClusterConfigMap darkClusterConfigMap = (lookupMap.containsKey(sourceClusterName)) ? lookupMap.get(sourceClusterName) : new DarkClusterConfigMap();
    darkClusterConfigMap.put(darkClusterName, darkClusterConfig);
    lookupMap.put(sourceClusterName, darkClusterConfigMap);
}
Also used : DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap)

Example 23 with DarkClusterConfig

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

the class DarkClustersConverter method toConfig.

public static DarkClusterConfigMap toConfig(Map<String, Object> properties) {
    DarkClusterConfigMap configMap = new DarkClusterConfigMap();
    for (Map.Entry<String, Object> entry : properties.entrySet()) {
        String darkClusterName = entry.getKey();
        DarkClusterConfig darkClusterConfig = new DarkClusterConfig();
        @SuppressWarnings("unchecked") Map<String, Object> props = (Map<String, Object>) entry.getValue();
        if (props.containsKey(PropertyKeys.DARK_CLUSTER_MULTIPLIER)) {
            darkClusterConfig.setMultiplier(PropertyUtil.coerce(props.get(PropertyKeys.DARK_CLUSTER_MULTIPLIER), Float.class));
        } else {
            // to maintain backwards compatibility with previously ser/de, set the default on deserialization
            darkClusterConfig.setMultiplier(DARK_CLUSTER_DEFAULT_MULTIPLIER);
        }
        if (props.containsKey(PropertyKeys.DARK_CLUSTER_OUTBOUND_TARGET_RATE)) {
            darkClusterConfig.setDispatcherOutboundTargetRate(PropertyUtil.coerce(props.get(PropertyKeys.DARK_CLUSTER_OUTBOUND_TARGET_RATE), Float.class));
        } else {
            darkClusterConfig.setDispatcherOutboundTargetRate(DARK_CLUSTER_DEFAULT_TARGET_RATE);
        }
        if (props.containsKey(PropertyKeys.DARK_CLUSTER_MAX_REQUESTS_TO_BUFFER)) {
            darkClusterConfig.setDispatcherMaxRequestsToBuffer(PropertyUtil.coerce(props.get(PropertyKeys.DARK_CLUSTER_MAX_REQUESTS_TO_BUFFER), Integer.class));
        } else {
            darkClusterConfig.setDispatcherMaxRequestsToBuffer(DARK_CLUSTER_DEFAULT_MAX_REQUESTS_TO_BUFFER);
        }
        if (props.containsKey(PropertyKeys.DARK_CLUSTER_BUFFERED_REQUEST_EXPIRY_IN_SECONDS)) {
            darkClusterConfig.setDispatcherBufferedRequestExpiryInSeconds(PropertyUtil.coerce(props.get(PropertyKeys.DARK_CLUSTER_BUFFERED_REQUEST_EXPIRY_IN_SECONDS), Integer.class));
        } else {
            darkClusterConfig.setDispatcherBufferedRequestExpiryInSeconds(DARK_CLUSTER_DEFAULT_BUFFERED_REQUEST_EXPIRY_IN_SECONDS);
        }
        if (props.containsKey(PropertyKeys.DARK_CLUSTER_STRATEGY_LIST)) {
            DataList dataList = new DataList();
            @SuppressWarnings("unchecked") List<String> strategyList = (List<String>) props.get(PropertyKeys.DARK_CLUSTER_STRATEGY_LIST);
            dataList.addAll(strategyList);
            DarkClusterStrategyNameArray darkClusterStrategyNameArray = new DarkClusterStrategyNameArray(dataList);
            darkClusterConfig.setDarkClusterStrategyPrioritizedList(darkClusterStrategyNameArray);
        }
        if (props.containsKey(PropertyKeys.DARK_CLUSTER_TRANSPORT_CLIENT_PROPERTIES)) {
            @SuppressWarnings("unchecked") D2TransportClientProperties transportClientProperties = TransportClientPropertiesConverter.toConfig((Map<String, Object>) props.get(PropertyKeys.DARK_CLUSTER_TRANSPORT_CLIENT_PROPERTIES));
            darkClusterConfig.setTransportClientProperties(transportClientProperties);
        }
        configMap.put(darkClusterName, darkClusterConfig);
    }
    return configMap;
}
Also used : DarkClusterStrategyNameArray(com.linkedin.d2.DarkClusterStrategyNameArray) D2TransportClientProperties(com.linkedin.d2.D2TransportClientProperties) DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) DataList(com.linkedin.data.DataList) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) DataList(com.linkedin.data.DataList) ArrayList(java.util.ArrayList) List(java.util.List) DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with DarkClusterConfig

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

the class ClusterPropertiesSerializerTest method test2DarkClusterJsonSerializer.

@Test
public void test2DarkClusterJsonSerializer() throws PropertySerializationException {
    ClusterPropertiesJsonSerializer jsonSerializer = new ClusterPropertiesJsonSerializer();
    DarkClusterConfig darkCluster1 = new DarkClusterConfig().setMultiplier(1.5f).setDispatcherBufferedRequestExpiryInSeconds(10).setDispatcherMaxRequestsToBuffer(100).setDispatcherOutboundTargetRate(50);
    DarkClusterConfigMap darkClusterConfigMap = new DarkClusterConfigMap();
    darkClusterConfigMap.put(DARK_CLUSTER1_KEY, darkCluster1);
    DarkClusterConfig darkCluster2 = new DarkClusterConfig().setDispatcherBufferedRequestExpiryInSeconds(10).setDispatcherMaxRequestsToBuffer(100).setDispatcherOutboundTargetRate(50).setMultiplier(0);
    darkClusterConfigMap.put(DARK_CLUSTER2_KEY, darkCluster2);
    ClusterProperties property = new ClusterProperties("test", new ArrayList<>(), new HashMap<>(), new HashSet<>(), NullPartitionProperties.getInstance(), Arrays.asList("principal1", "principal2"), DarkClustersConverter.toProperties(darkClusterConfigMap), false);
    assertEquals(jsonSerializer.fromBytes(jsonSerializer.toBytes(property)), property);
}
Also used : DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) Test(org.testng.annotations.Test)

Example 25 with DarkClusterConfig

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

the class DarkClustersConverterTest method testDarkClustersConverterDefaults.

@Test
public void testDarkClustersConverterDefaults() {
    DarkClusterConfigMap configMap = new DarkClusterConfigMap();
    DarkClusterConfig config = new DarkClusterConfig();
    configMap.put(DARK_CLUSTER_KEY, config);
    DarkClusterConfig resultConfig = DarkClustersConverter.toConfig(DarkClustersConverter.toProperties(configMap)).get(DARK_CLUSTER_KEY);
    Assert.assertEquals(resultConfig.getMultiplier(), DARK_CLUSTER_DEFAULT_MULTIPLIER);
    Assert.assertEquals(resultConfig.getDispatcherOutboundTargetRate(), DARK_CLUSTER_DEFAULT_TARGET_RATE);
    Assert.assertEquals((int) resultConfig.getDispatcherMaxRequestsToBuffer(), DARK_CLUSTER_DEFAULT_MAX_REQUESTS_TO_BUFFER);
    Assert.assertEquals((int) resultConfig.getDispatcherBufferedRequestExpiryInSeconds(), DARK_CLUSTER_DEFAULT_BUFFERED_REQUEST_EXPIRY_IN_SECONDS);
    Assert.assertEquals(resultConfig.getDarkClusterStrategyPrioritizedList().size(), 1, "default strategy list should be size 1");
    Assert.assertFalse(resultConfig.hasTransportClientProperties(), "default shouldn't have transportProperties");
}
Also used : DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) Test(org.testng.annotations.Test)

Aggregations

DarkClusterConfig (com.linkedin.d2.DarkClusterConfig)23 Test (org.testng.annotations.Test)20 DarkClusterConfigMap (com.linkedin.d2.DarkClusterConfigMap)10 NoOpDarkClusterStrategy (com.linkedin.darkcluster.api.NoOpDarkClusterStrategy)10 DarkClusterStrategyNameArray (com.linkedin.d2.DarkClusterStrategyNameArray)9 DarkClusterStrategy (com.linkedin.darkcluster.api.DarkClusterStrategy)9 RelativeTrafficMultiplierDarkClusterStrategy (com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy)8 RequestContext (com.linkedin.r2.message.RequestContext)5 RestRequest (com.linkedin.r2.message.rest.RestRequest)5 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 Facilities (com.linkedin.d2.balancer.Facilities)4 DarkClusterManager (com.linkedin.darkcluster.api.DarkClusterManager)4 DarkClusterManagerImpl (com.linkedin.darkcluster.impl.DarkClusterManagerImpl)4 D2TransportClientProperties (com.linkedin.d2.D2TransportClientProperties)2 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)2 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)2 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)2 MockStore (com.linkedin.d2.discovery.stores.mock.MockStore)2 DarkClusterStrategyFactoryImpl (com.linkedin.darkcluster.impl.DarkClusterStrategyFactoryImpl)2 ConstantQpsRateLimiter (com.linkedin.r2.transport.http.client.ConstantQpsRateLimiter)2