feat(lambdaweb): delete the oldest unused revisions past 50 as AWS does (#6518)

* feat(lambdaweb): delete the oldest unused revisions past 50 as AWS does

When a revision turns Active and the function has more than 50, AWS
deletes the oldest revisions no endpoint routes to until 50 are left.
Failed revisions count and go too. A new Failed revision deletes nothing.

* docs(lambdaweb): place the revision cleanup note after the code copy note
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-25 10:12:15 -04:00
1 parent fe928d67ab
commit 29eae434f6
4 files changed
+114 -6

No files matched your search

@@ -751,4 +751,40 @@ class LambdaWebIntegrationTest {
given().when().delete(BASE + "/web-functions/" + fn).then().statusCode(204);
}
@Test
@Order(13)
void anActiveRevisionPastFiftyDeletesTheOldestUnusedOne() {
String fn = FN + "-many";
String first = given().contentType("application/json")
.body("""
{"functionName": "%s",
"revisionConfig": {"buildConfig": %s, "serviceConfig": {"executionRoleArn": "%s"}},
"endpointConfig": {"endpointName": "dev", "endpointType": "HomeRegion",
"authType": "ApplicationManaged", "autoDeploymentMode": "LatestRevision"}}
""".formatted(fn, BUILD, ROLE))
.when().put(BASE + "/web-functions")
.then().statusCode(202)
.extract().path("revision.revisionId");
String revision = """
{"buildConfig": %s, "serviceConfig": {"executionRoleArn": "%s"}}
""".formatted(BUILD, ROLE);
for (int i = 0; i < 50; i++) {
given().contentType("application/json").body(revision)
.when().post(BASE + "/web-functions/" + fn + "/revisions")
.then().statusCode(202);
}
given().contentType("application/json").body("{}")
.when().post(BASE + "/web-functions/" + fn + "/list-revisions")
.then().statusCode(200)
.body("revisions", hasSize(50))
.body("nextToken", nullValue());
given().when().get(BASE + "/web-functions/" + fn + "/revisions/" + first)
.then().statusCode(404)
.header("X-Amzn-Errortype", "ResourceNotFoundException")
.body("message", equalTo("The revision " + first + " does not exist."));
given().when().delete(BASE + "/web-functions/" + fn).then().statusCode(204);
}
}
@@ -29,9 +29,11 @@ import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
@@ -970,6 +972,47 @@ class LambdaWebServiceTest {
message(() -> service.createFunction(REGION, createRequest("app"))));
}
@Test
void anActiveRevisionPastFiftyDeletesTheOldestUnusedOnes() {
S3Service s3 = mock(S3Service.class);
LambdaWebService service = service(new MemoryStorageFactory(), "http://localhost:4566", s3);
byte[] zip = LambdaWebZips.indexJs("");
when(s3.openObject("code", "app.zip", null)).thenAnswer(invocation -> object(zip));
String pinned = service.createFunction(REGION, createRequest("app")).revisions.keySet().iterator().next();
ObjectNode pin = endpointConfig("pin").put("autoDeploymentMode", "Disabled");
pin.putArray("revisionWeights").addObject().put("revisionId", pinned).put("weight", 100);
service.createEndpoint(REGION, "app", pin);
when(s3.openObject("code", "app.zip", null)).thenAnswer(invocation -> object(NOT_A_ZIP));
String failed = service.createRevision(REGION, "app", revisionConfig(30)).revisionId;
when(s3.openObject("code", "app.zip", null)).thenAnswer(invocation -> object(zip));
List<String> active = new ArrayList<>();
for (int i = 0; i < 48; i++) {
active.add(service.createRevision(REGION, "app", revisionConfig(30)).revisionId);
}
assertEquals(50, service.getFunction(REGION, "app").revisions.size());
service.createRevision(REGION, "app", revisionConfig(30));
Set<String> kept = service.getFunction(REGION, "app").revisions.keySet();
assertEquals(50, kept.size());
assertFalse(kept.contains(failed));
assertTrue(kept.contains(pinned));
when(s3.openObject("code", "app.zip", null)).thenAnswer(invocation -> object(NOT_A_ZIP));
service.createRevision(REGION, "app", revisionConfig(30));
assertEquals(51, service.getFunction(REGION, "app").revisions.size(), "a Failed revision cleans up nothing");
when(s3.openObject("code", "app.zip", null)).thenAnswer(invocation -> object(zip));
service.createRevision(REGION, "app", revisionConfig(30));
kept = service.getFunction(REGION, "app").revisions.keySet();
assertEquals(50, kept.size());
assertFalse(kept.contains(active.get(0)));
assertFalse(kept.contains(active.get(1)));
assertTrue(kept.contains(active.get(2)));
LambdaWebCode code = new LambdaWebCode(storageDir.resolve("code"));
assertFalse(Files.exists(code.file(ACCOUNT_ID, REGION, "app", active.get(0))));
assertTrue(Files.exists(code.file(ACCOUNT_ID, REGION, "app", active.get(2))));
}
private ObjectNode stateFilter(String state) {
ObjectNode request = mapper.createObjectNode();
request.putArray("filters").addObject().put("name", "state").putArray("values").add(state);