tillrohrmann commented on a change in pull request #18158: URL: https://github.com/apache/flink/pull/18158#discussion_r775836504
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/rest/messages/taskmanager/ThreadDumpInfo.java ########## @@ -106,5 +112,87 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(threadName, stringifiedThreadInfo); } + + /** + * Custom stringify format of JVM thread info to bypass the MAX_FRAMES = 8 limitation. + * + * <p>This method is based on + * https://github.com/openjdk/jdk/blob/master/src/java.management/share/classes/java/lang/management/ThreadInfo.java#L597 + */ + @VisibleForTesting + protected static String stringifyThreadInfo( + java.lang.management.ThreadInfo threadInfo, int maxDepth) { + StringBuilder sb = + new StringBuilder( + "\"" + + threadInfo.getThreadName() + + "\"" + + " Id=" + + threadInfo.getThreadId() + + " " + + threadInfo.getThreadState()); + if (threadInfo.getLockName() != null) { + sb.append(" on " + threadInfo.getLockName()); + } + if (threadInfo.getLockOwnerName() != null) { + sb.append( + " owned by \"" + + threadInfo.getLockOwnerName() + + "\" Id=" + + threadInfo.getLockOwnerId()); + } + if (threadInfo.isSuspended()) { + sb.append(" (suspended)"); + } + if (threadInfo.isInNative()) { + sb.append(" (in native)"); + } + sb.append('\n'); + int i = 0; + StackTraceElement[] stackTraceElements = threadInfo.getStackTrace(); + for (; i < stackTraceElements.length && i < maxDepth; i++) { + StackTraceElement ste = stackTraceElements[i]; + sb.append("\tat " + ste.toString()); + sb.append('\n'); + if (i == 0 && threadInfo.getLockInfo() != null) { + Thread.State ts = threadInfo.getThreadState(); + switch (ts) { + case BLOCKED: + sb.append("\t- blocked on " + threadInfo.getLockInfo()); + sb.append('\n'); + break; + case WAITING: + case TIMED_WAITING: + sb.append("\t- waiting on " + threadInfo.getLockInfo()); + sb.append('\n'); + break; + default: + } + } + + for (MonitorInfo mi : threadInfo.getLockedMonitors()) { + if (mi.getLockedStackDepth() == i) { + sb.append("\t- locked " + mi); + sb.append('\n'); + } + } + } + if (i < threadInfo.getStackTrace().length) { + sb.append("\t..."); + sb.append('\n'); + } + + LockInfo[] locks = threadInfo.getLockedSynchronizers(); + if (locks.length > 0) { + sb.append("\n\tNumber of locked synchronizers = " + locks.length); + sb.append('\n'); + for (LockInfo li : locks) { + sb.append("\t- " + li); + sb.append('\n'); + } + } + sb.append('\n'); + return sb.toString(); Review comment: This is nice. Shall we also add a test that ensures that `stringifyThreadInfo` produces the same result as `ThreadInfo.toString` when the depth is set to 8? That way we get notified whenever something changes on the Java side. ########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/rest/messages/taskmanager/ThreadDumpInfoTest.java ########## @@ -20,13 +20,27 @@ import org.apache.flink.runtime.rest.messages.RestResponseMarshallingTestBase; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.lang.management.LockInfo; +import java.lang.management.MonitorInfo; +import java.lang.management.ThreadInfo; import java.util.Arrays; import java.util.Collection; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.when; Review comment: Maybe we don't have to check for the exact content but for the number of entries. We could also exploit the fact that we know in which test method we are. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org