fix(emr): remove the release images a newer pull replaces (#8395)

A cluster's release image is named after the ids of the two images it
is made of, so a newer pull of either makes another, and the older one
stayed on the Docker daemon for good. Each release image is now
labelled with its kind: its release, the names of the two images and
what is copied. Once Floci makes a newer image of a kind, it removes the
others of that kind, without force, so one a container still uses stays
until the next newer one. Another kind (with or without Hive, or other
image names) is never touched, and an image made before the label is
left as it is.

Images of one kind are made one at a time, the two names resolved to
ids under that kind's lock, so one made of the older images never comes
after the newer one to remove it. A step or a bootstrap holds the image
it was given until it ends, as it may have no container of it yet, and
a held image is not removed either. A failure to remove one is logged
with its exception; the new image is there, so it fails nothing.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-14 22:20:04 -04:00
1 parent fb3c93018e
commit be6488b7ea
7 files changed
+280 -17

No files matched your search

@@ -1,7 +1,9 @@
package io.floci.aws.services.emr;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.exception.ConflictException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Image;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.config.EmulatorConfig.EcsServiceConfig.ImagePullBehavior;
import io.floci.aws.core.common.Digests;
@@ -22,6 +24,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -37,7 +40,8 @@ import java.util.concurrent.ConcurrentHashMap;
* leading there, and the Hive image's Hadoop configuration, which puts Tez on the class path, goes to
* {@link EmrServerlessSparkRunner#HIVE_HADOOP_CONF}, for Hive alone. The image is named after its release, the ids of
* the two images and what is copied, so a newer pull of either, or a change to what is copied, makes another, and it is
* kept for the release's next cluster. A release of the AWS runtime for Apache Spark has no Hive image, and another
* kept for the release's next cluster; the one a newer pull makes replaces those of its kind made before
* ({@link #KIND_LABEL}). A release of the AWS runtime for Apache Spark has no Hive image, and another
* release's HDFS and YARN do not run on its newer Hadoop, so its image gets the whole of that release's Hadoop, in a
* folder apart, which only its primary node's daemons run from.
*/
@@ -47,6 +51,9 @@ public class EmrReleaseImages implements ReleaseImages {
private static final Logger LOG = Logger.getLogger(EmrReleaseImages.class);
private static final String SERVICE = "emr";
private static final String COMPONENT = "emr-release";
// An image's kind: its release, the names of the two images it is made of, and what is copied. The image made after
// a newer pull of either replaces the others of its kind.
static final String KIND_LABEL = "io.floci.emr-release.kind";
private static final String EMR_SHARE = "/usr/share/aws/emr";
private static final String GOODIES = EMR_SHARE + "/serverless-goodies/lib";
private static final String HIVE_GOODIES = "serverless-goodies-hive/lib";
@@ -83,8 +90,13 @@ public class EmrReleaseImages implements ReleaseImages {
new Copy("/usr/lib/hadoop-mapreduce", HADOOP_APART),
new Copy("/usr/lib/hadoop-yarn", HADOOP_APART));
// One making at a time for each image; another thread asking for it waits, then finds it there.
// One making at a time for each kind of image, from the ids its two names lead to once it has the lock, so one made
// of images a newer pull has since replaced is never made after the newer one, to remove it; another thread asking
// for the same image waits, then finds it there.
private final Map<String, Object> making = new ConcurrentHashMap<>();
// The images given to a step or a bootstrap that has not ended, by id, with how many hold each: none of them is
// removed, as no container of it may be there yet to keep it.
private final Map<String, Integer> held = new ConcurrentHashMap<>();
private final ContainerLifecycleManager lifecycleManager;
private final ContainerBuilder containerBuilder;
@@ -117,14 +129,17 @@ public class EmrReleaseImages implements ReleaseImages {
: withHive ? COPIES : HADOOP_COPIES;
String spark = containerBuilder.resolveImage(sparkImage);
String hive = containerBuilder.resolveImage(hiveImage);
String sparkId = imageCacheService.resolveForLaunch(spark, ImagePullBehavior.PREFER_CACHED, null).imageId();
String hiveId = imageCacheService.resolveForLaunch(hive, ImagePullBehavior.PREFER_CACHED, null).imageId();
String repository = ContainerStorageHelper.dockerName(config, COMPONENT);
String tag = releaseLabel + "-" + Digests.sha256Hex(sparkId + "\n" + hiveId + "\n" + copies).substring(0, 12);
synchronized (making.computeIfAbsent(repository + ":" + tag, name -> new Object())) {
String kind = Digests.sha256Hex(releaseLabel + "\n" + spark + "\n" + hive + "\n" + copies).substring(0, 12);
synchronized (making.computeIfAbsent(repository + ":" + kind, name -> new Object())) {
String sparkId = imageCacheService.resolveForLaunch(spark, ImagePullBehavior.PREFER_CACHED, null)
.imageId();
String hiveId = imageCacheService.resolveForLaunch(hive, ImagePullBehavior.PREFER_CACHED, null).imageId();
String tag = releaseLabel + "-"
+ Digests.sha256Hex(sparkId + "\n" + hiveId + "\n" + copies).substring(0, 12);
DockerClient docker = lifecycleManager.getDockerClient();
try {
return docker.inspectImageCmd(repository + ":" + tag).exec().getId();
return hold(docker.inspectImageCmd(repository + ":" + tag).exec().getId());
} catch (NotFoundException e) {
LOG.infov("Making {0}:{1} for EMR clusters {2}, from {3} and {4}", repository, tag,
withHive ? "with Hive" : "without Hive", spark, hive);
@@ -134,6 +149,8 @@ public class EmrReleaseImages implements ReleaseImages {
labels.put(ContainerStorageHelper.SERVICE_LABEL, SERVICE);
labels.put(ContainerStorageHelper.COMPONENT_LABEL, COMPONENT);
labels.put(ContainerStorageHelper.OWNER_LABEL, ContainerStorageHelper.ownerIdentity(config));
labels.put(KIND_LABEL, kind);
String made;
List<String> containers = new ArrayList<>();
try {
String target = docker.createContainerCmd(sparkId).withLabels(labels).exec().getId();
@@ -144,7 +161,7 @@ public class EmrReleaseImages implements ReleaseImages {
RetryingTarCopier.copyStreamed(docker, target, copy.into(), copy.from(),
out -> copy(docker.copyArchiveFromContainerCmd(source, copy.from()).exec(), out, copy));
}
return lifecycleManager.commit(target, repository, tag,
made = lifecycleManager.commit(target, repository, tag,
user == null || user.isBlank() ? "root" : user, labels);
} finally {
for (String container : containers) {
@@ -156,6 +173,52 @@ public class EmrReleaseImages implements ReleaseImages {
}
}
}
removeReplaced(docker, labels, made, repository + ":" + tag);
return hold(made);
}
}
@Override
public void released(String imageId) {
held.computeIfPresent(imageId, (id, holders) -> holders > 1 ? holders - 1 : null);
}
// The image is held, under its kind's lock, until released(), before a step or a bootstrap gets its id.
private String hold(String imageId) {
held.merge(imageId, 1, Integer::sum);
return imageId;
}
/**
* Removes the images of {@code kind} that {@code made}, named {@code name}, replaces: made before a newer pull of
* either image, which no cluster asks for again, as the ids it would now be made of lead to {@code made}. One a
* step or a bootstrap holds stays, and so does one a container still uses, as Docker refuses to remove it without
* force, each until the next newer one is made. One made before images were labelled with their kind is left. The
* new image is there either way, so a failure to remove the others fails nothing, and is logged.
*/
private void removeReplaced(DockerClient docker, Map<String, String> kind, String made, String name) {
List<Image> images;
try {
images = docker.listImagesCmd().withLabelFilter(kind).exec();
} catch (RuntimeException e) {
LOG.warnv(e, "Could not list the EMR release images {0} replaces: {1}", name, e.getMessage());
return;
}
for (Image image : images) {
if (made.equals(image.getId()) || held.containsKey(image.getId())
|| (image.getRepoTags() != null && Arrays.asList(image.getRepoTags()).contains(name))) {
continue;
}
try {
docker.removeImageCmd(image.getId()).exec();
LOG.infov("Removed EMR release image {0}, which {1} replaces", image.getId(), name);
} catch (ConflictException e) {
LOG.debugv("Kept EMR release image {0}, which a container still uses: {1}", image.getId(),
e.getMessage());
} catch (RuntimeException e) {
LOG.warnv(e, "Could not remove EMR release image {0}, which {1} replaces: {2}", image.getId(), name,
e.getMessage());
}
}
}
@@ -1633,9 +1633,10 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
} else {
SparkJobRunner.Outcome outcome;
StepProgress progress = new StepProgress(launch);
String image = null;
try {
SparkJobRunner.Job running = releaseImage != null && !progress.cancelled()
? job.inImage(releaseImage.get()) : job;
image = releaseImage != null && !progress.cancelled() ? releaseImage.get() : null;
SparkJobRunner.Job running = image != null ? job.inImage(image) : job;
// The cluster's storage is there, its HDFS running, before its first step's container mounts it. A step
// a cancel, its cluster's end or a reset has passed does not start, so it makes none.
if (job.storage() != null && !progress.cancelled()) {
@@ -1656,6 +1657,11 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
LOG.warnv(e, "EMR step {0} could not run", launch.stepId());
outcome = SparkJobRunner.Outcome.couldNotStart(e.getMessage());
}
} finally {
// Its containers have come and gone, or never came, so a newer image of its kind may remove this one.
if (image != null) {
releaseImages.released(image);
}
}
// A reset since the step started has swept the storage and wiped its cluster, so nothing would remove
// volumes made, or mounted, after its sweep: they go now, the step's container being gone.
@@ -1907,12 +1913,19 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
BooleanSupplier cancelled = () -> teardowns.get() != launch.teardownsBefore()
|| !RequestScopes.callAs(launch.accountId(), launch.region(), () -> stillBootstrapping(launch));
BootstrapRunner.Outcome outcome;
String image = null;
try {
outcome = bootstrapRunner.run(releaseImage != null && !cancelled.getAsBoolean()
? bootstrap.inImage(releaseImage.get()) : bootstrap, cancelled);
image = releaseImage != null && !cancelled.getAsBoolean() ? releaseImage.get() : null;
outcome = bootstrapRunner.run(image != null ? bootstrap.inImage(image) : bootstrap, cancelled);
} catch (RuntimeException e) {
LOG.warnv(e, "The bootstrap actions of EMR cluster {0} could not run", launch.clusterId());
outcome = BootstrapRunner.Outcome.couldNotRun(e.getMessage());
} finally {
// An image it kept is built on this one, which Docker then keeps, so a newer one may remove it only as that
// image goes.
if (image != null) {
releaseImages.released(image);
}
}
BootstrapRunner.Outcome ended = outcome;
// One that ended, however, after a teardown began ends as one Floci stopped, which starts no step: the
@@ -15,4 +15,11 @@ public interface ReleaseImages {
* the two; a failure is thrown.
*/
String image(String releaseLabel, String sparkImage, String hiveImage, boolean withHive);
/**
* The step or the bootstrap that {@link #image} gave {@code imageId} to has ended, so a newer image of its kind may
* remove it; until then it stays, as no container of it may be there yet to keep it. Once for each id given.
*/
default void released(String imageId) {
}
}