Search in sources :

Example 1 with D2ClusterPartitionConfiguration

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

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

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

the class PartitionPropertiesConverterTest method testHashModuloPartitionProperties.

@Test
public void testHashModuloPartitionProperties() {
    final String partitionKeyRegex = "/foo/bar/(\\d+)";
    final int partitionCount = 16;
    final HashBasedPartitionProperties.HashAlgorithm hashAlgorithm = HashBasedPartitionProperties.HashAlgorithm.MODULO;
    PartitionProperties partitionProperties = new HashBasedPartitionProperties(partitionKeyRegex, partitionCount, hashAlgorithm);
    D2ClusterPartitionConfiguration.PartitionTypeSpecificData data = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
    data.setHashAlgorithm(com.linkedin.d2.HashAlgorithm.MODULO);
    D2ClusterPartitionConfiguration partitionConfig = new D2ClusterPartitionConfiguration().setType(PartitionTypeEnum.HASH).setPartitionKeyRegex(partitionKeyRegex).setPartitionCount(partitionCount).setPartitionTypeSpecificData(data);
    Assert.assertEquals(PartitionPropertiesConverter.toConfig(partitionProperties), partitionConfig);
    Assert.assertEquals(PartitionPropertiesConverter.toProperties(partitionConfig), partitionProperties);
}
Also used : 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) HashBasedPartitionProperties(com.linkedin.d2.balancer.properties.HashBasedPartitionProperties) D2ClusterPartitionConfiguration(com.linkedin.d2.D2ClusterPartitionConfiguration) Test(org.testng.annotations.Test)

Example 4 with D2ClusterPartitionConfiguration

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

the class PartitionPropertiesConverterTest method testNullPartitionProperties.

@Test
public void testNullPartitionProperties() {
    PartitionProperties partitionProperties = NullPartitionProperties.getInstance();
    D2ClusterPartitionConfiguration partitionConfig = new D2ClusterPartitionConfiguration().setType(PartitionTypeEnum.NONE);
    Assert.assertEquals(PartitionPropertiesConverter.toProperties(partitionConfig), partitionProperties);
    Assert.assertEquals(PartitionPropertiesConverter.toConfig(partitionProperties), partitionConfig);
}
Also used : 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) D2ClusterPartitionConfiguration(com.linkedin.d2.D2ClusterPartitionConfiguration) Test(org.testng.annotations.Test)

Example 5 with D2ClusterPartitionConfiguration

use of com.linkedin.d2.D2ClusterPartitionConfiguration 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)6 HashBasedPartitionProperties (com.linkedin.d2.balancer.properties.HashBasedPartitionProperties)6 RangeBasedPartitionProperties (com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties)6 D2ClusterPartitionConfiguration (com.linkedin.d2.D2ClusterPartitionConfiguration)5 NullPartitionProperties (com.linkedin.d2.balancer.properties.NullPartitionProperties)5 PartitionProperties (com.linkedin.d2.balancer.properties.PartitionProperties)5 Test (org.testng.annotations.Test)4 CustomizedPartitionProperties (com.linkedin.d2.balancer.properties.CustomizedPartitionProperties)2 PartitionAccessorList (com.linkedin.d2.PartitionAccessorList)1 StringArray (com.linkedin.data.template.StringArray)1