|
25 | 25 | import random |
26 | 26 | import uuid |
27 | 27 | from collections.abc import Callable, Iterable, Mapping, Sequence |
| 28 | +from copy import copy |
28 | 29 | from dataclasses import dataclass, field |
29 | 30 | from datetime import datetime, timezone |
30 | 31 | from typing import Any, TypeVar, cast |
@@ -450,6 +451,12 @@ class ScheduleActivity: |
450 | 451 | schedule_to_start_timeout: int | None = None |
451 | 452 | schedule_to_close_timeout: int | None = None |
452 | 453 | heartbeat_timeout: int | None = None |
| 454 | + _parallel_group_path: list[dict[str, Any]] | None = field( |
| 455 | + default=None, |
| 456 | + init=False, |
| 457 | + repr=False, |
| 458 | + compare=False, |
| 459 | + ) |
453 | 460 |
|
454 | 461 | def to_server_command( |
455 | 462 | self, |
@@ -535,6 +542,12 @@ class StartTimer: |
535 | 542 | """Command requesting a durable timer.""" |
536 | 543 |
|
537 | 544 | delay_seconds: int |
| 545 | + _parallel_group_path: list[dict[str, Any]] | None = field( |
| 546 | + default=None, |
| 547 | + init=False, |
| 548 | + repr=False, |
| 549 | + compare=False, |
| 550 | + ) |
538 | 551 |
|
539 | 552 | def to_server_command( |
540 | 553 | self, |
@@ -771,6 +784,12 @@ class StartChildWorkflow: |
771 | 784 | retry_policy: ChildWorkflowRetryPolicyInput | None = None |
772 | 785 | execution_timeout_seconds: int | None = None |
773 | 786 | run_timeout_seconds: int | None = None |
| 787 | + _parallel_group_path: list[dict[str, Any]] | None = field( |
| 788 | + default=None, |
| 789 | + init=False, |
| 790 | + repr=False, |
| 791 | + compare=False, |
| 792 | + ) |
774 | 793 |
|
775 | 794 | def to_server_command( |
776 | 795 | self, |
@@ -1004,6 +1023,18 @@ def _condition_predicate_fingerprint(predicate: Callable[[], bool]) -> str: |
1004 | 1023 | ) |
1005 | 1024 |
|
1006 | 1025 |
|
| 1026 | +def _apply_parallel_group_metadata( |
| 1027 | + command: Command, |
| 1028 | + server_command: dict[str, Any], |
| 1029 | +) -> None: |
| 1030 | + path = getattr(command, "_parallel_group_path", None) |
| 1031 | + if not path: |
| 1032 | + return |
| 1033 | + |
| 1034 | + server_command.update(path[-1]) |
| 1035 | + server_command["parallel_group_path"] = [dict(entry) for entry in path] |
| 1036 | + |
| 1037 | + |
1007 | 1038 | def commands_to_server_commands( |
1008 | 1039 | commands: Sequence[Command], |
1009 | 1040 | task_queue: str, |
@@ -1178,6 +1209,9 @@ def commands_to_server_commands( |
1178 | 1209 | for (index, key, _, _), blob in zip(encode_jobs, blobs, strict=True): |
1179 | 1210 | server_commands[index][key] = blob |
1180 | 1211 |
|
| 1212 | + for command, server_command in zip(commands, server_commands, strict=True): |
| 1213 | + _apply_parallel_group_metadata(command, server_command) |
| 1214 | + |
1181 | 1215 | return server_commands |
1182 | 1216 |
|
1183 | 1217 |
|
@@ -3143,6 +3177,54 @@ def _same_logical_condition_wait(opened: Mapping[str, Any], cmd: WaitCondition) |
3143 | 3177 | advanced_cmd: Any = None |
3144 | 3178 | terminal_condition_reopen_cmd: WaitCondition | None = None |
3145 | 3179 |
|
| 3180 | + def _annotate_parallel_commands(commands: list[Any]) -> list[Any]: |
| 3181 | + if not commands: |
| 3182 | + return commands |
| 3183 | + if len(commands) > 1000: |
| 3184 | + raise ValueError("parallel workflow group exceeds the deterministic limit of 1000 operations") |
| 3185 | + if not all(isinstance(command, ScheduleActivity | StartTimer | StartChildWorkflow) for command in commands): |
| 3186 | + return commands |
| 3187 | + |
| 3188 | + kinds = { |
| 3189 | + "activity" |
| 3190 | + if isinstance(command, ScheduleActivity) |
| 3191 | + else "timer" |
| 3192 | + if isinstance(command, StartTimer) |
| 3193 | + else "child" |
| 3194 | + for command in commands |
| 3195 | + } |
| 3196 | + kind = next(iter(kinds)) if len(kinds) == 1 else "mixed" |
| 3197 | + prefix = { |
| 3198 | + "activity": "parallel-activities", |
| 3199 | + "child": "parallel-children", |
| 3200 | + "timer": "parallel-timers", |
| 3201 | + "mixed": "parallel-calls", |
| 3202 | + }[kind] |
| 3203 | + candidates = _unconsumed_recorded_steps() |
| 3204 | + if candidates: |
| 3205 | + base_sequence = candidates[0].workflow_sequence |
| 3206 | + else: |
| 3207 | + recorded_sequences = [ |
| 3208 | + step.workflow_sequence for step in recorded_steps + recorded_wait_steps + recorded_pending_steps |
| 3209 | + ] |
| 3210 | + base_sequence = max(recorded_sequences, default=0) + len(pending) + 1 |
| 3211 | + |
| 3212 | + group_id = f"{prefix}:{base_sequence}:{len(commands)}" |
| 3213 | + annotated_commands: list[Any] = [] |
| 3214 | + for index, command in enumerate(commands): |
| 3215 | + occurrence = copy(command) |
| 3216 | + occurrence._parallel_group_path = [ |
| 3217 | + { |
| 3218 | + "parallel_group_id": group_id, |
| 3219 | + "parallel_group_kind": kind, |
| 3220 | + "parallel_group_base_sequence": base_sequence, |
| 3221 | + "parallel_group_size": len(commands), |
| 3222 | + "parallel_group_index": index, |
| 3223 | + } |
| 3224 | + ] |
| 3225 | + annotated_commands.append(occurrence) |
| 3226 | + return annotated_commands |
| 3227 | + |
3146 | 3228 | def _condition_wait_has_pending_receivers(condition_wait_id: str | None) -> bool: |
3147 | 3229 | if condition_wait_id is None: |
3148 | 3230 | return False |
@@ -3194,6 +3276,7 @@ def _terminal_state(value: Any, *, include_pending: bool) -> _ReplayState: |
3194 | 3276 | if isinstance(cmd, list): |
3195 | 3277 | if any(isinstance(child_command, NexusServiceCall) for child_command in cmd): |
3196 | 3278 | raise TypeError("Nexus service calls must be yielded one at a time") |
| 3279 | + cmd = _annotate_parallel_commands(cmd) |
3197 | 3280 | needed = len(cmd) |
3198 | 3281 | if result_cursor + needed <= len(resolved_results): |
3199 | 3282 | for offset, child_command in enumerate(cmd): |
|
0 commit comments