mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(docker): bound a retried Docker call by the call timeout, not each attempt (#8931)
api-timeout-seconds bounded each attempt, and the control-plane transport retries a replayable request up to six times, so a silent daemon held a call for about six times the setting plus backoff. DockerRetry now takes one deadline across attempts and backoffs, counted from the first attempt, and checks it again after each backoff so an overslept sleep starts no attempt past it. The producer gives it to the control-plane client only: a silent daemon fails the call after one attempt, fast transient failures are still retried within the limit, and long-lived streams (image pulls, container waits), which have no response timeout, keep their retries. A running attempt is not cut short. DockerRetry.call takes the clock and the backoff wait, so the tests advance a test clock instead of sleeping.
This commit is contained in:
1 parent
f3b8b12777
commit
eb1b9a0a7d
7 files changed
+194
-21
No files matched your search
@@ -84,7 +84,7 @@ floci:
|
||||
|
||||
Environment variable: `FLOCI_DOCKER_CONNECTION_REQUEST_TIMEOUT_SECONDS`
|
||||
|
||||
A Docker call that gets no response within `api-timeout-seconds` fails, on Unix-socket and TCP hosts alike, so a hung daemon cannot block a request indefinitely. The limit applies to each attempt: a control-plane request that is safe to repeat is tried up to six times, so against a silent daemon it can take about six times the limit, plus backoff, to fail. It defaults to 300 seconds (5 minutes) for both pools and must be at least 1, since httpclient5 reads 0 as no limit. Requests that follow a container for as long as it runs (container waits, exec and attach output, followed logs, streamed stats, daemon events, image pulls and builds) have no such limit, since they can stay silent while healthy:
|
||||
A Docker call that gets no response within `api-timeout-seconds` fails, on Unix-socket and TCP hosts alike, so a hung daemon cannot block a request indefinitely. A request that is safe to repeat is retried after a transient connection error, but only within the same limit, counted from its first attempt: no retry starts once the limit would pass, so a silent daemon fails the call after about the limit. An attempt already running is not cut short, so a fast failure followed by a silent retry can take up to twice the limit. It defaults to 300 seconds (5 minutes) for both pools and must be at least 1, since httpclient5 reads 0 as no limit. Requests that follow a container for as long as it runs (container waits, exec and attach output, followed logs, streamed stats, daemon events, image pulls and builds) have no such limit, since they can stay silent while healthy:
|
||||
|
||||
```yaml
|
||||
floci:
|
||||
|
||||
@@ -4431,8 +4431,9 @@ public interface EmulatorConfig {
|
||||
|
||||
/**
|
||||
* How long a Docker call waits for the daemon's response before it fails, in seconds, for
|
||||
* both pools. The limit is per attempt: a control-plane request that is safe to repeat is
|
||||
* tried up to six times. Requests that hold a stream open for as long as a container runs (container
|
||||
* both pools. It bounds the whole call: a request that is safe to repeat is retried after a
|
||||
* transient connection error only while no retry would start past the limit, counted from
|
||||
* the first attempt. Requests that hold a stream open for as long as a container runs (container
|
||||
* waits, exec and attach output, followed logs, streamed stats, events, image pulls and
|
||||
* builds) are exempt, since they can stay silent while healthy.
|
||||
*/
|
||||
|
||||
@@ -513,12 +513,12 @@ public class DockerClientProducer {
|
||||
LOG.infov("Creating {0} DockerClient pool (maxConnections={1}) for host: {2}",
|
||||
role, maxConnections, clientConfig.getDockerHost());
|
||||
|
||||
FlociDockerHttpClient transport = newHttpClient(clientConfig, maxConnections,
|
||||
Duration.ofSeconds(validateApiTimeoutSeconds(config.docker().apiTimeoutSeconds())),
|
||||
Duration apiTimeout = Duration.ofSeconds(validateApiTimeoutSeconds(config.docker().apiTimeoutSeconds()));
|
||||
FlociDockerHttpClient transport = newHttpClient(clientConfig, maxConnections, apiTimeout,
|
||||
Duration.ofSeconds(validateConnectionRequestTimeoutSeconds(
|
||||
config.docker().connectionRequestTimeoutSeconds())));
|
||||
gaugeLogger.register(role, transport::gauges);
|
||||
return DockerClientImpl.getInstance(clientConfig, wrapForRole(transport, role));
|
||||
return DockerClientImpl.getInstance(clientConfig, wrapForRole(transport, role, apiTimeout));
|
||||
}
|
||||
|
||||
static FlociDockerHttpClient newHttpClient(DefaultDockerClientConfig clientConfig, int maxConnections,
|
||||
@@ -538,14 +538,17 @@ public class DockerClientProducer {
|
||||
* docker call survives a transient socket drop. The streaming transport replays only container
|
||||
* waits, as the control-plane one does: its other requests (log follow, attach, exec output)
|
||||
* hold hijacked or long-lived streams that must never be replayed, and its exec-start calls are
|
||||
* the one request retrying could turn into a re-run command.
|
||||
* the one request retrying could turn into a re-run command. {@code callTimeout} bounds a retried
|
||||
* control-plane call across its attempts and backoffs, so it is the longest one call waits on a
|
||||
* silent daemon, not the length of each of its attempts.
|
||||
*/
|
||||
static DockerHttpClient wrapForRole(DockerHttpClient transport, String role) {
|
||||
static DockerHttpClient wrapForRole(DockerHttpClient transport, String role, Duration callTimeout) {
|
||||
if (!"control-plane".equals(role)) {
|
||||
// The streaming transport replays only container waits, long-lived streams the call timeout never bounds.
|
||||
return RetryingDockerHttpClient.forContainerWaits(transport);
|
||||
}
|
||||
LOG.info("Wrapping control-plane docker transport in transient-I/O retry");
|
||||
return new RetryingDockerHttpClient(transport);
|
||||
return new RetryingDockerHttpClient(transport, callTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,9 @@ import java.io.InterruptedIOException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.nio.file.AccessDeniedException;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.time.Duration;
|
||||
import java.util.Locale;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
/**
|
||||
* Retry policy for docker daemon calls. Every docker command travels over the single shared
|
||||
@@ -154,11 +156,35 @@ public final class DockerRetry {
|
||||
* immediately. A {@link RuntimeException} is rethrown as-is; a checked exception is wrapped.
|
||||
*/
|
||||
public static <T> T call(int maxAttempts, long backoffMillis, DockerCall<T> call) {
|
||||
return call(maxAttempts, backoffMillis, null, call);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #call(int, long, DockerCall)}, but with one {@code deadline} across every attempt and
|
||||
* backoff, counted from the first attempt: no attempt starts, and no backoff is slept, that would
|
||||
* begin past it. An attempt that times out at the deadline therefore ends the call, so a silent
|
||||
* daemon fails it after about one attempt rather than {@code maxAttempts} of them. An attempt
|
||||
* already running is not cut short. A null deadline means none.
|
||||
*/
|
||||
public static <T> T call(int maxAttempts, long backoffMillis, Duration deadline, DockerCall<T> call) {
|
||||
return call(maxAttempts, backoffMillis, deadline, System::nanoTime, Thread::sleep, call);
|
||||
}
|
||||
|
||||
/** Pause between attempts; tests advance the clock instead of sleeping. */
|
||||
@FunctionalInterface
|
||||
interface BackoffWait {
|
||||
void sleep(long millis) throws InterruptedException;
|
||||
}
|
||||
|
||||
/** As {@link #call(int, long, Duration, DockerCall)}, reading time from {@code nanoTime} and pausing with {@code wait}. */
|
||||
static <T> T call(int maxAttempts, long backoffMillis, Duration deadline, LongSupplier nanoTime,
|
||||
BackoffWait wait, DockerCall<T> call) {
|
||||
if (maxAttempts < 1) {
|
||||
// Zero or negative would run the call zero times and fall out of the loop returning
|
||||
// nothing, turning a misconfiguration into a silent no-op rather than a failure.
|
||||
throw new IllegalArgumentException("maxAttempts must be >= 1, got " + maxAttempts);
|
||||
}
|
||||
long start = nanoTime.getAsLong();
|
||||
int attempt = 0;
|
||||
while (true) {
|
||||
attempt++;
|
||||
@@ -172,9 +198,20 @@ public final class DockerRetry {
|
||||
if (attempt >= maxAttempts || !isTransientIo(e)) {
|
||||
throw asUnchecked(e);
|
||||
}
|
||||
long delay = backoffDelay(attempt, backoffMillis, BACKOFF_CAP_MS);
|
||||
if (deadline != null && nanoTime.getAsLong() - start + delay * 1_000_000L >= deadline.toNanos()) {
|
||||
LOG.warnv("Transient docker I/O error on attempt {0}/{1}, not retried: the {2} ms "
|
||||
+ "Docker call timeout would pass first: {3}",
|
||||
attempt, maxAttempts, deadline.toMillis(), e.getMessage());
|
||||
throw asUnchecked(e);
|
||||
}
|
||||
LOG.warnv("Transient docker I/O error on attempt {0}/{1}, retrying: {2}",
|
||||
attempt, maxAttempts, e.getMessage());
|
||||
sleep(backoffDelay(attempt, backoffMillis, BACKOFF_CAP_MS));
|
||||
sleep(wait, delay);
|
||||
if (deadline != null && nanoTime.getAsLong() - start >= deadline.toNanos()) {
|
||||
// The backoff overslept the deadline: no attempt starts past it.
|
||||
throw asUnchecked(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -191,12 +228,12 @@ public final class DockerRetry {
|
||||
return e instanceof RuntimeException re ? re : new RuntimeException(e);
|
||||
}
|
||||
|
||||
private static void sleep(long millis) {
|
||||
private static void sleep(BackoffWait wait, long millis) {
|
||||
if (millis <= 0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
wait.sleep(millis);
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException("Interrupted while backing off before a docker retry", ie);
|
||||
|
||||
@@ -6,6 +6,8 @@ import com.github.dockerjava.transport.DockerHttpClient;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.function.LongSupplier;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
@@ -67,21 +69,47 @@ public final class RetryingDockerHttpClient implements DockerHttpClient {
|
||||
private final int maxAttempts;
|
||||
private final long backoffMillis;
|
||||
private final Predicate<Request> replayable;
|
||||
/**
|
||||
* One limit across a call's attempts and backoffs (see {@link DockerRetry#call(int, long, Duration,
|
||||
* DockerRetry.DockerCall)}), the Docker call timeout, so retries cannot stretch a call to several
|
||||
* times it; null for none.
|
||||
*/
|
||||
private final Duration callDeadline;
|
||||
private final LongSupplier nanoTime;
|
||||
private final DockerRetry.BackoffWait backoffWait;
|
||||
|
||||
public RetryingDockerHttpClient(DockerHttpClient delegate) {
|
||||
this(delegate, MAX_ATTEMPTS, BACKOFF_MS);
|
||||
this(delegate, (Duration) null);
|
||||
}
|
||||
|
||||
/** @param callDeadline the longest a call may take across its attempts, or null for no limit */
|
||||
public RetryingDockerHttpClient(DockerHttpClient delegate, Duration callDeadline) {
|
||||
this(delegate, MAX_ATTEMPTS, BACKOFF_MS, RetryingDockerHttpClient::isReplayable, callDeadline,
|
||||
System::nanoTime, Thread::sleep);
|
||||
}
|
||||
|
||||
RetryingDockerHttpClient(DockerHttpClient delegate, int maxAttempts, long backoffMillis) {
|
||||
this(delegate, maxAttempts, backoffMillis, RetryingDockerHttpClient::isReplayable);
|
||||
this(delegate, maxAttempts, backoffMillis, RetryingDockerHttpClient::isReplayable, null,
|
||||
System::nanoTime, Thread::sleep);
|
||||
}
|
||||
|
||||
/** With a clock and a backoff wait of the test's own, so a deadline is checked without sleeping. */
|
||||
RetryingDockerHttpClient(DockerHttpClient delegate, int maxAttempts, long backoffMillis, Duration callDeadline,
|
||||
LongSupplier nanoTime, DockerRetry.BackoffWait backoffWait) {
|
||||
this(delegate, maxAttempts, backoffMillis, RetryingDockerHttpClient::isReplayable, callDeadline,
|
||||
nanoTime, backoffWait);
|
||||
}
|
||||
|
||||
private RetryingDockerHttpClient(DockerHttpClient delegate, int maxAttempts, long backoffMillis,
|
||||
Predicate<Request> replayable) {
|
||||
Predicate<Request> replayable, Duration callDeadline,
|
||||
LongSupplier nanoTime, DockerRetry.BackoffWait backoffWait) {
|
||||
this.delegate = delegate;
|
||||
this.maxAttempts = maxAttempts;
|
||||
this.backoffMillis = backoffMillis;
|
||||
this.replayable = replayable;
|
||||
this.callDeadline = callDeadline;
|
||||
this.nanoTime = nanoTime;
|
||||
this.backoffWait = backoffWait;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,8 +124,9 @@ public final class RetryingDockerHttpClient implements DockerHttpClient {
|
||||
|
||||
static RetryingDockerHttpClient forContainerWaits(DockerHttpClient delegate, int maxAttempts,
|
||||
long backoffMillis) {
|
||||
// No call timeout: a container wait is a long-lived stream, which the deadline never bounds.
|
||||
return new RetryingDockerHttpClient(delegate, maxAttempts, backoffMillis,
|
||||
request -> isReplayable(request) && isContainerWait(request));
|
||||
request -> isReplayable(request) && isContainerWait(request), null, System::nanoTime, Thread::sleep);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -105,7 +134,10 @@ public final class RetryingDockerHttpClient implements DockerHttpClient {
|
||||
if (!replayable.test(request)) {
|
||||
return delegate.execute(request);
|
||||
}
|
||||
return DockerRetry.call(maxAttempts, backoffMillis, () -> delegate.execute(request));
|
||||
// A long-lived stream has no response timeout, so the call timeout does not bound its retries either.
|
||||
Duration deadline = FlociDockerHttpClient.isLongLivedStream(request) ? null : callDeadline;
|
||||
return DockerRetry.call(maxAttempts, backoffMillis, deadline, nanoTime, backoffWait,
|
||||
() -> delegate.execute(request));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -418,8 +418,8 @@ floci:
|
||||
# EmulatorConfig.DockerConfig.connectionRequestTimeoutSeconds: a value set here WINS over the annotation.
|
||||
connection-request-timeout-seconds: 30
|
||||
# FLOCI_DOCKER_API_TIMEOUT_SECONDS: how long a Docker call waits for the daemon's response before
|
||||
# failing; long-lived streams (waits, followed logs, exec output, pulls) are exempt. It applies per
|
||||
# attempt: a control-plane request that is safe to repeat is tried up to six times. Keep in step with
|
||||
# failing; long-lived streams (waits, followed logs, exec output, pulls) are exempt. It bounds the
|
||||
# whole call: a request that is safe to repeat is not retried past it. Keep in step with
|
||||
# the @WithDefault on EmulatorConfig.DockerConfig.apiTimeoutSeconds: a value set here WINS over the annotation.
|
||||
api-timeout-seconds: 300
|
||||
# FLOCI_DOCKER_IMAGE_PULL_TIMEOUT_SECONDS: how long each attempt at an image pull may take; a pull
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.BindException;
|
||||
import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.StandardProtocolFamily;
|
||||
import java.net.URI;
|
||||
import java.net.UnixDomainSocketAddress;
|
||||
@@ -23,11 +24,14 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.PosixFilePermissions;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.IntFunction;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -79,6 +83,102 @@ class RetryingDockerHttpClientTest {
|
||||
return new RuntimeException(new IOException("Broken pipe"));
|
||||
}
|
||||
|
||||
/** A monotonic clock the tests advance by hand: an attempt or a backoff takes exactly what it adds. */
|
||||
private final AtomicLong clockNanos = new AtomicLong();
|
||||
|
||||
private RetryingDockerHttpClient onTheTestClock(DockerHttpClient delegate, long backoffMillis,
|
||||
Duration callTimeout, long backoffOversleepMillis) {
|
||||
return new RetryingDockerHttpClient(delegate, 6, backoffMillis, callTimeout, clockNanos::get,
|
||||
millis -> clockNanos.addAndGet(TimeUnit.MILLISECONDS.toNanos(millis + backoffOversleepMillis)));
|
||||
}
|
||||
|
||||
private void spend(Duration time) {
|
||||
clockNanos.addAndGet(time.toNanos());
|
||||
}
|
||||
|
||||
// Catches: the call timeout bounding each attempt, so a silent daemon held a replayable call for
|
||||
// six times the setting plus backoff (30 minutes at the default) before it failed.
|
||||
@Test
|
||||
void aDaemonThatNeverAnswersFailsTheCallAtTheCallTimeoutNotAfterEveryAttempt() {
|
||||
Duration callTimeout = Duration.ofSeconds(300);
|
||||
FakeTransport delegate = new FakeTransport(attempt -> {
|
||||
spend(callTimeout);
|
||||
throw new RuntimeException(new SocketTimeoutException("Read timed out"));
|
||||
});
|
||||
RetryingDockerHttpClient client = onTheTestClock(delegate, 500L, callTimeout, 0L);
|
||||
Request inspect = Request.builder().method(Request.Method.GET).path("/containers/abc/json").build();
|
||||
|
||||
RuntimeException failure = assertThrows(RuntimeException.class, () -> client.execute(inspect));
|
||||
|
||||
assertInstanceOf(SocketTimeoutException.class, failure.getCause());
|
||||
assertEquals(1, delegate.calls.get(), "an attempt that times out at the limit leaves no time to retry");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fastTransientFailuresAreStillRetriedWithinTheCallTimeout() {
|
||||
Response ok = mock(Response.class);
|
||||
FakeTransport delegate = new FakeTransport(attempt -> {
|
||||
if (attempt < 3) {
|
||||
throw brokenPipe();
|
||||
}
|
||||
return ok;
|
||||
});
|
||||
RetryingDockerHttpClient client = onTheTestClock(delegate, 500L, Duration.ofSeconds(300), 0L);
|
||||
|
||||
assertSame(ok, client.execute(Request.builder().method(Request.Method.GET).path("/_ping").build()));
|
||||
assertEquals(3, delegate.calls.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void noRetryStartsWhoseBackoffWouldEndPastTheCallTimeout() {
|
||||
FakeTransport delegate = new FakeTransport(attempt -> {
|
||||
throw brokenPipe();
|
||||
});
|
||||
RetryingDockerHttpClient client = onTheTestClock(delegate, 200L, Duration.ofMillis(500), 0L);
|
||||
|
||||
assertThrows(RuntimeException.class,
|
||||
() -> client.execute(Request.builder().method(Request.Method.GET).path("/_ping").build()));
|
||||
|
||||
// Backoffs of 200 then 400 ms: the second would end past 500 ms, so only two attempts run.
|
||||
assertEquals(2, delegate.calls.get());
|
||||
assertEquals(Duration.ofMillis(200).toNanos(), clockNanos.get(), "only the first backoff was slept");
|
||||
}
|
||||
|
||||
// Catches: a backoff that overslept the deadline still starting the next attempt.
|
||||
@Test
|
||||
void aBackoffThatOversleepsTheCallTimeoutStartsNoFurtherAttempt() {
|
||||
FakeTransport delegate = new FakeTransport(attempt -> {
|
||||
throw brokenPipe();
|
||||
});
|
||||
// The 200 ms backoff fits the 500 ms limit when it starts, but the wait returns 600 ms later.
|
||||
RetryingDockerHttpClient client = onTheTestClock(delegate, 200L, Duration.ofMillis(500), 400L);
|
||||
|
||||
assertThrows(RuntimeException.class,
|
||||
() -> client.execute(Request.builder().method(Request.Method.GET).path("/_ping").build()));
|
||||
|
||||
assertEquals(1, delegate.calls.get());
|
||||
}
|
||||
|
||||
// Catches: the call timeout cutting the retries of an image pull, which has no response timeout and
|
||||
// may take longer than it to answer before a dropped connection.
|
||||
@Test
|
||||
void aLongLivedStreamKeepsItsRetriesPastTheCallTimeout() {
|
||||
Response ok = mock(Response.class);
|
||||
Duration callTimeout = Duration.ofSeconds(300);
|
||||
FakeTransport delegate = new FakeTransport(attempt -> {
|
||||
if (attempt < 3) {
|
||||
spend(callTimeout.multipliedBy(2));
|
||||
throw brokenPipe();
|
||||
}
|
||||
return ok;
|
||||
});
|
||||
RetryingDockerHttpClient client = onTheTestClock(delegate, 500L, callTimeout, 0L);
|
||||
Request pull = Request.builder().method(Request.Method.POST).path("/images/create?fromImage=alpine").build();
|
||||
|
||||
assertSame(ok, client.execute(pull));
|
||||
assertEquals(3, delegate.calls.get(), "a pull is retried whatever the call timeout");
|
||||
}
|
||||
|
||||
@Test
|
||||
void retriesTransientBrokenPipeOnBodylessRequestThenSucceeds() {
|
||||
Response ok = mock(Response.class);
|
||||
@@ -559,7 +659,7 @@ class RetryingDockerHttpClientTest {
|
||||
@Test
|
||||
void producerWrapsEachTransportInItsOwnReplayPolicy() {
|
||||
DockerHttpClient raw = new FakeTransport(attempt -> null);
|
||||
DockerHttpClient controlPlane = DockerClientProducer.wrapForRole(raw, "control-plane");
|
||||
DockerHttpClient controlPlane = DockerClientProducer.wrapForRole(raw, "control-plane", Duration.ofSeconds(30));
|
||||
|
||||
assertSame(RetryingDockerHttpClient.class, controlPlane.getClass(),
|
||||
"the control-plane transport must be wrapped in the retry decorator");
|
||||
@@ -568,7 +668,7 @@ class RetryingDockerHttpClientTest {
|
||||
FakeTransport failing = new FakeTransport(attempt -> {
|
||||
throw brokenPipe();
|
||||
});
|
||||
DockerHttpClient streaming = DockerClientProducer.wrapForRole(failing, "streaming");
|
||||
DockerHttpClient streaming = DockerClientProducer.wrapForRole(failing, "streaming", Duration.ofSeconds(30));
|
||||
Request logs = Request.builder().method(Request.Method.GET).path("/containers/abc/logs?follow=1").build();
|
||||
|
||||
assertInstanceOf(RetryingDockerHttpClient.class, streaming,
|
||||
|
||||
Reference in new issue
Block a user