Repository navigation
fix: leave replay mode once an operation completed during suspension is consumed #895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
520263c
53ce997
a1d05b7
9d78a1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.lang.reflect.Proxy; | ||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.slf4j.Logger; | ||
| import software.amazon.lambda.durable.model.ExecutionStatus; | ||
| import software.amazon.lambda.durable.testing.LocalDurableTestRunner; | ||
|
|
||
| /** The context must leave replay mode once the first operation that completed during suspension is consumed. */ | ||
| class ReplayModeAfterSuspensionTest { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #859 also asks for coverage of child contexts and parallel branches. All tests here use only the top-level context. Can you add those? 🤖 AI-assisted comment |
||
|
|
||
| @Test | ||
| void waitCompletedDuringSuspension_codeAfterWaitIsNotReplay() { | ||
| var replayStates = new ArrayList<Boolean>(); | ||
| var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> { | ||
| ctx.step("before", String.class, s -> "a"); | ||
| ctx.wait("pause", Duration.ofSeconds(1)); | ||
| replayStates.add(ctx.isReplaying()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests check 🤖 AI-assisted comment |
||
| return "done"; | ||
| }); | ||
|
|
||
| assertEquals(ExecutionStatus.PENDING, runner.run("x").getStatus()); | ||
| replayStates.clear(); | ||
| runner.advanceTime(); | ||
| assertEquals(ExecutionStatus.SUCCEEDED, runner.run("x").getStatus()); | ||
| assertEquals(List.of(false), replayStates); | ||
| } | ||
|
|
||
| @Test | ||
| void callbackCompletedDuringSuspension_codeAfterGetIsNotReplay() { | ||
| var replayStates = new ArrayList<Boolean>(); | ||
| var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> { | ||
| var cb = ctx.createCallback("approval", String.class); | ||
| var value = cb.get(); | ||
| replayStates.add(ctx.isReplaying()); | ||
| return value; | ||
| }); | ||
|
|
||
| assertEquals(ExecutionStatus.PENDING, runner.run("x").getStatus()); | ||
| replayStates.clear(); | ||
| runner.completeCallback(runner.getCallbackId("approval"), "\"ok\""); | ||
| assertEquals(ExecutionStatus.SUCCEEDED, runner.run("x").getStatus()); | ||
| assertEquals(List.of(false), replayStates); | ||
| } | ||
|
|
||
| @Test | ||
| void waitAsyncThenStep_intermediateStepStaysReplay() { | ||
| var states = new ArrayList<String>(); | ||
| var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> { | ||
| var w = ctx.waitAsync("pause", Duration.ofSeconds(1)); | ||
| ctx.step("middle", String.class, s -> "m"); | ||
| states.add("middle=" + ctx.isReplaying()); | ||
| w.get(); | ||
| states.add("after=" + ctx.isReplaying()); | ||
| return "done"; | ||
| }); | ||
|
|
||
| assertEquals(ExecutionStatus.PENDING, runner.run("x").getStatus()); | ||
| states.clear(); | ||
| runner.advanceTime(); | ||
| assertEquals(ExecutionStatus.SUCCEEDED, runner.run("x").getStatus()); | ||
| assertEquals(List.of("middle=true", "after=false"), states); | ||
| } | ||
|
|
||
| @Test | ||
| void waitFinishedInOlderInvocation_staysReplay() { | ||
| var states = new ArrayList<String>(); | ||
| var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> { | ||
| ctx.wait("w1", Duration.ofSeconds(1)); | ||
| states.add("afterW1=" + ctx.isReplaying()); | ||
| ctx.wait("w2", Duration.ofSeconds(1)); | ||
| states.add("afterW2=" + ctx.isReplaying()); | ||
| return "done"; | ||
| }); | ||
|
|
||
| assertEquals(ExecutionStatus.PENDING, runner.run("x").getStatus()); | ||
| runner.advanceTime(); | ||
| assertEquals(ExecutionStatus.PENDING, runner.run("x").getStatus()); | ||
| states.clear(); | ||
| runner.advanceTime(); | ||
| assertEquals(ExecutionStatus.SUCCEEDED, runner.run("x").getStatus()); | ||
| assertEquals(List.of("afterW1=true", "afterW2=false"), states); | ||
| } | ||
|
|
||
| @Test | ||
| void logsAfterWait_reachTheLogger_andReplayedLogsDoNot() { | ||
| var messages = new ArrayList<String>(); | ||
| var delegate = recordingLogger(messages); | ||
| var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> { | ||
| var logger = ctx.getLogger(delegate); | ||
| logger.info("before wait"); | ||
| ctx.wait("pause", Duration.ofSeconds(1)); | ||
| logger.info("after wait"); | ||
| return "done"; | ||
| }); | ||
|
|
||
| assertEquals(ExecutionStatus.PENDING, runner.run("x").getStatus()); | ||
| assertEquals(List.of("before wait"), messages); | ||
| runner.advanceTime(); | ||
| assertEquals(ExecutionStatus.SUCCEEDED, runner.run("x").getStatus()); | ||
| assertEquals(List.of("before wait", "after wait"), messages); | ||
| } | ||
|
|
||
| @Test | ||
| void childContextWaitCompletedDuringSuspension_childLogsReachTheLogger() { | ||
| var messages = new ArrayList<String>(); | ||
| var delegate = recordingLogger(messages); | ||
| var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> ctx.runInChildContext( | ||
| "child", String.class, child -> { | ||
| var logger = child.getLogger(delegate); | ||
| logger.info("child before wait"); | ||
| child.wait("pause", Duration.ofSeconds(1)); | ||
| logger.info("child after wait"); | ||
| return "done"; | ||
| })); | ||
|
|
||
| assertEquals(ExecutionStatus.PENDING, runner.run("x").getStatus()); | ||
| runner.advanceTime(); | ||
| assertEquals(ExecutionStatus.SUCCEEDED, runner.run("x").getStatus()); | ||
| assertEquals(List.of("child before wait", "child after wait"), messages); | ||
| } | ||
|
|
||
| @Test | ||
| void parallelBranchWaitCompletedDuringSuspension_branchLogsReachTheLogger() { | ||
| var messages = new ArrayList<String>(); | ||
| var delegate = recordingLogger(messages); | ||
| var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> { | ||
| var futures = new ArrayList<DurableFuture<String>>(); | ||
| var parallel = ctx.parallel("par"); | ||
| try (parallel) { | ||
| futures.add(parallel.branch("branch-a", String.class, branch -> { | ||
| var logger = branch.getLogger(delegate); | ||
| logger.info("branch before wait"); | ||
| branch.wait("pause", Duration.ofSeconds(1)); | ||
| logger.info("branch after wait"); | ||
| return "A"; | ||
| })); | ||
| } | ||
| parallel.get(); | ||
| return futures.get(0).get(); | ||
| }); | ||
|
|
||
| assertEquals(ExecutionStatus.PENDING, runner.run("x").getStatus()); | ||
| runner.advanceTime(); | ||
| assertEquals(ExecutionStatus.SUCCEEDED, runner.run("x").getStatus()); | ||
| assertEquals(List.of("branch before wait", "branch after wait"), messages); | ||
| } | ||
|
|
||
| private static Logger recordingLogger(List<String> messages) { | ||
| return (Logger) Proxy.newProxyInstance( | ||
| Logger.class.getClassLoader(), new Class<?>[] {Logger.class}, (proxy, method, args) -> { | ||
| var name = method.getName(); | ||
| if (name.equals("info") && args != null && args.length > 0) { | ||
| messages.add((String) args[0]); | ||
| return null; | ||
| } | ||
| if (name.startsWith("is") && name.endsWith("Enabled")) { | ||
| return true; | ||
| } | ||
| if (name.equals("getName")) { | ||
| return "recording"; | ||
| } | ||
| if (name.equals("hashCode")) { | ||
| return System.identityHashCode(proxy); | ||
| } | ||
| if (name.equals("equals")) { | ||
| return proxy == args[0]; | ||
| } | ||
| return method.getReturnType() == boolean.class ? Boolean.FALSE : null; | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -261,6 +261,14 @@ protected Operation waitForOperationCompletion() { | |
| throw terminateExecutionWithIllegalDurableOperationException( | ||
| String.format("%s operation not found: %s", getType(), getOperationId())); | ||
| } | ||
| // An operation that completed while the execution was suspended is the first new event of this | ||
| // invocation. Once its result is consumed, the code that follows is no longer a replay, so leave | ||
| // replay mode and stop suppressing the handler's logs. | ||
|
Comment on lines
+264
to
+266
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check can show logs twice after a failed invocation. The local runner can't show this case. The service fills Steps also go through #859 says it does not need exactly-once logs across retries, so this may be fine. Could you:
🤖 AI-assisted comment |
||
| if (replayCompletedOperation.get() | ||
| && durableContext.isReplaying() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Codex AI review · Finding [P3] Approach: justify each retained condition under rule 4 Rule 4 requires an assertion-sensitive test for every new condition. Removing To suppress repeats, reply |
||
| && executionManager.isOperationUpdatedSinceLastInvocation(getOperationId())) { | ||
|
Comment on lines
+267
to
+269
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: could you add a test with an old wait? The A test with an old wait would stop a later change from breaking this for waits only, for example if the mode switch moves into var states = new ArrayList<String>();
var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> {
ctx.wait("w1", Duration.ofSeconds(1));
states.add("afterW1=" + ctx.isReplaying());
ctx.wait("w2", Duration.ofSeconds(1));
states.add("afterW2=" + ctx.isReplaying());
return "done";
});
assertEquals(ExecutionStatus.PENDING, runner.run("x").getStatus());
runner.advanceTime();
assertEquals(ExecutionStatus.PENDING, runner.run("x").getStatus());
states.clear();
runner.advanceTime();
assertEquals(ExecutionStatus.SUCCEEDED, runner.run("x").getStatus());
assertEquals(List.of("afterW1=true", "afterW2=false"), states);🤖 AI-assisted comment |
||
| durableContext.setExecutionMode(); | ||
|
Comment on lines
+268
to
+270
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Codex AI review · Finding [P2] Keep replay transitions local to the consuming context
To suppress repeats, reply |
||
| } | ||
| return op; | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codex AI review · Finding
arf_v1_cusqr2xcutrqallsrwjbwtztqe[P2] Approach: include the required unit regression coverage
AGENTS.md's Testing Requirements and CONTRIBUTING.md's pull-request requirement 3 require related unit tests, with integration tests added for cross-component behavior. This PR supplies only integration tests and explicitly reports “Unit Tests: No.” The shared replay transition therefore lacks the required isolated regression coverage. Under rule 9, return to the design phase described in rule 8 and revise the validation plan to include unit regressions alongside these integration scenarios.
To suppress repeats, reply
/ai verdict deferred,/ai verdict false-positive, or/ai verdict out-of-scopeon the first nonblank line. Add rationale on later lines. A new/ai verdict acceptedallows future repeats again.