Light, fluffy, and always free - The AWS Local Emulator alternative
perf(lambda): avoid code-copy helper shutdown delay (#5646)
Andre Kuraitf08a443ec92c
diff --git a/docs/services/lambda.md b/docs/services/lambda.mdindex 067be5c98b..da53951886 100644--- a/docs/services/lambda.md+++ b/docs/services/lambda.md@@ -534,6 +534,10 @@ These AWS Lambda operations have no handler in Floci. Calls will return `404` orOnly first-time populates are gated. Warm containers, already-populated volumes, andfunctions under 32 MB are never serialised by this.+ The temporary copy helper is removed without a stop grace period once copying+ finishes or fails. It runs no Lambda code; actual execution environments retain+ their normal shutdown and extension-drain behavior.+!!! note "Bounding warm containers"After an invocation the container stays warm so the next call skips the cold start, and isreaped only after `container-idle-timeout-seconds` of inactivity. Each function keeps up todiff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.javaindex 38097e4bf1..d5719edb94 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java@@ -989,7 +989,8 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {} finally {if (helperId != null) {try {- lifecycleManager.stopAndRemove(helperId, null);+ // This helper only sleeps while the copy runs; it has no runtime to drain.+ lifecycleManager.stopAndRemove(helperId, null, 0);} catch (Exception e) {LOG.warnv("Could not remove code-volume helper {0}: {1}", helperId, e.getMessage());}diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.javaindex 8508de431b..f98798ae5e 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java@@ -1017,6 +1017,7 @@ class ContainerLauncherTest {fn.setHandler("index.handler");fn.setCodeLocalPath(codePath.toString());fn.setCodeSha256("large-fn-sha-v1");+ when(lifecycleManager.create(any())).thenReturn("helper-123", "container-123");long original = ContainerLauncher.CODE_VOLUME_MIN_BYTES;try {@@ -1042,13 +1043,49 @@ class ContainerLauncherTest {// Two creates: the helper + the real container. The helper is discarded.verify(lifecycleManager, times(2)).create(any());verify(lifecycleManager, atLeastOnce()).ensureVolume(any());- verify(lifecycleManager, times(1)).stopAndRemove(any(), any()); // the helper only+ verify(lifecycleManager).stopAndRemove("helper-123", null, 0);+ verify(lifecycleManager, never()).stopAndRemove(any(), any());+ InOrder order = inOrder(lifecycleManager);+ order.verify(lifecycleManager).stopAndRemove("helper-123", null, 0);+ order.verify(lifecycleManager).create(any());+ order.verify(lifecycleManager).startCreated(eq("container-123"), any());// A superseded code-version volume is never deleted synchronously within a single launch// (see the cleanupSupersededVolumes tests below for the deferred sweep; this fn has no// prior volume to supersede, since it's the fn's first deploy here).verify(lifecycleManager, never()).removeVolume(any());}+ @Test+ void launchFunction_failedCodeCopyImmediatelyRemovesOnlyItsHelper() throws Exception {+ Path codePath = Files.createDirectory(tempDir.resolve("failed-code-copy"));+ Files.write(codePath.resolve("bundle.bin"), new byte[8 * 1024]);+ LambdaFunction fn = new LambdaFunction();+ fn.setFunctionName("copy-failure");+ fn.setRuntime("nodejs20.x");+ fn.setHandler("index.handler");+ fn.setCodeLocalPath(codePath.toString());+ fn.setCodeSha256("copy-failure-sha");+ when(lifecycleManager.create(any())).thenReturn("helper-123");+ doThrow(new IllegalStateException("fixture copy failed"))+ .when(dockerClient).copyArchiveToContainerCmd("helper-123");++ withLoweredCodeVolumeThreshold(() -> {+ RuntimeException failure = assertThrows(RuntimeException.class, () -> launcher.launch(fn));+ assertTrue(failure.getMessage().contains("fixture copy failed"));+ });++ InOrder order = inOrder(lifecycleManager, dockerClient);+ order.verify(lifecycleManager).startCreated(eq("helper-123"), any());+ order.verify(dockerClient).copyArchiveToContainerCmd("helper-123");+ order.verify(lifecycleManager).stopAndRemove("helper-123", null, 0);+ verify(lifecycleManager, times(1)).create(any());+ verify(lifecycleManager, never()).stopAndRemove(any(), any());+ verify(lifecycleManager, never()).removeVolume(any());+ verify(runtimeApiServerFactory).release(runtimeApiServer);+ assertFalse(Files.exists(tempDir.resolve("lambda-codevol-markers")+ .resolve(ContainerLauncher.codeVolumeName(fn))));+ }+@Testvoid launchFunction_releasesRuntimeApiServer_whenCodeVolumePopulateFails() throws Exception {// Regression for the runtime-api port leak: the volume path allocates a runtime-api server@@ -1915,4 +1952,14 @@ class ContainerLauncherTest {assertEquals(List.of("/opt/extensions/lambda-adapter", "/opt/extensions/otel-collector"),capturedLaunchPaths(launchCmds));}+ private static void withLoweredCodeVolumeThreshold(Runnable action) {+ long original = ContainerLauncher.CODE_VOLUME_MIN_BYTES;+ try {+ ContainerLauncher.CODE_VOLUME_MIN_BYTES = 4 * 1024;+ action.run();+ } finally {+ ContainerLauncher.CODE_VOLUME_MIN_BYTES = original;+ }+ }+}