Search in sources :

Example 11 with RemoteInvocationException

use of com.linkedin.r2.RemoteInvocationException 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();
            // Sets a reader that does not consume any byte
            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) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) 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)

Example 12 with RemoteInvocationException

use of com.linkedin.r2.RemoteInvocationException in project rest.li by linkedin.

the class RestClient method sendRestRequestImpl.

/**
 * Sends an untyped REST request using a callback.
 *
 * @param requestContext context for the request
 * @param uri for resource
 * @param method to perform
 * @param dataMap request body entity
 * @param headers additional headers to be added to the request
 * @param cookies the cookies to be sent with the request
 * @param methodName the method name (used for finders and actions)
 * @param protocolVersion the version of the Rest.li protocol used to build this request
 * @param requestOptions contains compression force on/off overrides, request content type and accept types
 * @param callback to call on request completion. In the event of an error, the callback
 *                 will receive a {@link com.linkedin.r2.RemoteInvocationException}. If a valid
 *                 error response was received from the remote server, the callback will receive
 *                 a {@link com.linkedin.r2.message.rest.RestException} containing the error details.
 */
private void sendRestRequestImpl(RequestContext requestContext, URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, String methodName, ProtocolVersion protocolVersion, RestliRequestOptions requestOptions, Callback<RestResponse> callback) {
    try {
        TimingContextUtil.beginTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_RESTLI_SERIALIZATION.key());
        RestRequest request = buildRestRequest(uri, method, dataMap, headers, cookies, protocolVersion, requestOptions.getContentType(), requestOptions.getAcceptTypes(), false);
        TimingContextUtil.endTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_RESTLI_SERIALIZATION.key());
        String operation = OperationNameGenerator.generate(method, methodName);
        requestContext.putLocalAttr(R2Constants.OPERATION, operation);
        requestContext.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestOptions.getRequestCompressionOverride());
        requestContext.putLocalAttr(R2Constants.RESPONSE_COMPRESSION_OVERRIDE, requestOptions.getResponseCompressionOverride());
        TimingContextUtil.endTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_RESTLI.key());
        TimingContextUtil.beginTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_R2.key());
        final Callback<RestResponse> wrappedCallback = new TimingCallback.Builder<>(callback, requestContext).addEndTimingKey(FrameworkTimingKeys.CLIENT_RESPONSE_R2.key()).addBeginTimingKey(FrameworkTimingKeys.CLIENT_RESPONSE_RESTLI.key()).build();
        _client.restRequest(request, requestContext, wrappedCallback);
    } catch (Exception e) {
        // No need to wrap the exception; RestLiCallbackAdapter.onError() will take care of that
        callback.onError(e);
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) TimingCallback(com.linkedin.r2.message.timing.TimingCallback) URISyntaxException(java.net.URISyntaxException) MimeTypeParseException(javax.activation.MimeTypeParseException) IOException(java.io.IOException) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException)

Example 13 with RemoteInvocationException

use of com.linkedin.r2.RemoteInvocationException in project rest.li by linkedin.

the class RestClient method sendStreamRequestImpl.

/**
 * Sends an untyped stream request using a callback.
 *
 * @param requestContext context for the request
 * @param uri for resource
 * @param method to perform
 * @param dataMap request body entity
 * @param headers additional headers to be added to the request
 * @param cookies the cookies to be sent with the request
 * @param methodName the method name (used for finders and actions)
 * @param protocolVersion the version of the Rest.li protocol used to build this request
 * @param requestOptions contains compression force on/off overrides, request content type and accept types
 * @param callback to call on request completion. In the event of an error, the callback
 *                 will receive a {@link com.linkedin.r2.RemoteInvocationException}. If a valid
 *                 error response was received from the remote server, the callback will receive
 *                 a {@link com.linkedin.r2.message.rest.RestException} containing the error details.
 */
private void sendStreamRequestImpl(RequestContext requestContext, URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, String methodName, ProtocolVersion protocolVersion, RestliRequestOptions requestOptions, List<Object> streamingAttachments, Callback<StreamResponse> callback) {
    try {
        TimingContextUtil.beginTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_RESTLI_SERIALIZATION.key());
        final StreamRequest request = buildStreamRequest(uri, method, dataMap, headers, cookies, protocolVersion, requestOptions.getContentType(), requestOptions.getAcceptTypes(), requestOptions.getAcceptResponseAttachments(), streamingAttachments);
        TimingContextUtil.endTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_RESTLI_SERIALIZATION.key());
        String operation = OperationNameGenerator.generate(method, methodName);
        requestContext.putLocalAttr(R2Constants.OPERATION, operation);
        requestContext.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestOptions.getRequestCompressionOverride());
        requestContext.putLocalAttr(R2Constants.RESPONSE_COMPRESSION_OVERRIDE, requestOptions.getResponseCompressionOverride());
        TimingContextUtil.endTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_RESTLI.key());
        TimingContextUtil.beginTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_R2.key());
        final Callback<StreamResponse> wrappedCallback = new TimingCallback.Builder<>(callback, requestContext).addEndTimingKey(FrameworkTimingKeys.CLIENT_RESPONSE_R2.key()).addBeginTimingKey(FrameworkTimingKeys.CLIENT_RESPONSE_RESTLI.key()).build();
        _client.streamRequest(request, requestContext, wrappedCallback);
    } catch (Exception e) {
        // No need to wrap the exception; RestLiCallbackAdapter.onError() will take care of that
        callback.onError(e);
    }
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) TimingCallback(com.linkedin.r2.message.timing.TimingCallback) URISyntaxException(java.net.URISyntaxException) MimeTypeParseException(javax.activation.MimeTypeParseException) IOException(java.io.IOException) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Example 14 with RemoteInvocationException

use of com.linkedin.r2.RemoteInvocationException in project rest.li by linkedin.

the class TestDebugRequestHandlers method testParseqTraceDebugDeleteRequestHandlerTracevis.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void testParseqTraceDebugDeleteRequestHandlerTracevis(RootBuilderWrapper<Long, Greeting> builders) throws URISyntaxException, ExecutionException, InterruptedException, RemoteInvocationException {
    Long newId = createNewGreetingOnTheServer(builders);
    RestRequest request = new RestRequestBuilder(new URI(URI_PREFIX + "greetingsPromise/" + newId + "/__debug/parseqtrace/tracevis")).setMethod("DELETE").setEntity(createNewGreetingBytes(newId)).build();
    sendRequestAndVerifyParseqTracevisResponse(request);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 15 with RemoteInvocationException

use of com.linkedin.r2.RemoteInvocationException in project rest.li by linkedin.

the class TestDebugRequestHandlers method testParseqTraceDebugPutRequestHandlerTracevis.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void testParseqTraceDebugPutRequestHandlerTracevis(RootBuilderWrapper<Long, Greeting> builders) throws URISyntaxException, ExecutionException, InterruptedException, RemoteInvocationException {
    Long newId = createNewGreetingOnTheServer(builders);
    RestRequest request = new RestRequestBuilder(new URI(URI_PREFIX + "greetingsPromise/" + newId + "/__debug/parseqtrace/tracevis")).setMethod("PUT").setEntity(createNewGreetingBytes(newId)).build();
    sendRequestAndVerifyParseqTracevisResponse(request);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)32 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)29 RestRequest (com.linkedin.r2.message.rest.RestRequest)13 RequestContext (com.linkedin.r2.message.RequestContext)10 HashMap (java.util.HashMap)10 ErrorResponse (com.linkedin.restli.common.ErrorResponse)8 URI (java.net.URI)8 FutureCallback (com.linkedin.common.callback.FutureCallback)7 EmptyRecord (com.linkedin.restli.common.EmptyRecord)7 Map (java.util.Map)7 ExecutionException (java.util.concurrent.ExecutionException)7 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)6 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)6 Callback (com.linkedin.common.callback.Callback)5 ByteString (com.linkedin.data.ByteString)5 RestException (com.linkedin.r2.message.rest.RestException)5 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)5 TimeoutException (java.util.concurrent.TimeoutException)5 RestResponse (com.linkedin.r2.message.rest.RestResponse)4 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)4