You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by ru...@apache.org on 2021/01/19 23:42:14 UTC

[incubator-ratis] branch master updated: RATIS-1287. Incompatible proto changes due to RATIS-1158. (#397)

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

runzhiwang 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 ba24a23  RATIS-1287. Incompatible proto changes due to RATIS-1158. (#397)
ba24a23 is described below

commit ba24a235934d09974b90d906ad16e0167988f8fd
Author: Tsz-Wo Nicholas Sze <sz...@apache.org>
AuthorDate: Tue Jan 19 15:42:04 2021 -0800

    RATIS-1287. Incompatible proto changes due to RATIS-1158. (#397)
---
 .../apache/ratis/client/impl/ClientProtoUtils.java | 78 ++++++++++++++++++++--
 ratis-proto/src/main/proto/Raft.proto              | 16 ++++-
 2 files changed, 88 insertions(+), 6 deletions(-)

diff --git a/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java b/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java
index d00c2b4..bb706e7 100644
--- a/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java
+++ b/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java
@@ -36,6 +36,7 @@ import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
 import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
 import org.apache.ratis.proto.RaftProtos.*;
 import org.apache.ratis.util.ProtoUtils;
+import org.apache.ratis.util.ReflectionUtils;
 
 import java.util.List;
 import java.util.stream.Collectors;
@@ -216,6 +217,22 @@ public interface ClientProtoUtils {
         .build();
   }
 
+  static StateMachineExceptionProto.Builder toStateMachineExceptionProtoBuilder(StateMachineException e) {
+    final Throwable t = e.getCause() != null? e.getCause(): e;
+    return StateMachineExceptionProto.newBuilder()
+        .setExceptionClassName(t.getClass().getName())
+        .setErrorMsg(t.getMessage())
+        .setStacktrace(ProtoUtils.writeObject2ByteString(t.getStackTrace()));
+  }
+
+  static AlreadyClosedExceptionProto.Builder toAlreadyClosedExceptionProtoBuilder(AlreadyClosedException ace) {
+    final Throwable t = ace.getCause() != null ? ace.getCause() : ace;
+    return AlreadyClosedExceptionProto.newBuilder()
+        .setExceptionClassName(t.getClass().getName())
+        .setErrorMsg(ace.getMessage())
+        .setStacktrace(ProtoUtils.writeObject2ByteString(ace.getStackTrace()));
+  }
+
   static RaftClientReplyProto toRaftClientReplyProto(RaftClientReply reply) {
     final RaftClientReplyProto.Builder b = RaftClientReplyProto.newBuilder();
     if (reply != null) {
@@ -257,7 +274,7 @@ public interface ClientProtoUtils {
       }
 
       Optional.ofNullable(reply.getStateMachineException())
-          .map(ProtoUtils::toThrowableProto)
+          .map(ClientProtoUtils::toStateMachineExceptionProtoBuilder)
           .ifPresent(b::setStateMachineException);
 
       Optional.ofNullable(reply.getDataStreamException())
@@ -265,7 +282,7 @@ public interface ClientProtoUtils {
           .ifPresent(b::setDataStreamException);
 
       Optional.ofNullable(reply.getAlreadyClosedException())
-          .map(ProtoUtils::toThrowableProto)
+          .map(ClientProtoUtils::toAlreadyClosedExceptionProtoBuilder)
           .ifPresent(b::setAlreadyClosedException);
 
       Optional.ofNullable(reply.getLeaderSteppingDownException())
@@ -353,14 +370,14 @@ public interface ClientProtoUtils {
       final NotReplicatedExceptionProto nre = replyProto.getNotReplicatedException();
       e = new NotReplicatedException(nre.getCallId(), nre.getReplication(), nre.getLogIndex());
     } else if (replyProto.getExceptionDetailsCase().equals(STATEMACHINEEXCEPTION)) {
-      e = ProtoUtils.toThrowable(replyProto.getStateMachineException(), StateMachineException.class);
+      e = toStateMachineException(serverMemberId, replyProto.getStateMachineException());
     } else if (replyProto.getExceptionDetailsCase().equals(DATASTREAMEXCEPTION)) {
       e = ProtoUtils.toThrowable(replyProto.getDataStreamException(), DataStreamException.class);
     } else if (replyProto.getExceptionDetailsCase().equals(LEADERNOTREADYEXCEPTION)) {
       LeaderNotReadyExceptionProto lnreProto = replyProto.getLeaderNotReadyException();
       e = new LeaderNotReadyException(ProtoUtils.toRaftGroupMemberId(lnreProto.getServerId()));
     } else if (replyProto.getExceptionDetailsCase().equals(ALREADYCLOSEDEXCEPTION)) {
-      e = ProtoUtils.toThrowable(replyProto.getAlreadyClosedException(), AlreadyClosedException.class);
+      e = toAlreadyClosedException(replyProto.getAlreadyClosedException());
     } else if (replyProto.getExceptionDetailsCase().equals(LEADERSTEPPINGDOWNEXCEPTION)) {
       e = ProtoUtils.toThrowable(replyProto.getLeaderSteppingDownException(), LeaderSteppingDownException.class);
     } else if (replyProto.getExceptionDetailsCase().equals(TRANSFERLEADERSHIPEXCEPTION)) {
@@ -381,6 +398,59 @@ public interface ClientProtoUtils {
         .build();
   }
 
+  static StateMachineException toStateMachineException(RaftGroupMemberId memberId, StateMachineExceptionProto proto) {
+    return toStateMachineException(memberId,
+        proto.getExceptionClassName(),
+        proto.getErrorMsg(),
+        proto.getStacktrace());
+  }
+
+  static StateMachineException toStateMachineException(RaftGroupMemberId memberId,
+      String className, String errorMsg, ByteString stackTraceBytes) {
+    StateMachineException sme;
+    if (className == null) {
+      sme = new StateMachineException(errorMsg);
+    } else {
+      try {
+        final Class<?> clazz = Class.forName(className);
+        final Exception e = ReflectionUtils.instantiateException(clazz.asSubclass(Exception.class), errorMsg);
+        sme = new StateMachineException(memberId, e);
+      } catch (Exception e) {
+        sme = new StateMachineException(className + ": " + errorMsg);
+      }
+    }
+    final StackTraceElement[] stacktrace = (StackTraceElement[]) ProtoUtils.toObject(stackTraceBytes);
+    sme.setStackTrace(stacktrace);
+    return sme;
+  }
+
+  static AlreadyClosedException toAlreadyClosedException(AlreadyClosedExceptionProto proto) {
+    return toAlreadyClosedException(
+        proto.getExceptionClassName(),
+        proto.getErrorMsg(),
+        proto.getStacktrace());
+  }
+
+  static AlreadyClosedException toAlreadyClosedException(
+      String className, String errorMsg, ByteString stackTraceBytes) {
+    AlreadyClosedException ace;
+    if (className == null) {
+      ace = new AlreadyClosedException(errorMsg);
+    } else {
+      try {
+        Class<?> clazz = Class.forName(className);
+        final Exception e = ReflectionUtils.instantiateException(clazz.asSubclass(Exception.class), errorMsg);
+        ace = new AlreadyClosedException(errorMsg, e);
+      } catch (Exception e) {
+        ace = new AlreadyClosedException(className + ": " + errorMsg);
+      }
+    }
+    StackTraceElement[] stacktrace =
+        (StackTraceElement[]) ProtoUtils.toObject(stackTraceBytes);
+    ace.setStackTrace(stacktrace);
+    return ace;
+  }
+
   static GroupListReply toGroupListReply(GroupListReplyProto replyProto) {
     final RaftRpcReplyProto rpc = replyProto.getRpcReply();
     final List<RaftGroupId> groupIds = replyProto.getGroupIdList().stream()
diff --git a/ratis-proto/src/main/proto/Raft.proto b/ratis-proto/src/main/proto/Raft.proto
index 0b2dda6..0ff5d66 100644
--- a/ratis-proto/src/main/proto/Raft.proto
+++ b/ratis-proto/src/main/proto/Raft.proto
@@ -356,6 +356,18 @@ message NotReplicatedExceptionProto {
   uint64 logIndex = 3;
 }
 
+message StateMachineExceptionProto {
+  string exceptionClassName = 1;
+  string errorMsg = 2;
+  bytes stacktrace = 3;
+}
+
+message AlreadyClosedExceptionProto {
+  string exceptionClassName = 1;
+  string errorMsg = 2;
+  bytes stacktrace = 3;
+}
+
 message ThrowableProto {
   string className = 1;
   string errorMessage = 2;
@@ -370,9 +382,9 @@ message RaftClientReplyProto {
   oneof ExceptionDetails {
     NotLeaderExceptionProto notLeaderException = 3;
     NotReplicatedExceptionProto notReplicatedException = 4;
-    ThrowableProto stateMachineException = 5;
+    StateMachineExceptionProto stateMachineException = 5;
     LeaderNotReadyExceptionProto leaderNotReadyException = 6;
-    ThrowableProto alreadyClosedException = 7;
+    AlreadyClosedExceptionProto alreadyClosedException = 7;
     ThrowableProto dataStreamException = 8;
     ThrowableProto leaderSteppingDownException = 9;
     ThrowableProto transferLeadershipException = 10;