mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(stepfunctions): retain claimed callback outcomes (#6311)
Retain accepted outcomes under the token lock without waiting for callback-thread completion. Retire tokens registered throughout reset, including overlapping resets, and complete futures outside the lock. Cover callback, reset, discard and timeout interleavings. Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
45ada484d7
commit
2babe62ce4
3 files changed
+217
-28
No files matched your search
@@ -60,6 +60,7 @@ import io.github.hectorvent.floci.services.scheduler.model.ScheduleRequest;
|
||||
import io.github.hectorvent.floci.services.sns.SnsJsonHandler;
|
||||
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.HistoryEvent;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.MapRun;
|
||||
@@ -1084,13 +1085,12 @@ public class AslExecutor {
|
||||
try {
|
||||
return future.get(wakeAtNanos - nanoTime.getAsLong(), TimeUnit.NANOSECONDS);
|
||||
} catch (java.util.concurrent.TimeoutException e) {
|
||||
TokenTimeout expired = sfnService.get().expirePendingToken(taskToken, nanoTime.getAsLong(),
|
||||
TokenWait decision = sfnService.get().expirePendingToken(taskToken, nanoTime.getAsLong(),
|
||||
executionDeadlineNanos, timeoutDeadlineNanos, heartbeatSeconds);
|
||||
if (expired == TokenTimeout.CLAIMED) {
|
||||
// Claiming removes the token before completing its future outside the lock.
|
||||
return future.get();
|
||||
if (decision.timeout() == TokenTimeout.CLAIMED) {
|
||||
return decision.result().get();
|
||||
}
|
||||
RuntimeException timeout = switch (expired) {
|
||||
RuntimeException timeout = switch (decision.timeout()) {
|
||||
case EXECUTION -> new ExecutionTimedOutException();
|
||||
case TASK -> new TaskTimedOutException("States.Timeout");
|
||||
case HEARTBEAT -> new TaskTimedOutException("States.HeartbeatTimeout");
|
||||
|
||||
+77
-22
@@ -62,13 +62,33 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
private final Map<String, ExecutionHistory> historyCache = new ConcurrentHashMap<>();
|
||||
private final Map<String, BlockingQueue<ActivityTask>> activityQueues = new ConcurrentHashMap<>();
|
||||
private final Map<String, PendingTask> pendingTaskTokens = new ConcurrentHashMap<>();
|
||||
private int tokenResetsInProgress;
|
||||
// Only recent closures are classified as TaskTimedOut: five minutes, at most 1,024 tokens.
|
||||
// Evicted/older tokens remain InvalidToken. No task payloads are retained.
|
||||
private final Map<String, ClosedTask> closedTaskTokens = new LinkedHashMap<>();
|
||||
private static final long CLOSED_TOKEN_RETENTION_NANOS = TimeUnit.MINUTES.toNanos(5);
|
||||
private static final int MAX_CLOSED_TOKENS = 1024;
|
||||
|
||||
private record PendingTask(String accountId, CompletableFuture<JsonNode> future) {}
|
||||
private record PendingTask(String accountId, CompletableFuture<JsonNode> future, TaskResult result) {}
|
||||
|
||||
record TaskResult(JsonNode output, AslExecutor.FailStateException failure) {
|
||||
JsonNode get() {
|
||||
if (failure != null) {
|
||||
throw failure;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
void complete(CompletableFuture<JsonNode> future) {
|
||||
if (failure != null) {
|
||||
future.completeExceptionally(failure);
|
||||
} else {
|
||||
future.complete(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
record TokenWait(TokenTimeout timeout, TaskResult result) {}
|
||||
private record ClosedTask(String accountId, long closedAtNanos) {}
|
||||
// When each pending token last showed progress, so a Task's HeartbeatSeconds can bound the gap
|
||||
// between heartbeats instead of the whole wait.
|
||||
@@ -157,12 +177,29 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
public void clear() {
|
||||
historyCache.clear();
|
||||
activityQueues.clear();
|
||||
pendingTaskTokens.values().forEach(f -> f.future().completeExceptionally(new RuntimeException("StepFunctionsService cleared")));
|
||||
pendingTaskTokens.clear();
|
||||
Map<String, PendingTask> pending;
|
||||
synchronized (this) {
|
||||
tokenResetsInProgress++;
|
||||
pendingTaskTokens.replaceAll((token, task) -> task.result() != null ? task
|
||||
: new PendingTask(task.accountId(), task.future(), new TaskResult(null,
|
||||
new AslExecutor.FailStateException("States.TaskFailed", "StepFunctionsService cleared"))));
|
||||
pending = new HashMap<>(pendingTaskTokens);
|
||||
closedTaskTokens.clear();
|
||||
taskHeartbeatNanos.clear();
|
||||
}
|
||||
try {
|
||||
for (Map.Entry<String, PendingTask> entry : pending.entrySet()) {
|
||||
PendingTask task = entry.getValue();
|
||||
task.result().complete(task.future());
|
||||
synchronized (this) {
|
||||
pendingTaskTokens.remove(entry.getKey(), task);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
synchronized (this) {
|
||||
tokenResetsInProgress--;
|
||||
}
|
||||
}
|
||||
taskHeartbeatNanos.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1196,10 +1233,17 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
queue.add(new ActivityTask(taskToken, input));
|
||||
}
|
||||
|
||||
public synchronized CompletableFuture<JsonNode> registerPendingToken(String token, String accountId) {
|
||||
public CompletableFuture<JsonNode> registerPendingToken(String token, String accountId) {
|
||||
Objects.requireNonNull(accountId);
|
||||
CompletableFuture<JsonNode> future = new CompletableFuture<>();
|
||||
pendingTaskTokens.put(token, new PendingTask(Objects.requireNonNull(accountId), future));
|
||||
taskHeartbeatNanos.put(token, System.nanoTime());
|
||||
synchronized (this) {
|
||||
if (tokenResetsInProgress == 0) {
|
||||
pendingTaskTokens.put(token, new PendingTask(accountId, future, null));
|
||||
taskHeartbeatNanos.put(token, System.nanoTime());
|
||||
return future;
|
||||
}
|
||||
}
|
||||
future.completeExceptionally(new AslExecutor.FailStateException("States.TaskFailed", "StepFunctionsService cleared"));
|
||||
return future;
|
||||
}
|
||||
|
||||
@@ -1216,10 +1260,14 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
enum TokenTimeout { NONE, CLAIMED, EXECUTION, TASK, HEARTBEAT }
|
||||
|
||||
/** Selects an expired clock and closes its token under the callback and heartbeat lock. */
|
||||
synchronized TokenTimeout expirePendingToken(String taskToken, long now, long executionDeadlineNanos,
|
||||
synchronized TokenWait expirePendingToken(String taskToken, long now, long executionDeadlineNanos,
|
||||
long timeoutDeadlineNanos, int heartbeatSeconds) {
|
||||
if (!pendingTaskTokens.containsKey(taskToken)) {
|
||||
return TokenTimeout.CLAIMED;
|
||||
PendingTask pending = pendingTaskTokens.get(taskToken);
|
||||
if (pending == null) {
|
||||
return new TokenWait(TokenTimeout.TASK, null);
|
||||
}
|
||||
if (pending.result() != null) {
|
||||
return new TokenWait(TokenTimeout.CLAIMED, pending.result());
|
||||
}
|
||||
TokenTimeout expired;
|
||||
if (now >= executionDeadlineNanos) {
|
||||
@@ -1230,10 +1278,10 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
+ TimeUnit.SECONDS.toNanos(heartbeatSeconds)) {
|
||||
expired = TokenTimeout.HEARTBEAT;
|
||||
} else {
|
||||
return TokenTimeout.NONE;
|
||||
return new TokenWait(TokenTimeout.NONE, null);
|
||||
}
|
||||
discardPendingToken(taskToken);
|
||||
return expired;
|
||||
return new TokenWait(expired, null);
|
||||
}
|
||||
|
||||
/** Retires a token when its task stops waiting, including timeout and resource failure. */
|
||||
@@ -1262,6 +1310,9 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
PendingTask pending = token == null ? null : pendingTaskTokens.get(token);
|
||||
if (pending != null) {
|
||||
requireTokenOwner(pending.accountId(), accountId);
|
||||
if (pending.result() != null) {
|
||||
throw new AwsException("TaskTimedOut", "The associated task has already been closed.", 400);
|
||||
}
|
||||
return pending;
|
||||
}
|
||||
ClosedTask closed = token == null ? null : closedTaskTokens.get(token);
|
||||
@@ -1282,12 +1333,14 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized CompletableFuture<JsonNode> claimPendingTask(String token) {
|
||||
PendingTask pending = requirePendingTask(token);
|
||||
pendingTaskTokens.remove(token);
|
||||
taskHeartbeatNanos.remove(token);
|
||||
rememberClosed(token, pending.accountId());
|
||||
return pending.future();
|
||||
CompletableFuture<JsonNode> claimPendingTask(String token, TaskResult result) {
|
||||
synchronized (this) {
|
||||
PendingTask pending = requirePendingTask(token);
|
||||
pendingTaskTokens.put(token, new PendingTask(pending.accountId(), pending.future(), result));
|
||||
taskHeartbeatNanos.remove(token);
|
||||
rememberClosed(token, pending.accountId());
|
||||
return pending.future();
|
||||
}
|
||||
}
|
||||
|
||||
/** Claims an owned callback exactly once; completion runs outside the token lock. */
|
||||
@@ -1304,14 +1357,16 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
if (result == null || result.isMissingNode()) {
|
||||
throw new AwsException("InvalidOutput", "Output is not valid JSON.", 400);
|
||||
}
|
||||
CompletableFuture<JsonNode> future = claimPendingTask(taskToken);
|
||||
future.complete(result);
|
||||
TaskResult completion = new TaskResult(result, null);
|
||||
CompletableFuture<JsonNode> future = claimPendingTask(taskToken, completion);
|
||||
completion.complete(future);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean sendTaskFailure(String taskToken, String cause, String error) {
|
||||
CompletableFuture<JsonNode> future = claimPendingTask(taskToken);
|
||||
future.completeExceptionally(new AslExecutor.FailStateException(error, cause));
|
||||
TaskResult completion = new TaskResult(null, new AslExecutor.FailStateException(error, cause));
|
||||
CompletableFuture<JsonNode> future = claimPendingTask(taskToken, completion);
|
||||
completion.complete(future);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user