fix(lambda): make the code-volume populate concurrency configurable (#2830)

Concurrent first-time code-volume populates are capped by a semaphore sized
Math.max(2, availableProcessors() / 2). The JVM honours the container's cgroup
CPU quota, so a CPU-constrained Floci container derives 2 permits and concurrent
cold starts of *distinct* large functions complete in waves of two instead of in
parallel.

Measured on main, six distinct functions with 41MB packages, all cold, fired
concurrently, varying only the quota on Floci's own container:

  unlimited (16 cpu)  ->  8 permits  ->  one wave of 6   ->   8.3s
  --cpus=8            ->  4 permits  ->  waves of 4, 2   ->  13.2s
  --cpus=4            ->  2 permits  ->  waves of 2,2,2  ->  19.2s

The wave sizes track the formula exactly, which rules out generic CPU starvation
- that would slow all six uniformly rather than produce discrete equal-size waves.

The populate is IO-bound (creating a helper container and streaming a tar into
it), so CPU count is a weak proxy for how many the Docker daemon can absorb. The
cap itself is still worth having: the daemon overload it was added to prevent is
real. What was missing is a way to decouple it from the CPU allocation, so this
adds floci.services.lambda.code-volume-populate-concurrency
(FLOCI_SERVICES_LAMBDA_CODE_VOLUME_POPULATE_CONCURRENCY) and leaves the derived
default untouched.

Values below 1 fall back to the derived default with a warning: zero permits
would deadlock every populate, and a negative Semaphore count would make
acquire() block until that many releases arrived.

The semaphore becomes an instance field so the permit count can come from
config. Floci runs a single @ApplicationScoped launcher, so it is still one gate
for the whole emulator.

Refs #2050
This commit is contained in:
Brian Taylor authored and GitHub committed 2026-08-31 11:14:29 -04:00
1 parent 63a2a60a30
commit 968ff78e25
6 files changed
+134 -6

No files matched your search

@@ -1840,6 +1840,21 @@ public interface EmulatorConfig {
*/
Optional<String> containerNamePrefix();
/**
* Maximum concurrent first-time code-volume populates. Populating streams a large
* function's unpacked code into a helper container, so a burst of them can overwhelm
* the Docker daemon; this caps how many run at once.
*
* <p>Unset derives {@code max(2, availableProcessors() / 2)}. That derivation reads the
* JVM's view of the cgroup CPU quota, so a CPU-constrained Floci container collapses the
* cap to 2 and concurrent cold starts of distinct functions serialize into pairs. Set
* this to decouple the cap from the CPU allocation. Values below 1 are ignored with a
* warning rather than deadlocking every populate.
*
* Env var: FLOCI_SERVICES_LAMBDA_CODE_VOLUME_POPULATE_CONCURRENCY
*/
Optional<Integer> codeVolumePopulateConcurrency();
/**
* Extra /etc/hosts entries added to every Lambda container, as "hostname:ip" pairs.
* The ip may be the literal "host-gateway" to map to the Docker host, mirroring
@@ -146,6 +146,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
this.layerService = layerService;
this.awsEnv = awsEnv;
this.executionRoleCredentials = executionRoleCredentials;
this.populateSemaphore = new java.util.concurrent.Semaphore(resolvePopulateConcurrency(config));
}
@PostConstruct
@@ -603,12 +604,40 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
// overwhelmed the Docker daemon, so copies hung/failed and left half-built "Created" containers.
// This gates ONLY the heavy populate — not every launch — so ordinary cold starts (volume mounts
// for already-populated large code, or the small-code direct copy) are never serialized.
private static final java.util.concurrent.Semaphore POPULATE_SEMAPHORE =
new java.util.concurrent.Semaphore(Math.max(2, Runtime.getRuntime().availableProcessors() / 2));
//
// Per-instance rather than static so the permit count can come from configuration; Floci runs a
// single @ApplicationScoped launcher, so this is still one gate for the whole emulator.
private final java.util.concurrent.Semaphore populateSemaphore;
private static void acquirePopulatePermit(String functionName) {
/**
* Permits for {@link #populateSemaphore}: the configured value when set and positive,
* otherwise {@code max(2, availableProcessors() / 2)}.
*
* <p>The derived default reads the JVM's view of the cgroup CPU quota, so a CPU-constrained
* Floci container collapses it to 2 and concurrent cold starts of distinct functions
* serialize into pairs. The populate itself is IO-bound (streaming a tar into a helper
* container), so CPU count is a weak proxy for how many the daemon can take — hence the
* override. The default is unchanged, since the daemon overload the cap exists to prevent is
* real and its safe ceiling is host-specific.
*/
static int resolvePopulateConcurrency(EmulatorConfig config) {
int derived = Math.max(2, Runtime.getRuntime().availableProcessors() / 2);
Optional<Integer> configured = config.services().lambda().codeVolumePopulateConcurrency();
if (configured.isEmpty()) {
return derived;
}
int permits = configured.get();
if (permits < 1) {
LOG.warnv("Ignoring floci.services.lambda.code-volume-populate-concurrency {0}: must be "
+ "at least 1; using {1}", permits, derived);
return derived;
}
return permits;
}
private void acquirePopulatePermit(String functionName) {
try {
POPULATE_SEMAPHORE.acquire();
populateSemaphore.acquire();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted while waiting to populate code volume for " + functionName, ie);
@@ -871,7 +900,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
LOG.warnv("Could not remove code-volume helper {0}: {1}", helperId, e.getMessage());
}
}
POPULATE_SEMAPHORE.release();
populateSemaphore.release();
}
}
@@ -1097,7 +1126,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
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
// populateSemaphore 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)) {