Skip to content

Commit

Permalink
[FIXED JENKINS-42322] Increased default timeout for docker client ope…
Browse files Browse the repository at this point in the history
…rations from 10s to 3m, and making it configurable.
  • Loading branch information
jglick committed Jun 16, 2017
1 parent b627d39 commit 8c18b06
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private static class Decorator extends LauncherDecorator implements Serializable
@Override public void kill(Map<String,String> modelEnvVars) throws IOException, InterruptedException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String executable = getExecutable();
if (getInner().launch().cmds(executable, "exec", container, "ps", "-A", "-o", "pid,command", "e").stdout(baos).quiet(true).start().joinWithTimeout(10, TimeUnit.SECONDS, listener) != 0) {
if (getInner().launch().cmds(executable, "exec", container, "ps", "-A", "-o", "pid,command", "e").stdout(baos).quiet(true).start().joinWithTimeout(DockerClient.CLIENT_TIMEOUT, TimeUnit.SECONDS, listener) != 0) {
throw new IOException("failed to run ps");
}
List<String> pids = new ArrayList<String>();
Expand All @@ -270,7 +270,7 @@ private static class Decorator extends LauncherDecorator implements Serializable
if (!pids.isEmpty()) {
List<String> cmds = new ArrayList<>(Arrays.asList(executable, "exec", container, "kill"));
cmds.addAll(pids);
if (getInner().launch().cmds(cmds).quiet(true).start().joinWithTimeout(10, TimeUnit.SECONDS, listener) != 0) {
if (getInner().launch().cmds(cmds).quiet(true).start().joinWithTimeout(DockerClient.CLIENT_TIMEOUT, TimeUnit.SECONDS, listener) != 0) {
throw new IOException("failed to run kill");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* Simple docker client for Pipeline.
Expand All @@ -61,6 +63,12 @@ public class DockerClient {

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

/**
* Maximum amount of time (in seconds) to wait for {@code docker} client operations which are supposed to be more or less instantaneous.
*/
@Restricted(NoExternalUse.class)
public static int CLIENT_TIMEOUT = Integer.getInteger(DockerClient.class.getName() + ".CLIENT_TIMEOUT", 180); // TODO 2.4+ SystemProperties

// e.g. 2015-04-09T13:40:21.981801679Z
public static final String DOCKER_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

Expand All @@ -74,7 +82,7 @@ public class DockerClient {
*/
public static final String CGROUP_MATCHER_PATTERN = "(?m)^\\d+:[\\w,?]+:(?:/[\\w.]+)?(?:/docker[-/])(/?(?:docker/)?(?<containerId>\\p{XDigit}{12,}))+(?:\\.scope)?$";

private Launcher launcher;
private final Launcher launcher;
private final @CheckForNull Node node;
private final @CheckForNull String toolName;

Expand Down Expand Up @@ -266,7 +274,7 @@ private LaunchResult launch(@CheckForNull @Nonnull EnvVars launchEnv, boolean qu
LaunchResult result = new LaunchResult();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
result.setStatus(procStarter.quiet(quiet).cmds(args).envs(launchEnv).stdout(out).stderr(err).start().joinWithTimeout(10, TimeUnit.SECONDS, launcher.getListener()));
result.setStatus(procStarter.quiet(quiet).cmds(args).envs(launchEnv).stdout(out).stderr(err).start().joinWithTimeout(CLIENT_TIMEOUT, TimeUnit.SECONDS, launcher.getListener()));
final String charsetName = Charset.defaultCharset().name();
result.setOut(out.toString(charsetName));
result.setErr(err.toString(charsetName));
Expand All @@ -280,10 +288,10 @@ private LaunchResult launch(@CheckForNull @Nonnull EnvVars launchEnv, boolean qu
*/
public String whoAmI() throws IOException, InterruptedException {
ByteArrayOutputStream userId = new ByteArrayOutputStream();
launcher.launch().cmds("id", "-u").quiet(true).stdout(userId).start().joinWithTimeout(10, TimeUnit.SECONDS, launcher.getListener());
launcher.launch().cmds("id", "-u").quiet(true).stdout(userId).start().joinWithTimeout(CLIENT_TIMEOUT, TimeUnit.SECONDS, launcher.getListener());

ByteArrayOutputStream groupId = new ByteArrayOutputStream();
launcher.launch().cmds("id", "-g").quiet(true).stdout(groupId).start().joinWithTimeout(10, TimeUnit.SECONDS, launcher.getListener());
launcher.launch().cmds("id", "-g").quiet(true).stdout(groupId).start().joinWithTimeout(CLIENT_TIMEOUT, TimeUnit.SECONDS, launcher.getListener());

final String charsetName = Charset.defaultCharset().name();
return String.format("%s:%s", userId.toString(charsetName).trim(), groupId.toString(charsetName).trim());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class DockerTestUtil {
public static void assumeDocker() throws Exception {
Launcher.LocalLauncher localLauncher = new Launcher.LocalLauncher(StreamTaskListener.NULL);
try {
Assume.assumeThat("Docker working", localLauncher.launch().cmds(DockerTool.getExecutable(null, null, null, null), "ps").start().joinWithTimeout(10, TimeUnit.SECONDS, localLauncher.getListener()), is(0));
Assume.assumeThat("Docker working", localLauncher.launch().cmds(DockerTool.getExecutable(null, null, null, null), "ps").start().joinWithTimeout(DockerClient.CLIENT_TIMEOUT, TimeUnit.SECONDS, localLauncher.getListener()), is(0));
} catch (IOException x) {
Assume.assumeNoException("have Docker installed", x);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.custom.CustomConfig;
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
import org.jenkinsci.plugins.docker.workflow.client.DockerClient;
import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand Down Expand Up @@ -111,13 +112,19 @@ public class WithContainerStepTest {
" }\n" +
" }\n" +
"}", true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
story.j.waitForMessage("+ sleep infinity", b);
Assume.assumeThat(new ProcessBuilder("sudo", "killall", "-STOP", "dockerd").inheritIO().start().waitFor(), Matchers.is(0));
int origTimeout = DockerClient.CLIENT_TIMEOUT;
DockerClient.CLIENT_TIMEOUT = 10;
try {
story.j.assertBuildStatus(Result.ABORTED, story.j.waitForCompletion(b));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
story.j.waitForMessage("+ sleep infinity", b);
Assume.assumeThat(new ProcessBuilder("sudo", "killall", "-STOP", "dockerd").inheritIO().start().waitFor(), Matchers.is(0));
try {
story.j.assertBuildStatus(Result.ABORTED, story.j.waitForCompletion(b));
} finally {
Assume.assumeThat(new ProcessBuilder("sudo", "killall", "-CONT", "dockerd").inheritIO().start().waitFor(), Matchers.is(0));
}
} finally {
Assume.assumeThat(new ProcessBuilder("sudo", "killall", "-CONT", "dockerd").inheritIO().start().waitFor(), Matchers.is(0));
DockerClient.CLIENT_TIMEOUT = origTimeout;
}
}
});
Expand Down

0 comments on commit 8c18b06

Please sign in to comment.