Search in sources :

Example 31 with Request

use of com.linkedin.r2.message.Request in project rest.li by linkedin.

the class RAPRequestEncoder method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (msg instanceof StreamRequest) {
        StreamRequest request = (StreamRequest) msg;
        HttpRequest nettyRequest = NettyRequestAdapter.toNettyRequest(request);
        ctx.write(nettyRequest, promise);
        _currentReader = new BufferedReader(ctx, MAX_BUFFERED_CHUNKS, FLUSH_THRESHOLD);
        request.getEntityStream().setReader(_currentReader);
    } else {
        _currentReader = null;
        ctx.write(msg, promise);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Example 32 with Request

use of com.linkedin.r2.message.Request in project rest.li by linkedin.

the class Http2InitializerHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof RequestWithCallback)) {
        ctx.write(msg, promise);
        return;
    }
    if (_setupComplete) {
        ctx.write(msg);
    } else {
        Request request = ((RequestWithCallback) msg).request();
        URI uri = request.getURI();
        String scheme = uri.getScheme();
        if (scheme.equalsIgnoreCase(HttpScheme.HTTPS.toString())) {
            configureHttpsPipeline(ctx);
        } else if (scheme.equalsIgnoreCase(HttpScheme.HTTP.toString())) {
            configureHttpPipeline(ctx, request);
        } else {
            throw new IllegalArgumentException("Invalid scheme " + scheme + ", expected either http or https");
        }
        ctx.write(msg);
    }
}
Also used : RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback) Request(com.linkedin.r2.message.Request) URI(java.net.URI)

Example 33 with Request

use of com.linkedin.r2.message.Request in project rest.li by linkedin.

the class Http2NettyStreamClient method doWriteRequest.

@Override
protected void doWriteRequest(Request request, final RequestContext context, SocketAddress address, TimeoutTransportCallback<StreamResponse> callback) {
    final AsyncPool<Channel> pool;
    try {
        pool = _channelPoolManager.getPoolForAddress(address);
    } catch (IllegalStateException e) {
        errorResponse(callback, e);
        return;
    }
    context.putLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_2);
    Callback<Channel> getCallback = new ChannelPoolGetCallback(pool, request, callback);
    final Cancellable pendingGet = pool.get(getCallback);
    if (pendingGet != null) {
        callback.addTimeoutTask(() -> pendingGet.cancel());
    }
}
Also used : Cancellable(com.linkedin.r2.util.Cancellable) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel)

Example 34 with Request

use of com.linkedin.r2.message.Request in project rest.li by linkedin.

the class TestHttpNettyClient method testShutdownStuckInPool.

@Test
public void testShutdownStuckInPool() throws InterruptedException, ExecutionException, TimeoutException {
    // Test that shutdown works when the outstanding request is stuck in the pool waiting for a channel
    HttpNettyClient client = new HttpNettyClient(new NoCreations(_scheduler), _scheduler, 60000, 1, 1024 * 1024 * 2);
    RestRequest r = new RestRequestBuilder(URI.create("http://some.host/")).build();
    FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
    client.restRequest(r, new RequestContext(), new HashMap<String, String>(), new TransportCallbackAdapter<RestResponse>(futureCallback));
    FutureCallback<None> shutdownCallback = new FutureCallback<None>();
    client.shutdown(shutdownCallback);
    shutdownCallback.get(30, TimeUnit.SECONDS);
    try {
        futureCallback.get(30, TimeUnit.SECONDS);
        Assert.fail("get should have thrown exception");
    } catch (ExecutionException e) {
        verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) 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) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 35 with Request

use of com.linkedin.r2.message.Request in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testSlowReaderTimeout.

/**
   * Tests slow EntityStream {@link Reader} implementation should be subject to streaming timeout even
   * if the entire response entity can be buffered in memory.
   *
   * @throws Exception
   */
@Test(dataProvider = "slowReaderTimeoutClientProvider")
public void testSlowReaderTimeout(AbstractNettyStreamClient client) throws Exception {
    // Sets the response size to be greater than zero but smaller than the in-memory buffer for HTTP/1.1
    // and smaller than the receiving window size for HTTP/2 so the receiver will not block sender
    Server server = new HttpServerBuilder().responseSize(R2Constants.DEFAULT_DATA_CHUNK_SIZE).build();
    StreamRequest request = new StreamRequestBuilder(new URI(URL)).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.emptyStream());
    final CountDownLatch responseLatch = new CountDownLatch(1);
    final CountDownLatch streamLatch = new CountDownLatch(1);
    final AtomicReference<TransportResponse<StreamResponse>> atomicTransportResponse = new AtomicReference<>();
    final AtomicReference<Throwable> atomicThrowable = new AtomicReference<>();
    try {
        server.start();
        client.streamRequest(request, new RequestContext(), new HashMap<>(), response -> {
            atomicTransportResponse.set(response);
            responseLatch.countDown();
            response.getResponse().getEntityStream().setReader(new Reader() {

                @Override
                public void onInit(ReadHandle rh) {
                }

                @Override
                public void onDataAvailable(ByteString data) {
                }

                @Override
                public void onDone() {
                }

                @Override
                public void onError(Throwable e) {
                    atomicThrowable.set(e);
                    streamLatch.countDown();
                }
            });
        });
    } finally {
        responseLatch.await(5, TimeUnit.SECONDS);
        streamLatch.await(5, TimeUnit.SECONDS);
        server.stop();
    }
    TransportResponse<StreamResponse> transportResponse = atomicTransportResponse.get();
    Assert.assertNotNull(transportResponse, "Expected to receive a response");
    Assert.assertFalse(transportResponse.hasError(), "Expected to receive a response without error");
    Assert.assertNotNull(transportResponse.getResponse());
    Assert.assertNotNull(transportResponse.getResponse().getEntityStream());
    Throwable throwable = atomicThrowable.get();
    Assert.assertNotNull(throwable, "Expected onError invoked with TimeoutException");
    Assert.assertTrue(throwable instanceof RemoteInvocationException);
    Assert.assertNotNull(throwable.getCause());
    Assert.assertTrue(throwable.getCause() instanceof TimeoutException);
}
Also used : 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) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) TransportResponse(com.linkedin.r2.transport.common.bridge.common.TransportResponse) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)209 RestRequest (com.linkedin.r2.message.rest.RestRequest)189 RequestContext (com.linkedin.r2.message.RequestContext)124 URI (java.net.URI)112 RestResponse (com.linkedin.r2.message.rest.RestResponse)111 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)105 ByteString (com.linkedin.data.ByteString)67 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)61 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)59 RestException (com.linkedin.r2.message.rest.RestException)54 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)50 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)47 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)44 FutureCallback (com.linkedin.common.callback.FutureCallback)41 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)38 HashMap (java.util.HashMap)38 Callback (com.linkedin.common.callback.Callback)34 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)34 ExecutionException (java.util.concurrent.ExecutionException)33 BeforeTest (org.testng.annotations.BeforeTest)31