Search in sources :

Example 1 with CustomizedPartitionProperties

use of com.linkedin.d2.balancer.properties.CustomizedPartitionProperties 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 CustomizedPartitionProperties

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

the class PartitionPropertiesConverter method toProperties.

public static PartitionProperties toProperties(D2ClusterPartitionConfiguration config) {
    final PartitionProperties partitionProperties;
    switch(config.getType()) {
        case RANGE:
            {
                RangedPartitionProperties rangedPartitionProperties = config.getPartitionTypeSpecificData().getRangedPartitionProperties();
                partitionProperties = new RangeBasedPartitionProperties(config.getPartitionKeyRegex(), rangedPartitionProperties.getKeyRangeStart(), rangedPartitionProperties.getPartitionSize(), config.getPartitionCount());
                break;
            }
        case HASH:
            HashBasedPartitionProperties.HashAlgorithm algorithm;
            switch(config.getPartitionTypeSpecificData().getHashAlgorithm()) {
                case MODULO:
                    algorithm = HashBasedPartitionProperties.HashAlgorithm.MODULO;
                    break;
                case MD5:
                    algorithm = HashBasedPartitionProperties.HashAlgorithm.MD5;
                    break;
                case XXHASH:
                    algorithm = HashBasedPartitionProperties.HashAlgorithm.XXHASH;
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported hash algorithm: " + config.getPartitionTypeSpecificData().getHashAlgorithm());
            }
            partitionProperties = new HashBasedPartitionProperties(config.getPartitionKeyRegex(), config.getPartitionCount(), algorithm);
            break;
        case CUSTOM:
            partitionProperties = new CustomizedPartitionProperties(config.getPartitionCount(), config.getPartitionTypeSpecificData().getPartitionAccessorList().getClassNames());
            break;
        case NONE:
            partitionProperties = NullPartitionProperties.getInstance();
            break;
        default:
            throw new IllegalArgumentException("Unsupported partitionType: " + config.getType());
    }
    return partitionProperties;
}
Also used : RangedPartitionProperties(com.linkedin.d2.RangedPartitionProperties) CustomizedPartitionProperties(com.linkedin.d2.balancer.properties.CustomizedPartitionProperties) PartitionProperties(com.linkedin.d2.balancer.properties.PartitionProperties) RangedPartitionProperties(com.linkedin.d2.RangedPartitionProperties) CustomizedPartitionProperties(com.linkedin.d2.balancer.properties.CustomizedPartitionProperties) NullPartitionProperties(com.linkedin.d2.balancer.properties.NullPartitionProperties) HashBasedPartitionProperties(com.linkedin.d2.balancer.properties.HashBasedPartitionProperties) RangeBasedPartitionProperties(com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties) HashBasedPartitionProperties(com.linkedin.d2.balancer.properties.HashBasedPartitionProperties) RangeBasedPartitionProperties(com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties)

Example 3 with CustomizedPartitionProperties

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

Aggregations

CustomizedPartitionProperties (com.linkedin.d2.balancer.properties.CustomizedPartitionProperties)3 HashBasedPartitionProperties (com.linkedin.d2.balancer.properties.HashBasedPartitionProperties)3 RangeBasedPartitionProperties (com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties)3 RangedPartitionProperties (com.linkedin.d2.RangedPartitionProperties)2 PartitionProperties (com.linkedin.d2.balancer.properties.PartitionProperties)2 D2ClusterPartitionConfiguration (com.linkedin.d2.D2ClusterPartitionConfiguration)1 PartitionAccessorList (com.linkedin.d2.PartitionAccessorList)1 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)1 NullPartitionProperties (com.linkedin.d2.balancer.properties.NullPartitionProperties)1 StringArray (com.linkedin.data.template.StringArray)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1