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<>());
}
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<>());
}
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);
}
};
}
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);
}
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<>());
}
Aggregations