Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
OperationIdentifier,
OperationIdNamespace,
)
from aws_durable_execution_sdk_python.lambda_service import ErrorObject, OperationType
from aws_durable_execution_sdk_python.lambda_service import (
ErrorObject,
OperationStatus,
OperationType,
)
from aws_durable_execution_sdk_python.operation.child import child_handler


Expand Down Expand Up @@ -276,6 +280,13 @@ def execute(
branch.index: branch for branch in self.branches
}

# A resumed batch can hold child contexts that already recorded a
# terminal outcome. Their replays reach the coordinator in thread
# completion order, so the batch decides completion only after every
# one has settled, and starts no item past the last recorded one.
recorded_unsettled: set[int] = self._recorded_terminal_indexes(execution_state)
last_recorded: int = max(recorded_unsettled, default=-1)

in_flight: int = 0
running: int = 0
succeeded: int = 0
Expand Down Expand Up @@ -330,10 +341,13 @@ def submit(branch: Branch[CallableType, ResultType]) -> None:
)
decision = self.policy.evaluate(succeeded, failed, items_snapshot)
needs_snapshot_rebuild = False
if self.policy.is_complete(succeeded, failed, items_snapshot, decision):
break
if not self.policy.should_continue(failed):
break
if not recorded_unsettled:
if self.policy.is_complete(
succeeded, failed, items_snapshot, decision
):
break
if not self.policy.should_continue(failed):
break

# Start branches in index order up to the in-flight limit.
# Suspended branches keep their slot: in_flight only
Expand All @@ -342,6 +356,7 @@ def submit(branch: Branch[CallableType, ResultType]) -> None:
pending
and in_flight < max_in_flight
and self.policy.should_continue(failed)
and (not recorded_unsettled or pending[0].index <= last_recorded)
):
submit(pending.popleft())
in_flight += 1
Expand Down Expand Up @@ -394,12 +409,14 @@ def submit(branch: Branch[CallableType, ResultType]) -> None:
match event.kind:
case BranchEventKind.COMPLETED:
applied.complete(event.result)
recorded_unsettled.discard(event.index)
succeeded += 1
running -= 1
in_flight -= 1
needs_snapshot_rebuild = True
case BranchEventKind.FAILED if event.error is not None:
applied.fail(event.error)
recorded_unsettled.discard(event.index)
failed += 1
running -= 1
in_flight -= 1
Expand Down Expand Up @@ -496,6 +513,22 @@ def submit(branch: Branch[CallableType, ResultType]) -> None:

return self._create_result(completion_reason)

def _recorded_terminal_indexes(self, execution_state: ExecutionState) -> set[int]:
"""Return the indexes whose NESTED child context is already terminal.

FLAT branches never checkpoint themselves, so they have no record.
"""
if self.nesting_type is NestingType.FLAT:
return set()
return {
executable.index
for executable in self.executables
if execution_state.get_checkpoint_result(
self.operation_id_namespace.create_id_for_step(executable.index)
).status
in (OperationStatus.SUCCEEDED, OperationStatus.FAILED)
}

def _create_result(
self, completion_reason: CompletionReason | None = None
) -> BatchResult[ResultType]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5493,3 +5493,66 @@ def _execute_item_in_child_context(
call.args[0].shutdown(wait=True)
binding.reset(token)
assert binding.get() == "empty"


def test_resumed_batch_settles_recorded_terminal_items_before_completing():
"""Recorded terminal items settle before the batch decides completion.

Items 0 and 1 are recorded SUCCEEDED, item 2 FAILED and item 3 STARTED. The
recorded outcomes replay on worker threads in thread-completion order, so
item 2's recorded failure can arrive after the second success has already
met min_successful. The result must still carry the recorded failure, and
item 3 (not a terminal outcome) must not be started.
"""
recorded = {
"op_0": OperationStatus.SUCCEEDED,
"op_1": OperationStatus.SUCCEEDED,
"op_2": OperationStatus.FAILED,
"op_3": OperationStatus.STARTED,
}
started: list[int] = []

class RecordedExecutor(ConcurrentExecutor):
def _execute_item_in_child_context(self, executor_context, executable):
started.append(executable.index)
if executable.index == 2:
time.sleep(0.2) # replays its recorded failure after items 0, 1
msg = "recorded failure"
raise ValueError(msg)
return f"result_{executable.index}"

executor = RecordedExecutor(
executables=[Executable(i, lambda: None) for i in range(4)],
max_concurrency=2,
completion_config=CompletionConfig(min_successful=2),
sub_type_top="TOP",
sub_type_iteration="ITER",
name_prefix="test_",
serdes=None,
operation_id_namespace=_StubNamespace(),
)

execution_state = Mock()
execution_state.get_checkpoint_result.side_effect = lambda operation_id: (
CheckpointedResult.create_from_operation(
Operation(
operation_id=operation_id,
operation_type=OperationType.CONTEXT,
status=recorded[operation_id],
)
)
)
executor_context = Mock()
executor_context._parent_id = "batch" # noqa: SLF001

result = executor.execute(execution_state, executor_context)

assert [item.status for item in result.all] == [
BatchItemStatus.SUCCEEDED,
BatchItemStatus.SUCCEEDED,
BatchItemStatus.FAILED,
]
assert result.success_count == 2
assert result.failure_count == 1
assert result.completion_reason is CompletionReason.MIN_SUCCESSFUL_REACHED
assert 3 not in started