diff --git a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java index d2ce061469..2aa25e049a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java +++ b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java @@ -70,12 +70,12 @@ public class CodePipelineService { private final S3Service s3Service; private final ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); private final ScheduledExecutorService sourcePoller = Executors.newSingleThreadScheduledExecutor(); - private final Map pipelineLocks = new ConcurrentHashMap<>(); - private final Map sourcePollLocks = new ConcurrentHashMap<>(); + private final KeyedLockPool pipelineLocks = new KeyedLockPool(); + private final KeyedLockPool sourcePollLocks = new KeyedLockPool(); // Admission is serialized per pipeline on its own lock. A QUEUED worker holds the pipelineLocks // monitor for its whole run, so counting under that monitor would block StartPipelineExecution // until the running execution finished. - private final Map startLocks = new ConcurrentHashMap<>(); + private final KeyedLockPool startLocks = new KeyedLockPool(); private final Map runtimeArtifacts = new ConcurrentHashMap<>(); @Inject @@ -218,14 +218,14 @@ public class CodePipelineService { private void pollS3Sources(CodePipelinePipeline pipeline) { String pollLockKey = pipelineLockKey( pipeline.getAccountId(), pipeline.getRegion(), pipeline.getName()); - synchronized (sourcePollLocks.computeIfAbsent(pollLockKey, ignored -> new Object())) { + sourcePollLocks.withLock(pollLockKey, () -> { Optional currentPipeline = pipelineStore.getForAccount( pipeline.getAccountId(), pipelineKey(pipeline.getRegion(), pipeline.getName())); if (currentPipeline.isEmpty()) { return; } pollS3SourcesLocked(currentPipeline.get()); - } + }); } private void pollS3SourcesLocked(CodePipelinePipeline pipeline) { @@ -348,10 +348,10 @@ public class CodePipelineService { pipeline.setTags(parseTags(request.path("tags"))); initializeTransitions(pipeline); String pollLockKey = pipelineLockKey(account, region, name); - synchronized (sourcePollLocks.computeIfAbsent(pollLockKey, ignored -> new Object())) { + sourcePollLocks.withLock(pollLockKey, () -> { putPipeline(pipeline); resetSourcePollingBaselines(pipeline); - } + }); ObjectNode response = mapper.createObjectNode(); response.set("pipeline", pipeline.getDeclaration()); if (!pipeline.getTags().isEmpty()) { @@ -370,10 +370,10 @@ public class CodePipelineService { pipeline.setDeclaration(normalizeDeclaration(declaration, version)); initializeTransitions(pipeline); String pollLockKey = pipelineLockKey(account, region, name); - synchronized (sourcePollLocks.computeIfAbsent(pollLockKey, ignored -> new Object())) { + sourcePollLocks.withLock(pollLockKey, () -> { putPipeline(pipeline); resetSourcePollingBaselines(pipeline); - } + }); ObjectNode response = mapper.createObjectNode(); response.set("pipeline", pipeline.getDeclaration()); return response; @@ -400,7 +400,7 @@ public class CodePipelineService { String name = text(request, "name"); validatePipelineName(name); String pollLockKey = pipelineLockKey(account, region, name); - synchronized (sourcePollLocks.computeIfAbsent(pollLockKey, ignored -> new Object())) { + sourcePollLocks.withLock(pollLockKey, () -> { pipelineStore.deleteForAccount(account, pipelineKey(region, name)); for (String key : executionStore.keysForAccount(account)) { if (key.startsWith(region + ":" + name + ":")) { @@ -408,7 +408,7 @@ public class CodePipelineService { } } deleteSourcePollingBaselines(account, region, name); - } + }); return mapper.createObjectNode(); } @@ -495,7 +495,7 @@ public class CodePipelineService { putExecution(execution); return true; } - synchronized (startLocks.computeIfAbsent(lockKey(execution), ignored -> new Object())) { + return startLocks.withLock(lockKey(execution), () -> { long active = executions(execution.getAccountId(), execution.getRegion(), execution.getPipelineName()) .stream() .filter(candidate -> "InProgress".equals(candidate.getStatus()) @@ -506,7 +506,7 @@ public class CodePipelineService { } putExecution(execution); return true; - } + }); } private ObjectNode stopPipelineExecution(JsonNode request, String region, String account) { @@ -1014,9 +1014,7 @@ public class CodePipelineService { } }; if ("QUEUED".equals(execution.getExecutionMode())) { - synchronized (pipelineLocks.computeIfAbsent(lockKey(execution), ignored -> new Object())) { - work.run(); - } + pipelineLocks.withLock(lockKey(execution), work); } else { work.run(); } diff --git a/src/main/java/io/github/hectorvent/floci/services/codepipeline/KeyedLockPool.java b/src/main/java/io/github/hectorvent/floci/services/codepipeline/KeyedLockPool.java new file mode 100644 index 0000000000..1d35226178 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/codepipeline/KeyedLockPool.java @@ -0,0 +1,55 @@ +package io.github.hectorvent.floci.services.codepipeline; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; + +final class KeyedLockPool { + private final Map entries = new ConcurrentHashMap<>(); + + T withLock(String key, Supplier action) { + // Count queued callers before they wait so the shared monitor cannot be replaced underneath them. + Entry entry = retain(key); + try { + synchronized (entry.monitor) { + return action.get(); + } + } finally { + release(key, entry); + } + } + + void withLock(String key, Runnable action) { + withLock(key, () -> { + action.run(); + return null; + }); + } + + int size() { + return entries.size(); + } + + private Entry retain(String key) { + return entries.compute(key, (ignored, current) -> { + Entry entry = current == null ? new Entry() : current; + entry.users++; + return entry; + }); + } + + private void release(String key, Entry entry) { + entries.compute(key, (ignored, current) -> { + if (current != entry) { + throw new IllegalStateException("Keyed lock entry changed while in use"); + } + entry.users--; + return entry.users == 0 ? null : entry; + }); + } + + private static final class Entry { + private final Object monitor = new Object(); + private int users; + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/codepipeline/KeyedLockPoolTest.java b/src/test/java/io/github/hectorvent/floci/services/codepipeline/KeyedLockPoolTest.java new file mode 100644 index 0000000000..5aaed0e7a7 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/codepipeline/KeyedLockPoolTest.java @@ -0,0 +1,88 @@ +package io.github.hectorvent.floci.services.codepipeline; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class KeyedLockPoolTest { + + @Test + void serializesSameKeyAndReleasesTheEntry() throws Exception { + KeyedLockPool locks = new KeyedLockPool(); + CountDownLatch ready = new CountDownLatch(20); + CountDownLatch start = new CountDownLatch(1); + AtomicInteger active = new AtomicInteger(); + AtomicInteger maxActive = new AtomicInteger(); + List> tasks = new ArrayList<>(); + ExecutorService executor = Executors.newFixedThreadPool(20); + + try { + for (int i = 0; i < 20; i++) { + tasks.add(CompletableFuture.runAsync(() -> { + ready.countDown(); + try { + assertTrue(start.await(5, TimeUnit.SECONDS)); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException(e); + } + locks.withLock("pipeline", () -> { + int current = active.incrementAndGet(); + maxActive.accumulateAndGet(current, Math::max); + try { + Thread.sleep(5); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException(e); + } finally { + active.decrementAndGet(); + } + }); + }, executor)); + } + + assertTrue(ready.await(5, TimeUnit.SECONDS)); + start.countDown(); + CompletableFuture.allOf(tasks.toArray(CompletableFuture[]::new)).get(10, TimeUnit.SECONDS); + + assertEquals(1, maxActive.get()); + assertEquals(0, locks.size()); + } finally { + start.countDown(); + executor.shutdownNow(); + } + } + + @Test + void releasesEntriesWhenActionsFail() { + KeyedLockPool locks = new KeyedLockPool(); + + assertThrows(IllegalStateException.class, () -> locks.withLock("pipeline", () -> { + throw new IllegalStateException("boom"); + })); + + assertEquals(0, locks.size()); + } + + @Test + void releasesEntriesForDistinctKeys() { + KeyedLockPool locks = new KeyedLockPool(); + + for (int i = 0; i < 1_000; i++) { + locks.withLock("pipeline-" + i, () -> {}); + } + + assertEquals(0, locks.size()); + } +}