diff --git a/docs/services/lambda.md b/docs/services/lambda.md index f6d5692c82..0f66bcd99b 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -243,7 +243,8 @@ Not emulated yet: ## Hot-Reloading via Reactive S3 Sync -Floci supports an automatic hot-reloading mechanism when functions are deployed via S3. This follows the standard AWS behavior where S3 and Lambda interact, but is optimized for a seamless local development experience. +Floci supports automatic hot reload for S3 deployments as a local development convenience. +AWS normally copies the deployment package: overwriting its S3 object alone does not redeploy the function. When a Lambda function is created using an S3 bucket and key, Floci maintains a link between the function and its source object. Any subsequent update to that S3 object (e.g., via `s3:PutObject`) automatically triggers a reactive synchronization: @@ -254,6 +255,9 @@ When a Lambda function is created using an S3 bucket and key, Floci maintains a This allows you to update your Lambda code by simply re-uploading your ZIP to S3, without having to manually call `UpdateFunctionCode` or restart any containers. +Published versions retain their own code snapshots. Reactive sync updates only `$LATEST`; +publication and extraction share the per-function lock so a version cannot capture a partial update. + ### Example ```bash @@ -273,8 +277,8 @@ aws s3 cp updated-function.zip s3://my-bucket/function.zip aws lambda invoke --function-name my-function out.json ``` -!!! note "Standard Behavior" - This mechanism requires no custom configuration or non-standard magic strings. It works with standard AWS SDKs and CLI tools, providing a "live" development feel while staying within the AWS API contract. +!!! note "Local development behavior" + This mechanism requires no custom configuration or special strings. It uses standard AWS SDK and CLI calls; automatic redeployment after an S3 upload is a Floci convenience. ## Hot-Reload via Bind Mount diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java index 5c99f84044..da1cea2ebe 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java @@ -765,9 +765,9 @@ public class LambdaService implements ResourceProvider { requireMatchingRevisionId(fn, request); if (zipFileBase64 != null) { + extractZipCode(fn, zipFileBase64, region); fn.setS3Bucket(null); fn.setS3Key(null); - extractZipCode(fn, zipFileBase64, region); } if (imageUri != null) { fn.setImageUri(imageUri); @@ -2292,14 +2292,7 @@ public class LambdaService implements ResourceProvider { .max(java.util.Comparator.comparingLong(v -> Long.parseLong(v.getVersion()))); } - /** - * Copies the function's current code into a directory belonging to this version, falling back - * to {@code $LATEST}'s path if there is nothing to copy or the copy fails. - * - *

A copy failure must not fail the publish: the version is still a correct snapshot of the - * configuration, and falling back leaves it exactly as good as every version published before - * this existed, rather than turning a working call into an error. - */ + /** Publishes an owned code snapshot; a failed copy must not reference mutable {@code $LATEST}. */ private String versionCodePath(String region, LambdaFunction fn, String version) { String current = fn.getCodeLocalPath(); if (current == null || fn.getHotReloadHostPath() != null) { @@ -2308,12 +2301,14 @@ public class LambdaService implements ResourceProvider { try { Path copied = codeStore.copyForVersion( ownerAccount(fn), region, fn.getFunctionName(), version, Path.of(current)); - return copied == null ? current : copied.toAbsolutePath().normalize().toString(); + if (copied == null) { + throw new IOException("Code directory is missing: " + current); + } + return copied.toAbsolutePath().normalize().toString(); } catch (IOException e) { - LOG.warnv("Could not give version {0} of {1} its own code directory, " - + "falling back to the shared one: {2}", - version, fn.getFunctionName(), e.getMessage()); - return current; + throw new AwsException("ServiceException", + "Could not create a code snapshot for version " + version + " of " + + fn.getFunctionName() + ": " + e.getMessage(), 500); } } @@ -2323,10 +2318,9 @@ public class LambdaService implements ResourceProvider { if (current == null && legacyCounterKey != null) { // Counters persisted before the key carried the owning account. Starting over // from 0 here would re-issue a version number that already names a snapshot. - current = versionCounters.remove(legacyCounterKey); + current = versionCounters.get(legacyCounterKey); } int next = (current != null ? current : 0) + 1; - versionCounters.put(counterKey, next); return next; } } @@ -2398,8 +2392,10 @@ public class LambdaService implements ResourceProvider { + "returning it rather than creating a duplicate", functionName, unchanged.getVersion()); return unchanged; } - int version = nextVersionNumber(versionCounterKey(region, fn), - legacyVersionCounterKey(region, functionName)); + String counterKey = versionCounterKey(region, fn); + String legacyCounterKey = legacyVersionCounterKey(region, functionName); + boolean migrateLegacyCounter = !versionCounters.containsKey(counterKey); + int version = nextVersionNumber(counterKey, legacyCounterKey); LambdaFunction snapshot = new LambdaFunction(); snapshot.setAccountId(fn.getAccountId()); snapshot.setFunctionName(fn.getFunctionName()); @@ -2460,6 +2456,10 @@ public class LambdaService implements ResourceProvider { snapshot.setDurableKmsKeyArn(fn.getDurableKmsKeyArn()); functionStore.save(region, snapshot); + versionCounters.put(counterKey, version); + if (migrateLegacyCounter) { + versionCounters.remove(legacyCounterKey); + } LOG.infov("Published version {0} for function {1}", version, functionName); return snapshot; } @@ -3405,25 +3405,24 @@ public class LambdaService implements ResourceProvider { private void extractZipCodeBytes(LambdaFunction fn, byte[] zipBytes, String region) { Path codePath = codeStore.getCodePath(ownerAccount(fn), region, fn.getFunctionName()); try { - zipExtractor.extractTo(zipBytes, codePath, configuredZipMaxEntries()); + zipExtractor.extractTo(zipBytes, codePath, configuredZipMaxEntries(), + () -> storeDeploymentPackage(fn, zipBytes, region)); // Publish the new code identity under the same per-function lock publishVersion holds. // PublishVersion's CodeSha256 precondition is a check-then-act: it compares the hash and // then snapshots the code. Mutating these fields without the lock lets an overlapping // deploy move $LATEST between those two steps, so a version would carry code whose hash - // the caller never authorised even though the check passed. Extraction to disk stays - // outside the lock; only the fields that decide what a snapshot copies are inside it. + // the caller never authorised even though the check passed. UpdateFunctionCode and reactive + // sync hold this monitor across extraction; a new function is saved after extraction. String newSha256 = Base64.getEncoder().encodeToString(Digests.sha256(zipBytes)); synchronized (lockForConcurrencyOp(fn.getFunctionArn())) { fn.setCodeLocalPath(codePath.toAbsolutePath().normalize().toString()); + fn.setHotReloadHostPath(null); fn.setCodeSizeBytes(zipBytes.length); if (newSha256 != null) { fn.setCodeSha256(newSha256); } } - // Deploy succeeded: keep the exact package so GetFunction can serve - // a real Code.Location. - storeDeploymentPackage(fn, zipBytes, region); // Reclaim a directory left behind by a function created before account-scoping; // codePath above is already the new location, so this is a no-op once migrated. // Locked against publishVersion (see its own comment) so a version cannot be @@ -3507,8 +3506,6 @@ public class LambdaService implements ResourceProvider { throw new AwsException("ServiceUnavailableException", "S3 service not available", 503); } - fn.setS3Bucket(s3Bucket); - fn.setS3Key(s3Key); S3Object obj; try { obj = s3Service.getObject(s3Bucket, s3Key); @@ -3517,6 +3514,8 @@ public class LambdaService implements ResourceProvider { "Unable to fetch code from s3://" + s3Bucket + "/" + s3Key + ": " + e.getMessage(), 400); } extractZipCodeBytes(fn, obj.getData(), region); + fn.setS3Bucket(s3Bucket); + fn.setS3Key(s3Key); } private void applyHotReload(LambdaFunction fn, String hostPath) { @@ -4106,23 +4105,28 @@ public class LambdaService implements ResourceProvider { String region = regionResolver.getDefaultRegion(); List functions = functionStore.listAllAccounts(region); for (LambdaFunction fn : functions) { - if (fn.isHotReload()) { + if (!event.bucketName().equals(fn.getS3Bucket()) || !event.key().equals(fn.getS3Key())) { continue; } - if (event.bucketName().equals(fn.getS3Bucket()) && event.key().equals(fn.getS3Key())) { - LOG.infov("Reactive S3 Sync: updating function {0} from s3://{1}/{2}", - fn.getFunctionName(), event.bucketName(), event.key()); - try { - S3Object obj = s3Service.getObject(event.bucketName(), event.key()); - extractZipCodeBytes(fn, obj.getData(), region); - fn.setLastModified(Instant.now().toEpochMilli()); - fn.setRevisionId(UUID.randomUUID().toString()); - functionStore.saveForAccount(ownerAccount(fn), region, fn); + synchronized (lockForConcurrencyOp(fn.getFunctionArn())) { + if (fn.isHotReload()) { + continue; + } + if (event.bucketName().equals(fn.getS3Bucket()) && event.key().equals(fn.getS3Key())) { + LOG.infov("Reactive S3 Sync: updating function {0} from s3://{1}/{2}", + fn.getFunctionName(), event.bucketName(), event.key()); + try { + S3Object obj = s3Service.getObject(event.bucketName(), event.key()); + extractZipCodeBytes(fn, obj.getData(), region); + fn.setLastModified(Instant.now().toEpochMilli()); + fn.setRevisionId(UUID.randomUUID().toString()); + functionStore.saveForAccount(ownerAccount(fn), region, fn); - // Push to warm workers - warmPool.pushCodeUpdate(fn); - } catch (Exception e) { - LOG.warnv("Failed reactive sync for function {0}: {1}", fn.getFunctionName(), e.getMessage()); + // Push to warm workers + warmPool.pushCodeUpdate(fn); + } catch (Exception e) { + LOG.warnv("Failed reactive sync for function {0}: {1}", fn.getFunctionName(), e.getMessage()); + } } } } diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/zip/CodeStore.java b/src/main/java/io/github/hectorvent/floci/services/lambda/zip/CodeStore.java index 616cac3a49..9917998150 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/zip/CodeStore.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/zip/CodeStore.java @@ -6,6 +6,7 @@ import jakarta.inject.Inject; import org.jboss.logging.Logger; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @@ -112,6 +113,12 @@ public class CodeStore { Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING); } } + } catch (UncheckedIOException e) { + deleteDirectory(target, functionName); + throw e.getCause(); + } catch (IOException e) { + deleteDirectory(target, functionName); + throw e; } return target; } @@ -159,7 +166,7 @@ public class CodeStore { } }); LOG.debugv("Deleted code for function: {0}", functionName); - } catch (IOException e) { + } catch (IOException | UncheckedIOException e) { LOG.warnv("Failed to delete code directory for {0}: {1}", functionName, e.getMessage()); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/zip/ZipExtractor.java b/src/main/java/io/github/hectorvent/floci/services/lambda/zip/ZipExtractor.java index 90e5398289..b79c1dd9c6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/zip/ZipExtractor.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/zip/ZipExtractor.java @@ -40,6 +40,10 @@ public class ZipExtractor { } public void extractTo(byte[] zipBytes, Path targetDir, int maxEntries) throws IOException { + extractTo(zipBytes, targetDir, maxEntries, null); + } + + public void extractTo(byte[] zipBytes, Path targetDir, int maxEntries, Runnable beforeInstall) throws IOException { if (maxEntries < 1) { throw new IOException("ZIP archive entry limit must be at least 1: " + maxEntries); } @@ -142,6 +146,9 @@ public class ZipExtractor { } } // Extraction succeeded in full: install the new tree. + if (beforeInstall != null) { + beforeInstall.run(); + } install(staging, absTarget); } finally { Files.deleteIfExists(staged); diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaS3DeploymentIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaS3DeploymentIntegrationTest.java new file mode 100644 index 0000000000..96bb626fea --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaS3DeploymentIntegrationTest.java @@ -0,0 +1,66 @@ +package io.github.hectorvent.floci.services.lambda; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.UUID; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +@QuarkusTest +class LambdaS3DeploymentIntegrationTest { + + @Test + void reactiveS3SyncPreservesPublishedVersions() throws Exception { + String name = "s3-deployment-" + UUID.randomUUID().toString().substring(0, 8); + String key = "function.zip"; + String functionPath = "/2015-03-31/functions/" + name; + given().when().put("/" + name).then().statusCode(200); + try { + given().body(makeZip("v1")).when().put("/" + name + "/" + key).then().statusCode(200); + String codeHash = given().contentType("application/json").body(Map.of( + "FunctionName", name, "Runtime", "nodejs20.x", + "Role", "arn:aws:iam::000000000000:role/role", + "Handler", "index.handler", "Timeout", 30, + "Code", Map.of("S3Bucket", name, "S3Key", key))) + .when().post("/2015-03-31/functions").then().statusCode(201) + .extract().path("CodeSha256"); + try { + String version = given().contentType("application/json").body("{}") + .when().post(functionPath + "/versions").then().statusCode(201) + .body("CodeSha256", equalTo(codeHash)).extract().path("Version"); + given().body("{}").when().post(functionPath + "/invocations") + .then().statusCode(200).body("body", equalTo("v1")); + + given().body(makeZip("v2")).when().put("/" + name + "/" + key).then().statusCode(200); + given().body("{}").when().post(functionPath + "/invocations") + .then().statusCode(200).body("body", equalTo("v2")); + given().queryParam("Qualifier", version).body("{}") + .when().post(functionPath + "/invocations") + .then().statusCode(200).body("body", equalTo("v1")); + } finally { + given().when().delete(functionPath).then().statusCode(204); + } + } finally { + given().when().delete("/" + name + "/" + key).then().statusCode(204); + given().when().delete("/" + name).then().statusCode(204); + } + } + + private static byte[] makeZip(String body) throws Exception { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + try (ZipOutputStream zip = new ZipOutputStream(bytes)) { + zip.putNextEntry(new ZipEntry("index.js")); + zip.write(("exports.handler = async () => ({ body: '" + body + "' });") + .getBytes(StandardCharsets.UTF_8)); + zip.closeEntry(); + } + return bytes.toByteArray(); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java index e158a2fc9f..744fbbb824 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.lambda; +import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -12,6 +13,9 @@ import io.github.hectorvent.floci.services.lambda.model.LambdaFunction; import io.github.hectorvent.floci.services.lambda.model.LambdaUrlConfig; import io.github.hectorvent.floci.services.lambda.zip.CodeStore; import io.github.hectorvent.floci.services.lambda.zip.ZipExtractor; +import io.github.hectorvent.floci.services.s3.S3Service; +import io.github.hectorvent.floci.services.s3.model.S3Object; +import io.github.hectorvent.floci.services.s3.model.S3ObjectUpdatedEvent; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -29,13 +33,19 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.UUID; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; +import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import static io.github.hectorvent.floci.testing.ThreadStates.awaitBlockedOrDone; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -974,6 +984,11 @@ class LambdaServiceTest { private LambdaService serviceWithHotReload(boolean enabled, List allowedPaths, int zipMaxEntries) { + return serviceWithHotReload(enabled, allowedPaths, zipMaxEntries, "docker"); + } + + private LambdaService serviceWithHotReload(boolean enabled, List allowedPaths, + int zipMaxEntries, String executor) { EmulatorConfig cfg = mock(EmulatorConfig.class); EmulatorConfig.ServicesConfig svc = mock(EmulatorConfig.ServicesConfig.class); EmulatorConfig.LambdaServiceConfig lambdaCfg = mock(EmulatorConfig.LambdaServiceConfig.class); @@ -985,6 +1000,7 @@ class LambdaServiceTest { when(lambdaCfg.defaultTimeoutSeconds()).thenReturn(3); when(lambdaCfg.defaultMemoryMb()).thenReturn(128); when(lambdaCfg.zipMaxEntries()).thenReturn(zipMaxEntries); + when(lambdaCfg.executor()).thenReturn(executor); when(hr.enabled()).thenReturn(enabled); when(hr.allowedPaths()).thenReturn(allowedPaths == null ? Optional.empty() : Optional.of(allowedPaths)); @@ -1205,8 +1221,7 @@ class LambdaServiceTest { @Test void hotReload_convertFromS3Backed_clearsBucketAndKey() { - // A function previously deployed from S3 that is later converted to hot-reload - // must have s3Bucket/s3Key cleared so the reactive S3 sync observer cannot fire. + // Clear the previous S3 location so reactive sync cannot overwrite the bind-mounted code. LambdaService svc = serviceWithHotReload(true, null); Map req = baseRequest("hr-convert"); req.put("Code", Map.of("S3Bucket", "my-code-bucket", "S3Key", "fn.zip")); @@ -1225,6 +1240,79 @@ class LambdaServiceTest { assertEquals("/tmp/converted", updated.getHotReloadHostPath()); } + @Test + void requiredPackageStorageFailurePreservesHotReloadState() { + LambdaService svc = serviceWithHotReload(true, null, ZipExtractor.DEFAULT_MAX_ENTRIES, "kubernetes"); + String name = "hot-reload-storage-failure"; + Map request = baseRequest(name); + request.put("Code", Map.of("S3Bucket", "hot-reload", "S3Key", "/tmp/host-code")); + LambdaFunction original = svc.createFunction(REGION, request); + String revision = original.getRevisionId(); + long modified = original.getLastModified(); + + AwsException failure = assertThrows(AwsException.class, + () -> svc.updateFunctionCode(REGION, name, Map.of("ZipFile", zipWithBody("new")))); + + assertEquals("ServiceException", failure.getErrorCode()); + LambdaFunction unchanged = svc.getFunction(REGION, name); + assertTrue(unchanged.isHotReload()); + assertEquals("/tmp/host-code", unchanged.getHotReloadHostPath()); + assertNull(unchanged.getCodeLocalPath()); + assertEquals(revision, unchanged.getRevisionId()); + assertEquals(modified, unchanged.getLastModified()); + assertEquals("", unchanged.getCodeSha256()); + } + + @Test + void hotReload_zipDeploymentConvertsToManagedCodeAndPreservesPublishedVersions() throws Exception { + LambdaService svc = serviceWithHotReload(true, null); + String name = "hr-managed-deployment"; + Map request = baseRequest(name); + request.put("Architectures", List.of("x86_64")); + request.put("Code", Map.of("S3Bucket", "hot-reload", "S3Key", "/tmp/host-code")); + request.put("Environment", Map.of("Variables", Map.of("MARKER", "configured"))); + LambdaFunction original = svc.createFunction(REGION, request); + String originalRevision = original.getRevisionId(); + long originalModified = original.getLastModified(); + + AwsException failure = assertThrows(AwsException.class, () -> svc.updateFunctionCode(REGION, name, + Map.of("ZipFile", Base64.getEncoder().encodeToString("not a ZIP".getBytes(StandardCharsets.UTF_8)), + "Architectures", List.of("arm64")))); + + assertEquals("InvalidParameterValueException", failure.getErrorCode()); + LambdaFunction unchanged = svc.getFunction(REGION, name); + assertTrue(unchanged.isHotReload()); + assertEquals("/tmp/host-code", unchanged.getHotReloadHostPath()); + assertNull(unchanged.getCodeLocalPath()); + assertEquals("", unchanged.getCodeSha256()); + assertEquals(0L, unchanged.getCodeSizeBytes()); + assertEquals(originalRevision, unchanged.getRevisionId()); + assertEquals(originalModified, unchanged.getLastModified()); + assertEquals(List.of("x86_64"), unchanged.getArchitectures()); + assertEquals(Map.of("MARKER", "configured"), unchanged.getEnvironment()); + + LambdaFunction converted = svc.updateFunctionCode(REGION, name, Map.of("ZipFile", zipWithBody("v1"))); + + assertFalse(converted.isHotReload()); + assertNull(converted.getHotReloadHostPath()); + Path latestPath = Path.of(converted.getCodeLocalPath()); + assertEquals("v1", Files.readString(latestPath.resolve("index.js")).trim()); + LambdaFunction published = svc.publishVersion(REGION, name, null); + Path versionPath = Path.of(published.getCodeLocalPath()); + String versionHash = published.getCodeSha256(); + assertFalse(published.isHotReload()); + assertNotEquals(latestPath, versionPath); + + svc.updateFunctionCode(REGION, name, Map.of("ZipFile", zipWithBody("v2"))); + + assertEquals("v2", Files.readString(latestPath.resolve("index.js")).trim()); + assertEquals("v1", Files.readString(versionPath.resolve("index.js")).trim()); + assertEquals(versionHash, published.getCodeSha256()); + assertEquals(Map.of("MARKER", "configured"), published.getEnvironment()); + assertEquals(10, published.getTimeout()); + assertEquals(256, published.getMemorySize()); + } + /** * Test helper: a LambdaFunctionStore whose save() throws on demand so * tests can exercise the LambdaService rollback path. @@ -2111,6 +2199,9 @@ class LambdaServiceTest { assertTrue(Files.isDirectory(v1Path), "a published version must have its own code directory"); assertEquals("v1", Files.readString(v1Path.resolve("index.js")).trim()); + assertFalse(Files.isSameFile(v1Path.resolve("index.js"), + Path.of(service.getFunction(REGION, "version-code-isolation-fn").getCodeLocalPath()) + .resolve("index.js"))); // Redeploy $LATEST. Extraction replaces its directory wholesale, which is what used to take // the published version's code with it. @@ -2128,6 +2219,8 @@ class LambdaServiceTest { assertTrue(Files.isDirectory(v1Path), "the version's code must survive a redeploy of $LATEST"); assertEquals("v1", Files.readString(v1Path.resolve("index.js")).trim()); assertEquals(v1Sha, v1.getCodeSha256()); + assertFalse(Files.isSameFile(v1Path.resolve("index.js"), + Path.of(latest.getCodeLocalPath()).resolve("index.js"))); } @Test @@ -2173,6 +2266,190 @@ class LambdaServiceTest { assertTrue(Files.isDirectory(latestPath), "$LATEST's code must be left alone"); } + @Test + void unrelatedS3EventsDoNotAcquireOtherFunctionsDeploymentLocks() throws Exception { + LambdaFunction first = service.createFunction(REGION, baseRequest("unrelated-sync-first")); + LambdaFunction second = service.createFunction(REGION, baseRequest("unrelated-sync-second")); + first.setS3Bucket("source-bucket"); + first.setS3Key("first.zip"); + second.setS3Bucket("source-bucket"); + second.setS3Key("second.zip"); + ExecutorService worker = Executors.newSingleThreadExecutor(); + try { + synchronized (service.lockForConcurrencyOp(first.getFunctionArn())) { + Future deployment = worker.submit(() -> { + synchronized (service.lockForConcurrencyOp(second.getFunctionArn())) { + service.onS3ObjectUpdated(new S3ObjectUpdatedEvent("tasks-bucket", "deployment.zip")); + } + }); + deployment.get(2, TimeUnit.SECONDS); + } + } finally { + worker.shutdownNow(); + } + } + + @Test + void reactiveSyncHoldsThePublicationLockThroughExtraction() throws Exception { + S3Service s3 = mock(S3Service.class); + WarmPool warmPool = mock(WarmPool.class); + ZipExtractor extractor = spy(new ZipExtractor()); + StorageFactory storageFactory = mock(StorageFactory.class); + when(storageFactory.create(anyString(), anyString(), any())) + .thenAnswer(inv -> AccountAwareStorageBackend.inMemory("000000000000")); + LambdaFunctionStore functionStore = new LambdaFunctionStore(new InMemoryStorage()); + LambdaAliasStore aliasStore = new LambdaAliasStore(storageFactory); + LambdaService syncingService = new LambdaService(functionStore, null, new LambdaConcurrencyLimiter(), + warmPool, new CodeStore(Path.of("target/test-data/lambda-code")), extractor, null, + new RegionResolver(REGION, "000000000000"), null, aliasStore, + new LambdaTargetResolver(functionStore, aliasStore), s3, null, null, null, null, + storageFactory, null, null, null, null, new ObjectMapper()); + String name = "reactive-snapshot-" + UUID.randomUUID(); + when(s3.getObject("code-bucket", "function.zip")) + .thenReturn(new S3Object("code-bucket", "function.zip", + Base64.getDecoder().decode(zipWithBody("v1")), "application/zip")) + .thenReturn(new S3Object("code-bucket", "function.zip", + Base64.getDecoder().decode(zipWithBody("v2")), "application/zip")); + Map request = baseRequest(name); + request.put("Code", Map.of("S3Bucket", "code-bucket", "S3Key", "function.zip")); + LambdaFunction latest = syncingService.createFunction(REGION, request); + LambdaFunction first = syncingService.publishVersion(REGION, name, null); + CountDownLatch extracting = new CountDownLatch(1); + CountDownLatch release = new CountDownLatch(1); + doAnswer(inv -> { + assertTrue(Thread.holdsLock(syncingService.lockForConcurrencyOp(latest.getFunctionArn()))); + extracting.countDown(); + assertTrue(release.await(5, TimeUnit.SECONDS)); + return inv.callRealMethod(); + }).when(extractor).extractTo(any(byte[].class), any(Path.class), anyInt(), any(Runnable.class)); + ExecutorService workers = Executors.newFixedThreadPool(2); + try { + Future sync = workers.submit(() -> syncingService.onS3ObjectUpdated( + new S3ObjectUpdatedEvent("code-bucket", "function.zip"))); + assertTrue(extracting.await(2, TimeUnit.SECONDS)); + AtomicReference publicationThread = new AtomicReference<>(); + Future publication = workers.submit(() -> { + publicationThread.set(Thread.currentThread()); + return syncingService.publishVersion(REGION, name, null); + }); + awaitBlockedOrDone(publicationThread, publication); + assertFalse(publication.isDone()); + release.countDown(); + sync.get(5, TimeUnit.SECONDS); + LambdaFunction second = publication.get(5, TimeUnit.SECONDS); + assertEquals("v1", Files.readString(Path.of(first.getCodeLocalPath()).resolve("index.js")).trim()); + assertEquals("v2", Files.readString(Path.of(second.getCodeLocalPath()).resolve("index.js")).trim()); + assertEquals(latest.getCodeSha256(), second.getCodeSha256()); + verify(warmPool).pushCodeUpdate(latest); + } finally { + release.countDown(); + workers.shutdownNow(); + } + } + + @Test + void deletingOneVersionPreservesAnotherVersionAndLatest() throws Exception { + String name = "shared-version-code"; + Map request = baseRequest(name); + request.put("Code", Map.of("ZipFile", zipWithBody("shared"))); + LambdaFunction latest = service.createFunction(REGION, request); + LambdaFunction first = service.publishVersion(REGION, name, null); + service.updateFunctionConfiguration(REGION, name, Map.of("Timeout", 20)); + LambdaFunction second = service.publishVersion(REGION, name, null); + Path firstFile = Path.of(first.getCodeLocalPath()).resolve("index.js"); + Path secondFile = Path.of(second.getCodeLocalPath()).resolve("index.js"); + Path latestFile = Path.of(latest.getCodeLocalPath()).resolve("index.js"); + assertFalse(Files.isSameFile(firstFile, secondFile)); + assertFalse(Files.isSameFile(secondFile, latestFile)); + + service.deleteFunction(REGION, name, first.getVersion()); + + assertFalse(Files.exists(firstFile)); + assertEquals("shared", Files.readString(secondFile).trim()); + assertEquals("shared", Files.readString(latestFile).trim()); + assertEquals(20, second.getTimeout()); + } + + @Test + void failedVersionSnapshotKeepsLatestVersionsAndAliasesIntact() throws Exception { + String name = "failed-version-snapshot-" + UUID.randomUUID(); + Map request = baseRequest(name); + request.put("Code", Map.of("ZipFile", zipWithBody("v1"))); + service.createFunction(REGION, request); + LambdaFunction first = service.publishVersion(REGION, name, null); + service.createAlias(REGION, name, "live", first.getVersion(), null, null); + LambdaFunction latest = service.updateFunctionCode(REGION, name, + Map.of("ZipFile", zipWithBody("v2"))); + Path latestPath = Path.of(latest.getCodeLocalPath()); + Path firstPath = Path.of(first.getCodeLocalPath()); + Path unreadable = Files.createSymbolicLink(latestPath.resolve("unreadable"), + Path.of("missing-file")); + try { + AwsException failure = assertThrows(AwsException.class, + () -> service.publishVersion(REGION, name, null)); + assertEquals("ServiceException", failure.getErrorCode()); + assertEquals(Set.of("$LATEST", first.getVersion()), + service.listVersionsByFunction(REGION, name).stream() + .map(LambdaFunction::getVersion).collect(Collectors.toSet())); + assertEquals(first.getVersion(), service.getAlias(REGION, name, "live").getFunctionVersion()); + assertEquals("v1", Files.readString(firstPath.resolve("index.js")).trim()); + assertEquals("v2", Files.readString(latestPath.resolve("index.js")).trim()); + try (Stream versions = Files.list(firstPath.getParent())) { + assertEquals(List.of(firstPath), versions.toList(), + "a failed snapshot must not leave a partial version directory"); + } + } finally { + Files.delete(unreadable); + } + LambdaFunction retry = service.publishVersion(REGION, name, null); + assertEquals("2", retry.getVersion(), "failed publication must not consume a version number"); + assertNotEquals(latestPath, Path.of(retry.getCodeLocalPath())); + assertEquals(latest.getCodeSha256(), retry.getCodeSha256()); + assertEquals("v2", Files.readString(Path.of(retry.getCodeLocalPath()).resolve("index.js")).trim()); + } + + @Test + void rejectedZipUpdatePreservesLatestCodeVersionsAliasesAndConfiguration() throws Exception { + String name = "rejected-zip-deployment"; + Map request = baseRequest(name); + request.put("Architectures", List.of("x86_64")); + request.put("Code", Map.of("ZipFile", zipWithBody("v1"))); + request.put("Environment", Map.of("Variables", Map.of("MARKER", "original"))); + service.createFunction(REGION, request); + LambdaFunction version = service.publishVersion(REGION, name, null); + service.createAlias(REGION, name, "live", version.getVersion(), null, null); + LambdaFunction latest = service.updateFunctionCode(REGION, name, Map.of("ZipFile", zipWithBody("v2"))); + String latestPath = latest.getCodeLocalPath(); + String latestHash = latest.getCodeSha256(); + long latestSize = latest.getCodeSizeBytes(); + String latestRevision = latest.getRevisionId(); + long latestModified = latest.getLastModified(); + String versionHash = version.getCodeSha256(); + + AwsException failure = assertThrows(AwsException.class, () -> service.updateFunctionCode(REGION, name, + Map.of("ZipFile", Base64.getEncoder().encodeToString("not a ZIP".getBytes(StandardCharsets.UTF_8)), + "Architectures", List.of("arm64"), "Publish", true))); + + assertEquals("InvalidParameterValueException", failure.getErrorCode()); + LambdaFunction unchanged = service.getFunction(REGION, name); + assertEquals(latestPath, unchanged.getCodeLocalPath()); + assertEquals("v2", Files.readString(Path.of(latestPath).resolve("index.js")).trim()); + assertEquals(latestHash, unchanged.getCodeSha256()); + assertEquals(latestSize, unchanged.getCodeSizeBytes()); + assertEquals(latestRevision, unchanged.getRevisionId()); + assertEquals(latestModified, unchanged.getLastModified()); + assertNull(unchanged.getHotReloadHostPath()); + assertEquals(List.of("x86_64"), unchanged.getArchitectures()); + assertEquals(Map.of("MARKER", "original"), unchanged.getEnvironment()); + assertEquals(10, unchanged.getTimeout()); + assertEquals(256, unchanged.getMemorySize()); + assertEquals("v1", Files.readString(Path.of(version.getCodeLocalPath()).resolve("index.js")).trim()); + assertEquals(versionHash, version.getCodeSha256()); + assertEquals(version.getVersion(), service.getAlias(REGION, name, "live").getFunctionVersion()); + assertEquals(Set.of("$LATEST", version.getVersion()), service.listVersionsByFunction(REGION, name).stream() + .map(LambdaFunction::getVersion).collect(Collectors.toSet())); + } + private static String zipWithBody(String body) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ZipOutputStream zos = new ZipOutputStream(baos)) { diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/zip/CodeStoreTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/zip/CodeStoreTest.java index 9c7ee1d773..980932a270 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/zip/CodeStoreTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/zip/CodeStoreTest.java @@ -2,10 +2,15 @@ package io.github.hectorvent.floci.services.lambda.zip; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import java.io.IOException; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Map; import static org.junit.jupiter.api.Assertions.*; @@ -174,6 +179,42 @@ class CodeStoreTest { assertTrue(version.normalize().startsWith(baseDir.normalize())); } + @Test + void versionFilesRemainIndependentAfterInPlaceHostEdits(@TempDir Path baseDir) + throws IOException { + CodeStore store = new CodeStore(baseDir); + Path latest = store.getCodePath(ACCOUNT_A, REGION, "fn"); + writeHandler(latest.resolve("lib"), "handler"); + Files.createSymbolicLink(latest.resolve("index.js"), Path.of("lib/index.js")); + + Path version = store.copyForVersion(ACCOUNT_A, REGION, "fn", "1", latest); + + assertFalse(Files.isSameFile(latest.resolve("lib/index.js"), version.resolve("lib/index.js"))); + Files.writeString(latest.resolve("lib/index.js"), "changed in place"); + assertEquals("handler", Files.readString(version.resolve("lib/index.js"))); + assertFalse(Files.isSymbolicLink(version.resolve("index.js"))); + assertFalse(Files.isSameFile(latest.resolve("index.js"), version.resolve("index.js"))); + assertEquals("handler", Files.readString(version.resolve("index.js"))); + } + + @ParameterizedTest + @CsvSource({"true, false", "false, true", "true, true"}) + void versionCopiesWorkAcrossFilesystemProviders(boolean zipSource, boolean zipTarget, @TempDir Path baseDir) + throws IOException { + try (FileSystem zip = FileSystems.newFileSystem(baseDir.resolve("code.zip"), Map.of("create", "true"))) { + CodeStore store = new CodeStore(zipTarget ? zip.getPath("/code") : baseDir.resolve("code")); + Path source = zipSource ? zip.getPath("/source") : baseDir.resolve("source"); + writeHandler(source.resolve("lib"), "original"); + + Path version = store.copyForVersion(ACCOUNT_A, REGION, "fn", "1", source); + writeHandler(source.resolve("lib"), "changed"); + + assertEquals("original", Files.readString(version.resolve("lib/index.js"))); + store.deleteVersion(ACCOUNT_A, REGION, "fn", "1"); + assertEquals("changed", Files.readString(source.resolve("lib/index.js"))); + } + } + private void writeHandler(Path codePath, String content) throws IOException { Files.createDirectories(codePath); Files.writeString(codePath.resolve("index.js"), content);