use of com.linkedin.d2.balancer.util.hashing.Ring in project rest.li by linkedin.
the class SimpleLoadBalancer method getRings.
public Map<Integer, Ring<URI>> getRings(URI serviceUri) throws ServiceUnavailableException {
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());
if (!orderedStrategies.isEmpty()) {
final PartitionAccessor accessor = getPartitionAccessor(serviceName, clusterName);
int maxPartitionId = accessor.getMaxPartitionId();
Map<Integer, Ring<URI>> ringMap = new HashMap<>((maxPartitionId + 1) * 2);
for (int partitionId = 0; partitionId <= maxPartitionId; partitionId++) {
for (LoadBalancerState.SchemeStrategyPair pair : orderedStrategies) {
TrackerClientSubsetItem subsetItem = getPotentialClients(serviceName, service, cluster, uris, pair.getScheme(), partitionId, uriItem.getVersion());
Ring<URI> ring = pair.getStrategy().getRing(uriItem.getVersion(), partitionId, subsetItem.getWeightedSubset(), subsetItem.shouldForceUpdate());
// ring will never be null; it can be empty
ringMap.put(partitionId, ring);
if (!ring.isEmpty()) {
// don't fallback to the next strategy if there are already hosts in the current one
break;
}
}
}
return ringMap;
} else {
throw new ServiceUnavailableException(serviceName, "PEGA_1003. Unable to find a load balancer strategy" + "Server Schemes: [" + String.join(", ", service.getPrioritizedSchemes()) + ']');
}
}
use of com.linkedin.d2.balancer.util.hashing.Ring in project rest.li by linkedin.
the class DeterministicSubsettingStrategy method getWeightedSubset.
@Override
public Map<T, Double> getWeightedSubset(Map<T, Double> weightMap, DeterministicSubsettingMetadata metadata) {
if (metadata != null) {
List<T> points = new ArrayList<>(weightMap.keySet());
Collections.sort(points);
Collections.shuffle(points, new Random(_randomSeed));
List<Double> weights = points.stream().map(weightMap::get).collect(Collectors.toList());
double totalWeight = weights.stream().mapToDouble(Double::doubleValue).sum();
if (totalWeight == 0) {
return null;
}
Ring ring = new Ring(weights, totalWeight);
double offset = metadata.getInstanceId() / (double) metadata.getTotalInstanceCount();
double subsetSliceWidth = getSubsetSliceWidth(metadata.getTotalInstanceCount(), points.size());
List<Integer> indices = ring.getIndices(offset, subsetSliceWidth);
return indices.stream().collect(Collectors.toMap(points::get, i -> round(ring.getWeight(i, offset, subsetSliceWidth), WEIGHT_DECIMAL_PLACE)));
} else {
_log.warn("Cannot retrieve metadata required for D2 subsetting. Revert to use all available hosts.");
return null;
}
}
Aggregations