Search in sources :

Example 6 with URIKeyPair

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

the class RingBasedURIMapperTest method testErrorHandling.

@Test
public void testErrorHandling() throws ServiceUnavailableException, URISyntaxException {
    int partitionCount = 10;
    int totalHostCount = 100;
    HashRingProvider ringProvider = createStaticHashRingProvider(totalHostCount, partitionCount, getHashFunction(true));
    PartitionInfoProvider infoProvider = createRangeBasedPartitionInfoProvider(partitionCount);
    URIMapper mapper = new RingBasedUriMapper(ringProvider, infoProvider);
    URIKeyPair<Integer> requestWithoutPartitionId = new URIKeyPair<>(42, new URI("d2://badService/2"));
    URIKeyPair<Integer> requestWithoutKey = new URIKeyPair<>(43, new URI("d2://badService/partitionId=3"));
    URIKeyPair<Integer> requestWithoutBoth = new URIKeyPair<>(44, new URI("d2://badService"));
    List<URIKeyPair<Integer>> requests = Arrays.asList(requestWithoutKey, requestWithoutPartitionId, requestWithoutBoth);
    URIMappingResult<Integer> result = mapper.mapUris(requests);
    Assert.assertTrue(result.getMappedKeys().isEmpty());
    Assert.assertTrue(result.getUnmappedKeys().get(-1).contains(42));
    Assert.assertTrue(result.getUnmappedKeys().get(-1).contains(43));
    Assert.assertTrue(result.getUnmappedKeys().get(-1).contains(44));
}
Also used : URIKeyPair(com.linkedin.d2.balancer.util.URIKeyPair) PartitionInfoProvider(com.linkedin.d2.balancer.util.partitions.PartitionInfoProvider) URIMapper(com.linkedin.d2.balancer.URIMapper) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 7 with URIKeyPair

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

the class RingBasedURIMapperTest method testStickyAndPartitioning.

@Test
public void testStickyAndPartitioning() throws ServiceUnavailableException {
    int partitionCount = 10;
    int requestPerPartition = 100;
    int totalHostCount = 100;
    HashRingProvider ringProvider = createStaticHashRingProvider(totalHostCount, partitionCount, getHashFunction(true));
    PartitionInfoProvider infoProvider = createRangeBasedPartitionInfoProvider(partitionCount);
    URIMapper mapper = new RingBasedUriMapper(ringProvider, infoProvider);
    List<URIKeyPair<Integer>> requests = testUtil.generateRequests(partitionCount, requestPerPartition);
    URIMappingResult<Integer> results = mapper.mapUris(requests);
    Map<URI, Set<Integer>> mapping = results.getMappedKeys();
    Map<Integer, Set<Integer>> unmappedKeys = results.getUnmappedKeys();
    Map<URI, Integer> hostToPartition = results.getHostPartitionInfo();
    Assert.assertTrue(unmappedKeys.isEmpty());
    Assert.assertEquals(100, mapping.size());
    Assert.assertEquals(100, hostToPartition.size());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) PartitionInfoProvider(com.linkedin.d2.balancer.util.partitions.PartitionInfoProvider) URI(java.net.URI) URIKeyPair(com.linkedin.d2.balancer.util.URIKeyPair) URIMapper(com.linkedin.d2.balancer.URIMapper) Test(org.testng.annotations.Test)

Example 8 with URIKeyPair

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

the class RingBasedURIMapperTest method testMapUrisStickyRoutingOnly.

@Test
public void testMapUrisStickyRoutingOnly() throws ServiceUnavailableException, PartitionAccessException {
    int partitionCount = 1;
    int requestPerPartition = 1000;
    int totalHostCount = 100;
    HashRingProvider ringProvider = createStaticHashRingProvider(totalHostCount, partitionCount, getHashFunction(true));
    PartitionInfoProvider infoProvider = createRangeBasedPartitionInfoProvider(partitionCount);
    URIMapper mapper = new RingBasedUriMapper(ringProvider, infoProvider);
    List<URIKeyPair<Integer>> requests = testUtil.generateRequests(partitionCount, requestPerPartition);
    URIMappingResult<Integer> results1 = mapper.mapUris(requests);
    URIMappingResult<Integer> results2 = mapper.mapUris(requests);
    // Sticky routing between two runs
    Assert.assertEquals(results1.getMappedKeys(), results2.getMappedKeys());
    Assert.assertEquals(results1.getUnmappedKeys(), results2.getUnmappedKeys());
    Map<URI, Set<Integer>> mapping = results1.getMappedKeys();
    // Testing universal stickiness, take out 50 requests randomly and make sure they would be resolved to the same host as does URIMapper
    Collections.shuffle(requests);
    HashFunction<Request> hashFunction = ringProvider.getRequestHashFunction(TEST_SERVICE);
    for (int i = 0; i < 50; i++) {
        URIKeyPair<Integer> request = requests.get(i);
        int partitionId = infoProvider.getPartitionAccessor(TEST_SERVICE).getPartitionId(request.getRequestUri());
        Ring<URI> ring = ringProvider.getRings(request.getRequestUri()).get(partitionId);
        URI uri = ring.get(hashFunction.hash(new URIRequest(request.getRequestUri())));
        Assert.assertTrue(mapping.keySet().contains(uri));
    }
    // Only one partition
    Assert.assertEquals(1, new HashSet<>(results1.getHostPartitionInfo().values()).size());
    Assert.assertEquals(1, new HashSet<>(results2.getHostPartitionInfo().values()).size());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) PartitionInfoProvider(com.linkedin.d2.balancer.util.partitions.PartitionInfoProvider) URIRequest(com.linkedin.d2.balancer.util.URIRequest) Request(com.linkedin.r2.message.Request) URIRequest(com.linkedin.d2.balancer.util.URIRequest) URI(java.net.URI) URIKeyPair(com.linkedin.d2.balancer.util.URIKeyPair) URIMapper(com.linkedin.d2.balancer.URIMapper) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 9 with URIKeyPair

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

the class RingBasedURIMapperTest method testSameHostSupportingMultiplePartitions.

/**
 * If one host supports multiple partitions and for those partitions, this same one host happens to be picked, URIMapper should
 * merge the key entries for those partitions.
 */
@Test
public void testSameHostSupportingMultiplePartitions() throws ServiceUnavailableException {
    int partitionCount = 10;
    int requestPerPartition = 100;
    // one host supporting 10 partitions
    URI host = createHostURI(0, 0);
    List<Ring<URI>> rings = IntStream.range(0, partitionCount).boxed().map(i -> new MPConsistentHashRing<>(Collections.singletonMap(host, 100))).collect(Collectors.toList());
    StaticRingProvider ringProvider = new StaticRingProvider(rings);
    ringProvider.setHashFunction(new RandomHash());
    PartitionInfoProvider infoProvider = createRangeBasedPartitionInfoProvider(partitionCount);
    URIMapper mapper = new RingBasedUriMapper(ringProvider, infoProvider);
    List<URIKeyPair<Integer>> requests = testUtil.generateRequests(partitionCount, requestPerPartition);
    URIMappingResult<Integer> results = mapper.mapUris(requests);
    Map<URI, Set<Integer>> mapping = results.getMappedKeys();
    Map<Integer, Set<Integer>> unmappedKeys = results.getUnmappedKeys();
    Assert.assertTrue(unmappedKeys.isEmpty());
    Assert.assertEquals(1, mapping.size());
    Assert.assertEquals(1000, mapping.values().iterator().next().size());
}
Also used : IntStream(java.util.stream.IntStream) PartitionAccessException(com.linkedin.d2.balancer.util.partitions.PartitionAccessException) Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) URIKeyPair(com.linkedin.d2.balancer.util.URIKeyPair) URISyntaxException(java.net.URISyntaxException) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) URIMappingResult(com.linkedin.d2.balancer.util.URIMappingResult) URIMapper(com.linkedin.d2.balancer.URIMapper) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) URIRequest(com.linkedin.d2.balancer.util.URIRequest) HashSet(java.util.HashSet) List(java.util.List) Request(com.linkedin.r2.message.Request) Assert(org.testng.Assert) Map(java.util.Map) URI(java.net.URI) PartitionInfoProvider(com.linkedin.d2.balancer.util.partitions.PartitionInfoProvider) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) Collections(java.util.Collections) URIMapperTestUtil(com.linkedin.d2.balancer.util.hashing.URIMapperTestUtil) Set(java.util.Set) HashSet(java.util.HashSet) PartitionInfoProvider(com.linkedin.d2.balancer.util.partitions.PartitionInfoProvider) URI(java.net.URI) URIKeyPair(com.linkedin.d2.balancer.util.URIKeyPair) URIMapper(com.linkedin.d2.balancer.URIMapper) Test(org.testng.annotations.Test)

Example 10 with URIKeyPair

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

the class RingBasedURIMapperTest method testNonStickyAndNonPartitioning.

@Test
public void testNonStickyAndNonPartitioning() throws ServiceUnavailableException {
    int partitionCount = 1;
    int requestPerPartition = 1000;
    int totalHostCount = 100;
    HashRingProvider ringProvider = createStaticHashRingProvider(totalHostCount, partitionCount, getHashFunction(false));
    PartitionInfoProvider infoProvider = createRangeBasedPartitionInfoProvider(partitionCount);
    URIMapper mapper = new RingBasedUriMapper(ringProvider, infoProvider);
    List<URIKeyPair<Integer>> requests = testUtil.generateRequests(partitionCount, requestPerPartition);
    URIMappingResult<Integer> results = mapper.mapUris(requests);
    Map<URI, Set<Integer>> mapping = results.getMappedKeys();
    Map<Integer, Set<Integer>> unmappedKeys = results.getUnmappedKeys();
    Map<URI, Integer> hostToPartitionId = results.getHostPartitionInfo();
    Assert.assertTrue(unmappedKeys.isEmpty());
    Assert.assertEquals(1, mapping.size());
    Assert.assertEquals(1, hostToPartitionId.size());
    Assert.assertEquals(1000, mapping.values().iterator().next().size());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) PartitionInfoProvider(com.linkedin.d2.balancer.util.partitions.PartitionInfoProvider) URI(java.net.URI) URIKeyPair(com.linkedin.d2.balancer.util.URIKeyPair) URIMapper(com.linkedin.d2.balancer.URIMapper) Test(org.testng.annotations.Test)

Aggregations

URI (java.net.URI)12 URIMapper (com.linkedin.d2.balancer.URIMapper)11 URIKeyPair (com.linkedin.d2.balancer.util.URIKeyPair)11 PartitionInfoProvider (com.linkedin.d2.balancer.util.partitions.PartitionInfoProvider)11 HashSet (java.util.HashSet)10 Set (java.util.Set)10 Test (org.testng.annotations.Test)9 URIRequest (com.linkedin.d2.balancer.util.URIRequest)6 Request (com.linkedin.r2.message.Request)6 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 URIMappingResult (com.linkedin.d2.balancer.util.URIMappingResult)4 PartitionAccessException (com.linkedin.d2.balancer.util.partitions.PartitionAccessException)4 List (java.util.List)4 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)3 DefaultPartitionAccessor (com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor)3 PartitionAccessor (com.linkedin.d2.balancer.util.partitions.PartitionAccessor)3 Collections (java.util.Collections)3 Collectors (java.util.stream.Collectors)3