From f9f030db8f3f64cad0f4f7bb9dc3bec6416f8dbc Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Mon, 28 Feb 2011 22:49:29 -0800 Subject: [PATCH] thread dump now reports all the threads from all the slaves, not just the master. --- changelog.html | 3 +- core/src/main/java/hudson/model/Hudson.java | 33 +++++++++++++++++++ .../java/hudson/util/RemotingDiagnostics.java | 8 +++++ .../hudson/model/Hudson/threadDump.jelly | 14 +++++--- .../java/hudson/remoting/AsyncFutureImpl.java | 10 +++++- 5 files changed, 62 insertions(+), 6 deletions(-) diff --git a/changelog.html b/changelog.html index 6477983581..5203618296 100644 --- a/changelog.html +++ b/changelog.html @@ -58,7 +58,8 @@ Upcoming changes diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index fc5bcf105b..3975a337bf 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -189,6 +189,7 @@ import java.io.FileFilter; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; +import java.io.StringWriter; import java.net.BindException; import java.net.URL; import java.nio.charset.Charset; @@ -2631,6 +2632,38 @@ public final class Hudson extends Node implements ItemGroup, Stapl rsp.sendRedirect2("threadDump"); } + /** + * Obtains the thread dump of all slaves (including the master.) + * + *

+ * Since this is for diagnostics, it has a built-in precautionary measure against hang slaves. + */ + public Map> getAllThreadDumps() throws IOException, InterruptedException { + checkPermission(ADMINISTER); + + // issue the requests all at once + Map>> future = new HashMap>>(); + for (Computer c : getComputers()) { + future.put(c.getName(), RemotingDiagnostics.getThreadDumpAsync(c.getChannel())); + } + + // if the result isn't available in 5 sec, ignore that. + // this is a precaution against hang nodes + long endTime = System.currentTimeMillis() + 5000; + + Map> r = new HashMap>(); + for (Entry>> e : future.entrySet()) { + try { + r.put(e.getKey(), e.getValue().get(endTime-System.currentTimeMillis(), TimeUnit.MILLISECONDS)); + } catch (Exception x) { + StringWriter sw = new StringWriter(); + x.printStackTrace(new PrintWriter(sw,true)); + r.put(e.getKey(), Collections.singletonMap("Failed to retrieve thread dump",sw.toString())); + } + } + return r; + } + public synchronized Item doCreateItem( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException { return itemGroupMixIn.createTopLevelItem(req, rsp); } diff --git a/core/src/main/java/hudson/util/RemotingDiagnostics.java b/core/src/main/java/hudson/util/RemotingDiagnostics.java index 326d1c7438..cc86c37bb9 100644 --- a/core/src/main/java/hudson/util/RemotingDiagnostics.java +++ b/core/src/main/java/hudson/util/RemotingDiagnostics.java @@ -27,8 +27,10 @@ import groovy.lang.GroovyShell; import hudson.FilePath; import hudson.Functions; import hudson.model.Hudson; +import hudson.remoting.AsyncFutureImpl; import hudson.remoting.Callable; import hudson.remoting.DelegatingCallable; +import hudson.remoting.Future; import hudson.remoting.VirtualChannel; import hudson.security.AccessControlled; import org.kohsuke.stapler.StaplerRequest; @@ -78,6 +80,12 @@ public final class RemotingDiagnostics { return channel.call(new GetThreadDump()); } + public static Future> getThreadDumpAsync(VirtualChannel channel) throws IOException, InterruptedException { + if(channel==null) + return new AsyncFutureImpl>(Collections.singletonMap("N/A","offline")); + return channel.callAsync(new GetThreadDump()); + } + private static final class GetThreadDump implements Callable,RuntimeException> { public Map call() { Map r = new LinkedHashMap(); diff --git a/core/src/main/resources/hudson/model/Hudson/threadDump.jelly b/core/src/main/resources/hudson/model/Hudson/threadDump.jelly index 6ba299a3ea..788ee47859 100644 --- a/core/src/main/resources/hudson/model/Hudson/threadDump.jelly +++ b/core/src/main/resources/hudson/model/Hudson/threadDump.jelly @@ -1,7 +1,7 @@