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;
}
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);
}
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");
}
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");
}
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");
}
Aggregations