perf(lambda): reuse a populated code volume across restarts via a completion marker (#1827)

* perf(lambda): reuse populated code volumes across restarts

* test(dynamodb): retain Quarkus port for cleanup

* chore: rerun CI after download outage

* ci: retry transient gosu downloads

---------

Co-authored-by: Alexander Banna <abanna@users.noreply.github.com>
This commit is contained in:
Alex BannaandAlexander Banna authored and GitHub committed 2026-08-13 16:27:37 -04:00
1 parent a7da1b9856
commit 5d61e95239
6 files changed
+549 -17

No files matched your search

@@ -5,6 +5,8 @@ import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.command.InspectVolumeResponse;
import com.github.dockerjava.api.command.ListVolumesResponse;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Bind;
@@ -26,6 +28,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
@@ -539,6 +542,34 @@ public class ContainerLifecycleManager {
}
}
/**
* Attempts to take an authoritative snapshot of all named volumes in the container runtime.
* An empty optional means the runtime could not be queried; it is intentionally distinct from
* a successful query that returned an empty set so cleanup callers fail closed.
*/
public Optional<Set<String>> tryListVolumeNames() {
try {
ListVolumesResponse response = dockerClient.listVolumesCmd().exec();
if (response == null) {
LOG.warn("Container runtime returned no volume-list response");
return Optional.empty();
}
List<InspectVolumeResponse> volumes = response.getVolumes();
if (volumes == null) {
LOG.warn("Container runtime returned a volume-list response without an inventory");
return Optional.empty();
}
Set<String> names = volumes.stream()
.map(InspectVolumeResponse::getName)
.filter(name -> name != null && !name.isBlank())
.collect(java.util.stream.Collectors.toUnmodifiableSet());
return Optional.of(names);
} catch (RuntimeException e) {
LOG.warnv("Failed to list container-runtime volumes: {0}", e.getMessage());
return Optional.empty();
}
}
private HostConfig buildHostConfig(ContainerSpec spec) {
HostConfig hostConfig = HostConfig.newHostConfig();
@@ -33,8 +33,10 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateFactory;
@@ -48,6 +50,7 @@ import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
/**
* Starts and stops Docker containers for Lambda function execution.
@@ -523,6 +526,8 @@ public class ContainerLauncher {
// `docker volume prune` would otherwise still read as "populated" here even though Docker has
// nothing under that name, silently mounting an empty /var/task into the next container.
// ensureCodeVolume re-checks lifecycleManager.volumeExists() rather than trusting this alone.
private static final String CODE_VOLUME_MARKER_DIR = "lambda-codevol-markers";
private static final String CODE_VOLUME_MARKER_PREFIX = "floci-code-";
private final java.util.Set<String> populatedCodeVolumes = java.util.concurrent.ConcurrentHashMap.newKeySet();
// Deliberately never pruned: removing an entry while a caller elsewhere still held a reference
// to its lock object let a third caller's computeIfAbsent create a replacement lock for the same
@@ -532,6 +537,7 @@ public class ContainerLauncher {
// combinations ever launched, each entry a single Object; negligible next to the disk space the
// rest of this fix reclaims.
private final java.util.concurrent.ConcurrentHashMap<String, Object> codeVolumeLocks = new java.util.concurrent.ConcurrentHashMap<>();
private final Object codeVolumeMarkerIoLock = new Object();
/** Each function's current code volume, so a redeploy can queue the superseded one for cleanup. */
private final java.util.concurrent.ConcurrentHashMap<String, String> functionCurrentVolume =
@@ -589,7 +595,7 @@ public class ContainerLauncher {
* throwaway helper container that has the volume mounted read-write; every real container then
* just mounts the volume read-only, turning a ~95s per-container copy into a ~0.2s mount.
*/
private String ensureCodeVolume(LambdaFunction fn, String image) {
String ensureCodeVolume(LambdaFunction fn, String image) {
String volName = codeVolumeName(fn);
// Held for the whole resolve-and-reconcile, not just the populate branch: this is the same
// lock cleanupSupersededVolumes acquires before claiming a volume for deletion, so a launch
@@ -605,15 +611,22 @@ public class ContainerLauncher {
Object lock = codeVolumeLocks.computeIfAbsent(volName, k -> new Object());
synchronized (lock) {
if (!(populatedCodeVolumes.contains(volName) && lifecycleManager.volumeExists(volName))) {
// Either never populated, or the bookkeeping says populated but Docker disagrees
// (removed out of band). Repopulate rather than trust a stale flag.
long t0 = System.currentTimeMillis();
LOG.infov("Populating code volume {0} for function {1} (one-time per code version)",
volName, fn.getFunctionName());
populateCodeVolume(volName, fn, image);
// Either never populated in this process, or the bookkeeping says populated but
// Docker disagrees (removed out of band). A completion marker from an earlier run
// is authoritative only while its independently persisted volume still exists.
if (reusePersistedCodeVolumeOrRemoveStaleMarker(volName)) {
LOG.infov("Reusing code volume {0} populated by a previous run (marker present)",
volName);
} else {
long t0 = System.currentTimeMillis();
LOG.infov("Populating code volume {0} for function {1} (one-time per code version)",
volName, fn.getFunctionName());
populateCodeVolume(volName, fn, image);
writeCodeVolumeMarkerAndPruneOrphans(volName);
LOG.infov("Populated code volume {0} in {1}ms; future cold starts mount it instead of copying",
volName, System.currentTimeMillis() - t0);
}
populatedCodeVolumes.add(volName);
LOG.infov("Populated code volume {0} in {1}ms; future cold starts mount it instead of copying",
volName, System.currentTimeMillis() - t0);
}
// Reconcile bookkeeping for the resolved volume even when the check above already found
// it populated: a redeploy rolled back to an older code version resolves a volume name
@@ -730,7 +743,7 @@ public class ContainerLauncher {
return count == null ? 0 : count.get();
}
private void populateCodeVolume(String volName, LambdaFunction fn, String image) {
void populateCodeVolume(String volName, LambdaFunction fn, String image) {
lifecycleManager.ensureVolume(volName);
String shortId = java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 8);
// A minimal helper container (sleep) with the volume mounted read-write at /var/task; we
@@ -749,16 +762,113 @@ public class ContainerLauncher {
try {
helperId = lifecycleManager.create(helperSpec);
lifecycleManager.startCreated(helperId, helperSpec);
copyDirToContainer(lifecycleManager.getDockerClient(), helperId,
copyDirToContainerStrict(lifecycleManager.getDockerClient(), helperId,
Path.of(fn.getCodeLocalPath()), TASK_DIR, fn.getFunctionName());
} finally {
if (helperId != null) {
try { lifecycleManager.stopAndRemove(helperId, null); } catch (Exception ignore) { /* best effort */ }
try {
lifecycleManager.stopAndRemove(helperId, null);
} catch (Exception e) {
LOG.warnv("Could not remove code-volume helper {0}: {1}", helperId, e.getMessage());
}
}
POPULATE_SEMAPHORE.release();
}
}
// A code volume survives an emulator restart, but the in-process populatedCodeVolumes set does not.
// A persisted completion marker lets a later run recognize an already-populated, content-addressed
// volume and skip re-copying its files. The marker is stored under storage.persistent-path
// regardless of storage mode; reuse still requires the independently persisted Docker volume.
private Path codeVolumeMarkerPath(String volName) {
return Path.of(config.storage().persistentPath(), CODE_VOLUME_MARKER_DIR, volName);
}
/**
* Reuses a completed volume when both marker and volume exist. If the volume is absent or cannot
* be verified, removes the marker before repopulation so a failed copy cannot leave the old
* completion signal beside partial data.
*/
private boolean reusePersistedCodeVolumeOrRemoveStaleMarker(String volName) {
synchronized (codeVolumeMarkerIoLock) {
Path marker = codeVolumeMarkerPath(volName);
if (!Files.isRegularFile(marker, java.nio.file.LinkOption.NOFOLLOW_LINKS)) {
return false;
}
java.util.Optional<java.util.Set<String>> volumeNames = lifecycleManager.tryListVolumeNames();
boolean inventoryUnavailable = volumeNames.isEmpty();
if (!inventoryUnavailable && volumeNames.get().contains(volName)) {
return true;
}
try {
Files.deleteIfExists(marker);
if (inventoryUnavailable) {
LOG.warnv("Could not verify backing volume {0}; removed its marker and re-populating",
volName);
} else {
LOG.debugv("Removed stale code-volume marker {0}; backing volume is absent", marker);
}
} catch (IOException e) {
throw new IllegalStateException("Could not invalidate code-volume marker " + marker, e);
}
return false;
}
}
private void writeCodeVolumeMarkerAndPruneOrphans(String volName) {
synchronized (codeVolumeMarkerIoLock) {
Path marker = codeVolumeMarkerPath(volName);
try {
Path markerDir = marker.getParent();
Files.createDirectories(markerDir);
Path tempMarker = Files.createTempFile(markerDir, marker.getFileName().toString(), ".tmp");
try {
Files.writeString(tempMarker, "");
try {
Files.move(tempMarker, marker, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE);
} catch (AtomicMoveNotSupportedException e) {
Files.move(tempMarker, marker, StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException | RuntimeException e) {
try {
Files.deleteIfExists(tempMarker);
} catch (IOException cleanupError) {
e.addSuppressed(cleanupError);
}
throw e;
}
} catch (IOException e) {
// A missing marker only costs one re-populate on the next boot; never fail a launch over it.
LOG.warnv("Could not write code-volume marker {0}: {1}", marker, e.getMessage());
}
pruneOrphanCodeVolumeMarkers(marker.getParent());
}
}
private void pruneOrphanCodeVolumeMarkers(Path markerDir) {
java.util.Optional<java.util.Set<String>> volumeNames = lifecycleManager.tryListVolumeNames();
if (volumeNames.isEmpty() || !Files.isDirectory(markerDir)) {
return;
}
try (java.util.stream.Stream<Path> markers = Files.list(markerDir)) {
markers.filter(path -> Files.isRegularFile(path, java.nio.file.LinkOption.NOFOLLOW_LINKS))
.filter(path -> path.getFileName().toString().startsWith(CODE_VOLUME_MARKER_PREFIX))
.filter(path -> !volumeNames.get().contains(path.getFileName().toString()))
.forEach(path -> {
try {
Files.deleteIfExists(path);
LOG.debugv("Pruned orphan code-volume marker {0}", path);
} catch (IOException e) {
LOG.warnv("Could not prune orphan code-volume marker {0}: {1}",
path, e.getMessage());
}
});
} catch (IOException e) {
LOG.warnv("Could not scan code-volume marker directory {0}: {1}", markerDir, e.getMessage());
}
}
/**
* Total code size (bytes) at or above which a function's {@code /var/task} is served from a
* shared read-only volume rather than copied into every container. This is roughly the point
@@ -843,26 +953,68 @@ public class ContainerLauncher {
*/
private static final int TAR_PIPE_BUFFER_BYTES = 16 * 1024 * 1024;
@FunctionalInterface
interface DirectoryTarWriter {
void write(Path sourceDir, OutputStream out) throws IOException;
}
private void copyDirToContainer(DockerClient dockerClient, String containerId,
Path sourceDir, String remotePath, String functionName) {
copyDirToContainer(dockerClient, containerId, sourceDir, remotePath, functionName,
ContainerLauncher::createTarFromDir);
}
static void copyDirToContainer(DockerClient dockerClient, String containerId,
Path sourceDir, String remotePath, String functionName,
DirectoryTarWriter tarWriter) {
copyDirToContainer(dockerClient, containerId, sourceDir, remotePath, functionName,
tarWriter, false);
}
private void copyDirToContainerStrict(DockerClient dockerClient, String containerId,
Path sourceDir, String remotePath, String functionName) {
copyDirToContainerStrict(dockerClient, containerId, sourceDir, remotePath, functionName,
ContainerLauncher::createTarFromDir);
}
static void copyDirToContainerStrict(DockerClient dockerClient, String containerId,
Path sourceDir, String remotePath, String functionName,
DirectoryTarWriter tarWriter) {
copyDirToContainer(dockerClient, containerId, sourceDir, remotePath, functionName,
tarWriter, true);
}
private static void copyDirToContainer(DockerClient dockerClient, String containerId,
Path sourceDir, String remotePath, String functionName,
DirectoryTarWriter tarWriter, boolean failOnTarFailure) {
// No per-copy gating here: the heavy /var/task populate for large code already holds a
// POPULATE_SEMAPHORE permit; small-code direct copies and layer copies are light enough
// to run unthrottled.
try (java.io.PipedOutputStream pos = new java.io.PipedOutputStream();
java.io.PipedInputStream pis = new java.io.PipedInputStream(pos, TAR_PIPE_BUFFER_BYTES)) {
new Thread(() -> {
AtomicReference<IOException> tarFailure = new AtomicReference<>();
Thread tarStreamer = new Thread(() -> {
try (pos) {
createTarFromDir(sourceDir, pos);
tarWriter.write(sourceDir, pos);
} catch (IOException e) {
LOG.errorv("Failed to stream tar for function {0}: {1}", functionName, e.getMessage());
if (failOnTarFailure) {
tarFailure.set(e);
} else {
LOG.errorv("Failed to stream directory tar for function {0}: {1}",
functionName, e.getMessage());
}
}
}, "tar-streamer-dir-" + functionName).start();
}, "tar-streamer-dir-" + functionName);
tarStreamer.start();
dockerClient.copyArchiveToContainerCmd(containerId)
.withRemotePath(remotePath)
.withTarInputStream(pis)
.exec();
if (failOnTarFailure) {
waitForTarStreamer(tarStreamer, tarFailure, functionName, sourceDir);
}
LOG.debugv("Copied directory {0} into container {1} at {2}", sourceDir, containerId, remotePath);
} catch (Exception e) {
// Fail loudly so launch() cleans up the half-built container instead of leaking it.
@@ -902,6 +1054,22 @@ public class ContainerLauncher {
}
}
private static void waitForTarStreamer(Thread tarStreamer, AtomicReference<IOException> tarFailure,
String functionName, Path sourcePath) throws IOException {
try {
tarStreamer.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Interrupted while streaming tar for function " + functionName
+ " from " + sourcePath, e);
}
IOException failure = tarFailure.get();
if (failure != null) {
throw new IOException("Failed to stream tar for function " + functionName
+ " from " + sourcePath, failure);
}
}
private static boolean isProvidedRuntime(String runtime) {
return runtime != null && runtime.startsWith("provided");
}