Search in sources :

Example 1 with DarkClusterConfig

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

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

the class DarkClustersConverterTest method testDarkClustersConverter.

@Test(dataProvider = "provideKeys")
public void testDarkClustersConverter(boolean successExpected, DarkClusterConfig darkClusterConfig) {
    DarkClusterConfigMap configMap = new DarkClusterConfigMap();
    configMap.put(DARK_CLUSTER_KEY, darkClusterConfig);
    try {
        Assert.assertEquals(DarkClustersConverter.toConfig(DarkClustersConverter.toProperties(configMap)), configMap);
    } catch (Exception | AssertionError e) {
        if (successExpected) {
            Assert.fail("expected success for conversion of: " + darkClusterConfig, e);
        }
    }
}
Also used : DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) Test(org.testng.annotations.Test)

Example 3 with DarkClusterConfig

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

the class DarkClustersConverterTest method testMultipleStrategies.

@Test
public void testMultipleStrategies() {
    DarkClusterConfigMap configMap = new DarkClusterConfigMap();
    DarkClusterStrategyNameArray darkClusterStrategyNameArray = new DarkClusterStrategyNameArray();
    darkClusterStrategyNameArray.add(DarkClusterStrategyName.RELATIVE_TRAFFIC);
    darkClusterStrategyNameArray.add(DarkClusterStrategyName.CONSTANT_QPS);
    DarkClusterConfig config = new DarkClusterConfig().setDarkClusterStrategyPrioritizedList(darkClusterStrategyNameArray);
    configMap.put(DARK_CLUSTER_KEY, config);
    // these are defaults that will be set if the fields are missing.
    config.setMultiplier(DARK_CLUSTER_DEFAULT_MULTIPLIER);
    DarkClusterConfigMap expectedConfigMap = new DarkClusterConfigMap();
    DarkClusterConfig expectedConfig = new DarkClusterConfig(config.data());
    expectedConfig.setMultiplier(0);
    expectedConfig.setDispatcherOutboundTargetRate(0);
    expectedConfig.setDispatcherMaxRequestsToBuffer(1);
    expectedConfig.setDispatcherBufferedRequestExpiryInSeconds(1);
    expectedConfigMap.put(DARK_CLUSTER_KEY, expectedConfig);
    DarkClusterConfigMap resultConfigMap = DarkClustersConverter.toConfig(DarkClustersConverter.toProperties(configMap));
    Assert.assertEquals(resultConfigMap, expectedConfigMap);
    Assert.assertEquals(resultConfigMap.get(DARK_CLUSTER_KEY).getDarkClusterStrategyPrioritizedList().get(0), DarkClusterStrategyName.RELATIVE_TRAFFIC, "expected first strategy to be RELATIVE_TRAFFIC");
    Assert.assertEquals(resultConfigMap.get(DARK_CLUSTER_KEY).getDarkClusterStrategyPrioritizedList().get(1), DarkClusterStrategyName.CONSTANT_QPS, "expected first strategy to be CONSTANT_QPS");
}
Also used : DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) DarkClusterStrategyNameArray(com.linkedin.d2.DarkClusterStrategyNameArray) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) Test(org.testng.annotations.Test)

Example 4 with DarkClusterConfig

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

the class SimpleLoadBalancerTest method testClusterInfoProviderGetDarkClusters.

@Test
public void testClusterInfoProviderGetDarkClusters() throws InterruptedException, ExecutionException, ServiceUnavailableException {
    int numHttp = 3;
    int numHttps = 4;
    int partitionIdForAdd = 0;
    MockStore<ServiceProperties> serviceRegistry = new MockStore<>();
    MockStore<ClusterProperties> clusterRegistry = new MockStore<>();
    MockStore<UriProperties> uriRegistry = new MockStore<>();
    SimpleLoadBalancer loadBalancer = setupLoadBalancer(null, serviceRegistry, clusterRegistry, uriRegistry);
    DarkClusterConfig darkClusterConfig = new DarkClusterConfig().setMultiplier(1.0f).setDispatcherOutboundTargetRate(1).setDispatcherMaxRequestsToBuffer(1).setDispatcherBufferedRequestExpiryInSeconds(1);
    DarkClusterConfigMap darkClusterConfigMap = new DarkClusterConfigMap();
    darkClusterConfigMap.put(DARK_CLUSTER1_NAME, darkClusterConfig);
    clusterRegistry.put(CLUSTER1_NAME, new ClusterProperties(CLUSTER1_NAME, Collections.emptyList(), Collections.emptyMap(), Collections.emptySet(), NullPartitionProperties.getInstance(), Collections.emptyList(), DarkClustersConverter.toProperties(darkClusterConfigMap), false));
    populateUriRegistry(numHttp, numHttps, partitionIdForAdd, uriRegistry);
    loadBalancer.getDarkClusterConfigMap(CLUSTER1_NAME, new Callback<DarkClusterConfigMap>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("getDarkClusterConfigMap threw exception", e);
        }

        @Override
        public void onSuccess(DarkClusterConfigMap returnedDarkClusterConfigMap) {
            Assert.assertEquals(returnedDarkClusterConfigMap, darkClusterConfigMap, "dark cluster configs should be equal");
            Assert.assertEquals(returnedDarkClusterConfigMap.get(DARK_CLUSTER1_NAME).getMultiplier(), 1.0f, "multiplier should match");
        }
    });
}
Also used : MockStore(com.linkedin.d2.discovery.stores.mock.MockStore) DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Test(org.testng.annotations.Test)

Example 5 with DarkClusterConfig

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

the class TestDarkClusterStrategyFactory method testStrategyFallThruWithNoFallback.

@Test
public void testStrategyFallThruWithNoFallback() {
    DarkClusterConfig darkClusterConfig1 = createRelativeTrafficMultiplierConfig(0.5f);
    DarkClusterStrategyNameArray darkClusterStrategyList = new DarkClusterStrategyNameArray();
    // Only ConstantQPS strategy is present, with no alternative.
    darkClusterStrategyList.addAll(Collections.singletonList(CONSTANT_QPS));
    darkClusterConfig1.setDarkClusterStrategyPrioritizedList(darkClusterStrategyList);
    _clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig1);
    _clusterInfoProvider.notifyListenersClusterAdded(SOURCE_CLUSTER_NAME);
    DarkClusterStrategy strategy = _strategyFactory.get(DARK_CLUSTER_NAME);
    // test that we didn't find a strategy corresponding to Constant QPS and fell through. It will end up with the NoOpStrategy.
    Assert.assertTrue(strategy instanceof NoOpDarkClusterStrategy);
}
Also used : DarkClusterStrategyNameArray(com.linkedin.d2.DarkClusterStrategyNameArray) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) NoOpDarkClusterStrategy(com.linkedin.darkcluster.api.NoOpDarkClusterStrategy) RelativeTrafficMultiplierDarkClusterStrategy(com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy) DarkClusterStrategy(com.linkedin.darkcluster.api.DarkClusterStrategy) NoOpDarkClusterStrategy(com.linkedin.darkcluster.api.NoOpDarkClusterStrategy) 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