mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 15:49:33 -05:00
fix(lambda): preserve immutable deployment snapshots (#5818)
Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
e1fe876149
commit
ae7e6d1240
7 files changed
+452
-46
No files matched your search
@@ -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.
|
||||
*
|
||||
* <p>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<LambdaFunction> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user