Skip to content

Commit 0900589

Browse files
committed
HADOOP-18958. UserGroupInformation debug log improve.
1 parent 2e712cc commit 0900589

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/UserGroupInformation.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import org.apache.hadoop.security.token.Token;
8989
import org.apache.hadoop.security.token.TokenIdentifier;
9090
import org.apache.hadoop.util.Shell;
91+
import org.apache.hadoop.util.StringUtils;
9192
import org.apache.hadoop.util.Time;
9293

9394
import org.slf4j.Logger;
@@ -1934,10 +1935,7 @@ protected Subject getSubject() {
19341935
@InterfaceAudience.Public
19351936
@InterfaceStability.Evolving
19361937
public <T> T doAs(PrivilegedAction<T> action) {
1937-
if (LOG.isDebugEnabled()) {
1938-
LOG.debug("PrivilegedAction [as: {}][action: {}][from: {}]", this, action,
1939-
Thread.currentThread().getStackTrace()[2]);
1940-
}
1938+
tracePrivilegedAction(action);
19411939
return Subject.doAs(subject, action);
19421940
}
19431941

@@ -1957,10 +1955,7 @@ public <T> T doAs(PrivilegedAction<T> action) {
19571955
public <T> T doAs(PrivilegedExceptionAction<T> action
19581956
) throws IOException, InterruptedException {
19591957
try {
1960-
if (LOG.isDebugEnabled()) {
1961-
LOG.debug("PrivilegedAction [as: {}][action: {}][from: {}]", this, action,
1962-
Thread.currentThread().getStackTrace()[2]);;
1963-
}
1958+
tracePrivilegedAction(action);
19641959
return Subject.doAs(subject, action);
19651960
} catch (PrivilegedActionException pae) {
19661961
Throwable cause = pae.getCause();
@@ -1982,6 +1977,14 @@ public <T> T doAs(PrivilegedExceptionAction<T> action
19821977
}
19831978
}
19841979

1980+
private void tracePrivilegedAction(Object action) {
1981+
if (LOG.isTraceEnabled()) {
1982+
// would be nice if action included a descriptive toString()
1983+
LOG.trace("PrivilegedAction [as: {}][action: {}][from: {}]", this, action,
1984+
StringUtils.getStackTrace(new Throwable()));
1985+
}
1986+
}
1987+
19851988
/**
19861989
* Log current UGI and token information into specified log.
19871990
* @param ugi - UGI

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,19 @@ public static String getStackTrace(Thread t) {
11481148
return str.toString();
11491149
}
11501150

1151+
/**
1152+
* Get stack trace from throwable exception.
1153+
* @param t Throwable.
1154+
* @return stack trace string.
1155+
*/
1156+
public static String getStackTrace(Throwable t) {
1157+
StringBuilder str = new StringBuilder();
1158+
for (StackTraceElement e : t.getStackTrace()) {
1159+
str.append(e.toString() + "\n\t");
1160+
}
1161+
return str.toString();
1162+
}
1163+
11511164
/**
11521165
* From a list of command-line arguments, remove both an option and the
11531166
* next argument.

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,15 @@ public void testStringCollectionSplitByEqualsFailure() throws Exception {
624624
() -> StringUtils.getTrimmedStringCollectionSplitByEquals(",="));
625625
}
626626

627+
@Test
628+
public void testForGetStackTrace() {
629+
Throwable throwable = new Throwable();
630+
int stackLen = throwable.getStackTrace().length;
631+
String stackTrace = StringUtils.getStackTrace(new Throwable());
632+
String[] splitTrace = stackTrace.split("\n\t");
633+
assertEquals(stackLen, splitTrace.length);
634+
}
635+
627636
// Benchmark for StringUtils split
628637
public static void main(String []args) {
629638
final String TO_SPLIT = "foo,bar,baz,blah,blah";

0 commit comments

Comments
 (0)