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;
}
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);
}
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;
}
}
}
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)));
}
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)));
}
Aggregations