From ad9bcb9e5a81ac9a080568ac4836b865aaffcd57 Mon Sep 17 00:00:00 2001 From: Todd Lipcon Date: Tue, 9 Oct 2012 00:33:57 +0000 Subject: [PATCH] HADOOP-8894. GenericTestUtils.waitFor should dump thread stacks on timeout. Contributed by Todd Lipcon. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1395825 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 ++ .../apache/hadoop/test/GenericTestUtils.java | 5 ++- .../hadoop/test/TimedOutTestsListener.java | 31 ++++++++++++------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 86deddeb5f..ecf895f1cb 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -297,6 +297,9 @@ Release 2.0.3-alpha - Unreleased HADOOP-8804. Improve Web UIs when the wildcard address is used. (Senthil Kumar via eli) + HADOOP-8894. GenericTestUtils.waitFor should dump thread stacks on timeout + (todd) + OPTIMIZATIONS HADOOP-8866. SampleQuantiles#query is O(N^2) instead of O(N). (Andrew Wang diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java index 806df54718..2a361d9192 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java @@ -104,7 +104,10 @@ public static void waitFor(Supplier check, Thread.sleep(checkEveryMillis); } while (Time.now() - st < waitForMillis); - throw new TimeoutException("Timed out waiting for condition"); + + throw new TimeoutException("Timed out waiting for condition. " + + "Thread diagnostics:\n" + + TimedOutTestsListener.buildThreadDiagnosticString()); } public static class LogCapturer { diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TimedOutTestsListener.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TimedOutTestsListener.java index a10edbbd27..220ab1daea 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TimedOutTestsListener.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TimedOutTestsListener.java @@ -58,19 +58,28 @@ public void testFailure(Failure failure) throws Exception { && failure.getMessage().startsWith(TEST_TIMED_OUT_PREFIX)) { output.println("====> TEST TIMED OUT. PRINTING THREAD DUMP. <===="); output.println(); - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS"); - output.println(String.format("Timestamp: %s", dateFormat.format(new Date()))); - output.println(); - output.println(buildThreadDump()); - - String deadlocksInfo = buildDeadlockInfo(); - if (deadlocksInfo != null) { - output.println("====> DEADLOCKS DETECTED <===="); - output.println(); - output.println(deadlocksInfo); - } + output.print(buildThreadDiagnosticString()); } } + + public static String buildThreadDiagnosticString() { + StringWriter sw = new StringWriter(); + PrintWriter output = new PrintWriter(sw); + + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS"); + output.println(String.format("Timestamp: %s", dateFormat.format(new Date()))); + output.println(); + output.println(buildThreadDump()); + + String deadlocksInfo = buildDeadlockInfo(); + if (deadlocksInfo != null) { + output.println("====> DEADLOCKS DETECTED <===="); + output.println(); + output.println(deadlocksInfo); + } + + return sw.toString(); + } static String buildThreadDump() { StringBuilder dump = new StringBuilder();