mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
perf(stepfunctions): wake nested waits on execution status changes (#6513)
This commit is contained in:
1 parent
985a2ea949
commit
fe928d67ab
8 files changed
+634
-67
No files matched your search
@@ -62,6 +62,7 @@ import io.github.hectorvent.floci.services.sqs.SqsJsonHandler;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService.TokenTimeout;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService.TokenWait;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.ExecutionStatusSubscription;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.HistoryEvent;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.MapRun;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.MockedResponseStep;
|
||||
@@ -278,12 +279,15 @@ public class AslExecutor {
|
||||
private final CustomResourceLiveness customResourceLiveness;
|
||||
private final Clock clock;
|
||||
private final Sleeper sleeper;
|
||||
private final NestedWaiter nestedWaiter;
|
||||
// Null in production, where the ceiling comes from the Step Functions config. Tests pin it.
|
||||
private final Integer maxWaitSecondsOverride;
|
||||
private final Duration expressMaxExecutionTime;
|
||||
// The monotonic clock every execution and task deadline is measured on. Tests move it forward.
|
||||
private final LongSupplier nanoTime;
|
||||
private final Map<String, ActiveMockExecution> activeMocks = new ConcurrentHashMap<>();
|
||||
private final Set<ExecutionStatusSubscription> nestedWaits = new HashSet<>();
|
||||
private long nestedWaitGeneration;
|
||||
private final ExecutorService executor = Executors.newCachedThreadPool(r -> {
|
||||
Thread t = new Thread(r, "sfn-executor");
|
||||
t.setDaemon(true);
|
||||
@@ -312,7 +316,7 @@ public class AslExecutor {
|
||||
objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness,
|
||||
Clock.systemUTC(), TimeUnit.NANOSECONDS::sleep, null, redshiftDataHandler, serviceRegistry,
|
||||
nestedTaskCredentials, lambdaTaskAuthorizer, null, null, apiGatewayTaskInvoker, emrTaskInvoker,
|
||||
emrServerlessTaskInvoker);
|
||||
emrServerlessTaskInvoker, null);
|
||||
}
|
||||
|
||||
AslExecutor(LambdaExecutorService lambdaExecutor, LambdaTargetResolver targetResolver,
|
||||
@@ -331,7 +335,7 @@ public class AslExecutor {
|
||||
NestedTaskCredentials nestedTaskCredentials, LambdaTaskAuthorizer lambdaTaskAuthorizer,
|
||||
Duration expressMaxExecutionTimeOverride, LongSupplier nanoTimeOverride,
|
||||
ApiGatewayTaskInvoker apiGatewayTaskInvoker, EmrTaskInvoker emrTaskInvoker,
|
||||
EmrServerlessTaskInvoker emrServerlessTaskInvoker) {
|
||||
EmrServerlessTaskInvoker emrServerlessTaskInvoker, NestedWaiter nestedWaiter) {
|
||||
this.apiGatewayTaskInvoker = apiGatewayTaskInvoker;
|
||||
this.emrTaskInvoker = emrTaskInvoker;
|
||||
this.emrServerlessTaskInvoker = emrServerlessTaskInvoker;
|
||||
@@ -367,6 +371,7 @@ public class AslExecutor {
|
||||
this.config = config;
|
||||
this.clock = clock;
|
||||
this.sleeper = sleeper;
|
||||
this.nestedWaiter = nestedWaiter != null ? nestedWaiter : ExecutionStatusSubscription::awaitChange;
|
||||
this.maxWaitSecondsOverride = maxWaitSecondsOverride;
|
||||
this.nanoTime = nanoTimeOverride != null ? nanoTimeOverride : System::nanoTime;
|
||||
this.expressMaxExecutionTime = expressMaxExecutionTimeOverride != null
|
||||
@@ -398,7 +403,7 @@ public class AslExecutor {
|
||||
eventBridgeHandler, schedulerService, schedulerController, rdsDataService,
|
||||
objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness,
|
||||
Clock.systemUTC(), TimeUnit.NANOSECONDS::sleep, null, null, null, null, null, null, null, null, null,
|
||||
null);
|
||||
null, null);
|
||||
}
|
||||
|
||||
// Kept for tests that do not exercise the Redshift Data API integration.
|
||||
@@ -419,7 +424,7 @@ public class AslExecutor {
|
||||
ec2Service, s3Service, ecsService, ecsJsonHandler,
|
||||
eventBridgeHandler, schedulerService, schedulerController, rdsDataService,
|
||||
objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness,
|
||||
clock, sleeper, maxWaitSecondsOverride, null, null, null, null, null, null, null, null, null);
|
||||
clock, sleeper, maxWaitSecondsOverride, null, null, null, null, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
AslExecutor(LambdaExecutorService lambdaExecutor, LambdaFunctionStore functionStore,
|
||||
@@ -441,6 +446,13 @@ public class AslExecutor {
|
||||
vertx, customResourceLiveness, clock, sleeper, maxWaitSecondsOverride);
|
||||
}
|
||||
|
||||
/** Test seam paired with the monotonic clock for nested deadline and cut coordination. */
|
||||
@FunctionalInterface
|
||||
interface NestedWaiter {
|
||||
void awaitChange(ExecutionStatusSubscription subscription, long observed, long timeoutNanos)
|
||||
throws InterruptedException;
|
||||
}
|
||||
|
||||
/** Test seam: lets Wait states be exercised without real time passing. */
|
||||
@FunctionalInterface
|
||||
interface Sleeper {
|
||||
@@ -1925,6 +1937,7 @@ public class AslExecutor {
|
||||
long executionDeadlineNanos, long taskDeadlineNanos,
|
||||
JsonNode rawParameters, Consumer<JsonNode> jobSubmitted)
|
||||
throws Exception {
|
||||
long waitGeneration = nestedWaitGeneration();
|
||||
String smArn = input.path("StateMachineArn").asText(null);
|
||||
if (smArn == null || smArn.isBlank()) {
|
||||
throw new FailStateException("States.TaskFailed",
|
||||
@@ -1951,7 +1964,7 @@ public class AslExecutor {
|
||||
childName = null;
|
||||
}
|
||||
|
||||
io.github.hectorvent.floci.services.stepfunctions.model.Execution exec;
|
||||
Execution exec;
|
||||
try {
|
||||
exec = sfnService.get().startExecution(smArn, childName, childInput, region);
|
||||
} catch (AwsException e) {
|
||||
@@ -1981,44 +1994,98 @@ public class AslExecutor {
|
||||
throw e;
|
||||
}
|
||||
|
||||
// .sync or .sync:2 polls until terminal. Whatever else ends the wait aborts the child, the way
|
||||
// .sync or .sync:2 waits for terminal publication. 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
|
||||
// TIMED_OUT; a failure in a sibling Parallel branch, which interrupts this one; and a
|
||||
// StopExecution on the parent, so a stopped execution does not keep a polling worker.
|
||||
while (true) {
|
||||
try {
|
||||
sleepOrTimeOutTask(TimeUnit.MILLISECONDS.toNanos(SYNC_POLL_INTERVAL_MS),
|
||||
executionDeadlineNanos, taskDeadlineNanos);
|
||||
} catch (TaskTimedOutException | ExecutionTimedOutException | InterruptedException e) {
|
||||
abortChildExecution(execArn, executionArn);
|
||||
throw e;
|
||||
// StopExecution on the parent. Register before reading either execution to cover
|
||||
// completion during registration and between a state read and parking.
|
||||
ExecutionStatusSubscription waiting = new ExecutionStatusSubscription(exec, nestedWaitParent(executionArn));
|
||||
registerNestedWait(waiting, waitGeneration);
|
||||
try (waiting) {
|
||||
while (true) {
|
||||
if (waiting.cancelled()) {
|
||||
abortChildExecution(execArn, executionArn);
|
||||
throw new FailStateException("States.TaskFailed", "StepFunctionsService cleared");
|
||||
}
|
||||
long observed = waiting.version();
|
||||
checkNestedTaskDeadline(executionDeadlineNanos, taskDeadlineNanos);
|
||||
if (abortedByCaller(executionArn)) {
|
||||
abortChildExecution(execArn, executionArn);
|
||||
throw new ExecutionAbortedException();
|
||||
}
|
||||
Execution current = sfnService.get().describeExecution(execArn);
|
||||
String status;
|
||||
// Terminal fields are written under this monitor, status last.
|
||||
synchronized (current) {
|
||||
status = current.getStatus();
|
||||
}
|
||||
if ("SUCCEEDED".equals(status)) {
|
||||
return nestedExecutionEnvelope(current, true, ".sync:2".equals(mode));
|
||||
}
|
||||
if (!"RUNNING".equals(status)) {
|
||||
throw new FailStateException("States.TaskFailed",
|
||||
nestedExecutionEnvelope(current, false, false).toString());
|
||||
}
|
||||
nestedWaiter.awaitChange(waiting, observed,
|
||||
Math.min(executionDeadlineNanos, taskDeadlineNanos) - nanoTime.getAsLong());
|
||||
}
|
||||
if (abortedByCaller(executionArn)) {
|
||||
abortChildExecution(execArn, executionArn);
|
||||
throw new ExecutionAbortedException();
|
||||
} catch (TaskTimedOutException | ExecutionTimedOutException | InterruptedException e) {
|
||||
abortChildExecution(execArn, executionArn);
|
||||
throw e;
|
||||
} finally {
|
||||
synchronized (nestedWaits) {
|
||||
nestedWaits.remove(waiting);
|
||||
}
|
||||
io.github.hectorvent.floci.services.stepfunctions.model.Execution current =
|
||||
sfnService.get().describeExecution(execArn);
|
||||
String status;
|
||||
// The child's worker and StopExecution write its terminal fields under this monitor,
|
||||
// status last. Reading the status under it makes everything written before it visible,
|
||||
// so a terminal status is never seen without the error, cause and stop date behind it.
|
||||
synchronized (current) {
|
||||
status = current.getStatus();
|
||||
}
|
||||
}
|
||||
|
||||
private long nestedWaitGeneration() {
|
||||
synchronized (nestedWaits) {
|
||||
return nestedWaitGeneration;
|
||||
}
|
||||
}
|
||||
|
||||
private void registerNestedWait(ExecutionStatusSubscription waiting, long generation) {
|
||||
synchronized (nestedWaits) {
|
||||
if (generation != nestedWaitGeneration) {
|
||||
waiting.cancel();
|
||||
} else {
|
||||
nestedWaits.add(waiting);
|
||||
}
|
||||
if ("RUNNING".equals(status)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
void clearNestedExecutionWaits() {
|
||||
synchronized (nestedWaits) {
|
||||
nestedWaitGeneration++;
|
||||
for (ExecutionStatusSubscription waiting : nestedWaits) {
|
||||
waiting.cancel();
|
||||
}
|
||||
if ("SUCCEEDED".equals(status)) {
|
||||
// Both modes return the envelope (measured); .sync:2 differs only in carrying
|
||||
// Input and Output as JSON values where .sync carries them as JSON strings.
|
||||
return nestedExecutionEnvelope(current, true, ".sync:2".equals(mode));
|
||||
}
|
||||
// However the child ended, FAILED, TIMED_OUT or ABORTED, and whatever its own error, the
|
||||
// parent sees States.TaskFailed (measured), so a Catch on the child's error never fires.
|
||||
throw new FailStateException("States.TaskFailed",
|
||||
nestedExecutionEnvelope(current, false, false).toString());
|
||||
nestedWaits.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private Execution nestedWaitParent(String executionArn) {
|
||||
if (executionArn == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return RequestScopes.callAs(AwsArnUtils.parse(executionArn).accountId(),
|
||||
() -> sfnService.get().describeExecution(executionArn));
|
||||
} catch (AwsException e) {
|
||||
LOG.warnv("Parent execution {0} vanished before its nested wait: {1}", executionArn, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void checkNestedTaskDeadline(long executionDeadlineNanos, long taskDeadlineNanos) {
|
||||
long now = nanoTime.getAsLong();
|
||||
if (now >= executionDeadlineNanos) {
|
||||
throw new ExecutionTimedOutException();
|
||||
}
|
||||
if (now >= taskDeadlineNanos) {
|
||||
throw new TaskTimedOutException("States.Timeout");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
@@ -176,6 +176,9 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
if (aslExecutor != null) {
|
||||
aslExecutor.clearNestedExecutionWaits();
|
||||
}
|
||||
historyCache.clear();
|
||||
activityQueues.clear();
|
||||
Map<String, PendingTask> pending;
|
||||
|
||||
+35
-1
@@ -1,10 +1,13 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@@ -22,6 +25,8 @@ public class Execution {
|
||||
private String error;
|
||||
private String cause;
|
||||
private List<HistoryEvent> history = new ArrayList<>();
|
||||
@JsonIgnore
|
||||
private transient Set<ExecutionStatusSubscription> statusSubscriptions;
|
||||
|
||||
public Execution() {
|
||||
this.startDate = System.currentTimeMillis() / 1000.0;
|
||||
@@ -47,7 +52,36 @@ public class Execution {
|
||||
public void setName(String name) { this.name = name; }
|
||||
|
||||
public String getStatus() { return status; }
|
||||
public void setStatus(String status) { this.status = status; }
|
||||
public synchronized void setStatus(String status) {
|
||||
this.status = status;
|
||||
if (!"RUNNING".equals(status) && statusSubscriptions != null) {
|
||||
Set<ExecutionStatusSubscription> subscriptions = statusSubscriptions;
|
||||
statusSubscriptions = null;
|
||||
for (ExecutionStatusSubscription subscription : subscriptions) {
|
||||
subscription.signal();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
synchronized void registerStatusSubscription(ExecutionStatusSubscription subscription) {
|
||||
if (!"RUNNING".equals(status)) {
|
||||
subscription.signal();
|
||||
return;
|
||||
}
|
||||
if (statusSubscriptions == null) {
|
||||
statusSubscriptions = new HashSet<>();
|
||||
}
|
||||
statusSubscriptions.add(subscription);
|
||||
}
|
||||
|
||||
synchronized void unregisterStatusSubscription(ExecutionStatusSubscription subscription) {
|
||||
if (statusSubscriptions != null) {
|
||||
statusSubscriptions.remove(subscription);
|
||||
if (statusSubscriptions.isEmpty()) {
|
||||
statusSubscriptions = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getInput() { return input; }
|
||||
public void setInput(String input) { this.input = input; }
|
||||
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions.model;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* A scoped wakeup for a child execution ending or its parent being stopped.
|
||||
* Read the version before authoritative state so completion cannot be lost
|
||||
* between that read and parking. Closing removes both registrations.
|
||||
*/
|
||||
public final class ExecutionStatusSubscription implements AutoCloseable {
|
||||
private final Execution child;
|
||||
private final Execution parent;
|
||||
private long version;
|
||||
private boolean closed;
|
||||
private boolean cancelled;
|
||||
|
||||
public ExecutionStatusSubscription(Execution child, Execution parent) {
|
||||
this.child = Objects.requireNonNull(child);
|
||||
this.parent = parent == child ? null : parent;
|
||||
child.registerStatusSubscription(this);
|
||||
if (this.parent != null) {
|
||||
this.parent.registerStatusSubscription(this);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized long version() {
|
||||
return version;
|
||||
}
|
||||
|
||||
synchronized void signal() {
|
||||
version++;
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
public synchronized void cancel() {
|
||||
cancelled = true;
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
public synchronized boolean cancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
public synchronized boolean awaitChange(long observed, long timeoutNanos) throws InterruptedException {
|
||||
// The executor owns the deadline and its clock. Recheck there after every wake,
|
||||
// including a spurious wake, rather than creating a second real-clock deadline.
|
||||
if (!closed && !cancelled && version == observed && timeoutNanos > 0) {
|
||||
TimeUnit.NANOSECONDS.timedWait(this, timeoutNanos);
|
||||
}
|
||||
return closed || cancelled || version != observed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
synchronized (this) {
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
closed = true;
|
||||
notifyAll();
|
||||
}
|
||||
// setStatus holds an execution monitor while signalling. Do not take
|
||||
// an execution monitor while holding this subscription's monitor.
|
||||
child.unregisterStatusSubscription(this);
|
||||
if (parent != null) {
|
||||
parent.unregisterStatusSubscription(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user