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

@@ -2477,23 +2477,43 @@ public class AslExecutor {
+ "] which was managing this resource was aborted";
}
/** Best effort, like AWS: a task that is already gone does not change how the state ends. */
/**
* Best effort, like AWS: a task that is already gone, which ECS answers with InvalidParameterException, does not
* change how the state ends, nor does one already stopped, which StopTask answers as it is. Any other refusal is a
* fault, raised as the integration's ECS error once every task has been asked to stop, as the emr-serverless undo
* raises its own; any other failure is raised at once.
*/
private void stopEcsTasks(String cluster, List<String> taskArns, String executionArn, String region) {
FailStateException fault = null;
for (String taskArn : taskArns) {
try {
ecsService.stopTask(cluster, taskArn, syncAbortCause(executionArn), region);
} catch (RuntimeException e) {
} catch (AwsException e) {
if (!"InvalidParameterException".equals(e.getErrorCode())) {
fault = fault != null ? fault : new FailStateException("ECS." + e.getErrorCode(), e.getMessage());
continue;
}
LOG.warnv("ecs:runTask.sync ended before its task {0} did, and the task could not be stopped: {1}",
taskArn, e.getMessage());
}
}
if (fault != null) {
throw fault;
}
}
/** Best effort, like AWS: a child that already ended does not change how the state ends. */
/**
* Best effort, like AWS: a child that is gone, which Step Functions answers with ExecutionDoesNotExist, does not
* change how the state ends, nor does one that already ended, which StopExecution leaves as it is. Any other
* refusal is a fault, raised as the integration names a refused StartExecution, and any other failure is raised.
*/
private void abortChildExecution(String childExecutionArn, String executionArn) {
try {
sfnService.get().stopExecution(childExecutionArn, syncAbortCause(executionArn), null);
} catch (RuntimeException e) {
} catch (AwsException e) {
if (!"ExecutionDoesNotExist".equals(e.getErrorCode())) {
throw new FailStateException(optimizedSfnErrorName(e.getErrorCode()), e.getMessage());
}
LOG.warnv("states:startExecution.sync ended before its child {0} did, and the child could not be stopped: {1}",
childExecutionArn, e.getMessage());
}
@@ -37,6 +37,9 @@ public class EmrTaskInvoker {
// a ValidationException fails addStep.sync as EMR.AmazonElasticMapReduceException.
private static final Set<String> MODELED_ERRORS = Set.of("InvalidRequestException", "InternalServerException");
private static final String BASE_ERROR = "AmazonElasticMapReduceException";
// What EMR answers a step's cancel when its cluster is gone, or the step past cancelling where a cluster's release
// takes no cancel.
private static final String NOTHING_TO_UNDO = "EMR.InvalidRequestException";
private static final Set<String> APIS = Set.of("createCluster", "setClusterTerminationProtection",
"terminateCluster", "addStep", "cancelStep", "modifyInstanceFleetByName", "modifyInstanceGroupByName");
private static final Set<String> SYNC_APIS = Set.of("createCluster", "terminateCluster", "addStep");
@@ -130,7 +133,14 @@ public class EmrTaskInvoker {
request.putArray("StepIds").add(job.stepId());
try {
call("CancelSteps", request, region);
} catch (RuntimeException e) {
} catch (AslExecutor.FailStateException e) {
// EMR's InvalidRequestException says the cluster is gone or its release cannot cancel steps (a step it
// will not cancel comes back FAILED in an answer that succeeds). Being best effort, either leaves the Task
// ending as it was. Any other answer, like any other failure, is a fault and is raised, as the
// emr-serverless undo raises its own.
if (!NOTHING_TO_UNDO.equals(e.error)) {
throw e;
}
LOG.warnv("elasticmapreduce:addStep.sync ended before its step {0} did, and the step could not be "
+ "cancelled: {1}", job.stepId(), e.getMessage());
}