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);
}
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;
}
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;
}
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);
}
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);
}
Aggregations