Search in sources :

Example 1 with ClientRetryEvent

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());
    }
}
Also used : ClientRetryEvent(org.apache.ratis.client.retry.ClientRetryEvent) RequestTypeDependentRetryPolicy(org.apache.ratis.client.retry.RequestTypeDependentRetryPolicy)

Example 2 with ClientRetryEvent

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());
        }
    }
}
Also used : ClientRetryEvent(org.apache.ratis.client.retry.ClientRetryEvent)

Example 3 with ClientRetryEvent

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);
        }
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) TransferLeadershipException(org.apache.ratis.protocol.exceptions.TransferLeadershipException) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException) ClientRetryEvent(org.apache.ratis.client.retry.ClientRetryEvent) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) TimeDuration(org.apache.ratis.util.TimeDuration) RetryPolicy(org.apache.ratis.retry.RetryPolicy)

Example 4 with ClientRetryEvent

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;
    });
}
Also used : Preconditions(org.apache.ratis.util.Preconditions) SlidingWindowEntry(org.apache.ratis.proto.RaftProtos.SlidingWindowEntry) LoggerFactory(org.slf4j.LoggerFactory) ClientRetryEvent(org.apache.ratis.client.retry.ClientRetryEvent) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) CallId(org.apache.ratis.rpc.CallId) ConcurrentMap(java.util.concurrent.ConcurrentMap) Message(org.apache.ratis.protocol.Message) NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) ProtoUtils(org.apache.ratis.util.ProtoUtils) JavaUtils(org.apache.ratis.util.JavaUtils) IOUtils(org.apache.ratis.util.IOUtils) SlidingWindow(org.apache.ratis.util.SlidingWindow) Logger(org.slf4j.Logger) LongFunction(java.util.function.LongFunction) RetryPolicy(org.apache.ratis.retry.RetryPolicy) Semaphore(java.util.concurrent.Semaphore) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) TypeCase(org.apache.ratis.proto.RaftProtos.RaftClientRequestProto.TypeCase) Objects(java.util.Objects) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) Optional(java.util.Optional) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) RaftClientConfigKeys(org.apache.ratis.client.RaftClientConfigKeys) PendingClientRequest(org.apache.ratis.client.impl.RaftClientImpl.PendingClientRequest) TimeDuration(org.apache.ratis.util.TimeDuration) ClientRetryEvent(org.apache.ratis.client.retry.ClientRetryEvent) NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) CompletionException(java.util.concurrent.CompletionException) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) IOException(java.io.IOException) RetryPolicy(org.apache.ratis.retry.RetryPolicy)

Example 5 with ClientRetryEvent

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));
}
Also used : ClientRetryEvent(org.apache.ratis.client.retry.ClientRetryEvent) TimeDuration(org.apache.ratis.util.TimeDuration)

Aggregations

ClientRetryEvent (org.apache.ratis.client.retry.ClientRetryEvent)8 TimeDuration (org.apache.ratis.util.TimeDuration)6 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)5 IOException (java.io.IOException)4 RequestTypeDependentRetryPolicy (org.apache.ratis.client.retry.RequestTypeDependentRetryPolicy)3 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)3 GroupMismatchException (org.apache.ratis.protocol.exceptions.GroupMismatchException)3 NotLeaderException (org.apache.ratis.protocol.exceptions.NotLeaderException)3 RetryPolicy (org.apache.ratis.retry.RetryPolicy)3 CompletionException (java.util.concurrent.CompletionException)2 BaseTest (org.apache.ratis.BaseTest)2 Test (org.junit.Test)2 InterruptedIOException (java.io.InterruptedIOException)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1