Search in sources :

Example 1 with PartitionAccessorRegistry

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

the class TestD2Config method testSingleClusterCustomizedPartitions.

// preliminary test for customized partitioning cluster
@Test
public static void testSingleClusterCustomizedPartitions() throws IOException, InterruptedException, URISyntaxException, Exception {
    @SuppressWarnings("serial") final Map<String, List<String>> clustersData = new HashMap<String, List<String>>() {

        {
            put("partitioned-cluster", Arrays.asList("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);
        }
    };
    final BasePartitionAccessor customizedAccessor = new BasePartitionAccessor() {

        @Override
        public int getPartitionId(URI uri) throws PartitionAccessException {
            return testGetPartitionId(uri);
        }
    };
    registry.register("partitioned-cluster", customizedAccessor);
    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(1, accessor.getPartitionId(URI.create(legalUri2)));
    assertEquals(2, accessor.getPartitionId(URI.create(legalUri3)));
    assertEquals(0, accessor.getPartitionId(URI.create(illegalUri)));
    // Start Echo server on cluster-1
    Map<Integer, Double> serverConfig1 = new HashMap<>();
    serverConfig1.put(0, 0.5d);
    serverConfig1.put(3, 0.5d);
    Map<Integer, Double> serverConfig2 = new HashMap<>();
    serverConfig2.put(0, 0.25d);
    serverConfig2.put(1, 0.5d);
    serverConfig2.put(2, 0.5d);
    final int echoServerPort1 = 2346;
    final int echoServerPort2 = 2347;
    _echoServerList.add(startEchoServer(echoServerPort1, "partitioned-cluster", serverConfig1));
    _echoServerList.add(startEchoServer(echoServerPort2, "partitioned-cluster", serverConfig2));
    Map<URI, Map<Integer, Double>> partitionWeights = new HashMap<>();
    partitionWeights.put(URI.create("http://127.0.0.1:" + echoServerPort1 + "/partitioned-cluster"), serverConfig1);
    partitionWeights.put(URI.create("http://127.0.0.1:" + echoServerPort2 + "/partitioned-cluster"), serverConfig2);
    verifyPartitionedUriProperties("partitioned-cluster", partitionWeights);
}
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)

Example 2 with PartitionAccessorRegistry

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

the class TestD2Config method testPartitionAccessorFactoryWithoutMatch.

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

        {
            put("partitioned-cluster", Arrays.asList("partitioned-service-1", "partitioned-service-2"));
        }
    };
    final Map<String, Object> partitionProperties = new HashMap<>();
    Map<String, Object> customized = new HashMap<>();
    List<String> classList = Arrays.asList("NoClass");
    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)

Example 3 with PartitionAccessorRegistry

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

the class TestD2Config method testPartitionAccessorFactory.

// Test PartitionAccessorFactory: match ClassList
@Test
public static void testPartitionAccessorFactory() 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 = Arrays.asList("TestPartitionAccessor1", "TestPartitionAccessor2");
    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(1, accessor.getPartitionId(URI.create(legalUri2)));
    assertEquals(2, 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)

Example 4 with PartitionAccessorRegistry

use of com.linkedin.d2.balancer.util.partitions.PartitionAccessorRegistry 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)4 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 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 Test (org.testng.annotations.Test)4