pan3793 commented on code in PR #8987:
URL: https://github.com/apache/hadoop/pull/8987#discussion_r4611015078


##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Server.java:
##########
@@ -521,6 +522,10 @@ protected ResponseBuffer initialValue() {
   private volatile boolean logSlowRPC = false;
   /** Threshold time for log slow rpc. */
   private volatile long logSlowRPCThresholdTime;
+  /** Whether the caller context is printed in the slow rpc log. */
+  private final boolean callerContextEnabled;

Review Comment:
   `hadoop.caller.context.enabled` is reconfigurable on the NameNode, but 
`NameNode#reconfCallerContextEnabled` only updates the audit logger, so after a 
reconfig the slow RPC log disagrees with the audit log until restart. Please 
make this `volatile` with a setter, and call it from 
`reconfCallerContextEnabled` on the client/service/lifeline RPC servers, like 
`reconfigureLogSlowRPC` does with `setLogSlowRPC`.



##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Server.java:
##########
@@ -609,13 +614,31 @@ void logSlowRpcCalls(String methodName, Call call,
     if ((rpcMetrics.getProcessingSampleCount() > minSampleSize) &&
         (processingTime > threeSigma) &&
         (processingTime > getLogSlowRPCThresholdTime())) {
-      LOG.warn("Slow RPC : {} took {} {} to process from client {}, the 
processing detail is {}," +
+      LOG.warn("Slow RPC : {} took {} {} to process from client {}, user {}," +
+              " callerContext {}, the processing detail is {}," +
               " and the threshold time is {} {}.", methodName, processingTime, 
metricsTimeUnit,
-          call, details.toString(), getLogSlowRPCThresholdTime(), 
metricsTimeUnit);
+          call, call.getRemoteUser(), getCallerContextForLog(call), 
details.toString(),
+          getLogSlowRPCThresholdTime(), metricsTimeUnit);

Review Comment:
   1. Both audit logs (`FSNamesystem`, `RMAuditLogger#appendCallerContext`) 
omit the caller context field when there is none, while here the default config 
prints `callerContext null` on every slow RPC line of every server.
   2. Appending the new fields after the existing ones keeps the current prefix 
intact for log parsers, and keeps the client-supplied context, which may 
contain `, `, from shifting the fixed fields.
   
   ```suggestion
         LOG.warn("Slow RPC : {} took {} {} to process from client {}, the 
processing detail is {}," +
                 " and the threshold time is {} {}, user {}{}.", methodName, 
processingTime,
             metricsTimeUnit, call, details.toString(), 
getLogSlowRPCThresholdTime(),
             metricsTimeUnit, call.getRemoteUser(), 
getCallerContextForLog(call));
   ```



##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Server.java:
##########
@@ -609,13 +614,31 @@ void logSlowRpcCalls(String methodName, Call call,
     if ((rpcMetrics.getProcessingSampleCount() > minSampleSize) &&
         (processingTime > threeSigma) &&
         (processingTime > getLogSlowRPCThresholdTime())) {
-      LOG.warn("Slow RPC : {} took {} {} to process from client {}, the 
processing detail is {}," +
+      LOG.warn("Slow RPC : {} took {} {} to process from client {}, user {}," +
+              " callerContext {}, the processing detail is {}," +
               " and the threshold time is {} {}.", methodName, processingTime, 
metricsTimeUnit,
-          call, details.toString(), getLogSlowRPCThresholdTime(), 
metricsTimeUnit);
+          call, call.getRemoteUser(), getCallerContextForLog(call), 
details.toString(),
+          getLogSlowRPCThresholdTime(), metricsTimeUnit);
       rpcMetrics.incrSlowRpc();
     }
   }
 
+  /**
+   * Format the caller context in the same way as the HDFS audit log: only
+   * when hadoop.caller.context.enabled is set, escaped and truncated to
+   * hadoop.caller.context.max.size. The signature is not logged.
+   */
+  private String getCallerContextForLog(Call call) {
+    CallerContext callerContext = call.getCallerContext();
+    if (!callerContextEnabled || callerContext == null ||
+        !callerContext.isContextValid()) {
+      return null;
+    }
+    String context = escapeJava(callerContext.getContext());
+    return context.length() > callerContextMaxLen ?
+        context.substring(0, callerContextMaxLen) : context;
+  }

Review Comment:
   1. Return the whole `, callerContext ...` suffix, empty when absent, to go 
with the log line change above.
   2. The received context is bounded only by `ipc.maximum.data.length`, but 
only `callerContextMaxLen` chars are kept. Escaping never shortens a string, so 
escaping just the prefix gives identical output.
   
   ```suggestion
     /**
      * Format the caller context as a log suffix in the same way as the HDFS
      * audit log: only when hadoop.caller.context.enabled is set, escaped and
      * truncated to hadoop.caller.context.max.size. The signature is not 
logged.
      */
     private String getCallerContextForLog(Call call) {
       CallerContext callerContext = call.getCallerContext();
       if (!callerContextEnabled || callerContext == null ||
           !callerContext.isContextValid()) {
         return "";
       }
       // Escaping never shortens, so escaping only the prefix gives the same 
result.
       String raw = callerContext.getContext();
       String context = escapeJava(raw.substring(0, Math.min(raw.length(), 
callerContextMaxLen)));
       return ", callerContext " + (context.length() > callerContextMaxLen ?
           context.substring(0, callerContextMaxLen) : context);
     }
   ```



##########
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestProtoBufRpc.java:
##########
@@ -395,11 +398,29 @@ public void testLogSlowRPC(boolean pTestWithLegacy,
     assertThat(before).isEqualTo(after);
 
     // Make a really slow call. Sleep sleeps for 3000ms.
-    client.sleep(null, newSleepRequest(SLEEP_DURATION * 3));
-
-    // Ensure slow call is logged.
-    GenericTestUtils.waitFor(()
-        -> rpcMetrics.getRpcSlowCalls() == before + 1L, 10, 1000);
+    // The context is escaped and truncated.
+    String longContext = "slowRpc\nContext\r\t_" + StringUtils.repeat("X", 
200);
+    String expectedContext = ("slowRpc\\nContext\\r\\t_" + 
StringUtils.repeat("X", 200))
+        .substring(0, 
CommonConfigurationKeys.HADOOP_CALLER_CONTEXT_MAX_SIZE_DEFAULT);
+    GenericTestUtils.LogCapturer logs =
+        GenericTestUtils.LogCapturer.captureLogs(Server.LOG);
+    CallerContext.setCurrent(new CallerContext.Builder(longContext)
+        .setSignature("sig".getBytes(StandardCharsets.UTF_8)).build());
+    try {
+      client.sleep(null, newSleepRequest(SLEEP_DURATION * 3));
+
+      // Ensure slow call is logged.
+      GenericTestUtils.waitFor(()
+          -> rpcMetrics.getRpcSlowCalls() == before + 1L, 10, 1000);
+      // The log carries the user and the truncated caller context.
+      assertThat(logs.getOutput())
+          .contains("Slow RPC : sleep")
+          .contains("user " + 
UserGroupInformation.getCurrentUser().getShortUserName())
+          .contains("callerContext " + expectedContext + ",");

Review Comment:
   The server logs `UGI.toString()`, so pin the full value. The trailing `.` 
follows the log line change above.
   
   ```suggestion
             .contains("user " + 
UserGroupInformation.getCurrentUser().getUserName() + " (auth:SIMPLE)")
             .contains("callerContext " + expectedContext + ".");
   ```



##########
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestProtoBufRpc.java:
##########
@@ -163,6 +165,7 @@ public void setUp() throws IOException { // Setup server 
for both protocols
     conf = new Configuration();
     conf.setInt(CommonConfigurationKeys.IPC_MAXIMUM_DATA_LENGTH, 1024);
     conf.setBoolean(CommonConfigurationKeys.IPC_SERVER_LOG_SLOW_RPC, true);
+    conf.setBoolean(CommonConfigurationKeys.HADOOP_CALLER_CONTEXT_ENABLED_KEY, 
true);

Review Comment:
   This enables the flag for every test in the class, so the default (disabled) 
path in `getCallerContextForLog` is untested. Please add a case with the flag 
off that asserts the slow RPC line has no `callerContext`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to