Search in sources :

Example 11 with PartitionAccessor

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);
}
Also used : SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) PartitionedLoadBalancerTestState(com.linkedin.d2.balancer.PartitionedLoadBalancerTestState) URI(java.net.URI) LoadBalancerState(com.linkedin.d2.balancer.LoadBalancerState) PartitionAccessor(com.linkedin.d2.balancer.util.partitions.PartitionAccessor) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) ConsistentHashKeyMapper(com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 12 with PartitionAccessor

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");
    }
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) URI(java.net.URI) LoadBalancerState(com.linkedin.d2.balancer.LoadBalancerState) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) PartitionAccessor(com.linkedin.d2.balancer.util.partitions.PartitionAccessor) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) Ring(com.linkedin.d2.balancer.util.hashing.Ring) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties)

Example 13 with PartitionAccessor

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);
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties)

Aggregations

PartitionAccessor (com.linkedin.d2.balancer.util.partitions.PartitionAccessor)12 URI (java.net.URI)11 HashMap (java.util.HashMap)11 LoadBalancerState (com.linkedin.d2.balancer.LoadBalancerState)10 ArrayList (java.util.ArrayList)10 Map (java.util.Map)8 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)7 PartitionedLoadBalancerTestState (com.linkedin.d2.balancer.PartitionedLoadBalancerTestState)6 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)5 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)5 Test (org.testng.annotations.Test)5 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)4 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)4 SimpleLoadBalancer (com.linkedin.d2.balancer.simple.SimpleLoadBalancer)4 DefaultPartitionAccessor (com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor)4 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)3 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)3 PartitionAccessException (com.linkedin.d2.balancer.util.partitions.PartitionAccessException)3 IdentityHashMap (java.util.IdentityHashMap)3 DegraderLoadBalancerStrategyConfig (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyConfig)2