use of com.linkedin.d2.loadBalancerStrategyType in project rest.li by linkedin.
the class TestLoadBalancerStrategy method testSlowStartWithInitialHealthScore.
@Test(dataProvider = "strategy")
public void testSlowStartWithInitialHealthScore(loadBalancerStrategyType type) {
Map<String, String> degraderPropertiesWithSlowStart = new HashMap<>();
D2RelativeStrategyProperties relativePropertiesWithSlowStart = new D2RelativeStrategyProperties();
degraderPropertiesWithSlowStart.put(PropertyKeys.DEGRADER_INITIAL_DROP_RATE, "0.99");
degraderPropertiesWithSlowStart.put(PropertyKeys.DEGRADER_SLOW_START_THRESHOLD, "0.5");
relativePropertiesWithSlowStart.setInitialHealthScore(0.01);
relativePropertiesWithSlowStart.setSlowStartThreshold(0.5);
LoadBalancerStrategyTestRunnerBuilder builder = new LoadBalancerStrategyTestRunnerBuilder(type, DEFAULT_SERVICE_NAME, 5).setConstantRequestCount(60).setNumIntervals(30).setDegraderStrategies(new HashMap<>(), degraderPropertiesWithSlowStart).setConstantLatency(Arrays.asList(HEALTHY_HOST_CONSTANT_LATENCY, HEALTHY_HOST_CONSTANT_LATENCY, HEALTHY_HOST_CONSTANT_LATENCY, HEALTHY_HOST_CONSTANT_LATENCY, HEALTHY_HOST_CONSTANT_LATENCY));
LoadBalancerStrategyTestRunner testRunner = type == loadBalancerStrategyType.DEGRADER ? builder.setDegraderStrategies(new HashMap<>(), degraderPropertiesWithSlowStart).build() : builder.setRelativeLoadBalancerStrategies(relativePropertiesWithSlowStart).build();
testRunner.runWait();
List<Integer> pointHistory = testRunner.getPointHistory().get(testRunner.getUri(0));
assertTrue(hasPointsInHistory(pointHistory, Arrays.asList(1, 4, 16)));
}
use of com.linkedin.d2.loadBalancerStrategyType 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.loadBalancerStrategyType in project rest.li by linkedin.
the class SimpleLoadBalancer method getPartitionInformation.
/**
* If given a collection of keys, the method will maps keys to partitions and
* return the servers that belongs to that partition up to limitHostPerPartition.
*
* If no keys are specified, the method will return hosts in all partitions
*
* @param serviceUri for example d2://articles
* @param keys all the keys we want to find the partition for
* @param limitHostPerPartition the number of hosts that we should return for this partition. Must be larger than 0.
* @param hash this will be used to create Iterator for the hosts in the hash ring
* @return Number of hosts in requested partitions. See {@link com.linkedin.d2.balancer.util.HostToKeyMapper} for more details.
* @throws ServiceUnavailableException
*/
@Override
public <K> HostToKeyMapper<K> getPartitionInformation(URI serviceUri, Collection<K> keys, int limitHostPerPartition, int hash) throws ServiceUnavailableException {
if (limitHostPerPartition <= 0) {
throw new IllegalArgumentException("limitHostPartition cannot be 0 or less");
}
ServiceProperties service = listenToServiceAndCluster(serviceUri);
String serviceName = service.getServiceName();
String clusterName = service.getClusterName();
ClusterProperties cluster = getClusterProperties(serviceName, clusterName);
LoadBalancerStateItem<UriProperties> uriItem = getUriItem(serviceName, clusterName, cluster);
UriProperties uris = uriItem.getProperty();
List<LoadBalancerState.SchemeStrategyPair> orderedStrategies = _state.getStrategiesForService(serviceName, service.getPrioritizedSchemes());
Map<Integer, Integer> partitionWithoutEnoughHost = new HashMap<>();
if (!orderedStrategies.isEmpty()) {
// get the partitionId -> keys mapping
final PartitionAccessor accessor = getPartitionAccessor(serviceName, clusterName);
int maxPartitionId = accessor.getMaxPartitionId();
List<K> unmappedKeys = new ArrayList<>();
Map<Integer, Set<K>> partitionSet = getPartitionSet(keys, accessor, unmappedKeys);
// get the partitionId -> host URIs list
Map<Integer, KeysAndHosts<K>> partitionDataMap = new HashMap<>();
for (Integer partitionId : partitionSet.keySet()) {
for (LoadBalancerState.SchemeStrategyPair pair : orderedStrategies) {
TrackerClientSubsetItem subsetItem = getPotentialClients(serviceName, service, cluster, uris, pair.getScheme(), partitionId, uriItem.getVersion());
Map<URI, TrackerClient> trackerClients = subsetItem.getWeightedSubset();
int size = Math.min(trackerClients.size(), limitHostPerPartition);
List<URI> rankedUri = new ArrayList<>(size);
Ring<URI> ring = pair.getStrategy().getRing(uriItem.getVersion(), partitionId, trackerClients, subsetItem.shouldForceUpdate());
Iterator<URI> iterator = ring.getIterator(hash);
while (iterator.hasNext() && rankedUri.size() < size) {
URI uri = iterator.next();
if (!rankedUri.contains(uri)) {
rankedUri.add(uri);
}
}
if (rankedUri.size() < limitHostPerPartition) {
partitionWithoutEnoughHost.put(partitionId, limitHostPerPartition - rankedUri.size());
}
KeysAndHosts<K> keysAndHosts = new KeysAndHosts<>(partitionSet.get(partitionId), rankedUri);
partitionDataMap.put(partitionId, keysAndHosts);
if (!rankedUri.isEmpty()) {
// don't go to the next strategy if there are already hosts in the current one
break;
}
}
}
return new HostToKeyMapper<>(unmappedKeys, partitionDataMap, limitHostPerPartition, maxPartitionId + 1, partitionWithoutEnoughHost);
} else {
throw new ServiceUnavailableException(serviceName, "PEGA_1009. Unable to find a load balancer strategy" + "Server Schemes: [" + String.join(", ", service.getPrioritizedSchemes()) + ']');
}
}
use of com.linkedin.d2.loadBalancerStrategyType in project rest.li by linkedin.
the class ConsistentHashRingSimulatorConfig method toSimulator.
/**
* Creates a {@link ConsistentHashRingSimulator} from the config
*
* @return A ConsistentHashRingSimulator instance
*/
public ConsistentHashRingSimulator toSimulator() {
String hashingAlgorithm = getHashingAlgorithm();
double balancingFactor = getBoundedLoadBalancingFactor();
DegraderLoadBalancerStrategyConfig degraderLoadBalancerStrategyConfig = getConfig(hashingAlgorithm, balancingFactor);
RingFactory<String> testFactory = new DelegatingRingFactory<>(degraderLoadBalancerStrategyConfig);
Map<String, Integer> pointsMap = new HashMap<>();
int serverID = 0;
for (Server server : getServers()) {
for (int i = 0; i < server.getNumber(); i++) {
pointsMap.put("Server" + serverID, server.getPoints());
serverID += 1;
}
}
DegraderLoadBalancerStrategyConfig consistentConfig = getConfig(hashingAlgorithm, Double.POSITIVE_INFINITY);
RingFactory<String> consistentFactory = new DelegatingRingFactory<>(consistentConfig);
List<com.linkedin.d2.balancer.util.hashing.simulator.Client> clients = new ArrayList<>();
int clientID = 0;
for (ConsistentHashRingSimulatorConfig.Client client : getClients()) {
for (int i = 0; i < client.getNumber(); i++) {
clients.add(new com.linkedin.d2.balancer.util.hashing.simulator.Client("Client" + clientID, client, getShuffleRequests()));
clientID++;
}
}
int serverCapacity = getServerCapacity();
return new ConsistentHashRingSimulator(testFactory, consistentFactory, clients, pointsMap, serverCapacity);
}
use of com.linkedin.d2.loadBalancerStrategyType in project rest.li by linkedin.
the class TestLoadBalancerStrategy method testOneHost.
@Test(dataProvider = "strategy")
public void testOneHost(loadBalancerStrategyType type) {
LoadBalancerStrategyTestRunner testRunner = new LoadBalancerStrategyTestRunnerBuilder(type, // Set to corner case - only 1 host
DEFAULT_SERVICE_NAME, 1).setConstantRequestCount(100).setNumIntervals(3).setConstantLatency(Arrays.asList(HEALTHY_HOST_CONSTANT_LATENCY)).build();
testRunner.runWait();
List<Integer> pointHistory = testRunner.getPointHistory().get(testRunner.getUri(0));
assertEquals(pointHistory.get(2).intValue(), HEALTHY_POINTS);
}
Aggregations