use of com.linkedin.d2.balancer.properties.HashBasedPartitionProperties 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;
}
use of com.linkedin.d2.balancer.properties.HashBasedPartitionProperties 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;
}
use of com.linkedin.d2.balancer.properties.HashBasedPartitionProperties 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);
}
use of com.linkedin.d2.balancer.properties.HashBasedPartitionProperties 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;
}
}
}
use of com.linkedin.d2.balancer.properties.HashBasedPartitionProperties in project rest.li by linkedin.
the class URIMapperTestUtil method createHashBasedPartitionInfoProvider.
/**
* Create a mock PartitionInfoProvider that returns {@link HashBasedPartitionAccessor} for testing
*/
public static PartitionInfoProvider createHashBasedPartitionInfoProvider(int partitionCount, String regex) throws ServiceUnavailableException {
PartitionInfoProvider infoProvider = Mockito.mock(PartitionInfoProvider.class);
HashBasedPartitionProperties properties = new HashBasedPartitionProperties(regex, partitionCount, HashBasedPartitionProperties.HashAlgorithm.MODULO);
HashBasedPartitionAccessor accessor = new HashBasedPartitionAccessor(properties);
Mockito.when(infoProvider.getPartitionAccessor(anyObject())).thenReturn(accessor);
return infoProvider;
}
Aggregations