Skip to content
Draft
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
19 changes: 14 additions & 5 deletions temporalio/worker/_workflow_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,17 @@ def activate(
self._is_replaying = act.is_replaying
self._current_thread_id = threading.get_ident()
self._current_internal_flags = act.available_internal_flags
self._single_batch_activation = self._workflow_logic_flag_enabled(
# The original flag also changed cancellation behavior. Histories using
# it must retain those semantics; the new flag only changes scheduling.
self._single_batch_cancellation = self._workflow_logic_flag_enabled(
_WorkflowLogicFlag.PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH
)
single_batch_activation = (
self._single_batch_cancellation
or self._workflow_logic_flag_enabled(
_WorkflowLogicFlag.APPLY_ACTIVATION_JOBS_BEFORE_RUNNING_TASKS
)
)
activation_err: Exception | None = None
try:
# Split into job sets with patches, then signals + updates, then
Expand Down Expand Up @@ -532,7 +540,7 @@ def activate(
if start_job:
self._workflow_input = self._make_workflow_input(start_job)

if self._single_batch_activation:
if single_batch_activation:
# Applying every job before giving workflow tasks a chance to
# run prevents their order in the activation from hiding state
# that arrived in the same workflow task.
Expand Down Expand Up @@ -700,7 +708,7 @@ def _apply_cancel_workflow(
# workflow the ability to receive the cancellation, so we must defer
# this cancellation to the next iteration of the event loop.
self.call_soon(self._primary_task.cancel)
elif self._single_batch_activation:
elif self._single_batch_cancellation:
# Initialization is the only job that creates the primary task, so
# retain a same-activation cancellation until that task exists.
self._cancel_primary_task_pending = True
Expand Down Expand Up @@ -2028,7 +2036,7 @@ async def workflow_wait_condition(
# cancellation and keep going.
def cancellation_arrived() -> bool:
return (
self._single_batch_activation
self._single_batch_cancellation
and not cancellation_requested_before
and self._cancel_reason is not None
)
Expand Down Expand Up @@ -2423,7 +2431,7 @@ async def _await_temporal_operation(
if fut.done():
# Retrying the shield after both futures become ready would
# return the result and erase the task cancellation.
if self._single_batch_activation:
if self._single_batch_cancellation:
raise
if completed_cancellation_flag is not None and (
not self._is_replaying
Expand Down Expand Up @@ -4266,6 +4274,7 @@ class _WorkflowLogicFlag(IntEnum):

RAISE_ON_CANCELLING_COMPLETED_ACTIVITY = 1
PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH = 2
APPLY_ACTIVATION_JOBS_BEFORE_RUNNING_TASKS = 3


_DEFAULT_ENABLED_WORKFLOW_LOGIC_FLAGS: frozenset[_WorkflowLogicFlag] = frozenset()
23 changes: 21 additions & 2 deletions tests/worker/test_replayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,18 +557,37 @@ async def test_replayer_async_ordering(
Verify legacy and single-batch histories replay with the asyncio scheduling order that their
original executions observed.
"""
global test_replayer_workflow_res

with Path(__file__).with_name(history_filename).open() as f:
history = WorkflowHistory.from_json("fake", f.read())
assert (
_history_uses_workflow_logic_flag(history, _SINGLE_BATCH_WORKFLOW_LOGIC_FLAG)
is uses_single_batch
)
await Replayer(
replayer = Replayer(
workflows=[SignalsActivitiesTimersUpdatesTracingWorkflow],
interceptors=[WorkerWorkflowResultInterceptor()],
).replay_workflow(history)
)
test_replayer_workflow_res = None
await replayer.replay_workflow(history)
assert test_replayer_workflow_res == expected

if uses_single_batch:
# These histories do not exercise cancellation, so the batching-only
# flag must replay them with the same scheduling order.
for event in history.events:
if event.HasField("workflow_task_completed_event_attributes"):
flags = event.workflow_task_completed_event_attributes.sdk_metadata.lang_used_flags
if _SINGLE_BATCH_WORKFLOW_LOGIC_FLAG in flags:
flags.remove(_SINGLE_BATCH_WORKFLOW_LOGIC_FLAG)
flags.append(
_WorkflowLogicFlag.APPLY_ACTIVATION_JOBS_BEFORE_RUNNING_TASKS
)
test_replayer_workflow_res = None
await replayer.replay_workflow(history)
assert test_replayer_workflow_res == expected


async def test_replayer_unflagged_history_uses_legacy_async_ordering() -> None:
with (
Expand Down
46 changes: 31 additions & 15 deletions tests/worker/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,11 +1518,19 @@ async def run(self) -> str:
_SINGLE_BATCH_WORKFLOW_LOGIC_FLAG = (
_WorkflowLogicFlag.PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH
)
_BATCH_ONLY_WORKFLOW_LOGIC_FLAG = (
_WorkflowLogicFlag.APPLY_ACTIVATION_JOBS_BEFORE_RUNNING_TASKS
)


def test_single_batch_workflow_activation_jobs_default_disabled() -> None:
@pytest.mark.parametrize(
"flag", [_SINGLE_BATCH_WORKFLOW_LOGIC_FLAG, _BATCH_ONLY_WORKFLOW_LOGIC_FLAG]
)
def test_single_batch_workflow_activation_jobs_default_disabled(
flag: _WorkflowLogicFlag,
) -> None:
assert (
_SINGLE_BATCH_WORKFLOW_LOGIC_FLAG
flag
not in temporalio.worker._workflow_instance._DEFAULT_ENABLED_WORKFLOW_LOGIC_FLAGS
)

Expand Down Expand Up @@ -1781,8 +1789,15 @@ def child_started(self) -> bool:
return self._child_started


@pytest.mark.parametrize(
"flag",
[
pytest.param(_SINGLE_BATCH_WORKFLOW_LOGIC_FLAG, id="with-cancellation-changes"),
pytest.param(_BATCH_ONLY_WORKFLOW_LOGIC_FLAG, id="batch-only"),
],
)
async def test_workflow_cancel_and_child_completion_in_same_activation(
client: Client,
client: Client, flag: _WorkflowLogicFlag
):
parent_task_queue = str(uuid.uuid4())
child_task_queue = str(uuid.uuid4())
Expand All @@ -1795,9 +1810,6 @@ async def test_workflow_cancel_and_child_completion_in_same_activation(
CompleteChildOnSignalWorkflow,
task_queue=child_task_queue,
)
child_worker._set_default_workflow_logic_flag(
_SINGLE_BATCH_WORKFLOW_LOGIC_FLAG, enabled=True
)
async with child_worker:
handle = await client.start_workflow(
CancelAtChildCompletionWorkflow.run,
Expand All @@ -1815,9 +1827,7 @@ async def test_workflow_cancel_and_child_completion_in_same_activation(
# worker stops on the normal queue.
max_cached_workflows=0,
)
parent_worker._set_default_workflow_logic_flag(
_SINGLE_BATCH_WORKFLOW_LOGIC_FLAG, enabled=True
)
parent_worker._set_default_workflow_logic_flag(flag, enabled=True)
async with parent_worker:

async def child_started() -> bool:
Expand Down Expand Up @@ -1847,14 +1857,20 @@ async def child_completion_recorded() -> None:
task_queue=parent_task_queue,
workflow_runner=runner,
)
parent_worker._set_default_workflow_logic_flag(
_SINGLE_BATCH_WORKFLOW_LOGIC_FLAG, enabled=True
)
parent_worker._set_default_workflow_logic_flag(flag, enabled=True)
async with parent_worker:
with pytest.raises(WorkflowFailureError) as err:
await handle.result()
assert isinstance(err.value.cause, CancelledError)
if flag == _BATCH_ONLY_WORKFLOW_LOGIC_FLAG:
# Legacy cancellation handling lets the completed child result win.
assert await handle.result() == "child complete"
else:
with pytest.raises(WorkflowFailureError) as err:
await handle.result()
assert isinstance(err.value.cause, CancelledError)

assert any(
flag in completion.successful.used_internal_flags
for _, completion in runner._pairs
)
assert any(
{"resolve_child_workflow_execution", "cancel_workflow"}.issubset(
{job.WhichOneof("variant") for job in activation.jobs}
Expand Down
Loading