You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by sh...@apache.org on 2020/04/22 11:02:25 UTC

[incubator-ratis] branch master updated: RATIS-853. Unordered Client request should not sleep when NotLeaderException provides leader information. Contributed by Lokesh Jain.

This is an automated email from the ASF dual-hosted git repository.

shashikant pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new 64586b1  RATIS-853. Unordered Client request should not sleep when NotLeaderException provides leader information. Contributed by Lokesh Jain.
64586b1 is described below

commit 64586b105ef6d116d775ea395d023056f152f3f4
Author: Shashikant Banerjee <sh...@apache.org>
AuthorDate: Wed Apr 22 16:32:12 2020 +0530

    RATIS-853. Unordered Client request should not sleep when NotLeaderException provides leader information. Contributed by Lokesh Jain.
---
 .../main/java/org/apache/ratis/client/impl/OrderedAsync.java  | 11 +++--------
 .../java/org/apache/ratis/client/impl/RaftClientImpl.java     | 10 +++++++++-
 .../java/org/apache/ratis/client/impl/UnorderedAsync.java     | 10 +++++-----
 3 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/ratis-client/src/main/java/org/apache/ratis/client/impl/OrderedAsync.java b/ratis-client/src/main/java/org/apache/ratis/client/impl/OrderedAsync.java
index 03b1de7..27a8c79 100644
--- a/ratis-client/src/main/java/org/apache/ratis/client/impl/OrderedAsync.java
+++ b/ratis-client/src/main/java/org/apache/ratis/client/impl/OrderedAsync.java
@@ -30,7 +30,6 @@ import org.apache.ratis.protocol.RaftClientReply;
 import org.apache.ratis.protocol.RaftClientRequest;
 import org.apache.ratis.protocol.RaftException;
 import org.apache.ratis.protocol.RaftPeerId;
-import org.apache.ratis.retry.RetryPolicies;
 import org.apache.ratis.retry.RetryPolicy;
 import org.apache.ratis.util.IOUtils;
 import org.apache.ratis.util.JavaUtils;
@@ -198,12 +197,7 @@ public final class OrderedAsync {
     }).exceptionally(e -> {
       if (e instanceof CompletionException) {
         e = JavaUtils.unwrapCompletionException(e);
-        RetryPolicy retryPolicyToUse = retryPolicy;
-        if (e instanceof NotLeaderException) {
-          retryPolicyToUse = ((NotLeaderException) e).getSuggestedLeader() != null ?
-              RetryPolicies.retryForeverNoSleep() : retryPolicy;
-        }
-        scheduleWithTimeout(pending, request, retryPolicyToUse, e);
+        scheduleWithTimeout(pending, request, retryPolicy, e);
         return null;
       }
       f.completeExceptionally(e);
@@ -216,7 +210,8 @@ public final class OrderedAsync {
     final int attempt = pending.getAttemptCount();
     final ClientRetryEvent event = new ClientRetryEvent(attempt, request,
         pending.getExceptionCount(e), e);
-    final TimeDuration sleepTime = retryPolicy.handleAttemptFailure(event).getSleepTime();
+    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));
   }
diff --git a/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java b/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java
index c4c1f2c..665f2e6 100644
--- a/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java
+++ b/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java
@@ -31,6 +31,7 @@ import org.apache.ratis.retry.RetryPolicy;
 import org.apache.ratis.util.CollectionUtils;
 import org.apache.ratis.util.JavaUtils;
 import org.apache.ratis.util.Preconditions;
+import org.apache.ratis.util.TimeDuration;
 import org.apache.ratis.util.TimeoutScheduler;
 
 import java.io.IOException;
@@ -127,6 +128,11 @@ final class RaftClientImpl implements RaftClient {
     return retryPolicy;
   }
 
+  TimeDuration getEffectiveSleepTime(Throwable t, TimeDuration sleepDefault) {
+    return t instanceof NotLeaderException && ((NotLeaderException) t).getSuggestedLeader() != null ?
+        TimeDuration.ZERO : sleepDefault;
+  }
+
   TimeoutScheduler getScheduler() {
     return scheduler;
   }
@@ -291,12 +297,14 @@ final class RaftClientImpl implements RaftClient {
       final int exceptionCount = ioe != null ? pending.incrementExceptionCount(ioe) : 0;
       ClientRetryEvent event = new ClientRetryEvent(attemptCount, request, exceptionCount, ioe);
       final RetryPolicy.Action action = retryPolicy.handleAttemptFailure(event);
+      TimeDuration sleepTime = getEffectiveSleepTime(ioe, action.getSleepTime());
+
       if (!action.shouldRetry()) {
         throw (IOException)noMoreRetries(event);
       }
 
       try {
-        action.getSleepTime().sleep();
+        sleepTime.sleep();
       } catch (InterruptedException e) {
         throw new InterruptedIOException("retry policy=" + retryPolicy);
       }
diff --git a/ratis-client/src/main/java/org/apache/ratis/client/impl/UnorderedAsync.java b/ratis-client/src/main/java/org/apache/ratis/client/impl/UnorderedAsync.java
index ca9e637..c84e7c7 100644
--- a/ratis-client/src/main/java/org/apache/ratis/client/impl/UnorderedAsync.java
+++ b/ratis-client/src/main/java/org/apache/ratis/client/impl/UnorderedAsync.java
@@ -25,9 +25,9 @@ import org.apache.ratis.protocol.NotLeaderException;
 import org.apache.ratis.protocol.RaftClientReply;
 import org.apache.ratis.protocol.RaftClientRequest;
 import org.apache.ratis.protocol.RaftException;
-import org.apache.ratis.retry.RetryPolicies;
 import org.apache.ratis.retry.RetryPolicy;
 import org.apache.ratis.util.JavaUtils;
+import org.apache.ratis.util.TimeDuration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -82,11 +82,13 @@ public interface UnorderedAsync {
           f.complete(reply);
           return;
         }
-        RetryPolicy retryPolicy = client.getRetryPolicy();
+
         final Throwable cause = replyException != null ? replyException : e;
         final int causeCount = pending.incrementExceptionCount(cause);
         final ClientRetryEvent event = new ClientRetryEvent(attemptCount, request, causeCount, cause);
+        RetryPolicy retryPolicy = client.getRetryPolicy();
         final RetryPolicy.Action action = retryPolicy.handleAttemptFailure(event);
+        TimeDuration sleepTime = client.getEffectiveSleepTime(cause, action.getSleepTime());
         if (!action.shouldRetry()) {
           f.completeExceptionally(client.noMoreRetries(event));
           return;
@@ -103,8 +105,6 @@ public interface UnorderedAsync {
           if (e instanceof IOException) {
             if (e instanceof NotLeaderException) {
               client.handleNotLeaderException(request, (NotLeaderException) e, null);
-              retryPolicy = ((NotLeaderException) e).getSuggestedLeader() != null ?
-                  RetryPolicies.retryForeverNoSleep() : retryPolicy;
             } else if (e instanceof GroupMismatchException) {
               f.completeExceptionally(e);
               return;
@@ -120,7 +120,7 @@ public interface UnorderedAsync {
         }
 
         LOG.debug("schedule retry for attempt #{}, policy={}, request={}", attemptCount, retryPolicy, request);
-        client.getScheduler().onTimeout(action.getSleepTime(),
+        client.getScheduler().onTimeout(sleepTime,
             () -> sendRequestWithRetry(pending, client), LOG, () -> clientId + ": Failed~ to retry " + request);
       } catch (Throwable t) {
         LOG.error(clientId + ": Failed " + request, t);