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 ffeb55de32530b..305e5e10af3057 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: {}][from: {}]", this, action, - Thread.currentThread().getStackTrace()[2]); - } + 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: {}][from: {}]", this, action, - Thread.currentThread().getStackTrace()[2]);; - } + 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 2585729950b553..ccb0991dcdf3ac 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,17 @@ public static String getStackTrace(Thread t) { return str.toString(); } + /** + * Get stack trace from throwable exception. + */ + 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 c9b42b07f4c951..a139a526977472 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 stackLen = throwable.getStackTrace().length; + String stackTrace = StringUtils.getStackTrace(new Throwable()); + String[] splitTrace = stackTrace.split("\n\t"); + assertEquals(stackLen, splitTrace.length); + } + // Benchmark for StringUtils split public static void main(String []args) { final String TO_SPLIT = "foo,bar,baz,blah,blah";