Skip to content

Commit

Permalink
HADOOP-18958. UserGroupInformation debug log improve.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiwangzhihui committed May 6, 2024
1 parent 2e712cc commit ac6b742
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1934,10 +1935,7 @@ protected Subject getSubject() {
@InterfaceAudience.Public
@InterfaceStability.Evolving
public <T> T doAs(PrivilegedAction<T> action) {
if (LOG.isDebugEnabled()) {
LOG.debug("PrivilegedAction [as: {}][action: {}][from: {}]", this, action,
Thread.currentThread().getStackTrace()[2]);
}
tracePrivilegedAction(action);
return Subject.doAs(subject, action);
}

Expand All @@ -1957,10 +1955,7 @@ public <T> T doAs(PrivilegedAction<T> action) {
public <T> T doAs(PrivilegedExceptionAction<T> 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();
Expand All @@ -1982,6 +1977,14 @@ public <T> T doAs(PrivilegedExceptionAction<T> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check failure on line 1154 in hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java

View check run for this annotation

ASF Cloudbees Jenkins ci-hadoop / Apache Yetus

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

javadoc: warning: no @param for t

Check failure on line 1154 in hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java

View check run for this annotation

ASF Cloudbees Jenkins ci-hadoop / Apache Yetus

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

javadoc: warning: no @return
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit ac6b742

Please sign in to comment.