Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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