mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): end the invocation when a checkpoint closes a durable execution (#5299)
* fix(lambda): end the invocation at the checkpoint that closes a durable execution On AWS a checkpoint that closes the execution also ends the invocation. The history shows InvocationCompleted before ExecutionSucceeded or ExecutionFailed. A chained invoke started in that batch comes between them. What the handler returns after that checkpoint is ignored. The invocation now runs under a request id that Floci picks, so InvocationCompleted names the same request the function saw. * fix(lambda): answer a closing durable checkpoint with no operations AWS answers the checkpoint that closes the execution with an empty Operations list and no token, even when the batch changed other operations. * fix(lambda): fail an unreachable chained invoke before the invocation ends When a chained invoke target cannot be invoked, AWS logs ChainedInvokeStarted at its place in the batch and ChainedInvokeFailed after the batch, before InvocationCompleted. This also holds in a batch that closes the execution, where Floci logged no ChainedInvokeFailed. A target that can be invoked still starts after the batch, and after InvocationCompleted when the batch closes the execution.
This commit is contained in:
1 parent
6fc957a3b4
commit
ebd364e23e
10 files changed
+155
-66
No files matched your search
+12
-10
@@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@@ -20,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
class DurableCheckpointApplierTest {
|
||||
|
||||
private static final long NOW = 1_700_000_000_000L;
|
||||
private static final Function<String, AwsException> REACHABLE = functionName -> null;
|
||||
private static final String EXECUTION_ARN =
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:fn:1/durable-execution/exec/exec-id";
|
||||
|
||||
@@ -56,7 +58,7 @@ class DurableCheckpointApplierTest {
|
||||
"Cannot update the same operation twice in a single request.");
|
||||
DurableExecution execution = execution();
|
||||
DurableCheckpointApplier.apply(execution, List.of(stepStart("s1", null), update("s1", null,
|
||||
DurableOperationType.STEP, DurableOperationAction.SUCCEED, "1")), NOW);
|
||||
DurableOperationType.STEP, DurableOperationAction.SUCCEED, "1")), NOW, REACHABLE);
|
||||
assertEquals(DurableOperationStatus.SUCCEEDED, execution.getOperations().get("s1").getStatus());
|
||||
}
|
||||
|
||||
@@ -64,7 +66,7 @@ class DurableCheckpointApplierTest {
|
||||
void parentMustBeAContextAndMetadataMustStayConsistent() {
|
||||
assertRejected(List.of(stepStart("s1", "missing")), "Invalid parent operation id.");
|
||||
DurableExecution execution = execution();
|
||||
DurableCheckpointApplier.apply(execution, List.of(stepStart("s1", null)), NOW);
|
||||
DurableCheckpointApplier.apply(execution, List.of(stepStart("s1", null)), NOW, REACHABLE);
|
||||
assertEquals("Inconsistent operation type.", reject(execution, List.of(waitStart("s1"))));
|
||||
assertEquals("Inconsistent parent operation id.", reject(execution, List.of(stepStart("ctx", null),
|
||||
update("s1", "ctx", DurableOperationType.STEP, DurableOperationAction.SUCCEED, "1"))));
|
||||
@@ -73,7 +75,7 @@ class DurableCheckpointApplierTest {
|
||||
@Test
|
||||
void stepTransitionsAreGuarded() {
|
||||
DurableExecution execution = execution();
|
||||
DurableCheckpointApplier.apply(execution, List.of(stepStart("s1", null)), NOW);
|
||||
DurableCheckpointApplier.apply(execution, List.of(stepStart("s1", null)), NOW, REACHABLE);
|
||||
assertEquals("Invalid current STEP state to start.", reject(execution, List.of(stepStart("s1", null))));
|
||||
assertEquals("Cannot provide an Error for SUCCEED action.", reject(execution, List.of(
|
||||
new DurableOperationUpdate("s1", null, null, DurableOperationType.STEP, null,
|
||||
@@ -81,7 +83,7 @@ class DurableCheckpointApplierTest {
|
||||
assertEquals("Invalid StepOptions for the given action.", reject(execution, List.of(
|
||||
update("s1", null, DurableOperationType.STEP, DurableOperationAction.RETRY, null))));
|
||||
DurableCheckpointApplier.apply(execution, List.of(update("s1", null, DurableOperationType.STEP,
|
||||
DurableOperationAction.FAIL, null)), NOW);
|
||||
DurableOperationAction.FAIL, null)), NOW, REACHABLE);
|
||||
assertEquals("Invalid current STEP state to close.", reject(execution, List.of(
|
||||
update("s1", null, DurableOperationType.STEP, DurableOperationAction.SUCCEED, "1"))));
|
||||
}
|
||||
@@ -93,10 +95,10 @@ class DurableCheckpointApplierTest {
|
||||
assertRejected(List.of(update("w1", null, DurableOperationType.WAIT, DurableOperationAction.CANCEL, null)),
|
||||
"Cannot cancel a WAIT that does not exist or has already completed.");
|
||||
DurableExecution execution = execution();
|
||||
DurableCheckpointApplier.apply(execution, List.of(waitStart("w1")), NOW);
|
||||
DurableCheckpointApplier.apply(execution, List.of(waitStart("w1")), NOW, REACHABLE);
|
||||
assertEquals("Cannot start a WAIT that already exist.", reject(execution, List.of(waitStart("w1"))));
|
||||
DurableCheckpointApplier.apply(execution, List.of(update("w1", null, DurableOperationType.WAIT,
|
||||
DurableOperationAction.CANCEL, null)), NOW);
|
||||
DurableOperationAction.CANCEL, null)), NOW, REACHABLE);
|
||||
assertEquals(DurableOperationStatus.CANCELLED, execution.getOperations().get("w1").getStatus());
|
||||
}
|
||||
|
||||
@@ -131,7 +133,7 @@ class DurableCheckpointApplierTest {
|
||||
DurableExecution execution = execution();
|
||||
DurableCheckpointApplier.Outcome outcome = DurableCheckpointApplier.apply(execution,
|
||||
List.of(chainedStart("i1", "arn:aws:lambda:us-east-1:000000000000:function:target-fn",
|
||||
"\"" + "x".repeat(300 * 1024) + "\"")), NOW);
|
||||
"\"" + "x".repeat(300 * 1024) + "\"")), NOW, REACHABLE);
|
||||
assertEquals(List.of("i1"), outcome.chainedInvokes(), "the service runs what the batch started");
|
||||
DurableOperation invoke = execution.getOperations().get("i1");
|
||||
assertEquals(DurableOperationStatus.STARTED, invoke.getStatus());
|
||||
@@ -143,7 +145,7 @@ class DurableCheckpointApplierTest {
|
||||
@Test
|
||||
void aCallbackStartsWithAnIdThatNamesItsExecution() {
|
||||
DurableExecution execution = execution();
|
||||
DurableCheckpointApplier.apply(execution, List.of(callbackStart("c1", 30, 10)), NOW);
|
||||
DurableCheckpointApplier.apply(execution, List.of(callbackStart("c1", 30, 10)), NOW, REACHABLE);
|
||||
|
||||
DurableOperation callback = execution.getOperations().get("c1");
|
||||
assertEquals(DurableOperationStatus.STARTED, callback.getStatus());
|
||||
@@ -162,7 +164,7 @@ class DurableCheckpointApplierTest {
|
||||
void aCallbackTimesOutOnTheDeadlineItMissesFirst() {
|
||||
DurableExecution execution = execution();
|
||||
DurableCheckpointApplier.apply(execution, List.of(callbackStart("slow", 5, 0), callbackStart("silent", 60, 3)),
|
||||
NOW);
|
||||
NOW, REACHABLE);
|
||||
|
||||
DurableCheckpointApplier.fireDueTimers(execution, NOW + 3_000);
|
||||
DurableOperation silent = execution.getOperations().get("silent");
|
||||
@@ -197,7 +199,7 @@ class DurableCheckpointApplierTest {
|
||||
|
||||
private static String reject(DurableExecution execution, List<DurableOperationUpdate> updates) {
|
||||
AwsException rejected = assertThrows(AwsException.class,
|
||||
() -> DurableCheckpointApplier.apply(execution, updates, NOW));
|
||||
() -> DurableCheckpointApplier.apply(execution, updates, NOW, REACHABLE));
|
||||
assertEquals(400, rejected.getHttpStatus());
|
||||
assertEquals("InvalidParameterValueException", rejected.getErrorCode());
|
||||
return rejected.getMessage();
|
||||
|
||||
+1
-1
@@ -373,7 +373,7 @@ class DurableExecutionServicePersistenceTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DurableInvocationResult invoke(ResolvedDurableTarget target, byte[] payload) {
|
||||
public DurableInvocationResult invoke(ResolvedDurableTarget target, byte[] payload, String requestId) {
|
||||
if (!target.durable()) {
|
||||
plainPayloads.add(new String(payload, StandardCharsets.UTF_8));
|
||||
return new DurableInvocationResult("req", plainOutput.getBytes(StandardCharsets.UTF_8), null);
|
||||
|
||||
+41
-3
@@ -207,6 +207,26 @@ class DurableExecutionServiceTest {
|
||||
assertEquals("\"r\"", execution.getResult(), "the checkpointed result wins over the handler return");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClosingCheckpointEndsTheInvocationAndTheHandlerReturnIsIgnored() {
|
||||
List<CheckpointResult> closing = new ArrayList<>();
|
||||
invoker.script(event -> {
|
||||
closing.add(checkpoint(event, token(event), List.of(step("s1", DurableOperationAction.START, null, null),
|
||||
executionSucceed("\"closed\""))));
|
||||
return functionError("{\"errorMessage\":\"after close\"}");
|
||||
});
|
||||
|
||||
DurableExecution execution = service.get(start("exec-1", "{}", false).getExecutionArn());
|
||||
|
||||
assertEquals(DurableExecutionStatus.SUCCEEDED, execution.getStatus());
|
||||
assertEquals("\"closed\"", execution.getResult());
|
||||
assertEquals(List.of("ExecutionStarted", "StepStarted", "InvocationCompleted", "ExecutionSucceeded"),
|
||||
eventTypes(execution));
|
||||
Map<?, ?> completed = (Map<?, ?>) execution.getHistory().get(2).getDetails().get("InvocationCompletedDetails");
|
||||
assertEquals(invoker.requestIds.get(0), completed.get("RequestId"));
|
||||
assertTrue(closing.get(0).newExecutionState().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRetriedCheckpointWithTheSameClientTokenGetsTheSameAnswer() {
|
||||
invoker.script(event -> {
|
||||
@@ -794,11 +814,27 @@ class DurableExecutionServiceTest {
|
||||
|
||||
assertEquals(List.of("{\"n\":1}"), received);
|
||||
assertEquals("\"closed\"", execution.getResult());
|
||||
assertTrue(eventTypes(execution).contains("ChainedInvokeStarted"));
|
||||
assertFalse(eventTypes(execution).contains("ChainedInvokeSucceeded"));
|
||||
assertEquals(List.of("ExecutionStarted", "InvocationCompleted", "ChainedInvokeStarted", "ExecutionSucceeded"),
|
||||
eventTypes(execution));
|
||||
assertEquals(1, invoker.events.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTargetThatCannotBeInvokedStartsInBatchOrderAndFailsBeforeTheInvocationEnds() {
|
||||
invoker.script(event -> {
|
||||
checkpoint(event, token(event), List.of(step("s1", DurableOperationAction.START, null, null),
|
||||
chainedStart("i1", "missing-fn", null), step("s2", DurableOperationAction.START, null, null),
|
||||
executionSucceed("\"closed\"")));
|
||||
return succeeded("");
|
||||
});
|
||||
|
||||
DurableExecution execution = service.get(start("exec-1", "{}", false).getExecutionArn());
|
||||
|
||||
assertEquals(List.of("ExecutionStarted", "StepStarted", "ChainedInvokeStarted", "StepStarted",
|
||||
"ChainedInvokeFailed", "InvocationCompleted", "ExecutionSucceeded"), eventTypes(execution));
|
||||
assertEquals(DurableOperationStatus.FAILED, execution.getOperations().get("i1").getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stoppingTheParentLeavesADurableChildRunning() {
|
||||
invoker.childScript("child-fn", event -> {
|
||||
@@ -920,6 +956,7 @@ class DurableExecutionServiceTest {
|
||||
|
||||
final Deque<Function<JsonNode, DurableInvocationResult>> scripts = new ArrayDeque<>();
|
||||
final List<JsonNode> events = new ArrayList<>();
|
||||
final List<String> requestIds = new ArrayList<>();
|
||||
/** Durable functions other than the one under test, each with its own scripts. */
|
||||
final Map<String, Deque<Function<JsonNode, DurableInvocationResult>>> childScripts = new HashMap<>();
|
||||
/** Plain functions a chained invoke may call, answering the raw payload. */
|
||||
@@ -956,7 +993,7 @@ class DurableExecutionServiceTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DurableInvocationResult invoke(ResolvedDurableTarget target, byte[] payload) {
|
||||
public DurableInvocationResult invoke(ResolvedDurableTarget target, byte[] payload, String requestId) {
|
||||
JsonNode event;
|
||||
try {
|
||||
event = MAPPER.readTree(payload);
|
||||
@@ -972,6 +1009,7 @@ class DurableExecutionServiceTest {
|
||||
return script != null ? script.apply(event) : pending();
|
||||
}
|
||||
events.add(event);
|
||||
requestIds.add(requestId);
|
||||
Function<JsonNode, DurableInvocationResult> script = scripts.poll();
|
||||
if (script == null) {
|
||||
return handlerResponse("{\"Status\":\"FAILED\",\"Error\":{\"ErrorMessage\":\"no script for "
|
||||
|
||||
+1
-1
@@ -39,7 +39,7 @@ public class ScriptedDurableFunctionInvoker implements DurableFunctionInvoker {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DurableInvocationResult invoke(ResolvedDurableTarget target, byte[] payload) {
|
||||
public DurableInvocationResult invoke(ResolvedDurableTarget target, byte[] payload, String requestId) {
|
||||
Invocation invocation = new Invocation(target, payload, new CompletableFuture<>());
|
||||
invocations.add(invocation);
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user