Search in sources :

Example 1 with Http2NettyStreamClient

use of com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient in project rest.li by linkedin.

the class TestHttp2NettyStreamClient method testChannelReusedAfterStreamingTimeout.

/**
 * When a request fails due to {@link TimeoutException}, connection should not be destroyed.
 * @throws Exception
 */
@Test(timeOut = TEST_TIMEOUT, retryAnalyzer = SingleRetry.class)
public void testChannelReusedAfterStreamingTimeout() throws Exception {
    final HttpServerBuilder.HttpServerStatsProvider statsProvider = new HttpServerBuilder.HttpServerStatsProvider();
    final HttpServerBuilder serverBuilder = new HttpServerBuilder();
    final Server server = serverBuilder.serverStatsProvider(statsProvider).stopTimeout(0).build();
    final HttpClientBuilder clientBuilder = new HttpClientBuilder(_eventLoop, _scheduler);
    final Http2NettyStreamClient client = clientBuilder.setRequestTimeout(1000).buildHttp2StreamClient();
    final TransportResponse<StreamResponse> response1;
    final TransportResponse<StreamResponse> response2;
    try {
        server.start();
        final StreamRequestBuilder builder1 = new StreamRequestBuilder(new URI(URL));
        final StreamRequest request1 = builder1.setMethod(METHOD).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[REQUEST_SIZE]))));
        final FutureTransportCallback<StreamResponse> callback1 = new FutureTransportCallback<>();
        client.streamRequest(request1, new RequestContext(), new HashMap<>(), callback1);
        response1 = callback1.get();
        Assert.assertNotNull(response1);
        Assert.assertFalse(response1.hasError());
        response1.getResponse().getEntityStream().setReader(new TimeoutReader());
        final StreamRequestBuilder builder2 = new StreamRequestBuilder(new URI(URL));
        final StreamRequest request2 = builder2.setMethod(METHOD).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[REQUEST_SIZE]))));
        final FutureTransportCallback<StreamResponse> callback2 = new FutureTransportCallback<>();
        client.streamRequest(request2, new RequestContext(), new HashMap<>(), callback2);
        response2 = callback2.get();
    } finally {
        server.stop();
    }
    // The 2nd request should succeed
    Assert.assertNotNull(response2);
    Assert.assertFalse(response2.hasError());
    response2.getResponse().getEntityStream().setReader(new DrainReader());
    // The server should have seen 3 requests but establishes only 1 connection with the client
    Assert.assertEquals(statsProvider.requestCount(), 3);
    Assert.assertEquals(statsProvider.clientConnections().size(), 1);
}
Also used : Server(org.eclipse.jetty.server.Server) FutureTransportCallback(com.linkedin.r2.transport.common.bridge.common.FutureTransportCallback) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) HttpClientBuilder(com.linkedin.r2.transport.http.client.HttpClientBuilder) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test)

Example 2 with Http2NettyStreamClient

use of com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient in project rest.li by linkedin.

the class TestHttp2NettyStreamClient method testMaxConcurrentStreamExhaustion.

/**
 * When the maximum number of concurrent streams is exhausted, the client is expected to throw
 * an {@link StreamException} immediately.
 */
@Test(timeOut = TEST_TIMEOUT)
public void testMaxConcurrentStreamExhaustion() throws Exception {
    final HttpServerBuilder serverBuilder = new HttpServerBuilder();
    final Server server = serverBuilder.maxConcurrentStreams(0).build();
    final HttpClientBuilder clientBuilder = new HttpClientBuilder(_eventLoop, _scheduler);
    final Http2NettyStreamClient client = clientBuilder.buildHttp2StreamClient();
    final FutureTransportCallback<StreamResponse> callback = new FutureTransportCallback<>();
    final TransportResponse<StreamResponse> response;
    try {
        server.start();
        // Sends the stream request
        final StreamRequestBuilder builder = new StreamRequestBuilder(new URI(URL));
        final StreamRequest request = builder.setMethod(METHOD).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[REQUEST_SIZE]))));
        client.streamRequest(request, new RequestContext(), new HashMap<>(), callback);
        response = callback.get();
    } finally {
        server.stop();
    }
    Assert.assertNotNull(response);
    Assert.assertTrue(response.hasError());
    Assert.assertNotNull(response.getError());
    ExceptionTestUtil.verifyCauseChain(response.getError(), Http2Exception.StreamException.class);
}
Also used : Http2Exception(io.netty.handler.codec.http2.Http2Exception) Server(org.eclipse.jetty.server.Server) FutureTransportCallback(com.linkedin.r2.transport.common.bridge.common.FutureTransportCallback) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) HttpClientBuilder(com.linkedin.r2.transport.http.client.HttpClientBuilder) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test)

Example 3 with Http2NettyStreamClient

use of com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient in project rest.li by linkedin.

the class TestHttp2NettyStreamClient method testChannelReusedAfterRequestTimeout.

/**
 * When a request fails due to {@link TimeoutException}, connection should not be destroyed.
 * @throws Exception
 */
@Test(timeOut = TEST_TIMEOUT)
public void testChannelReusedAfterRequestTimeout() throws Exception {
    final HttpServerBuilder.HttpServerStatsProvider statsProvider = new HttpServerBuilder.HttpServerStatsProvider();
    final HttpServerBuilder serverBuilder = new HttpServerBuilder();
    final Server server = serverBuilder.serverStatsProvider(statsProvider).stopTimeout(0).build();
    final HttpClientBuilder clientBuilder = new HttpClientBuilder(_eventLoop, _scheduler);
    final Http2NettyStreamClient client = clientBuilder.setRequestTimeout(1000).buildHttp2StreamClient();
    final TransportResponse<StreamResponse> response1;
    final TransportResponse<StreamResponse> response2;
    try {
        server.start();
        final StreamRequestBuilder builder1 = new StreamRequestBuilder(new URI(URL));
        final StreamRequest request1 = builder1.setMethod(METHOD).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new TimeoutWriter()));
        final FutureTransportCallback<StreamResponse> callback1 = new FutureTransportCallback<>();
        client.streamRequest(request1, new RequestContext(), new HashMap<>(), callback1);
        response1 = callback1.get();
        final StreamRequestBuilder builder2 = new StreamRequestBuilder(new URI(URL));
        final StreamRequest request2 = builder2.setMethod(METHOD).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[REQUEST_SIZE]))));
        final FutureTransportCallback<StreamResponse> callback2 = new FutureTransportCallback<>();
        client.streamRequest(request2, new RequestContext(), new HashMap<>(), callback2);
        response2 = callback2.get();
    } finally {
        server.stop();
    }
    // The 1st request should be failed with timeout
    Assert.assertNotNull(response1);
    Assert.assertTrue(response1.hasError());
    Assert.assertNotNull(response1.getError());
    ExceptionTestUtil.verifyCauseChain(response1.getError(), TimeoutException.class);
    // The 2nd request should succeed
    Assert.assertNotNull(response2);
    Assert.assertFalse(response2.hasError());
    response2.getResponse().getEntityStream().setReader(new DrainReader());
    // The server should have seen 2 requests but establishes only 1 connection with the client
    Assert.assertEquals(statsProvider.requestCount(), 3);
    Assert.assertEquals(statsProvider.clientConnections().size(), 1);
}
Also used : Server(org.eclipse.jetty.server.Server) FutureTransportCallback(com.linkedin.r2.transport.common.bridge.common.FutureTransportCallback) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) HttpClientBuilder(com.linkedin.r2.transport.http.client.HttpClientBuilder) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test)

Example 4 with Http2NettyStreamClient

use of com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient 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 5 with Http2NettyStreamClient

use of com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testRequestContextAttributes.

@Test(dataProvider = "remoteClientAddressClients")
public void testRequestContextAttributes(AbstractNettyStreamClient client) throws InterruptedException, IOException, TimeoutException {
    RestRequest r = new RestRequestBuilder(URI.create("http://localhost")).build();
    FutureCallback<StreamResponse> cb = new FutureCallback<>();
    TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
    RequestContext requestContext = new RequestContext();
    client.streamRequest(Messages.toStreamRequest(r), requestContext, new HashMap<>(), callback);
    final String actualRemoteAddress = (String) requestContext.getLocalAttr(R2Constants.REMOTE_SERVER_ADDR);
    final HttpProtocolVersion actualProtocolVersion = (HttpProtocolVersion) requestContext.getLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION);
    Assert.assertTrue("127.0.0.1".equals(actualRemoteAddress) || "0:0:0:0:0:0:0:1".equals(actualRemoteAddress), "Actual remote client address is not expected. " + "The local attribute field must be IP address in string type" + actualRemoteAddress);
    if (client instanceof HttpNettyStreamClient) {
        Assert.assertEquals(actualProtocolVersion, HttpProtocolVersion.HTTP_1_1);
    } else if (client instanceof Http2NettyStreamClient) {
        Assert.assertEquals(actualProtocolVersion, HttpProtocolVersion.HTTP_2);
    } else {
        Assert.fail("Unexpected client instance type");
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) HttpProtocolVersion(com.linkedin.r2.transport.http.common.HttpProtocolVersion) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AsciiString(io.netty.util.AsciiString) ByteString(com.linkedin.data.ByteString) HttpNettyStreamClient(com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) Http2NettyStreamClient(com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Aggregations

RequestContext (com.linkedin.r2.message.RequestContext)6 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)5 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)5 URI (java.net.URI)5 Server (org.eclipse.jetty.server.Server)5 Test (org.testng.annotations.Test)5 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)4 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)4 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)4 HttpClientBuilder (com.linkedin.r2.transport.http.client.HttpClientBuilder)4 FutureTransportCallback (com.linkedin.r2.transport.common.bridge.common.FutureTransportCallback)3 HttpNettyStreamClient (com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient)3 Http2NettyStreamClient (com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient)3 FutureCallback (com.linkedin.common.callback.FutureCallback)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 DrainReader (com.linkedin.r2.message.stream.entitystream.DrainReader)2 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)2 HttpProtocolVersion (com.linkedin.r2.transport.http.common.HttpProtocolVersion)2 ByteString (com.linkedin.data.ByteString)1