feat(lambdaweb): keep the code a revision was created from (#6113)

* feat(lambdaweb): keep the code a revision was created from

A revision reads its code object at creation and keeps its own copy under
the Lambda code path. New environments start from that copy, so a later
write or delete in the bucket no longer changes what the revision runs.
AWS pins the version that was current at creation when the request names
none, and serves it after that version is deleted.

A versionId that cannot be read gets the AWS wording. A 404 from S3 says
the object does not exist with the versionId, and any other error says the
object is inaccessible.

* fix(lambdaweb): start environments only from the kept code

Lambda Web is in no release yet, so every stored revision has its copy and
the S3 fallback could only run changed code. A missing copy is now an error.

At start the copies that no stored revision names are removed, such as
those a restart in memory mode or a failed delete left behind. A failed
write removes its partial file. Path segments escape every byte but
letters, digits, - and _, so two region names never share a directory.

* fix(lambdaweb): stream the code copy instead of holding it in memory

A revision now streams its code object from S3 into its copy, and an environment opens that copy in place through a new ZipExtractor overload. Neither step loads the object into the heap, and a start no longer writes the archive to disk again before unpacking it.

* chore: share one recursive delete across services

FileTrees.delete replaces the copies in Lambda Web, ZipExtractor, CodeStore, Lambda layers, CodeBuild, S3 and CodeArtifact. Each caller keeps its own log message and level. A failure no longer stops the rest of the tree, and the walk stream is always closed.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-21 00:51:57 -04:00
1 parent 03836d5244
commit bf82846971
15 files changed
+439 -106

No files matched your search

@@ -0,0 +1,43 @@
package io.github.hectorvent.floci.core.common;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class FileTreesTest {
@Test
void deletesADirectoryAndEverythingUnderIt(@TempDir Path dir) throws IOException {
Path root = dir.resolve("root");
Files.createDirectories(root.resolve("a").resolve("b"));
Files.writeString(root.resolve("a").resolve("b").resolve("file.txt"), "x");
Files.writeString(root.resolve("top.txt"), "y");
Files.writeString(dir.resolve("sibling.txt"), "z");
FileTrees.delete(root);
assertFalse(Files.exists(root));
assertTrue(Files.exists(dir.resolve("sibling.txt")));
}
@Test
void deletesASingleFile(@TempDir Path dir) throws IOException {
Path file = Files.writeString(dir.resolve("file.txt"), "x");
FileTrees.delete(file);
assertFalse(Files.exists(file));
}
@Test
void aMissingPathIsNotAnError(@TempDir Path dir) {
assertDoesNotThrow(() -> FileTrees.delete(dir.resolve("missing")));
}
}
@@ -453,6 +453,20 @@ class ZipExtractorTest {
}
}
@Test
void extractsAnArchiveFileWithoutStagingACopy(@TempDir Path codeStore) throws IOException {
Path archive = codeStore.resolve("code.zip");
Files.write(archive, zipWith("index.js", "v1"));
Path target = codeStore.resolve("fn");
extractor.extractTo(archive, target, ZipExtractor.DEFAULT_MAX_ENTRIES);
assertEquals("v1", Files.readString(target.resolve("index.js")));
try (Stream<Path> entries = Files.list(codeStore)) {
assertEquals(List.of("code.zip", "fn"), entries.map(p -> p.getFileName().toString()).sorted().toList());
}
}
@Test
void rejectsAnEntryWhoseCompressionMethodCannotBeRead(@TempDir Path target) throws IOException {
// A package must never be half-deployed because an entry used a codec the reader does
@@ -403,6 +403,15 @@ class LambdaWebDataPlaneDockerIntegrationTest {
.then().statusCode(200).header("lambda-web-logstream", not(equalTo(logStream)));
}
@Test
@Timeout(180)
void anEnvironmentRunsTheCodeTheRevisionWasCreatedFrom() throws Exception {
String domain = createFunction(30);
s3Service.putObject(BUCKET, functionName + ".zip", zip("process.exit(1);"), "application/zip", Map.of());
given().header("Host", domain).when().get("/status/200").then().statusCode(200).body(equalTo("status body"));
}
@Test
@Timeout(240)
void environmentsScaleUpToMaxEnvironmentsAndTheRateLimitThrottles() throws Exception {
@@ -16,9 +16,15 @@ import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.lambdaweb.model.WebFunction;
import io.github.hectorvent.floci.services.lambdaweb.model.WebFunctionEndpoint;
import io.github.hectorvent.floci.services.lambdaweb.model.WebFunctionRevision;
import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.services.s3.model.S3Object;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
@@ -30,6 +36,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -54,10 +61,14 @@ class LambdaWebServiceTest {
}
private LambdaWebService service(StorageFactory storage, String baseUrl) {
return service(storage, baseUrl, null);
}
private LambdaWebService service(StorageFactory storage, String baseUrl, S3Service s3Service) {
EmulatorConfig config = mock(EmulatorConfig.class);
when(config.effectiveBaseUrl()).thenReturn(baseUrl);
LambdaWebService service = new LambdaWebService(new RegionResolver(REGION, ACCOUNT_ID), storage, config, null,
mapper, null);
LambdaWebService service = new LambdaWebService(new RegionResolver(REGION, ACCOUNT_ID), storage, config,
s3Service, new LambdaWebCode(storageDir.resolve("code")), mapper, null);
service.initializeStorage();
return service;
}
@@ -587,6 +598,81 @@ class LambdaWebServiceTest {
assertTrue(service.endpointDomain(host).isEmpty(), "a deleted endpoint's domain is gone");
}
@Test
void eachRevisionKeepsTheCodeTheObjectHadWhenItWasCreated() throws IOException {
S3Service s3 = mock(S3Service.class);
LambdaWebService service = service(new MemoryStorageFactory(), "http://localhost:4566", s3);
LambdaWebCode code = new LambdaWebCode(storageDir.resolve("code"));
when(s3.openObject("code", "app.zip", null)).thenReturn(object("first"));
String first = service.createFunction(REGION, createRequest("app")).revisions.keySet().iterator().next();
when(s3.openObject("code", "app.zip", null)).thenReturn(object("second"));
String second = service.createRevision(REGION, "app", revisionConfig(30)).revisionId;
assertEquals("first", Files.readString(code.file(ACCOUNT_ID, REGION, "app", first)));
assertEquals("second", Files.readString(code.file(ACCOUNT_ID, REGION, "app", second)));
service.deleteRevision(REGION, "app", first);
assertFalse(Files.exists(code.file(ACCOUNT_ID, REGION, "app", first)));
service.deleteFunction(REGION, "app");
assertFalse(Files.exists(code.file(ACCOUNT_ID, REGION, "app", second)));
}
@Test
void aStartKeepsOnlyTheCopiesOfStoredRevisions() throws IOException {
S3Service s3 = mock(S3Service.class);
when(s3.openObject("code", "app.zip", null)).thenReturn(object("code"));
FileStorageFactory storage = new FileStorageFactory(storageDir);
String revisionId = service(storage, "http://localhost:4566", s3).createFunction(REGION, createRequest("app"))
.revisions.keySet().iterator().next();
LambdaWebCode code = new LambdaWebCode(storageDir.resolve("code"));
code.store(ACCOUNT_ID, REGION, "app", "rev-stray", new ByteArrayInputStream(new byte[] {1}));
service(new FileStorageFactory(storageDir), "http://localhost:4566", s3);
assertEquals("code", Files.readString(code.file(ACCOUNT_ID, REGION, "app", revisionId)));
assertFalse(Files.exists(code.file(ACCOUNT_ID, REGION, "app", "rev-stray")));
service(new MemoryStorageFactory(), "http://localhost:4566", s3);
assertFalse(Files.exists(code.file(ACCOUNT_ID, REGION, "app", revisionId)));
}
@Test
void regionsThatLookAlikeKeepTheirOwnCopies() throws IOException {
LambdaWebCode code = new LambdaWebCode(storageDir.resolve("code"));
code.store(ACCOUNT_ID, ".", "app", "rev-aaaaa", new ByteArrayInputStream(new byte[] {1}));
code.store(ACCOUNT_ID, "_", "app", "rev-aaaaa", new ByteArrayInputStream(new byte[] {2}));
code.deleteFunction(ACCOUNT_ID, ".", "app");
assertEquals(2, Files.readAllBytes(code.file(ACCOUNT_ID, "_", "app", "rev-aaaaa"))[0]);
}
@Test
void aVersionThatCannotBeReadIsNamedInTheError() {
S3Service s3 = mock(S3Service.class);
LambdaWebService service = service(new MemoryStorageFactory(), "http://localhost:4566", s3);
ObjectNode pinned = createRequest("app");
((ObjectNode) pinned.path("revisionConfig").path("buildConfig").path("codeConfig").path("s3Object"))
.put("versionId", "v1");
when(s3.openObject("code", "app.zip", "v1"))
.thenThrow(new AwsException("NoSuchVersion", "The specified version does not exist.", 404));
assertEquals("The specified S3 object does not exist: s3://code/app.zip (versionId=v1). Verify the object exists"
+ " and the versionId is correct.", message(() -> service.createFunction(REGION, pinned)));
doThrow(new AwsException("InvalidArgument", "Invalid version id specified", 400))
.when(s3).openObject("code", "app.zip", "v1");
assertEquals("The S3 object s3://code/app.zip (versionId=v1) is inaccessible. Verify the request parameters and"
+ " permissions are correct.", message(() -> service.createFunction(REGION, pinned)));
when(s3.openObject("code", "app.zip", null))
.thenThrow(new AwsException("NoSuchKey", "The specified key does not exist.", 404));
assertEquals("The specified S3 object does not exist: s3://code/app.zip. Verify the object exists in the bucket.",
message(() -> service.createFunction(REGION, createRequest("app"))));
}
private static S3Service.ObjectRead object(String data) {
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
return new S3Service.ObjectRead(new S3Object("code", "app.zip", bytes, "application/zip"),
new ByteArrayInputStream(bytes));
}
private static final class MemoryStorageFactory extends StorageFactory {
private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();