fix(codepipeline): release per-pipeline locks after use (#3712)

Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
XuJianandxujiantop-crypto authored and GitHub committed 2026-09-15 23:29:53 -04:00
1 parent 801edd863d
commit e5f9baa12d
3 files changed
+157 -16

No files matched your search

@@ -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<String, Object> pipelineLocks = new ConcurrentHashMap<>();
private final Map<String, Object> 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<String, Object> startLocks = new ConcurrentHashMap<>();
private final KeyedLockPool startLocks = new KeyedLockPool();
private final Map<String, byte[]> 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<CodePipelinePipeline> 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();
}
@@ -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<String, Entry> entries = new ConcurrentHashMap<>();
<T> T withLock(String key, Supplier<T> 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;
}
}
@@ -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<CompletableFuture<Void>> 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());
}
}