use of com.linkedin.d2.HttpStatusCodeRange in project rest.li by linkedin.
the class RelativeStrategyPropertiesConverter method toProperties.
/**
* Convert from map to {@link D2RelativeStrategyProperties}
*
* @param properties key-value map that defines the relative load balancer related properties
* @return The converted {@link D2RelativeStrategyProperties}
*/
@SuppressWarnings({ "unchecked" })
public static D2RelativeStrategyProperties toProperties(Map<String, Object> properties) {
D2RelativeStrategyProperties config = new D2RelativeStrategyProperties();
if (properties.containsKey(PropertyKeys.UP_STEP)) {
config.setUpStep(coerce(properties.get(PropertyKeys.UP_STEP), Double.class));
}
if (properties.containsKey(PropertyKeys.DOWN_STEP)) {
config.setDownStep(coerce(properties.get(PropertyKeys.DOWN_STEP), Double.class));
}
if (properties.containsKey(PropertyKeys.RELATIVE_LATENCY_HIGH_THRESHOLD_FACTOR)) {
config.setRelativeLatencyHighThresholdFactor(coerce(properties.get(PropertyKeys.RELATIVE_LATENCY_HIGH_THRESHOLD_FACTOR), Double.class));
}
if (properties.containsKey(PropertyKeys.RELATIVE_LATENCY_LOW_THRESHOLD_FACTOR)) {
config.setRelativeLatencyLowThresholdFactor(coerce(properties.get(PropertyKeys.RELATIVE_LATENCY_LOW_THRESHOLD_FACTOR), Double.class));
}
if (properties.containsKey(PropertyKeys.HIGH_ERROR_RATE)) {
config.setHighErrorRate(coerce(properties.get(PropertyKeys.HIGH_ERROR_RATE), Double.class));
}
if (properties.containsKey(PropertyKeys.LOW_ERROR_RATE)) {
config.setLowErrorRate(coerce(properties.get(PropertyKeys.LOW_ERROR_RATE), Double.class));
}
if (properties.containsKey(PropertyKeys.MIN_CALL_COUNT)) {
config.setMinCallCount(coerce(properties.get(PropertyKeys.MIN_CALL_COUNT), Integer.class));
}
if (properties.containsKey(PropertyKeys.UPDATE_INTERVAL_MS)) {
config.setUpdateIntervalMs(coerce(properties.get(PropertyKeys.UPDATE_INTERVAL_MS), Long.class));
}
if (properties.containsKey(PropertyKeys.INITIAL_HEALTH_SCORE)) {
config.setInitialHealthScore(coerce(properties.get(PropertyKeys.INITIAL_HEALTH_SCORE), Double.class));
}
if (properties.containsKey(PropertyKeys.SLOW_START_THRESHOLD)) {
config.setSlowStartThreshold(coerce(properties.get(PropertyKeys.SLOW_START_THRESHOLD), Double.class));
}
if (properties.containsKey(PropertyKeys.EMITTING_INTERVAL_MS)) {
config.setEmittingIntervalMs(coerce(properties.get(PropertyKeys.EMITTING_INTERVAL_MS), Long.class));
}
if (properties.containsKey(PropertyKeys.ENABLE_FAST_RECOVERY)) {
config.setEnableFastRecovery(coerce(properties.get(PropertyKeys.ENABLE_FAST_RECOVERY), Boolean.class));
}
if (properties.containsKey(PropertyKeys.ERROR_STATUS_FILTER)) {
HttpStatusCodeRangeArray array = new HttpStatusCodeRangeArray();
List<Map<String, Object>> errorStatusFilterList = (List<Map<String, Object>>) properties.get(PropertyKeys.ERROR_STATUS_FILTER);
for (Map<String, Object> errorStatusRange : errorStatusFilterList) {
HttpStatusCodeRange httpStatusCodeRange = new HttpStatusCodeRange().setUpperBound(coerce(errorStatusRange.get(PropertyKeys.ERROR_STATUS_UPPER_BOUND), Integer.class)).setLowerBound(coerce(errorStatusRange.get(PropertyKeys.ERROR_STATUS_LOWER_BOUND), Integer.class));
array.add(httpStatusCodeRange);
}
config.setErrorStatusFilter(array);
}
if (properties.containsKey(PropertyKeys.QUARANTINE_PROPERTIES)) {
config.setQuarantineProperties(toQuarantineProperties((Map<String, Object>) properties.get(PropertyKeys.QUARANTINE_PROPERTIES)));
}
if (properties.containsKey(PropertyKeys.RING_PROPERTIES)) {
config.setRingProperties(toRingProperties((Map<String, Object>) properties.get(PropertyKeys.RING_PROPERTIES)));
}
return config;
}
use of com.linkedin.d2.HttpStatusCodeRange in project rest.li by linkedin.
the class TestLoadBalancerStrategy method testErrorStatusMatch.
@Test(dataProvider = "strategy")
public void testErrorStatusMatch(loadBalancerStrategyType type) {
Map<String, Object> strategyPropertiesWithErrorFilter = new HashMap<>();
D2RelativeStrategyProperties relativePropertiesWithErrorFilter = new D2RelativeStrategyProperties();
// Only 503 is counted as error
strategyPropertiesWithErrorFilter.put(PropertyKeys.HTTP_LB_ERROR_STATUS_REGEX, "(503)");
relativePropertiesWithErrorFilter.setErrorStatusFilter(new HttpStatusCodeRangeArray(Arrays.asList(new HttpStatusCodeRange().setLowerBound(503).setUpperBound(503))));
LoadBalancerStrategyTestRunnerBuilder builder = new LoadBalancerStrategyTestRunnerBuilder(type, DEFAULT_SERVICE_NAME, DEFAULT_NUM_HOSTS).setConstantRequestCount(DEFAULT_REQUESTS_PER_INTERVAL).setNumIntervals(6).setConstantLatency(Arrays.asList(HEALTHY_HOST_CONSTANT_LATENCY, HEALTHY_HOST_CONSTANT_LATENCY, HEALTHY_HOST_CONSTANT_LATENCY, HEALTHY_HOST_CONSTANT_LATENCY, HEALTHY_HOST_CONSTANT_LATENCY)).setConstantErrorCount(Arrays.asList(UNHEALTHY_ERROR_COUNT, HEALTHY_ERROR_COUNT, HEALTHY_ERROR_COUNT, HEALTHY_ERROR_COUNT, HEALTHY_ERROR_COUNT));
LoadBalancerStrategyTestRunner testRunner = type == loadBalancerStrategyType.DEGRADER ? builder.setDegraderStrategies(strategyPropertiesWithErrorFilter, new HashMap<>()).build() : builder.setRelativeLoadBalancerStrategies(relativePropertiesWithErrorFilter).build();
testRunner.runWait();
Map<URI, Integer> pointsMap = testRunner.getPoints();
// Event with the error 500, the host is not marked as unhealthy
assertEquals(pointsMap.get(testRunner.getUri(0)).intValue(), HEALTHY_POINTS);
assertEquals(pointsMap.get(testRunner.getUri(1)).intValue(), HEALTHY_POINTS);
assertEquals(pointsMap.get(testRunner.getUri(2)).intValue(), HEALTHY_POINTS);
assertEquals(pointsMap.get(testRunner.getUri(3)).intValue(), HEALTHY_POINTS);
assertEquals(pointsMap.get(testRunner.getUri(4)).intValue(), HEALTHY_POINTS);
}
use of com.linkedin.d2.HttpStatusCodeRange in project rest.li by linkedin.
the class RelativeStrategyPropertiesConverterTest method testRelativeStrategyPropertiesConverter.
@Test
public void testRelativeStrategyPropertiesConverter() {
double upStep = 0.2;
double downStep = 0.1;
double relativeLatencyHighThresholdFactor = 1.5;
double relativeLatencyLowThresholdFactor = 1.2;
double highErrorRate = 0.2;
double lowErrorRate = 0.1;
int minCallCount = 1000;
long updateIntervalMs = 5000;
double initialHealthScore = 0.0;
double slowStartThreshold = 0.32;
HttpStatusCodeRangeArray errorStatusRange = new HttpStatusCodeRangeArray(new HttpStatusCodeRange().setLowerBound(500).setUpperBound(599));
int emittingIntervalMs = 5000;
double quarantineMaxPercent = 0.1;
HttpMethod quarantineMethod = HttpMethod.OPTIONS;
String healthCheckPath = "";
int pointsPerWeight = 100;
HashMethod hashMethod = HashMethod.URI_REGEX;
StringArray regexes = new StringArray("+231{w+)");
boolean failOnNoMatch = false;
boolean warnOnNoMatch = true;
double hashringPointCleanupRate = 0.2;
ConsistentHashAlgorithm consistentHashAlgorithm = ConsistentHashAlgorithm.POINT_BASED;
int numberOfProbes = 1024;
int numberOfPointsPerHost = 1;
double boundedLoadBalancingFactor = 1.5;
D2QuarantineProperties quarantineProperties = new D2QuarantineProperties().setQuarantineMaxPercent(quarantineMaxPercent).setHealthCheckMethod(quarantineMethod).setHealthCheckPath(healthCheckPath);
HashConfig hashConfig = new HashConfig().setFailOnNoMatch(failOnNoMatch).setUriRegexes(regexes).setWarnOnNoMatch(warnOnNoMatch);
D2RingProperties ringProperties = new D2RingProperties().setHashRingPointCleanupRate(hashringPointCleanupRate).setBoundedLoadBalancingFactor(boundedLoadBalancingFactor).setConsistentHashAlgorithm(consistentHashAlgorithm).setHashConfig(hashConfig).setHashMethod(hashMethod).setPointsPerWeight(pointsPerWeight).setNumberOfProbes(numberOfProbes).setNumberOfPointsPerHost(numberOfPointsPerHost);
D2RelativeStrategyProperties properties = new D2RelativeStrategyProperties().setQuarantineProperties(quarantineProperties).setRingProperties(ringProperties).setUpStep(upStep).setDownStep(downStep).setRelativeLatencyHighThresholdFactor(relativeLatencyHighThresholdFactor).setRelativeLatencyLowThresholdFactor(relativeLatencyLowThresholdFactor).setHighErrorRate(highErrorRate).setLowErrorRate(lowErrorRate).setMinCallCount(minCallCount).setUpdateIntervalMs(updateIntervalMs).setInitialHealthScore(initialHealthScore).setSlowStartThreshold(slowStartThreshold).setErrorStatusFilter(errorStatusRange).setEmittingIntervalMs(emittingIntervalMs);
Map<String, Object> propertyMap = new HashMap<>();
Map<String, Object> ringPropertyMap = new HashMap<>();
Map<String, Object> quarantinePropertyMap = new HashMap<>();
Map<String, Object> hashConfigMap = new HashMap<>();
Map<String, String> errorStatusRangeMap = new HashMap<>();
quarantinePropertyMap.put(PropertyKeys.QUARANTINE_MAX_PERCENT, String.valueOf(quarantineMaxPercent));
quarantinePropertyMap.put(PropertyKeys.QUARANTINE_HEALTH_CHECK_METHOD, quarantineMethod.toString());
quarantinePropertyMap.put(PropertyKeys.QUARANTINE_HEALTH_CHECK_PATH, healthCheckPath);
hashConfigMap.put(URIRegexHash.KEY_REGEXES, new ArrayList<>(regexes));
hashConfigMap.put(URIRegexHash.KEY_WARN_ON_NO_MATCH, String.valueOf(warnOnNoMatch));
hashConfigMap.put(URIRegexHash.KEY_FAIL_ON_NO_MATCH, String.valueOf(failOnNoMatch));
ringPropertyMap.put(PropertyKeys.RING_HASH_RING_POINT_CLEANUP_RATE, String.valueOf(hashringPointCleanupRate));
ringPropertyMap.put(PropertyKeys.RING_BOUNDED_LOAD_BALANCING_FACTOR, String.valueOf(boundedLoadBalancingFactor));
ringPropertyMap.put(PropertyKeys.RING_CONSISTENT_HASH_ALGORITHM, DelegatingRingFactory.POINT_BASED_CONSISTENT_HASH);
ringPropertyMap.put(PropertyKeys.RING_HASH_CONFIG, hashConfigMap);
ringPropertyMap.put(PropertyKeys.RING_HASH_METHOD, RelativeLoadBalancerStrategy.HASH_METHOD_URI_REGEX);
ringPropertyMap.put(PropertyKeys.RING_POINTS_PER_WEIGHT, String.valueOf(pointsPerWeight));
ringPropertyMap.put(PropertyKeys.RING_NUMBER_OF_PROBES, String.valueOf(numberOfProbes));
ringPropertyMap.put(PropertyKeys.RING_NUMBER_OF_POINTS_PER_HOST, String.valueOf(numberOfPointsPerHost));
errorStatusRangeMap.put(PropertyKeys.ERROR_STATUS_UPPER_BOUND, String.valueOf(errorStatusRange.get(0).getUpperBound()));
errorStatusRangeMap.put(PropertyKeys.ERROR_STATUS_LOWER_BOUND, String.valueOf(errorStatusRange.get(0).getLowerBound()));
propertyMap.put(PropertyKeys.QUARANTINE_PROPERTIES, quarantinePropertyMap);
propertyMap.put(PropertyKeys.RING_PROPERTIES, ringPropertyMap);
propertyMap.put(PropertyKeys.UP_STEP, String.valueOf(upStep));
propertyMap.put(PropertyKeys.DOWN_STEP, String.valueOf(downStep));
propertyMap.put(PropertyKeys.RELATIVE_LATENCY_HIGH_THRESHOLD_FACTOR, String.valueOf(relativeLatencyHighThresholdFactor));
propertyMap.put(PropertyKeys.RELATIVE_LATENCY_LOW_THRESHOLD_FACTOR, String.valueOf(relativeLatencyLowThresholdFactor));
propertyMap.put(PropertyKeys.HIGH_ERROR_RATE, String.valueOf(highErrorRate));
propertyMap.put(PropertyKeys.LOW_ERROR_RATE, String.valueOf(lowErrorRate));
propertyMap.put(PropertyKeys.MIN_CALL_COUNT, String.valueOf(minCallCount));
propertyMap.put(PropertyKeys.UPDATE_INTERVAL_MS, String.valueOf(updateIntervalMs));
propertyMap.put(PropertyKeys.INITIAL_HEALTH_SCORE, String.valueOf(initialHealthScore));
propertyMap.put(PropertyKeys.SLOW_START_THRESHOLD, String.valueOf(slowStartThreshold));
propertyMap.put(PropertyKeys.ERROR_STATUS_FILTER, Arrays.asList(errorStatusRangeMap));
propertyMap.put(PropertyKeys.EMITTING_INTERVAL_MS, String.valueOf(emittingIntervalMs));
Assert.assertEquals(RelativeStrategyPropertiesConverter.toMap(properties), propertyMap);
Assert.assertEquals(RelativeStrategyPropertiesConverter.toProperties(propertyMap), properties);
}
use of com.linkedin.d2.HttpStatusCodeRange in project rest.li by linkedin.
the class ServicePropertiesSerializerTest method createRelativeStrategyProperties.
private D2RelativeStrategyProperties createRelativeStrategyProperties() {
double upStep = 0.2;
double downStep = 0.1;
double relativeLatencyHighThresholdFactor = 1.5;
double relativeLatencyLowThresholdFactor = 1.2;
double highErrorRate = 0.2;
double lowErrorRate = 0.1;
int minCallCount = 1000;
long updateIntervalMs = 5000;
double initialHealthScore = 0.0;
double slowStartThreshold = 0.32;
HttpStatusCodeRangeArray errorStatusRange = new HttpStatusCodeRangeArray(new HttpStatusCodeRange().setLowerBound(500).setUpperBound(599));
double quarantineMaxPercent = 0.1;
HttpMethod quarantineMethod = HttpMethod.OPTIONS;
String healthCheckPath = "";
ConsistentHashAlgorithm consistentHashAlgorithm = ConsistentHashAlgorithm.POINT_BASED;
D2QuarantineProperties quarantineProperties = new D2QuarantineProperties().setQuarantineMaxPercent(quarantineMaxPercent).setHealthCheckMethod(quarantineMethod).setHealthCheckPath(healthCheckPath);
D2RingProperties ringProperties = new D2RingProperties().setConsistentHashAlgorithm(consistentHashAlgorithm);
return new D2RelativeStrategyProperties().setQuarantineProperties(quarantineProperties).setRingProperties(ringProperties).setUpStep(upStep).setDownStep(downStep).setRelativeLatencyHighThresholdFactor(relativeLatencyHighThresholdFactor).setRelativeLatencyLowThresholdFactor(relativeLatencyLowThresholdFactor).setHighErrorRate(highErrorRate).setLowErrorRate(lowErrorRate).setMinCallCount(minCallCount).setUpdateIntervalMs(updateIntervalMs).setInitialHealthScore(initialHealthScore).setSlowStartThreshold(slowStartThreshold).setErrorStatusFilter(errorStatusRange);
}
use of com.linkedin.d2.HttpStatusCodeRange in project rest.li by linkedin.
the class RelativeStrategyPropertiesConverter method toMap.
/**
* Convert {@link D2RelativeStrategyProperties} to Map
*
* @param properties relative strategy properties
* @return The converted key-value map
*/
public static Map<String, Object> toMap(D2RelativeStrategyProperties properties) {
if (properties == null) {
return Collections.emptyMap();
}
Map<String, Object> map = new HashMap<>();
if (properties.hasUpStep()) {
map.put(PropertyKeys.UP_STEP, properties.getUpStep().toString());
}
if (properties.hasDownStep()) {
map.put(PropertyKeys.DOWN_STEP, properties.getDownStep().toString());
}
if (properties.hasRelativeLatencyHighThresholdFactor()) {
map.put(PropertyKeys.RELATIVE_LATENCY_HIGH_THRESHOLD_FACTOR, properties.getRelativeLatencyHighThresholdFactor().toString());
}
if (properties.hasRelativeLatencyLowThresholdFactor()) {
map.put(PropertyKeys.RELATIVE_LATENCY_LOW_THRESHOLD_FACTOR, properties.getRelativeLatencyLowThresholdFactor().toString());
}
if (properties.hasHighErrorRate()) {
map.put(PropertyKeys.HIGH_ERROR_RATE, properties.getHighErrorRate().toString());
}
if (properties.hasLowErrorRate()) {
map.put(PropertyKeys.LOW_ERROR_RATE, properties.getLowErrorRate().toString());
}
if (properties.hasMinCallCount()) {
map.put(PropertyKeys.MIN_CALL_COUNT, properties.getMinCallCount().toString());
}
if (properties.hasUpdateIntervalMs()) {
map.put(PropertyKeys.UPDATE_INTERVAL_MS, properties.getUpdateIntervalMs().toString());
}
if (properties.hasInitialHealthScore()) {
map.put(PropertyKeys.INITIAL_HEALTH_SCORE, properties.getInitialHealthScore().toString());
}
if (properties.hasSlowStartThreshold()) {
map.put(PropertyKeys.SLOW_START_THRESHOLD, properties.getSlowStartThreshold().toString());
}
if (properties.hasEmittingIntervalMs()) {
map.put(PropertyKeys.EMITTING_INTERVAL_MS, properties.getEmittingIntervalMs().toString());
}
if (properties.hasEnableFastRecovery()) {
map.put(PropertyKeys.ENABLE_FAST_RECOVERY, properties.isEnableFastRecovery().toString());
}
if (properties.hasErrorStatusFilter()) {
List<Map<String, Object>> errorStatusFilterList = new ArrayList<>();
for (HttpStatusCodeRange errorStatusRange : properties.getErrorStatusFilter()) {
Map<String, Object> errorStatusFilterMap = new HashMap<>();
errorStatusFilterMap.put(PropertyKeys.ERROR_STATUS_LOWER_BOUND, errorStatusRange.getLowerBound().toString());
errorStatusFilterMap.put(PropertyKeys.ERROR_STATUS_UPPER_BOUND, errorStatusRange.getUpperBound().toString());
errorStatusFilterList.add(errorStatusFilterMap);
}
map.put(PropertyKeys.ERROR_STATUS_FILTER, errorStatusFilterList);
}
if (properties.hasQuarantineProperties()) {
D2QuarantineProperties quarantineProperties = properties.getQuarantineProperties();
Map<String, Object> quarantinePropertyMap = toQuarantinePropertyMap(quarantineProperties);
map.put(PropertyKeys.QUARANTINE_PROPERTIES, quarantinePropertyMap);
}
if (properties.hasRingProperties()) {
D2RingProperties ringProperties = properties.getRingProperties();
Map<String, Object> ringPropertyMap = toRingPropertyMap(ringProperties);
map.put(PropertyKeys.RING_PROPERTIES, ringPropertyMap);
}
return map;
}
Aggregations