Search in sources :

Example 16 with TrackerClient

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

the class DegraderLoadBalancerStrategyV3 method getTrackerClient.

@Override
public TrackerClient getTrackerClient(Request request, RequestContext requestContext, long clusterGenerationId, int partitionId, List<TrackerClient> trackerClients) {
    debug(_log, "getTrackerClient with generation id ", clusterGenerationId, " partition id: ", partitionId, " on tracker clients: ", trackerClients);
    if (trackerClients == null || trackerClients.size() == 0) {
        warn(_log, "getTrackerClient called with null/empty trackerClients, so returning null");
        return null;
    }
    // only one thread will be allowed to enter updatePartitionState for any partition
    checkUpdatePartitionState(clusterGenerationId, partitionId, trackerClients);
    Ring<URI> ring = _state.getRing(partitionId);
    URI targetHostUri = KeyMapper.TargetHostHints.getRequestContextTargetHost(requestContext);
    Set<URI> excludedUris = ExcludedHostHints.getRequestContextExcludedHosts(requestContext);
    if (excludedUris == null) {
        excludedUris = new HashSet<>();
    }
    //no valid target host header was found in the request
    TrackerClient client;
    if (targetHostUri == null) {
        client = findValidClientFromRing(request, ring, trackerClients, excludedUris, requestContext);
    } else {
        debug(_log, "Degrader honoring target host header in request, skipping hashing.  URI: ", targetHostUri);
        client = searchClientFromUri(targetHostUri, trackerClients);
        if (client == null) {
            warn(_log, "No client found for ", targetHostUri, ". Target host specified is no longer part of cluster");
        }
    }
    boolean dropCall = client == null;
    if (!dropCall) {
        dropCall = client.getDegrader(partitionId).checkDrop();
        if (dropCall) {
            warn(_log, "client's degrader is dropping call for: ", client);
        } else {
            debug(_log, "returning client: ", client);
        }
    }
    return (!dropCall) ? client : null;
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) URI(java.net.URI)

Example 17 with TrackerClient

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

the class TrackerClientTest method testClientRestRequest.

@Test(groups = { "small", "back-end" })
public void testClientRestRequest() throws URISyntaxException {
    URI uri = URI.create("http://test.qa.com:1234/foo");
    double weight = 3d;
    TestClient wrappedClient = new TestClient();
    Clock clock = new SettableClock();
    Map<Integer, PartitionData> partitionDataMap = new HashMap<Integer, PartitionData>(2);
    partitionDataMap.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(3d));
    TrackerClient client = new TrackerClient(uri, partitionDataMap, wrappedClient, clock, null);
    assertEquals(client.getUri(), uri);
    Double clientWeight = client.getPartitionWeight(DefaultPartitionAccessor.DEFAULT_PARTITION_ID);
    assertEquals(clientWeight, weight);
    assertEquals(client.getWrappedClient(), wrappedClient);
    RestRequest restRequest = new RestRequestBuilder(uri).build();
    Map<String, String> restWireAttrs = new HashMap<String, String>();
    TestTransportCallback<RestResponse> restCallback = new TestTransportCallback<RestResponse>();
    client.restRequest(restRequest, new RequestContext(), restWireAttrs, restCallback);
    assertFalse(restCallback.response.hasError());
    assertEquals(wrappedClient.restRequest, restRequest);
    assertEquals(wrappedClient.restWireAttrs, restWireAttrs);
}
Also used : HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) Clock(com.linkedin.util.clock.Clock) SettableClock(com.linkedin.util.clock.SettableClock) URI(java.net.URI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RestRequest(com.linkedin.r2.message.rest.RestRequest) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) SettableClock(com.linkedin.util.clock.SettableClock) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 18 with TrackerClient

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

the class TrackerClientTest method createTrackerClient.

private TrackerClient createTrackerClient(TransportClient tc, Clock clock, URI uri) {
    double weight = 3d;
    Map<Integer, PartitionData> partitionDataMap = new HashMap<Integer, PartitionData>(2);
    partitionDataMap.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(3d));
    DegraderImpl.Config config = new DegraderImpl.Config();
    config.setHighErrorRate(0.1);
    config.setLowErrorRate(0.0);
    config.setMinCallCount(1);
    return new TrackerClient(uri, partitionDataMap, tc, clock, config);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) DegraderImpl(com.linkedin.util.degrader.DegraderImpl)

Example 19 with TrackerClient

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

the class DegraderLoadBalancerStrategyV2_1 method getUnhealthyTrackerClients.

private static List<String> getUnhealthyTrackerClients(List<TrackerClientUpdater> trackerClientUpdaters, Map<URI, Integer> pointsMap, DegraderLoadBalancerStrategyConfig config) {
    List<String> unhealthyClients = new ArrayList<String>();
    for (TrackerClientUpdater clientUpdater : trackerClientUpdaters) {
        TrackerClient client = clientUpdater.getTrackerClient();
        int perfectHealth = (int) (client.getPartitionWeight(DEFAULT_PARTITION_ID) * config.getPointsPerWeight());
        Integer point = pointsMap.get(client.getUri());
        if (point < perfectHealth) {
            unhealthyClients.add(client.getUri() + ":" + point + "/" + perfectHealth);
        }
    }
    return unhealthyClients;
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) ArrayList(java.util.ArrayList)

Example 20 with TrackerClient

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

the class DegraderLoadBalancerStrategyV2_1 method getTrackerClient.

@Override
public TrackerClient getTrackerClient(Request request, RequestContext requestContext, long clusterGenerationId, int partitionId, List<TrackerClient> trackerClients) {
    if (partitionId != DEFAULT_PARTITION_ID) {
        throw new UnsupportedOperationException("Trying to access partition: " + partitionId + "on an unpartitioned cluster");
    }
    debug(_log, "getTrackerClient with generation id ", clusterGenerationId, " on tracker clients: ", clusterGenerationId);
    if (trackerClients == null || trackerClients.size() == 0) {
        warn(_log, "getTrackerClient called with null/empty trackerClients, so returning null");
        return null;
    }
    // only one thread will be allowed to enter updateState.
    checkUpdateState(clusterGenerationId, trackerClients);
    URI targetHostUri = KeyMapper.TargetHostHints.getRequestContextTargetHost(requestContext);
    Set<URI> excludedUris = ExcludedHostHints.getRequestContextExcludedHosts(requestContext);
    URI hostHeaderUri = targetHostUri;
    //no valid target host header was found in the request
    if (targetHostUri == null) {
        // Compute the hash code
        int hashCode = _hashFunction.hash(request);
        // we operate only on URIs to ensure that we never hold on to an old tracker client
        // that the cluster manager has removed
        Ring<URI> ring = _state.getRing();
        Iterator<URI> iterator = ring.getIterator(hashCode);
        while (iterator.hasNext() && targetHostUri == null) {
            URI uri = iterator.next();
            if (excludedUris == null || !excludedUris.contains(uri)) {
                targetHostUri = uri;
            }
        }
        ExcludedHostHints.addRequestContextExcludedHost(requestContext, targetHostUri);
    } else {
        debug(_log, "Degrader honoring target host header in request, skipping hashing.  URI: " + targetHostUri.toString());
    }
    TrackerClient client = null;
    if (targetHostUri != null) {
        // consistent hash ring! Therefore, this linear scan is the best we can do.
        for (TrackerClient trackerClient : trackerClients) {
            if (trackerClient.getUri().equals(targetHostUri)) {
                client = trackerClient;
                break;
            }
        }
        if (client == null) {
            warn(_log, "No client found for " + targetHostUri + (hostHeaderUri == null ? ", degrader load balancer state is inconsistent with cluster manager" : ", target host specified is no longer part of cluster"));
        }
    } else {
        warn(_log, "unable to find a URI to use");
    }
    boolean dropCall = client == null;
    if (!dropCall) {
        dropCall = client.getDegrader(DEFAULT_PARTITION_ID).checkDrop();
        if (dropCall) {
            warn(_log, "client's degrader is dropping call for: ", client);
        } else {
            debug(_log, "returning client: ", client);
        }
    }
    return (!dropCall) ? client : null;
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) URI(java.net.URI)

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