Search in sources :

Example 6 with RetriableRequestException

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

the class TestServerRetryFilter method testRetryFilter.

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

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Assert.assertEquals(wireAttrs.get(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY), retryMessage);
        }
    };
    FilterChain filterChain = FilterChains.createRestChain(captureFilter, retryFilter);
    FilterUtil.fireRestError(filterChain, new RestException(null, new RetriableRequestException(retryMessage)), new HashMap<>());
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) ServerRetryFilter(com.linkedin.r2.filter.transport.ServerRetryFilter) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) RestException(com.linkedin.r2.message.rest.RestException) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 7 with RetriableRequestException

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

the class TestServerRetryFilter method testStreamRetryFilter.

@Test
public void testStreamRetryFilter() {
    String retryMessage = "this is a retry";
    ServerRetryFilter retryFilter = new ServerRetryFilter();
    StreamFilter captureFilter = new StreamFilter() {

        @Override
        public void onStreamError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<StreamRequest, StreamResponse> nextFilter) {
            Assert.assertEquals(wireAttrs.get(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY), retryMessage);
        }
    };
    FilterChain filterChain = FilterChains.createStreamChain(captureFilter, retryFilter);
    FilterUtil.fireRestError(filterChain, new StreamException(null, new RetriableRequestException(retryMessage)), new HashMap<>());
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) ServerRetryFilter(com.linkedin.r2.filter.transport.ServerRetryFilter) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) RequestContext(com.linkedin.r2.message.RequestContext) StreamFilter(com.linkedin.r2.filter.message.stream.StreamFilter) HashMap(java.util.HashMap) Map(java.util.Map) StreamException(com.linkedin.r2.message.stream.StreamException) Test(org.testng.annotations.Test)

Example 8 with RetriableRequestException

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

the class RetryClient method decorateCallback.

private <R, T> Callback<T> decorateCallback(R request, RequestContext requestContext, DecoratorClient<R, T> client, Callback<T> callback) {
    return new Callback<T>() {

        @Override
        public void onError(Throwable e) {
            // Retry will be triggered if and only if:
            // 1. A RetriableRequestException is thrown
            // 2. There is no target host hint
            boolean retry = false;
            if (e instanceof RetriableRequestException) {
                URI targetHostUri = KeyMapper.TargetHostHints.getRequestContextTargetHost(requestContext);
                if (targetHostUri == null) {
                    Set<URI> exclusionSet = ExcludedHostHints.getRequestContextExcludedHosts(requestContext);
                    int attempts = exclusionSet.size();
                    if (attempts <= _limit) {
                        LOG.warn("A retriable exception happens. Going to retry. This is attempt {}. Current exclusion set: ", attempts, ". Current exclusion set: " + exclusionSet);
                        retry = true;
                        client.doRequest(request, requestContext, this);
                    } else {
                        LOG.warn("Retry limit exceeded. This request will fail.");
                    }
                }
            }
            if (!retry) {
                ExcludedHostHints.clearRequestContextExcludedHosts(requestContext);
                callback.onError(e);
            }
        }

        @Override
        public void onSuccess(T result) {
            ExcludedHostHints.clearRequestContextExcludedHosts(requestContext);
            callback.onSuccess(result);
        }
    };
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) URI(java.net.URI)

Example 9 with RetriableRequestException

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

the class RetryTrackerClient method restRequest.

@Override
public void restRequest(RestRequest request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<RestResponse> callback) {
    TransportResponse<RestResponse> 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(new RestResponseBuilder().build(), wireAttrs);
    }
    callback.onResponse(response);
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder)

Example 10 with RetriableRequestException

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

the class TestServerRetryFilter method testNestedException.

@Test
public void testNestedException() {
    String retryMessage = "this is a retry";
    ServerRetryFilter retryFilter = new ServerRetryFilter();
    RestFilter captureFilter = new RestFilter() {

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Assert.assertEquals(wireAttrs.get(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY), retryMessage);
        }
    };
    FilterChain filterChain = FilterChains.createRestChain(captureFilter, retryFilter);
    Throwable nestedException = new RetriableRequestException(retryMessage);
    for (int i = 0; i < 5; i++) {
        nestedException = new RuntimeException(nestedException);
    }
    FilterUtil.fireRestError(filterChain, new RestException(null, nestedException), new HashMap<>());
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) ServerRetryFilter(com.linkedin.r2.filter.transport.ServerRetryFilter) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) RestException(com.linkedin.r2.message.rest.RestException) RetriableRequestException(com.linkedin.r2.RetriableRequestException) 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