Search in sources :

Example 1 with URIMapper

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

the class RingBasedURIMapperTest method testNeedScatterGather.

@Test
public void testNeedScatterGather() throws ServiceUnavailableException {
    // Both sticky and partitioned
    HashRingProvider ringProvider = createStaticHashRingProvider(100, 10, getHashFunction(true));
    PartitionInfoProvider infoProvider = createRangeBasedPartitionInfoProvider(10);
    URIMapper mapper = new RingBasedUriMapper(ringProvider, infoProvider);
    Assert.assertTrue(mapper.needScatterGather(TEST_SERVICE));
    // Only sticky
    ringProvider = createStaticHashRingProvider(100, 1, getHashFunction(true));
    infoProvider = createRangeBasedPartitionInfoProvider(1);
    mapper = new RingBasedUriMapper(ringProvider, infoProvider);
    Assert.assertTrue(mapper.needScatterGather(TEST_SERVICE));
    // Only partitioned
    ringProvider = createStaticHashRingProvider(100, 10, getHashFunction(false));
    infoProvider = createRangeBasedPartitionInfoProvider(10);
    mapper = new RingBasedUriMapper(ringProvider, infoProvider);
    Assert.assertTrue(mapper.needScatterGather(TEST_SERVICE));
    // neither
    ringProvider = createStaticHashRingProvider(100, 1, getHashFunction(false));
    infoProvider = createRangeBasedPartitionInfoProvider(1);
    mapper = new RingBasedUriMapper(ringProvider, infoProvider);
    Assert.assertFalse(mapper.needScatterGather(TEST_SERVICE));
}
Also used : PartitionInfoProvider(com.linkedin.d2.balancer.util.partitions.PartitionInfoProvider) URIMapper(com.linkedin.d2.balancer.URIMapper) Test(org.testng.annotations.Test)

Example 2 with URIMapper

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

the class RingBasedURIMapperTest method testPartitionIdOverride.

@Test(dataProvider = "stickyPartitionPermutation")
public void testPartitionIdOverride(boolean sticky, boolean partitioned) throws Exception {
    int partitionCount = partitioned ? 10 : 1;
    int totalHostCount = 100;
    HashRingProvider ringProvider = createStaticHashRingProvider(totalHostCount, partitionCount, getHashFunction(sticky));
    PartitionInfoProvider infoProvider = createRangeBasedPartitionInfoProvider(partitionCount);
    URIMapper mapper = new RingBasedUriMapper(ringProvider, infoProvider);
    URIKeyPair<Integer> request = new URIKeyPair<>(new URI("d2://testService/1"), IntStream.range(0, partitionCount).boxed().collect(Collectors.toSet()));
    if (partitioned) {
        Assert.assertThrows(() -> mapper.mapUris(Arrays.asList(request, request)));
    }
    URIMappingResult<Integer> uriMapperResult = mapper.mapUris(Collections.singletonList(request));
    Map<URI, Set<Integer>> mappedKeys = uriMapperResult.getMappedKeys();
    Assert.assertTrue(uriMapperResult.getUnmappedKeys().isEmpty());
    Assert.assertEquals(mappedKeys.size(), partitionCount);
    Assert.assertEquals(mappedKeys.keySet().stream().map(URIMapperTestUtil::getPartitionIdForURI).collect(Collectors.toSet()).size(), partitionCount);
    for (Set<Integer> keys : mappedKeys.values()) {
        Assert.assertTrue(keys.isEmpty());
    }
}
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 3 with URIMapper

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

the class RingBasedURIMapperTest method testUnmappedKeys.

@Test(dataProvider = "stickyPartitionPermutation")
public void testUnmappedKeys(boolean sticky, boolean partitioned) throws Exception {
    int partitionCount = partitioned ? 10 : 1;
    int requestPerPartition = 100;
    List<Ring<URI>> rings = new ArrayList<>();
    IntStream.range(0, partitionCount).forEach(i -> rings.add(new MPConsistentHashRing<>(Collections.emptyMap())));
    StaticRingProvider ringProvider = new StaticRingProvider(rings);
    ringProvider.setHashFunction(getHashFunction(sticky));
    PartitionInfoProvider infoProvider = createRangeBasedPartitionInfoProvider(partitionCount);
    URIMapper mapper = new RingBasedUriMapper(ringProvider, infoProvider);
    List<URIKeyPair<Integer>> requests = testUtil.generateRequests(partitionCount, requestPerPartition);
    URIMappingResult<Integer> uriMapperResultNormal = mapper.mapUris(requests);
    Assert.assertTrue(uriMapperResultNormal.getUnmappedKeys().size() == partitionCount);
    uriMapperResultNormal.getUnmappedKeys().forEach((key, value) -> Assert.assertTrue(value.size() == requestPerPartition));
    URIKeyPair<Integer> request = new URIKeyPair<>(new URI("d2://testService/1"), IntStream.range(0, partitionCount).boxed().collect(Collectors.toSet()));
    URIMappingResult<Integer> uriMapperResultCustom = mapper.mapUris(Collections.singletonList(request));
    Assert.assertTrue(uriMapperResultCustom.getUnmappedKeys().size() == partitionCount);
    uriMapperResultCustom.getUnmappedKeys().forEach((key, value) -> Assert.assertTrue(value.isEmpty()));
}
Also used : PartitionInfoProvider(com.linkedin.d2.balancer.util.partitions.PartitionInfoProvider) ArrayList(java.util.ArrayList) URI(java.net.URI) URIKeyPair(com.linkedin.d2.balancer.util.URIKeyPair) URIMapper(com.linkedin.d2.balancer.URIMapper) Test(org.testng.annotations.Test)

Example 4 with URIMapper

use of com.linkedin.d2.balancer.URIMapper 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 5 with URIMapper

use of com.linkedin.d2.balancer.URIMapper 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)

Aggregations

PartitionInfoProvider (com.linkedin.d2.balancer.util.partitions.PartitionInfoProvider)11 URIMapper (com.linkedin.d2.balancer.URIMapper)10 Test (org.testng.annotations.Test)10 URIKeyPair (com.linkedin.d2.balancer.util.URIKeyPair)9 URI (java.net.URI)9 HashSet (java.util.HashSet)7 Set (java.util.Set)7 URIRequest (com.linkedin.d2.balancer.util.URIRequest)3 Request (com.linkedin.r2.message.Request)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 PartitionAccessException (com.linkedin.d2.balancer.util.partitions.PartitionAccessException)2 ArrayList (java.util.ArrayList)2 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)1 URIMappingResult (com.linkedin.d2.balancer.util.URIMappingResult)1 HashRingProvider (com.linkedin.d2.balancer.util.hashing.HashRingProvider)1 RingBasedUriMapper (com.linkedin.d2.balancer.util.hashing.RingBasedUriMapper)1 URIMapperTestUtil (com.linkedin.d2.balancer.util.hashing.URIMapperTestUtil)1 URIMapperTestUtil.createHashBasedPartitionInfoProvider (com.linkedin.d2.balancer.util.hashing.URIMapperTestUtil.createHashBasedPartitionInfoProvider)1 URIMapperTestUtil.createStaticHashRingProvider (com.linkedin.d2.balancer.util.hashing.URIMapperTestUtil.createStaticHashRingProvider)1