Search in sources :

Example 1 with HashMethod

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

the class DegraderLoadBalancerStrategyV3 method setConfig.

public void setConfig(DegraderLoadBalancerStrategyConfig config) {
    _config = config;
    String hashMethod = _config.getHashMethod();
    Map<String, Object> hashConfig = _config.getHashConfig();
    if (hashMethod == null || hashMethod.equals(HASH_METHOD_NONE)) {
        _hashFunction = new RandomHash();
    } else if (HASH_METHOD_URI_REGEX.equals(hashMethod)) {
        _hashFunction = new URIRegexHash(hashConfig);
    } else {
        _log.warn("Unknown hash method {}, falling back to random", hashMethod);
        _hashFunction = new RandomHash();
    }
}
Also used : URIRegexHash(com.linkedin.d2.balancer.util.hashing.URIRegexHash) RandomHash(com.linkedin.d2.balancer.util.hashing.RandomHash)

Example 2 with HashMethod

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

the class DegraderLoadBalancerStrategyV2_1 method setConfig.

public void setConfig(DegraderLoadBalancerStrategyConfig config) {
    _config = config;
    String hashMethod = _config.getHashMethod();
    Map<String, Object> hashConfig = _config.getHashConfig();
    if (hashMethod == null || hashMethod.equals(HASH_METHOD_NONE)) {
        _hashFunction = new RandomHash();
    } else if (HASH_METHOD_URI_REGEX.equals(hashMethod)) {
        _hashFunction = new URIRegexHash(hashConfig);
    } else {
        _log.warn("Unknown hash method {}, falling back to random", hashMethod);
        _hashFunction = new RandomHash();
    }
}
Also used : URIRegexHash(com.linkedin.d2.balancer.util.hashing.URIRegexHash) RandomHash(com.linkedin.d2.balancer.util.hashing.RandomHash)

Example 3 with HashMethod

use of com.linkedin.d2.HashMethod 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 4 with HashMethod

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

the class RelativeLoadBalancerStrategyFactory method getRequestHashFunction.

private HashFunction<Request> getRequestHashFunction(D2RelativeStrategyProperties relativeStrategyProperties) {
    if (relativeStrategyProperties.hasRingProperties() && relativeStrategyProperties.getRingProperties().hasHashConfig()) {
        HashMethod hashMethod = relativeStrategyProperties.getRingProperties().getHashMethod();
        HashConfig hashConfig = relativeStrategyProperties.getRingProperties().getHashConfig();
        switch(hashMethod) {
            case URI_REGEX:
                return new URIRegexHash(RelativeStrategyPropertiesConverter.toHashConfigMap(hashConfig));
            case RANDOM:
            default:
                return new RandomHash();
        }
    }
    // Fall back to RandomHash if not specified
    return new RandomHash();
}
Also used : HashMethod(com.linkedin.d2.HashMethod) URIRegexHash(com.linkedin.d2.balancer.util.hashing.URIRegexHash) RandomHash(com.linkedin.d2.balancer.util.hashing.RandomHash) HashConfig(com.linkedin.d2.HashConfig)

Example 5 with HashMethod

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

the class LoadBalancerStrategyPropertiesConverterTest method testLoadBalancerStrategyPropertiesConverter.

@Test
public void testLoadBalancerStrategyPropertiesConverter() {
    final Double globalStepDown = 0.4;
    final Double globalStepUp = 0.3;
    final Double recoveryLevel = 1.0;
    final Double ringRampFactor = 0.01;
    final Double highWaterMark = 1000d;
    final Double lowWaterMark = 500d;
    final Integer pointsPerWeight = 100;
    final Long updateIntervalMs = 50000l;
    final Integer minCallCountHighWaterMark = 3000;
    final Integer minCallCountLowWaterMark = 1500;
    final hashMethodEnum hashMethod = hashMethodEnum.URI_REGEX;
    final hashConfigType hashConfig = new hashConfigType();
    final StringArray regexes = new StringArray("+231{w+)");
    final Double hashringPointCleanupRate = 0.2;
    final ConsistentHashAlgorithmEnum consistentHashAlgorithm = ConsistentHashAlgorithmEnum.MULTI_PROBE;
    final Integer numProbes = 1024;
    final Integer numPointsPerHost = 1;
    final Double quarantineMaxPercent = 0.2;
    final String quarantineMethod = "OPTIONS:/test/path";
    final quarantineInfo quarantineInfo = new quarantineInfo().setQuarantineMaxPercent(quarantineMaxPercent).setQuarantineMethod(quarantineMethod);
    final String errorStatusRegex = "(5..)";
    final Integer lowEmittingInterval = 10;
    final Integer highEmittingInterval = 60;
    hashConfig.setUriRegexes(regexes);
    hashConfig.setWarnOnNoMatch(false);
    hashConfig.setFailOnNoMatch(true);
    Map<String, Object> loadBalancerStrategyProperties = new HashMap<>();
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_GLOBAL_STEP_DOWN, globalStepDown.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_GLOBAL_STEP_UP, globalStepUp.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_INITIAL_RECOVERY_LEVEL, recoveryLevel.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_RING_RAMP_FACTOR, ringRampFactor.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_HIGH_WATER_MARK, highWaterMark.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_LOW_WATER_MARK, lowWaterMark.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_POINTS_PER_WEIGHT, pointsPerWeight.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_UPDATE_INTERVAL_MS, updateIntervalMs.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_CLUSTER_MIN_CALL_COUNT_HIGH_WATER_MARK, minCallCountHighWaterMark.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_CLUSTER_MIN_CALL_COUNT_LOW_WATER_MARK, minCallCountLowWaterMark.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_HASH_METHOD, DegraderLoadBalancerStrategyV3.HASH_METHOD_URI_REGEX);
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_HASHRING_POINT_CLEANUP_RATE, hashringPointCleanupRate.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_CONSISTENT_HASH_ALGORITHM, DelegatingRingFactory.MULTI_PROBE_CONSISTENT_HASH);
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_CONSISTENT_HASH_NUM_PROBES, numProbes.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_CONSISTENT_HASH_POINTS_PER_HOST, numPointsPerHost.toString());
    Map<String, Object> hashConfigMap = new HashMap<>();
    hashConfigMap.put(URIRegexHash.KEY_REGEXES, new ArrayList<>(regexes));
    hashConfigMap.put(URIRegexHash.KEY_WARN_ON_NO_MATCH, "false");
    hashConfigMap.put(URIRegexHash.KEY_FAIL_ON_NO_MATCH, "true");
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_HASH_CONFIG, hashConfigMap);
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_QUARANTINE_MAX_PERCENT, quarantineMaxPercent.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_QUARANTINE_METHOD, quarantineMethod);
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_ERROR_STATUS_REGEX, errorStatusRegex);
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_LOW_EVENT_EMITTING_INTERVAL, lowEmittingInterval.toString());
    loadBalancerStrategyProperties.put(PropertyKeys.HTTP_LB_HIGH_EVENT_EMITTING_INTERVAL, highEmittingInterval.toString());
    D2LoadBalancerStrategyProperties d2LoadBalancerStrategyProperties = new D2LoadBalancerStrategyProperties().setGlobalStepDown(globalStepDown).setGlobalStepUp(globalStepUp).setInitialRecoveryLevel(recoveryLevel).setRingRampFactor(ringRampFactor).setHighWaterMark(highWaterMark).setLowWaterMark(lowWaterMark).setPointsPerWeight(pointsPerWeight).setUpdateIntervalMs(updateIntervalMs).setMinCallCountHighWaterMark(minCallCountHighWaterMark).setMinCallCountLowWaterMark(minCallCountLowWaterMark).setHashMethod(hashMethod).setHashConfig(hashConfig).setHashRingPointCleanupRate(hashringPointCleanupRate).setConsistentHashAlgorithm(consistentHashAlgorithm).setNumberOfProbes(numProbes).setNumberOfPointsPerHost(numPointsPerHost).setQuarantineCfg(quarantineInfo).setErrorStatusRegex(errorStatusRegex).setLowEmittingInterval(lowEmittingInterval).setHighEmittingInterval(highEmittingInterval);
    Assert.assertEquals(LoadBalancerStrategyPropertiesConverter.toConfig(loadBalancerStrategyProperties), d2LoadBalancerStrategyProperties);
    Assert.assertEquals(LoadBalancerStrategyPropertiesConverter.toProperties(d2LoadBalancerStrategyProperties), loadBalancerStrategyProperties);
}
Also used : ConsistentHashAlgorithmEnum(com.linkedin.d2.ConsistentHashAlgorithmEnum) HashMap(java.util.HashMap) StringArray(com.linkedin.data.template.StringArray) com.linkedin.d2.quarantineInfo(com.linkedin.d2.quarantineInfo) com.linkedin.d2.hashMethodEnum(com.linkedin.d2.hashMethodEnum) D2LoadBalancerStrategyProperties(com.linkedin.d2.D2LoadBalancerStrategyProperties) com.linkedin.d2.hashConfigType(com.linkedin.d2.hashConfigType) Test(org.testng.annotations.Test)

Aggregations

RandomHash (com.linkedin.d2.balancer.util.hashing.RandomHash)4 URIRegexHash (com.linkedin.d2.balancer.util.hashing.URIRegexHash)4 HashConfig (com.linkedin.d2.HashConfig)3 D2RingProperties (com.linkedin.d2.D2RingProperties)2 HashMethod (com.linkedin.d2.HashMethod)2 StringArray (com.linkedin.data.template.StringArray)2 HashMap (java.util.HashMap)2 Test (org.testng.annotations.Test)2 ConsistentHashAlgorithm (com.linkedin.d2.ConsistentHashAlgorithm)1 ConsistentHashAlgorithmEnum (com.linkedin.d2.ConsistentHashAlgorithmEnum)1 D2LoadBalancerStrategyProperties (com.linkedin.d2.D2LoadBalancerStrategyProperties)1 D2QuarantineProperties (com.linkedin.d2.D2QuarantineProperties)1 D2RelativeStrategyProperties (com.linkedin.d2.D2RelativeStrategyProperties)1 HttpMethod (com.linkedin.d2.HttpMethod)1 HttpStatusCodeRange (com.linkedin.d2.HttpStatusCodeRange)1 HttpStatusCodeRangeArray (com.linkedin.d2.HttpStatusCodeRangeArray)1 com.linkedin.d2.hashConfigType (com.linkedin.d2.hashConfigType)1 com.linkedin.d2.hashMethodEnum (com.linkedin.d2.hashMethodEnum)1 com.linkedin.d2.quarantineInfo (com.linkedin.d2.quarantineInfo)1