Search in sources :

Example 1 with TrackerClient

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

the class SimpleLoadBalancer method getClient.

/**
   * Given a Request, returns a TransportClient that can handle requests for the Request.
   *
   *
   * @param request
   *          A request whose URI is a URL of the format "d2://>servicename</optional/path".
   * @param requestContext context for this request
   * @return A client that can be called to retrieve data for the URN.
   * @throws ServiceUnavailableException
   *           If the load balancer can't figure out how to reach a service for the given
   *           URN, an ServiceUnavailableException will be thrown.
   */
@Override
public TransportClient getClient(Request request, RequestContext requestContext) throws ServiceUnavailableException {
    TransportClient client;
    URI uri = request.getURI();
    debug(_log, "get client for uri: ", uri);
    ServiceProperties service = listenToServiceAndCluster(uri);
    String serviceName = service.getServiceName();
    String clusterName = service.getClusterName();
    ClusterProperties cluster = getClusterProperties(serviceName, clusterName);
    // Check if we want to override the service URL and bypass choosing among the existing
    // tracker clients. This is useful when the service we want is not announcing itself to
    // the cluster, ie a private service for a set of clients.
    URI targetService = LoadBalancerUtil.TargetHints.getRequestContextTargetService(requestContext);
    if (targetService == null) {
        LoadBalancerStateItem<UriProperties> uriItem = getUriItem(serviceName, clusterName, cluster);
        UriProperties uris = uriItem.getProperty();
        List<LoadBalancerState.SchemeStrategyPair> orderedStrategies = _state.getStrategiesForService(serviceName, service.getPrioritizedSchemes());
        TrackerClient trackerClient = chooseTrackerClient(request, requestContext, serviceName, clusterName, cluster, uriItem, uris, orderedStrategies, service);
        String clusterAndServiceUriString = trackerClient.getUri() + service.getPath();
        client = new RewriteClient(serviceName, URI.create(clusterAndServiceUriString), trackerClient);
        _serviceAvailableStats.inc();
    } else {
        _log.debug("service hint found, using generic client for target: {}", targetService);
        TransportClient transportClient = _state.getClient(serviceName, targetService.getScheme());
        client = new RewriteClient(serviceName, targetService, transportClient);
    }
    return client;
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) URI(java.net.URI) RewriteClient(com.linkedin.d2.balancer.clients.RewriteClient)

Example 2 with TrackerClient

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

the class SimpleLoadBalancerState method refreshTransportClientsPerService.

void refreshTransportClientsPerService(ServiceProperties serviceProperties) {
    String serviceName = serviceProperties.getServiceName();
    //create new TransportClients
    Map<String, TransportClient> newTransportClients = createAndInsertTransportClientTo(serviceProperties);
    // clients-by-scheme map is never edited, only replaced.
    newTransportClients = Collections.unmodifiableMap(newTransportClients);
    final Map<String, TransportClient> oldTransportClients = _serviceClients.put(serviceName, newTransportClients);
    // gets the information for configuring the parameter for how DegraderImpl should behave for
    // each tracker clients that we instantiate here. If there's no such information, then we'll instantiate
    // each tracker clients with default configuration
    DegraderImpl.Config config = null;
    if (serviceProperties.getDegraderProperties() != null && !serviceProperties.getDegraderProperties().isEmpty()) {
        config = DegraderConfigFactory.toDegraderConfig(serviceProperties.getDegraderProperties());
    } else {
        debug(_log, "trying to see if there's a special degraderImpl properties but serviceInfo.getDegraderImpl() is null" + " for service name = " + serviceName + " so we'll set config to default");
    }
    Clock clk = SystemClock.instance();
    if (serviceProperties.getLoadBalancerStrategyProperties() != null) {
        Map<String, Object> loadBalancerStrategyProperties = serviceProperties.getLoadBalancerStrategyProperties();
        clk = MapUtil.getWithDefault(loadBalancerStrategyProperties, PropertyKeys.CLOCK, SystemClock.instance(), Clock.class);
    }
    Map<URI, TrackerClient> newTrackerClients;
    // update all tracker clients to use new configs
    LoadBalancerStateItem<UriProperties> uriItem = _uriProperties.get(serviceProperties.getClusterName());
    UriProperties uriProperties = uriItem == null ? null : uriItem.getProperty();
    if (uriProperties != null) {
        Set<URI> uris = uriProperties.Uris();
        // clients-by-uri map may be edited later by UriPropertiesListener.handlePut
        newTrackerClients = new ConcurrentHashMap<URI, TrackerClient>(CollectionUtils.getMapInitialCapacity(uris.size(), 0.75f), 0.75f, 1);
        long trackerClientInterval = getTrackerClientInterval(serviceProperties);
        String errorStatusPattern = getErrorStatusPattern(serviceProperties);
        for (URI uri : uris) {
            TrackerClient trackerClient = getTrackerClient(serviceName, uri, uriProperties.getPartitionDataMap(uri), config, clk, trackerClientInterval, errorStatusPattern);
            if (trackerClient != null) {
                newTrackerClients.put(uri, trackerClient);
            }
        }
    } else {
        // clients-by-uri map may be edited later by UriPropertiesListener.handlePut
        newTrackerClients = new ConcurrentHashMap<URI, TrackerClient>(16, 0.75f, 1);
    }
    //override the oldTrackerClients with newTrackerClients
    _trackerClients.put(serviceName, newTrackerClients);
    // No need to shut down oldTrackerClients, because they all point directly to the TransportClient for the service
    // We do need to shut down the old transport clients
    shutdownTransportClients(oldTransportClients, serviceName);
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) DegraderImpl(com.linkedin.util.degrader.DegraderImpl) SystemClock(com.linkedin.util.clock.SystemClock) Clock(com.linkedin.util.clock.Clock) URI(java.net.URI) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) UriProperties(com.linkedin.d2.balancer.properties.UriProperties)

Example 3 with TrackerClient

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

the class DegraderLoadBalancerStrategyV2 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, so if multiple threads call
    // getTrackerClient while the _state is not populated, they won't be able return a
    // tracker client from the hash ring, and will return null.
    checkUpdateState(clusterGenerationId, trackerClients);
    boolean hasInitializationError = _state.hasError();
    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 (!hasInitializationError && 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 if (hasInitializationError && targetHostUri == null) {
        //if we encounter an error while initializing the state, we'll choose a tracker client at random
        targetHostUri = trackerClients.get(new Random().nextInt(trackerClients.size())).getUri();
    } 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 : Random(java.util.Random) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) URI(java.net.URI)

Example 4 with TrackerClient

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

the class DegraderLoadBalancerStrategyV2 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 trackerClients
   * @param pointsMap
   * @param pointsPerWeight
   */
public static void overrideMinCallCount(double newOverrideDropRate, List<TrackerClient> trackerClients, Map<URI, Integer> pointsMap, int pointsPerWeight) {
    for (TrackerClient client : trackerClients) {
        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) {
            client.getDegraderControl(DEFAULT_PARTITION_ID).setOverrideMinCallCount(newOverrideMinCallCount);
            warn(_log, "overriding Min Call Count to ", newOverrideMinCallCount, " for client: ", client.getUri());
        }
    }
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient)

Example 5 with TrackerClient

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

the class DegraderLoadBalancerStrategyV2 method getUnhealthyTrackerClients.

private static List<String> getUnhealthyTrackerClients(List<TrackerClient> trackerClients, Map<URI, Integer> pointsMap, DegraderLoadBalancerStrategyConfig config) {
    List<String> unhealthyClients = new ArrayList<String>();
    for (TrackerClient client : trackerClients) {
        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)

Aggregations

TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)117 URI (java.net.URI)83 Test (org.testng.annotations.Test)58 HashMap (java.util.HashMap)50 ArrayList (java.util.ArrayList)49 RequestContext (com.linkedin.r2.message.RequestContext)39 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)30 DegraderTrackerClient (com.linkedin.d2.balancer.clients.DegraderTrackerClient)26 Map (java.util.Map)25 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)22 DegraderTrackerClientImpl (com.linkedin.d2.balancer.clients.DegraderTrackerClientImpl)18 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)18 D2RelativeStrategyProperties (com.linkedin.d2.D2RelativeStrategyProperties)17 DegraderTrackerClientTest (com.linkedin.d2.balancer.clients.DegraderTrackerClientTest)17 NullStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback)15 DegraderLoadBalancerTest (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)14 URIRequest (com.linkedin.d2.balancer.util.URIRequest)14 DegraderControl (com.linkedin.util.degrader.DegraderControl)14 HashSet (java.util.HashSet)13 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)12