Light, fluffy, and always free - The AWS Local Emulator alternative
fix(codepipeline): guard worker polling intervals (#6274)
Daniël van Zielf6d67461d2fb
diff --git a/docs/services/codepipeline.md b/docs/services/codepipeline.mdindex ce177529f4..9b1333a171 100644--- a/docs/services/codepipeline.md+++ b/docs/services/codepipeline.md@@ -115,6 +115,8 @@ the approval token, `expires` as an ISO-8601 timestamp, `consoleLink`, `approval| `FLOCI_SERVICES_CODEPIPELINE_SOURCE_POLL_INTERVAL_MS` | `500` | How often (ms) S3 sources are polled for a new object revision || `FLOCI_SERVICES_CODEPIPELINE_WORKER_POLL_INTERVAL_MS` | `100` | How often (ms) execution workers check for progress, independently of S3 source polling |+A zero or negative worker polling interval logs a warning and falls back to 100 ms.+## Example```bashdiff --git a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.javaindex 7607ed0c5c..d15c934f0d 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@@ -126,8 +126,9 @@ public class CodePipelineService {// An execution's status turns Failed as soon as one action fails, while its runner is still waiting// on sibling actions. Retries check this set so they never overlap a runner that has not finished.final Set<String> activeRuns = ConcurrentHashMap.newKeySet();- private final long sourcePollIntervalMs;- private long workerPollIntervalMs = DEFAULT_WORKER_POLL_INTERVAL_MS;+ // Package-private so tests can assert the resolved intervals instead of timing schedules and sleeps.+ final long sourcePollIntervalMs;+ final long workerPollIntervalMs;// Test hooks, set only through the package-private constructor: a shortened wait, and a probe// for the gap before the override timeout cleanup.private final long runFinishWaitNanos;@@ -149,7 +150,7 @@ public class CodePipelineService {CodeBuildService codeBuildService, CodeDeployService codeDeployService,LambdaService lambdaService, S3Service s3Service) {this(storageFactory, mapper, codeBuildService, codeDeployService, lambdaService, s3Service,- detachedEventPublisher(mapper), DEFAULT_SOURCE_POLL_INTERVAL_MS,+ detachedEventPublisher(mapper), DEFAULT_SOURCE_POLL_INTERVAL_MS, DEFAULT_WORKER_POLL_INTERVAL_MS,DEFAULT_RUN_FINISH_WAIT_NANOS, () -> { });}@@ -168,9 +169,9 @@ public class CodePipelineService {EmulatorConfig config, CodePipelineEventPublisher eventPublisher) {this(storageFactory, mapper, codeBuildService, codeDeployService, lambdaService, s3Service,eventPublisher,- resolveSourcePollInterval(config.services().codepipeline().sourcePollIntervalMs()),+ config.services().codepipeline().sourcePollIntervalMs(),+ config.services().codepipeline().workerPollIntervalMs(),DEFAULT_RUN_FINISH_WAIT_NANOS, () -> { });- this.workerPollIntervalMs = config.services().codepipeline().workerPollIntervalMs();}/**@@ -186,20 +187,20 @@ public class CodePipelineService {* observer and stop the whole emulator becoming ready. A misconfigured value is not worth* failing every other service for, so it falls back with a warning.*/- private static long resolveSourcePollInterval(long configured) {+ static long resolvePollInterval(String kind, long configured, long fallback) {if (configured > 0) {return configured;}- LOG.warnv("Ignoring CodePipeline source poll interval {0}ms: must be a positive number of "- + "milliseconds, falling back to {1}ms", configured, DEFAULT_SOURCE_POLL_INTERVAL_MS);- return DEFAULT_SOURCE_POLL_INTERVAL_MS;+ LOG.warnv("Ignoring CodePipeline {0} poll interval {1}ms: must be a positive number of "+ + "milliseconds, falling back to {2}ms", kind, configured, fallback);+ return fallback;}@SuppressWarnings("unchecked")CodePipelineService(StorageFactory storageFactory, ObjectMapper mapper,CodeBuildService codeBuildService, CodeDeployService codeDeployService,LambdaService lambdaService, S3Service s3Service,- CodePipelineEventPublisher eventPublisher, long sourcePollIntervalMs,+ CodePipelineEventPublisher eventPublisher, long sourcePollIntervalMs, long workerPollIntervalMs,long runFinishWaitNanos, Runnable beforeOverrideTimeoutCleanup) {this.pipelineStore = storageFactory.create("codepipeline", "codepipeline-pipelines.json", new TypeReference<Map<String, CodePipelinePipeline>>() {});@@ -213,7 +214,10 @@ public class CodePipelineService {this.lambdaService = lambdaService;this.s3Service = s3Service;this.eventPublisher = eventPublisher;- this.sourcePollIntervalMs = sourcePollIntervalMs;+ this.sourcePollIntervalMs = resolvePollInterval("source", sourcePollIntervalMs,+ DEFAULT_SOURCE_POLL_INTERVAL_MS);+ this.workerPollIntervalMs = resolvePollInterval("worker", workerPollIntervalMs,+ DEFAULT_WORKER_POLL_INTERVAL_MS);this.runFinishWaitNanos = runFinishWaitNanos;this.beforeOverrideTimeoutCleanup = beforeOverrideTimeoutCleanup;}diff --git a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineRetryRollbackTest.java b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineRetryRollbackTest.javaindex d74d2ecfb6..25c51d9e3f 100644--- a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineRetryRollbackTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineRetryRollbackTest.java@@ -87,7 +87,7 @@ class CodePipelineRetryRollbackTest {EventBridgeService eventBridge) {return new CodePipelineService(storage, mapper, mock(CodeBuildService.class),mock(CodeDeployService.class), lambda, s3,- new CodePipelineEventPublisher(eventBridge, null, mapper), 500L,+ new CodePipelineEventPublisher(eventBridge, null, mapper), 500L, 100L,TimeUnit.SECONDS.toNanos(5), () -> { });}diff --git a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineServiceTest.javaindex 4a78d2479d..d7f748da67 100644--- a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineServiceTest.java@@ -167,7 +167,11 @@ class CodePipelineServiceTest {* with "not an interface".*/private static EmulatorConfig configWithPollInterval(long intervalMs) {- Map<String, Object> answers = Map.of("sourcePollIntervalMs", intervalMs, "workerPollIntervalMs", 1L);+ return configWithPollIntervals(intervalMs, 1L);+ }++ private static EmulatorConfig configWithPollIntervals(long sourceMs, long workerMs) {+ Map<String, Object> answers = Map.of("sourcePollIntervalMs", sourceMs, "workerPollIntervalMs", workerMs);InvocationHandler handler = new InvocationHandler() {@Override@@ -190,6 +194,161 @@ class CodePipelineServiceTest {new Class<?>[] {EmulatorConfig.class}, handler);}+ @Test+ void nonPositiveWorkerIntervalsFallBackToTheDefault() {+ for (boolean direct : new boolean[] {false, true}) {+ for (long interval : new long[] {0L, -1L, Long.MIN_VALUE}) {+ CodePipelineService pipelineService =+ workerPollingService(mock(CodeBuildService.class), interval, direct);+ try {+ assertEquals(100L, pipelineService.workerPollIntervalMs,+ "interval " + interval + " (direct=" + direct + ") must fall back to 100ms");+ } finally {+ pipelineService.shutdown();+ }+ }+ }+ }++ @Test+ void nonPositiveSourceIntervalsFallBackToTheDefault() {+ for (long interval : new long[] {0L, -1L, Long.MIN_VALUE}) {+ CodePipelineService pipelineService = new CodePipelineService(new InMemoryStorageFactory(), mapper,+ mock(CodeBuildService.class), mock(CodeDeployService.class), mock(LambdaService.class),+ mock(S3Service.class), configWithPollIntervals(interval, 739L));+ try {+ assertEquals(500L, pipelineService.sourcePollIntervalMs,+ "source interval " + interval + " must fall back to 500ms");+ assertEquals(739L, pipelineService.workerPollIntervalMs,+ "a valid worker interval must not follow the source fallback");+ } finally {+ pipelineService.shutdown();+ }+ }+ }++ @Test+ void resolvePollIntervalUsesTheFallbackOfEachKind() {+ for (long interval : new long[] {0L, -1L, Long.MIN_VALUE}) {+ assertEquals(500L, CodePipelineService.resolvePollInterval("source", interval, 500L));+ assertEquals(100L, CodePipelineService.resolvePollInterval("worker", interval, 100L));+ }+ for (long interval : new long[] {1L, 739L, Long.MAX_VALUE}) {+ assertEquals(interval, CodePipelineService.resolvePollInterval("source", interval, 500L));+ assertEquals(interval, CodePipelineService.resolvePollInterval("worker", interval, 100L));+ }+ }++ @Test+ void positiveWorkerIntervalsArePreserved() {+ for (boolean direct : new boolean[] {false, true}) {+ for (long interval : new long[] {1L, 739L, Long.MAX_VALUE}) {+ CodePipelineService pipelineService =+ workerPollingService(mock(CodeBuildService.class), interval, direct);+ try {+ assertEquals(interval, pipelineService.workerPollIntervalMs,+ "interval " + interval + " (direct=" + direct + ") must not be replaced");+ } finally {+ pipelineService.shutdown();+ }+ }+ }+ }++ @Test+ void invalidConfiguredWorkerIntervalKeepsPollingAndStopsAfterBuildCompletion() throws Exception {+ assertWorkerPollingProgress(0L, false);+ }++ @Test+ void invalidDirectWorkerIntervalKeepsPollingAndStopsAfterBuildCompletion() throws Exception {+ assertWorkerPollingProgress(Long.MIN_VALUE, true);+ }++ private CodePipelineService workerPollingService(CodeBuildService codeBuild, long interval, boolean direct) {+ if (direct) {+ return new CodePipelineService(new InMemoryStorageFactory(), mapper, codeBuild,+ mock(CodeDeployService.class), mock(LambdaService.class), mock(S3Service.class),+ new CodePipelineEventPublisher(null, null, mapper), 500L, interval,+ TimeUnit.SECONDS.toNanos(5), () -> { });+ }+ return new CodePipelineService(new InMemoryStorageFactory(), mapper, codeBuild,+ mock(CodeDeployService.class), mock(LambdaService.class), mock(S3Service.class),+ configWithPollIntervals(500L, interval));+ }++ private void assertWorkerPollingProgress(long interval, boolean direct) throws Exception {+ CodeBuildService codeBuild = mock(CodeBuildService.class);+ AtomicReference<Build> currentBuild = new AtomicReference<>(build("IN_PROGRESS", false));+ CountDownLatch repeatedPolls = new CountDownLatch(3);+ when(codeBuild.startBuild(anyString(), anyString(), anyString(),+ any(), any(), any(), any(), any(), any(), any())).thenAnswer(invocation -> currentBuild.get());+ when(codeBuild.getBuild(anyString(), anyString(), anyString())).thenAnswer(invocation -> {+ repeatedPolls.countDown();+ return currentBuild.get();+ });+ CodePipelineService pipelineService = workerPollingService(codeBuild, interval, direct);+ String pipeline = "worker-poll-guard";+ try {+ createExternalActionPipeline(pipelineService, pipeline, """+ {"name":"BuildAction","actionTypeId":{+ "category":"Build","owner":"AWS","provider":"CodeBuild","version":"1"},+ "configuration":{"ProjectName":"build-project"}}+ """);+ String execution = startExecution(pipelineService, pipeline);+ // The fallback interval itself is asserted directly; here only progress matters, so+ // there is a generous deadline and no upper bound on how many polls happen meanwhile.+ assertTrue(repeatedPolls.await(30, TimeUnit.SECONDS), "Worker must keep polling the build");+ stopExecution(pipelineService, pipeline, execution, false);+ waitForExecutionStatus(pipelineService, pipeline, execution, "Stopping");+ currentBuild.set(build("SUCCEEDED", true));+ waitForExecutionStatus(pipelineService, pipeline, execution, "Stopped");+ await().pollInterval(Duration.ofMillis(10)).atMost(Duration.ofSeconds(3))+ .until(() -> !pipelineService.activeRuns.contains(ACCOUNT + ":" + execution));+ assertEquals("Succeeded", pipelineState(pipelineService, pipeline).path("stageStates").path(0)+ .path("actionStates").path(0).path("latestExecution").path("status").asText());+ verify(codeBuild, never()).stopBuild(anyString(), anyString(), anyString());+ } finally {+ pipelineService.shutdown();+ }+ }++ @Test+ void largePositiveWorkerIntervalIsPreservedAndShutdownInterruptsTheWorker() throws Exception {+ for (boolean direct : new boolean[] {false, true}) {+ CodeBuildService codeBuild = mock(CodeBuildService.class);+ CountDownLatch started = new CountDownLatch(1);+ AtomicReference<Thread> worker = new AtomicReference<>();+ when(codeBuild.startBuild(anyString(), anyString(), anyString(),+ any(), any(), any(), any(), any(), any(), any())).thenAnswer(invocation -> {+ worker.set(Thread.currentThread());+ started.countDown();+ return build("IN_PROGRESS", false);+ });+ CodePipelineService pipelineService = workerPollingService(codeBuild, Long.MAX_VALUE, direct);+ try {+ createExternalActionPipeline(pipelineService, "large-worker-poll", """+ {"name":"BuildAction","actionTypeId":{+ "category":"Build","owner":"AWS","provider":"CodeBuild","version":"1"},+ "configuration":{"ProjectName":"build-project"}}+ """);+ String execution = startExecution(pipelineService, "large-worker-poll");+ assertTrue(started.await(30, TimeUnit.SECONDS));+ await().pollInterval(Duration.ofMillis(10)).atMost(Duration.ofSeconds(30))+ .until(() -> worker.get().getState() == Thread.State.TIMED_WAITING);+ pipelineService.shutdown();+ // The pool can report termination while its thread is still unwinding, so join it.+ worker.get().join(TimeUnit.SECONDS.toMillis(30));+ assertFalse(worker.get().isAlive(), "Shutdown must interrupt the worker's long sleep");+ // A preserved huge interval means the interrupted sleep was the first and only one.+ verify(codeBuild, never()).getBuild(anyString(), anyString(), anyString());+ assertFalse(pipelineService.activeRuns.contains(ACCOUNT + ":" + execution));+ } finally {+ pipelineService.shutdown();+ }+ }+ }+@Testvoid getPipelineTreatsMissingStoredVersionAsVersionOne() throws Exception {CapturingStorageFactory storageFactory = new CapturingStorageFactory();diff --git a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineV2ConditionsTest.java b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineV2ConditionsTest.javaindex 819d6c4e11..bf72353019 100644--- a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineV2ConditionsTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineV2ConditionsTest.java@@ -31,6 +31,7 @@ import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicBoolean;import java.util.concurrent.atomic.AtomicReference;+import static org.awaitility.Awaitility.await;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertThrows;@@ -279,7 +280,7 @@ class CodePipelineV2ConditionsTest {EventBridgeService eventBridge = mock(EventBridgeService.class);service = new CodePipelineService(storageFactory, mapper, mock(CodeBuildService.class),mock(CodeDeployService.class), lambdaService, s3Service,- new CodePipelineEventPublisher(eventBridge, null, mapper), 500L,+ new CodePipelineEventPublisher(eventBridge, null, mapper), 500L, 100L,TimeUnit.SECONDS.toNanos(5), () -> { });ObjectNode deploy = lambdaStage("Deploy");addRule(deploy, "beforeEntry", "SKIP", "VariableCheck")@@ -436,7 +437,7 @@ class CodePipelineV2ConditionsTest {AtomicReference<ObjectNode> pendingOverride = new AtomicReference<>();service = new CodePipelineService(storageFactory, mapper, mock(CodeBuildService.class),mock(CodeDeployService.class), lambdaService, s3Service,- new CodePipelineEventPublisher(null, null, mapper), 500L,+ new CodePipelineEventPublisher(null, null, mapper), 500L, 100L,TimeUnit.MILLISECONDS.toNanos(200), () -> {if (cleanupProbed.compareAndSet(false, true)) {ObjectNode request = pendingOverride.get();@@ -455,8 +456,12 @@ class CodePipelineV2ConditionsTest {.put("pipelineExecutionId", executionId).put("stageName", "Deploy").put("conditionType", "ON_SUCCESS");pendingOverride.set(override);- // The run looks like it is still finishing, so the first override waits and then times out.+ // The run marks itself Failed before it leaves activeRuns. Wait for it to leave, or its own+ // removal can erase the synthetic marker below and let the first override resume the run.String runKey = ACCOUNT + ":" + executionId;+ await().pollInterval(Duration.ofMillis(10)).atMost(Duration.ofSeconds(30))+ .until(() -> !service.activeRuns.contains(runKey));+ // The run looks like it is still finishing, so the first override waits and then times out.service.activeRuns.add(runKey);AwsException thrown = assertThrows(AwsException.class,