Search in sources :

Example 91 with Client

use of com.linkedin.r2.transport.common.Client 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<StreamResponse>();
        TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
        client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), 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) AsciiString(io.netty.util.AsciiString) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) 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)

Example 92 with Client

use of com.linkedin.r2.transport.common.Client in project rest.li by linkedin.

the class HttpNettyClient method writeRequest.

private void writeRequest(RestRequest request, RequestContext requestContext, Map<String, String> wireAttrs, final TimeoutTransportCallback<RestResponse> callback) {
    State state = _state.get();
    if (state != State.RUNNING) {
        errorResponse(callback, new IllegalStateException("Client is " + state));
        return;
    }
    URI uri = request.getURI();
    String scheme = uri.getScheme();
    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        errorResponse(callback, new IllegalArgumentException("Unknown scheme: " + scheme + " (only http/https is supported)"));
        return;
    }
    String host = uri.getHost();
    int port = uri.getPort();
    if (port == -1) {
        port = "http".equalsIgnoreCase(scheme) ? HTTP_DEFAULT_PORT : HTTPS_DEFAULT_PORT;
    }
    final RestRequest newRequest = new RestRequestBuilder(request).overwriteHeaders(WireAttributeHelper.toWireAttributes(wireAttrs)).build();
    final SocketAddress address;
    try {
        // TODO investigate DNS resolution and timing
        InetAddress inetAddress = InetAddress.getByName(host);
        address = new InetSocketAddress(inetAddress, port);
        requestContext.putLocalAttr(R2Constants.REMOTE_SERVER_ADDR, inetAddress.getHostAddress());
    } catch (UnknownHostException e) {
        errorResponse(callback, e);
        return;
    }
    requestContext.putLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_1_1);
    final AsyncPool<Channel> pool;
    try {
        pool = _channelPoolManager.getPoolForAddress(address);
    } catch (IllegalStateException e) {
        errorResponse(callback, e);
        return;
    }
    final Cancellable pendingGet = pool.get(new Callback<Channel>() {

        @Override
        public void onSuccess(final Channel channel) {
            // This handler ensures the channel is returned to the pool at the end of the
            // Netty pipeline.
            channel.attr(ChannelPoolHandler.CHANNEL_POOL_ATTR_KEY).set(pool);
            callback.addTimeoutTask(new Runnable() {

                @Override
                public void run() {
                    AsyncPool<Channel> pool = channel.attr(ChannelPoolHandler.CHANNEL_POOL_ATTR_KEY).getAndRemove();
                    if (pool != null) {
                        pool.dispose(channel);
                    }
                }
            });
            // This handler invokes the callback with the response once it arrives.
            channel.attr(RAPResponseHandler.CALLBACK_ATTR_KEY).set(callback);
            final State state = _state.get();
            if (state == State.REQUESTS_STOPPING || state == State.SHUTDOWN) {
                // In this case, we acquired a channel from the pool as request processing is halting.
                // The shutdown task might not timeout this callback, since it may already have scanned
                // all the channels for pending requests before we set the callback as the channel
                // attachment.  The TimeoutTransportCallback ensures the user callback in never
                // invoked more than once, so it is safe to invoke it unconditionally.
                errorResponse(callback, new TimeoutException("Operation did not complete before shutdown"));
                return;
            }
            // here we want the exception in outbound operations to be passed back through pipeline so that
            // the user callback would be invoked with the exception and the channel can be put back into the pool
            channel.writeAndFlush(newRequest).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        }

        @Override
        public void onError(Throwable e) {
            errorResponse(callback, e);
        }
    });
    if (pendingGet != null) {
        callback.addTimeoutTask(new Runnable() {

            @Override
            public void run() {
                pendingGet.cancel();
            }
        });
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) Cancellable(com.linkedin.r2.util.Cancellable) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) TimeoutRunnable(com.linkedin.r2.util.TimeoutRunnable) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) TimeoutException(java.util.concurrent.TimeoutException)

Example 93 with Client

use of com.linkedin.r2.transport.common.Client in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testResponseSize.

public void testResponseSize(AbstractNettyStreamClient client, int responseSize, int expectedResult) throws Exception {
    Server server = new HttpServerBuilder().responseSize(responseSize).build();
    try {
        server.start();
        RestRequest r = new RestRequestBuilder(new URI(URL)).build();
        FutureCallback<StreamResponse> cb = new FutureCallback<StreamResponse>();
        TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
        client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), callback);
        StreamResponse response = cb.get(30, TimeUnit.SECONDS);
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
        response.getEntityStream().setReader(new Reader() {

            @Override
            public void onInit(ReadHandle rh) {
                rh.request(Integer.MAX_VALUE);
            }

            @Override
            public void onDataAvailable(ByteString data) {
            }

            @Override
            public void onDone() {
                latch.countDown();
            }

            @Override
            public void onError(Throwable e) {
                error.set(e);
                latch.countDown();
            }
        });
        if (!latch.await(30, TimeUnit.SECONDS)) {
            Assert.fail("Timeout waiting for response");
        }
        if (expectedResult == TOO_LARGE) {
            Assert.assertNotNull(error.get(), "Max response size exceeded, expected exception. ");
            verifyCauseChain(error.get(), TooLongFrameException.class);
        }
        if (expectedResult == RESPONSE_OK) {
            Assert.assertNull(error.get(), "Unexpected Exception: response size <= max size");
        }
    } catch (ExecutionException e) {
        if (expectedResult == RESPONSE_OK) {
            Assert.fail("Unexpected ExecutionException, response was <= max response size.", e);
        }
        verifyCauseChain(e, RemoteInvocationException.class, TooLongFrameException.class);
    } 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) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Reader(com.linkedin.r2.message.stream.entitystream.Reader) AtomicReference(java.util.concurrent.atomic.AtomicReference) AsciiString(io.netty.util.AsciiString) ByteString(com.linkedin.data.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) 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)

Example 94 with Client

use of com.linkedin.r2.transport.common.Client in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testShutdownRequestOutstanding.

private void testShutdownRequestOutstanding(AbstractNettyStreamClient client, Class<?>... causeChain) throws Exception {
    CountDownLatch responseLatch = new CountDownLatch(1);
    Server server = new HttpServerBuilder().responseLatch(responseLatch).build();
    try {
        server.start();
        RestRequest r = new RestRequestBuilder(new URI(URL)).build();
        FutureCallback<StreamResponse> cb = new FutureCallback<StreamResponse>();
        TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
        client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), callback);
        FutureCallback<None> shutdownCallback = new FutureCallback<None>();
        client.shutdown(shutdownCallback);
        shutdownCallback.get(30, TimeUnit.SECONDS);
        // 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("Get timed out, should have thrown ExecutionException", e);
    } catch (ExecutionException e) {
        verifyCauseChain(e, causeChain);
    } finally {
        responseLatch.countDown();
        server.stop();
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) Server(org.eclipse.jetty.server.Server) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AsciiString(io.netty.util.AsciiString) ByteString(com.linkedin.data.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) ExecutionException(java.util.concurrent.ExecutionException) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException)

Example 95 with Client

use of com.linkedin.r2.transport.common.Client in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testBadAddress.

@Test(dataProvider = "badAddressClients")
public void testBadAddress(AbstractNettyStreamClient client) throws InterruptedException, IOException, TimeoutException {
    RestRequest r = new RestRequestBuilder(URI.create("http://this.host.does.not.exist.linkedin.com")).build();
    FutureCallback<StreamResponse> cb = new FutureCallback<StreamResponse>();
    TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
    client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), callback);
    try {
        cb.get(30, TimeUnit.SECONDS);
        Assert.fail("Get was supposed to fail");
    } catch (ExecutionException e) {
        verifyCauseChain(e, RemoteInvocationException.class, UnknownHostException.class);
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) UnknownHostException(java.net.UnknownHostException) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AsciiString(io.netty.util.AsciiString) ByteString(com.linkedin.data.ByteString) 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) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)126 RequestContext (com.linkedin.r2.message.RequestContext)94 URI (java.net.URI)82 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)65 FutureCallback (com.linkedin.common.callback.FutureCallback)62 RestRequest (com.linkedin.r2.message.rest.RestRequest)62 HashMap (java.util.HashMap)61 RestResponse (com.linkedin.r2.message.rest.RestResponse)48 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)44 ArrayList (java.util.ArrayList)37 None (com.linkedin.common.util.None)36 Client (com.linkedin.r2.transport.common.Client)36 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)36 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)35 CountDownLatch (java.util.concurrent.CountDownLatch)35 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)34 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)33 ExecutionException (java.util.concurrent.ExecutionException)33 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)26 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)26