Search in sources :

Example 1 with TransportClientFactory

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;
}
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) HashSet(java.util.HashSet)

Example 2 with TransportClientFactory

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();
    }
}
Also used : Client(com.linkedin.r2.transport.common.Client) None(com.linkedin.common.util.None) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) FutureCallback(com.linkedin.common.callback.FutureCallback) AfterClass(org.testng.annotations.AfterClass)

Example 3 with TransportClientFactory

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;
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) TransportDispatcherBuilder(com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) EncodingType(com.linkedin.r2.filter.compression.EncodingType) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) Client(com.linkedin.r2.transport.common.Client) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) ClientCompressionFilter(com.linkedin.r2.filter.compression.ClientCompressionFilter) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) DataProvider(org.testng.annotations.DataProvider)

Example 4 with TransportClientFactory

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;
}
Also used : HashMap(java.util.HashMap) TransportDispatcherBuilder(com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) ByteString(com.linkedin.data.ByteString) StreamFilter(com.linkedin.r2.filter.message.stream.StreamFilter) StreamEncodingType(com.linkedin.r2.filter.compression.streaming.StreamEncodingType) ClientStreamCompressionFilter(com.linkedin.r2.filter.compression.ClientStreamCompressionFilter) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) Client(com.linkedin.r2.transport.common.Client) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) DataProvider(org.testng.annotations.DataProvider)

Example 5 with TransportClientFactory

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();
}
Also used : Client(com.linkedin.r2.transport.common.Client) None(com.linkedin.common.util.None) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) FutureCallback(com.linkedin.common.callback.FutureCallback) AfterClass(org.testng.annotations.AfterClass)

Aggregations

TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)41 HashMap (java.util.HashMap)34 Test (org.testng.annotations.Test)22 URI (java.net.URI)21 RequestContext (com.linkedin.r2.message.RequestContext)18 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)16 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)15 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)15 LoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory)15 FutureCallback (com.linkedin.common.callback.FutureCallback)14 None (com.linkedin.common.util.None)14 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)14 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)13 ArrayList (java.util.ArrayList)13 RandomLoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory)12 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)11 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)11 Map (java.util.Map)11