use of com.linkedin.d2.balancer.util.MapKeyResult in project rest.li by linkedin.
the class ConsistentHashKeyMapper method mapKeysV2.
@Override
public <K> MapKeyResult<URI, K> mapKeysV2(URI serviceUri, Iterable<K> keys) throws ServiceUnavailableException {
// distribute keys to rings for partitions
// Note that we assume the keys are partitioning keys
MapKeyResult<Ring<URI>, K> keyToPartitionResult = _ringProvider.getRings(serviceUri, keys);
Map<Ring<URI>, Collection<K>> ringToKeys = keyToPartitionResult.getMapResult();
Map<URI, Collection<K>> result = new HashMap<>();
Collection<MapKeyResult.UnmappedKey<K>> unmappedKeys = new ArrayList<>();
// first collect unmappedkeys in ditributing keys to partitions
unmappedKeys.addAll(keyToPartitionResult.getUnmappedKeys());
// for each partition, distribute keys to different server uris
for (Map.Entry<Ring<URI>, Collection<K>> entry : ringToKeys.entrySet()) {
MapKeyResult<URI, K> keyToHostResult = doMapKeys(entry.getKey(), entry.getValue());
// collect map key to host result
Map<URI, Collection<K>> hostToKeys = keyToHostResult.getMapResult();
for (Map.Entry<URI, Collection<K>> hostEntry : hostToKeys.entrySet()) {
URI uri = hostEntry.getKey();
Collection<K> collection = result.get(uri);
if (collection == null) {
collection = new ArrayList<>();
result.put(uri, collection);
}
collection.addAll(hostEntry.getValue());
}
// collect unmapped keys
unmappedKeys.addAll(keyToHostResult.getUnmappedKeys());
}
return new MapKeyResult<>(result, unmappedKeys);
}
Aggregations