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

@@ -0,0 +1,62 @@
package io.github.hectorvent.floci.services.lambda.launcher;
import io.github.hectorvent.floci.config.EmulatorConfig;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Tests for the configurable code-volume populate concurrency
* ({@code floci.services.lambda.code-volume-populate-concurrency}). Plain {@code mock()} rather
* than the Mockito extension, for the same strict-stubbing reason as
* {@link ContainerLauncherNamePrefixTest}.
*/
class ContainerLauncherPopulateConcurrencyTest {
private static int derivedDefault() {
return Math.max(2, Runtime.getRuntime().availableProcessors() / 2);
}
@Test
void derivesFromCpuCountWhenUnset() {
assertEquals(derivedDefault(), ContainerLauncher.resolvePopulateConcurrency(config(null)));
}
@Test
void usesConfiguredValueWhenSet() {
assertEquals(1, ContainerLauncher.resolvePopulateConcurrency(config(1)));
assertEquals(16, ContainerLauncher.resolvePopulateConcurrency(config(16)));
}
@Test
void configuredValueDecouplesTheCapFromTheCpuCount() {
// The point of the option: a CPU-constrained container derives 2, which serializes
// concurrent cold starts of distinct large functions into pairs. An explicit value must
// win over the derivation whether it is above or below it.
int derived = derivedDefault();
assertEquals(derived + 4, ContainerLauncher.resolvePopulateConcurrency(config(derived + 4)));
assertEquals(1, ContainerLauncher.resolvePopulateConcurrency(config(1)));
}
@Test
void ignoresNonPositiveValues() {
// Zero permits would deadlock every populate, and a negative Semaphore count would make
// acquire() block until as many releases arrived. Fall back instead of hanging.
assertEquals(derivedDefault(), ContainerLauncher.resolvePopulateConcurrency(config(0)));
assertEquals(derivedDefault(), ContainerLauncher.resolvePopulateConcurrency(config(-1)));
}
private static EmulatorConfig config(Integer permits) {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.LambdaServiceConfig lambda = mock(EmulatorConfig.LambdaServiceConfig.class);
when(config.services()).thenReturn(services);
when(services.lambda()).thenReturn(lambda);
when(lambda.codeVolumePopulateConcurrency()).thenReturn(Optional.ofNullable(permits));
return config;
}
}