Search in sources :

Example 16 with TransportClient

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

the class HttpClientFactory method getRawClient.

TransportClient getRawClient(Map<String, ? extends Object> properties, SSLContext sslContext, SSLParameters sslParameters) {
    // key which identifies and contains the set of transport properties to create a channel pool manager
    ChannelPoolManagerKey key = createChannelPoolManagerKey(properties, null, null);
    ChannelPoolManagerKey sslKey = createChannelPoolManagerKey(properties, sslContext, sslParameters);
    // Raw Client properties
    int shutdownTimeout = chooseNewOverDefault(getIntValue(properties, HTTP_SHUTDOWN_TIMEOUT), DEFAULT_SHUTDOWN_TIMEOUT);
    int requestTimeout = chooseNewOverDefault(getIntValue(properties, HTTP_REQUEST_TIMEOUT), DEFAULT_REQUEST_TIMEOUT);
    int streamingTimeout = chooseNewOverDefault(getIntValue(properties, HTTP_STREAMING_TIMEOUT), DEFAULT_STREAMING_TIMEOUT);
    if (streamingTimeout > DEFAULT_STREAMING_TIMEOUT) {
        // Minimum value for idle timeout so we don't have a busy thread checking for idle timeout too frequently!
        if (streamingTimeout < DEFAULT_MINIMUM_STREAMING_TIMEOUT) {
            streamingTimeout = DEFAULT_MINIMUM_STREAMING_TIMEOUT;
            LOG.warn("Streaming timeout is too small, resetting to the minimum allowed timeout value of {}ms", DEFAULT_MINIMUM_STREAMING_TIMEOUT);
        }
    }
    String httpServiceName = (String) properties.get(HTTP_SERVICE_NAME);
    HttpProtocolVersion httpProtocolVersion = chooseNewOverDefault(getHttpProtocolVersion(properties, HTTP_PROTOCOL_VERSION), _defaultHttpVersion);
    LOG.info("The service '{}' has been assigned to the ChannelPoolManager with key '{}', http.protocolVersion={}, usePipelineV2={}, requestTimeout={}ms, streamingTimeout={}ms", httpServiceName, key.getName(), httpProtocolVersion, _usePipelineV2, requestTimeout, streamingTimeout);
    if (_usePipelineV2) {
        ChannelPoolManager channelPoolManager;
        ChannelPoolManager sslChannelPoolManager;
        switch(httpProtocolVersion) {
            case HTTP_1_1:
                channelPoolManager = _channelPoolManagerFactory.buildStream(key);
                sslChannelPoolManager = _channelPoolManagerFactory.buildStream(sslKey);
                break;
            case HTTP_2:
                channelPoolManager = _channelPoolManagerFactory.buildHttp2Stream(key);
                sslChannelPoolManager = _channelPoolManagerFactory.buildHttp2Stream(sslKey);
                break;
            default:
                throw new IllegalArgumentException("Unrecognized HTTP protocol version " + httpProtocolVersion);
        }
        return new com.linkedin.r2.netty.client.HttpNettyClient(_eventLoopGroup, _executor, _callbackExecutorGroup, channelPoolManager, sslChannelPoolManager, httpProtocolVersion, SystemClock.instance(), requestTimeout, streamingTimeout, shutdownTimeout);
    }
    TransportClient streamClient;
    switch(httpProtocolVersion) {
        case HTTP_1_1:
            streamClient = new HttpNettyStreamClient(_eventLoopGroup, _executor, requestTimeout, shutdownTimeout, _callbackExecutorGroup, _jmxManager, _channelPoolManagerFactory.buildStream(key), _channelPoolManagerFactory.buildStream(sslKey));
            break;
        case HTTP_2:
            streamClient = new Http2NettyStreamClient(_eventLoopGroup, _executor, requestTimeout, shutdownTimeout, _callbackExecutorGroup, _jmxManager, _channelPoolManagerFactory.buildHttp2Stream(key), _channelPoolManagerFactory.buildHttp2Stream(sslKey));
            break;
        default:
            throw new IllegalArgumentException("Unrecognized HTTP protocol version " + httpProtocolVersion);
    }
    HttpNettyClient legacyClient = new HttpNettyClient(_eventLoopGroup, _executor, requestTimeout, shutdownTimeout, _callbackExecutorGroup, _jmxManager, _channelPoolManagerFactory.buildRest(key), _channelPoolManagerFactory.buildRest(sslKey));
    return new MixedClient(legacyClient, streamClient);
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) HttpProtocolVersion(com.linkedin.r2.transport.http.common.HttpProtocolVersion) ChannelPoolManager(com.linkedin.r2.transport.http.client.common.ChannelPoolManager) HttpNettyClient(com.linkedin.r2.transport.http.client.rest.HttpNettyClient) HttpNettyStreamClient(com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient) Http2NettyStreamClient(com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient) ChannelPoolManagerKey(com.linkedin.r2.transport.http.client.common.ChannelPoolManagerKey)

Example 17 with TransportClient

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

the class Bootstrap method createHttp2Client.

public static Client createHttp2Client(FilterChain filters, boolean restOverStream) {
    HashMap<String, String> properties = new HashMap<>();
    properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_2.name());
    final TransportClient client = new HttpClientFactory.Builder().setFilterChain(filters).build().getClient(properties);
    return new TransportClientAdapter(client, restOverStream);
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) HashMap(java.util.HashMap) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory)

Example 18 with TransportClient

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

the class SimpleLoadBalancerState method getTrackerClient.

private TrackerClient getTrackerClient(String serviceName, URI uri, Map<Integer, PartitionData> partitionDataMap, DegraderImpl.Config config, Clock clk, long callTrackerInterval, String errorStatusPattern) {
    Map<String, TransportClient> clientsByScheme = _serviceClients.get(serviceName);
    if (clientsByScheme == null) {
        _log.error("getTrackerClient: unknown service name {} for URI {} and partitionDataMap {}", new Object[] { serviceName, uri, partitionDataMap });
        return null;
    }
    TransportClient client = clientsByScheme.get(uri.getScheme().toLowerCase());
    if (client == null) {
        // logging this at debug because there may be situations where a service may want some of its
        // clients talking https while others are ok using http.
        debug(_log, "No TransportClient for scheme {}, service {}, URI {} and partitionDataMap {}. " + "This client may not be configured to handle URIs in this scheme.", new Object[] { uri.getScheme(), serviceName, uri, partitionDataMap });
        return null;
    }
    TrackerClient trackerClient = new TrackerClient(uri, partitionDataMap, client, clk, config, callTrackerInterval, errorStatusPattern);
    return trackerClient;
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient)

Example 19 with TransportClient

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

the class TestDisruptor method testStreamErrorDisrupt.

@Test
public void testStreamErrorDisrupt() throws Exception {
    final Map<String, String> properties = new HashMap<>();
    properties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(REQUEST_TIMEOUT));
    final TransportClientFactory factory = new HttpClientFactory.Builder().build();
    final TransportClient client = factory.getClient(properties);
    final RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.error(REQUEST_LATENCY));
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(false);
    client.streamRequest(new StreamRequestBuilder(new URI(REQUEST_URI)).build(EntityStreams.emptyStream()), requestContext, new HashMap<>(), response -> {
        success.set(response.hasError() && response.getError() instanceof DisruptedException);
        latch.countDown();
    });
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
    Assert.assertTrue(success.get(), "Unexpected transport response");
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DisruptedException(com.linkedin.r2.disruptor.DisruptedException) RequestContext(com.linkedin.r2.message.RequestContext) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) Test(org.testng.annotations.Test)

Example 20 with TransportClient

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

the class TestDisruptor method testStreamNoDisrupt.

@Test
public void testStreamNoDisrupt() throws Exception {
    final Map<String, String> properties = new HashMap<>();
    final TransportClientFactory factory = new HttpClientFactory.Builder().build();
    final TransportClient client = factory.getClient(properties);
    final RequestContext requestContext = new RequestContext();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(false);
    client.streamRequest(new StreamRequestBuilder(new URI(REQUEST_URI)).build(EntityStreams.emptyStream()), requestContext, new HashMap<>(), response -> {
        success.set(!response.hasError() && response.getResponse() != null);
        latch.countDown();
    });
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
    Assert.assertTrue(success.get(), "Unexpected transport response");
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RequestContext(com.linkedin.r2.message.RequestContext) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) Test(org.testng.annotations.Test)

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