Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinstate retries for image pulls #1712

Merged
merged 5 commits into from Aug 22, 2019
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 31 additions & 13 deletions core/src/main/java/org/testcontainers/images/RemoteDockerImage.java
Expand Up @@ -3,6 +3,7 @@
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.ListImagesCmd;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.InternalServerErrorException;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.core.command.PullImageResultCallback;
import lombok.NonNull;
Expand All @@ -14,13 +15,18 @@
import org.testcontainers.utility.DockerLoggerFactory;
import org.testcontainers.utility.LazyFuture;

import java.time.Duration;
import java.time.Instant;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.time.Duration.between;
rnorth marked this conversation as resolved.
Show resolved Hide resolved
import static java.time.Instant.now;

@ToString
public class RemoteDockerImage extends LazyFuture<String> {

Expand All @@ -29,6 +35,7 @@ public class RemoteDockerImage extends LazyFuture<String> {
*/
@Deprecated
public static final Set<DockerImageName> AVAILABLE_IMAGE_NAME_CACHE = new HashSet<>();
private static final Duration PULL_RETRY_TIME_LIMIT = Duration.ofMinutes(2);

private DockerImageName imageName;

Expand Down Expand Up @@ -73,23 +80,34 @@ protected final String resolve() {
return imageName.toString();
}

// The image is not available locally - pull it
logger.info("Pulling docker image: {}. Please be patient; this may take some time but only needs to be done once.", imageName);

// The image is not available locally - pull it
try {
final PullImageResultCallback callback = new TimeLimitedLoggedPullImageResultCallback(logger);
dockerClient
.pullImageCmd(imageName.getUnversionedPart())
.withTag(imageName.getVersionPart())
.exec(callback);
callback.awaitCompletion();
AVAILABLE_IMAGE_NAME_CACHE.add(imageName);
} catch (Exception e) {
logger.error("Failed to pull image: {}. Please check output of `docker pull {}`", imageName, imageName);
throw new ContainerFetchException("Failed to pull image: " + imageName, e);
Exception lastFailure = null;
final Instant lastRetryAllowed = now().plus(PULL_RETRY_TIME_LIMIT);

while (now().isBefore(lastRetryAllowed)) {
try {
final PullImageResultCallback callback = new TimeLimitedLoggedPullImageResultCallback(logger);
dockerClient
.pullImageCmd(imageName.getUnversionedPart())
.withTag(imageName.getVersionPart())
.exec(callback);
callback.awaitCompletion();
AVAILABLE_IMAGE_NAME_CACHE.add(imageName);

return imageName.toString();
} catch (InterruptedException | InternalServerErrorException e) {
// these classes of exception often relate to timeout/connection errors so should be retried
lastFailure = e;
logger.warn("Retrying pull for image: {} ({}s remaining)",
imageName,
between(now(), lastRetryAllowed).getSeconds());
}
}
logger.error("Failed to pull image: {}. Please check output of `docker pull {}`", imageName, imageName, lastFailure);

return imageName.toString();
throw new ContainerFetchException("Failed to pull image: " + imageName, lastFailure);
} catch (DockerClientException e) {
throw new ContainerFetchException("Failed to get Docker client for " + imageName, e);
}
Expand Down