Search in sources :

Example 1 with ConsistentHashKeyMapper

use of com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper in project rest.li by linkedin.

the class ConsistentHashKeyMapperTest method getConsistentHashKeyMapper.

private ConsistentHashKeyMapper getConsistentHashKeyMapper(RingFactory<URI> ringFactory) throws URISyntaxException {
    String serviceName = "articles";
    String clusterName = "cluster";
    String path = "path";
    String strategyName = "degrader";
    //setup partition
    Map<URI, Map<Integer, PartitionData>> partitionDescriptions = new HashMap<URI, Map<Integer, PartitionData>>();
    final URI foo1 = new URI("http://foo1.com");
    Map<Integer, PartitionData> foo1Data = new HashMap<Integer, PartitionData>();
    foo1Data.put(0, new PartitionData(1.0));
    partitionDescriptions.put(foo1, foo1Data);
    final URI foo2 = new URI("http://foo2.com");
    Map<Integer, PartitionData> foo2Data = new HashMap<Integer, PartitionData>();
    foo2Data.put(3, new PartitionData(1.0));
    foo2Data.put(4, new PartitionData(1.0));
    partitionDescriptions.put(foo2, foo2Data);
    final URI foo3 = new URI("http://foo3.com");
    Map<Integer, PartitionData> foo3Data = new HashMap<Integer, PartitionData>();
    foo3Data.put(0, new PartitionData(1.0));
    partitionDescriptions.put(foo3, foo3Data);
    final URI foo4 = new URI("http://foo4.com");
    Map<Integer, PartitionData> foo4Data = new HashMap<Integer, PartitionData>();
    foo4Data.put(1, new PartitionData(1.0));
    partitionDescriptions.put(foo4, foo4Data);
    final URI foo5 = new URI("http://foo5.com");
    Map<Integer, PartitionData> foo5Data = new HashMap<Integer, PartitionData>();
    foo5Data.put(1, new PartitionData(1.0));
    partitionDescriptions.put(foo5, foo5Data);
    final URI foo6 = new URI("http://foo6.com");
    Map<Integer, PartitionData> foo6Data = new HashMap<Integer, PartitionData>();
    foo6Data.put(1, new PartitionData(1.0));
    partitionDescriptions.put(foo6, foo6Data);
    //setup strategy which involves tweaking the hash ring to get partitionId -> URI host
    List<LoadBalancerState.SchemeStrategyPair> orderedStrategies = new ArrayList<LoadBalancerState.SchemeStrategyPair>();
    LoadBalancerStrategy strategy = new TestLoadBalancerStrategy(partitionDescriptions, ringFactory);
    orderedStrategies.add(new LoadBalancerState.SchemeStrategyPair("http", strategy));
    //setup the partition accessor which is used to get partitionId -> keys
    PartitionAccessor accessor = new TestPartitionAccessor();
    URI serviceURI = new URI("d2://" + serviceName);
    SimpleLoadBalancer balancer = new SimpleLoadBalancer(new PartitionedLoadBalancerTestState(clusterName, serviceName, path, strategyName, partitionDescriptions, orderedStrategies, accessor));
    ConsistentHashKeyMapper mapper = new ConsistentHashKeyMapper(balancer, balancer);
    return mapper;
}
Also used : SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) HashMap(java.util.HashMap) 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) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ConsistentHashKeyMapper

use of com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper in project rest.li by linkedin.

the class ConsistentHashKeyMapperTest method testMapKeysConcurrency.

@SuppressWarnings("rawtypes")
@Test
public void testMapKeysConcurrency() throws Exception {
    String serviceName = "articles";
    String clusterName = "cluster";
    String path = "path";
    String strategyName = "degrader";
    int numPartitions = 500;
    // setup partition
    Map<URI, Map<Integer, PartitionData>> partitionDescriptions = new HashMap<URI, Map<Integer, PartitionData>>();
    final URI foo1 = new URI("http://foo1.com");
    Map<Integer, PartitionData> foo1Data = new HashMap<Integer, PartitionData>();
    for (int i = 0; i < numPartitions; i++) {
        foo1Data.put(i, new PartitionData(1.0));
    }
    partitionDescriptions.put(foo1, foo1Data);
    DegraderLoadBalancerStrategyV3 strategy = new DegraderLoadBalancerStrategyV3(new DegraderLoadBalancerStrategyConfig(5000), serviceName, null);
    List<LoadBalancerState.SchemeStrategyPair> orderedStrategies = new ArrayList<LoadBalancerState.SchemeStrategyPair>();
    orderedStrategies.add(new LoadBalancerState.SchemeStrategyPair("http", strategy));
    PartitionAccessor accessor = new TestDeadlockPartitionAccessor(numPartitions);
    SimpleLoadBalancer balancer = new SimpleLoadBalancer(new PartitionedLoadBalancerTestState(clusterName, serviceName, path, strategyName, partitionDescriptions, orderedStrategies, accessor));
    ConsistentHashKeyMapper mapper = new ConsistentHashKeyMapper(balancer, balancer);
    CountDownLatch latch = new CountDownLatch(numPartitions);
    List<Runnable> runnables = createRunnables(numPartitions, mapper, serviceName, latch);
    final ExecutorService executor = Executors.newFixedThreadPool(numPartitions);
    List<Future> futures = new ArrayList<Future>();
    for (int i = 0; i < numPartitions; i++) {
        futures.add(executor.submit(runnables.get(i)));
    }
    for (Future future : futures) {
        future.get(30, TimeUnit.SECONDS);
    }
}
Also used : HashMap(java.util.HashMap) DegraderLoadBalancerStrategyConfig(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyConfig) ArrayList(java.util.ArrayList) URI(java.net.URI) LoadBalancerState(com.linkedin.d2.balancer.LoadBalancerState) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) PartitionedLoadBalancerTestState(com.linkedin.d2.balancer.PartitionedLoadBalancerTestState) CountDownLatch(java.util.concurrent.CountDownLatch) DegraderLoadBalancerStrategyV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyV3) PartitionAccessor(com.linkedin.d2.balancer.util.partitions.PartitionAccessor) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 3 with ConsistentHashKeyMapper

use of com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper 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 4 with ConsistentHashKeyMapper

use of com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper in project rest.li by linkedin.

the class TestScatterGather method testBuildSGRequests.

public static void testBuildSGRequests(int endPointsNum, int partitionNum, RootBuilderWrapper<Long, Greeting> builders) throws URISyntaxException, RestException, ServiceUnavailableException {
    final int NUM_ENDPOINTS = endPointsNum;
    ConsistentHashKeyMapper mapper;
    if (partitionNum > 0) {
        mapper = getKeyToHostMapper(endPointsNum, partitionNum);
    } else {
        mapper = getKeyToHostMapper(endPointsNum);
    }
    ScatterGatherBuilder<Greeting> sg = new ScatterGatherBuilder<Greeting>(mapper);
    final int NUM_IDS = 100;
    Long[] ids = generateIds(NUM_IDS);
    Map<Long, Greeting> updates = generateUpdates(ids);
    testBuildSGGetRequests(NUM_ENDPOINTS, sg, ids);
    testBuildSGDeleteRequests(NUM_ENDPOINTS, sg, ids, builders);
    testBuildSGUpdateRequests(NUM_ENDPOINTS, sg, updates, builders);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ConsistentHashKeyMapper(com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper)

Example 5 with ConsistentHashKeyMapper

use of com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper in project rest.li by linkedin.

the class TestScatterGather method testScatterGatherKVLoadBalancerIntegration.

@Test(dataProvider = "requestBuilderDataProvider")
public static void testScatterGatherKVLoadBalancerIntegration(RootBuilderWrapper<Long, Greeting> builders) throws Exception {
    SimpleLoadBalancer loadBalancer = MockLBFactory.createLoadBalancer();
    KeyMapper keyMapper = new ConsistentHashKeyMapper(loadBalancer, new TestPartitionInfoProvider());
    try {
        keyMapper.mapKeysV2(URI.create("http://badurischeme/"), new HashSet<String>());
        Assert.fail("keyMapper should reject non-D2 URI scheme");
    } catch (IllegalArgumentException e) {
    //expected
    }
    ScatterGatherBuilder<Greeting> sg = new ScatterGatherBuilder<Greeting>(keyMapper);
    final int NUM_IDS = 20;
    Long[] requestIds = generateIds(NUM_IDS);
    Collection<ScatterGatherBuilder.KVRequestInfo<Long, Greeting>> scatterGatherRequests = buildScatterGatherGetKVRequests(sg, requestIds);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) HostToKeyMapper(com.linkedin.d2.balancer.util.HostToKeyMapper) KeyMapper(com.linkedin.d2.balancer.KeyMapper) ConsistentHashKeyMapper(com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper) ConsistentHashKeyMapper(com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper) Test(org.testng.annotations.Test) RestLiIntegrationTest(com.linkedin.restli.examples.RestLiIntegrationTest)

Aggregations

ConsistentHashKeyMapper (com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper)10 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)8 SimpleLoadBalancer (com.linkedin.d2.balancer.simple.SimpleLoadBalancer)6 URI (java.net.URI)6 ArrayList (java.util.ArrayList)6 Test (org.testng.annotations.Test)6 RestLiIntegrationTest (com.linkedin.restli.examples.RestLiIntegrationTest)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 KeyMapper (com.linkedin.d2.balancer.KeyMapper)3 LoadBalancerState (com.linkedin.d2.balancer.LoadBalancerState)3 PartitionedLoadBalancerTestState (com.linkedin.d2.balancer.PartitionedLoadBalancerTestState)3 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)3 HostToKeyMapper (com.linkedin.d2.balancer.util.HostToKeyMapper)3 PartitionAccessor (com.linkedin.d2.balancer.util.partitions.PartitionAccessor)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Callback (com.linkedin.common.callback.Callback)1 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)1