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

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


The following commit(s) were added to refs/heads/master by this push:
     new 51244e42c RATIS-2068. Avoid logging raw StateMachine data body. (#1074)
51244e42c is described below

commit 51244e42cabb98f7ac9ca144e311222d9c6e5a45
Author: Duong Nguyen <[email protected]>
AuthorDate: Fri May 3 11:48:52 2024 -0700

    RATIS-2068. Avoid logging raw StateMachine data body. (#1074)
---
 .../ratis/grpc/server/GrpcServerProtocolService.java  |  2 +-
 .../org/apache/ratis/server/impl/RaftServerImpl.java  |  5 +++--
 .../apache/ratis/server/raftlog/LogProtoUtils.java    | 19 ++++++++++++-------
 .../org/apache/ratis/server/raftlog/RaftLogBase.java  |  4 ++--
 .../server/raftlog/segmented/SegmentedRaftLog.java    |  8 ++++----
 .../apache/ratis/server/util/ServerStringUtils.java   |  8 ++++++--
 6 files changed, 28 insertions(+), 18 deletions(-)

diff --git 
a/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServerProtocolService.java
 
b/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServerProtocolService.java
index 9c426f322..00cb699cf 100644
--- 
a/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServerProtocolService.java
+++ 
b/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServerProtocolService.java
@@ -316,7 +316,7 @@ class GrpcServerProtocolService extends 
RaftServerProtocolServiceImplBase {
 
       @Override
       String requestToString(AppendEntriesRequestProto request) {
-        return ServerStringUtils.toAppendEntriesRequestString(request);
+        return ServerStringUtils.toAppendEntriesRequestString(request, null);
       }
 
       @Override
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 34fede600..4512a2c22 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
@@ -1471,7 +1471,8 @@ class RaftServerImpl implements RaftServer.Division,
 
       return appendEntriesAsync(leaderId, request.getCallId(), previous, 
requestRef);
     } catch(Exception t) {
-      LOG.error("{}: Failed appendEntries* {}", getMemberId(), 
toAppendEntriesRequestString(r), t);
+      LOG.error("{}: Failed appendEntries* {}", getMemberId(),
+          toAppendEntriesRequestString(r, 
stateMachine::toStateMachineLogEntryString), t);
       throw IOUtils.asIOException(t);
     } finally {
       requestRef.release();
@@ -1530,7 +1531,7 @@ class RaftServerImpl implements RaftServer.Division,
     final List<LogEntryProto> entries = proto.getEntriesList();
     final boolean isHeartbeat = entries.isEmpty();
     logAppendEntries(isHeartbeat, () -> getMemberId() + ": appendEntries* "
-        + toAppendEntriesRequestString(proto));
+        + toAppendEntriesRequestString(proto, 
stateMachine::toStateMachineLogEntryString));
 
     final long leaderTerm = proto.getLeaderTerm();
     final long currentTerm;
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/LogProtoUtils.java 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/LogProtoUtils.java
index 59da4c3fd..e969eaa48 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/LogProtoUtils.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/LogProtoUtils.java
@@ -19,7 +19,6 @@ package org.apache.ratis.server.raftlog;
 
 import org.apache.ratis.proto.RaftProtos.*;
 import org.apache.ratis.protocol.ClientId;
-import org.apache.ratis.protocol.ClientInvocationId;
 import org.apache.ratis.protocol.RaftClientRequest;
 import org.apache.ratis.protocol.RaftPeer;
 import org.apache.ratis.server.RaftConfiguration;
@@ -46,9 +45,10 @@ public final class LogProtoUtils {
     }
     final String s;
     if (entry.hasStateMachineLogEntry()) {
-      s = ", " + Optional.ofNullable(function)
-          .orElseGet(() -> proto -> "" + ClientInvocationId.valueOf(proto))
-          .apply(entry.getStateMachineLogEntry());
+      if (function == null) {
+        function = LogProtoUtils::stateMachineLogEntryProtoToString;
+      }
+      s = ", " + function.apply(entry.getStateMachineLogEntry());
     } else if (entry.hasMetadataEntry()) {
       final MetadataProto metadata = entry.getMetadataEntry();
       s = "(c:" + metadata.getCommitIndex() + ")";
@@ -70,7 +70,11 @@ public final class LogProtoUtils {
   }
 
   static String stateMachineLogEntryProtoToString(StateMachineLogEntryProto p) 
{
-    return "logData:" + p.getLogData() + ", stateMachineEntry:" + p.getType() 
+ ":" + p.getStateMachineEntry();
+    final StateMachineEntryProto stateMachineEntry = p.getStateMachineEntry();
+    return p.getType()
+        + ": logData.size=" + p.getLogData().size()
+        + ", stateMachineData.size=" + 
stateMachineEntry.getStateMachineData().size()
+        + ", logEntryProtoSerializedSize=" + 
stateMachineEntry.getLogEntryProtoSerializedSize();
   }
 
   public static String toLogEntryString(LogEntryProto entry) {
@@ -82,10 +86,11 @@ public final class LogProtoUtils {
         : 
entries.stream().map(LogProtoUtils::toLogEntryString).collect(Collectors.toList()).toString();
   }
 
-  public static String toLogEntriesShortString(List<LogEntryProto> entries) {
+  public static String toLogEntriesShortString(List<LogEntryProto> entries,
+      Function<StateMachineLogEntryProto, String> stateMachineToString) {
     return entries == null ? null
         : entries.isEmpty()? "<empty>"
-        : "size=" + entries.size() + ", first=" + 
LogProtoUtils.toLogEntryString(entries.get(0));
+        : "size=" + entries.size() + ", first=" + 
toLogEntryString(entries.get(0), stateMachineToString);
   }
 
   public static LogEntryProto toLogEntryProto(RaftConfiguration conf, Long 
term, long index) {
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/RaftLogBase.java 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/RaftLogBase.java
index a41d90b1a..3b9724cc6 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/RaftLogBase.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/RaftLogBase.java
@@ -199,10 +199,10 @@ public abstract class RaftLogBase implements RaftLog {
 
       appendEntry(operation.wrap(e), operation).whenComplete((returned, t) -> {
         if (t != null) {
-          LOG.error(name + ": Failed to write log entry " + 
LogProtoUtils.toLogEntryString(e), t);
+          LOG.error(name + ": Failed to write log entry " + 
toLogEntryString(e), t);
         } else if (returned != nextIndex) {
           LOG.error("{}: Indices mismatched: returned index={} but 
nextIndex={} for log entry {}",
-              name, returned, nextIndex, LogProtoUtils.toLogEntryString(e));
+              name, returned, nextIndex, toLogEntryString(e));
         } else {
           return; // no error
         }
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLog.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLog.java
index a3c18f01c..7c33adf00 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLog.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLog.java
@@ -345,8 +345,7 @@ public final class SegmentedRaftLog extends RaftLogBase {
       }
       return future != null? newEntryWithData(entryRef, future): 
newEntryWithData(entryRef);
     } catch (Exception e) {
-      final String err = getName() + ": Failed readStateMachineData for " +
-          LogProtoUtils.toLogEntryString(entry);
+      final String err = getName() + ": Failed readStateMachineData for " + 
toLogEntryString(entry);
       LOG.error(err, e);
       throw new RaftLogIOException(err, 
JavaUtils.unwrapCompletionException(e));
     }
@@ -467,7 +466,7 @@ public final class SegmentedRaftLog extends RaftLogBase {
       }
       return write.getFuture().whenComplete((clientReply, exception) -> 
appendEntryTimerContext.stop());
     } catch (Exception e) {
-      LOG.error("{}: Failed to append {}", getName(), 
LogProtoUtils.toLogEntryString(entry), e);
+      LOG.error("{}: Failed to append {}", getName(), toLogEntryString(entry), 
e);
       throw e;
     } finally {
       entryRef.release();
@@ -578,7 +577,8 @@ public final class SegmentedRaftLog extends RaftLogBase {
 
   @Override
   public String toLogEntryString(LogEntryProto logEntry) {
-    return LogProtoUtils.toLogEntryString(logEntry, 
stateMachine::toStateMachineLogEntryString);
+    return LogProtoUtils.toLogEntryString(logEntry, stateMachine != null ?
+        stateMachine::toStateMachineLogEntryString : null);
   }
 
   public static Builder newBuilder() {
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/util/ServerStringUtils.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/util/ServerStringUtils.java
index 284664d01..6e0fce4d6 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/util/ServerStringUtils.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/util/ServerStringUtils.java
@@ -23,11 +23,13 @@ import 
org.apache.ratis.proto.RaftProtos.InstallSnapshotReplyProto;
 import org.apache.ratis.proto.RaftProtos.InstallSnapshotRequestProto;
 import org.apache.ratis.proto.RaftProtos.LogEntryProto;
 import org.apache.ratis.proto.RaftProtos.RequestVoteReplyProto;
+import org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto;
 import org.apache.ratis.server.protocol.TermIndex;
 import org.apache.ratis.server.raftlog.LogProtoUtils;
 import org.apache.ratis.util.ProtoUtils;
 
 import java.util.List;
+import java.util.function.Function;
 
 /**
  *  This class provides convenient utilities for converting Protocol Buffers 
messages to strings.
@@ -41,7 +43,8 @@ import java.util.List;
 public final class ServerStringUtils {
   private ServerStringUtils() {}
 
-  public static String toAppendEntriesRequestString(AppendEntriesRequestProto 
request) {
+  public static String toAppendEntriesRequestString(AppendEntriesRequestProto 
request,
+      Function<StateMachineLogEntryProto, String> stateMachineToString) {
     if (request == null) {
       return null;
     }
@@ -51,7 +54,8 @@ public final class ServerStringUtils {
         + ",previous=" + TermIndex.valueOf(request.getPreviousLog())
         + ",leaderCommit=" + request.getLeaderCommit()
         + ",initializing? " + request.getInitializing()
-        + "," + (entries.isEmpty()? "HEARTBEAT" : "entries: " + 
LogProtoUtils.toLogEntriesShortString(entries));
+        + "," + (entries.isEmpty()? "HEARTBEAT" : "entries: " +
+        LogProtoUtils.toLogEntriesShortString(entries, stateMachineToString));
   }
 
   public static String toAppendEntriesReplyString(AppendEntriesReplyProto 
reply) {

Reply via email to