use of com.linkedin.d2.balancer.util.partitions.PartitionAccessor in project rest.li by linkedin.
the class TestAllPartitionsRequestBuilder method getKeyToHostMapper.
private ConsistentHashKeyMapper getKeyToHostMapper(int partitionNum, List<URI> expectedUris, RingFactory<URI> ringFactory) throws URISyntaxException {
Map<URI, Map<Integer, PartitionData>> partitionDescriptions = new HashMap<URI, Map<Integer, PartitionData>>();
for (int i = 0; i < partitionNum; i++) {
final URI foo = new URI("http://foo" + i + ".com");
expectedUris.add(foo);
Map<Integer, PartitionData> foo1Data = new HashMap<Integer, PartitionData>();
foo1Data.put(i, new PartitionData(1.0));
partitionDescriptions.put(foo, foo1Data);
}
List<LoadBalancerState.SchemeStrategyPair> orderedStrategies = new ArrayList<LoadBalancerState.SchemeStrategyPair>();
LoadBalancerStrategy strategy = new ConsistentHashKeyMapperTest.TestLoadBalancerStrategy(partitionDescriptions, ringFactory);
orderedStrategies.add(new LoadBalancerState.SchemeStrategyPair("http", strategy));
PartitionAccessor accessor = new ConsistentHashKeyMapperTest.TestPartitionAccessor();
SimpleLoadBalancer balancer = new SimpleLoadBalancer(new PartitionedLoadBalancerTestState("clusterName", "serviceName", "path", "strategyName", partitionDescriptions, orderedStrategies, accessor));
return new ConsistentHashKeyMapper(balancer, balancer);
}
use of com.linkedin.d2.balancer.util.partitions.PartitionAccessor 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 LoadBalancerState.SchemeStrategyPair pair = orderedStrategies.get(0);
final PartitionAccessor accessor = getPartitionAccessor(serviceName, clusterName);
int maxPartitionId = accessor.getMaxPartitionId();
Map<Integer, Ring<URI>> ringMap = new HashMap<Integer, Ring<URI>>((maxPartitionId + 1) * 2);
for (int partitionId = 0; partitionId <= maxPartitionId; partitionId++) {
Set<URI> possibleUris = uris.getUriBySchemeAndPartition(pair.getScheme(), partitionId);
List<TrackerClient> trackerClients = getPotentialClients(serviceName, service, possibleUris);
Ring<URI> ring = pair.getStrategy().getRing(uriItem.getVersion(), partitionId, trackerClients);
// ring will never be null; it can be empty
ringMap.put(partitionId, ring);
}
return ringMap;
} else {
throw new ServiceUnavailableException(serviceName, "Unable to find a load balancer strategy");
}
}
use of com.linkedin.d2.balancer.util.partitions.PartitionAccessor in project rest.li by linkedin.
the class SimpleLoadBalancer method getPartitionAccessor.
@Override
public PartitionAccessor getPartitionAccessor(URI serviceUri) throws ServiceUnavailableException {
ServiceProperties service = listenToServiceAndCluster(serviceUri);
String serviceName = service.getServiceName();
String clusterName = service.getClusterName();
return getPartitionAccessor(serviceName, clusterName);
}
Aggregations