This is an automated email from the ASF dual-hosted git repository.
msingh 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 937f68d RATIS-601. Fix NotLeaderException handling. Contributed by
Mukul Kumar Singh.
937f68d is described below
commit 937f68d5a2e6fd21ba344ebab9259b6ffe9a292c
Author: Mukul Kumar Singh <[email protected]>
AuthorDate: Thu Jun 27 21:24:51 2019 +0530
RATIS-601. Fix NotLeaderException handling. Contributed by Mukul Kumar
Singh.
---
.../src/main/java/org/apache/ratis/client/RaftClientRpc.java | 3 ++-
.../src/main/java/org/apache/ratis/client/impl/OrderedAsync.java | 7 ++++++-
.../src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java | 7 +++++--
.../src/main/java/org/apache/ratis/grpc/client/GrpcClientRpc.java | 2 +-
.../src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java | 5 ++---
5 files changed, 16 insertions(+), 8 deletions(-)
diff --git
a/ratis-client/src/main/java/org/apache/ratis/client/RaftClientRpc.java
b/ratis-client/src/main/java/org/apache/ratis/client/RaftClientRpc.java
index 395dc59..cf8300e 100644
--- a/ratis-client/src/main/java/org/apache/ratis/client/RaftClientRpc.java
+++ b/ratis-client/src/main/java/org/apache/ratis/client/RaftClientRpc.java
@@ -21,6 +21,7 @@ import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftClientRequest;
import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.protocol.RaftPeerId;
+import org.apache.ratis.util.IOUtils;
import org.apache.ratis.util.JavaUtils;
import java.io.Closeable;
@@ -61,6 +62,6 @@ public interface RaftClientRpc extends Closeable {
* @return true if the given throwable should be handled; otherwise, return
false.
*/
default boolean shouldReconnect(Throwable t) {
- return false;
+ return IOUtils.shouldReconnect(t);
}
}
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 2ad393e..35681b8 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
@@ -229,7 +229,12 @@ class OrderedAsync {
if (!retryPolicy.shouldRetry(attemptCount, request)) {
handleAsyncRetryFailure(request, attemptCount, e);
} else {
- client.handleIOException(request, (IOException) e, null,
this::resetSlidingWindow);
+ if (e instanceof NotLeaderException) {
+ NotLeaderException nle = (NotLeaderException)e;
+ client.handleNotLeaderException(request, nle,
this::resetSlidingWindow);
+ } else {
+ client.handleIOException(request, (IOException) e, null,
this::resetSlidingWindow);
+ }
}
if (e instanceof NotLeaderException) {
throw new CompletionException(e);
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 65c84a2..9798287 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
@@ -361,11 +361,14 @@ final class RaftClientImpl implements RaftClient {
}
LOG.debug("{}: oldLeader={}, curLeader={}, newLeader={}", clientId,
oldLeader, curLeader, newLeader);
- final boolean changeLeader = newLeader != null && stillLeader;
+ final boolean changeLeader =
+ !(ioe instanceof AlreadyClosedException || ioe instanceof
TimeoutIOException)
+ && newLeader != null && stillLeader;
final boolean reconnect = changeLeader || clientRpc.shouldReconnect(ioe);
if (reconnect) {
if (changeLeader && oldLeader.equals(leaderId)) {
- LOG.debug("{}: change Leader from {} to {}", clientId, oldLeader,
newLeader);
+ LOG.debug("{} {}: client change Leader from {} to {} ex={}", groupId,
+ clientId, oldLeader, newLeader, ioe.getClass().getName());
this.leaderId = newLeader;
}
clientRpc.handleException(oldLeader, ioe, reconnect);
diff --git
a/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/GrpcClientRpc.java
b/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/GrpcClientRpc.java
index 31b34ee..5883598 100644
--- a/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/GrpcClientRpc.java
+++ b/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/GrpcClientRpc.java
@@ -170,6 +170,6 @@ public class GrpcClientRpc extends
RaftClientRpcWithProxy<GrpcClientProtocolClie
} else if (e instanceof IllegalArgumentException) {
return e.getMessage().contains("null frame before EOS");
}
- return false;
+ return super.shouldReconnect(e);
}
}
diff --git
a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
index ecd790d..ec21a17 100644
---
a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
+++
b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
@@ -451,9 +451,8 @@ public class RaftServerImpl implements RaftServerProtocol,
RaftServerAsynchronou
RaftPeerId leaderId = state.getLeaderId();
if (leaderId == null || leaderId.equals(state.getSelfId())) {
// No idea about who is the current leader. Or the peer is the current
- // leader, but it is about to step down
- RaftPeer suggestedLeader =
getRaftConf().getRandomPeer(state.getSelfId());
- leaderId = suggestedLeader == null ? null : suggestedLeader.getId();
+ // leader, but it is about to step down. set the suggested leader as
null.
+ leaderId = null;
}
RaftConfiguration conf = getRaftConf();
Collection<RaftPeer> peers = conf.getPeers();