Skip to content

Commit

Permalink
HADOOP-18958. Improve UserGroupInformation debug log. (#6255)
Browse files Browse the repository at this point in the history

Contributed by zhihui wang
  • Loading branch information
hiwangzhihui authored May 14, 2024
1 parent bda7045 commit 39dee8e
Show file tree
Hide file tree
Showing 3 changed files with 33 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: {}]", this, action,
new Exception());
}
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: {}]", this, action,
new Exception());
}
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,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.
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 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";
Expand Down

0 comments on commit 39dee8e

Please sign in to comment.