mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
ea5925f630
commit
2104b33ea7
4 files changed
+262
-2
No files matched your search
+115
@@ -8,8 +8,11 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
@@ -1165,6 +1168,50 @@ class CodePipelineIntegrationTest {
|
||||
.body("__type", containsString("InvalidStructureException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parallelPipelineRejectsExecutionAfterAwsActiveLimit() throws Exception {
|
||||
String pipelineName = "parallel-limit-pipeline";
|
||||
post("CreatePipeline", approvalPipeline(pipelineName, "PARALLEL")).then().statusCode(200);
|
||||
List<String> executionIds = new ArrayList<>();
|
||||
try {
|
||||
while (executionIds.size() < 50) {
|
||||
executionIds.add(startExecution(pipelineName));
|
||||
}
|
||||
waitForActiveExecutions(pipelineName, 50);
|
||||
|
||||
post("StartPipelineExecution", "{\"name\": \"%s\"}".formatted(pipelineName))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ConcurrentPipelineExecutionsLimitExceededException"));
|
||||
} finally {
|
||||
stopExecutions(pipelineName, executionIds);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void queuedPipelineAcceptsStartsWhileAnExecutionRunsAndRejectsTheFiftyFirst() throws Exception {
|
||||
String pipelineName = "queued-limit-pipeline";
|
||||
post("CreatePipeline", approvalPipeline(pipelineName, "QUEUED")).then().statusCode(200);
|
||||
List<String> executionIds = new ArrayList<>();
|
||||
try {
|
||||
executionIds.add(startExecution(pipelineName));
|
||||
waitForApprovalToken(pipelineName);
|
||||
|
||||
assertTimeoutPreemptively(Duration.ofSeconds(10), () -> {
|
||||
while (executionIds.size() < 50) {
|
||||
executionIds.add(startExecution(pipelineName));
|
||||
}
|
||||
}, "StartPipelineExecution waited for the running execution to finish");
|
||||
|
||||
post("StartPipelineExecution", "{\"name\": \"%s\"}".formatted(pipelineName))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ConcurrentPipelineExecutionsLimitExceededException"));
|
||||
} finally {
|
||||
stopExecutions(pipelineName, executionIds);
|
||||
}
|
||||
}
|
||||
|
||||
private Response waitForJob() throws Exception {
|
||||
Instant deadline = Instant.now().plus(Duration.ofSeconds(5));
|
||||
Response response;
|
||||
@@ -1244,6 +1291,74 @@ class CodePipelineIntegrationTest {
|
||||
throw new AssertionError("Approval token was not issued for pipeline " + pipelineName);
|
||||
}
|
||||
|
||||
private void waitForActiveExecutions(String pipelineName, int expected) throws Exception {
|
||||
Instant deadline = Instant.now().plus(Duration.ofSeconds(5));
|
||||
do {
|
||||
int active = post("ListPipelineExecutions", "{\"pipelineName\": \"%s\"}".formatted(pipelineName))
|
||||
.jsonPath().getList("pipelineExecutionSummaries.status", String.class).stream()
|
||||
.filter("InProgress"::equals)
|
||||
.toList()
|
||||
.size();
|
||||
if (active >= expected) {
|
||||
return;
|
||||
}
|
||||
Thread.sleep(50);
|
||||
} while (Instant.now().isBefore(deadline));
|
||||
throw new AssertionError("Pipeline did not reach " + expected + " active executions");
|
||||
}
|
||||
|
||||
private static String approvalPipeline(String name, String executionMode) {
|
||||
return """
|
||||
{
|
||||
"pipeline": {
|
||||
"name": "%s",
|
||||
"roleArn": "arn:aws:iam::000000000000:role/codepipeline-role",
|
||||
"pipelineType": "V2",
|
||||
"executionMode": "%s",
|
||||
"artifactStore": {"type": "S3", "location": "codepipeline-artifacts"},
|
||||
"stages": [{
|
||||
"name": "Approve",
|
||||
"actions": [{
|
||||
"name": "ManualApproval",
|
||||
"actionTypeId": {
|
||||
"category": "Approval",
|
||||
"owner": "AWS",
|
||||
"provider": "Manual",
|
||||
"version": "1"
|
||||
}
|
||||
}]
|
||||
}, {
|
||||
"name": "Complete",
|
||||
"actions": [{
|
||||
"name": "ManualApprovalComplete",
|
||||
"actionTypeId": {
|
||||
"category": "Approval",
|
||||
"owner": "AWS",
|
||||
"provider": "Manual",
|
||||
"version": "1"
|
||||
}
|
||||
}]
|
||||
}]
|
||||
}
|
||||
}
|
||||
""".formatted(name, executionMode);
|
||||
}
|
||||
|
||||
private static String startExecution(String pipelineName) {
|
||||
return post("StartPipelineExecution", "{\"name\": \"%s\"}".formatted(pipelineName))
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().jsonPath().getString("pipelineExecutionId");
|
||||
}
|
||||
|
||||
private static void stopExecutions(String pipelineName, List<String> executionIds) {
|
||||
for (String executionId : executionIds) {
|
||||
post("StopPipelineExecution", """
|
||||
{"pipelineName": "%s", "pipelineExecutionId": "%s", "abandon": true}
|
||||
""".formatted(pipelineName, executionId)).then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
private static Response post(String action, String body) {
|
||||
return given()
|
||||
.header("X-Amz-Target", TARGET + action)
|
||||
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package io.github.hectorvent.floci.services.codepipeline;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.codebuild.CodeBuildService;
|
||||
import io.github.hectorvent.floci.services.codedeploy.CodeDeployService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class CodePipelineServiceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final CodePipelineService service = new CodePipelineService(
|
||||
new InMemoryStorageFactory(),
|
||||
mapper,
|
||||
mock(CodeBuildService.class),
|
||||
mock(CodeDeployService.class),
|
||||
mock(LambdaService.class),
|
||||
mock(S3Service.class));
|
||||
|
||||
@Test
|
||||
void aStartAfterShutdownIsRefusedAndTheExecutionIsPersistedAsFailed() throws Exception {
|
||||
handle("CreatePipeline", """
|
||||
{
|
||||
"pipeline": {
|
||||
"name": "pipeline",
|
||||
"roleArn": "arn:aws:iam::000000000000:role/codepipeline-role",
|
||||
"artifactStore": {"type": "S3", "location": "codepipeline-artifacts"},
|
||||
"stages": [{
|
||||
"name": "Approve",
|
||||
"actions": [{
|
||||
"name": "ManualApproval",
|
||||
"actionTypeId": {
|
||||
"category": "Approval",
|
||||
"owner": "AWS",
|
||||
"provider": "Manual",
|
||||
"version": "1"
|
||||
}
|
||||
}]
|
||||
}, {
|
||||
"name": "Complete",
|
||||
"actions": [{
|
||||
"name": "ManualApprovalComplete",
|
||||
"actionTypeId": {
|
||||
"category": "Approval",
|
||||
"owner": "AWS",
|
||||
"provider": "Manual",
|
||||
"version": "1"
|
||||
}
|
||||
}]
|
||||
}]
|
||||
}
|
||||
}
|
||||
""");
|
||||
service.shutdown();
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> handle("StartPipelineExecution", "{\"name\": \"pipeline\"}"));
|
||||
|
||||
assertEquals("ConflictException", refused.getErrorCode());
|
||||
assertEquals(400, refused.getHttpStatus());
|
||||
JsonNode summaries = handle("ListPipelineExecutions", "{\"pipelineName\": \"pipeline\"}")
|
||||
.path("pipelineExecutionSummaries");
|
||||
assertEquals(1, summaries.size());
|
||||
assertEquals("Failed", summaries.get(0).path("status").asText());
|
||||
}
|
||||
|
||||
private JsonNode handle(String action, String body) throws Exception {
|
||||
return service.handle(action, mapper.readTree(body), REGION, ACCOUNT);
|
||||
}
|
||||
|
||||
private static final class InMemoryStorageFactory extends StorageFactory {
|
||||
private InMemoryStorageFactory() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return AccountAwareStorageBackend.inMemory(ACCOUNT);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user