mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(lambda): do not reclaim a legacy code directory another account's function still uses
The pre-account-scoped layout gave every account's same-named function the exact same on-disk directory. CodeStore.delete() and code re-extraction both reclaimed that legacy directory unconditionally once a function migrated to the account-scoped path, so a delete or code update in one account could destroy the legacy directory while another account's never-migrated $LATEST still served invocations from it -- its next cold start would then fail with a missing code directory. CodeStore.delete() no longer touches the legacy path at all; only LambdaService calls deleteLegacy() explicitly, after checking every account's $LATEST functions (via the existing cross-account listAllAccounts) for one still pointing its codeLocalPath at that directory.
This commit is contained in:
4 files changed
+93
-10
No files matched your search
@@ -712,6 +712,7 @@ public class LambdaService implements ResourceProvider {
|
||||
}
|
||||
codeStore.delete(ownerAccount(fn), functionName);
|
||||
functionStore.delete(region, functionName);
|
||||
reclaimLegacyCodeDirectoryIfUnused(region, functionName);
|
||||
versionCounters.remove(versionCounterKey(region, fn));
|
||||
versionCounters.remove(legacyVersionCounterKey(region, functionName));
|
||||
if (aliasStore != null) {
|
||||
@@ -732,6 +733,22 @@ public class LambdaService implements ResourceProvider {
|
||||
LOG.infov("Deleted Lambda function: {0}", functionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* The pre-account-scoped code layout gave every account's same-named function the exact
|
||||
* same on-disk directory, so it is only safe to reclaim once no account's {@code $LATEST}
|
||||
* still has {@code codeLocalPath} pointing at it — otherwise a delete or code update in one
|
||||
* account destroys a directory another account's function still serves invocations from.
|
||||
*/
|
||||
private void reclaimLegacyCodeDirectoryIfUnused(String region, String functionName) {
|
||||
String legacyPath = codeStore.getLegacyCodePath(functionName).toAbsolutePath().normalize().toString();
|
||||
boolean stillLive = functionStore.listAllAccounts(region).stream()
|
||||
.anyMatch(other -> functionName.equals(other.getFunctionName())
|
||||
&& legacyPath.equals(other.getCodeLocalPath()));
|
||||
if (!stillLive) {
|
||||
codeStore.deleteLegacy(functionName);
|
||||
}
|
||||
}
|
||||
|
||||
public InvokeResult invoke(String region, String functionName, byte[] payload, InvocationType type) {
|
||||
LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolve(functionName);
|
||||
enforceRegion(region, ref);
|
||||
@@ -1868,7 +1885,7 @@ public class LambdaService implements ResourceProvider {
|
||||
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.
|
||||
codeStore.deleteLegacy(fn.getFunctionName());
|
||||
reclaimLegacyCodeDirectoryIfUnused(region, fn.getFunctionName());
|
||||
} catch (AwsException e) {
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -48,13 +48,13 @@ public class CodeStore {
|
||||
|
||||
public void delete(String accountId, String functionName) {
|
||||
deleteDirectory(getCodePath(accountId, functionName), functionName);
|
||||
deleteLegacy(functionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Best-effort removal of a function's pre-account-scoped directory, independent of its
|
||||
* current account-scoped one. Called after a successful code update re-extracts to the new
|
||||
* path, so an upgraded deployment reclaims the old directory instead of leaving it orphaned.
|
||||
* Best-effort removal of a function's pre-account-scoped directory. Deliberately NOT called
|
||||
* automatically from {@link #delete}: the pre-account-scoped layout gave every account's
|
||||
* same-named function the exact same directory, so it is only safe to remove once the caller
|
||||
* (see {@code LambdaService}) has confirmed no other account's function still references it.
|
||||
*/
|
||||
public void deleteLegacy(String functionName) {
|
||||
deleteDirectory(getLegacyCodePath(functionName), functionName);
|
||||
|
||||
+53
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.github.hectorvent.floci.services.lambda.zip.CodeStore;
|
||||
@@ -108,6 +109,58 @@ class LambdaAccountScopedCodeTest {
|
||||
assertFalse(Files.exists(legacyPath), "the pre-account-scoped directory must be reclaimed on update");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateFunctionCodeDoesNotDeleteALegacyDirectoryStillLiveForAnotherAccount(@TempDir Path baseDir) throws Exception {
|
||||
// Before account-scoping, two accounts' same-named functions shared the exact same
|
||||
// on-disk directory. If account B's function was never updated since the migration, its
|
||||
// $LATEST still points at that shared legacy directory. Account A updating its own code
|
||||
// must not delete it out from under B.
|
||||
CodeStore codeStore = new CodeStore(baseDir);
|
||||
LambdaFunctionStore sharedStore = new LambdaFunctionStore(AccountAwareStorageBackend.inMemory(ACCOUNT_A));
|
||||
LambdaService svcA = new LambdaService(sharedStore, new WarmPool(), codeStore, new ZipExtractor(),
|
||||
new RegionResolver(REGION, ACCOUNT_A));
|
||||
svcA.createFunction(REGION, zipRequest("shared-fn", "A"));
|
||||
|
||||
Path legacyPath = codeStore.getLegacyCodePath("shared-fn");
|
||||
Files.createDirectories(legacyPath);
|
||||
Files.writeString(legacyPath.resolve("index.js"), "B-legacy");
|
||||
LambdaFunction bFn = new LambdaFunction();
|
||||
bFn.setFunctionName("shared-fn");
|
||||
bFn.setAccountId(ACCOUNT_B);
|
||||
bFn.setVersion("$LATEST");
|
||||
bFn.setCodeLocalPath(legacyPath.toAbsolutePath().normalize().toString());
|
||||
sharedStore.saveForAccount(ACCOUNT_B, REGION, bFn);
|
||||
|
||||
svcA.updateFunctionCode(REGION, "shared-fn", Map.of("ZipFile", zipBase64("index.js", "A-v2")));
|
||||
|
||||
assertTrue(Files.exists(legacyPath),
|
||||
"a legacy directory another account's $LATEST still points at must survive this update");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteFunctionDoesNotDeleteALegacyDirectoryStillLiveForAnotherAccount(@TempDir Path baseDir) throws Exception {
|
||||
CodeStore codeStore = new CodeStore(baseDir);
|
||||
LambdaFunctionStore sharedStore = new LambdaFunctionStore(AccountAwareStorageBackend.inMemory(ACCOUNT_A));
|
||||
LambdaService svcA = new LambdaService(sharedStore, new WarmPool(), codeStore, new ZipExtractor(),
|
||||
new RegionResolver(REGION, ACCOUNT_A));
|
||||
svcA.createFunction(REGION, zipRequest("shared-fn", "A"));
|
||||
|
||||
Path legacyPath = codeStore.getLegacyCodePath("shared-fn");
|
||||
Files.createDirectories(legacyPath);
|
||||
Files.writeString(legacyPath.resolve("index.js"), "B-legacy");
|
||||
LambdaFunction bFn = new LambdaFunction();
|
||||
bFn.setFunctionName("shared-fn");
|
||||
bFn.setAccountId(ACCOUNT_B);
|
||||
bFn.setVersion("$LATEST");
|
||||
bFn.setCodeLocalPath(legacyPath.toAbsolutePath().normalize().toString());
|
||||
sharedStore.saveForAccount(ACCOUNT_B, REGION, bFn);
|
||||
|
||||
svcA.deleteFunction(REGION, "shared-fn");
|
||||
|
||||
assertTrue(Files.exists(legacyPath),
|
||||
"a legacy directory another account's $LATEST still points at must survive this delete");
|
||||
}
|
||||
|
||||
private LambdaFunction functionOwnedBy(String accountId) {
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
fn.setAccountId(accountId);
|
||||
|
||||
@@ -75,10 +75,12 @@ class CodeStoreTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteAlsoRemovesAPreAccountScopedLegacyDirectory(@TempDir Path baseDir) throws IOException {
|
||||
// Before account-scoping, code extracted to baseDir/<functionName> directly. delete()
|
||||
// only targeted the new baseDir/<accountId>/<functionName> path, so a function created
|
||||
// before the migration would leave its old directory (and disk usage) behind forever.
|
||||
void deleteDoesNotTouchAPreAccountScopedLegacyDirectory(@TempDir Path baseDir) throws IOException {
|
||||
// The pre-account-scoped layout gave every account's same-named function the exact same
|
||||
// directory, so CodeStore itself cannot safely know whether another account's function
|
||||
// still depends on it. That decision belongs to the caller (LambdaService, which can
|
||||
// check every account's persisted functions) via the separate deleteLegacy() below -
|
||||
// delete() must only ever touch its own account-scoped path.
|
||||
CodeStore store = new CodeStore(baseDir);
|
||||
Path legacyPath = baseDir.resolve("legacy-fn");
|
||||
writeHandler(legacyPath, "legacy");
|
||||
@@ -86,10 +88,21 @@ class CodeStoreTest {
|
||||
|
||||
store.delete(ACCOUNT_A, "legacy-fn");
|
||||
|
||||
assertFalse(Files.exists(legacyPath), "the pre-account-scoped directory must be cleaned up too");
|
||||
assertTrue(Files.exists(legacyPath), "delete() must not unilaterally remove the legacy directory");
|
||||
assertFalse(store.exists(ACCOUNT_A, "legacy-fn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteLegacyRemovesThePreAccountScopedDirectory(@TempDir Path baseDir) throws IOException {
|
||||
CodeStore store = new CodeStore(baseDir);
|
||||
Path legacyPath = store.getLegacyCodePath("legacy-fn");
|
||||
writeHandler(legacyPath, "legacy");
|
||||
|
||||
store.deleteLegacy("legacy-fn");
|
||||
|
||||
assertFalse(Files.exists(legacyPath));
|
||||
}
|
||||
|
||||
private void writeHandler(Path codePath, String content) throws IOException {
|
||||
Files.createDirectories(codePath);
|
||||
Files.writeString(codePath.resolve("index.js"), content);
|
||||
|
||||
Reference in new issue
Block a user