mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(lambda): show a durable step's attempt and result by its status (#8264)
* fix(lambda): show a durable step's attempt and result by its status
This commit is contained in:
1 parent
01d61afd8a
commit
ea8d875cad
3 files changed
+129
-4
No files matched your search
@@ -12,6 +12,7 @@ import io.floci.aws.services.lambda.durable.model.DurableExecution;
|
||||
import io.floci.aws.services.lambda.durable.model.DurableHistoryEvent;
|
||||
import io.floci.aws.services.lambda.durable.model.DurableOperation;
|
||||
import io.floci.aws.services.lambda.durable.model.DurableOperationAction;
|
||||
import io.floci.aws.services.lambda.durable.model.DurableOperationStatus;
|
||||
import io.floci.aws.services.lambda.durable.model.DurableOperationType;
|
||||
import io.floci.aws.services.lambda.durable.model.DurableOperationUpdate;
|
||||
|
||||
@@ -107,13 +108,16 @@ public final class DurableWire {
|
||||
}
|
||||
case STEP -> {
|
||||
ObjectNode details = node.putObject("StepDetails");
|
||||
if (operation.getAttempt() != null) {
|
||||
details.put("Attempt", operation.getAttempt());
|
||||
}
|
||||
details.put("Attempt", operation.getAttempt() == null ? 0 : operation.getAttempt());
|
||||
if (operation.getNextAttemptTimestamp() != null) {
|
||||
putTimestamp(details, "NextAttemptTimestamp", operation.getNextAttemptTimestamp(), millis);
|
||||
}
|
||||
putResultAndError(details, operation);
|
||||
// AWS shows a step's Result or Error only once it is READY or closed.
|
||||
DurableOperationStatus status = operation.getStatus();
|
||||
if (status == DurableOperationStatus.READY || status == DurableOperationStatus.SUCCEEDED
|
||||
|| status == DurableOperationStatus.FAILED) {
|
||||
putResultAndError(details, operation);
|
||||
}
|
||||
}
|
||||
case WAIT -> {
|
||||
ObjectNode details = node.putObject("WaitDetails");
|
||||
|
||||
@@ -216,6 +216,72 @@ class DurableExecutionServiceTest {
|
||||
assertTrue(eventTypes(execution).contains("StepFailed"), "a RETRY with an error records StepFailed");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stepDetailsFollowTheStatusThroughARetry() {
|
||||
invoker.script(event -> {
|
||||
String arn = event.get("DurableExecutionArn").asText();
|
||||
CheckpointResult started = checkpoint(event, token(event),
|
||||
List.of(step("s1", DurableOperationAction.START, null, null)));
|
||||
assertStepDetails(started.newExecutionState().get(0), 0, false, false, false);
|
||||
assertStepDetails(stateOf(arn, started.checkpointToken()), 0, false, false, false);
|
||||
|
||||
CheckpointResult pending = checkpoint(event, started.checkpointToken(), List.of(
|
||||
stepRetry("s1", 3, DurableErrorObject.of("boom", "Error"))));
|
||||
assertEquals(DurableOperationStatus.PENDING, pending.newExecutionState().get(0).getStatus());
|
||||
assertStepDetails(pending.newExecutionState().get(0), 1, true, false, false);
|
||||
assertStepDetails(stateOf(arn, pending.checkpointToken()), 1, true, false, false);
|
||||
return pending();
|
||||
});
|
||||
invoker.script(event -> {
|
||||
String arn = event.get("DurableExecutionArn").asText();
|
||||
assertEquals("READY", operation(event, "s1").get("Status").asText());
|
||||
assertStepDetails(operation(event, "s1").get("StepDetails"), 1, false, true, false);
|
||||
|
||||
CheckpointResult restarted = checkpoint(event, token(event),
|
||||
List.of(step("s1", DurableOperationAction.START, null, null)));
|
||||
assertEquals(DurableOperationStatus.STARTED, restarted.newExecutionState().get(0).getStatus());
|
||||
assertStepDetails(restarted.newExecutionState().get(0), 1, false, false, false);
|
||||
assertStepDetails(stateOf(arn, restarted.checkpointToken()), 1, false, false, false);
|
||||
|
||||
CheckpointResult succeeded = checkpoint(event, restarted.checkpointToken(),
|
||||
List.of(step("s1", DurableOperationAction.SUCCEED, "ok", null)));
|
||||
assertStepDetails(succeeded.newExecutionState().get(0), 2, false, false, true);
|
||||
assertStepDetails(stateOf(arn, succeeded.checkpointToken()), 2, false, false, true);
|
||||
return succeeded("\"done\"");
|
||||
});
|
||||
|
||||
String arn = start("exec-1", "{}", false).getExecutionArn();
|
||||
clock.advance(Duration.ofSeconds(3));
|
||||
service.sweep();
|
||||
|
||||
assertEquals(DurableExecutionStatus.SUCCEEDED, service.get(arn).getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRetryPayloadShowsAsAResultOnlyOnceTheStepIsReady() {
|
||||
invoker.script(event -> {
|
||||
CheckpointResult pending = checkpoint(event, token(event), List.of(
|
||||
step("s1", DurableOperationAction.START, null, null),
|
||||
new DurableOperationUpdate("s1", null, null, DurableOperationType.STEP, "Step",
|
||||
DurableOperationAction.RETRY, "state", null, 3, null, null, null, null, null, null,
|
||||
null)));
|
||||
assertEquals(DurableOperationStatus.PENDING, pending.newExecutionState().get(0).getStatus());
|
||||
assertStepDetails(pending.newExecutionState().get(0), 1, true, false, false);
|
||||
return pending();
|
||||
});
|
||||
invoker.script(event -> {
|
||||
JsonNode details = operation(event, "s1").get("StepDetails");
|
||||
assertEquals("state", details.get("Result").asText());
|
||||
assertFalse(details.has("Error"));
|
||||
assertFalse(details.has("NextAttemptTimestamp"));
|
||||
return succeeded("\"done\"");
|
||||
});
|
||||
|
||||
start("exec-1", "{}", false);
|
||||
clock.advance(Duration.ofSeconds(3));
|
||||
service.sweep();
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyCheckpointCompletesADueWaitInsideTheInvocation() {
|
||||
invoker.script(event -> {
|
||||
@@ -1129,6 +1195,29 @@ class DurableExecutionServiceTest {
|
||||
return event.get("CheckpointToken").asText();
|
||||
}
|
||||
|
||||
private DurableOperation stateOf(String arn, String token) {
|
||||
for (DurableOperation operation : service.getState(arn, token, null, null).operations()) {
|
||||
if ("s1".equals(operation.getId())) {
|
||||
return operation;
|
||||
}
|
||||
}
|
||||
throw new AssertionError("no step s1");
|
||||
}
|
||||
|
||||
private static void assertStepDetails(DurableOperation operation, int attempt, boolean nextAttempt,
|
||||
boolean error, boolean result) {
|
||||
assertStepDetails(DurableWire.operation(operation, false).get("StepDetails"), attempt, nextAttempt, error,
|
||||
result);
|
||||
}
|
||||
|
||||
private static void assertStepDetails(JsonNode details, int attempt, boolean nextAttempt, boolean error,
|
||||
boolean result) {
|
||||
assertEquals(attempt, details.get("Attempt").asInt());
|
||||
assertEquals(nextAttempt, details.has("NextAttemptTimestamp"));
|
||||
assertEquals(error, details.has("Error"));
|
||||
assertEquals(result, details.has("Result"));
|
||||
}
|
||||
|
||||
private static JsonNode operation(JsonNode event, String id) {
|
||||
for (JsonNode operation : event.get("InitialExecutionState").get("Operations")) {
|
||||
if (id.equals(operation.get("Id").asText())) {
|
||||
|
||||
+32
@@ -466,6 +466,38 @@ class LambdaDurableProtocolIntegrationTest {
|
||||
.body("Events[0].EventType", equalTo("ExecutionStopped"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNewlyStartedStepAnswersAttemptZero() throws Exception {
|
||||
CompletableFuture<Response> invoke = CompletableFuture.supplyAsync(() -> given()
|
||||
.header("X-Amz-Durable-Execution-Name", "step-attempt-1")
|
||||
.body("{}")
|
||||
.post(LAMBDA + "/functions/" + FUNCTION + ":$LATEST/invocations"));
|
||||
|
||||
Invocation invocation = invoker.awaitInvocation(Duration.ofSeconds(30));
|
||||
JsonNode event = MAPPER.readTree(invocation.payload());
|
||||
String arn = event.get("DurableExecutionArn").asText();
|
||||
String token = given()
|
||||
.urlEncodingEnabled(false)
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{
|
||||
"CheckpointToken": "%s",
|
||||
"Updates": [{"Id": "s1", "Type": "STEP", "SubType": "Step", "Action": "START"}]
|
||||
}
|
||||
""".formatted(event.get("CheckpointToken").asText()))
|
||||
.post(DURABLE + "/durable-executions/" + arn + "/checkpoint")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("NewExecutionState.Operations[0].Status", equalTo("STARTED"))
|
||||
.body("NewExecutionState.Operations[0].StepDetails.Attempt", equalTo(0))
|
||||
.extract().jsonPath().getString("CheckpointToken");
|
||||
checkpoint(arn, token, """
|
||||
{"Id": "x", "Type": "EXECUTION", "Action": "SUCCEED", "Payload": "\\"done\\""}
|
||||
""");
|
||||
invocation.response().complete(handlerResponse("{\"Status\": \"SUCCEEDED\"}"));
|
||||
assertEquals(200, invoke.get(30, TimeUnit.SECONDS).statusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCallbackCompletedOverTheApiResumesTheExecution() throws Exception {
|
||||
Response accepted = given()
|
||||
|
||||
Reference in new issue
Block a user