From b3374d073a797b3c2127768d270c4e4a2de7f382 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sat, 21 Nov 2026 22:47:29 -0600 Subject: [PATCH] chore(lambda): bound the warm pool to 128 idle containers by default (#9173) --- .../configuration/advanced/application-yml.md | 2 +- docs/configuration/environment-variables.md | 2 +- docs/services/lambda.md | 25 ++++++++++--------- .../io/floci/aws/config/EmulatorConfig.java | 7 +++--- .../lambda/ProvisionedConcurrencyPool.java | 7 +++++- src/main/resources/application.yml | 2 +- .../aws/config/ApplicationDefaultsTest.java | 18 +++++++++++++ .../ProvisionedConcurrencyPoolTest.java | 19 ++++++++++++++ 8 files changed, 63 insertions(+), 19 deletions(-) diff --git a/docs/configuration/advanced/application-yml.md b/docs/configuration/advanced/application-yml.md index 41220c018c..7b78484bb4 100644 --- a/docs/configuration/advanced/application-yml.md +++ b/docs/configuration/advanced/application-yml.md @@ -189,7 +189,7 @@ floci: container-idle-timeout-seconds: 300 # Remove idle containers after this init-timeout-seconds: 10 # Time a cold runtime has to request its first invocation; the function's Timeout wins when longer # warm-pool-max-per-function: # Idle containers kept per function (default max(4, cpus)) - warm-pool-max-total: 0 # Idle containers kept across all functions; 0 = unbounded + warm-pool-max-total: 128 # Idle containers kept across all functions; 0 = unbounded region-concurrency-limit: 1000 # Concurrent executions ceiling per region unreserved-concurrency-min: 100 # Minimum unreserved capacity PutFunctionConcurrency must leave hot-reload: diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md index 614eff6b9f..80b95d7e81 100644 --- a/docs/configuration/environment-variables.md +++ b/docs/configuration/environment-variables.md @@ -322,7 +322,7 @@ See [Initialization Hooks](./initialization-hooks.md) for lifecycle phases and s | `FLOCI_SERVICES_LAMBDA_INIT_TIMEOUT_SECONDS` | `10` | Seconds a newly started runtime has, counted from its container's start, to finish its init and request its first invocation; warm containers are not affected; a function whose `Timeout` plus 2 seconds is longer waits that long instead. The function's `Timeout` starts at dispatch. See [Slow cold starts](../services/lambda.md#slow-cold-starts) | | `FLOCI_SERVICES_LAMBDA_CONTAINER_DRAIN_TIMEOUT_SECONDS` | `15` | Shared shutdown drain budget for Lambda containers; 1 through 300 seconds, invalid values fail initialization | | `FLOCI_SERVICES_LAMBDA_WARM_POOL_MAX_PER_FUNCTION` | `max(4, cpus)` | Maximum idle (warm) containers kept per function; a container released beyond this is stopped | -| `FLOCI_SERVICES_LAMBDA_WARM_POOL_MAX_TOTAL` | `0` | Maximum idle (warm) containers kept across all functions. Beyond it, the least-recently-used idle container of any function is stopped to make room for the one just released. `0` leaves the total unbounded; a negative value is ignored with a warning. See [Bounding warm containers](../services/lambda.md#bounding-warm-containers) | +| `FLOCI_SERVICES_LAMBDA_WARM_POOL_MAX_TOTAL` | `128` | Maximum idle (warm) containers kept across all functions. Beyond it, the least-recently-used idle container of any function is stopped to make room for the one just released. `0` leaves the total unbounded; a negative value is ignored with a warning. See [Bounding warm containers](../services/lambda.md#bounding-warm-containers) | | `FLOCI_SERVICES_LAMBDA_REGION_CONCURRENCY_LIMIT` | `1000` | Maximum concurrent Lambda invocations across all functions in a region | | `FLOCI_SERVICES_LAMBDA_UNRESERVED_CONCURRENCY_MIN` | `100` | Minimum unreserved concurrency pool | | `FLOCI_SERVICES_LAMBDA_CODE_VOLUME_POPULATE_CONCURRENCY` | `max(2, cpus/2)` | Maximum concurrent first-time code-volume populates (functions whose unpacked code is at least 32 MB). The default is derived from the CPU count the JVM sees, so a CPU-constrained Floci container collapses it to 2 and concurrent cold starts of distinct functions serialise into pairs; set this to decouple the cap from the CPU allocation | diff --git a/docs/services/lambda.md b/docs/services/lambda.md index e78cd1d98f..49cdcecc66 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -667,8 +667,8 @@ unsupported weighted routing. This implementation supports the non-ephemeral Docker executor with immutable published versions and simple aliases. It refuses `$LATEST`, weighted aliases, hot reload, durable functions and other executors. Allocations share the configured `warm-pool-max-per-function` -ceiling across a function's qualifiers, and the optional `warm-pool-max-total` ceiling across -provisioned allocations. A function's reserved concurrency is also a ceiling. These local +ceiling across a function's qualifiers; `warm-pool-max-total`, which bounds idle warm +containers, does not apply to them. A function's reserved concurrency is also a ceiling. These local resource bounds do not implement AWS's complete regional provisioned-concurrency quota accounting, automatic scaling, or weighted-alias allocation. Provisioned runtimes receive their numeric `AWS_LAMBDA_FUNCTION_VERSION` and @@ -742,7 +742,7 @@ Configuration resource reads and `ListFunctionsByCodeSigningConfig` return | `FLOCI_SERVICES_LAMBDA_CONTAINER_IDLE_TIMEOUT_SECONDS` | `300` | Idle container shutdown timeout (seconds) | | `FLOCI_SERVICES_LAMBDA_INIT_TIMEOUT_SECONDS` | `10` | Time a newly started runtime has to finish its init and request its first invocation (seconds). A function whose `Timeout` plus 2 seconds is longer waits that long instead. See [Slow cold starts](#slow-cold-starts) | | `FLOCI_SERVICES_LAMBDA_WARM_POOL_MAX_PER_FUNCTION` | `max(4, cpus)` | Maximum idle (warm) containers kept per function | -| `FLOCI_SERVICES_LAMBDA_WARM_POOL_MAX_TOTAL` | `0` | Maximum idle (warm) containers kept across all functions, LRU-evicted beyond it. `0` = unbounded. See the note below | +| `FLOCI_SERVICES_LAMBDA_WARM_POOL_MAX_TOTAL` | `128` | Maximum idle (warm) containers kept across all functions, LRU-evicted beyond it. `0` = unbounded. See the note below | | `FLOCI_SERVICES_LAMBDA_REGION_CONCURRENCY_LIMIT` | `1000` | Maximum concurrent executions per region | | `FLOCI_SERVICES_LAMBDA_UNRESERVED_CONCURRENCY_MIN` | `100` | Minimum unreserved capacity `PutFunctionConcurrency` must leave | | `FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ENABLED` | `false` | Enable bind-mount hot-reload via `S3Bucket=hot-reload` | @@ -817,15 +817,16 @@ Configuration resource reads and `ListFunctionsByCodeSigningConfig` return !!! note "Bounding warm containers" After an invocation the container stays warm so the next call skips the cold start, and is reaped only after `container-idle-timeout-seconds` of inactivity. Each function keeps up to - `warm-pool-max-per-function` idle containers, but nothing limits the sum: a stack of many - functions that fan out through streams and triggers can accumulate one warm container per - function touched, and each runtime container holds its own memory. On a Docker Desktop with - a modest memory allocation that is enough to have Floci OOM-killed. `ephemeral: true` avoids - it by cold-starting every invocation, at a large latency cost. + `warm-pool-max-per-function` idle containers, and a stack of many functions that fan out + through streams and triggers can accumulate one warm container per function touched, each + holding its own memory. On a Docker Desktop with a modest memory allocation that is enough to + have Floci OOM-killed. `ephemeral: true` avoids it by cold-starting every invocation, at a + large latency cost. - `warm-pool-max-total` bounds the idle containers across all functions. When a release - would exceed it, the least-recently-used idle container of any function is stopped first, - so the function that just ran (the most likely to run again) keeps its container: + `warm-pool-max-total` bounds the idle containers across all functions, 128 by default. When a + release would exceed it, the least-recently-used idle container of any function is stopped + first, so the function that just ran (the most likely to run again) keeps its container. Lower + it on a small Docker host: ```bash FLOCI_SERVICES_LAMBDA_WARM_POOL_MAX_TOTAL=24 @@ -836,7 +837,7 @@ Configuration resource reads and `ListFunctionsByCodeSigningConfig` return peak container count is `warm-pool-max-total` plus the in-flight invocations across every active region. A cap that is smaller than the number of functions invoked in a tight loop trades some warm hits for the memory bound. A negative value is ignored with a warning and - leaves the total unbounded, like `0`. + leaves the total unbounded, like `0`, which turns the bound off. ### Slow cold starts diff --git a/src/main/java/io/floci/aws/config/EmulatorConfig.java b/src/main/java/io/floci/aws/config/EmulatorConfig.java index f577833bc4..3e07921da1 100644 --- a/src/main/java/io/floci/aws/config/EmulatorConfig.java +++ b/src/main/java/io/floci/aws/config/EmulatorConfig.java @@ -3594,15 +3594,16 @@ public interface EmulatorConfig { * Maximum idle (warm) containers kept across all functions. When a release would push * the total past this, the least-recently-used idle container of any function is * stopped first, so the container that just ran (the most likely to run again) stays - * warm. {@code 0} disables the bound; a negative value is ignored with a warning and - * also leaves it unbounded. This bounds idle containers only, emulator-wide. Busy + * warm. The default, 128, keeps a stack of many functions from accumulating one warm + * container per function touched. {@code 0} disables the bound; a negative value is ignored + * with a warning and also leaves it unbounded. This bounds idle containers only, emulator-wide. Busy * containers are not counted: their ceiling is {@code region-concurrency-limit}, which * applies independently in each region, so peak container count is this cap plus the * in-flight invocations across every active region. * * Env var: FLOCI_SERVICES_LAMBDA_WARM_POOL_MAX_TOTAL */ - @WithDefault("0") + @WithDefault("128") int warmPoolMaxTotal(); /** Docker network to attach Lambda containers to. Empty = default bridge. */ diff --git a/src/main/java/io/floci/aws/services/lambda/ProvisionedConcurrencyPool.java b/src/main/java/io/floci/aws/services/lambda/ProvisionedConcurrencyPool.java index d67194fcf6..6ec15f6d2a 100644 --- a/src/main/java/io/floci/aws/services/lambda/ProvisionedConcurrencyPool.java +++ b/src/main/java/io/floci/aws/services/lambda/ProvisionedConcurrencyPool.java @@ -109,13 +109,18 @@ public class ProvisionedConcurrencyPool implements Resettable { } } + /** + * Provisioned environments are not bounded by {@code warm-pool-max-total}, which bounds idle warm + * containers: as on AWS, their ceilings are the function's reserved concurrency and the region's + * concurrency, plus {@code warm-pool-max-per-function} locally. + */ @Inject public ProvisionedConcurrencyPool(LambdaRuntimeLauncher launcher, LambdaFunctionStore functions, LambdaAliasStore aliases, StorageFactory storageFactory, EmulatorConfig config, Clock clock) { this(launcher, functions, aliases, storageFactory.create("lambda", "lambda-provisioned-concurrency.json", new TypeReference<>() {}), - clock, WarmPool.resolveMaxPerFunction(config), WarmPool.resolveMaxTotal(config), + clock, WarmPool.resolveMaxPerFunction(config), 0, config.services().lambda().ephemeral(), WarmPool.resolveDrainTimeout(config)); } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 63165eaad1..95127c2402 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -518,7 +518,7 @@ floci: init-timeout-seconds: 10 # FLOCI_SERVICES_LAMBDA_INIT_TIMEOUT_SECONDS - time a cold runtime has to request its first invocation; the function's Timeout wins when longer container-drain-timeout-seconds: 15 # FLOCI_SERVICES_LAMBDA_CONTAINER_DRAIN_TIMEOUT_SECONDS - shared shutdown budget for both Lambda pools # warm-pool-max-per-function: # FLOCI_SERVICES_LAMBDA_WARM_POOL_MAX_PER_FUNCTION - idle containers kept per function (default max(4, cpus)) - warm-pool-max-total: 0 # FLOCI_SERVICES_LAMBDA_WARM_POOL_MAX_TOTAL - idle containers kept across all functions, LRU evicted beyond it; 0 = unbounded + warm-pool-max-total: 128 # FLOCI_SERVICES_LAMBDA_WARM_POOL_MAX_TOTAL - idle containers kept across all functions, LRU evicted beyond it; 0 = unbounded. Keep in step with the @WithDefault on EmulatorConfig.LambdaServiceConfig.warmPoolMaxTotal region-concurrency-limit: 1000 unreserved-concurrency-min: 100 accept-external-layer-arns: false # FLOCI_SERVICES_LAMBDA_ACCEPT_EXTERNAL_LAYER_ARNS - accept a Layers ARN from another account (content is never mounted). AWS answers these through the layer resource policy, which Floci does not model, so the default refuses them like AWS refuses a non-public one. Turn on to attach public layers such as Powertools. diff --git a/src/test/java/io/floci/aws/config/ApplicationDefaultsTest.java b/src/test/java/io/floci/aws/config/ApplicationDefaultsTest.java index 001523eb31..174ec93727 100644 --- a/src/test/java/io/floci/aws/config/ApplicationDefaultsTest.java +++ b/src/test/java/io/floci/aws/config/ApplicationDefaultsTest.java @@ -246,4 +246,22 @@ class ApplicationDefaultsTest { assertEquals("0", EmulatorConfig.DockerConfig.class.getMethod("maxConcurrentLaunches") .getAnnotation(WithDefault.class).value(), "the @WithDefault must match application.yml"); } + + // Catches: warm-pool-max-total shipped unbounded again, so a stack of many functions keeps one + // warm container per function touched, or a yml value that disagrees with the @WithDefault. + @Test + void productionConfigBoundsTheLambdaWarmPoolTotalByDefault() throws IOException, NoSuchMethodException { + JsonNode maxTotal = productionConfig() + .path("floci").path("services").path("lambda").path("warm-pool-max-total"); + + assertFalse(maxTotal.isMissingNode(), "warm-pool-max-total should be declared in application.yml"); + assertEquals(128, maxTotal.asInt()); + + WithDefault fallback = EmulatorConfig.LambdaServiceConfig.class + .getMethod("warmPoolMaxTotal") + .getAnnotation(WithDefault.class); + assertNotNull(fallback, "warmPoolMaxTotal should declare a fallback default"); + assertEquals("128", fallback.value(), + "the @WithDefault must match application.yml, which wins over it"); + } } diff --git a/src/test/java/io/floci/aws/services/lambda/ProvisionedConcurrencyPoolTest.java b/src/test/java/io/floci/aws/services/lambda/ProvisionedConcurrencyPoolTest.java index 6cda8166e8..1d203eb70a 100644 --- a/src/test/java/io/floci/aws/services/lambda/ProvisionedConcurrencyPoolTest.java +++ b/src/test/java/io/floci/aws/services/lambda/ProvisionedConcurrencyPoolTest.java @@ -1,5 +1,6 @@ package io.floci.aws.services.lambda; +import com.fasterxml.jackson.core.type.TypeReference; import io.floci.aws.config.EmulatorConfig; import io.floci.aws.core.common.AwsArnUtils; import io.floci.aws.core.common.AwsException; @@ -438,6 +439,24 @@ class ProvisionedConcurrencyPoolTest { assertEquals(1, first.stops.get()); } + // Catches: the idle warm-pool total (128 by default) capping provisioned concurrency again, which + // AWS bounds only by reserved and regional concurrency. + @Test + @SuppressWarnings("unchecked") + void theWarmPoolTotalDoesNotBoundProvisionedConcurrency() { + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.services().lambda().warmPoolMaxTotal()).thenReturn(1); + when(config.services().lambda().warmPoolMaxPerFunction()).thenReturn(Optional.of(4)); + when(config.services().lambda().containerDrainTimeoutSeconds()).thenReturn(15); + StorageFactory storageFactory = mock(StorageFactory.class); + when(storageFactory.create(eq("lambda"), eq("lambda-provisioned-concurrency.json"), any(TypeReference.class))) + .thenReturn(storage); + pool.shutdown(); + pool = new ProvisionedConcurrencyPool(launcher, functions, aliases, storageFactory, config, Clock.systemUTC()); + + assertEquals(2, put(2).requested()); + } + @ParameterizedTest @CsvSource({"1,0,false", "4,1,false", "4,1,true"}) void retiredLeasesStillCountAgainstContainerCeilings(int perFunction, int total,