|
| 1 | +import inspect |
| 2 | +import simplejson as json |
| 3 | + |
| 4 | +from datetime import datetime |
| 5 | +from types import GeneratorType |
| 6 | +from typing import Any, Generator, List |
| 7 | + |
| 8 | +import durabletask.protos.helpers as ph |
| 9 | +import durabletask.protos.orchestrator_service_pb2 as pb |
| 10 | +import durabletask.task.task as task |
| 11 | + |
| 12 | +from durabletask.task.orchestrator import OrchestrationContext, Orchestrator |
| 13 | +from durabletask.task.registry import Registry |
| 14 | +from durabletask.task.task import Task |
| 15 | + |
| 16 | + |
| 17 | +class NonDeterminismError(Exception): |
| 18 | + pass |
| 19 | + |
| 20 | + |
| 21 | +class OrchestratorNotFound(ValueError): |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +class OrchestrationStateError(Exception): |
| 26 | + pass |
| 27 | + |
| 28 | + |
| 29 | +class RuntimeOrchestrationContext(OrchestrationContext): |
| 30 | + _generator: Generator[Task, Any, Any] | None |
| 31 | + _previous_task: Task | None |
| 32 | + |
| 33 | + def __init__(self): |
| 34 | + self._generator = None |
| 35 | + self._is_replaying = True |
| 36 | + self._is_complete = False |
| 37 | + self._result = None |
| 38 | + self._pending_actions = dict[int, pb.OrchestratorAction]() |
| 39 | + self._pending_tasks = dict[int, task.CompletableTask]() |
| 40 | + self._sequence_number = 0 |
| 41 | + self._current_utc_datetime = datetime(1000, 1, 1) |
| 42 | + |
| 43 | + def run(self, generator: Generator[task.Task, Any, Any]): |
| 44 | + self._generator = generator |
| 45 | + # TODO: Do something with this task |
| 46 | + task = next(generator) # this starts the generator |
| 47 | + # TODO: Check if the task is null? |
| 48 | + self._previous_task = task |
| 49 | + |
| 50 | + def resume(self): |
| 51 | + if self._generator is None: |
| 52 | + # This is never expected unless maybe there's an issue with the history |
| 53 | + raise TypeError("The orchestrator generator is not initialized! Was the orchestration history corrupted?") |
| 54 | + |
| 55 | + # We can resume the generator only if the previously yielded task |
| 56 | + # has reached a completed state. The only time this won't be the |
| 57 | + # case is if the user yielded on a WhenAll task and there are still |
| 58 | + # outstanding child tasks that need to be completed. |
| 59 | + if self._previous_task is not None and self._previous_task.is_complete(): |
| 60 | + # Resume the generator. This will either return a Task or raise StopIteration if it's done. |
| 61 | + next_task = self._generator.send(self._previous_task.get_result()) |
| 62 | + # TODO: Validate the return value |
| 63 | + self._previous_task = next_task |
| 64 | + |
| 65 | + def set_complete(self, result: Any): |
| 66 | + self._is_complete = True |
| 67 | + self._result = result |
| 68 | + result_json: str | None = None |
| 69 | + if result != None: |
| 70 | + result_json = json.dumps(result) |
| 71 | + action = ph.new_complete_orchestration_action( |
| 72 | + self.next_sequence_number(), pb.ORCHESTRATION_STATUS_COMPLETED, result_json) |
| 73 | + self._pending_actions[action.id] = action |
| 74 | + |
| 75 | + def set_failed(self, ex: Exception): |
| 76 | + self._is_complete = True |
| 77 | + self._pending_actions.clear() # Cancel any pending actions |
| 78 | + action = ph.new_complete_orchestration_action( |
| 79 | + self.next_sequence_number(), pb.ORCHESTRATION_STATUS_FAILED, None, ph.new_failure_details(ex) |
| 80 | + ) |
| 81 | + self._pending_actions[action.id] = action |
| 82 | + |
| 83 | + def get_actions(self) -> List[pb.OrchestratorAction]: |
| 84 | + return list(self._pending_actions.values()) |
| 85 | + |
| 86 | + def next_sequence_number(self) -> int: |
| 87 | + self._sequence_number += 1 |
| 88 | + return self._sequence_number |
| 89 | + |
| 90 | + @property |
| 91 | + def current_utc_datetime(self) -> datetime: |
| 92 | + return self._current_utc_datetime |
| 93 | + |
| 94 | + @current_utc_datetime.setter |
| 95 | + def current_utc_datetime(self, value: datetime): |
| 96 | + self._current_utc_datetime = value |
| 97 | + |
| 98 | + def create_timer(self, fire_at: datetime) -> task.Task: |
| 99 | + id = self.next_sequence_number() |
| 100 | + action = ph.create_timer_action(id, fire_at) |
| 101 | + self._pending_actions[id] = action |
| 102 | + |
| 103 | + timer_task = task.CompletableTask() |
| 104 | + self._pending_tasks[id] = timer_task |
| 105 | + return timer_task |
| 106 | + |
| 107 | + |
| 108 | +class OrchestrationExecutor: |
| 109 | + generator: Orchestrator | None |
| 110 | + |
| 111 | + def __init__(self, registry: Registry): |
| 112 | + self.registry = registry |
| 113 | + self.generator = None |
| 114 | + |
| 115 | + def execute(self, instance_id: str, old_Events: List[pb.HistoryEvent], new_events: List[pb.HistoryEvent]) -> List[pb.OrchestratorAction]: |
| 116 | + if new_events is None or len(new_events) == 0: |
| 117 | + raise OrchestrationStateError( |
| 118 | + "The new history event list must have at least one event in it.") |
| 119 | + |
| 120 | + ctx = RuntimeOrchestrationContext() |
| 121 | + |
| 122 | + try: |
| 123 | + # Rebuild local state by replaying old history into the orchestrator function |
| 124 | + ctx._is_replaying = True |
| 125 | + for old_event in old_Events: |
| 126 | + self.process_event(ctx, old_event) |
| 127 | + |
| 128 | + # Get new actions by executing newly received events into the orchestrator function |
| 129 | + ctx._is_replaying = False |
| 130 | + for new_event in new_events: |
| 131 | + self.process_event(ctx, new_event) |
| 132 | + except Exception as ex: |
| 133 | + # Unhandled exceptions fail the orchestration |
| 134 | + ctx.set_failed(ex) |
| 135 | + |
| 136 | + return ctx.get_actions() |
| 137 | + |
| 138 | + def process_event(self, ctx: RuntimeOrchestrationContext, event: pb.HistoryEvent) -> None: |
| 139 | + try: |
| 140 | + if event.HasField("orchestratorStarted"): |
| 141 | + ctx.current_utc_datetime = event.timestamp.ToDatetime() |
| 142 | + elif event.HasField("executionStarted"): |
| 143 | + # TODO: Check if we already started the orchestration |
| 144 | + fn = self.registry.get_orchestrator(event.executionStarted.name) |
| 145 | + if fn is None: |
| 146 | + raise OrchestratorNotFound(f"A '{event.executionStarted.name}' orchestrator was not registered.") |
| 147 | + result = fn(ctx) # this does not execute the generator, only creates it |
| 148 | + if isinstance(result, GeneratorType): |
| 149 | + # Start the orchestrator's generator function |
| 150 | + ctx.run(result) |
| 151 | + else: |
| 152 | + # This is an orchestrator that doesn't schedule any tasks |
| 153 | + ctx.set_complete(result) |
| 154 | + elif event.HasField("timerCreated"): |
| 155 | + id = event.eventId |
| 156 | + if ctx._pending_actions.pop(id, None) is None: |
| 157 | + raise NonDeterminismError(inspect.cleandoc(f""" |
| 158 | + A previous execution called create_timer with sequence number {id}, but the current |
| 159 | + execution doesn't have this action with this sequence number. This problem occurs |
| 160 | + when either the orchestration has non-deterministic logic or if the code was changed |
| 161 | + after an instance of this orchestration already started running.""")) |
| 162 | + elif event.HasField("timerFired"): |
| 163 | + id = event.timerFired.timerId |
| 164 | + timer_task = ctx._pending_tasks.pop(id, None) |
| 165 | + if timer_task is None: |
| 166 | + # TODO: This could be a duplicate event or it could be a non-deterministic orchestration. |
| 167 | + # Duplicate events should be handled gracefully with a warning. Otherwise, the |
| 168 | + # orchestration should probably fail with an error. |
| 169 | + return |
| 170 | + timer_task.complete(None) |
| 171 | + ctx.resume() |
| 172 | + else: |
| 173 | + eventType = event.WhichOneof("eventType") |
| 174 | + raise OrchestrationStateError(f"Don't know how to handle event of type '{eventType}'") |
| 175 | + except StopIteration as generatorStopped: |
| 176 | + # The orchestrator generator function completed |
| 177 | + ctx.set_complete(generatorStopped.value) |
0 commit comments