fix(stepfunctions): let a .sync cleanup pass over only a job that is gone (#8540)

When a .sync Task's wait ends early, Step Functions "will make a
best-effort attempt to cancel the task". Floci's EMR Serverless undo
already passes over only the answers that say the application or run is
gone or past undoing, and fails the Task with any other. Its three
siblings passed over every failure: the EMR step's cancel, the ECS
tasks' stop and the child execution's abort.

Each now passes over only its service's answer for a job that is gone or
past undoing, logged as before: EMR's InvalidRequestException for a
cluster that is gone or a release that cannot cancel steps (a step it
will not cancel comes back FAILED in an answer that succeeds), ECS's
InvalidParameterException for a task it does not know, and Step
Functions' ExecutionDoesNotExist; a task already stopped and a child
already ended are answered without an error. Any other refusal fails the
state with the integration's error (EMR.<name>, ECS.<code>,
StepFunctions.<code>) and the service's message, the ECS one once every
task has been asked to stop, and any other failure is raised.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 22:00:35 -04:00
1 parent f15c14979e
commit b31b76e4aa
6 files changed
+160 -6

No files matched your search

@@ -3,6 +3,7 @@ package io.floci.aws.services.stepfunctions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.ecs.EcsJsonHandler;
import io.floci.aws.services.ecs.EcsService;
import io.floci.aws.services.ecs.container.HostVolumePolicy;
@@ -179,6 +180,48 @@ class AslExecutorEcsRunTaskModeTest {
+ "] which was managing this resource was aborted"), eq(REGION));
}
@Test
void syncStillTimesOutWhenTheTaskItStopsIsAlreadyGone() throws Exception {
launchOneTask();
when(ecsService.describeTasks(any(), any(), any())).thenReturn(List.of(task("RUNNING")));
when(ecsService.stopTask(any(), any(), any(), any()))
.thenThrow(new AwsException("InvalidParameterException", "Task not found: task", 400));
Execution execution = runDefinition("""
{
"StartAt": "RunTask",
"States": {
"RunTask": { "Type": "Task", "Resource": "arn:aws:states:::ecs:runTask.sync",
"TimeoutSeconds": 1, "End": true }
}
}
""", "{\"TaskDefinition\":\"my-task-def\"}");
// Best effort: a task that is gone leaves the state ending as it was.
assertEquals("States.Timeout", execution.getError());
}
@Test
void anotherRefusalStoppingTheTaskFailsTheStateWithIt() throws Exception {
launchOneTask();
when(ecsService.describeTasks(any(), any(), any())).thenReturn(List.of(task("RUNNING")));
when(ecsService.stopTask(any(), any(), any(), any()))
.thenThrow(new AwsException("ServerException", "The ECS service failed", 500));
Execution execution = runDefinition("""
{
"StartAt": "RunTask",
"States": {
"RunTask": { "Type": "Task", "Resource": "arn:aws:states:::ecs:runTask.sync",
"TimeoutSeconds": 1, "End": true }
}
}
""", "{\"TaskDefinition\":\"my-task-def\"}");
assertEquals("ECS.ServerException", execution.getError());
assertEquals("The ECS service failed", execution.getCause());
}
/**
* AWS records TaskSubmitted once RunTask has returned and before it waits on the task (measured:
* resourceType ecs, resource runTask.sync, the response as Failures then Tasks in PascalCase,
@@ -2,6 +2,7 @@ package io.floci.aws.services.stepfunctions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.emr.EmrHandler;
import io.floci.aws.services.emr.EmrService;
import io.floci.aws.services.emr.model.EmrCluster;
@@ -90,6 +91,53 @@ class AslExecutorEmrSyncTest {
verify(emrService).cancelSteps(CLUSTER_ID, List.of(STEP_ID));
}
@Test
void addStepSyncStillTimesOutWhenEmrNoLongerHasTheCluster() {
when(emrService.describeStep(CLUSTER_ID, STEP_ID)).thenReturn(step("RUNNING"));
when(emrService.cancelSteps(CLUSTER_ID, List.of(STEP_ID))).thenThrow(new AwsException(
"InvalidRequestException", "Cluster id 'j-SYNCTEST00001' is not valid.", 400));
Execution execution = run("""
{"StartAt": "Add", "States": {"Add": {"Type": "Task",
"Resource": "arn:aws:states:::elasticmapreduce:addStep.sync", "TimeoutSeconds": 1, "End": true}}}
""", ADD_STEP);
// Best effort: nothing left to cancel leaves the Task ending as it was.
assertEquals("States.Timeout", execution.getError());
verify(emrService).cancelSteps(CLUSTER_ID, List.of(STEP_ID));
}
@Test
void addStepSyncStillTimesOutWhenEmrWillNotCancelTheStep() {
when(emrService.describeStep(CLUSTER_ID, STEP_ID)).thenReturn(step("RUNNING"));
when(emrService.cancelSteps(CLUSTER_ID, List.of(STEP_ID))).thenReturn(List.of(
new EmrService.CancelStepInfo(STEP_ID, "FAILED", "Step is not in a cancellable state: RUNNING")));
Execution execution = run("""
{"StartAt": "Add", "States": {"Add": {"Type": "Task",
"Resource": "arn:aws:states:::elasticmapreduce:addStep.sync", "TimeoutSeconds": 1, "End": true}}}
""", ADD_STEP);
// A step EMR will not cancel comes back FAILED in an answer that succeeds, which the cleanup passes over.
assertEquals("States.Timeout", execution.getError());
verify(emrService).cancelSteps(CLUSTER_ID, List.of(STEP_ID));
}
@Test
void anInternalErrorCancellingTheStepFailsTheTaskWithIt() {
when(emrService.describeStep(CLUSTER_ID, STEP_ID)).thenReturn(step("RUNNING"));
when(emrService.cancelSteps(CLUSTER_ID, List.of(STEP_ID)))
.thenThrow(new AwsException("InternalServerException", "Request processing failed", 500));
Execution execution = run("""
{"StartAt": "Add", "States": {"Add": {"Type": "Task",
"Resource": "arn:aws:states:::elasticmapreduce:addStep.sync", "TimeoutSeconds": 1, "End": true}}}
""", ADD_STEP);
assertEquals("EMR.InternalServerException", execution.getError());
assertEquals("Request processing failed", execution.getCause());
}
@Test
void addStepSyncFailsWithTheStepWhenTheStepFails() throws Exception {
EmrStep failed = step("FAILED");
@@ -2,6 +2,7 @@ package io.floci.aws.services.stepfunctions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.stepfunctions.model.Execution;
import io.floci.aws.services.stepfunctions.model.ExecutionStatusSubscription;
import io.floci.aws.services.stepfunctions.model.HistoryEvent;
@@ -32,6 +33,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
@@ -88,6 +90,30 @@ class AslExecutorNestedSyncTimeoutTest {
verify(sfnService).stopExecution(eq(CHILD_ARN), eq(ABORT_CAUSE), isNull());
}
@Test
void syncStillTimesOutWhenTheChildItAbortsIsGone() {
when(sfnService.describeExecution(CHILD_ARN)).thenReturn(child("RUNNING"));
doThrow(new AwsException("ExecutionDoesNotExist", "Execution does not exist", 400))
.when(sfnService).stopExecution(eq(CHILD_ARN), any(), any());
Execution execution = run(newExecutor(TimeUnit.NANOSECONDS::sleep), ".sync", 1, false);
// Best effort: a child that is gone leaves the state ending as it was.
assertEquals("States.Timeout", execution.getError());
}
@Test
void anotherRefusalAbortingTheChildFailsTheStateWithIt() {
when(sfnService.describeExecution(CHILD_ARN)).thenReturn(child("RUNNING"));
doThrow(new AwsException("ValidationException", "The execution could not be stopped", 400))
.when(sfnService).stopExecution(eq(CHILD_ARN), any(), any());
Execution execution = run(newExecutor(TimeUnit.NANOSECONDS::sleep), ".sync", 1, false);
assertEquals("StepFunctions.ValidationException", execution.getError());
assertEquals("The execution could not be stopped", execution.getCause());
}
@Test
void sync2TimeoutIsCaughtUnderStatesTimeout() throws Exception {
when(sfnService.describeExecution(CHILD_ARN)).thenReturn(child("RUNNING"));