use of com.linkedin.r2.transport.common.TransportClientFactory in project rest.li by linkedin.
the class SimpleLoadBalancerState method createAndInsertTransportClientTo.
private Map<String, TransportClient> createAndInsertTransportClientTo(ServiceProperties serviceProperties) {
Map<String, Object> transportClientProperties = new HashMap<String, Object>(serviceProperties.getTransportClientProperties());
Object allowedClientOverrideKeysObj = transportClientProperties.remove(PropertyKeys.ALLOWED_CLIENT_OVERRIDE_KEYS);
Set<String> allowedClientOverrideKeys = new HashSet<String>(ConfigValueExtractor.buildList(allowedClientOverrideKeysObj, LIST_SEPARATOR));
Map<String, Object> clientSuppliedServiceProperties = _clientServicesConfig.get(serviceProperties.getServiceName());
if (clientSuppliedServiceProperties != null) {
debug(_log, "Client supplied configs for service {}", new Object[] { serviceProperties.getServiceName() });
// check for overrides
for (String clientSuppliedKey : clientSuppliedServiceProperties.keySet()) {
// clients can only override config properties which have been allowed by the service
if (allowedClientOverrideKeys.contains(clientSuppliedKey)) {
if (ClientServiceConfigValidator.isValidValue(transportClientProperties, clientSuppliedServiceProperties, clientSuppliedKey)) {
transportClientProperties.put(clientSuppliedKey, clientSuppliedServiceProperties.get(clientSuppliedKey));
info(_log, "Client overrode config property {} for service {}. This is being used to instantiate the Transport Client", new Object[] { clientSuppliedKey, serviceProperties.getServiceName() });
} else {
warn(_log, "Client supplied config property {} with an invalid value {} for service {}", new Object[] { clientSuppliedKey, clientSuppliedServiceProperties.get(clientSuppliedKey), serviceProperties.getServiceName() });
}
}
}
}
List<String> schemes = serviceProperties.getPrioritizedSchemes();
Map<String, TransportClient> newTransportClients = new HashMap<String, TransportClient>();
if (schemes != null && !schemes.isEmpty()) {
for (String scheme : schemes) {
TransportClientFactory factory = _clientFactories.get(scheme);
if ("https".equals(scheme)) {
if (_isSSLEnabled) {
// should have been passed in during creation.
if (_sslContext != null && _sslParameters != null) {
transportClientProperties.put(HttpClientFactory.HTTP_SSL_CONTEXT, _sslContext);
transportClientProperties.put(HttpClientFactory.HTTP_SSL_PARAMS, _sslParameters);
} else {
_log.error("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.");
}
// Do not create the transport client for https.
continue;
}
} else {
// is requested later on, getTrackerClient will catch this situation and log an error.
continue;
}
}
if (factory != null) {
transportClientProperties.put(HttpClientFactory.HTTP_SERVICE_NAME, serviceProperties.getServiceName());
TransportClient client = factory.getClient(transportClientProperties);
newTransportClients.put(scheme.toLowerCase(), client);
} else {
_log.warn("Failed to find client factory for scheme {}", scheme);
}
}
} else {
_log.warn("Prioritized schemes is null for service properties = " + serviceProperties.getServiceName());
}
return newTransportClients;
}
use of com.linkedin.r2.transport.common.TransportClientFactory in project rest.li by linkedin.
the class TestRestCompressionEcho method tearDown.
@AfterClass
public void tearDown() throws Exception {
for (Client client : _clients) {
final FutureCallback<None> clientShutdownCallback = new FutureCallback<None>();
client.shutdown(clientShutdownCallback);
clientShutdownCallback.get();
}
for (TransportClientFactory factory : _clientFactories) {
final FutureCallback<None> factoryShutdownCallback = new FutureCallback<None>();
factory.shutdown(factoryShutdownCallback);
factoryShutdownCallback.get();
}
if (_server != null) {
_server.stop();
_server.waitForStop();
}
}
use of com.linkedin.r2.transport.common.TransportClientFactory in project rest.li by linkedin.
the class TestRestCompressionEcho method compressionEchoData.
@DataProvider
public Object[][] compressionEchoData() {
EncodingType[] encodings = new EncodingType[] { EncodingType.GZIP, EncodingType.SNAPPY, EncodingType.IDENTITY };
Object[][] args = new Object[4 * encodings.length * encodings.length][2];
int cur = 0;
for (EncodingType requestEncoding : encodings) {
for (EncodingType acceptEncoding : encodings) {
RestFilter clientCompressionFilter = new ClientCompressionFilter(requestEncoding, new CompressionConfig(THRESHOLD), new EncodingType[] { acceptEncoding }, new CompressionConfig(THRESHOLD), Arrays.asList(new String[] { "*" }));
TransportClientFactory factory = new HttpClientFactory.Builder().setFilterChain(FilterChains.createRestChain(clientCompressionFilter)).build();
Client http1Client = new TransportClientAdapter(factory.getClient(getHttp1ClientProperties()), REST_OVER_STREAM);
Client http2Client = new TransportClientAdapter(factory.getClient(getHttp2ClientProperties()), REST_OVER_STREAM);
args[cur][0] = http1Client;
args[cur][1] = LARGE_BYTES_NUM;
args[cur + 1][0] = http2Client;
args[cur + 1][1] = LARGE_BYTES_NUM;
cur += 2;
_clientFactories.add(factory);
_clients.add(http1Client);
_clients.add(http2Client);
}
}
// test data that won't trigger compression
for (EncodingType requestEncoding : encodings) {
for (EncodingType acceptEncoding : encodings) {
RestFilter clientCompressionFilter = new ClientCompressionFilter(requestEncoding, new CompressionConfig(THRESHOLD), new EncodingType[] { acceptEncoding }, new CompressionConfig(THRESHOLD), Arrays.asList(new String[] { "*" }));
TransportClientFactory factory = new HttpClientFactory.Builder().setFilterChain(FilterChains.createRestChain(clientCompressionFilter)).build();
Client http1Client = new TransportClientAdapter(factory.getClient(getHttp1ClientProperties()), REST_OVER_STREAM);
Client http2Client = new TransportClientAdapter(factory.getClient(getHttp2ClientProperties()), REST_OVER_STREAM);
args[cur][0] = http1Client;
args[cur][1] = SMALL_BYTES_NUM;
args[cur + 1][0] = http2Client;
args[cur + 1][1] = SMALL_BYTES_NUM;
cur += 2;
_clientFactories.add(factory);
_clients.add(http1Client);
_clients.add(http2Client);
}
}
return args;
}
use of com.linkedin.r2.transport.common.TransportClientFactory in project rest.li by linkedin.
the class TestRequestCompression method requestCompressionData.
@DataProvider
public Object[][] requestCompressionData() {
StreamEncodingType[] encodings = new StreamEncodingType[] { StreamEncodingType.GZIP, StreamEncodingType.DEFLATE, StreamEncodingType.SNAPPY_FRAMED, StreamEncodingType.BZIP2 };
String[] protocols = new String[] { HttpProtocolVersion.HTTP_1_1.name(), HttpProtocolVersion.HTTP_2.name() };
Object[][] args = new Object[encodings.length * protocols.length][2];
int cur = 0;
for (StreamEncodingType requestEncoding : encodings) {
for (String protocol : protocols) {
StreamFilter clientCompressionFilter = new ClientStreamCompressionFilter(requestEncoding, new CompressionConfig(THRESHOLD), null, new CompressionConfig(THRESHOLD), Arrays.asList(new String[] { "*" }), _executor);
TransportClientFactory factory = new HttpClientFactory.Builder().setFilterChain(FilterChains.createStreamChain(clientCompressionFilter)).build();
HashMap<String, String> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocol);
Client client = new TransportClientAdapter(factory.getClient(properties), true);
args[cur][0] = client;
args[cur][1] = URI.create("/" + requestEncoding.getHttpName());
cur++;
_clientFactories.add(factory);
_clients.add(client);
}
}
return args;
}
use of com.linkedin.r2.transport.common.TransportClientFactory in project rest.li by linkedin.
the class TestCompressionEcho method tearDown.
@AfterClass
public void tearDown() throws Exception {
for (Client client : _clients) {
final FutureCallback<None> clientShutdownCallback = new FutureCallback<None>();
client.shutdown(clientShutdownCallback);
clientShutdownCallback.get();
}
for (TransportClientFactory factory : _clientFactories) {
final FutureCallback<None> factoryShutdownCallback = new FutureCallback<None>();
factory.shutdown(factoryShutdownCallback);
factoryShutdownCallback.get();
}
if (_server != null) {
_server.stop();
_server.waitForStop();
}
_executor.shutdown();
}
Aggregations