Skip to content

Commit

Permalink
[FIXED JENKINS-22249] Rewrote ProcessLiveness to use a simple [ -d /p…
Browse files Browse the repository at this point in the history
…roc/$pid ] check.
  • Loading branch information
jglick committed Oct 10, 2014
1 parent bb56c1c commit d75ffad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 54 deletions.
Expand Up @@ -110,7 +110,6 @@ private synchronized int pid(FilePath ws) throws IOException, InterruptedExcepti
if (pidFile.exists()) {
try {
pid = Integer.parseInt(pidFile.readToString().trim());
ProcessLiveness.reset(ws.getChannel());
} catch (NumberFormatException x) {
throw new IOException("corrupted content in " + pidFile + ": " + x, x);
}
Expand Down
Expand Up @@ -26,75 +26,36 @@

import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
import hudson.util.ProcessTree;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Utility class to track whether a given process is still alive.
* Since loading a complete {@link ProcessTree} may be expensive, this is done only once per {@link #CACHE_EXPIRY}.
* Might be more efficient and reliable to use JNA to look up this information with a direct system call,
* but this would be a longer-term project with more platform-specific code.
*/
final class ProcessLiveness {

/** By default, one minute. */
private static final long CACHE_EXPIRY = TimeUnit.MINUTES.toMillis(1);

private static final Logger LOGGER = Logger.getLogger(ProcessLiveness.class.getName());

private static final class ProcessTreeCache {
ProcessTree tree;
long lastChecked;
ProcessTreeCache() {}
}

private static final Map<VirtualChannel,ProcessTreeCache> processTrees = new WeakHashMap<VirtualChannel,ProcessTreeCache>();

/**
* Determines whether a process is believed to still be alive.
* @param channel a connection to the machine on which it would be running
* @param pid a process ID
* @return true if it is probably still alive (but might have recently died); false if it is believed to not be running
* @return true if it is apparently still alive (or we cannot tell); false if it is believed to not be running
*/
public static boolean isAlive(VirtualChannel channel, int pid) throws IOException, InterruptedException {
ProcessTreeCache cache;
synchronized (processTrees) {
cache = processTrees.get(channel);
if (cache == null) {
cache = new ProcessTreeCache();
processTrees.put(channel, cache);
}
}
long now = System.currentTimeMillis();
synchronized (cache) {
if (cache.tree == null || now - cache.lastChecked > CACHE_EXPIRY) {
LOGGER.log(Level.FINE, "(re)loading process tree on {0}", channel);
cache.tree = channel.call(new LoadProcessTree());
cache.lastChecked = now;
}
return cache.tree.get(pid) != null;
}
return channel.call(new Liveness(pid));
}

private static final class LoadProcessTree implements Callable<ProcessTree,RuntimeException> {
@Override public ProcessTree call() throws RuntimeException {
return ProcessTree.get();
private static final class Liveness implements Callable<Boolean,RuntimeException> {
private final int pid;
Liveness(int pid) {
this.pid = pid;
}
}

/**
* Clears any cache for a given machine.
* Should be done when a new process has been started.
* @param channel a connection to a machine
*/
public static void reset(VirtualChannel channel) {
synchronized (processTrees) {
processTrees.remove(channel);
@Override public Boolean call() throws RuntimeException {
File proc = new File("/proc");
if (!proc.isDirectory()) {
// procfs not in use here? Give up.
return true;
}
return new File(proc, Integer.toString(pid)).isDirectory();
}
}

Expand Down

0 comments on commit d75ffad

Please sign in to comment.