Search in sources :

Example 21 with TrackerClient

use of com.linkedin.d2.balancer.clients.TrackerClient in project rest.li by linkedin.

the class DegraderLoadBalancerStrategyV2_1 method overrideMinCallCount.

/**
   * Both the drop in hash ring points and the global drop rate influence the minimum call count
   * that we should see to qualify for a state update. Currently, both factors are equally weighed,
   * and multiplied together to come up with a scale factor. With this scheme, if either factor is
   * zero, then the overrideMinCallCount will be set to 1. If both factors are at half weight, then
   * the overall weight will be .5 * .5 = .25 of the original minCallCount.
   *
   * @param newOverrideDropRate
   * @param trackerClientUpdaters
   * @param pointsMap
   * @param pointsPerWeight
   */
public static void overrideMinCallCount(double newOverrideDropRate, List<TrackerClientUpdater> trackerClientUpdaters, Map<URI, Integer> pointsMap, int pointsPerWeight) {
    for (TrackerClientUpdater clientUpdater : trackerClientUpdaters) {
        TrackerClient client = clientUpdater.getTrackerClient();
        int currentOverrideMinCallCount = client.getDegraderControl(DEFAULT_PARTITION_ID).getOverrideMinCallCount();
        double hashFactor = pointsMap.get(client.getUri()) / pointsPerWeight;
        double transmitFactor = 1.0 - newOverrideDropRate;
        int newOverrideMinCallCount = (int) Math.max(Math.round(client.getDegraderControl(DEFAULT_PARTITION_ID).getMinCallCount() * hashFactor * transmitFactor), 1);
        if (newOverrideMinCallCount != currentOverrideMinCallCount) {
            clientUpdater.setOverrideMinCallCount(newOverrideMinCallCount);
            warn(_log, "overriding Min Call Count to ", newOverrideMinCallCount, " for client: ", client.getUri());
        }
    }
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient)

Example 22 with TrackerClient

use of com.linkedin.d2.balancer.clients.TrackerClient in project rest.li by linkedin.

the class DegraderLoadBalancerStrategyV2_1 method updateState.

private void updateState(long clusterGenerationId, List<TrackerClient> trackerClients, DegraderLoadBalancerStrategyConfig config) {
    List<TrackerClientUpdater> clientUpdaters = new ArrayList<TrackerClientUpdater>();
    for (TrackerClient client : trackerClients) {
        clientUpdaters.add(new TrackerClientUpdater(client, DEFAULT_PARTITION_ID));
    }
    // doUpdateState has no side effects on _state or trackerClients.
    // all changes to the trackerClients would be recorded in clientUpdaters
    DegraderLoadBalancerState state = doUpdateState(clusterGenerationId, _state, config, clientUpdaters);
    _state = state;
    // only if state update succeeded, do we actually apply the recorded changes to trackerClients
    for (TrackerClientUpdater clientUpdater : clientUpdaters) {
        clientUpdater.update();
    }
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) ArrayList(java.util.ArrayList)

Example 23 with TrackerClient

use of com.linkedin.d2.balancer.clients.TrackerClient in project rest.li by linkedin.

the class SimpleLoadBalancerStateTest method testSSLDisabledWithHttpsInstances.

@Test(groups = { "small", "back-end" })
public void testSSLDisabledWithHttpsInstances() throws URISyntaxException {
    reset();
    URI uri = URI.create("http://cluster-1/test");
    URI httpsUri = URI.create("https://cluster-1/test");
    List<String> schemes = new ArrayList<String>();
    Map<Integer, PartitionData> partitionData = new HashMap<Integer, PartitionData>(1);
    partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
    Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<URI, Map<Integer, PartitionData>>();
    uriData.put(uri, partitionData);
    uriData.put(httpsUri, partitionData);
    schemes.add("https");
    schemes.add("http");
    assertNull(_state.getClient("service-1", uri));
    // set up state
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    assertNull(_state.getClient("service-1", uri));
    _state.listenToService("service-1", new NullStateListenerCallback());
    assertNull(_state.getClient("service-1", uri));
    Map<String, Object> transportClientProperties = new HashMap<String, Object>();
    transportClientProperties.put(HttpClientFactory.HTTP_SSL_CONTEXT, _sslContext);
    transportClientProperties.put(HttpClientFactory.HTTP_SSL_PARAMS, _sslParameters);
    transportClientProperties = Collections.unmodifiableMap(transportClientProperties);
    ServiceProperties serviceProperties = new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"), Collections.<String, Object>emptyMap(), transportClientProperties, null, schemes, null);
    _serviceRegistry.put("service-1", serviceProperties);
    assertNull(_state.getClient("service-1", uri));
    _uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
    List<LoadBalancerState.SchemeStrategyPair> schemeStrategyPairs = _state.getStrategiesForService("service-1", schemes);
    for (LoadBalancerState.SchemeStrategyPair pair : schemeStrategyPairs) {
        assertFalse("https".equalsIgnoreCase(pair.getScheme()), "https shouldn't be in any schemeStrategyPair");
    }
    TrackerClient client = _state.getClient("service-1", uri);
    assertNotNull(client);
    assertEquals(client.getUri(), uri);
    // Unfortunately, I don't know a good way to intercept the logs
    client = _state.getClient("service-1", httpsUri);
    assertNull(client, "shouldn't pick an https uri");
    _state.refreshTransportClientsPerService(serviceProperties);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(java.net.URI) LoadBalancerState(com.linkedin.d2.balancer.LoadBalancerState) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 24 with TrackerClient

use of com.linkedin.d2.balancer.clients.TrackerClient in project rest.li by linkedin.

the class SimpleLoadBalancerStateTest method testShutdown.

@Test(groups = { "small", "back-end" })
public void testShutdown() throws URISyntaxException, InterruptedException {
    reset();
    URI uri = URI.create("http://cluster-1/test");
    TestListener listener = new TestListener();
    List<String> schemes = new ArrayList<String>();
    Map<Integer, PartitionData> partitionData = new HashMap<Integer, PartitionData>(1);
    partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
    Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<URI, Map<Integer, PartitionData>>();
    uriData.put(uri, partitionData);
    schemes.add("http");
    _state.register(listener);
    assertNull(listener.scheme);
    assertNull(listener.strategy);
    assertNull(listener.serviceName);
    // set up state
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    _state.listenToService("service-1", new NullStateListenerCallback());
    _clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1", schemes));
    _uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random")));
    TrackerClient client = _state.getClient("cluster-1", uri);
    TestShutdownCallback callback = new TestShutdownCallback();
    _state.shutdown(callback);
    if (!callback.await(10, TimeUnit.SECONDS)) {
        fail("unable to shut down state");
    }
    for (TransportClientFactory factory : _clientFactories.values()) {
        SimpleLoadBalancerTest.DoNothingClientFactory f = (SimpleLoadBalancerTest.DoNothingClientFactory) factory;
        assertEquals(f.getRunningClientCount(), 0, "Not all clients were shut down");
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(java.net.URI) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Map(java.util.Map) HashMap(java.util.HashMap) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 25 with TrackerClient

use of com.linkedin.d2.balancer.clients.TrackerClient in project rest.li by linkedin.

the class SimpleLoadBalancerStateTest method testShutdownWithListener.

@Test(groups = { "small", "back-end" })
public void testShutdownWithListener() throws URISyntaxException, InterruptedException {
    reset();
    URI uri = URI.create("http://cluster-1/test");
    TestListener listener = new TestListener();
    List<String> schemes = new ArrayList<String>();
    Map<Integer, PartitionData> partitionData = new HashMap<Integer, PartitionData>(1);
    partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
    Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<URI, Map<Integer, PartitionData>>();
    uriData.put(uri, partitionData);
    schemes.add("http");
    _state.register(listener);
    assertNull(listener.scheme);
    assertNull(listener.strategy);
    assertNull(listener.serviceName);
    // set up state
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    _state.listenToService("service-1", new NullStateListenerCallback());
    List<String> strategyList = Arrays.asList("degraderV3");
    _state.refreshServiceStrategies(new ServiceProperties("service-1", "cluster-1", "/test", strategyList, Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap(), Collections.<String, String>emptyMap(), schemes, Collections.<URI>emptySet()));
    assertEquals(listener.scheme, "http");
    assertNotNull(listener.strategy);
    assertEquals(listener.serviceName, "service-1");
    TrackerClient client = _state.getClient("cluster-1", uri);
    TestShutdownCallback callback = new TestShutdownCallback();
    _state.shutdown(callback);
    if (!callback.await(10, TimeUnit.SECONDS)) {
        fail("unable to shut down state");
    }
    for (TransportClientFactory factory : _clientFactories.values()) {
        SimpleLoadBalancerTest.DoNothingClientFactory f = (SimpleLoadBalancerTest.DoNothingClientFactory) factory;
        assertEquals(f.getRunningClientCount(), 0, "Not all clients were shut down");
    }
    assertNull(listener.scheme);
    assertNull(listener.strategy);
    assertNull(listener.serviceName);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(java.net.URI) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) Map(java.util.Map) HashMap(java.util.HashMap) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Aggregations

TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)94 URI (java.net.URI)57 Test (org.testng.annotations.Test)53 ArrayList (java.util.ArrayList)52 HashMap (java.util.HashMap)51 TrackerClientTest (com.linkedin.d2.balancer.clients.TrackerClientTest)39 RequestContext (com.linkedin.r2.message.RequestContext)33 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)23 DegraderImpl (com.linkedin.util.degrader.DegraderImpl)17 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)16 AtomicLong (java.util.concurrent.atomic.AtomicLong)16 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)15 DegraderControl (com.linkedin.util.degrader.DegraderControl)13 Map (java.util.Map)13 NullStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback)11 DegraderLoadBalancerTest (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)11 CallCompletion (com.linkedin.util.degrader.CallCompletion)11 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)9 URIRequest (com.linkedin.d2.balancer.util.URIRequest)9 HashSet (java.util.HashSet)8