Search in sources :

Example 41 with TransportClient

use of com.linkedin.r2.transport.common.bridge.client.TransportClient in project rest.li by linkedin.

the class SimpleLoadBalancerState method refreshClients.

/**
 * Creates new {@link TrackerClient} and {@link TransportClient} for service and shut down any old ones.
 *
 * @param serviceProperties
 */
void refreshClients(ServiceProperties serviceProperties) {
    String serviceName = serviceProperties.getServiceName();
    Map<String, TransportClient> newTransportClients = createTransportClients(serviceProperties);
    // clients-by-scheme map is never edited, only replaced.
    newTransportClients = Collections.unmodifiableMap(newTransportClients);
    Map<String, TransportClient> oldTransportClients = _serviceClients.put(serviceName, newTransportClients);
    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();
        newTrackerClients = new ConcurrentHashMap<>(CollectionUtils.getMapInitialCapacity(uris.size(), 0.75f), 0.75f, 1);
        for (URI uri : uris) {
            TrackerClient trackerClient = buildTrackerClient(uri, uriProperties, serviceName, serviceProperties);
            if (trackerClient != null) {
                newTrackerClients.put(uri, trackerClient);
            }
        }
    } else {
        newTrackerClients = new ConcurrentHashMap<>();
    }
    _trackerClients.put(serviceName, newTrackerClients);
    shutdownTransportClients(oldTransportClients, serviceName);
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) URI(java.net.URI)

Example 42 with TransportClient

use of com.linkedin.r2.transport.common.bridge.client.TransportClient in project rest.li by linkedin.

the class SimpleLoadBalancerState method getTransportClient.

/**
 * Gets a {@link TransportClient} for a service and URI.
 */
@Nullable
private TransportClient getTransportClient(String serviceName, URI uri) {
    Map<String, TransportClient> clientsByScheme = _serviceClients.get(serviceName);
    if (clientsByScheme == null || uri == null || uri.getScheme() == null) {
        warn(_log, "Issue building client for service ", serviceName, " and uri ", uri);
        return null;
    }
    TransportClient client = clientsByScheme.get(uri.getScheme().toLowerCase());
    if (client == null) {
        debug(_log, "No TransportClient for scheme ", uri.getScheme(), " service ", serviceName, "URI ", uri);
        return null;
    }
    return client;
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) Nullable(javax.annotation.Nullable)

Example 43 with TransportClient

use of com.linkedin.r2.transport.common.bridge.client.TransportClient in project rest.li by linkedin.

the class SimpleLoadBalancerState method createTransportClients.

private Map<String, TransportClient> createTransportClients(ServiceProperties serviceProperties) {
    Map<String, Object> transportClientProperties = new HashMap<>(serviceProperties.getTransportClientProperties());
    List<String> schemes = serviceProperties.getPrioritizedSchemes();
    Map<String, TransportClient> newTransportClients = new HashMap<>();
    if (schemes == null || schemes.isEmpty()) {
        warn(_log, "Prioritized schemes is null for service properties = ", serviceProperties.getServiceName());
        return newTransportClients;
    }
    for (String scheme : schemes) {
        TransportClientFactory factory = _clientFactories.get(scheme);
        if ("https".equals(scheme)) {
            if (_isSSLEnabled) {
                if (_sslContext != null && _sslParameters != null) {
                    transportClientProperties.put(HttpClientFactory.HTTP_SSL_CONTEXT, _sslContext);
                    transportClientProperties.put(HttpClientFactory.HTTP_SSL_PARAMS, _sslParameters);
                } else {
                    error(_log, "https specified as a prioritized scheme for service: ", serviceProperties.getServiceName(), " but no SSLContext or SSLParameters have been configured.");
                    if (schemes.size() == 1) {
                        // throw exception when https is the only scheme specified
                        throw new IllegalStateException("SSL enabled but required SSLContext and SSLParameters" + "were not both present.");
                    }
                    continue;
                }
            } else {
                continue;
            }
        }
        if (factory == null) {
            warn(_log, "Failed to find client factory for scheme ", scheme);
            continue;
        }
        final String clusterName = serviceProperties.getClusterName();
        transportClientProperties.put(HttpClientFactory.HTTP_SERVICE_NAME, serviceProperties.getServiceName());
        transportClientProperties.put(HttpClientFactory.HTTP_POOL_STATS_NAME_PREFIX, clusterName);
        TransportClient client = _sslSessionValidatorFactory == null ? factory.getClient(transportClientProperties) : new ClusterAwareTransportClient(clusterName, factory.getClient(transportClientProperties), _clusterInfo, _sslSessionValidatorFactory);
        newTransportClients.put(scheme.toLowerCase(), client);
    }
    return newTransportClients;
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory)

Example 44 with TransportClient

use of com.linkedin.r2.transport.common.bridge.client.TransportClient in project rest.li by linkedin.

the class SimpleLoadBalancerState method buildTrackerClient.

@Nullable
private TrackerClient buildTrackerClient(URI uri, UriProperties uriProperties, String serviceName, ServiceProperties serviceProperties) {
    TransportClient transportClient = getTransportClient(serviceName, uri);
    LoadBalancerStrategy loadBalancerStrategy = _serviceStrategies.get(serviceName).get(uri.getScheme().toLowerCase());
    if (transportClient == null) {
        return null;
    }
    if (loadBalancerStrategy == null) {
        return null;
    }
    return serviceProperties == null ? null : TrackerClientFactory.createTrackerClient(uri, uriProperties, serviceProperties, loadBalancerStrategy.getName(), transportClient);
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) RelativeLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) Nullable(javax.annotation.Nullable)

Example 45 with TransportClient

use of com.linkedin.r2.transport.common.bridge.client.TransportClient in project rest.li by linkedin.

the class SimpleLoadBalancerState method shutdownClients.

void shutdownClients(String serviceName) {
    _log.warn("shutting down all tracker clients and transport clients for service " + serviceName);
    Map<URI, TrackerClient> clients = _trackerClients.remove(serviceName);
    if (clients != null) {
        for (TrackerClient client : clients.values()) {
            for (SimpleLoadBalancerStateListener listener : _listeners) {
                listener.onClientRemoved(serviceName, client);
            }
        }
    }
    // we also need to shutdown the transport client owned by this service
    Map<String, TransportClient> schemeToTransportClients = _serviceClients.get(serviceName);
    shutdownTransportClients(schemeToTransportClients, serviceName);
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) URI(java.net.URI)

Aggregations

TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)46 HashMap (java.util.HashMap)28 RequestContext (com.linkedin.r2.message.RequestContext)22 URI (java.net.URI)21 Test (org.testng.annotations.Test)17 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)15 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)13 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)10 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)10 Map (java.util.Map)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 Callback (com.linkedin.common.callback.Callback)9 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)8 RestRequest (com.linkedin.r2.message.rest.RestRequest)8 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)7 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 None (com.linkedin.common.util.None)6 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)6 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)6