Search in sources :

Example 1 with PartitionAccessorList

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

the class PartitionPropertiesConverter method toConfig.

public static D2ClusterPartitionConfiguration toConfig(PartitionProperties property) {
    final D2ClusterPartitionConfiguration config;
    final D2ClusterPartitionConfiguration.PartitionTypeSpecificData specificData;
    switch(property.getPartitionType()) {
        case RANGE:
            RangeBasedPartitionProperties range = (RangeBasedPartitionProperties) property;
            config = new D2ClusterPartitionConfiguration();
            config.setType(PartitionTypeEnum.RANGE);
            config.setPartitionKeyRegex(range.getPartitionKeyRegex());
            config.setPartitionCount(range.getPartitionCount());
            specificData = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
            RangedPartitionProperties rangedPartitionProperties = new RangedPartitionProperties();
            rangedPartitionProperties.setKeyRangeStart(range.getKeyRangeStart());
            rangedPartitionProperties.setPartitionSize(range.getPartitionSize());
            specificData.setRangedPartitionProperties(rangedPartitionProperties);
            config.setPartitionTypeSpecificData(specificData);
            break;
        case HASH:
            HashBasedPartitionProperties hash = (HashBasedPartitionProperties) property;
            config = new D2ClusterPartitionConfiguration();
            config.setType(PartitionTypeEnum.HASH);
            config.setPartitionKeyRegex(hash.getPartitionKeyRegex());
            config.setPartitionCount(hash.getPartitionCount());
            specificData = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
            specificData.setHashAlgorithm(HashAlgorithm.valueOf(hash.getHashAlgorithm().name()));
            config.setPartitionTypeSpecificData(specificData);
            break;
        case CUSTOM:
            {
                CustomizedPartitionProperties properties = (CustomizedPartitionProperties) property;
                config = new D2ClusterPartitionConfiguration();
                config.setType(PartitionTypeEnum.CUSTOM);
                config.setPartitionCount(properties.getPartitionCount());
                specificData = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
                PartitionAccessorList partitionList = new PartitionAccessorList();
                partitionList.setClassNames(new StringArray(properties.getPartitionAccessorList()));
                specificData.setPartitionAccessorList(partitionList);
                config.setPartitionTypeSpecificData(specificData);
                break;
            }
        case NONE:
            config = new D2ClusterPartitionConfiguration();
            config.setType(PartitionTypeEnum.NONE);
            break;
        default:
            throw new IllegalArgumentException("Unsupported partitionType: " + property.getPartitionType());
    }
    return config;
}
Also used : RangedPartitionProperties(com.linkedin.d2.RangedPartitionProperties) CustomizedPartitionProperties(com.linkedin.d2.balancer.properties.CustomizedPartitionProperties) PartitionAccessorList(com.linkedin.d2.PartitionAccessorList) StringArray(com.linkedin.data.template.StringArray) HashBasedPartitionProperties(com.linkedin.d2.balancer.properties.HashBasedPartitionProperties) RangeBasedPartitionProperties(com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties) D2ClusterPartitionConfiguration(com.linkedin.d2.D2ClusterPartitionConfiguration)

Example 2 with PartitionAccessorList

use of com.linkedin.d2.PartitionAccessorList 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 3 with PartitionAccessorList

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

the class TestD2Config method verifyPartitionProperties.

public static void verifyPartitionProperties(String cluster, Map<String, Object> propertiesMap) throws IOException, URISyntaxException, PropertyStoreException {
    final ClusterProperties clusterprops = getClusterProperties(_zkclient, cluster);
    if (propertiesMap.get("partitionProperties") != null) {
        @SuppressWarnings("unchecked") Map<String, Object> properties = (Map<String, Object>) propertiesMap.get("partitionProperties");
        PartitionProperties.PartitionType partitionType = PartitionProperties.PartitionType.valueOf((String) properties.get("partitionType"));
        switch(partitionType) {
            case RANGE:
                {
                    long keyRangeStart = ((Number) properties.get("keyRangeStart")).longValue();
                    long partitionSize = ((Number) properties.get("partitionSize")).longValue();
                    int partitionCount = ((Number) properties.get("partitionCount")).intValue();
                    String regex = (String) properties.get("partitionKeyRegex");
                    RangeBasedPartitionProperties rbp = (RangeBasedPartitionProperties) clusterprops.getPartitionProperties();
                    assertEquals(keyRangeStart, rbp.getKeyRangeStart());
                    assertEquals(partitionSize, rbp.getPartitionSize());
                    assertEquals(partitionCount, rbp.getPartitionCount());
                    assertEquals(regex, rbp.getPartitionKeyRegex());
                }
                break;
            case HASH:
                {
                    int partitionCount = ((Number) properties.get("partitionCount")).intValue();
                    String regex = (String) properties.get("partitionKeyRegex");
                    String algorithm = (String) properties.get("hashAlgorithm");
                    HashBasedPartitionProperties.HashAlgorithm hashAlgorithm = HashBasedPartitionProperties.HashAlgorithm.valueOf(algorithm.toUpperCase());
                    HashBasedPartitionProperties hbp = (HashBasedPartitionProperties) clusterprops.getPartitionProperties();
                    assertEquals(partitionCount, hbp.getPartitionCount());
                    assertEquals(regex, hbp.getPartitionKeyRegex());
                    assertEquals(hashAlgorithm, hbp.getHashAlgorithm());
                }
                break;
            case CUSTOM:
                {
                    int partitionCount = ((Number) properties.get("partitionCount")).intValue();
                    @SuppressWarnings("unchecked") List<String> classList = (List<String>) properties.get("partitionAccessorList");
                    CustomizedPartitionProperties cbp = (CustomizedPartitionProperties) clusterprops.getPartitionProperties();
                    assertEquals(partitionCount, cbp.getPartitionCount());
                    assertEquals(classList, cbp.getPartitionAccessorList());
                    break;
                }
            default:
                break;
        }
    }
}
Also used : CustomizedPartitionProperties(com.linkedin.d2.balancer.properties.CustomizedPartitionProperties) HashBasedPartitionProperties(com.linkedin.d2.balancer.properties.HashBasedPartitionProperties) CustomizedPartitionProperties(com.linkedin.d2.balancer.properties.CustomizedPartitionProperties) PartitionProperties(com.linkedin.d2.balancer.properties.PartitionProperties) HashBasedPartitionProperties(com.linkedin.d2.balancer.properties.HashBasedPartitionProperties) RangeBasedPartitionProperties(com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) RangeBasedPartitionProperties(com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with PartitionAccessorList

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

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

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