Search in sources :

Example 1 with HttpNettyStreamClient

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

the class TestHttpNettyStreamClient method testMakingOutboundHttpsRequest.

@Test(enabled = false)
public void testMakingOutboundHttpsRequest() throws NoSuchAlgorithmException, InterruptedException, ExecutionException, TimeoutException {
    SSLContext context = SSLContext.getDefault();
    SSLParameters sslParameters = context.getDefaultSSLParameters();
    HttpNettyStreamClient client = new HttpClientBuilder(_eventLoop, _scheduler).setSSLContext(context).setSSLParameters(sslParameters).buildStreamClient();
    RestRequest r = new RestRequestBuilder(URI.create("https://www.howsmyssl.com/a/check")).build();
    FutureCallback<StreamResponse> cb = new FutureCallback<>();
    TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
    client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
    cb.get(30, TimeUnit.SECONDS);
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) SSLContext(javax.net.ssl.SSLContext) HttpNettyStreamClient(com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) SSLParameters(javax.net.ssl.SSLParameters) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 2 with HttpNettyStreamClient

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

the class TestHttpNettyStreamClient method testNoChannelTimeout.

@Test
public void testNoChannelTimeout() throws InterruptedException {
    HttpNettyStreamClient client = new HttpNettyStreamClient(new NoCreations(_scheduler), _scheduler, 500, 500);
    RestRequest r = new RestRequestBuilder(URI.create("http://localhost/")).build();
    FutureCallback<StreamResponse> cb = new FutureCallback<>();
    TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
    client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
    try {
        // This timeout needs to be significantly larger than the getTimeout of the netty client;
        // we're testing that the client will generate its own timeout
        cb.get(30, TimeUnit.SECONDS);
        Assert.fail("Get was supposed to time out");
    } catch (TimeoutException e) {
        // TimeoutException means the timeout for Future.get() elapsed and nothing happened.
        // Instead, we are expecting our callback to be invoked before the future timeout
        // with a timeout generated by the HttpNettyClient.
        Assert.fail("Unexpected TimeoutException, should have been ExecutionException", e);
    } catch (ExecutionException e) {
        verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) HttpNettyStreamClient(com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 3 with HttpNettyStreamClient

use of com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient 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 4 with HttpNettyStreamClient

use of com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient 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)

Example 5 with HttpNettyStreamClient

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

the class TestHttpNettyStreamClient method testHeaderSize.

public void testHeaderSize(AbstractNettyStreamClient client, int headerSize, int expectedResult) throws Exception {
    Server server = new HttpServerBuilder().headerSize(headerSize).build();
    try {
        server.start();
        RestRequest r = new RestRequestBuilder(new URI(URL)).build();
        FutureCallback<StreamResponse> cb = new FutureCallback<>();
        TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
        client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
        cb.get(300, TimeUnit.SECONDS);
        if (expectedResult == TOO_LARGE) {
            Assert.fail("Max header size exceeded, expected exception. ");
        }
    } catch (ExecutionException e) {
        if (expectedResult == RESPONSE_OK) {
            Assert.fail("Unexpected ExecutionException, header was <= max header size.", e);
        }
        if (client instanceof HttpNettyStreamClient) {
            verifyCauseChain(e, RemoteInvocationException.class, TooLongFrameException.class);
        } else if (client instanceof Http2NettyStreamClient) {
            verifyCauseChain(e, RemoteInvocationException.class, Http2Exception.class);
        } else {
            Assert.fail("Unrecognized client");
        }
    } finally {
        server.stop();
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Server(org.eclipse.jetty.server.Server) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) URI(java.net.URI) 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) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback)

Aggregations

HttpNettyStreamClient (com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient)6 FutureCallback (com.linkedin.common.callback.FutureCallback)5 RequestContext (com.linkedin.r2.message.RequestContext)5 RestRequest (com.linkedin.r2.message.rest.RestRequest)5 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)5 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)4 Test (org.testng.annotations.Test)4 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)3 Http2NettyStreamClient (com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient)3 ExecutionException (java.util.concurrent.ExecutionException)3 HttpProtocolVersion (com.linkedin.r2.transport.http.common.HttpProtocolVersion)2 TimeoutException (java.util.concurrent.TimeoutException)2 None (com.linkedin.common.util.None)1 ByteString (com.linkedin.data.ByteString)1 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)1 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)1 ChannelPoolManager (com.linkedin.r2.transport.http.client.common.ChannelPoolManager)1 ChannelPoolManagerKey (com.linkedin.r2.transport.http.client.common.ChannelPoolManagerKey)1 HttpNettyClient (com.linkedin.r2.transport.http.client.rest.HttpNettyClient)1