Search in sources :

Example 1 with D2RingProperties

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

the class RelativeStrategyPropertiesConverter method toRingProperties.

@SuppressWarnings({ "unchecked" })
private static D2RingProperties toRingProperties(Map<String, Object> ringPropertyMap) {
    D2RingProperties ringProperties = new D2RingProperties();
    if (ringPropertyMap.containsKey(PropertyKeys.RING_POINTS_PER_WEIGHT)) {
        ringProperties.setPointsPerWeight(coerce(ringPropertyMap.get(PropertyKeys.RING_POINTS_PER_WEIGHT), Integer.class));
    }
    if (ringPropertyMap.containsKey(PropertyKeys.RING_HASH_METHOD)) {
        String hashMethod = (String) ringPropertyMap.get(PropertyKeys.RING_HASH_METHOD);
        if (HashMethod.URI_REGEX.name().equalsIgnoreCase(hashMethod) || RelativeLoadBalancerStrategy.HASH_METHOD_URI_REGEX.equalsIgnoreCase(hashMethod)) {
            ringProperties.setHashMethod(HashMethod.URI_REGEX);
        } else if (HashMethod.RANDOM.name().equalsIgnoreCase(hashMethod)) {
            ringProperties.setHashMethod(HashMethod.RANDOM);
        }
    }
    if (ringPropertyMap.containsKey(PropertyKeys.RING_HASH_CONFIG)) {
        HashConfig hashConfig = toHashConfig((Map<String, Object>) ringPropertyMap.get(PropertyKeys.RING_HASH_CONFIG));
        ringProperties.setHashConfig(hashConfig);
    }
    if (ringPropertyMap.containsKey(PropertyKeys.RING_HASH_RING_POINT_CLEANUP_RATE)) {
        ringProperties.setHashRingPointCleanupRate(coerce(ringPropertyMap.get(PropertyKeys.RING_HASH_RING_POINT_CLEANUP_RATE), Double.class));
    }
    if (ringPropertyMap.containsKey(PropertyKeys.RING_CONSISTENT_HASH_ALGORITHM)) {
        String consistentHashAlgorithm = (String) ringPropertyMap.get(PropertyKeys.RING_CONSISTENT_HASH_ALGORITHM);
        if (DelegatingRingFactory.POINT_BASED_CONSISTENT_HASH.equalsIgnoreCase(consistentHashAlgorithm)) {
            ringProperties.setConsistentHashAlgorithm(ConsistentHashAlgorithm.POINT_BASED);
        } else if (DelegatingRingFactory.MULTI_PROBE_CONSISTENT_HASH.equalsIgnoreCase(consistentHashAlgorithm)) {
            ringProperties.setConsistentHashAlgorithm(ConsistentHashAlgorithm.MULTI_PROBE);
        } else if (DelegatingRingFactory.DISTRIBUTION_NON_HASH.equalsIgnoreCase(consistentHashAlgorithm)) {
            ringProperties.setConsistentHashAlgorithm(ConsistentHashAlgorithm.DISTRIBUTION_BASED);
        }
    }
    if (ringPropertyMap.containsKey(PropertyKeys.RING_NUMBER_OF_PROBES)) {
        ringProperties.setNumberOfProbes(coerce(ringPropertyMap.get(PropertyKeys.RING_NUMBER_OF_PROBES), Integer.class));
    }
    if (ringPropertyMap.containsKey(PropertyKeys.RING_NUMBER_OF_POINTS_PER_HOST)) {
        ringProperties.setNumberOfPointsPerHost(coerce(ringPropertyMap.get(PropertyKeys.RING_NUMBER_OF_POINTS_PER_HOST), Integer.class));
    }
    if (ringPropertyMap.containsKey(PropertyKeys.RING_BOUNDED_LOAD_BALANCING_FACTOR)) {
        ringProperties.setBoundedLoadBalancingFactor(coerce(ringPropertyMap.get(PropertyKeys.RING_BOUNDED_LOAD_BALANCING_FACTOR), Double.class));
    }
    return ringProperties;
}
Also used : HashConfig(com.linkedin.d2.HashConfig) D2RingProperties(com.linkedin.d2.D2RingProperties)

Example 2 with D2RingProperties

use of com.linkedin.d2.D2RingProperties 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);
}
Also used : HashMethod(com.linkedin.d2.HashMethod) HashMap(java.util.HashMap) HttpStatusCodeRange(com.linkedin.d2.HttpStatusCodeRange) HttpStatusCodeRangeArray(com.linkedin.d2.HttpStatusCodeRangeArray) D2RingProperties(com.linkedin.d2.D2RingProperties) D2QuarantineProperties(com.linkedin.d2.D2QuarantineProperties) StringArray(com.linkedin.data.template.StringArray) ConsistentHashAlgorithm(com.linkedin.d2.ConsistentHashAlgorithm) HashConfig(com.linkedin.d2.HashConfig) HttpMethod(com.linkedin.d2.HttpMethod) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) Test(org.testng.annotations.Test)

Example 3 with D2RingProperties

use of com.linkedin.d2.D2RingProperties 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);
}
Also used : D2QuarantineProperties(com.linkedin.d2.D2QuarantineProperties) ConsistentHashAlgorithm(com.linkedin.d2.ConsistentHashAlgorithm) HttpStatusCodeRange(com.linkedin.d2.HttpStatusCodeRange) HttpStatusCodeRangeArray(com.linkedin.d2.HttpStatusCodeRangeArray) D2RingProperties(com.linkedin.d2.D2RingProperties) HttpMethod(com.linkedin.d2.HttpMethod) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties)

Example 4 with D2RingProperties

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

the class RelativeLoadBalancerStrategyFactory method putDefaultValues.

static D2RelativeStrategyProperties putDefaultValues(D2RelativeStrategyProperties properties) {
    properties.setUpStep(getOrDefault(properties.getUpStep(), DEFAULT_UP_STEP));
    properties.setDownStep(getOrDefault(properties.getDownStep(), DEFAULT_DOWN_STEP));
    properties.setHighErrorRate(getOrDefault(properties.getHighErrorRate(), DEFAULT_HIGH_ERROR_RATE));
    properties.setLowErrorRate(getOrDefault(properties.getLowErrorRate(), DEFAULT_LOW_ERROR_RATE));
    properties.setRelativeLatencyHighThresholdFactor(getOrDefault(properties.getRelativeLatencyHighThresholdFactor(), DEFAULT_RELATIVE_LATENCY_HIGH_THRESHOLD_FACTOR));
    properties.setRelativeLatencyLowThresholdFactor(getOrDefault(properties.getRelativeLatencyLowThresholdFactor(), DEFAULT_RELATIVE_LATENCY_LOW_THRESHOLD_FACTOR));
    properties.setMinCallCount(getOrDefault(properties.getMinCallCount(), DEFAULT_MIN_CALL_COUNT));
    properties.setUpdateIntervalMs(getOrDefault(properties.getUpdateIntervalMs(), DEFAULT_UPDATE_INTERVAL_MS));
    properties.setInitialHealthScore(getOrDefault(properties.getInitialHealthScore(), DEFAULT_INITIAL_HEALTH_SCORE));
    properties.setSlowStartThreshold(getOrDefault(properties.getSlowStartThreshold(), DEFAULT_SLOW_START_THRESHOLD));
    properties.setErrorStatusFilter(getOrDefault(properties.getErrorStatusFilter(), DEFAULT_ERROR_STATUS_FILTER));
    properties.setEmittingIntervalMs(getOrDefault(properties.getEmittingIntervalMs(), DEFAULT_EMITTING_INTERVAL_MS));
    properties.setEnableFastRecovery(getOrDefault(properties.isEnableFastRecovery(), DEFAULT_ENABLE_FAST_RECOVERY));
    D2QuarantineProperties quarantineProperties = properties.hasQuarantineProperties() ? properties.getQuarantineProperties() : new D2QuarantineProperties();
    quarantineProperties.setQuarantineMaxPercent(getOrDefault(quarantineProperties.getQuarantineMaxPercent(), DEFAULT_QUARANTINE_MAX_PERCENT));
    quarantineProperties.setHealthCheckMethod(getOrDefault(quarantineProperties.getHealthCheckMethod(), DEFAULT_HTTP_METHOD));
    properties.setQuarantineProperties(quarantineProperties);
    // Most ring properties are initialized in {@link DelegatingRingFactory}
    D2RingProperties ringProperties = properties.hasRingProperties() ? properties.getRingProperties() : new D2RingProperties();
    ringProperties.setPointsPerWeight(getOrDefault(ringProperties.getPointsPerWeight(), DEFAULT_POINTS_PER_WEIGHT));
    properties.setRingProperties(ringProperties);
    return properties;
}
Also used : D2QuarantineProperties(com.linkedin.d2.D2QuarantineProperties) D2RingProperties(com.linkedin.d2.D2RingProperties)

Example 5 with D2RingProperties

use of com.linkedin.d2.D2RingProperties 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;
}
Also used : D2QuarantineProperties(com.linkedin.d2.D2QuarantineProperties) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HttpStatusCodeRange(com.linkedin.d2.HttpStatusCodeRange) D2RingProperties(com.linkedin.d2.D2RingProperties) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

D2RingProperties (com.linkedin.d2.D2RingProperties)5 D2QuarantineProperties (com.linkedin.d2.D2QuarantineProperties)4 HttpStatusCodeRange (com.linkedin.d2.HttpStatusCodeRange)3 ConsistentHashAlgorithm (com.linkedin.d2.ConsistentHashAlgorithm)2 D2RelativeStrategyProperties (com.linkedin.d2.D2RelativeStrategyProperties)2 HashConfig (com.linkedin.d2.HashConfig)2 HttpMethod (com.linkedin.d2.HttpMethod)2 HttpStatusCodeRangeArray (com.linkedin.d2.HttpStatusCodeRangeArray)2 HashMap (java.util.HashMap)2 HashMethod (com.linkedin.d2.HashMethod)1 StringArray (com.linkedin.data.template.StringArray)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Test (org.testng.annotations.Test)1