use of com.linkedin.d2.quarantineInfo in project rest.li by linkedin.
the class LoadBalancerStrategyPropertiesConverter method toConfig.
@SuppressWarnings({ "unchecked" })
public static D2LoadBalancerStrategyProperties toConfig(Map<String, Object> properties) {
D2LoadBalancerStrategyProperties config = new D2LoadBalancerStrategyProperties();
if (properties.containsKey(PropertyKeys.HTTP_LB_GLOBAL_STEP_DOWN)) {
config.setGlobalStepDown(coerce(properties.get(PropertyKeys.HTTP_LB_GLOBAL_STEP_DOWN), Double.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_GLOBAL_STEP_UP)) {
config.setGlobalStepUp(coerce(properties.get(PropertyKeys.HTTP_LB_GLOBAL_STEP_UP), Double.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_INITIAL_RECOVERY_LEVEL)) {
config.setInitialRecoveryLevel(coerce(properties.get(PropertyKeys.HTTP_LB_INITIAL_RECOVERY_LEVEL), Double.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_RING_RAMP_FACTOR)) {
config.setRingRampFactor(coerce(properties.get(PropertyKeys.HTTP_LB_RING_RAMP_FACTOR), Double.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_HIGH_WATER_MARK)) {
config.setHighWaterMark(coerce(properties.get(PropertyKeys.HTTP_LB_HIGH_WATER_MARK), Double.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_LOW_WATER_MARK)) {
config.setLowWaterMark(coerce(properties.get(PropertyKeys.HTTP_LB_LOW_WATER_MARK), Double.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_POINTS_PER_WEIGHT)) {
config.setPointsPerWeight(coerce(properties.get(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_POINTS_PER_WEIGHT), Integer.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_UPDATE_INTERVAL_MS)) {
config.setUpdateIntervalMs(coerce(properties.get(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_UPDATE_INTERVAL_MS), Long.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_CLUSTER_MIN_CALL_COUNT_HIGH_WATER_MARK)) {
config.setMinCallCountHighWaterMark(coerce(properties.get(PropertyKeys.HTTP_LB_CLUSTER_MIN_CALL_COUNT_HIGH_WATER_MARK), Long.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_CLUSTER_MIN_CALL_COUNT_LOW_WATER_MARK)) {
config.setMinCallCountLowWaterMark(coerce(properties.get(PropertyKeys.HTTP_LB_CLUSTER_MIN_CALL_COUNT_LOW_WATER_MARK), Long.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_HASHRING_POINT_CLEANUP_RATE)) {
config.setHashRingPointCleanupRate(coerce(properties.get(PropertyKeys.HTTP_LB_HASHRING_POINT_CLEANUP_RATE), Double.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_HASH_METHOD)) {
String hashMethodString = coerce(properties.get(PropertyKeys.HTTP_LB_HASH_METHOD), String.class);
if (DegraderLoadBalancerStrategyV3.HASH_METHOD_NONE.equalsIgnoreCase(hashMethodString)) {
config.setHashMethod(hashMethodEnum.RANDOM);
} else if (DegraderLoadBalancerStrategyV3.HASH_METHOD_URI_REGEX.equalsIgnoreCase(hashMethodString)) {
config.setHashMethod(hashMethodEnum.URI_REGEX);
}
}
if (properties.containsKey(PropertyKeys.HTTP_LB_HASH_CONFIG)) {
hashConfigType hashConfig = new hashConfigType();
Map<String, Object> hashConfigProperties = (Map<String, Object>) properties.get(PropertyKeys.HTTP_LB_HASH_CONFIG);
if (hashConfigProperties.containsKey(URIRegexHash.KEY_REGEXES)) {
List<String> uriRegexes = (List<String>) hashConfigProperties.get(URIRegexHash.KEY_REGEXES);
hashConfig.setUriRegexes(new StringArray(uriRegexes));
}
if (hashConfigProperties.containsKey(URIRegexHash.KEY_WARN_ON_NO_MATCH)) {
String warnOnNoMatchString = (String) hashConfigProperties.get(URIRegexHash.KEY_WARN_ON_NO_MATCH);
hashConfig.setWarnOnNoMatch(Boolean.parseBoolean(warnOnNoMatchString));
}
if (hashConfigProperties.containsKey(URIRegexHash.KEY_FAIL_ON_NO_MATCH)) {
String failOnNoMatchString = (String) hashConfigProperties.get(URIRegexHash.KEY_FAIL_ON_NO_MATCH);
hashConfig.setFailOnNoMatch(Boolean.parseBoolean(failOnNoMatchString));
}
config.setHashConfig(hashConfig);
}
if (properties.containsKey(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_UPDATE_ONLY_AT_INTERVAL)) {
config.setUpdateOnlyAtInterval(coerce(properties.get(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_UPDATE_ONLY_AT_INTERVAL), Boolean.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_CONSISTENT_HASH_ALGORITHM)) {
String consistentHashAlgorithm = coerce(properties.get(PropertyKeys.HTTP_LB_CONSISTENT_HASH_ALGORITHM), String.class);
if (DelegatingRingFactory.POINT_BASED_CONSISTENT_HASH.equalsIgnoreCase(consistentHashAlgorithm)) {
config.setConsistentHashAlgorithm(ConsistentHashAlgorithmEnum.POINT_BASED);
} else if (DelegatingRingFactory.MULTI_PROBE_CONSISTENT_HASH.equalsIgnoreCase(consistentHashAlgorithm)) {
config.setConsistentHashAlgorithm(ConsistentHashAlgorithmEnum.MULTI_PROBE);
} else if (DelegatingRingFactory.DISTRIBUTION_NON_HASH.equalsIgnoreCase(consistentHashAlgorithm)) {
config.setConsistentHashAlgorithm(ConsistentHashAlgorithmEnum.DISTRIBUTION_BASED);
}
}
if (properties.containsKey(PropertyKeys.HTTP_LB_CONSISTENT_HASH_NUM_PROBES)) {
config.setNumberOfProbes(coerce(properties.get(PropertyKeys.HTTP_LB_CONSISTENT_HASH_NUM_PROBES), Integer.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_CONSISTENT_HASH_POINTS_PER_HOST)) {
config.setNumberOfPointsPerHost(coerce(properties.get(PropertyKeys.HTTP_LB_CONSISTENT_HASH_POINTS_PER_HOST), Integer.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_CONSISTENT_HASH_BOUNDED_LOAD_BALANCING_FACTOR)) {
config.setBoundedLoadBalancingFactor(coerce(properties.get(PropertyKeys.HTTP_LB_CONSISTENT_HASH_BOUNDED_LOAD_BALANCING_FACTOR), Double.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_QUARANTINE_MAX_PERCENT) || properties.containsKey(PropertyKeys.HTTP_LB_QUARANTINE_METHOD)) {
quarantineInfo quarantineInfo = new quarantineInfo();
if (properties.containsKey(PropertyKeys.HTTP_LB_QUARANTINE_MAX_PERCENT)) {
quarantineInfo.setQuarantineMaxPercent(coerce(properties.get(PropertyKeys.HTTP_LB_QUARANTINE_MAX_PERCENT), Double.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_QUARANTINE_METHOD)) {
quarantineInfo.setQuarantineMethod(coerce(properties.get(PropertyKeys.HTTP_LB_QUARANTINE_METHOD), String.class));
}
config.setQuarantineCfg(quarantineInfo);
}
if (properties.containsKey(PropertyKeys.HTTP_LB_ERROR_STATUS_REGEX)) {
config.setErrorStatusRegex(coerce(properties.get(PropertyKeys.HTTP_LB_ERROR_STATUS_REGEX), String.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_LOW_EVENT_EMITTING_INTERVAL)) {
config.setLowEmittingInterval(coerce(properties.get(PropertyKeys.HTTP_LB_LOW_EVENT_EMITTING_INTERVAL), Integer.class));
}
if (properties.containsKey(PropertyKeys.HTTP_LB_HIGH_EVENT_EMITTING_INTERVAL)) {
config.setHighEmittingInterval(coerce(properties.get(PropertyKeys.HTTP_LB_HIGH_EVENT_EMITTING_INTERVAL), Integer.class));
}
return config;
}
use of com.linkedin.d2.quarantineInfo in project rest.li by linkedin.
the class LoadBalancerStrategyPropertiesConverter method toProperties.
public static Map<String, Object> toProperties(D2LoadBalancerStrategyProperties config) {
if (config == null) {
return Collections.emptyMap();
}
Map<String, Object> map = new HashMap<>();
if (config.hasGlobalStepDown()) {
map.put(PropertyKeys.HTTP_LB_GLOBAL_STEP_DOWN, config.getGlobalStepDown().toString());
}
if (config.hasGlobalStepUp()) {
map.put(PropertyKeys.HTTP_LB_GLOBAL_STEP_UP, config.getGlobalStepUp().toString());
}
if (config.hasInitialRecoveryLevel()) {
map.put(PropertyKeys.HTTP_LB_INITIAL_RECOVERY_LEVEL, config.getInitialRecoveryLevel().toString());
}
if (config.hasRingRampFactor()) {
map.put(PropertyKeys.HTTP_LB_RING_RAMP_FACTOR, config.getRingRampFactor().toString());
}
if (config.hasHighWaterMark()) {
map.put(PropertyKeys.HTTP_LB_HIGH_WATER_MARK, config.getHighWaterMark().toString());
}
if (config.hasLowWaterMark()) {
map.put(PropertyKeys.HTTP_LB_LOW_WATER_MARK, config.getLowWaterMark().toString());
}
if (config.hasPointsPerWeight()) {
map.put(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_POINTS_PER_WEIGHT, config.getPointsPerWeight().toString());
}
if (config.hasUpdateIntervalMs()) {
map.put(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_UPDATE_INTERVAL_MS, config.getUpdateIntervalMs().toString());
}
if (config.hasMinCallCountHighWaterMark()) {
map.put(PropertyKeys.HTTP_LB_CLUSTER_MIN_CALL_COUNT_HIGH_WATER_MARK, config.getMinCallCountHighWaterMark().toString());
}
if (config.hasHashRingPointCleanupRate()) {
map.put(PropertyKeys.HTTP_LB_HASHRING_POINT_CLEANUP_RATE, config.getHashRingPointCleanupRate().toString());
}
if (config.hasMinCallCountLowWaterMark()) {
map.put(PropertyKeys.HTTP_LB_CLUSTER_MIN_CALL_COUNT_LOW_WATER_MARK, config.getMinCallCountLowWaterMark().toString());
}
if (config.hasHashMethod()) {
switch(config.getHashMethod()) {
case RANDOM:
map.put(PropertyKeys.HTTP_LB_HASH_METHOD, DegraderLoadBalancerStrategyV3.HASH_METHOD_NONE);
break;
case URI_REGEX:
map.put(PropertyKeys.HTTP_LB_HASH_METHOD, DegraderLoadBalancerStrategyV3.HASH_METHOD_URI_REGEX);
break;
default:
// default to random hash method.
map.put(PropertyKeys.HTTP_LB_HASH_METHOD, DegraderLoadBalancerStrategyV3.HASH_METHOD_NONE);
}
}
if (config.hasHashConfig()) {
hashConfigType hashConfig = config.getHashConfig();
Map<String, Object> hashConfigProperties = new HashMap<>();
if (hashConfig.hasUriRegexes()) {
hashConfigProperties.put(URIRegexHash.KEY_REGEXES, hashConfig.getUriRegexes().stream().collect(Collectors.toList()));
}
if (hashConfig.hasFailOnNoMatch()) {
hashConfigProperties.put(URIRegexHash.KEY_FAIL_ON_NO_MATCH, hashConfig.isFailOnNoMatch().toString());
}
if (hashConfig.hasWarnOnNoMatch()) {
hashConfigProperties.put(URIRegexHash.KEY_WARN_ON_NO_MATCH, hashConfig.isWarnOnNoMatch().toString());
}
map.put(PropertyKeys.HTTP_LB_HASH_CONFIG, hashConfigProperties);
}
if (config.hasUpdateOnlyAtInterval()) {
map.put(PropertyKeys.HTTP_LB_STRATEGY_PROPERTIES_UPDATE_ONLY_AT_INTERVAL, config.isUpdateOnlyAtInterval().toString());
}
if (config.hasConsistentHashAlgorithm()) {
switch(config.getConsistentHashAlgorithm()) {
case MULTI_PROBE:
map.put(PropertyKeys.HTTP_LB_CONSISTENT_HASH_ALGORITHM, DelegatingRingFactory.MULTI_PROBE_CONSISTENT_HASH);
break;
case POINT_BASED:
map.put(PropertyKeys.HTTP_LB_CONSISTENT_HASH_ALGORITHM, DelegatingRingFactory.POINT_BASED_CONSISTENT_HASH);
break;
case DISTRIBUTION_BASED:
map.put(PropertyKeys.HTTP_LB_CONSISTENT_HASH_ALGORITHM, DelegatingRingFactory.DISTRIBUTION_NON_HASH);
}
}
if (config.hasNumberOfProbes()) {
map.put(PropertyKeys.HTTP_LB_CONSISTENT_HASH_NUM_PROBES, config.getNumberOfProbes().toString());
}
if (config.hasNumberOfPointsPerHost()) {
map.put(PropertyKeys.HTTP_LB_CONSISTENT_HASH_POINTS_PER_HOST, config.getNumberOfPointsPerHost().toString());
}
if (config.hasBoundedLoadBalancingFactor()) {
map.put(PropertyKeys.HTTP_LB_CONSISTENT_HASH_BOUNDED_LOAD_BALANCING_FACTOR, config.getBoundedLoadBalancingFactor().toString());
}
if (config.hasQuarantineCfg()) {
quarantineInfo quarantineInfo = config.getQuarantineCfg();
if (quarantineInfo.hasQuarantineMaxPercent()) {
map.put(PropertyKeys.HTTP_LB_QUARANTINE_MAX_PERCENT, quarantineInfo.getQuarantineMaxPercent().toString());
}
if (quarantineInfo.hasQuarantineMethod()) {
map.put(PropertyKeys.HTTP_LB_QUARANTINE_METHOD, quarantineInfo.getQuarantineMethod().toString());
}
}
if (config.hasErrorStatusRegex()) {
map.put(PropertyKeys.HTTP_LB_ERROR_STATUS_REGEX, config.getErrorStatusRegex());
}
if (config.hasLowEmittingInterval()) {
map.put(PropertyKeys.HTTP_LB_LOW_EVENT_EMITTING_INTERVAL, config.getLowEmittingInterval().toString());
}
if (config.hasHighEmittingInterval()) {
map.put(PropertyKeys.HTTP_LB_HIGH_EVENT_EMITTING_INTERVAL, config.getHighEmittingInterval().toString());
}
return map;
}
use of com.linkedin.d2.quarantineInfo in project rest.li by linkedin.
the class PartitionPropertiesConverterTest method testHashModuloPartitionProperties.
@Test
public void testHashModuloPartitionProperties() {
final String partitionKeyRegex = "/foo/bar/(\\d+)";
final int partitionCount = 16;
final HashBasedPartitionProperties.HashAlgorithm hashAlgorithm = HashBasedPartitionProperties.HashAlgorithm.MODULO;
PartitionProperties partitionProperties = new HashBasedPartitionProperties(partitionKeyRegex, partitionCount, hashAlgorithm);
D2ClusterPartitionConfiguration.PartitionTypeSpecificData data = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
data.setHashAlgorithm(com.linkedin.d2.HashAlgorithm.MODULO);
D2ClusterPartitionConfiguration partitionConfig = new D2ClusterPartitionConfiguration().setType(PartitionTypeEnum.HASH).setPartitionKeyRegex(partitionKeyRegex).setPartitionCount(partitionCount).setPartitionTypeSpecificData(data);
Assert.assertEquals(PartitionPropertiesConverter.toConfig(partitionProperties), partitionConfig);
Assert.assertEquals(PartitionPropertiesConverter.toProperties(partitionConfig), partitionProperties);
}
use of com.linkedin.d2.quarantineInfo 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);
}
use of com.linkedin.d2.quarantineInfo in project rest.li by linkedin.
the class SimpleLoadBalancer method getPartitionInformation.
/**
* If given a collection of keys, the method will maps keys to partitions and
* return the servers that belongs to that partition up to limitHostPerPartition.
*
* If no keys are specified, the method will return hosts in all partitions
*
* @param serviceUri for example d2://articles
* @param keys all the keys we want to find the partition for
* @param limitHostPerPartition the number of hosts that we should return for this partition. Must be larger than 0.
* @param hash this will be used to create Iterator for the hosts in the hash ring
* @return Number of hosts in requested partitions. See {@link com.linkedin.d2.balancer.util.HostToKeyMapper} for more details.
* @throws ServiceUnavailableException
*/
@Override
public <K> HostToKeyMapper<K> getPartitionInformation(URI serviceUri, Collection<K> keys, int limitHostPerPartition, int hash) throws ServiceUnavailableException {
if (limitHostPerPartition <= 0) {
throw new IllegalArgumentException("limitHostPartition cannot be 0 or less");
}
ServiceProperties service = listenToServiceAndCluster(serviceUri);
String serviceName = service.getServiceName();
String clusterName = service.getClusterName();
ClusterProperties cluster = getClusterProperties(serviceName, clusterName);
LoadBalancerStateItem<UriProperties> uriItem = getUriItem(serviceName, clusterName, cluster);
UriProperties uris = uriItem.getProperty();
List<LoadBalancerState.SchemeStrategyPair> orderedStrategies = _state.getStrategiesForService(serviceName, service.getPrioritizedSchemes());
Map<Integer, Integer> partitionWithoutEnoughHost = new HashMap<>();
if (!orderedStrategies.isEmpty()) {
// get the partitionId -> keys mapping
final PartitionAccessor accessor = getPartitionAccessor(serviceName, clusterName);
int maxPartitionId = accessor.getMaxPartitionId();
List<K> unmappedKeys = new ArrayList<>();
Map<Integer, Set<K>> partitionSet = getPartitionSet(keys, accessor, unmappedKeys);
// get the partitionId -> host URIs list
Map<Integer, KeysAndHosts<K>> partitionDataMap = new HashMap<>();
for (Integer partitionId : partitionSet.keySet()) {
for (LoadBalancerState.SchemeStrategyPair pair : orderedStrategies) {
TrackerClientSubsetItem subsetItem = getPotentialClients(serviceName, service, cluster, uris, pair.getScheme(), partitionId, uriItem.getVersion());
Map<URI, TrackerClient> trackerClients = subsetItem.getWeightedSubset();
int size = Math.min(trackerClients.size(), limitHostPerPartition);
List<URI> rankedUri = new ArrayList<>(size);
Ring<URI> ring = pair.getStrategy().getRing(uriItem.getVersion(), partitionId, trackerClients, subsetItem.shouldForceUpdate());
Iterator<URI> iterator = ring.getIterator(hash);
while (iterator.hasNext() && rankedUri.size() < size) {
URI uri = iterator.next();
if (!rankedUri.contains(uri)) {
rankedUri.add(uri);
}
}
if (rankedUri.size() < limitHostPerPartition) {
partitionWithoutEnoughHost.put(partitionId, limitHostPerPartition - rankedUri.size());
}
KeysAndHosts<K> keysAndHosts = new KeysAndHosts<>(partitionSet.get(partitionId), rankedUri);
partitionDataMap.put(partitionId, keysAndHosts);
if (!rankedUri.isEmpty()) {
// don't go to the next strategy if there are already hosts in the current one
break;
}
}
}
return new HostToKeyMapper<>(unmappedKeys, partitionDataMap, limitHostPerPartition, maxPartitionId + 1, partitionWithoutEnoughHost);
} else {
throw new ServiceUnavailableException(serviceName, "PEGA_1009. Unable to find a load balancer strategy" + "Server Schemes: [" + String.join(", ", service.getPrioritizedSchemes()) + ']');
}
}
Aggregations