in ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java [264:342]
static RaftClientReplyProto toRaftClientReplyProto(RaftClientReply reply) {
final RaftClientReplyProto.Builder b = RaftClientReplyProto.newBuilder();
if (reply != null) {
b.setRpcReply(toRaftRpcReplyProtoBuilder(reply.getClientId().toByteString(),
reply.getServerId().toByteString(), reply.getRaftGroupId(),
reply.getCallId(), reply.isSuccess()));
b.setLogIndex(reply.getLogIndex());
if (reply.getMessage() != null) {
b.setMessage(toClientMessageEntryProtoBuilder(reply.getMessage()));
}
b.addAllCommitInfos(reply.getCommitInfos());
final NotLeaderException nle = reply.getNotLeaderException();
if (nle != null) {
NotLeaderExceptionProto.Builder nleBuilder =
NotLeaderExceptionProto.newBuilder();
final RaftPeer suggestedLeader = nle.getSuggestedLeader();
if (suggestedLeader != null) {
nleBuilder.setSuggestedLeader(suggestedLeader.getRaftPeerProto());
}
nleBuilder.addAllPeersInConf(ProtoUtils.toRaftPeerProtos(nle.getPeers()));
b.setNotLeaderException(nleBuilder.build());
}
final NotReplicatedException nre = reply.getNotReplicatedException();
if (nre != null) {
final NotReplicatedExceptionProto.Builder nreBuilder = NotReplicatedExceptionProto.newBuilder()
.setCallId(nre.getCallId())
.setReplication(nre.getRequiredReplication())
.setLogIndex(nre.getLogIndex());
b.setNotReplicatedException(nreBuilder);
}
Optional.ofNullable(reply.getLeaderNotReadyException())
.map(e -> LeaderNotReadyExceptionProto.newBuilder().setServerId(e.getRaftGroupMemberIdProto()))
.ifPresent(b::setLeaderNotReadyException);
Optional.ofNullable(reply.getStateMachineException())
.map(ClientProtoUtils::toStateMachineExceptionProtoBuilder)
.ifPresent(b::setStateMachineException);
Optional.ofNullable(reply.getDataStreamException())
.map(ProtoUtils::toThrowableProto)
.ifPresent(b::setDataStreamException);
Optional.ofNullable(reply.getAlreadyClosedException())
.map(ClientProtoUtils::toAlreadyClosedExceptionProtoBuilder)
.ifPresent(b::setAlreadyClosedException);
Optional.ofNullable(reply.getLeaderSteppingDownException())
.map(ProtoUtils::toThrowableProto)
.ifPresent(b::setLeaderSteppingDownException);
Optional.ofNullable(reply.getTransferLeadershipException())
.map(ProtoUtils::toThrowableProto)
.ifPresent(b::setTransferLeadershipException);
Optional.ofNullable(reply.getReadException())
.map(ProtoUtils::toThrowableProto)
.ifPresent(b::setReadException);
Optional.ofNullable(reply.getReadIndexException())
.map(ProtoUtils::toThrowableProto)
.ifPresent(b::setReadIndexException);
final RaftClientReplyProto serialized = b.build();
final RaftException e = reply.getException();
if (e != null) {
final RaftClientReply deserialized = toRaftClientReply(serialized);
if (!Optional.ofNullable(deserialized.getException())
.map(Object::getClass).filter(e.getClass()::equals).isPresent()) {
throw new AssertionError("Corruption while serializing reply= " + reply
+ " but serialized=" + serialized + " and deserialized=" + deserialized, e);
}
}
return serialized;
}
return b.build();
}