|
| 1 | +"""Bounded source example using the unreleased checkpoint API. See CHECKPOINTED_BATCH.md.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import asyncio |
| 6 | +import hashlib |
| 7 | +import json |
| 8 | +import os |
| 9 | +from collections.abc import Awaitable, Callable, Generator |
| 10 | +from dataclasses import asdict, dataclass |
| 11 | +from pathlib import Path |
| 12 | +from typing import Any |
| 13 | +from uuid import uuid4 |
| 14 | + |
| 15 | +from durable_workflow import ActivityRetryPolicy, Client, Worker, activity, workflow |
| 16 | +from durable_workflow.errors import ActivityHeartbeatResumeUnavailable, NonRetryableError |
| 17 | + |
| 18 | +FAILURES = ("before-commit", "after-commit", "after-checkpoint") |
| 19 | +ACTIVITY = "examples.checkpointed-batch" |
| 20 | + |
| 21 | + |
| 22 | +@dataclass(frozen=True) |
| 23 | +class Batch: |
| 24 | + business_id: str |
| 25 | + items: list[str] |
| 26 | + failure: str |
| 27 | + |
| 28 | + def snapshot(self) -> tuple[str, tuple[str, ...], str]: |
| 29 | + if not isinstance(self.items, list) or not 1 <= len(self.items) <= 16 or self.failure not in FAILURES: |
| 30 | + raise NonRetryableError("use a business ID and one to sixteen bounded items") |
| 31 | + try: |
| 32 | + valid = all(isinstance(value, str) and 1 <= len(value.encode()) <= 128 |
| 33 | + for value in [self.business_id, *self.items]) |
| 34 | + except UnicodeEncodeError: |
| 35 | + valid = False |
| 36 | + if not valid: |
| 37 | + raise NonRetryableError("use a business ID and one to sixteen bounded items") |
| 38 | + items = tuple(self.items) |
| 39 | + encoded = json.dumps([self.business_id, items], ensure_ascii=False, separators=(",", ":")).encode() |
| 40 | + return self.business_id, items, hashlib.sha256(encoded).hexdigest() |
| 41 | + |
| 42 | + |
| 43 | +@dataclass(frozen=True) |
| 44 | +class Progress: |
| 45 | + batch_sha256: str |
| 46 | + next_item: int |
| 47 | + |
| 48 | + |
| 49 | +@dataclass(frozen=True) |
| 50 | +class ReceiptStore: |
| 51 | + """Synthetic durable provider state, independent of Server checkpoints.""" |
| 52 | + |
| 53 | + root: Path |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def create(cls, root: Path) -> ReceiptStore: |
| 57 | + root.mkdir(mode=0o700) |
| 58 | + return cls(root) |
| 59 | + |
| 60 | + def sync_directory(self) -> None: |
| 61 | + descriptor = os.open(self.root, os.O_RDONLY | os.O_DIRECTORY) |
| 62 | + try: |
| 63 | + os.fsync(descriptor) |
| 64 | + finally: |
| 65 | + os.close(descriptor) |
| 66 | + |
| 67 | + def commit_or_reconcile(self, business_id: str, fingerprint: str, index: int, item: str) -> bool: |
| 68 | + key = json.dumps([business_id, index], ensure_ascii=False, separators=(",", ":")).encode() |
| 69 | + path = self.root / (hashlib.sha256(key).hexdigest() + ".json") |
| 70 | + receipt = json.dumps({"key": [business_id, index], "batch_sha256": fingerprint, "item": item}, |
| 71 | + ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode() |
| 72 | + try: |
| 73 | + with path.open("xb") as stream: |
| 74 | + stream.write(receipt) |
| 75 | + stream.flush() |
| 76 | + os.fsync(stream.fileno()) |
| 77 | + created = True |
| 78 | + except FileExistsError: |
| 79 | + with path.open("rb") as stream: |
| 80 | + if stream.read(4097) != receipt: |
| 81 | + raise NonRetryableError( |
| 82 | + "provider receipt conflicts or is partial; reconcile before retrying", |
| 83 | + ) from None |
| 84 | + os.fsync(stream.fileno()) |
| 85 | + created = False |
| 86 | + self.sync_directory() |
| 87 | + return created |
| 88 | + |
| 89 | + |
| 90 | +async def process_batch( |
| 91 | + store: ReceiptStore, batch: Batch, received: Progress | None, attempt: int, |
| 92 | + acknowledge: Callable[[Progress], Awaitable[None]], |
| 93 | +) -> dict[str, Any]: |
| 94 | + business_id, items, fingerprint = batch.snapshot() |
| 95 | + if type(attempt) is not int or attempt < 1: |
| 96 | + raise NonRetryableError("the activity attempt must be positive") |
| 97 | + if received is not None and ( |
| 98 | + received.batch_sha256 != fingerprint or type(received.next_item) is not int |
| 99 | + or not 0 <= received.next_item <= len(items) |
| 100 | + ): |
| 101 | + raise NonRetryableError("checkpoint belongs to different input or is out of range") |
| 102 | + start = received.next_item if received is not None else 0 |
| 103 | + def fail(point: str, index: int) -> None: |
| 104 | + if attempt == 1 and index == 0 and batch.failure == point: |
| 105 | + raise RuntimeError("planned first-attempt failure at " + point) |
| 106 | + |
| 107 | + for index in range(start, len(items)): |
| 108 | + fail("before-commit", index) |
| 109 | + await asyncio.to_thread(store.commit_or_reconcile, business_id, fingerprint, index, items[index]) |
| 110 | + fail("after-commit", index) |
| 111 | + await acknowledge(Progress(fingerprint, index + 1)) |
| 112 | + fail("after-checkpoint", index) |
| 113 | + return {"business_id": business_id, "completed": len(items)} |
| 114 | + |
| 115 | + |
| 116 | +@workflow.defn(name=ACTIVITY) |
| 117 | +class BatchWorkflow: |
| 118 | + def run(self, ctx: workflow.WorkflowContext, batch: dict[str, Any]) -> Generator[Any, Any, Any]: |
| 119 | + return (yield ctx.schedule_activity( |
| 120 | + ACTIVITY, [batch], retry_policy=ActivityRetryPolicy(max_attempts=2, backoff_seconds=[1]), |
| 121 | + heartbeat_timeout=10, start_to_close_timeout=30, schedule_to_close_timeout=60, |
| 122 | + )) |
| 123 | + |
| 124 | + |
| 125 | +async def run(root: Path, failure: str) -> None: |
| 126 | + store = await asyncio.to_thread(ReceiptStore.create, root) |
| 127 | + |
| 128 | + @activity.defn(name=ACTIVITY) |
| 129 | + async def execute(body: dict[str, Any]) -> dict[str, Any]: |
| 130 | + ctx = activity.context() |
| 131 | + try: |
| 132 | + received = ctx.last_heartbeat_details(result_type=Progress) |
| 133 | + except ActivityHeartbeatResumeUnavailable: |
| 134 | + raise NonRetryableError("this example requires activity_heartbeat_resume v1") from None |
| 135 | + |
| 136 | + async def acknowledge(progress: Progress) -> None: |
| 137 | + await ctx.heartbeat(asdict(progress)) |
| 138 | + |
| 139 | + return await process_batch(store, Batch(**body), received, ctx.info.attempt_number, acknowledge) |
| 140 | + |
| 141 | + queue = "checkpointed-batch-" + uuid4().hex |
| 142 | + async with Client(os.environ.get("SERVER_URL", "http://127.0.0.1:8080"), |
| 143 | + token=os.environ.get("WORKFLOW_TOKEN"), namespace="default", timeout=10) as client: |
| 144 | + worker = Worker(client, task_queue=queue, workflows=[BatchWorkflow], activities=[execute]) |
| 145 | + handle = await client.start_workflow( |
| 146 | + workflow_type=ACTIVITY, task_queue=queue, workflow_id=queue, |
| 147 | + input=[asdict(Batch("demo-" + uuid4().hex, ["first", "second", "third"], failure))], |
| 148 | + ) |
| 149 | + await worker.run_until(workflow_id=handle.workflow_id, timeout=70) |
| 150 | + print(json.dumps(await handle.result(timeout=3))) |
| 151 | + |
| 152 | + |
| 153 | +def main() -> None: |
| 154 | + parser = argparse.ArgumentParser(description=__doc__) |
| 155 | + parser.add_argument("receipts", type=Path, help="a new private synthetic-provider directory") |
| 156 | + parser.add_argument("failure", choices=FAILURES) |
| 157 | + args = parser.parse_args() |
| 158 | + asyncio.run(run(args.receipts, args.failure)) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + main() |
0 commit comments