Search in sources :

Example 6 with PartitionAccessorList

use of com.linkedin.d2.PartitionAccessorList in project rest.li by linkedin.

the class TestD2Config method testPartitionAccessorFactoryWithEmptyClassList.

// Test PartitionAccessorFactory: empty ClassList
@Test
public static void testPartitionAccessorFactoryWithEmptyClassList() throws IOException, InterruptedException, URISyntaxException, Exception {
    @SuppressWarnings("serial") final Map<String, List<String>> clustersData = new HashMap<String, List<String>>() {

        {
            put("partitioned-cluster", Arrays.asList(new String[] { "partitioned-service-1", "partitioned-service-2" }));
        }
    };
    final Map<String, Object> partitionProperties = new HashMap<>();
    Map<String, Object> customized = new HashMap<>();
    List<String> classList = Collections.emptyList();
    customized.put("partitionType", "CUSTOM");
    customized.put("partitionCount", "10");
    customized.put("partitionAccessorList", classList);
    partitionProperties.put("partitionProperties", customized);
    final PartitionAccessorRegistry registry = new PartitionAccessorRegistry() {

        private final Map<String, List<BasePartitionAccessor>> _registry = new HashMap<>();

        @Override
        public void register(String clusterName, BasePartitionAccessor accessor) {
            List<BasePartitionAccessor> accessors = _registry.computeIfAbsent(clusterName, k -> new ArrayList<>());
            accessors.add(accessor);
        }

        @Override
        public List<BasePartitionAccessor> getPartitionAccessors(String clusterName) {
            return _registry.get(clusterName);
        }
    };
    class TestPartitionAccessor1 implements PartitionAccessor {

        @Override
        public int getPartitionId(URI uri) throws PartitionAccessException {
            return testGetPartitionId(uri);
        }

        @Override
        public int getMaxPartitionId() {
            return 10;
        }
    }
    ;
    class TestPartitionAccessor2 implements PartitionAccessor {

        @Override
        public int getPartitionId(URI uri) throws PartitionAccessException {
            return 8;
        }

        @Override
        public int getMaxPartitionId() {
            return 10;
        }
    }
    ;
    PartitionAccessor testAccessor1 = new TestPartitionAccessor1();
    PartitionAccessor testAccessor2 = new TestPartitionAccessor2();
    registry.register("partitioned-cluster", DefaultPartitionAccessor.getInstance());
    registry.register("partitioned-cluster", testAccessor1);
    registry.register("partitioned-cluster", testAccessor2);
    D2ConfigTestUtil d2Conf = new D2ConfigTestUtil(clustersData, partitionProperties);
    assertEquals(d2Conf.runDiscovery(_zkHosts), 0);
    verifyPartitionProperties("partitioned-cluster", partitionProperties);
    final ClusterProperties clusterprops = getClusterProperties(_zkclient, "partitioned-cluster");
    final PartitionAccessor accessor = PartitionAccessorFactory.getPartitionAccessor("partitioned-cluster", registry, clusterprops.getPartitionProperties());
    final String legalUri1 = "/profiles?field=position&id=100";
    final String legalUri2 = "/cap?wid=99&id=176&randid=301";
    final String legalUri3 = "/seas?id=3324";
    final String illegalUri = "/?id=1000000000000000000000000000000000000000000000111111111";
    assertEquals(0, accessor.getPartitionId(URI.create(legalUri1)));
    assertEquals(0, accessor.getPartitionId(URI.create(legalUri2)));
    assertEquals(0, accessor.getPartitionId(URI.create(legalUri3)));
    assertEquals(0, accessor.getPartitionId(URI.create(illegalUri)));
}
Also used : HashMap(java.util.HashMap) URI(java.net.URI) DefaultPartitionAccessor(com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor) PartitionAccessor(com.linkedin.d2.balancer.util.partitions.PartitionAccessor) BasePartitionAccessor(com.linkedin.d2.balancer.util.partitions.BasePartitionAccessor) PartitionAccessorRegistry(com.linkedin.d2.balancer.util.partitions.PartitionAccessorRegistry) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) List(java.util.List) ArrayList(java.util.ArrayList) BasePartitionAccessor(com.linkedin.d2.balancer.util.partitions.BasePartitionAccessor) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Aggregations

ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Map (java.util.Map)5 BasePartitionAccessor (com.linkedin.d2.balancer.util.partitions.BasePartitionAccessor)4 DefaultPartitionAccessor (com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor)4 PartitionAccessor (com.linkedin.d2.balancer.util.partitions.PartitionAccessor)4 PartitionAccessorRegistry (com.linkedin.d2.balancer.util.partitions.PartitionAccessorRegistry)4 URI (java.net.URI)4 Test (org.testng.annotations.Test)4 CustomizedPartitionProperties (com.linkedin.d2.balancer.properties.CustomizedPartitionProperties)2 HashBasedPartitionProperties (com.linkedin.d2.balancer.properties.HashBasedPartitionProperties)2 RangeBasedPartitionProperties (com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties)2 D2ClusterPartitionConfiguration (com.linkedin.d2.D2ClusterPartitionConfiguration)1 PartitionAccessorList (com.linkedin.d2.PartitionAccessorList)1 RangedPartitionProperties (com.linkedin.d2.RangedPartitionProperties)1 PartitionProperties (com.linkedin.d2.balancer.properties.PartitionProperties)1 StringArray (com.linkedin.data.template.StringArray)1