mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(stepfunctions): record TaskSubmitted for a .sync Task, named as AWS names it (#4719)
* fix(stepfunctions): record TaskSubmitted for a .sync Task, named as AWS names it
AWS records TaskSubmitted for every .sync Task once the call that starts
the job has returned and before it waits on the job, chained to
TaskStarted and carrying that call's response as output in PascalCase:
{ExecutionArn, StartDate} in epoch milliseconds for states:startExecution
.sync and .sync:2, {Failures, Tasks} for ecs:runTask.sync. Floci recorded
nothing between TaskStarted and the terminal event, so a client reading
the job's id from the history found nothing until the Task ended.
The two poll loops do not have the HistoryChain; invokeResource hands
them a callback that publishes the event. The SDK's SdkHttpMetadata and
SdkResponseMetadata, which AWS folds into the response, are left out,
as everywhere else in Floci.
The task event profile also split a well-formed integration id at its
last colon, so every event of a .sync:2 Task read resourceType
states:startExecution.sync, resource 2. AWS reports states /
startExecution.sync:2, so a parsed id now splits at its service.
Closes #4648
* test(stepfunctions): name the types in the TaskSubmitted history test
* fix(stepfunctions): stop the job when TaskSubmitted cannot be recorded
TaskSubmitted is the first event after the job exists. When it is the
event that hits the 25,000-event history limit, the publish throws
before the poll loop whose catch stops the job is entered, so the parent
failed with States.Runtime while the child execution or ECS task ran on.
Both loops now abort the job on that path, the way the wait would have.
---------
Co-authored-by: mitsukado-shinagawa <mitsukado-shinagawa@users.noreply.github.com>
This commit is contained in:
1 parent
560de5c13f
commit
2eadc106c5
5 files changed
+330
-17
No files matched your search
@@ -118,6 +118,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ApplicationScoped
|
||||
@@ -825,7 +826,8 @@ public class AslExecutor {
|
||||
: invokeResource(effectiveResource, effectiveInput, sm, taskToken,
|
||||
context.path("Execution").path("Id").asText(null),
|
||||
executionDeadlineNanos, taskDeadlineNanos,
|
||||
jsonata ? null : stateDef.path("Parameters"));
|
||||
jsonata ? null : stateDef.path("Parameters"),
|
||||
jobResponse -> addTaskSubmittedEvent(chain, profile, jobResponse));
|
||||
if (tokenFuture != null) {
|
||||
taskResult = awaitToken(tokenFuture, stateDef, taskToken, executionDeadlineNanos,
|
||||
taskDeadlineNanos);
|
||||
@@ -1075,9 +1077,13 @@ public class AslExecutor {
|
||||
return new FailStateException("Exception", cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param jobSubmitted called with the response of the call that started a {@code .sync} job,
|
||||
* once it has returned and before the job is waited on
|
||||
*/
|
||||
private JsonNode invokeResource(String resource, JsonNode input, StateMachine sm, String taskToken,
|
||||
String executionArn, long executionDeadlineNanos, long taskDeadlineNanos,
|
||||
JsonNode rawParameters) throws Exception {
|
||||
JsonNode rawParameters, Consumer<JsonNode> jobSubmitted) throws Exception {
|
||||
// Support Lambda resources: direct ARN or optimized integration
|
||||
String functionName = null;
|
||||
String functionRef = null;
|
||||
@@ -1226,7 +1232,8 @@ public class AslExecutor {
|
||||
? ".waitForTaskToken"
|
||||
: integration.suffix();
|
||||
String region = extractRegionFromArn(sm.getStateMachineArn());
|
||||
return invokeEcsRunTask(mode, input, region, executionArn, executionDeadlineNanos, taskDeadlineNanos);
|
||||
return invokeEcsRunTask(mode, input, region, executionArn, executionDeadlineNanos, taskDeadlineNanos,
|
||||
jobSubmitted);
|
||||
}
|
||||
|
||||
// AWS SDK service integrations: Step Functions
|
||||
@@ -1252,7 +1259,7 @@ public class AslExecutor {
|
||||
String mode = integration.suffix();
|
||||
String region = extractRegionFromArn(sm.getStateMachineArn());
|
||||
return invokeNestedStateMachine(mode, input, region, executionArn, executionDeadlineNanos,
|
||||
taskDeadlineNanos, rawParameters);
|
||||
taskDeadlineNanos, rawParameters, jobSubmitted);
|
||||
}
|
||||
|
||||
throw new FailStateException("States.TaskFailed", "Unsupported resource: " + resource);
|
||||
@@ -1671,7 +1678,8 @@ public class AslExecutor {
|
||||
|
||||
private JsonNode invokeNestedStateMachine(String mode, JsonNode input, String region, String executionArn,
|
||||
long executionDeadlineNanos, long taskDeadlineNanos,
|
||||
JsonNode rawParameters) throws Exception {
|
||||
JsonNode rawParameters, Consumer<JsonNode> jobSubmitted)
|
||||
throws Exception {
|
||||
String smArn = input.path("StateMachineArn").asText(null);
|
||||
if (smArn == null || smArn.isBlank()) {
|
||||
throw new FailStateException("States.TaskFailed",
|
||||
@@ -1717,6 +1725,20 @@ public class AslExecutor {
|
||||
return result;
|
||||
}
|
||||
|
||||
// The job is submitted: AWS records the StartExecution response here, before the wait
|
||||
// (measured: PascalCase, StartDate in epoch milliseconds).
|
||||
ObjectNode startResponse = objectMapper.createObjectNode();
|
||||
startResponse.put("ExecutionArn", execArn);
|
||||
startResponse.put("StartDate", Math.round(exec.getStartDate() * 1000));
|
||||
try {
|
||||
jobSubmitted.accept(startResponse);
|
||||
} catch (RuntimeException e) {
|
||||
// The event could not be recorded (the history-event limit): the child has started and
|
||||
// the wait that would abort it is never entered, so it is aborted here.
|
||||
abortChildExecution(execArn, executionArn);
|
||||
throw e;
|
||||
}
|
||||
|
||||
// .sync or .sync:2 polls until terminal. Whatever else ends the wait aborts the child, the way
|
||||
// AWS does (measured: child ABORTED, no error, the cause below): the Task's own TimeoutSeconds,
|
||||
// which fails the state with States.Timeout; the parent execution's budget, which ends it
|
||||
@@ -1825,7 +1847,8 @@ public class AslExecutor {
|
||||
* (both ".sync" and ".waitForTaskToken" fail the state on a placement failure).
|
||||
*/
|
||||
private JsonNode invokeEcsRunTask(String mode, JsonNode input, String region, String executionArn,
|
||||
long executionDeadlineNanos, long taskDeadlineNanos) throws Exception {
|
||||
long executionDeadlineNanos, long taskDeadlineNanos,
|
||||
Consumer<JsonNode> jobSubmitted) throws Exception {
|
||||
String taskDefinition = input.path("TaskDefinition").asText(null);
|
||||
if (taskDefinition == null || taskDefinition.isBlank()) {
|
||||
throw new FailStateException("States.TaskFailed",
|
||||
@@ -1901,6 +1924,22 @@ public class AslExecutor {
|
||||
// clock, a failure in a sibling Parallel branch, which interrupts this one, and a
|
||||
// StopExecution on the execution, which otherwise would leave this worker polling.
|
||||
List<String> taskArns = launched.stream().map(EcsTask::getTaskArn).toList();
|
||||
// The job is submitted: AWS records the RunTask response here, before the wait (measured:
|
||||
// Failures then Tasks, PascalCase).
|
||||
ObjectNode runTaskResponse = objectMapper.createObjectNode();
|
||||
runTaskResponse.putArray("Failures");
|
||||
ArrayNode submittedTasks = runTaskResponse.putArray("Tasks");
|
||||
for (EcsTask t : launched) {
|
||||
submittedTasks.add(recaseKeys(objectMapper, ecsJsonHandler.taskNode(t), true));
|
||||
}
|
||||
try {
|
||||
jobSubmitted.accept(runTaskResponse);
|
||||
} catch (RuntimeException e) {
|
||||
// Same as the nested case: the tasks are running and the wait that would stop them is
|
||||
// never entered, so they are stopped here.
|
||||
stopEcsTasks(cluster, taskArns, executionArn, region);
|
||||
throw e;
|
||||
}
|
||||
while (true) {
|
||||
try {
|
||||
sleepOrTimeOutTask(TimeUnit.MILLISECONDS.toNanos(SYNC_POLL_INTERVAL_MS),
|
||||
@@ -5391,6 +5430,14 @@ public class AslExecutor {
|
||||
if (resource.contains(":lambda:") && resource.contains(":function:")) {
|
||||
return new TaskEventProfile("LambdaFunction", null, resource);
|
||||
}
|
||||
// A well-formed integration id splits at its service: AWS reports states / startExecution.sync:2,
|
||||
// and a split at the last colon would report startExecution.sync / 2 for it.
|
||||
StatesIntegration integration = StatesIntegration.parse(resource).orElse(null);
|
||||
if (integration != null) {
|
||||
return new TaskEventProfile("Task",
|
||||
(integration.sdk() ? "aws-sdk:" : "") + integration.service(),
|
||||
integration.api() + integration.suffix());
|
||||
}
|
||||
String tail = StatesIntegration.tail(resource).orElse(null);
|
||||
if (tail != null) {
|
||||
int idx = tail.lastIndexOf(':');
|
||||
@@ -5434,6 +5481,20 @@ public class AslExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recorded for a {@code .sync} Task once the call that started its job has returned, carrying
|
||||
* that response as {@code output}. AWS also folds the SDK's {@code SdkHttpMetadata} and
|
||||
* {@code SdkResponseMetadata} into it; Floci records the response fields alone.
|
||||
*/
|
||||
private void addTaskSubmittedEvent(HistoryChain chain, TaskEventProfile profile, JsonNode jobResponse) {
|
||||
LinkedHashMap<String, Object> details = new LinkedHashMap<>();
|
||||
details.put("resourceType", profile.resourceType());
|
||||
details.put("resource", profile.resource());
|
||||
details.put("output", jobResponse.toString());
|
||||
details.put("outputDetails", Map.of("truncated", false));
|
||||
chain.publish("TaskSubmitted", details);
|
||||
}
|
||||
|
||||
private void addTaskSucceededEvent(HistoryChain chain, TaskEventProfile profile, JsonNode taskResult) {
|
||||
var output = taskResult.toString();
|
||||
if ("Task".equals(profile.prefix())) {
|
||||
|
||||
Reference in new issue
Block a user