mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(lambda): give each published version its own copy of the code (#3041)
A published version's metadata was immutable but the directory it named was not. CodeStore.getCodePath has no version component, so every version shared one directory with $LATEST and publishVersion copied only the codeLocalPath pointer. UpdateFunctionCode replaces that directory wholesale, so a redeploy silently rewrote what an already-published version ran: the version kept advertising the CodeSha256 it was published with while the bytes behind it were a different build, and ContainerLauncher tar-copies that directory at launch, so a qualified invoke executed $LATEST's code. publishVersion now copies the code into a directory belonging to that version. Image-backed and hot-reload functions keep the reference they had: an image is already immutable by digest, and a hot-reload function's bind-mounted directory is meant to track the working tree. A copy failure falls back to the shared path rather than failing the publish. Version directories live under a "<function>@versions" container rather than as "<function>.v<n>" siblings. sanitizeName maps every name into [a-zA-Z0-9_.-], so no function can produce a directory name containing "@" and the two namespaces cannot overlap. A prefix scan over flat siblings could not be made correct: FunctionName has no charset validation in floci today, so a function genuinely named "foo.v1" owns the directory "foo"'s version 1 would claim, and deleting either corrupted the other. DeleteFunction with a qualifier now reclaims that version's directory, inside the per-function lock it already held, and only when the version actually owned one. Previously nothing but whole-function delete reclaimed version code, so a repeated publish/delete cycle grew on disk without bound. Closes #2958
This commit is contained in:
1 parent
f8a1c3134a
commit
4ee5a528f3
5 files changed
+295
-13
No files matched your search
@@ -850,7 +850,8 @@ public class LambdaService implements ResourceProvider {
|
||||
synchronized (lockForConcurrencyOp(fn.getFunctionArn())) {
|
||||
warmPool.drainEnvironment(version.get());
|
||||
functionStore.deleteVersion(region, name, qualifier);
|
||||
// The snapshot shares $LATEST's code directory, so this only reclaims once no
|
||||
reclaimVersionCodeDirectory(fn, qualifier, version.get());
|
||||
// The snapshot may still share $LATEST's code directory, so this only reclaims once no
|
||||
// remaining version references it.
|
||||
reclaimLegacyCodeDirectoryIfUnused(name);
|
||||
}
|
||||
@@ -896,6 +897,28 @@ public class LambdaService implements ResourceProvider {
|
||||
LOG.infov("Deleted Lambda function: {0}", functionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the code directory a deleted version owned. Without this the only thing that ever
|
||||
* reclaimed version code was deleting the whole function, so a repeated publish/delete cycle
|
||||
* left one package on disk per version ever published.
|
||||
*
|
||||
* <p>Guarded on the version actually owning that directory rather than deleting it outright.
|
||||
* A version whose copy could not be made fell back to {@code $LATEST}'s path, and image-backed
|
||||
* and hot-reload versions never had a copy at all: for those the recorded path is the live
|
||||
* function's own directory, and removing it would delete the code {@code $LATEST} still runs.
|
||||
*/
|
||||
private void reclaimVersionCodeDirectory(LambdaFunction fn, String version, LambdaFunction snapshot) {
|
||||
String recorded = snapshot.getCodeLocalPath();
|
||||
if (recorded == null) {
|
||||
return;
|
||||
}
|
||||
String owned = codeStore.getVersionCodePath(ownerAccount(fn), fn.getFunctionName(), version)
|
||||
.toAbsolutePath().normalize().toString();
|
||||
if (owned.equals(Path.of(recorded).toAbsolutePath().normalize().toString())) {
|
||||
codeStore.deleteVersion(ownerAccount(fn), fn.getFunctionName(), version);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The pre-account-scoped code layout gave every account's same-named function the exact
|
||||
* same on-disk directory (CodeStore never scoped it by region either), so it is only safe
|
||||
@@ -1792,6 +1815,31 @@ 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.
|
||||
*/
|
||||
private String versionCodePath(LambdaFunction fn, String version) {
|
||||
String current = fn.getCodeLocalPath();
|
||||
if (current == null || fn.getHotReloadHostPath() != null) {
|
||||
return current;
|
||||
}
|
||||
try {
|
||||
Path copied = codeStore.copyForVersion(
|
||||
ownerAccount(fn), fn.getFunctionName(), version, Path.of(current));
|
||||
return copied == null ? current : 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;
|
||||
}
|
||||
}
|
||||
|
||||
private int nextVersionNumber(String counterKey, String legacyCounterKey) {
|
||||
synchronized (versionCounterLocks.computeIfAbsent(counterKey, k -> new Object())) {
|
||||
Integer current = versionCounters.get(counterKey);
|
||||
@@ -1908,7 +1956,13 @@ public class LambdaService implements ResourceProvider {
|
||||
// with nothing in it, and hangs to the function timeout instead of failing (#1987). A
|
||||
// published version is an immutable snapshot of code plus configuration, so it carries the
|
||||
// code location for every package type, not only Zip.
|
||||
snapshot.setCodeLocalPath(fn.getCodeLocalPath());
|
||||
// A version's own copy of the code, not a reference to $LATEST's directory. Sharing that
|
||||
// directory meant a later UpdateFunctionCode rewrote what an already-published version
|
||||
// ran, so the version advertised one CodeSha256 over a different build (issue #2958).
|
||||
// Nothing to copy for image-backed or hot-reload functions, which keep the reference
|
||||
// they had: an image is already immutable by digest, and a hot-reload function's whole
|
||||
// point is that its bind-mounted directory tracks the developer's working tree.
|
||||
snapshot.setCodeLocalPath(versionCodePath(fn, String.valueOf(version)));
|
||||
snapshot.setCodeSha256(fn.getCodeSha256());
|
||||
snapshot.setS3Bucket(fn.getS3Bucket());
|
||||
snapshot.setS3Key(fn.getS3Key());
|
||||
|
||||
@@ -6,6 +6,7 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
@@ -22,6 +23,12 @@ public class CodeStore {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CodeStore.class);
|
||||
|
||||
/**
|
||||
* Separates a function's versions directory from any function's own code directory. Deliberately
|
||||
* outside {@link #sanitizeName}'s output character set, so the two namespaces cannot overlap.
|
||||
*/
|
||||
private static final String VERSIONS_DIR_SUFFIX = "@versions";
|
||||
|
||||
private final Path baseDir;
|
||||
|
||||
@Inject
|
||||
@@ -46,8 +53,75 @@ public class CodeStore {
|
||||
return baseDir.resolve(sanitizeName(functionName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a function's published-version code lives: one directory, holding a subdirectory per
|
||||
* published version.
|
||||
*
|
||||
* <p>A sibling of the {@code $LATEST} directory rather than a child of it. Extraction replaces
|
||||
* {@link #getCodePath}'s directory wholesale, so anything nested inside would be deleted on the
|
||||
* next deploy, which is the very thing a version's copy exists to survive.
|
||||
*
|
||||
* <p>The {@link #VERSIONS_DIR_SUFFIX} is what keeps that sibling from colliding with a real
|
||||
* function. {@link #sanitizeName} maps every name into {@code [a-zA-Z0-9_.-]}, so no function
|
||||
* can produce a directory name containing {@code @}, whatever it is called. Floci does not
|
||||
* restrict the character set of {@code FunctionName} today, only that it is non-blank, so a
|
||||
* plainer {@code <name>.v<n>} sibling carried no such guarantee: a function genuinely named
|
||||
* {@code foo.v1} owns the exact directory {@code foo}'s version 1 would otherwise claim, and
|
||||
* deleting either one silently corrupts the other.
|
||||
*/
|
||||
public Path getVersionsPath(String accountId, String functionName) {
|
||||
return baseDir.resolve(sanitizeName(accountId))
|
||||
.resolve(sanitizeName(functionName) + VERSIONS_DIR_SUFFIX);
|
||||
}
|
||||
|
||||
/** Where one published version's own copy of the code lives. */
|
||||
public Path getVersionCodePath(String accountId, String functionName, String version) {
|
||||
return getVersionsPath(accountId, functionName).resolve(sanitizeName(version));
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies a function's current code into its own directory for {@code version}, replacing any
|
||||
* directory already there. Returns null when there is nothing to copy, which is the case for
|
||||
* image-backed and hot-reload functions.
|
||||
*/
|
||||
public Path copyForVersion(String accountId, String functionName, String version, Path source)
|
||||
throws IOException {
|
||||
if (source == null || !Files.isDirectory(source)) {
|
||||
return null;
|
||||
}
|
||||
Path target = getVersionCodePath(accountId, functionName, version);
|
||||
deleteDirectory(target, functionName);
|
||||
try (var walk = Files.walk(source)) {
|
||||
for (Path from : walk.toList()) {
|
||||
Path to = target.resolve(source.relativize(from).toString());
|
||||
if (Files.isDirectory(from)) {
|
||||
Files.createDirectories(to);
|
||||
} else {
|
||||
Files.createDirectories(to.getParent());
|
||||
Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
/** Removes every published version's code for a function. */
|
||||
public void deleteVersions(String accountId, String functionName) {
|
||||
deleteDirectory(getVersionsPath(accountId, functionName), functionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes one published version's code, leaving the function's other versions and
|
||||
* {@code $LATEST} in place. Deleting a single version otherwise left its package on disk for
|
||||
* as long as the data directory lived, so a repeated publish/delete cycle grew without bound.
|
||||
*/
|
||||
public void deleteVersion(String accountId, String functionName, String version) {
|
||||
deleteDirectory(getVersionCodePath(accountId, functionName, version), functionName);
|
||||
}
|
||||
|
||||
public void delete(String accountId, String functionName) {
|
||||
deleteDirectory(getCodePath(accountId, functionName), functionName);
|
||||
deleteVersions(accountId, functionName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user