Search in sources :

Example 1 with RangedPartitionProperties

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

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

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

the class PartitionPropertiesConverterTest method testRangePartitionProperties.

@Test
public void testRangePartitionProperties() {
    final String partitionKeyRegex = "/foo/bar/(\\d+)";
    final long keyRangeStart = 1;
    final long paritionSize = 1024;
    final int partitionCount = 32;
    PartitionProperties partitionProperties = new RangeBasedPartitionProperties(partitionKeyRegex, keyRangeStart, paritionSize, partitionCount);
    D2ClusterPartitionConfiguration.PartitionTypeSpecificData data = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
    data.setRangedPartitionProperties(new RangedPartitionProperties().setKeyRangeStart(keyRangeStart).setPartitionSize(paritionSize));
    D2ClusterPartitionConfiguration partitionConfig = new D2ClusterPartitionConfiguration().setType(PartitionTypeEnum.RANGE).setPartitionKeyRegex(partitionKeyRegex).setPartitionCount(partitionCount).setPartitionTypeSpecificData(data);
    Assert.assertEquals(PartitionPropertiesConverter.toProperties(partitionConfig), partitionProperties);
    Assert.assertEquals(PartitionPropertiesConverter.toConfig(partitionProperties), partitionConfig);
}
Also used : RangedPartitionProperties(com.linkedin.d2.RangedPartitionProperties) PartitionProperties(com.linkedin.d2.balancer.properties.PartitionProperties) RangedPartitionProperties(com.linkedin.d2.RangedPartitionProperties) NullPartitionProperties(com.linkedin.d2.balancer.properties.NullPartitionProperties) HashBasedPartitionProperties(com.linkedin.d2.balancer.properties.HashBasedPartitionProperties) RangeBasedPartitionProperties(com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties) RangeBasedPartitionProperties(com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties) D2ClusterPartitionConfiguration(com.linkedin.d2.D2ClusterPartitionConfiguration) Test(org.testng.annotations.Test)

Aggregations

RangedPartitionProperties (com.linkedin.d2.RangedPartitionProperties)3 HashBasedPartitionProperties (com.linkedin.d2.balancer.properties.HashBasedPartitionProperties)3 RangeBasedPartitionProperties (com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties)3 D2ClusterPartitionConfiguration (com.linkedin.d2.D2ClusterPartitionConfiguration)2 CustomizedPartitionProperties (com.linkedin.d2.balancer.properties.CustomizedPartitionProperties)2 NullPartitionProperties (com.linkedin.d2.balancer.properties.NullPartitionProperties)2 PartitionProperties (com.linkedin.d2.balancer.properties.PartitionProperties)2 PartitionAccessorList (com.linkedin.d2.PartitionAccessorList)1 StringArray (com.linkedin.data.template.StringArray)1 Test (org.testng.annotations.Test)1