Search in sources :

Example 1 with RetriableRequestException

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

the class ChannelPoolLifecycle method create.

@Override
public void create(final Callback<Channel> channelCallback) {
    final long start = System.currentTimeMillis();
    _bootstrap.connect(_remoteAddress).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture channelFuture) throws Exception {
            if (channelFuture.isSuccess()) {
                synchronized (_createTimeTracker) {
                    _createTimeTracker.addValue(System.currentTimeMillis() - start);
                }
                Channel c = channelFuture.channel();
                if (_tcpNoDelay) {
                    c.config().setOption(ChannelOption.TCP_NODELAY, true);
                }
                _channelGroup.add(c);
                channelCallback.onSuccess(c);
            } else {
                Throwable cause = channelFuture.cause();
                if (cause instanceof ConnectException) {
                    channelCallback.onError(new RetriableRequestException(cause));
                } else {
                    channelCallback.onError(HttpNettyStreamClient.toException(cause));
                }
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RetriableRequestException(com.linkedin.r2.RetriableRequestException) Channel(io.netty.channel.Channel) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ConnectException(java.net.ConnectException) RetriableRequestException(com.linkedin.r2.RetriableRequestException) ConnectException(java.net.ConnectException)

Example 2 with RetriableRequestException

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

the class RetryTrackerClient method handleRequest.

private <REQ extends Request, RESP extends Response> void handleRequest(REQ request, Map<String, String> wireAttrs, TransportCallback<RESP> callback, Consumer<REQ> requestConsumer, Supplier<RESP> responseSupplier) {
    // Process request
    requestConsumer.accept(request);
    // Prepare response
    TransportResponse<RESP> response;
    if (_uri.toString().startsWith("http://test.linkedin.com/retry")) {
        RetriableRequestException ex = new RetriableRequestException("Data not available");
        response = TransportResponseImpl.error(ex);
    } else if (_uri.toString().equals("http://test.linkedin.com/bad")) {
        response = TransportResponseImpl.error(RestException.forError(404, "exception happens"), wireAttrs);
    } else {
        response = TransportResponseImpl.success(responseSupplier.get(), wireAttrs);
    }
    callback.onResponse(response);
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException)

Example 3 with RetriableRequestException

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

the class ClientRetryFilter method processError.

private <REQ extends Request, RES extends Response> void processError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<REQ, RES> nextFilter) {
    String retryAttr = wireAttrs.get(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY);
    if (retryAttr != null) {
        if (ex instanceof RestException) {
            ex = new RestException(((RestException) ex).getResponse(), new RetriableRequestException(retryAttr, ex.getCause()));
        } else if (ex instanceof StreamException) {
            ex = new StreamException(((StreamException) ex).getResponse(), new RetriableRequestException(retryAttr, ex.getCause()));
        } else {
            ex = new RetriableRequestException(retryAttr, ex);
        }
    }
    nextFilter.onError(ex, requestContext, wireAttrs);
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) RestException(com.linkedin.r2.message.rest.RestException) StreamException(com.linkedin.r2.message.stream.StreamException)

Example 4 with RetriableRequestException

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

the class TestClientRetryFilter method testRetryFilter.

@Test
public void testRetryFilter() {
    String retryMessage = "this is a retry";
    ClientRetryFilter clientRetryFilter = new ClientRetryFilter();
    RestFilter captureFilter = new RestFilter() {

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Assert.assertTrue(ex instanceof RetriableRequestException);
            Assert.assertEquals(retryMessage, ex.getMessage());
        }
    };
    Map<String, String> wireAttributes = new HashMap<>();
    wireAttributes.put(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY, retryMessage);
    FilterChain filterChain = FilterChains.createRestChain(captureFilter, clientRetryFilter);
    FilterUtil.fireRestError(filterChain, new RemoteInvocationException("exception"), wireAttributes);
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) HashMap(java.util.HashMap) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) ClientRetryFilter(com.linkedin.r2.filter.transport.ClientRetryFilter) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 5 with RetriableRequestException

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

the class TestClientRetryFilter method testClientSideRetriableException.

@Test
public void testClientSideRetriableException() {
    ClientRetryFilter clientRetryFilter = new ClientRetryFilter();
    RestFilter captureFilter = new RestFilter() {

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Assert.assertTrue(ex instanceof RetriableRequestException);
            Assert.assertFalse(((RetriableRequestException) ex).getDoNotRetryOverride());
        }
    };
    FilterChain filterChain = FilterChains.createRestChain(captureFilter, clientRetryFilter);
    FilterUtil.fireRestError(filterChain, new RetriableRequestException("exception"), new HashMap<>());
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) ClientRetryFilter(com.linkedin.r2.filter.transport.ClientRetryFilter) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

RetriableRequestException (com.linkedin.r2.RetriableRequestException)10 FilterChain (com.linkedin.r2.filter.FilterChain)5 NextFilter (com.linkedin.r2.filter.NextFilter)5 RequestContext (com.linkedin.r2.message.RequestContext)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Test (org.testng.annotations.Test)5 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)4 ServerRetryFilter (com.linkedin.r2.filter.transport.ServerRetryFilter)3 RestException (com.linkedin.r2.message.rest.RestException)3 ClientRetryFilter (com.linkedin.r2.filter.transport.ClientRetryFilter)2 StreamException (com.linkedin.r2.message.stream.StreamException)2 Callback (com.linkedin.common.callback.Callback)1 FutureCallback (com.linkedin.common.callback.FutureCallback)1 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)1 StreamFilter (com.linkedin.r2.filter.message.stream.StreamFilter)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1