Search in sources :

Example 6 with com.linkedin.d2.loadBalancerStrategyType

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)));
}
Also used : LoadBalancerStrategyTestRunnerBuilder(com.linkedin.d2.balancer.strategies.framework.LoadBalancerStrategyTestRunnerBuilder) HashMap(java.util.HashMap) LoadBalancerStrategyTestRunner(com.linkedin.d2.balancer.strategies.framework.LoadBalancerStrategyTestRunner) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) Test(org.testng.annotations.Test)

Example 7 with com.linkedin.d2.loadBalancerStrategyType

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);
}
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 8 with com.linkedin.d2.loadBalancerStrategyType

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()) + ']');
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KeysAndHosts(com.linkedin.d2.balancer.util.KeysAndHosts) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) URI(java.net.URI) LoadBalancerState(com.linkedin.d2.balancer.LoadBalancerState) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) HostToKeyMapper(com.linkedin.d2.balancer.util.HostToKeyMapper) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) PartitionAccessor(com.linkedin.d2.balancer.util.partitions.PartitionAccessor) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties)

Example 9 with com.linkedin.d2.loadBalancerStrategyType

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);
}
Also used : DegraderLoadBalancerStrategyConfig(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyConfig) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DelegatingRingFactory(com.linkedin.d2.balancer.strategies.DelegatingRingFactory)

Example 10 with com.linkedin.d2.loadBalancerStrategyType

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);
}
Also used : LoadBalancerStrategyTestRunnerBuilder(com.linkedin.d2.balancer.strategies.framework.LoadBalancerStrategyTestRunnerBuilder) LoadBalancerStrategyTestRunner(com.linkedin.d2.balancer.strategies.framework.LoadBalancerStrategyTestRunner) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)15 HashMap (java.util.HashMap)14 LoadBalancerStrategyTestRunner (com.linkedin.d2.balancer.strategies.framework.LoadBalancerStrategyTestRunner)13 LoadBalancerStrategyTestRunnerBuilder (com.linkedin.d2.balancer.strategies.framework.LoadBalancerStrategyTestRunnerBuilder)13 D2RelativeStrategyProperties (com.linkedin.d2.D2RelativeStrategyProperties)11 URI (java.net.URI)6 D2QuarantineProperties (com.linkedin.d2.D2QuarantineProperties)4 D2ClusterPartitionConfiguration (com.linkedin.d2.D2ClusterPartitionConfiguration)2 RangedPartitionProperties (com.linkedin.d2.RangedPartitionProperties)2 HashBasedPartitionProperties (com.linkedin.d2.balancer.properties.HashBasedPartitionProperties)2 NullPartitionProperties (com.linkedin.d2.balancer.properties.NullPartitionProperties)2 PartitionProperties (com.linkedin.d2.balancer.properties.PartitionProperties)2 RangeBasedPartitionProperties (com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties)2 ArrayList (java.util.ArrayList)2 DarkClusterStrategyNameArray (com.linkedin.d2.DarkClusterStrategyNameArray)1 HttpStatusCodeRange (com.linkedin.d2.HttpStatusCodeRange)1 HttpStatusCodeRangeArray (com.linkedin.d2.HttpStatusCodeRangeArray)1 LoadBalancerState (com.linkedin.d2.balancer.LoadBalancerState)1 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)1 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)1