Search in sources :

Example 11 with DarkClusterConfigMap

use of com.linkedin.d2.DarkClusterConfigMap 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 12 with DarkClusterConfigMap

use of com.linkedin.d2.DarkClusterConfigMap 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 13 with DarkClusterConfigMap

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

Example 14 with DarkClusterConfigMap

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

Example 15 with DarkClusterConfigMap

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

the class DarkClustersConverterTest method testEntriesInClusterConfig.

@Test
public void testEntriesInClusterConfig() {
    DarkClusterConfigMap configMap = new DarkClusterConfigMap();
    DarkClusterStrategyNameArray multiplierStrategyTypeArray = new DarkClusterStrategyNameArray();
    multiplierStrategyTypeArray.add(DarkClusterStrategyName.RELATIVE_TRAFFIC.RELATIVE_TRAFFIC);
    D2TransportClientProperties transportClientProperties = new D2TransportClientProperties().setRequestTimeout(1000);
    DarkClusterConfig config = new DarkClusterConfig().setDarkClusterStrategyPrioritizedList(multiplierStrategyTypeArray).setTransportClientProperties(transportClientProperties);
    configMap.put(DARK_CLUSTER_KEY, config);
    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);
    // verify values are converted properly.
    DarkClusterConfig darkClusterConfig = resultConfigMap.get(DARK_CLUSTER_KEY);
    Assert.assertEquals(darkClusterConfig.getMultiplier(), DARK_CLUSTER_DEFAULT_MULTIPLIER, "unexpected multiplier");
    Assert.assertEquals(darkClusterConfig.getDarkClusterStrategyPrioritizedList().size(), 1, "there should be one strategy");
    Assert.assertEquals(darkClusterConfig.getDarkClusterStrategyPrioritizedList().get(0), DarkClusterStrategyName.RELATIVE_TRAFFIC, "expected RELATIVE_TRAFFIC strategy");
    Assert.assertTrue(darkClusterConfig.hasTransportClientProperties());
    D2TransportClientProperties returnedTransportClientProperties = darkClusterConfig.getTransportClientProperties();
    Assert.assertNotNull(returnedTransportClientProperties);
    Assert.assertTrue(returnedTransportClientProperties.hasRequestTimeout());
    Assert.assertEquals(Objects.requireNonNull(returnedTransportClientProperties.getRequestTimeout()).longValue(), 1000, "expected 1000 request Timeout");
}
Also used : DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) DarkClusterStrategyNameArray(com.linkedin.d2.DarkClusterStrategyNameArray) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) D2TransportClientProperties(com.linkedin.d2.D2TransportClientProperties) Test(org.testng.annotations.Test)

Aggregations

DarkClusterConfigMap (com.linkedin.d2.DarkClusterConfigMap)17 Test (org.testng.annotations.Test)10 DarkClusterConfig (com.linkedin.d2.DarkClusterConfig)8 DarkClusterStrategyNameArray (com.linkedin.d2.DarkClusterStrategyNameArray)5 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 D2TransportClientProperties (com.linkedin.d2.D2TransportClientProperties)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 Map (java.util.Map)2 FutureCallback (com.linkedin.common.callback.FutureCallback)1 DarkClusterStrategyName (com.linkedin.d2.DarkClusterStrategyName)1 DarkClusterStrategy (com.linkedin.darkcluster.api.DarkClusterStrategy)1 DataList (com.linkedin.data.DataList)1 RequestContext (com.linkedin.r2.message.RequestContext)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 TimeoutCallback (com.linkedin.r2.transport.http.client.TimeoutCallback)1 List (java.util.List)1