fix(swf): wait for new events after empty decisions (#5746)

* fix(swf): wait for new events after empty decisions

Signed-off-by: Andre Kurait <akurait@amazon.com>

* fix(swf): wake senders after successful external outcomes

Signed-off-by: Andre Kurait <akurait@amazon.com>

---------

Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-16 14:01:24 -04:00
1 parent 4776bfb899
commit e245dbbdd5
4 files changed
+125 -7

No files matched your search

+5
View File
@@ -89,6 +89,11 @@ fresh decision task. A closing decision must be last in the batch: a batch with
after it is rejected with `ValidationException` ("Close must be last decision in list") and
no decision is applied.
An empty decision response leaves the execution open and waits for a new event.
It does not schedule another task merely because no activity, timer, or child is
pending. A signal or outcome received during a started decision is retained and
schedules one successor after that decision completes.
## Timeouts
A background sweep expires timeouts and appends the matching event:
@@ -614,7 +614,7 @@ public class SwfService implements Resettable {
applyDecisions(execution, decisions, queued, decisionEventId);
if (execution.isOpen() && (execution.isDecisionNeeded() || hasNothingPending(execution))) {
if (execution.isOpen() && execution.isDecisionNeeded()) {
execution.setDecisionNeeded(false);
scheduleDecisionTask(execution);
}
@@ -1036,6 +1036,7 @@ public class SwfService implements Resettable {
.attr("workflowExecution", Map.of("workflowId", target.getWorkflowId(),
"runId", target.getRunId()))
.attr("initiatedEventId", initiated.getEventId());
execution.setDecisionNeeded(true);
}
private void cancelExternal(SwfWorkflowExecution execution, Decision decision, long decisionEventId) {
@@ -1067,6 +1068,7 @@ public class SwfService implements Resettable {
.attr("workflowExecution", Map.of("workflowId", target.getWorkflowId(),
"runId", target.getRunId()))
.attr("initiatedEventId", initiated.getEventId());
execution.setDecisionNeeded(true);
}
private void startChild(SwfWorkflowExecution execution, Decision decision, long decisionEventId) {
@@ -515,13 +515,23 @@ class SwfIntegrationTest {
{"taskToken": "%s", "decisions": []}
""".formatted(first.path("taskToken").toString())).then().statusCode(200);
call("PollForDecisionTask", """
Response deferred = call("PollForDecisionTask", """
{"domain": "%s", "taskList": {"name": "floci-tl"}, "identity": "d4"}
""".formatted(domain))
.then()
""".formatted(domain));
deferred.then()
.statusCode(200)
.body("taskToken", org.hamcrest.Matchers.not(equalTo("")))
.body("events.eventType", hasItem("WorkflowExecutionSignaled"));
call("RespondDecisionTaskCompleted", """
{"taskToken": "%s", "decisions": []}
""".formatted(deferred.path("taskToken").toString())).then().statusCode(200);
call("CountPendingDecisionTasks", """
{"domain": "%s", "taskList": {"name": "floci-tl"}}
""".formatted(domain)).then().statusCode(200).body("count", equalTo(0));
call("PollForDecisionTask", """
{"domain": "%s", "taskList": {"name": "floci-tl"}, "identity": "d5"}
""".formatted(domain)).then().statusCode(200).body("taskToken", equalTo(""));
}
@Test
@@ -20,6 +20,9 @@ import io.github.hectorvent.floci.services.swf.model.SwfWorkflowType;
import io.github.hectorvent.floci.testing.MutableClock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
@@ -167,6 +170,38 @@ class SwfServiceTest {
"a second decider must not receive the same execution's task");
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void emptyDecisionCompletion_waitsForANewEventWithoutGrowingHistory(boolean omitDecisions) {
String runId = start("wf-idle");
SwfDecisionTask first = service.pollForDecisionTask(REGION, DOMAIN, "tl", "d1").orElseThrow();
service.respondDecisionTaskCompleted(first.getTaskToken(), omitDecisions ? null : List.of(), "waiting");
List<SwfHistoryEvent> history = service.getWorkflowExecutionHistory(
REGION, DOMAIN, "wf-idle", runId, false);
assertEquals(List.of("WorkflowExecutionStarted", "DecisionTaskScheduled",
"DecisionTaskStarted", "DecisionTaskCompleted"),
history.stream().map(SwfHistoryEvent::getEventType).toList());
assertEquals(first.getStartedEventId(), history.getLast().getAttributes().get("startedEventId"));
for (int i = 0; i < 5; i++) {
assertTrue(service.pollForDecisionTask(REGION, DOMAIN, "tl", "idle").isEmpty());
service.sweep();
}
SwfWorkflowExecution execution = service.describeWorkflowExecution(REGION, DOMAIN, "wf-idle", runId);
assertTrue(execution.isOpen());
assertEquals("waiting", execution.getLatestExecutionContext());
assertEquals(4, execution.getEvents().size(), "idle polls must not manufacture history");
assertEquals(0, service.countPendingDecisionTasks(REGION, DOMAIN, "tl"));
service.signalWorkflowExecution(REGION, DOMAIN, "wf-idle", runId, "resume", null);
SwfDecisionTask resumed = service.pollForDecisionTask(REGION, DOMAIN, "tl", "d2").orElseThrow();
assertEquals(first.getStartedEventId(), resumed.getPreviousStartedEventId());
service.respondDecisionTaskCompleted(resumed.getTaskToken(),
List.of(new Decision("CompleteWorkflowExecution", Map.of("result", "done"))), null);
assertEquals("COMPLETED",
service.describeWorkflowExecution(REGION, DOMAIN, "wf-idle", runId).getCloseStatus());
}
@Test
void signalWhileDecisionOutstanding_defersTheNextTaskUntilTheCurrentOneCompletes() {
String runId = start("wf-defer");
@@ -177,8 +212,40 @@ class SwfServiceTest {
"the signal must not create a concurrent decision task");
service.respondDecisionTaskCompleted(first.getTaskToken(), List.of(), null);
assertTrue(service.pollForDecisionTask(REGION, DOMAIN, "tl", "d3").isPresent(),
"completing the outstanding task must release the deferred one");
SwfDecisionTask deferred = service.pollForDecisionTask(REGION, DOMAIN, "tl", "d3").orElseThrow();
assertEquals(first.getStartedEventId(), deferred.getPreviousStartedEventId());
service.respondDecisionTaskCompleted(deferred.getTaskToken(), List.of(), null);
assertTrue(service.pollForDecisionTask(REGION, DOMAIN, "tl", "d4").isEmpty(),
"the signal must schedule only one successor");
}
@Test
void activityAndTimerDuringDecision_deferOneSuccessorThenWait() {
String runId = start("wf-deferred-outcomes");
SwfDecisionTask first = service.pollForDecisionTask(REGION, DOMAIN, "tl", "d1").orElseThrow();
service.respondDecisionTaskCompleted(first.getTaskToken(), List.of(
new Decision("ScheduleActivityTask", Map.of("activityId", "act",
"activityType", Map.of("name", "A", "version", "1"))),
new Decision("StartTimer", Map.of("timerId", "timer", "startToFireTimeout", "1"))), null);
service.signalWorkflowExecution(REGION, DOMAIN, "wf-deferred-outcomes", runId, "inspect", null);
SwfDecisionTask current = service.pollForDecisionTask(REGION, DOMAIN, "tl", "d2").orElseThrow();
SwfActivityTask activity = service.pollForActivityTask(REGION, DOMAIN, "act-tl", "worker").orElseThrow();
service.respondActivityTaskCompleted(activity.getTaskToken(), "done");
clock.advance(Duration.ofSeconds(2));
service.sweep();
assertTrue(service.pollForDecisionTask(REGION, DOMAIN, "tl", "d3").isEmpty(),
"outcomes must not create a task alongside the started one");
service.respondDecisionTaskCompleted(current.getTaskToken(), List.of(), null);
assertEquals(1, service.countPendingDecisionTasks(REGION, DOMAIN, "tl"));
List<SwfHistoryEvent> history = service.getWorkflowExecutionHistory(
REGION, DOMAIN, "wf-deferred-outcomes", runId, false);
assertTrue(history.stream().anyMatch(event -> "ActivityTaskCompleted".equals(event.getEventType())));
assertTrue(history.stream().anyMatch(event -> "TimerFired".equals(event.getEventType())));
SwfDecisionTask deferred = service.pollForDecisionTask(REGION, DOMAIN, "tl", "d4").orElseThrow();
assertEquals(current.getStartedEventId(), deferred.getPreviousStartedEventId());
service.respondDecisionTaskCompleted(deferred.getTaskToken(), List.of(), null);
assertTrue(service.pollForDecisionTask(REGION, DOMAIN, "tl", "d5").isEmpty());
}
@Test
@@ -1129,6 +1196,39 @@ class SwfServiceTest {
assertNotNull(lastAttribute(sender, "ExternalWorkflowExecutionSignaled", "initiatedEventId"));
}
@ParameterizedTest
@CsvSource({
"SignalExternalWorkflowExecution, ExternalWorkflowExecutionSignaled, WorkflowExecutionSignaled",
"RequestCancelExternalWorkflowExecution, ExternalWorkflowExecutionCancelRequested, WorkflowExecutionCancelRequested"
})
void successfulExternalOutcome_deliversOneSuccessorToTheSender(
String decisionType, String outcomeEvent, String targetEvent) {
String senderRun = start("wf-sender");
SwfDecisionTask first = service.pollForDecisionTask(REGION, DOMAIN, "tl", "sender").orElseThrow();
String receiverRun = service.startWorkflowExecution(new StartWorkflowExecutionRequest(
REGION, DOMAIN, "wf-receiver", "W", "1", "receiver-tl", null, null, null, null, null, null, null));
Map<String, Object> attributes = "SignalExternalWorkflowExecution".equals(decisionType)
? Map.of("workflowId", "wf-receiver", "runId", receiverRun, "signalName", "ping")
: Map.of("workflowId", "wf-receiver", "runId", receiverRun);
service.respondDecisionTaskCompleted(first.getTaskToken(),
List.of(new Decision(decisionType, attributes)), null);
SwfWorkflowExecution sender = service.describeWorkflowExecution(REGION, DOMAIN, "wf-sender", senderRun);
SwfWorkflowExecution receiver = service.describeWorkflowExecution(REGION, DOMAIN, "wf-receiver", receiverRun);
assertNotNull(lastAttribute(sender, outcomeEvent, "initiatedEventId"));
assertNotNull(lastAttribute(receiver, targetEvent, "externalInitiatedEventId"));
assertEquals(1, service.countPendingDecisionTasks(REGION, DOMAIN, "tl"));
SwfDecisionTask successor = service.pollForDecisionTask(REGION, DOMAIN, "tl", "sender").orElseThrow();
assertEquals(senderRun, successor.getRunId());
assertEquals(first.getStartedEventId(), successor.getPreviousStartedEventId());
assertTrue(service.pollForDecisionTask(REGION, DOMAIN, "tl", "other").isEmpty());
service.respondDecisionTaskCompleted(successor.getTaskToken(), List.of(), null);
assertEquals(0, service.countPendingDecisionTasks(REGION, DOMAIN, "tl"));
assertTrue(service.pollForDecisionTask(REGION, DOMAIN, "tl", "sender").isEmpty());
}
@Test
void signalUnknownExternalExecution_recordsFailureRatherThanThrowing() {
start("wf-lonely");
@@ -1216,7 +1316,8 @@ class SwfServiceTest {
if (workflowId.equals(task.getWorkflowId())) {
return task;
}
// Release a sibling's task so its own poller can claim it.
service.signalWorkflowExecution(REGION, DOMAIN, task.getWorkflowId(),
task.getRunId(), "test-poller-handoff", null);
service.respondDecisionTaskCompleted(task.getTaskToken(), List.of(), null);
}
// Back off after a steal too, so the sibling's own poller gets a window to