mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(docker): retry transient registry failures during image pull (#1117)
ImageCacheService.ensureImageExists surfaced docker-java's pull
exceptions straight to its caller on the first failure. When the
underlying registry returned a transient 5xx — most notably ECR
Public's / Docker Hub's "toomanyrequests" rate limit — this caused
the surrounding operation to fail outright even though a retry a
moment later would succeed. In CodeBuild's case it manifested as
build status FAULT (CodeBuildRunner catches the exception and sets
status=FAULT + buildComplete=true), showing up as flakes in the
sdk-test-java CI job on PRs that don't touch CodeBuild at all.
- Add runWithRetry(imageUri, maxAttempts, initialBackoffMs, attempt)
static helper that retries transient pull failures up to
MAX_PULL_ATTEMPTS=3 times with exponential backoff starting at
INITIAL_BACKOFF_MS=500ms (500ms -> 1s -> 2s). The throwable is
passed to LOG.warnv(e, ...) so the cause survives in the log
stream, matching the Floci-wide pattern (SmtpRelay,
CognitoAuthFlowHandler, etc.).
- A failure is treated as transient when either:
(a) the docker daemon throws InternalServerErrorException directly
(HTTP 500, sync path), or
(b) PullImageResultCallback.awaitCompletion rewraps the same
daemon error as DockerClientException whose message starts
with "Could not pull image: " (async-callback path; same root
cause, different exception class). Both forms have been
observed against ECR Public and Docker Hub in CI.
- isTransientPullFailure(e) helper centralizes the classification.
Permanent failures (auth, missing image, malformed request, or
any other DockerClientException not coming from the pull wrapper)
keep surfacing their original docker-java exception subclass on
the first attempt and are not retried.
- Wire ensureImageExists's existing pull invocation through the
retry helper. The synchronized-once and isLocalImagePresent
fast-path semantics are unchanged.
- New ImageCacheServiceTest covers nine cases against the static
helper (no DockerClient mocking needed): first-attempt success,
retry-then-succeed (sync InternalServerErrorException),
max-attempts-exhausted-rethrows, no-retry-on-NotFoundException,
no-retry-on-UnauthorizedException, no-retry-on-generic-
DockerException, retry-then-succeed on the pull-wrapper
DockerClientException, no-retry on an unrelated
DockerClientException (container start failure), and
InterruptedException propagated as-is.
Out of scope: NeptuneContainerManager and other container managers
that call dockerClient.pullImageCmd directly (bypassing
ImageCacheService) still hit the same rate-limit path without
retries. Routing those through ImageCacheService is a separate
refactor.
This commit is contained in:
1 parent
d7ddac73fe
commit
44da9da6bb
2 files changed
+178
-4
No files matched your search
+60
-4
@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.lambda.launcher;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.PullImageResultCallback;
|
||||
import com.github.dockerjava.api.exception.DockerClientException;
|
||||
import com.github.dockerjava.api.exception.InternalServerErrorException;
|
||||
import com.github.dockerjava.api.model.AuthConfig;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -22,6 +24,9 @@ public class ImageCacheService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ImageCacheService.class);
|
||||
|
||||
static final int MAX_PULL_ATTEMPTS = 3;
|
||||
static final long INITIAL_BACKOFF_MS = 500L;
|
||||
|
||||
private final DockerClient dockerClient;
|
||||
private final List<EmulatorConfig.DockerConfig.RegistryCredential> registryCredentials;
|
||||
private final Set<String> pulledImages = ConcurrentHashMap.newKeySet();
|
||||
@@ -49,10 +54,11 @@ public class ImageCacheService {
|
||||
}
|
||||
LOG.infov("Pulling image: {0}", imageUri);
|
||||
try {
|
||||
dockerClient.pullImageCmd(imageUri)
|
||||
.withAuthConfig(resolveAuth(imageUri))
|
||||
.exec(new PullImageResultCallback())
|
||||
.awaitCompletion(5, TimeUnit.MINUTES);
|
||||
runWithRetry(imageUri, MAX_PULL_ATTEMPTS, INITIAL_BACKOFF_MS,
|
||||
() -> dockerClient.pullImageCmd(imageUri)
|
||||
.withAuthConfig(resolveAuth(imageUri))
|
||||
.exec(new PullImageResultCallback())
|
||||
.awaitCompletion(5, TimeUnit.MINUTES));
|
||||
pulledImages.add(imageUri);
|
||||
LOG.infov("Image pulled successfully: {0}", imageUri);
|
||||
} catch (InterruptedException e) {
|
||||
@@ -62,6 +68,56 @@ public class ImageCacheService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the given pull attempt, retrying on transient registry failures with exponential
|
||||
* backoff. A failure is considered transient when either:
|
||||
* <ul>
|
||||
* <li>the docker daemon throws {@link InternalServerErrorException} directly (HTTP 500,
|
||||
* e.g. ECR Public's {@code "toomanyrequests: Rate exceeded"}), or</li>
|
||||
* <li>{@link PullImageResultCallback#awaitCompletion} rewraps a daemon error as
|
||||
* {@link DockerClientException} with a message starting with
|
||||
* {@code "Could not pull image: "} (the async-callback path; same root cause, just
|
||||
* a different exception class).</li>
|
||||
* </ul>
|
||||
* Permanent failures (auth, missing image, malformed request, or any other
|
||||
* {@code DockerClientException} not coming from the pull wrapper) keep surfacing their
|
||||
* original docker-java exception subclass on the first attempt and are not retried.
|
||||
*/
|
||||
static void runWithRetry(String imageUri, int maxAttempts, long initialBackoffMs,
|
||||
PullAttempt attempt) throws InterruptedException {
|
||||
long backoffMs = initialBackoffMs;
|
||||
for (int i = 1; i <= maxAttempts; i++) {
|
||||
try {
|
||||
attempt.run();
|
||||
return;
|
||||
} catch (RuntimeException e) {
|
||||
if (!isTransientPullFailure(e) || i == maxAttempts) {
|
||||
throw e;
|
||||
}
|
||||
LOG.warnv(e, "Transient image pull failure for {0} (attempt {1}/{2}). "
|
||||
+ "Retrying in {3}ms.", imageUri, i, maxAttempts, backoffMs);
|
||||
Thread.sleep(backoffMs);
|
||||
backoffMs *= 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isTransientPullFailure(RuntimeException e) {
|
||||
if (e instanceof InternalServerErrorException) {
|
||||
return true;
|
||||
}
|
||||
if (e instanceof DockerClientException && e.getMessage() != null
|
||||
&& e.getMessage().startsWith("Could not pull image: ")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface PullAttempt {
|
||||
void run() throws InterruptedException;
|
||||
}
|
||||
|
||||
private boolean isLocalImagePresent(String imageUri) {
|
||||
try {
|
||||
dockerClient.inspectImageCmd(imageUri).exec();
|
||||
|
||||
Reference in new issue
Block a user