use of org.apache.ratis.client.retry.ClientRetryEvent in project incubator-ratis by apache.
the class TestRetryPolicy method checkEvent.
/**
* Check Event with the policy defined.
* @param exceptionAttemptCount
* @param retryPolicy
* @param raftClientRequest
* @param exception
* @param exceptionPolicyPair
*/
private void checkEvent(int exceptionAttemptCount, RetryPolicy retryPolicy, RaftClientRequest raftClientRequest, Throwable exception, Pair exceptionPolicyPair) {
final ClientRetryEvent event = new ClientRetryEvent(exceptionAttemptCount, raftClientRequest, exception);
final RetryPolicy.Action action = retryPolicy.handleAttemptFailure(event);
final boolean expected = exceptionAttemptCount < exceptionPolicyPair.retries;
Assert.assertEquals(expected, action.shouldRetry());
if (expected) {
Assert.assertEquals(exceptionPolicyPair.sleepTime, action.getSleepTime().getDuration());
} else {
Assert.assertEquals(0L, action.getSleepTime().getDuration());
}
}
use of org.apache.ratis.client.retry.ClientRetryEvent in project incubator-ratis by apache.
the class TestExceptionDependentRetry method testException.
private void testException(int retries, int maxAttempts, ExceptionDependentRetry exceptionDependentRetry, Exception exception, long sleepTime) {
for (int i = 0; i < retries + 1; i++) {
RetryPolicy.Action action = exceptionDependentRetry.handleAttemptFailure(new ClientRetryEvent(i, null, exception));
final boolean expected = i < retries && i < maxAttempts;
Assert.assertEquals(expected, action.shouldRetry());
if (expected) {
Assert.assertEquals(sleepTime, action.getSleepTime().getDuration());
} else {
Assert.assertEquals(0L, action.getSleepTime().getDuration());
}
}
}
use of org.apache.ratis.client.retry.ClientRetryEvent in project incubator-ratis by apache.
the class BlockingImpl method sendRequestWithRetry.
RaftClientReply sendRequestWithRetry(Supplier<RaftClientRequest> supplier) throws IOException {
RaftClientImpl.PendingClientRequest pending = new RaftClientImpl.PendingClientRequest() {
@Override
public RaftClientRequest newRequestImpl() {
return supplier.get();
}
};
while (true) {
final RaftClientRequest request = pending.newRequest();
IOException ioe = null;
try {
final RaftClientReply reply = sendRequest(request);
if (reply != null) {
return client.handleReply(request, reply);
}
} catch (GroupMismatchException | StateMachineException | TransferLeadershipException | LeaderSteppingDownException | AlreadyClosedException | AlreadyExistsException e) {
throw e;
} catch (IOException e) {
ioe = e;
}
pending.incrementExceptionCount(ioe);
ClientRetryEvent event = new ClientRetryEvent(request, ioe, pending);
final RetryPolicy retryPolicy = client.getRetryPolicy();
final RetryPolicy.Action action = retryPolicy.handleAttemptFailure(event);
TimeDuration sleepTime = client.getEffectiveSleepTime(ioe, action.getSleepTime());
if (!action.shouldRetry()) {
throw (IOException) client.noMoreRetries(event);
}
try {
sleepTime.sleep();
} catch (InterruptedException e) {
throw new InterruptedIOException("retry policy=" + retryPolicy);
}
}
}
use of org.apache.ratis.client.retry.ClientRetryEvent in project incubator-ratis by apache.
the class OrderedAsync method sendRequest.
private CompletableFuture<RaftClientReply> sendRequest(PendingOrderedRequest pending) {
final RetryPolicy retryPolicy = client.getRetryPolicy();
final CompletableFuture<RaftClientReply> f;
final RaftClientRequest request;
if (getSlidingWindow((RaftPeerId) null).isFirst(pending.getSeqNum())) {
pending.setFirstRequest();
}
request = pending.newRequest();
LOG.debug("{}: send* {}", client.getId(), request);
f = client.getClientRpc().sendRequestAsync(request);
return f.thenApply(reply -> {
LOG.debug("{}: receive* {}", client.getId(), reply);
getSlidingWindow(request).receiveReply(request.getSlidingWindowEntry().getSeqNum(), reply, this::sendRequestWithRetry);
return reply;
}).exceptionally(e -> {
if (LOG.isTraceEnabled()) {
LOG.trace(client.getId() + ": Failed* " + request, e);
} else {
LOG.debug("{}: Failed* {} with {}", client.getId(), request, e);
}
e = JavaUtils.unwrapCompletionException(e);
if (e instanceof IOException && !(e instanceof GroupMismatchException)) {
pending.incrementExceptionCount(e);
final ClientRetryEvent event = new ClientRetryEvent(request, e, pending);
if (!retryPolicy.handleAttemptFailure(event).shouldRetry()) {
handleAsyncRetryFailure(event);
} else {
if (e instanceof NotLeaderException) {
NotLeaderException nle = (NotLeaderException) e;
client.handleNotLeaderException(request, nle, this::resetSlidingWindow);
} else {
client.handleIOException(request, (IOException) e, null, this::resetSlidingWindow);
}
}
throw new CompletionException(e);
}
failAllAsyncRequests(request, e);
return null;
});
}
use of org.apache.ratis.client.retry.ClientRetryEvent in project incubator-ratis by apache.
the class OrderedAsync method scheduleWithTimeout.
private void scheduleWithTimeout(PendingOrderedRequest pending, RaftClientRequest request, RetryPolicy retryPolicy, Throwable e) {
final int attempt = pending.getAttemptCount();
final ClientRetryEvent event = new ClientRetryEvent(request, e, pending);
final TimeDuration sleepTime = client.getEffectiveSleepTime(e, retryPolicy.handleAttemptFailure(event).getSleepTime());
LOG.debug("schedule* attempt #{} with sleep {} and policy {} for {}", attempt, sleepTime, retryPolicy, request);
scheduleWithTimeout(pending, sleepTime, getSlidingWindow(request));
}
Aggregations