diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java index b48312a231..49498948d6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java @@ -133,8 +133,9 @@ public class ApiGatewayService implements ResourceProvider { private final Object domainNameLock = new Object(); private final Object vpcLinkTagLock = new Object(); /** - * Guards a usage plan patch. A patch writes back the stages, throttle and quota it read, so two at - * once would restore one another's stale values. + * Guards a usage plan patch and delete. A patch writes back the stages, throttle and quota it + * read, so two at once would restore one another's stale values, and a delete between its read + * and its write would be undone. */ private final Object usagePlanLock = new Object(); private final TlsCertificateManager certificateManager; @@ -1604,8 +1605,10 @@ public class ApiGatewayService implements ResourceProvider { } public void deleteUsagePlan(String region, String usagePlanId) { - getUsagePlan(region, usagePlanId); - usagePlanStore.delete(usagePlanKey(region, usagePlanId)); + synchronized (usagePlanLock) { + getUsagePlan(region, usagePlanId); + usagePlanStore.delete(usagePlanKey(region, usagePlanId)); + } } /** diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanServiceTest.java index 1532021262..65ca1589c8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanServiceTest.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.apigateway; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.config.TlsCertificateManager; +import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; @@ -125,4 +126,43 @@ class ApiGatewayUsagePlanServiceTest { assertEquals(0L, mixed.get(), "limits mixing two revisions, for example " + samples); } } + + // A patch writes back the plan it read, so without the lock a delete between the two is undone. + @Test + void aDeleteDuringAPatchStaysDeleted() throws Exception { + ApiGatewayService service = service(); + List> patch = new ArrayList<>(); + for (int i = 0; i < 200; i++) { + patch.add(Map.of("op", "add", "path", "/apiStages", "value", "api:s" + i)); + } + int resurrected = 0; + try (ExecutorService executor = Executors.newFixedThreadPool(2)) { + for (int round = 0; round < 200; round++) { + String planId = service.createUsagePlan(REGION, Map.of("name", "delete-race")).getId(); + CountDownLatch start = new CountDownLatch(1); + Future patcher = executor.submit(() -> { + assertTrue(start.await(5, TimeUnit.SECONDS)); + try { + service.updateUsagePlan(REGION, planId, patch); + } catch (AwsException expected) { + // The delete went first, so the patch finds no plan. + assertEquals("NotFoundException", expected.getErrorCode()); + } + return null; + }); + Future deleter = executor.submit(() -> { + assertTrue(start.await(5, TimeUnit.SECONDS)); + service.deleteUsagePlan(REGION, planId); + return null; + }); + start.countDown(); + patcher.get(10, TimeUnit.SECONDS); + deleter.get(10, TimeUnit.SECONDS); + if (service.getUsagePlans(REGION).stream().anyMatch(plan -> plan.getId().equals(planId))) { + resurrected++; + } + } + } + assertEquals(0, resurrected, "plans a patch wrote back after they were deleted"); + } }