fix(codepipeline): cap QUEUED and PARALLEL pipelines at 50 active executions (#3503)

AWS allows at most 50 concurrent executions per QUEUED or PARALLEL
pipeline. StartPipelineExecution now counts the pipeline's InProgress
and Stopping executions under a per-pipeline admission lock and returns
ConcurrentPipelineExecutionsLimitExceededException at the limit. The
admission lock is separate from the monitor a QUEUED worker holds for
its whole run, so a start never waits for the running execution.

A start that reaches the executor after shutdown is persisted as Failed
and returns ConflictException instead of leaking
RejectedExecutionException. Shutdown waits up to five seconds for
execution workers to stop so their final status writes complete before
the process exits.
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-12 14:25:07 -04:00
1 parent ea5925f630
commit 2104b33ea7
4 files changed
+262 -2

No files matched your search

@@ -45,6 +45,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
@ApplicationScoped
@@ -52,6 +53,7 @@ public class CodePipelineService {
private static final Logger LOG = Logger.getLogger(CodePipelineService.class);
private static final String DEFAULT_EXECUTION_MODE = "SUPERSEDED";
private static final String DEFAULT_PIPELINE_TYPE = "V1";
private static final int MAX_ACTIVE_EXECUTIONS = 50;
private static final long POLL_INTERVAL_MS = 100L;
private final AccountAwareStorageBackend<CodePipelinePipeline> pipelineStore;
@@ -64,6 +66,10 @@ public class CodePipelineService {
private final S3Service s3Service;
private final ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
private final Map<String, Object> pipelineLocks = new ConcurrentHashMap<>();
// 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 Map<String, byte[]> runtimeArtifacts = new ConcurrentHashMap<>();
@Inject
@@ -282,12 +288,44 @@ public class CodePipelineService {
trigger.put("clientRequestToken", clientToken);
}
execution.setTrigger(trigger);
putExecution(execution);
if (!persistExecutionIfSlotAvailable(execution)) {
throw new AwsException("ConcurrentPipelineExecutionsLimitExceededException",
"The pipeline has reached the limit for concurrent pipeline executions", 400);
}
applyExecutionMode(execution);
executor.submit(() -> runExecution(pipeline, execution));
try {
executor.submit(() -> runExecution(pipeline, execution));
} catch (RejectedExecutionException exception) {
execution.setStatus("Failed");
execution.setStatusSummary("Pipeline execution could not be scheduled.");
execution.setLastUpdateTime(now());
putExecution(execution);
throw new AwsException("ConflictException",
"Your request cannot be handled because the pipeline is busy handling ongoing activities. "
+ "Try again later.", 400);
}
return mapper.createObjectNode().put("pipelineExecutionId", execution.getPipelineExecutionId());
}
private boolean persistExecutionIfSlotAvailable(CodePipelineExecution execution) {
if (!List.of("QUEUED", "PARALLEL").contains(execution.getExecutionMode())) {
putExecution(execution);
return true;
}
synchronized (startLocks.computeIfAbsent(lockKey(execution), ignored -> new Object())) {
long active = executions(execution.getAccountId(), execution.getRegion(), execution.getPipelineName())
.stream()
.filter(candidate -> "InProgress".equals(candidate.getStatus())
|| "Stopping".equals(candidate.getStatus()))
.count();
if (active >= MAX_ACTIVE_EXECUTIONS) {
return false;
}
putExecution(execution);
return true;
}
}
private ObjectNode stopPipelineExecution(JsonNode request, String region, String account) {
CodePipelineExecution execution = requireExecution(
account, region, text(request, "pipelineName"), text(request, "pipelineExecutionId"));
@@ -1509,6 +1547,14 @@ public class CodePipelineService {
@PreDestroy
void shutdown() {
executor.shutdownNow();
try {
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
LOG.warn("CodePipeline execution workers did not stop within the shutdown timeout");
}
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
LOG.warn("Interrupted while stopping CodePipeline execution workers", exception);
}
}
private record Page(int start, int end) {