fix(lambda): scope extracted function code by region (#3265)

* fix(lambda): scope extracted function code by region

* fix(lambda): isolate new paths from legacy code

* chore(ci): retry transient pull request checks
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-08 23:43:48 -05:00
1 parent e02accff4a
commit 290559cff0
4 files changed
+149 -61

No files matched your search

@@ -906,7 +906,7 @@ public class LambdaService implements ResourceProvider {
synchronized (lockForConcurrencyOp(fn.getFunctionArn())) {
warmPool.drainEnvironment(version.get());
functionStore.deleteVersion(region, name, qualifier);
reclaimVersionCodeDirectory(fn, qualifier, version.get());
reclaimVersionCodeDirectory(region, 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);
@@ -930,7 +930,7 @@ public class LambdaService implements ResourceProvider {
if (concurrencyLimiter != null) {
concurrencyLimiter.reset(arn);
}
codeStore.delete(ownerAccount(fn), functionName);
codeStore.delete(ownerAccount(fn), region, functionName);
functionStore.delete(region, functionName);
reclaimLegacyCodeDirectoryIfUnused(functionName);
versionCounters.remove(versionCounterKey(region, fn));
@@ -963,15 +963,15 @@ public class LambdaService implements ResourceProvider {
* 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) {
private void reclaimVersionCodeDirectory(String region, LambdaFunction fn, String version, LambdaFunction snapshot) {
String recorded = snapshot.getCodeLocalPath();
if (recorded == null) {
return;
}
String owned = codeStore.getVersionCodePath(ownerAccount(fn), fn.getFunctionName(), version)
String owned = codeStore.getVersionCodePath(ownerAccount(fn), region, fn.getFunctionName(), version)
.toAbsolutePath().normalize().toString();
if (owned.equals(Path.of(recorded).toAbsolutePath().normalize().toString())) {
codeStore.deleteVersion(ownerAccount(fn), fn.getFunctionName(), version);
codeStore.deleteVersion(ownerAccount(fn), region, fn.getFunctionName(), version);
}
}
@@ -1911,14 +1911,14 @@ public class LambdaService implements ResourceProvider {
* 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) {
private String versionCodePath(String region, 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));
ownerAccount(fn), region, 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, "
@@ -2051,7 +2051,7 @@ public class LambdaService implements ResourceProvider {
// 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.setCodeLocalPath(versionCodePath(region, fn, String.valueOf(version)));
snapshot.setCodeSha256(fn.getCodeSha256());
snapshot.setS3Bucket(fn.getS3Bucket());
snapshot.setS3Key(fn.getS3Key());
@@ -2751,7 +2751,7 @@ public class LambdaService implements ResourceProvider {
}
private void extractZipCodeBytes(LambdaFunction fn, byte[] zipBytes, String region) {
Path codePath = codeStore.getCodePath(ownerAccount(fn), fn.getFunctionName());
Path codePath = codeStore.getCodePath(ownerAccount(fn), region, fn.getFunctionName());
try {
zipExtractor.extractTo(zipBytes, codePath, configuredZipMaxEntries());
// Publish the new code identity under the same per-function lock publishVersion holds.
@@ -13,10 +13,11 @@ import java.util.Comparator;
/**
* Manages on-disk locations of extracted Lambda function code.
* Each function gets its own directory under {@code <codePath>/<accountId>/}, mirroring
* the account-prefixed S3 key {@code LambdaService.codeObjectKey} uses for the same
* deployment package. Without the account segment two accounts' same-named functions in
* one region share a single extraction directory and overwrite each other's code.
* Each function gets its own directory under
* {@code <codePath>/<accountId>/@regions/<region>/}, mirroring the account- and
* region-scoped Lambda namespace. The reserved namespace marker is intentional: it keeps
* this layout disjoint from the preceding account/name layout, including when a legacy
* function is named like a region.
*/
@ApplicationScoped
public class CodeStore {
@@ -29,6 +30,9 @@ public class CodeStore {
*/
private static final String VERSIONS_DIR_SUFFIX = "@versions";
/** Namespace marker keeps the new layout disjoint from the preceding account/name layout. */
private static final String REGIONS_DIR = "@regions";
private final Path baseDir;
@Inject
@@ -40,8 +44,11 @@ public class CodeStore {
this.baseDir = baseDir;
}
public Path getCodePath(String accountId, String functionName) {
return baseDir.resolve(sanitizeName(accountId)).resolve(sanitizeName(functionName));
public Path getCodePath(String accountId, String region, String functionName) {
return baseDir.resolve(sanitizeName(accountId))
.resolve(REGIONS_DIR)
.resolve(sanitizeName(region))
.resolve(sanitizeName(functionName));
}
/**
@@ -69,14 +76,16 @@ public class CodeStore {
* {@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) {
public Path getVersionsPath(String accountId, String region, String functionName) {
return baseDir.resolve(sanitizeName(accountId))
.resolve(REGIONS_DIR)
.resolve(sanitizeName(region))
.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));
public Path getVersionCodePath(String accountId, String region, String functionName, String version) {
return getVersionsPath(accountId, region, functionName).resolve(sanitizeName(version));
}
/**
@@ -84,12 +93,12 @@ public class CodeStore {
* 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)
public Path copyForVersion(String accountId, String region, String functionName, String version, Path source)
throws IOException {
if (source == null || !Files.isDirectory(source)) {
return null;
}
Path target = getVersionCodePath(accountId, functionName, version);
Path target = getVersionCodePath(accountId, region, functionName, version);
deleteDirectory(target, functionName);
try (var walk = Files.walk(source)) {
for (Path from : walk.toList()) {
@@ -106,8 +115,8 @@ public class CodeStore {
}
/** Removes every published version's code for a function. */
public void deleteVersions(String accountId, String functionName) {
deleteDirectory(getVersionsPath(accountId, functionName), functionName);
public void deleteVersions(String accountId, String region, String functionName) {
deleteDirectory(getVersionsPath(accountId, region, functionName), functionName);
}
/**
@@ -115,13 +124,13 @@ public class CodeStore {
* {@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 deleteVersion(String accountId, String region, String functionName, String version) {
deleteDirectory(getVersionCodePath(accountId, region, functionName, version), functionName);
}
public void delete(String accountId, String functionName) {
deleteDirectory(getCodePath(accountId, functionName), functionName);
deleteVersions(accountId, functionName);
public void delete(String accountId, String region, String functionName) {
deleteDirectory(getCodePath(accountId, region, functionName), functionName);
deleteVersions(accountId, region, functionName);
}
/**
@@ -153,8 +162,8 @@ public class CodeStore {
}
}
public boolean exists(String accountId, String functionName) {
Path codePath = getCodePath(accountId, functionName);
public boolean exists(String accountId, String region, String functionName) {
Path codePath = getCodePath(accountId, region, functionName);
if (!Files.exists(codePath)) {
return false;
}