From 39dee8ea19172ad09cdc0f130545d6e6f8f7a67c Mon Sep 17 00:00:00 2001 From: zhihui wang <146961954+hiwangzhihui@users.noreply.github.com> Date: Wed, 15 May 2024 03:03:49 +0800 Subject: [PATCH] HADOOP-18958. Improve UserGroupInformation debug log. (#6255) Contributed by zhihui wang --- .../hadoop/security/UserGroupInformation.java | 19 +++++++++++-------- .../org/apache/hadoop/util/StringUtils.java | 13 +++++++++++++ .../apache/hadoop/util/TestStringUtils.java | 9 +++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/UserGroupInformation.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/UserGroupInformation.java index 07be1f8e54..305e5e10af 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/UserGroupInformation.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/UserGroupInformation.java @@ -88,6 +88,7 @@ import org.apache.hadoop.security.token.Token; import org.apache.hadoop.security.token.TokenIdentifier; import org.apache.hadoop.util.Shell; +import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.Time; import org.slf4j.Logger; @@ -1934,10 +1935,7 @@ protected Subject getSubject() { @InterfaceAudience.Public @InterfaceStability.Evolving public T doAs(PrivilegedAction action) { - if (LOG.isDebugEnabled()) { - LOG.debug("PrivilegedAction [as: {}][action: {}]", this, action, - new Exception()); - } + tracePrivilegedAction(action); return Subject.doAs(subject, action); } @@ -1957,10 +1955,7 @@ public T doAs(PrivilegedAction action) { public T doAs(PrivilegedExceptionAction action ) throws IOException, InterruptedException { try { - if (LOG.isDebugEnabled()) { - LOG.debug("PrivilegedAction [as: {}][action: {}]", this, action, - new Exception()); - } + tracePrivilegedAction(action); return Subject.doAs(subject, action); } catch (PrivilegedActionException pae) { Throwable cause = pae.getCause(); @@ -1982,6 +1977,14 @@ public T doAs(PrivilegedExceptionAction action } } + private void tracePrivilegedAction(Object action) { + if (LOG.isTraceEnabled()) { + // would be nice if action included a descriptive toString() + LOG.trace("PrivilegedAction [as: {}][action: {}][from: {}]", this, action, + StringUtils.getStackTrace(new Throwable())); + } + } + /** * Log current UGI and token information into specified log. * @param ugi - UGI diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java index 2585729950..b109d8bacb 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java @@ -1148,6 +1148,19 @@ public static String getStackTrace(Thread t) { return str.toString(); } + /** + * Get stack trace from throwable exception. + * @param t Throwable. + * @return stack trace string. + */ + public static String getStackTrace(Throwable t) { + StringBuilder str = new StringBuilder(); + for (StackTraceElement e : t.getStackTrace()) { + str.append(e.toString() + "\n\t"); + } + return str.toString(); + } + /** * From a list of command-line arguments, remove both an option and the * next argument. diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java index c9b42b07f4..e6dcc5e8de 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java @@ -624,6 +624,15 @@ public void testStringCollectionSplitByEqualsFailure() throws Exception { () -> StringUtils.getTrimmedStringCollectionSplitByEquals(",=")); } + @Test + public void testForGetStackTrace() { + Throwable throwable = new Throwable(); + int stackLength = throwable.getStackTrace().length; + String stackTrace = StringUtils.getStackTrace(new Throwable()); + String[] splitTrace = stackTrace.split("\n\t"); + assertEquals(stackLength, splitTrace.length); + } + // Benchmark for StringUtils split public static void main(String []args) { final String TO_SPLIT = "foo,bar,baz,blah,blah";