From 171ecdc4c2125091a82193e92db860a66ef7bfb0 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Fri, 13 Nov 2026 17:50:24 -0700 Subject: [PATCH 01/35] Added the Stream Stores and the hooks of the generated Streams API. A client takes its Stream Stores in a `StreamConfig`, and each call of the generated Streams API goes to one of them. The Stores run Core's Stream runtime through the bridge. --- .github/actions/install-redis/action.yml | 25 + .github/workflows/ci.yml | 5 + changelog/added/chattering-ledger-otter.md | 1 + pyproject.toml | 17 +- scripts/gen_nexus_system_api.py | 80 +- scripts/gen_payload_visitor.py | 34 +- temporalio/bridge/Cargo.lock | 212 +++++- temporalio/bridge/Cargo.toml | 20 +- temporalio/bridge/sdk-core | 2 +- temporalio/bridge/src/lib.rs | 67 ++ temporalio/bridge/src/streams.rs | 130 ++++ temporalio/bridge/streams.py | 104 +++ temporalio/client/__init__.py | 12 + temporalio/client/_client.py | 24 +- temporalio/client/_impl.py | 51 ++ temporalio/client/_stream_appender.py | 193 +++++ temporalio/client/_stream_store.py | 554 ++++++++++++++ temporalio/contrib/redis/__init__.py | 1 + temporalio/contrib/redis/streams.py | 116 +++ temporalio/exceptions.py | 109 +++ temporalio/nexus/system/__init__.py | 38 +- temporalio/nexus/system/_support.py | 153 +++- temporalio/testing/__init__.py | 2 + temporalio/testing/_stream_store.py | 57 ++ temporalio/testing/_workflow.py | 12 + temporalio/worker/_interceptor.py | 11 +- temporalio/worker/_replayer.py | 12 + temporalio/worker/_stream_store.py | 121 ++++ temporalio/worker/_worker.py | 27 +- temporalio/worker/_workflow.py | 4 + temporalio/worker/_workflow_instance.py | 113 ++- tests/contrib/redis/__init__.py | 0 tests/contrib/redis/test_streams.py | 243 +++++++ tests/helpers/streams.py | 186 +++++ tests/nexus/test_stream_appender.py | 159 ++++ tests/nexus/test_stream_store.py | 630 ++++++++++++++++ tests/nexus/test_stream_store_workflow.py | 806 +++++++++++++++++++++ 37 files changed, 4247 insertions(+), 84 deletions(-) create mode 100644 .github/actions/install-redis/action.yml create mode 100644 changelog/added/chattering-ledger-otter.md create mode 100644 temporalio/bridge/src/streams.rs create mode 100644 temporalio/bridge/streams.py create mode 100644 temporalio/client/_stream_appender.py create mode 100644 temporalio/client/_stream_store.py create mode 100644 temporalio/contrib/redis/__init__.py create mode 100644 temporalio/contrib/redis/streams.py create mode 100644 temporalio/testing/_stream_store.py create mode 100644 temporalio/worker/_stream_store.py create mode 100644 tests/contrib/redis/__init__.py create mode 100644 tests/contrib/redis/test_streams.py create mode 100644 tests/helpers/streams.py create mode 100644 tests/nexus/test_stream_appender.py create mode 100644 tests/nexus/test_stream_store.py create mode 100644 tests/nexus/test_stream_store_workflow.py diff --git a/.github/actions/install-redis/action.yml b/.github/actions/install-redis/action.yml new file mode 100644 index 0000000000..de0a2d6390 --- /dev/null +++ b/.github/actions/install-redis/action.yml @@ -0,0 +1,25 @@ +name: Install Redis +description: Install and start a Redis server for the tests of the Redis Stream Store. +runs: + using: composite + steps: + - if: runner.os == 'Linux' + shell: bash + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends redis-server + - if: runner.os == 'macOS' + shell: bash + run: brew install redis + # Redis has no official Windows build, so the Redis Store tests skip there. The port is not the + # default one, since the Linux package starts its own server on 6379. + - if: runner.os != 'Windows' + shell: bash + run: | + redis-server --port 6380 --save "" --appendonly no --daemonize yes + for _ in $(seq 1 50); do + redis-cli -p 6380 ping && break + sleep 0.1 + done + redis-cli -p 6380 ping + echo "STREAMS_REDIS_URL=redis://127.0.0.1:6380" >> "$GITHUB_ENV" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9e3f5e2daa..c60bb33161 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,6 +58,8 @@ jobs: if: ${{ matrix.clippyLinter }} - run: poe build-develop - run: poe lint + - name: Install and start Redis for the Redis Stream Store tests + uses: ./.github/actions/install-redis - run: mkdir junit-xml - run: poe test ${{matrix.pytestExtraArgs}} -s --junit-xml=junit-xml/${{ matrix.python }}--${{ matrix.os }}.xml timeout-minutes: 20 @@ -226,6 +228,9 @@ jobs: - run: uv tool install poethepoet - run: uv sync --all-extras - run: cargo install --locked nexgen --version 0.2.8 --features advanced --force + # The Stream package needs the `main` of nexgen, which no release has yet. + - run: cargo install --locked --git https://github.com/temporalio/nexgen --rev 9653a0f5a845df0c5059554f1b5813db6a74cd1a --features advanced --root "$RUNNER_TEMP/nexgen-stream" nexgen + - run: echo "NEX_GEN_STREAM_BIN=$RUNNER_TEMP/nexgen-stream/bin/nexgen" >> "$GITHUB_ENV" - run: poe build-develop - run: poe gen-protos - name: Check generation unchanged diff --git a/changelog/added/chattering-ledger-otter.md b/changelog/added/chattering-ledger-otter.md new file mode 100644 index 0000000000..f15fdc478b --- /dev/null +++ b/changelog/added/chattering-ledger-otter.md @@ -0,0 +1 @@ +Add the experimental Streams API. Client and Activity code create a Stream, append Records to it, read them and close it, and Workflow code creates and describes one. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `temporalio.contrib.redis.streams.RedisStreamStore`, or `temporalio.testing.MemoryStreamStore` in tests. diff --git a/pyproject.toml b/pyproject.toml index 6c41cbba54..8c32f20eb7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -93,8 +93,8 @@ dev = [ ] [tool.poe.tasks] -build-develop = "uv run maturin develop --uv" -build-develop-with-release = { cmd = "uv run maturin develop --release --uv" } +build-develop = "uv run maturin develop --uv --features streams-redis" +build-develop-with-release = { cmd = "uv run maturin develop --release --uv --features streams-redis" } # FIPS builds: swap the rustls stack onto aws-lc-rs FIPS mode (aws-lc-fips-sys), eliminating `ring`. build-develop-fips = { cmd = "uv run maturin develop --uv --no-default-features --features fips", env = { TEMPORALIO_FIPS = "1" } } build-wheel-fips = { cmd = "uv run maturin build --release --no-default-features --features fips", env = { TEMPORALIO_FIPS = "1" } } @@ -153,18 +153,20 @@ filterwarnings = [ before-all = "pip install protoc-wheel-0" build = "cp311-win_amd64 cp311-manylinux_x86_64 cp311-manylinux_aarch64 cp311-musllinux_x86_64 cp311-musllinux_aarch64 cp311-macosx_x86_64 cp311-macosx_arm64" build-verbosity = 1 +# The official wheels link the Stream runtime. +environment = { MATURIN_PEP517_ARGS = "--features streams-redis" } [tool.cibuildwheel.macos] -environment = { MACOSX_DEPLOYMENT_TARGET = "10.12" } +environment = { MACOSX_DEPLOYMENT_TARGET = "10.12", MATURIN_PEP517_ARGS = "--features streams-redis" } [tool.cibuildwheel.linux] before-all = "sh scripts/cibuildwheel_before_all_linux.sh" before-build = "sh scripts/cibuildwheel_before_build_linux.sh" -environment = { PATH = "$PATH:$HOME/.cargo/bin", CARGO_NET_GIT_FETCH_WITH_CLI = "true" } +environment = { PATH = "$PATH:$HOME/.cargo/bin", CARGO_NET_GIT_FETCH_WITH_CLI = "true", MATURIN_PEP517_ARGS = "--features streams-redis" } [[tool.cibuildwheel.overrides]] select = "*musllinux*" -environment = { PATH = "$PATH:$HOME/.cargo/bin", CARGO_NET_GIT_FETCH_WITH_CLI = "true", RUSTFLAGS = "-C target-feature=-crt-static" } +environment = { PATH = "$PATH:$HOME/.cargo/bin", CARGO_NET_GIT_FETCH_WITH_CLI = "true", RUSTFLAGS = "-C target-feature=-crt-static", MATURIN_PEP517_ARGS = "--features streams-redis" } [tool.mypy] ignore_missing_imports = true @@ -173,12 +175,17 @@ exclude = [ 'temporalio/api', 'temporalio/bridge/proto', 'temporalio/nexus/system/workflow_service', + 'temporalio/nexus/system/stream_service', ] [[tool.mypy.overrides]] module = "temporalio.nexus.system.workflow_service.*" ignore_errors = true +[[tool.mypy.overrides]] +module = "temporalio.nexus.system.stream_service.*" +ignore_errors = true + [tool.pydocstyle] convention = "google" # https://github.com/PyCQA/pydocstyle/issues/363#issuecomment-625563088 diff --git a/scripts/gen_nexus_system_api.py b/scripts/gen_nexus_system_api.py index 3b16ce1bb3..48998fc7c8 100644 --- a/scripts/gen_nexus_system_api.py +++ b/scripts/gen_nexus_system_api.py @@ -23,9 +23,14 @@ ) wit_deps_dir = wit_input_dir / "deps" workflow_output_dir = base_dir / "temporalio" / "nexus" / "system" / "workflow_service" +stream_output_dir = base_dir / "temporalio" / "nexus" / "system" / "stream_service" notification_output_dir = base_dir / "temporalio" / "nexus" / "notifications" workflow_init_path = base_dir / "temporalio" / "workflow" / "__init__.py" NEX_GEN_VERSION = "0.2.8" +# The Stream package needs a newer nexgen, which rejects the other WITs. One pin is enough +# again once the api WITs and a nexgen release agree. No release has nexgen commit 9653a0f5, +# the `main` of nexgen, yet, so NEX_GEN_STREAM_BIN points at a build of it. +NEX_GEN_STREAM_VERSION = "0.2.12" proto_files = [ gen_protos.api_proto_dir / "temporal" @@ -36,10 +41,22 @@ gen_protos.api_proto_dir / "temporal" / "api" + / "streamservice" + / "v1" + / "request_response.proto", + gen_protos.api_proto_dir + / "temporal" + / "api" / "notificationservice" / "v1" / "request_response.proto", ] +# The Workflow functions of these generated packages are exported by temporalio.workflow. +# They are in import order, so that sorting the imports keeps them between the markers. +workflow_export_packages = [ + ("temporalio.nexus.system.stream_service.workflow", stream_output_dir / "workflow"), + ("temporalio.nexus.system.workflow_service", workflow_output_dir), +] def nex_gen_command() -> list[str]: @@ -66,6 +83,20 @@ def nex_gen_command() -> list[str]: return ["nexgen"] +def nex_gen_stream_command() -> list[str]: + bin_path = os.environ.get("NEX_GEN_STREAM_BIN") + if not bin_path: + raise RuntimeError( + "NEX_GEN_STREAM_BIN must point at a nexgen build of commit 9653a0f5" + ) + version = subprocess.check_output([bin_path, "--version"], text=True).strip() + if version != f"nexgen {NEX_GEN_STREAM_VERSION}": + raise RuntimeError( + f"NEX_GEN_STREAM_BIN is {version}, not nexgen {NEX_GEN_STREAM_VERSION}" + ) + return [bin_path] + + def build_descriptor_set(descriptor_path: Path) -> None: subprocess.check_call( [ @@ -85,14 +116,15 @@ def generate_package( output_dir: Path, descriptor_path: Path, *, - native_api: bool, + flags: list[str], ) -> None: - args = [*command, "python", str(wit_input_dir / wit_name), str(wit_deps_dir)] - if native_api: - args.append("--native-api") subprocess.check_call( [ - *args, + *command, + "python", + str(wit_input_dir / wit_name), + str(wit_deps_dir), + *flags, "--system-nexus", "--support-package", "temporalio.nexus.system._support", @@ -132,17 +164,26 @@ def replace_marker_block( def generate_workflow_exports() -> None: - exports = generated_exports(workflow_output_dir / "__init__.py") + package_exports = [ + (module, generated_exports(package_dir / "__init__.py")) + for module, package_dir in workflow_export_packages + ] import_block = [ "# BEGIN GENERATED NEXUS SYSTEM EXPORTS\n", - "from temporalio.nexus.system.workflow_service import (\n", - *[f" {export},\n" for export in exports], - ")\n", + *[ + line + for module, exports in package_exports + for line in [ + f"from {module} import (\n", + *[f" {export},\n" for export in exports], + ")\n", + ] + ], "# END GENERATED NEXUS SYSTEM EXPORTS\n", ] all_block = [ " # BEGIN GENERATED NEXUS SYSTEM __ALL__\n", - *[f' "{export}",\n' for export in exports], + *[f' "{export}",\n' for _, exports in package_exports for export in exports], " # END GENERATED NEXUS SYSTEM __ALL__\n", ] content = workflow_init_path.read_text() @@ -162,6 +203,7 @@ def generate_workflow_exports() -> None: def generate_nexus_system_api() -> None: required_paths = [ wit_input_dir / "workflow-service.wit", + wit_input_dir / "stream-service.wit", wit_input_dir / "notification-service.wit", wit_deps_dir, *proto_files, @@ -173,23 +215,31 @@ def generate_nexus_system_api() -> None: descriptor_path = Path(temp_dir) / "temporal_api.bin" build_descriptor_set(descriptor_path) command = nex_gen_command() - for wit_name, output_dir, native_api in ( - ("workflow-service.wit", workflow_output_dir, True), - ("notification-service.wit", notification_output_dir, False), + stream_command = nex_gen_stream_command() + for wit_name, output_dir, package_command, flags in ( + ("workflow-service.wit", workflow_output_dir, command, ["--native-api"]), + ( + "stream-service.wit", + stream_output_dir, + stream_command, + ["--targets", "workflow,client"], + ), + ("notification-service.wit", notification_output_dir, command, []), ): shutil.rmtree(output_dir, ignore_errors=True) output_dir.parent.mkdir(parents=True, exist_ok=True) generate_package( - command, + package_command, wit_name, output_dir, descriptor_path, - native_api=native_api, + flags=flags, ) workflow_output_dir.parent.joinpath("__init__.py").touch() generate_workflow_exports() format_paths = [ str(workflow_output_dir), + str(stream_output_dir), str(notification_output_dir), str(workflow_init_path), ] diff --git a/scripts/gen_payload_visitor.py b/scripts/gen_payload_visitor.py index ba5ddc112b..eb41d72864 100644 --- a/scripts/gen_payload_visitor.py +++ b/scripts/gen_payload_visitor.py @@ -24,20 +24,30 @@ def discover_system_nexus_roots() -> list[Descriptor]: - module_path = base_dir / "temporalio" / "nexus" / "system" / "workflow_service" - spec = spec_from_file_location( - "temporalio_nexus_system_workflow_service", - module_path / "__init__.py", - submodule_search_locations=[str(module_path)], - ) - if spec is None or spec.loader is None: - raise RuntimeError(f"Cannot load generated system service from {module_path}") - module = module_from_spec(spec) - sys.modules[spec.name] = module - spec.loader.exec_module(module) + services: list[type] = [] + system_path = base_dir / "temporalio" / "nexus" / "system" + for module_path in sorted(p.parent for p in system_path.glob("*/services.py")): + spec = spec_from_file_location( + f"temporalio_nexus_system_{module_path.name}", + module_path / "__init__.py", + submodule_search_locations=[str(module_path)], + ) + if spec is None or spec.loader is None: + raise RuntimeError( + f"Cannot load generated system service from {module_path}" + ) + module = module_from_spec(spec) + sys.modules[spec.name] = module + spec.loader.exec_module(module) + services.extend( + value + for value in vars(module.services).values() + if isinstance(value, type) and nexusrpc.get_service_definition(value) + ) + annotations = [a for service in services for a in get_type_hints(service).values()] roots: list[Descriptor] = [] - for annotation in get_type_hints(module._services.WorkflowService).values(): + for annotation in annotations: if get_origin(annotation) is not nexusrpc.Operation: continue for operation_type in get_args(annotation): diff --git a/temporalio/bridge/Cargo.lock b/temporalio/bridge/Cargo.lock index 3285c012ca..850fbaa8d1 100644 --- a/temporalio/bridge/Cargo.lock +++ b/temporalio/bridge/Cargo.lock @@ -29,6 +29,32 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "arc-swap" +version = "1.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b5d51209f6de83d37a0b33d320acb8da88823e42a94c075140708f032a5" +dependencies = [ + "rustversion", +] + +[[package]] +name = "arcstr" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03918c3dbd7701a85c6b9887732e2921175f26c350b4563841d0958c21d57e6d" + +[[package]] +name = "async-lock" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0876700b0d007ad25d5508865a4259c3af9af6d296bf663a0d586d0bc3369f6f" +dependencies = [ + "event-listener", + "event-listener-strategy", + "pin-project-lite", +] + [[package]] name = "async-trait" version = "0.1.92" @@ -181,6 +207,15 @@ version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bon" version = "3.10.3" @@ -265,7 +300,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ "cfg-if", - "cpufeatures", + "cpufeatures 0.3.1", "rand_core 0.10.1", ] @@ -306,7 +341,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4356eba6976435498d05a311ee9fc486da3bc81c200d3f0d7dd9703b41913f92" dependencies = [ "bytes", + "futures-core", "memchr", + "pin-project-lite", + "tokio", + "tokio-util", ] [[package]] @@ -343,6 +382,15 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "cpufeatures" version = "0.3.1" @@ -352,6 +400,12 @@ dependencies = [ "libc", ] +[[package]] +name = "crc16" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "338089f42c427b86394a5ee60ff321da23a5c89c9d89514c829687b26359fcff" + [[package]] name = "crc32fast" version = "1.5.5" @@ -376,6 +430,16 @@ version = "0.8.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "darling" version = "0.24.1" @@ -433,6 +497,16 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "dirs" version = "7.0.0" @@ -548,6 +622,26 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "event-listener" +version = "5.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a23add41df1562121a9393cb065eab5146a1242410f23a644851e90cfd669d2" +dependencies = [ + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be9f3dfaaffdae2972880079a491a1a8bb7cbed0b8dd7a347f668b4150a3b93" +dependencies = [ + "event-listener", + "pin-project-lite", +] + [[package]] name = "fastrand" version = "2.5.0" @@ -728,6 +822,16 @@ dependencies = [ "slab", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "gethostname" version = "1.1.0" @@ -1501,6 +1605,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + [[package]] name = "parking_lot" version = "0.12.5" @@ -2041,6 +2151,39 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "redis" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db7f3d75b5a3301746bff39a767bf4c5c42506b08513527308b3875bfb64a031" +dependencies = [ + "arc-swap", + "arcstr", + "async-lock", + "backon", + "bytes", + "cfg-if", + "combine", + "crc16", + "futures-channel", + "futures-util", + "itoa", + "log", + "percent-encoding", + "pin-project-lite", + "rand 0.10.3", + "rustls", + "rustls-native-certs", + "ryu", + "sha1_smol", + "socket2", + "tokio", + "tokio-rustls", + "tokio-util", + "url", + "xxhash-rust", +] + [[package]] name = "redox_syscall" version = "0.5.18" @@ -2390,6 +2533,23 @@ dependencies = [ "serde", ] +[[package]] +name = "sha1_smol" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d" + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -2593,6 +2753,8 @@ dependencies = [ "temporalio-client", "temporalio-common", "temporalio-sdk-core", + "temporalio-streams", + "temporalio-streams-redis", "tokio", "tokio-rustls", "tokio-stream", @@ -2776,6 +2938,42 @@ dependencies = [ "zip", ] +[[package]] +name = "temporalio-streams" +version = "0.1.0" +dependencies = [ + "async-trait", + "parking_lot", + "prost", + "prost-wkt-types", + "sha2", + "temporalio-common", + "thiserror", + "tokio", + "tonic", +] + +[[package]] +name = "temporalio-streams-redis" +version = "0.1.0" +source = "git+https://github.com/temporal-community/sdk-streams?rev=63799c4fd9431a9f12bed78fdff66f144f1e0ac4#63799c4fd9431a9f12bed78fdff66f144f1e0ac4" +dependencies = [ + "async-trait", + "aws-lc-rs", + "crc16", + "parking_lot", + "prost", + "prost-wkt-types", + "redis", + "temporalio-common", + "temporalio-streams", + "thiserror", + "tokio", + "tonic-prost-build", + "tracing", + "url", +] + [[package]] name = "termtree" version = "0.5.1" @@ -3141,6 +3339,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c" +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + [[package]] name = "typetag" version = "0.2.23" @@ -3631,6 +3835,12 @@ dependencies = [ "rustix", ] +[[package]] +name = "xxhash-rust" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "550a2b930b62486a393c52d5c3b84bff264b28aa437ed64694d31e93b1757af7" + [[package]] name = "yoke" version = "0.8.4" diff --git a/temporalio/bridge/Cargo.toml b/temporalio/bridge/Cargo.toml index 4b23ae6ce9..a49caf5eee 100644 --- a/temporalio/bridge/Cargo.toml +++ b/temporalio/bridge/Cargo.toml @@ -29,14 +29,21 @@ default = ["tls-ring"] tls-ring = [ "temporalio-sdk-core/tls-ring", "temporalio-client/tls-ring", + "temporalio-streams-redis?/tls-ring", "tokio-rustls/ring", ] tls-aws-lc = [ "temporalio-sdk-core/tls-aws-lc", "temporalio-client/tls-aws-lc", + "temporalio-streams-redis?/tls-aws-lc", "tokio-rustls/aws_lc_rs", ] -fips = ["tls-aws-lc", "dep:aws-lc-rs"] +fips = ["tls-aws-lc", "dep:aws-lc-rs", "streams-redis", "temporalio-streams-redis/fips"] +# Links the Stream runtime and its memory Store. Off by default. +streams = ["dep:temporalio-streams"] +# Adds the Redis Store and turns on `streams`. The official builds turn it on. Cargo fetches the +# git dependency below for every build, with this feature off too. +streams-redis = ["streams", "dep:temporalio-streams-redis"] [dependencies] anyhow = "1.0" @@ -72,6 +79,10 @@ temporalio-sdk-core = { version = "0.11.0", path = "./sdk-core/crates/sdk-core", "envconfig", "prometheus", ] } +# The Stream runtime and its memory Store, from the submodule. +temporalio-streams = { path = "./sdk-core/crates/streams", optional = true } +# The Redis Store, which lives in its own repo. +temporalio-streams-redis = { git = "https://github.com/temporal-community/sdk-streams", rev = "63799c4fd9431a9f12bed78fdff66f144f1e0ac4", default-features = false, optional = true } tokio = "1.26" # Matches the sdk-core client crate's rustls stack; used to build the custom # server certificate verifier for ClientTlsConfig.verification_server_name. @@ -84,6 +95,13 @@ tonic = "0.14" tracing = "0.1" url = "2.2" +# The Redis crate takes temporalio-common and temporalio-streams from crates.io. This makes it use +# the submodule's crates, so the bridge sees one copy of each type. The versions must be semver +# compatible. +[patch.crates-io] +temporalio-common = { path = "./sdk-core/crates/common" } +temporalio-streams = { path = "./sdk-core/crates/streams" } + [profile.release] opt-level = 3 debug = false diff --git a/temporalio/bridge/sdk-core b/temporalio/bridge/sdk-core index c675c32469..9a110b3cda 160000 --- a/temporalio/bridge/sdk-core +++ b/temporalio/bridge/sdk-core @@ -1 +1 @@ -Subproject commit c675c32469af707e1c979a7b808c70b4a0608b82 +Subproject commit 9a110b3cda949199b7792348ff5d265b1d1c414a diff --git a/temporalio/bridge/src/lib.rs b/temporalio/bridge/src/lib.rs index a4fda26db6..1eb7b0be62 100644 --- a/temporalio/bridge/src/lib.rs +++ b/temporalio/bridge/src/lib.rs @@ -6,6 +6,7 @@ mod client_rpc_generated; mod envconfig; mod metric; mod runtime; +mod streams; mod testing; mod worker; @@ -48,6 +49,16 @@ fn temporal_sdk_bridge(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(init_runtime, m)?)?; m.add_function(wrap_pyfunction!(raise_in_thread, m)?)?; + // Stream stuff + m.add("StreamCallError", py.get_type::())?; + #[cfg(feature = "streams")] + { + m.add_class::()?; + m.add_function(wrap_pyfunction!(new_memory_stream_runtime, m)?)?; + #[cfg(feature = "streams-redis")] + m.add_function(wrap_pyfunction!(new_redis_stream_runtime, m)?)?; + } + // Testing stuff m.add_class::()?; m.add_function(wrap_pyfunction!(start_dev_server, m)?)?; @@ -104,6 +115,62 @@ fn raise_in_thread(py: Python, thread_id: std::os::raw::c_long, exc: &Bound<'_, runtime::raise_in_thread(py, thread_id, exc) } +#[cfg(feature = "streams")] +#[pyfunction] +#[pyo3(signature = (runtime_ref, store_name, retention=None, last_write_ttl=None))] +fn new_memory_stream_runtime( + runtime_ref: &runtime::RuntimeRef, + store_name: String, + retention: Option, + last_write_ttl: Option, +) -> PyResult { + streams::new_memory_stream_runtime(runtime_ref, store_name, retention, last_write_ttl) +} + +#[cfg(feature = "streams-redis")] +#[pyfunction] +#[pyo3(signature = ( + runtime_ref, + urls, + *, + cluster, + key_prefix, + retention, + last_write_ttl, + response_timeout, + store_name, + wait_replicas, + wait_timeout, +))] +#[allow(clippy::too_many_arguments)] +fn new_redis_stream_runtime( + runtime_ref: &runtime::RuntimeRef, + urls: Vec, + cluster: bool, + key_prefix: Option, + retention: Option, + last_write_ttl: Option, + response_timeout: Option, + store_name: String, + wait_replicas: i32, + wait_timeout: Option, +) -> PyResult { + streams::new_redis_stream_runtime( + runtime_ref, + streams::RedisStreamOptions { + urls, + cluster, + key_prefix, + retention, + last_write_ttl, + response_timeout, + store_name, + wait_replicas, + wait_timeout, + }, + ) +} + #[pyfunction] fn start_dev_server<'a>( py: Python<'a>, diff --git a/temporalio/bridge/src/streams.rs b/temporalio/bridge/src/streams.rs new file mode 100644 index 0000000000..0f9218523c --- /dev/null +++ b/temporalio/bridge/src/streams.rs @@ -0,0 +1,130 @@ +//! Bindings to the Stream runtime of the `temporalio-streams` crate, on the memory Store or, with +//! the `streams-redis` feature, on the Redis Store of `temporalio-streams-redis`. Python calls the +//! five Stream service operations by name, with the encoded request, and gets the encoded response. + +use pyo3::exceptions::PyException; +#[cfg(feature = "streams")] +use pyo3::prelude::*; + +#[cfg(feature = "streams")] +use crate::runtime; + +// The exception args are the gRPC code, the message and the encoded details, as in `RPCError`. +pyo3::create_exception!(temporal_sdk_bridge, StreamCallError, PyException); + +/// The crate's runtime object. Each Store on the bridge holds one. +#[cfg(feature = "streams")] +#[pyclass(frozen)] +pub struct StreamRuntimeRef { + streams: std::sync::Arc, + runtime: runtime::Runtime, +} + +#[cfg(feature = "streams")] +#[pymethods] +impl StreamRuntimeRef { + /// Runs one Stream operation on an encoded request. The future gives the encoded response. + fn call<'p>( + &self, + py: Python<'p>, + operation: String, + request: Vec, + ) -> PyResult> { + self.runtime + .assert_same_process("call the Stream runtime")?; + let streams = self.streams.clone(); + self.runtime.future_into_py(py, async move { + streams.call(&operation, &request).await.map_err(call_error) + }) + } +} + +#[cfg(feature = "streams")] +fn call_error(err: temporalio_streams::StreamError) -> PyErr { + StreamCallError::new_err((err.code() as u32, err.message.clone(), err.details())) +} + +/// Builds the runtime on a new memory Store. A `None` duration takes the crate's default. +#[cfg(feature = "streams")] +pub fn new_memory_stream_runtime( + runtime_ref: &runtime::RuntimeRef, + store_name: String, + retention: Option, + last_write_ttl: Option, +) -> PyResult { + let runtime = runtime_ref.runtime.clone(); + runtime.assert_same_process("create a Stream runtime")?; + // The runtime can spawn Tokio tasks, which need an entered Tokio runtime. + let _guard = runtime.core.tokio_handle().enter(); + let meter = runtime.core.telemetry().get_temporal_metric_meter(); + let streams = temporalio_streams::MemoryStreamRuntime::new( + store_name, + temporalio_streams::MemoryStreamOptions { + retention, + last_write_ttl, + }, + meter, + ); + Ok(StreamRuntimeRef { streams, runtime }) +} + +/// The options of the Redis Store, as Python passes them. A `None` takes the crate's default. +#[cfg(feature = "streams-redis")] +pub struct RedisStreamOptions { + pub urls: Vec, + pub cluster: bool, + pub key_prefix: Option, + pub retention: Option, + pub last_write_ttl: Option, + pub response_timeout: Option, + pub store_name: String, + pub wait_replicas: i32, + pub wait_timeout: Option, +} + +/// Builds the runtime on Redis. It connects at its first call, so a server that is down fails that +/// call and not this one. A bad option raises `ValueError` with the crate's message. +#[cfg(feature = "streams-redis")] +pub fn new_redis_stream_runtime( + runtime_ref: &runtime::RuntimeRef, + options: RedisStreamOptions, +) -> PyResult { + use pyo3::exceptions::PyValueError; + use temporalio_streams_redis::{proto::RedisStreamConfig, RedisStreamRuntime, RuntimeOptions}; + + let runtime = runtime_ref.runtime.clone(); + runtime.assert_same_process("create a Stream runtime")?; + // The crate reads an empty or zero field as its default, so `None` maps to that. + let config = RedisStreamConfig { + urls: options.urls, + cluster: options.cluster, + key_prefix: options.key_prefix.unwrap_or_default(), + retention: proto_duration("retention", options.retention)?, + last_write_ttl: proto_duration("last_write_ttl", options.last_write_ttl)?, + response_timeout: proto_duration("response_timeout", options.response_timeout)?, + store_name: options.store_name, + wait_replicas: options.wait_replicas, + wait_timeout: proto_duration("wait_timeout", options.wait_timeout)?, + ..Default::default() + }; + let options = + RuntimeOptions::try_from(config).map_err(|err| PyValueError::new_err(err.to_string()))?; + // The runtime can spawn Tokio tasks, which need an entered Tokio runtime. + let _guard = runtime.core.tokio_handle().enter(); + let meter = runtime.core.telemetry().get_temporal_metric_meter(); + let streams = RedisStreamRuntime::create(options, meter); + Ok(StreamRuntimeRef { streams, runtime }) +} + +// The config's durations are the `prost-wkt-types` type that the Redis crate's protos use. The +// bridge doesn't depend on that crate, so the field's type picks the conversion. +#[cfg(feature = "streams-redis")] +fn proto_duration(field: &str, value: Option) -> PyResult> +where + T: TryFrom, + T::Error: std::fmt::Display, +{ + value.map(T::try_from).transpose().map_err(|err| { + pyo3::exceptions::PyValueError::new_err(format!("The {field} is too long: {err}")) + }) +} diff --git a/temporalio/bridge/streams.py b/temporalio/bridge/streams.py new file mode 100644 index 0000000000..b189517c25 --- /dev/null +++ b/temporalio/bridge/streams.py @@ -0,0 +1,104 @@ +"""The Stream runtime that this bridge build links. (unstable) + +Nothing in this module should be considered stable. The API may change. +""" + +from __future__ import annotations + +from collections.abc import Sequence +from datetime import timedelta +from typing import Any + +import temporalio.bridge.runtime +import temporalio.bridge.temporal_sdk_bridge +import temporalio.exceptions + +StreamCallError: type[Exception] = temporalio.bridge.temporal_sdk_bridge.StreamCallError + + +class StreamRuntime: + """The runtime of the ``temporalio-streams`` crate on one Store.""" + + @staticmethod + def new_memory( + runtime: temporalio.bridge.runtime.Runtime, + *, + name: str, + retention: timedelta | None, + last_write_ttl: timedelta | None, + ) -> StreamRuntime: + """Build the runtime on a new memory Store. + + Raises: + temporalio.exceptions.StreamConfigError: The bridge build has no Stream + runtime. + """ + new = getattr( + temporalio.bridge.temporal_sdk_bridge, "new_memory_stream_runtime", None + ) + if new is None: + raise temporalio.exceptions.StreamConfigError( + "This build of temporalio has no Stream runtime" + ) + return StreamRuntime(new(runtime._ref, name, retention, last_write_ttl)) + + @staticmethod + def new_redis( + runtime: temporalio.bridge.runtime.Runtime, + *, + urls: Sequence[str], + cluster: bool, + key_prefix: str | None, + retention: timedelta | None, + last_write_ttl: timedelta | None, + response_timeout: timedelta | None, + name: str, + wait_replicas: int, + wait_timeout: timedelta | None, + ) -> StreamRuntime: + """Build the runtime on Redis. It connects at its first call. + + Raises: + temporalio.exceptions.StreamConfigError: The bridge build has no Redis + Stream Store. + ValueError: An option is bad. + """ + new = getattr( + temporalio.bridge.temporal_sdk_bridge, "new_redis_stream_runtime", None + ) + if new is None: + raise temporalio.exceptions.StreamConfigError( + "This build of temporalio has no Redis Stream Store" + ) + return StreamRuntime( + new( + runtime._ref, + list(urls), + cluster=cluster, + key_prefix=key_prefix, + retention=retention, + last_write_ttl=last_write_ttl, + response_timeout=response_timeout, + store_name=name, + wait_replicas=wait_replicas, + wait_timeout=wait_timeout, + ) + ) + + def __init__(self, ref: Any) -> None: + """Wrap the bridge's runtime object. Use ``new_memory`` or ``new_redis`` instead.""" + self._ref = ref + + async def call(self, operation: str, request: bytes) -> bytes: + """Run one Stream operation on an encoded request and return the encoded response. + + Raises: + temporalio.exceptions.StreamError: The call failed. + """ + try: + return await self._ref.call(operation, request) + except StreamCallError as err: + _, message, details = err.args + raise temporalio.exceptions._stream_error_from_call( + message, details + ) from None diff --git a/temporalio/client/__init__.py b/temporalio/client/__init__.py index 2eef41a39d..7305b4be59 100644 --- a/temporalio/client/__init__.py +++ b/temporalio/client/__init__.py @@ -185,6 +185,13 @@ ScheduleUpdate, ScheduleUpdateInput, ) +from ._stream_appender import BufferedAppender +from ._stream_store import ( + StreamConfig, + StreamSelectContext, + StreamStore, + StreamStoreCallOptions, +) from ._worker_versioning import ( BuildIdOp, BuildIdOpAddNewCompatible, @@ -350,6 +357,11 @@ "TaskReachabilityType", "CloudOperationsClient", "Plugin", + "BufferedAppender", + "StreamConfig", + "StreamSelectContext", + "StreamStore", + "StreamStoreCallOptions", "Callback", "_ClientImpl", "_apply_headers", diff --git a/temporalio/client/_client.py b/temporalio/client/_client.py index efef5cb528..d42d7d9b0e 100644 --- a/temporalio/client/_client.py +++ b/temporalio/client/_client.py @@ -25,6 +25,7 @@ import temporalio.api.workflowservice.v1 import temporalio.common import temporalio.converter +import temporalio.nexus.system.stream_service.client import temporalio.runtime import temporalio.service import temporalio.workflow @@ -94,6 +95,7 @@ ScheduleBackfill, ScheduleHandle, ) +from ._stream_store import StreamConfig, _refuse_store_plugins from ._worker_versioning import ( BuildIdOp, TaskReachabilityType, @@ -114,7 +116,9 @@ from ._plugin import Plugin -class Client: +class Client( + temporalio.nexus.system.stream_service.client.StreamServiceClientExtension +): """Client for accessing Temporal. Most users will use :py:meth:`connect` to create a client. The @@ -156,6 +160,7 @@ async def connect( grpc_compression: GrpcCompression = GrpcCompression.GZIP, payload_limits: PayloadLimitsConfig = PayloadLimitsConfig(), header_codec_behavior: HeaderCodecBehavior = HeaderCodecBehavior.NO_CODEC, + stream_config: StreamConfig | None = None, ) -> Self: """Connect to a Temporal server. @@ -225,7 +230,18 @@ async def connect( payload_limits: Warning thresholds for outbound payload/memo sizes. Over-threshold fields are logged but still sent. Set a threshold to 0 to disable it. header_codec_behavior: Encoding behavior for headers sent by the client. + stream_config: The Stream Stores of this client and the selector that + picks one for a new Stream, as + :py:class:`temporalio.converter.ExternalStorage` holds drivers and a + selector. A client with it sends each Stream operation to one of its + Stores. This release has no Streams on the server, so with ``None`` a + Stream operation raises + :py:class:`temporalio.exceptions.StreamConfigError`. + + .. warning:: + This API is experimental and subject to change. """ + _refuse_store_plugins(plugins) connect_config = temporalio.service.ConnectConfig( target_host=target_host, api_key=api_key, @@ -262,6 +278,7 @@ def make_lambda( default_workflow_query_reject_condition=default_workflow_query_reject_condition, header_codec_behavior=header_codec_behavior, plugins=plugins, + stream_config=stream_config, ) def __init__( @@ -275,11 +292,13 @@ def __init__( default_workflow_query_reject_condition: None | (temporalio.common.QueryRejectCondition) = None, header_codec_behavior: HeaderCodecBehavior = HeaderCodecBehavior.NO_CODEC, + stream_config: StreamConfig | None = None, ): """Create a Temporal client from a service client. See :py:meth:`connect` for details on the parameters. """ + _refuse_store_plugins(plugins) # Store the config for tracking config = ClientConfig( service_client=service_client, @@ -289,6 +308,7 @@ def __init__( interceptors=interceptors, default_workflow_query_reject_condition=default_workflow_query_reject_condition, header_codec_behavior=header_codec_behavior, + stream_config=stream_config, ) self._initial_config = config.copy() @@ -3029,6 +3049,7 @@ class ClientConnectConfig(TypedDict, total=False): grpc_compression: GrpcCompression payload_limits: PayloadLimitsConfig header_codec_behavior: HeaderCodecBehavior + stream_config: StreamConfig | None class ClientConfig(TypedDict, total=False): @@ -3043,3 +3064,4 @@ class ClientConfig(TypedDict, total=False): temporalio.common.QueryRejectCondition | None ] header_codec_behavior: Required[HeaderCodecBehavior] + stream_config: Required[StreamConfig | None] diff --git a/temporalio/client/_impl.py b/temporalio/client/_impl.py index 72986ffa70..4766126ae8 100644 --- a/temporalio/client/_impl.py +++ b/temporalio/client/_impl.py @@ -33,6 +33,7 @@ import temporalio.exceptions import temporalio.nexus import temporalio.nexus._operation_context +import temporalio.nexus.system from temporalio.activity import ActivityCancellationDetails from temporalio.converter import ( ActivitySerializationContext, @@ -125,6 +126,13 @@ ScheduleUpdate, ScheduleUpdateInput, ) +from ._stream_store import ( + _STREAM_SERVICE, + StreamConfig, + _activity_target, + _call_stream_store, + _request_ref, +) from ._worker_versioning import WorkerBuildIdVersionSets, WorkerTaskReachability from ._workflow import ( WorkflowExecutionAsyncIterator, @@ -1565,6 +1573,18 @@ async def start_nexus_operation( self, input: StartNexusOperationInput ) -> NexusOperationHandle[Any]: """Start a nexus operation and return a handle to it.""" + if ( + temporalio.nexus.system.is_system_endpoint(input.endpoint) + and input.service == _STREAM_SERVICE.name + ): + # This release has no Streams on the server, so a Stream call needs a Store. + stream_config = self._client._config.get("stream_config") + if stream_config is None: + raise temporalio.exceptions.StreamConfigError( + "This client has no Stream Store. Give one in " + "stream_config=StreamConfig(stores=[...])." + ) + return await self._start_stream_store_operation(stream_config, input) nexus_context = temporalio.converter.NexusSerializationContext( endpoint=input.endpoint, service=input.service, @@ -1648,6 +1668,37 @@ async def start_nexus_operation( operation=input.operation, ) + async def _start_stream_store_operation( + self, stream_config: StreamConfig, input: StartNexusOperationInput + ) -> NexusOperationHandle[Any]: + data_converter = self._client.data_converter + serialization_context = temporalio.nexus.system._get_serialization_context( + input.service, input.operation, input.arg + ) + if serialization_context is not None: + data_converter = data_converter.with_context(serialization_context) + payload_converter = temporalio.nexus.system._get_payload_converter( + data_converter.payload_converter, data_converter.failure_converter + ) + # The generated model converts to its proto message with no payload in between. + request = payload_converter._to_transfer_value(input.arg) + store = stream_config._store_for(_request_ref(input.arg), _activity_target()) + response = await _call_stream_store( + store, data_converter, input.operation, request + ) + handle: NexusOperationHandle[Any] = NexusOperationHandle( + self._client, + input.id, + result_type=input.result_type, + endpoint=input.endpoint, + service=input.service, + operation=input.operation, + ) + handle._known_outcome = payload_converter._from_transfer_value( + response, input.result_type + ) + return handle + async def describe_nexus_operation( self, input: DescribeNexusOperationInput ) -> NexusOperationExecutionDescription: diff --git a/temporalio/client/_stream_appender.py b/temporalio/client/_stream_appender.py new file mode 100644 index 0000000000..ec8d3d49d3 --- /dev/null +++ b/temporalio/client/_stream_appender.py @@ -0,0 +1,193 @@ +"""A helper that batches the appends of one Stream.""" + +from __future__ import annotations + +import asyncio +from datetime import timedelta +from types import TracebackType +from typing import Generic, TypeVar + +from typing_extensions import Self + +import temporalio.nexus.system.stream_service.client + +from ._stream_store import _check_not_negative + +BodyT = TypeVar("BodyT") + +# The Store refuses an append of more Records than this. Many small Records fit under its byte +# limit, but they keep the Store busy for long. +_MAX_BATCH_RECORDS = 1000 + + +class BufferedAppender(Generic[BodyT]): + """Batches the appends of one Stream. + + Use it in an Activity or a client that appends often, such as one that streams model + tokens. :py:meth:`append` returns at once and does no I/O. The appender writes the + buffered Records as one batch at most ``max_delay`` after the first of them, or at once + when they fill a batch of 1,000 Records. One write is in flight at a time. Records + appended during a write wait for the next batch, and they keep their order. + + Use it in ``async with``, which writes the remaining Records at the end of the block, or + await :py:meth:`flush`. Without ``async with`` or a :py:meth:`flush`, the Records still in + the buffer are lost. The end of an Activity attempt doesn't write them. + + ``await flush()`` is the backpressure point. A caller that appends faster than the Store + accepts Records must await it from time to time, so the buffer doesn't grow without bound. + + After a batch fails, the appender writes nothing more, so a reader sees no gap. Each later + :py:meth:`append` and :py:meth:`flush` raises the error of that batch. + + The appender doesn't count bytes, so a batch over the size limit of the Store fails with + the Store's error. + + It takes a client Stream, so Workflow code has no use for it. + + .. warning:: + This API is experimental and subject to change. + """ + + def __init__( + self, + stream: temporalio.nexus.system.stream_service.client.Stream[BodyT], + *, + max_delay: timedelta = timedelta(milliseconds=100), + ) -> None: + """Create an appender. It starts no write until the first append. + + Args: + stream: The Stream to append to. + max_delay: The longest time that a Record waits in the buffer before its batch + starts to write. It must not be negative. + """ + _check_not_negative(max_delay=max_delay) + self._stream = stream + self._max_delay = max_delay + self._buffer: list[BodyT] = [] + # When the oldest buffered Record arrived, so its batch starts at most max_delay later. + self._first_at = 0.0 + self._timer: asyncio.TimerHandle | None = None + self._writing: asyncio.Task[None] | None = None + self._appended = 0 + # Count of the Records that a write took or that a failed batch left, written or not. + self._settled = 0 + self._error: BaseException | None = None + self._last_offset: bytes | None = None + self._unwritten = 0 + + def append(self, *records: BodyT) -> None: + """Add one Record for each value to the buffer, and return at once. + + A list is one value. + + Raises: + TypeError: No value was given. + temporalio.exceptions.StreamError: A batch failed, so the appender takes no more + Records. + """ + self._raise_error() + if not records: + raise TypeError("append takes one or more values") + loop = asyncio.get_running_loop() + if not self._buffer: + self._first_at = loop.time() + self._buffer.extend(records) + self._appended += len(records) + self._schedule() + + async def flush(self) -> bytes | None: + """Write every Record appended before the call. + + Returns: + The ``last_offset`` of the last written batch, or ``None`` if the appender has + written nothing. + + Raises: + temporalio.exceptions.StreamError: A batch failed. + """ + target = self._appended + while self._settled < target: + if self._writing is None: + self._start() + assert self._writing is not None + # The write goes on when the caller is cancelled, so no batch is left half done. + await asyncio.shield(self._writing) + self._raise_error() + return self._last_offset + + @property + def unwritten(self) -> int: + """How many Records the failed batch and the ones after it left unwritten.""" + return self._unwritten + + async def __aenter__(self) -> Self: + """Use the appender in ``async with``.""" + return self + + async def __aexit__( + self, + exc_type: type[BaseException] | None, + exc: BaseException | None, + tb: TracebackType | None, + ) -> None: + """Write the buffered Records. A write error doesn't hide an error of the block.""" + try: + await self.flush() + except Exception: + if exc is None: + raise + + def _raise_error(self) -> None: + if self._error is not None: + raise self._error + + def _schedule(self) -> None: + if self._writing is not None or not self._buffer: + return + if len(self._buffer) >= _MAX_BATCH_RECORDS: + self._start() + elif self._timer is None: + self._timer = asyncio.get_running_loop().call_at( + self._first_at + self._max_delay.total_seconds(), self._start + ) + + def _start(self) -> None: + if self._timer is not None: + self._timer.cancel() + self._timer = None + if self._writing is not None or not self._buffer: + return + batch = self._buffer[:_MAX_BATCH_RECORDS] + del self._buffer[:_MAX_BATCH_RECORDS] + self._writing = asyncio.get_running_loop().create_task(self._write(batch)) + + async def _write(self, batch: list[BodyT]) -> None: + try: + # The Store numbers each batch, since the appender sends no `sequence` and never + # sends a batch twice. A `sequence` counts for a Producer on a Stream, and two + # appenders of one Activity attempt would each start at 1. + response = await self._stream.append(*batch) + self._last_offset = response.last_offset + except asyncio.CancelledError: + # A later call must not report a cancel that its caller didn't ask for. + self._fail(RuntimeError("The write of a batch was cancelled"), len(batch)) + raise + except Exception as err: + self._fail(err, len(batch)) + finally: + self._settled += len(batch) + self._writing = None + # Schedule the Records appended during the write, for when their delay ends or + # they fill a batch. + self._schedule() + + def _fail(self, error: BaseException, count: int) -> None: + # The Records after a failed batch are never written, so a reader sees no gap. + self._error = error + self._unwritten += count + len(self._buffer) + self._settled += len(self._buffer) + self._buffer.clear() + if self._timer is not None: + self._timer.cancel() + self._timer = None diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py new file mode 100644 index 0000000000..622f00bf18 --- /dev/null +++ b/temporalio/client/_stream_store.py @@ -0,0 +1,554 @@ +from __future__ import annotations + +import dataclasses +from abc import ABC, abstractmethod +from collections.abc import Awaitable, Callable, Sequence +from datetime import timedelta +from typing import TYPE_CHECKING, Any + +import google.protobuf.message +import nexusrpc + +import temporalio.activity +import temporalio.api.streamservice.v1 +import temporalio.bridge._visitor +import temporalio.bridge.runtime +import temporalio.common +import temporalio.converter +import temporalio.nexus.system.stream_service +import temporalio.runtime +import temporalio.workflow +from temporalio.converter import StorageDriverActivityInfo, StorageDriverWorkflowInfo +from temporalio.exceptions import StreamConfigError + +if TYPE_CHECKING: + import temporalio.bridge.streams + + +@dataclasses.dataclass(frozen=True) +class StreamStoreCallOptions: + """Options of the Local Activity that calls a Stream Store for Workflow code. + + Workflow code can't do I/O, so the default + :py:meth:`StreamStore.call_from_workflow` calls its Store from a Local Activity. + + A :py:class:`StreamConfig` holds them in + :py:attr:`StreamConfig.workflow_call_options`, and every Store of that config + gets the same options. + + .. warning:: + This API is experimental and subject to change. + """ + + start_to_close_timeout: timedelta | None = timedelta(seconds=60) + """Most time for one attempt.""" + + schedule_to_close_timeout: timedelta | None = None + """Most time for all attempts.""" + + retry_policy: temporalio.common.RetryPolicy = dataclasses.field( + default_factory=lambda: temporalio.common.RetryPolicy(maximum_attempts=1) + ) + """Retries of a failed call. The default is one attempt.""" + + def __post_init__(self) -> None: + """Check that a timeout is set.""" + if not self.start_to_close_timeout and not self.schedule_to_close_timeout: + raise ValueError( + "start_to_close_timeout or schedule_to_close_timeout must be set" + ) + + +class StreamStore(ABC): + """A Store that answers the Stream operations of a client. + + A client with a Store sends each Stream operation to it. This release has no + Streams on the server, so a client needs a Store for any Stream operation. Use + :py:class:`temporalio.contrib.redis.streams.RedisStreamStore`, or + :py:class:`temporalio.testing.MemoryStreamStore` in tests. + + To keep Streams in another system, write a Store that implements :py:attr:`name` + and the five operations. Each operation takes and gives a message of + ``temporalio.api.streamservice.v1``. The bodies of the Records arrive as payloads + that the SDK already encoded with the data converter and the payload codec, so a + Store keeps payloads and not the user's types. A failed call raises + :py:class:`temporalio.exceptions.StreamError` with a reason. + + .. warning:: + This API is experimental and subject to change. + """ + + @property + @abstractmethod + def name(self) -> str: + """Name of the Store, which the refs of its Streams carry.""" + ... + + @abstractmethod + async def create_stream( + self, request: temporalio.api.streamservice.v1.CreateStreamRequest + ) -> temporalio.api.streamservice.v1.CreateStreamResponse: + """Create a Stream, or get the open one when the conflict policy allows it. + + The ref of the response carries the name of this Store. + """ + ... + + @abstractmethod + async def append_records( + self, request: temporalio.api.streamservice.v1.AppendRecordsRequest + ) -> temporalio.api.streamservice.v1.AppendRecordsResponse: + """Append one batch of Records as one Producer. + + A batch that is sent again with the same Producer, epoch and sequence is + written once. + """ + ... + + @abstractmethod + async def read_records( + self, request: temporalio.api.streamservice.v1.ReadRecordsRequest + ) -> temporalio.api.streamservice.v1.ReadRecordsResponse: + """Read one page of Records after an offset. + + At the end of the Stream it waits up to the request's ``wait`` for a new Record. + """ + ... + + @abstractmethod + async def close_stream( + self, request: temporalio.api.streamservice.v1.CloseStreamRequest + ) -> temporalio.api.streamservice.v1.CloseStreamResponse: + """Close a Stream. Readers drain what is left, and later appends fail.""" + ... + + @abstractmethod + async def describe_stream( + self, request: temporalio.api.streamservice.v1.DescribeStreamRequest + ) -> temporalio.api.streamservice.v1.DescribeStreamResponse: + """Give the status and the newest offset of a Stream.""" + ... + + def worker_activities(self) -> Sequence[Callable[..., Any]]: + """The Activities that a Worker registers for this Store when it runs Workflows. + + The default is one Activity, named for this Store, that + :py:meth:`call_from_workflow` runs as a Local Activity. A Store that overrides + one of the two methods overrides the other to match. + """ + return [_store_call_activity(self)] + + async def call_from_workflow( + self, + operation: str, + request: google.protobuf.message.Message, + options: StreamStoreCallOptions, + ) -> google.protobuf.message.Message: + """Answer one Stream operation for Workflow code. + + It runs in Workflow code, so it does no I/O itself. The default runs the + Activity of :py:meth:`worker_activities` as a Local Activity, with the timeouts + and the retry policy of ``options``. A replay takes the result from History and + calls no method of this Store. + + Args: + operation: The Nexus operation name, ``CreateStream`` or ``DescribeStream`` + in this release. + request: The request message of ``temporalio.api.streamservice.v1``. + options: The :py:attr:`StreamConfig.workflow_call_options` of the config. + + Returns: + The response message of the operation. + """ + stream_operation = _STREAM_OPERATIONS[operation] + # Workflow code can start only a create and a describe in this release, and their + # messages carry no payload. So the Local Activity has no payload codec step. + response = await temporalio.workflow.execute_local_activity( + _store_call_activity_name(self.name), + args=[operation, request.SerializeToString()], + result_type=bytes, + schedule_to_close_timeout=options.schedule_to_close_timeout, + start_to_close_timeout=options.start_to_close_timeout, + retry_policy=options.retry_policy, + ) + return stream_operation.response_type.FromString(response) + + +class _BridgeStreamStore(StreamStore): # pyright: ignore[reportUnusedClass] + """A Store that runs on the Stream runtime of the bridge. + + It builds the runtime at its first call, so the Store can be built before the + Runtime and its Tokio threads exist. + """ + + def __init__( + self, + *, + name: str, + runtime: temporalio.runtime.Runtime | None, + ) -> None: + self._name = name + self._runtime = runtime + self._stream_runtime: temporalio.bridge.streams.StreamRuntime | None = None + + @property + def name(self) -> str: + """Name of the Store.""" + return self._name + + @abstractmethod + def _new_stream_runtime( + self, runtime: temporalio.bridge.runtime.Runtime + ) -> temporalio.bridge.streams.StreamRuntime: ... + + async def create_stream( + self, request: temporalio.api.streamservice.v1.CreateStreamRequest + ) -> temporalio.api.streamservice.v1.CreateStreamResponse: + """See base class.""" + return await self._call_bridge("CreateStream", request) + + async def append_records( + self, request: temporalio.api.streamservice.v1.AppendRecordsRequest + ) -> temporalio.api.streamservice.v1.AppendRecordsResponse: + """See base class.""" + return await self._call_bridge("AppendRecords", request) + + async def read_records( + self, request: temporalio.api.streamservice.v1.ReadRecordsRequest + ) -> temporalio.api.streamservice.v1.ReadRecordsResponse: + """See base class.""" + return await self._call_bridge("ReadRecords", request) + + async def close_stream( + self, request: temporalio.api.streamservice.v1.CloseStreamRequest + ) -> temporalio.api.streamservice.v1.CloseStreamResponse: + """See base class.""" + return await self._call_bridge("CloseStream", request) + + async def describe_stream( + self, request: temporalio.api.streamservice.v1.DescribeStreamRequest + ) -> temporalio.api.streamservice.v1.DescribeStreamResponse: + """See base class.""" + return await self._call_bridge("DescribeStream", request) + + async def _call_bridge( + self, operation: str, request: google.protobuf.message.Message + ) -> Any: + """Answer one Stream operation with the Stream runtime of the bridge. + + Raises: + temporalio.exceptions.StreamError: The call failed. + temporalio.exceptions.StreamConfigError: This build of temporalio has no + Stream runtime, or not this Store. + """ + if self._stream_runtime is None: + runtime = self._runtime or temporalio.runtime.Runtime.default() + self._stream_runtime = self._new_stream_runtime(runtime._core_runtime) + # The bridge takes and gives the messages in their wire form. + response = await self._stream_runtime.call( + operation, request.SerializeToString() + ) + return _STREAM_OPERATIONS[operation].response_type.FromString(response) + + +@dataclasses.dataclass(frozen=True) +class _StreamOperation: + """How a Store answers one operation of the Stream service.""" + + store_method: str + """The name of the :py:class:`StreamStore` method that answers it.""" + + request_type: type[google.protobuf.message.Message] + + response_type: type[google.protobuf.message.Message] + + workflow_refusal: str | None = None + """Why Workflow code can't start it in this release, or ``None`` when it can.""" + + async def call( + self, store: StreamStore, request: google.protobuf.message.Message + ) -> google.protobuf.message.Message: + """Call the method of ``store`` for this operation and check its answer.""" + return self.check_response( + store, await getattr(store, self.store_method)(request) + ) + + def check_response( + self, store: StreamStore, response: object + ) -> google.protobuf.message.Message: + """Give ``response`` back when it has the response type of this operation.""" + # A Store that someone wrote can give another type, and the error names it here + # rather than in the conversion to the user's type. + if not isinstance(response, self.response_type): + raise TypeError( + f"The Stream Store '{store.name}' answered {self.store_method} with " + f"{type(response).__name__}, not {self.response_type.__name__}" + ) + return response + + +_STREAM_OPERATIONS: dict[str, _StreamOperation] = { + "CreateStream": _StreamOperation( + "create_stream", + temporalio.api.streamservice.v1.CreateStreamRequest, + temporalio.api.streamservice.v1.CreateStreamResponse, + ), + "AppendRecords": _StreamOperation( + "append_records", + temporalio.api.streamservice.v1.AppendRecordsRequest, + temporalio.api.streamservice.v1.AppendRecordsResponse, + workflow_refusal=( + "Workflow code can't append to a Stream on a Store in this release. " + "Call a Local Activity that appends through its client." + ), + ), + "ReadRecords": _StreamOperation( + "read_records", + temporalio.api.streamservice.v1.ReadRecordsRequest, + temporalio.api.streamservice.v1.ReadRecordsResponse, + workflow_refusal=( + "Workflow code can't read a Stream on a Store in this release. " + "Call an Activity that reads it through its client." + ), + ), + "CloseStream": _StreamOperation( + "close_stream", + temporalio.api.streamservice.v1.CloseStreamRequest, + temporalio.api.streamservice.v1.CloseStreamResponse, + workflow_refusal=( + "Workflow code can't close a Stream on a Store in this release. " + "Call an Activity that closes it through its client." + ), + ), + "DescribeStream": _StreamOperation( + "describe_stream", + temporalio.api.streamservice.v1.DescribeStreamRequest, + temporalio.api.streamservice.v1.DescribeStreamResponse, + ), +} +"""The operations of the Stream service, by their Nexus operation name.""" + +_STORE_CALL_ACTIVITY = "__temporal_stream_store_call" + + +def _store_call_activity_name(store_name: str) -> str: + # The name carries the Store's name, so two Stores on one Worker don't clash. + return f"{_STORE_CALL_ACTIVITY}:{store_name}" + + +def _store_call_activity( + store: StreamStore, +) -> Callable[[str, bytes], Awaitable[bytes]]: + @temporalio.activity.defn(name=_store_call_activity_name(store.name)) + async def call(operation: str, request: bytes) -> bytes: + stream_operation = _STREAM_OPERATIONS[operation] + response = await stream_operation.call( + store, stream_operation.request_type.FromString(request) + ) + return response.SerializeToString() + + return call + + +@dataclasses.dataclass(frozen=True) +class StreamSelectContext: + """Context passed to :py:attr:`StreamConfig.store_selector` calls. + + .. warning:: + This API is experimental and subject to change. + """ + + id: str + """The id of the Stream.""" + + target: StorageDriverActivityInfo | StorageDriverWorkflowInfo | None = None + """The Workflow or Activity that makes the call, or ``None`` for a plain client. + + A call in an Activity has that Activity here. A Workflow's call runs the selector + in Workflow code, and has that Workflow here. + """ + + +@dataclasses.dataclass(frozen=True) +class StreamConfig: + """The Stream Stores of a client, and the rule that picks one for a new Stream. + + A client with this config sends each Stream operation to one of its Stores. + This release has no Streams on the server, so a client without this config + raises :py:class:`temporalio.exceptions.StreamConfigError` for a Stream + operation. It holds Stores and a selector, as + :py:class:`temporalio.converter.ExternalStorage` holds drivers and a selector. + + A ref carries the name of its Store, so a call on a known Stream goes to the + Store of that name. A call whose ref names no Store, such as a create, goes to + the one Store, or to the Store that :py:attr:`store_selector` returns. + + .. warning:: + This API is experimental and subject to change. + """ + + stores: Sequence[StreamStore] + """At least one Store. Each Store has a unique name. + + A ref carries the :py:attr:`StreamStore.name` of its Store, and a call looks + the Store up by that name. With more than one Store, :py:attr:`store_selector` + must also be set. A Worker serves every Store of its client. + """ + + store_selector: Callable[[StreamSelectContext], StreamStore] | None = None + """Picks the Store of a new Stream. Required with more than one Store. + + It gets a :py:class:`StreamSelectContext` for each call whose ref names no + Store, and returns one of the instances in :py:attr:`stores`. When it is + ``None`` and there is one Store, every new Stream goes to that Store. + + For a call of Workflow code, the selector runs in Workflow code, also on a + replay. So it must give the same Store for the same context. The Local Activity + of the chosen Store is in History, and another answer on a replay is a + non-determinism error. A Stream that code builds from an id only goes through the + selector on each call, so the selector must also give the same Store for the same + id. + """ + + workflow_call_options: StreamStoreCallOptions = dataclasses.field( + default_factory=StreamStoreCallOptions + ) + """Options of a Store's call for Workflow code, for every Store of this config. + + :py:meth:`StreamStore.call_from_workflow` gets them. The default runs its Local + Activity with their timeouts and retry policy. + """ + + _store_map: dict[str, StreamStore] = dataclasses.field( + init=False, repr=False, compare=False + ) + """Name-keyed index of :py:attr:`stores`, built at construction time.""" + + def __post_init__(self) -> None: + """Check the Stores and build the name-keyed map of them. + + Raises :py:class:`temporalio.exceptions.StreamConfigError` if there is no + Store, if two Stores share a name, or if more than one Store is given + without a :py:attr:`store_selector`. + """ + if not self.stores: + raise StreamConfigError("StreamConfig.stores must have at least one Store") + store_map: dict[str, StreamStore] = {} + for store in self.stores: + if store.name in store_map: + raise StreamConfigError( + f"StreamConfig.stores has more than one Store named '{store.name}'. " + "Each Store needs a unique name." + ) + store_map[store.name] = store + if len(self.stores) > 1 and self.store_selector is None: + raise StreamConfigError( + "StreamConfig.store_selector must be set when there is more than one Store" + ) + object.__setattr__(self, "_store_map", store_map) + + def _select_store(self, context: StreamSelectContext) -> StreamStore: + selector = self.store_selector + if selector is None: + return self.stores[0] + store = selector(context) + if ( + not isinstance(store, StreamStore) + or self._store_map.get(store.name) is not store + ): + raise StreamConfigError( + f"The store_selector returned {store!r}, which isn't one of " + "StreamConfig.stores" + ) + return store + + def _get_store_by_name(self, name: str) -> StreamStore: + store = self._store_map.get(name) + if store is None: + raise StreamConfigError( + f"The ref names the Stream Store '{name}', which this client doesn't have" + ) + return store + + def _store_for( + self, + ref: temporalio.nexus.system.stream_service.StreamRef[Any], + target: StorageDriverActivityInfo | StorageDriverWorkflowInfo | None, + ) -> StreamStore: + if ref.store: + return self._get_store_by_name(ref.store) + return self._select_store(StreamSelectContext(id=ref.id, target=target)) + + +def _request_ref( # pyright: ignore[reportUnusedFunction] + request: Any, +) -> temporalio.nexus.system.stream_service.StreamRef[Any]: + # A create calls its ref `stream_ref` and every other request calls it `ref`, so + # the field is found by its type. + if dataclasses.is_dataclass(request) and not isinstance(request, type): + for field in dataclasses.fields(request): + value = getattr(request, field.name) + if isinstance(value, temporalio.nexus.system.stream_service.StreamRef): + return value + raise TypeError(f"The request {type(request).__name__} has no StreamRef") + + +def _activity_target() -> StorageDriverActivityInfo | None: # pyright: ignore[reportUnusedFunction] + # External storage builds the same value from the running Activity. + if not temporalio.activity.in_activity(): + return None + info = temporalio.activity.info() + return StorageDriverActivityInfo( + id=info.activity_id, + type=info.activity_type, + run_id=info.activity_run_id, + namespace=info.namespace, + ) + + +def _refuse_store_plugins( # pyright: ignore[reportUnusedFunction] + plugins: Sequence[object], +) -> None: + for plugin in plugins: + if isinstance(plugin, StreamStore): + raise StreamConfigError( + f"The Stream Store '{plugin.name}' is in plugins. Give it in " + "stream_config=StreamConfig(stores=[...]) instead." + ) + + +def _check_not_negative(**durations: timedelta | None) -> None: # pyright: ignore[reportUnusedFunction] + for label, value in durations.items(): + if value is not None and value < timedelta(): + raise ValueError(f"{label} must not be negative") + + +def _stream_service() -> nexusrpc.ServiceDefinition: + service = nexusrpc.get_service_definition( + temporalio.nexus.system.stream_service.StreamService + ) + if service is None: + raise RuntimeError("The generated StreamService has no service definition") + return service + + +_STREAM_SERVICE = _stream_service() + + +async def _call_stream_store( # pyright: ignore[reportUnusedFunction] + store: StreamStore, + data_converter: temporalio.converter.DataConverter, + operation: str, + request: google.protobuf.message.Message, +) -> google.protobuf.message.Message: + stream_operation = _STREAM_OPERATIONS[operation] + if not data_converter.payload_codec: + return await stream_operation.call(store, request) + # The worker package imports the client, so a module level import would cycle. + from temporalio.bridge.worker import _Visitor + + visitor = temporalio.bridge._visitor.PayloadVisitor(skip_search_attributes=True) + await visitor.visit(_Visitor(data_converter._encode_payload_sequence), request) + response = await stream_operation.call(store, request) + await visitor.visit(_Visitor(data_converter._decode_payload_sequence), response) + return response diff --git a/temporalio/contrib/redis/__init__.py b/temporalio/contrib/redis/__init__.py new file mode 100644 index 0000000000..34dad945bf --- /dev/null +++ b/temporalio/contrib/redis/__init__.py @@ -0,0 +1 @@ +"""Redis integrations for the Temporal SDK.""" diff --git a/temporalio/contrib/redis/streams.py b/temporalio/contrib/redis/streams.py new file mode 100644 index 0000000000..389601406e --- /dev/null +++ b/temporalio/contrib/redis/streams.py @@ -0,0 +1,116 @@ +"""The Redis Stream Store. + +A client takes it in ``stream_config``, and its Workers use the same Store:: + + from temporalio.client import Client, StreamConfig + from temporalio.contrib.redis.streams import RedisStreamStore + + client = await Client.connect( + "localhost:7233", + stream_config=StreamConfig(stores=[RedisStreamStore("redis://localhost:6379")]), + ) + +.. warning:: + This module is experimental and subject to change. +""" + +from __future__ import annotations + +from collections.abc import Sequence +from datetime import timedelta + +import temporalio.bridge.runtime +import temporalio.bridge.streams +import temporalio.client._stream_store +import temporalio.runtime + + +class RedisStreamStore(temporalio.client._stream_store._BridgeStreamStore): + """A Stream Store that keeps its Streams in Redis 7.0 or later. + + It runs the Stream runtime of the bridge on the Redis Store of the + ``temporalio-streams-redis`` crate. It connects at its first call, so building + it needs no server, and a server that is down fails that call. A failed call + raises :py:class:`temporalio.exceptions.StreamError`. + + A key has no namespace, so a Stream id is unique in one Redis and key prefix. + Give each namespace its own key prefix to keep their Streams apart. + + .. warning:: + This API is experimental and subject to change. + """ + + def __init__( + self, + url: str | Sequence[str], + *, + cluster: bool = False, + key_prefix: str | None = None, + retention: timedelta | None = None, + last_write_ttl: timedelta | None = None, + response_timeout: timedelta | None = None, + wait_replicas: int = 0, + wait_timeout: timedelta | None = None, + name: str = "redis", + runtime: temporalio.runtime.Runtime | None = None, + ) -> None: + """Create a Redis Store. It builds its Stream runtime and connects at its first call. + + A bad option raises ``ValueError`` at that first call, with the message of the + Redis crate. + + Args: + url: A ``redis://`` or ``rediss://`` URL, or the seed URLs of a cluster. + Credentials and TLS come from the URL. + cluster: Whether the URLs are of a Redis Cluster. + key_prefix: The prefix of every key. ``None`` takes ``temporal-streams``. + A Stream id is unique in one Redis and key prefix. + retention: The default Retention of a new Stream, which is how long the + Stream stays readable after its close. ``None`` takes 7 days. + last_write_ttl: How long an open Stream stays after its last write. + ``None`` keeps an open Stream and every Record until its close. + response_timeout: The longest time that one Redis call can take. ``None`` + takes 5 seconds. + wait_replicas: How many replicas must have each write before it answers, + with the Redis ``WAIT`` command. Zero sends no ``WAIT``. + wait_timeout: How long a write waits for its replicas, at most half of + ``response_timeout``. ``None`` takes 1 second. + name: Name of the Store, which the refs of its Streams carry, so that every + process can route a ref that it gets. + runtime: Runtime whose Tokio threads run the Store. ``None`` takes + :py:meth:`temporalio.runtime.Runtime.default` at the first call. + """ + temporalio.client._stream_store._check_not_negative( + retention=retention, + last_write_ttl=last_write_ttl, + response_timeout=response_timeout, + wait_timeout=wait_timeout, + ) + super().__init__(name=name, runtime=runtime) + self._urls = [url] if isinstance(url, str) else list(url) + self._cluster = cluster + self._key_prefix = key_prefix + self._retention = retention + self._last_write_ttl = last_write_ttl + self._response_timeout = response_timeout + self._wait_replicas = wait_replicas + self._wait_timeout = wait_timeout + + def _new_stream_runtime( + self, runtime: temporalio.bridge.runtime.Runtime + ) -> temporalio.bridge.streams.StreamRuntime: + return temporalio.bridge.streams.StreamRuntime.new_redis( + runtime, + urls=self._urls, + cluster=self._cluster, + key_prefix=self._key_prefix, + retention=self._retention, + last_write_ttl=self._last_write_ttl, + response_timeout=self._response_timeout, + name=self._name, + wait_replicas=self._wait_replicas, + wait_timeout=self._wait_timeout, + ) + + +__all__ = ["RedisStreamStore"] diff --git a/temporalio/exceptions.py b/temporalio/exceptions.py index 03a623c21e..63135cd9fc 100644 --- a/temporalio/exceptions.py +++ b/temporalio/exceptions.py @@ -8,6 +8,7 @@ import temporalio.api.enums.v1 import temporalio.api.failure.v1 +import temporalio.api.stream.v1 class TemporalError(Exception): @@ -198,6 +199,114 @@ def category(self) -> ApplicationErrorCategory: return self._category +_STREAM_ERROR_REASON_UNSPECIFIED = ( + temporalio.api.enums.v1.StreamErrorReason.STREAM_ERROR_REASON_UNSPECIFIED +) + +# The one reason that a retry can fix. +_STREAM_RETRYABLE_REASONS: frozenset[int] = frozenset( + {temporalio.api.enums.v1.StreamErrorReason.STREAM_ERROR_REASON_STORAGE_UNAVAILABLE} +) + + +class StreamError(ApplicationError): + """A failed Stream call. ``reason`` tells the cases apart. + + One class covers every failed call in client, Activity and Workflow code, so match + on ``reason`` and not on the type. The reason is a ``StreamErrorReason``, such as + ``STREAM_ERROR_REASON_NOT_FOUND`` (nobody created the Stream), + ``STREAM_ERROR_REASON_CLOSED`` (the Stream is closed), or + ``STREAM_ERROR_REASON_ALREADY_EXISTS`` (a strict create of a Stream that exists). + A Store outage, ``STREAM_ERROR_REASON_STORAGE_UNAVAILABLE``, is the one retryable + reason. + + .. warning:: + This API is experimental and subject to change. + """ + + def __init__( + self, + message: str, + *, + reason: temporalio.api.enums.v1.StreamErrorReason.ValueType | None = None, + ) -> None: + """Initialize a Stream error. ``reason`` defaults to the unspecified reason.""" + if reason is None: + reason = _STREAM_ERROR_REASON_UNSPECIFIED + # The details carry the reason, so the error keeps it when it crosses an + # Activity or a Nexus operation. + super().__init__( + message, + temporalio.api.stream.v1.StreamErrorDetails(reason=reason), + type="StreamError", + non_retryable=reason not in _STREAM_RETRYABLE_REASONS, + ) + self._reason = reason + + @property + def reason(self) -> temporalio.api.enums.v1.StreamErrorReason.ValueType: + """Why the Stream call failed.""" + return self._reason + + +def _stream_error_from_reason(reason: int, message: str) -> StreamError: + if reason not in temporalio.api.enums.v1.StreamErrorReason.values(): + # A reason that this SDK doesn't know gives the unspecified reason. + reason = _STREAM_ERROR_REASON_UNSPECIFIED + return StreamError( + message, reason=temporalio.api.enums.v1.StreamErrorReason.ValueType(reason) + ) + + +def _stream_error_from_call( # pyright: ignore[reportUnusedFunction] + message: str, details: bytes +) -> StreamError: + # A Store on the bridge fails with the encoded details, as a gRPC status has them. + proto = temporalio.api.stream.v1.StreamErrorDetails.FromString(details) + return _stream_error_from_reason(proto.reason, message) + + +def _stream_error_from_failure( # pyright: ignore[reportUnusedFunction] + err: BaseException, +) -> StreamError | None: + # A Local Activity or a Nexus operation hands back a plain application error, so the + # error is rebuilt from its type and details. + if isinstance(err, StreamError): + return err + if not isinstance(err, ApplicationError) or err.type != "StreamError": + return None + reason: int = _STREAM_ERROR_REASON_UNSPECIFIED + for detail in err.details: + if isinstance(detail, temporalio.api.stream.v1.StreamErrorDetails): + reason = detail.reason + break + return _stream_error_from_reason(reason, err.message) + + +class StreamConfigError(RuntimeError): + """A Stream call can't find its Store, because of how the Stores are set up. + + It is not a :py:class:`StreamError`. A Store didn't fail. The setup of the Stores + is wrong, and a retry won't help. These are its cases. + + * A Stream call is made on a client with no Stream config. This release has no + Streams on the server, so every Stream call needs a Store. + * A Workflow's Stream call is made on a Worker whose client has no Stream config. + This fails the Workflow Task. + * A :py:class:`temporalio.client.StreamConfig` has no Store, has two Stores with + one name, or has more than one Store and no selector. + * A ref names a Store that the config of the client doesn't have. + * The selector returns a Store that isn't one of the config's Stores. + * A :py:class:`temporalio.client.StreamStore` is given in ``plugins``. + * A Workflow's Stream call has a ref that names a Store that the config of the + Worker's client doesn't have. This fails the Workflow Task. + * This build of temporalio has no Stream runtime, or no Redis Stream Store. + + .. warning:: + This API is experimental and subject to change. + """ + + class CancelledError(FailureError): """Error raised on workflow/activity cancellation.""" diff --git a/temporalio/nexus/system/__init__.py b/temporalio/nexus/system/__init__.py index 1f8f7c6d29..d3096b1a00 100644 --- a/temporalio/nexus/system/__init__.py +++ b/temporalio/nexus/system/__init__.py @@ -19,6 +19,7 @@ from temporalio.bridge._visitor_functions import VisitorFunctions from temporalio.converter import BinaryProtoPayloadConverter, CompositePayloadConverter from temporalio.converter._payload_converter import ( + _get_transfer_type_converter, _TemporalTransferTypePayloadConverter, ) @@ -123,6 +124,27 @@ def from_payloads( with _user_converter_context(self._user_converters): return self._outer_payload_converter.from_payloads(payloads, type_hints) + def _to_transfer_value(self, value: Any) -> Any: + """Convert a system Nexus model to its proto message, with no payload in between.""" + converter = _get_transfer_type_converter(type(value)) + if converter is None: + raise TypeError(f"{type(value).__name__} has no transfer type") + with _user_converter_context(self._user_converters): + return converter.to_transfer_type(value) + + def _from_transfer_value(self, value: Any, type_hint: type | None) -> Any: + """Convert a proto message to the system Nexus model of ``type_hint``. + + With no ``type_hint``, or one that has no transfer type, it gives the message. + """ + converter = ( + _get_transfer_type_converter(type_hint) if type_hint is not None else None + ) + if converter is None or type_hint is None: + return value + with _user_converter_context(self._user_converters): + return converter.from_transfer_type(value, type_hint) + def is_system_endpoint(endpoint: str) -> bool: """Return whether a Nexus endpoint is the Temporal system endpoint. @@ -171,7 +193,7 @@ async def maybe_visit_payload( def _get_payload_converter( # pyright: ignore[reportUnusedFunction] user_payload_converter: temporalio.converter.PayloadConverter, user_failure_converter: temporalio.converter.FailureConverter, -) -> temporalio.converter.PayloadConverter: +) -> _SystemNexusPayloadConverter: """Return the fixed payload converter for system Nexus outer envelopes.""" return _SystemNexusPayloadConverter(user_payload_converter, user_failure_converter) @@ -182,9 +204,14 @@ def _get_serialization_context( # pyright: ignore[reportUnusedFunction] request: Any, ) -> temporalio.converter.SerializationContext | None: """Return the serialization context for a system Nexus operation.""" + from .stream_service.workflow import ( + __nexus_operation_registry__ as stream_operation_registry, + ) from .workflow_service import __nexus_operation_registry__ - operation_info = __nexus_operation_registry__.get((service, operation)) + operation_info = __nexus_operation_registry__.get( + (service, operation) + ) or stream_operation_registry.get((service, operation)) if operation_info is None or operation_info.serialization_context is None: return None return operation_info.serialization_context(request) @@ -194,3 +221,10 @@ def _get_serialization_context( # pyright: ignore[reportUnusedFunction] "TEMPORAL_SYSTEM_ENDPOINT", "is_system_endpoint", ] + +# The generated Workflow modules of `stream_service` import `temporalio.workflow` at their top, +# and `temporalio.workflow` imports `create_stream` and `get_stream` from them. So this package +# loads `temporalio.workflow` as its last statement, when every name above exists. Then +# `temporalio.workflow` is complete before a generated Workflow module starts, whichever module +# is the first import. +import temporalio.workflow # noqa: E402 # pyright: ignore[reportUnusedImport] diff --git a/temporalio/nexus/system/_support.py b/temporalio/nexus/system/_support.py index 15a8e80102..55f53b59a1 100644 --- a/temporalio/nexus/system/_support.py +++ b/temporalio/nexus/system/_support.py @@ -1,9 +1,15 @@ import collections.abc +import dataclasses +import functools import typing -from datetime import timedelta +import urllib.parse +import uuid +from datetime import datetime, timedelta import google.protobuf.duration_pb2 +import google.protobuf.timestamp_pb2 +import temporalio.activity import temporalio.api.common.v1.message_pb2 as common_pb2 import temporalio.api.enums.v1.workflow_pb2 as workflow_enums_pb2 import temporalio.api.failure.v1.message_pb2 as failure_pb2 @@ -13,12 +19,157 @@ import temporalio.converter import temporalio.nexus.system +if typing.TYPE_CHECKING: + from temporalio.nexus.system.stream_service.models import ( + Producer, + ReadRecordsRequest, + ReadRecordsResponse, + StreamRecord, + ) + +_BodyT = typing.TypeVar("_BodyT") + class SignalWithStartWorkflowModelRequest(typing.Protocol): namespace: str id: str +def request_id() -> str: + import temporalio.workflow + + # Each run of a chain sends the same create, and the Store answers a create with the + # request id that made the Stream as a success. One id for the chain also replays. + if temporalio.workflow.in_workflow(): + return temporalio.workflow.info().first_execution_run_id + return str(uuid.uuid4()) + + +def _producer_id(*parts: str) -> str: + # Quoting keeps a ':' inside an id from shifting the parts. + return ":".join(urllib.parse.quote(part, safe="") for part in parts) + + +# One id for the process, because nothing fences a client write. +@functools.cache +def _client_producer_id() -> str: + return _producer_id("client", str(uuid.uuid4())) + + +def _timestamp(value: datetime) -> google.protobuf.timestamp_pb2.Timestamp: + proto = google.protobuf.timestamp_pb2.Timestamp() + proto.FromDatetime(value) + return proto + + +def default_producer() -> "Producer": + from temporalio.nexus.system.stream_service.models import ( + ActivityEpoch, + ActivityProducer, + ClientProducer, + LocalActivityEpoch, + LocalActivityProducer, + Producer, + ProducerKindClient, + ProducerKindLocalActivity, + ProducerKindStandaloneActivity, + ProducerKindWorkflowActivity, + Timestamp, + WorkflowTaskEpoch, + ) + + if not temporalio.activity.in_activity(): + return Producer( + id=_client_producer_id(), kind=ProducerKindClient(ClientProducer()) + ) + info = temporalio.activity.info() + if info.is_local: + task = info.workflow_task + if task is None or info.workflow_run_id is None: + raise RuntimeError( + "A Stream write from a Local Activity needs its Workflow Task, and the " + "Worker's Core gave none" + ) + started_time = None + if task.started_time is not None: + proto_time = _timestamp(task.started_time) + started_time = Timestamp(seconds=proto_time.seconds, nanos=proto_time.nanos) + # The activity id of a Local Activity is its sequence number in the run, so a + # retried Workflow Task writes as the same Producer with a higher epoch. + return Producer( + id=_producer_id("la", info.workflow_run_id, info.activity_id), + kind=ProducerKindLocalActivity( + LocalActivityProducer( + run_id=info.workflow_run_id, + epoch=LocalActivityEpoch( + task=WorkflowTaskEpoch( + started_event_id=task.started_event_id, + attempt=task.attempt, + started_time=started_time, + ), + attempt=info.attempt, + ), + ) + ), + ) + attempt_time = _timestamp(info.current_attempt_scheduled_time) + epoch = ActivityEpoch( + attempt_scheduled_time=Timestamp( + seconds=attempt_time.seconds, nanos=attempt_time.nanos + ), + attempt=info.attempt, + ) + if info.workflow_run_id is not None: + # The scheduled time stays the same across attempts, and it tells apart two uses + # of one activity id in a run. + schedule_key = str(_timestamp(info.scheduled_time).ToNanoseconds()) + return Producer( + id=_producer_id( + "act", info.workflow_run_id, info.activity_id, schedule_key + ), + kind=ProducerKindWorkflowActivity( + ActivityProducer(run_id=info.workflow_run_id, epoch=epoch) + ), + ) + if info.activity_run_id is None: + raise RuntimeError("A Standalone Activity has no activity run id") + return Producer( + id=_producer_id("sa", info.activity_id, info.activity_run_id), + kind=ProducerKindStandaloneActivity(ActivityProducer(epoch=epoch)), + ) + + +def workflow_producer() -> "Producer": + import temporalio.workflow + from temporalio.nexus.system.stream_service.models import ( + Producer, + ProducerKindWorkflow, + WorkflowProducer, + ) + + run_id = temporalio.workflow.info().run_id + return Producer( + id=_producer_id("wf", run_id), + kind=ProducerKindWorkflow(WorkflowProducer(run_id=run_id)), + ) + + +def next_read_request( + request: "ReadRecordsRequest[_BodyT]", response: "ReadRecordsResponse[_BodyT]" +) -> "ReadRecordsRequest[_BodyT] | None": + if response.done or (request.wait is None and not response.records): + return None + if response.last_offset is None: + return request + return dataclasses.replace(request, from_offset=response.last_offset) + + +def resume_read_request( + request: "ReadRecordsRequest[_BodyT]", item: "StreamRecord[_BodyT]" +) -> "ReadRecordsRequest[_BodyT]": + return dataclasses.replace(request, from_offset=item.offset) + + def retry_policy_from_proto( proto: common_pb2.RetryPolicy, ) -> temporalio.common.RetryPolicy: diff --git a/temporalio/testing/__init__.py b/temporalio/testing/__init__.py index 0cd652d86b..5574a0a39b 100644 --- a/temporalio/testing/__init__.py +++ b/temporalio/testing/__init__.py @@ -1,11 +1,13 @@ """Test framework for workflows and activities.""" from ._activity import ActivityEnvironment +from ._stream_store import MemoryStreamStore from ._timeskipping import TimeSkipper, TimeSkippingConfig from ._workflow import WorkflowEnvironment __all__ = [ "ActivityEnvironment", + "MemoryStreamStore", "TimeSkipper", "WorkflowEnvironment", "TimeSkippingConfig", diff --git a/temporalio/testing/_stream_store.py b/temporalio/testing/_stream_store.py new file mode 100644 index 0000000000..86ba0ac16d --- /dev/null +++ b/temporalio/testing/_stream_store.py @@ -0,0 +1,57 @@ +"""A Stream Store in the memory of the process, for tests.""" + +from __future__ import annotations + +from datetime import timedelta + +import temporalio.bridge.runtime +import temporalio.bridge.streams +import temporalio.client._stream_store +import temporalio.runtime + + +class MemoryStreamStore(temporalio.client._stream_store._BridgeStreamStore): + """A Stream Store that keeps its Streams in the memory of this process, for tests. + + It runs the same Stream runtime and rules as every other Store on the bridge. A + failed call raises :py:class:`temporalio.exceptions.StreamError`. + + .. warning:: + This API is experimental and subject to change. + """ + + def __init__( + self, + *, + name: str = "memory", + retention: timedelta | None = None, + last_write_ttl: timedelta | None = None, + runtime: temporalio.runtime.Runtime | None = None, + ) -> None: + """Create a memory Store. It builds its Stream runtime at its first call. + + Args: + name: Name of the Store, which the refs of its Streams carry. + retention: How long a closed Stream stays. ``None`` takes the default of + the Stream runtime. + last_write_ttl: How long an open Stream stays after its last write. + ``None`` keeps an open Stream until its close. + runtime: Runtime whose Tokio threads run the Store. ``None`` takes + :py:meth:`temporalio.runtime.Runtime.default` at the first call. + """ + temporalio.client._stream_store._check_not_negative( + retention=retention, last_write_ttl=last_write_ttl + ) + super().__init__(name=name, runtime=runtime) + self._retention = retention + self._last_write_ttl = last_write_ttl + + def _new_stream_runtime( + self, runtime: temporalio.bridge.runtime.Runtime + ) -> temporalio.bridge.streams.StreamRuntime: + return temporalio.bridge.streams.StreamRuntime.new_memory( + runtime, + name=self._name, + retention=self._retention, + last_write_ttl=self._last_write_ttl, + ) diff --git a/temporalio/testing/_workflow.py b/temporalio/testing/_workflow.py index 6c8354162c..54add4e363 100644 --- a/temporalio/testing/_workflow.py +++ b/temporalio/testing/_workflow.py @@ -102,6 +102,7 @@ async def start_local( dev_server_download_ttl: timedelta | None = None, dev_server_startup_timeout: timedelta | None = None, ui_port: int | None = None, + stream_config: temporalio.client.StreamConfig | None = None, ) -> WorkflowEnvironment: """Start a full Temporal server locally, downloading if necessary. @@ -165,6 +166,8 @@ async def start_local( become ready after spawning. Defaults to 5 seconds and does not include download time. ui_port: UI port to use if UI is enabled. + stream_config: See parameter of the same name on + :py:meth:`temporalio.client.Client.connect`. Returns: The started CLI dev server workflow environment. @@ -194,6 +197,7 @@ async def start_local( dev_server_download_ttl=dev_server_download_ttl, dev_server_startup_timeout=dev_server_startup_timeout, ui_port=ui_port, + stream_config=stream_config, ) @classmethod @@ -216,6 +220,7 @@ async def start_time_skipping( test_server_extra_args: Sequence[str] = [], test_server_download_ttl: timedelta | None = None, test_server_startup_timeout: timedelta | None = None, + stream_config: temporalio.client.StreamConfig | None = None, ) -> WorkflowEnvironment: """Start a V1 time skipping workflow environment. @@ -276,6 +281,8 @@ async def start_time_skipping( test_server_startup_timeout: Maximum time to wait for the test server to become ready after spawning. Defaults to 5 seconds and does not include download time. + stream_config: See parameter of the same name on + :py:meth:`temporalio.client.Client.connect`. Returns: The started workflow environment with time skipping. @@ -296,6 +303,7 @@ async def start_time_skipping( test_server_extra_args=test_server_extra_args, test_server_download_ttl=test_server_download_ttl, test_server_startup_timeout=test_server_startup_timeout, + stream_config=stream_config, ) @classmethod @@ -582,6 +590,7 @@ async def _bootstrap_dev_server( dev_server_download_ttl: timedelta | None = None, dev_server_startup_timeout: timedelta | None = None, ui_port: int | None = None, + stream_config: temporalio.client.StreamConfig | None = None, ) -> tuple[ temporalio.bridge.testing.EphemeralServer, temporalio.client.Client, @@ -649,6 +658,7 @@ async def _bootstrap_dev_server( rpc_metadata=rpc_metadata, identity=identity, runtime=runtime, + stream_config=stream_config, ) except: try: @@ -719,6 +729,7 @@ async def _create( test_server_extra_args: Sequence[str], test_server_download_ttl: timedelta | None, test_server_startup_timeout: timedelta | None, + stream_config: temporalio.client.StreamConfig | None, ) -> _V1WorkflowEnvironment: runtime = runtime or temporalio.runtime.Runtime.default() download_ttl_ms = None @@ -753,6 +764,7 @@ async def _create( rpc_metadata=rpc_metadata, identity=identity, runtime=runtime, + stream_config=stream_config, ) except: try: diff --git a/temporalio/worker/_interceptor.py b/temporalio/worker/_interceptor.py index 6196f5efe1..9cfab54022 100644 --- a/temporalio/worker/_interceptor.py +++ b/temporalio/worker/_interceptor.py @@ -21,6 +21,9 @@ import temporalio.nexus import temporalio.nexus._util import temporalio.workflow +from temporalio.nexus.system.stream_service.workflow._system_nexus_interceptor import ( + _SystemNexusWorkflowOutboundInterceptorBase as _StreamSystemNexusInterceptorBase, +) from temporalio.nexus.system.workflow_service._system_nexus_interceptor import ( _SystemNexusWorkflowOutboundInterceptorBase, ) @@ -455,7 +458,9 @@ async def handle_update_handler(self, input: HandleUpdateInput) -> Any: return await self.next.handle_update_handler(input) -class WorkflowOutboundInterceptor(_SystemNexusWorkflowOutboundInterceptorBase): +class WorkflowOutboundInterceptor( + _SystemNexusWorkflowOutboundInterceptorBase, _StreamSystemNexusInterceptorBase +): """Outbound interceptor to wrap calls made from within workflows. This should be extended by any workflow outbound interceptors. @@ -472,9 +477,7 @@ def __init__(self, next: WorkflowOutboundInterceptor) -> None: """ self.next = next - def _next_system_nexus_interceptor( - self, - ) -> _SystemNexusWorkflowOutboundInterceptorBase: + def _next_system_nexus_interceptor(self) -> WorkflowOutboundInterceptor: return self.next def continue_as_new(self, input: ContinueAsNewInput) -> NoReturn: diff --git a/temporalio/worker/_replayer.py b/temporalio/worker/_replayer.py index 62a27b29cf..540e9d4052 100644 --- a/temporalio/worker/_replayer.py +++ b/temporalio/worker/_replayer.py @@ -22,6 +22,7 @@ from ..common import HeaderCodecBehavior from ._interceptor import Interceptor +from ._stream_store import _WorkflowStreamStore from ._worker import load_default_build_id from ._workflow import _WorkflowWorker from ._workflow_instance import ( @@ -56,6 +57,7 @@ def __init__( runtime: temporalio.runtime.Runtime | None = None, disable_safe_workflow_eviction: bool = False, header_codec_behavior: HeaderCodecBehavior = HeaderCodecBehavior.NO_CODEC, + stream_config: temporalio.client.StreamConfig | None = None, ) -> None: """Create a replayer to replay workflows from history. @@ -64,6 +66,11 @@ def __init__( the replayer that were passed to the worker when the workflow originally ran. + ``stream_config`` takes the Stream config of the client that the + worker had, so a replay makes the same commands. A history with a Stream + call needs it, since a Stream call with no Stream config fails the + Workflow Task. A replay calls no method of its Stores. + Note, unlike the worker, for the replayer the workflow_task_executor will default to a new thread pool executor with no max_workers set that will be shared across all replay calls and never explicitly shut down. @@ -86,6 +93,7 @@ def __init__( runtime=runtime, disable_safe_workflow_eviction=disable_safe_workflow_eviction, header_codec_behavior=header_codec_behavior, + stream_config=stream_config, ) self._initial_config = self._config.copy() self._default_workflow_logic_flags = set(_DEFAULT_ENABLED_WORKFLOW_LOGIC_FLAGS) @@ -287,6 +295,9 @@ def on_eviction_hook( default_workflow_logic_flags=frozenset( self._default_workflow_logic_flags ), + stream_store=_WorkflowStreamStore.from_config( + self._config.get("stream_config") + ), ) external_storage = data_converter.external_storage storage_driver_types = ( @@ -435,6 +446,7 @@ class ReplayerConfig(TypedDict, total=False): runtime: temporalio.runtime.Runtime | None disable_safe_workflow_eviction: bool header_codec_behavior: HeaderCodecBehavior + stream_config: temporalio.client.StreamConfig | None @dataclass(frozen=True) diff --git a/temporalio/worker/_stream_store.py b/temporalio/worker/_stream_store.py new file mode 100644 index 0000000000..da9faa689c --- /dev/null +++ b/temporalio/worker/_stream_store.py @@ -0,0 +1,121 @@ +"""How Workflow code reaches the Stream Stores of its Worker's client.""" + +from __future__ import annotations + +import asyncio +import dataclasses +from collections.abc import Generator +from typing import TYPE_CHECKING, Any + +from nexusrpc import OutputT + +import temporalio.exceptions +import temporalio.workflow +from temporalio.client._stream_store import ( + _STREAM_OPERATIONS, + StreamConfig, + _request_ref, +) +from temporalio.converter import StorageDriverWorkflowInfo + +from ._interceptor import StartNexusOperationInput + +if TYPE_CHECKING: + from ._workflow_instance import _WorkflowInstanceImpl + + +def _workflow_target() -> StorageDriverWorkflowInfo: + # External storage builds the same value for a Workflow. + info = temporalio.workflow.info() + return StorageDriverWorkflowInfo( + id=info.workflow_id, + type=info.workflow_type, + run_id=info.run_id, + namespace=info.namespace, + ) + + +@dataclasses.dataclass(frozen=True) +class _WorkflowStreamStore: + """The Worker's Stream Stores as Workflow code reaches them.""" + + config: StreamConfig + """The Stream config of the Worker's client.""" + + @classmethod + def from_config(cls, config: StreamConfig | None) -> _WorkflowStreamStore | None: + """Build it from the Stream config of a client, or give ``None`` when it has none.""" + return cls(config=config) if config is not None else None + + def start_operation( + self, + instance: _WorkflowInstanceImpl, + input: StartNexusOperationInput[Any, OutputT], + ) -> temporalio.workflow.NexusOperationHandle[OutputT]: + """Answer a Stream call of Workflow code with the Store that the call goes to.""" + stream_operation = _STREAM_OPERATIONS[input.operation_name] + # The Workflow `Stream` has no `append`, `close` or `read`, but the generated + # Workflow service still has their operations, so Workflow code can start one + # through a Nexus client. In this release, Workflow code writes and reads a Stream + # on a Store through its own Activities. + if stream_operation.workflow_refusal is not None: + raise temporalio.exceptions.ApplicationError( + stream_operation.workflow_refusal, non_retryable=True + ) + payload_converter, _ = instance._system_nexus_converters(input) + request = payload_converter._to_transfer_value(input.input) + # The choice runs in Workflow code, so a ref of a Store that the config doesn't + # have raises `StreamConfigError` here, with no I/O, and fails the Workflow Task. + store = self.config._store_for(_request_ref(input.input), _workflow_target()) + options = self.config.workflow_call_options + + async def result() -> OutputT: + try: + response = await store.call_from_workflow( + input.operation_name, request, options + ) + except temporalio.exceptions.ApplicationError as err: + # Core hands back a failed Local Activity's error without a wrapper. + stream_error = temporalio.exceptions._stream_error_from_failure(err) + if stream_error is not None: + raise stream_error from None + raise + except temporalio.exceptions.ActivityError as err: + # Workflow code gets the Store's error, not the Local Activity's wrapper. + if err.cause is not None: + stream_error = temporalio.exceptions._stream_error_from_failure( + err.cause + ) + if stream_error is not None: + raise stream_error from None + if isinstance( + err.cause, + ( + temporalio.exceptions.ApplicationError, + temporalio.exceptions.TimeoutError, + ), + ): + raise err.cause + raise + return payload_converter._from_transfer_value( + stream_operation.check_response(store, response), input.output_type + ) + + return _StreamStoreOperationHandle( + instance.create_task(result(), name=f"stream store: {input.operation_name}") + ) + + +class _StreamStoreOperationHandle(temporalio.workflow.NexusOperationHandle[OutputT]): + def __init__(self, task: asyncio.Task[OutputT]) -> None: + self._task = task + + def cancel(self) -> bool: + return self._task.cancel() + + def __await__(self) -> Generator[Any, Any, OutputT]: + return self._task.__await__() + + @property + def operation_token(self) -> str | None: + return None diff --git a/temporalio/worker/_worker.py b/temporalio/worker/_worker.py index e06bb99a4b..bbf68e2bf4 100644 --- a/temporalio/worker/_worker.py +++ b/temporalio/worker/_worker.py @@ -34,6 +34,7 @@ from ._interceptor import Interceptor from ._nexus import _NexusWorker from ._plugin import Plugin +from ._stream_store import _WorkflowStreamStore from ._tuning import WorkerTuner from ._workflow import ( _DEFAULT_WORKFLOW_TASK_EXTERNAL_STORAGE_CONCURRENCY, @@ -498,7 +499,11 @@ def _init_from_config(self, client: temporalio.client.Client, config: WorkerConf or bridge_client.config.runtime or temporalio.runtime.Runtime.default() ) - activities = config.get("activities") + activities = list(config.get("activities") or []) + stream_config = client_config.get("stream_config") + if stream_config is not None and config.get("workflows"): + for store in stream_config.stores: + activities.extend(store.worker_activities()) if activities: _warn_if_activity_executor_max_workers_is_inconsistent(config) self._activity_worker = _ActivityWorker( @@ -578,6 +583,7 @@ def check_activity(activity: str): encode_headers=client_config["header_codec_behavior"] != HeaderCodecBehavior.NO_CODEC, max_workflow_task_external_storage_concurrency=max_workflow_task_external_storage_concurrency, + stream_store=_WorkflowStreamStore.from_config(stream_config), ) tuner = config.get("tuner") @@ -667,7 +673,9 @@ def check_activity(activity: str): enable_workflows=self._workflow_worker is not None, enable_local_activities=self._activity_worker is not None and self._workflow_worker is not None, - enable_remote_activities=self._activity_worker is not None + # The Activities of the Stream Stores run as Local Activities, so + # alone they don't make this worker poll for Activity Tasks. + enable_remote_activities=bool(config.get("activities")) and not config.get("no_remote_activities"), enable_nexus=self._nexus_worker is not None, ), @@ -751,13 +759,19 @@ def client(self, value: temporalio.client.Client) -> None: Changing the client will make sure the worker starts using it for the next calls it makes. However, outstanding client calls will still complete with the existing client. The new client cannot be "lazy" and - must be using the same runtime as the current client. + must be using the same runtime and Stream Stores as the current client. """ bridge_client = _extract_bridge_client_for_worker(value) if self._runtime is not bridge_client._runtime: raise ValueError( "New client is not on the same runtime as the existing client" ) + # Running Workflows route their Stream calls by the Store that the worker + # started with, so a new client can't change it. + if _stream_config_stores(value) != _stream_config_stores(self.client): + raise ValueError( + "New client doesn't have the Stream Stores of the existing client" + ) assert bridge_client._bridge_client self._bridge_worker.replace_client(bridge_client._bridge_client) self._config["client"] = value @@ -1013,6 +1027,13 @@ async def __aexit__(self, exc_type: type[BaseException] | None, *args: Any) -> N raise self._async_context_run_exception +def _stream_config_stores( + client: temporalio.client.Client, +) -> list[temporalio.client.StreamStore]: + config = client.config(active_config=True).get("stream_config") + return list(config.stores) if config is not None else [] + + class WorkerConfig(TypedDict, total=False): """TypedDict of config originally passed to :py:class:`Worker`.""" diff --git a/temporalio/worker/_workflow.py b/temporalio/worker/_workflow.py index d8380a6b90..3e20e6adbb 100644 --- a/temporalio/worker/_workflow.py +++ b/temporalio/worker/_workflow.py @@ -40,6 +40,7 @@ WorkflowInboundInterceptor, WorkflowInterceptorClassInput, ) +from ._stream_store import _WorkflowStreamStore from ._workflow_instance import ( _DEFAULT_ENABLED_WORKFLOW_LOGIC_FLAGS, PatchActivationInput, @@ -128,6 +129,7 @@ def __init__( encode_headers: bool, max_workflow_task_external_storage_concurrency: int, default_workflow_logic_flags: frozenset[_WorkflowLogicFlag] | None = None, + stream_store: _WorkflowStreamStore | None = None, ) -> None: # Debug mode is enabled if specified or if the TEMPORAL_DEBUG env var is truthy debug_mode = debug_mode or bool(os.environ.get("TEMPORAL_DEBUG")) @@ -197,6 +199,7 @@ def __init__( self._max_workflow_task_external_storage_concurrency = ( max_workflow_task_external_storage_concurrency ) + self._stream_store = stream_store self._throw_after_activation: Exception | None = None # If debug mode is enabled, disable deadlock detection @@ -819,6 +822,7 @@ def _create_workflow_instance( nexus_serialization_context=temporalio.converter._serialization_context._propagated_nexus_serialization_context( init ), + stream_store=self._stream_store, ) if defn.sandboxed: return self._workflow_runner.create_instance(det) diff --git a/temporalio/worker/_workflow_instance.py b/temporalio/worker/_workflow_instance.py index 03463e2c03..09f1ae53fc 100644 --- a/temporalio/worker/_workflow_instance.py +++ b/temporalio/worker/_workflow_instance.py @@ -59,7 +59,14 @@ import temporalio.exceptions import temporalio.nexus.system import temporalio.workflow +from temporalio.client._stream_store import _STREAM_SERVICE from temporalio.converter import StorageDriverStoreContext, StorageDriverWorkflowInfo +from temporalio.nexus.system.stream_service.workflow._system_nexus_interceptor import ( + _start_system_nexus_operation as _start_stream_system_nexus_operation, +) +from temporalio.nexus.system.stream_service.workflow._system_nexus_interceptor import ( + _SystemNexusWorkflowOutboundInterceptorTerminal as _StreamSystemNexusInterceptorTerminal, +) from temporalio.nexus.system.workflow_service._system_nexus_interceptor import ( _start_system_nexus_operation, _SystemNexusWorkflowOutboundInterceptorTerminal, @@ -88,6 +95,7 @@ WorkflowInboundInterceptor, WorkflowOutboundInterceptor, ) +from ._stream_store import _WorkflowStreamStore logger = logging.getLogger(__name__) @@ -185,6 +193,10 @@ class WorkflowInstanceDetails: temporalio.converter.NexusSerializationContext | None ) = None """Serialization context of the Nexus operation handler that started the workflow, if any.""" + stream_store: _WorkflowStreamStore | None = None + """The Worker's Stream Stores as Workflow code reaches them, or ``None`` when the + Worker's client has none. This release has no Streams on the server, so with ``None`` + a Stream call fails the Workflow Task.""" class WorkflowInstance(ABC): @@ -301,6 +313,7 @@ def __init__(self, det: WorkflowInstanceDetails) -> None: # the Nexus caller, which decodes them with the handler's Nexus context. A retried or cron run # reads the previous run's result and failure with the same context. self._nexus_serialization_context = det.nexus_serialization_context + self._stream_store = det.stream_store if self._nexus_serialization_context: self._result_payload_converter = self._payload_converter_with_context( self._nexus_serialization_context @@ -1830,23 +1843,25 @@ async def workflow_start_nexus_operation( event_groups: Sequence[temporalio.workflow.EventGroup] | None = None, ) -> temporalio.workflow.NexusOperationHandle[OutputT]: if temporalio.nexus.system.is_system_endpoint(endpoint): - return await _start_system_nexus_operation( - self._outbound, - StartNexusOperationInput( - endpoint=temporalio.nexus.system.TEMPORAL_SYSTEM_ENDPOINT, - service=service, - operation=operation, - input=input, - output_type=output_type, - schedule_to_close_timeout=schedule_to_close_timeout, - schedule_to_start_timeout=schedule_to_start_timeout, - start_to_close_timeout=start_to_close_timeout, - cancellation_type=cancellation_type, - headers=None, - summary=summary, - event_groups=event_groups, - ), + system_input = StartNexusOperationInput( + endpoint=temporalio.nexus.system.TEMPORAL_SYSTEM_ENDPOINT, + service=service, + operation=operation, + input=input, + output_type=output_type, + schedule_to_close_timeout=schedule_to_close_timeout, + schedule_to_start_timeout=schedule_to_start_timeout, + start_to_close_timeout=start_to_close_timeout, + cancellation_type=cancellation_type, + headers=None, + summary=summary, + event_groups=event_groups, ) + if service == _STREAM_SERVICE.name: + return await _start_stream_system_nexus_operation( + self._outbound, system_input + ) + return await _start_system_nexus_operation(self._outbound, system_input) return await self._outbound.start_nexus_operation( StartNexusOperationInput( endpoint=endpoint, @@ -2288,6 +2303,7 @@ async def _outbound_start_nexus_operation( # examples of the evolution of the resulting handle state machine in the sync and async # Nexus response cases. handle: _NexusOperationHandle[OutputT] + payload_converter: temporalio.converter.PayloadConverter async def operation_handle_fn() -> OutputT: return cast( @@ -2299,25 +2315,7 @@ async def operation_handle_fn() -> OutputT: ) if temporalio.nexus.system.is_system_endpoint(input.endpoint): - serialization_context = temporalio.nexus.system._get_serialization_context( - input.service, - input.operation_name, - input.input, - ) - user_payload_converter = self._context_free_payload_converter - user_failure_converter = self._context_free_failure_converter - if serialization_context is not None: - user_payload_converter = self._payload_converter_with_context( - serialization_context - ) - user_failure_converter = self._failure_converter_with_context( - serialization_context - ) - payload_converter = temporalio.nexus.system._get_payload_converter( - user_payload_converter, - user_failure_converter, - ) - failure_converter = user_failure_converter + payload_converter, failure_converter = self._system_nexus_converters(input) else: serialization_context = temporalio.converter.NexusSerializationContext( endpoint=input.endpoint, @@ -2355,9 +2353,46 @@ async def _intercept_system_nexus_operation( async def _schedule_system_nexus_operation( self, input: StartNexusOperationInput[Any, OutputT] - ) -> _NexusOperationHandle[OutputT]: + ) -> temporalio.workflow.NexusOperationHandle[OutputT]: + # This release has no Streams on the server, so a Worker answers a Stream call with + # a Store of its client. On a Worker whose client has none, the `StreamConfigError` + # fails the Workflow Task. Every other system Nexus operation goes to the server. + if input.service == _STREAM_SERVICE.name: + store = self._stream_store + if store is None: + raise temporalio.exceptions.StreamConfigError( + "The client of this Worker has no Stream Store. Give one in " + "stream_config=StreamConfig(stores=[...])." + ) + return store.start_operation(self, input) return await self._outbound_start_nexus_operation(input) + def _system_nexus_converters( + self, input: StartNexusOperationInput[Any, Any] + ) -> tuple[ + temporalio.nexus.system._SystemNexusPayloadConverter, + temporalio.converter.FailureConverter, + ]: + serialization_context = temporalio.nexus.system._get_serialization_context( + input.service, + input.operation_name, + input.input, + ) + user_payload_converter = self._context_free_payload_converter + user_failure_converter = self._context_free_failure_converter + if serialization_context is not None: + user_payload_converter = self._payload_converter_with_context( + serialization_context + ) + user_failure_converter = self._failure_converter_with_context( + serialization_context + ) + payload_converter = temporalio.nexus.system._get_payload_converter( + user_payload_converter, + user_failure_converter, + ) + return payload_converter, user_failure_converter + #### Miscellaneous helpers #### # These are in alphabetical order. @@ -3360,7 +3395,9 @@ async def handle_update_handler(self, input: HandleUpdateInput) -> Any: class _WorkflowOutboundImpl( - _SystemNexusWorkflowOutboundInterceptorTerminal, WorkflowOutboundInterceptor + _SystemNexusWorkflowOutboundInterceptorTerminal, + _StreamSystemNexusInterceptorTerminal, + WorkflowOutboundInterceptor, ): def __init__(self, instance: _WorkflowInstanceImpl) -> None: # type: ignore # We are intentionally not calling the base class's __init__ here @@ -3407,7 +3444,7 @@ async def _intercept_system_nexus_operation( async def start_system_nexus_operation( self, input: StartNexusOperationInput[Any, OutputT] - ) -> _NexusOperationHandle[OutputT]: + ) -> temporalio.workflow.NexusOperationHandle[OutputT]: return await self._instance._schedule_system_nexus_operation(input) def start_local_activity( diff --git a/tests/contrib/redis/__init__.py b/tests/contrib/redis/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/contrib/redis/test_streams.py b/tests/contrib/redis/test_streams.py new file mode 100644 index 0000000000..0bae0921f1 --- /dev/null +++ b/tests/contrib/redis/test_streams.py @@ -0,0 +1,243 @@ +from __future__ import annotations + +import asyncio +import os +import socket +import uuid +from collections.abc import Awaitable +from datetime import timedelta + +import pytest + +from temporalio import activity, workflow +from temporalio.api.enums.v1 import StreamErrorReason +from temporalio.client import Client, StreamConfig, StreamStore +from temporalio.contrib.redis.streams import RedisStreamStore +from temporalio.exceptions import StreamError +from temporalio.nexus.system.stream_service import ( + ClientProducer, + Producer, + ProducerKindClient, + ProducerKindLocalActivity, + ProducerKindWorkflowActivity, + StreamRef, + StreamStatus, +) +from temporalio.nexus.system.stream_service.client import Stream +from temporalio.nexus.system.stream_service.workflow import create_stream +from tests.helpers import new_worker + +_REDIS_URL = os.environ.get("STREAMS_REDIS_URL") + +pytestmark = pytest.mark.skipif( + not _REDIS_URL, reason="STREAMS_REDIS_URL is not set to a Redis server" +) + + +def _store() -> RedisStreamStore: + assert _REDIS_URL + # Each test has its own key prefix, so that runs on one Redis don't share Streams. + return RedisStreamStore(_REDIS_URL, key_prefix=f"test-{uuid.uuid4()}") + + +def _with_store(client: Client, store: StreamStore) -> Client: + config = client.config() + config["stream_config"] = StreamConfig(stores=[store]) + return Client(**config) + + +async def _create_stream(client: Client) -> Stream[str]: + return await client.create_stream(id=f"stream-{uuid.uuid4()}", body_type=str) + + +async def _stream_error(call: Awaitable[object]) -> StreamError: + with pytest.raises(StreamError) as err: + await call + return err.value + + +@activity.defn +async def append_values(ref: StreamRef[str], values: list[str]) -> None: + await activity.client().get_stream(ref, body_type=str).append(*values) + + +@workflow.defn +class AppendWorkflow: + @workflow.run + async def run(self, stream_id: str) -> StreamRef[str]: + stream = await create_stream(id=stream_id, body_type=str) + await workflow.execute_local_activity( + append_values, + args=[stream.ref, ["a", "b"]], + start_to_close_timeout=timedelta(seconds=10), + ) + return stream.ref + + +@activity.defn +async def append_in_activity(stream_id: str) -> None: + stream = activity.client().get_stream(StreamRef(id=stream_id), body_type=str) + await stream.append("from-activity") + + +@workflow.defn +class RunActivityWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + await workflow.execute_activity( + append_in_activity, + stream_id, + start_to_close_timeout=timedelta(seconds=10), + ) + + +async def test_client_creates_appends_reads_and_closes(client: Client): + stream_client = _with_store(client, _store()) + stream = await _create_stream(stream_client) + assert stream.created + assert stream.ref.store == "redis" + + appended = await stream.append("a", "b") + assert appended.first_offset is not None + assert appended.last_offset is not None + records = [record async for record in stream.iter_read_items()] + assert [(record.body, record.offset) for record in records] == [ + ("a", appended.first_offset), + ("b", appended.last_offset), + ] + + await stream.close() + described = await stream.describe() + assert described.status == StreamStatus.StreamStatusClosed + assert described.last_offset == appended.last_offset + page = await stream.read(from_offset=appended.last_offset) + assert page.done + assert page.records == [] + + +async def test_workflow_local_activity_appends_with_local_activity_producer( + client: Client, +): + stream_client = _with_store(client, _store()) + async with new_worker( + stream_client, AppendWorkflow, activities=[append_values] + ) as worker: + handle = await stream_client.start_workflow( + AppendWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + ref = await handle.result() + + stream = stream_client.get_stream(ref, body_type=str) + records = [record async for record in stream.iter_read_items()] + assert [record.body for record in records] == ["a", "b"] + run_id = handle.result_run_id + for record in records: + assert record.producer is not None + assert isinstance(record.producer.kind, ProducerKindLocalActivity) + assert record.producer.kind.value.run_id == run_id + + +async def test_activity_appends_with_activity_producer(client: Client): + stream_client = _with_store(client, _store()) + stream = await _create_stream(stream_client) + async with new_worker( + stream_client, RunActivityWorkflow, activities=[append_in_activity] + ) as worker: + handle = await stream_client.start_workflow( + RunActivityWorkflow.run, + stream.ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + await handle.result() + + [record] = [record async for record in stream.iter_read_items()] + assert record.body == "from-activity" + assert record.producer is not None + assert isinstance(record.producer.kind, ProducerKindWorkflowActivity) + assert record.producer.kind.value.run_id == handle.result_run_id + + +async def test_client_append_with_sequence_writes_once(client: Client): + stream = await _create_stream(_with_store(client, _store())) + producer = Producer(id="mine", kind=ProducerKindClient(ClientProducer())) + first = await stream.append("a", "b", producer=producer, sequence=5) + second = await stream.append("a", "b", producer=producer, sequence=5) + assert second == first + records = [record async for record in stream.iter_read_items()] + assert [record.body for record in records] == ["a", "b"] + assert [record.offset for record in records] == [ + first.first_offset, + first.last_offset, + ] + + +async def test_failed_calls_raise_stream_error_with_reason(client: Client): + stream_client = _with_store(client, _store()) + stream = await _create_stream(stream_client) + + err = await _stream_error( + stream_client.create_stream(id=stream.ref.id, body_type=str) + ) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_ALREADY_EXISTS + + unknown = stream_client.get_stream( + StreamRef(id=f"stream-{uuid.uuid4()}"), body_type=str + ) + err = await _stream_error(unknown.describe()) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_NOT_FOUND + + await stream.close() + err = await _stream_error(stream.append("a")) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_CLOSED + + +async def test_read_with_wait_gets_a_later_record(client: Client): + stream_client = _with_store(client, _store()) + stream = await _create_stream(stream_client) + + async def append_later() -> None: + await asyncio.sleep(0.5) + await stream.append("late") + + append = asyncio.create_task(append_later()) + page = await stream.read(wait=timedelta(seconds=10)) + await append + assert [record.body for record in page.records] == ["late"] + + +async def test_key_prefixes_keep_streams_apart(client: Client): + first = _with_store(client, _store()) + second = _with_store(client, _store()) + stream = await _create_stream(first) + await stream.append("a") + + other = second.get_stream(StreamRef(id=stream.ref.id), body_type=str) + err = await _stream_error(other.describe()) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_NOT_FOUND + created = await second.create_stream(id=stream.ref.id, body_type=str) + assert created.created + + +def _free_port() -> int: + with socket.socket() as sock: + sock.bind(("127.0.0.1", 0)) + return sock.getsockname()[1] + + +async def test_store_without_server_fails_at_first_call(client: Client): + store = RedisStreamStore(f"redis://127.0.0.1:{_free_port()}") + stream_client = _with_store(client, store) + err = await _stream_error(_create_stream(stream_client)) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_STORAGE_UNAVAILABLE + assert not err.non_retryable + + +async def test_bad_option_fails_with_the_crate_message(client: Client): + assert _REDIS_URL + store = RedisStreamStore([_REDIS_URL, _REDIS_URL]) + with pytest.raises(ValueError, match="cluster"): + await _create_stream(_with_store(client, store)) diff --git a/tests/helpers/streams.py b/tests/helpers/streams.py new file mode 100644 index 0000000000..beb4eaaa45 --- /dev/null +++ b/tests/helpers/streams.py @@ -0,0 +1,186 @@ +"""Stream Stores that the Stream tests share.""" + +from __future__ import annotations + +import dataclasses +from collections.abc import Awaitable, Callable +from typing import TypeVar + +from google.protobuf.message import Message + +from temporalio.api.enums.v1 import ( + StreamErrorReason, + StreamIdConflictPolicy, + StreamStatus, +) +from temporalio.api.stream.v1 import StreamRecord, StreamRef +from temporalio.api.streamservice.v1 import ( + AppendRecordsRequest, + AppendRecordsResponse, + CloseStreamRequest, + CloseStreamResponse, + CreateStreamRequest, + CreateStreamResponse, + DescribeStreamRequest, + DescribeStreamResponse, + ReadRecordsRequest, + ReadRecordsResponse, +) +from temporalio.client import StreamStore +from temporalio.exceptions import StreamError + +_RequestT = TypeVar("_RequestT", bound=Message) +_ResponseT = TypeVar("_ResponseT", bound=Message) + + +class RecordingStreamStore(StreamStore): + """A Store that passes each call to another Store and keeps the operation names. + + It has the name of the Store that it wraps, since that Store writes its own name + in the ref of a create. A test overrides :py:meth:`before_call` to fail a call or + to hold it. + """ + + def __init__(self, inner: StreamStore) -> None: + self.inner = inner + self.operations: list[str] = [] + + @property + def name(self) -> str: + return self.inner.name + + async def before_call(self, operation: str, request: Message) -> None: + del operation, request + + async def _forward( + self, + operation: str, + request: _RequestT, + call: Callable[[_RequestT], Awaitable[_ResponseT]], + ) -> _ResponseT: + self.operations.append(operation) + await self.before_call(operation, request) + return await call(request) + + async def create_stream(self, request: CreateStreamRequest) -> CreateStreamResponse: + return await self._forward("CreateStream", request, self.inner.create_stream) + + async def append_records( + self, request: AppendRecordsRequest + ) -> AppendRecordsResponse: + return await self._forward("AppendRecords", request, self.inner.append_records) + + async def read_records(self, request: ReadRecordsRequest) -> ReadRecordsResponse: + return await self._forward("ReadRecords", request, self.inner.read_records) + + async def close_stream(self, request: CloseStreamRequest) -> CloseStreamResponse: + return await self._forward("CloseStream", request, self.inner.close_stream) + + async def describe_stream( + self, request: DescribeStreamRequest + ) -> DescribeStreamResponse: + return await self._forward( + "DescribeStream", request, self.inner.describe_stream + ) + + +@dataclasses.dataclass +class _DictStream: + request_id: str + records: list[StreamRecord] = dataclasses.field(default_factory=list) + closed: bool = False + + +def _offset(index: int) -> bytes: + return index.to_bytes(8, "big") + + +class DictStreamStore(StreamStore): + """A Store written from scratch, with no bridge, that keeps its Streams in a dict. + + It has enough of each operation for the tests, and no dedupe, no wait and no + Retention. + """ + + def __init__(self, name: str = "dict") -> None: + self._name = name + self.streams: dict[str, _DictStream] = {} + + @property + def name(self) -> str: + return self._name + + def _stream(self, ref: StreamRef) -> _DictStream: + stream = self.streams.get(ref.id) + if stream is None: + raise StreamError( + f"No Stream '{ref.id}'", + reason=StreamErrorReason.STREAM_ERROR_REASON_NOT_FOUND, + ) + return stream + + async def create_stream(self, request: CreateStreamRequest) -> CreateStreamResponse: + ref = StreamRef(id=request.stream_ref.id, store=self.name) + stream = self.streams.get(ref.id) + if stream is None: + self.streams[ref.id] = _DictStream(request_id=request.request_id) + return CreateStreamResponse(ref=ref, created=True) + if stream.request_id == request.request_id: + return CreateStreamResponse(ref=ref, created=True) + if ( + request.id_conflict_policy + == StreamIdConflictPolicy.STREAM_ID_CONFLICT_POLICY_USE_EXISTING + ): + return CreateStreamResponse(ref=ref, created=False) + raise StreamError( + f"The Stream '{ref.id}' exists", + reason=StreamErrorReason.STREAM_ERROR_REASON_ALREADY_EXISTS, + ) + + async def append_records( + self, request: AppendRecordsRequest + ) -> AppendRecordsResponse: + stream = self._stream(request.ref) + if stream.closed: + raise StreamError( + f"The Stream '{request.ref.id}' is closed", + reason=StreamErrorReason.STREAM_ERROR_REASON_CLOSED, + ) + first = len(stream.records) + 1 + for record in request.records: + stream.records.append( + StreamRecord( + producer=request.producer, + body=record.body, + offset=_offset(len(stream.records) + 1), + ) + ) + return AppendRecordsResponse( + first_offset=_offset(first), last_offset=_offset(len(stream.records)) + ) + + async def read_records(self, request: ReadRecordsRequest) -> ReadRecordsResponse: + stream = self._stream(request.ref) + start = int.from_bytes(request.from_offset, "big") if request.from_offset else 0 + page = stream.records[start : start + (request.max_records or 1000)] + last_offset = page[-1].offset if page else request.from_offset + return ReadRecordsResponse( + records=page, + last_offset=last_offset, + done=stream.closed and start + len(page) == len(stream.records), + ) + + async def close_stream(self, request: CloseStreamRequest) -> CloseStreamResponse: + self._stream(request.ref).closed = True + return CloseStreamResponse() + + async def describe_stream( + self, request: DescribeStreamRequest + ) -> DescribeStreamResponse: + stream = self._stream(request.ref) + return DescribeStreamResponse( + status=StreamStatus.STREAM_STATUS_CLOSED + if stream.closed + else StreamStatus.STREAM_STATUS_OPEN, + last_offset=stream.records[-1].offset if stream.records else b"", + ) diff --git a/tests/nexus/test_stream_appender.py b/tests/nexus/test_stream_appender.py new file mode 100644 index 0000000000..fd320ff474 --- /dev/null +++ b/tests/nexus/test_stream_appender.py @@ -0,0 +1,159 @@ +from __future__ import annotations + +import asyncio +import uuid +from datetime import timedelta + +import pytest +from google.protobuf.message import Message + +import temporalio.api.streamservice.v1 +from temporalio.api.enums.v1 import StreamErrorReason +from temporalio.client import BufferedAppender, Client +from temporalio.nexus.system.stream_service import StreamRef +from temporalio.nexus.system.stream_service.client import Stream +from temporalio.testing import MemoryStreamStore +from tests.helpers.streams import RecordingStreamStore +from tests.nexus.test_stream_store import _create_stream, _stream_error, _with_store + +# Long enough that only a flush, a full batch or the end of `async with` starts a write. +_NEVER = timedelta(hours=1) + + +class RecordingStore(RecordingStreamStore): + def __init__(self) -> None: + super().__init__(MemoryStreamStore()) + self.batches: list[int] = [] + self.append_started = asyncio.Event() + self.release: asyncio.Event | None = None + + async def before_call(self, operation: str, request: Message) -> None: + if isinstance(request, temporalio.api.streamservice.v1.AppendRecordsRequest): + self.batches.append(len(request.records)) + self.append_started.set() + if self.release is not None: + await self.release.wait() + + +async def _bodies(stream: Stream[str]) -> list[str]: + return [record.body async for record in stream.iter_read_items()] + + +async def test_appends_inside_one_delay_go_as_one_batch(client: Client): + store = RecordingStore() + stream = await _create_stream(client, store) + appender = BufferedAppender(stream, max_delay=_NEVER) + appender.append("a") + appender.append("b", "c") + appender.append("d") + await appender.flush() + assert store.batches == [4] + assert await _bodies(stream) == ["a", "b", "c", "d"] + + +async def test_flush_returns_the_last_offset(client: Client): + stream = await _create_stream(client, MemoryStreamStore()) + appender = BufferedAppender(stream) + assert await appender.flush() is None + appender.append("a", "b") + offset = await appender.flush() + assert offset is not None + assert offset == (await stream.describe()).last_offset + + +async def test_appends_during_a_write_go_in_the_next_batch(client: Client): + store = RecordingStore() + stream = await _create_stream(client, store) + store.release = asyncio.Event() + appender = BufferedAppender(stream, max_delay=_NEVER) + appender.append("a") + first = asyncio.create_task(appender.flush()) + await store.append_started.wait() + appender.append("b") + appender.append("c") + assert store.batches == [1] + store.release.set() + await first + await appender.flush() + assert store.batches == [1, 2] + assert await _bodies(stream) == ["a", "b", "c"] + + +async def test_a_large_buffer_goes_as_batches_of_at_most_1000(client: Client): + store = RecordingStore() + stream = await _create_stream(client, store) + appender = BufferedAppender(stream, max_delay=_NEVER) + values = [str(index) for index in range(2500)] + appender.append(*values) + # A full batch doesn't wait for the delay or a flush. + await store.append_started.wait() + await appender.flush() + assert store.batches == [1000, 1000, 500] + assert await _bodies(stream) == values + + +async def test_the_timer_alone_writes_the_batch(client: Client): + store = RecordingStore() + stream = await _create_stream(client, store) + appender = BufferedAppender(stream, max_delay=timedelta(milliseconds=10)) + appender.append("a", "b") + page = await stream.read(wait=timedelta(seconds=10)) + assert [record.body for record in page.records] == ["a", "b"] + assert store.batches == [2] + + +async def test_a_failed_batch_stops_the_appender(client: Client): + store = RecordingStore() + stream = await _create_stream(client, store) + await stream.close() + store.release = asyncio.Event() + appender = BufferedAppender(stream, max_delay=_NEVER) + appender.append("a") + first = asyncio.create_task(appender.flush()) + await store.append_started.wait() + appender.append("b", "c") + store.release.set() + err = await _stream_error(first) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_CLOSED + assert await _stream_error(appender.flush()) is err + with pytest.raises(type(err)) as later: + appender.append("d") + assert later.value is err + assert appender.unwritten == 3 + assert store.batches == [1] + + +async def test_async_with_flushes_at_its_end(client: Client): + store = RecordingStore() + stream = await _create_stream(client, store) + async with BufferedAppender(stream, max_delay=_NEVER) as appender: + appender.append("a", "b") + assert store.batches == [2] + assert await _bodies(stream) == ["a", "b"] + + +async def test_async_with_keeps_the_error_of_its_block(client: Client): + store = RecordingStore() + stream = await _create_stream(client, store) + await stream.close() + appender = BufferedAppender(stream, max_delay=_NEVER) + with pytest.raises(RuntimeError, match="the block failed"): + async with appender: + appender.append("a") + raise RuntimeError("the block failed") + # The flush at the end of the block ran and failed. + assert store.batches == [1] + assert appender.unwritten == 1 + err = await _stream_error(appender.flush()) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_CLOSED + + +async def test_append_without_a_value_fails(client: Client): + stream = _with_store(client, MemoryStreamStore()).get_stream( + StreamRef(id=f"stream-{uuid.uuid4()}"), body_type=str + ) + appender = BufferedAppender(stream) + with pytest.raises(TypeError, match="one or more values"): + appender.append() + with pytest.raises(ValueError, match="max_delay must not be negative"): + BufferedAppender(stream, max_delay=timedelta(seconds=-1)) diff --git a/tests/nexus/test_stream_store.py b/tests/nexus/test_stream_store.py new file mode 100644 index 0000000000..db37459f9b --- /dev/null +++ b/tests/nexus/test_stream_store.py @@ -0,0 +1,630 @@ +from __future__ import annotations + +import ast +import dataclasses +import subprocess +import sys +import uuid +from collections.abc import Awaitable, Callable +from datetime import datetime, timedelta, timezone +from pathlib import Path +from unittest import mock + +import google.protobuf.timestamp_pb2 +import pytest + +import temporalio +import temporalio.activity +import temporalio.api.stream.v1 +import temporalio.api.workflowservice.v1 +import temporalio.client._stream_store +import temporalio.common +import temporalio.converter._payload_converter +import temporalio.exceptions +import temporalio.nexus.system +import temporalio.nexus.system._support +import temporalio.workflow +from temporalio.api.enums.v1 import StreamErrorReason +from temporalio.client import ( + Client, + StreamConfig, + StreamSelectContext, + StreamStore, +) +from temporalio.client._stream_store import _STREAM_OPERATIONS, _request_ref +from temporalio.converter import DataConverter, StorageDriverActivityInfo +from temporalio.exceptions import ApplicationError, StreamConfigError, StreamError +from temporalio.nexus.system.stream_service import ( + ClientProducer, + DescribeStreamRequest, + Producer, + ProducerKindClient, + ProducerKindLocalActivity, + ProducerKindStandaloneActivity, + ProducerKindWorkflowActivity, + StreamIdConflictPolicy, + StreamRef, + StreamService, + StreamStatus, + Timestamp, +) +from temporalio.nexus.system.stream_service.client import ( + Stream, + StreamServiceClientExtension, +) +from temporalio.testing import ( + ActivityEnvironment, + MemoryStreamStore, + WorkflowEnvironment, +) +from tests import DEV_SERVER_DOWNLOAD_VERSION +from tests.helpers.streams import DictStreamStore +from tests.test_converter import SimpleCodec + + +def _with_store(client: Client, store: StreamStore) -> Client: + return _with_config(client, StreamConfig(stores=[store])) + + +def _with_config(client: Client, stream_config: StreamConfig) -> Client: + config = client.config() + config["stream_config"] = stream_config + return Client(**config) + + +def _two_stores( + selector: Callable[[StreamSelectContext], StreamStore], +) -> tuple[MemoryStreamStore, MemoryStreamStore, StreamConfig]: + first = MemoryStreamStore(name="first") + second = MemoryStreamStore(name="second") + return first, second, StreamConfig(stores=[first, second], store_selector=selector) + + +async def _create_stream(client: Client, store: StreamStore) -> Stream[str]: + return await _with_store(client, store).create_stream( + id=f"stream-{uuid.uuid4()}", body_type=str + ) + + +async def _only_producer(stream: Stream[str]) -> Producer: + [record] = [record async for record in stream.iter_read_items()] + assert record.producer is not None + return record.producer + + +async def _append_in_activity( + client: Client, info: temporalio.activity.Info +) -> Producer: + stream = await _create_stream(client, MemoryStreamStore()) + env = ActivityEnvironment() + env.info = info + await env.run(stream.append, "a") + return await _only_producer(stream) + + +def _timestamp(value: datetime) -> Timestamp: + proto = google.protobuf.timestamp_pb2.Timestamp() + proto.FromDatetime(value) + return Timestamp(seconds=proto.seconds, nanos=proto.nanos) + + +async def _stream_error(call: Awaitable[object]) -> StreamError: + with pytest.raises(StreamError) as err: + await call + return err.value + + +async def test_client_stream_uses_store(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + + stream = await stream_client.create_stream( + id=f"stream-{uuid.uuid4()}", body_type=str + ) + assert stream.created + assert stream.ref.store == "memory" + appended = await stream.append("a", "b") + assert appended.first_offset is not None + assert appended.last_offset is not None + assert appended.first_offset < appended.last_offset + records = [record async for record in stream.iter_read_items()] + assert [(record.body, record.offset) for record in records] == [ + ("a", appended.first_offset), + ("b", appended.last_offset), + ] + + other = stream_client.get_stream(stream.ref, body_type=str) + assert [record.body async for record in other.iter_read_items()] == ["a", "b"] + + described = await stream.describe() + assert described.status == StreamStatus.StreamStatusOpen + assert described.last_offset == appended.last_offset + + await stream.close() + described = await stream.describe() + assert described.status == StreamStatus.StreamStatusClosed + assert described.last_offset == appended.last_offset + page = await stream.read(from_offset=records[-1].offset) + assert page.done + assert page.records == [] + pages = [page async for page in stream.iter_read()] + assert pages[-1].done + assert [record.body for page in pages for record in page.records] == ["a", "b"] + + +async def test_client_stream_uses_a_store_written_from_scratch(client: Client): + store = DictStreamStore() + config = client.config() + config["data_converter"] = DataConverter(payload_codec=SimpleCodec()) + config["stream_config"] = StreamConfig(stores=[store]) + stream_client = Client(**config) + + stream_id = f"stream-{uuid.uuid4()}" + stream = await stream_client.create_stream(id=stream_id, body_type=str) + assert stream.created + assert stream.ref.store == "dict" + appended = await stream.append("a", "b") + assert [record.body async for record in stream.iter_read_items()] == ["a", "b"] + # The Store gets payloads that the client already encoded with the codec. + stored = store.streams[stream_id].records + assert [record.body.metadata["simple-codec"] for record in stored] == [ + b"true", + b"true", + ] + described = await stream.describe() + assert described.status == StreamStatus.StreamStatusOpen + assert described.last_offset == appended.last_offset + err = await _stream_error(stream_client.create_stream(id=stream_id, body_type=str)) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_ALREADY_EXISTS + + await stream.close() + err = await _stream_error(stream.append("c")) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_CLOSED + + +def test_stream_operations_match_the_stream_service(): + service = temporalio.client._stream_store._STREAM_SERVICE + assert set(_STREAM_OPERATIONS) == set(service.operation_definitions) + for name, operation in _STREAM_OPERATIONS.items(): + definition = service.operation_definitions[name] + for model, message in ( + (definition.input_type, operation.request_type), + (definition.output_type, operation.response_type), + ): + converter = ( + temporalio.converter._payload_converter._get_transfer_type_converter( + model + ) + ) + assert converter is not None + assert converter.transfer_type is message + assert callable(getattr(StreamStore, operation.store_method)) + + +async def test_client_strict_create_of_existing_stream_fails(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + await stream_client.create_stream(id=stream_id, body_type=str) + err = await _stream_error(stream_client.create_stream(id=stream_id, body_type=str)) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_ALREADY_EXISTS + assert err.non_retryable + existing = await stream_client.create_stream( + id=stream_id, + body_type=str, + id_conflict_policy=StreamIdConflictPolicy.StreamIdConflictPolicyUseExisting, + ) + # The response's `created` is false, which the generated model reads as unset. + assert not existing.created + + +async def test_client_read_or_describe_of_unknown_stream_fails(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream = stream_client.get_stream( + StreamRef(id=f"stream-{uuid.uuid4()}"), body_type=str + ) + for call in (stream.read(), stream.describe()): + err = await _stream_error(call) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_NOT_FOUND + + +async def test_client_append_after_close_fails(client: Client): + stream = await _create_stream(client, MemoryStreamStore()) + await stream.append("a") + await stream.close() + err = await _stream_error(stream.append("b")) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_CLOSED + assert err.type == "StreamError" + + +async def test_client_stream_store_uses_payload_codec(client: Client): + store = MemoryStreamStore() + config = client.config() + config["data_converter"] = DataConverter(payload_codec=SimpleCodec()) + config["stream_config"] = StreamConfig(stores=[store]) + stream_client = Client(**config) + + stream = await stream_client.create_stream( + id=f"stream-{uuid.uuid4()}", body_type=str + ) + await stream.append("a", "b") + # A client without the codec reads the bodies as the Store keeps them. + raw = _with_store(client, store).get_stream( + StreamRef(id=stream.ref.id), body_type=temporalio.common.RawValue + ) + stored = [record.body.payload async for record in raw.iter_read_items()] + assert [body.metadata["simple-codec"] for body in stored] == [b"true", b"true"] + assert [record.body async for record in stream.iter_read_items()] == ["a", "b"] + + +async def test_client_append_has_client_producer(client: Client): + stream = await _create_stream(client, MemoryStreamStore()) + await stream.append("a") + producer = await _only_producer(stream) + assert producer.id.startswith("client:") + assert isinstance(producer.kind, ProducerKindClient) + + +async def test_workflow_activity_append_has_activity_producer(client: Client): + scheduled = datetime(2026, 11, 12, 10, 0, 0, 123456, tzinfo=timezone.utc) + attempt_scheduled = scheduled + timedelta(seconds=30) + producer = await _append_in_activity( + client, + dataclasses.replace( + ActivityEnvironment.default_info(), + activity_id="step:1", + attempt=3, + workflow_run_id="run-1", + scheduled_time=scheduled, + current_attempt_scheduled_time=attempt_scheduled, + ), + ) + epoch_nanos = ( + (scheduled - datetime(1970, 1, 1, tzinfo=timezone.utc)) + // timedelta(microseconds=1) + * 1000 + ) + assert producer.id == f"act:run-1:step%3A1:{epoch_nanos}" + assert isinstance(producer.kind, ProducerKindWorkflowActivity) + assert producer.kind.value.run_id == "run-1" + assert producer.kind.value.epoch.attempt == 3 + assert producer.kind.value.epoch.attempt_scheduled_time == _timestamp( + attempt_scheduled + ) + + +async def test_standalone_activity_append_has_activity_producer(client: Client): + attempt_scheduled = datetime(2026, 11, 12, 10, 0, 30, tzinfo=timezone.utc) + producer = await _append_in_activity( + client, + dataclasses.replace( + ActivityEnvironment.default_info(), + activity_id="job", + activity_run_id="job-run", + attempt=2, + workflow_id=None, + workflow_namespace=None, + workflow_run_id=None, + workflow_type=None, + current_attempt_scheduled_time=attempt_scheduled, + ), + ) + assert producer.id == "sa:job:job-run" + assert isinstance(producer.kind, ProducerKindStandaloneActivity) + assert not producer.kind.value.run_id + assert producer.kind.value.epoch.attempt == 2 + assert producer.kind.value.epoch.attempt_scheduled_time == _timestamp( + attempt_scheduled + ) + + +async def test_local_activity_append_has_local_activity_producer(client: Client): + task_started = datetime(2026, 11, 12, 10, 0, 0, 123456, tzinfo=timezone.utc) + producer = await _append_in_activity( + client, + dataclasses.replace( + ActivityEnvironment.default_info(), + activity_id="3", + attempt=2, + is_local=True, + workflow_run_id="run:1", + workflow_task=temporalio.activity.WorkflowTaskAttempt( + started_event_id=7, attempt=1, started_time=task_started + ), + ), + ) + assert producer.id == "la:run%3A1:3" + assert isinstance(producer.kind, ProducerKindLocalActivity) + assert producer.kind.value.run_id == "run:1" + assert producer.kind.value.epoch.task.started_event_id == 7 + assert producer.kind.value.epoch.task.attempt == 1 + assert producer.kind.value.epoch.task.started_time == _timestamp(task_started) + assert producer.kind.value.epoch.attempt == 2 + + +async def test_local_activity_append_without_workflow_task_fails(client: Client): + with pytest.raises(RuntimeError, match="Workflow Task"): + await _append_in_activity( + client, + dataclasses.replace(ActivityEnvironment.default_info(), is_local=True), + ) + + +async def test_explicit_producer_is_sent(client: Client): + stream = await _create_stream(client, MemoryStreamStore()) + with mock.patch.object( + temporalio.nexus.system._support, "default_producer" + ) as default_producer: + await stream.append( + "a", producer=Producer(id="mine", kind=ProducerKindClient(ClientProducer())) + ) + default_producer.assert_not_called() + producer = await _only_producer(stream) + assert producer.id == "mine" + assert isinstance(producer.kind, ProducerKindClient) + + +async def test_client_append_with_sequence_writes_once(client: Client): + stream = await _create_stream(client, MemoryStreamStore()) + producer = Producer(id="mine", kind=ProducerKindClient(ClientProducer())) + first = await stream.append("a", "b", producer=producer, sequence=5) + second = await stream.append("a", "b", producer=producer, sequence=5) + assert second == first + records = [record async for record in stream.iter_read_items()] + assert [record.body for record in records] == ["a", "b"] + assert [record.offset for record in records] == [ + first.first_offset, + first.last_offset, + ] + + +def test_client_stream_methods_are_generated(): + assert "create_stream" not in vars(Client) + assert "get_stream" not in vars(Client) + assert Client.create_stream is StreamServiceClientExtension.create_stream + assert Client.get_stream is StreamServiceClientExtension.get_stream + + +def test_no_hand_written_stream_classes(): + root = Path(temporalio.__file__).parent + generated = root / "nexus" / "system" / "stream_service" + names = {"Stream", "WorkflowStream", "StreamCalls", "StreamClient"} + found: list[tuple[str, str]] = [] + for source in sorted(root.rglob("*.py")): + if generated in source.parents: + continue + for node in ast.walk(ast.parse(source.read_text(encoding="utf8"))): + if isinstance(node, ast.ClassDef) and node.name in names: + found.append((source.relative_to(root).as_posix(), node.name)) + # The Workflow Streams contrib library is older than the generated Stream API and + # doesn't use it. + found.remove(("contrib/workflow_streams/_stream.py", "WorkflowStream")) + assert found == [] + for name in ("Stream", "WorkflowStream"): + value = getattr(temporalio.workflow, name, None) + assert value is None or value.__module__.startswith( + "temporalio.nexus.system.stream_service." + ) + + +def test_each_stream_module_can_be_the_first_import(): + modules = [ + "temporalio.nexus.system.stream_service.workflow", + "temporalio.nexus.system.stream_service.client", + "temporalio.nexus.system.stream_service", + "temporalio.nexus.system", + "temporalio.workflow", + "temporalio.client", + "temporalio.worker", + "temporalio.contrib.redis.streams", + ] + failures: dict[str, str] = {} + for module in modules: + # Only a new interpreter has no module loaded, so the import order is real. + result = subprocess.run( + [sys.executable, "-c", f"import {module}"], + capture_output=True, + text=True, + check=False, + ) + if result.returncode != 0: + failures[module] = result.stderr.strip().splitlines()[-1] + assert failures == {} + + +async def test_client_without_stream_store_raises_and_sends_nothing(client: Client): + requests: list[ + temporalio.api.workflowservice.v1.StartNexusOperationExecutionRequest + ] = [] + + async def start_nexus_operation_execution( + req: temporalio.api.workflowservice.v1.StartNexusOperationExecutionRequest, + **_: object, + ) -> temporalio.api.workflowservice.v1.StartNexusOperationExecutionResponse: + requests.append(req) + return temporalio.api.workflowservice.v1.StartNexusOperationExecutionResponse( + run_id="run" + ) + + nexus_client = client.create_nexus_client( + StreamService, temporalio.nexus.system.TEMPORAL_SYSTEM_ENDPOINT + ) + with mock.patch.object( + client.workflow_service, + "start_nexus_operation_execution", + start_nexus_operation_execution, + ): + with pytest.raises(StreamConfigError) as err: + await nexus_client.start_operation( + StreamService.describe_stream, + DescribeStreamRequest(ref=StreamRef(id="stream")), + id="operation", + ) + # The generated client method goes the same way. + with pytest.raises(StreamConfigError): + await client.create_stream(id="stream", body_type=str) + assert str(err.value) == ( + "This client has no Stream Store. Give one in " + "stream_config=StreamConfig(stores=[...])." + ) + assert requests == [] + + +def test_stream_config_refuses_a_bad_set_of_stores(): + with pytest.raises(StreamConfigError, match="at least one Store"): + StreamConfig(stores=[]) + same_name = [MemoryStreamStore(name="a"), MemoryStreamStore(name="a")] + with pytest.raises(StreamConfigError, match="more than one Store named 'a'"): + StreamConfig(stores=same_name, store_selector=lambda _: same_name[0]) + with pytest.raises(StreamConfigError, match="store_selector must be set"): + StreamConfig(stores=[MemoryStreamStore(name="a"), MemoryStreamStore(name="b")]) + assert not issubclass(StreamConfigError, StreamError) + + +def test_client_refuses_a_store_in_plugins(client: Client): + config = client.config() + config["plugins"] = [MemoryStreamStore()] # type: ignore[list-item] + with pytest.raises(StreamConfigError, match="plugins"): + Client(**config) + + +async def test_selector_picks_the_store_of_a_new_stream(client: Client): + contexts: list[StreamSelectContext] = [] + + def select(context: StreamSelectContext) -> StreamStore: + contexts.append(context) + return second + + first, second, stream_config = _two_stores(select) + stream_client = _with_config(client, stream_config) + stream_id = f"stream-{uuid.uuid4()}" + stream = await stream_client.create_stream(id=stream_id, body_type=str) + assert stream.ref.store == "second" + assert contexts == [StreamSelectContext(id=stream_id, target=None)] + + await stream_client.get_stream(stream.ref, body_type=str).append("a") + # A call through a ref that names its Store doesn't ask the selector. + assert len(contexts) == 1 + in_second = _with_store(client, second).get_stream(stream.ref, body_type=str) + assert [record.body async for record in in_second.iter_read_items()] == ["a"] + in_first = _with_store(client, first).get_stream( + StreamRef(id=stream_id), body_type=str + ) + err = await _stream_error(in_first.describe()) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_NOT_FOUND + + +async def test_selector_gets_the_activity_that_makes_the_call(client: Client): + contexts: list[StreamSelectContext] = [] + + def select(context: StreamSelectContext) -> StreamStore: + contexts.append(context) + return first + + first, _, stream_config = _two_stores(select) + stream_client = _with_config(client, stream_config) + stream_id = f"stream-{uuid.uuid4()}" + env = ActivityEnvironment() + env.info = dataclasses.replace( + ActivityEnvironment.default_info(), + activity_id="job", + activity_type="make_stream", + activity_run_id="job-run", + namespace="streams", + ) + + async def create() -> Stream[str]: + return await stream_client.create_stream(id=stream_id, body_type=str) + + stream = await env.run(create) + assert stream.ref.store == "first" + assert contexts == [ + StreamSelectContext( + id=stream_id, + target=StorageDriverActivityInfo( + id="job", type="make_stream", run_id="job-run", namespace="streams" + ), + ) + ] + + +async def test_selector_that_returns_another_store_fails(client: Client): + _, _, stream_config = _two_stores(lambda _: MemoryStreamStore(name="first")) + stream_client = _with_config(client, stream_config) + with pytest.raises(StreamConfigError, match="isn't one of"): + await stream_client.create_stream(id=f"stream-{uuid.uuid4()}", body_type=str) + + +async def test_ref_of_a_store_that_the_client_lacks_fails(client: Client): + stream_client = _with_store(client, MemoryStreamStore(name="first")) + stream = stream_client.get_stream( + StreamRef(id=f"stream-{uuid.uuid4()}", store="other"), body_type=str + ) + with pytest.raises(StreamConfigError, match="'other'"): + await stream.describe() + + +def test_request_without_a_stream_ref_fails(): + request = Producer(id="mine", kind=ProducerKindClient(ClientProducer())) + with pytest.raises(TypeError, match="Producer has no StreamRef"): + _request_ref(request) + + +async def test_start_local_gives_its_client_the_stream_config(): + stream_config = StreamConfig(stores=[MemoryStreamStore()]) + async with await WorkflowEnvironment.start_local( + stream_config=stream_config, + dev_server_download_version=DEV_SERVER_DOWNLOAD_VERSION, + ) as env: + assert env.client.config()["stream_config"] is stream_config + stream = await env.client.create_stream( + id=f"stream-{uuid.uuid4()}", body_type=str + ) + assert stream.ref.store == "memory" + + +def test_stream_error_carries_its_reason(): + err = StreamError( + "the Stream is closed", reason=StreamErrorReason.STREAM_ERROR_REASON_CLOSED + ) + assert err.type == "StreamError" + assert err.message == "the Stream is closed" + assert err.non_retryable + assert list(err.details) == [ + temporalio.api.stream.v1.StreamErrorDetails( + reason=StreamErrorReason.STREAM_ERROR_REASON_CLOSED + ) + ] + assert not StreamError( + "down", reason=StreamErrorReason.STREAM_ERROR_REASON_STORAGE_UNAVAILABLE + ).non_retryable + assert StreamError("x").reason == ( + StreamErrorReason.STREAM_ERROR_REASON_UNSPECIFIED + ) + + +def test_stream_error_from_application_error(): + from_failure = temporalio.exceptions._stream_error_from_failure + rebuilt = from_failure( + ApplicationError( + "the Stream exists", + temporalio.api.stream.v1.StreamErrorDetails( + reason=StreamErrorReason.STREAM_ERROR_REASON_ALREADY_EXISTS + ), + type="StreamError", + ) + ) + assert isinstance(rebuilt, StreamError) + assert rebuilt.reason == StreamErrorReason.STREAM_ERROR_REASON_ALREADY_EXISTS + assert rebuilt.message == "the Stream exists" + unknown = from_failure( + ApplicationError( + "x", + temporalio.api.stream.v1.StreamErrorDetails( + reason=StreamErrorReason.ValueType(999) + ), + type="StreamError", + ) + ) + assert unknown is not None + assert unknown.reason == StreamErrorReason.STREAM_ERROR_REASON_UNSPECIFIED + assert from_failure(ApplicationError("x", type="RuntimeError")) is None + assert from_failure(RuntimeError("x")) is None diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py new file mode 100644 index 0000000000..7913a60498 --- /dev/null +++ b/tests/nexus/test_stream_store_workflow.py @@ -0,0 +1,806 @@ +from __future__ import annotations + +import asyncio +import uuid +from collections.abc import Callable, Sequence +from datetime import timedelta +from typing import Any + +import pytest +from google.protobuf.message import Message + +import temporalio.common +import temporalio.exceptions +from temporalio import activity, workflow +from temporalio.api.enums.v1 import EventType, StreamErrorReason +from temporalio.api.streamservice.v1 import ( + CreateStreamRequest as CreateStreamRequestMessage, +) +from temporalio.api.streamservice.v1 import ( + CreateStreamResponse as CreateStreamResponseMessage, +) +from temporalio.api.streamservice.v1 import ( + DescribeStreamRequest as DescribeStreamRequestMessage, +) +from temporalio.api.streamservice.v1 import ( + DescribeStreamResponse as DescribeStreamResponseMessage, +) +from temporalio.client import ( + Client, + StreamConfig, + StreamSelectContext, + StreamStore, + StreamStoreCallOptions, + WorkflowHandle, +) +from temporalio.common import RetryPolicy +from temporalio.converter import DataConverter, StorageDriverWorkflowInfo +from temporalio.exceptions import ApplicationError, StreamError +from temporalio.nexus.system import TEMPORAL_SYSTEM_ENDPOINT +from temporalio.nexus.system.stream_service import ( + AppendRecord, + AppendRecordsRequest, + ClientProducer, + CloseStreamRequest, + Producer, + ProducerKindClient, + ProducerKindLocalActivity, + ReadRecordsRequest, + StreamIdConflictPolicy, + StreamRecord, + StreamRef, + StreamService, +) +from temporalio.nexus.system.stream_service.workflow import create_stream, get_stream +from temporalio.nexus.system.stream_service.workflow._resources.stream import ( + Stream as WorkflowStream, +) +from temporalio.testing import MemoryStreamStore +from temporalio.worker import ( + ActivityInboundInterceptor, + ExecuteActivityInput, + Interceptor, + Replayer, +) +from tests.helpers import assert_task_fail_eventually, new_worker +from tests.helpers.streams import DictStreamStore, RecordingStreamStore +from tests.test_converter import SimpleCodec + +# Workflow code writes these values, so the History shows only whether a Record body +# leaks into it. +_BODIES = ["first-record-body", "second-record-body"] + + +@activity.defn +async def append_values(ref: StreamRef[str], values: list[str]) -> None: + await activity.client().get_stream(ref, body_type=str).append(*values) + + +async def _append_in_local_activity(ref: StreamRef[str], values: list[str]) -> None: + await workflow.execute_local_activity( + append_values, args=[ref, values], start_to_close_timeout=timedelta(seconds=10) + ) + + +@workflow.defn +class AppendWorkflow: + @workflow.run + async def run(self, stream_id: str) -> StreamRef[str]: + stream = await create_stream(id=stream_id, body_type=str) + await _append_in_local_activity(stream.ref, _BODIES) + return stream.ref + + +@workflow.defn +class RefusedOperationWorkflow: + @workflow.run + async def run(self, operation: str) -> str: + # The Workflow `Stream` has no method for these operations, so the test starts + # them through a Nexus client, as Workflow code still can. + ref = StreamRef[str](id="stream") + producer = Producer(id="workflow", kind=ProducerKindClient(ClientProducer())) + requests: dict[str, Any] = { + "AppendRecords": AppendRecordsRequest( + ref=ref, records=[AppendRecord(body="a")], producer=producer + ), + "CloseStream": CloseStreamRequest(ref=ref, producer=producer), + "ReadRecords": ReadRecordsRequest(ref=ref), + } + nexus_client = workflow.create_nexus_client( + service=StreamService, endpoint=TEMPORAL_SYSTEM_ENDPOINT + ) + try: + await nexus_client.start_operation(operation, requests[operation]) + except ApplicationError as err: + assert err.non_retryable + return err.message + return "started" + + +@workflow.defn +class DescribeResultWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = await create_stream(id=stream_id, body_type=str) + try: + await stream.describe() + except StreamError as err: + return f"StreamError {StreamErrorReason.Name(err.reason)}: {err.message}" + except ApplicationError as err: + return f"ApplicationError {err.type}: {err.message}" + except temporalio.exceptions.TimeoutError as err: + return f"TimeoutError {err.type and err.type.name}" + return "described" + + +@workflow.defn +class DescribeStreamWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = get_stream(StreamRef(id=stream_id), body_type=str) + try: + await stream.describe() + except StreamError as err: + assert isinstance(err, ApplicationError) + return StreamErrorReason.Name(err.reason) + return "described" + + +@workflow.defn +class CreateThenContinueAsNewWorkflow: + @workflow.run + async def run(self, stream_id: str, run: int) -> list[bool]: + stream = await create_stream(id=stream_id, body_type=str) + await _append_in_local_activity(stream.ref, [f"run-{run}"]) + if run == 1: + workflow.continue_as_new(args=[stream_id, 2]) + return [bool(stream.created)] + + +@workflow.defn +class DescribeOtherStoreWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + await get_stream( + StreamRef(id=stream_id, store="other"), body_type=str + ).describe() + + +@workflow.defn +class CreateAndDescribeWorkflow: + @workflow.run + async def run(self, stream_id: str) -> StreamRef[str]: + stream = await create_stream(id=stream_id, body_type=str) + await stream.describe() + return stream.ref + + +class CountingStore(RecordingStreamStore): + def __init__(self, name: str) -> None: + super().__init__(MemoryStreamStore(name=name)) + + +@activity.defn +async def user_activity() -> None: + pass + + +@workflow.defn +class CreateAndActivityWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + await create_stream(id=stream_id, body_type=str) + await workflow.execute_local_activity( + user_activity, start_to_close_timeout=timedelta(seconds=10) + ) + + +_fail_next_task = True + + +@workflow.defn(sandboxed=False) +class AppendThenFailTaskOnceWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + stream = await create_stream( + id=stream_id, + body_type=str, + id_conflict_policy=StreamIdConflictPolicy.StreamIdConflictPolicyUseExisting, + ) + await _append_in_local_activity(stream.ref, _BODIES) + global _fail_next_task + if _fail_next_task: + _fail_next_task = False + raise RuntimeError("Failed the Workflow Task once") + + +@activity.defn +async def append_from_local_activity(stream_id: str) -> None: + stream = activity.client().get_stream(StreamRef(id=stream_id), body_type=str) + await stream.append("a") + + +@workflow.defn +class UserLocalActivityAppendWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + await workflow.execute_local_activity( + append_from_local_activity, + stream_id, + start_to_close_timeout=timedelta(seconds=10), + ) + + +class FlakyDescribeStore(RecordingStreamStore): + def __init__(self, *, failures: int = 0, delay: timedelta = timedelta()) -> None: + super().__init__(MemoryStreamStore()) + self._failures = failures + self._delay = delay + self.describes = 0 + + async def before_call(self, operation: str, request: Message) -> None: + if operation == "DescribeStream": + self.describes += 1 + await asyncio.sleep(self._delay.total_seconds()) + if self.describes <= self._failures: + raise RuntimeError("describe failed") + + +class FlakyCreateStore(CountingStore): + def __init__(self, name: str) -> None: + super().__init__(name) + self.failed_creates = 0 + + async def before_call(self, operation: str, request: Message) -> None: + if operation == "CreateStream" and not self.failed_creates: + self.failed_creates += 1 + raise RuntimeError("create failed") + + +class RecordingInterceptor(Interceptor): + def __init__(self) -> None: + self.activity_types: list[str] = [] + + def intercept_activity( + self, next: ActivityInboundInterceptor + ) -> ActivityInboundInterceptor: + return _RecordingActivityInbound(next, self.activity_types) + + +class _RecordingActivityInbound(ActivityInboundInterceptor): + def __init__( + self, next: ActivityInboundInterceptor, activity_types: list[str] + ) -> None: + super().__init__(next) + self._activity_types = activity_types + + async def execute_activity(self, input: ExecuteActivityInput) -> Any: + self._activity_types.append(activity.info().activity_type) + return await super().execute_activity(input) + + +def _with_store(client: Client, store: StreamStore) -> Client: + return _with_config(client, StreamConfig(stores=[store])) + + +def _with_config(client: Client, stream_config: StreamConfig) -> Client: + config = client.config() + config["stream_config"] = stream_config + return Client(**config) + + +def _two_counting_stores( + contexts: list[StreamSelectContext], +) -> tuple[CountingStore, CountingStore, StreamConfig]: + first = CountingStore("first") + second = CountingStore("second") + + def select(context: StreamSelectContext) -> StreamStore: + contexts.append(context) + return second + + return first, second, StreamConfig(stores=[first, second], store_selector=select) + + +async def _run_create_and_describe( + client: Client, stream_id: str +) -> tuple[WorkflowHandle[CreateAndDescribeWorkflow, StreamRef[str]], StreamRef[str]]: + async with new_worker(client, CreateAndDescribeWorkflow) as worker: + handle = await client.start_workflow( + CreateAndDescribeWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + return handle, await handle.result() + + +async def _read_all(client: Client, stream_id: str) -> list[StreamRecord[str]]: + stream = client.get_stream(StreamRef(id=stream_id), body_type=str) + return [record async for record in stream.iter_read_items()] + + +def _producers(records: list[StreamRecord[str]]) -> list[Producer]: + producers = [record.producer for record in records] + assert all(producer is not None for producer in producers) + return [producer for producer in producers if producer is not None] + + +async def _run_append_workflow( + client: Client, +) -> tuple[WorkflowHandle[AppendWorkflow, StreamRef[str]], StreamRef[str]]: + async with new_worker(client, AppendWorkflow, activities=[append_values]) as worker: + handle = await client.start_workflow( + AppendWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + return handle, await handle.result() + + +async def _describe_result( + client: Client, + store: StreamStore, + options: StreamStoreCallOptions | None = None, +) -> str: + stream_config = StreamConfig( + stores=[store], workflow_call_options=options or StreamStoreCallOptions() + ) + stream_client = _with_config(client, stream_config) + async with new_worker(stream_client, DescribeResultWorkflow) as worker: + return await stream_client.execute_workflow( + DescribeResultWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + + +async def test_workflow_stream_uses_store(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + handle, ref = await _run_append_workflow(stream_client) + + stream = stream_client.get_stream(ref, body_type=str) + records = [record async for record in stream.iter_read_items()] + assert [record.body for record in records] == _BODIES + run_id = handle.result_run_id + assert run_id + first, second = _producers(records) + assert first == second + # The create is the run's first Local Activity, so the append is its second. + assert first.id == f"la:{run_id}:2" + assert isinstance(first.kind, ProducerKindLocalActivity) + assert first.kind.value.run_id == run_id + assert first.kind.value.epoch.task.started_event_id > 0 + assert first.kind.value.epoch.attempt == 1 + + +async def test_workflow_stream_uses_a_store_written_from_scratch(client: Client): + store = DictStreamStore() + stream_client = _with_store(client, store) + stream_id = f"stream-{uuid.uuid4()}" + _, ref = await _run_create_and_describe(stream_client, stream_id) + assert ref == StreamRef(id=stream_id, store="dict") + assert list(store.streams) == [stream_id] + + stream = stream_client.get_stream(ref, body_type=str) + await stream.append("a") + assert [record.body async for record in stream.iter_read_items()] == ["a"] + + +async def test_workflow_stream_store_uses_payload_codec(client: Client): + store = MemoryStreamStore() + config = client.config() + config["data_converter"] = DataConverter(payload_codec=SimpleCodec()) + config["stream_config"] = StreamConfig(stores=[store]) + stream_client = Client(**config) + _, ref = await _run_append_workflow(stream_client) + + # A client without the codec reads the bodies as the Store keeps them. + raw = _with_store(client, store).get_stream( + StreamRef(id=ref.id), body_type=temporalio.common.RawValue + ) + stored = [record.body.payload async for record in raw.iter_read_items()] + assert [body.metadata["simple-codec"] for body in stored] == [b"true", b"true"] + stream = stream_client.get_stream(ref, body_type=str) + assert [record.body async for record in stream.iter_read_items()] == _BODIES + + +async def test_workflow_stream_history_has_no_bodies(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + handle, _ = await _run_append_workflow(stream_client) + events = (await handle.fetch_history()).events + markers = [ + event.marker_recorded_event_attributes.marker_name + for event in events + if event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED + ] + # One marker for the create and one for the Local Activity that appends. + assert markers == ["core_local_activity", "core_local_activity"] + for event in events: + for body in _BODIES: + assert body.encode() not in event.SerializeToString() + + +async def test_workflow_stream_replay_does_not_call_store(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + handle, _ = await _run_append_workflow(stream_client) + replay_store = CountingStore("memory") + await Replayer( + workflows=[AppendWorkflow], + stream_config=StreamConfig(stores=[replay_store]), + ).replay_workflow(await handle.fetch_history()) + assert replay_store.operations == [] + + +async def test_workflow_create_without_store_fails_the_task(client: Client): + async with new_worker(client, CreateAndDescribeWorkflow) as worker: + handle = await client.start_workflow( + CreateAndDescribeWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + execution_timeout=timedelta(seconds=30), + ) + await assert_task_fail_eventually( + handle, + message_contains=( + "The client of this Worker has no Stream Store. Give one in " + "stream_config=StreamConfig(stores=[...])." + ), + ) + await handle.terminate() + + +@pytest.mark.parametrize( + "operation, message", + [ + ( + "AppendRecords", + "Workflow code can't append to a Stream on a Store in this release. " + "Call a Local Activity that appends through its client.", + ), + ( + "CloseStream", + "Workflow code can't close a Stream on a Store in this release. " + "Call an Activity that closes it through its client.", + ), + ( + "ReadRecords", + "Workflow code can't read a Stream on a Store in this release. " + "Call an Activity that reads it through its client.", + ), + ], +) +async def test_workflow_stream_operation_fails_with_store( + client: Client, operation: str, message: str +): + stream_client = _with_store(client, MemoryStreamStore()) + async with new_worker(stream_client, RefusedOperationWorkflow) as worker: + result = await stream_client.execute_workflow( + RefusedOperationWorkflow.run, + operation, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert result == message + + +def test_workflow_stream_has_no_append_close_or_read(): + for name in ("append", "close", "read"): + assert not hasattr(WorkflowStream, name) + assert hasattr(WorkflowStream, "describe") + + +async def test_workflow_stream_store_error_reaches_workflow_code(client: Client): + store = FlakyDescribeStore(failures=1) + result = await _describe_result(client, store) + assert result == "ApplicationError RuntimeError: describe failed" + assert store.describes == 1 + + +async def test_workflow_describe_of_unknown_stream_raises_stream_error(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + async with new_worker(stream_client, DescribeStreamWorkflow) as worker: + reason = await stream_client.execute_workflow( + DescribeStreamWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert reason == "STREAM_ERROR_REASON_NOT_FOUND" + + +async def test_strict_create_in_each_run_of_a_chain_succeeds(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker( + stream_client, CreateThenContinueAsNewWorkflow, activities=[append_values] + ) as worker: + handle = await stream_client.start_workflow( + CreateThenContinueAsNewWorkflow.run, + args=[stream_id, 1], + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert await handle.result() == [True] + first_run_id = handle.first_execution_run_id + last_run = await stream_client.get_workflow_handle(handle.id).describe() + assert first_run_id + assert last_run.run_id != first_run_id + records = await _read_all(stream_client, stream_id) + assert [record.body for record in records] == ["run-1", "run-2"] + first, second = _producers(records) + assert isinstance(first.kind, ProducerKindLocalActivity) + assert isinstance(second.kind, ProducerKindLocalActivity) + assert first.kind.value.run_id == first_run_id + assert second.kind.value.run_id == last_run.run_id + + +async def test_client_append_after_local_activity_append_keeps_offset_order( + client: Client, +): + stream_client = _with_store(client, MemoryStreamStore()) + _, ref = await _run_append_workflow(stream_client) + stream = stream_client.get_stream(ref, body_type=str) + appended = await stream.append("from-client") + records = await _read_all(stream_client, ref.id) + assert [record.body for record in records] == [*_BODIES, "from-client"] + offsets = [record.offset for record in records] + assert offsets == sorted(offsets) + assert len(set(offsets)) == len(offsets) + assert offsets[-1] == appended.last_offset + kinds = [type(producer.kind) for producer in _producers(records)] + assert kinds == [ + ProducerKindLocalActivity, + ProducerKindLocalActivity, + ProducerKindClient, + ] + + +async def test_workflow_stream_store_call_options_retry(client: Client): + options = StreamStoreCallOptions( + retry_policy=RetryPolicy( + initial_interval=timedelta(milliseconds=10), maximum_attempts=2 + ) + ) + store = FlakyDescribeStore(failures=1) + assert await _describe_result(client, store, options) == "described" + assert store.describes == 2 + + +async def test_workflow_stream_store_call_options_timeout(client: Client): + options = StreamStoreCallOptions(start_to_close_timeout=timedelta(milliseconds=200)) + store = FlakyDescribeStore(delay=timedelta(seconds=5)) + result = await _describe_result(client, store, options) + assert result == "TimeoutError START_TO_CLOSE" + + +async def test_worker_keeps_its_stream_stores(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + async with new_worker(stream_client, AppendWorkflow) as worker: + with pytest.raises(ValueError, match="Stream Stores"): + worker.client = client + worker.client = stream_client + + +async def test_activity_interceptors_see_stream_store_activity(client: Client): + interceptor = RecordingInterceptor() + stream_client = _with_store(client, MemoryStreamStore()) + async with new_worker( + stream_client, + CreateAndActivityWorkflow, + activities=[user_activity], + interceptors=[interceptor], + ) as worker: + await stream_client.execute_workflow( + CreateAndActivityWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + # The create runs as the SDK's Local Activity, before the Workflow's own Activity. + assert interceptor.activity_types == [ + "__temporal_stream_store_call:memory", + "user_activity", + ] + + +async def test_workflow_task_retry_appends_with_higher_epoch(client: Client): + global _fail_next_task + _fail_next_task = True + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker( + stream_client, AppendThenFailTaskOnceWorkflow, activities=[append_values] + ) as worker: + await stream_client.execute_workflow( + AppendThenFailTaskOnceWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + records = await _read_all(stream_client, stream_id) + # The failed Workflow Task records no marker, so its retry runs the Local Activity + # again and writes a second copy. + assert [record.body for record in records] == [*_BODIES, *_BODIES] + producers = _producers(records) + assert len({producer.id for producer in producers}) == 1 + + def epoch(producer: Producer) -> tuple[int, int, int]: + assert isinstance(producer.kind, ProducerKindLocalActivity) + local = producer.kind.value.epoch + return (local.task.started_event_id, local.task.attempt, local.attempt) + + first, second, third, fourth = (epoch(producer) for producer in producers) + assert first == second + assert third == fourth + assert third > first + + +async def test_user_local_activity_append_has_local_activity_producer(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream = await stream_client.create_stream( + id=f"stream-{uuid.uuid4()}", body_type=str + ) + async with new_worker( + stream_client, + UserLocalActivityAppendWorkflow, + activities=[append_from_local_activity], + ) as worker: + handle = await stream_client.start_workflow( + UserLocalActivityAppendWorkflow.run, + stream.ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + await handle.result() + [producer] = _producers(await _read_all(stream_client, stream.ref.id)) + run_id = handle.result_run_id + assert producer.id == f"la:{run_id}:1" + assert isinstance(producer.kind, ProducerKindLocalActivity) + assert producer.kind.value.run_id == run_id + assert producer.kind.value.epoch.task.started_event_id > 0 + assert producer.kind.value.epoch.attempt == 1 + + +async def test_workflow_ref_of_another_store_fails_the_task(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + async with new_worker(stream_client, DescribeOtherStoreWorkflow) as worker: + handle = await stream_client.start_workflow( + DescribeOtherStoreWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + execution_timeout=timedelta(seconds=30), + ) + await assert_task_fail_eventually( + handle, message_contains="names the Stream Store 'other'" + ) + await handle.terminate() + + +async def test_workflow_create_goes_to_the_selected_store(client: Client): + contexts: list[StreamSelectContext] = [] + first, second, stream_config = _two_counting_stores(contexts) + stream_id = f"stream-{uuid.uuid4()}" + handle, ref = await _run_create_and_describe( + _with_config(client, stream_config), stream_id + ) + assert ref.store == "second" + assert first.operations == [] + assert second.operations == ["CreateStream", "DescribeStream"] + # The describe goes by the Store that the ref names, so the selector runs once. + assert contexts == [ + StreamSelectContext( + id=stream_id, + target=StorageDriverWorkflowInfo( + id=handle.id, + type="CreateAndDescribeWorkflow", + run_id=handle.result_run_id, + namespace=client.namespace, + ), + ) + ] + + +async def test_workflow_replay_calls_no_store(client: Client): + _, _, stream_config = _two_counting_stores([]) + stream_id = f"stream-{uuid.uuid4()}" + handle, _ = await _run_create_and_describe( + _with_config(client, stream_config), stream_id + ) + contexts: list[StreamSelectContext] = [] + first, second, replay_config = _two_counting_stores(contexts) + await Replayer( + workflows=[CreateAndDescribeWorkflow], stream_config=replay_config + ).replay_workflow(await handle.fetch_history()) + # The selector runs in Workflow code, so a replay runs it again with the Workflow. + assert [context.id for context in contexts] == [stream_id] + target = contexts[0].target + assert isinstance(target, StorageDriverWorkflowInfo) + assert (target.id, target.run_id) == (handle.id, handle.result_run_id) + assert first.operations == [] + assert second.operations == [] + + +async def test_workflow_create_with_several_stores_takes_the_config_options( + client: Client, +): + first = CountingStore("first") + second = FlakyCreateStore("second") + stream_config = StreamConfig( + stores=[first, second], + store_selector=lambda _: second, + workflow_call_options=StreamStoreCallOptions( + retry_policy=RetryPolicy( + initial_interval=timedelta(milliseconds=10), maximum_attempts=2 + ) + ), + ) + # The default options make one attempt, so only the config's retry policy lets + # the create pass after its first attempt fails. + _, ref = await _run_create_and_describe( + _with_config(client, stream_config), f"stream-{uuid.uuid4()}" + ) + assert ref.store == "second" + assert first.operations == [] + assert second.operations == ["CreateStream", "CreateStream", "DescribeStream"] + + +class OwnActivityStore(CountingStore): + """A Store that brings its own Activity for the calls of Workflow code.""" + + def __init__(self) -> None: + super().__init__("own") + self.workflow_calls: list[str] = [] + + def worker_activities(self) -> Sequence[Callable[..., Any]]: + @activity.defn(name="own_store_call") + async def own_store_call(operation: str, request: bytes) -> bytes: + if operation == "CreateStream": + created = await self.create_stream( + CreateStreamRequestMessage.FromString(request) + ) + return created.SerializeToString() + described = await self.describe_stream( + DescribeStreamRequestMessage.FromString(request) + ) + return described.SerializeToString() + + return [own_store_call] + + async def call_from_workflow( + self, operation: str, request: Message, options: StreamStoreCallOptions + ) -> Message: + self.workflow_calls.append(operation) + response = await workflow.execute_local_activity( + "own_store_call", + args=[operation, request.SerializeToString()], + result_type=bytes, + start_to_close_timeout=options.start_to_close_timeout, + ) + if operation == "CreateStream": + return CreateStreamResponseMessage.FromString(response) + return DescribeStreamResponseMessage.FromString(response) + + +async def test_workflow_call_goes_through_the_store_own_activity(client: Client): + store = OwnActivityStore() + interceptor = RecordingInterceptor() + stream_client = _with_store(client, store) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker( + stream_client, CreateAndDescribeWorkflow, interceptors=[interceptor] + ) as worker: + ref = await stream_client.execute_workflow( + CreateAndDescribeWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert ref == StreamRef(id=stream_id, store="own") + assert store.workflow_calls == ["CreateStream", "DescribeStream"] + assert store.operations == ["CreateStream", "DescribeStream"] + assert interceptor.activity_types == ["own_store_call", "own_store_call"] From 514998aac5ea65de48867b2454946a8ba007553f Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Fri, 13 Nov 2026 18:55:01 -0700 Subject: [PATCH 02/35] Moved Core to the Stream WIT with a Workflow append and close. The Stream WIT now gives the Workflow target `append` and `close`, and keeps `read` out of it. --- temporalio/bridge/sdk-core | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/temporalio/bridge/sdk-core b/temporalio/bridge/sdk-core index 9a110b3cda..59f4b33f1d 160000 --- a/temporalio/bridge/sdk-core +++ b/temporalio/bridge/sdk-core @@ -1 +1 @@ -Subproject commit 9a110b3cda949199b7792348ff5d265b1d1c414a +Subproject commit 59f4b33f1d90bb5ac4582ea465b4e8597c507214 From 6be125988269c1f679e1e6f90a12bae92380e9da Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Fri, 13 Nov 2026 18:59:53 -0700 Subject: [PATCH 03/35] Ran a Workflow's Stream append in the Store's Local Activity. The Workflow `Stream` has `append` again, and Workflow code can't do I/O. The Local Activity writes as its own Producer with a fixed attempt, so a retry in one Workflow Task writes once. --- temporalio/client/_stream_store.py | 52 +++-- temporalio/worker/_stream_store.py | 6 +- tests/nexus/test_stream_store_workflow.py | 239 +++++++++++++++++++--- 3 files changed, 250 insertions(+), 47 deletions(-) diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index 622f00bf18..21183b750e 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -15,6 +15,8 @@ import temporalio.bridge.runtime import temporalio.common import temporalio.converter +import temporalio.converter._payload_converter +import temporalio.nexus.system._support import temporalio.nexus.system.stream_service import temporalio.runtime import temporalio.workflow @@ -49,7 +51,9 @@ class StreamStoreCallOptions: retry_policy: temporalio.common.RetryPolicy = dataclasses.field( default_factory=lambda: temporalio.common.RetryPolicy(maximum_attempts=1) ) - """Retries of a failed call. The default is one attempt.""" + """Retries of a failed call. The default is one attempt. In one Workflow Task, each + attempt of a Workflow's append without an explicit ``producer`` sends the same + Producer, epoch and sequence, so the Store writes the batch once.""" def __post_init__(self) -> None: """Check that a timeout is set.""" @@ -152,8 +156,8 @@ async def call_from_workflow( calls no method of this Store. Args: - operation: The Nexus operation name, ``CreateStream`` or ``DescribeStream`` - in this release. + operation: The Nexus operation name, ``CreateStream``, ``AppendRecords`` + or ``DescribeStream`` in this release. request: The request message of ``temporalio.api.streamservice.v1``. options: The :py:attr:`StreamConfig.workflow_call_options` of the config. @@ -161,8 +165,8 @@ async def call_from_workflow( The response message of the operation. """ stream_operation = _STREAM_OPERATIONS[operation] - # Workflow code can start only a create and a describe in this release, and their - # messages carry no payload. So the Local Activity has no payload codec step. + # Workflow code runs no payload codec, so the Activity encodes the bodies of an + # append before the Store gets them. response = await temporalio.workflow.execute_local_activity( _store_call_activity_name(self.name), args=[operation, request.SerializeToString()], @@ -297,10 +301,6 @@ def check_response( "append_records", temporalio.api.streamservice.v1.AppendRecordsRequest, temporalio.api.streamservice.v1.AppendRecordsResponse, - workflow_refusal=( - "Workflow code can't append to a Stream on a Store in this release. " - "Call a Local Activity that appends through its client." - ), ), "ReadRecords": _StreamOperation( "read_records", @@ -341,15 +341,43 @@ def _store_call_activity( ) -> Callable[[str, bytes], Awaitable[bytes]]: @temporalio.activity.defn(name=_store_call_activity_name(store.name)) async def call(operation: str, request: bytes) -> bytes: - stream_operation = _STREAM_OPERATIONS[operation] - response = await stream_operation.call( - store, stream_operation.request_type.FromString(request) + message = _STREAM_OPERATIONS[operation].request_type.FromString(request) + if isinstance(message, temporalio.api.streamservice.v1.AppendRecordsRequest): + _with_local_activity_producer(message) + response = await _call_stream_store( + store, temporalio.activity.client().data_converter, operation, message ) return response.SerializeToString() return call +def _with_local_activity_producer( + request: temporalio.api.streamservice.v1.AppendRecordsRequest, +) -> None: + # On a Store, a Workflow appends as the Local Activity that runs the append, so the + # Store can fence the write of a failed Workflow Task. A Producer that Workflow code + # gave of another kind reaches the Store as it is. + if ( + request.producer.WhichOneof("kind") != "workflow" + or not temporalio.activity.info().is_local + ): + return + converter = temporalio.converter._payload_converter._get_transfer_type_converter( + temporalio.nexus.system.stream_service.Producer + ) + if converter is None: + raise TypeError("The generated Producer has no proto transfer type") + request.producer.CopyFrom( + converter.to_transfer_type(temporalio.nexus.system._support.default_producer()) + ) + # Each retry of this Local Activity in one Workflow Task sends the same Producer, epoch + # and sequence, so the Store writes the batch once and gives back its first offsets. + request.producer.local_activity.epoch.attempt = 1 + if not request.sequence: + request.sequence = 1 + + @dataclasses.dataclass(frozen=True) class StreamSelectContext: """Context passed to :py:attr:`StreamConfig.store_selector` calls. diff --git a/temporalio/worker/_stream_store.py b/temporalio/worker/_stream_store.py index da9faa689c..bd134bf4b1 100644 --- a/temporalio/worker/_stream_store.py +++ b/temporalio/worker/_stream_store.py @@ -54,10 +54,8 @@ def start_operation( ) -> temporalio.workflow.NexusOperationHandle[OutputT]: """Answer a Stream call of Workflow code with the Store that the call goes to.""" stream_operation = _STREAM_OPERATIONS[input.operation_name] - # The Workflow `Stream` has no `append`, `close` or `read`, but the generated - # Workflow service still has their operations, so Workflow code can start one - # through a Nexus client. In this release, Workflow code writes and reads a Stream - # on a Store through its own Activities. + # Workflow code can start any operation of the generated service through a Nexus + # client, also one that the Workflow `Stream` leaves out. if stream_operation.workflow_refusal is not None: raise temporalio.exceptions.ApplicationError( stream_operation.workflow_refusal, non_retryable=True diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index 7913a60498..f6c520e1a3 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -9,6 +9,7 @@ import pytest from google.protobuf.message import Message +import temporalio.api.streamservice.v1 import temporalio.common import temporalio.exceptions from temporalio import activity, workflow @@ -40,6 +41,7 @@ from temporalio.nexus.system.stream_service import ( AppendRecord, AppendRecordsRequest, + AppendRecordsResponse, ClientProducer, CloseStreamRequest, Producer, @@ -84,6 +86,15 @@ async def _append_in_local_activity(ref: StreamRef[str], values: list[str]) -> N @workflow.defn class AppendWorkflow: + @workflow.run + async def run(self, stream_id: str) -> StreamRef[str]: + stream = await create_stream(id=stream_id, body_type=str) + await stream.append(*_BODIES) + return stream.ref + + +@workflow.defn +class LocalActivityAppendWorkflow: @workflow.run async def run(self, stream_id: str) -> StreamRef[str]: stream = await create_stream(id=stream_id, body_type=str) @@ -91,6 +102,44 @@ async def run(self, stream_id: str) -> StreamRef[str]: return stream.ref +# Workflow code appends either with the Workflow `Stream` or with a Local Activity of +# its own, and both write as the Local Activity that runs the append. +_APPEND_WORKFLOWS = pytest.mark.parametrize( + "append_workflow", [AppendWorkflow, LocalActivityAppendWorkflow] +) + + +@workflow.defn +class AppendOffsetsWorkflow: + @workflow.run + async def run(self, stream_id: str) -> AppendRecordsResponse: + stream = await create_stream(id=stream_id, body_type=str) + return await stream.append(*_BODIES) + + +@workflow.defn +class AppendToStreamWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = get_stream(StreamRef(id=stream_id), body_type=str) + try: + await stream.append("a") + except StreamError as err: + assert isinstance(err, ApplicationError) + return StreamErrorReason.Name(err.reason) + return "appended" + + +@workflow.defn +class ExplicitProducerWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + stream = await create_stream(id=stream_id, body_type=str) + await stream.append( + "a", producer=Producer(id="mine", kind=ProducerKindClient(ClientProducer())) + ) + + @workflow.defn class RefusedOperationWorkflow: @workflow.run @@ -201,13 +250,16 @@ async def run(self, stream_id: str) -> None: @workflow.defn(sandboxed=False) class AppendThenFailTaskOnceWorkflow: @workflow.run - async def run(self, stream_id: str) -> None: + async def run(self, stream_id: str, in_local_activity: bool) -> None: stream = await create_stream( id=stream_id, body_type=str, id_conflict_policy=StreamIdConflictPolicy.StreamIdConflictPolicyUseExisting, ) - await _append_in_local_activity(stream.ref, _BODIES) + if in_local_activity: + await _append_in_local_activity(stream.ref, _BODIES) + else: + await stream.append(*_BODIES) global _fail_next_task if _fail_next_task: _fail_next_task = False @@ -257,6 +309,34 @@ async def before_call(self, operation: str, request: Message) -> None: raise RuntimeError("create failed") +class LostAnswerStore(RecordingStreamStore): + """Keeps each append request, and can lose the answer of the first append.""" + + def __init__(self, *, lose_first_answer: bool = True) -> None: + super().__init__(MemoryStreamStore()) + self._lose_first_answer = lose_first_answer + self.appends: list[temporalio.api.streamservice.v1.AppendRecordsRequest] = [] + self.first_response: ( + temporalio.api.streamservice.v1.AppendRecordsResponse | None + ) = None + + async def append_records( + self, request: temporalio.api.streamservice.v1.AppendRecordsRequest + ) -> temporalio.api.streamservice.v1.AppendRecordsResponse: + kept = temporalio.api.streamservice.v1.AppendRecordsRequest() + kept.CopyFrom(request) + self.appends.append(kept) + response = await super().append_records(request) + if self.first_response is None: + self.first_response = response + if self._lose_first_answer: + raise StreamError( + "Lost the answer of a write", + reason=StreamErrorReason.STREAM_ERROR_REASON_STORAGE_UNAVAILABLE, + ) + return response + + class RecordingInterceptor(Interceptor): def __init__(self) -> None: self.activity_types: list[str] = [] @@ -327,11 +407,13 @@ def _producers(records: list[StreamRecord[str]]) -> list[Producer]: async def _run_append_workflow( - client: Client, -) -> tuple[WorkflowHandle[AppendWorkflow, StreamRef[str]], StreamRef[str]]: - async with new_worker(client, AppendWorkflow, activities=[append_values]) as worker: - handle = await client.start_workflow( - AppendWorkflow.run, + client: Client, append_workflow: type[Any] = AppendWorkflow +) -> tuple[WorkflowHandle[Any, StreamRef[str]], StreamRef[str]]: + async with new_worker( + client, append_workflow, activities=[append_values] + ) as worker: + handle: WorkflowHandle[Any, StreamRef[str]] = await client.start_workflow( + append_workflow.run, f"stream-{uuid.uuid4()}", id=f"workflow-{uuid.uuid4()}", task_queue=worker.task_queue, @@ -357,9 +439,10 @@ async def _describe_result( ) -async def test_workflow_stream_uses_store(client: Client): +@_APPEND_WORKFLOWS +async def test_workflow_stream_uses_store(client: Client, append_workflow: type[Any]): stream_client = _with_store(client, MemoryStreamStore()) - handle, ref = await _run_append_workflow(stream_client) + handle, ref = await _run_append_workflow(stream_client, append_workflow) stream = stream_client.get_stream(ref, body_type=str) records = [record async for record in stream.iter_read_items()] @@ -389,13 +472,16 @@ async def test_workflow_stream_uses_a_store_written_from_scratch(client: Client) assert [record.body async for record in stream.iter_read_items()] == ["a"] -async def test_workflow_stream_store_uses_payload_codec(client: Client): +@_APPEND_WORKFLOWS +async def test_workflow_stream_store_uses_payload_codec( + client: Client, append_workflow: type[Any] +): store = MemoryStreamStore() config = client.config() config["data_converter"] = DataConverter(payload_codec=SimpleCodec()) config["stream_config"] = StreamConfig(stores=[store]) stream_client = Client(**config) - _, ref = await _run_append_workflow(stream_client) + _, ref = await _run_append_workflow(stream_client, append_workflow) # A client without the codec reads the bodies as the Store keeps them. raw = _with_store(client, store).get_stream( @@ -407,28 +493,34 @@ async def test_workflow_stream_store_uses_payload_codec(client: Client): assert [record.body async for record in stream.iter_read_items()] == _BODIES -async def test_workflow_stream_history_has_no_bodies(client: Client): +@_APPEND_WORKFLOWS +async def test_workflow_stream_history_has_no_bodies( + client: Client, append_workflow: type[Any] +): stream_client = _with_store(client, MemoryStreamStore()) - handle, _ = await _run_append_workflow(stream_client) + handle, _ = await _run_append_workflow(stream_client, append_workflow) events = (await handle.fetch_history()).events markers = [ event.marker_recorded_event_attributes.marker_name for event in events if event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED ] - # One marker for the create and one for the Local Activity that appends. + # One marker for the create and one for the append. assert markers == ["core_local_activity", "core_local_activity"] for event in events: for body in _BODIES: assert body.encode() not in event.SerializeToString() -async def test_workflow_stream_replay_does_not_call_store(client: Client): +@_APPEND_WORKFLOWS +async def test_workflow_stream_replay_does_not_call_store( + client: Client, append_workflow: type[Any] +): stream_client = _with_store(client, MemoryStreamStore()) - handle, _ = await _run_append_workflow(stream_client) + handle, _ = await _run_append_workflow(stream_client, append_workflow) replay_store = CountingStore("memory") await Replayer( - workflows=[AppendWorkflow], + workflows=[append_workflow], stream_config=StreamConfig(stores=[replay_store]), ).replay_workflow(await handle.fetch_history()) assert replay_store.operations == [] @@ -456,11 +548,6 @@ async def test_workflow_create_without_store_fails_the_task(client: Client): @pytest.mark.parametrize( "operation, message", [ - ( - "AppendRecords", - "Workflow code can't append to a Stream on a Store in this release. " - "Call a Local Activity that appends through its client.", - ), ( "CloseStream", "Workflow code can't close a Stream on a Store in this release. " @@ -487,10 +574,10 @@ async def test_workflow_stream_operation_fails_with_store( assert result == message -def test_workflow_stream_has_no_append_close_or_read(): - for name in ("append", "close", "read"): - assert not hasattr(WorkflowStream, name) - assert hasattr(WorkflowStream, "describe") +def test_workflow_stream_has_append_and_close_and_no_read(): + for name in ("append", "close", "describe", "__aenter__", "__aexit__"): + assert hasattr(WorkflowStream, name) + assert not hasattr(WorkflowStream, "read") async def test_workflow_stream_store_error_reaches_workflow_code(client: Client): @@ -538,11 +625,12 @@ async def test_strict_create_in_each_run_of_a_chain_succeeds(client: Client): assert second.kind.value.run_id == last_run.run_id -async def test_client_append_after_local_activity_append_keeps_offset_order( - client: Client, +@_APPEND_WORKFLOWS +async def test_client_append_after_workflow_append_keeps_offset_order( + client: Client, append_workflow: type[Any] ): stream_client = _with_store(client, MemoryStreamStore()) - _, ref = await _run_append_workflow(stream_client) + _, ref = await _run_append_workflow(stream_client, append_workflow) stream = stream_client.get_stream(ref, body_type=str) appended = await stream.append("from-client") records = await _read_all(stream_client, ref.id) @@ -607,7 +695,10 @@ async def test_activity_interceptors_see_stream_store_activity(client: Client): ] -async def test_workflow_task_retry_appends_with_higher_epoch(client: Client): +@pytest.mark.parametrize("in_local_activity", [False, True]) +async def test_workflow_task_retry_appends_with_higher_epoch( + client: Client, in_local_activity: bool +): global _fail_next_task _fail_next_task = True stream_client = _with_store(client, MemoryStreamStore()) @@ -617,7 +708,7 @@ async def test_workflow_task_retry_appends_with_higher_epoch(client: Client): ) as worker: await stream_client.execute_workflow( AppendThenFailTaskOnceWorkflow.run, - stream_id, + args=[stream_id, in_local_activity], id=f"workflow-{uuid.uuid4()}", task_queue=worker.task_queue, ) @@ -804,3 +895,89 @@ async def test_workflow_call_goes_through_the_store_own_activity(client: Client) assert store.workflow_calls == ["CreateStream", "DescribeStream"] assert store.operations == ["CreateStream", "DescribeStream"] assert interceptor.activity_types == ["own_store_call", "own_store_call"] + + +async def test_workflow_append_returns_offsets_of_records_in_order(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, AppendOffsetsWorkflow) as worker: + appended = await stream_client.execute_workflow( + AppendOffsetsWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + records = await _read_all(stream_client, stream_id) + assert [record.body for record in records] == _BODIES + assert [appended.first_offset, appended.last_offset] == [ + record.offset for record in records + ] + + +async def test_workflow_append_to_closed_stream_raises_stream_error(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream = await stream_client.create_stream( + id=f"stream-{uuid.uuid4()}", body_type=str + ) + await stream.close() + async with new_worker(stream_client, AppendToStreamWorkflow) as worker: + reason = await stream_client.execute_workflow( + AppendToStreamWorkflow.run, + stream.ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert reason == "STREAM_ERROR_REASON_CLOSED" + + +async def test_workflow_append_retry_in_one_task_writes_once(client: Client): + store = LostAnswerStore() + stream_config = StreamConfig( + stores=[store], + workflow_call_options=StreamStoreCallOptions( + retry_policy=RetryPolicy( + initial_interval=timedelta(milliseconds=10), maximum_attempts=2 + ) + ), + ) + stream_client = _with_config(client, stream_config) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, AppendOffsetsWorkflow) as worker: + appended = await stream_client.execute_workflow( + AppendOffsetsWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + first, second = store.appends + assert first.producer == second.producer + assert first.producer.local_activity.epoch.attempt == 1 + assert first.sequence == second.sequence == 1 + records = await _read_all(stream_client, stream_id) + assert [record.body for record in records] == _BODIES + assert store.first_response is not None + assert appended.first_offset == store.first_response.first_offset + assert appended.last_offset == store.first_response.last_offset + assert [appended.first_offset, appended.last_offset] == [ + record.offset for record in records + ] + + +async def test_workflow_explicit_producer_reaches_store(client: Client): + store = LostAnswerStore(lose_first_answer=False) + stream_client = _with_store(client, store) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, ExplicitProducerWorkflow) as worker: + await stream_client.execute_workflow( + ExplicitProducerWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + [producer] = _producers(await _read_all(stream_client, stream_id)) + assert producer.id == "mine" + assert isinstance(producer.kind, ProducerKindClient) + [request] = store.appends + assert request.producer.id == "mine" + assert request.producer.WhichOneof("kind") == "client" + assert request.sequence == 0 From b2f807193178daafd7747c89db794fc06eb5af1f Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Fri, 13 Nov 2026 19:03:03 -0700 Subject: [PATCH 04/35] Ran a Workflow's Stream close in an Activity after its Workflow Task. A Local Activity reaches the Store before its Workflow Task completes, so a failed task would leave a closed Stream. A Worker with Stream Stores now polls for Activity Tasks, so it can run the close. --- changelog/added/chattering-ledger-otter.md | 2 +- temporalio/client/_stream_store.py | 71 ++++-- temporalio/worker/_worker.py | 9 +- tests/contrib/redis/test_streams.py | 35 +++ tests/nexus/test_stream_store_workflow.py | 251 ++++++++++++++++++--- 5 files changed, 306 insertions(+), 62 deletions(-) diff --git a/changelog/added/chattering-ledger-otter.md b/changelog/added/chattering-ledger-otter.md index f15fdc478b..b628c9c7f8 100644 --- a/changelog/added/chattering-ledger-otter.md +++ b/changelog/added/chattering-ledger-otter.md @@ -1 +1 @@ -Add the experimental Streams API. Client and Activity code create a Stream, append Records to it, read them and close it, and Workflow code creates and describes one. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `temporalio.contrib.redis.streams.RedisStreamStore`, or `temporalio.testing.MemoryStreamStore` in tests. +Add the experimental Streams API. Client, Activity and Workflow code create a Stream, append Records to it and close it, and client and Activity code read it. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `temporalio.contrib.redis.streams.RedisStreamStore`, or `temporalio.testing.MemoryStreamStore` in tests. diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index 21183b750e..bf68c129b1 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -29,10 +29,12 @@ @dataclasses.dataclass(frozen=True) class StreamStoreCallOptions: - """Options of the Local Activity that calls a Stream Store for Workflow code. + """Options of the Activity that calls a Stream Store for Workflow code. Workflow code can't do I/O, so the default - :py:meth:`StreamStore.call_from_workflow` calls its Store from a Local Activity. + :py:meth:`StreamStore.call_from_workflow` calls its Store from an Activity. A close + runs it as a regular Activity, and every other operation runs it as a Local + Activity. Both get these options. A :py:class:`StreamConfig` holds them in :py:attr:`StreamConfig.workflow_call_options`, and every Store of that config @@ -137,8 +139,9 @@ def worker_activities(self) -> Sequence[Callable[..., Any]]: """The Activities that a Worker registers for this Store when it runs Workflows. The default is one Activity, named for this Store, that - :py:meth:`call_from_workflow` runs as a Local Activity. A Store that overrides - one of the two methods overrides the other to match. + :py:meth:`call_from_workflow` runs as a Local Activity, or as a regular Activity + for a close. A Store that overrides one of the two methods overrides the other to + match. """ return [_store_call_activity(self)] @@ -151,13 +154,15 @@ async def call_from_workflow( """Answer one Stream operation for Workflow code. It runs in Workflow code, so it does no I/O itself. The default runs the - Activity of :py:meth:`worker_activities` as a Local Activity, with the timeouts - and the retry policy of ``options``. A replay takes the result from History and - calls no method of this Store. + Activity of :py:meth:`worker_activities` with the timeouts and the retry policy + of ``options``. A close runs it as a regular Activity on the Workflow's Task + Queue, after the Workflow Task completes. Every other operation runs it as a + Local Activity. A replay takes the result from History and calls no method of + this Store. Args: - operation: The Nexus operation name, ``CreateStream``, ``AppendRecords`` - or ``DescribeStream`` in this release. + operation: The Nexus operation name, ``CreateStream``, ``AppendRecords``, + ``CloseStream`` or ``DescribeStream`` in this release. request: The request message of ``temporalio.api.streamservice.v1``. options: The :py:attr:`StreamConfig.workflow_call_options` of the config. @@ -167,14 +172,29 @@ async def call_from_workflow( stream_operation = _STREAM_OPERATIONS[operation] # Workflow code runs no payload codec, so the Activity encodes the bodies of an # append before the Store gets them. - response = await temporalio.workflow.execute_local_activity( - _store_call_activity_name(self.name), - args=[operation, request.SerializeToString()], - result_type=bytes, - schedule_to_close_timeout=options.schedule_to_close_timeout, - start_to_close_timeout=options.start_to_close_timeout, - retry_policy=options.retry_policy, - ) + if operation == "CloseStream": + # A Local Activity reaches the Store before its Workflow Task completes. If + # that task then failed, the Stream would stay closed and the retry of the + # task could not append. The server schedules a regular Activity only when + # the task completes. + response = await temporalio.workflow.execute_activity( + _store_call_activity_name(self.name), + args=[operation, request.SerializeToString()], + result_type=bytes, + task_queue=temporalio.workflow.info().task_queue, + schedule_to_close_timeout=options.schedule_to_close_timeout, + start_to_close_timeout=options.start_to_close_timeout, + retry_policy=options.retry_policy, + ) + else: + response = await temporalio.workflow.execute_local_activity( + _store_call_activity_name(self.name), + args=[operation, request.SerializeToString()], + result_type=bytes, + schedule_to_close_timeout=options.schedule_to_close_timeout, + start_to_close_timeout=options.start_to_close_timeout, + retry_policy=options.retry_policy, + ) return stream_operation.response_type.FromString(response) @@ -315,10 +335,6 @@ def check_response( "close_stream", temporalio.api.streamservice.v1.CloseStreamRequest, temporalio.api.streamservice.v1.CloseStreamResponse, - workflow_refusal=( - "Workflow code can't close a Stream on a Store in this release. " - "Call an Activity that closes it through its client." - ), ), "DescribeStream": _StreamOperation( "describe_stream", @@ -411,6 +427,10 @@ class StreamConfig: Store of that name. A call whose ref names no Store, such as a create, goes to the one Store, or to the Store that :py:attr:`store_selector` returns. + A Worker with Workflows and this config polls for Activity Tasks, since a Workflow's + close runs the Store's Activity as a regular Activity. With ``no_remote_activities`` + on that Worker, another Worker of the Task Queue must run the close. + .. warning:: This API is experimental and subject to change. """ @@ -431,8 +451,8 @@ class StreamConfig: ``None`` and there is one Store, every new Stream goes to that Store. For a call of Workflow code, the selector runs in Workflow code, also on a - replay. So it must give the same Store for the same context. The Local Activity - of the chosen Store is in History, and another answer on a replay is a + replay. So it must give the same Store for the same context. The Activity of the + chosen Store is in History, and another answer on a replay is a non-determinism error. A Stream that code builds from an id only goes through the selector on each call, so the selector must also give the same Store for the same id. @@ -443,8 +463,9 @@ class StreamConfig: ) """Options of a Store's call for Workflow code, for every Store of this config. - :py:meth:`StreamStore.call_from_workflow` gets them. The default runs its Local - Activity with their timeouts and retry policy. + :py:meth:`StreamStore.call_from_workflow` gets them. The default runs the Store's + Activity with their timeouts and retry policy, as a Local Activity, or as a regular + Activity for a close. """ _store_map: dict[str, StreamStore] = dataclasses.field( diff --git a/temporalio/worker/_worker.py b/temporalio/worker/_worker.py index bbf68e2bf4..4ecce29d15 100644 --- a/temporalio/worker/_worker.py +++ b/temporalio/worker/_worker.py @@ -668,14 +668,15 @@ def check_activity(activity: str): # are not running an activity worker at all. Otherwise shutdown # will not proceed properly. no_remote_activities=config.get("no_remote_activities") - or not config.get("activities"), + or self._activity_worker is None, task_types=temporalio.bridge.worker.WorkerTaskTypes( enable_workflows=self._workflow_worker is not None, enable_local_activities=self._activity_worker is not None and self._workflow_worker is not None, - # The Activities of the Stream Stores run as Local Activities, so - # alone they don't make this worker poll for Activity Tasks. - enable_remote_activities=bool(config.get("activities")) + # A Workflow's Stream close runs a Store's Activity as a regular + # Activity, so the Activities of the Stream Stores alone make this + # worker poll for Activity Tasks. + enable_remote_activities=self._activity_worker is not None and not config.get("no_remote_activities"), enable_nexus=self._nexus_worker is not None, ), diff --git a/tests/contrib/redis/test_streams.py b/tests/contrib/redis/test_streams.py index 0bae0921f1..95a5b3a37e 100644 --- a/tests/contrib/redis/test_streams.py +++ b/tests/contrib/redis/test_streams.py @@ -74,6 +74,16 @@ async def run(self, stream_id: str) -> StreamRef[str]: return stream.ref +@workflow.defn +class AppendAndCloseWorkflow: + @workflow.run + async def run(self, stream_id: str) -> StreamRef[str]: + stream = await create_stream(id=stream_id, body_type=str) + await stream.append("a", "b") + await stream.close() + return stream.ref + + @activity.defn async def append_in_activity(stream_id: str) -> None: stream = activity.client().get_stream(StreamRef(id=stream_id), body_type=str) @@ -140,6 +150,31 @@ async def test_workflow_local_activity_appends_with_local_activity_producer( assert record.producer.kind.value.run_id == run_id +async def test_workflow_appends_and_closes(client: Client): + stream_client = _with_store(client, _store()) + async with new_worker(stream_client, AppendAndCloseWorkflow) as worker: + handle = await stream_client.start_workflow( + AppendAndCloseWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + ref = await handle.result() + + stream = stream_client.get_stream(ref, body_type=str) + records = [record async for record in stream.iter_read_items()] + assert [record.body for record in records] == ["a", "b"] + run_id = handle.result_run_id + for record in records: + assert record.producer is not None + assert isinstance(record.producer.kind, ProducerKindLocalActivity) + assert record.producer.kind.value.run_id == run_id + described = await stream.describe() + assert described.status == StreamStatus.StreamStatusClosed + err = await _stream_error(stream.append("c")) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_CLOSED + + async def test_activity_appends_with_activity_producer(client: Client): stream_client = _with_store(client, _store()) stream = await _create_stream(stream_client) diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index f6c520e1a3..480a931118 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -39,11 +39,8 @@ from temporalio.exceptions import ApplicationError, StreamError from temporalio.nexus.system import TEMPORAL_SYSTEM_ENDPOINT from temporalio.nexus.system.stream_service import ( - AppendRecord, - AppendRecordsRequest, AppendRecordsResponse, ClientProducer, - CloseStreamRequest, Producer, ProducerKindClient, ProducerKindLocalActivity, @@ -52,6 +49,7 @@ StreamRecord, StreamRef, StreamService, + StreamStatus, ) from temporalio.nexus.system.stream_service.workflow import create_stream, get_stream from temporalio.nexus.system.stream_service.workflow._resources.stream import ( @@ -63,6 +61,11 @@ ExecuteActivityInput, Interceptor, Replayer, + StartActivityInput, + Worker, + WorkflowInboundInterceptor, + WorkflowInterceptorClassInput, + WorkflowOutboundInterceptor, ) from tests.helpers import assert_task_fail_eventually, new_worker from tests.helpers.streams import DictStreamStore, RecordingStreamStore @@ -144,16 +147,10 @@ async def run(self, stream_id: str) -> None: class RefusedOperationWorkflow: @workflow.run async def run(self, operation: str) -> str: - # The Workflow `Stream` has no method for these operations, so the test starts - # them through a Nexus client, as Workflow code still can. - ref = StreamRef[str](id="stream") - producer = Producer(id="workflow", kind=ProducerKindClient(ClientProducer())) + # The Workflow `Stream` has no `read`, so the test starts its operation through + # a Nexus client, as Workflow code still can. requests: dict[str, Any] = { - "AppendRecords": AppendRecordsRequest( - ref=ref, records=[AppendRecord(body="a")], producer=producer - ), - "CloseStream": CloseStreamRequest(ref=ref, producer=producer), - "ReadRecords": ReadRecordsRequest(ref=ref), + "ReadRecords": ReadRecordsRequest(ref=StreamRef[str](id="stream")), } nexus_client = workflow.create_nexus_client( service=StreamService, endpoint=TEMPORAL_SYSTEM_ENDPOINT @@ -337,6 +334,86 @@ async def append_records( return response +class CloseRequestStore(RecordingStreamStore): + """Keeps each close request.""" + + def __init__(self) -> None: + super().__init__(MemoryStreamStore()) + self.closes: list[temporalio.api.streamservice.v1.CloseStreamRequest] = [] + + async def close_stream( + self, request: temporalio.api.streamservice.v1.CloseStreamRequest + ) -> temporalio.api.streamservice.v1.CloseStreamResponse: + kept = temporalio.api.streamservice.v1.CloseStreamRequest() + kept.CopyFrom(request) + self.closes.append(kept) + return await super().close_stream(request) + + +@workflow.defn +class CloseWorkflow: + @workflow.run + async def run(self, stream_id: str) -> StreamRef[str]: + stream = await create_stream(id=stream_id, body_type=str) + await stream.append("a") + await stream.close() + return stream.ref + + +@workflow.defn +class AsyncWithCloseWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + async with create_stream(id=stream_id, body_type=str) as stream: + await stream.append("a") + described = await stream.describe() + assert described.status is not None + return described.status.name + + +_fail_close_task = True +_started_activities: list[str] = [] + + +@workflow.defn(sandboxed=False) +class CloseThenFailTaskOnceWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + stream = await create_stream( + id=stream_id, + body_type=str, + id_conflict_policy=StreamIdConflictPolicy.StreamIdConflictPolicyUseExisting, + ) + await stream.append("a") + close = asyncio.create_task(stream.close()) + # A few turns of the loop let the close schedule its Activity in this task. + for _ in range(10): + await asyncio.sleep(0) + global _fail_close_task + if _fail_close_task: + _fail_close_task = False + raise RuntimeError("Failed the Workflow Task once") + await close + + +class StartedActivityInterceptor(Interceptor): + def workflow_interceptor_class( + self, input: WorkflowInterceptorClassInput + ) -> type[WorkflowInboundInterceptor]: + return _StartedActivityInbound + + +class _StartedActivityInbound(WorkflowInboundInterceptor): + def init(self, outbound: WorkflowOutboundInterceptor) -> None: + super().init(_StartedActivityOutbound(outbound)) + + +class _StartedActivityOutbound(WorkflowOutboundInterceptor): + def start_activity(self, input: StartActivityInput) -> workflow.ActivityHandle[Any]: + _started_activities.append(str(input.activity)) + return super().start_activity(input) + + class RecordingInterceptor(Interceptor): def __init__(self) -> None: self.activity_types: list[str] = [] @@ -545,33 +622,19 @@ async def test_workflow_create_without_store_fails_the_task(client: Client): await handle.terminate() -@pytest.mark.parametrize( - "operation, message", - [ - ( - "CloseStream", - "Workflow code can't close a Stream on a Store in this release. " - "Call an Activity that closes it through its client.", - ), - ( - "ReadRecords", - "Workflow code can't read a Stream on a Store in this release. " - "Call an Activity that reads it through its client.", - ), - ], -) -async def test_workflow_stream_operation_fails_with_store( - client: Client, operation: str, message: str -): +async def test_workflow_read_fails_with_store(client: Client): stream_client = _with_store(client, MemoryStreamStore()) async with new_worker(stream_client, RefusedOperationWorkflow) as worker: result = await stream_client.execute_workflow( RefusedOperationWorkflow.run, - operation, + "ReadRecords", id=f"workflow-{uuid.uuid4()}", task_queue=worker.task_queue, ) - assert result == message + assert result == ( + "Workflow code can't read a Stream on a Store in this release. " + "Call an Activity that reads it through its client." + ) def test_workflow_stream_has_append_and_close_and_no_read(): @@ -981,3 +1044,127 @@ async def test_workflow_explicit_producer_reaches_store(client: Client): assert request.producer.id == "mine" assert request.producer.WhichOneof("kind") == "client" assert request.sequence == 0 + + +async def _describe_status(client: Client, stream_id: str) -> StreamStatus | None: + stream = client.get_stream(StreamRef(id=stream_id), body_type=str) + return (await stream.describe()).status + + +async def test_workflow_close_runs_in_activity_after_its_task(client: Client): + store = CloseRequestStore() + stream_client = _with_store(client, store) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, CloseWorkflow) as worker: + handle = await stream_client.start_workflow( + CloseWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + ref = await handle.result() + assert await _describe_status(stream_client, stream_id) == ( + StreamStatus.StreamStatusClosed + ) + with pytest.raises(StreamError) as err: + await stream_client.get_stream(ref, body_type=str).append("b") + assert err.value.reason == StreamErrorReason.STREAM_ERROR_REASON_CLOSED + # The close keeps the Workflow Producer of the request. + [close] = store.closes + assert close.producer.WhichOneof("kind") == "workflow" + assert close.producer.workflow.run_id == handle.result_run_id + events = (await handle.fetch_history()).events + scheduled = [ + event.activity_task_scheduled_event_attributes + for event in events + if event.event_type == EventType.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED + ] + assert [attributes.activity_type.name for attributes in scheduled] == [ + "__temporal_stream_store_call:memory" + ] + assert scheduled[0].task_queue.name == worker.task_queue + markers = [ + event.marker_recorded_event_attributes.marker_name + for event in events + if event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED + ] + # One marker for the create and one for the append, and none for the close. + assert markers == ["core_local_activity", "core_local_activity"] + + +async def test_workflow_async_with_closes_the_stream(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, AsyncWithCloseWorkflow) as worker: + status = await stream_client.execute_workflow( + AsyncWithCloseWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert status == StreamStatus.StreamStatusOpen.name + assert await _describe_status(stream_client, stream_id) == ( + StreamStatus.StreamStatusClosed + ) + records = await _read_all(stream_client, stream_id) + assert [record.body for record in records] == ["a"] + + +async def test_worker_with_workflows_only_runs_the_close(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with Worker( + stream_client, task_queue=str(uuid.uuid4()), workflows=[CloseWorkflow] + ) as worker: + await stream_client.execute_workflow( + CloseWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert await _describe_status(stream_client, stream_id) == ( + StreamStatus.StreamStatusClosed + ) + + +async def test_failed_workflow_task_leaves_the_stream_open(client: Client): + global _fail_close_task + _fail_close_task = True + _started_activities.clear() + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker( + stream_client, + CloseThenFailTaskOnceWorkflow, + interceptors=[StartedActivityInterceptor()], + ) as worker: + await stream_client.execute_workflow( + CloseThenFailTaskOnceWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + # The failed task started the close too, but the server never scheduled it. + assert _started_activities == ["__temporal_stream_store_call:memory"] * 2 + records = await _read_all(stream_client, stream_id) + # The retried task appends again to the open Stream, and then closes it. + assert [record.body for record in records] == ["a", "a"] + assert await _describe_status(stream_client, stream_id) == ( + StreamStatus.StreamStatusClosed + ) + + +async def test_activity_interceptors_see_the_close_activity(client: Client): + interceptor = RecordingInterceptor() + stream_client = _with_store(client, MemoryStreamStore()) + async with new_worker( + stream_client, CloseWorkflow, interceptors=[interceptor] + ) as worker: + await stream_client.execute_workflow( + CloseWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + # The create and the append run as Local Activities, and the close as an Activity. + assert interceptor.activity_types == ["__temporal_stream_store_call:memory"] * 3 From 92dcedf83d073e3f48a47aa6fe125976c587f364 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Sat, 14 Nov 2026 00:43:50 -0700 Subject: [PATCH 05/35] Let a Stream config hold several Stores with no selector. A create can name its Store with `store=`, so a config with several Stores needs no selector. A call that names no Store on such a config raises `StreamConfigError` at the call. --- temporalio/client/_stream_store.py | 33 ++++++++++------- temporalio/exceptions.py | 5 ++- tests/nexus/test_stream_store.py | 45 ++++++++++++++++++++++- tests/nexus/test_stream_store_workflow.py | 27 ++++++++++++++ 4 files changed, 92 insertions(+), 18 deletions(-) diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index bf68c129b1..d8b6f85656 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -424,8 +424,10 @@ class StreamConfig: :py:class:`temporalio.converter.ExternalStorage` holds drivers and a selector. A ref carries the name of its Store, so a call on a known Stream goes to the - Store of that name. A call whose ref names no Store, such as a create, goes to - the one Store, or to the Store that :py:attr:`store_selector` returns. + Store of that name. A create can name its Store with ``store=``. A call whose ref + names no Store goes to the Store that :py:attr:`store_selector` returns, or to the + one Store when there is no selector. With more than one Store and no selector, + such a call raises :py:class:`temporalio.exceptions.StreamConfigError`. A Worker with Workflows and this config polls for Activity Tasks, since a Workflow's close runs the Store's Activity as a regular Activity. With ``no_remote_activities`` @@ -439,16 +441,18 @@ class StreamConfig: """At least one Store. Each Store has a unique name. A ref carries the :py:attr:`StreamStore.name` of its Store, and a call looks - the Store up by that name. With more than one Store, :py:attr:`store_selector` - must also be set. A Worker serves every Store of its client. + the Store up by that name. With more than one Store and no + :py:attr:`store_selector`, each call must name its Store. A Worker serves every + Store of its client. """ store_selector: Callable[[StreamSelectContext], StreamStore] | None = None - """Picks the Store of a new Stream. Required with more than one Store. + """Picks the Store of a call whose ref names no Store. - It gets a :py:class:`StreamSelectContext` for each call whose ref names no - Store, and returns one of the instances in :py:attr:`stores`. When it is - ``None`` and there is one Store, every new Stream goes to that Store. + It gets a :py:class:`StreamSelectContext` for each such call, and returns one of + the instances in :py:attr:`stores`. When it is ``None`` and there is one Store, + such a call goes to that Store. When it is ``None`` and there is more than one + Store, such a call raises :py:class:`temporalio.exceptions.StreamConfigError`. For a call of Workflow code, the selector runs in Workflow code, also on a replay. So it must give the same Store for the same context. The Activity of the @@ -477,8 +481,7 @@ def __post_init__(self) -> None: """Check the Stores and build the name-keyed map of them. Raises :py:class:`temporalio.exceptions.StreamConfigError` if there is no - Store, if two Stores share a name, or if more than one Store is given - without a :py:attr:`store_selector`. + Store, or if two Stores share a name. """ if not self.stores: raise StreamConfigError("StreamConfig.stores must have at least one Store") @@ -490,15 +493,17 @@ def __post_init__(self) -> None: "Each Store needs a unique name." ) store_map[store.name] = store - if len(self.stores) > 1 and self.store_selector is None: - raise StreamConfigError( - "StreamConfig.store_selector must be set when there is more than one Store" - ) object.__setattr__(self, "_store_map", store_map) def _select_store(self, context: StreamSelectContext) -> StreamStore: selector = self.store_selector if selector is None: + if len(self.stores) > 1: + raise StreamConfigError( + "StreamConfig has more than one Store and no store_selector, and " + f"the call on the Stream '{context.id}' names no Store. Name one " + "with store=, or set StreamConfig.store_selector." + ) return self.stores[0] store = selector(context) if ( diff --git a/temporalio/exceptions.py b/temporalio/exceptions.py index 63135cd9fc..702f5885bb 100644 --- a/temporalio/exceptions.py +++ b/temporalio/exceptions.py @@ -293,8 +293,9 @@ class StreamConfigError(RuntimeError): Streams on the server, so every Stream call needs a Store. * A Workflow's Stream call is made on a Worker whose client has no Stream config. This fails the Workflow Task. - * A :py:class:`temporalio.client.StreamConfig` has no Store, has two Stores with - one name, or has more than one Store and no selector. + * A :py:class:`temporalio.client.StreamConfig` has no Store, or has two Stores + with one name. + * A call names no Store on a config with more than one Store and no selector. * A ref names a Store that the config of the client doesn't have. * The selector returns a Store that isn't one of the config's Stores. * A :py:class:`temporalio.client.StreamStore` is given in ``plugins``. diff --git a/tests/nexus/test_stream_store.py b/tests/nexus/test_stream_store.py index db37459f9b..0b10411c5f 100644 --- a/tests/nexus/test_stream_store.py +++ b/tests/nexus/test_stream_store.py @@ -474,11 +474,52 @@ def test_stream_config_refuses_a_bad_set_of_stores(): same_name = [MemoryStreamStore(name="a"), MemoryStreamStore(name="a")] with pytest.raises(StreamConfigError, match="more than one Store named 'a'"): StreamConfig(stores=same_name, store_selector=lambda _: same_name[0]) - with pytest.raises(StreamConfigError, match="store_selector must be set"): - StreamConfig(stores=[MemoryStreamStore(name="a"), MemoryStreamStore(name="b")]) assert not issubclass(StreamConfigError, StreamError) +def test_stream_config_takes_several_stores_with_no_selector(): + first, second = MemoryStreamStore(name="a"), MemoryStreamStore(name="b") + stream_config = StreamConfig(stores=[first, second]) + assert stream_config.store_selector is None + assert stream_config._store_for(StreamRef(id="stream", store="b"), None) is second + with pytest.raises(StreamConfigError, match="more than one Store"): + stream_config._store_for(StreamRef(id="stream"), None) + + +async def test_client_create_with_store_on_several_stores_needs_no_selector( + client: Client, +): + first = MemoryStreamStore(name="first") + second = MemoryStreamStore(name="second") + stream_client = _with_config(client, StreamConfig(stores=[first, second])) + stream = await stream_client.create_stream( + id=f"stream-{uuid.uuid4()}", store="second", body_type=str + ) + assert stream.ref.store == "second" + await stream.append("a") + in_second = _with_store(client, second).get_stream(stream.ref, body_type=str) + assert [record.body async for record in in_second.iter_read_items()] == ["a"] + in_first = _with_store(client, first).get_stream( + StreamRef(id=stream.ref.id), body_type=str + ) + err = await _stream_error(in_first.describe()) + assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_NOT_FOUND + + +async def test_client_create_without_store_on_several_stores_fails(client: Client): + stream_config = StreamConfig( + stores=[MemoryStreamStore(name="first"), MemoryStreamStore(name="second")] + ) + stream_client = _with_config(client, stream_config) + with pytest.raises(StreamConfigError) as err: + await stream_client.create_stream(id="stream", body_type=str) + assert str(err.value) == ( + "StreamConfig has more than one Store and no store_selector, and the call " + "on the Stream 'stream' names no Store. Name one with store=, or set " + "StreamConfig.store_selector." + ) + + def test_client_refuses_a_store_in_plugins(client: Client): config = client.config() config["plugins"] = [MemoryStreamStore()] # type: ignore[list-item] diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index 480a931118..1403994790 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -835,6 +835,33 @@ async def test_workflow_ref_of_another_store_fails_the_task(client: Client): await handle.terminate() +async def test_workflow_create_without_store_on_several_stores_fails_the_task( + client: Client, +): + stream_config = StreamConfig( + stores=[CountingStore("first"), CountingStore("second")] + ) + stream_client = _with_config(client, stream_config) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, CreateAndDescribeWorkflow) as worker: + handle = await stream_client.start_workflow( + CreateAndDescribeWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + execution_timeout=timedelta(seconds=30), + ) + await assert_task_fail_eventually( + handle, + message_contains=( + "StreamConfig has more than one Store and no store_selector, and the " + f"call on the Stream '{stream_id}' names no Store. Name one with " + "store=, or set StreamConfig.store_selector." + ), + ) + await handle.terminate() + + async def test_workflow_create_goes_to_the_selected_store(client: Client): contexts: list[StreamSelectContext] = [] first, second, stream_config = _two_counting_stores(contexts) From fc4909c63735d67f5c6e805b1244b349b6135c8f Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Sat, 14 Nov 2026 00:53:42 -0700 Subject: [PATCH 06/35] Failed the Workflow for a Stream call with no Stream config. A failed Workflow Task retries for good, and a Worker with no Stream config never recovers. Workflow code now gets a non-retryable `ApplicationError`, so a user sees the failed Workflow. --- temporalio/client/_stream_store.py | 7 ++++++ temporalio/exceptions.py | 11 +++++++-- temporalio/worker/_replayer.py | 7 +++--- temporalio/worker/_stream_store.py | 12 ++++++++++ temporalio/worker/_workflow_instance.py | 12 ++++------ tests/nexus/test_stream_store_workflow.py | 28 +++++++++++++++-------- 6 files changed, 55 insertions(+), 22 deletions(-) diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index d8b6f85656..74ed6961b8 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -423,6 +423,13 @@ class StreamConfig: operation. It holds Stores and a selector, as :py:class:`temporalio.converter.ExternalStorage` holds drivers and a selector. + On a Worker whose client has no config, a Stream call of Workflow code raises + :py:class:`temporalio.exceptions.ApplicationError` with the ``type`` + ``StreamConfigError``, and an uncaught one fails the Workflow. Workflow code can + catch this error. A Workflow that catches it must not go on to make the same call + on a Worker that has a config, since the History then has no record of the first + call. + A ref carries the name of its Store, so a call on a known Stream goes to the Store of that name. A create can name its Store with ``store=``. A call whose ref names no Store goes to the Store that :py:attr:`store_selector` returns, or to the diff --git a/temporalio/exceptions.py b/temporalio/exceptions.py index 702f5885bb..658012ee4f 100644 --- a/temporalio/exceptions.py +++ b/temporalio/exceptions.py @@ -291,8 +291,8 @@ class StreamConfigError(RuntimeError): * A Stream call is made on a client with no Stream config. This release has no Streams on the server, so every Stream call needs a Store. - * A Workflow's Stream call is made on a Worker whose client has no Stream config. - This fails the Workflow Task. + * A Workflow's Stream call has a ref that names no Store, on a Worker whose + client has more than one Store and no selector. This fails the Workflow Task. * A :py:class:`temporalio.client.StreamConfig` has no Store, or has two Stores with one name. * A call names no Store on a config with more than one Store and no selector. @@ -303,6 +303,13 @@ class StreamConfigError(RuntimeError): Worker's client doesn't have. This fails the Workflow Task. * This build of temporalio has no Stream runtime, or no Redis Stream Store. + A Workflow's Stream call on a Worker whose client has no Stream config doesn't + raise this class. Workflow code gets an :py:class:`ApplicationError` with the + ``type`` ``StreamConfigError`` and ``non_retryable`` set, so an uncaught one fails + the Workflow. Workflow code can catch this error. A Workflow that catches it must + not go on to make the same call on a Worker that has a config, since the History + then has no record of the first call. + .. warning:: This API is experimental and subject to change. """ diff --git a/temporalio/worker/_replayer.py b/temporalio/worker/_replayer.py index 540e9d4052..7ad6bdeaaf 100644 --- a/temporalio/worker/_replayer.py +++ b/temporalio/worker/_replayer.py @@ -67,9 +67,10 @@ def __init__( ran. ``stream_config`` takes the Stream config of the client that the - worker had, so a replay makes the same commands. A history with a Stream - call needs it, since a Stream call with no Stream config fails the - Workflow Task. A replay calls no method of its Stores. + worker had, so a replay makes the same commands. A history that records a + Stream call needs it. With no Stream config, Workflow code gets an error in + place of that call, and the replay fails with a nondeterminism error. A + replay calls no method of its Stores. Note, unlike the worker, for the replayer the workflow_task_executor will default to a new thread pool executor with no max_workers set that diff --git a/temporalio/worker/_stream_store.py b/temporalio/worker/_stream_store.py index bd134bf4b1..947cc7aeba 100644 --- a/temporalio/worker/_stream_store.py +++ b/temporalio/worker/_stream_store.py @@ -24,6 +24,18 @@ from ._workflow_instance import _WorkflowInstanceImpl +def _no_stream_config_error() -> temporalio.exceptions.ApplicationError: # pyright: ignore[reportUnusedFunction] + # A `StreamConfigError` would fail the Workflow Task, and its retries would fail the + # same way for good. A Worker with no config is a setup that a user has to see, so + # the Workflow fails instead. + return temporalio.exceptions.ApplicationError( + "The client of this Worker has no Stream Store. Give one in " + "stream_config=StreamConfig(stores=[...]).", + type="StreamConfigError", + non_retryable=True, + ) + + def _workflow_target() -> StorageDriverWorkflowInfo: # External storage builds the same value for a Workflow. info = temporalio.workflow.info() diff --git a/temporalio/worker/_workflow_instance.py b/temporalio/worker/_workflow_instance.py index 09f1ae53fc..07f63a69dc 100644 --- a/temporalio/worker/_workflow_instance.py +++ b/temporalio/worker/_workflow_instance.py @@ -95,7 +95,7 @@ WorkflowInboundInterceptor, WorkflowOutboundInterceptor, ) -from ._stream_store import _WorkflowStreamStore +from ._stream_store import _no_stream_config_error, _WorkflowStreamStore logger = logging.getLogger(__name__) @@ -196,7 +196,7 @@ class WorkflowInstanceDetails: stream_store: _WorkflowStreamStore | None = None """The Worker's Stream Stores as Workflow code reaches them, or ``None`` when the Worker's client has none. This release has no Streams on the server, so with ``None`` - a Stream call fails the Workflow Task.""" + a Stream call fails the Workflow.""" class WorkflowInstance(ABC): @@ -2355,15 +2355,11 @@ async def _schedule_system_nexus_operation( self, input: StartNexusOperationInput[Any, OutputT] ) -> temporalio.workflow.NexusOperationHandle[OutputT]: # This release has no Streams on the server, so a Worker answers a Stream call with - # a Store of its client. On a Worker whose client has none, the `StreamConfigError` - # fails the Workflow Task. Every other system Nexus operation goes to the server. + # a Store of its client. Every other system Nexus operation goes to the server. if input.service == _STREAM_SERVICE.name: store = self._stream_store if store is None: - raise temporalio.exceptions.StreamConfigError( - "The client of this Worker has no Stream Store. Give one in " - "stream_config=StreamConfig(stores=[...])." - ) + raise _no_stream_config_error() return store.start_operation(self, input) return await self._outbound_start_nexus_operation(input) diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index 1403994790..07ae119841 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -32,6 +32,7 @@ StreamSelectContext, StreamStore, StreamStoreCallOptions, + WorkflowFailureError, WorkflowHandle, ) from temporalio.common import RetryPolicy @@ -603,7 +604,9 @@ async def test_workflow_stream_replay_does_not_call_store( assert replay_store.operations == [] -async def test_workflow_create_without_store_fails_the_task(client: Client): +async def test_workflow_create_without_stream_config_fails_the_workflow( + client: Client, +): async with new_worker(client, CreateAndDescribeWorkflow) as worker: handle = await client.start_workflow( CreateAndDescribeWorkflow.run, @@ -612,14 +615,21 @@ async def test_workflow_create_without_store_fails_the_task(client: Client): task_queue=worker.task_queue, execution_timeout=timedelta(seconds=30), ) - await assert_task_fail_eventually( - handle, - message_contains=( - "The client of this Worker has no Stream Store. Give one in " - "stream_config=StreamConfig(stores=[...])." - ), - ) - await handle.terminate() + with pytest.raises(WorkflowFailureError) as err: + await asyncio.wait_for(handle.result(), timeout=10) + cause = err.value.cause + assert isinstance(cause, ApplicationError) + assert not isinstance(cause, StreamError) + assert cause.type == "StreamConfigError" + assert cause.non_retryable + assert cause.message == ( + "The client of this Worker has no Stream Store. Give one in " + "stream_config=StreamConfig(stores=[...])." + ) + # A Replayer with no Stream config fails the Workflow the same way. + await Replayer(workflows=[CreateAndDescribeWorkflow]).replay_workflow( + await handle.fetch_history() + ) async def test_workflow_read_fails_with_store(client: Client): From ad825d3b8710221319df9f628b18c938aef99f07 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Sat, 14 Nov 2026 01:24:18 -0700 Subject: [PATCH 07/35] Let BufferedAppender batch the appends of Workflow code. A timer of the Workflow event loop goes into History, so in Workflow code the appender starts its write at the next yield instead. The appends of one activation become one Local Activity. --- temporalio/client/_stream_appender.py | 71 +++++-- tests/nexus/test_stream_appender_workflow.py | 197 +++++++++++++++++++ 2 files changed, 252 insertions(+), 16 deletions(-) create mode 100644 tests/nexus/test_stream_appender_workflow.py diff --git a/temporalio/client/_stream_appender.py b/temporalio/client/_stream_appender.py index ec8d3d49d3..1fda0cc776 100644 --- a/temporalio/client/_stream_appender.py +++ b/temporalio/client/_stream_appender.py @@ -5,33 +5,51 @@ import asyncio from datetime import timedelta from types import TracebackType -from typing import Generic, TypeVar +from typing import TYPE_CHECKING, Generic, TypeVar from typing_extensions import Self import temporalio.nexus.system.stream_service.client +import temporalio.workflow from ._stream_store import _check_not_negative +if TYPE_CHECKING: + # The generated Workflow modules import `temporalio.workflow`, which a runtime import + # here would pull into the client package. + import temporalio.nexus.system.stream_service.workflow._resources.stream + BodyT = TypeVar("BodyT") # The Store refuses an append of more Records than this. Many small Records fit under its byte # limit, but they keep the Store busy for long. _MAX_BATCH_RECORDS = 1000 +_DEFAULT_MAX_DELAY = timedelta(milliseconds=100) + class BufferedAppender(Generic[BodyT]): """Batches the appends of one Stream. - Use it in an Activity or a client that appends often, such as one that streams model - tokens. :py:meth:`append` returns at once and does no I/O. The appender writes the - buffered Records as one batch at most ``max_delay`` after the first of them, or at once - when they fill a batch of 1,000 Records. One write is in flight at a time. Records - appended during a write wait for the next batch, and they keep their order. + Use it in code that appends often, such as an Activity that streams model tokens. + :py:meth:`append` returns at once and does no I/O. One write is in flight at a time. + Records appended during a write wait for the next batch, and they keep their order. A batch + holds at most 1,000 Records. + + It takes a client Stream or a Workflow Stream. When a write starts depends on where + :py:meth:`append` runs, not on the type of the Stream: + + * In a client or an Activity, the appender writes the buffered Records at most + ``max_delay`` after the first of them, or at once when they fill a batch. + * In Workflow code, a timer would go into History, so the appender uses none. The first + write starts when the calling code next yields, so the appends of one activation go as + one write. Each write is the Workflow ``append`` of the Stream, so it is one Local + Activity and one marker in History. A replay takes the results from History and writes + nothing. Use it in ``async with``, which writes the remaining Records at the end of the block, or await :py:meth:`flush`. Without ``async with`` or a :py:meth:`flush`, the Records still in - the buffer are lost. The end of an Activity attempt doesn't write them. + the buffer are lost. The end of an Activity attempt or of a Workflow doesn't write them. ``await flush()`` is the backpressure point. A caller that appends faster than the Store accepts Records must await it from time to time, so the buffer doesn't grow without bound. @@ -42,32 +60,39 @@ class BufferedAppender(Generic[BodyT]): The appender doesn't count bytes, so a batch over the size limit of the Store fails with the Store's error. - It takes a client Stream, so Workflow code has no use for it. - .. warning:: This API is experimental and subject to change. """ def __init__( self, - stream: temporalio.nexus.system.stream_service.client.Stream[BodyT], + stream: temporalio.nexus.system.stream_service.client.Stream[BodyT] + | temporalio.nexus.system.stream_service.workflow._resources.stream.Stream[ + BodyT + ], *, - max_delay: timedelta = timedelta(milliseconds=100), + max_delay: timedelta | None = None, ) -> None: """Create an appender. It starts no write until the first append. Args: - stream: The Stream to append to. + stream: The Stream to append to, a client Stream or a Workflow Stream. max_delay: The longest time that a Record waits in the buffer before its batch - starts to write. It must not be negative. + starts to write, in a client or an Activity. It must not be negative. + ``None`` means 100 milliseconds. Workflow code must leave it ``None``. + + Raises: + ValueError: ``max_delay`` is negative, or it is set in Workflow code. """ _check_not_negative(max_delay=max_delay) self._stream = stream self._max_delay = max_delay + self._check_delay() self._buffer: list[BodyT] = [] # When the oldest buffered Record arrived, so its batch starts at most max_delay later. self._first_at = 0.0 - self._timer: asyncio.TimerHandle | None = None + # The timer of a client or an Activity, or the start at the next yield of Workflow code. + self._timer: asyncio.Handle | None = None self._writing: asyncio.Task[None] | None = None self._appended = 0 # Count of the Records that a write took or that a failed batch left, written or not. @@ -89,6 +114,7 @@ def append(self, *records: BodyT) -> None: self._raise_error() if not records: raise TypeError("append takes one or more values") + self._check_delay() loop = asyncio.get_running_loop() if not self._buffer: self._first_at = loop.time() @@ -142,14 +168,27 @@ def _raise_error(self) -> None: if self._error is not None: raise self._error + def _check_delay(self) -> None: + if self._max_delay is not None and temporalio.workflow.in_workflow(): + raise ValueError( + "BufferedAppender takes no max_delay in Workflow code, since a delay " + "would put a timer into History. Leave max_delay as None." + ) + def _schedule(self) -> None: if self._writing is not None or not self._buffer: return - if len(self._buffer) >= _MAX_BATCH_RECORDS: + if temporalio.workflow.in_workflow(): + # A timer of the Workflow event loop is durable. A start at the next yield + # takes every Record of this activation into one write, and adds no event. + if self._timer is None: + self._timer = asyncio.get_running_loop().call_soon(self._start) + elif len(self._buffer) >= _MAX_BATCH_RECORDS: self._start() elif self._timer is None: + max_delay = self._max_delay or _DEFAULT_MAX_DELAY self._timer = asyncio.get_running_loop().call_at( - self._first_at + self._max_delay.total_seconds(), self._start + self._first_at + max_delay.total_seconds(), self._start ) def _start(self) -> None: diff --git a/tests/nexus/test_stream_appender_workflow.py b/tests/nexus/test_stream_appender_workflow.py new file mode 100644 index 0000000000..0bb9cf2216 --- /dev/null +++ b/tests/nexus/test_stream_appender_workflow.py @@ -0,0 +1,197 @@ +from __future__ import annotations + +import asyncio +import uuid +from datetime import timedelta +from typing import Any + +from google.protobuf.message import Message + +import temporalio.api.streamservice.v1 +from temporalio import workflow +from temporalio.api.enums.v1 import EventType, StreamErrorReason +from temporalio.api.history.v1 import HistoryEvent +from temporalio.client import BufferedAppender, Client, WorkflowHandle +from temporalio.exceptions import StreamError +from temporalio.nexus.system.stream_service import StreamRef +from temporalio.nexus.system.stream_service.workflow import create_stream, get_stream +from temporalio.testing import MemoryStreamStore +from temporalio.worker import Replayer +from tests.helpers import new_worker +from tests.helpers.streams import RecordingStreamStore +from tests.nexus.test_stream_store import _with_store + +_BURST = [f"record-{index}" for index in range(40)] + + +class BatchStore(RecordingStreamStore): + """Keeps the number of Records in each append.""" + + def __init__(self) -> None: + super().__init__(MemoryStreamStore()) + self.batches: list[int] = [] + + async def before_call(self, operation: str, request: Message) -> None: + if isinstance(request, temporalio.api.streamservice.v1.AppendRecordsRequest): + self.batches.append(len(request.records)) + + +async def _yield_a_few_times() -> None: + # A turn of the loop with no timer, so the write starts and waits for its Local Activity. + for _ in range(5): + await asyncio.sleep(0) + + +@workflow.defn +class BurstWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = await create_stream(id=stream_id, body_type=str) + appender = BufferedAppender(stream) + for value in _BURST: + appender.append(value) + last_offset = await appender.flush() + assert last_offset is not None + return last_offset.hex() + + +@workflow.defn +class InFlightWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + stream = await create_stream(id=stream_id, body_type=str) + async with BufferedAppender(stream) as appender: + appender.append("a") + await _yield_a_few_times() + # The write of "a" waits for its Local Activity, so these go into the next one. + appender.append("b") + appender.append("c", "d") + + +@workflow.defn +class FailedAppendWorkflow: + @workflow.run + async def run(self, stream_id: str) -> list[Any]: + appender = BufferedAppender(get_stream(StreamRef(id=stream_id), body_type=str)) + appender.append("a") + await _yield_a_few_times() + appender.append("b", "c") + try: + await appender.flush() + except StreamError as err: + first = err + else: + return ["flushed"] + try: + appender.append("d") + except StreamError as err: + later = err + else: + return ["appended"] + return [ + StreamErrorReason.Name(first.reason), + later is first, + appender.unwritten, + ] + + +@workflow.defn +class MaxDelayWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = get_stream(StreamRef(id=stream_id), body_type=str) + try: + BufferedAppender(stream, max_delay=timedelta(milliseconds=10)) + except ValueError as err: + return str(err) + return "built" + + +async def _run( + client: Client, workflow_type: type[Any], stream_id: str +) -> tuple[WorkflowHandle[Any, Any], Any]: + async with new_worker(client, workflow_type) as worker: + handle: WorkflowHandle[Any, Any] = await client.start_workflow( + workflow_type.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + execution_timeout=timedelta(seconds=30), + ) + return handle, await handle.result() + + +def _of_type(events: list[HistoryEvent], event_type: EventType.ValueType) -> int: + return sum(1 for event in events if event.event_type == event_type) + + +async def _bodies(client: Client, stream_id: str) -> list[str]: + stream = client.get_stream(StreamRef(id=stream_id), body_type=str) + return [record.body async for record in stream.iter_read_items()] + + +async def test_appends_of_one_activation_are_one_append(client: Client): + store = BatchStore() + stream_client = _with_store(client, store) + stream_id = f"stream-{uuid.uuid4()}" + handle, last_offset = await _run(stream_client, BurstWorkflow, stream_id) + + assert store.batches == [40] + assert await _bodies(stream_client, stream_id) == _BURST + described = await stream_client.get_stream( + StreamRef(id=stream_id), body_type=str + ).describe() + assert described.last_offset is not None + assert last_offset == described.last_offset.hex() + events = list((await handle.fetch_history()).events) + # One marker for the create and one for the append. + assert _of_type(events, EventType.EVENT_TYPE_MARKER_RECORDED) == 2 + assert _of_type(events, EventType.EVENT_TYPE_TIMER_STARTED) == 0 + + +async def test_appends_during_a_workflow_append_join_the_next_one(client: Client): + store = BatchStore() + stream_client = _with_store(client, store) + stream_id = f"stream-{uuid.uuid4()}" + handle, _ = await _run(stream_client, InFlightWorkflow, stream_id) + + assert store.batches == [1, 3] + assert await _bodies(stream_client, stream_id) == ["a", "b", "c", "d"] + events = list((await handle.fetch_history()).events) + assert _of_type(events, EventType.EVENT_TYPE_MARKER_RECORDED) == 3 + assert _of_type(events, EventType.EVENT_TYPE_TIMER_STARTED) == 0 + + +async def test_a_failed_workflow_append_stops_the_appender(client: Client): + store = BatchStore() + _, result = await _run( + _with_store(client, store), FailedAppendWorkflow, f"stream-{uuid.uuid4()}" + ) + assert result == ["STREAM_ERROR_REASON_NOT_FOUND", True, 3] + assert store.batches == [1] + + +async def test_replay_of_workflow_appender_writes_nothing(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + handle, _ = await _run(stream_client, InFlightWorkflow, stream_id) + + replay_store = BatchStore() + await Replayer( + workflows=[InFlightWorkflow], + stream_config=_with_store(client, replay_store).config()["stream_config"], + ).replay_workflow(await handle.fetch_history()) + assert replay_store.operations == [] + assert await _bodies(stream_client, stream_id) == ["a", "b", "c", "d"] + + +async def test_workflow_appender_refuses_a_max_delay(client: Client): + _, message = await _run( + _with_store(client, MemoryStreamStore()), + MaxDelayWorkflow, + f"stream-{uuid.uuid4()}", + ) + assert message == ( + "BufferedAppender takes no max_delay in Workflow code, since a delay would put " + "a timer into History. Leave max_delay as None." + ) From b53f74c9d06ef0708f0f674d16d2c06b836a8565 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Sat, 14 Nov 2026 02:16:58 -0700 Subject: [PATCH 08/35] Moved BufferedAppender to temporalio.contrib.streams. The public Streams API is the generated code. A helper on top of it is a library, so it belongs in contrib. --- temporalio/client/__init__.py | 2 -- temporalio/contrib/streams/__init__.py | 14 ++++++++++++++ .../streams/_appender.py} | 15 +++++++++------ tests/contrib/streams/__init__.py | 0 .../streams/test_appender.py} | 3 ++- .../streams/test_appender_workflow.py} | 3 ++- 6 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 temporalio/contrib/streams/__init__.py rename temporalio/{client/_stream_appender.py => contrib/streams/_appender.py} (95%) create mode 100644 tests/contrib/streams/__init__.py rename tests/{nexus/test_stream_appender.py => contrib/streams/test_appender.py} (98%) rename tests/{nexus/test_stream_appender_workflow.py => contrib/streams/test_appender_workflow.py} (98%) diff --git a/temporalio/client/__init__.py b/temporalio/client/__init__.py index 7305b4be59..6fa6dd710c 100644 --- a/temporalio/client/__init__.py +++ b/temporalio/client/__init__.py @@ -185,7 +185,6 @@ ScheduleUpdate, ScheduleUpdateInput, ) -from ._stream_appender import BufferedAppender from ._stream_store import ( StreamConfig, StreamSelectContext, @@ -357,7 +356,6 @@ "TaskReachabilityType", "CloudOperationsClient", "Plugin", - "BufferedAppender", "StreamConfig", "StreamSelectContext", "StreamStore", diff --git a/temporalio/contrib/streams/__init__.py b/temporalio/contrib/streams/__init__.py new file mode 100644 index 0000000000..d1ee83436d --- /dev/null +++ b/temporalio/contrib/streams/__init__.py @@ -0,0 +1,14 @@ +"""Helpers that sit on top of the Streams API. + +:py:class:`BufferedAppender` batches the appends of one Stream. It takes a client Stream or a +Workflow Stream. + +.. warning:: + This package is experimental and may change in future versions. +""" + +from temporalio.contrib.streams._appender import BufferedAppender + +__all__ = [ + "BufferedAppender", +] diff --git a/temporalio/client/_stream_appender.py b/temporalio/contrib/streams/_appender.py similarity index 95% rename from temporalio/client/_stream_appender.py rename to temporalio/contrib/streams/_appender.py index 1fda0cc776..7b194ddf09 100644 --- a/temporalio/client/_stream_appender.py +++ b/temporalio/contrib/streams/_appender.py @@ -1,4 +1,8 @@ -"""A helper that batches the appends of one Stream.""" +"""A helper that batches the appends of one Stream. + +.. warning:: + This module is experimental and subject to change. +""" from __future__ import annotations @@ -12,11 +16,9 @@ import temporalio.nexus.system.stream_service.client import temporalio.workflow -from ._stream_store import _check_not_negative - if TYPE_CHECKING: - # The generated Workflow modules import `temporalio.workflow`, which a runtime import - # here would pull into the client package. + # The Workflow Stream class is in a private module of the generated code. Only its type + # is needed, since the appender calls the same `append` on both Streams. import temporalio.nexus.system.stream_service.workflow._resources.stream BodyT = TypeVar("BodyT") @@ -84,7 +86,8 @@ def __init__( Raises: ValueError: ``max_delay`` is negative, or it is set in Workflow code. """ - _check_not_negative(max_delay=max_delay) + if max_delay is not None and max_delay < timedelta(): + raise ValueError("max_delay must not be negative") self._stream = stream self._max_delay = max_delay self._check_delay() diff --git a/tests/contrib/streams/__init__.py b/tests/contrib/streams/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/nexus/test_stream_appender.py b/tests/contrib/streams/test_appender.py similarity index 98% rename from tests/nexus/test_stream_appender.py rename to tests/contrib/streams/test_appender.py index fd320ff474..a864335387 100644 --- a/tests/nexus/test_stream_appender.py +++ b/tests/contrib/streams/test_appender.py @@ -9,7 +9,8 @@ import temporalio.api.streamservice.v1 from temporalio.api.enums.v1 import StreamErrorReason -from temporalio.client import BufferedAppender, Client +from temporalio.client import Client +from temporalio.contrib.streams import BufferedAppender from temporalio.nexus.system.stream_service import StreamRef from temporalio.nexus.system.stream_service.client import Stream from temporalio.testing import MemoryStreamStore diff --git a/tests/nexus/test_stream_appender_workflow.py b/tests/contrib/streams/test_appender_workflow.py similarity index 98% rename from tests/nexus/test_stream_appender_workflow.py rename to tests/contrib/streams/test_appender_workflow.py index 0bb9cf2216..998149b2e9 100644 --- a/tests/nexus/test_stream_appender_workflow.py +++ b/tests/contrib/streams/test_appender_workflow.py @@ -11,7 +11,8 @@ from temporalio import workflow from temporalio.api.enums.v1 import EventType, StreamErrorReason from temporalio.api.history.v1 import HistoryEvent -from temporalio.client import BufferedAppender, Client, WorkflowHandle +from temporalio.client import Client, WorkflowHandle +from temporalio.contrib.streams import BufferedAppender from temporalio.exceptions import StreamError from temporalio.nexus.system.stream_service import StreamRef from temporalio.nexus.system.stream_service.workflow import create_stream, get_stream From c89f7d3eb1c05768949d2281165846f99cb42e89 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Sat, 14 Nov 2026 03:16:38 -0700 Subject: [PATCH 09/35] Added a README to temporalio.contrib.streams. The other contrib packages have one, and the appender's rules for Workflow code need a place. --- temporalio/contrib/streams/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 temporalio/contrib/streams/README.md diff --git a/temporalio/contrib/streams/README.md b/temporalio/contrib/streams/README.md new file mode 100644 index 0000000000..f64fa2d009 --- /dev/null +++ b/temporalio/contrib/streams/README.md @@ -0,0 +1,23 @@ +# Streams helpers + +> This package is experimental. Its API can change in a later release. + +`temporalio.contrib.streams` holds helpers that sit on top of the Streams API of the SDK. A +helper takes a client `Stream` or a Workflow `Stream`. + +## `BufferedAppender` + +`BufferedAppender` batches the appends of one Stream. `append` returns at once, and the +appender writes the buffered Records as one batch. In a client or an Activity a batch waits +at most `max_delay`. In Workflow code a batch starts when the code next yields, so it adds no +timer to History. + +```python +from temporalio.contrib.streams import BufferedAppender + +async with BufferedAppender(tokens) as appender: + for chunk in chunks: + appender.append(chunk) +``` + +The end of the `async with` block writes the Records that are still in the buffer. From f8d19e75f106082b7eeba65f2dc0b1992a5492b7 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Mon, 16 Nov 2026 19:58:05 -0700 Subject: [PATCH 10/35] Ran a Workflow's Stream close in a Local Activity. A design review decided that Workflow code needs no Activity Task Queue for a Stream. A Worker with Workflows and Stream Stores polls for Activity Tasks only for its own Activities. The docs state that a failed Workflow Task after a close leaves the Stream closed. --- temporalio/client/_stream_store.py | 68 +++++------ temporalio/worker/_worker.py | 9 +- tests/nexus/test_stream_store_workflow.py | 131 +++++++++++----------- 3 files changed, 93 insertions(+), 115 deletions(-) diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index 74ed6961b8..326711d76b 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -29,12 +29,10 @@ @dataclasses.dataclass(frozen=True) class StreamStoreCallOptions: - """Options of the Activity that calls a Stream Store for Workflow code. + """Options of the Local Activity that calls a Stream Store for Workflow code. Workflow code can't do I/O, so the default - :py:meth:`StreamStore.call_from_workflow` calls its Store from an Activity. A close - runs it as a regular Activity, and every other operation runs it as a Local - Activity. Both get these options. + :py:meth:`StreamStore.call_from_workflow` calls its Store from a Local Activity. A :py:class:`StreamConfig` holds them in :py:attr:`StreamConfig.workflow_call_options`, and every Store of that config @@ -139,9 +137,8 @@ def worker_activities(self) -> Sequence[Callable[..., Any]]: """The Activities that a Worker registers for this Store when it runs Workflows. The default is one Activity, named for this Store, that - :py:meth:`call_from_workflow` runs as a Local Activity, or as a regular Activity - for a close. A Store that overrides one of the two methods overrides the other to - match. + :py:meth:`call_from_workflow` runs as a Local Activity. A Store that overrides + one of the two methods overrides the other to match. """ return [_store_call_activity(self)] @@ -154,11 +151,13 @@ async def call_from_workflow( """Answer one Stream operation for Workflow code. It runs in Workflow code, so it does no I/O itself. The default runs the - Activity of :py:meth:`worker_activities` with the timeouts and the retry policy - of ``options``. A close runs it as a regular Activity on the Workflow's Task - Queue, after the Workflow Task completes. Every other operation runs it as a - Local Activity. A replay takes the result from History and calls no method of - this Store. + Activity of :py:meth:`worker_activities` as a Local Activity for each operation. + It takes the timeouts and the retry policy of ``options``. A replay takes the + result from History and calls no method of this Store. + + A Local Activity reaches the Store before its Workflow Task completes. If that + task fails after a close, the Stream stays closed. The retry of the task then + can't append to it. Args: operation: The Nexus operation name, ``CreateStream``, ``AppendRecords``, @@ -172,29 +171,14 @@ async def call_from_workflow( stream_operation = _STREAM_OPERATIONS[operation] # Workflow code runs no payload codec, so the Activity encodes the bodies of an # append before the Store gets them. - if operation == "CloseStream": - # A Local Activity reaches the Store before its Workflow Task completes. If - # that task then failed, the Stream would stay closed and the retry of the - # task could not append. The server schedules a regular Activity only when - # the task completes. - response = await temporalio.workflow.execute_activity( - _store_call_activity_name(self.name), - args=[operation, request.SerializeToString()], - result_type=bytes, - task_queue=temporalio.workflow.info().task_queue, - schedule_to_close_timeout=options.schedule_to_close_timeout, - start_to_close_timeout=options.start_to_close_timeout, - retry_policy=options.retry_policy, - ) - else: - response = await temporalio.workflow.execute_local_activity( - _store_call_activity_name(self.name), - args=[operation, request.SerializeToString()], - result_type=bytes, - schedule_to_close_timeout=options.schedule_to_close_timeout, - start_to_close_timeout=options.start_to_close_timeout, - retry_policy=options.retry_policy, - ) + response = await temporalio.workflow.execute_local_activity( + _store_call_activity_name(self.name), + args=[operation, request.SerializeToString()], + result_type=bytes, + schedule_to_close_timeout=options.schedule_to_close_timeout, + start_to_close_timeout=options.start_to_close_timeout, + retry_policy=options.retry_policy, + ) return stream_operation.response_type.FromString(response) @@ -436,9 +420,10 @@ class StreamConfig: one Store when there is no selector. With more than one Store and no selector, such a call raises :py:class:`temporalio.exceptions.StreamConfigError`. - A Worker with Workflows and this config polls for Activity Tasks, since a Workflow's - close runs the Store's Activity as a regular Activity. With ``no_remote_activities`` - on that Worker, another Worker of the Task Queue must run the close. + Workflow code calls a Store from a Local Activity, also for a close. So it needs + no Worker that polls for Activity Tasks. A Local Activity reaches the Store before + its Workflow Task completes. If that task fails after a close, the Stream stays + closed. The retry of the task then can't append to it. .. warning:: This API is experimental and subject to change. @@ -462,8 +447,8 @@ class StreamConfig: Store, such a call raises :py:class:`temporalio.exceptions.StreamConfigError`. For a call of Workflow code, the selector runs in Workflow code, also on a - replay. So it must give the same Store for the same context. The Activity of the - chosen Store is in History, and another answer on a replay is a + replay. So it must give the same Store for the same context. The Local Activity + of the chosen Store is in History, and another answer on a replay is a non-determinism error. A Stream that code builds from an id only goes through the selector on each call, so the selector must also give the same Store for the same id. @@ -475,8 +460,7 @@ class StreamConfig: """Options of a Store's call for Workflow code, for every Store of this config. :py:meth:`StreamStore.call_from_workflow` gets them. The default runs the Store's - Activity with their timeouts and retry policy, as a Local Activity, or as a regular - Activity for a close. + Local Activity with their timeouts and retry policy. """ _store_map: dict[str, StreamStore] = dataclasses.field( diff --git a/temporalio/worker/_worker.py b/temporalio/worker/_worker.py index 4ecce29d15..bbf68e2bf4 100644 --- a/temporalio/worker/_worker.py +++ b/temporalio/worker/_worker.py @@ -668,15 +668,14 @@ def check_activity(activity: str): # are not running an activity worker at all. Otherwise shutdown # will not proceed properly. no_remote_activities=config.get("no_remote_activities") - or self._activity_worker is None, + or not config.get("activities"), task_types=temporalio.bridge.worker.WorkerTaskTypes( enable_workflows=self._workflow_worker is not None, enable_local_activities=self._activity_worker is not None and self._workflow_worker is not None, - # A Workflow's Stream close runs a Store's Activity as a regular - # Activity, so the Activities of the Stream Stores alone make this - # worker poll for Activity Tasks. - enable_remote_activities=self._activity_worker is not None + # The Activities of the Stream Stores run as Local Activities, so + # alone they don't make this worker poll for Activity Tasks. + enable_remote_activities=bool(config.get("activities")) and not config.get("no_remote_activities"), enable_nexus=self._nexus_worker is not None, ), diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index 07ae119841..be700df1b3 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -62,11 +62,7 @@ ExecuteActivityInput, Interceptor, Replayer, - StartActivityInput, Worker, - WorkflowInboundInterceptor, - WorkflowInterceptorClassInput, - WorkflowOutboundInterceptor, ) from tests.helpers import assert_task_fail_eventually, new_worker from tests.helpers.streams import DictStreamStore, RecordingStreamStore @@ -373,67 +369,48 @@ async def run(self, stream_id: str) -> str: _fail_close_task = True -_started_activities: list[str] = [] @workflow.defn(sandboxed=False) class CloseThenFailTaskOnceWorkflow: @workflow.run - async def run(self, stream_id: str) -> None: - stream = await create_stream( - id=stream_id, - body_type=str, - id_conflict_policy=StreamIdConflictPolicy.StreamIdConflictPolicyUseExisting, - ) - await stream.append("a") - close = asyncio.create_task(stream.close()) - # A few turns of the loop let the close schedule its Activity in this task. - for _ in range(10): - await asyncio.sleep(0) + async def run(self, stream_id: str) -> str: + # The test creates the Stream, so the retry of the task starts with the append. + stream = get_stream(StreamRef(id=stream_id), body_type=str) + try: + await stream.append("a") + except StreamError as err: + return f"{StreamErrorReason.Name(err.reason)}: {err.message}" + await stream.close() global _fail_close_task if _fail_close_task: _fail_close_task = False raise RuntimeError("Failed the Workflow Task once") - await close - - -class StartedActivityInterceptor(Interceptor): - def workflow_interceptor_class( - self, input: WorkflowInterceptorClassInput - ) -> type[WorkflowInboundInterceptor]: - return _StartedActivityInbound - - -class _StartedActivityInbound(WorkflowInboundInterceptor): - def init(self, outbound: WorkflowOutboundInterceptor) -> None: - super().init(_StartedActivityOutbound(outbound)) - - -class _StartedActivityOutbound(WorkflowOutboundInterceptor): - def start_activity(self, input: StartActivityInput) -> workflow.ActivityHandle[Any]: - _started_activities.append(str(input.activity)) - return super().start_activity(input) + return "closed" class RecordingInterceptor(Interceptor): def __init__(self) -> None: self.activity_types: list[str] = [] + self.is_local: list[bool] = [] def intercept_activity( self, next: ActivityInboundInterceptor ) -> ActivityInboundInterceptor: - return _RecordingActivityInbound(next, self.activity_types) + return _RecordingActivityInbound(next, self) class _RecordingActivityInbound(ActivityInboundInterceptor): def __init__( - self, next: ActivityInboundInterceptor, activity_types: list[str] + self, next: ActivityInboundInterceptor, recording: RecordingInterceptor ) -> None: super().__init__(next) - self._activity_types = activity_types + self._recording = recording async def execute_activity(self, input: ExecuteActivityInput) -> Any: - self._activity_types.append(activity.info().activity_type) + info = activity.info() + self._recording.activity_types.append(info.activity_type) + self._recording.is_local.append(info.is_local) return await super().execute_activity(input) @@ -1088,7 +1065,7 @@ async def _describe_status(client: Client, stream_id: str) -> StreamStatus | Non return (await stream.describe()).status -async def test_workflow_close_runs_in_activity_after_its_task(client: Client): +async def test_workflow_close_runs_as_local_activity_in_its_task(client: Client): store = CloseRequestStore() stream_client = _with_store(client, store) stream_id = f"stream-{uuid.uuid4()}" @@ -1111,22 +1088,27 @@ async def test_workflow_close_runs_in_activity_after_its_task(client: Client): assert close.producer.WhichOneof("kind") == "workflow" assert close.producer.workflow.run_id == handle.result_run_id events = (await handle.fetch_history()).events - scheduled = [ - event.activity_task_scheduled_event_attributes + assert not [ + event for event in events if event.event_type == EventType.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED ] - assert [attributes.activity_type.name for attributes in scheduled] == [ - "__temporal_stream_store_call:memory" - ] - assert scheduled[0].task_queue.name == worker.task_queue markers = [ - event.marker_recorded_event_attributes.marker_name + event.marker_recorded_event_attributes for event in events if event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED ] - # One marker for the create and one for the append, and none for the close. - assert markers == ["core_local_activity", "core_local_activity"] + # One marker each for the create, the append and the close. + assert [marker.marker_name for marker in markers] == ["core_local_activity"] * 3 + # The Workflow's first task runs all three calls and completes the Workflow. + [task_completed] = [ + event.event_id + for event in events + if event.event_type == EventType.EVENT_TYPE_WORKFLOW_TASK_COMPLETED + ] + assert [marker.workflow_task_completed_event_id for marker in markers] == [ + task_completed + ] * 3 async def test_workflow_async_with_closes_the_stream(client: Client): @@ -1147,11 +1129,17 @@ async def test_workflow_async_with_closes_the_stream(client: Client): assert [record.body for record in records] == ["a"] -async def test_worker_with_workflows_only_runs_the_close(client: Client): +@pytest.mark.parametrize("no_remote_activities", [False, True]) +async def test_worker_with_workflows_only_runs_the_close( + client: Client, no_remote_activities: bool +): stream_client = _with_store(client, MemoryStreamStore()) stream_id = f"stream-{uuid.uuid4()}" async with Worker( - stream_client, task_queue=str(uuid.uuid4()), workflows=[CloseWorkflow] + stream_client, + task_queue=str(uuid.uuid4()), + workflows=[CloseWorkflow], + no_remote_activities=no_remote_activities, ) as worker: await stream_client.execute_workflow( CloseWorkflow.run, @@ -1164,29 +1152,35 @@ async def test_worker_with_workflows_only_runs_the_close(client: Client): ) -async def test_failed_workflow_task_leaves_the_stream_open(client: Client): +async def test_failed_workflow_task_after_close_leaves_the_stream_closed( + client: Client, +): global _fail_close_task _fail_close_task = True - _started_activities.clear() stream_client = _with_store(client, MemoryStreamStore()) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker( - stream_client, - CloseThenFailTaskOnceWorkflow, - interceptors=[StartedActivityInterceptor()], - ) as worker: - await stream_client.execute_workflow( + stream = await stream_client.create_stream( + id=f"stream-{uuid.uuid4()}", body_type=str + ) + async with new_worker(stream_client, CloseThenFailTaskOnceWorkflow) as worker: + handle = await stream_client.start_workflow( CloseThenFailTaskOnceWorkflow.run, - stream_id, + stream.ref.id, id=f"workflow-{uuid.uuid4()}", task_queue=worker.task_queue, ) - # The failed task started the close too, but the server never scheduled it. - assert _started_activities == ["__temporal_stream_store_call:memory"] * 2 - records = await _read_all(stream_client, stream_id) - # The retried task appends again to the open Stream, and then closes it. - assert [record.body for record in records] == ["a", "a"] - assert await _describe_status(stream_client, stream_id) == ( + result = await handle.result() + # The close of the failed task reached the Store, so the retry can't append. + assert result == "STREAM_ERROR_REASON_CLOSED: the Stream is closed" + events = (await handle.fetch_history()).events + failed_tasks = [ + event + for event in events + if event.event_type == EventType.EVENT_TYPE_WORKFLOW_TASK_FAILED + ] + assert len(failed_tasks) == 1 + records = await _read_all(stream_client, stream.ref.id) + assert [record.body for record in records] == ["a"] + assert await _describe_status(stream_client, stream.ref.id) == ( StreamStatus.StreamStatusClosed ) @@ -1203,5 +1197,6 @@ async def test_activity_interceptors_see_the_close_activity(client: Client): id=f"workflow-{uuid.uuid4()}", task_queue=worker.task_queue, ) - # The create and the append run as Local Activities, and the close as an Activity. + # The create, the append and the close each run as a Local Activity. assert interceptor.activity_types == ["__temporal_stream_store_call:memory"] * 3 + assert interceptor.is_local == [True] * 3 From af3640aea2d414b6abde99f555383e450445d08f Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Mon, 16 Nov 2026 20:03:59 -0700 Subject: [PATCH 11/35] Added current_producer to temporalio.contrib.streams. Code can keep the Producer of its attempt and pass it to a later append with `producer=`. The tests cover each kind of Producer as an Activity argument and an Activity result. --- temporalio/contrib/streams/__init__.py | 5 +- temporalio/contrib/streams/_producer.py | 30 +++++ tests/contrib/streams/test_producer.py | 162 ++++++++++++++++++++++++ 3 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 temporalio/contrib/streams/_producer.py create mode 100644 tests/contrib/streams/test_producer.py diff --git a/temporalio/contrib/streams/__init__.py b/temporalio/contrib/streams/__init__.py index d1ee83436d..5355bc28f8 100644 --- a/temporalio/contrib/streams/__init__.py +++ b/temporalio/contrib/streams/__init__.py @@ -1,14 +1,17 @@ """Helpers that sit on top of the Streams API. :py:class:`BufferedAppender` batches the appends of one Stream. It takes a client Stream or a -Workflow Stream. +Workflow Stream. :py:func:`current_producer` gives the Producer that an append of the +calling code sends by default. .. warning:: This package is experimental and may change in future versions. """ from temporalio.contrib.streams._appender import BufferedAppender +from temporalio.contrib.streams._producer import current_producer __all__ = [ "BufferedAppender", + "current_producer", ] diff --git a/temporalio/contrib/streams/_producer.py b/temporalio/contrib/streams/_producer.py new file mode 100644 index 0000000000..edbac0d527 --- /dev/null +++ b/temporalio/contrib/streams/_producer.py @@ -0,0 +1,30 @@ +"""The Producer of the calling code.""" + +from __future__ import annotations + +import temporalio.nexus.system._support +import temporalio.workflow +from temporalio.nexus.system.stream_service import Producer + + +def current_producer() -> Producer: + """Give the Producer that an append or a close of the calling code sends by default. + + An ``append`` or a ``close`` with no ``producer=`` sends this Producer. In an + Activity or a Local Activity it is the Producer of the current attempt. In Workflow + code it is the Workflow's Producer. In any other code it is the client Producer of + this process. It does no I/O. + + On a Store, an append of Workflow code writes as the Local Activity that runs it. + That holds also when the code passes this Producer in ``producer=``. + + An Activity can keep the result and pass it to a later ``append`` or ``close`` with + ``producer=``. That call then writes as this Producer with its epoch, also in a + later attempt. + + .. warning:: + This API is experimental and subject to change. + """ + if temporalio.workflow.in_workflow(): + return temporalio.nexus.system._support.workflow_producer() + return temporalio.nexus.system._support.default_producer() diff --git a/tests/contrib/streams/test_producer.py b/tests/contrib/streams/test_producer.py new file mode 100644 index 0000000000..a91c49a780 --- /dev/null +++ b/tests/contrib/streams/test_producer.py @@ -0,0 +1,162 @@ +from __future__ import annotations + +import dataclasses +import uuid +from datetime import datetime, timedelta, timezone + +import pytest + +import temporalio.activity +from temporalio import workflow +from temporalio.client import Client +from temporalio.contrib.streams import current_producer +from temporalio.converter import DataConverter +from temporalio.nexus.system.stream_service import ( + Producer, + ProducerKindClient, + ProducerKindLocalActivity, + ProducerKindStandaloneActivity, + ProducerKindWorkflow, + ProducerKindWorkflowActivity, + StreamRecord, + StreamRef, + WorkflowProducer, +) +from temporalio.nexus.system.stream_service.client import Stream +from temporalio.nexus.system.stream_service.workflow import create_stream +from temporalio.testing import ActivityEnvironment, MemoryStreamStore +from tests.helpers import new_worker +from tests.nexus.test_stream_store import _create_stream, _with_store + +_SCHEDULED = datetime(2026, 11, 12, 10, 0, 0, 123456, tzinfo=timezone.utc) + +# One `activity.Info` for each kind of Activity that can write to a Stream. +_ACTIVITY_INFOS = { + "workflow_activity": dataclasses.replace( + ActivityEnvironment.default_info(), + activity_id="step", + attempt=2, + workflow_run_id="run-1", + scheduled_time=_SCHEDULED, + current_attempt_scheduled_time=_SCHEDULED + timedelta(seconds=30), + ), + "standalone_activity": dataclasses.replace( + ActivityEnvironment.default_info(), + activity_id="job", + activity_run_id="job-run", + attempt=2, + workflow_id=None, + workflow_namespace=None, + workflow_run_id=None, + workflow_type=None, + current_attempt_scheduled_time=_SCHEDULED, + ), + "local_activity": dataclasses.replace( + ActivityEnvironment.default_info(), + activity_id="3", + attempt=2, + is_local=True, + workflow_run_id="run-1", + workflow_task=temporalio.activity.WorkflowTaskAttempt( + started_event_id=7, attempt=1, started_time=_SCHEDULED + ), + ), +} + +_ACTIVITY_KINDS = { + "workflow_activity": ProducerKindWorkflowActivity, + "standalone_activity": ProducerKindStandaloneActivity, + "local_activity": ProducerKindLocalActivity, +} + + +def _activity_producer(kind: str) -> Producer: + env = ActivityEnvironment() + env.info = _ACTIVITY_INFOS[kind] + return env.run(current_producer) + + +def _producer_of_each_kind() -> dict[str, Producer]: + return { + "workflow": Producer( + id="wf:run-1", kind=ProducerKindWorkflow(WorkflowProducer(run_id="run-1")) + ), + **{kind: _activity_producer(kind) for kind in _ACTIVITY_INFOS}, + "client": current_producer(), + } + + +async def _records(stream: Stream[str]) -> list[StreamRecord[str]]: + return [record async for record in stream.iter_read_items()] + + +async def test_current_producer_in_other_code_is_the_client_producer(client: Client): + stream = await _create_stream(client, MemoryStreamStore()) + await stream.append("a") + [record] = await _records(stream) + producer = current_producer() + assert record.producer == producer + assert isinstance(producer.kind, ProducerKindClient) + + +@pytest.mark.parametrize("kind", list(_ACTIVITY_INFOS)) +async def test_current_producer_in_an_activity_is_its_attempt_producer( + client: Client, kind: str +): + stream = await _create_stream(client, MemoryStreamStore()) + + async def append_and_give_producer() -> Producer: + await stream.append("a") + return current_producer() + + env = ActivityEnvironment() + env.info = _ACTIVITY_INFOS[kind] + producer = await env.run(append_and_give_producer) + [record] = await _records(stream) + assert record.producer == producer + assert isinstance(producer.kind, _ACTIVITY_KINDS[kind]) + + +@workflow.defn +class CurrentProducerWorkflow: + @workflow.run + async def run(self, stream_id: str) -> Producer: + stream = await create_stream(id=stream_id, body_type=str) + producer = current_producer() + await stream.append("a", producer=producer) + return producer + + +async def test_current_producer_in_workflow_code_is_the_workflow_producer( + client: Client, +): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, CurrentProducerWorkflow) as worker: + handle = await stream_client.start_workflow( + CurrentProducerWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + producer = await handle.result() + run_id = handle.result_run_id + assert run_id + assert producer == Producer( + id=f"wf:{run_id}", kind=ProducerKindWorkflow(WorkflowProducer(run_id=run_id)) + ) + # On a Store, the append writes as the Local Activity that runs it. + stream = stream_client.get_stream(StreamRef(id=stream_id), body_type=str) + [record] = await _records(stream) + assert record.producer is not None + assert isinstance(record.producer.kind, ProducerKindLocalActivity) + + +@pytest.mark.parametrize("kind", ["workflow", *_ACTIVITY_INFOS, "client"]) +async def test_producer_round_trips_as_activity_argument_and_result(kind: str): + producer = _producer_of_each_kind()[kind] + converter = DataConverter.default + # A Worker decodes an Activity's arguments and its result with their type hints. + [payload] = await converter.encode([producer]) + assert payload.metadata["messageType"] == b"temporal.api.stream.v1.Producer" + assert await converter.decode([payload], [Producer]) == [producer] From a3b1a2c076d782d7afa3d5e09898a358e52103a7 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Mon, 16 Nov 2026 22:56:19 -0700 Subject: [PATCH 12/35] Added as_producer for a Producer that an Activity stored. Heartbeat details decode with no type hint, so a stored Producer comes back as its proto message. `as_producer` gives the `Producer` back, so an Activity's retry can append as its first attempt. --- changelog/added/chattering-ledger-otter.md | 1 + temporalio/contrib/streams/README.md | 23 +++++- temporalio/contrib/streams/__init__.py | 6 +- temporalio/contrib/streams/_producer.py | 38 +++++++++- tests/contrib/streams/test_producer.py | 83 ++++++++++++++++++++-- 5 files changed, 142 insertions(+), 9 deletions(-) diff --git a/changelog/added/chattering-ledger-otter.md b/changelog/added/chattering-ledger-otter.md index b628c9c7f8..6ef01dd08d 100644 --- a/changelog/added/chattering-ledger-otter.md +++ b/changelog/added/chattering-ledger-otter.md @@ -1 +1,2 @@ Add the experimental Streams API. Client, Activity and Workflow code create a Stream, append Records to it and close it, and client and Activity code read it. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `temporalio.contrib.redis.streams.RedisStreamStore`, or `temporalio.testing.MemoryStreamStore` in tests. +Add `temporalio.contrib.streams.current_producer` and `as_producer`. An Activity can store its Producer in its heartbeat details and pass it to the appends of its retry. The retry's Records then continue the first attempt's epoch, so a reader does not take them as a replacement of the first attempt's Records. diff --git a/temporalio/contrib/streams/README.md b/temporalio/contrib/streams/README.md index f64fa2d009..2e013f6666 100644 --- a/temporalio/contrib/streams/README.md +++ b/temporalio/contrib/streams/README.md @@ -7,7 +7,7 @@ helper takes a client `Stream` or a Workflow `Stream`. ## `BufferedAppender` -`BufferedAppender` batches the appends of one Stream. `append` returns at once, and the +`BufferedAppender` batches the appends of one Stream. `append` returns immediately, and the appender writes the buffered Records as one batch. In a client or an Activity a batch waits at most `max_delay`. In Workflow code a batch starts when the code next yields, so it adds no timer to History. @@ -21,3 +21,24 @@ async with BufferedAppender(tokens) as appender: ``` The end of the `async with` block writes the Records that are still in the buffer. + +## A stored Producer + +`current_producer` gives the Producer that an append of the calling code sends when it passes +no `producer=`. An Activity that resumes from its heartbeat details can store it there, and its +retry can write as the same Producer. Heartbeat details give the Producer back as its proto +message, so `as_producer` turns it back into a `Producer`. + +```python +@activity.defn +async def write_parts(ref: StreamRef[str], parts: list[str]) -> None: + details = activity.info().heartbeat_details + producer = as_producer(details[0]) if details else current_producer() + stream = activity.client().get_stream(ref, body_type=str) + for index in range(details[1] if details else 0, len(parts)): + await stream.append(parts[index], producer=producer) + activity.heartbeat(producer, index + 1) +``` + +The retry's Records continue the first attempt's epoch, so a reader does not take them as a +replacement of the first attempt's Records. diff --git a/temporalio/contrib/streams/__init__.py b/temporalio/contrib/streams/__init__.py index 5355bc28f8..ac9a7c6392 100644 --- a/temporalio/contrib/streams/__init__.py +++ b/temporalio/contrib/streams/__init__.py @@ -2,16 +2,18 @@ :py:class:`BufferedAppender` batches the appends of one Stream. It takes a client Stream or a Workflow Stream. :py:func:`current_producer` gives the Producer that an append of the -calling code sends by default. +calling code sends by default, and :py:func:`as_producer` gives back a Producer that code +stored. .. warning:: This package is experimental and may change in future versions. """ from temporalio.contrib.streams._appender import BufferedAppender -from temporalio.contrib.streams._producer import current_producer +from temporalio.contrib.streams._producer import as_producer, current_producer __all__ = [ "BufferedAppender", + "as_producer", "current_producer", ] diff --git a/temporalio/contrib/streams/_producer.py b/temporalio/contrib/streams/_producer.py index edbac0d527..fd53cfab51 100644 --- a/temporalio/contrib/streams/_producer.py +++ b/temporalio/contrib/streams/_producer.py @@ -1,7 +1,9 @@ -"""The Producer of the calling code.""" +"""The Producer of the calling code, and a Producer from its stored form.""" from __future__ import annotations +import temporalio.api.stream.v1 +import temporalio.converter._payload_converter import temporalio.nexus.system._support import temporalio.workflow from temporalio.nexus.system.stream_service import Producer @@ -20,7 +22,8 @@ def current_producer() -> Producer: An Activity can keep the result and pass it to a later ``append`` or ``close`` with ``producer=``. That call then writes as this Producer with its epoch, also in a - later attempt. + later attempt. Heartbeat details give it back as its proto message, so pass it + through :py:func:`as_producer` first. .. warning:: This API is experimental and subject to change. @@ -28,3 +31,34 @@ def current_producer() -> Producer: if temporalio.workflow.in_workflow(): return temporalio.nexus.system._support.workflow_producer() return temporalio.nexus.system._support.default_producer() + + +def as_producer(value: object) -> Producer: + """Give a :py:class:`Producer` for a Producer or for its proto message. + + A Producer that went through a data converter with no type hint, such as one in + ``activity.info().heartbeat_details``, comes back as the proto message + ``temporalio.api.stream.v1.Producer``. This function turns it back into a + :py:class:`Producer` that ``append`` and ``close`` take in ``producer=``. A + :py:class:`Producer` comes back as it is. + + Raises: + TypeError: The value is neither a Producer nor its proto message. + + .. warning:: + This API is experimental and subject to change. + """ + if isinstance(value, Producer): + return value + if isinstance(value, temporalio.api.stream.v1.Producer): + converter = ( + temporalio.converter._payload_converter._get_transfer_type_converter( + Producer + ) + ) + if converter is None: + raise TypeError("The generated Producer has no proto transfer type") + return converter.from_transfer_type(value, Producer) + raise TypeError( + f"as_producer takes a Producer or its proto message, not {type(value).__name__}" + ) diff --git a/tests/contrib/streams/test_producer.py b/tests/contrib/streams/test_producer.py index a91c49a780..7a0a70132a 100644 --- a/tests/contrib/streams/test_producer.py +++ b/tests/contrib/streams/test_producer.py @@ -7,9 +7,11 @@ import pytest import temporalio.activity -from temporalio import workflow +import temporalio.api.stream.v1 +from temporalio import activity, workflow from temporalio.client import Client -from temporalio.contrib.streams import current_producer +from temporalio.common import RetryPolicy +from temporalio.contrib.streams import as_producer, current_producer from temporalio.converter import DataConverter from temporalio.nexus.system.stream_service import ( Producer, @@ -153,10 +155,83 @@ async def test_current_producer_in_workflow_code_is_the_workflow_producer( @pytest.mark.parametrize("kind", ["workflow", *_ACTIVITY_INFOS, "client"]) -async def test_producer_round_trips_as_activity_argument_and_result(kind: str): +async def test_producer_round_trips_through_the_default_data_converter(kind: str): producer = _producer_of_each_kind()[kind] converter = DataConverter.default - # A Worker decodes an Activity's arguments and its result with their type hints. [payload] = await converter.encode([producer]) assert payload.metadata["messageType"] == b"temporal.api.stream.v1.Producer" + # A Worker decodes an Activity's arguments and its result with their type hints. assert await converter.decode([payload], [Producer]) == [producer] + # It decodes heartbeat details with no type hint, so they give the proto message. + [detail] = await converter.decode([payload]) + assert isinstance(detail, temporalio.api.stream.v1.Producer) + assert as_producer(detail) == producer + + +def test_as_producer_keeps_a_producer_and_refuses_other_values(): + producer = current_producer() + assert as_producer(producer) is producer + with pytest.raises(TypeError, match="not str"): + as_producer("client:1") + + +@activity.defn +async def append_once_per_attempt(stream_id: str, keep_producer: bool) -> None: + info = activity.info() + stream = activity.client().get_stream(StreamRef(id=stream_id), body_type=str) + if info.attempt == 1: + await stream.append("first") + activity.heartbeat(current_producer()) + raise RuntimeError("Failed the first attempt") + producer = as_producer(info.heartbeat_details[0]) if keep_producer else None + await stream.append("second", producer=producer) + + +@workflow.defn +class AppendOncePerAttemptWorkflow: + @workflow.run + async def run(self, stream_id: str, keep_producer: bool) -> None: + await workflow.execute_activity( + append_once_per_attempt, + args=[stream_id, keep_producer], + start_to_close_timeout=timedelta(seconds=10), + heartbeat_timeout=timedelta(seconds=10), + retry_policy=RetryPolicy( + initial_interval=timedelta(milliseconds=10), maximum_attempts=2 + ), + ) + + +@pytest.mark.parametrize("keep_producer", [True, False]) +async def test_activity_retry_appends_as_its_stored_producer( + client: Client, keep_producer: bool +): + stream_client = _with_store(client, MemoryStreamStore()) + stream = await stream_client.create_stream( + id=f"stream-{uuid.uuid4()}", body_type=str + ) + async with new_worker( + stream_client, + AppendOncePerAttemptWorkflow, + activities=[append_once_per_attempt], + ) as worker: + # A FENCED append would fail the second and last attempt, and so the Workflow. + await stream_client.execute_workflow( + AppendOncePerAttemptWorkflow.run, + args=[stream.ref.id, keep_producer], + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + first, second = await _records(stream) + assert [first.body, second.body] == ["first", "second"] + assert first.producer is not None and second.producer is not None + assert first.producer.id == second.producer.id + assert isinstance(first.producer.kind, ProducerKindWorkflowActivity) + assert isinstance(second.producer.kind, ProducerKindWorkflowActivity) + first_epoch = first.producer.kind.value.epoch + second_epoch = second.producer.kind.value.epoch + if keep_producer: + # The retry continues the first attempt's epoch. + assert second_epoch == first_epoch + else: + assert (first_epoch.attempt, second_epoch.attempt) == (1, 2) From 38c8284da1a0b6c62ba5b3e51ec63e54fd283613 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Mon, 16 Nov 2026 23:01:21 -0700 Subject: [PATCH 13/35] Linked the docs of as_producer to the Producer of the Streams API. The API docs have two classes called Producer, so the short link had no target in `gen-docs`. --- temporalio/contrib/streams/_producer.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/temporalio/contrib/streams/_producer.py b/temporalio/contrib/streams/_producer.py index fd53cfab51..42169ecf4f 100644 --- a/temporalio/contrib/streams/_producer.py +++ b/temporalio/contrib/streams/_producer.py @@ -34,13 +34,13 @@ def current_producer() -> Producer: def as_producer(value: object) -> Producer: - """Give a :py:class:`Producer` for a Producer or for its proto message. + """Give a Producer for a Producer or for its proto message. A Producer that went through a data converter with no type hint, such as one in ``activity.info().heartbeat_details``, comes back as the proto message - ``temporalio.api.stream.v1.Producer``. This function turns it back into a - :py:class:`Producer` that ``append`` and ``close`` take in ``producer=``. A - :py:class:`Producer` comes back as it is. + ``temporalio.api.stream.v1.Producer``. This function turns it back into the + :py:class:`temporalio.nexus.system.stream_service.Producer` that ``append`` and + ``close`` take in ``producer=``. Such a Producer comes back as it is. Raises: TypeError: The value is neither a Producer nor its proto message. From ac04e4a6b1e415d20e7cd110bb67814255979793 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Mon, 16 Nov 2026 23:08:42 -0700 Subject: [PATCH 14/35] Moved the Stream crates and renamed last_write_ttl to producer_idle_ttl. The Stream crate lets a Workflow Producer append with its Workflow Task as the epoch. Both crates call the option `producer_idle_ttl`, since it counts from any Producer's last write. --- temporalio/bridge/Cargo.lock | 2 +- temporalio/bridge/Cargo.toml | 2 +- temporalio/bridge/sdk-core | 2 +- temporalio/bridge/src/lib.rs | 12 ++++++------ temporalio/bridge/src/streams.rs | 8 ++++---- temporalio/bridge/streams.py | 8 ++++---- temporalio/contrib/redis/streams.py | 13 +++++++------ temporalio/testing/_stream_store.py | 12 ++++++------ 8 files changed, 30 insertions(+), 29 deletions(-) diff --git a/temporalio/bridge/Cargo.lock b/temporalio/bridge/Cargo.lock index 850fbaa8d1..064e7b3694 100644 --- a/temporalio/bridge/Cargo.lock +++ b/temporalio/bridge/Cargo.lock @@ -2956,7 +2956,7 @@ dependencies = [ [[package]] name = "temporalio-streams-redis" version = "0.1.0" -source = "git+https://github.com/temporal-community/sdk-streams?rev=63799c4fd9431a9f12bed78fdff66f144f1e0ac4#63799c4fd9431a9f12bed78fdff66f144f1e0ac4" +source = "git+https://github.com/temporal-community/sdk-streams?rev=0800f33196fd82e05fee6c2741cf788b6d4335d9#0800f33196fd82e05fee6c2741cf788b6d4335d9" dependencies = [ "async-trait", "aws-lc-rs", diff --git a/temporalio/bridge/Cargo.toml b/temporalio/bridge/Cargo.toml index a49caf5eee..5d48965de6 100644 --- a/temporalio/bridge/Cargo.toml +++ b/temporalio/bridge/Cargo.toml @@ -82,7 +82,7 @@ temporalio-sdk-core = { version = "0.11.0", path = "./sdk-core/crates/sdk-core", # The Stream runtime and its memory Store, from the submodule. temporalio-streams = { path = "./sdk-core/crates/streams", optional = true } # The Redis Store, which lives in its own repo. -temporalio-streams-redis = { git = "https://github.com/temporal-community/sdk-streams", rev = "63799c4fd9431a9f12bed78fdff66f144f1e0ac4", default-features = false, optional = true } +temporalio-streams-redis = { git = "https://github.com/temporal-community/sdk-streams", rev = "0800f33196fd82e05fee6c2741cf788b6d4335d9", default-features = false, optional = true } tokio = "1.26" # Matches the sdk-core client crate's rustls stack; used to build the custom # server certificate verifier for ClientTlsConfig.verification_server_name. diff --git a/temporalio/bridge/sdk-core b/temporalio/bridge/sdk-core index 59f4b33f1d..2c5da4644b 160000 --- a/temporalio/bridge/sdk-core +++ b/temporalio/bridge/sdk-core @@ -1 +1 @@ -Subproject commit 59f4b33f1d90bb5ac4582ea465b4e8597c507214 +Subproject commit 2c5da4644be319357ba30e89b6a380d98bc6a97b diff --git a/temporalio/bridge/src/lib.rs b/temporalio/bridge/src/lib.rs index 1eb7b0be62..ebe9fef7de 100644 --- a/temporalio/bridge/src/lib.rs +++ b/temporalio/bridge/src/lib.rs @@ -117,14 +117,14 @@ fn raise_in_thread(py: Python, thread_id: std::os::raw::c_long, exc: &Bound<'_, #[cfg(feature = "streams")] #[pyfunction] -#[pyo3(signature = (runtime_ref, store_name, retention=None, last_write_ttl=None))] +#[pyo3(signature = (runtime_ref, store_name, retention=None, producer_idle_ttl=None))] fn new_memory_stream_runtime( runtime_ref: &runtime::RuntimeRef, store_name: String, retention: Option, - last_write_ttl: Option, + producer_idle_ttl: Option, ) -> PyResult { - streams::new_memory_stream_runtime(runtime_ref, store_name, retention, last_write_ttl) + streams::new_memory_stream_runtime(runtime_ref, store_name, retention, producer_idle_ttl) } #[cfg(feature = "streams-redis")] @@ -136,7 +136,7 @@ fn new_memory_stream_runtime( cluster, key_prefix, retention, - last_write_ttl, + producer_idle_ttl, response_timeout, store_name, wait_replicas, @@ -149,7 +149,7 @@ fn new_redis_stream_runtime( cluster: bool, key_prefix: Option, retention: Option, - last_write_ttl: Option, + producer_idle_ttl: Option, response_timeout: Option, store_name: String, wait_replicas: i32, @@ -162,7 +162,7 @@ fn new_redis_stream_runtime( cluster, key_prefix, retention, - last_write_ttl, + producer_idle_ttl, response_timeout, store_name, wait_replicas, diff --git a/temporalio/bridge/src/streams.rs b/temporalio/bridge/src/streams.rs index 0f9218523c..5eb56cc724 100644 --- a/temporalio/bridge/src/streams.rs +++ b/temporalio/bridge/src/streams.rs @@ -50,7 +50,7 @@ pub fn new_memory_stream_runtime( runtime_ref: &runtime::RuntimeRef, store_name: String, retention: Option, - last_write_ttl: Option, + producer_idle_ttl: Option, ) -> PyResult { let runtime = runtime_ref.runtime.clone(); runtime.assert_same_process("create a Stream runtime")?; @@ -61,7 +61,7 @@ pub fn new_memory_stream_runtime( store_name, temporalio_streams::MemoryStreamOptions { retention, - last_write_ttl, + producer_idle_ttl, }, meter, ); @@ -75,7 +75,7 @@ pub struct RedisStreamOptions { pub cluster: bool, pub key_prefix: Option, pub retention: Option, - pub last_write_ttl: Option, + pub producer_idle_ttl: Option, pub response_timeout: Option, pub store_name: String, pub wait_replicas: i32, @@ -100,7 +100,7 @@ pub fn new_redis_stream_runtime( cluster: options.cluster, key_prefix: options.key_prefix.unwrap_or_default(), retention: proto_duration("retention", options.retention)?, - last_write_ttl: proto_duration("last_write_ttl", options.last_write_ttl)?, + producer_idle_ttl: proto_duration("producer_idle_ttl", options.producer_idle_ttl)?, response_timeout: proto_duration("response_timeout", options.response_timeout)?, store_name: options.store_name, wait_replicas: options.wait_replicas, diff --git a/temporalio/bridge/streams.py b/temporalio/bridge/streams.py index b189517c25..221c3d0d67 100644 --- a/temporalio/bridge/streams.py +++ b/temporalio/bridge/streams.py @@ -25,7 +25,7 @@ def new_memory( *, name: str, retention: timedelta | None, - last_write_ttl: timedelta | None, + producer_idle_ttl: timedelta | None, ) -> StreamRuntime: """Build the runtime on a new memory Store. @@ -40,7 +40,7 @@ def new_memory( raise temporalio.exceptions.StreamConfigError( "This build of temporalio has no Stream runtime" ) - return StreamRuntime(new(runtime._ref, name, retention, last_write_ttl)) + return StreamRuntime(new(runtime._ref, name, retention, producer_idle_ttl)) @staticmethod def new_redis( @@ -50,7 +50,7 @@ def new_redis( cluster: bool, key_prefix: str | None, retention: timedelta | None, - last_write_ttl: timedelta | None, + producer_idle_ttl: timedelta | None, response_timeout: timedelta | None, name: str, wait_replicas: int, @@ -77,7 +77,7 @@ def new_redis( cluster=cluster, key_prefix=key_prefix, retention=retention, - last_write_ttl=last_write_ttl, + producer_idle_ttl=producer_idle_ttl, response_timeout=response_timeout, store_name=name, wait_replicas=wait_replicas, diff --git a/temporalio/contrib/redis/streams.py b/temporalio/contrib/redis/streams.py index 389601406e..8a366fe5ff 100644 --- a/temporalio/contrib/redis/streams.py +++ b/temporalio/contrib/redis/streams.py @@ -47,7 +47,7 @@ def __init__( cluster: bool = False, key_prefix: str | None = None, retention: timedelta | None = None, - last_write_ttl: timedelta | None = None, + producer_idle_ttl: timedelta | None = None, response_timeout: timedelta | None = None, wait_replicas: int = 0, wait_timeout: timedelta | None = None, @@ -67,8 +67,9 @@ def __init__( A Stream id is unique in one Redis and key prefix. retention: The default Retention of a new Stream, which is how long the Stream stays readable after its close. ``None`` takes 7 days. - last_write_ttl: How long an open Stream stays after its last write. - ``None`` keeps an open Stream and every Record until its close. + producer_idle_ttl: How long an open Stream stays after the last write of any + Producer. ``None`` keeps an open Stream and every Record until its + close. response_timeout: The longest time that one Redis call can take. ``None`` takes 5 seconds. wait_replicas: How many replicas must have each write before it answers, @@ -82,7 +83,7 @@ def __init__( """ temporalio.client._stream_store._check_not_negative( retention=retention, - last_write_ttl=last_write_ttl, + producer_idle_ttl=producer_idle_ttl, response_timeout=response_timeout, wait_timeout=wait_timeout, ) @@ -91,7 +92,7 @@ def __init__( self._cluster = cluster self._key_prefix = key_prefix self._retention = retention - self._last_write_ttl = last_write_ttl + self._producer_idle_ttl = producer_idle_ttl self._response_timeout = response_timeout self._wait_replicas = wait_replicas self._wait_timeout = wait_timeout @@ -105,7 +106,7 @@ def _new_stream_runtime( cluster=self._cluster, key_prefix=self._key_prefix, retention=self._retention, - last_write_ttl=self._last_write_ttl, + producer_idle_ttl=self._producer_idle_ttl, response_timeout=self._response_timeout, name=self._name, wait_replicas=self._wait_replicas, diff --git a/temporalio/testing/_stream_store.py b/temporalio/testing/_stream_store.py index 86ba0ac16d..1220bbc5b7 100644 --- a/temporalio/testing/_stream_store.py +++ b/temporalio/testing/_stream_store.py @@ -25,7 +25,7 @@ def __init__( *, name: str = "memory", retention: timedelta | None = None, - last_write_ttl: timedelta | None = None, + producer_idle_ttl: timedelta | None = None, runtime: temporalio.runtime.Runtime | None = None, ) -> None: """Create a memory Store. It builds its Stream runtime at its first call. @@ -34,17 +34,17 @@ def __init__( name: Name of the Store, which the refs of its Streams carry. retention: How long a closed Stream stays. ``None`` takes the default of the Stream runtime. - last_write_ttl: How long an open Stream stays after its last write. - ``None`` keeps an open Stream until its close. + producer_idle_ttl: How long an open Stream stays after the last write of any + Producer. ``None`` keeps an open Stream until its close. runtime: Runtime whose Tokio threads run the Store. ``None`` takes :py:meth:`temporalio.runtime.Runtime.default` at the first call. """ temporalio.client._stream_store._check_not_negative( - retention=retention, last_write_ttl=last_write_ttl + retention=retention, producer_idle_ttl=producer_idle_ttl ) super().__init__(name=name, runtime=runtime) self._retention = retention - self._last_write_ttl = last_write_ttl + self._producer_idle_ttl = producer_idle_ttl def _new_stream_runtime( self, runtime: temporalio.bridge.runtime.Runtime @@ -53,5 +53,5 @@ def _new_stream_runtime( runtime, name=self._name, retention=self._retention, - last_write_ttl=self._last_write_ttl, + producer_idle_ttl=self._producer_idle_ttl, ) From 3f67ec88fb6fe83af0716005561f2aa2c3ef8c2a Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Mon, 16 Nov 2026 23:10:41 -0700 Subject: [PATCH 15/35] Wrote every append and close of a Workflow as the Workflow Producer. The Store keeps one Producer state for a run, so it fences a late write of a failed Workflow Task. A Workflow's appends and close on one Stream run one at a time, so their sequences go up in order. --- temporalio/client/_stream_store.py | 69 +++++---- temporalio/contrib/streams/_producer.py | 4 +- temporalio/worker/_stream_store.py | 32 +++- temporalio/worker/_workflow_instance.py | 3 + tests/contrib/redis/test_streams.py | 6 +- tests/contrib/streams/test_producer.py | 12 +- tests/nexus/test_stream_store_workflow.py | 170 ++++++++++++++++++++-- 7 files changed, 247 insertions(+), 49 deletions(-) diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index 326711d76b..c2f1364c99 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -15,8 +15,6 @@ import temporalio.bridge.runtime import temporalio.common import temporalio.converter -import temporalio.converter._payload_converter -import temporalio.nexus.system._support import temporalio.nexus.system.stream_service import temporalio.runtime import temporalio.workflow @@ -52,8 +50,8 @@ class StreamStoreCallOptions: default_factory=lambda: temporalio.common.RetryPolicy(maximum_attempts=1) ) """Retries of a failed call. The default is one attempt. In one Workflow Task, each - attempt of a Workflow's append without an explicit ``producer`` sends the same - Producer, epoch and sequence, so the Store writes the batch once.""" + attempt of an append with the Workflow's Producer sends the same epoch and + sequence, so the Store writes the batch once.""" def __post_init__(self) -> None: """Check that a timeout is set.""" @@ -342,8 +340,14 @@ def _store_call_activity( @temporalio.activity.defn(name=_store_call_activity_name(store.name)) async def call(operation: str, request: bytes) -> bytes: message = _STREAM_OPERATIONS[operation].request_type.FromString(request) - if isinstance(message, temporalio.api.streamservice.v1.AppendRecordsRequest): - _with_local_activity_producer(message) + if isinstance( + message, + ( + temporalio.api.streamservice.v1.AppendRecordsRequest, + temporalio.api.streamservice.v1.CloseStreamRequest, + ), + ): + _set_workflow_task_epoch_and_sequence(message) response = await _call_stream_store( store, temporalio.activity.client().data_converter, operation, message ) @@ -352,30 +356,38 @@ async def call(operation: str, request: bytes) -> bytes: return call -def _with_local_activity_producer( - request: temporalio.api.streamservice.v1.AppendRecordsRequest, +def _set_workflow_task_epoch_and_sequence( + request: temporalio.api.streamservice.v1.AppendRecordsRequest + | temporalio.api.streamservice.v1.CloseStreamRequest, ) -> None: - # On a Store, a Workflow appends as the Local Activity that runs the append, so the - # Store can fence the write of a failed Workflow Task. A Producer that Workflow code + # Every append and the close of a Workflow run write as one Producer, the Workflow. Its + # epoch is the Workflow Task that dispatched this Local Activity, so the Store fences a + # late write of a failed task, whichever call made it. A Producer that Workflow code # gave of another kind reaches the Store as it is. + info = temporalio.activity.info() + if request.producer.WhichOneof("kind") != "workflow" or not info.is_local: + return + task = info.workflow_task + if task is None: + raise RuntimeError( + "A Stream write of Workflow code needs the Workflow Task of its Local " + "Activity, and the Worker's Core gave none" + ) + epoch = request.producer.workflow.epoch + epoch.Clear() + epoch.started_event_id = task.started_event_id + epoch.attempt = task.attempt + if task.started_time is not None: + epoch.started_time.FromDatetime(task.started_time) if ( - request.producer.WhichOneof("kind") != "workflow" - or not temporalio.activity.info().is_local + isinstance(request, temporalio.api.streamservice.v1.AppendRecordsRequest) + and not request.sequence ): - return - converter = temporalio.converter._payload_converter._get_transfer_type_converter( - temporalio.nexus.system.stream_service.Producer - ) - if converter is None: - raise TypeError("The generated Producer has no proto transfer type") - request.producer.CopyFrom( - converter.to_transfer_type(temporalio.nexus.system._support.default_producer()) - ) - # Each retry of this Local Activity in one Workflow Task sends the same Producer, epoch - # and sequence, so the Store writes the batch once and gives back its first offsets. - request.producer.local_activity.epoch.attempt = 1 - if not request.sequence: - request.sequence = 1 + # The activity id of a Local Activity is its sequence number in the run. The + # Workflow's calls on one Stream run one at a time, so these numbers go up in the + # order of the appends. Each retry of this Local Activity sends the same number, + # so the Store writes the batch once and gives back its first offsets. + request.sequence = int(info.activity_id) @dataclasses.dataclass(frozen=True) @@ -420,6 +432,11 @@ class StreamConfig: one Store when there is no selector. With more than one Store and no selector, such a call raises :py:class:`temporalio.exceptions.StreamConfigError`. + An append or a close of Workflow code writes as the Workflow, with the Workflow Task + that runs it as its epoch. So the Store fences a late write of a failed Workflow + Task. The appends and the close of one Workflow on one Stream run one at a time, in + the order of the calls. + Workflow code calls a Store from a Local Activity, also for a close. So it needs no Worker that polls for Activity Tasks. A Local Activity reaches the Store before its Workflow Task completes. If that task fails after a close, the Stream stays diff --git a/temporalio/contrib/streams/_producer.py b/temporalio/contrib/streams/_producer.py index 42169ecf4f..f94b0d1baf 100644 --- a/temporalio/contrib/streams/_producer.py +++ b/temporalio/contrib/streams/_producer.py @@ -17,8 +17,8 @@ def current_producer() -> Producer: code it is the Workflow's Producer. In any other code it is the client Producer of this process. It does no I/O. - On a Store, an append of Workflow code writes as the Local Activity that runs it. - That holds also when the code passes this Producer in ``producer=``. + On a Store, an append or a close of Workflow code writes as this Producer, with the + Workflow Task that runs it as its epoch. An Activity can keep the result and pass it to a later ``append`` or ``close`` with ``producer=``. That call then writes as this Producer with its epoch, also in a diff --git a/temporalio/worker/_stream_store.py b/temporalio/worker/_stream_store.py index 947cc7aeba..daaac73e6b 100644 --- a/temporalio/worker/_stream_store.py +++ b/temporalio/worker/_stream_store.py @@ -36,6 +36,11 @@ def _no_stream_config_error() -> temporalio.exceptions.ApplicationError: # pyri ) +# The writes of a Workflow to one Stream, which run one at a time in the order of the +# calls. A create and a describe need no order. +_ORDERED_OPERATIONS = frozenset({"AppendRecords", "CloseStream"}) + + def _workflow_target() -> StorageDriverWorkflowInfo: # External storage builds the same value for a Workflow. info = temporalio.workflow.info() @@ -74,12 +79,24 @@ def start_operation( ) payload_converter, _ = instance._system_nexus_converters(input) request = payload_converter._to_transfer_value(input.input) + ref = _request_ref(input.input) # The choice runs in Workflow code, so a ref of a Store that the config doesn't # have raises `StreamConfigError` here, with no I/O, and fails the Workflow Task. - store = self.config._store_for(_request_ref(input.input), _workflow_target()) + store = self.config._store_for(ref, _workflow_target()) options = self.config.workflow_call_options + tails = instance._stream_write_tails + tail_key = (store.name, ref.id) + previous = ( + tails.get(tail_key) if input.operation_name in _ORDERED_OPERATIONS else None + ) async def result() -> OutputT: + if previous is not None and not previous.done(): + # The Workflow's appends carry sequences that go up in the order of the + # calls, and the Store refuses one below the last. So a write starts its + # Local Activity only after the one before it ended, also when that one + # failed. + await temporalio.workflow.wait([previous]) try: response = await store.call_from_workflow( input.operation_name, request, options @@ -111,9 +128,18 @@ async def result() -> OutputT: stream_operation.check_response(store, response), input.output_type ) - return _StreamStoreOperationHandle( - instance.create_task(result(), name=f"stream store: {input.operation_name}") + task = instance.create_task( + result(), name=f"stream store: {input.operation_name}" ) + if input.operation_name in _ORDERED_OPERATIONS: + tails[tail_key] = task + + def forget(done: asyncio.Task[Any]) -> None: + if tails.get(tail_key) is done: + del tails[tail_key] + + task.add_done_callback(forget) + return _StreamStoreOperationHandle(task) class _StreamStoreOperationHandle(temporalio.workflow.NexusOperationHandle[OutputT]): diff --git a/temporalio/worker/_workflow_instance.py b/temporalio/worker/_workflow_instance.py index 07f63a69dc..454a7c2c04 100644 --- a/temporalio/worker/_workflow_instance.py +++ b/temporalio/worker/_workflow_instance.py @@ -314,6 +314,9 @@ def __init__(self, det: WorkflowInstanceDetails) -> None: # reads the previous run's result and failure with the same context. self._nexus_serialization_context = det.nexus_serialization_context self._stream_store = det.stream_store + # The last append or close of each Stream, by Store name and Stream id, so the + # next one of the same Stream starts after it. + self._stream_write_tails: dict[tuple[str, str], asyncio.Task[Any]] = {} if self._nexus_serialization_context: self._result_payload_converter = self._payload_converter_with_context( self._nexus_serialization_context diff --git a/tests/contrib/redis/test_streams.py b/tests/contrib/redis/test_streams.py index 95a5b3a37e..b60a7e4dfb 100644 --- a/tests/contrib/redis/test_streams.py +++ b/tests/contrib/redis/test_streams.py @@ -19,6 +19,7 @@ Producer, ProducerKindClient, ProducerKindLocalActivity, + ProducerKindWorkflow, ProducerKindWorkflowActivity, StreamRef, StreamStatus, @@ -166,9 +167,12 @@ async def test_workflow_appends_and_closes(client: Client): assert [record.body for record in records] == ["a", "b"] run_id = handle.result_run_id for record in records: + # A Workflow writes as itself, with its Workflow Task as the epoch. assert record.producer is not None - assert isinstance(record.producer.kind, ProducerKindLocalActivity) + assert record.producer.id == f"wf:{run_id}" + assert isinstance(record.producer.kind, ProducerKindWorkflow) assert record.producer.kind.value.run_id == run_id + assert record.producer.kind.value.epoch is not None described = await stream.describe() assert described.status == StreamStatus.StreamStatusClosed err = await _stream_error(stream.append("c")) diff --git a/tests/contrib/streams/test_producer.py b/tests/contrib/streams/test_producer.py index 7a0a70132a..9dcd4e3408 100644 --- a/tests/contrib/streams/test_producer.py +++ b/tests/contrib/streams/test_producer.py @@ -22,6 +22,7 @@ ProducerKindWorkflowActivity, StreamRecord, StreamRef, + StreamStatus, WorkflowProducer, ) from temporalio.nexus.system.stream_service.client import Stream @@ -126,6 +127,7 @@ async def run(self, stream_id: str) -> Producer: stream = await create_stream(id=stream_id, body_type=str) producer = current_producer() await stream.append("a", producer=producer) + await stream.close(producer=producer) return producer @@ -147,11 +149,17 @@ async def test_current_producer_in_workflow_code_is_the_workflow_producer( assert producer == Producer( id=f"wf:{run_id}", kind=ProducerKindWorkflow(WorkflowProducer(run_id=run_id)) ) - # On a Store, the append writes as the Local Activity that runs it. + # On a Store, the append writes as the Workflow, with its Workflow Task as the epoch. + # The close passes the Store's check of that epoch too. stream = stream_client.get_stream(StreamRef(id=stream_id), body_type=str) [record] = await _records(stream) assert record.producer is not None - assert isinstance(record.producer.kind, ProducerKindLocalActivity) + assert record.producer.id == producer.id + assert isinstance(record.producer.kind, ProducerKindWorkflow) + assert record.producer.kind.value.epoch is not None + assert record.producer.kind.value.epoch.attempt == 1 + described = await stream.describe() + assert described.status == StreamStatus.StreamStatusClosed @pytest.mark.parametrize("kind", ["workflow", *_ACTIVITY_INFOS, "client"]) diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index be700df1b3..9bb641aa9e 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -45,6 +45,7 @@ Producer, ProducerKindClient, ProducerKindLocalActivity, + ProducerKindWorkflow, ReadRecordsRequest, StreamIdConflictPolicy, StreamRecord, @@ -455,6 +456,17 @@ async def _read_all(client: Client, stream_id: str) -> list[StreamRecord[str]]: return [record async for record in stream.iter_read_items()] +def _epoch(producer: Producer) -> tuple[int, int, int]: + # The Workflow Task, then the attempt of the Local Activity, which a Workflow lacks. + if isinstance(producer.kind, ProducerKindWorkflow): + task = producer.kind.value.epoch + assert task is not None + return (task.started_event_id, task.attempt, 0) + assert isinstance(producer.kind, ProducerKindLocalActivity) + local = producer.kind.value.epoch + return (local.task.started_event_id, local.task.attempt, local.attempt) + + def _producers(records: list[StreamRecord[str]]) -> list[Producer]: producers = [record.producer for record in records] assert all(producer is not None for producer in producers) @@ -506,6 +518,15 @@ async def test_workflow_stream_uses_store(client: Client, append_workflow: type[ assert run_id first, second = _producers(records) assert first == second + if append_workflow is AppendWorkflow: + # The Workflow writes as itself, with its first Workflow Task as the epoch. + assert first.id == f"wf:{run_id}" + assert isinstance(first.kind, ProducerKindWorkflow) + assert first.kind.value.run_id == run_id + assert first.kind.value.epoch is not None + assert first.kind.value.epoch.started_event_id > 0 + assert first.kind.value.epoch.attempt == 1 + return # The create is the run's first Local Activity, so the append is its second. assert first.id == f"la:{run_id}:2" assert isinstance(first.kind, ProducerKindLocalActivity) @@ -690,11 +711,12 @@ async def test_client_append_after_workflow_append_keeps_offset_order( assert len(set(offsets)) == len(offsets) assert offsets[-1] == appended.last_offset kinds = [type(producer.kind) for producer in _producers(records)] - assert kinds == [ - ProducerKindLocalActivity, - ProducerKindLocalActivity, - ProducerKindClient, - ] + workflow_kind = ( + ProducerKindWorkflow + if append_workflow is AppendWorkflow + else ProducerKindLocalActivity + ) + assert kinds == [workflow_kind, workflow_kind, ProducerKindClient] async def test_workflow_stream_store_call_options_retry(client: Client): @@ -768,13 +790,10 @@ async def test_workflow_task_retry_appends_with_higher_epoch( assert [record.body for record in records] == [*_BODIES, *_BODIES] producers = _producers(records) assert len({producer.id for producer in producers}) == 1 + if not in_local_activity: + assert producers[0].id.startswith("wf:") - def epoch(producer: Producer) -> tuple[int, int, int]: - assert isinstance(producer.kind, ProducerKindLocalActivity) - local = producer.kind.value.epoch - return (local.task.started_event_id, local.task.attempt, local.attempt) - - first, second, third, fourth = (epoch(producer) for producer in producers) + first, second, third, fourth = (_epoch(producer) for producer in producers) assert first == second assert third == fourth assert third > first @@ -1028,8 +1047,9 @@ async def test_workflow_append_retry_in_one_task_writes_once(client: Client): ) first, second = store.appends assert first.producer == second.producer - assert first.producer.local_activity.epoch.attempt == 1 - assert first.sequence == second.sequence == 1 + assert first.producer.workflow.epoch.attempt == 1 + # The append is the run's second Local Activity, after the create. + assert first.sequence == second.sequence == 2 records = await _read_all(stream_client, stream_id) assert [record.body for record in records] == _BODIES assert store.first_response is not None @@ -1083,10 +1103,12 @@ async def test_workflow_close_runs_as_local_activity_in_its_task(client: Client) with pytest.raises(StreamError) as err: await stream_client.get_stream(ref, body_type=str).append("b") assert err.value.reason == StreamErrorReason.STREAM_ERROR_REASON_CLOSED - # The close keeps the Workflow Producer of the request. + # The close writes as the Workflow, with its Workflow Task as the epoch. [close] = store.closes - assert close.producer.WhichOneof("kind") == "workflow" + assert close.producer.id == f"wf:{handle.result_run_id}" assert close.producer.workflow.run_id == handle.result_run_id + assert close.producer.workflow.epoch.started_event_id > 0 + assert close.producer.workflow.epoch.attempt == 1 events = (await handle.fetch_history()).events assert not [ event @@ -1200,3 +1222,121 @@ async def test_activity_interceptors_see_the_close_activity(client: Client): # The create, the append and the close each run as a Local Activity. assert interceptor.activity_types == ["__temporal_stream_store_call:memory"] * 3 assert interceptor.is_local == [True] * 3 + + +class SlowFirstAppendStore(LostAnswerStore): + """Keeps each append request, and holds the first append for a while.""" + + def __init__(self) -> None: + super().__init__(lose_first_answer=False) + self._held = False + + async def before_call(self, operation: str, request: Message) -> None: + # Without the order of the Workflow's writes, a later call would reach the Store + # first while this one waits. + if operation == "AppendRecords" and not self._held: + self._held = True + await asyncio.sleep(0.5) + + +@workflow.defn +class TwoAppendsWorkflow: + @workflow.run + async def run(self, stream_id: str, sleep_between: bool) -> None: + stream = await create_stream(id=stream_id, body_type=str) + await stream.append("a") + if sleep_between: + # A timer ends the Workflow Task, so the next append runs in a later one. + await workflow.sleep(timedelta(milliseconds=10)) + await stream.append("b") + + +@workflow.defn +class GatheredAppendsWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + stream = await create_stream(id=stream_id, body_type=str) + await asyncio.gather(stream.append("a"), stream.append("b"), stream.append("c")) + + +@workflow.defn +class GatheredAppendAndCloseWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + stream = await create_stream(id=stream_id, body_type=str) + await asyncio.gather(stream.append("a"), stream.close()) + + +async def _run_two_appends( + client: Client, store: StreamStore, sleep_between: bool +) -> tuple[str, list[StreamRecord[str]]]: + stream_client = _with_store(client, store) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, TwoAppendsWorkflow) as worker: + handle = await stream_client.start_workflow( + TwoAppendsWorkflow.run, + args=[stream_id, sleep_between], + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + await handle.result() + assert handle.result_run_id + return handle.result_run_id, await _read_all(stream_client, stream_id) + + +async def test_workflow_appends_in_one_task_write_as_one_producer(client: Client): + store = LostAnswerStore(lose_first_answer=False) + run_id, records = await _run_two_appends(client, store, sleep_between=False) + assert [record.body for record in records] == ["a", "b"] + first, second = _producers(records) + assert first == second + assert first.id == f"wf:{run_id}" + first_request, second_request = store.appends + assert first_request.producer == second_request.producer + assert 0 < first_request.sequence < second_request.sequence + + +async def test_workflow_append_in_a_later_task_has_a_higher_epoch(client: Client): + run_id, records = await _run_two_appends( + client, MemoryStreamStore(), sleep_between=True + ) + first, second = _producers(records) + assert first.id == second.id == f"wf:{run_id}" + assert _epoch(second) > _epoch(first) + + +async def test_workflow_appends_started_together_write_in_call_order(client: Client): + store = SlowFirstAppendStore() + stream_client = _with_store(client, store) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, GatheredAppendsWorkflow) as worker: + await stream_client.execute_workflow( + GatheredAppendsWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + records = await _read_all(stream_client, stream_id) + assert [record.body for record in records] == ["a", "b", "c"] + assert [request.sequence for request in store.appends] == sorted( + request.sequence for request in store.appends + ) + + +async def test_workflow_close_started_with_an_append_comes_after_it(client: Client): + store = SlowFirstAppendStore() + stream_client = _with_store(client, store) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, GatheredAppendAndCloseWorkflow) as worker: + await stream_client.execute_workflow( + GatheredAppendAndCloseWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert store.operations == ["CreateStream", "AppendRecords", "CloseStream"] + records = await _read_all(stream_client, stream_id) + assert [record.body for record in records] == ["a"] + assert await _describe_status(stream_client, stream_id) == ( + StreamStatus.StreamStatusClosed + ) From 688209ce12d1e4d8edfae767574b988be3113fc6 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Mon, 16 Nov 2026 23:14:34 -0700 Subject: [PATCH 16/35] Moved Core to the commit with the idle TTL's name in a comment. The expired reason of a Stream said the old name of the Store option. --- temporalio/api/enums/v1/stream_pb2.pyi | 4 ++-- temporalio/bridge/sdk-core | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/temporalio/api/enums/v1/stream_pb2.pyi b/temporalio/api/enums/v1/stream_pb2.pyi index ac57ccd183..7cbd228962 100644 --- a/temporalio/api/enums/v1/stream_pb2.pyi +++ b/temporalio/api/enums/v1/stream_pb2.pyi @@ -92,7 +92,7 @@ class _StreamErrorReasonEnumTypeWrapper( STREAM_ERROR_REASON_NOT_FOUND: _StreamErrorReason.ValueType # 1 """Nobody created the Stream. NOT_FOUND.""" STREAM_ERROR_REASON_EXPIRED: _StreamErrorReason.ValueType # 2 - """The Stream's Retention or its last-write TTL passed. NOT_FOUND.""" + """The Stream's Retention or its Producer idle TTL passed. NOT_FOUND.""" STREAM_ERROR_REASON_CLOSED: _StreamErrorReason.ValueType # 3 """The Stream is closed. FAILED_PRECONDITION.""" STREAM_ERROR_REASON_FENCED: _StreamErrorReason.ValueType # 4 @@ -139,7 +139,7 @@ STREAM_ERROR_REASON_UNSPECIFIED: StreamErrorReason.ValueType # 0 STREAM_ERROR_REASON_NOT_FOUND: StreamErrorReason.ValueType # 1 """Nobody created the Stream. NOT_FOUND.""" STREAM_ERROR_REASON_EXPIRED: StreamErrorReason.ValueType # 2 -"""The Stream's Retention or its last-write TTL passed. NOT_FOUND.""" +"""The Stream's Retention or its Producer idle TTL passed. NOT_FOUND.""" STREAM_ERROR_REASON_CLOSED: StreamErrorReason.ValueType # 3 """The Stream is closed. FAILED_PRECONDITION.""" STREAM_ERROR_REASON_FENCED: StreamErrorReason.ValueType # 4 diff --git a/temporalio/bridge/sdk-core b/temporalio/bridge/sdk-core index 2c5da4644b..9b121cf32d 160000 --- a/temporalio/bridge/sdk-core +++ b/temporalio/bridge/sdk-core @@ -1 +1 @@ -Subproject commit 2c5da4644be319357ba30e89b6a380d98bc6a97b +Subproject commit 9b121cf32dcd7920a067a04fb61bbb000178290b From abaf7a02b653476a03ebe69320e73ac9b758511b Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Tue, 17 Nov 2026 15:00:57 -0700 Subject: [PATCH 17/35] Removed the Stream calls of Workflow code and their Local Activities. The first release step gives Streams to Activity and client code only. A Stream call of Workflow code fails its Workflow Task with one message, and the Store gets no call. --- temporalio/client/__init__.py | 2 - temporalio/client/_stream_store.py | 219 +-- temporalio/contrib/redis/streams.py | 3 +- temporalio/contrib/streams/README.md | 10 +- temporalio/contrib/streams/__init__.py | 8 +- temporalio/contrib/streams/_appender.py | 55 +- temporalio/contrib/streams/_producer.py | 15 +- temporalio/exceptions.py | 32 +- temporalio/worker/_replayer.py | 13 - temporalio/worker/_stream_store.py | 157 -- temporalio/worker/_worker.py | 27 +- temporalio/worker/_workflow.py | 4 - temporalio/worker/_workflow_instance.py | 69 +- tests/contrib/redis/test_streams.py | 56 +- .../contrib/streams/test_appender_workflow.py | 198 --- tests/contrib/streams/test_producer.py | 44 +- tests/nexus/test_stream_store.py | 32 +- tests/nexus/test_stream_store_workflow.py | 1277 +---------------- 18 files changed, 164 insertions(+), 2057 deletions(-) delete mode 100644 temporalio/worker/_stream_store.py delete mode 100644 tests/contrib/streams/test_appender_workflow.py diff --git a/temporalio/client/__init__.py b/temporalio/client/__init__.py index 6fa6dd710c..75e32e5e2c 100644 --- a/temporalio/client/__init__.py +++ b/temporalio/client/__init__.py @@ -189,7 +189,6 @@ StreamConfig, StreamSelectContext, StreamStore, - StreamStoreCallOptions, ) from ._worker_versioning import ( BuildIdOp, @@ -359,7 +358,6 @@ "StreamConfig", "StreamSelectContext", "StreamStore", - "StreamStoreCallOptions", "Callback", "_ClientImpl", "_apply_headers", diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index c2f1364c99..c0df1872db 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -2,7 +2,7 @@ import dataclasses from abc import ABC, abstractmethod -from collections.abc import Awaitable, Callable, Sequence +from collections.abc import Callable, Sequence from datetime import timedelta from typing import TYPE_CHECKING, Any @@ -13,54 +13,16 @@ import temporalio.api.streamservice.v1 import temporalio.bridge._visitor import temporalio.bridge.runtime -import temporalio.common import temporalio.converter import temporalio.nexus.system.stream_service import temporalio.runtime -import temporalio.workflow -from temporalio.converter import StorageDriverActivityInfo, StorageDriverWorkflowInfo +from temporalio.converter import StorageDriverActivityInfo from temporalio.exceptions import StreamConfigError if TYPE_CHECKING: import temporalio.bridge.streams -@dataclasses.dataclass(frozen=True) -class StreamStoreCallOptions: - """Options of the Local Activity that calls a Stream Store for Workflow code. - - Workflow code can't do I/O, so the default - :py:meth:`StreamStore.call_from_workflow` calls its Store from a Local Activity. - - A :py:class:`StreamConfig` holds them in - :py:attr:`StreamConfig.workflow_call_options`, and every Store of that config - gets the same options. - - .. warning:: - This API is experimental and subject to change. - """ - - start_to_close_timeout: timedelta | None = timedelta(seconds=60) - """Most time for one attempt.""" - - schedule_to_close_timeout: timedelta | None = None - """Most time for all attempts.""" - - retry_policy: temporalio.common.RetryPolicy = dataclasses.field( - default_factory=lambda: temporalio.common.RetryPolicy(maximum_attempts=1) - ) - """Retries of a failed call. The default is one attempt. In one Workflow Task, each - attempt of an append with the Workflow's Producer sends the same epoch and - sequence, so the Store writes the batch once.""" - - def __post_init__(self) -> None: - """Check that a timeout is set.""" - if not self.start_to_close_timeout and not self.schedule_to_close_timeout: - raise ValueError( - "start_to_close_timeout or schedule_to_close_timeout must be set" - ) - - class StreamStore(ABC): """A Store that answers the Stream operations of a client. @@ -131,54 +93,6 @@ async def describe_stream( """Give the status and the newest offset of a Stream.""" ... - def worker_activities(self) -> Sequence[Callable[..., Any]]: - """The Activities that a Worker registers for this Store when it runs Workflows. - - The default is one Activity, named for this Store, that - :py:meth:`call_from_workflow` runs as a Local Activity. A Store that overrides - one of the two methods overrides the other to match. - """ - return [_store_call_activity(self)] - - async def call_from_workflow( - self, - operation: str, - request: google.protobuf.message.Message, - options: StreamStoreCallOptions, - ) -> google.protobuf.message.Message: - """Answer one Stream operation for Workflow code. - - It runs in Workflow code, so it does no I/O itself. The default runs the - Activity of :py:meth:`worker_activities` as a Local Activity for each operation. - It takes the timeouts and the retry policy of ``options``. A replay takes the - result from History and calls no method of this Store. - - A Local Activity reaches the Store before its Workflow Task completes. If that - task fails after a close, the Stream stays closed. The retry of the task then - can't append to it. - - Args: - operation: The Nexus operation name, ``CreateStream``, ``AppendRecords``, - ``CloseStream`` or ``DescribeStream`` in this release. - request: The request message of ``temporalio.api.streamservice.v1``. - options: The :py:attr:`StreamConfig.workflow_call_options` of the config. - - Returns: - The response message of the operation. - """ - stream_operation = _STREAM_OPERATIONS[operation] - # Workflow code runs no payload codec, so the Activity encodes the bodies of an - # append before the Store gets them. - response = await temporalio.workflow.execute_local_activity( - _store_call_activity_name(self.name), - args=[operation, request.SerializeToString()], - result_type=bytes, - schedule_to_close_timeout=options.schedule_to_close_timeout, - start_to_close_timeout=options.start_to_close_timeout, - retry_policy=options.retry_policy, - ) - return stream_operation.response_type.FromString(response) - class _BridgeStreamStore(StreamStore): # pyright: ignore[reportUnusedClass] """A Store that runs on the Stream runtime of the bridge. @@ -268,9 +182,6 @@ class _StreamOperation: response_type: type[google.protobuf.message.Message] - workflow_refusal: str | None = None - """Why Workflow code can't start it in this release, or ``None`` when it can.""" - async def call( self, store: StreamStore, request: google.protobuf.message.Message ) -> google.protobuf.message.Message: @@ -308,10 +219,6 @@ def check_response( "read_records", temporalio.api.streamservice.v1.ReadRecordsRequest, temporalio.api.streamservice.v1.ReadRecordsResponse, - workflow_refusal=( - "Workflow code can't read a Stream on a Store in this release. " - "Call an Activity that reads it through its client." - ), ), "CloseStream": _StreamOperation( "close_stream", @@ -326,69 +233,6 @@ def check_response( } """The operations of the Stream service, by their Nexus operation name.""" -_STORE_CALL_ACTIVITY = "__temporal_stream_store_call" - - -def _store_call_activity_name(store_name: str) -> str: - # The name carries the Store's name, so two Stores on one Worker don't clash. - return f"{_STORE_CALL_ACTIVITY}:{store_name}" - - -def _store_call_activity( - store: StreamStore, -) -> Callable[[str, bytes], Awaitable[bytes]]: - @temporalio.activity.defn(name=_store_call_activity_name(store.name)) - async def call(operation: str, request: bytes) -> bytes: - message = _STREAM_OPERATIONS[operation].request_type.FromString(request) - if isinstance( - message, - ( - temporalio.api.streamservice.v1.AppendRecordsRequest, - temporalio.api.streamservice.v1.CloseStreamRequest, - ), - ): - _set_workflow_task_epoch_and_sequence(message) - response = await _call_stream_store( - store, temporalio.activity.client().data_converter, operation, message - ) - return response.SerializeToString() - - return call - - -def _set_workflow_task_epoch_and_sequence( - request: temporalio.api.streamservice.v1.AppendRecordsRequest - | temporalio.api.streamservice.v1.CloseStreamRequest, -) -> None: - # Every append and the close of a Workflow run write as one Producer, the Workflow. Its - # epoch is the Workflow Task that dispatched this Local Activity, so the Store fences a - # late write of a failed task, whichever call made it. A Producer that Workflow code - # gave of another kind reaches the Store as it is. - info = temporalio.activity.info() - if request.producer.WhichOneof("kind") != "workflow" or not info.is_local: - return - task = info.workflow_task - if task is None: - raise RuntimeError( - "A Stream write of Workflow code needs the Workflow Task of its Local " - "Activity, and the Worker's Core gave none" - ) - epoch = request.producer.workflow.epoch - epoch.Clear() - epoch.started_event_id = task.started_event_id - epoch.attempt = task.attempt - if task.started_time is not None: - epoch.started_time.FromDatetime(task.started_time) - if ( - isinstance(request, temporalio.api.streamservice.v1.AppendRecordsRequest) - and not request.sequence - ): - # The activity id of a Local Activity is its sequence number in the run. The - # Workflow's calls on one Stream run one at a time, so these numbers go up in the - # order of the appends. Each retry of this Local Activity sends the same number, - # so the Store writes the batch once and gives back its first offsets. - request.sequence = int(info.activity_id) - @dataclasses.dataclass(frozen=True) class StreamSelectContext: @@ -401,12 +245,8 @@ class StreamSelectContext: id: str """The id of the Stream.""" - target: StorageDriverActivityInfo | StorageDriverWorkflowInfo | None = None - """The Workflow or Activity that makes the call, or ``None`` for a plain client. - - A call in an Activity has that Activity here. A Workflow's call runs the selector - in Workflow code, and has that Workflow here. - """ + target: StorageDriverActivityInfo | None = None + """The Activity that makes the call, or ``None`` for a plain client.""" @dataclasses.dataclass(frozen=True) @@ -419,28 +259,16 @@ class StreamConfig: operation. It holds Stores and a selector, as :py:class:`temporalio.converter.ExternalStorage` holds drivers and a selector. - On a Worker whose client has no config, a Stream call of Workflow code raises - :py:class:`temporalio.exceptions.ApplicationError` with the ``type`` - ``StreamConfigError``, and an uncaught one fails the Workflow. Workflow code can - catch this error. A Workflow that catches it must not go on to make the same call - on a Worker that has a config, since the History then has no record of the first - call. - A ref carries the name of its Store, so a call on a known Stream goes to the Store of that name. A create can name its Store with ``store=``. A call whose ref names no Store goes to the Store that :py:attr:`store_selector` returns, or to the one Store when there is no selector. With more than one Store and no selector, such a call raises :py:class:`temporalio.exceptions.StreamConfigError`. - An append or a close of Workflow code writes as the Workflow, with the Workflow Task - that runs it as its epoch. So the Store fences a late write of a failed Workflow - Task. The appends and the close of one Workflow on one Stream run one at a time, in - the order of the calls. - - Workflow code calls a Store from a Local Activity, also for a close. So it needs - no Worker that polls for Activity Tasks. A Local Activity reaches the Store before - its Workflow Task completes. If that task fails after a close, the Stream stays - closed. The retry of the task then can't append to it. + An Activity reaches the Stores of its Worker's client through + :py:func:`temporalio.activity.client`. Workflow code can't call a Stream in this + version. A Stream call of Workflow code raises ``RuntimeError``, which fails the + Workflow Task. .. warning:: This API is experimental and subject to change. @@ -451,8 +279,7 @@ class StreamConfig: A ref carries the :py:attr:`StreamStore.name` of its Store, and a call looks the Store up by that name. With more than one Store and no - :py:attr:`store_selector`, each call must name its Store. A Worker serves every - Store of its client. + :py:attr:`store_selector`, each call must name its Store. """ store_selector: Callable[[StreamSelectContext], StreamStore] | None = None @@ -463,21 +290,8 @@ class StreamConfig: such a call goes to that Store. When it is ``None`` and there is more than one Store, such a call raises :py:class:`temporalio.exceptions.StreamConfigError`. - For a call of Workflow code, the selector runs in Workflow code, also on a - replay. So it must give the same Store for the same context. The Local Activity - of the chosen Store is in History, and another answer on a replay is a - non-determinism error. A Stream that code builds from an id only goes through the - selector on each call, so the selector must also give the same Store for the same - id. - """ - - workflow_call_options: StreamStoreCallOptions = dataclasses.field( - default_factory=StreamStoreCallOptions - ) - """Options of a Store's call for Workflow code, for every Store of this config. - - :py:meth:`StreamStore.call_from_workflow` gets them. The default runs the Store's - Local Activity with their timeouts and retry policy. + A Stream that code builds from an id only goes through the selector on each + call, so the selector must give the same Store for the same id. """ _store_map: dict[str, StreamStore] = dataclasses.field( @@ -535,7 +349,7 @@ def _get_store_by_name(self, name: str) -> StreamStore: def _store_for( self, ref: temporalio.nexus.system.stream_service.StreamRef[Any], - target: StorageDriverActivityInfo | StorageDriverWorkflowInfo | None, + target: StorageDriverActivityInfo | None, ) -> StreamStore: if ref.store: return self._get_store_by_name(ref.store) @@ -597,6 +411,15 @@ def _stream_service() -> nexusrpc.ServiceDefinition: _STREAM_SERVICE = _stream_service() +def _workflow_stream_call_error() -> RuntimeError: # pyright: ignore[reportUnusedFunction] + # Not an `ApplicationError`, so it fails the Workflow Task and not the Workflow. A + # Worker with code that has no Stream call can then finish the Workflow. + return RuntimeError( + "Workflow code can't call a Stream in this version. Call it from an Activity " + "through activity.client()." + ) + + async def _call_stream_store( # pyright: ignore[reportUnusedFunction] store: StreamStore, data_converter: temporalio.converter.DataConverter, diff --git a/temporalio/contrib/redis/streams.py b/temporalio/contrib/redis/streams.py index 8a366fe5ff..705d1cddb0 100644 --- a/temporalio/contrib/redis/streams.py +++ b/temporalio/contrib/redis/streams.py @@ -1,6 +1,7 @@ """The Redis Stream Store. -A client takes it in ``stream_config``, and its Workers use the same Store:: +A client takes it in ``stream_config``. The Activities of a Worker on that client reach +the same Store through ``activity.client()``:: from temporalio.client import Client, StreamConfig from temporalio.contrib.redis.streams import RedisStreamStore diff --git a/temporalio/contrib/streams/README.md b/temporalio/contrib/streams/README.md index 2e013f6666..4c5a8e46a4 100644 --- a/temporalio/contrib/streams/README.md +++ b/temporalio/contrib/streams/README.md @@ -1,16 +1,14 @@ # Streams helpers -> This package is experimental. Its API can change in a later release. +> This package is experimental, and its API can change. -`temporalio.contrib.streams` holds helpers that sit on top of the Streams API of the SDK. A -helper takes a client `Stream` or a Workflow `Stream`. +`temporalio.contrib.streams` holds helpers that sit on top of the Streams API of the SDK. They +run in a client or an Activity, and take a client `Stream`. ## `BufferedAppender` `BufferedAppender` batches the appends of one Stream. `append` returns immediately, and the -appender writes the buffered Records as one batch. In a client or an Activity a batch waits -at most `max_delay`. In Workflow code a batch starts when the code next yields, so it adds no -timer to History. +appender writes the buffered Records as one batch. A batch waits at most `max_delay`. ```python from temporalio.contrib.streams import BufferedAppender diff --git a/temporalio/contrib/streams/__init__.py b/temporalio/contrib/streams/__init__.py index ac9a7c6392..95318f94d1 100644 --- a/temporalio/contrib/streams/__init__.py +++ b/temporalio/contrib/streams/__init__.py @@ -1,9 +1,9 @@ """Helpers that sit on top of the Streams API. -:py:class:`BufferedAppender` batches the appends of one Stream. It takes a client Stream or a -Workflow Stream. :py:func:`current_producer` gives the Producer that an append of the -calling code sends by default, and :py:func:`as_producer` gives back a Producer that code -stored. +:py:class:`BufferedAppender` batches the appends of one client Stream. +:py:func:`current_producer` gives the Producer that an append of the calling code sends by +default, and :py:func:`as_producer` gives back a Producer that code stored. They run in a +client or an Activity. .. warning:: This package is experimental and may change in future versions. diff --git a/temporalio/contrib/streams/_appender.py b/temporalio/contrib/streams/_appender.py index 7b194ddf09..475c4ee551 100644 --- a/temporalio/contrib/streams/_appender.py +++ b/temporalio/contrib/streams/_appender.py @@ -9,17 +9,11 @@ import asyncio from datetime import timedelta from types import TracebackType -from typing import TYPE_CHECKING, Generic, TypeVar +from typing import Generic, TypeVar from typing_extensions import Self import temporalio.nexus.system.stream_service.client -import temporalio.workflow - -if TYPE_CHECKING: - # The Workflow Stream class is in a private module of the generated code. Only its type - # is needed, since the appender calls the same `append` on both Streams. - import temporalio.nexus.system.stream_service.workflow._resources.stream BodyT = TypeVar("BodyT") @@ -34,24 +28,16 @@ class BufferedAppender(Generic[BodyT]): """Batches the appends of one Stream. Use it in code that appends often, such as an Activity that streams model tokens. - :py:meth:`append` returns at once and does no I/O. One write is in flight at a time. + :py:meth:`append` returns immediately and does no I/O. One write is in flight at a time. Records appended during a write wait for the next batch, and they keep their order. A batch holds at most 1,000 Records. - It takes a client Stream or a Workflow Stream. When a write starts depends on where - :py:meth:`append` runs, not on the type of the Stream: - - * In a client or an Activity, the appender writes the buffered Records at most - ``max_delay`` after the first of them, or at once when they fill a batch. - * In Workflow code, a timer would go into History, so the appender uses none. The first - write starts when the calling code next yields, so the appends of one activation go as - one write. Each write is the Workflow ``append`` of the Stream, so it is one Local - Activity and one marker in History. A replay takes the results from History and writes - nothing. + It takes a client Stream, in a client or an Activity. It writes the buffered Records at + most ``max_delay`` after the first of them, or immediately when they fill a batch. Use it in ``async with``, which writes the remaining Records at the end of the block, or await :py:meth:`flush`. Without ``async with`` or a :py:meth:`flush`, the Records still in - the buffer are lost. The end of an Activity attempt or of a Workflow doesn't write them. + the buffer are lost. The end of an Activity attempt doesn't write them. ``await flush()`` is the backpressure point. A caller that appends faster than the Store accepts Records must await it from time to time, so the buffer doesn't grow without bound. @@ -68,33 +54,27 @@ class BufferedAppender(Generic[BodyT]): def __init__( self, - stream: temporalio.nexus.system.stream_service.client.Stream[BodyT] - | temporalio.nexus.system.stream_service.workflow._resources.stream.Stream[ - BodyT - ], + stream: temporalio.nexus.system.stream_service.client.Stream[BodyT], *, max_delay: timedelta | None = None, ) -> None: """Create an appender. It starts no write until the first append. Args: - stream: The Stream to append to, a client Stream or a Workflow Stream. + stream: The client Stream to append to. max_delay: The longest time that a Record waits in the buffer before its batch - starts to write, in a client or an Activity. It must not be negative. - ``None`` means 100 milliseconds. Workflow code must leave it ``None``. + starts to write. It must not be negative. ``None`` means 100 milliseconds. Raises: - ValueError: ``max_delay`` is negative, or it is set in Workflow code. + ValueError: ``max_delay`` is negative. """ if max_delay is not None and max_delay < timedelta(): raise ValueError("max_delay must not be negative") self._stream = stream self._max_delay = max_delay - self._check_delay() self._buffer: list[BodyT] = [] # When the oldest buffered Record arrived, so its batch starts at most max_delay later. self._first_at = 0.0 - # The timer of a client or an Activity, or the start at the next yield of Workflow code. self._timer: asyncio.Handle | None = None self._writing: asyncio.Task[None] | None = None self._appended = 0 @@ -105,7 +85,7 @@ def __init__( self._unwritten = 0 def append(self, *records: BodyT) -> None: - """Add one Record for each value to the buffer, and return at once. + """Add one Record for each value to the buffer, and return immediately. A list is one value. @@ -117,7 +97,6 @@ def append(self, *records: BodyT) -> None: self._raise_error() if not records: raise TypeError("append takes one or more values") - self._check_delay() loop = asyncio.get_running_loop() if not self._buffer: self._first_at = loop.time() @@ -171,22 +150,10 @@ def _raise_error(self) -> None: if self._error is not None: raise self._error - def _check_delay(self) -> None: - if self._max_delay is not None and temporalio.workflow.in_workflow(): - raise ValueError( - "BufferedAppender takes no max_delay in Workflow code, since a delay " - "would put a timer into History. Leave max_delay as None." - ) - def _schedule(self) -> None: if self._writing is not None or not self._buffer: return - if temporalio.workflow.in_workflow(): - # A timer of the Workflow event loop is durable. A start at the next yield - # takes every Record of this activation into one write, and adds no event. - if self._timer is None: - self._timer = asyncio.get_running_loop().call_soon(self._start) - elif len(self._buffer) >= _MAX_BATCH_RECORDS: + if len(self._buffer) >= _MAX_BATCH_RECORDS: self._start() elif self._timer is None: max_delay = self._max_delay or _DEFAULT_MAX_DELAY diff --git a/temporalio/contrib/streams/_producer.py b/temporalio/contrib/streams/_producer.py index f94b0d1baf..51518d942c 100644 --- a/temporalio/contrib/streams/_producer.py +++ b/temporalio/contrib/streams/_producer.py @@ -3,6 +3,7 @@ from __future__ import annotations import temporalio.api.stream.v1 +import temporalio.client._stream_store import temporalio.converter._payload_converter import temporalio.nexus.system._support import temporalio.workflow @@ -13,23 +14,23 @@ def current_producer() -> Producer: """Give the Producer that an append or a close of the calling code sends by default. An ``append`` or a ``close`` with no ``producer=`` sends this Producer. In an - Activity or a Local Activity it is the Producer of the current attempt. In Workflow - code it is the Workflow's Producer. In any other code it is the client Producer of - this process. It does no I/O. - - On a Store, an append or a close of Workflow code writes as this Producer, with the - Workflow Task that runs it as its epoch. + Activity or a Local Activity it is the Producer of the current attempt. In any other + code it is the client Producer of this process. It does no I/O. An Activity can keep the result and pass it to a later ``append`` or ``close`` with ``producer=``. That call then writes as this Producer with its epoch, also in a later attempt. Heartbeat details give it back as its proto message, so pass it through :py:func:`as_producer` first. + Raises: + RuntimeError: It runs in Workflow code, which can't call a Stream in this + version. + .. warning:: This API is experimental and subject to change. """ if temporalio.workflow.in_workflow(): - return temporalio.nexus.system._support.workflow_producer() + raise temporalio.client._stream_store._workflow_stream_call_error() return temporalio.nexus.system._support.default_producer() diff --git a/temporalio/exceptions.py b/temporalio/exceptions.py index 658012ee4f..c64e7a8129 100644 --- a/temporalio/exceptions.py +++ b/temporalio/exceptions.py @@ -212,8 +212,8 @@ def category(self) -> ApplicationErrorCategory: class StreamError(ApplicationError): """A failed Stream call. ``reason`` tells the cases apart. - One class covers every failed call in client, Activity and Workflow code, so match - on ``reason`` and not on the type. The reason is a ``StreamErrorReason``, such as + One class covers every failed call in client and Activity code, so match on + ``reason`` and not on the type. The reason is a ``StreamErrorReason``, such as ``STREAM_ERROR_REASON_NOT_FOUND`` (nobody created the Stream), ``STREAM_ERROR_REASON_CLOSED`` (the Stream is closed), or ``STREAM_ERROR_REASON_ALREADY_EXISTS`` (a strict create of a Stream that exists). @@ -266,23 +266,6 @@ def _stream_error_from_call( # pyright: ignore[reportUnusedFunction] return _stream_error_from_reason(proto.reason, message) -def _stream_error_from_failure( # pyright: ignore[reportUnusedFunction] - err: BaseException, -) -> StreamError | None: - # A Local Activity or a Nexus operation hands back a plain application error, so the - # error is rebuilt from its type and details. - if isinstance(err, StreamError): - return err - if not isinstance(err, ApplicationError) or err.type != "StreamError": - return None - reason: int = _STREAM_ERROR_REASON_UNSPECIFIED - for detail in err.details: - if isinstance(detail, temporalio.api.stream.v1.StreamErrorDetails): - reason = detail.reason - break - return _stream_error_from_reason(reason, err.message) - - class StreamConfigError(RuntimeError): """A Stream call can't find its Store, because of how the Stores are set up. @@ -291,25 +274,14 @@ class StreamConfigError(RuntimeError): * A Stream call is made on a client with no Stream config. This release has no Streams on the server, so every Stream call needs a Store. - * A Workflow's Stream call has a ref that names no Store, on a Worker whose - client has more than one Store and no selector. This fails the Workflow Task. * A :py:class:`temporalio.client.StreamConfig` has no Store, or has two Stores with one name. * A call names no Store on a config with more than one Store and no selector. * A ref names a Store that the config of the client doesn't have. * The selector returns a Store that isn't one of the config's Stores. * A :py:class:`temporalio.client.StreamStore` is given in ``plugins``. - * A Workflow's Stream call has a ref that names a Store that the config of the - Worker's client doesn't have. This fails the Workflow Task. * This build of temporalio has no Stream runtime, or no Redis Stream Store. - A Workflow's Stream call on a Worker whose client has no Stream config doesn't - raise this class. Workflow code gets an :py:class:`ApplicationError` with the - ``type`` ``StreamConfigError`` and ``non_retryable`` set, so an uncaught one fails - the Workflow. Workflow code can catch this error. A Workflow that catches it must - not go on to make the same call on a Worker that has a config, since the History - then has no record of the first call. - .. warning:: This API is experimental and subject to change. """ diff --git a/temporalio/worker/_replayer.py b/temporalio/worker/_replayer.py index 7ad6bdeaaf..62a27b29cf 100644 --- a/temporalio/worker/_replayer.py +++ b/temporalio/worker/_replayer.py @@ -22,7 +22,6 @@ from ..common import HeaderCodecBehavior from ._interceptor import Interceptor -from ._stream_store import _WorkflowStreamStore from ._worker import load_default_build_id from ._workflow import _WorkflowWorker from ._workflow_instance import ( @@ -57,7 +56,6 @@ def __init__( runtime: temporalio.runtime.Runtime | None = None, disable_safe_workflow_eviction: bool = False, header_codec_behavior: HeaderCodecBehavior = HeaderCodecBehavior.NO_CODEC, - stream_config: temporalio.client.StreamConfig | None = None, ) -> None: """Create a replayer to replay workflows from history. @@ -66,12 +64,6 @@ def __init__( the replayer that were passed to the worker when the workflow originally ran. - ``stream_config`` takes the Stream config of the client that the - worker had, so a replay makes the same commands. A history that records a - Stream call needs it. With no Stream config, Workflow code gets an error in - place of that call, and the replay fails with a nondeterminism error. A - replay calls no method of its Stores. - Note, unlike the worker, for the replayer the workflow_task_executor will default to a new thread pool executor with no max_workers set that will be shared across all replay calls and never explicitly shut down. @@ -94,7 +86,6 @@ def __init__( runtime=runtime, disable_safe_workflow_eviction=disable_safe_workflow_eviction, header_codec_behavior=header_codec_behavior, - stream_config=stream_config, ) self._initial_config = self._config.copy() self._default_workflow_logic_flags = set(_DEFAULT_ENABLED_WORKFLOW_LOGIC_FLAGS) @@ -296,9 +287,6 @@ def on_eviction_hook( default_workflow_logic_flags=frozenset( self._default_workflow_logic_flags ), - stream_store=_WorkflowStreamStore.from_config( - self._config.get("stream_config") - ), ) external_storage = data_converter.external_storage storage_driver_types = ( @@ -447,7 +435,6 @@ class ReplayerConfig(TypedDict, total=False): runtime: temporalio.runtime.Runtime | None disable_safe_workflow_eviction: bool header_codec_behavior: HeaderCodecBehavior - stream_config: temporalio.client.StreamConfig | None @dataclass(frozen=True) diff --git a/temporalio/worker/_stream_store.py b/temporalio/worker/_stream_store.py deleted file mode 100644 index daaac73e6b..0000000000 --- a/temporalio/worker/_stream_store.py +++ /dev/null @@ -1,157 +0,0 @@ -"""How Workflow code reaches the Stream Stores of its Worker's client.""" - -from __future__ import annotations - -import asyncio -import dataclasses -from collections.abc import Generator -from typing import TYPE_CHECKING, Any - -from nexusrpc import OutputT - -import temporalio.exceptions -import temporalio.workflow -from temporalio.client._stream_store import ( - _STREAM_OPERATIONS, - StreamConfig, - _request_ref, -) -from temporalio.converter import StorageDriverWorkflowInfo - -from ._interceptor import StartNexusOperationInput - -if TYPE_CHECKING: - from ._workflow_instance import _WorkflowInstanceImpl - - -def _no_stream_config_error() -> temporalio.exceptions.ApplicationError: # pyright: ignore[reportUnusedFunction] - # A `StreamConfigError` would fail the Workflow Task, and its retries would fail the - # same way for good. A Worker with no config is a setup that a user has to see, so - # the Workflow fails instead. - return temporalio.exceptions.ApplicationError( - "The client of this Worker has no Stream Store. Give one in " - "stream_config=StreamConfig(stores=[...]).", - type="StreamConfigError", - non_retryable=True, - ) - - -# The writes of a Workflow to one Stream, which run one at a time in the order of the -# calls. A create and a describe need no order. -_ORDERED_OPERATIONS = frozenset({"AppendRecords", "CloseStream"}) - - -def _workflow_target() -> StorageDriverWorkflowInfo: - # External storage builds the same value for a Workflow. - info = temporalio.workflow.info() - return StorageDriverWorkflowInfo( - id=info.workflow_id, - type=info.workflow_type, - run_id=info.run_id, - namespace=info.namespace, - ) - - -@dataclasses.dataclass(frozen=True) -class _WorkflowStreamStore: - """The Worker's Stream Stores as Workflow code reaches them.""" - - config: StreamConfig - """The Stream config of the Worker's client.""" - - @classmethod - def from_config(cls, config: StreamConfig | None) -> _WorkflowStreamStore | None: - """Build it from the Stream config of a client, or give ``None`` when it has none.""" - return cls(config=config) if config is not None else None - - def start_operation( - self, - instance: _WorkflowInstanceImpl, - input: StartNexusOperationInput[Any, OutputT], - ) -> temporalio.workflow.NexusOperationHandle[OutputT]: - """Answer a Stream call of Workflow code with the Store that the call goes to.""" - stream_operation = _STREAM_OPERATIONS[input.operation_name] - # Workflow code can start any operation of the generated service through a Nexus - # client, also one that the Workflow `Stream` leaves out. - if stream_operation.workflow_refusal is not None: - raise temporalio.exceptions.ApplicationError( - stream_operation.workflow_refusal, non_retryable=True - ) - payload_converter, _ = instance._system_nexus_converters(input) - request = payload_converter._to_transfer_value(input.input) - ref = _request_ref(input.input) - # The choice runs in Workflow code, so a ref of a Store that the config doesn't - # have raises `StreamConfigError` here, with no I/O, and fails the Workflow Task. - store = self.config._store_for(ref, _workflow_target()) - options = self.config.workflow_call_options - tails = instance._stream_write_tails - tail_key = (store.name, ref.id) - previous = ( - tails.get(tail_key) if input.operation_name in _ORDERED_OPERATIONS else None - ) - - async def result() -> OutputT: - if previous is not None and not previous.done(): - # The Workflow's appends carry sequences that go up in the order of the - # calls, and the Store refuses one below the last. So a write starts its - # Local Activity only after the one before it ended, also when that one - # failed. - await temporalio.workflow.wait([previous]) - try: - response = await store.call_from_workflow( - input.operation_name, request, options - ) - except temporalio.exceptions.ApplicationError as err: - # Core hands back a failed Local Activity's error without a wrapper. - stream_error = temporalio.exceptions._stream_error_from_failure(err) - if stream_error is not None: - raise stream_error from None - raise - except temporalio.exceptions.ActivityError as err: - # Workflow code gets the Store's error, not the Local Activity's wrapper. - if err.cause is not None: - stream_error = temporalio.exceptions._stream_error_from_failure( - err.cause - ) - if stream_error is not None: - raise stream_error from None - if isinstance( - err.cause, - ( - temporalio.exceptions.ApplicationError, - temporalio.exceptions.TimeoutError, - ), - ): - raise err.cause - raise - return payload_converter._from_transfer_value( - stream_operation.check_response(store, response), input.output_type - ) - - task = instance.create_task( - result(), name=f"stream store: {input.operation_name}" - ) - if input.operation_name in _ORDERED_OPERATIONS: - tails[tail_key] = task - - def forget(done: asyncio.Task[Any]) -> None: - if tails.get(tail_key) is done: - del tails[tail_key] - - task.add_done_callback(forget) - return _StreamStoreOperationHandle(task) - - -class _StreamStoreOperationHandle(temporalio.workflow.NexusOperationHandle[OutputT]): - def __init__(self, task: asyncio.Task[OutputT]) -> None: - self._task = task - - def cancel(self) -> bool: - return self._task.cancel() - - def __await__(self) -> Generator[Any, Any, OutputT]: - return self._task.__await__() - - @property - def operation_token(self) -> str | None: - return None diff --git a/temporalio/worker/_worker.py b/temporalio/worker/_worker.py index bbf68e2bf4..e06bb99a4b 100644 --- a/temporalio/worker/_worker.py +++ b/temporalio/worker/_worker.py @@ -34,7 +34,6 @@ from ._interceptor import Interceptor from ._nexus import _NexusWorker from ._plugin import Plugin -from ._stream_store import _WorkflowStreamStore from ._tuning import WorkerTuner from ._workflow import ( _DEFAULT_WORKFLOW_TASK_EXTERNAL_STORAGE_CONCURRENCY, @@ -499,11 +498,7 @@ def _init_from_config(self, client: temporalio.client.Client, config: WorkerConf or bridge_client.config.runtime or temporalio.runtime.Runtime.default() ) - activities = list(config.get("activities") or []) - stream_config = client_config.get("stream_config") - if stream_config is not None and config.get("workflows"): - for store in stream_config.stores: - activities.extend(store.worker_activities()) + activities = config.get("activities") if activities: _warn_if_activity_executor_max_workers_is_inconsistent(config) self._activity_worker = _ActivityWorker( @@ -583,7 +578,6 @@ def check_activity(activity: str): encode_headers=client_config["header_codec_behavior"] != HeaderCodecBehavior.NO_CODEC, max_workflow_task_external_storage_concurrency=max_workflow_task_external_storage_concurrency, - stream_store=_WorkflowStreamStore.from_config(stream_config), ) tuner = config.get("tuner") @@ -673,9 +667,7 @@ def check_activity(activity: str): enable_workflows=self._workflow_worker is not None, enable_local_activities=self._activity_worker is not None and self._workflow_worker is not None, - # The Activities of the Stream Stores run as Local Activities, so - # alone they don't make this worker poll for Activity Tasks. - enable_remote_activities=bool(config.get("activities")) + enable_remote_activities=self._activity_worker is not None and not config.get("no_remote_activities"), enable_nexus=self._nexus_worker is not None, ), @@ -759,19 +751,13 @@ def client(self, value: temporalio.client.Client) -> None: Changing the client will make sure the worker starts using it for the next calls it makes. However, outstanding client calls will still complete with the existing client. The new client cannot be "lazy" and - must be using the same runtime and Stream Stores as the current client. + must be using the same runtime as the current client. """ bridge_client = _extract_bridge_client_for_worker(value) if self._runtime is not bridge_client._runtime: raise ValueError( "New client is not on the same runtime as the existing client" ) - # Running Workflows route their Stream calls by the Store that the worker - # started with, so a new client can't change it. - if _stream_config_stores(value) != _stream_config_stores(self.client): - raise ValueError( - "New client doesn't have the Stream Stores of the existing client" - ) assert bridge_client._bridge_client self._bridge_worker.replace_client(bridge_client._bridge_client) self._config["client"] = value @@ -1027,13 +1013,6 @@ async def __aexit__(self, exc_type: type[BaseException] | None, *args: Any) -> N raise self._async_context_run_exception -def _stream_config_stores( - client: temporalio.client.Client, -) -> list[temporalio.client.StreamStore]: - config = client.config(active_config=True).get("stream_config") - return list(config.stores) if config is not None else [] - - class WorkerConfig(TypedDict, total=False): """TypedDict of config originally passed to :py:class:`Worker`.""" diff --git a/temporalio/worker/_workflow.py b/temporalio/worker/_workflow.py index 3e20e6adbb..d8380a6b90 100644 --- a/temporalio/worker/_workflow.py +++ b/temporalio/worker/_workflow.py @@ -40,7 +40,6 @@ WorkflowInboundInterceptor, WorkflowInterceptorClassInput, ) -from ._stream_store import _WorkflowStreamStore from ._workflow_instance import ( _DEFAULT_ENABLED_WORKFLOW_LOGIC_FLAGS, PatchActivationInput, @@ -129,7 +128,6 @@ def __init__( encode_headers: bool, max_workflow_task_external_storage_concurrency: int, default_workflow_logic_flags: frozenset[_WorkflowLogicFlag] | None = None, - stream_store: _WorkflowStreamStore | None = None, ) -> None: # Debug mode is enabled if specified or if the TEMPORAL_DEBUG env var is truthy debug_mode = debug_mode or bool(os.environ.get("TEMPORAL_DEBUG")) @@ -199,7 +197,6 @@ def __init__( self._max_workflow_task_external_storage_concurrency = ( max_workflow_task_external_storage_concurrency ) - self._stream_store = stream_store self._throw_after_activation: Exception | None = None # If debug mode is enabled, disable deadlock detection @@ -822,7 +819,6 @@ def _create_workflow_instance( nexus_serialization_context=temporalio.converter._serialization_context._propagated_nexus_serialization_context( init ), - stream_store=self._stream_store, ) if defn.sandboxed: return self._workflow_runner.create_instance(det) diff --git a/temporalio/worker/_workflow_instance.py b/temporalio/worker/_workflow_instance.py index 454a7c2c04..bd01c67c3b 100644 --- a/temporalio/worker/_workflow_instance.py +++ b/temporalio/worker/_workflow_instance.py @@ -59,7 +59,10 @@ import temporalio.exceptions import temporalio.nexus.system import temporalio.workflow -from temporalio.client._stream_store import _STREAM_SERVICE +from temporalio.client._stream_store import ( + _STREAM_SERVICE, + _workflow_stream_call_error, +) from temporalio.converter import StorageDriverStoreContext, StorageDriverWorkflowInfo from temporalio.nexus.system.stream_service.workflow._system_nexus_interceptor import ( _start_system_nexus_operation as _start_stream_system_nexus_operation, @@ -95,7 +98,6 @@ WorkflowInboundInterceptor, WorkflowOutboundInterceptor, ) -from ._stream_store import _no_stream_config_error, _WorkflowStreamStore logger = logging.getLogger(__name__) @@ -193,10 +195,6 @@ class WorkflowInstanceDetails: temporalio.converter.NexusSerializationContext | None ) = None """Serialization context of the Nexus operation handler that started the workflow, if any.""" - stream_store: _WorkflowStreamStore | None = None - """The Worker's Stream Stores as Workflow code reaches them, or ``None`` when the - Worker's client has none. This release has no Streams on the server, so with ``None`` - a Stream call fails the Workflow.""" class WorkflowInstance(ABC): @@ -313,10 +311,6 @@ def __init__(self, det: WorkflowInstanceDetails) -> None: # the Nexus caller, which decodes them with the handler's Nexus context. A retried or cron run # reads the previous run's result and failure with the same context. self._nexus_serialization_context = det.nexus_serialization_context - self._stream_store = det.stream_store - # The last append or close of each Stream, by Store name and Stream id, so the - # next one of the same Stream starts after it. - self._stream_write_tails: dict[tuple[str, str], asyncio.Task[Any]] = {} if self._nexus_serialization_context: self._result_payload_converter = self._payload_converter_with_context( self._nexus_serialization_context @@ -2318,7 +2312,25 @@ async def operation_handle_fn() -> OutputT: ) if temporalio.nexus.system.is_system_endpoint(input.endpoint): - payload_converter, failure_converter = self._system_nexus_converters(input) + serialization_context = temporalio.nexus.system._get_serialization_context( + input.service, + input.operation_name, + input.input, + ) + user_payload_converter = self._context_free_payload_converter + user_failure_converter = self._context_free_failure_converter + if serialization_context is not None: + user_payload_converter = self._payload_converter_with_context( + serialization_context + ) + user_failure_converter = self._failure_converter_with_context( + serialization_context + ) + payload_converter = temporalio.nexus.system._get_payload_converter( + user_payload_converter, + user_failure_converter, + ) + failure_converter = user_failure_converter else: serialization_context = temporalio.converter.NexusSerializationContext( endpoint=input.endpoint, @@ -2357,41 +2369,12 @@ async def _intercept_system_nexus_operation( async def _schedule_system_nexus_operation( self, input: StartNexusOperationInput[Any, OutputT] ) -> temporalio.workflow.NexusOperationHandle[OutputT]: - # This release has no Streams on the server, so a Worker answers a Stream call with - # a Store of its client. Every other system Nexus operation goes to the server. + # The server has no Streams, and Workflow code reaches no Store. So a Stream call + # stops here, after the interceptors saw it. if input.service == _STREAM_SERVICE.name: - store = self._stream_store - if store is None: - raise _no_stream_config_error() - return store.start_operation(self, input) + raise _workflow_stream_call_error() return await self._outbound_start_nexus_operation(input) - def _system_nexus_converters( - self, input: StartNexusOperationInput[Any, Any] - ) -> tuple[ - temporalio.nexus.system._SystemNexusPayloadConverter, - temporalio.converter.FailureConverter, - ]: - serialization_context = temporalio.nexus.system._get_serialization_context( - input.service, - input.operation_name, - input.input, - ) - user_payload_converter = self._context_free_payload_converter - user_failure_converter = self._context_free_failure_converter - if serialization_context is not None: - user_payload_converter = self._payload_converter_with_context( - serialization_context - ) - user_failure_converter = self._failure_converter_with_context( - serialization_context - ) - payload_converter = temporalio.nexus.system._get_payload_converter( - user_payload_converter, - user_failure_converter, - ) - return payload_converter, user_failure_converter - #### Miscellaneous helpers #### # These are in alphabetical order. diff --git a/tests/contrib/redis/test_streams.py b/tests/contrib/redis/test_streams.py index b60a7e4dfb..0e504b0868 100644 --- a/tests/contrib/redis/test_streams.py +++ b/tests/contrib/redis/test_streams.py @@ -19,13 +19,11 @@ Producer, ProducerKindClient, ProducerKindLocalActivity, - ProducerKindWorkflow, ProducerKindWorkflowActivity, StreamRef, StreamStatus, ) from temporalio.nexus.system.stream_service.client import Stream -from temporalio.nexus.system.stream_service.workflow import create_stream from tests.helpers import new_worker _REDIS_URL = os.environ.get("STREAMS_REDIS_URL") @@ -65,24 +63,12 @@ async def append_values(ref: StreamRef[str], values: list[str]) -> None: @workflow.defn class AppendWorkflow: @workflow.run - async def run(self, stream_id: str) -> StreamRef[str]: - stream = await create_stream(id=stream_id, body_type=str) + async def run(self, ref: StreamRef[str]) -> None: await workflow.execute_local_activity( append_values, - args=[stream.ref, ["a", "b"]], + args=[ref, ["a", "b"]], start_to_close_timeout=timedelta(seconds=10), ) - return stream.ref - - -@workflow.defn -class AppendAndCloseWorkflow: - @workflow.run - async def run(self, stream_id: str) -> StreamRef[str]: - stream = await create_stream(id=stream_id, body_type=str) - await stream.append("a", "b") - await stream.close() - return stream.ref @activity.defn @@ -126,22 +112,20 @@ async def test_client_creates_appends_reads_and_closes(client: Client): assert page.records == [] -async def test_workflow_local_activity_appends_with_local_activity_producer( - client: Client, -): +async def test_local_activity_appends_with_local_activity_producer(client: Client): stream_client = _with_store(client, _store()) + stream = await _create_stream(stream_client) async with new_worker( stream_client, AppendWorkflow, activities=[append_values] ) as worker: handle = await stream_client.start_workflow( AppendWorkflow.run, - f"stream-{uuid.uuid4()}", + stream.ref, id=f"workflow-{uuid.uuid4()}", task_queue=worker.task_queue, ) - ref = await handle.result() + await handle.result() - stream = stream_client.get_stream(ref, body_type=str) records = [record async for record in stream.iter_read_items()] assert [record.body for record in records] == ["a", "b"] run_id = handle.result_run_id @@ -151,34 +135,6 @@ async def test_workflow_local_activity_appends_with_local_activity_producer( assert record.producer.kind.value.run_id == run_id -async def test_workflow_appends_and_closes(client: Client): - stream_client = _with_store(client, _store()) - async with new_worker(stream_client, AppendAndCloseWorkflow) as worker: - handle = await stream_client.start_workflow( - AppendAndCloseWorkflow.run, - f"stream-{uuid.uuid4()}", - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - ref = await handle.result() - - stream = stream_client.get_stream(ref, body_type=str) - records = [record async for record in stream.iter_read_items()] - assert [record.body for record in records] == ["a", "b"] - run_id = handle.result_run_id - for record in records: - # A Workflow writes as itself, with its Workflow Task as the epoch. - assert record.producer is not None - assert record.producer.id == f"wf:{run_id}" - assert isinstance(record.producer.kind, ProducerKindWorkflow) - assert record.producer.kind.value.run_id == run_id - assert record.producer.kind.value.epoch is not None - described = await stream.describe() - assert described.status == StreamStatus.StreamStatusClosed - err = await _stream_error(stream.append("c")) - assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_CLOSED - - async def test_activity_appends_with_activity_producer(client: Client): stream_client = _with_store(client, _store()) stream = await _create_stream(stream_client) diff --git a/tests/contrib/streams/test_appender_workflow.py b/tests/contrib/streams/test_appender_workflow.py deleted file mode 100644 index 998149b2e9..0000000000 --- a/tests/contrib/streams/test_appender_workflow.py +++ /dev/null @@ -1,198 +0,0 @@ -from __future__ import annotations - -import asyncio -import uuid -from datetime import timedelta -from typing import Any - -from google.protobuf.message import Message - -import temporalio.api.streamservice.v1 -from temporalio import workflow -from temporalio.api.enums.v1 import EventType, StreamErrorReason -from temporalio.api.history.v1 import HistoryEvent -from temporalio.client import Client, WorkflowHandle -from temporalio.contrib.streams import BufferedAppender -from temporalio.exceptions import StreamError -from temporalio.nexus.system.stream_service import StreamRef -from temporalio.nexus.system.stream_service.workflow import create_stream, get_stream -from temporalio.testing import MemoryStreamStore -from temporalio.worker import Replayer -from tests.helpers import new_worker -from tests.helpers.streams import RecordingStreamStore -from tests.nexus.test_stream_store import _with_store - -_BURST = [f"record-{index}" for index in range(40)] - - -class BatchStore(RecordingStreamStore): - """Keeps the number of Records in each append.""" - - def __init__(self) -> None: - super().__init__(MemoryStreamStore()) - self.batches: list[int] = [] - - async def before_call(self, operation: str, request: Message) -> None: - if isinstance(request, temporalio.api.streamservice.v1.AppendRecordsRequest): - self.batches.append(len(request.records)) - - -async def _yield_a_few_times() -> None: - # A turn of the loop with no timer, so the write starts and waits for its Local Activity. - for _ in range(5): - await asyncio.sleep(0) - - -@workflow.defn -class BurstWorkflow: - @workflow.run - async def run(self, stream_id: str) -> str: - stream = await create_stream(id=stream_id, body_type=str) - appender = BufferedAppender(stream) - for value in _BURST: - appender.append(value) - last_offset = await appender.flush() - assert last_offset is not None - return last_offset.hex() - - -@workflow.defn -class InFlightWorkflow: - @workflow.run - async def run(self, stream_id: str) -> None: - stream = await create_stream(id=stream_id, body_type=str) - async with BufferedAppender(stream) as appender: - appender.append("a") - await _yield_a_few_times() - # The write of "a" waits for its Local Activity, so these go into the next one. - appender.append("b") - appender.append("c", "d") - - -@workflow.defn -class FailedAppendWorkflow: - @workflow.run - async def run(self, stream_id: str) -> list[Any]: - appender = BufferedAppender(get_stream(StreamRef(id=stream_id), body_type=str)) - appender.append("a") - await _yield_a_few_times() - appender.append("b", "c") - try: - await appender.flush() - except StreamError as err: - first = err - else: - return ["flushed"] - try: - appender.append("d") - except StreamError as err: - later = err - else: - return ["appended"] - return [ - StreamErrorReason.Name(first.reason), - later is first, - appender.unwritten, - ] - - -@workflow.defn -class MaxDelayWorkflow: - @workflow.run - async def run(self, stream_id: str) -> str: - stream = get_stream(StreamRef(id=stream_id), body_type=str) - try: - BufferedAppender(stream, max_delay=timedelta(milliseconds=10)) - except ValueError as err: - return str(err) - return "built" - - -async def _run( - client: Client, workflow_type: type[Any], stream_id: str -) -> tuple[WorkflowHandle[Any, Any], Any]: - async with new_worker(client, workflow_type) as worker: - handle: WorkflowHandle[Any, Any] = await client.start_workflow( - workflow_type.run, - stream_id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - execution_timeout=timedelta(seconds=30), - ) - return handle, await handle.result() - - -def _of_type(events: list[HistoryEvent], event_type: EventType.ValueType) -> int: - return sum(1 for event in events if event.event_type == event_type) - - -async def _bodies(client: Client, stream_id: str) -> list[str]: - stream = client.get_stream(StreamRef(id=stream_id), body_type=str) - return [record.body async for record in stream.iter_read_items()] - - -async def test_appends_of_one_activation_are_one_append(client: Client): - store = BatchStore() - stream_client = _with_store(client, store) - stream_id = f"stream-{uuid.uuid4()}" - handle, last_offset = await _run(stream_client, BurstWorkflow, stream_id) - - assert store.batches == [40] - assert await _bodies(stream_client, stream_id) == _BURST - described = await stream_client.get_stream( - StreamRef(id=stream_id), body_type=str - ).describe() - assert described.last_offset is not None - assert last_offset == described.last_offset.hex() - events = list((await handle.fetch_history()).events) - # One marker for the create and one for the append. - assert _of_type(events, EventType.EVENT_TYPE_MARKER_RECORDED) == 2 - assert _of_type(events, EventType.EVENT_TYPE_TIMER_STARTED) == 0 - - -async def test_appends_during_a_workflow_append_join_the_next_one(client: Client): - store = BatchStore() - stream_client = _with_store(client, store) - stream_id = f"stream-{uuid.uuid4()}" - handle, _ = await _run(stream_client, InFlightWorkflow, stream_id) - - assert store.batches == [1, 3] - assert await _bodies(stream_client, stream_id) == ["a", "b", "c", "d"] - events = list((await handle.fetch_history()).events) - assert _of_type(events, EventType.EVENT_TYPE_MARKER_RECORDED) == 3 - assert _of_type(events, EventType.EVENT_TYPE_TIMER_STARTED) == 0 - - -async def test_a_failed_workflow_append_stops_the_appender(client: Client): - store = BatchStore() - _, result = await _run( - _with_store(client, store), FailedAppendWorkflow, f"stream-{uuid.uuid4()}" - ) - assert result == ["STREAM_ERROR_REASON_NOT_FOUND", True, 3] - assert store.batches == [1] - - -async def test_replay_of_workflow_appender_writes_nothing(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) - stream_id = f"stream-{uuid.uuid4()}" - handle, _ = await _run(stream_client, InFlightWorkflow, stream_id) - - replay_store = BatchStore() - await Replayer( - workflows=[InFlightWorkflow], - stream_config=_with_store(client, replay_store).config()["stream_config"], - ).replay_workflow(await handle.fetch_history()) - assert replay_store.operations == [] - assert await _bodies(stream_client, stream_id) == ["a", "b", "c", "d"] - - -async def test_workflow_appender_refuses_a_max_delay(client: Client): - _, message = await _run( - _with_store(client, MemoryStreamStore()), - MaxDelayWorkflow, - f"stream-{uuid.uuid4()}", - ) - assert message == ( - "BufferedAppender takes no max_delay in Workflow code, since a delay would put " - "a timer into History. Leave max_delay as None." - ) diff --git a/tests/contrib/streams/test_producer.py b/tests/contrib/streams/test_producer.py index 9dcd4e3408..e579a2042b 100644 --- a/tests/contrib/streams/test_producer.py +++ b/tests/contrib/streams/test_producer.py @@ -22,11 +22,9 @@ ProducerKindWorkflowActivity, StreamRecord, StreamRef, - StreamStatus, WorkflowProducer, ) from temporalio.nexus.system.stream_service.client import Stream -from temporalio.nexus.system.stream_service.workflow import create_stream from temporalio.testing import ActivityEnvironment, MemoryStreamStore from tests.helpers import new_worker from tests.nexus.test_stream_store import _create_stream, _with_store @@ -123,43 +121,25 @@ async def append_and_give_producer() -> Producer: @workflow.defn class CurrentProducerWorkflow: @workflow.run - async def run(self, stream_id: str) -> Producer: - stream = await create_stream(id=stream_id, body_type=str) - producer = current_producer() - await stream.append("a", producer=producer) - await stream.close(producer=producer) - return producer + async def run(self) -> str: + try: + current_producer() + except RuntimeError as err: + return str(err) + return "no error" -async def test_current_producer_in_workflow_code_is_the_workflow_producer( - client: Client, -): - stream_client = _with_store(client, MemoryStreamStore()) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker(stream_client, CurrentProducerWorkflow) as worker: - handle = await stream_client.start_workflow( +async def test_current_producer_in_workflow_code_raises(client: Client): + async with new_worker(client, CurrentProducerWorkflow) as worker: + message = await client.execute_workflow( CurrentProducerWorkflow.run, - stream_id, id=f"workflow-{uuid.uuid4()}", task_queue=worker.task_queue, ) - producer = await handle.result() - run_id = handle.result_run_id - assert run_id - assert producer == Producer( - id=f"wf:{run_id}", kind=ProducerKindWorkflow(WorkflowProducer(run_id=run_id)) + assert message == ( + "Workflow code can't call a Stream in this version. Call it from an Activity " + "through activity.client()." ) - # On a Store, the append writes as the Workflow, with its Workflow Task as the epoch. - # The close passes the Store's check of that epoch too. - stream = stream_client.get_stream(StreamRef(id=stream_id), body_type=str) - [record] = await _records(stream) - assert record.producer is not None - assert record.producer.id == producer.id - assert isinstance(record.producer.kind, ProducerKindWorkflow) - assert record.producer.kind.value.epoch is not None - assert record.producer.kind.value.epoch.attempt == 1 - described = await stream.describe() - assert described.status == StreamStatus.StreamStatusClosed @pytest.mark.parametrize("kind", ["workflow", *_ACTIVITY_INFOS, "client"]) diff --git a/tests/nexus/test_stream_store.py b/tests/nexus/test_stream_store.py index 0b10411c5f..75cda17c87 100644 --- a/tests/nexus/test_stream_store.py +++ b/tests/nexus/test_stream_store.py @@ -20,7 +20,6 @@ import temporalio.client._stream_store import temporalio.common import temporalio.converter._payload_converter -import temporalio.exceptions import temporalio.nexus.system import temporalio.nexus.system._support import temporalio.workflow @@ -33,7 +32,7 @@ ) from temporalio.client._stream_store import _STREAM_OPERATIONS, _request_ref from temporalio.converter import DataConverter, StorageDriverActivityInfo -from temporalio.exceptions import ApplicationError, StreamConfigError, StreamError +from temporalio.exceptions import StreamConfigError, StreamError from temporalio.nexus.system.stream_service import ( ClientProducer, DescribeStreamRequest, @@ -640,32 +639,3 @@ def test_stream_error_carries_its_reason(): assert StreamError("x").reason == ( StreamErrorReason.STREAM_ERROR_REASON_UNSPECIFIED ) - - -def test_stream_error_from_application_error(): - from_failure = temporalio.exceptions._stream_error_from_failure - rebuilt = from_failure( - ApplicationError( - "the Stream exists", - temporalio.api.stream.v1.StreamErrorDetails( - reason=StreamErrorReason.STREAM_ERROR_REASON_ALREADY_EXISTS - ), - type="StreamError", - ) - ) - assert isinstance(rebuilt, StreamError) - assert rebuilt.reason == StreamErrorReason.STREAM_ERROR_REASON_ALREADY_EXISTS - assert rebuilt.message == "the Stream exists" - unknown = from_failure( - ApplicationError( - "x", - temporalio.api.stream.v1.StreamErrorDetails( - reason=StreamErrorReason.ValueType(999) - ), - type="StreamError", - ) - ) - assert unknown is not None - assert unknown.reason == StreamErrorReason.STREAM_ERROR_REASON_UNSPECIFIED - assert from_failure(ApplicationError("x", type="RuntimeError")) is None - assert from_failure(RuntimeError("x")) is None diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index 9bb641aa9e..6ab541d0d5 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -1,242 +1,85 @@ from __future__ import annotations -import asyncio import uuid -from collections.abc import Callable, Sequence from datetime import timedelta -from typing import Any import pytest -from google.protobuf.message import Message -import temporalio.api.streamservice.v1 -import temporalio.common -import temporalio.exceptions from temporalio import activity, workflow -from temporalio.api.enums.v1 import EventType, StreamErrorReason -from temporalio.api.streamservice.v1 import ( - CreateStreamRequest as CreateStreamRequestMessage, -) -from temporalio.api.streamservice.v1 import ( - CreateStreamResponse as CreateStreamResponseMessage, -) -from temporalio.api.streamservice.v1 import ( - DescribeStreamRequest as DescribeStreamRequestMessage, -) -from temporalio.api.streamservice.v1 import ( - DescribeStreamResponse as DescribeStreamResponseMessage, -) -from temporalio.client import ( - Client, - StreamConfig, - StreamSelectContext, - StreamStore, - StreamStoreCallOptions, - WorkflowFailureError, - WorkflowHandle, -) -from temporalio.common import RetryPolicy -from temporalio.converter import DataConverter, StorageDriverWorkflowInfo -from temporalio.exceptions import ApplicationError, StreamError +from temporalio.client import Client from temporalio.nexus.system import TEMPORAL_SYSTEM_ENDPOINT from temporalio.nexus.system.stream_service import ( - AppendRecordsResponse, - ClientProducer, Producer, - ProducerKindClient, ProducerKindLocalActivity, - ProducerKindWorkflow, ReadRecordsRequest, - StreamIdConflictPolicy, StreamRecord, StreamRef, StreamService, - StreamStatus, ) from temporalio.nexus.system.stream_service.workflow import create_stream, get_stream -from temporalio.nexus.system.stream_service.workflow._resources.stream import ( - Stream as WorkflowStream, -) from temporalio.testing import MemoryStreamStore -from temporalio.worker import ( - ActivityInboundInterceptor, - ExecuteActivityInput, - Interceptor, - Replayer, - Worker, -) from tests.helpers import assert_task_fail_eventually, new_worker -from tests.helpers.streams import DictStreamStore, RecordingStreamStore -from tests.test_converter import SimpleCodec +from tests.helpers.streams import RecordingStreamStore +from tests.nexus.test_stream_store import _with_store -# Workflow code writes these values, so the History shows only whether a Record body -# leaks into it. _BODIES = ["first-record-body", "second-record-body"] -@activity.defn -async def append_values(ref: StreamRef[str], values: list[str]) -> None: - await activity.client().get_stream(ref, body_type=str).append(*values) - - -async def _append_in_local_activity(ref: StreamRef[str], values: list[str]) -> None: - await workflow.execute_local_activity( - append_values, args=[ref, values], start_to_close_timeout=timedelta(seconds=10) - ) - - -@workflow.defn -class AppendWorkflow: - @workflow.run - async def run(self, stream_id: str) -> StreamRef[str]: - stream = await create_stream(id=stream_id, body_type=str) - await stream.append(*_BODIES) - return stream.ref - - -@workflow.defn -class LocalActivityAppendWorkflow: - @workflow.run - async def run(self, stream_id: str) -> StreamRef[str]: - stream = await create_stream(id=stream_id, body_type=str) - await _append_in_local_activity(stream.ref, _BODIES) - return stream.ref - - -# Workflow code appends either with the Workflow `Stream` or with a Local Activity of -# its own, and both write as the Local Activity that runs the append. -_APPEND_WORKFLOWS = pytest.mark.parametrize( - "append_workflow", [AppendWorkflow, LocalActivityAppendWorkflow] -) - - -@workflow.defn -class AppendOffsetsWorkflow: - @workflow.run - async def run(self, stream_id: str) -> AppendRecordsResponse: - stream = await create_stream(id=stream_id, body_type=str) - return await stream.append(*_BODIES) - - @workflow.defn -class AppendToStreamWorkflow: +class StreamCallWorkflow: @workflow.run - async def run(self, stream_id: str) -> str: - stream = get_stream(StreamRef(id=stream_id), body_type=str) - try: + async def run(self, operation: str) -> None: + stream = get_stream(StreamRef(id="stream"), body_type=str) + if operation == "CreateStream": + await create_stream(id="stream", body_type=str) + elif operation == "AppendRecords": await stream.append("a") - except StreamError as err: - assert isinstance(err, ApplicationError) - return StreamErrorReason.Name(err.reason) - return "appended" + elif operation == "CloseStream": + await stream.close() + elif operation == "DescribeStream": + await stream.describe() + else: + # The Workflow `Stream` has no `read`, but a Nexus client can start one. + nexus_client = workflow.create_nexus_client( + service=StreamService, endpoint=TEMPORAL_SYSTEM_ENDPOINT + ) + await nexus_client.start_operation( + operation, ReadRecordsRequest(ref=StreamRef[str](id="stream")) + ) -@workflow.defn -class ExplicitProducerWorkflow: - @workflow.run - async def run(self, stream_id: str) -> None: - stream = await create_stream(id=stream_id, body_type=str) - await stream.append( - "a", producer=Producer(id="mine", kind=ProducerKindClient(ClientProducer())) +@pytest.mark.parametrize( + "operation", + ["CreateStream", "AppendRecords", "CloseStream", "DescribeStream", "ReadRecords"], +) +async def test_workflow_stream_call_fails_the_workflow_task( + client: Client, operation: str +): + store = RecordingStreamStore(MemoryStreamStore()) + stream_client = _with_store(client, store) + async with new_worker(stream_client, StreamCallWorkflow) as worker: + handle = await stream_client.start_workflow( + StreamCallWorkflow.run, + operation, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + execution_timeout=timedelta(seconds=30), ) - - -@workflow.defn -class RefusedOperationWorkflow: - @workflow.run - async def run(self, operation: str) -> str: - # The Workflow `Stream` has no `read`, so the test starts its operation through - # a Nexus client, as Workflow code still can. - requests: dict[str, Any] = { - "ReadRecords": ReadRecordsRequest(ref=StreamRef[str](id="stream")), - } - nexus_client = workflow.create_nexus_client( - service=StreamService, endpoint=TEMPORAL_SYSTEM_ENDPOINT + await assert_task_fail_eventually( + handle, + message_contains=( + "Workflow code can't call a Stream in this version. Call it from an " + "Activity through activity.client()." + ), ) - try: - await nexus_client.start_operation(operation, requests[operation]) - except ApplicationError as err: - assert err.non_retryable - return err.message - return "started" - - -@workflow.defn -class DescribeResultWorkflow: - @workflow.run - async def run(self, stream_id: str) -> str: - stream = await create_stream(id=stream_id, body_type=str) - try: - await stream.describe() - except StreamError as err: - return f"StreamError {StreamErrorReason.Name(err.reason)}: {err.message}" - except ApplicationError as err: - return f"ApplicationError {err.type}: {err.message}" - except temporalio.exceptions.TimeoutError as err: - return f"TimeoutError {err.type and err.type.name}" - return "described" - - -@workflow.defn -class DescribeStreamWorkflow: - @workflow.run - async def run(self, stream_id: str) -> str: - stream = get_stream(StreamRef(id=stream_id), body_type=str) - try: - await stream.describe() - except StreamError as err: - assert isinstance(err, ApplicationError) - return StreamErrorReason.Name(err.reason) - return "described" - - -@workflow.defn -class CreateThenContinueAsNewWorkflow: - @workflow.run - async def run(self, stream_id: str, run: int) -> list[bool]: - stream = await create_stream(id=stream_id, body_type=str) - await _append_in_local_activity(stream.ref, [f"run-{run}"]) - if run == 1: - workflow.continue_as_new(args=[stream_id, 2]) - return [bool(stream.created)] - - -@workflow.defn -class DescribeOtherStoreWorkflow: - @workflow.run - async def run(self, stream_id: str) -> None: - await get_stream( - StreamRef(id=stream_id, store="other"), body_type=str - ).describe() - - -@workflow.defn -class CreateAndDescribeWorkflow: - @workflow.run - async def run(self, stream_id: str) -> StreamRef[str]: - stream = await create_stream(id=stream_id, body_type=str) - await stream.describe() - return stream.ref - - -class CountingStore(RecordingStreamStore): - def __init__(self, name: str) -> None: - super().__init__(MemoryStreamStore(name=name)) + await handle.terminate() + # The Store of the Worker's client gets no call. + assert store.operations == [] @activity.defn -async def user_activity() -> None: - pass - - -@workflow.defn -class CreateAndActivityWorkflow: - @workflow.run - async def run(self, stream_id: str) -> None: - await create_stream(id=stream_id, body_type=str) - await workflow.execute_local_activity( - user_activity, start_to_close_timeout=timedelta(seconds=10) - ) +async def append_values(ref: StreamRef[str], values: list[str]) -> None: + await activity.client().get_stream(ref, body_type=str).append(*values) _fail_next_task = True @@ -245,16 +88,12 @@ async def run(self, stream_id: str) -> None: @workflow.defn(sandboxed=False) class AppendThenFailTaskOnceWorkflow: @workflow.run - async def run(self, stream_id: str, in_local_activity: bool) -> None: - stream = await create_stream( - id=stream_id, - body_type=str, - id_conflict_policy=StreamIdConflictPolicy.StreamIdConflictPolicyUseExisting, + async def run(self, ref: StreamRef[str]) -> None: + await workflow.execute_local_activity( + append_values, + args=[ref, _BODIES], + start_to_close_timeout=timedelta(seconds=10), ) - if in_local_activity: - await _append_in_local_activity(stream.ref, _BODIES) - else: - await stream.append(*_BODIES) global _fail_next_task if _fail_next_task: _fail_next_task = False @@ -278,190 +117,13 @@ async def run(self, stream_id: str) -> None: ) -class FlakyDescribeStore(RecordingStreamStore): - def __init__(self, *, failures: int = 0, delay: timedelta = timedelta()) -> None: - super().__init__(MemoryStreamStore()) - self._failures = failures - self._delay = delay - self.describes = 0 - - async def before_call(self, operation: str, request: Message) -> None: - if operation == "DescribeStream": - self.describes += 1 - await asyncio.sleep(self._delay.total_seconds()) - if self.describes <= self._failures: - raise RuntimeError("describe failed") - - -class FlakyCreateStore(CountingStore): - def __init__(self, name: str) -> None: - super().__init__(name) - self.failed_creates = 0 - - async def before_call(self, operation: str, request: Message) -> None: - if operation == "CreateStream" and not self.failed_creates: - self.failed_creates += 1 - raise RuntimeError("create failed") - - -class LostAnswerStore(RecordingStreamStore): - """Keeps each append request, and can lose the answer of the first append.""" - - def __init__(self, *, lose_first_answer: bool = True) -> None: - super().__init__(MemoryStreamStore()) - self._lose_first_answer = lose_first_answer - self.appends: list[temporalio.api.streamservice.v1.AppendRecordsRequest] = [] - self.first_response: ( - temporalio.api.streamservice.v1.AppendRecordsResponse | None - ) = None - - async def append_records( - self, request: temporalio.api.streamservice.v1.AppendRecordsRequest - ) -> temporalio.api.streamservice.v1.AppendRecordsResponse: - kept = temporalio.api.streamservice.v1.AppendRecordsRequest() - kept.CopyFrom(request) - self.appends.append(kept) - response = await super().append_records(request) - if self.first_response is None: - self.first_response = response - if self._lose_first_answer: - raise StreamError( - "Lost the answer of a write", - reason=StreamErrorReason.STREAM_ERROR_REASON_STORAGE_UNAVAILABLE, - ) - return response - - -class CloseRequestStore(RecordingStreamStore): - """Keeps each close request.""" - - def __init__(self) -> None: - super().__init__(MemoryStreamStore()) - self.closes: list[temporalio.api.streamservice.v1.CloseStreamRequest] = [] - - async def close_stream( - self, request: temporalio.api.streamservice.v1.CloseStreamRequest - ) -> temporalio.api.streamservice.v1.CloseStreamResponse: - kept = temporalio.api.streamservice.v1.CloseStreamRequest() - kept.CopyFrom(request) - self.closes.append(kept) - return await super().close_stream(request) - - -@workflow.defn -class CloseWorkflow: - @workflow.run - async def run(self, stream_id: str) -> StreamRef[str]: - stream = await create_stream(id=stream_id, body_type=str) - await stream.append("a") - await stream.close() - return stream.ref - - -@workflow.defn -class AsyncWithCloseWorkflow: - @workflow.run - async def run(self, stream_id: str) -> str: - async with create_stream(id=stream_id, body_type=str) as stream: - await stream.append("a") - described = await stream.describe() - assert described.status is not None - return described.status.name - - -_fail_close_task = True - - -@workflow.defn(sandboxed=False) -class CloseThenFailTaskOnceWorkflow: - @workflow.run - async def run(self, stream_id: str) -> str: - # The test creates the Stream, so the retry of the task starts with the append. - stream = get_stream(StreamRef(id=stream_id), body_type=str) - try: - await stream.append("a") - except StreamError as err: - return f"{StreamErrorReason.Name(err.reason)}: {err.message}" - await stream.close() - global _fail_close_task - if _fail_close_task: - _fail_close_task = False - raise RuntimeError("Failed the Workflow Task once") - return "closed" - - -class RecordingInterceptor(Interceptor): - def __init__(self) -> None: - self.activity_types: list[str] = [] - self.is_local: list[bool] = [] - - def intercept_activity( - self, next: ActivityInboundInterceptor - ) -> ActivityInboundInterceptor: - return _RecordingActivityInbound(next, self) - - -class _RecordingActivityInbound(ActivityInboundInterceptor): - def __init__( - self, next: ActivityInboundInterceptor, recording: RecordingInterceptor - ) -> None: - super().__init__(next) - self._recording = recording - - async def execute_activity(self, input: ExecuteActivityInput) -> Any: - info = activity.info() - self._recording.activity_types.append(info.activity_type) - self._recording.is_local.append(info.is_local) - return await super().execute_activity(input) - - -def _with_store(client: Client, store: StreamStore) -> Client: - return _with_config(client, StreamConfig(stores=[store])) - - -def _with_config(client: Client, stream_config: StreamConfig) -> Client: - config = client.config() - config["stream_config"] = stream_config - return Client(**config) - - -def _two_counting_stores( - contexts: list[StreamSelectContext], -) -> tuple[CountingStore, CountingStore, StreamConfig]: - first = CountingStore("first") - second = CountingStore("second") - - def select(context: StreamSelectContext) -> StreamStore: - contexts.append(context) - return second - - return first, second, StreamConfig(stores=[first, second], store_selector=select) - - -async def _run_create_and_describe( - client: Client, stream_id: str -) -> tuple[WorkflowHandle[CreateAndDescribeWorkflow, StreamRef[str]], StreamRef[str]]: - async with new_worker(client, CreateAndDescribeWorkflow) as worker: - handle = await client.start_workflow( - CreateAndDescribeWorkflow.run, - stream_id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - return handle, await handle.result() - - async def _read_all(client: Client, stream_id: str) -> list[StreamRecord[str]]: stream = client.get_stream(StreamRef(id=stream_id), body_type=str) return [record async for record in stream.iter_read_items()] -def _epoch(producer: Producer) -> tuple[int, int, int]: - # The Workflow Task, then the attempt of the Local Activity, which a Workflow lacks. - if isinstance(producer.kind, ProducerKindWorkflow): - task = producer.kind.value.epoch - assert task is not None - return (task.started_event_id, task.attempt, 0) +def _local_activity_epoch(producer: Producer) -> tuple[int, int, int]: + # The Workflow Task, then the attempt of the Local Activity. assert isinstance(producer.kind, ProducerKindLocalActivity) local = producer.kind.value.epoch return (local.task.started_event_id, local.task.attempt, local.attempt) @@ -473,327 +135,33 @@ def _producers(records: list[StreamRecord[str]]) -> list[Producer]: return [producer for producer in producers if producer is not None] -async def _run_append_workflow( - client: Client, append_workflow: type[Any] = AppendWorkflow -) -> tuple[WorkflowHandle[Any, StreamRef[str]], StreamRef[str]]: - async with new_worker( - client, append_workflow, activities=[append_values] - ) as worker: - handle: WorkflowHandle[Any, StreamRef[str]] = await client.start_workflow( - append_workflow.run, - f"stream-{uuid.uuid4()}", - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - return handle, await handle.result() - - -async def _describe_result( +async def test_local_activity_append_after_a_failed_workflow_task_has_a_higher_epoch( client: Client, - store: StreamStore, - options: StreamStoreCallOptions | None = None, -) -> str: - stream_config = StreamConfig( - stores=[store], workflow_call_options=options or StreamStoreCallOptions() - ) - stream_client = _with_config(client, stream_config) - async with new_worker(stream_client, DescribeResultWorkflow) as worker: - return await stream_client.execute_workflow( - DescribeResultWorkflow.run, - f"stream-{uuid.uuid4()}", - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - - -@_APPEND_WORKFLOWS -async def test_workflow_stream_uses_store(client: Client, append_workflow: type[Any]): - stream_client = _with_store(client, MemoryStreamStore()) - handle, ref = await _run_append_workflow(stream_client, append_workflow) - - stream = stream_client.get_stream(ref, body_type=str) - records = [record async for record in stream.iter_read_items()] - assert [record.body for record in records] == _BODIES - run_id = handle.result_run_id - assert run_id - first, second = _producers(records) - assert first == second - if append_workflow is AppendWorkflow: - # The Workflow writes as itself, with its first Workflow Task as the epoch. - assert first.id == f"wf:{run_id}" - assert isinstance(first.kind, ProducerKindWorkflow) - assert first.kind.value.run_id == run_id - assert first.kind.value.epoch is not None - assert first.kind.value.epoch.started_event_id > 0 - assert first.kind.value.epoch.attempt == 1 - return - # The create is the run's first Local Activity, so the append is its second. - assert first.id == f"la:{run_id}:2" - assert isinstance(first.kind, ProducerKindLocalActivity) - assert first.kind.value.run_id == run_id - assert first.kind.value.epoch.task.started_event_id > 0 - assert first.kind.value.epoch.attempt == 1 - - -async def test_workflow_stream_uses_a_store_written_from_scratch(client: Client): - store = DictStreamStore() - stream_client = _with_store(client, store) - stream_id = f"stream-{uuid.uuid4()}" - _, ref = await _run_create_and_describe(stream_client, stream_id) - assert ref == StreamRef(id=stream_id, store="dict") - assert list(store.streams) == [stream_id] - - stream = stream_client.get_stream(ref, body_type=str) - await stream.append("a") - assert [record.body async for record in stream.iter_read_items()] == ["a"] - - -@_APPEND_WORKFLOWS -async def test_workflow_stream_store_uses_payload_codec( - client: Client, append_workflow: type[Any] -): - store = MemoryStreamStore() - config = client.config() - config["data_converter"] = DataConverter(payload_codec=SimpleCodec()) - config["stream_config"] = StreamConfig(stores=[store]) - stream_client = Client(**config) - _, ref = await _run_append_workflow(stream_client, append_workflow) - - # A client without the codec reads the bodies as the Store keeps them. - raw = _with_store(client, store).get_stream( - StreamRef(id=ref.id), body_type=temporalio.common.RawValue - ) - stored = [record.body.payload async for record in raw.iter_read_items()] - assert [body.metadata["simple-codec"] for body in stored] == [b"true", b"true"] - stream = stream_client.get_stream(ref, body_type=str) - assert [record.body async for record in stream.iter_read_items()] == _BODIES - - -@_APPEND_WORKFLOWS -async def test_workflow_stream_history_has_no_bodies( - client: Client, append_workflow: type[Any] -): - stream_client = _with_store(client, MemoryStreamStore()) - handle, _ = await _run_append_workflow(stream_client, append_workflow) - events = (await handle.fetch_history()).events - markers = [ - event.marker_recorded_event_attributes.marker_name - for event in events - if event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED - ] - # One marker for the create and one for the append. - assert markers == ["core_local_activity", "core_local_activity"] - for event in events: - for body in _BODIES: - assert body.encode() not in event.SerializeToString() - - -@_APPEND_WORKFLOWS -async def test_workflow_stream_replay_does_not_call_store( - client: Client, append_workflow: type[Any] -): - stream_client = _with_store(client, MemoryStreamStore()) - handle, _ = await _run_append_workflow(stream_client, append_workflow) - replay_store = CountingStore("memory") - await Replayer( - workflows=[append_workflow], - stream_config=StreamConfig(stores=[replay_store]), - ).replay_workflow(await handle.fetch_history()) - assert replay_store.operations == [] - - -async def test_workflow_create_without_stream_config_fails_the_workflow( - client: Client, -): - async with new_worker(client, CreateAndDescribeWorkflow) as worker: - handle = await client.start_workflow( - CreateAndDescribeWorkflow.run, - f"stream-{uuid.uuid4()}", - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - execution_timeout=timedelta(seconds=30), - ) - with pytest.raises(WorkflowFailureError) as err: - await asyncio.wait_for(handle.result(), timeout=10) - cause = err.value.cause - assert isinstance(cause, ApplicationError) - assert not isinstance(cause, StreamError) - assert cause.type == "StreamConfigError" - assert cause.non_retryable - assert cause.message == ( - "The client of this Worker has no Stream Store. Give one in " - "stream_config=StreamConfig(stores=[...])." - ) - # A Replayer with no Stream config fails the Workflow the same way. - await Replayer(workflows=[CreateAndDescribeWorkflow]).replay_workflow( - await handle.fetch_history() - ) - - -async def test_workflow_read_fails_with_store(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) - async with new_worker(stream_client, RefusedOperationWorkflow) as worker: - result = await stream_client.execute_workflow( - RefusedOperationWorkflow.run, - "ReadRecords", - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - assert result == ( - "Workflow code can't read a Stream on a Store in this release. " - "Call an Activity that reads it through its client." - ) - - -def test_workflow_stream_has_append_and_close_and_no_read(): - for name in ("append", "close", "describe", "__aenter__", "__aexit__"): - assert hasattr(WorkflowStream, name) - assert not hasattr(WorkflowStream, "read") - - -async def test_workflow_stream_store_error_reaches_workflow_code(client: Client): - store = FlakyDescribeStore(failures=1) - result = await _describe_result(client, store) - assert result == "ApplicationError RuntimeError: describe failed" - assert store.describes == 1 - - -async def test_workflow_describe_of_unknown_stream_raises_stream_error(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) - async with new_worker(stream_client, DescribeStreamWorkflow) as worker: - reason = await stream_client.execute_workflow( - DescribeStreamWorkflow.run, - f"stream-{uuid.uuid4()}", - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - assert reason == "STREAM_ERROR_REASON_NOT_FOUND" - - -async def test_strict_create_in_each_run_of_a_chain_succeeds(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker( - stream_client, CreateThenContinueAsNewWorkflow, activities=[append_values] - ) as worker: - handle = await stream_client.start_workflow( - CreateThenContinueAsNewWorkflow.run, - args=[stream_id, 1], - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - assert await handle.result() == [True] - first_run_id = handle.first_execution_run_id - last_run = await stream_client.get_workflow_handle(handle.id).describe() - assert first_run_id - assert last_run.run_id != first_run_id - records = await _read_all(stream_client, stream_id) - assert [record.body for record in records] == ["run-1", "run-2"] - first, second = _producers(records) - assert isinstance(first.kind, ProducerKindLocalActivity) - assert isinstance(second.kind, ProducerKindLocalActivity) - assert first.kind.value.run_id == first_run_id - assert second.kind.value.run_id == last_run.run_id - - -@_APPEND_WORKFLOWS -async def test_client_append_after_workflow_append_keeps_offset_order( - client: Client, append_workflow: type[Any] -): - stream_client = _with_store(client, MemoryStreamStore()) - _, ref = await _run_append_workflow(stream_client, append_workflow) - stream = stream_client.get_stream(ref, body_type=str) - appended = await stream.append("from-client") - records = await _read_all(stream_client, ref.id) - assert [record.body for record in records] == [*_BODIES, "from-client"] - offsets = [record.offset for record in records] - assert offsets == sorted(offsets) - assert len(set(offsets)) == len(offsets) - assert offsets[-1] == appended.last_offset - kinds = [type(producer.kind) for producer in _producers(records)] - workflow_kind = ( - ProducerKindWorkflow - if append_workflow is AppendWorkflow - else ProducerKindLocalActivity - ) - assert kinds == [workflow_kind, workflow_kind, ProducerKindClient] - - -async def test_workflow_stream_store_call_options_retry(client: Client): - options = StreamStoreCallOptions( - retry_policy=RetryPolicy( - initial_interval=timedelta(milliseconds=10), maximum_attempts=2 - ) - ) - store = FlakyDescribeStore(failures=1) - assert await _describe_result(client, store, options) == "described" - assert store.describes == 2 - - -async def test_workflow_stream_store_call_options_timeout(client: Client): - options = StreamStoreCallOptions(start_to_close_timeout=timedelta(milliseconds=200)) - store = FlakyDescribeStore(delay=timedelta(seconds=5)) - result = await _describe_result(client, store, options) - assert result == "TimeoutError START_TO_CLOSE" - - -async def test_worker_keeps_its_stream_stores(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) - async with new_worker(stream_client, AppendWorkflow) as worker: - with pytest.raises(ValueError, match="Stream Stores"): - worker.client = client - worker.client = stream_client - - -async def test_activity_interceptors_see_stream_store_activity(client: Client): - interceptor = RecordingInterceptor() - stream_client = _with_store(client, MemoryStreamStore()) - async with new_worker( - stream_client, - CreateAndActivityWorkflow, - activities=[user_activity], - interceptors=[interceptor], - ) as worker: - await stream_client.execute_workflow( - CreateAndActivityWorkflow.run, - f"stream-{uuid.uuid4()}", - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - # The create runs as the SDK's Local Activity, before the Workflow's own Activity. - assert interceptor.activity_types == [ - "__temporal_stream_store_call:memory", - "user_activity", - ] - - -@pytest.mark.parametrize("in_local_activity", [False, True]) -async def test_workflow_task_retry_appends_with_higher_epoch( - client: Client, in_local_activity: bool ): global _fail_next_task _fail_next_task = True stream_client = _with_store(client, MemoryStreamStore()) - stream_id = f"stream-{uuid.uuid4()}" + stream = await stream_client.create_stream( + id=f"stream-{uuid.uuid4()}", body_type=str + ) async with new_worker( stream_client, AppendThenFailTaskOnceWorkflow, activities=[append_values] ) as worker: await stream_client.execute_workflow( AppendThenFailTaskOnceWorkflow.run, - args=[stream_id, in_local_activity], + stream.ref, id=f"workflow-{uuid.uuid4()}", task_queue=worker.task_queue, ) - records = await _read_all(stream_client, stream_id) + records = await _read_all(stream_client, stream.ref.id) # The failed Workflow Task records no marker, so its retry runs the Local Activity # again and writes a second copy. assert [record.body for record in records] == [*_BODIES, *_BODIES] producers = _producers(records) assert len({producer.id for producer in producers}) == 1 - if not in_local_activity: - assert producers[0].id.startswith("wf:") - - first, second, third, fourth = (_epoch(producer) for producer in producers) + first, second, third, fourth = ( + _local_activity_epoch(producer) for producer in producers + ) assert first == second assert third == fourth assert third > first @@ -823,520 +191,3 @@ async def test_user_local_activity_append_has_local_activity_producer(client: Cl assert producer.kind.value.run_id == run_id assert producer.kind.value.epoch.task.started_event_id > 0 assert producer.kind.value.epoch.attempt == 1 - - -async def test_workflow_ref_of_another_store_fails_the_task(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) - async with new_worker(stream_client, DescribeOtherStoreWorkflow) as worker: - handle = await stream_client.start_workflow( - DescribeOtherStoreWorkflow.run, - f"stream-{uuid.uuid4()}", - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - execution_timeout=timedelta(seconds=30), - ) - await assert_task_fail_eventually( - handle, message_contains="names the Stream Store 'other'" - ) - await handle.terminate() - - -async def test_workflow_create_without_store_on_several_stores_fails_the_task( - client: Client, -): - stream_config = StreamConfig( - stores=[CountingStore("first"), CountingStore("second")] - ) - stream_client = _with_config(client, stream_config) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker(stream_client, CreateAndDescribeWorkflow) as worker: - handle = await stream_client.start_workflow( - CreateAndDescribeWorkflow.run, - stream_id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - execution_timeout=timedelta(seconds=30), - ) - await assert_task_fail_eventually( - handle, - message_contains=( - "StreamConfig has more than one Store and no store_selector, and the " - f"call on the Stream '{stream_id}' names no Store. Name one with " - "store=, or set StreamConfig.store_selector." - ), - ) - await handle.terminate() - - -async def test_workflow_create_goes_to_the_selected_store(client: Client): - contexts: list[StreamSelectContext] = [] - first, second, stream_config = _two_counting_stores(contexts) - stream_id = f"stream-{uuid.uuid4()}" - handle, ref = await _run_create_and_describe( - _with_config(client, stream_config), stream_id - ) - assert ref.store == "second" - assert first.operations == [] - assert second.operations == ["CreateStream", "DescribeStream"] - # The describe goes by the Store that the ref names, so the selector runs once. - assert contexts == [ - StreamSelectContext( - id=stream_id, - target=StorageDriverWorkflowInfo( - id=handle.id, - type="CreateAndDescribeWorkflow", - run_id=handle.result_run_id, - namespace=client.namespace, - ), - ) - ] - - -async def test_workflow_replay_calls_no_store(client: Client): - _, _, stream_config = _two_counting_stores([]) - stream_id = f"stream-{uuid.uuid4()}" - handle, _ = await _run_create_and_describe( - _with_config(client, stream_config), stream_id - ) - contexts: list[StreamSelectContext] = [] - first, second, replay_config = _two_counting_stores(contexts) - await Replayer( - workflows=[CreateAndDescribeWorkflow], stream_config=replay_config - ).replay_workflow(await handle.fetch_history()) - # The selector runs in Workflow code, so a replay runs it again with the Workflow. - assert [context.id for context in contexts] == [stream_id] - target = contexts[0].target - assert isinstance(target, StorageDriverWorkflowInfo) - assert (target.id, target.run_id) == (handle.id, handle.result_run_id) - assert first.operations == [] - assert second.operations == [] - - -async def test_workflow_create_with_several_stores_takes_the_config_options( - client: Client, -): - first = CountingStore("first") - second = FlakyCreateStore("second") - stream_config = StreamConfig( - stores=[first, second], - store_selector=lambda _: second, - workflow_call_options=StreamStoreCallOptions( - retry_policy=RetryPolicy( - initial_interval=timedelta(milliseconds=10), maximum_attempts=2 - ) - ), - ) - # The default options make one attempt, so only the config's retry policy lets - # the create pass after its first attempt fails. - _, ref = await _run_create_and_describe( - _with_config(client, stream_config), f"stream-{uuid.uuid4()}" - ) - assert ref.store == "second" - assert first.operations == [] - assert second.operations == ["CreateStream", "CreateStream", "DescribeStream"] - - -class OwnActivityStore(CountingStore): - """A Store that brings its own Activity for the calls of Workflow code.""" - - def __init__(self) -> None: - super().__init__("own") - self.workflow_calls: list[str] = [] - - def worker_activities(self) -> Sequence[Callable[..., Any]]: - @activity.defn(name="own_store_call") - async def own_store_call(operation: str, request: bytes) -> bytes: - if operation == "CreateStream": - created = await self.create_stream( - CreateStreamRequestMessage.FromString(request) - ) - return created.SerializeToString() - described = await self.describe_stream( - DescribeStreamRequestMessage.FromString(request) - ) - return described.SerializeToString() - - return [own_store_call] - - async def call_from_workflow( - self, operation: str, request: Message, options: StreamStoreCallOptions - ) -> Message: - self.workflow_calls.append(operation) - response = await workflow.execute_local_activity( - "own_store_call", - args=[operation, request.SerializeToString()], - result_type=bytes, - start_to_close_timeout=options.start_to_close_timeout, - ) - if operation == "CreateStream": - return CreateStreamResponseMessage.FromString(response) - return DescribeStreamResponseMessage.FromString(response) - - -async def test_workflow_call_goes_through_the_store_own_activity(client: Client): - store = OwnActivityStore() - interceptor = RecordingInterceptor() - stream_client = _with_store(client, store) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker( - stream_client, CreateAndDescribeWorkflow, interceptors=[interceptor] - ) as worker: - ref = await stream_client.execute_workflow( - CreateAndDescribeWorkflow.run, - stream_id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - assert ref == StreamRef(id=stream_id, store="own") - assert store.workflow_calls == ["CreateStream", "DescribeStream"] - assert store.operations == ["CreateStream", "DescribeStream"] - assert interceptor.activity_types == ["own_store_call", "own_store_call"] - - -async def test_workflow_append_returns_offsets_of_records_in_order(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker(stream_client, AppendOffsetsWorkflow) as worker: - appended = await stream_client.execute_workflow( - AppendOffsetsWorkflow.run, - stream_id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - records = await _read_all(stream_client, stream_id) - assert [record.body for record in records] == _BODIES - assert [appended.first_offset, appended.last_offset] == [ - record.offset for record in records - ] - - -async def test_workflow_append_to_closed_stream_raises_stream_error(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) - stream = await stream_client.create_stream( - id=f"stream-{uuid.uuid4()}", body_type=str - ) - await stream.close() - async with new_worker(stream_client, AppendToStreamWorkflow) as worker: - reason = await stream_client.execute_workflow( - AppendToStreamWorkflow.run, - stream.ref.id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - assert reason == "STREAM_ERROR_REASON_CLOSED" - - -async def test_workflow_append_retry_in_one_task_writes_once(client: Client): - store = LostAnswerStore() - stream_config = StreamConfig( - stores=[store], - workflow_call_options=StreamStoreCallOptions( - retry_policy=RetryPolicy( - initial_interval=timedelta(milliseconds=10), maximum_attempts=2 - ) - ), - ) - stream_client = _with_config(client, stream_config) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker(stream_client, AppendOffsetsWorkflow) as worker: - appended = await stream_client.execute_workflow( - AppendOffsetsWorkflow.run, - stream_id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - first, second = store.appends - assert first.producer == second.producer - assert first.producer.workflow.epoch.attempt == 1 - # The append is the run's second Local Activity, after the create. - assert first.sequence == second.sequence == 2 - records = await _read_all(stream_client, stream_id) - assert [record.body for record in records] == _BODIES - assert store.first_response is not None - assert appended.first_offset == store.first_response.first_offset - assert appended.last_offset == store.first_response.last_offset - assert [appended.first_offset, appended.last_offset] == [ - record.offset for record in records - ] - - -async def test_workflow_explicit_producer_reaches_store(client: Client): - store = LostAnswerStore(lose_first_answer=False) - stream_client = _with_store(client, store) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker(stream_client, ExplicitProducerWorkflow) as worker: - await stream_client.execute_workflow( - ExplicitProducerWorkflow.run, - stream_id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - [producer] = _producers(await _read_all(stream_client, stream_id)) - assert producer.id == "mine" - assert isinstance(producer.kind, ProducerKindClient) - [request] = store.appends - assert request.producer.id == "mine" - assert request.producer.WhichOneof("kind") == "client" - assert request.sequence == 0 - - -async def _describe_status(client: Client, stream_id: str) -> StreamStatus | None: - stream = client.get_stream(StreamRef(id=stream_id), body_type=str) - return (await stream.describe()).status - - -async def test_workflow_close_runs_as_local_activity_in_its_task(client: Client): - store = CloseRequestStore() - stream_client = _with_store(client, store) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker(stream_client, CloseWorkflow) as worker: - handle = await stream_client.start_workflow( - CloseWorkflow.run, - stream_id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - ref = await handle.result() - assert await _describe_status(stream_client, stream_id) == ( - StreamStatus.StreamStatusClosed - ) - with pytest.raises(StreamError) as err: - await stream_client.get_stream(ref, body_type=str).append("b") - assert err.value.reason == StreamErrorReason.STREAM_ERROR_REASON_CLOSED - # The close writes as the Workflow, with its Workflow Task as the epoch. - [close] = store.closes - assert close.producer.id == f"wf:{handle.result_run_id}" - assert close.producer.workflow.run_id == handle.result_run_id - assert close.producer.workflow.epoch.started_event_id > 0 - assert close.producer.workflow.epoch.attempt == 1 - events = (await handle.fetch_history()).events - assert not [ - event - for event in events - if event.event_type == EventType.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED - ] - markers = [ - event.marker_recorded_event_attributes - for event in events - if event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED - ] - # One marker each for the create, the append and the close. - assert [marker.marker_name for marker in markers] == ["core_local_activity"] * 3 - # The Workflow's first task runs all three calls and completes the Workflow. - [task_completed] = [ - event.event_id - for event in events - if event.event_type == EventType.EVENT_TYPE_WORKFLOW_TASK_COMPLETED - ] - assert [marker.workflow_task_completed_event_id for marker in markers] == [ - task_completed - ] * 3 - - -async def test_workflow_async_with_closes_the_stream(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker(stream_client, AsyncWithCloseWorkflow) as worker: - status = await stream_client.execute_workflow( - AsyncWithCloseWorkflow.run, - stream_id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - assert status == StreamStatus.StreamStatusOpen.name - assert await _describe_status(stream_client, stream_id) == ( - StreamStatus.StreamStatusClosed - ) - records = await _read_all(stream_client, stream_id) - assert [record.body for record in records] == ["a"] - - -@pytest.mark.parametrize("no_remote_activities", [False, True]) -async def test_worker_with_workflows_only_runs_the_close( - client: Client, no_remote_activities: bool -): - stream_client = _with_store(client, MemoryStreamStore()) - stream_id = f"stream-{uuid.uuid4()}" - async with Worker( - stream_client, - task_queue=str(uuid.uuid4()), - workflows=[CloseWorkflow], - no_remote_activities=no_remote_activities, - ) as worker: - await stream_client.execute_workflow( - CloseWorkflow.run, - stream_id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - assert await _describe_status(stream_client, stream_id) == ( - StreamStatus.StreamStatusClosed - ) - - -async def test_failed_workflow_task_after_close_leaves_the_stream_closed( - client: Client, -): - global _fail_close_task - _fail_close_task = True - stream_client = _with_store(client, MemoryStreamStore()) - stream = await stream_client.create_stream( - id=f"stream-{uuid.uuid4()}", body_type=str - ) - async with new_worker(stream_client, CloseThenFailTaskOnceWorkflow) as worker: - handle = await stream_client.start_workflow( - CloseThenFailTaskOnceWorkflow.run, - stream.ref.id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - result = await handle.result() - # The close of the failed task reached the Store, so the retry can't append. - assert result == "STREAM_ERROR_REASON_CLOSED: the Stream is closed" - events = (await handle.fetch_history()).events - failed_tasks = [ - event - for event in events - if event.event_type == EventType.EVENT_TYPE_WORKFLOW_TASK_FAILED - ] - assert len(failed_tasks) == 1 - records = await _read_all(stream_client, stream.ref.id) - assert [record.body for record in records] == ["a"] - assert await _describe_status(stream_client, stream.ref.id) == ( - StreamStatus.StreamStatusClosed - ) - - -async def test_activity_interceptors_see_the_close_activity(client: Client): - interceptor = RecordingInterceptor() - stream_client = _with_store(client, MemoryStreamStore()) - async with new_worker( - stream_client, CloseWorkflow, interceptors=[interceptor] - ) as worker: - await stream_client.execute_workflow( - CloseWorkflow.run, - f"stream-{uuid.uuid4()}", - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - # The create, the append and the close each run as a Local Activity. - assert interceptor.activity_types == ["__temporal_stream_store_call:memory"] * 3 - assert interceptor.is_local == [True] * 3 - - -class SlowFirstAppendStore(LostAnswerStore): - """Keeps each append request, and holds the first append for a while.""" - - def __init__(self) -> None: - super().__init__(lose_first_answer=False) - self._held = False - - async def before_call(self, operation: str, request: Message) -> None: - # Without the order of the Workflow's writes, a later call would reach the Store - # first while this one waits. - if operation == "AppendRecords" and not self._held: - self._held = True - await asyncio.sleep(0.5) - - -@workflow.defn -class TwoAppendsWorkflow: - @workflow.run - async def run(self, stream_id: str, sleep_between: bool) -> None: - stream = await create_stream(id=stream_id, body_type=str) - await stream.append("a") - if sleep_between: - # A timer ends the Workflow Task, so the next append runs in a later one. - await workflow.sleep(timedelta(milliseconds=10)) - await stream.append("b") - - -@workflow.defn -class GatheredAppendsWorkflow: - @workflow.run - async def run(self, stream_id: str) -> None: - stream = await create_stream(id=stream_id, body_type=str) - await asyncio.gather(stream.append("a"), stream.append("b"), stream.append("c")) - - -@workflow.defn -class GatheredAppendAndCloseWorkflow: - @workflow.run - async def run(self, stream_id: str) -> None: - stream = await create_stream(id=stream_id, body_type=str) - await asyncio.gather(stream.append("a"), stream.close()) - - -async def _run_two_appends( - client: Client, store: StreamStore, sleep_between: bool -) -> tuple[str, list[StreamRecord[str]]]: - stream_client = _with_store(client, store) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker(stream_client, TwoAppendsWorkflow) as worker: - handle = await stream_client.start_workflow( - TwoAppendsWorkflow.run, - args=[stream_id, sleep_between], - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - await handle.result() - assert handle.result_run_id - return handle.result_run_id, await _read_all(stream_client, stream_id) - - -async def test_workflow_appends_in_one_task_write_as_one_producer(client: Client): - store = LostAnswerStore(lose_first_answer=False) - run_id, records = await _run_two_appends(client, store, sleep_between=False) - assert [record.body for record in records] == ["a", "b"] - first, second = _producers(records) - assert first == second - assert first.id == f"wf:{run_id}" - first_request, second_request = store.appends - assert first_request.producer == second_request.producer - assert 0 < first_request.sequence < second_request.sequence - - -async def test_workflow_append_in_a_later_task_has_a_higher_epoch(client: Client): - run_id, records = await _run_two_appends( - client, MemoryStreamStore(), sleep_between=True - ) - first, second = _producers(records) - assert first.id == second.id == f"wf:{run_id}" - assert _epoch(second) > _epoch(first) - - -async def test_workflow_appends_started_together_write_in_call_order(client: Client): - store = SlowFirstAppendStore() - stream_client = _with_store(client, store) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker(stream_client, GatheredAppendsWorkflow) as worker: - await stream_client.execute_workflow( - GatheredAppendsWorkflow.run, - stream_id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - records = await _read_all(stream_client, stream_id) - assert [record.body for record in records] == ["a", "b", "c"] - assert [request.sequence for request in store.appends] == sorted( - request.sequence for request in store.appends - ) - - -async def test_workflow_close_started_with_an_append_comes_after_it(client: Client): - store = SlowFirstAppendStore() - stream_client = _with_store(client, store) - stream_id = f"stream-{uuid.uuid4()}" - async with new_worker(stream_client, GatheredAppendAndCloseWorkflow) as worker: - await stream_client.execute_workflow( - GatheredAppendAndCloseWorkflow.run, - stream_id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - assert store.operations == ["CreateStream", "AppendRecords", "CloseStream"] - records = await _read_all(stream_client, stream_id) - assert [record.body for record in records] == ["a"] - assert await _describe_status(stream_client, stream_id) == ( - StreamStatus.StreamStatusClosed - ) From 17b70ac6f838065b8155119175f6b18a7837fdbb Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Tue, 17 Nov 2026 15:00:57 -0700 Subject: [PATCH 18/35] Said in the changelog that Workflow code can't call a Stream. The changelog lists only what this step ships. --- changelog/added/chattering-ledger-otter.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/added/chattering-ledger-otter.md b/changelog/added/chattering-ledger-otter.md index 6ef01dd08d..d454ff510f 100644 --- a/changelog/added/chattering-ledger-otter.md +++ b/changelog/added/chattering-ledger-otter.md @@ -1,2 +1,2 @@ -Add the experimental Streams API. Client, Activity and Workflow code create a Stream, append Records to it and close it, and client and Activity code read it. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `temporalio.contrib.redis.streams.RedisStreamStore`, or `temporalio.testing.MemoryStreamStore` in tests. +Add the experimental Streams API. Client and Activity code create a Stream, append Records to it, read it and close it. Workflow code can't call a Stream in this version. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `temporalio.contrib.redis.streams.RedisStreamStore`, or `temporalio.testing.MemoryStreamStore` in tests. Add `temporalio.contrib.streams.current_producer` and `as_producer`. An Activity can store its Producer in its heartbeat details and pass it to the appends of its retry. The retry's Records then continue the first attempt's epoch, so a reader does not take them as a replacement of the first attempt's Records. From a377a3f53102fefbefa850aac46572e729a99b6e Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Tue, 17 Nov 2026 18:28:00 -0700 Subject: [PATCH 19/35] Scheduled the Stream calls of Workflow code without the generic hook. The regenerated Stream terminal calls `_schedule_system_nexus_operation`, as upstream nexgen does. An interceptor of Workflow code then sees a Stream operation only in its operation-specific method. --- changelog/added/chattering-ledger-otter.md | 1 + temporalio/worker/_interceptor.py | 6 +++++- temporalio/worker/_workflow_instance.py | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/changelog/added/chattering-ledger-otter.md b/changelog/added/chattering-ledger-otter.md index d454ff510f..0735dbdff1 100644 --- a/changelog/added/chattering-ledger-otter.md +++ b/changelog/added/chattering-ledger-otter.md @@ -1,2 +1,3 @@ Add the experimental Streams API. Client and Activity code create a Stream, append Records to it, read it and close it. Workflow code can't call a Stream in this version. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `temporalio.contrib.redis.streams.RedisStreamStore`, or `temporalio.testing.MemoryStreamStore` in tests. Add `temporalio.contrib.streams.current_producer` and `as_producer`. An Activity can store its Producer in its heartbeat details and pass it to the appends of its retry. The retry's Records then continue the first attempt's epoch, so a reader does not take them as a replacement of the first attempt's Records. +A Workflow outbound interceptor sees a Stream operation only in its operation-specific method, such as `start_append_records`, and not in `start_system_nexus_operation`. An unknown Stream operation raises `ValueError`. diff --git a/temporalio/worker/_interceptor.py b/temporalio/worker/_interceptor.py index 9cfab54022..d5d49e1ba9 100644 --- a/temporalio/worker/_interceptor.py +++ b/temporalio/worker/_interceptor.py @@ -535,7 +535,11 @@ async def start_nexus_operation( async def start_system_nexus_operation( self, input: StartNexusOperationInput[InputT, OutputT] ) -> temporalio.workflow.NexusOperationHandle[OutputT]: - """Intercept a Temporal System Nexus operation started by a workflow.""" + """Intercept a Temporal System Nexus operation started by a workflow. + + A Stream operation does not reach this method. Its operation-specific method, such as + ``start_append_records``, intercepts it. + """ return await self.next.start_system_nexus_operation(input) diff --git a/temporalio/worker/_workflow_instance.py b/temporalio/worker/_workflow_instance.py index bd01c67c3b..b4629a293a 100644 --- a/temporalio/worker/_workflow_instance.py +++ b/temporalio/worker/_workflow_instance.py @@ -3429,6 +3429,11 @@ async def start_system_nexus_operation( ) -> temporalio.workflow.NexusOperationHandle[OutputT]: return await self._instance._schedule_system_nexus_operation(input) + async def _schedule_system_nexus_operation( + self, input: StartNexusOperationInput[InputT, OutputT] + ) -> temporalio.workflow.NexusOperationHandle[OutputT]: + return await self._instance._schedule_system_nexus_operation(input) + def start_local_activity( self, input: StartLocalActivityInput ) -> temporalio.workflow.ActivityHandle[Any]: From 1b73059ae26ccd90726635fbae44e3dfcf651506 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Tue, 17 Nov 2026 18:28:50 -0700 Subject: [PATCH 20/35] Fitted the Stream reads to the new names of a polled read. A loop over Records uses `read`, a loop over pages uses `read_pages`, and one request uses `read_page`. The Stream package is generated from commit 07b565b6 of the nexgen fork. --- .github/workflows/ci.yml | 5 +++-- scripts/gen_nexus_system_api.py | 8 +++++--- tests/contrib/redis/test_streams.py | 12 ++++++------ tests/contrib/streams/test_appender.py | 4 ++-- tests/contrib/streams/test_producer.py | 2 +- tests/nexus/test_stream_store.py | 24 +++++++++++------------ tests/nexus/test_stream_store_workflow.py | 2 +- 7 files changed, 30 insertions(+), 27 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c60bb33161..a92b7d7c9d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -228,8 +228,9 @@ jobs: - run: uv tool install poethepoet - run: uv sync --all-extras - run: cargo install --locked nexgen --version 0.2.8 --features advanced --force - # The Stream package needs the `main` of nexgen, which no release has yet. - - run: cargo install --locked --git https://github.com/temporalio/nexgen --rev 9653a0f5a845df0c5059554f1b5813db6a74cd1a --features advanced --root "$RUNNER_TEMP/nexgen-stream" nexgen + # The Stream package needs the read, read pages and read page methods of a poll, which only + # a nexgen fork has. + - run: cargo install --locked --git https://github.com/moetemp/nexgen --rev 07b565b6d4339dc8a3f67ce2e9e72ba4925d7f9c --features advanced --root "$RUNNER_TEMP/nexgen-stream" nexgen - run: echo "NEX_GEN_STREAM_BIN=$RUNNER_TEMP/nexgen-stream/bin/nexgen" >> "$GITHUB_ENV" - run: poe build-develop - run: poe gen-protos diff --git a/scripts/gen_nexus_system_api.py b/scripts/gen_nexus_system_api.py index 48998fc7c8..fdf38fa90c 100644 --- a/scripts/gen_nexus_system_api.py +++ b/scripts/gen_nexus_system_api.py @@ -28,8 +28,9 @@ workflow_init_path = base_dir / "temporalio" / "workflow" / "__init__.py" NEX_GEN_VERSION = "0.2.8" # The Stream package needs a newer nexgen, which rejects the other WITs. One pin is enough -# again once the api WITs and a nexgen release agree. No release has nexgen commit 9653a0f5, -# the `main` of nexgen, yet, so NEX_GEN_STREAM_BIN points at a build of it. +# again once the api WITs and a nexgen release agree. The read, read pages and read page methods +# of a poll are only in commit 07b565b6 of the fork https://github.com/moetemp/nexgen. So +# NEX_GEN_STREAM_BIN points at a build of that commit. NEX_GEN_STREAM_VERSION = "0.2.12" proto_files = [ gen_protos.api_proto_dir @@ -87,7 +88,8 @@ def nex_gen_stream_command() -> list[str]: bin_path = os.environ.get("NEX_GEN_STREAM_BIN") if not bin_path: raise RuntimeError( - "NEX_GEN_STREAM_BIN must point at a nexgen build of commit 9653a0f5" + "NEX_GEN_STREAM_BIN must point at a nexgen build of commit 07b565b6 " + "of https://github.com/moetemp/nexgen" ) version = subprocess.check_output([bin_path, "--version"], text=True).strip() if version != f"nexgen {NEX_GEN_STREAM_VERSION}": diff --git a/tests/contrib/redis/test_streams.py b/tests/contrib/redis/test_streams.py index 0e504b0868..0276cd8f48 100644 --- a/tests/contrib/redis/test_streams.py +++ b/tests/contrib/redis/test_streams.py @@ -97,7 +97,7 @@ async def test_client_creates_appends_reads_and_closes(client: Client): appended = await stream.append("a", "b") assert appended.first_offset is not None assert appended.last_offset is not None - records = [record async for record in stream.iter_read_items()] + records = [record async for record in stream.read()] assert [(record.body, record.offset) for record in records] == [ ("a", appended.first_offset), ("b", appended.last_offset), @@ -107,7 +107,7 @@ async def test_client_creates_appends_reads_and_closes(client: Client): described = await stream.describe() assert described.status == StreamStatus.StreamStatusClosed assert described.last_offset == appended.last_offset - page = await stream.read(from_offset=appended.last_offset) + page = await stream.read_page(from_offset=appended.last_offset) assert page.done assert page.records == [] @@ -126,7 +126,7 @@ async def test_local_activity_appends_with_local_activity_producer(client: Clien ) await handle.result() - records = [record async for record in stream.iter_read_items()] + records = [record async for record in stream.read()] assert [record.body for record in records] == ["a", "b"] run_id = handle.result_run_id for record in records: @@ -149,7 +149,7 @@ async def test_activity_appends_with_activity_producer(client: Client): ) await handle.result() - [record] = [record async for record in stream.iter_read_items()] + [record] = [record async for record in stream.read()] assert record.body == "from-activity" assert record.producer is not None assert isinstance(record.producer.kind, ProducerKindWorkflowActivity) @@ -162,7 +162,7 @@ async def test_client_append_with_sequence_writes_once(client: Client): first = await stream.append("a", "b", producer=producer, sequence=5) second = await stream.append("a", "b", producer=producer, sequence=5) assert second == first - records = [record async for record in stream.iter_read_items()] + records = [record async for record in stream.read()] assert [record.body for record in records] == ["a", "b"] assert [record.offset for record in records] == [ first.first_offset, @@ -199,7 +199,7 @@ async def append_later() -> None: await stream.append("late") append = asyncio.create_task(append_later()) - page = await stream.read(wait=timedelta(seconds=10)) + page = await stream.read_page(wait=timedelta(seconds=10)) await append assert [record.body for record in page.records] == ["late"] diff --git a/tests/contrib/streams/test_appender.py b/tests/contrib/streams/test_appender.py index a864335387..6115ab5a15 100644 --- a/tests/contrib/streams/test_appender.py +++ b/tests/contrib/streams/test_appender.py @@ -37,7 +37,7 @@ async def before_call(self, operation: str, request: Message) -> None: async def _bodies(stream: Stream[str]) -> list[str]: - return [record.body async for record in stream.iter_read_items()] + return [record.body async for record in stream.read()] async def test_appends_inside_one_delay_go_as_one_batch(client: Client): @@ -98,7 +98,7 @@ async def test_the_timer_alone_writes_the_batch(client: Client): stream = await _create_stream(client, store) appender = BufferedAppender(stream, max_delay=timedelta(milliseconds=10)) appender.append("a", "b") - page = await stream.read(wait=timedelta(seconds=10)) + page = await stream.read_page(wait=timedelta(seconds=10)) assert [record.body for record in page.records] == ["a", "b"] assert store.batches == [2] diff --git a/tests/contrib/streams/test_producer.py b/tests/contrib/streams/test_producer.py index e579a2042b..a452c03882 100644 --- a/tests/contrib/streams/test_producer.py +++ b/tests/contrib/streams/test_producer.py @@ -88,7 +88,7 @@ def _producer_of_each_kind() -> dict[str, Producer]: async def _records(stream: Stream[str]) -> list[StreamRecord[str]]: - return [record async for record in stream.iter_read_items()] + return [record async for record in stream.read()] async def test_current_producer_in_other_code_is_the_client_producer(client: Client): diff --git a/tests/nexus/test_stream_store.py b/tests/nexus/test_stream_store.py index 75cda17c87..a81b2caa11 100644 --- a/tests/nexus/test_stream_store.py +++ b/tests/nexus/test_stream_store.py @@ -86,7 +86,7 @@ async def _create_stream(client: Client, store: StreamStore) -> Stream[str]: async def _only_producer(stream: Stream[str]) -> Producer: - [record] = [record async for record in stream.iter_read_items()] + [record] = [record async for record in stream.read()] assert record.producer is not None return record.producer @@ -125,14 +125,14 @@ async def test_client_stream_uses_store(client: Client): assert appended.first_offset is not None assert appended.last_offset is not None assert appended.first_offset < appended.last_offset - records = [record async for record in stream.iter_read_items()] + records = [record async for record in stream.read()] assert [(record.body, record.offset) for record in records] == [ ("a", appended.first_offset), ("b", appended.last_offset), ] other = stream_client.get_stream(stream.ref, body_type=str) - assert [record.body async for record in other.iter_read_items()] == ["a", "b"] + assert [record.body async for record in other.read()] == ["a", "b"] described = await stream.describe() assert described.status == StreamStatus.StreamStatusOpen @@ -142,10 +142,10 @@ async def test_client_stream_uses_store(client: Client): described = await stream.describe() assert described.status == StreamStatus.StreamStatusClosed assert described.last_offset == appended.last_offset - page = await stream.read(from_offset=records[-1].offset) + page = await stream.read_page(from_offset=records[-1].offset) assert page.done assert page.records == [] - pages = [page async for page in stream.iter_read()] + pages = [page async for page in stream.read_pages()] assert pages[-1].done assert [record.body for page in pages for record in page.records] == ["a", "b"] @@ -162,7 +162,7 @@ async def test_client_stream_uses_a_store_written_from_scratch(client: Client): assert stream.created assert stream.ref.store == "dict" appended = await stream.append("a", "b") - assert [record.body async for record in stream.iter_read_items()] == ["a", "b"] + assert [record.body async for record in stream.read()] == ["a", "b"] # The Store gets payloads that the client already encoded with the codec. stored = store.streams[stream_id].records assert [record.body.metadata["simple-codec"] for record in stored] == [ @@ -220,7 +220,7 @@ async def test_client_read_or_describe_of_unknown_stream_fails(client: Client): stream = stream_client.get_stream( StreamRef(id=f"stream-{uuid.uuid4()}"), body_type=str ) - for call in (stream.read(), stream.describe()): + for call in (stream.read_page(), stream.describe()): err = await _stream_error(call) assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_NOT_FOUND @@ -249,9 +249,9 @@ async def test_client_stream_store_uses_payload_codec(client: Client): raw = _with_store(client, store).get_stream( StreamRef(id=stream.ref.id), body_type=temporalio.common.RawValue ) - stored = [record.body.payload async for record in raw.iter_read_items()] + stored = [record.body.payload async for record in raw.read()] assert [body.metadata["simple-codec"] for body in stored] == [b"true", b"true"] - assert [record.body async for record in stream.iter_read_items()] == ["a", "b"] + assert [record.body async for record in stream.read()] == ["a", "b"] async def test_client_append_has_client_producer(client: Client): @@ -367,7 +367,7 @@ async def test_client_append_with_sequence_writes_once(client: Client): first = await stream.append("a", "b", producer=producer, sequence=5) second = await stream.append("a", "b", producer=producer, sequence=5) assert second == first - records = [record async for record in stream.iter_read_items()] + records = [record async for record in stream.read()] assert [record.body for record in records] == ["a", "b"] assert [record.offset for record in records] == [ first.first_offset, @@ -497,7 +497,7 @@ async def test_client_create_with_store_on_several_stores_needs_no_selector( assert stream.ref.store == "second" await stream.append("a") in_second = _with_store(client, second).get_stream(stream.ref, body_type=str) - assert [record.body async for record in in_second.iter_read_items()] == ["a"] + assert [record.body async for record in in_second.read()] == ["a"] in_first = _with_store(client, first).get_stream( StreamRef(id=stream.ref.id), body_type=str ) @@ -544,7 +544,7 @@ def select(context: StreamSelectContext) -> StreamStore: # A call through a ref that names its Store doesn't ask the selector. assert len(contexts) == 1 in_second = _with_store(client, second).get_stream(stream.ref, body_type=str) - assert [record.body async for record in in_second.iter_read_items()] == ["a"] + assert [record.body async for record in in_second.read()] == ["a"] in_first = _with_store(client, first).get_stream( StreamRef(id=stream_id), body_type=str ) diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index 6ab541d0d5..4e594d44ec 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -119,7 +119,7 @@ async def run(self, stream_id: str) -> None: async def _read_all(client: Client, stream_id: str) -> list[StreamRecord[str]]: stream = client.get_stream(StreamRef(id=stream_id), body_type=str) - return [record async for record in stream.iter_read_items()] + return [record async for record in stream.read()] def _local_activity_epoch(producer: Producer) -> tuple[int, int, int]: From d4e05f4a8d82ca25724f3d4b9bea383a5ac01b8f Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Wed, 18 Nov 2026 01:32:14 -0700 Subject: [PATCH 21/35] Moved the Redis Stream Store out of the bridge. The Store is now the `temporalio-streams-redis` package, so the bridge has no code for one Store only. A user's Store has the same path as this one. --- .github/actions/install-redis/action.yml | 25 --- .github/workflows/ci.yml | 2 - changelog/added/chattering-ledger-otter.md | 2 +- pyproject.toml | 12 +- temporalio/bridge/Cargo.lock | 129 ----------- temporalio/bridge/Cargo.toml | 18 +- temporalio/bridge/src/lib.rs | 46 ---- temporalio/bridge/src/streams.rs | 67 +----- temporalio/bridge/streams.py | 46 +--- temporalio/client/_stream_store.py | 6 +- temporalio/contrib/redis/__init__.py | 1 - temporalio/contrib/redis/streams.py | 118 ---------- temporalio/exceptions.py | 2 +- tests/contrib/redis/__init__.py | 0 tests/contrib/redis/test_streams.py | 238 --------------------- tests/nexus/test_stream_store.py | 1 - 16 files changed, 17 insertions(+), 696 deletions(-) delete mode 100644 .github/actions/install-redis/action.yml delete mode 100644 temporalio/contrib/redis/__init__.py delete mode 100644 temporalio/contrib/redis/streams.py delete mode 100644 tests/contrib/redis/__init__.py delete mode 100644 tests/contrib/redis/test_streams.py diff --git a/.github/actions/install-redis/action.yml b/.github/actions/install-redis/action.yml deleted file mode 100644 index de0a2d6390..0000000000 --- a/.github/actions/install-redis/action.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: Install Redis -description: Install and start a Redis server for the tests of the Redis Stream Store. -runs: - using: composite - steps: - - if: runner.os == 'Linux' - shell: bash - run: | - sudo apt-get update - sudo apt-get install -y --no-install-recommends redis-server - - if: runner.os == 'macOS' - shell: bash - run: brew install redis - # Redis has no official Windows build, so the Redis Store tests skip there. The port is not the - # default one, since the Linux package starts its own server on 6379. - - if: runner.os != 'Windows' - shell: bash - run: | - redis-server --port 6380 --save "" --appendonly no --daemonize yes - for _ in $(seq 1 50); do - redis-cli -p 6380 ping && break - sleep 0.1 - done - redis-cli -p 6380 ping - echo "STREAMS_REDIS_URL=redis://127.0.0.1:6380" >> "$GITHUB_ENV" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a92b7d7c9d..12e3b8e566 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,8 +58,6 @@ jobs: if: ${{ matrix.clippyLinter }} - run: poe build-develop - run: poe lint - - name: Install and start Redis for the Redis Stream Store tests - uses: ./.github/actions/install-redis - run: mkdir junit-xml - run: poe test ${{matrix.pytestExtraArgs}} -s --junit-xml=junit-xml/${{ matrix.python }}--${{ matrix.os }}.xml timeout-minutes: 20 diff --git a/changelog/added/chattering-ledger-otter.md b/changelog/added/chattering-ledger-otter.md index 0735dbdff1..c62ec20425 100644 --- a/changelog/added/chattering-ledger-otter.md +++ b/changelog/added/chattering-ledger-otter.md @@ -1,3 +1,3 @@ -Add the experimental Streams API. Client and Activity code create a Stream, append Records to it, read it and close it. Workflow code can't call a Stream in this version. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `temporalio.contrib.redis.streams.RedisStreamStore`, or `temporalio.testing.MemoryStreamStore` in tests. +Add the experimental Streams API. Client and Activity code create a Stream, append Records to it, read it and close it. Workflow code can't call a Stream in this version. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `RedisStreamStore` of the `temporalio-streams-redis` package, or `temporalio.testing.MemoryStreamStore` in tests. Add `temporalio.contrib.streams.current_producer` and `as_producer`. An Activity can store its Producer in its heartbeat details and pass it to the appends of its retry. The retry's Records then continue the first attempt's epoch, so a reader does not take them as a replacement of the first attempt's Records. A Workflow outbound interceptor sees a Stream operation only in its operation-specific method, such as `start_append_records`, and not in `start_system_nexus_operation`. An unknown Stream operation raises `ValueError`. diff --git a/pyproject.toml b/pyproject.toml index 8c32f20eb7..d3f4f12454 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -93,8 +93,8 @@ dev = [ ] [tool.poe.tasks] -build-develop = "uv run maturin develop --uv --features streams-redis" -build-develop-with-release = { cmd = "uv run maturin develop --release --uv --features streams-redis" } +build-develop = "uv run maturin develop --uv --features streams" +build-develop-with-release = { cmd = "uv run maturin develop --release --uv --features streams" } # FIPS builds: swap the rustls stack onto aws-lc-rs FIPS mode (aws-lc-fips-sys), eliminating `ring`. build-develop-fips = { cmd = "uv run maturin develop --uv --no-default-features --features fips", env = { TEMPORALIO_FIPS = "1" } } build-wheel-fips = { cmd = "uv run maturin build --release --no-default-features --features fips", env = { TEMPORALIO_FIPS = "1" } } @@ -154,19 +154,19 @@ before-all = "pip install protoc-wheel-0" build = "cp311-win_amd64 cp311-manylinux_x86_64 cp311-manylinux_aarch64 cp311-musllinux_x86_64 cp311-musllinux_aarch64 cp311-macosx_x86_64 cp311-macosx_arm64" build-verbosity = 1 # The official wheels link the Stream runtime. -environment = { MATURIN_PEP517_ARGS = "--features streams-redis" } +environment = { MATURIN_PEP517_ARGS = "--features streams" } [tool.cibuildwheel.macos] -environment = { MACOSX_DEPLOYMENT_TARGET = "10.12", MATURIN_PEP517_ARGS = "--features streams-redis" } +environment = { MACOSX_DEPLOYMENT_TARGET = "10.12", MATURIN_PEP517_ARGS = "--features streams" } [tool.cibuildwheel.linux] before-all = "sh scripts/cibuildwheel_before_all_linux.sh" before-build = "sh scripts/cibuildwheel_before_build_linux.sh" -environment = { PATH = "$PATH:$HOME/.cargo/bin", CARGO_NET_GIT_FETCH_WITH_CLI = "true", MATURIN_PEP517_ARGS = "--features streams-redis" } +environment = { PATH = "$PATH:$HOME/.cargo/bin", CARGO_NET_GIT_FETCH_WITH_CLI = "true", MATURIN_PEP517_ARGS = "--features streams" } [[tool.cibuildwheel.overrides]] select = "*musllinux*" -environment = { PATH = "$PATH:$HOME/.cargo/bin", CARGO_NET_GIT_FETCH_WITH_CLI = "true", RUSTFLAGS = "-C target-feature=-crt-static", MATURIN_PEP517_ARGS = "--features streams-redis" } +environment = { PATH = "$PATH:$HOME/.cargo/bin", CARGO_NET_GIT_FETCH_WITH_CLI = "true", RUSTFLAGS = "-C target-feature=-crt-static", MATURIN_PEP517_ARGS = "--features streams" } [tool.mypy] ignore_missing_imports = true diff --git a/temporalio/bridge/Cargo.lock b/temporalio/bridge/Cargo.lock index 064e7b3694..e2a45f5d20 100644 --- a/temporalio/bridge/Cargo.lock +++ b/temporalio/bridge/Cargo.lock @@ -29,32 +29,6 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" -[[package]] -name = "arc-swap" -version = "1.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84a22b5d51209f6de83d37a0b33d320acb8da88823e42a94c075140708f032a5" -dependencies = [ - "rustversion", -] - -[[package]] -name = "arcstr" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03918c3dbd7701a85c6b9887732e2921175f26c350b4563841d0958c21d57e6d" - -[[package]] -name = "async-lock" -version = "3.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0876700b0d007ad25d5508865a4259c3af9af6d296bf663a0d586d0bc3369f6f" -dependencies = [ - "event-listener", - "event-listener-strategy", - "pin-project-lite", -] - [[package]] name = "async-trait" version = "0.1.92" @@ -341,11 +315,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4356eba6976435498d05a311ee9fc486da3bc81c200d3f0d7dd9703b41913f92" dependencies = [ "bytes", - "futures-core", "memchr", - "pin-project-lite", - "tokio", - "tokio-util", ] [[package]] @@ -400,12 +370,6 @@ dependencies = [ "libc", ] -[[package]] -name = "crc16" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "338089f42c427b86394a5ee60ff321da23a5c89c9d89514c829687b26359fcff" - [[package]] name = "crc32fast" version = "1.5.5" @@ -622,26 +586,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "event-listener" -version = "5.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a23add41df1562121a9393cb065eab5146a1242410f23a644851e90cfd669d2" -dependencies = [ - "parking", - "pin-project-lite", -] - -[[package]] -name = "event-listener-strategy" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8be9f3dfaaffdae2972880079a491a1a8bb7cbed0b8dd7a347f668b4150a3b93" -dependencies = [ - "event-listener", - "pin-project-lite", -] - [[package]] name = "fastrand" version = "2.5.0" @@ -1605,12 +1549,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" -[[package]] -name = "parking" -version = "2.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" - [[package]] name = "parking_lot" version = "0.12.5" @@ -2151,39 +2089,6 @@ dependencies = [ "rand_core 0.10.1", ] -[[package]] -name = "redis" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db7f3d75b5a3301746bff39a767bf4c5c42506b08513527308b3875bfb64a031" -dependencies = [ - "arc-swap", - "arcstr", - "async-lock", - "backon", - "bytes", - "cfg-if", - "combine", - "crc16", - "futures-channel", - "futures-util", - "itoa", - "log", - "percent-encoding", - "pin-project-lite", - "rand 0.10.3", - "rustls", - "rustls-native-certs", - "ryu", - "sha1_smol", - "socket2", - "tokio", - "tokio-rustls", - "tokio-util", - "url", - "xxhash-rust", -] - [[package]] name = "redox_syscall" version = "0.5.18" @@ -2533,12 +2438,6 @@ dependencies = [ "serde", ] -[[package]] -name = "sha1_smol" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d" - [[package]] name = "sha2" version = "0.10.9" @@ -2754,7 +2653,6 @@ dependencies = [ "temporalio-common", "temporalio-sdk-core", "temporalio-streams", - "temporalio-streams-redis", "tokio", "tokio-rustls", "tokio-stream", @@ -2953,27 +2851,6 @@ dependencies = [ "tonic", ] -[[package]] -name = "temporalio-streams-redis" -version = "0.1.0" -source = "git+https://github.com/temporal-community/sdk-streams?rev=0800f33196fd82e05fee6c2741cf788b6d4335d9#0800f33196fd82e05fee6c2741cf788b6d4335d9" -dependencies = [ - "async-trait", - "aws-lc-rs", - "crc16", - "parking_lot", - "prost", - "prost-wkt-types", - "redis", - "temporalio-common", - "temporalio-streams", - "thiserror", - "tokio", - "tonic-prost-build", - "tracing", - "url", -] - [[package]] name = "termtree" version = "0.5.1" @@ -3835,12 +3712,6 @@ dependencies = [ "rustix", ] -[[package]] -name = "xxhash-rust" -version = "0.8.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "550a2b930b62486a393c52d5c3b84bff264b28aa437ed64694d31e93b1757af7" - [[package]] name = "yoke" version = "0.8.4" diff --git a/temporalio/bridge/Cargo.toml b/temporalio/bridge/Cargo.toml index 5d48965de6..7c4fc4e9fa 100644 --- a/temporalio/bridge/Cargo.toml +++ b/temporalio/bridge/Cargo.toml @@ -29,21 +29,16 @@ default = ["tls-ring"] tls-ring = [ "temporalio-sdk-core/tls-ring", "temporalio-client/tls-ring", - "temporalio-streams-redis?/tls-ring", "tokio-rustls/ring", ] tls-aws-lc = [ "temporalio-sdk-core/tls-aws-lc", "temporalio-client/tls-aws-lc", - "temporalio-streams-redis?/tls-aws-lc", "tokio-rustls/aws_lc_rs", ] -fips = ["tls-aws-lc", "dep:aws-lc-rs", "streams-redis", "temporalio-streams-redis/fips"] -# Links the Stream runtime and its memory Store. Off by default. +fips = ["tls-aws-lc", "dep:aws-lc-rs", "streams"] +# Links the Stream runtime and its memory Store. Off by default. The official builds turn it on. streams = ["dep:temporalio-streams"] -# Adds the Redis Store and turns on `streams`. The official builds turn it on. Cargo fetches the -# git dependency below for every build, with this feature off too. -streams-redis = ["streams", "dep:temporalio-streams-redis"] [dependencies] anyhow = "1.0" @@ -81,8 +76,6 @@ temporalio-sdk-core = { version = "0.11.0", path = "./sdk-core/crates/sdk-core", ] } # The Stream runtime and its memory Store, from the submodule. temporalio-streams = { path = "./sdk-core/crates/streams", optional = true } -# The Redis Store, which lives in its own repo. -temporalio-streams-redis = { git = "https://github.com/temporal-community/sdk-streams", rev = "0800f33196fd82e05fee6c2741cf788b6d4335d9", default-features = false, optional = true } tokio = "1.26" # Matches the sdk-core client crate's rustls stack; used to build the custom # server certificate verifier for ClientTlsConfig.verification_server_name. @@ -95,13 +88,6 @@ tonic = "0.14" tracing = "0.1" url = "2.2" -# The Redis crate takes temporalio-common and temporalio-streams from crates.io. This makes it use -# the submodule's crates, so the bridge sees one copy of each type. The versions must be semver -# compatible. -[patch.crates-io] -temporalio-common = { path = "./sdk-core/crates/common" } -temporalio-streams = { path = "./sdk-core/crates/streams" } - [profile.release] opt-level = 3 debug = false diff --git a/temporalio/bridge/src/lib.rs b/temporalio/bridge/src/lib.rs index ebe9fef7de..5781b15dcc 100644 --- a/temporalio/bridge/src/lib.rs +++ b/temporalio/bridge/src/lib.rs @@ -55,8 +55,6 @@ fn temporal_sdk_bridge(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { { m.add_class::()?; m.add_function(wrap_pyfunction!(new_memory_stream_runtime, m)?)?; - #[cfg(feature = "streams-redis")] - m.add_function(wrap_pyfunction!(new_redis_stream_runtime, m)?)?; } // Testing stuff @@ -127,50 +125,6 @@ fn new_memory_stream_runtime( streams::new_memory_stream_runtime(runtime_ref, store_name, retention, producer_idle_ttl) } -#[cfg(feature = "streams-redis")] -#[pyfunction] -#[pyo3(signature = ( - runtime_ref, - urls, - *, - cluster, - key_prefix, - retention, - producer_idle_ttl, - response_timeout, - store_name, - wait_replicas, - wait_timeout, -))] -#[allow(clippy::too_many_arguments)] -fn new_redis_stream_runtime( - runtime_ref: &runtime::RuntimeRef, - urls: Vec, - cluster: bool, - key_prefix: Option, - retention: Option, - producer_idle_ttl: Option, - response_timeout: Option, - store_name: String, - wait_replicas: i32, - wait_timeout: Option, -) -> PyResult { - streams::new_redis_stream_runtime( - runtime_ref, - streams::RedisStreamOptions { - urls, - cluster, - key_prefix, - retention, - producer_idle_ttl, - response_timeout, - store_name, - wait_replicas, - wait_timeout, - }, - ) -} - #[pyfunction] fn start_dev_server<'a>( py: Python<'a>, diff --git a/temporalio/bridge/src/streams.rs b/temporalio/bridge/src/streams.rs index 5eb56cc724..fff45180a2 100644 --- a/temporalio/bridge/src/streams.rs +++ b/temporalio/bridge/src/streams.rs @@ -1,6 +1,6 @@ -//! Bindings to the Stream runtime of the `temporalio-streams` crate, on the memory Store or, with -//! the `streams-redis` feature, on the Redis Store of `temporalio-streams-redis`. Python calls the -//! five Stream service operations by name, with the encoded request, and gets the encoded response. +//! Bindings to the Stream runtime of the `temporalio-streams` crate, on the memory Store. Python +//! calls the five Stream service operations by name, with the encoded request, and gets the encoded +//! response. use pyo3::exceptions::PyException; #[cfg(feature = "streams")] @@ -67,64 +67,3 @@ pub fn new_memory_stream_runtime( ); Ok(StreamRuntimeRef { streams, runtime }) } - -/// The options of the Redis Store, as Python passes them. A `None` takes the crate's default. -#[cfg(feature = "streams-redis")] -pub struct RedisStreamOptions { - pub urls: Vec, - pub cluster: bool, - pub key_prefix: Option, - pub retention: Option, - pub producer_idle_ttl: Option, - pub response_timeout: Option, - pub store_name: String, - pub wait_replicas: i32, - pub wait_timeout: Option, -} - -/// Builds the runtime on Redis. It connects at its first call, so a server that is down fails that -/// call and not this one. A bad option raises `ValueError` with the crate's message. -#[cfg(feature = "streams-redis")] -pub fn new_redis_stream_runtime( - runtime_ref: &runtime::RuntimeRef, - options: RedisStreamOptions, -) -> PyResult { - use pyo3::exceptions::PyValueError; - use temporalio_streams_redis::{proto::RedisStreamConfig, RedisStreamRuntime, RuntimeOptions}; - - let runtime = runtime_ref.runtime.clone(); - runtime.assert_same_process("create a Stream runtime")?; - // The crate reads an empty or zero field as its default, so `None` maps to that. - let config = RedisStreamConfig { - urls: options.urls, - cluster: options.cluster, - key_prefix: options.key_prefix.unwrap_or_default(), - retention: proto_duration("retention", options.retention)?, - producer_idle_ttl: proto_duration("producer_idle_ttl", options.producer_idle_ttl)?, - response_timeout: proto_duration("response_timeout", options.response_timeout)?, - store_name: options.store_name, - wait_replicas: options.wait_replicas, - wait_timeout: proto_duration("wait_timeout", options.wait_timeout)?, - ..Default::default() - }; - let options = - RuntimeOptions::try_from(config).map_err(|err| PyValueError::new_err(err.to_string()))?; - // The runtime can spawn Tokio tasks, which need an entered Tokio runtime. - let _guard = runtime.core.tokio_handle().enter(); - let meter = runtime.core.telemetry().get_temporal_metric_meter(); - let streams = RedisStreamRuntime::create(options, meter); - Ok(StreamRuntimeRef { streams, runtime }) -} - -// The config's durations are the `prost-wkt-types` type that the Redis crate's protos use. The -// bridge doesn't depend on that crate, so the field's type picks the conversion. -#[cfg(feature = "streams-redis")] -fn proto_duration(field: &str, value: Option) -> PyResult> -where - T: TryFrom, - T::Error: std::fmt::Display, -{ - value.map(T::try_from).transpose().map_err(|err| { - pyo3::exceptions::PyValueError::new_err(format!("The {field} is too long: {err}")) - }) -} diff --git a/temporalio/bridge/streams.py b/temporalio/bridge/streams.py index 221c3d0d67..66c4034dd4 100644 --- a/temporalio/bridge/streams.py +++ b/temporalio/bridge/streams.py @@ -5,7 +5,6 @@ from __future__ import annotations -from collections.abc import Sequence from datetime import timedelta from typing import Any @@ -42,51 +41,8 @@ def new_memory( ) return StreamRuntime(new(runtime._ref, name, retention, producer_idle_ttl)) - @staticmethod - def new_redis( - runtime: temporalio.bridge.runtime.Runtime, - *, - urls: Sequence[str], - cluster: bool, - key_prefix: str | None, - retention: timedelta | None, - producer_idle_ttl: timedelta | None, - response_timeout: timedelta | None, - name: str, - wait_replicas: int, - wait_timeout: timedelta | None, - ) -> StreamRuntime: - """Build the runtime on Redis. It connects at its first call. - - Raises: - temporalio.exceptions.StreamConfigError: The bridge build has no Redis - Stream Store. - ValueError: An option is bad. - """ - new = getattr( - temporalio.bridge.temporal_sdk_bridge, "new_redis_stream_runtime", None - ) - if new is None: - raise temporalio.exceptions.StreamConfigError( - "This build of temporalio has no Redis Stream Store" - ) - return StreamRuntime( - new( - runtime._ref, - list(urls), - cluster=cluster, - key_prefix=key_prefix, - retention=retention, - producer_idle_ttl=producer_idle_ttl, - response_timeout=response_timeout, - store_name=name, - wait_replicas=wait_replicas, - wait_timeout=wait_timeout, - ) - ) - def __init__(self, ref: Any) -> None: - """Wrap the bridge's runtime object. Use ``new_memory`` or ``new_redis`` instead.""" + """Wrap the bridge's runtime object. Use ``new_memory`` instead.""" self._ref = ref async def call(self, operation: str, request: bytes) -> bytes: diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index c0df1872db..f14c9ee35a 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -27,8 +27,8 @@ class StreamStore(ABC): """A Store that answers the Stream operations of a client. A client with a Store sends each Stream operation to it. This release has no - Streams on the server, so a client needs a Store for any Stream operation. Use - :py:class:`temporalio.contrib.redis.streams.RedisStreamStore`, or + Streams on the server, so a client needs a Store for any Stream operation. Use a + Store of a package, such as ``RedisStreamStore`` of ``temporalio-streams-redis``, or :py:class:`temporalio.testing.MemoryStreamStore` in tests. To keep Streams in another system, write a Store that implements :py:attr:`name` @@ -159,7 +159,7 @@ async def _call_bridge( Raises: temporalio.exceptions.StreamError: The call failed. temporalio.exceptions.StreamConfigError: This build of temporalio has no - Stream runtime, or not this Store. + Stream runtime. """ if self._stream_runtime is None: runtime = self._runtime or temporalio.runtime.Runtime.default() diff --git a/temporalio/contrib/redis/__init__.py b/temporalio/contrib/redis/__init__.py deleted file mode 100644 index 34dad945bf..0000000000 --- a/temporalio/contrib/redis/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Redis integrations for the Temporal SDK.""" diff --git a/temporalio/contrib/redis/streams.py b/temporalio/contrib/redis/streams.py deleted file mode 100644 index 705d1cddb0..0000000000 --- a/temporalio/contrib/redis/streams.py +++ /dev/null @@ -1,118 +0,0 @@ -"""The Redis Stream Store. - -A client takes it in ``stream_config``. The Activities of a Worker on that client reach -the same Store through ``activity.client()``:: - - from temporalio.client import Client, StreamConfig - from temporalio.contrib.redis.streams import RedisStreamStore - - client = await Client.connect( - "localhost:7233", - stream_config=StreamConfig(stores=[RedisStreamStore("redis://localhost:6379")]), - ) - -.. warning:: - This module is experimental and subject to change. -""" - -from __future__ import annotations - -from collections.abc import Sequence -from datetime import timedelta - -import temporalio.bridge.runtime -import temporalio.bridge.streams -import temporalio.client._stream_store -import temporalio.runtime - - -class RedisStreamStore(temporalio.client._stream_store._BridgeStreamStore): - """A Stream Store that keeps its Streams in Redis 7.0 or later. - - It runs the Stream runtime of the bridge on the Redis Store of the - ``temporalio-streams-redis`` crate. It connects at its first call, so building - it needs no server, and a server that is down fails that call. A failed call - raises :py:class:`temporalio.exceptions.StreamError`. - - A key has no namespace, so a Stream id is unique in one Redis and key prefix. - Give each namespace its own key prefix to keep their Streams apart. - - .. warning:: - This API is experimental and subject to change. - """ - - def __init__( - self, - url: str | Sequence[str], - *, - cluster: bool = False, - key_prefix: str | None = None, - retention: timedelta | None = None, - producer_idle_ttl: timedelta | None = None, - response_timeout: timedelta | None = None, - wait_replicas: int = 0, - wait_timeout: timedelta | None = None, - name: str = "redis", - runtime: temporalio.runtime.Runtime | None = None, - ) -> None: - """Create a Redis Store. It builds its Stream runtime and connects at its first call. - - A bad option raises ``ValueError`` at that first call, with the message of the - Redis crate. - - Args: - url: A ``redis://`` or ``rediss://`` URL, or the seed URLs of a cluster. - Credentials and TLS come from the URL. - cluster: Whether the URLs are of a Redis Cluster. - key_prefix: The prefix of every key. ``None`` takes ``temporal-streams``. - A Stream id is unique in one Redis and key prefix. - retention: The default Retention of a new Stream, which is how long the - Stream stays readable after its close. ``None`` takes 7 days. - producer_idle_ttl: How long an open Stream stays after the last write of any - Producer. ``None`` keeps an open Stream and every Record until its - close. - response_timeout: The longest time that one Redis call can take. ``None`` - takes 5 seconds. - wait_replicas: How many replicas must have each write before it answers, - with the Redis ``WAIT`` command. Zero sends no ``WAIT``. - wait_timeout: How long a write waits for its replicas, at most half of - ``response_timeout``. ``None`` takes 1 second. - name: Name of the Store, which the refs of its Streams carry, so that every - process can route a ref that it gets. - runtime: Runtime whose Tokio threads run the Store. ``None`` takes - :py:meth:`temporalio.runtime.Runtime.default` at the first call. - """ - temporalio.client._stream_store._check_not_negative( - retention=retention, - producer_idle_ttl=producer_idle_ttl, - response_timeout=response_timeout, - wait_timeout=wait_timeout, - ) - super().__init__(name=name, runtime=runtime) - self._urls = [url] if isinstance(url, str) else list(url) - self._cluster = cluster - self._key_prefix = key_prefix - self._retention = retention - self._producer_idle_ttl = producer_idle_ttl - self._response_timeout = response_timeout - self._wait_replicas = wait_replicas - self._wait_timeout = wait_timeout - - def _new_stream_runtime( - self, runtime: temporalio.bridge.runtime.Runtime - ) -> temporalio.bridge.streams.StreamRuntime: - return temporalio.bridge.streams.StreamRuntime.new_redis( - runtime, - urls=self._urls, - cluster=self._cluster, - key_prefix=self._key_prefix, - retention=self._retention, - producer_idle_ttl=self._producer_idle_ttl, - response_timeout=self._response_timeout, - name=self._name, - wait_replicas=self._wait_replicas, - wait_timeout=self._wait_timeout, - ) - - -__all__ = ["RedisStreamStore"] diff --git a/temporalio/exceptions.py b/temporalio/exceptions.py index c64e7a8129..23ef532211 100644 --- a/temporalio/exceptions.py +++ b/temporalio/exceptions.py @@ -280,7 +280,7 @@ class StreamConfigError(RuntimeError): * A ref names a Store that the config of the client doesn't have. * The selector returns a Store that isn't one of the config's Stores. * A :py:class:`temporalio.client.StreamStore` is given in ``plugins``. - * This build of temporalio has no Stream runtime, or no Redis Stream Store. + * This build of temporalio has no Stream runtime. .. warning:: This API is experimental and subject to change. diff --git a/tests/contrib/redis/__init__.py b/tests/contrib/redis/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/contrib/redis/test_streams.py b/tests/contrib/redis/test_streams.py deleted file mode 100644 index 0276cd8f48..0000000000 --- a/tests/contrib/redis/test_streams.py +++ /dev/null @@ -1,238 +0,0 @@ -from __future__ import annotations - -import asyncio -import os -import socket -import uuid -from collections.abc import Awaitable -from datetime import timedelta - -import pytest - -from temporalio import activity, workflow -from temporalio.api.enums.v1 import StreamErrorReason -from temporalio.client import Client, StreamConfig, StreamStore -from temporalio.contrib.redis.streams import RedisStreamStore -from temporalio.exceptions import StreamError -from temporalio.nexus.system.stream_service import ( - ClientProducer, - Producer, - ProducerKindClient, - ProducerKindLocalActivity, - ProducerKindWorkflowActivity, - StreamRef, - StreamStatus, -) -from temporalio.nexus.system.stream_service.client import Stream -from tests.helpers import new_worker - -_REDIS_URL = os.environ.get("STREAMS_REDIS_URL") - -pytestmark = pytest.mark.skipif( - not _REDIS_URL, reason="STREAMS_REDIS_URL is not set to a Redis server" -) - - -def _store() -> RedisStreamStore: - assert _REDIS_URL - # Each test has its own key prefix, so that runs on one Redis don't share Streams. - return RedisStreamStore(_REDIS_URL, key_prefix=f"test-{uuid.uuid4()}") - - -def _with_store(client: Client, store: StreamStore) -> Client: - config = client.config() - config["stream_config"] = StreamConfig(stores=[store]) - return Client(**config) - - -async def _create_stream(client: Client) -> Stream[str]: - return await client.create_stream(id=f"stream-{uuid.uuid4()}", body_type=str) - - -async def _stream_error(call: Awaitable[object]) -> StreamError: - with pytest.raises(StreamError) as err: - await call - return err.value - - -@activity.defn -async def append_values(ref: StreamRef[str], values: list[str]) -> None: - await activity.client().get_stream(ref, body_type=str).append(*values) - - -@workflow.defn -class AppendWorkflow: - @workflow.run - async def run(self, ref: StreamRef[str]) -> None: - await workflow.execute_local_activity( - append_values, - args=[ref, ["a", "b"]], - start_to_close_timeout=timedelta(seconds=10), - ) - - -@activity.defn -async def append_in_activity(stream_id: str) -> None: - stream = activity.client().get_stream(StreamRef(id=stream_id), body_type=str) - await stream.append("from-activity") - - -@workflow.defn -class RunActivityWorkflow: - @workflow.run - async def run(self, stream_id: str) -> None: - await workflow.execute_activity( - append_in_activity, - stream_id, - start_to_close_timeout=timedelta(seconds=10), - ) - - -async def test_client_creates_appends_reads_and_closes(client: Client): - stream_client = _with_store(client, _store()) - stream = await _create_stream(stream_client) - assert stream.created - assert stream.ref.store == "redis" - - appended = await stream.append("a", "b") - assert appended.first_offset is not None - assert appended.last_offset is not None - records = [record async for record in stream.read()] - assert [(record.body, record.offset) for record in records] == [ - ("a", appended.first_offset), - ("b", appended.last_offset), - ] - - await stream.close() - described = await stream.describe() - assert described.status == StreamStatus.StreamStatusClosed - assert described.last_offset == appended.last_offset - page = await stream.read_page(from_offset=appended.last_offset) - assert page.done - assert page.records == [] - - -async def test_local_activity_appends_with_local_activity_producer(client: Client): - stream_client = _with_store(client, _store()) - stream = await _create_stream(stream_client) - async with new_worker( - stream_client, AppendWorkflow, activities=[append_values] - ) as worker: - handle = await stream_client.start_workflow( - AppendWorkflow.run, - stream.ref, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - await handle.result() - - records = [record async for record in stream.read()] - assert [record.body for record in records] == ["a", "b"] - run_id = handle.result_run_id - for record in records: - assert record.producer is not None - assert isinstance(record.producer.kind, ProducerKindLocalActivity) - assert record.producer.kind.value.run_id == run_id - - -async def test_activity_appends_with_activity_producer(client: Client): - stream_client = _with_store(client, _store()) - stream = await _create_stream(stream_client) - async with new_worker( - stream_client, RunActivityWorkflow, activities=[append_in_activity] - ) as worker: - handle = await stream_client.start_workflow( - RunActivityWorkflow.run, - stream.ref.id, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - await handle.result() - - [record] = [record async for record in stream.read()] - assert record.body == "from-activity" - assert record.producer is not None - assert isinstance(record.producer.kind, ProducerKindWorkflowActivity) - assert record.producer.kind.value.run_id == handle.result_run_id - - -async def test_client_append_with_sequence_writes_once(client: Client): - stream = await _create_stream(_with_store(client, _store())) - producer = Producer(id="mine", kind=ProducerKindClient(ClientProducer())) - first = await stream.append("a", "b", producer=producer, sequence=5) - second = await stream.append("a", "b", producer=producer, sequence=5) - assert second == first - records = [record async for record in stream.read()] - assert [record.body for record in records] == ["a", "b"] - assert [record.offset for record in records] == [ - first.first_offset, - first.last_offset, - ] - - -async def test_failed_calls_raise_stream_error_with_reason(client: Client): - stream_client = _with_store(client, _store()) - stream = await _create_stream(stream_client) - - err = await _stream_error( - stream_client.create_stream(id=stream.ref.id, body_type=str) - ) - assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_ALREADY_EXISTS - - unknown = stream_client.get_stream( - StreamRef(id=f"stream-{uuid.uuid4()}"), body_type=str - ) - err = await _stream_error(unknown.describe()) - assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_NOT_FOUND - - await stream.close() - err = await _stream_error(stream.append("a")) - assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_CLOSED - - -async def test_read_with_wait_gets_a_later_record(client: Client): - stream_client = _with_store(client, _store()) - stream = await _create_stream(stream_client) - - async def append_later() -> None: - await asyncio.sleep(0.5) - await stream.append("late") - - append = asyncio.create_task(append_later()) - page = await stream.read_page(wait=timedelta(seconds=10)) - await append - assert [record.body for record in page.records] == ["late"] - - -async def test_key_prefixes_keep_streams_apart(client: Client): - first = _with_store(client, _store()) - second = _with_store(client, _store()) - stream = await _create_stream(first) - await stream.append("a") - - other = second.get_stream(StreamRef(id=stream.ref.id), body_type=str) - err = await _stream_error(other.describe()) - assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_NOT_FOUND - created = await second.create_stream(id=stream.ref.id, body_type=str) - assert created.created - - -def _free_port() -> int: - with socket.socket() as sock: - sock.bind(("127.0.0.1", 0)) - return sock.getsockname()[1] - - -async def test_store_without_server_fails_at_first_call(client: Client): - store = RedisStreamStore(f"redis://127.0.0.1:{_free_port()}") - stream_client = _with_store(client, store) - err = await _stream_error(_create_stream(stream_client)) - assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_STORAGE_UNAVAILABLE - assert not err.non_retryable - - -async def test_bad_option_fails_with_the_crate_message(client: Client): - assert _REDIS_URL - store = RedisStreamStore([_REDIS_URL, _REDIS_URL]) - with pytest.raises(ValueError, match="cluster"): - await _create_stream(_with_store(client, store)) diff --git a/tests/nexus/test_stream_store.py b/tests/nexus/test_stream_store.py index a81b2caa11..6cecd7e93b 100644 --- a/tests/nexus/test_stream_store.py +++ b/tests/nexus/test_stream_store.py @@ -413,7 +413,6 @@ def test_each_stream_module_can_be_the_first_import(): "temporalio.workflow", "temporalio.client", "temporalio.worker", - "temporalio.contrib.redis.streams", ] failures: dict[str, str] = {} for module in modules: From 20b53dddd8eea9f8fa14bd16b8dc44366d06afec Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Wed, 18 Nov 2026 02:05:56 -0700 Subject: [PATCH 22/35] Ran the Stream tests on a Store written in Python. The next commit removes the memory Store from the SDK. `DictStreamStore` now has the three behaviors that the tests took from it. These are the resend of a batch, the fence of an older epoch and the wait of a read. --- tests/contrib/streams/test_appender.py | 9 ++- tests/contrib/streams/test_producer.py | 9 +-- tests/helpers/streams.py | 84 +++++++++++++++++++++-- tests/nexus/test_stream_store.py | 52 +++++++------- tests/nexus/test_stream_store_workflow.py | 9 ++- 5 files changed, 116 insertions(+), 47 deletions(-) diff --git a/tests/contrib/streams/test_appender.py b/tests/contrib/streams/test_appender.py index 6115ab5a15..d607176654 100644 --- a/tests/contrib/streams/test_appender.py +++ b/tests/contrib/streams/test_appender.py @@ -13,8 +13,7 @@ from temporalio.contrib.streams import BufferedAppender from temporalio.nexus.system.stream_service import StreamRef from temporalio.nexus.system.stream_service.client import Stream -from temporalio.testing import MemoryStreamStore -from tests.helpers.streams import RecordingStreamStore +from tests.helpers.streams import DictStreamStore, RecordingStreamStore from tests.nexus.test_stream_store import _create_stream, _stream_error, _with_store # Long enough that only a flush, a full batch or the end of `async with` starts a write. @@ -23,7 +22,7 @@ class RecordingStore(RecordingStreamStore): def __init__(self) -> None: - super().__init__(MemoryStreamStore()) + super().__init__(DictStreamStore()) self.batches: list[int] = [] self.append_started = asyncio.Event() self.release: asyncio.Event | None = None @@ -53,7 +52,7 @@ async def test_appends_inside_one_delay_go_as_one_batch(client: Client): async def test_flush_returns_the_last_offset(client: Client): - stream = await _create_stream(client, MemoryStreamStore()) + stream = await _create_stream(client, DictStreamStore()) appender = BufferedAppender(stream) assert await appender.flush() is None appender.append("a", "b") @@ -150,7 +149,7 @@ async def test_async_with_keeps_the_error_of_its_block(client: Client): async def test_append_without_a_value_fails(client: Client): - stream = _with_store(client, MemoryStreamStore()).get_stream( + stream = _with_store(client, DictStreamStore()).get_stream( StreamRef(id=f"stream-{uuid.uuid4()}"), body_type=str ) appender = BufferedAppender(stream) diff --git a/tests/contrib/streams/test_producer.py b/tests/contrib/streams/test_producer.py index a452c03882..86b6d922c9 100644 --- a/tests/contrib/streams/test_producer.py +++ b/tests/contrib/streams/test_producer.py @@ -25,8 +25,9 @@ WorkflowProducer, ) from temporalio.nexus.system.stream_service.client import Stream -from temporalio.testing import ActivityEnvironment, MemoryStreamStore +from temporalio.testing import ActivityEnvironment from tests.helpers import new_worker +from tests.helpers.streams import DictStreamStore from tests.nexus.test_stream_store import _create_stream, _with_store _SCHEDULED = datetime(2026, 11, 12, 10, 0, 0, 123456, tzinfo=timezone.utc) @@ -92,7 +93,7 @@ async def _records(stream: Stream[str]) -> list[StreamRecord[str]]: async def test_current_producer_in_other_code_is_the_client_producer(client: Client): - stream = await _create_stream(client, MemoryStreamStore()) + stream = await _create_stream(client, DictStreamStore()) await stream.append("a") [record] = await _records(stream) producer = current_producer() @@ -104,7 +105,7 @@ async def test_current_producer_in_other_code_is_the_client_producer(client: Cli async def test_current_producer_in_an_activity_is_its_attempt_producer( client: Client, kind: str ): - stream = await _create_stream(client, MemoryStreamStore()) + stream = await _create_stream(client, DictStreamStore()) async def append_and_give_producer() -> Producer: await stream.append("a") @@ -194,7 +195,7 @@ async def run(self, stream_id: str, keep_producer: bool) -> None: async def test_activity_retry_appends_as_its_stored_producer( client: Client, keep_producer: bool ): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream = await stream_client.create_stream( id=f"stream-{uuid.uuid4()}", body_type=str ) diff --git a/tests/helpers/streams.py b/tests/helpers/streams.py index beb4eaaa45..f2a3aad34e 100644 --- a/tests/helpers/streams.py +++ b/tests/helpers/streams.py @@ -2,18 +2,26 @@ from __future__ import annotations +import asyncio +import contextlib import dataclasses from collections.abc import Awaitable, Callable from typing import TypeVar from google.protobuf.message import Message +from google.protobuf.timestamp_pb2 import Timestamp from temporalio.api.enums.v1 import ( StreamErrorReason, StreamIdConflictPolicy, StreamStatus, ) -from temporalio.api.stream.v1 import StreamRecord, StreamRef +from temporalio.api.stream.v1 import ( + Producer, + StreamRecord, + StreamRef, + WorkflowTaskEpoch, +) from temporalio.api.streamservice.v1 import ( AppendRecordsRequest, AppendRecordsResponse, @@ -84,27 +92,64 @@ async def describe_stream( ) +@dataclasses.dataclass +class _LastBatch: + epoch: tuple[int, ...] + sequence: int + response: AppendRecordsResponse + + @dataclasses.dataclass class _DictStream: request_id: str records: list[StreamRecord] = dataclasses.field(default_factory=list) closed: bool = False + # The id of a Producer is unique in one Stream. + last_batches: dict[str, _LastBatch] = dataclasses.field(default_factory=dict) def _offset(index: int) -> bytes: return index.to_bytes(8, "big") +def _epoch(producer: Producer) -> tuple[int, ...]: + """Give a key that orders the epochs of one Producer, as ``Producer`` documents it.""" + kind = producer.WhichOneof("kind") + task = WorkflowTaskEpoch() + scheduled = Timestamp() + attempt = 0 + if kind == "workflow": + task = producer.workflow.epoch + elif kind == "local_activity": + task = producer.local_activity.epoch.task + attempt = producer.local_activity.epoch.attempt + elif kind in ("workflow_activity", "standalone_activity"): + scheduled = getattr(producer, kind).epoch.attempt_scheduled_time + attempt = getattr(producer, kind).epoch.attempt + return ( + task.started_event_id, + task.attempt, + task.started_time.seconds, + task.started_time.nanos, + scheduled.seconds, + scheduled.nanos, + attempt, + ) + + class DictStreamStore(StreamStore): - """A Store written from scratch, with no bridge, that keeps its Streams in a dict. + """A Store that keeps its Streams in a dict, for the tests. - It has enough of each operation for the tests, and no dedupe, no wait and no - Retention. + It has no Retention and no expiry, and it checks no request. It resends the answer + of a batch that comes again with the same Producer, epoch and sequence, and it + fences a Producer whose epoch is lower than its newest one. A read waits for a new + Record up to the ``wait`` of its request. """ def __init__(self, name: str = "dict") -> None: self._name = name self.streams: dict[str, _DictStream] = {} + self._changed = asyncio.Condition() @property def name(self) -> str: @@ -119,6 +164,10 @@ def _stream(self, ref: StreamRef) -> _DictStream: ) return stream + async def _notify(self) -> None: + async with self._changed: + self._changed.notify_all() + async def create_stream(self, request: CreateStreamRequest) -> CreateStreamResponse: ref = StreamRef(id=request.stream_ref.id, store=self.name) stream = self.streams.get(ref.id) @@ -141,11 +190,22 @@ async def append_records( self, request: AppendRecordsRequest ) -> AppendRecordsResponse: stream = self._stream(request.ref) + epoch = _epoch(request.producer) + last = stream.last_batches.get(request.producer.id) + previous = last if last is not None and last.epoch == epoch else None + sequence = request.sequence or (previous.sequence + 1 if previous else 1) + if previous is not None and sequence == previous.sequence: + return previous.response if stream.closed: raise StreamError( f"The Stream '{request.ref.id}' is closed", reason=StreamErrorReason.STREAM_ERROR_REASON_CLOSED, ) + if last is not None and epoch < last.epoch: + raise StreamError( + "The epoch is lower than the newest one of the Producer", + reason=StreamErrorReason.STREAM_ERROR_REASON_FENCED, + ) first = len(stream.records) + 1 for record in request.records: stream.records.append( @@ -155,13 +215,26 @@ async def append_records( offset=_offset(len(stream.records) + 1), ) ) - return AppendRecordsResponse( + response = AppendRecordsResponse( first_offset=_offset(first), last_offset=_offset(len(stream.records)) ) + stream.last_batches[request.producer.id] = _LastBatch(epoch, sequence, response) + await self._notify() + return response async def read_records(self, request: ReadRecordsRequest) -> ReadRecordsResponse: stream = self._stream(request.ref) start = int.from_bytes(request.from_offset, "big") if request.from_offset else 0 + wait = request.wait.ToTimedelta() + if wait and start >= len(stream.records) and not stream.closed: + async with self._changed: + with contextlib.suppress(TimeoutError): + await asyncio.wait_for( + self._changed.wait_for( + lambda: len(stream.records) > start or stream.closed + ), + wait.total_seconds(), + ) page = stream.records[start : start + (request.max_records or 1000)] last_offset = page[-1].offset if page else request.from_offset return ReadRecordsResponse( @@ -172,6 +245,7 @@ async def read_records(self, request: ReadRecordsRequest) -> ReadRecordsResponse async def close_stream(self, request: CloseStreamRequest) -> CloseStreamResponse: self._stream(request.ref).closed = True + await self._notify() return CloseStreamResponse() async def describe_stream( diff --git a/tests/nexus/test_stream_store.py b/tests/nexus/test_stream_store.py index 6cecd7e93b..040e00fdc3 100644 --- a/tests/nexus/test_stream_store.py +++ b/tests/nexus/test_stream_store.py @@ -51,11 +51,7 @@ Stream, StreamServiceClientExtension, ) -from temporalio.testing import ( - ActivityEnvironment, - MemoryStreamStore, - WorkflowEnvironment, -) +from temporalio.testing import ActivityEnvironment, WorkflowEnvironment from tests import DEV_SERVER_DOWNLOAD_VERSION from tests.helpers.streams import DictStreamStore from tests.test_converter import SimpleCodec @@ -73,9 +69,9 @@ def _with_config(client: Client, stream_config: StreamConfig) -> Client: def _two_stores( selector: Callable[[StreamSelectContext], StreamStore], -) -> tuple[MemoryStreamStore, MemoryStreamStore, StreamConfig]: - first = MemoryStreamStore(name="first") - second = MemoryStreamStore(name="second") +) -> tuple[DictStreamStore, DictStreamStore, StreamConfig]: + first = DictStreamStore(name="first") + second = DictStreamStore(name="second") return first, second, StreamConfig(stores=[first, second], store_selector=selector) @@ -94,7 +90,7 @@ async def _only_producer(stream: Stream[str]) -> Producer: async def _append_in_activity( client: Client, info: temporalio.activity.Info ) -> Producer: - stream = await _create_stream(client, MemoryStreamStore()) + stream = await _create_stream(client, DictStreamStore()) env = ActivityEnvironment() env.info = info await env.run(stream.append, "a") @@ -114,13 +110,13 @@ async def _stream_error(call: Awaitable[object]) -> StreamError: async def test_client_stream_uses_store(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream = await stream_client.create_stream( id=f"stream-{uuid.uuid4()}", body_type=str ) assert stream.created - assert stream.ref.store == "memory" + assert stream.ref.store == "dict" appended = await stream.append("a", "b") assert appended.first_offset is not None assert appended.last_offset is not None @@ -200,7 +196,7 @@ def test_stream_operations_match_the_stream_service(): async def test_client_strict_create_of_existing_stream_fails(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" await stream_client.create_stream(id=stream_id, body_type=str) err = await _stream_error(stream_client.create_stream(id=stream_id, body_type=str)) @@ -216,7 +212,7 @@ async def test_client_strict_create_of_existing_stream_fails(client: Client): async def test_client_read_or_describe_of_unknown_stream_fails(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream = stream_client.get_stream( StreamRef(id=f"stream-{uuid.uuid4()}"), body_type=str ) @@ -226,7 +222,7 @@ async def test_client_read_or_describe_of_unknown_stream_fails(client: Client): async def test_client_append_after_close_fails(client: Client): - stream = await _create_stream(client, MemoryStreamStore()) + stream = await _create_stream(client, DictStreamStore()) await stream.append("a") await stream.close() err = await _stream_error(stream.append("b")) @@ -235,7 +231,7 @@ async def test_client_append_after_close_fails(client: Client): async def test_client_stream_store_uses_payload_codec(client: Client): - store = MemoryStreamStore() + store = DictStreamStore() config = client.config() config["data_converter"] = DataConverter(payload_codec=SimpleCodec()) config["stream_config"] = StreamConfig(stores=[store]) @@ -255,7 +251,7 @@ async def test_client_stream_store_uses_payload_codec(client: Client): async def test_client_append_has_client_producer(client: Client): - stream = await _create_stream(client, MemoryStreamStore()) + stream = await _create_stream(client, DictStreamStore()) await stream.append("a") producer = await _only_producer(stream) assert producer.id.startswith("client:") @@ -348,7 +344,7 @@ async def test_local_activity_append_without_workflow_task_fails(client: Client) async def test_explicit_producer_is_sent(client: Client): - stream = await _create_stream(client, MemoryStreamStore()) + stream = await _create_stream(client, DictStreamStore()) with mock.patch.object( temporalio.nexus.system._support, "default_producer" ) as default_producer: @@ -362,7 +358,7 @@ async def test_explicit_producer_is_sent(client: Client): async def test_client_append_with_sequence_writes_once(client: Client): - stream = await _create_stream(client, MemoryStreamStore()) + stream = await _create_stream(client, DictStreamStore()) producer = Producer(id="mine", kind=ProducerKindClient(ClientProducer())) first = await stream.append("a", "b", producer=producer, sequence=5) second = await stream.append("a", "b", producer=producer, sequence=5) @@ -469,14 +465,14 @@ async def start_nexus_operation_execution( def test_stream_config_refuses_a_bad_set_of_stores(): with pytest.raises(StreamConfigError, match="at least one Store"): StreamConfig(stores=[]) - same_name = [MemoryStreamStore(name="a"), MemoryStreamStore(name="a")] + same_name = [DictStreamStore(name="a"), DictStreamStore(name="a")] with pytest.raises(StreamConfigError, match="more than one Store named 'a'"): StreamConfig(stores=same_name, store_selector=lambda _: same_name[0]) assert not issubclass(StreamConfigError, StreamError) def test_stream_config_takes_several_stores_with_no_selector(): - first, second = MemoryStreamStore(name="a"), MemoryStreamStore(name="b") + first, second = DictStreamStore(name="a"), DictStreamStore(name="b") stream_config = StreamConfig(stores=[first, second]) assert stream_config.store_selector is None assert stream_config._store_for(StreamRef(id="stream", store="b"), None) is second @@ -487,8 +483,8 @@ def test_stream_config_takes_several_stores_with_no_selector(): async def test_client_create_with_store_on_several_stores_needs_no_selector( client: Client, ): - first = MemoryStreamStore(name="first") - second = MemoryStreamStore(name="second") + first = DictStreamStore(name="first") + second = DictStreamStore(name="second") stream_client = _with_config(client, StreamConfig(stores=[first, second])) stream = await stream_client.create_stream( id=f"stream-{uuid.uuid4()}", store="second", body_type=str @@ -506,7 +502,7 @@ async def test_client_create_with_store_on_several_stores_needs_no_selector( async def test_client_create_without_store_on_several_stores_fails(client: Client): stream_config = StreamConfig( - stores=[MemoryStreamStore(name="first"), MemoryStreamStore(name="second")] + stores=[DictStreamStore(name="first"), DictStreamStore(name="second")] ) stream_client = _with_config(client, stream_config) with pytest.raises(StreamConfigError) as err: @@ -520,7 +516,7 @@ async def test_client_create_without_store_on_several_stores_fails(client: Clien def test_client_refuses_a_store_in_plugins(client: Client): config = client.config() - config["plugins"] = [MemoryStreamStore()] # type: ignore[list-item] + config["plugins"] = [DictStreamStore()] # type: ignore[list-item] with pytest.raises(StreamConfigError, match="plugins"): Client(**config) @@ -586,14 +582,14 @@ async def create() -> Stream[str]: async def test_selector_that_returns_another_store_fails(client: Client): - _, _, stream_config = _two_stores(lambda _: MemoryStreamStore(name="first")) + _, _, stream_config = _two_stores(lambda _: DictStreamStore(name="first")) stream_client = _with_config(client, stream_config) with pytest.raises(StreamConfigError, match="isn't one of"): await stream_client.create_stream(id=f"stream-{uuid.uuid4()}", body_type=str) async def test_ref_of_a_store_that_the_client_lacks_fails(client: Client): - stream_client = _with_store(client, MemoryStreamStore(name="first")) + stream_client = _with_store(client, DictStreamStore(name="first")) stream = stream_client.get_stream( StreamRef(id=f"stream-{uuid.uuid4()}", store="other"), body_type=str ) @@ -608,7 +604,7 @@ def test_request_without_a_stream_ref_fails(): async def test_start_local_gives_its_client_the_stream_config(): - stream_config = StreamConfig(stores=[MemoryStreamStore()]) + stream_config = StreamConfig(stores=[DictStreamStore()]) async with await WorkflowEnvironment.start_local( stream_config=stream_config, dev_server_download_version=DEV_SERVER_DOWNLOAD_VERSION, @@ -617,7 +613,7 @@ async def test_start_local_gives_its_client_the_stream_config(): stream = await env.client.create_stream( id=f"stream-{uuid.uuid4()}", body_type=str ) - assert stream.ref.store == "memory" + assert stream.ref.store == "dict" def test_stream_error_carries_its_reason(): diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index 4e594d44ec..c1df44daaa 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -17,9 +17,8 @@ StreamService, ) from temporalio.nexus.system.stream_service.workflow import create_stream, get_stream -from temporalio.testing import MemoryStreamStore from tests.helpers import assert_task_fail_eventually, new_worker -from tests.helpers.streams import RecordingStreamStore +from tests.helpers.streams import DictStreamStore, RecordingStreamStore from tests.nexus.test_stream_store import _with_store _BODIES = ["first-record-body", "second-record-body"] @@ -55,7 +54,7 @@ async def run(self, operation: str) -> None: async def test_workflow_stream_call_fails_the_workflow_task( client: Client, operation: str ): - store = RecordingStreamStore(MemoryStreamStore()) + store = RecordingStreamStore(DictStreamStore()) stream_client = _with_store(client, store) async with new_worker(stream_client, StreamCallWorkflow) as worker: handle = await stream_client.start_workflow( @@ -140,7 +139,7 @@ async def test_local_activity_append_after_a_failed_workflow_task_has_a_higher_e ): global _fail_next_task _fail_next_task = True - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream = await stream_client.create_stream( id=f"stream-{uuid.uuid4()}", body_type=str ) @@ -168,7 +167,7 @@ async def test_local_activity_append_after_a_failed_workflow_task_has_a_higher_e async def test_user_local_activity_append_has_local_activity_producer(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream = await stream_client.create_stream( id=f"stream-{uuid.uuid4()}", body_type=str ) From 8ddc7ff9a7edf0f94c58083c008b90bcc4466b53 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Wed, 18 Nov 2026 02:16:15 -0700 Subject: [PATCH 23/35] Removed the memory Stream Store and the Stream runtime from the bridge. The memory Store moves to the `temporalio-streams-memory` package. The bridge has no Stream code left, so a Store is any subclass of `StreamStore`. Core keeps only the protos and the WIT that the generated code needs. --- changelog/added/chattering-ledger-otter.md | 2 +- pyproject.toml | 12 ++- temporalio/bridge/Cargo.lock | 83 +------------------ temporalio/bridge/Cargo.toml | 6 +- temporalio/bridge/sdk-core | 2 +- temporalio/bridge/src/lib.rs | 21 ----- temporalio/bridge/src/streams.rs | 69 ---------------- temporalio/bridge/streams.py | 60 -------------- temporalio/client/_stream_store.py | 93 +--------------------- temporalio/exceptions.py | 18 ----- temporalio/testing/__init__.py | 2 - temporalio/testing/_stream_store.py | 57 ------------- 12 files changed, 11 insertions(+), 414 deletions(-) delete mode 100644 temporalio/bridge/src/streams.rs delete mode 100644 temporalio/bridge/streams.py delete mode 100644 temporalio/testing/_stream_store.py diff --git a/changelog/added/chattering-ledger-otter.md b/changelog/added/chattering-ledger-otter.md index c62ec20425..1c8c58885f 100644 --- a/changelog/added/chattering-ledger-otter.md +++ b/changelog/added/chattering-ledger-otter.md @@ -1,3 +1,3 @@ -Add the experimental Streams API. Client and Activity code create a Stream, append Records to it, read it and close it. Workflow code can't call a Stream in this version. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `RedisStreamStore` of the `temporalio-streams-redis` package, or `temporalio.testing.MemoryStreamStore` in tests. +Add the experimental Streams API. Client and Activity code create a Stream, append Records to it, read it and close it. Workflow code can't call a Stream in this version. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `RedisStreamStore` of the `temporalio-streams-redis` package, or `MemoryStreamStore` of the `temporalio-streams-memory` package in tests. Add `temporalio.contrib.streams.current_producer` and `as_producer`. An Activity can store its Producer in its heartbeat details and pass it to the appends of its retry. The retry's Records then continue the first attempt's epoch, so a reader does not take them as a replacement of the first attempt's Records. A Workflow outbound interceptor sees a Stream operation only in its operation-specific method, such as `start_append_records`, and not in `start_system_nexus_operation`. An unknown Stream operation raises `ValueError`. diff --git a/pyproject.toml b/pyproject.toml index d3f4f12454..2942a54a08 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -93,8 +93,8 @@ dev = [ ] [tool.poe.tasks] -build-develop = "uv run maturin develop --uv --features streams" -build-develop-with-release = { cmd = "uv run maturin develop --release --uv --features streams" } +build-develop = "uv run maturin develop --uv" +build-develop-with-release = { cmd = "uv run maturin develop --release --uv" } # FIPS builds: swap the rustls stack onto aws-lc-rs FIPS mode (aws-lc-fips-sys), eliminating `ring`. build-develop-fips = { cmd = "uv run maturin develop --uv --no-default-features --features fips", env = { TEMPORALIO_FIPS = "1" } } build-wheel-fips = { cmd = "uv run maturin build --release --no-default-features --features fips", env = { TEMPORALIO_FIPS = "1" } } @@ -153,20 +153,18 @@ filterwarnings = [ before-all = "pip install protoc-wheel-0" build = "cp311-win_amd64 cp311-manylinux_x86_64 cp311-manylinux_aarch64 cp311-musllinux_x86_64 cp311-musllinux_aarch64 cp311-macosx_x86_64 cp311-macosx_arm64" build-verbosity = 1 -# The official wheels link the Stream runtime. -environment = { MATURIN_PEP517_ARGS = "--features streams" } [tool.cibuildwheel.macos] -environment = { MACOSX_DEPLOYMENT_TARGET = "10.12", MATURIN_PEP517_ARGS = "--features streams" } +environment = { MACOSX_DEPLOYMENT_TARGET = "10.12" } [tool.cibuildwheel.linux] before-all = "sh scripts/cibuildwheel_before_all_linux.sh" before-build = "sh scripts/cibuildwheel_before_build_linux.sh" -environment = { PATH = "$PATH:$HOME/.cargo/bin", CARGO_NET_GIT_FETCH_WITH_CLI = "true", MATURIN_PEP517_ARGS = "--features streams" } +environment = { PATH = "$PATH:$HOME/.cargo/bin", CARGO_NET_GIT_FETCH_WITH_CLI = "true" } [[tool.cibuildwheel.overrides]] select = "*musllinux*" -environment = { PATH = "$PATH:$HOME/.cargo/bin", CARGO_NET_GIT_FETCH_WITH_CLI = "true", RUSTFLAGS = "-C target-feature=-crt-static", MATURIN_PEP517_ARGS = "--features streams" } +environment = { PATH = "$PATH:$HOME/.cargo/bin", CARGO_NET_GIT_FETCH_WITH_CLI = "true", RUSTFLAGS = "-C target-feature=-crt-static" } [tool.mypy] ignore_missing_imports = true diff --git a/temporalio/bridge/Cargo.lock b/temporalio/bridge/Cargo.lock index e2a45f5d20..3285c012ca 100644 --- a/temporalio/bridge/Cargo.lock +++ b/temporalio/bridge/Cargo.lock @@ -181,15 +181,6 @@ version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" -[[package]] -name = "block-buffer" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" -dependencies = [ - "generic-array", -] - [[package]] name = "bon" version = "3.10.3" @@ -274,7 +265,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ "cfg-if", - "cpufeatures 0.3.1", + "cpufeatures", "rand_core 0.10.1", ] @@ -352,15 +343,6 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" -[[package]] -name = "cpufeatures" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" -dependencies = [ - "libc", -] - [[package]] name = "cpufeatures" version = "0.3.1" @@ -394,16 +376,6 @@ version = "0.8.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" -[[package]] -name = "crypto-common" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" -dependencies = [ - "generic-array", - "typenum", -] - [[package]] name = "darling" version = "0.24.1" @@ -461,16 +433,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "digest" -version = "0.10.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" -dependencies = [ - "block-buffer", - "crypto-common", -] - [[package]] name = "dirs" version = "7.0.0" @@ -766,16 +728,6 @@ dependencies = [ "slab", ] -[[package]] -name = "generic-array" -version = "0.14.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" -dependencies = [ - "typenum", - "version_check", -] - [[package]] name = "gethostname" version = "1.1.0" @@ -2438,17 +2390,6 @@ dependencies = [ "serde", ] -[[package]] -name = "sha2" -version = "0.10.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" -dependencies = [ - "cfg-if", - "cpufeatures 0.2.17", - "digest", -] - [[package]] name = "sharded-slab" version = "0.1.7" @@ -2652,7 +2593,6 @@ dependencies = [ "temporalio-client", "temporalio-common", "temporalio-sdk-core", - "temporalio-streams", "tokio", "tokio-rustls", "tokio-stream", @@ -2836,21 +2776,6 @@ dependencies = [ "zip", ] -[[package]] -name = "temporalio-streams" -version = "0.1.0" -dependencies = [ - "async-trait", - "parking_lot", - "prost", - "prost-wkt-types", - "sha2", - "temporalio-common", - "thiserror", - "tokio", - "tonic", -] - [[package]] name = "termtree" version = "0.5.1" @@ -3216,12 +3141,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c" -[[package]] -name = "typenum" -version = "1.20.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" - [[package]] name = "typetag" version = "0.2.23" diff --git a/temporalio/bridge/Cargo.toml b/temporalio/bridge/Cargo.toml index 7c4fc4e9fa..4b23ae6ce9 100644 --- a/temporalio/bridge/Cargo.toml +++ b/temporalio/bridge/Cargo.toml @@ -36,9 +36,7 @@ tls-aws-lc = [ "temporalio-client/tls-aws-lc", "tokio-rustls/aws_lc_rs", ] -fips = ["tls-aws-lc", "dep:aws-lc-rs", "streams"] -# Links the Stream runtime and its memory Store. Off by default. The official builds turn it on. -streams = ["dep:temporalio-streams"] +fips = ["tls-aws-lc", "dep:aws-lc-rs"] [dependencies] anyhow = "1.0" @@ -74,8 +72,6 @@ temporalio-sdk-core = { version = "0.11.0", path = "./sdk-core/crates/sdk-core", "envconfig", "prometheus", ] } -# The Stream runtime and its memory Store, from the submodule. -temporalio-streams = { path = "./sdk-core/crates/streams", optional = true } tokio = "1.26" # Matches the sdk-core client crate's rustls stack; used to build the custom # server certificate verifier for ClientTlsConfig.verification_server_name. diff --git a/temporalio/bridge/sdk-core b/temporalio/bridge/sdk-core index 9b121cf32d..7bb86122ba 160000 --- a/temporalio/bridge/sdk-core +++ b/temporalio/bridge/sdk-core @@ -1 +1 @@ -Subproject commit 9b121cf32dcd7920a067a04fb61bbb000178290b +Subproject commit 7bb86122bab4ea1efca2295da75fa7751aeaed0c diff --git a/temporalio/bridge/src/lib.rs b/temporalio/bridge/src/lib.rs index 5781b15dcc..a4fda26db6 100644 --- a/temporalio/bridge/src/lib.rs +++ b/temporalio/bridge/src/lib.rs @@ -6,7 +6,6 @@ mod client_rpc_generated; mod envconfig; mod metric; mod runtime; -mod streams; mod testing; mod worker; @@ -49,14 +48,6 @@ fn temporal_sdk_bridge(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(init_runtime, m)?)?; m.add_function(wrap_pyfunction!(raise_in_thread, m)?)?; - // Stream stuff - m.add("StreamCallError", py.get_type::())?; - #[cfg(feature = "streams")] - { - m.add_class::()?; - m.add_function(wrap_pyfunction!(new_memory_stream_runtime, m)?)?; - } - // Testing stuff m.add_class::()?; m.add_function(wrap_pyfunction!(start_dev_server, m)?)?; @@ -113,18 +104,6 @@ fn raise_in_thread(py: Python, thread_id: std::os::raw::c_long, exc: &Bound<'_, runtime::raise_in_thread(py, thread_id, exc) } -#[cfg(feature = "streams")] -#[pyfunction] -#[pyo3(signature = (runtime_ref, store_name, retention=None, producer_idle_ttl=None))] -fn new_memory_stream_runtime( - runtime_ref: &runtime::RuntimeRef, - store_name: String, - retention: Option, - producer_idle_ttl: Option, -) -> PyResult { - streams::new_memory_stream_runtime(runtime_ref, store_name, retention, producer_idle_ttl) -} - #[pyfunction] fn start_dev_server<'a>( py: Python<'a>, diff --git a/temporalio/bridge/src/streams.rs b/temporalio/bridge/src/streams.rs deleted file mode 100644 index fff45180a2..0000000000 --- a/temporalio/bridge/src/streams.rs +++ /dev/null @@ -1,69 +0,0 @@ -//! Bindings to the Stream runtime of the `temporalio-streams` crate, on the memory Store. Python -//! calls the five Stream service operations by name, with the encoded request, and gets the encoded -//! response. - -use pyo3::exceptions::PyException; -#[cfg(feature = "streams")] -use pyo3::prelude::*; - -#[cfg(feature = "streams")] -use crate::runtime; - -// The exception args are the gRPC code, the message and the encoded details, as in `RPCError`. -pyo3::create_exception!(temporal_sdk_bridge, StreamCallError, PyException); - -/// The crate's runtime object. Each Store on the bridge holds one. -#[cfg(feature = "streams")] -#[pyclass(frozen)] -pub struct StreamRuntimeRef { - streams: std::sync::Arc, - runtime: runtime::Runtime, -} - -#[cfg(feature = "streams")] -#[pymethods] -impl StreamRuntimeRef { - /// Runs one Stream operation on an encoded request. The future gives the encoded response. - fn call<'p>( - &self, - py: Python<'p>, - operation: String, - request: Vec, - ) -> PyResult> { - self.runtime - .assert_same_process("call the Stream runtime")?; - let streams = self.streams.clone(); - self.runtime.future_into_py(py, async move { - streams.call(&operation, &request).await.map_err(call_error) - }) - } -} - -#[cfg(feature = "streams")] -fn call_error(err: temporalio_streams::StreamError) -> PyErr { - StreamCallError::new_err((err.code() as u32, err.message.clone(), err.details())) -} - -/// Builds the runtime on a new memory Store. A `None` duration takes the crate's default. -#[cfg(feature = "streams")] -pub fn new_memory_stream_runtime( - runtime_ref: &runtime::RuntimeRef, - store_name: String, - retention: Option, - producer_idle_ttl: Option, -) -> PyResult { - let runtime = runtime_ref.runtime.clone(); - runtime.assert_same_process("create a Stream runtime")?; - // The runtime can spawn Tokio tasks, which need an entered Tokio runtime. - let _guard = runtime.core.tokio_handle().enter(); - let meter = runtime.core.telemetry().get_temporal_metric_meter(); - let streams = temporalio_streams::MemoryStreamRuntime::new( - store_name, - temporalio_streams::MemoryStreamOptions { - retention, - producer_idle_ttl, - }, - meter, - ); - Ok(StreamRuntimeRef { streams, runtime }) -} diff --git a/temporalio/bridge/streams.py b/temporalio/bridge/streams.py deleted file mode 100644 index 66c4034dd4..0000000000 --- a/temporalio/bridge/streams.py +++ /dev/null @@ -1,60 +0,0 @@ -"""The Stream runtime that this bridge build links. (unstable) - -Nothing in this module should be considered stable. The API may change. -""" - -from __future__ import annotations - -from datetime import timedelta -from typing import Any - -import temporalio.bridge.runtime -import temporalio.bridge.temporal_sdk_bridge -import temporalio.exceptions - -StreamCallError: type[Exception] = temporalio.bridge.temporal_sdk_bridge.StreamCallError - - -class StreamRuntime: - """The runtime of the ``temporalio-streams`` crate on one Store.""" - - @staticmethod - def new_memory( - runtime: temporalio.bridge.runtime.Runtime, - *, - name: str, - retention: timedelta | None, - producer_idle_ttl: timedelta | None, - ) -> StreamRuntime: - """Build the runtime on a new memory Store. - - Raises: - temporalio.exceptions.StreamConfigError: The bridge build has no Stream - runtime. - """ - new = getattr( - temporalio.bridge.temporal_sdk_bridge, "new_memory_stream_runtime", None - ) - if new is None: - raise temporalio.exceptions.StreamConfigError( - "This build of temporalio has no Stream runtime" - ) - return StreamRuntime(new(runtime._ref, name, retention, producer_idle_ttl)) - - def __init__(self, ref: Any) -> None: - """Wrap the bridge's runtime object. Use ``new_memory`` instead.""" - self._ref = ref - - async def call(self, operation: str, request: bytes) -> bytes: - """Run one Stream operation on an encoded request and return the encoded response. - - Raises: - temporalio.exceptions.StreamError: The call failed. - """ - try: - return await self._ref.call(operation, request) - except StreamCallError as err: - _, message, details = err.args - raise temporalio.exceptions._stream_error_from_call( - message, details - ) from None diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index f14c9ee35a..ba56b907ed 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -3,8 +3,7 @@ import dataclasses from abc import ABC, abstractmethod from collections.abc import Callable, Sequence -from datetime import timedelta -from typing import TYPE_CHECKING, Any +from typing import Any import google.protobuf.message import nexusrpc @@ -12,16 +11,11 @@ import temporalio.activity import temporalio.api.streamservice.v1 import temporalio.bridge._visitor -import temporalio.bridge.runtime import temporalio.converter import temporalio.nexus.system.stream_service -import temporalio.runtime from temporalio.converter import StorageDriverActivityInfo from temporalio.exceptions import StreamConfigError -if TYPE_CHECKING: - import temporalio.bridge.streams - class StreamStore(ABC): """A Store that answers the Stream operations of a client. @@ -29,7 +23,7 @@ class StreamStore(ABC): A client with a Store sends each Stream operation to it. This release has no Streams on the server, so a client needs a Store for any Stream operation. Use a Store of a package, such as ``RedisStreamStore`` of ``temporalio-streams-redis``, or - :py:class:`temporalio.testing.MemoryStreamStore` in tests. + ``MemoryStreamStore`` of ``temporalio-streams-memory`` in tests. To keep Streams in another system, write a Store that implements :py:attr:`name` and the five operations. Each operation takes and gives a message of @@ -94,83 +88,6 @@ async def describe_stream( ... -class _BridgeStreamStore(StreamStore): # pyright: ignore[reportUnusedClass] - """A Store that runs on the Stream runtime of the bridge. - - It builds the runtime at its first call, so the Store can be built before the - Runtime and its Tokio threads exist. - """ - - def __init__( - self, - *, - name: str, - runtime: temporalio.runtime.Runtime | None, - ) -> None: - self._name = name - self._runtime = runtime - self._stream_runtime: temporalio.bridge.streams.StreamRuntime | None = None - - @property - def name(self) -> str: - """Name of the Store.""" - return self._name - - @abstractmethod - def _new_stream_runtime( - self, runtime: temporalio.bridge.runtime.Runtime - ) -> temporalio.bridge.streams.StreamRuntime: ... - - async def create_stream( - self, request: temporalio.api.streamservice.v1.CreateStreamRequest - ) -> temporalio.api.streamservice.v1.CreateStreamResponse: - """See base class.""" - return await self._call_bridge("CreateStream", request) - - async def append_records( - self, request: temporalio.api.streamservice.v1.AppendRecordsRequest - ) -> temporalio.api.streamservice.v1.AppendRecordsResponse: - """See base class.""" - return await self._call_bridge("AppendRecords", request) - - async def read_records( - self, request: temporalio.api.streamservice.v1.ReadRecordsRequest - ) -> temporalio.api.streamservice.v1.ReadRecordsResponse: - """See base class.""" - return await self._call_bridge("ReadRecords", request) - - async def close_stream( - self, request: temporalio.api.streamservice.v1.CloseStreamRequest - ) -> temporalio.api.streamservice.v1.CloseStreamResponse: - """See base class.""" - return await self._call_bridge("CloseStream", request) - - async def describe_stream( - self, request: temporalio.api.streamservice.v1.DescribeStreamRequest - ) -> temporalio.api.streamservice.v1.DescribeStreamResponse: - """See base class.""" - return await self._call_bridge("DescribeStream", request) - - async def _call_bridge( - self, operation: str, request: google.protobuf.message.Message - ) -> Any: - """Answer one Stream operation with the Stream runtime of the bridge. - - Raises: - temporalio.exceptions.StreamError: The call failed. - temporalio.exceptions.StreamConfigError: This build of temporalio has no - Stream runtime. - """ - if self._stream_runtime is None: - runtime = self._runtime or temporalio.runtime.Runtime.default() - self._stream_runtime = self._new_stream_runtime(runtime._core_runtime) - # The bridge takes and gives the messages in their wire form. - response = await self._stream_runtime.call( - operation, request.SerializeToString() - ) - return _STREAM_OPERATIONS[operation].response_type.FromString(response) - - @dataclasses.dataclass(frozen=True) class _StreamOperation: """How a Store answers one operation of the Stream service.""" @@ -393,12 +310,6 @@ def _refuse_store_plugins( # pyright: ignore[reportUnusedFunction] ) -def _check_not_negative(**durations: timedelta | None) -> None: # pyright: ignore[reportUnusedFunction] - for label, value in durations.items(): - if value is not None and value < timedelta(): - raise ValueError(f"{label} must not be negative") - - def _stream_service() -> nexusrpc.ServiceDefinition: service = nexusrpc.get_service_definition( temporalio.nexus.system.stream_service.StreamService diff --git a/temporalio/exceptions.py b/temporalio/exceptions.py index 23ef532211..3f2df0fc9a 100644 --- a/temporalio/exceptions.py +++ b/temporalio/exceptions.py @@ -249,23 +249,6 @@ def reason(self) -> temporalio.api.enums.v1.StreamErrorReason.ValueType: return self._reason -def _stream_error_from_reason(reason: int, message: str) -> StreamError: - if reason not in temporalio.api.enums.v1.StreamErrorReason.values(): - # A reason that this SDK doesn't know gives the unspecified reason. - reason = _STREAM_ERROR_REASON_UNSPECIFIED - return StreamError( - message, reason=temporalio.api.enums.v1.StreamErrorReason.ValueType(reason) - ) - - -def _stream_error_from_call( # pyright: ignore[reportUnusedFunction] - message: str, details: bytes -) -> StreamError: - # A Store on the bridge fails with the encoded details, as a gRPC status has them. - proto = temporalio.api.stream.v1.StreamErrorDetails.FromString(details) - return _stream_error_from_reason(proto.reason, message) - - class StreamConfigError(RuntimeError): """A Stream call can't find its Store, because of how the Stores are set up. @@ -280,7 +263,6 @@ class StreamConfigError(RuntimeError): * A ref names a Store that the config of the client doesn't have. * The selector returns a Store that isn't one of the config's Stores. * A :py:class:`temporalio.client.StreamStore` is given in ``plugins``. - * This build of temporalio has no Stream runtime. .. warning:: This API is experimental and subject to change. diff --git a/temporalio/testing/__init__.py b/temporalio/testing/__init__.py index 5574a0a39b..0cd652d86b 100644 --- a/temporalio/testing/__init__.py +++ b/temporalio/testing/__init__.py @@ -1,13 +1,11 @@ """Test framework for workflows and activities.""" from ._activity import ActivityEnvironment -from ._stream_store import MemoryStreamStore from ._timeskipping import TimeSkipper, TimeSkippingConfig from ._workflow import WorkflowEnvironment __all__ = [ "ActivityEnvironment", - "MemoryStreamStore", "TimeSkipper", "WorkflowEnvironment", "TimeSkippingConfig", diff --git a/temporalio/testing/_stream_store.py b/temporalio/testing/_stream_store.py deleted file mode 100644 index 1220bbc5b7..0000000000 --- a/temporalio/testing/_stream_store.py +++ /dev/null @@ -1,57 +0,0 @@ -"""A Stream Store in the memory of the process, for tests.""" - -from __future__ import annotations - -from datetime import timedelta - -import temporalio.bridge.runtime -import temporalio.bridge.streams -import temporalio.client._stream_store -import temporalio.runtime - - -class MemoryStreamStore(temporalio.client._stream_store._BridgeStreamStore): - """A Stream Store that keeps its Streams in the memory of this process, for tests. - - It runs the same Stream runtime and rules as every other Store on the bridge. A - failed call raises :py:class:`temporalio.exceptions.StreamError`. - - .. warning:: - This API is experimental and subject to change. - """ - - def __init__( - self, - *, - name: str = "memory", - retention: timedelta | None = None, - producer_idle_ttl: timedelta | None = None, - runtime: temporalio.runtime.Runtime | None = None, - ) -> None: - """Create a memory Store. It builds its Stream runtime at its first call. - - Args: - name: Name of the Store, which the refs of its Streams carry. - retention: How long a closed Stream stays. ``None`` takes the default of - the Stream runtime. - producer_idle_ttl: How long an open Stream stays after the last write of any - Producer. ``None`` keeps an open Stream until its close. - runtime: Runtime whose Tokio threads run the Store. ``None`` takes - :py:meth:`temporalio.runtime.Runtime.default` at the first call. - """ - temporalio.client._stream_store._check_not_negative( - retention=retention, producer_idle_ttl=producer_idle_ttl - ) - super().__init__(name=name, runtime=runtime) - self._retention = retention - self._producer_idle_ttl = producer_idle_ttl - - def _new_stream_runtime( - self, runtime: temporalio.bridge.runtime.Runtime - ) -> temporalio.bridge.streams.StreamRuntime: - return temporalio.bridge.streams.StreamRuntime.new_memory( - runtime, - name=self._name, - retention=self._retention, - producer_idle_ttl=self._producer_idle_ttl, - ) From f93c9596ec598790020c2b6ec62509afbea8da90 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Wed, 18 Nov 2026 11:19:01 -0700 Subject: [PATCH 24/35] Added the batch limits of a Stream Store. --- temporalio/client/__init__.py | 2 + temporalio/client/_stream_store.py | 50 +++++++++++++++++++++++++ temporalio/contrib/streams/_appender.py | 24 +++++++++--- tests/contrib/streams/test_appender.py | 25 +++++++++++++ tests/nexus/test_stream_store.py | 28 ++++++++++++++ 5 files changed, 124 insertions(+), 5 deletions(-) diff --git a/temporalio/client/__init__.py b/temporalio/client/__init__.py index 75e32e5e2c..586b3b96cd 100644 --- a/temporalio/client/__init__.py +++ b/temporalio/client/__init__.py @@ -186,6 +186,7 @@ ScheduleUpdateInput, ) from ._stream_store import ( + StreamBatchLimits, StreamConfig, StreamSelectContext, StreamStore, @@ -355,6 +356,7 @@ "TaskReachabilityType", "CloudOperationsClient", "Plugin", + "StreamBatchLimits", "StreamConfig", "StreamSelectContext", "StreamStore", diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index ba56b907ed..6b6d5778e9 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -16,6 +16,47 @@ from temporalio.converter import StorageDriverActivityInfo from temporalio.exceptions import StreamConfigError +_MAX_RECORD_BYTES = 1536 * 1024 +_MAX_BATCH_BYTES = 4 * 1024 * 1024 +_MAX_BATCH_RECORDS = 1000 + + +@dataclasses.dataclass(frozen=True) +class StreamBatchLimits: + """The limits of one batch of Records on a Stream Store. + + The defaults are the largest batch that any Store may take. A Store can allow less, and + never more. + + .. warning:: + This API is experimental and subject to change. + """ + + max_record_bytes: int = _MAX_RECORD_BYTES + """The most bytes of one stored Record, from 1 to 1,572,864. The stored form of a Record + has its Producer and its body.""" + + max_batch_bytes: int = _MAX_BATCH_BYTES + """The most bytes of the stored Records of one batch, from 1 to 4,194,304.""" + + max_batch_records: int = _MAX_BATCH_RECORDS + """The most Records of one batch, from 1 to 1,000.""" + + def __post_init__(self) -> None: + """Check that each limit is from 1 to the default of its field. + + Raises: + ValueError: A limit is below 1 or above its default. + """ + for name, most in ( + ("max_record_bytes", _MAX_RECORD_BYTES), + ("max_batch_bytes", _MAX_BATCH_BYTES), + ("max_batch_records", _MAX_BATCH_RECORDS), + ): + value = getattr(self, name) + if not 1 <= value <= most: + raise ValueError(f"{name} must be from 1 to {most}, not {value}") + class StreamStore(ABC): """A Store that answers the Stream operations of a client. @@ -42,6 +83,15 @@ def name(self) -> str: """Name of the Store, which the refs of its Streams carry.""" ... + @property + def limits(self) -> StreamBatchLimits: + """The limits of one batch on this Store. + + The default is the largest batch that any Store may take. A Store that allows less + overrides this property, and refuses a larger batch itself. + """ + return StreamBatchLimits() + @abstractmethod async def create_stream( self, request: temporalio.api.streamservice.v1.CreateStreamRequest diff --git a/temporalio/contrib/streams/_appender.py b/temporalio/contrib/streams/_appender.py index 475c4ee551..8625da3d80 100644 --- a/temporalio/contrib/streams/_appender.py +++ b/temporalio/contrib/streams/_appender.py @@ -30,7 +30,7 @@ class BufferedAppender(Generic[BodyT]): Use it in code that appends often, such as an Activity that streams model tokens. :py:meth:`append` returns immediately and does no I/O. One write is in flight at a time. Records appended during a write wait for the next batch, and they keep their order. A batch - holds at most 1,000 Records. + holds at most ``max_batch_records`` Records. It takes a client Stream, in a client or an Activity. It writes the buffered Records at most ``max_delay`` after the first of them, or immediately when they fill a batch. @@ -57,6 +57,7 @@ def __init__( stream: temporalio.nexus.system.stream_service.client.Stream[BodyT], *, max_delay: timedelta | None = None, + max_batch_records: int | None = None, ) -> None: """Create an appender. It starts no write until the first append. @@ -64,14 +65,27 @@ def __init__( stream: The client Stream to append to. max_delay: The longest time that a Record waits in the buffer before its batch starts to write. It must not be negative. ``None`` means 100 milliseconds. + max_batch_records: The most Records of one batch, from 1 to 1,000. Set it to the + ``max_batch_records`` of the Store's + :py:attr:`temporalio.client.StreamStore.limits` when the Store allows less. + ``None`` means 1,000. Raises: - ValueError: ``max_delay`` is negative. + ValueError: ``max_delay`` is negative, or ``max_batch_records`` is below 1 or + above 1,000. """ if max_delay is not None and max_delay < timedelta(): raise ValueError("max_delay must not be negative") + if max_batch_records is None: + max_batch_records = _MAX_BATCH_RECORDS + elif not 1 <= max_batch_records <= _MAX_BATCH_RECORDS: + raise ValueError( + f"max_batch_records must be from 1 to {_MAX_BATCH_RECORDS}, " + f"not {max_batch_records}" + ) self._stream = stream self._max_delay = max_delay + self._max_batch_records = max_batch_records self._buffer: list[BodyT] = [] # When the oldest buffered Record arrived, so its batch starts at most max_delay later. self._first_at = 0.0 @@ -153,7 +167,7 @@ def _raise_error(self) -> None: def _schedule(self) -> None: if self._writing is not None or not self._buffer: return - if len(self._buffer) >= _MAX_BATCH_RECORDS: + if len(self._buffer) >= self._max_batch_records: self._start() elif self._timer is None: max_delay = self._max_delay or _DEFAULT_MAX_DELAY @@ -167,8 +181,8 @@ def _start(self) -> None: self._timer = None if self._writing is not None or not self._buffer: return - batch = self._buffer[:_MAX_BATCH_RECORDS] - del self._buffer[:_MAX_BATCH_RECORDS] + batch = self._buffer[: self._max_batch_records] + del self._buffer[: self._max_batch_records] self._writing = asyncio.get_running_loop().create_task(self._write(batch)) async def _write(self, batch: list[BodyT]) -> None: diff --git a/tests/contrib/streams/test_appender.py b/tests/contrib/streams/test_appender.py index d607176654..9458ed217a 100644 --- a/tests/contrib/streams/test_appender.py +++ b/tests/contrib/streams/test_appender.py @@ -92,6 +92,17 @@ async def test_a_large_buffer_goes_as_batches_of_at_most_1000(client: Client): assert await _bodies(stream) == values +async def test_max_batch_records_cuts_the_batches(client: Client): + store = RecordingStore() + stream = await _create_stream(client, store) + appender = BufferedAppender(stream, max_delay=_NEVER, max_batch_records=2) + values = ["a", "b", "c", "d", "e"] + appender.append(*values) + await appender.flush() + assert store.batches == [2, 2, 1] + assert await _bodies(stream) == values + + async def test_the_timer_alone_writes_the_batch(client: Client): store = RecordingStore() stream = await _create_stream(client, store) @@ -157,3 +168,17 @@ async def test_append_without_a_value_fails(client: Client): appender.append() with pytest.raises(ValueError, match="max_delay must not be negative"): BufferedAppender(stream, max_delay=timedelta(seconds=-1)) + + +async def test_max_batch_records_must_be_from_1_to_1000(client: Client): + stream = _with_store(client, DictStreamStore()).get_stream( + StreamRef(id=f"stream-{uuid.uuid4()}"), body_type=str + ) + for value in (0, 1001): + with pytest.raises(ValueError) as err: + BufferedAppender(stream, max_batch_records=value) + assert ( + str(err.value) == f"max_batch_records must be from 1 to 1000, not {value}" + ) + for value in (1, 1000): + BufferedAppender(stream, max_batch_records=value) diff --git a/tests/nexus/test_stream_store.py b/tests/nexus/test_stream_store.py index 040e00fdc3..f9a582519f 100644 --- a/tests/nexus/test_stream_store.py +++ b/tests/nexus/test_stream_store.py @@ -26,6 +26,7 @@ from temporalio.api.enums.v1 import StreamErrorReason from temporalio.client import ( Client, + StreamBatchLimits, StreamConfig, StreamSelectContext, StreamStore, @@ -480,6 +481,33 @@ def test_stream_config_takes_several_stores_with_no_selector(): stream_config._store_for(StreamRef(id="stream"), None) +_BATCH_LIMIT_DEFAULTS = { + "max_record_bytes": 1536 * 1024, + "max_batch_bytes": 4 * 1024 * 1024, + "max_batch_records": 1000, +} + + +def test_stream_batch_limits_default_to_the_largest_batch(): + assert dataclasses.asdict(StreamBatchLimits()) == _BATCH_LIMIT_DEFAULTS + + +@pytest.mark.parametrize("field,default", _BATCH_LIMIT_DEFAULTS.items()) +def test_stream_batch_limits_take_a_value_from_1_to_the_default( + field: str, default: int +): + for value in (1, default): + assert getattr(StreamBatchLimits(**{field: value}), field) == value + for value in (0, default + 1): + with pytest.raises(ValueError) as err: + StreamBatchLimits(**{field: value}) + assert str(err.value) == f"{field} must be from 1 to {default}, not {value}" + + +def test_stream_store_without_its_own_limits_gives_the_default(): + assert DictStreamStore().limits == StreamBatchLimits() + + async def test_client_create_with_store_on_several_stores_needs_no_selector( client: Client, ): From 37bddc0f0609e2b9f4039cb7f9acd1fc1c408ee5 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Tue, 17 Nov 2026 15:29:07 -0700 Subject: [PATCH 25/35] Let Workflow code read, append and close a Stream through Core. Core makes a Workflow's read, append and close inside the Workflow Task, with one marker for the task. A create and a describe run in a Local Activity. --- .github/workflows/ci.yml | 6 +- changelog/added/chattering-ledger-otter.md | 2 +- changelog/added/patient-river-heron.md | 9 + scripts/gen_nexus_system_api.py | 7 +- temporalio/api/stream/v1/message_pb2.pyi | 8 +- temporalio/bridge/Cargo.lock | 1 + temporalio/bridge/_visitor.py | 59 +- temporalio/bridge/proto/common/__init__.py | 2 + temporalio/bridge/proto/common/common_pb2.py | 8 +- temporalio/bridge/proto/common/common_pb2.pyi | 39 + .../bridge/proto/external_data/__init__.py | 15 +- .../proto/external_data/external_data_pb2.py | 30 +- .../proto/external_data/external_data_pb2.pyi | 343 ++ .../proto/workflow_activation/__init__.py | 10 + .../workflow_activation_pb2.py | 119 +- .../workflow_activation_pb2.pyi | 217 ++ .../proto/workflow_commands/__init__.py | 8 + .../workflow_commands_pb2.py | 145 +- .../workflow_commands_pb2.pyi | 245 ++ temporalio/bridge/sdk-core | 2 +- temporalio/bridge/src/streams.rs | 69 + temporalio/bridge/src/worker.rs | 67 +- temporalio/bridge/worker.py | 13 + temporalio/client/__init__.py | 4 + temporalio/client/_stream_store.py | 403 ++- temporalio/contrib/streams/README.md | 137 +- temporalio/contrib/streams/__init__.py | 8 +- temporalio/contrib/streams/_appender.py | 53 +- temporalio/contrib/streams/_producer.py | 15 +- temporalio/exceptions.py | 81 +- .../system/stream_service/workflow/_poll.py | 77 + .../workflow/_resources/stream.py | 118 + temporalio/testing/_stream_store.py | 57 + temporalio/worker/_command_aware_visitor.py | 51 + temporalio/worker/_replayer.py | 37 + temporalio/worker/_stream_store.py | 517 +++ temporalio/worker/_worker.py | 44 +- temporalio/worker/_workflow.py | 20 + temporalio/worker/_workflow_instance.py | 256 +- .../contrib/streams/test_appender_workflow.py | 230 ++ tests/contrib/streams/test_producer.py | 53 +- tests/nexus/test_stream_direct.py | 2849 +++++++++++++++++ tests/nexus/test_stream_store.py | 32 +- tests/nexus/test_stream_store_workflow.py | 1144 ++++++- 44 files changed, 7267 insertions(+), 343 deletions(-) create mode 100644 changelog/added/patient-river-heron.md create mode 100644 temporalio/bridge/src/streams.rs create mode 100644 temporalio/nexus/system/stream_service/workflow/_poll.py create mode 100644 temporalio/testing/_stream_store.py create mode 100644 temporalio/worker/_stream_store.py create mode 100644 tests/contrib/streams/test_appender_workflow.py create mode 100644 tests/nexus/test_stream_direct.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 12e3b8e566..f121ae1dd3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -226,9 +226,9 @@ jobs: - run: uv tool install poethepoet - run: uv sync --all-extras - run: cargo install --locked nexgen --version 0.2.8 --features advanced --force - # The Stream package needs the read, read pages and read page methods of a poll, which only - # a nexgen fork has. - - run: cargo install --locked --git https://github.com/moetemp/nexgen --rev 07b565b6d4339dc8a3f67ce2e9e72ba4925d7f9c --features advanced --root "$RUNNER_TEMP/nexgen-stream" nexgen + # The Stream package needs `@nexus.helper` with `sync=true`, which only a nexgen fork has. + # This commit of the fork also has the poll iterators of the Workflow target. + - run: cargo install --locked --git https://github.com/moetemp/nexgen --rev 2785462ecdaba5da7a27051fbaaccbaee26ae9e0 --features advanced --root "$RUNNER_TEMP/nexgen-stream" nexgen - run: echo "NEX_GEN_STREAM_BIN=$RUNNER_TEMP/nexgen-stream/bin/nexgen" >> "$GITHUB_ENV" - run: poe build-develop - run: poe gen-protos diff --git a/changelog/added/chattering-ledger-otter.md b/changelog/added/chattering-ledger-otter.md index 1c8c58885f..acb0a8fba7 100644 --- a/changelog/added/chattering-ledger-otter.md +++ b/changelog/added/chattering-ledger-otter.md @@ -1,3 +1,3 @@ -Add the experimental Streams API. Client and Activity code create a Stream, append Records to it, read it and close it. Workflow code can't call a Stream in this version. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `RedisStreamStore` of the `temporalio-streams-redis` package, or `MemoryStreamStore` of the `temporalio-streams-memory` package in tests. +Add the experimental Streams API. Client and Activity code create a Stream, append Records to it, read it and close it. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `RedisStreamStore` of the `temporalio-streams-redis` package, or `temporalio.testing.MemoryStreamStore` in tests. Add `temporalio.contrib.streams.current_producer` and `as_producer`. An Activity can store its Producer in its heartbeat details and pass it to the appends of its retry. The retry's Records then continue the first attempt's epoch, so a reader does not take them as a replacement of the first attempt's Records. A Workflow outbound interceptor sees a Stream operation only in its operation-specific method, such as `start_append_records`, and not in `start_system_nexus_operation`. An unknown Stream operation raises `ValueError`. diff --git a/changelog/added/patient-river-heron.md b/changelog/added/patient-river-heron.md new file mode 100644 index 0000000000..a0dc05af38 --- /dev/null +++ b/changelog/added/patient-river-heron.md @@ -0,0 +1,9 @@ +Workflow code creates a Stream, appends Records to it, reads it, describes it and closes it, with `workflow.create_stream` and `workflow.get_stream`. A create and a describe run in the Store's Local Activity, with the options of `StreamConfig.workflow_call_options`. Core makes a read, an append and a close itself, with no Activity, on a Store with a Rust runtime such as the Redis Store or `MemoryStreamStore`. On a Store written in Python, a read, an append and a close of Workflow code fail the Workflow. `Replayer` takes the `stream_config` of the Worker's client. +The Workflow `Stream` has `read`, `iter_read` and `iter_read_items`, so Workflow code reads a Stream with the same calls as a client. Core reads the Store inside the Workflow Task. A page that Workflow code gets in one activation has at most 256 Records, and the rest come in the next activation with no wait. A loop that asks for its next page in the same activation keeps one read open, and a read that no code waits on closes before the activation completes, so it never holds a Workflow Task open. With a `wait`, a quiet Stream gives empty pages, so a loop on a Stream that can be quiet for long uses `iter_read` and leaves after an empty page. A call that waits on a quiet Stream holds its Workflow Task open, with one Workflow Task about every 8 s. The calls of one Workflow Task write one marker in History. `StreamConfig.workflow_read_replay_from` decides whether the marker holds the Records (`StreamReplaySource.HISTORY`) or only their offsets and a digest, which a replay reads from the Store again (`StreamReplaySource.STORE`, the default). With `HISTORY`, one Workflow Task's marker holds at most 1 MiB of Records, and the read goes on in the next task. +A Workflow's append runs in Core inside the Workflow Task, with no Activity. It writes as the Workflow, with its Workflow Task as the epoch. A `producer=` of another kind raises `StreamError` with the reason `STREAM_ERROR_REASON_INVALID`. +A Workflow's close runs in Core, with no Activity, and spans two Workflow Tasks. The task that calls `close` records the intent, Core closes the Stream after the server took that task, and the next task gives the result, so a failed task closes nothing. Await the close. A graceful shutdown of the Worker waits at most 10 s for a close that the code doesn't await, and a Worker that dies when the Workflow ends loses it. An append and a close of Workflow code always carry the Workflow Task as their epoch. An append is at-least-once. After a Worker crash or an eviction, the rerun of its task can write a second copy with the epoch of the new task. +Core records metrics for the Direct calls of a Stream, such as `temporal_stream_read_total`, `temporal_stream_read_records` and `temporal_stream_close_latency`, each with the attribute `stream_store`, and `temporal_stream_close_abandoned` for a close that a graceful shutdown gave up. +Add `StreamConfig.workflow_read_ahead_bytes`, the most bytes of one read's Records that wait in the Worker for Workflow code, 4 MiB by default. +Add `temporalio.exceptions.StreamReplayError`. With it in `workflow_failure_exception_types`, a replay that can't read the Records of a read from the Store again fails the Workflow, and not only the Workflow Task. +Core tries a read, an append or a close of Workflow code again while its Store is unavailable, from 100 ms up to 2 s apart, for at most 30 s. Only then does Workflow code get the retryable `StreamError`. A replay tries its Store for 5 s. If the Store stays down, only the Workflow Task fails, with `StreamReplayUnavailable`, also with `StreamReplayError` in `workflow_failure_exception_types`. +In Workflow code, `temporalio.contrib.streams.current_producer` gives the Workflow's Producer, and `BufferedAppender` writes the appends of one activation as one append, with no timer. diff --git a/scripts/gen_nexus_system_api.py b/scripts/gen_nexus_system_api.py index fdf38fa90c..b62c423ef8 100644 --- a/scripts/gen_nexus_system_api.py +++ b/scripts/gen_nexus_system_api.py @@ -28,8 +28,9 @@ workflow_init_path = base_dir / "temporalio" / "workflow" / "__init__.py" NEX_GEN_VERSION = "0.2.8" # The Stream package needs a newer nexgen, which rejects the other WITs. One pin is enough -# again once the api WITs and a nexgen release agree. The read, read pages and read page methods -# of a poll are only in commit 07b565b6 of the fork https://github.com/moetemp/nexgen. So +# again once the api WITs and a nexgen release agree. The Stream WIT uses `@nexus.helper` with +# `sync=true` and `@nexus.support-type`, which only the fork https://github.com/moetemp/nexgen +# has. Its commit 2785462e also has the poll iterators of the Workflow target. So # NEX_GEN_STREAM_BIN points at a build of that commit. NEX_GEN_STREAM_VERSION = "0.2.12" proto_files = [ @@ -88,7 +89,7 @@ def nex_gen_stream_command() -> list[str]: bin_path = os.environ.get("NEX_GEN_STREAM_BIN") if not bin_path: raise RuntimeError( - "NEX_GEN_STREAM_BIN must point at a nexgen build of commit 07b565b6 " + "NEX_GEN_STREAM_BIN must point at a nexgen build of commit 2785462e " "of https://github.com/moetemp/nexgen" ) version = subprocess.check_output([bin_path, "--version"], text=True).strip() diff --git a/temporalio/api/stream/v1/message_pb2.pyi b/temporalio/api/stream/v1/message_pb2.pyi index 1d01036026..1e42e74a20 100644 --- a/temporalio/api/stream/v1/message_pb2.pyi +++ b/temporalio/api/stream/v1/message_pb2.pyi @@ -167,8 +167,8 @@ global___ActivityEpoch = ActivityEpoch @typing.final class WorkflowProducer(google.protobuf.message.Message): """Experimental. Might change in the future. - A Workflow run Producer. On the server's Streams, a Workflow writes as this Producer in its - Workflow Task. On a Store, Workflow code writes through a Local Activity, so its writes carry a + A Workflow run Producer. Workflow code that writes in its Workflow Task writes as this + Producer, with that task as the epoch. A write that it makes through a Local Activity has a Local Activity Producer. """ @@ -180,7 +180,9 @@ class WorkflowProducer(google.protobuf.message.Message): """Required. The run id of the Workflow run, not of its chain. Each run is its own Producer.""" @property def epoch(self) -> global___WorkflowTaskEpoch: - """The Workflow Task of the write.""" + """The Workflow Task of the write. Workflow code leaves it empty, and the SDK sets it before + the write goes to the Store. + """ def __init__( self, diff --git a/temporalio/bridge/Cargo.lock b/temporalio/bridge/Cargo.lock index 3285c012ca..8a76ca598a 100644 --- a/temporalio/bridge/Cargo.lock +++ b/temporalio/bridge/Cargo.lock @@ -2765,6 +2765,7 @@ dependencies = [ "temporalio-client", "temporalio-common", "temporalio-macros", + "temporalio-streams", "thiserror", "tokio", "tokio-stream", diff --git a/temporalio/bridge/_visitor.py b/temporalio/bridge/_visitor.py index 8832b56bc4..54238f8916 100644 --- a/temporalio/bridge/_visitor.py +++ b/temporalio/bridge/_visitor.py @@ -308,6 +308,38 @@ async def _visit_coresdk_workflow_activation_ResolveNexusOperation( if o.HasField("result"): await self._visit_coresdk_nexus_NexusOperationResult(fs, o.result) + async def _visit_temporal_api_stream_v1_StreamRecord( + self, fs: VisitorFunctions, o: Any + ): + if o.HasField("body"): + await self._visit_temporal_api_common_v1_Payload(fs, o.body) + + async def _visit_coresdk_workflow_activation_StreamReadEnd( + self, fs: VisitorFunctions, o: Any + ): + if o.HasField("failure"): + await self._visit_temporal_api_failure_v1_Failure(fs, o.failure) + + async def _visit_coresdk_workflow_activation_ResolveStreamRead( + self, fs: VisitorFunctions, o: Any + ): + for v in o.records: + await self._visit_temporal_api_stream_v1_StreamRecord(fs, v) + if o.HasField("end"): + await self._visit_coresdk_workflow_activation_StreamReadEnd(fs, o.end) + + async def _visit_coresdk_workflow_activation_ResolveStreamAppend( + self, fs: VisitorFunctions, o: Any + ): + if o.HasField("failed"): + await self._visit_temporal_api_failure_v1_Failure(fs, o.failed) + + async def _visit_coresdk_workflow_activation_ResolveStreamClose( + self, fs: VisitorFunctions, o: Any + ): + if o.HasField("failed"): + await self._visit_temporal_api_failure_v1_Failure(fs, o.failed) + async def _visit_coresdk_workflow_activation_WorkflowActivationJob( self, fs: VisitorFunctions, o: Any ): @@ -353,6 +385,18 @@ async def _visit_coresdk_workflow_activation_WorkflowActivationJob( await self._visit_coresdk_workflow_activation_ResolveNexusOperation( fs, o.resolve_nexus_operation ) + elif o.HasField("resolve_stream_read"): + await self._visit_coresdk_workflow_activation_ResolveStreamRead( + fs, o.resolve_stream_read + ) + elif o.HasField("resolve_stream_append"): + await self._visit_coresdk_workflow_activation_ResolveStreamAppend( + fs, o.resolve_stream_append + ) + elif o.HasField("resolve_stream_close"): + await self._visit_coresdk_workflow_activation_ResolveStreamClose( + fs, o.resolve_stream_close + ) async def _visit_coresdk_workflow_activation_WorkflowActivation( self, fs: VisitorFunctions, o: Any @@ -492,6 +536,11 @@ async def _visit_coresdk_workflow_commands_ScheduleNexusOperation( if o.HasField("input"): await self._visit_nexus_operation_input_payload(fs, o.input) + async def _visit_coresdk_workflow_commands_AppendToStream( + self, fs: VisitorFunctions, o: Any + ): + await self._visit_payload_container(fs, o.bodies) + async def _visit_coresdk_workflow_commands_WorkflowCommand( self, fs: VisitorFunctions, o: Any ): @@ -551,6 +600,10 @@ async def _visit_coresdk_workflow_commands_WorkflowCommand( await self._visit_coresdk_workflow_commands_ScheduleNexusOperation( fs, o.schedule_nexus_operation ) + elif o.HasField("append_to_stream"): + await self._visit_coresdk_workflow_commands_AppendToStream( + fs, o.append_to_stream + ) async def _visit_coresdk_workflow_completion_Success( self, fs: VisitorFunctions, o: Any @@ -681,12 +734,6 @@ async def _visit_temporal_api_streamservice_v1_ReadRecordsRequest( ) -> None: pass - async def _visit_temporal_api_stream_v1_StreamRecord( - self, fs: VisitorFunctions, o: Any - ): - if o.HasField("body"): - await self._visit_temporal_api_common_v1_Payload(fs, o.body) - async def _visit_temporal_api_streamservice_v1_ReadRecordsResponse( self, fs: VisitorFunctions, o: Any ): diff --git a/temporalio/bridge/proto/common/__init__.py b/temporalio/bridge/proto/common/__init__.py index a8506090de..b9b0cce027 100644 --- a/temporalio/bridge/proto/common/__init__.py +++ b/temporalio/bridge/proto/common/__init__.py @@ -1,6 +1,7 @@ from .common_pb2 import ( ExternalStorageMetrics, NamespacedWorkflowExecution, + StreamReadStop, VersioningIntent, WorkerDeploymentVersion, ) @@ -8,6 +9,7 @@ __all__ = [ "ExternalStorageMetrics", "NamespacedWorkflowExecution", + "StreamReadStop", "VersioningIntent", "WorkerDeploymentVersion", ] diff --git a/temporalio/bridge/proto/common/common_pb2.py b/temporalio/bridge/proto/common/common_pb2.py index 0b9a5c8169..e492e93c10 100644 --- a/temporalio/bridge/proto/common/common_pb2.py +++ b/temporalio/bridge/proto/common/common_pb2.py @@ -16,7 +16,7 @@ from google.protobuf import duration_pb2 as google_dot_protobuf_dot_duration__pb2 DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile( - b'\n%temporal/sdk/core/common/common.proto\x12\x0e\x63oresdk.common\x1a\x1egoogle/protobuf/duration.proto"U\n\x1bNamespacedWorkflowExecution\x12\x11\n\tnamespace\x18\x01 \x01(\t\x12\x13\n\x0bworkflow_id\x18\x02 \x01(\t\x12\x0e\n\x06run_id\x18\x03 \x01(\t"D\n\x17WorkerDeploymentVersion\x12\x17\n\x0f\x64\x65ployment_name\x18\x01 \x01(\t\x12\x10\n\x08\x62uild_id\x18\x02 \x01(\t"\x92\x01\n\x16\x45xternalStorageMetrics\x12\x15\n\rpayload_count\x18\x01 \x01(\x04\x12\x18\n\x10total_size_bytes\x18\x02 \x01(\x04\x12\x31\n\x0etotal_duration\x18\x03 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x14\n\x0c\x64river_names\x18\x04 \x03(\t*@\n\x10VersioningIntent\x12\x0f\n\x0bUNSPECIFIED\x10\x00\x12\x0e\n\nCOMPATIBLE\x10\x01\x12\x0b\n\x07\x44\x45\x46\x41ULT\x10\x02\x42,\xea\x02)Temporalio::Internal::Bridge::Api::Commonb\x06proto3' + b'\n%temporal/sdk/core/common/common.proto\x12\x0e\x63oresdk.common\x1a\x1egoogle/protobuf/duration.proto"U\n\x1bNamespacedWorkflowExecution\x12\x11\n\tnamespace\x18\x01 \x01(\t\x12\x13\n\x0bworkflow_id\x18\x02 \x01(\t\x12\x0e\n\x06run_id\x18\x03 \x01(\t"D\n\x17WorkerDeploymentVersion\x12\x17\n\x0f\x64\x65ployment_name\x18\x01 \x01(\t\x12\x10\n\x08\x62uild_id\x18\x02 \x01(\t"\x92\x01\n\x16\x45xternalStorageMetrics\x12\x15\n\rpayload_count\x18\x01 \x01(\x04\x12\x18\n\x10total_size_bytes\x18\x02 \x01(\x04\x12\x31\n\x0etotal_duration\x18\x03 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x14\n\x0c\x64river_names\x18\x04 \x03(\t*\xc8\x01\n\x0eStreamReadStop\x12 \n\x1cSTREAM_READ_STOP_UNSPECIFIED\x10\x00\x12\x19\n\x15STREAM_READ_STOP_DONE\x10\x01\x12 \n\x1cSTREAM_READ_STOP_MAX_RECORDS\x10\x02\x12\x1e\n\x1aSTREAM_READ_STOP_MAX_BYTES\x10\x03\x12\x19\n\x15STREAM_READ_STOP_IDLE\x10\x04\x12\x1c\n\x18STREAM_READ_STOP_TIMEOUT\x10\x05*@\n\x10VersioningIntent\x12\x0f\n\x0bUNSPECIFIED\x10\x00\x12\x0e\n\nCOMPATIBLE\x10\x01\x12\x0b\n\x07\x44\x45\x46\x41ULT\x10\x02\x42,\xea\x02)Temporalio::Internal::Bridge::Api::Commonb\x06proto3' ) _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals()) @@ -26,8 +26,10 @@ DESCRIPTOR._serialized_options = ( b"\352\002)Temporalio::Internal::Bridge::Api::Common" ) - _VERSIONINGINTENT._serialized_start = 395 - _VERSIONINGINTENT._serialized_end = 459 + _STREAMREADSTOP._serialized_start = 396 + _STREAMREADSTOP._serialized_end = 596 + _VERSIONINGINTENT._serialized_start = 598 + _VERSIONINGINTENT._serialized_end = 662 _NAMESPACEDWORKFLOWEXECUTION._serialized_start = 89 _NAMESPACEDWORKFLOWEXECUTION._serialized_end = 174 _WORKERDEPLOYMENTVERSION._serialized_start = 176 diff --git a/temporalio/bridge/proto/common/common_pb2.pyi b/temporalio/bridge/proto/common/common_pb2.pyi index 7c2b057024..da85e098dd 100644 --- a/temporalio/bridge/proto/common/common_pb2.pyi +++ b/temporalio/bridge/proto/common/common_pb2.pyi @@ -21,6 +21,45 @@ else: DESCRIPTOR: google.protobuf.descriptor.FileDescriptor +class _StreamReadStop: + ValueType = typing.NewType("ValueType", builtins.int) + V: typing_extensions.TypeAlias = ValueType + +class _StreamReadStopEnumTypeWrapper( + google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[ + _StreamReadStop.ValueType + ], + builtins.type, +): + DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor + STREAM_READ_STOP_UNSPECIFIED: _StreamReadStop.ValueType # 0 + STREAM_READ_STOP_DONE: _StreamReadStop.ValueType # 1 + """The Stream is closed, and the read gave its last Record.""" + STREAM_READ_STOP_MAX_RECORDS: _StreamReadStop.ValueType # 2 + """The read gave `max_records` Records.""" + STREAM_READ_STOP_MAX_BYTES: _StreamReadStop.ValueType # 3 + """The next Record would pass `max_bytes`.""" + STREAM_READ_STOP_IDLE: _StreamReadStop.ValueType # 4 + """No Record came for `idle_timeout`.""" + STREAM_READ_STOP_TIMEOUT: _StreamReadStop.ValueType # 5 + """`timeout` passed since the read started.""" + +class StreamReadStop(_StreamReadStop, metaclass=_StreamReadStopEnumTypeWrapper): + """Why a read of a Stream ended. A read that failed has no stop.""" + +STREAM_READ_STOP_UNSPECIFIED: StreamReadStop.ValueType # 0 +STREAM_READ_STOP_DONE: StreamReadStop.ValueType # 1 +"""The Stream is closed, and the read gave its last Record.""" +STREAM_READ_STOP_MAX_RECORDS: StreamReadStop.ValueType # 2 +"""The read gave `max_records` Records.""" +STREAM_READ_STOP_MAX_BYTES: StreamReadStop.ValueType # 3 +"""The next Record would pass `max_bytes`.""" +STREAM_READ_STOP_IDLE: StreamReadStop.ValueType # 4 +"""No Record came for `idle_timeout`.""" +STREAM_READ_STOP_TIMEOUT: StreamReadStop.ValueType # 5 +"""`timeout` passed since the read started.""" +global___StreamReadStop = StreamReadStop + class _VersioningIntent: ValueType = typing.NewType("ValueType", builtins.int) V: typing_extensions.TypeAlias = ValueType diff --git a/temporalio/bridge/proto/external_data/__init__.py b/temporalio/bridge/proto/external_data/__init__.py index 836e97ef43..ffe40588eb 100644 --- a/temporalio/bridge/proto/external_data/__init__.py +++ b/temporalio/bridge/proto/external_data/__init__.py @@ -1,6 +1,19 @@ -from .external_data_pb2 import LocalActivityMarkerData, PatchedMarkerData +from .external_data_pb2 import ( + LocalActivityMarkerData, + PatchedMarkerData, + StreamAppendEntry, + StreamCloseEntry, + StreamMarkerData, + StreamReadEnded, + StreamReadEntry, +) __all__ = [ "LocalActivityMarkerData", "PatchedMarkerData", + "StreamAppendEntry", + "StreamCloseEntry", + "StreamMarkerData", + "StreamReadEnded", + "StreamReadEntry", ] diff --git a/temporalio/bridge/proto/external_data/external_data_pb2.py b/temporalio/bridge/proto/external_data/external_data_pb2.py index 4bda6f5144..3e13e28c1f 100644 --- a/temporalio/bridge/proto/external_data/external_data_pb2.py +++ b/temporalio/bridge/proto/external_data/external_data_pb2.py @@ -16,8 +16,18 @@ from google.protobuf import duration_pb2 as google_dot_protobuf_dot_duration__pb2 from google.protobuf import timestamp_pb2 as google_dot_protobuf_dot_timestamp__pb2 +from temporalio.api.failure.v1 import ( + message_pb2 as temporal_dot_api_dot_failure_dot_v1_dot_message__pb2, +) +from temporalio.api.stream.v1 import ( + message_pb2 as temporal_dot_api_dot_stream_dot_v1_dot_message__pb2, +) +from temporalio.bridge.proto.common import ( + common_pb2 as temporal_dot_sdk_dot_core_dot_common_dot_common__pb2, +) + DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile( - b'\n3temporal/sdk/core/external_data/external_data.proto\x12\x15\x63oresdk.external_data\x1a\x1egoogle/protobuf/duration.proto\x1a\x1fgoogle/protobuf/timestamp.proto"\xb2\x02\n\x17LocalActivityMarkerData\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x0f\n\x07\x61ttempt\x18\x02 \x01(\r\x12\x13\n\x0b\x61\x63tivity_id\x18\x03 \x01(\t\x12\x15\n\ractivity_type\x18\x04 \x01(\t\x12\x31\n\rcomplete_time\x18\x05 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12*\n\x07\x62\x61\x63koff\x18\x06 \x01(\x0b\x32\x19.google.protobuf.Duration\x12:\n\x16original_schedule_time\x18\x07 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x1d\n\x10\x61\x63tivation_index\x18\x08 \x01(\x04H\x00\x88\x01\x01\x42\x13\n\x11_activation_index"3\n\x11PatchedMarkerData\x12\n\n\x02id\x18\x01 \x01(\t\x12\x12\n\ndeprecated\x18\x02 \x01(\x08\x42\x32\xea\x02/Temporalio::Internal::Bridge::Api::ExternalDatab\x06proto3' + b'\n3temporal/sdk/core/external_data/external_data.proto\x12\x15\x63oresdk.external_data\x1a\x1egoogle/protobuf/duration.proto\x1a\x1fgoogle/protobuf/timestamp.proto\x1a%temporal/api/failure/v1/message.proto\x1a$temporal/api/stream/v1/message.proto\x1a%temporal/sdk/core/common/common.proto"\xb2\x02\n\x17LocalActivityMarkerData\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x0f\n\x07\x61ttempt\x18\x02 \x01(\r\x12\x13\n\x0b\x61\x63tivity_id\x18\x03 \x01(\t\x12\x15\n\ractivity_type\x18\x04 \x01(\t\x12\x31\n\rcomplete_time\x18\x05 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12*\n\x07\x62\x61\x63koff\x18\x06 \x01(\x0b\x32\x19.google.protobuf.Duration\x12:\n\x16original_schedule_time\x18\x07 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x1d\n\x10\x61\x63tivation_index\x18\x08 \x01(\x04H\x00\x88\x01\x01\x42\x13\n\x11_activation_index"3\n\x11PatchedMarkerData\x12\n\n\x02id\x18\x01 \x01(\t\x12\x12\n\ndeprecated\x18\x02 \x01(\x08"\xf1\x01\n\x10StreamMarkerData\x12\x35\n\x05reads\x18\x01 \x03(\x0b\x32&.coresdk.external_data.StreamReadEntry\x12\x39\n\x07\x61ppends\x18\x02 \x03(\x0b\x32(.coresdk.external_data.StreamAppendEntry\x12\x37\n\x06\x63loses\x18\x03 \x03(\x0b\x32\'.coresdk.external_data.StreamCloseEntry\x12\x32\n\x07streams\x18\x04 \x03(\x0b\x32!.temporal.api.stream.v1.StreamRef"\xae\x02\n\x0fStreamReadEntry\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x14\n\x0cstream_index\x18\x02 \x01(\r\x12\x13\n\x0b\x66rom_offset\x18\x03 \x01(\x0c\x12\x14\n\x0c\x61\x66ter_offset\x18\x04 \x01(\x0c\x12\x13\n\x0blast_offset\x18\x05 \x01(\x0c\x12\r\n\x05\x63ount\x18\x06 \x01(\r\x12\x0e\n\x06\x64igest\x18\x07 \x01(\x0c\x12\x35\n\x07records\x18\x08 \x03(\x0b\x32$.temporal.api.stream.v1.StreamRecord\x12\x13\n\x0bpage_counts\x18\t \x03(\r\x12\x18\n\x10page_activations\x18\n \x03(\r\x12\x33\n\x03\x65nd\x18\x0b \x01(\x0b\x32&.coresdk.external_data.StreamReadEnded"\xa9\x01\n\x0fStreamReadEnded\x12\x13\n\x0blast_offset\x18\x01 \x01(\x0c\x12,\n\x04stop\x18\x02 \x01(\x0e\x32\x1e.coresdk.common.StreamReadStop\x12\x0c\n\x04\x64one\x18\x03 \x01(\x08\x12\x31\n\x07\x66\x61ilure\x18\x04 \x01(\x0b\x32 .temporal.api.failure.v1.Failure\x12\x12\n\nactivation\x18\x05 \x01(\r"\xa8\x01\n\x11StreamAppendEntry\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x14\n\x0cstream_index\x18\x02 \x01(\r\x12\x14\n\x0c\x66irst_offset\x18\x03 \x01(\x0c\x12\x13\n\x0blast_offset\x18\x04 \x01(\x0c\x12\x31\n\x07\x66\x61ilure\x18\x05 \x01(\x0b\x32 .temporal.api.failure.v1.Failure\x12\x12\n\nactivation\x18\x06 \x01(\r"\xcf\x01\n\x10StreamCloseEntry\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x14\n\x0cstream_index\x18\x02 \x01(\r\x12?\n\x0cintent_epoch\x18\x03 \x01(\x0b\x32).temporal.api.stream.v1.WorkflowTaskEpoch\x12\x10\n\x08resolved\x18\x04 \x01(\x08\x12\x31\n\x07\x66\x61ilure\x18\x05 \x01(\x0b\x32 .temporal.api.failure.v1.Failure\x12\x12\n\nactivation\x18\x06 \x01(\rB2\xea\x02/Temporalio::Internal::Bridge::Api::ExternalDatab\x06proto3' ) _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals()) @@ -27,8 +37,18 @@ DESCRIPTOR._serialized_options = ( b"\352\002/Temporalio::Internal::Bridge::Api::ExternalData" ) - _LOCALACTIVITYMARKERDATA._serialized_start = 144 - _LOCALACTIVITYMARKERDATA._serialized_end = 450 - _PATCHEDMARKERDATA._serialized_start = 452 - _PATCHEDMARKERDATA._serialized_end = 503 + _LOCALACTIVITYMARKERDATA._serialized_start = 260 + _LOCALACTIVITYMARKERDATA._serialized_end = 566 + _PATCHEDMARKERDATA._serialized_start = 568 + _PATCHEDMARKERDATA._serialized_end = 619 + _STREAMMARKERDATA._serialized_start = 622 + _STREAMMARKERDATA._serialized_end = 863 + _STREAMREADENTRY._serialized_start = 866 + _STREAMREADENTRY._serialized_end = 1168 + _STREAMREADENDED._serialized_start = 1171 + _STREAMREADENDED._serialized_end = 1340 + _STREAMAPPENDENTRY._serialized_start = 1343 + _STREAMAPPENDENTRY._serialized_end = 1511 + _STREAMCLOSEENTRY._serialized_start = 1514 + _STREAMCLOSEENTRY._serialized_end = 1721 # @@protoc_insertion_point(module_scope) diff --git a/temporalio/bridge/proto/external_data/external_data_pb2.pyi b/temporalio/bridge/proto/external_data/external_data_pb2.pyi index edcd4581a1..61b5009772 100644 --- a/temporalio/bridge/proto/external_data/external_data_pb2.pyi +++ b/temporalio/bridge/proto/external_data/external_data_pb2.pyi @@ -4,13 +4,19 @@ isort:skip_file """ import builtins +import collections.abc import typing import google.protobuf.descriptor import google.protobuf.duration_pb2 +import google.protobuf.internal.containers import google.protobuf.message import google.protobuf.timestamp_pb2 +import temporalio.api.failure.v1.message_pb2 +import temporalio.api.stream.v1.message_pb2 +import temporalio.bridge.proto.common.common_pb2 + DESCRIPTOR: google.protobuf.descriptor.FileDescriptor @typing.final @@ -139,3 +145,340 @@ class PatchedMarkerData(google.protobuf.message.Message): ) -> None: ... global___PatchedMarkerData = PatchedMarkerData + +@typing.final +class StreamMarkerData(google.protobuf.message.Message): + """The data of the marker `core_stream`, which Core records once for each workflow task in which + the workflow got a Stream result. It is in the detail `data` as `binary/protobuf`, so that a + marker with Records stays small. + """ + + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + READS_FIELD_NUMBER: builtins.int + APPENDS_FIELD_NUMBER: builtins.int + CLOSES_FIELD_NUMBER: builtins.int + STREAMS_FIELD_NUMBER: builtins.int + @property + def reads( + self, + ) -> google.protobuf.internal.containers.RepeatedCompositeFieldContainer[ + global___StreamReadEntry + ]: ... + @property + def appends( + self, + ) -> google.protobuf.internal.containers.RepeatedCompositeFieldContainer[ + global___StreamAppendEntry + ]: ... + @property + def closes( + self, + ) -> google.protobuf.internal.containers.RepeatedCompositeFieldContainer[ + global___StreamCloseEntry + ]: ... + @property + def streams( + self, + ) -> google.protobuf.internal.containers.RepeatedCompositeFieldContainer[ + temporalio.api.stream.v1.message_pb2.StreamRef + ]: + """Each Stream of the entries, once. An entry gives the index of its Stream here, so that a task + with many results on one Stream holds its ref once. + """ + + def __init__( + self, + *, + reads: collections.abc.Iterable[global___StreamReadEntry] | None = ..., + appends: collections.abc.Iterable[global___StreamAppendEntry] | None = ..., + closes: collections.abc.Iterable[global___StreamCloseEntry] | None = ..., + streams: collections.abc.Iterable[ + temporalio.api.stream.v1.message_pb2.StreamRef + ] + | None = ..., + ) -> None: ... + def ClearField( + self, + field_name: typing.Literal[ + "appends", + b"appends", + "closes", + b"closes", + "reads", + b"reads", + "streams", + b"streams", + ], + ) -> None: ... + +global___StreamMarkerData = StreamMarkerData + +@typing.final +class StreamReadEntry(google.protobuf.message.Message): + """What one read gave the workflow in one workflow task.""" + + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + SEQ_FIELD_NUMBER: builtins.int + STREAM_INDEX_FIELD_NUMBER: builtins.int + FROM_OFFSET_FIELD_NUMBER: builtins.int + AFTER_OFFSET_FIELD_NUMBER: builtins.int + LAST_OFFSET_FIELD_NUMBER: builtins.int + COUNT_FIELD_NUMBER: builtins.int + DIGEST_FIELD_NUMBER: builtins.int + RECORDS_FIELD_NUMBER: builtins.int + PAGE_COUNTS_FIELD_NUMBER: builtins.int + PAGE_ACTIVATIONS_FIELD_NUMBER: builtins.int + END_FIELD_NUMBER: builtins.int + seq: builtins.int + stream_index: builtins.int + """The index of the read's Stream in `StreamMarkerData.streams`.""" + from_offset: builtins.bytes + """The `from_offset` of the read, so a replay can check that the read is the same.""" + after_offset: builtins.bytes + """The offset before the first Record of this task. A replay reads after it.""" + last_offset: builtins.bytes + """The offset of the last Record of this task. Empty when the task got no Record.""" + count: builtins.int + """The number of Records of this task.""" + digest: builtins.bytes + """The digest of the Records of this task. Empty when `records` holds them.""" + @property + def records( + self, + ) -> google.protobuf.internal.containers.RepeatedCompositeFieldContainer[ + temporalio.api.stream.v1.message_pb2.StreamRecord + ]: + """The Records of this task, only for a read with its Records in history.""" + + @property + def page_counts( + self, + ) -> google.protobuf.internal.containers.RepeatedScalarFieldContainer[builtins.int]: + """The number of Records of each page, in order. A page ends where its count ends.""" + + @property + def page_activations( + self, + ) -> google.protobuf.internal.containers.RepeatedScalarFieldContainer[builtins.int]: + """The activation of each page in the task. The first is the index, and each later one is the + gap to the one before. + """ + + @property + def end(self) -> global___StreamReadEnded: + """Set when the read ended in this task.""" + + def __init__( + self, + *, + seq: builtins.int = ..., + stream_index: builtins.int = ..., + from_offset: builtins.bytes = ..., + after_offset: builtins.bytes = ..., + last_offset: builtins.bytes = ..., + count: builtins.int = ..., + digest: builtins.bytes = ..., + records: collections.abc.Iterable[ + temporalio.api.stream.v1.message_pb2.StreamRecord + ] + | None = ..., + page_counts: collections.abc.Iterable[builtins.int] | None = ..., + page_activations: collections.abc.Iterable[builtins.int] | None = ..., + end: global___StreamReadEnded | None = ..., + ) -> None: ... + def HasField(self, field_name: typing.Literal["end", b"end"]) -> builtins.bool: ... + def ClearField( + self, + field_name: typing.Literal[ + "after_offset", + b"after_offset", + "count", + b"count", + "digest", + b"digest", + "end", + b"end", + "from_offset", + b"from_offset", + "last_offset", + b"last_offset", + "page_activations", + b"page_activations", + "page_counts", + b"page_counts", + "records", + b"records", + "seq", + b"seq", + "stream_index", + b"stream_index", + ], + ) -> None: ... + +global___StreamReadEntry = StreamReadEntry + +@typing.final +class StreamReadEnded(google.protobuf.message.Message): + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + LAST_OFFSET_FIELD_NUMBER: builtins.int + STOP_FIELD_NUMBER: builtins.int + DONE_FIELD_NUMBER: builtins.int + FAILURE_FIELD_NUMBER: builtins.int + ACTIVATION_FIELD_NUMBER: builtins.int + last_offset: builtins.bytes + stop: temporalio.bridge.proto.common.common_pb2.StreamReadStop.ValueType + done: builtins.bool + activation: builtins.int + """The activation in the task that got the end.""" + @property + def failure(self) -> temporalio.api.failure.v1.message_pb2.Failure: ... + def __init__( + self, + *, + last_offset: builtins.bytes = ..., + stop: temporalio.bridge.proto.common.common_pb2.StreamReadStop.ValueType = ..., + done: builtins.bool = ..., + failure: temporalio.api.failure.v1.message_pb2.Failure | None = ..., + activation: builtins.int = ..., + ) -> None: ... + def HasField( + self, field_name: typing.Literal["failure", b"failure"] + ) -> builtins.bool: ... + def ClearField( + self, + field_name: typing.Literal[ + "activation", + b"activation", + "done", + b"done", + "failure", + b"failure", + "last_offset", + b"last_offset", + "stop", + b"stop", + ], + ) -> None: ... + +global___StreamReadEnded = StreamReadEnded + +@typing.final +class StreamAppendEntry(google.protobuf.message.Message): + """The result of one append, in the workflow task whose activation got it.""" + + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + SEQ_FIELD_NUMBER: builtins.int + STREAM_INDEX_FIELD_NUMBER: builtins.int + FIRST_OFFSET_FIELD_NUMBER: builtins.int + LAST_OFFSET_FIELD_NUMBER: builtins.int + FAILURE_FIELD_NUMBER: builtins.int + ACTIVATION_FIELD_NUMBER: builtins.int + seq: builtins.int + stream_index: builtins.int + """The index of the append's Stream in `StreamMarkerData.streams`.""" + first_offset: builtins.bytes + last_offset: builtins.bytes + activation: builtins.int + """The activation in the task that got the result.""" + @property + def failure(self) -> temporalio.api.failure.v1.message_pb2.Failure: ... + def __init__( + self, + *, + seq: builtins.int = ..., + stream_index: builtins.int = ..., + first_offset: builtins.bytes = ..., + last_offset: builtins.bytes = ..., + failure: temporalio.api.failure.v1.message_pb2.Failure | None = ..., + activation: builtins.int = ..., + ) -> None: ... + def HasField( + self, field_name: typing.Literal["failure", b"failure"] + ) -> builtins.bool: ... + def ClearField( + self, + field_name: typing.Literal[ + "activation", + b"activation", + "failure", + b"failure", + "first_offset", + b"first_offset", + "last_offset", + b"last_offset", + "seq", + b"seq", + "stream_index", + b"stream_index", + ], + ) -> None: ... + +global___StreamAppendEntry = StreamAppendEntry + +@typing.final +class StreamCloseEntry(google.protobuf.message.Message): + """A close in one workflow task: its intent, in the task that asked for it, or its result, in the + task whose activation got it. + """ + + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + SEQ_FIELD_NUMBER: builtins.int + STREAM_INDEX_FIELD_NUMBER: builtins.int + INTENT_EPOCH_FIELD_NUMBER: builtins.int + RESOLVED_FIELD_NUMBER: builtins.int + FAILURE_FIELD_NUMBER: builtins.int + ACTIVATION_FIELD_NUMBER: builtins.int + seq: builtins.int + stream_index: builtins.int + """The index of the closed Stream in `StreamMarkerData.streams`.""" + resolved: builtins.bool + """True on the result. `failure` is unset when the Stream is closed.""" + activation: builtins.int + """The activation in the task that got the result.""" + @property + def intent_epoch(self) -> temporalio.api.stream.v1.message_pb2.WorkflowTaskEpoch: + """Set on the intent, the epoch that Core closes with after the server took the task.""" + + @property + def failure(self) -> temporalio.api.failure.v1.message_pb2.Failure: ... + def __init__( + self, + *, + seq: builtins.int = ..., + stream_index: builtins.int = ..., + intent_epoch: temporalio.api.stream.v1.message_pb2.WorkflowTaskEpoch + | None = ..., + resolved: builtins.bool = ..., + failure: temporalio.api.failure.v1.message_pb2.Failure | None = ..., + activation: builtins.int = ..., + ) -> None: ... + def HasField( + self, + field_name: typing.Literal[ + "failure", b"failure", "intent_epoch", b"intent_epoch" + ], + ) -> builtins.bool: ... + def ClearField( + self, + field_name: typing.Literal[ + "activation", + b"activation", + "failure", + b"failure", + "intent_epoch", + b"intent_epoch", + "resolved", + b"resolved", + "seq", + b"seq", + "stream_index", + b"stream_index", + ], + ) -> None: ... + +global___StreamCloseEntry = StreamCloseEntry diff --git a/temporalio/bridge/proto/workflow_activation/__init__.py b/temporalio/bridge/proto/workflow_activation/__init__.py index 5c178e2185..4e459cc9b5 100644 --- a/temporalio/bridge/proto/workflow_activation/__init__.py +++ b/temporalio/bridge/proto/workflow_activation/__init__.py @@ -16,7 +16,12 @@ ResolveNexusOperationStart, ResolveRequestCancelExternalWorkflow, ResolveSignalExternalWorkflow, + ResolveStreamAppend, + ResolveStreamClose, + ResolveStreamRead, SignalWorkflow, + StreamAppended, + StreamReadEnd, UpdateRandomSeed, WorkflowActivation, WorkflowActivationJob, @@ -40,7 +45,12 @@ "ResolveNexusOperationStart", "ResolveRequestCancelExternalWorkflow", "ResolveSignalExternalWorkflow", + "ResolveStreamAppend", + "ResolveStreamClose", + "ResolveStreamRead", "SignalWorkflow", + "StreamAppended", + "StreamReadEnd", "UpdateRandomSeed", "WorkflowActivation", "WorkflowActivationJob", diff --git a/temporalio/bridge/proto/workflow_activation/workflow_activation_pb2.py b/temporalio/bridge/proto/workflow_activation/workflow_activation_pb2.py index 589119c72b..863fbc082a 100644 --- a/temporalio/bridge/proto/workflow_activation/workflow_activation_pb2.py +++ b/temporalio/bridge/proto/workflow_activation/workflow_activation_pb2.py @@ -32,6 +32,9 @@ from temporalio.api.nexus.v1 import ( message_pb2 as temporal_dot_api_dot_nexus_dot_v1_dot_message__pb2, ) +from temporalio.api.stream.v1 import ( + message_pb2 as temporal_dot_api_dot_stream_dot_v1_dot_message__pb2, +) from temporalio.api.update.v1 import ( message_pb2 as temporal_dot_api_dot_update_dot_v1_dot_message__pb2, ) @@ -49,7 +52,7 @@ ) DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile( - b'\n?temporal/sdk/core/workflow_activation/workflow_activation.proto\x12\x1b\x63oresdk.workflow_activation\x1a\x1fgoogle/protobuf/timestamp.proto\x1a\x1egoogle/protobuf/duration.proto\x1a\x1bgoogle/protobuf/empty.proto\x1a%temporal/api/failure/v1/message.proto\x1a#temporal/api/nexus/v1/message.proto\x1a$temporal/api/update/v1/message.proto\x1a$temporal/api/common/v1/message.proto\x1a(temporal/api/enums/v1/failed_cause.proto\x1a$temporal/api/enums/v1/workflow.proto\x1a\x37temporal/sdk/core/activity_result/activity_result.proto\x1a\x35temporal/sdk/core/child_workflow/child_workflow.proto\x1a%temporal/sdk/core/common/common.proto\x1a#temporal/sdk/core/nexus/nexus.proto"\xa2\x04\n\x12WorkflowActivation\x12\x0e\n\x06run_id\x18\x01 \x01(\t\x12-\n\ttimestamp\x18\x02 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x14\n\x0cis_replaying\x18\x03 \x01(\x08\x12\x16\n\x0ehistory_length\x18\x04 \x01(\r\x12@\n\x04jobs\x18\x05 \x03(\x0b\x32\x32.coresdk.workflow_activation.WorkflowActivationJob\x12 \n\x18\x61vailable_internal_flags\x18\x06 \x03(\r\x12\x1a\n\x12history_size_bytes\x18\x07 \x01(\x04\x12!\n\x19\x63ontinue_as_new_suggested\x18\x08 \x01(\x08\x12T\n#deployment_version_for_current_task\x18\t \x01(\x0b\x32\'.coresdk.common.WorkerDeploymentVersion\x12\x18\n\x10last_sdk_version\x18\n \x01(\t\x12Z\n\x1fsuggest_continue_as_new_reasons\x18\x0b \x03(\x0e\x32\x31.temporal.api.enums.v1.SuggestContinueAsNewReason\x12\x30\n(target_worker_deployment_version_changed\x18\x0c \x01(\x08"\xe0\n\n\x15WorkflowActivationJob\x12N\n\x13initialize_workflow\x18\x01 \x01(\x0b\x32/.coresdk.workflow_activation.InitializeWorkflowH\x00\x12<\n\nfire_timer\x18\x02 \x01(\x0b\x32&.coresdk.workflow_activation.FireTimerH\x00\x12K\n\x12update_random_seed\x18\x04 \x01(\x0b\x32-.coresdk.workflow_activation.UpdateRandomSeedH\x00\x12\x44\n\x0equery_workflow\x18\x05 \x01(\x0b\x32*.coresdk.workflow_activation.QueryWorkflowH\x00\x12\x46\n\x0f\x63\x61ncel_workflow\x18\x06 \x01(\x0b\x32+.coresdk.workflow_activation.CancelWorkflowH\x00\x12\x46\n\x0fsignal_workflow\x18\x07 \x01(\x0b\x32+.coresdk.workflow_activation.SignalWorkflowH\x00\x12H\n\x10resolve_activity\x18\x08 \x01(\x0b\x32,.coresdk.workflow_activation.ResolveActivityH\x00\x12G\n\x10notify_has_patch\x18\t \x01(\x0b\x32+.coresdk.workflow_activation.NotifyHasPatchH\x00\x12q\n&resolve_child_workflow_execution_start\x18\n \x01(\x0b\x32?.coresdk.workflow_activation.ResolveChildWorkflowExecutionStartH\x00\x12\x66\n resolve_child_workflow_execution\x18\x0b \x01(\x0b\x32:.coresdk.workflow_activation.ResolveChildWorkflowExecutionH\x00\x12\x66\n resolve_signal_external_workflow\x18\x0c \x01(\x0b\x32:.coresdk.workflow_activation.ResolveSignalExternalWorkflowH\x00\x12u\n(resolve_request_cancel_external_workflow\x18\r \x01(\x0b\x32\x41.coresdk.workflow_activation.ResolveRequestCancelExternalWorkflowH\x00\x12:\n\tdo_update\x18\x0e \x01(\x0b\x32%.coresdk.workflow_activation.DoUpdateH\x00\x12`\n\x1dresolve_nexus_operation_start\x18\x0f \x01(\x0b\x32\x37.coresdk.workflow_activation.ResolveNexusOperationStartH\x00\x12U\n\x17resolve_nexus_operation\x18\x10 \x01(\x0b\x32\x32.coresdk.workflow_activation.ResolveNexusOperationH\x00\x12I\n\x11remove_from_cache\x18\x32 \x01(\x0b\x32,.coresdk.workflow_activation.RemoveFromCacheH\x00\x42\t\n\x07variant"\xe3\x0b\n\x12InitializeWorkflow\x12\x15\n\rworkflow_type\x18\x01 \x01(\t\x12\x13\n\x0bworkflow_id\x18\x02 \x01(\t\x12\x32\n\targuments\x18\x03 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12\x17\n\x0frandomness_seed\x18\x04 \x01(\x04\x12M\n\x07headers\x18\x05 \x03(\x0b\x32<.coresdk.workflow_activation.InitializeWorkflow.HeadersEntry\x12\x10\n\x08identity\x18\x06 \x01(\t\x12I\n\x14parent_workflow_info\x18\x07 \x01(\x0b\x32+.coresdk.common.NamespacedWorkflowExecution\x12=\n\x1aworkflow_execution_timeout\x18\x08 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x37\n\x14workflow_run_timeout\x18\t \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x38\n\x15workflow_task_timeout\x18\n \x01(\x0b\x32\x19.google.protobuf.Duration\x12\'\n\x1f\x63ontinued_from_execution_run_id\x18\x0b \x01(\t\x12J\n\x13\x63ontinued_initiator\x18\x0c \x01(\x0e\x32-.temporal.api.enums.v1.ContinueAsNewInitiator\x12;\n\x11\x63ontinued_failure\x18\r \x01(\x0b\x32 .temporal.api.failure.v1.Failure\x12@\n\x16last_completion_result\x18\x0e \x01(\x0b\x32 .temporal.api.common.v1.Payloads\x12\x1e\n\x16\x66irst_execution_run_id\x18\x0f \x01(\t\x12\x39\n\x0cretry_policy\x18\x10 \x01(\x0b\x32#.temporal.api.common.v1.RetryPolicy\x12\x0f\n\x07\x61ttempt\x18\x11 \x01(\x05\x12\x15\n\rcron_schedule\x18\x12 \x01(\t\x12\x46\n"workflow_execution_expiration_time\x18\x13 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x45\n"cron_schedule_to_schedule_interval\x18\x14 \x01(\x0b\x32\x19.google.protobuf.Duration\x12*\n\x04memo\x18\x15 \x01(\x0b\x32\x1c.temporal.api.common.v1.Memo\x12\x43\n\x11search_attributes\x18\x16 \x01(\x0b\x32(.temporal.api.common.v1.SearchAttributes\x12.\n\nstart_time\x18\x17 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12@\n\rroot_workflow\x18\x18 \x01(\x0b\x32).temporal.api.common.v1.WorkflowExecution\x12\x32\n\x08priority\x18\x19 \x01(\x0b\x32 .temporal.api.common.v1.Priority\x12!\n\x19original_execution_run_id\x18\x1a \x01(\t\x12\x65\n&propagated_nexus_serialization_context\x18\x1b \x01(\x0b\x32\x35.temporal.api.nexus.v1.PropagatedSerializationContext\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01"\x18\n\tFireTimer\x12\x0b\n\x03seq\x18\x01 \x01(\r"m\n\x0fResolveActivity\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12;\n\x06result\x18\x02 \x01(\x0b\x32+.coresdk.activity_result.ActivityResolution\x12\x10\n\x08is_local\x18\x03 \x01(\x08"\xd1\x02\n"ResolveChildWorkflowExecutionStart\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12[\n\tsucceeded\x18\x02 \x01(\x0b\x32\x46.coresdk.workflow_activation.ResolveChildWorkflowExecutionStartSuccessH\x00\x12X\n\x06\x66\x61iled\x18\x03 \x01(\x0b\x32\x46.coresdk.workflow_activation.ResolveChildWorkflowExecutionStartFailureH\x00\x12]\n\tcancelled\x18\x04 \x01(\x0b\x32H.coresdk.workflow_activation.ResolveChildWorkflowExecutionStartCancelledH\x00\x42\x08\n\x06status";\n)ResolveChildWorkflowExecutionStartSuccess\x12\x0e\n\x06run_id\x18\x01 \x01(\t"\xa6\x01\n)ResolveChildWorkflowExecutionStartFailure\x12\x13\n\x0bworkflow_id\x18\x01 \x01(\t\x12\x15\n\rworkflow_type\x18\x02 \x01(\t\x12M\n\x05\x63\x61use\x18\x03 \x01(\x0e\x32>.coresdk.child_workflow.StartChildWorkflowExecutionFailedCause"`\n+ResolveChildWorkflowExecutionStartCancelled\x12\x31\n\x07\x66\x61ilure\x18\x01 \x01(\x0b\x32 .temporal.api.failure.v1.Failure"i\n\x1dResolveChildWorkflowExecution\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12;\n\x06result\x18\x02 \x01(\x0b\x32+.coresdk.child_workflow.ChildWorkflowResult"+\n\x10UpdateRandomSeed\x12\x17\n\x0frandomness_seed\x18\x01 \x01(\x04"\x84\x02\n\rQueryWorkflow\x12\x10\n\x08query_id\x18\x01 \x01(\t\x12\x12\n\nquery_type\x18\x02 \x01(\t\x12\x32\n\targuments\x18\x03 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12H\n\x07headers\x18\x05 \x03(\x0b\x32\x37.coresdk.workflow_activation.QueryWorkflow.HeadersEntry\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01" \n\x0e\x43\x61ncelWorkflow\x12\x0e\n\x06reason\x18\x01 \x01(\t"\xa1\x02\n\x0eSignalWorkflow\x12\x13\n\x0bsignal_name\x18\x01 \x01(\t\x12.\n\x05input\x18\x02 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12\x10\n\x08identity\x18\x03 \x01(\t\x12I\n\x07headers\x18\x05 \x03(\x0b\x32\x38.coresdk.workflow_activation.SignalWorkflow.HeadersEntry\x12\x1c\n\x14originating_event_id\x18\x06 \x01(\x03\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01""\n\x0eNotifyHasPatch\x12\x10\n\x08patch_id\x18\x01 \x01(\t"\xb1\x01\n\x1dResolveSignalExternalWorkflow\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x31\n\x07\x66\x61ilure\x18\x02 \x01(\x0b\x32 .temporal.api.failure.v1.Failure\x12P\n\x05\x63\x61use\x18\x03 \x01(\x0e\x32\x41.temporal.api.enums.v1.SignalExternalWorkflowExecutionFailedCause"\xb8\x01\n$ResolveRequestCancelExternalWorkflow\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x31\n\x07\x66\x61ilure\x18\x02 \x01(\x0b\x32 .temporal.api.failure.v1.Failure\x12P\n\x05\x63\x61use\x18\x03 \x01(\x0e\x32\x41.temporal.api.enums.v1.CancelExternalWorkflowExecutionFailedCause"\xb2\x03\n\x08\x44oUpdate\x12\n\n\x02id\x18\x01 \x01(\t\x12\x1c\n\x14protocol_instance_id\x18\x02 \x01(\t\x12\x0c\n\x04name\x18\x03 \x01(\t\x12.\n\x05input\x18\x04 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12\x43\n\x07headers\x18\x05 \x03(\x0b\x32\x32.coresdk.workflow_activation.DoUpdate.HeadersEntry\x12*\n\x04meta\x18\x06 \x01(\x0b\x32\x1c.temporal.api.update.v1.Meta\x12\x15\n\rrun_validator\x18\x07 \x01(\x08\x12\x65\n&propagated_nexus_serialization_context\x18\x08 \x01(\x0b\x32\x35.temporal.api.nexus.v1.PropagatedSerializationContext\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01"\x9a\x01\n\x1aResolveNexusOperationStart\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x19\n\x0foperation_token\x18\x02 \x01(\tH\x00\x12\x16\n\x0cstarted_sync\x18\x03 \x01(\x08H\x00\x12\x32\n\x06\x66\x61iled\x18\x04 \x01(\x0b\x32 .temporal.api.failure.v1.FailureH\x00\x42\x08\n\x06status"Y\n\x15ResolveNexusOperation\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x33\n\x06result\x18\x02 \x01(\x0b\x32#.coresdk.nexus.NexusOperationResult"\xe0\x02\n\x0fRemoveFromCache\x12\x0f\n\x07message\x18\x01 \x01(\t\x12K\n\x06reason\x18\x02 \x01(\x0e\x32;.coresdk.workflow_activation.RemoveFromCache.EvictionReason"\xee\x01\n\x0e\x45victionReason\x12\x0f\n\x0bUNSPECIFIED\x10\x00\x12\x0e\n\nCACHE_FULL\x10\x01\x12\x0e\n\nCACHE_MISS\x10\x02\x12\x12\n\x0eNONDETERMINISM\x10\x03\x12\r\n\tLANG_FAIL\x10\x04\x12\x12\n\x0eLANG_REQUESTED\x10\x05\x12\x12\n\x0eTASK_NOT_FOUND\x10\x06\x12\x15\n\x11UNHANDLED_COMMAND\x10\x07\x12\t\n\x05\x46\x41TAL\x10\x08\x12\x1f\n\x1bPAGINATION_OR_HISTORY_FETCH\x10\t\x12\x1d\n\x19WORKFLOW_EXECUTION_ENDING\x10\nB8\xea\x02\x35Temporalio::Internal::Bridge::Api::WorkflowActivationb\x06proto3' + b'\n?temporal/sdk/core/workflow_activation/workflow_activation.proto\x12\x1b\x63oresdk.workflow_activation\x1a\x1fgoogle/protobuf/timestamp.proto\x1a\x1egoogle/protobuf/duration.proto\x1a\x1bgoogle/protobuf/empty.proto\x1a%temporal/api/failure/v1/message.proto\x1a#temporal/api/nexus/v1/message.proto\x1a$temporal/api/update/v1/message.proto\x1a$temporal/api/common/v1/message.proto\x1a$temporal/api/stream/v1/message.proto\x1a(temporal/api/enums/v1/failed_cause.proto\x1a$temporal/api/enums/v1/workflow.proto\x1a\x37temporal/sdk/core/activity_result/activity_result.proto\x1a\x35temporal/sdk/core/child_workflow/child_workflow.proto\x1a%temporal/sdk/core/common/common.proto\x1a#temporal/sdk/core/nexus/nexus.proto"\xa2\x04\n\x12WorkflowActivation\x12\x0e\n\x06run_id\x18\x01 \x01(\t\x12-\n\ttimestamp\x18\x02 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x14\n\x0cis_replaying\x18\x03 \x01(\x08\x12\x16\n\x0ehistory_length\x18\x04 \x01(\r\x12@\n\x04jobs\x18\x05 \x03(\x0b\x32\x32.coresdk.workflow_activation.WorkflowActivationJob\x12 \n\x18\x61vailable_internal_flags\x18\x06 \x03(\r\x12\x1a\n\x12history_size_bytes\x18\x07 \x01(\x04\x12!\n\x19\x63ontinue_as_new_suggested\x18\x08 \x01(\x08\x12T\n#deployment_version_for_current_task\x18\t \x01(\x0b\x32\'.coresdk.common.WorkerDeploymentVersion\x12\x18\n\x10last_sdk_version\x18\n \x01(\t\x12Z\n\x1fsuggest_continue_as_new_reasons\x18\x0b \x03(\x0e\x32\x31.temporal.api.enums.v1.SuggestContinueAsNewReason\x12\x30\n(target_worker_deployment_version_changed\x18\x0c \x01(\x08"\xd3\x0c\n\x15WorkflowActivationJob\x12N\n\x13initialize_workflow\x18\x01 \x01(\x0b\x32/.coresdk.workflow_activation.InitializeWorkflowH\x00\x12<\n\nfire_timer\x18\x02 \x01(\x0b\x32&.coresdk.workflow_activation.FireTimerH\x00\x12K\n\x12update_random_seed\x18\x04 \x01(\x0b\x32-.coresdk.workflow_activation.UpdateRandomSeedH\x00\x12\x44\n\x0equery_workflow\x18\x05 \x01(\x0b\x32*.coresdk.workflow_activation.QueryWorkflowH\x00\x12\x46\n\x0f\x63\x61ncel_workflow\x18\x06 \x01(\x0b\x32+.coresdk.workflow_activation.CancelWorkflowH\x00\x12\x46\n\x0fsignal_workflow\x18\x07 \x01(\x0b\x32+.coresdk.workflow_activation.SignalWorkflowH\x00\x12H\n\x10resolve_activity\x18\x08 \x01(\x0b\x32,.coresdk.workflow_activation.ResolveActivityH\x00\x12G\n\x10notify_has_patch\x18\t \x01(\x0b\x32+.coresdk.workflow_activation.NotifyHasPatchH\x00\x12q\n&resolve_child_workflow_execution_start\x18\n \x01(\x0b\x32?.coresdk.workflow_activation.ResolveChildWorkflowExecutionStartH\x00\x12\x66\n resolve_child_workflow_execution\x18\x0b \x01(\x0b\x32:.coresdk.workflow_activation.ResolveChildWorkflowExecutionH\x00\x12\x66\n resolve_signal_external_workflow\x18\x0c \x01(\x0b\x32:.coresdk.workflow_activation.ResolveSignalExternalWorkflowH\x00\x12u\n(resolve_request_cancel_external_workflow\x18\r \x01(\x0b\x32\x41.coresdk.workflow_activation.ResolveRequestCancelExternalWorkflowH\x00\x12:\n\tdo_update\x18\x0e \x01(\x0b\x32%.coresdk.workflow_activation.DoUpdateH\x00\x12`\n\x1dresolve_nexus_operation_start\x18\x0f \x01(\x0b\x32\x37.coresdk.workflow_activation.ResolveNexusOperationStartH\x00\x12U\n\x17resolve_nexus_operation\x18\x10 \x01(\x0b\x32\x32.coresdk.workflow_activation.ResolveNexusOperationH\x00\x12M\n\x13resolve_stream_read\x18\x11 \x01(\x0b\x32..coresdk.workflow_activation.ResolveStreamReadH\x00\x12Q\n\x15resolve_stream_append\x18\x12 \x01(\x0b\x32\x30.coresdk.workflow_activation.ResolveStreamAppendH\x00\x12O\n\x14resolve_stream_close\x18\x13 \x01(\x0b\x32/.coresdk.workflow_activation.ResolveStreamCloseH\x00\x12I\n\x11remove_from_cache\x18\x32 \x01(\x0b\x32,.coresdk.workflow_activation.RemoveFromCacheH\x00\x42\t\n\x07variant"\xe3\x0b\n\x12InitializeWorkflow\x12\x15\n\rworkflow_type\x18\x01 \x01(\t\x12\x13\n\x0bworkflow_id\x18\x02 \x01(\t\x12\x32\n\targuments\x18\x03 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12\x17\n\x0frandomness_seed\x18\x04 \x01(\x04\x12M\n\x07headers\x18\x05 \x03(\x0b\x32<.coresdk.workflow_activation.InitializeWorkflow.HeadersEntry\x12\x10\n\x08identity\x18\x06 \x01(\t\x12I\n\x14parent_workflow_info\x18\x07 \x01(\x0b\x32+.coresdk.common.NamespacedWorkflowExecution\x12=\n\x1aworkflow_execution_timeout\x18\x08 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x37\n\x14workflow_run_timeout\x18\t \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x38\n\x15workflow_task_timeout\x18\n \x01(\x0b\x32\x19.google.protobuf.Duration\x12\'\n\x1f\x63ontinued_from_execution_run_id\x18\x0b \x01(\t\x12J\n\x13\x63ontinued_initiator\x18\x0c \x01(\x0e\x32-.temporal.api.enums.v1.ContinueAsNewInitiator\x12;\n\x11\x63ontinued_failure\x18\r \x01(\x0b\x32 .temporal.api.failure.v1.Failure\x12@\n\x16last_completion_result\x18\x0e \x01(\x0b\x32 .temporal.api.common.v1.Payloads\x12\x1e\n\x16\x66irst_execution_run_id\x18\x0f \x01(\t\x12\x39\n\x0cretry_policy\x18\x10 \x01(\x0b\x32#.temporal.api.common.v1.RetryPolicy\x12\x0f\n\x07\x61ttempt\x18\x11 \x01(\x05\x12\x15\n\rcron_schedule\x18\x12 \x01(\t\x12\x46\n"workflow_execution_expiration_time\x18\x13 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x45\n"cron_schedule_to_schedule_interval\x18\x14 \x01(\x0b\x32\x19.google.protobuf.Duration\x12*\n\x04memo\x18\x15 \x01(\x0b\x32\x1c.temporal.api.common.v1.Memo\x12\x43\n\x11search_attributes\x18\x16 \x01(\x0b\x32(.temporal.api.common.v1.SearchAttributes\x12.\n\nstart_time\x18\x17 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12@\n\rroot_workflow\x18\x18 \x01(\x0b\x32).temporal.api.common.v1.WorkflowExecution\x12\x32\n\x08priority\x18\x19 \x01(\x0b\x32 .temporal.api.common.v1.Priority\x12!\n\x19original_execution_run_id\x18\x1a \x01(\t\x12\x65\n&propagated_nexus_serialization_context\x18\x1b \x01(\x0b\x32\x35.temporal.api.nexus.v1.PropagatedSerializationContext\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01"\x18\n\tFireTimer\x12\x0b\n\x03seq\x18\x01 \x01(\r"m\n\x0fResolveActivity\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12;\n\x06result\x18\x02 \x01(\x0b\x32+.coresdk.activity_result.ActivityResolution\x12\x10\n\x08is_local\x18\x03 \x01(\x08"\xd1\x02\n"ResolveChildWorkflowExecutionStart\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12[\n\tsucceeded\x18\x02 \x01(\x0b\x32\x46.coresdk.workflow_activation.ResolveChildWorkflowExecutionStartSuccessH\x00\x12X\n\x06\x66\x61iled\x18\x03 \x01(\x0b\x32\x46.coresdk.workflow_activation.ResolveChildWorkflowExecutionStartFailureH\x00\x12]\n\tcancelled\x18\x04 \x01(\x0b\x32H.coresdk.workflow_activation.ResolveChildWorkflowExecutionStartCancelledH\x00\x42\x08\n\x06status";\n)ResolveChildWorkflowExecutionStartSuccess\x12\x0e\n\x06run_id\x18\x01 \x01(\t"\xa6\x01\n)ResolveChildWorkflowExecutionStartFailure\x12\x13\n\x0bworkflow_id\x18\x01 \x01(\t\x12\x15\n\rworkflow_type\x18\x02 \x01(\t\x12M\n\x05\x63\x61use\x18\x03 \x01(\x0e\x32>.coresdk.child_workflow.StartChildWorkflowExecutionFailedCause"`\n+ResolveChildWorkflowExecutionStartCancelled\x12\x31\n\x07\x66\x61ilure\x18\x01 \x01(\x0b\x32 .temporal.api.failure.v1.Failure"i\n\x1dResolveChildWorkflowExecution\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12;\n\x06result\x18\x02 \x01(\x0b\x32+.coresdk.child_workflow.ChildWorkflowResult"+\n\x10UpdateRandomSeed\x12\x17\n\x0frandomness_seed\x18\x01 \x01(\x04"\x84\x02\n\rQueryWorkflow\x12\x10\n\x08query_id\x18\x01 \x01(\t\x12\x12\n\nquery_type\x18\x02 \x01(\t\x12\x32\n\targuments\x18\x03 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12H\n\x07headers\x18\x05 \x03(\x0b\x32\x37.coresdk.workflow_activation.QueryWorkflow.HeadersEntry\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01" \n\x0e\x43\x61ncelWorkflow\x12\x0e\n\x06reason\x18\x01 \x01(\t"\xa1\x02\n\x0eSignalWorkflow\x12\x13\n\x0bsignal_name\x18\x01 \x01(\t\x12.\n\x05input\x18\x02 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12\x10\n\x08identity\x18\x03 \x01(\t\x12I\n\x07headers\x18\x05 \x03(\x0b\x32\x38.coresdk.workflow_activation.SignalWorkflow.HeadersEntry\x12\x1c\n\x14originating_event_id\x18\x06 \x01(\x03\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01""\n\x0eNotifyHasPatch\x12\x10\n\x08patch_id\x18\x01 \x01(\t"\xb1\x01\n\x1dResolveSignalExternalWorkflow\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x31\n\x07\x66\x61ilure\x18\x02 \x01(\x0b\x32 .temporal.api.failure.v1.Failure\x12P\n\x05\x63\x61use\x18\x03 \x01(\x0e\x32\x41.temporal.api.enums.v1.SignalExternalWorkflowExecutionFailedCause"\xb8\x01\n$ResolveRequestCancelExternalWorkflow\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x31\n\x07\x66\x61ilure\x18\x02 \x01(\x0b\x32 .temporal.api.failure.v1.Failure\x12P\n\x05\x63\x61use\x18\x03 \x01(\x0e\x32\x41.temporal.api.enums.v1.CancelExternalWorkflowExecutionFailedCause"\xb2\x03\n\x08\x44oUpdate\x12\n\n\x02id\x18\x01 \x01(\t\x12\x1c\n\x14protocol_instance_id\x18\x02 \x01(\t\x12\x0c\n\x04name\x18\x03 \x01(\t\x12.\n\x05input\x18\x04 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12\x43\n\x07headers\x18\x05 \x03(\x0b\x32\x32.coresdk.workflow_activation.DoUpdate.HeadersEntry\x12*\n\x04meta\x18\x06 \x01(\x0b\x32\x1c.temporal.api.update.v1.Meta\x12\x15\n\rrun_validator\x18\x07 \x01(\x08\x12\x65\n&propagated_nexus_serialization_context\x18\x08 \x01(\x0b\x32\x35.temporal.api.nexus.v1.PropagatedSerializationContext\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01"\x9a\x01\n\x1aResolveNexusOperationStart\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x19\n\x0foperation_token\x18\x02 \x01(\tH\x00\x12\x16\n\x0cstarted_sync\x18\x03 \x01(\x08H\x00\x12\x32\n\x06\x66\x61iled\x18\x04 \x01(\x0b\x32 .temporal.api.failure.v1.FailureH\x00\x42\x08\n\x06status"Y\n\x15ResolveNexusOperation\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x33\n\x06result\x18\x02 \x01(\x0b\x32#.coresdk.nexus.NexusOperationResult"\xe0\x02\n\x0fRemoveFromCache\x12\x0f\n\x07message\x18\x01 \x01(\t\x12K\n\x06reason\x18\x02 \x01(\x0e\x32;.coresdk.workflow_activation.RemoveFromCache.EvictionReason"\xee\x01\n\x0e\x45victionReason\x12\x0f\n\x0bUNSPECIFIED\x10\x00\x12\x0e\n\nCACHE_FULL\x10\x01\x12\x0e\n\nCACHE_MISS\x10\x02\x12\x12\n\x0eNONDETERMINISM\x10\x03\x12\r\n\tLANG_FAIL\x10\x04\x12\x12\n\x0eLANG_REQUESTED\x10\x05\x12\x12\n\x0eTASK_NOT_FOUND\x10\x06\x12\x15\n\x11UNHANDLED_COMMAND\x10\x07\x12\t\n\x05\x46\x41TAL\x10\x08\x12\x1f\n\x1bPAGINATION_OR_HISTORY_FETCH\x10\t\x12\x1d\n\x19WORKFLOW_EXECUTION_ENDING\x10\n"\x90\x01\n\x11ResolveStreamRead\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x35\n\x07records\x18\x02 \x03(\x0b\x32$.temporal.api.stream.v1.StreamRecord\x12\x37\n\x03\x65nd\x18\x03 \x01(\x0b\x32*.coresdk.workflow_activation.StreamReadEnd"\x93\x01\n\rStreamReadEnd\x12\x13\n\x0blast_offset\x18\x01 \x01(\x0c\x12,\n\x04stop\x18\x02 \x01(\x0e\x32\x1e.coresdk.common.StreamReadStop\x12\x0c\n\x04\x64one\x18\x03 \x01(\x08\x12\x31\n\x07\x66\x61ilure\x18\x04 \x01(\x0b\x32 .temporal.api.failure.v1.Failure"\xa1\x01\n\x13ResolveStreamAppend\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12?\n\x08\x61ppended\x18\x02 \x01(\x0b\x32+.coresdk.workflow_activation.StreamAppendedH\x00\x12\x32\n\x06\x66\x61iled\x18\x03 \x01(\x0b\x32 .temporal.api.failure.v1.FailureH\x00\x42\x08\n\x06status";\n\x0eStreamAppended\x12\x14\n\x0c\x66irst_offset\x18\x01 \x01(\x0c\x12\x13\n\x0blast_offset\x18\x02 \x01(\x0c"S\n\x12ResolveStreamClose\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x30\n\x06\x66\x61iled\x18\x02 \x01(\x0b\x32 .temporal.api.failure.v1.FailureB8\xea\x02\x35Temporalio::Internal::Bridge::Api::WorkflowActivationb\x06proto3' ) _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals()) @@ -67,56 +70,66 @@ _SIGNALWORKFLOW_HEADERSENTRY._serialized_options = b"8\001" _DOUPDATE_HEADERSENTRY._options = None _DOUPDATE_HEADERSENTRY._serialized_options = b"8\001" - _WORKFLOWACTIVATION._serialized_start = 611 - _WORKFLOWACTIVATION._serialized_end = 1157 - _WORKFLOWACTIVATIONJOB._serialized_start = 1160 - _WORKFLOWACTIVATIONJOB._serialized_end = 2536 - _INITIALIZEWORKFLOW._serialized_start = 2539 - _INITIALIZEWORKFLOW._serialized_end = 4046 - _INITIALIZEWORKFLOW_HEADERSENTRY._serialized_start = 3967 - _INITIALIZEWORKFLOW_HEADERSENTRY._serialized_end = 4046 - _FIRETIMER._serialized_start = 4048 - _FIRETIMER._serialized_end = 4072 - _RESOLVEACTIVITY._serialized_start = 4074 - _RESOLVEACTIVITY._serialized_end = 4183 - _RESOLVECHILDWORKFLOWEXECUTIONSTART._serialized_start = 4186 - _RESOLVECHILDWORKFLOWEXECUTIONSTART._serialized_end = 4523 - _RESOLVECHILDWORKFLOWEXECUTIONSTARTSUCCESS._serialized_start = 4525 - _RESOLVECHILDWORKFLOWEXECUTIONSTARTSUCCESS._serialized_end = 4584 - _RESOLVECHILDWORKFLOWEXECUTIONSTARTFAILURE._serialized_start = 4587 - _RESOLVECHILDWORKFLOWEXECUTIONSTARTFAILURE._serialized_end = 4753 - _RESOLVECHILDWORKFLOWEXECUTIONSTARTCANCELLED._serialized_start = 4755 - _RESOLVECHILDWORKFLOWEXECUTIONSTARTCANCELLED._serialized_end = 4851 - _RESOLVECHILDWORKFLOWEXECUTION._serialized_start = 4853 - _RESOLVECHILDWORKFLOWEXECUTION._serialized_end = 4958 - _UPDATERANDOMSEED._serialized_start = 4960 - _UPDATERANDOMSEED._serialized_end = 5003 - _QUERYWORKFLOW._serialized_start = 5006 - _QUERYWORKFLOW._serialized_end = 5266 - _QUERYWORKFLOW_HEADERSENTRY._serialized_start = 3967 - _QUERYWORKFLOW_HEADERSENTRY._serialized_end = 4046 - _CANCELWORKFLOW._serialized_start = 5268 - _CANCELWORKFLOW._serialized_end = 5300 - _SIGNALWORKFLOW._serialized_start = 5303 - _SIGNALWORKFLOW._serialized_end = 5592 - _SIGNALWORKFLOW_HEADERSENTRY._serialized_start = 3967 - _SIGNALWORKFLOW_HEADERSENTRY._serialized_end = 4046 - _NOTIFYHASPATCH._serialized_start = 5594 - _NOTIFYHASPATCH._serialized_end = 5628 - _RESOLVESIGNALEXTERNALWORKFLOW._serialized_start = 5631 - _RESOLVESIGNALEXTERNALWORKFLOW._serialized_end = 5808 - _RESOLVEREQUESTCANCELEXTERNALWORKFLOW._serialized_start = 5811 - _RESOLVEREQUESTCANCELEXTERNALWORKFLOW._serialized_end = 5995 - _DOUPDATE._serialized_start = 5998 - _DOUPDATE._serialized_end = 6432 - _DOUPDATE_HEADERSENTRY._serialized_start = 3967 - _DOUPDATE_HEADERSENTRY._serialized_end = 4046 - _RESOLVENEXUSOPERATIONSTART._serialized_start = 6435 - _RESOLVENEXUSOPERATIONSTART._serialized_end = 6589 - _RESOLVENEXUSOPERATION._serialized_start = 6591 - _RESOLVENEXUSOPERATION._serialized_end = 6680 - _REMOVEFROMCACHE._serialized_start = 6683 - _REMOVEFROMCACHE._serialized_end = 7035 - _REMOVEFROMCACHE_EVICTIONREASON._serialized_start = 6797 - _REMOVEFROMCACHE_EVICTIONREASON._serialized_end = 7035 + _WORKFLOWACTIVATION._serialized_start = 649 + _WORKFLOWACTIVATION._serialized_end = 1195 + _WORKFLOWACTIVATIONJOB._serialized_start = 1198 + _WORKFLOWACTIVATIONJOB._serialized_end = 2817 + _INITIALIZEWORKFLOW._serialized_start = 2820 + _INITIALIZEWORKFLOW._serialized_end = 4327 + _INITIALIZEWORKFLOW_HEADERSENTRY._serialized_start = 4248 + _INITIALIZEWORKFLOW_HEADERSENTRY._serialized_end = 4327 + _FIRETIMER._serialized_start = 4329 + _FIRETIMER._serialized_end = 4353 + _RESOLVEACTIVITY._serialized_start = 4355 + _RESOLVEACTIVITY._serialized_end = 4464 + _RESOLVECHILDWORKFLOWEXECUTIONSTART._serialized_start = 4467 + _RESOLVECHILDWORKFLOWEXECUTIONSTART._serialized_end = 4804 + _RESOLVECHILDWORKFLOWEXECUTIONSTARTSUCCESS._serialized_start = 4806 + _RESOLVECHILDWORKFLOWEXECUTIONSTARTSUCCESS._serialized_end = 4865 + _RESOLVECHILDWORKFLOWEXECUTIONSTARTFAILURE._serialized_start = 4868 + _RESOLVECHILDWORKFLOWEXECUTIONSTARTFAILURE._serialized_end = 5034 + _RESOLVECHILDWORKFLOWEXECUTIONSTARTCANCELLED._serialized_start = 5036 + _RESOLVECHILDWORKFLOWEXECUTIONSTARTCANCELLED._serialized_end = 5132 + _RESOLVECHILDWORKFLOWEXECUTION._serialized_start = 5134 + _RESOLVECHILDWORKFLOWEXECUTION._serialized_end = 5239 + _UPDATERANDOMSEED._serialized_start = 5241 + _UPDATERANDOMSEED._serialized_end = 5284 + _QUERYWORKFLOW._serialized_start = 5287 + _QUERYWORKFLOW._serialized_end = 5547 + _QUERYWORKFLOW_HEADERSENTRY._serialized_start = 4248 + _QUERYWORKFLOW_HEADERSENTRY._serialized_end = 4327 + _CANCELWORKFLOW._serialized_start = 5549 + _CANCELWORKFLOW._serialized_end = 5581 + _SIGNALWORKFLOW._serialized_start = 5584 + _SIGNALWORKFLOW._serialized_end = 5873 + _SIGNALWORKFLOW_HEADERSENTRY._serialized_start = 4248 + _SIGNALWORKFLOW_HEADERSENTRY._serialized_end = 4327 + _NOTIFYHASPATCH._serialized_start = 5875 + _NOTIFYHASPATCH._serialized_end = 5909 + _RESOLVESIGNALEXTERNALWORKFLOW._serialized_start = 5912 + _RESOLVESIGNALEXTERNALWORKFLOW._serialized_end = 6089 + _RESOLVEREQUESTCANCELEXTERNALWORKFLOW._serialized_start = 6092 + _RESOLVEREQUESTCANCELEXTERNALWORKFLOW._serialized_end = 6276 + _DOUPDATE._serialized_start = 6279 + _DOUPDATE._serialized_end = 6713 + _DOUPDATE_HEADERSENTRY._serialized_start = 4248 + _DOUPDATE_HEADERSENTRY._serialized_end = 4327 + _RESOLVENEXUSOPERATIONSTART._serialized_start = 6716 + _RESOLVENEXUSOPERATIONSTART._serialized_end = 6870 + _RESOLVENEXUSOPERATION._serialized_start = 6872 + _RESOLVENEXUSOPERATION._serialized_end = 6961 + _REMOVEFROMCACHE._serialized_start = 6964 + _REMOVEFROMCACHE._serialized_end = 7316 + _REMOVEFROMCACHE_EVICTIONREASON._serialized_start = 7078 + _REMOVEFROMCACHE_EVICTIONREASON._serialized_end = 7316 + _RESOLVESTREAMREAD._serialized_start = 7319 + _RESOLVESTREAMREAD._serialized_end = 7463 + _STREAMREADEND._serialized_start = 7466 + _STREAMREADEND._serialized_end = 7613 + _RESOLVESTREAMAPPEND._serialized_start = 7616 + _RESOLVESTREAMAPPEND._serialized_end = 7777 + _STREAMAPPENDED._serialized_start = 7779 + _STREAMAPPENDED._serialized_end = 7838 + _RESOLVESTREAMCLOSE._serialized_start = 7840 + _RESOLVESTREAMCLOSE._serialized_end = 7923 # @@protoc_insertion_point(module_scope) diff --git a/temporalio/bridge/proto/workflow_activation/workflow_activation_pb2.pyi b/temporalio/bridge/proto/workflow_activation/workflow_activation_pb2.pyi index af1a2e38ad..de21dd362c 100644 --- a/temporalio/bridge/proto/workflow_activation/workflow_activation_pb2.pyi +++ b/temporalio/bridge/proto/workflow_activation/workflow_activation_pb2.pyi @@ -23,6 +23,7 @@ import temporalio.api.enums.v1.failed_cause_pb2 import temporalio.api.enums.v1.workflow_pb2 import temporalio.api.failure.v1.message_pb2 import temporalio.api.nexus.v1.message_pb2 +import temporalio.api.stream.v1.message_pb2 import temporalio.api.update.v1.message_pb2 import temporalio.bridge.proto.activity_result.activity_result_pb2 import temporalio.bridge.proto.child_workflow.child_workflow_pb2 @@ -244,6 +245,9 @@ class WorkflowActivationJob(google.protobuf.message.Message): DO_UPDATE_FIELD_NUMBER: builtins.int RESOLVE_NEXUS_OPERATION_START_FIELD_NUMBER: builtins.int RESOLVE_NEXUS_OPERATION_FIELD_NUMBER: builtins.int + RESOLVE_STREAM_READ_FIELD_NUMBER: builtins.int + RESOLVE_STREAM_APPEND_FIELD_NUMBER: builtins.int + RESOLVE_STREAM_CLOSE_FIELD_NUMBER: builtins.int REMOVE_FROM_CACHE_FIELD_NUMBER: builtins.int @property def initialize_workflow(self) -> global___InitializeWorkflow: @@ -318,6 +322,18 @@ class WorkflowActivationJob(google.protobuf.message.Message): def resolve_nexus_operation(self) -> global___ResolveNexusOperation: """A nexus operation resolved.""" + @property + def resolve_stream_read(self) -> global___ResolveStreamRead: + """A read of a Stream gave Records, ended, or both.""" + + @property + def resolve_stream_append(self) -> global___ResolveStreamAppend: + """An append to a Stream resolved.""" + + @property + def resolve_stream_close(self) -> global___ResolveStreamClose: + """A close of a Stream resolved.""" + @property def remove_from_cache(self) -> global___RemoveFromCache: """Remove the workflow identified by the [WorkflowActivation] containing this job from the @@ -347,6 +363,9 @@ class WorkflowActivationJob(google.protobuf.message.Message): do_update: global___DoUpdate | None = ..., resolve_nexus_operation_start: global___ResolveNexusOperationStart | None = ..., resolve_nexus_operation: global___ResolveNexusOperation | None = ..., + resolve_stream_read: global___ResolveStreamRead | None = ..., + resolve_stream_append: global___ResolveStreamAppend | None = ..., + resolve_stream_close: global___ResolveStreamClose | None = ..., remove_from_cache: global___RemoveFromCache | None = ..., ) -> None: ... def HasField( @@ -380,6 +399,12 @@ class WorkflowActivationJob(google.protobuf.message.Message): b"resolve_request_cancel_external_workflow", "resolve_signal_external_workflow", b"resolve_signal_external_workflow", + "resolve_stream_append", + b"resolve_stream_append", + "resolve_stream_close", + b"resolve_stream_close", + "resolve_stream_read", + b"resolve_stream_read", "signal_workflow", b"signal_workflow", "update_random_seed", @@ -419,6 +444,12 @@ class WorkflowActivationJob(google.protobuf.message.Message): b"resolve_request_cancel_external_workflow", "resolve_signal_external_workflow", b"resolve_signal_external_workflow", + "resolve_stream_append", + b"resolve_stream_append", + "resolve_stream_close", + b"resolve_stream_close", + "resolve_stream_read", + b"resolve_stream_read", "signal_workflow", b"signal_workflow", "update_random_seed", @@ -446,6 +477,9 @@ class WorkflowActivationJob(google.protobuf.message.Message): "do_update", "resolve_nexus_operation_start", "resolve_nexus_operation", + "resolve_stream_read", + "resolve_stream_append", + "resolve_stream_close", "remove_from_cache", ] | None @@ -1639,3 +1673,186 @@ class RemoveFromCache(google.protobuf.message.Message): ) -> None: ... global___RemoveFromCache = RemoveFromCache + +@typing.final +class ResolveStreamRead(google.protobuf.message.Message): + """The Records that a read of a Stream gave since its last job, and its end if it ended.""" + + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + SEQ_FIELD_NUMBER: builtins.int + RECORDS_FIELD_NUMBER: builtins.int + END_FIELD_NUMBER: builtins.int + seq: builtins.int + """The sequence number of the `StartStreamRead`""" + @property + def records( + self, + ) -> google.protobuf.internal.containers.RepeatedCompositeFieldContainer[ + temporalio.api.stream.v1.message_pb2.StreamRecord + ]: ... + @property + def end(self) -> global___StreamReadEnd: + """Set on the last job of a read.""" + + def __init__( + self, + *, + seq: builtins.int = ..., + records: collections.abc.Iterable[ + temporalio.api.stream.v1.message_pb2.StreamRecord + ] + | None = ..., + end: global___StreamReadEnd | None = ..., + ) -> None: ... + def HasField(self, field_name: typing.Literal["end", b"end"]) -> builtins.bool: ... + def ClearField( + self, + field_name: typing.Literal["end", b"end", "records", b"records", "seq", b"seq"], + ) -> None: ... + +global___ResolveStreamRead = ResolveStreamRead + +@typing.final +class StreamReadEnd(google.protobuf.message.Message): + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + LAST_OFFSET_FIELD_NUMBER: builtins.int + STOP_FIELD_NUMBER: builtins.int + DONE_FIELD_NUMBER: builtins.int + FAILURE_FIELD_NUMBER: builtins.int + last_offset: builtins.bytes + """The offset of the last Record that the workflow got, or `from_offset` if it got none.""" + stop: temporalio.bridge.proto.common.common_pb2.StreamReadStop.ValueType + """Why the read ended. Unspecified when it failed.""" + done: builtins.bool + """True if the Stream is closed and the read gave its last Record.""" + @property + def failure(self) -> temporalio.api.failure.v1.message_pb2.Failure: + """Set if the read failed.""" + + def __init__( + self, + *, + last_offset: builtins.bytes = ..., + stop: temporalio.bridge.proto.common.common_pb2.StreamReadStop.ValueType = ..., + done: builtins.bool = ..., + failure: temporalio.api.failure.v1.message_pb2.Failure | None = ..., + ) -> None: ... + def HasField( + self, field_name: typing.Literal["failure", b"failure"] + ) -> builtins.bool: ... + def ClearField( + self, + field_name: typing.Literal[ + "done", + b"done", + "failure", + b"failure", + "last_offset", + b"last_offset", + "stop", + b"stop", + ], + ) -> None: ... + +global___StreamReadEnd = StreamReadEnd + +@typing.final +class ResolveStreamAppend(google.protobuf.message.Message): + """The result of an `AppendToStream`.""" + + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + SEQ_FIELD_NUMBER: builtins.int + APPENDED_FIELD_NUMBER: builtins.int + FAILED_FIELD_NUMBER: builtins.int + seq: builtins.int + """The sequence number of the `AppendToStream`""" + @property + def appended(self) -> global___StreamAppended: ... + @property + def failed(self) -> temporalio.api.failure.v1.message_pb2.Failure: ... + def __init__( + self, + *, + seq: builtins.int = ..., + appended: global___StreamAppended | None = ..., + failed: temporalio.api.failure.v1.message_pb2.Failure | None = ..., + ) -> None: ... + def HasField( + self, + field_name: typing.Literal[ + "appended", b"appended", "failed", b"failed", "status", b"status" + ], + ) -> builtins.bool: ... + def ClearField( + self, + field_name: typing.Literal[ + "appended", + b"appended", + "failed", + b"failed", + "seq", + b"seq", + "status", + b"status", + ], + ) -> None: ... + def WhichOneof( + self, oneof_group: typing.Literal["status", b"status"] + ) -> typing.Literal["appended", "failed"] | None: ... + +global___ResolveStreamAppend = ResolveStreamAppend + +@typing.final +class StreamAppended(google.protobuf.message.Message): + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + FIRST_OFFSET_FIELD_NUMBER: builtins.int + LAST_OFFSET_FIELD_NUMBER: builtins.int + first_offset: builtins.bytes + last_offset: builtins.bytes + def __init__( + self, + *, + first_offset: builtins.bytes = ..., + last_offset: builtins.bytes = ..., + ) -> None: ... + def ClearField( + self, + field_name: typing.Literal[ + "first_offset", b"first_offset", "last_offset", b"last_offset" + ], + ) -> None: ... + +global___StreamAppended = StreamAppended + +@typing.final +class ResolveStreamClose(google.protobuf.message.Message): + """The result of a `CloseStream`, in a workflow task after the one that asked for it.""" + + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + SEQ_FIELD_NUMBER: builtins.int + FAILED_FIELD_NUMBER: builtins.int + seq: builtins.int + """The sequence number of the `CloseStream`""" + @property + def failed(self) -> temporalio.api.failure.v1.message_pb2.Failure: + """Unset when the Stream is closed, also when it was closed already.""" + + def __init__( + self, + *, + seq: builtins.int = ..., + failed: temporalio.api.failure.v1.message_pb2.Failure | None = ..., + ) -> None: ... + def HasField( + self, field_name: typing.Literal["failed", b"failed"] + ) -> builtins.bool: ... + def ClearField( + self, field_name: typing.Literal["failed", b"failed", "seq", b"seq"] + ) -> None: ... + +global___ResolveStreamClose = ResolveStreamClose diff --git a/temporalio/bridge/proto/workflow_commands/__init__.py b/temporalio/bridge/proto/workflow_commands/__init__.py index f599917afa..15dde24ee9 100644 --- a/temporalio/bridge/proto/workflow_commands/__init__.py +++ b/temporalio/bridge/proto/workflow_commands/__init__.py @@ -1,9 +1,12 @@ from .workflow_commands_pb2 import ( ActivityCancellationType, + AppendToStream, CancelChildWorkflowExecution, CancelSignalWorkflow, CancelTimer, CancelWorkflowExecution, + CloseStream, + CloseStreamRead, CompleteWorkflowExecution, ContinueAsNewWorkflowExecution, FailWorkflowExecution, @@ -20,6 +23,7 @@ SetPatchMarker, SignalExternalWorkflowExecution, StartChildWorkflowExecution, + StartStreamRead, StartTimer, UpdateResponse, UpsertWorkflowSearchAttributes, @@ -28,10 +32,13 @@ __all__ = [ "ActivityCancellationType", + "AppendToStream", "CancelChildWorkflowExecution", "CancelSignalWorkflow", "CancelTimer", "CancelWorkflowExecution", + "CloseStream", + "CloseStreamRead", "CompleteWorkflowExecution", "ContinueAsNewWorkflowExecution", "FailWorkflowExecution", @@ -48,6 +55,7 @@ "SetPatchMarker", "SignalExternalWorkflowExecution", "StartChildWorkflowExecution", + "StartStreamRead", "StartTimer", "UpdateResponse", "UpsertWorkflowSearchAttributes", diff --git a/temporalio/bridge/proto/workflow_commands/workflow_commands_pb2.py b/temporalio/bridge/proto/workflow_commands/workflow_commands_pb2.py index 3573f36b11..1908ea226e 100644 --- a/temporalio/bridge/proto/workflow_commands/workflow_commands_pb2.py +++ b/temporalio/bridge/proto/workflow_commands/workflow_commands_pb2.py @@ -32,6 +32,9 @@ from temporalio.api.sdk.v1 import ( user_metadata_pb2 as temporal_dot_api_dot_sdk_dot_v1_dot_user__metadata__pb2, ) +from temporalio.api.stream.v1 import ( + message_pb2 as temporal_dot_api_dot_stream_dot_v1_dot_message__pb2, +) from temporalio.api.workflow.v1 import ( message_pb2 as temporal_dot_api_dot_workflow_dot_v1_dot_message__pb2, ) @@ -46,7 +49,7 @@ ) DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile( - b'\n;temporal/sdk/core/workflow_commands/workflow_commands.proto\x12\x19\x63oresdk.workflow_commands\x1a\x1egoogle/protobuf/duration.proto\x1a\x1fgoogle/protobuf/timestamp.proto\x1a\x1bgoogle/protobuf/empty.proto\x1a$temporal/api/common/v1/message.proto\x1a$temporal/api/enums/v1/workflow.proto\x1a%temporal/api/failure/v1/message.proto\x1a\'temporal/api/sdk/v1/user_metadata.proto\x1a,temporal/api/sdk/v1/event_group_marker.proto\x1a&temporal/api/workflow/v1/message.proto\x1a\x35temporal/sdk/core/child_workflow/child_workflow.proto\x1a#temporal/sdk/core/nexus/nexus.proto\x1a%temporal/sdk/core/common/common.proto"\xa9\x10\n\x0fWorkflowCommand\x12\x38\n\ruser_metadata\x18\x64 \x01(\x0b\x32!.temporal.api.sdk.v1.UserMetadata\x12\x42\n\x13\x65vent_group_markers\x18\x65 \x03(\x0b\x32%.temporal.api.sdk.v1.EventGroupMarker\x12<\n\x0bstart_timer\x18\x01 \x01(\x0b\x32%.coresdk.workflow_commands.StartTimerH\x00\x12H\n\x11schedule_activity\x18\x02 \x01(\x0b\x32+.coresdk.workflow_commands.ScheduleActivityH\x00\x12\x42\n\x10respond_to_query\x18\x03 \x01(\x0b\x32&.coresdk.workflow_commands.QueryResultH\x00\x12S\n\x17request_cancel_activity\x18\x04 \x01(\x0b\x32\x30.coresdk.workflow_commands.RequestCancelActivityH\x00\x12>\n\x0c\x63\x61ncel_timer\x18\x05 \x01(\x0b\x32&.coresdk.workflow_commands.CancelTimerH\x00\x12[\n\x1b\x63omplete_workflow_execution\x18\x06 \x01(\x0b\x32\x34.coresdk.workflow_commands.CompleteWorkflowExecutionH\x00\x12S\n\x17\x66\x61il_workflow_execution\x18\x07 \x01(\x0b\x32\x30.coresdk.workflow_commands.FailWorkflowExecutionH\x00\x12g\n"continue_as_new_workflow_execution\x18\x08 \x01(\x0b\x32\x39.coresdk.workflow_commands.ContinueAsNewWorkflowExecutionH\x00\x12W\n\x19\x63\x61ncel_workflow_execution\x18\t \x01(\x0b\x32\x32.coresdk.workflow_commands.CancelWorkflowExecutionH\x00\x12\x45\n\x10set_patch_marker\x18\n \x01(\x0b\x32).coresdk.workflow_commands.SetPatchMarkerH\x00\x12`\n\x1estart_child_workflow_execution\x18\x0b \x01(\x0b\x32\x36.coresdk.workflow_commands.StartChildWorkflowExecutionH\x00\x12\x62\n\x1f\x63\x61ncel_child_workflow_execution\x18\x0c \x01(\x0b\x32\x37.coresdk.workflow_commands.CancelChildWorkflowExecutionH\x00\x12w\n*request_cancel_external_workflow_execution\x18\r \x01(\x0b\x32\x41.coresdk.workflow_commands.RequestCancelExternalWorkflowExecutionH\x00\x12h\n"signal_external_workflow_execution\x18\x0e \x01(\x0b\x32:.coresdk.workflow_commands.SignalExternalWorkflowExecutionH\x00\x12Q\n\x16\x63\x61ncel_signal_workflow\x18\x0f \x01(\x0b\x32/.coresdk.workflow_commands.CancelSignalWorkflowH\x00\x12S\n\x17schedule_local_activity\x18\x10 \x01(\x0b\x32\x30.coresdk.workflow_commands.ScheduleLocalActivityH\x00\x12^\n\x1drequest_cancel_local_activity\x18\x11 \x01(\x0b\x32\x35.coresdk.workflow_commands.RequestCancelLocalActivityH\x00\x12\x66\n!upsert_workflow_search_attributes\x18\x12 \x01(\x0b\x32\x39.coresdk.workflow_commands.UpsertWorkflowSearchAttributesH\x00\x12Y\n\x1amodify_workflow_properties\x18\x13 \x01(\x0b\x32\x33.coresdk.workflow_commands.ModifyWorkflowPropertiesH\x00\x12\x44\n\x0fupdate_response\x18\x14 \x01(\x0b\x32).coresdk.workflow_commands.UpdateResponseH\x00\x12U\n\x18schedule_nexus_operation\x18\x15 \x01(\x0b\x32\x31.coresdk.workflow_commands.ScheduleNexusOperationH\x00\x12`\n\x1erequest_cancel_nexus_operation\x18\x16 \x01(\x0b\x32\x36.coresdk.workflow_commands.RequestCancelNexusOperationH\x00\x42\t\n\x07variant"S\n\nStartTimer\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x38\n\x15start_to_fire_timeout\x18\x02 \x01(\x0b\x32\x19.google.protobuf.Duration"\x1a\n\x0b\x43\x61ncelTimer\x12\x0b\n\x03seq\x18\x01 \x01(\r"\xb8\x06\n\x10ScheduleActivity\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x13\n\x0b\x61\x63tivity_id\x18\x02 \x01(\t\x12\x15\n\ractivity_type\x18\x03 \x01(\t\x12\x12\n\ntask_queue\x18\x05 \x01(\t\x12I\n\x07headers\x18\x06 \x03(\x0b\x32\x38.coresdk.workflow_commands.ScheduleActivity.HeadersEntry\x12\x32\n\targuments\x18\x07 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12<\n\x19schedule_to_close_timeout\x18\x08 \x01(\x0b\x32\x19.google.protobuf.Duration\x12<\n\x19schedule_to_start_timeout\x18\t \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x39\n\x16start_to_close_timeout\x18\n \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x34\n\x11heartbeat_timeout\x18\x0b \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x39\n\x0cretry_policy\x18\x0c \x01(\x0b\x32#.temporal.api.common.v1.RetryPolicy\x12N\n\x11\x63\x61ncellation_type\x18\r \x01(\x0e\x32\x33.coresdk.workflow_commands.ActivityCancellationType\x12\x1e\n\x16\x64o_not_eagerly_execute\x18\x0e \x01(\x08\x12;\n\x11versioning_intent\x18\x0f \x01(\x0e\x32 .coresdk.common.VersioningIntent\x12\x32\n\x08priority\x18\x10 \x01(\x0b\x32 .temporal.api.common.v1.Priority\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01"\x93\x06\n\x15ScheduleLocalActivity\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x13\n\x0b\x61\x63tivity_id\x18\x02 \x01(\t\x12\x15\n\ractivity_type\x18\x03 \x01(\t\x12\x0f\n\x07\x61ttempt\x18\x04 \x01(\r\x12:\n\x16original_schedule_time\x18\x05 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12N\n\x07headers\x18\x06 \x03(\x0b\x32=.coresdk.workflow_commands.ScheduleLocalActivity.HeadersEntry\x12\x32\n\targuments\x18\x07 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12<\n\x19schedule_to_close_timeout\x18\x08 \x01(\x0b\x32\x19.google.protobuf.Duration\x12<\n\x19schedule_to_start_timeout\x18\t \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x39\n\x16start_to_close_timeout\x18\n \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x39\n\x0cretry_policy\x18\x0b \x01(\x0b\x32#.temporal.api.common.v1.RetryPolicy\x12\x38\n\x15local_retry_threshold\x18\x0c \x01(\x0b\x32\x19.google.protobuf.Duration\x12N\n\x11\x63\x61ncellation_type\x18\r \x01(\x0e\x32\x33.coresdk.workflow_commands.ActivityCancellationType\x12#\n\x1binclude_arguments_in_marker\x18\x0e \x01(\x08\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01"$\n\x15RequestCancelActivity\x12\x0b\n\x03seq\x18\x01 \x01(\r")\n\x1aRequestCancelLocalActivity\x12\x0b\n\x03seq\x18\x01 \x01(\r"\x9c\x01\n\x0bQueryResult\x12\x10\n\x08query_id\x18\x01 \x01(\t\x12<\n\tsucceeded\x18\x02 \x01(\x0b\x32\'.coresdk.workflow_commands.QuerySuccessH\x00\x12\x32\n\x06\x66\x61iled\x18\x03 \x01(\x0b\x32 .temporal.api.failure.v1.FailureH\x00\x42\t\n\x07variant"A\n\x0cQuerySuccess\x12\x31\n\x08response\x18\x01 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload"L\n\x19\x43ompleteWorkflowExecution\x12/\n\x06result\x18\x01 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload"J\n\x15\x46\x61ilWorkflowExecution\x12\x31\n\x07\x66\x61ilure\x18\x01 \x01(\x0b\x32 .temporal.api.failure.v1.Failure"\x92\x07\n\x1e\x43ontinueAsNewWorkflowExecution\x12\x15\n\rworkflow_type\x18\x01 \x01(\t\x12\x12\n\ntask_queue\x18\x02 \x01(\t\x12\x32\n\targuments\x18\x03 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12\x37\n\x14workflow_run_timeout\x18\x04 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x38\n\x15workflow_task_timeout\x18\x05 \x01(\x0b\x32\x19.google.protobuf.Duration\x12Q\n\x04memo\x18\x06 \x03(\x0b\x32\x43.coresdk.workflow_commands.ContinueAsNewWorkflowExecution.MemoEntry\x12W\n\x07headers\x18\x07 \x03(\x0b\x32\x46.coresdk.workflow_commands.ContinueAsNewWorkflowExecution.HeadersEntry\x12\x43\n\x11search_attributes\x18\x08 \x01(\x0b\x32(.temporal.api.common.v1.SearchAttributes\x12\x39\n\x0cretry_policy\x18\t \x01(\x0b\x32#.temporal.api.common.v1.RetryPolicy\x12;\n\x11versioning_intent\x18\n \x01(\x0e\x32 .coresdk.common.VersioningIntent\x12[\n\x1binitial_versioning_behavior\x18\x0b \x01(\x0e\x32\x36.temporal.api.enums.v1.ContinueAsNewVersioningBehavior\x12\x39\n\x16\x62\x61\x63koff_start_interval\x18\x0c \x01(\x0b\x32\x19.google.protobuf.Duration\x1aL\n\tMemoEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01"L\n\x17\x43\x61ncelWorkflowExecution\x12\x31\n\x07\x64\x65tails\x18\x01 \x01(\x0b\x32 .temporal.api.common.v1.Payloads"6\n\x0eSetPatchMarker\x12\x10\n\x08patch_id\x18\x01 \x01(\t\x12\x12\n\ndeprecated\x18\x02 \x01(\x08"\xe1\t\n\x1bStartChildWorkflowExecution\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x11\n\tnamespace\x18\x02 \x01(\t\x12\x13\n\x0bworkflow_id\x18\x03 \x01(\t\x12\x15\n\rworkflow_type\x18\x04 \x01(\t\x12\x12\n\ntask_queue\x18\x05 \x01(\t\x12.\n\x05input\x18\x06 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12=\n\x1aworkflow_execution_timeout\x18\x07 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x37\n\x14workflow_run_timeout\x18\x08 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x38\n\x15workflow_task_timeout\x18\t \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x46\n\x13parent_close_policy\x18\n \x01(\x0e\x32).coresdk.child_workflow.ParentClosePolicy\x12N\n\x18workflow_id_reuse_policy\x18\x0c \x01(\x0e\x32,.temporal.api.enums.v1.WorkflowIdReusePolicy\x12\x39\n\x0cretry_policy\x18\r \x01(\x0b\x32#.temporal.api.common.v1.RetryPolicy\x12\x15\n\rcron_schedule\x18\x0e \x01(\t\x12T\n\x07headers\x18\x0f \x03(\x0b\x32\x43.coresdk.workflow_commands.StartChildWorkflowExecution.HeadersEntry\x12N\n\x04memo\x18\x10 \x03(\x0b\x32@.coresdk.workflow_commands.StartChildWorkflowExecution.MemoEntry\x12\x43\n\x11search_attributes\x18\x11 \x01(\x0b\x32(.temporal.api.common.v1.SearchAttributes\x12P\n\x11\x63\x61ncellation_type\x18\x12 \x01(\x0e\x32\x35.coresdk.child_workflow.ChildWorkflowCancellationType\x12;\n\x11versioning_intent\x18\x13 \x01(\x0e\x32 .coresdk.common.VersioningIntent\x12\x32\n\x08priority\x18\x14 \x01(\x0b\x32 .temporal.api.common.v1.Priority\x12I\n\x13versioning_override\x18\x15 \x01(\x0b\x32,.temporal.api.workflow.v1.VersioningOverride\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01\x1aL\n\tMemoEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01"J\n\x1c\x43\x61ncelChildWorkflowExecution\x12\x1a\n\x12\x63hild_workflow_seq\x18\x01 \x01(\r\x12\x0e\n\x06reason\x18\x02 \x01(\t"\x8e\x01\n&RequestCancelExternalWorkflowExecution\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12G\n\x12workflow_execution\x18\x02 \x01(\x0b\x32+.coresdk.common.NamespacedWorkflowExecution\x12\x0e\n\x06reason\x18\x03 \x01(\t"\x8f\x03\n\x1fSignalExternalWorkflowExecution\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12I\n\x12workflow_execution\x18\x02 \x01(\x0b\x32+.coresdk.common.NamespacedWorkflowExecutionH\x00\x12\x1b\n\x11\x63hild_workflow_id\x18\x03 \x01(\tH\x00\x12\x13\n\x0bsignal_name\x18\x04 \x01(\t\x12-\n\x04\x61rgs\x18\x05 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12X\n\x07headers\x18\x06 \x03(\x0b\x32G.coresdk.workflow_commands.SignalExternalWorkflowExecution.HeadersEntry\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01\x42\x08\n\x06target"#\n\x14\x43\x61ncelSignalWorkflow\x12\x0b\n\x03seq\x18\x01 \x01(\r"e\n\x1eUpsertWorkflowSearchAttributes\x12\x43\n\x11search_attributes\x18\x01 \x01(\x0b\x32(.temporal.api.common.v1.SearchAttributes"O\n\x18ModifyWorkflowProperties\x12\x33\n\rupserted_memo\x18\x01 \x01(\x0b\x32\x1c.temporal.api.common.v1.Memo"\xd2\x01\n\x0eUpdateResponse\x12\x1c\n\x14protocol_instance_id\x18\x01 \x01(\t\x12*\n\x08\x61\x63\x63\x65pted\x18\x02 \x01(\x0b\x32\x16.google.protobuf.EmptyH\x00\x12\x34\n\x08rejected\x18\x03 \x01(\x0b\x32 .temporal.api.failure.v1.FailureH\x00\x12\x34\n\tcompleted\x18\x04 \x01(\x0b\x32\x1f.temporal.api.common.v1.PayloadH\x00\x42\n\n\x08response"\x9a\x04\n\x16ScheduleNexusOperation\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x10\n\x08\x65ndpoint\x18\x02 \x01(\t\x12\x0f\n\x07service\x18\x03 \x01(\t\x12\x11\n\toperation\x18\x04 \x01(\t\x12.\n\x05input\x18\x05 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12<\n\x19schedule_to_close_timeout\x18\x06 \x01(\x0b\x32\x19.google.protobuf.Duration\x12X\n\x0cnexus_header\x18\x07 \x03(\x0b\x32\x42.coresdk.workflow_commands.ScheduleNexusOperation.NexusHeaderEntry\x12H\n\x11\x63\x61ncellation_type\x18\x08 \x01(\x0e\x32-.coresdk.nexus.NexusOperationCancellationType\x12<\n\x19schedule_to_start_timeout\x18\t \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x39\n\x16start_to_close_timeout\x18\n \x01(\x0b\x32\x19.google.protobuf.Duration\x1a\x32\n\x10NexusHeaderEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01"*\n\x1bRequestCancelNexusOperation\x12\x0b\n\x03seq\x18\x01 \x01(\r*X\n\x18\x41\x63tivityCancellationType\x12\x0e\n\nTRY_CANCEL\x10\x00\x12\x1f\n\x1bWAIT_CANCELLATION_COMPLETED\x10\x01\x12\x0b\n\x07\x41\x42\x41NDON\x10\x02\x42\x36\xea\x02\x33Temporalio::Internal::Bridge::Api::WorkflowCommandsb\x06proto3' + b'\n;temporal/sdk/core/workflow_commands/workflow_commands.proto\x12\x19\x63oresdk.workflow_commands\x1a\x1egoogle/protobuf/duration.proto\x1a\x1fgoogle/protobuf/timestamp.proto\x1a\x1bgoogle/protobuf/empty.proto\x1a$temporal/api/common/v1/message.proto\x1a$temporal/api/enums/v1/workflow.proto\x1a%temporal/api/failure/v1/message.proto\x1a\'temporal/api/sdk/v1/user_metadata.proto\x1a,temporal/api/sdk/v1/event_group_marker.proto\x1a$temporal/api/stream/v1/message.proto\x1a&temporal/api/workflow/v1/message.proto\x1a\x35temporal/sdk/core/child_workflow/child_workflow.proto\x1a#temporal/sdk/core/nexus/nexus.proto\x1a%temporal/sdk/core/common/common.proto"\xc2\x12\n\x0fWorkflowCommand\x12\x38\n\ruser_metadata\x18\x64 \x01(\x0b\x32!.temporal.api.sdk.v1.UserMetadata\x12\x42\n\x13\x65vent_group_markers\x18\x65 \x03(\x0b\x32%.temporal.api.sdk.v1.EventGroupMarker\x12<\n\x0bstart_timer\x18\x01 \x01(\x0b\x32%.coresdk.workflow_commands.StartTimerH\x00\x12H\n\x11schedule_activity\x18\x02 \x01(\x0b\x32+.coresdk.workflow_commands.ScheduleActivityH\x00\x12\x42\n\x10respond_to_query\x18\x03 \x01(\x0b\x32&.coresdk.workflow_commands.QueryResultH\x00\x12S\n\x17request_cancel_activity\x18\x04 \x01(\x0b\x32\x30.coresdk.workflow_commands.RequestCancelActivityH\x00\x12>\n\x0c\x63\x61ncel_timer\x18\x05 \x01(\x0b\x32&.coresdk.workflow_commands.CancelTimerH\x00\x12[\n\x1b\x63omplete_workflow_execution\x18\x06 \x01(\x0b\x32\x34.coresdk.workflow_commands.CompleteWorkflowExecutionH\x00\x12S\n\x17\x66\x61il_workflow_execution\x18\x07 \x01(\x0b\x32\x30.coresdk.workflow_commands.FailWorkflowExecutionH\x00\x12g\n"continue_as_new_workflow_execution\x18\x08 \x01(\x0b\x32\x39.coresdk.workflow_commands.ContinueAsNewWorkflowExecutionH\x00\x12W\n\x19\x63\x61ncel_workflow_execution\x18\t \x01(\x0b\x32\x32.coresdk.workflow_commands.CancelWorkflowExecutionH\x00\x12\x45\n\x10set_patch_marker\x18\n \x01(\x0b\x32).coresdk.workflow_commands.SetPatchMarkerH\x00\x12`\n\x1estart_child_workflow_execution\x18\x0b \x01(\x0b\x32\x36.coresdk.workflow_commands.StartChildWorkflowExecutionH\x00\x12\x62\n\x1f\x63\x61ncel_child_workflow_execution\x18\x0c \x01(\x0b\x32\x37.coresdk.workflow_commands.CancelChildWorkflowExecutionH\x00\x12w\n*request_cancel_external_workflow_execution\x18\r \x01(\x0b\x32\x41.coresdk.workflow_commands.RequestCancelExternalWorkflowExecutionH\x00\x12h\n"signal_external_workflow_execution\x18\x0e \x01(\x0b\x32:.coresdk.workflow_commands.SignalExternalWorkflowExecutionH\x00\x12Q\n\x16\x63\x61ncel_signal_workflow\x18\x0f \x01(\x0b\x32/.coresdk.workflow_commands.CancelSignalWorkflowH\x00\x12S\n\x17schedule_local_activity\x18\x10 \x01(\x0b\x32\x30.coresdk.workflow_commands.ScheduleLocalActivityH\x00\x12^\n\x1drequest_cancel_local_activity\x18\x11 \x01(\x0b\x32\x35.coresdk.workflow_commands.RequestCancelLocalActivityH\x00\x12\x66\n!upsert_workflow_search_attributes\x18\x12 \x01(\x0b\x32\x39.coresdk.workflow_commands.UpsertWorkflowSearchAttributesH\x00\x12Y\n\x1amodify_workflow_properties\x18\x13 \x01(\x0b\x32\x33.coresdk.workflow_commands.ModifyWorkflowPropertiesH\x00\x12\x44\n\x0fupdate_response\x18\x14 \x01(\x0b\x32).coresdk.workflow_commands.UpdateResponseH\x00\x12U\n\x18schedule_nexus_operation\x18\x15 \x01(\x0b\x32\x31.coresdk.workflow_commands.ScheduleNexusOperationH\x00\x12`\n\x1erequest_cancel_nexus_operation\x18\x16 \x01(\x0b\x32\x36.coresdk.workflow_commands.RequestCancelNexusOperationH\x00\x12G\n\x11start_stream_read\x18\x17 \x01(\x0b\x32*.coresdk.workflow_commands.StartStreamReadH\x00\x12G\n\x11\x63lose_stream_read\x18\x18 \x01(\x0b\x32*.coresdk.workflow_commands.CloseStreamReadH\x00\x12\x45\n\x10\x61ppend_to_stream\x18\x19 \x01(\x0b\x32).coresdk.workflow_commands.AppendToStreamH\x00\x12>\n\x0c\x63lose_stream\x18\x1a \x01(\x0b\x32&.coresdk.workflow_commands.CloseStreamH\x00\x42\t\n\x07variant"S\n\nStartTimer\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x38\n\x15start_to_fire_timeout\x18\x02 \x01(\x0b\x32\x19.google.protobuf.Duration"\x1a\n\x0b\x43\x61ncelTimer\x12\x0b\n\x03seq\x18\x01 \x01(\r"\xb8\x06\n\x10ScheduleActivity\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x13\n\x0b\x61\x63tivity_id\x18\x02 \x01(\t\x12\x15\n\ractivity_type\x18\x03 \x01(\t\x12\x12\n\ntask_queue\x18\x05 \x01(\t\x12I\n\x07headers\x18\x06 \x03(\x0b\x32\x38.coresdk.workflow_commands.ScheduleActivity.HeadersEntry\x12\x32\n\targuments\x18\x07 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12<\n\x19schedule_to_close_timeout\x18\x08 \x01(\x0b\x32\x19.google.protobuf.Duration\x12<\n\x19schedule_to_start_timeout\x18\t \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x39\n\x16start_to_close_timeout\x18\n \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x34\n\x11heartbeat_timeout\x18\x0b \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x39\n\x0cretry_policy\x18\x0c \x01(\x0b\x32#.temporal.api.common.v1.RetryPolicy\x12N\n\x11\x63\x61ncellation_type\x18\r \x01(\x0e\x32\x33.coresdk.workflow_commands.ActivityCancellationType\x12\x1e\n\x16\x64o_not_eagerly_execute\x18\x0e \x01(\x08\x12;\n\x11versioning_intent\x18\x0f \x01(\x0e\x32 .coresdk.common.VersioningIntent\x12\x32\n\x08priority\x18\x10 \x01(\x0b\x32 .temporal.api.common.v1.Priority\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01"\x93\x06\n\x15ScheduleLocalActivity\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x13\n\x0b\x61\x63tivity_id\x18\x02 \x01(\t\x12\x15\n\ractivity_type\x18\x03 \x01(\t\x12\x0f\n\x07\x61ttempt\x18\x04 \x01(\r\x12:\n\x16original_schedule_time\x18\x05 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12N\n\x07headers\x18\x06 \x03(\x0b\x32=.coresdk.workflow_commands.ScheduleLocalActivity.HeadersEntry\x12\x32\n\targuments\x18\x07 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12<\n\x19schedule_to_close_timeout\x18\x08 \x01(\x0b\x32\x19.google.protobuf.Duration\x12<\n\x19schedule_to_start_timeout\x18\t \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x39\n\x16start_to_close_timeout\x18\n \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x39\n\x0cretry_policy\x18\x0b \x01(\x0b\x32#.temporal.api.common.v1.RetryPolicy\x12\x38\n\x15local_retry_threshold\x18\x0c \x01(\x0b\x32\x19.google.protobuf.Duration\x12N\n\x11\x63\x61ncellation_type\x18\r \x01(\x0e\x32\x33.coresdk.workflow_commands.ActivityCancellationType\x12#\n\x1binclude_arguments_in_marker\x18\x0e \x01(\x08\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01"$\n\x15RequestCancelActivity\x12\x0b\n\x03seq\x18\x01 \x01(\r")\n\x1aRequestCancelLocalActivity\x12\x0b\n\x03seq\x18\x01 \x01(\r"\x9c\x01\n\x0bQueryResult\x12\x10\n\x08query_id\x18\x01 \x01(\t\x12<\n\tsucceeded\x18\x02 \x01(\x0b\x32\'.coresdk.workflow_commands.QuerySuccessH\x00\x12\x32\n\x06\x66\x61iled\x18\x03 \x01(\x0b\x32 .temporal.api.failure.v1.FailureH\x00\x42\t\n\x07variant"A\n\x0cQuerySuccess\x12\x31\n\x08response\x18\x01 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload"L\n\x19\x43ompleteWorkflowExecution\x12/\n\x06result\x18\x01 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload"J\n\x15\x46\x61ilWorkflowExecution\x12\x31\n\x07\x66\x61ilure\x18\x01 \x01(\x0b\x32 .temporal.api.failure.v1.Failure"\x92\x07\n\x1e\x43ontinueAsNewWorkflowExecution\x12\x15\n\rworkflow_type\x18\x01 \x01(\t\x12\x12\n\ntask_queue\x18\x02 \x01(\t\x12\x32\n\targuments\x18\x03 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12\x37\n\x14workflow_run_timeout\x18\x04 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x38\n\x15workflow_task_timeout\x18\x05 \x01(\x0b\x32\x19.google.protobuf.Duration\x12Q\n\x04memo\x18\x06 \x03(\x0b\x32\x43.coresdk.workflow_commands.ContinueAsNewWorkflowExecution.MemoEntry\x12W\n\x07headers\x18\x07 \x03(\x0b\x32\x46.coresdk.workflow_commands.ContinueAsNewWorkflowExecution.HeadersEntry\x12\x43\n\x11search_attributes\x18\x08 \x01(\x0b\x32(.temporal.api.common.v1.SearchAttributes\x12\x39\n\x0cretry_policy\x18\t \x01(\x0b\x32#.temporal.api.common.v1.RetryPolicy\x12;\n\x11versioning_intent\x18\n \x01(\x0e\x32 .coresdk.common.VersioningIntent\x12[\n\x1binitial_versioning_behavior\x18\x0b \x01(\x0e\x32\x36.temporal.api.enums.v1.ContinueAsNewVersioningBehavior\x12\x39\n\x16\x62\x61\x63koff_start_interval\x18\x0c \x01(\x0b\x32\x19.google.protobuf.Duration\x1aL\n\tMemoEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01"L\n\x17\x43\x61ncelWorkflowExecution\x12\x31\n\x07\x64\x65tails\x18\x01 \x01(\x0b\x32 .temporal.api.common.v1.Payloads"6\n\x0eSetPatchMarker\x12\x10\n\x08patch_id\x18\x01 \x01(\t\x12\x12\n\ndeprecated\x18\x02 \x01(\x08"\xe1\t\n\x1bStartChildWorkflowExecution\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x11\n\tnamespace\x18\x02 \x01(\t\x12\x13\n\x0bworkflow_id\x18\x03 \x01(\t\x12\x15\n\rworkflow_type\x18\x04 \x01(\t\x12\x12\n\ntask_queue\x18\x05 \x01(\t\x12.\n\x05input\x18\x06 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12=\n\x1aworkflow_execution_timeout\x18\x07 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x37\n\x14workflow_run_timeout\x18\x08 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x38\n\x15workflow_task_timeout\x18\t \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x46\n\x13parent_close_policy\x18\n \x01(\x0e\x32).coresdk.child_workflow.ParentClosePolicy\x12N\n\x18workflow_id_reuse_policy\x18\x0c \x01(\x0e\x32,.temporal.api.enums.v1.WorkflowIdReusePolicy\x12\x39\n\x0cretry_policy\x18\r \x01(\x0b\x32#.temporal.api.common.v1.RetryPolicy\x12\x15\n\rcron_schedule\x18\x0e \x01(\t\x12T\n\x07headers\x18\x0f \x03(\x0b\x32\x43.coresdk.workflow_commands.StartChildWorkflowExecution.HeadersEntry\x12N\n\x04memo\x18\x10 \x03(\x0b\x32@.coresdk.workflow_commands.StartChildWorkflowExecution.MemoEntry\x12\x43\n\x11search_attributes\x18\x11 \x01(\x0b\x32(.temporal.api.common.v1.SearchAttributes\x12P\n\x11\x63\x61ncellation_type\x18\x12 \x01(\x0e\x32\x35.coresdk.child_workflow.ChildWorkflowCancellationType\x12;\n\x11versioning_intent\x18\x13 \x01(\x0e\x32 .coresdk.common.VersioningIntent\x12\x32\n\x08priority\x18\x14 \x01(\x0b\x32 .temporal.api.common.v1.Priority\x12I\n\x13versioning_override\x18\x15 \x01(\x0b\x32,.temporal.api.workflow.v1.VersioningOverride\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01\x1aL\n\tMemoEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01"J\n\x1c\x43\x61ncelChildWorkflowExecution\x12\x1a\n\x12\x63hild_workflow_seq\x18\x01 \x01(\r\x12\x0e\n\x06reason\x18\x02 \x01(\t"\x8e\x01\n&RequestCancelExternalWorkflowExecution\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12G\n\x12workflow_execution\x18\x02 \x01(\x0b\x32+.coresdk.common.NamespacedWorkflowExecution\x12\x0e\n\x06reason\x18\x03 \x01(\t"\x8f\x03\n\x1fSignalExternalWorkflowExecution\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12I\n\x12workflow_execution\x18\x02 \x01(\x0b\x32+.coresdk.common.NamespacedWorkflowExecutionH\x00\x12\x1b\n\x11\x63hild_workflow_id\x18\x03 \x01(\tH\x00\x12\x13\n\x0bsignal_name\x18\x04 \x01(\t\x12-\n\x04\x61rgs\x18\x05 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12X\n\x07headers\x18\x06 \x03(\x0b\x32G.coresdk.workflow_commands.SignalExternalWorkflowExecution.HeadersEntry\x1aO\n\x0cHeadersEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12.\n\x05value\x18\x02 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload:\x02\x38\x01\x42\x08\n\x06target"#\n\x14\x43\x61ncelSignalWorkflow\x12\x0b\n\x03seq\x18\x01 \x01(\r"e\n\x1eUpsertWorkflowSearchAttributes\x12\x43\n\x11search_attributes\x18\x01 \x01(\x0b\x32(.temporal.api.common.v1.SearchAttributes"O\n\x18ModifyWorkflowProperties\x12\x33\n\rupserted_memo\x18\x01 \x01(\x0b\x32\x1c.temporal.api.common.v1.Memo"\xd2\x01\n\x0eUpdateResponse\x12\x1c\n\x14protocol_instance_id\x18\x01 \x01(\t\x12*\n\x08\x61\x63\x63\x65pted\x18\x02 \x01(\x0b\x32\x16.google.protobuf.EmptyH\x00\x12\x34\n\x08rejected\x18\x03 \x01(\x0b\x32 .temporal.api.failure.v1.FailureH\x00\x12\x34\n\tcompleted\x18\x04 \x01(\x0b\x32\x1f.temporal.api.common.v1.PayloadH\x00\x42\n\n\x08response"\x9a\x04\n\x16ScheduleNexusOperation\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x10\n\x08\x65ndpoint\x18\x02 \x01(\t\x12\x0f\n\x07service\x18\x03 \x01(\t\x12\x11\n\toperation\x18\x04 \x01(\t\x12.\n\x05input\x18\x05 \x01(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12<\n\x19schedule_to_close_timeout\x18\x06 \x01(\x0b\x32\x19.google.protobuf.Duration\x12X\n\x0cnexus_header\x18\x07 \x03(\x0b\x32\x42.coresdk.workflow_commands.ScheduleNexusOperation.NexusHeaderEntry\x12H\n\x11\x63\x61ncellation_type\x18\x08 \x01(\x0e\x32-.coresdk.nexus.NexusOperationCancellationType\x12<\n\x19schedule_to_start_timeout\x18\t \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x39\n\x16start_to_close_timeout\x18\n \x01(\x0b\x32\x19.google.protobuf.Duration\x1a\x32\n\x10NexusHeaderEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01"*\n\x1bRequestCancelNexusOperation\x12\x0b\n\x03seq\x18\x01 \x01(\r"\x87\x02\n\x0fStartStreamRead\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x31\n\x06stream\x18\x02 \x01(\x0b\x32!.temporal.api.stream.v1.StreamRef\x12\x13\n\x0b\x66rom_offset\x18\x03 \x01(\x0c\x12*\n\x07timeout\x18\x04 \x01(\x0b\x32\x19.google.protobuf.Duration\x12/\n\x0cidle_timeout\x18\x05 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x13\n\x0bmax_records\x18\x06 \x01(\r\x12\x11\n\tmax_bytes\x18\x07 \x01(\x04\x12\x1a\n\x12records_in_history\x18\x08 \x01(\x08"\x1e\n\x0f\x43loseStreamRead\x12\x0b\n\x03seq\x18\x01 \x01(\r"\xc7\x01\n\x0e\x41ppendToStream\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x31\n\x06stream\x18\x02 \x01(\x0b\x32!.temporal.api.stream.v1.StreamRef\x12/\n\x06\x62odies\x18\x03 \x03(\x0b\x32\x1f.temporal.api.common.v1.Payload\x12\x32\n\x08producer\x18\x04 \x01(\x0b\x32 .temporal.api.stream.v1.Producer\x12\x10\n\x08sequence\x18\x05 \x01(\x03"\x81\x01\n\x0b\x43loseStream\x12\x0b\n\x03seq\x18\x01 \x01(\r\x12\x31\n\x06stream\x18\x02 \x01(\x0b\x32!.temporal.api.stream.v1.StreamRef\x12\x32\n\x08producer\x18\x03 \x01(\x0b\x32 .temporal.api.stream.v1.Producer*X\n\x18\x41\x63tivityCancellationType\x12\x0e\n\nTRY_CANCEL\x10\x00\x12\x1f\n\x1bWAIT_CANCELLATION_COMPLETED\x10\x01\x12\x0b\n\x07\x41\x42\x41NDON\x10\x02\x42\x36\xea\x02\x33Temporalio::Internal::Bridge::Api::WorkflowCommandsb\x06proto3' ) _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals()) @@ -72,70 +75,78 @@ _SIGNALEXTERNALWORKFLOWEXECUTION_HEADERSENTRY._serialized_options = b"8\001" _SCHEDULENEXUSOPERATION_NEXUSHEADERENTRY._options = None _SCHEDULENEXUSOPERATION_NEXUSHEADERENTRY._serialized_options = b"8\001" - _ACTIVITYCANCELLATIONTYPE._serialized_start = 8785 - _ACTIVITYCANCELLATIONTYPE._serialized_end = 8873 - _WORKFLOWCOMMAND._serialized_start = 558 - _WORKFLOWCOMMAND._serialized_end = 2647 - _STARTTIMER._serialized_start = 2649 - _STARTTIMER._serialized_end = 2732 - _CANCELTIMER._serialized_start = 2734 - _CANCELTIMER._serialized_end = 2760 - _SCHEDULEACTIVITY._serialized_start = 2763 - _SCHEDULEACTIVITY._serialized_end = 3587 - _SCHEDULEACTIVITY_HEADERSENTRY._serialized_start = 3508 - _SCHEDULEACTIVITY_HEADERSENTRY._serialized_end = 3587 - _SCHEDULELOCALACTIVITY._serialized_start = 3590 - _SCHEDULELOCALACTIVITY._serialized_end = 4377 - _SCHEDULELOCALACTIVITY_HEADERSENTRY._serialized_start = 3508 - _SCHEDULELOCALACTIVITY_HEADERSENTRY._serialized_end = 3587 - _REQUESTCANCELACTIVITY._serialized_start = 4379 - _REQUESTCANCELACTIVITY._serialized_end = 4415 - _REQUESTCANCELLOCALACTIVITY._serialized_start = 4417 - _REQUESTCANCELLOCALACTIVITY._serialized_end = 4458 - _QUERYRESULT._serialized_start = 4461 - _QUERYRESULT._serialized_end = 4617 - _QUERYSUCCESS._serialized_start = 4619 - _QUERYSUCCESS._serialized_end = 4684 - _COMPLETEWORKFLOWEXECUTION._serialized_start = 4686 - _COMPLETEWORKFLOWEXECUTION._serialized_end = 4762 - _FAILWORKFLOWEXECUTION._serialized_start = 4764 - _FAILWORKFLOWEXECUTION._serialized_end = 4838 - _CONTINUEASNEWWORKFLOWEXECUTION._serialized_start = 4841 - _CONTINUEASNEWWORKFLOWEXECUTION._serialized_end = 5755 - _CONTINUEASNEWWORKFLOWEXECUTION_MEMOENTRY._serialized_start = 5598 - _CONTINUEASNEWWORKFLOWEXECUTION_MEMOENTRY._serialized_end = 5674 - _CONTINUEASNEWWORKFLOWEXECUTION_HEADERSENTRY._serialized_start = 3508 - _CONTINUEASNEWWORKFLOWEXECUTION_HEADERSENTRY._serialized_end = 3587 - _CANCELWORKFLOWEXECUTION._serialized_start = 5757 - _CANCELWORKFLOWEXECUTION._serialized_end = 5833 - _SETPATCHMARKER._serialized_start = 5835 - _SETPATCHMARKER._serialized_end = 5889 - _STARTCHILDWORKFLOWEXECUTION._serialized_start = 5892 - _STARTCHILDWORKFLOWEXECUTION._serialized_end = 7141 - _STARTCHILDWORKFLOWEXECUTION_HEADERSENTRY._serialized_start = 3508 - _STARTCHILDWORKFLOWEXECUTION_HEADERSENTRY._serialized_end = 3587 - _STARTCHILDWORKFLOWEXECUTION_MEMOENTRY._serialized_start = 5598 - _STARTCHILDWORKFLOWEXECUTION_MEMOENTRY._serialized_end = 5674 - _CANCELCHILDWORKFLOWEXECUTION._serialized_start = 7143 - _CANCELCHILDWORKFLOWEXECUTION._serialized_end = 7217 - _REQUESTCANCELEXTERNALWORKFLOWEXECUTION._serialized_start = 7220 - _REQUESTCANCELEXTERNALWORKFLOWEXECUTION._serialized_end = 7362 - _SIGNALEXTERNALWORKFLOWEXECUTION._serialized_start = 7365 - _SIGNALEXTERNALWORKFLOWEXECUTION._serialized_end = 7764 - _SIGNALEXTERNALWORKFLOWEXECUTION_HEADERSENTRY._serialized_start = 3508 - _SIGNALEXTERNALWORKFLOWEXECUTION_HEADERSENTRY._serialized_end = 3587 - _CANCELSIGNALWORKFLOW._serialized_start = 7766 - _CANCELSIGNALWORKFLOW._serialized_end = 7801 - _UPSERTWORKFLOWSEARCHATTRIBUTES._serialized_start = 7803 - _UPSERTWORKFLOWSEARCHATTRIBUTES._serialized_end = 7904 - _MODIFYWORKFLOWPROPERTIES._serialized_start = 7906 - _MODIFYWORKFLOWPROPERTIES._serialized_end = 7985 - _UPDATERESPONSE._serialized_start = 7988 - _UPDATERESPONSE._serialized_end = 8198 - _SCHEDULENEXUSOPERATION._serialized_start = 8201 - _SCHEDULENEXUSOPERATION._serialized_end = 8739 - _SCHEDULENEXUSOPERATION_NEXUSHEADERENTRY._serialized_start = 8689 - _SCHEDULENEXUSOPERATION_NEXUSHEADERENTRY._serialized_end = 8739 - _REQUESTCANCELNEXUSOPERATION._serialized_start = 8741 - _REQUESTCANCELNEXUSOPERATION._serialized_end = 8783 + _ACTIVITYCANCELLATIONTYPE._serialized_start = 9736 + _ACTIVITYCANCELLATIONTYPE._serialized_end = 9824 + _WORKFLOWCOMMAND._serialized_start = 596 + _WORKFLOWCOMMAND._serialized_end = 2966 + _STARTTIMER._serialized_start = 2968 + _STARTTIMER._serialized_end = 3051 + _CANCELTIMER._serialized_start = 3053 + _CANCELTIMER._serialized_end = 3079 + _SCHEDULEACTIVITY._serialized_start = 3082 + _SCHEDULEACTIVITY._serialized_end = 3906 + _SCHEDULEACTIVITY_HEADERSENTRY._serialized_start = 3827 + _SCHEDULEACTIVITY_HEADERSENTRY._serialized_end = 3906 + _SCHEDULELOCALACTIVITY._serialized_start = 3909 + _SCHEDULELOCALACTIVITY._serialized_end = 4696 + _SCHEDULELOCALACTIVITY_HEADERSENTRY._serialized_start = 3827 + _SCHEDULELOCALACTIVITY_HEADERSENTRY._serialized_end = 3906 + _REQUESTCANCELACTIVITY._serialized_start = 4698 + _REQUESTCANCELACTIVITY._serialized_end = 4734 + _REQUESTCANCELLOCALACTIVITY._serialized_start = 4736 + _REQUESTCANCELLOCALACTIVITY._serialized_end = 4777 + _QUERYRESULT._serialized_start = 4780 + _QUERYRESULT._serialized_end = 4936 + _QUERYSUCCESS._serialized_start = 4938 + _QUERYSUCCESS._serialized_end = 5003 + _COMPLETEWORKFLOWEXECUTION._serialized_start = 5005 + _COMPLETEWORKFLOWEXECUTION._serialized_end = 5081 + _FAILWORKFLOWEXECUTION._serialized_start = 5083 + _FAILWORKFLOWEXECUTION._serialized_end = 5157 + _CONTINUEASNEWWORKFLOWEXECUTION._serialized_start = 5160 + _CONTINUEASNEWWORKFLOWEXECUTION._serialized_end = 6074 + _CONTINUEASNEWWORKFLOWEXECUTION_MEMOENTRY._serialized_start = 5917 + _CONTINUEASNEWWORKFLOWEXECUTION_MEMOENTRY._serialized_end = 5993 + _CONTINUEASNEWWORKFLOWEXECUTION_HEADERSENTRY._serialized_start = 3827 + _CONTINUEASNEWWORKFLOWEXECUTION_HEADERSENTRY._serialized_end = 3906 + _CANCELWORKFLOWEXECUTION._serialized_start = 6076 + _CANCELWORKFLOWEXECUTION._serialized_end = 6152 + _SETPATCHMARKER._serialized_start = 6154 + _SETPATCHMARKER._serialized_end = 6208 + _STARTCHILDWORKFLOWEXECUTION._serialized_start = 6211 + _STARTCHILDWORKFLOWEXECUTION._serialized_end = 7460 + _STARTCHILDWORKFLOWEXECUTION_HEADERSENTRY._serialized_start = 3827 + _STARTCHILDWORKFLOWEXECUTION_HEADERSENTRY._serialized_end = 3906 + _STARTCHILDWORKFLOWEXECUTION_MEMOENTRY._serialized_start = 5917 + _STARTCHILDWORKFLOWEXECUTION_MEMOENTRY._serialized_end = 5993 + _CANCELCHILDWORKFLOWEXECUTION._serialized_start = 7462 + _CANCELCHILDWORKFLOWEXECUTION._serialized_end = 7536 + _REQUESTCANCELEXTERNALWORKFLOWEXECUTION._serialized_start = 7539 + _REQUESTCANCELEXTERNALWORKFLOWEXECUTION._serialized_end = 7681 + _SIGNALEXTERNALWORKFLOWEXECUTION._serialized_start = 7684 + _SIGNALEXTERNALWORKFLOWEXECUTION._serialized_end = 8083 + _SIGNALEXTERNALWORKFLOWEXECUTION_HEADERSENTRY._serialized_start = 3827 + _SIGNALEXTERNALWORKFLOWEXECUTION_HEADERSENTRY._serialized_end = 3906 + _CANCELSIGNALWORKFLOW._serialized_start = 8085 + _CANCELSIGNALWORKFLOW._serialized_end = 8120 + _UPSERTWORKFLOWSEARCHATTRIBUTES._serialized_start = 8122 + _UPSERTWORKFLOWSEARCHATTRIBUTES._serialized_end = 8223 + _MODIFYWORKFLOWPROPERTIES._serialized_start = 8225 + _MODIFYWORKFLOWPROPERTIES._serialized_end = 8304 + _UPDATERESPONSE._serialized_start = 8307 + _UPDATERESPONSE._serialized_end = 8517 + _SCHEDULENEXUSOPERATION._serialized_start = 8520 + _SCHEDULENEXUSOPERATION._serialized_end = 9058 + _SCHEDULENEXUSOPERATION_NEXUSHEADERENTRY._serialized_start = 9008 + _SCHEDULENEXUSOPERATION_NEXUSHEADERENTRY._serialized_end = 9058 + _REQUESTCANCELNEXUSOPERATION._serialized_start = 9060 + _REQUESTCANCELNEXUSOPERATION._serialized_end = 9102 + _STARTSTREAMREAD._serialized_start = 9105 + _STARTSTREAMREAD._serialized_end = 9368 + _CLOSESTREAMREAD._serialized_start = 9370 + _CLOSESTREAMREAD._serialized_end = 9400 + _APPENDTOSTREAM._serialized_start = 9403 + _APPENDTOSTREAM._serialized_end = 9602 + _CLOSESTREAM._serialized_start = 9605 + _CLOSESTREAM._serialized_end = 9734 # @@protoc_insertion_point(module_scope) diff --git a/temporalio/bridge/proto/workflow_commands/workflow_commands_pb2.pyi b/temporalio/bridge/proto/workflow_commands/workflow_commands_pb2.pyi index 81c47c0b03..233572f0a6 100644 --- a/temporalio/bridge/proto/workflow_commands/workflow_commands_pb2.pyi +++ b/temporalio/bridge/proto/workflow_commands/workflow_commands_pb2.pyi @@ -25,6 +25,7 @@ import temporalio.api.enums.v1.workflow_pb2 import temporalio.api.failure.v1.message_pb2 import temporalio.api.sdk.v1.event_group_marker_pb2 import temporalio.api.sdk.v1.user_metadata_pb2 +import temporalio.api.stream.v1.message_pb2 import temporalio.api.workflow.v1.message_pb2 import temporalio.bridge.proto.child_workflow.child_workflow_pb2 import temporalio.bridge.proto.common.common_pb2 @@ -105,6 +106,10 @@ class WorkflowCommand(google.protobuf.message.Message): UPDATE_RESPONSE_FIELD_NUMBER: builtins.int SCHEDULE_NEXUS_OPERATION_FIELD_NUMBER: builtins.int REQUEST_CANCEL_NEXUS_OPERATION_FIELD_NUMBER: builtins.int + START_STREAM_READ_FIELD_NUMBER: builtins.int + CLOSE_STREAM_READ_FIELD_NUMBER: builtins.int + APPEND_TO_STREAM_FIELD_NUMBER: builtins.int + CLOSE_STREAM_FIELD_NUMBER: builtins.int @property def user_metadata(self) -> temporalio.api.sdk.v1.user_metadata_pb2.UserMetadata: """User metadata that may or may not be persisted into history depending on the command type. @@ -181,6 +186,14 @@ class WorkflowCommand(google.protobuf.message.Message): def request_cancel_nexus_operation( self, ) -> global___RequestCancelNexusOperation: ... + @property + def start_stream_read(self) -> global___StartStreamRead: ... + @property + def close_stream_read(self) -> global___CloseStreamRead: ... + @property + def append_to_stream(self) -> global___AppendToStream: ... + @property + def close_stream(self) -> global___CloseStream: ... def __init__( self, *, @@ -219,10 +232,16 @@ class WorkflowCommand(google.protobuf.message.Message): schedule_nexus_operation: global___ScheduleNexusOperation | None = ..., request_cancel_nexus_operation: global___RequestCancelNexusOperation | None = ..., + start_stream_read: global___StartStreamRead | None = ..., + close_stream_read: global___CloseStreamRead | None = ..., + append_to_stream: global___AppendToStream | None = ..., + close_stream: global___CloseStream | None = ..., ) -> None: ... def HasField( self, field_name: typing.Literal[ + "append_to_stream", + b"append_to_stream", "cancel_child_workflow_execution", b"cancel_child_workflow_execution", "cancel_signal_workflow", @@ -231,6 +250,10 @@ class WorkflowCommand(google.protobuf.message.Message): b"cancel_timer", "cancel_workflow_execution", b"cancel_workflow_execution", + "close_stream", + b"close_stream", + "close_stream_read", + b"close_stream_read", "complete_workflow_execution", b"complete_workflow_execution", "continue_as_new_workflow_execution", @@ -261,6 +284,8 @@ class WorkflowCommand(google.protobuf.message.Message): b"signal_external_workflow_execution", "start_child_workflow_execution", b"start_child_workflow_execution", + "start_stream_read", + b"start_stream_read", "start_timer", b"start_timer", "update_response", @@ -276,6 +301,8 @@ class WorkflowCommand(google.protobuf.message.Message): def ClearField( self, field_name: typing.Literal[ + "append_to_stream", + b"append_to_stream", "cancel_child_workflow_execution", b"cancel_child_workflow_execution", "cancel_signal_workflow", @@ -284,6 +311,10 @@ class WorkflowCommand(google.protobuf.message.Message): b"cancel_timer", "cancel_workflow_execution", b"cancel_workflow_execution", + "close_stream", + b"close_stream", + "close_stream_read", + b"close_stream_read", "complete_workflow_execution", b"complete_workflow_execution", "continue_as_new_workflow_execution", @@ -316,6 +347,8 @@ class WorkflowCommand(google.protobuf.message.Message): b"signal_external_workflow_execution", "start_child_workflow_execution", b"start_child_workflow_execution", + "start_stream_read", + b"start_stream_read", "start_timer", b"start_timer", "update_response", @@ -354,6 +387,10 @@ class WorkflowCommand(google.protobuf.message.Message): "update_response", "schedule_nexus_operation", "request_cancel_nexus_operation", + "start_stream_read", + "close_stream_read", + "append_to_stream", + "close_stream", ] | None ): ... @@ -1931,3 +1968,211 @@ class RequestCancelNexusOperation(google.protobuf.message.Message): def ClearField(self, field_name: typing.Literal["seq", b"seq"]) -> None: ... global___RequestCancelNexusOperation = RequestCancelNexusOperation + +@typing.final +class StartStreamRead(google.protobuf.message.Message): + """Start a read of a Stream. Core reads the Store itself and gives each page of Records to the + workflow in a `ResolveStreamRead` job, until the read ends or the workflow closes it. + """ + + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + SEQ_FIELD_NUMBER: builtins.int + STREAM_FIELD_NUMBER: builtins.int + FROM_OFFSET_FIELD_NUMBER: builtins.int + TIMEOUT_FIELD_NUMBER: builtins.int + IDLE_TIMEOUT_FIELD_NUMBER: builtins.int + MAX_RECORDS_FIELD_NUMBER: builtins.int + MAX_BYTES_FIELD_NUMBER: builtins.int + RECORDS_IN_HISTORY_FIELD_NUMBER: builtins.int + seq: builtins.int + """Lang's incremental sequence number of Stream reads, used as the read's identifier""" + from_offset: builtins.bytes + """The read starts after this offset. Empty means the start of the Stream.""" + max_records: builtins.int + """The most Records the read gives. Zero means no cap.""" + max_bytes: builtins.int + """The most bytes of Records the read gives. The first Record is always given. Zero means no + cap. + """ + records_in_history: builtins.bool + """If set, the Records go into the workflow history, so a replay reads no Store.""" + @property + def stream(self) -> temporalio.api.stream.v1.message_pb2.StreamRef: ... + @property + def timeout(self) -> google.protobuf.duration_pb2.Duration: + """The longest time the read may take. Unset means no bound. A set value of zero or less fails + the workflow task. A read that gave no Record yet looks at the Store once before its timeout + ends it. + """ + + @property + def idle_timeout(self) -> google.protobuf.duration_pb2.Duration: + """The read ends when no Record comes for this long. Unset means 1 minute. Zero gives the + Records that are in the Stream, and ends with no wait. A read that must wait longer sets a + longer value. + """ + + def __init__( + self, + *, + seq: builtins.int = ..., + stream: temporalio.api.stream.v1.message_pb2.StreamRef | None = ..., + from_offset: builtins.bytes = ..., + timeout: google.protobuf.duration_pb2.Duration | None = ..., + idle_timeout: google.protobuf.duration_pb2.Duration | None = ..., + max_records: builtins.int = ..., + max_bytes: builtins.int = ..., + records_in_history: builtins.bool = ..., + ) -> None: ... + def HasField( + self, + field_name: typing.Literal[ + "idle_timeout", b"idle_timeout", "stream", b"stream", "timeout", b"timeout" + ], + ) -> builtins.bool: ... + def ClearField( + self, + field_name: typing.Literal[ + "from_offset", + b"from_offset", + "idle_timeout", + b"idle_timeout", + "max_bytes", + b"max_bytes", + "max_records", + b"max_records", + "records_in_history", + b"records_in_history", + "seq", + b"seq", + "stream", + b"stream", + "timeout", + b"timeout", + ], + ) -> None: ... + +global___StartStreamRead = StartStreamRead + +@typing.final +class CloseStreamRead(google.protobuf.message.Message): + """Stop a read that `StartStreamRead` started. The workflow gets no job for it.""" + + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + SEQ_FIELD_NUMBER: builtins.int + seq: builtins.int + """Lang's sequence number as passed to `StartStreamRead`""" + def __init__( + self, + *, + seq: builtins.int = ..., + ) -> None: ... + def ClearField(self, field_name: typing.Literal["seq", b"seq"]) -> None: ... + +global___CloseStreamRead = CloseStreamRead + +@typing.final +class AppendToStream(google.protobuf.message.Message): + """Append Records to a Stream. Core calls the Store itself and gives the result in a + `ResolveStreamAppend` job. + """ + + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + SEQ_FIELD_NUMBER: builtins.int + STREAM_FIELD_NUMBER: builtins.int + BODIES_FIELD_NUMBER: builtins.int + PRODUCER_FIELD_NUMBER: builtins.int + SEQUENCE_FIELD_NUMBER: builtins.int + seq: builtins.int + """Lang's incremental sequence number of Stream appends, used as the append's identifier""" + sequence: builtins.int + """The `sequence` of the Store request, as the caller gives it. Zero means Core uses `seq`.""" + @property + def stream(self) -> temporalio.api.stream.v1.message_pb2.StreamRef: ... + @property + def bodies( + self, + ) -> google.protobuf.internal.containers.RepeatedCompositeFieldContainer[ + temporalio.api.common.v1.message_pb2.Payload + ]: + """The bodies of the Records, in order""" + + @property + def producer(self) -> temporalio.api.stream.v1.message_pb2.Producer: + """The workflow's Producer, of kind workflow, with its id and run id and no epoch. Core sets the + epoch to the current workflow task. + """ + + def __init__( + self, + *, + seq: builtins.int = ..., + stream: temporalio.api.stream.v1.message_pb2.StreamRef | None = ..., + bodies: collections.abc.Iterable[temporalio.api.common.v1.message_pb2.Payload] + | None = ..., + producer: temporalio.api.stream.v1.message_pb2.Producer | None = ..., + sequence: builtins.int = ..., + ) -> None: ... + def HasField( + self, field_name: typing.Literal["producer", b"producer", "stream", b"stream"] + ) -> builtins.bool: ... + def ClearField( + self, + field_name: typing.Literal[ + "bodies", + b"bodies", + "producer", + b"producer", + "seq", + b"seq", + "sequence", + b"sequence", + "stream", + b"stream", + ], + ) -> None: ... + +global___AppendToStream = AppendToStream + +@typing.final +class CloseStream(google.protobuf.message.Message): + """Close a Stream. Core records the intent in this workflow task, closes the Stream after the + server took the task, and gives the result in a `ResolveStreamClose` job of a later task. + """ + + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + SEQ_FIELD_NUMBER: builtins.int + STREAM_FIELD_NUMBER: builtins.int + PRODUCER_FIELD_NUMBER: builtins.int + seq: builtins.int + """Lang's incremental sequence number of Stream closes, used as the close's identifier""" + @property + def stream(self) -> temporalio.api.stream.v1.message_pb2.StreamRef: ... + @property + def producer(self) -> temporalio.api.stream.v1.message_pb2.Producer: + """The workflow's Producer, of kind workflow, with its id and run id and no epoch. Core sets the + epoch to the workflow task that recorded the close. + """ + + def __init__( + self, + *, + seq: builtins.int = ..., + stream: temporalio.api.stream.v1.message_pb2.StreamRef | None = ..., + producer: temporalio.api.stream.v1.message_pb2.Producer | None = ..., + ) -> None: ... + def HasField( + self, field_name: typing.Literal["producer", b"producer", "stream", b"stream"] + ) -> builtins.bool: ... + def ClearField( + self, + field_name: typing.Literal[ + "producer", b"producer", "seq", b"seq", "stream", b"stream" + ], + ) -> None: ... + +global___CloseStream = CloseStream diff --git a/temporalio/bridge/sdk-core b/temporalio/bridge/sdk-core index 7bb86122ba..3bec8fc0eb 160000 --- a/temporalio/bridge/sdk-core +++ b/temporalio/bridge/sdk-core @@ -1 +1 @@ -Subproject commit 7bb86122bab4ea1efca2295da75fa7751aeaed0c +Subproject commit 3bec8fc0eb42f47f2b250f21773448491a921afc diff --git a/temporalio/bridge/src/streams.rs b/temporalio/bridge/src/streams.rs new file mode 100644 index 0000000000..c575fd7916 --- /dev/null +++ b/temporalio/bridge/src/streams.rs @@ -0,0 +1,69 @@ +//! Bindings to the Stream runtime of the `temporalio-streams` crate, on the memory Store. Python +//! calls the five Stream service operations by name, with the encoded request, and gets the encoded +//! response. + +use pyo3::exceptions::PyException; +#[cfg(feature = "streams")] +use pyo3::prelude::*; + +#[cfg(feature = "streams")] +use crate::runtime; + +// The exception args are the gRPC code, the message and the encoded details, as in `RPCError`. +pyo3::create_exception!(temporal_sdk_bridge, StreamCallError, PyException); + +/// The crate's runtime object. Each Store on the bridge holds one. +#[cfg(feature = "streams")] +#[pyclass(frozen)] +pub struct StreamRuntimeRef { + pub(crate) streams: std::sync::Arc, + runtime: runtime::Runtime, +} + +#[cfg(feature = "streams")] +#[pymethods] +impl StreamRuntimeRef { + /// Runs one Stream operation on an encoded request. The future gives the encoded response. + fn call<'p>( + &self, + py: Python<'p>, + operation: String, + request: Vec, + ) -> PyResult> { + self.runtime + .assert_same_process("call the Stream runtime")?; + let streams = self.streams.clone(); + self.runtime.future_into_py(py, async move { + streams.call(&operation, &request).await.map_err(call_error) + }) + } +} + +#[cfg(feature = "streams")] +fn call_error(err: temporalio_streams::StreamError) -> PyErr { + StreamCallError::new_err((err.code() as u32, err.message.clone(), err.details())) +} + +/// Builds the runtime on a new memory Store. A `None` duration takes the crate's default. +#[cfg(feature = "streams")] +pub fn new_memory_stream_runtime( + runtime_ref: &runtime::RuntimeRef, + store_name: String, + retention: Option, + producer_idle_ttl: Option, +) -> PyResult { + let runtime = runtime_ref.runtime.clone(); + runtime.assert_same_process("create a Stream runtime")?; + // The runtime can spawn Tokio tasks, which need an entered Tokio runtime. + let _guard = runtime.core.tokio_handle().enter(); + let meter = runtime.core.telemetry().get_temporal_metric_meter(); + let streams = temporalio_streams::MemoryStreamRuntime::new( + store_name, + temporalio_streams::MemoryStreamOptions { + retention, + producer_idle_ttl, + }, + meter, + ); + Ok(StreamRuntimeRef { streams, runtime }) +} diff --git a/temporalio/bridge/src/worker.rs b/temporalio/bridge/src/worker.rs index 39cdc5e773..48e3b77c30 100644 --- a/temporalio/bridge/src/worker.rs +++ b/temporalio/bridge/src/worker.rs @@ -5,7 +5,7 @@ use prost::Message; use pyo3::exceptions::{PyException, PyRuntimeError, PyValueError}; use pyo3::prelude::*; use pyo3::types::{PyBytes, PyTuple}; -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use std::marker::PhantomData; use std::sync::{Arc, OnceLock}; use std::time::Duration; @@ -65,6 +65,18 @@ pub struct WorkerConfig { plugins: Vec<(String, String)>, storage_drivers: HashSet, disable_payload_error_limit: bool, + /// The Stream runtime of each Store that has one, by Store name. Core reads and appends a + /// Stream of Workflow code through them. + #[cfg(feature = "streams")] + stream_runtimes: Vec<(String, Py)>, + /// The most bytes of one read's Records that wait for Workflow code. `None` keeps Core's + /// default. + stream_read_ahead_bytes: Option, + /// The least time between two batches of one read. `None` keeps Core's default. + /// Whether a replay that lost the Records of a read fails the Workflow, for every type or + /// for the types listed. + stream_replay_as_workflow_fail: bool, + stream_replay_as_workflow_fail_for_types: HashSet, } #[derive(FromPyObject)] @@ -718,6 +730,40 @@ fn convert_worker_config( ) -> PyResult { let converted_tuner = convert_tuner_holder(conf.tuner, task_locals)?; let converted_versioning_strategy = convert_versioning_strategy(conf.versioning_strategy); + // A build with no Stream runtime has no Store that Core can call, so Core gets none. + #[cfg(feature = "streams")] + let stream_runtimes = temporalio_sdk_core::StreamRuntimes::new( + conf.stream_runtimes + .into_iter() + .map(|(store, runtime)| (store, runtime.get().streams.clone())), + ); + #[cfg(not(feature = "streams"))] + let stream_runtimes = temporalio_sdk_core::StreamRuntimes::default(); + let stream_runtimes = match conf.stream_read_ahead_bytes { + Some(bytes) => stream_runtimes.with_read_ahead_bytes(bytes), + None => stream_runtimes, + }; + let mut workflow_failure_errors = HashSet::new(); + if conf.nondeterminism_as_workflow_fail { + workflow_failure_errors.insert(WorkflowErrorType::Nondeterminism); + } + if conf.stream_replay_as_workflow_fail { + workflow_failure_errors.insert(WorkflowErrorType::StreamReplay); + } + let mut workflow_types_to_failure_errors: HashMap> = + HashMap::new(); + for workflow_type in conf.nondeterminism_as_workflow_fail_for_types { + workflow_types_to_failure_errors + .entry(workflow_type) + .or_default() + .insert(WorkflowErrorType::Nondeterminism); + } + for workflow_type in conf.stream_replay_as_workflow_fail_for_types { + workflow_types_to_failure_errors + .entry(workflow_type) + .or_default() + .insert(WorkflowErrorType::StreamReplay); + } temporalio_sdk_core::WorkerConfig::builder() .namespace(conf.namespace) .task_queue(conf.task_queue) @@ -747,22 +793,8 @@ fn convert_worker_config( // auto-cancel-activity behavior of shutdown will not occur, so we // always set it even if 0. .graceful_shutdown_period(Duration::from_millis(conf.graceful_shutdown_period_millis)) - .workflow_failure_errors(if conf.nondeterminism_as_workflow_fail { - HashSet::from([WorkflowErrorType::Nondeterminism]) - } else { - HashSet::new() - }) - .workflow_types_to_failure_errors( - conf.nondeterminism_as_workflow_fail_for_types - .iter() - .map(|s| { - ( - s.to_owned(), - HashSet::from([WorkflowErrorType::Nondeterminism]), - ) - }) - .collect(), - ) + .workflow_failure_errors(workflow_failure_errors) + .workflow_types_to_failure_errors(workflow_types_to_failure_errors) .maybe_nexus_task_poller_behavior(conf.nexus_task_poller_behavior.map(Into::into)) .plugins( conf.plugins @@ -777,6 +809,7 @@ fn convert_worker_config( .collect::>(), ) .disable_payload_error_limit(conf.disable_payload_error_limit) + .stream_runtimes(stream_runtimes) .build() .map_err(|err| PyValueError::new_err(format!("Invalid worker config: {err}"))) } diff --git a/temporalio/bridge/worker.py b/temporalio/bridge/worker.py index fa5f337ca7..8b14a05595 100644 --- a/temporalio/bridge/worker.py +++ b/temporalio/bridge/worker.py @@ -5,9 +5,11 @@ from __future__ import annotations +import dataclasses from collections.abc import Awaitable, Callable, Sequence from dataclasses import dataclass from typing import ( + Any, TypeAlias, ) @@ -60,6 +62,16 @@ class WorkerConfig: plugins: Sequence[tuple[str, str]] storage_drivers: set[str] disable_payload_error_limit: bool + stream_runtimes: Sequence[tuple[str, Any]] = () + """The bridge's object of each Store's Stream runtime, by Store name.""" + stream_read_ahead_bytes: int | None = None + """The most bytes of one read's Records that wait for Workflow code, or Core's default.""" + stream_replay_as_workflow_fail: bool = False + """Whether a replay that lost the Records of a read fails the Workflow.""" + stream_replay_as_workflow_fail_for_types: set[str] = dataclasses.field( + default_factory=set + ) + """The Workflow types for which such a replay fails the Workflow.""" @dataclass @@ -368,6 +380,7 @@ async def encode_completion( skip_search_attributes=True, skip_headers=not encode_headers, concurrency_limit=storage_concurrency_limit, + skip_stream_bodies=True, ).visit( _Visitor(data_converter._external_store_payload_sequence), completion, diff --git a/temporalio/client/__init__.py b/temporalio/client/__init__.py index 586b3b96cd..8d9870e51c 100644 --- a/temporalio/client/__init__.py +++ b/temporalio/client/__init__.py @@ -188,8 +188,10 @@ from ._stream_store import ( StreamBatchLimits, StreamConfig, + StreamReplaySource, StreamSelectContext, StreamStore, + StreamStoreCallOptions, ) from ._worker_versioning import ( BuildIdOp, @@ -358,8 +360,10 @@ "Plugin", "StreamBatchLimits", "StreamConfig", + "StreamReplaySource", "StreamSelectContext", "StreamStore", + "StreamStoreCallOptions", "Callback", "_ClientImpl", "_apply_headers", diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index 6b6d5778e9..024f4213fc 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -1,9 +1,11 @@ from __future__ import annotations import dataclasses +import enum from abc import ABC, abstractmethod -from collections.abc import Callable, Sequence -from typing import Any +from collections.abc import Awaitable, Callable, Sequence +from datetime import timedelta +from typing import TYPE_CHECKING, Any import google.protobuf.message import nexusrpc @@ -11,11 +13,18 @@ import temporalio.activity import temporalio.api.streamservice.v1 import temporalio.bridge._visitor +import temporalio.bridge.runtime +import temporalio.common import temporalio.converter import temporalio.nexus.system.stream_service -from temporalio.converter import StorageDriverActivityInfo +import temporalio.runtime +import temporalio.workflow +from temporalio.converter import StorageDriverActivityInfo, StorageDriverWorkflowInfo from temporalio.exceptions import StreamConfigError +if TYPE_CHECKING: + import temporalio.bridge.streams + _MAX_RECORD_BYTES = 1536 * 1024 _MAX_BATCH_BYTES = 4 * 1024 * 1024 _MAX_BATCH_RECORDS = 1000 @@ -58,16 +67,94 @@ def __post_init__(self) -> None: raise ValueError(f"{name} must be from 1 to {most}, not {value}") +class StreamReplaySource(enum.Enum): + """Where a replay of a read of Workflow code gets the Records of the read. + + :py:attr:`StreamConfig.workflow_read_replay_from` holds it. It decides what a new read + records in History. A replay follows what History holds, so a change of the setting + doesn't break a Workflow that is running. Client and Activity code have no History, so + the setting doesn't touch them. + + .. warning:: + This API is experimental and subject to change. + """ + + HISTORY = "history" + """The marker in History holds the Records of the read with their bodies. + + A replay takes the Records from History, so it reads no Store and needs no Stream + config. Each Record adds to History. The marker of one Workflow Task holds at most + 1 MiB of Records, and a read goes on in the next task after that. + """ + + STORE = "store" + """The marker in History holds no Record and no body. This is the default. + + For each Workflow Task of a read, the marker holds the range of offsets, the count + and a digest of the Records that the Workflow got. A replay reads the Records from the + Store again and checks the count and the digest. So the replay needs the Stream config + and the Records. The Records must stay in the Store, and Retention must keep them, for + as long as the Workflow can replay. When they can't be read, the Workflow Task fails + with a message that starts with ``StreamReplayError``. A Store that stays unavailable + for 5 s fails the task with a message that starts with ``StreamReplayUnavailable`` + in its place. A replay on a Worker with no cache reads the Store again on every + Workflow Task. + """ + + +@dataclasses.dataclass(frozen=True) +class StreamStoreCallOptions: + """Options of the Local Activity that creates or describes a Stream for Workflow code. + + Workflow code can't do I/O, so the default + :py:meth:`StreamStore.call_from_workflow` calls its Store from a Local Activity. + Core makes a read, an append and a close of Workflow code itself, with no Activity, + so these options don't apply to them. Core tries such a call again while its Store + is unavailable, for at most 30 s. + + A :py:class:`StreamConfig` holds them in + :py:attr:`StreamConfig.workflow_call_options`, and every Store of that config + gets the same options. + + .. warning:: + This API is experimental and subject to change. + """ + + start_to_close_timeout: timedelta | None = timedelta(seconds=60) + """Most time for one attempt.""" + + schedule_to_close_timeout: timedelta | None = None + """Most time for all attempts.""" + + retry_policy: temporalio.common.RetryPolicy = dataclasses.field( + default_factory=lambda: temporalio.common.RetryPolicy(maximum_attempts=1) + ) + """Retries of a failed call. The default is one attempt.""" + + def __post_init__(self) -> None: + """Check that a timeout is set.""" + if not self.start_to_close_timeout and not self.schedule_to_close_timeout: + raise ValueError( + "start_to_close_timeout or schedule_to_close_timeout must be set" + ) + + class StreamStore(ABC): """A Store that answers the Stream operations of a client. A client with a Store sends each Stream operation to it. This release has no Streams on the server, so a client needs a Store for any Stream operation. Use a Store of a package, such as ``RedisStreamStore`` of ``temporalio-streams-redis``, or - ``MemoryStreamStore`` of ``temporalio-streams-memory`` in tests. + :py:class:`temporalio.testing.MemoryStreamStore` in tests. - To keep Streams in another system, write a Store that implements :py:attr:`name` - and the five operations. Each operation takes and gives a message of + The SDK's Stores run on the bridge, in Rust, and this class isn't meant to be + subclassed in Python. A Store that is written in Python works for a client and an + Activity, for example as a double in a test. Core can't call it, so Workflow code has + no Direct call on it. A read, an append and a close of Workflow code then fail the + Workflow. A create and a describe still run in the Store's Local Activity. + + Such a Store implements :py:attr:`name` and the five operations. Each operation + takes and gives a message of ``temporalio.api.streamservice.v1``. The bodies of the Records arrive as payloads that the SDK already encoded with the data converter and the payload codec, so a Store keeps payloads and not the user's types. A failed call raises @@ -137,6 +224,136 @@ async def describe_stream( """Give the status and the newest offset of a Stream.""" ... + def worker_activities(self) -> Sequence[Callable[..., Any]]: + """The Activities that a Worker registers for this Store when it runs Workflows. + + The default is one Activity, named for this Store, that + :py:meth:`call_from_workflow` runs as a Local Activity. A Store that overrides + one of the two methods overrides the other to match. + """ + return [_store_call_activity(self)] + + async def call_from_workflow( + self, + operation: str, + request: google.protobuf.message.Message, + options: StreamStoreCallOptions, + ) -> google.protobuf.message.Message: + """Answer a create or a describe of a Stream for Workflow code. + + It runs in Workflow code, so it does no I/O itself. The default runs the + Activity of :py:meth:`worker_activities` as a Local Activity for each operation. + It takes the timeouts and the retry policy of ``options``. A replay takes the + result from History and calls no method of this Store. + + Core makes a read, an append and a close of Workflow code itself, so this method + gets none of them. + + Args: + operation: The Nexus operation name, ``CreateStream`` or ``DescribeStream``. + request: The request message of ``temporalio.api.streamservice.v1``. + options: The :py:attr:`StreamConfig.workflow_call_options` of the config. + + Returns: + The response message of the operation. + """ + stream_operation = _STREAM_OPERATIONS[operation] + response = await temporalio.workflow.execute_local_activity( + _store_call_activity_name(self.name), + args=[operation, request.SerializeToString()], + result_type=bytes, + schedule_to_close_timeout=options.schedule_to_close_timeout, + start_to_close_timeout=options.start_to_close_timeout, + retry_policy=options.retry_policy, + ) + return stream_operation.response_type.FromString(response) + + +class _BridgeStreamStore(StreamStore): # pyright: ignore[reportUnusedClass] + """A Store that runs on the Stream runtime of the bridge. + + It builds the runtime at its first call, or when a Worker with Workflows gives it to + Core, so the Store can be built before the Runtime and its Tokio threads exist. + """ + + def __init__( + self, + *, + name: str, + runtime: temporalio.runtime.Runtime | None, + ) -> None: + self._name = name + self._runtime = runtime + self._stream_runtime: temporalio.bridge.streams.StreamRuntime | None = None + + @property + def name(self) -> str: + """Name of the Store.""" + return self._name + + @abstractmethod + def _new_stream_runtime( + self, runtime: temporalio.bridge.runtime.Runtime + ) -> temporalio.bridge.streams.StreamRuntime: ... + + async def create_stream( + self, request: temporalio.api.streamservice.v1.CreateStreamRequest + ) -> temporalio.api.streamservice.v1.CreateStreamResponse: + """See base class.""" + return await self._call_bridge("CreateStream", request) + + async def append_records( + self, request: temporalio.api.streamservice.v1.AppendRecordsRequest + ) -> temporalio.api.streamservice.v1.AppendRecordsResponse: + """See base class.""" + return await self._call_bridge("AppendRecords", request) + + async def read_records( + self, request: temporalio.api.streamservice.v1.ReadRecordsRequest + ) -> temporalio.api.streamservice.v1.ReadRecordsResponse: + """See base class.""" + return await self._call_bridge("ReadRecords", request) + + async def close_stream( + self, request: temporalio.api.streamservice.v1.CloseStreamRequest + ) -> temporalio.api.streamservice.v1.CloseStreamResponse: + """See base class.""" + return await self._call_bridge("CloseStream", request) + + async def describe_stream( + self, request: temporalio.api.streamservice.v1.DescribeStreamRequest + ) -> temporalio.api.streamservice.v1.DescribeStreamResponse: + """See base class.""" + return await self._call_bridge("DescribeStream", request) + + def _get_stream_runtime(self) -> temporalio.bridge.streams.StreamRuntime: + """Give the Stream runtime of the bridge, and build it at the first use. + + Raises: + temporalio.exceptions.StreamConfigError: This build of temporalio has no + Stream runtime, or not this Store. + """ + if self._stream_runtime is None: + runtime = self._runtime or temporalio.runtime.Runtime.default() + self._stream_runtime = self._new_stream_runtime(runtime._core_runtime) + return self._stream_runtime + + async def _call_bridge( + self, operation: str, request: google.protobuf.message.Message + ) -> Any: + """Answer one Stream operation with the Stream runtime of the bridge. + + Raises: + temporalio.exceptions.StreamError: The call failed. + temporalio.exceptions.StreamConfigError: This build of temporalio has no + Stream runtime. + """ + # The bridge takes and gives the messages in their wire form. + response = await self._get_stream_runtime().call( + operation, request.SerializeToString() + ) + return _STREAM_OPERATIONS[operation].response_type.FromString(response) + @dataclasses.dataclass(frozen=True) class _StreamOperation: @@ -200,6 +417,27 @@ def check_response( } """The operations of the Stream service, by their Nexus operation name.""" +_STORE_CALL_ACTIVITY = "__temporal_stream_store_call" + + +def _store_call_activity_name(store_name: str) -> str: + # The name carries the Store's name, so two Stores on one Worker don't clash. + return f"{_STORE_CALL_ACTIVITY}:{store_name}" + + +def _store_call_activity( + store: StreamStore, +) -> Callable[[str, bytes], Awaitable[bytes]]: + @temporalio.activity.defn(name=_store_call_activity_name(store.name)) + async def call(operation: str, request: bytes) -> bytes: + message = _STREAM_OPERATIONS[operation].request_type.FromString(request) + response = await _call_stream_store( + store, temporalio.activity.client().data_converter, operation, message + ) + return response.SerializeToString() + + return call + @dataclasses.dataclass(frozen=True) class StreamSelectContext: @@ -212,8 +450,12 @@ class StreamSelectContext: id: str """The id of the Stream.""" - target: StorageDriverActivityInfo | None = None - """The Activity that makes the call, or ``None`` for a plain client.""" + target: StorageDriverActivityInfo | StorageDriverWorkflowInfo | None = None + """The Workflow or Activity that makes the call, or ``None`` for a plain client. + + A call in an Activity has that Activity here. A Workflow's call runs the selector + in Workflow code, and has that Workflow here. + """ @dataclasses.dataclass(frozen=True) @@ -226,16 +468,70 @@ class StreamConfig: operation. It holds Stores and a selector, as :py:class:`temporalio.converter.ExternalStorage` holds drivers and a selector. + On a Worker whose client has no config, a Stream call of Workflow code raises + :py:class:`temporalio.exceptions.ApplicationError` with the ``type`` + ``StreamConfigError``, and an uncaught one fails the Workflow. Workflow code can + catch this error. A Workflow that catches it must not go on to make the same call + on a Worker that has a config, since the History then has no record of the first + call. + A ref carries the name of its Store, so a call on a known Stream goes to the Store of that name. A create can name its Store with ``store=``. A call whose ref names no Store goes to the Store that :py:attr:`store_selector` returns, or to the one Store when there is no selector. With more than one Store and no selector, such a call raises :py:class:`temporalio.exceptions.StreamConfigError`. - An Activity reaches the Stores of its Worker's client through - :py:func:`temporalio.activity.client`. Workflow code can't call a Stream in this - version. A Stream call of Workflow code raises ``RuntimeError``, which fails the - Workflow Task. + An append or a close of Workflow code writes as the Workflow, with the Workflow Task + that runs it as its epoch. So the Store fences a late write of a failed Workflow + Task. A ``producer=`` of another kind raises + :py:class:`temporalio.exceptions.StreamError` with the reason + ``STREAM_ERROR_REASON_INVALID``. Core keeps the appends and the close of one Workflow on one Stream in the order + of the calls. + + Core appends and closes for Workflow code itself, with no Activity. Core closes the + Stream only after the server took the Workflow Task of the close, so a failed task + closes nothing. A create and a describe of Workflow code run in the Store's Local + Activity. So Workflow code needs no Worker that polls for Activity Tasks. + + Workflow code reads with the same ``read``, ``iter_read`` and ``iter_read_items`` as + a client. On a Store with a Rust runtime, Core reads the Store inside the Workflow + Task. A page that Workflow code gets in one activation has at most 256 Records, and + the rest come in the next activation with no wait. A loop that asks for its next page in the same + activation keeps one read open in Core. A read that no code waits on closes before + the activation completes, so it never holds a Workflow Task open. + + .. code-block:: python + + async for record in events.iter_read_items(wait=timedelta(seconds=10)): + if record.body.kind == "tool_call": + tools.append( + workflow.start_activity( + run_tool, + record.body.tool_call, + start_to_close_timeout=timedelta(minutes=1), + ) + ) + + The loop ends after the page that is ``done``, when the Stream is closed and the + read gave its last Record. With a ``wait``, a quiet Stream gives empty pages, and + the loop goes on. So a loop on a Stream that can be quiet for long uses + ``iter_read`` and leaves after an empty page. A read has these costs. + + * A call keeps its Workflow Task open while it waits for its page. A Signal, an + Update, a cancel or an Activity result reaches the Workflow at the next Workflow + Task, at the heartbeat of the task at the latest. That is about 8 s with the + default task timeout of 10 s. + * The calls of one Workflow Task write one marker in History, whatever the number of + Records. + * With the default :py:attr:`StreamReplaySource.STORE`, History holds no Record. A + replay reads the Records from the Store again, so they must stay in the Store for + as long as the Workflow can replay. + * A replay on a Worker with no cache reads the Store again on every Workflow Task. + + On a Store written in Python, a read, an append and a close of Workflow code fail + the Workflow with a non-retryable + :py:class:`temporalio.exceptions.ApplicationError`. Code in a Query handler or an + Update validator can't read. .. warning:: This API is experimental and subject to change. @@ -246,7 +542,8 @@ class StreamConfig: A ref carries the :py:attr:`StreamStore.name` of its Store, and a call looks the Store up by that name. With more than one Store and no - :py:attr:`store_selector`, each call must name its Store. + :py:attr:`store_selector`, each call must name its Store. A Worker serves every + Store of its client. """ store_selector: Callable[[StreamSelectContext], StreamStore] | None = None @@ -257,8 +554,45 @@ class StreamConfig: such a call goes to that Store. When it is ``None`` and there is more than one Store, such a call raises :py:class:`temporalio.exceptions.StreamConfigError`. - A Stream that code builds from an id only goes through the selector on each - call, so the selector must give the same Store for the same id. + For a call of Workflow code, the selector runs in Workflow code, also on a + replay. So it must give the same Store for the same context. The call of the chosen + Store is in History, and another answer on a replay is a non-determinism error. A Stream that code builds from an id only goes through the + selector on each call, so the selector must also give the same Store for the same + id. + """ + + workflow_call_options: StreamStoreCallOptions = dataclasses.field( + default_factory=StreamStoreCallOptions + ) + """Options of a create and a describe of Workflow code, for every Store of this config. + + :py:meth:`StreamStore.call_from_workflow` gets them. The default runs the Store's + Local Activity with their timeouts and retry policy. + """ + + workflow_read_replay_from: StreamReplaySource = dataclasses.field( + default=StreamReplaySource.STORE + ) + """Where a replay of a read of Workflow code gets the Records of the read. + + It applies to every read of Workflow code on a Worker whose client has this config, + and decides what a new read records in History. :py:attr:`StreamReplaySource.STORE`, + the default, puts only the range of offsets, the count and a digest of the Records + into History, so a replay reads them from the Store again. The Records must then stay + in the Store for as long as the Workflow can replay. The option + :py:attr:`StreamReplaySource.HISTORY` puts the Records with their bodies into + History, so a replay needs no Store. A replay follows the marker in History and not + this setting, so a Workflow that is running replays after a change of it. + """ + + workflow_read_ahead_bytes: int | None = None + """The most bytes of one read's Records that wait in the Worker for Workflow code. + + A read of Workflow code stops asking the Store while this many bytes of its Records + wait in the Worker's memory for Workflow code to take them. The read goes + on when Workflow code takes Records. The first page of a read always comes. ``None`` + takes Core's default of 4 MiB. Set a larger value for large Records, and a smaller one + for a Worker with little memory, since each open read can hold this much. """ _store_map: dict[str, StreamStore] = dataclasses.field( @@ -270,8 +604,14 @@ def __post_init__(self) -> None: """Check the Stores and build the name-keyed map of them. Raises :py:class:`temporalio.exceptions.StreamConfigError` if there is no - Store, or if two Stores share a name. + Store, or if two Stores share a name, and ``ValueError`` if + :py:attr:`workflow_read_ahead_bytes` is below 1. """ + if ( + self.workflow_read_ahead_bytes is not None + and self.workflow_read_ahead_bytes < 1 + ): + raise ValueError("workflow_read_ahead_bytes must be at least 1") if not self.stores: raise StreamConfigError("StreamConfig.stores must have at least one Store") store_map: dict[str, StreamStore] = {} @@ -313,10 +653,24 @@ def _get_store_by_name(self, name: str) -> StreamStore: ) return store + def _bridge_stream_runtimes( + self, + ) -> list[tuple[str, temporalio.bridge.streams.StreamRuntime]]: + """Give the Stream runtime of each Store that runs on the bridge, by Store name. + + Core reads and appends a Stream of Workflow code through these runtimes. A Store + that a user wrote in Python has none. + """ + return [ + (store.name, store._get_stream_runtime()) + for store in self.stores + if isinstance(store, _BridgeStreamStore) + ] + def _store_for( self, ref: temporalio.nexus.system.stream_service.StreamRef[Any], - target: StorageDriverActivityInfo | None, + target: StorageDriverActivityInfo | StorageDriverWorkflowInfo | None, ) -> StreamStore: if ref.store: return self._get_store_by_name(ref.store) @@ -360,6 +714,12 @@ def _refuse_store_plugins( # pyright: ignore[reportUnusedFunction] ) +def _check_not_negative(**durations: timedelta | None) -> None: # pyright: ignore[reportUnusedFunction] + for label, value in durations.items(): + if value is not None and value < timedelta(): + raise ValueError(f"{label} must not be negative") + + def _stream_service() -> nexusrpc.ServiceDefinition: service = nexusrpc.get_service_definition( temporalio.nexus.system.stream_service.StreamService @@ -372,15 +732,6 @@ def _stream_service() -> nexusrpc.ServiceDefinition: _STREAM_SERVICE = _stream_service() -def _workflow_stream_call_error() -> RuntimeError: # pyright: ignore[reportUnusedFunction] - # Not an `ApplicationError`, so it fails the Workflow Task and not the Workflow. A - # Worker with code that has no Stream call can then finish the Workflow. - return RuntimeError( - "Workflow code can't call a Stream in this version. Call it from an Activity " - "through activity.client()." - ) - - async def _call_stream_store( # pyright: ignore[reportUnusedFunction] store: StreamStore, data_converter: temporalio.converter.DataConverter, diff --git a/temporalio/contrib/streams/README.md b/temporalio/contrib/streams/README.md index 4c5a8e46a4..2dff451391 100644 --- a/temporalio/contrib/streams/README.md +++ b/temporalio/contrib/streams/README.md @@ -1,14 +1,16 @@ # Streams helpers -> This package is experimental, and its API can change. +> This package is experimental. Its API can change in a later release. -`temporalio.contrib.streams` holds helpers that sit on top of the Streams API of the SDK. They -run in a client or an Activity, and take a client `Stream`. +`temporalio.contrib.streams` holds helpers that sit on top of the Streams API of the SDK. A +helper takes a client `Stream` or a Workflow `Stream`. ## `BufferedAppender` `BufferedAppender` batches the appends of one Stream. `append` returns immediately, and the -appender writes the buffered Records as one batch. A batch waits at most `max_delay`. +appender writes the buffered Records as one batch. In a client or an Activity a batch waits +at most `max_delay`. In Workflow code a batch starts when the code next yields, so it adds no +timer to History. ```python from temporalio.contrib.streams import BufferedAppender @@ -40,3 +42,130 @@ async def write_parts(ref: StreamRef[str], parts: list[str]) -> None: The retry's Records continue the first attempt's epoch, so a reader does not take them as a replacement of the first attempt's Records. + +## Reads and appends in Workflow code + +The helpers run on the Streams API, so they follow how Workflow code reads, appends and +closes. On a Store with a Rust runtime, such as the Redis Store or `MemoryStreamStore`, Core +reads, appends and closes a Stream itself, with no Activity. Workflow code reads with the same +`read`, `iter_read` and `iter_read_items` as a client. It gets the same pages, but each page has +at most 256 Records. + +Here the Workflow reads the events of a model's answer while the model Activity still runs, +and starts each tool as its call arrives. + +```python +events = workflow.get_stream(events_ref, body_type=ModelEvent) +status = workflow.get_stream(status_ref, body_type=str) +tools = [] +async for record in events.iter_read_items(wait=timedelta(seconds=10)): + event = record.body + if event.kind == "tool_call": + # The tool starts now, before the model ends its answer. + tools.append( + workflow.start_activity( + run_tool, event.tool_call, start_to_close_timeout=timedelta(minutes=1) + ) + ) + await status.append(f"Running {event.tool_call.name}") +results = await asyncio.gather(*tools) +``` + +The loop ends after the page that is `done`, when the Stream is closed and the read gave its +last Record. With no `wait`, a call gives what is in the Stream, and the loop ends at the first +empty page. With a `wait`, a call on a quiet Stream gives an empty page after the wait, and +the loop asks again. So a loop on a Stream that can be quiet for long uses `iter_read` and +leaves after an empty page. A later loop from the `last_offset` of that page goes on there. + +```python +last = None +async for page in tokens.iter_read(from_offset=last, wait=timedelta(seconds=30)): + for record in page.records: + await shown.append(record.body.text) + last = page.last_offset + if not page.records: + # The Stream was quiet for 30 s, or it is done. + break +``` + +Core keeps one read open while a loop asks for its next page in the same activation. A loop +that awaits something else between two pages, as the `append` above does, lets the read close +before the activation completes, and its next page starts a new read. A read that no code +waits on never holds a Workflow Task open. + +The calls of one Workflow Task write one marker in History, whatever the number of Records. A +read has three other costs. + +- A call keeps its Workflow Task open while it waits for its page. A Signal, an Update, a + cancel or an Activity result reaches the Workflow at the next Workflow Task, at the + heartbeat of the task at the latest. That is about 8 s with the default task timeout of + 10 s. A call with a `wait` on a quiet Stream holds its task open for the whole wait. +- By default, `StreamReplaySource.STORE`, the marker holds a range of offsets, a count and a + digest, and no Record. A replay reads the Store again, so the Records must stay in the + Store for as long as the Workflow can replay. A replay whose Store stays unavailable for + 5 s fails only its Workflow Task, with `StreamReplayUnavailable`, and never the Workflow. + The option + `workflow_read_replay_from=StreamReplaySource.HISTORY` of `StreamConfig` puts the Records + into the marker, so a replay needs no Store. Then one Workflow Task's marker holds at most + 1 MiB of Records. Past that, the task completes and the read goes on in the next one. +- A replay on a Worker with no cache reads the Store again on every Workflow Task. + +While Workflow code is busy, a read holds at most 4 MiB of its Records in the Worker, then +waits until the code takes them. `workflow_read_ahead_bytes` of `StreamConfig` changes that +bound, for large Records or a Worker with little memory. A read gives Workflow code its Records +as soon as no activation of the Workflow is pending. A page that Workflow code gets in one +activation has at most 256 Records, and the rest come in the next activation with no wait. + +An append and a close of Workflow code always write as the Workflow, with the Workflow Task as +their epoch. A `producer=` of another kind raises `StreamError` with the reason +`STREAM_ERROR_REASON_INVALID`. An append runs inside its Workflow Task and records a marker with no body. An +append is at-least-once: when a Worker crashes or its run is evicted before the task with the +append completes, the rerun of that task writes a second copy with the epoch of the new task. + +Core tries a read, an append or a close again while its Store answers `StorageUnavailable`. +It waits from 100 ms up to 2 s between tries, for at most 30 s. Only then does Workflow code +get the retryable `StreamError`. Every other reason raises on the first try. An append of +Workflow code always carries a `sequence`, so the Store writes its Records once, also when a +try reached the Store but its answer got lost. + +A close spans two Workflow Tasks. The task that calls `close` records only the intent. Core +closes the Stream after the server took that task, and the next task gives the result. So a +failed task closes nothing, and its retry can still append. A Workflow that awaits its close +never completes in the task that asked for it. Await the close, also in a `finally` block. + +```python +stream = await workflow.create_stream(id=stream_id, body_type=str) +try: + await stream.append("a") +finally: + await stream.close() +``` + +A close that the code doesn't await still happens when the Workflow ends in the same task, +after the server took that task. A graceful shutdown of the Worker waits for such a close, for +at most 10 s. Only a Worker that dies there, or a Store that doesn't answer in that time, loses +the close, and the Stream stays open. + +A Store isn't meant to be written in Python, since such a Store has no Rust runtime. On one, +a read, an append and a close of Workflow code fail the Workflow. + +## Metrics + +Core records these metrics for the Direct calls of Workflow code. Their prefix is the metric +prefix of the `Runtime`, which is `temporal_` by default. Each metric has the attribute +`stream_store` with the Store of the call, next to the `namespace`, `task_queue` and +`workflow_type` that a Local Activity metric has. A replay that takes a result from a marker +counts nothing. + +| Metric | What it counts | +| --- | --- | +| `temporal_stream_read_total` | Reads that Core started | +| `temporal_stream_read_failed` | Reads that ended with a failure | +| `temporal_stream_read_records` | Records that reads gave to Workflow code | +| `temporal_stream_append_total`, `_failed` | Appends that Core sent, and those that failed | +| `temporal_stream_append_latency` | From the start of an append in Core to its result | +| `temporal_stream_close_total`, `_failed` | Closes that Core sent, and those that failed | +| `temporal_stream_close_latency` | From the start of a close in Core to its result | +| `temporal_stream_close_abandoned` | Closes that a graceful shutdown gave up after 10 s | +| `temporal_stream_replay_read_total`, `_failed` | Marker entries that a replay read again | +| `temporal_stream_replay_read_latency` | The time of one such read and its check | diff --git a/temporalio/contrib/streams/__init__.py b/temporalio/contrib/streams/__init__.py index 95318f94d1..ac9a7c6392 100644 --- a/temporalio/contrib/streams/__init__.py +++ b/temporalio/contrib/streams/__init__.py @@ -1,9 +1,9 @@ """Helpers that sit on top of the Streams API. -:py:class:`BufferedAppender` batches the appends of one client Stream. -:py:func:`current_producer` gives the Producer that an append of the calling code sends by -default, and :py:func:`as_producer` gives back a Producer that code stored. They run in a -client or an Activity. +:py:class:`BufferedAppender` batches the appends of one Stream. It takes a client Stream or a +Workflow Stream. :py:func:`current_producer` gives the Producer that an append of the +calling code sends by default, and :py:func:`as_producer` gives back a Producer that code +stored. .. warning:: This package is experimental and may change in future versions. diff --git a/temporalio/contrib/streams/_appender.py b/temporalio/contrib/streams/_appender.py index 8625da3d80..75a6213e05 100644 --- a/temporalio/contrib/streams/_appender.py +++ b/temporalio/contrib/streams/_appender.py @@ -9,11 +9,17 @@ import asyncio from datetime import timedelta from types import TracebackType -from typing import Generic, TypeVar +from typing import TYPE_CHECKING, Generic, TypeVar from typing_extensions import Self import temporalio.nexus.system.stream_service.client +import temporalio.workflow + +if TYPE_CHECKING: + # The Workflow Stream class is in a private module of the generated code. Only its type + # is needed, since the appender calls the same `append` on both Streams. + import temporalio.nexus.system.stream_service.workflow._resources.stream BodyT = TypeVar("BodyT") @@ -32,12 +38,20 @@ class BufferedAppender(Generic[BodyT]): Records appended during a write wait for the next batch, and they keep their order. A batch holds at most ``max_batch_records`` Records. - It takes a client Stream, in a client or an Activity. It writes the buffered Records at - most ``max_delay`` after the first of them, or immediately when they fill a batch. + It takes a client Stream or a Workflow Stream. When a write starts depends on where + :py:meth:`append` runs, not on the type of the Stream: + + * In a client or an Activity, the appender writes the buffered Records at most + ``max_delay`` after the first of them, or immediately when they fill a batch. + * In Workflow code, a timer would go into History, so the appender uses none. The first + write starts when the calling code next yields, so the appends of one activation go as + one write. Each write is the Workflow ``append`` of the Stream, which Core makes inside + the Workflow Task with no Activity. A replay takes the results from History and writes + nothing. Use it in ``async with``, which writes the remaining Records at the end of the block, or await :py:meth:`flush`. Without ``async with`` or a :py:meth:`flush`, the Records still in - the buffer are lost. The end of an Activity attempt doesn't write them. + the buffer are lost. The end of an Activity attempt or of a Workflow doesn't write them. ``await flush()`` is the backpressure point. A caller that appends faster than the Store accepts Records must await it from time to time, so the buffer doesn't grow without bound. @@ -54,7 +68,10 @@ class BufferedAppender(Generic[BodyT]): def __init__( self, - stream: temporalio.nexus.system.stream_service.client.Stream[BodyT], + stream: temporalio.nexus.system.stream_service.client.Stream[BodyT] + | temporalio.nexus.system.stream_service.workflow._resources.stream.Stream[ + BodyT + ], *, max_delay: timedelta | None = None, max_batch_records: int | None = None, @@ -62,17 +79,18 @@ def __init__( """Create an appender. It starts no write until the first append. Args: - stream: The client Stream to append to. + stream: The Stream to append to, a client Stream or a Workflow Stream. max_delay: The longest time that a Record waits in the buffer before its batch - starts to write. It must not be negative. ``None`` means 100 milliseconds. + starts to write, in a client or an Activity. It must not be negative. + ``None`` means 100 milliseconds. Workflow code must leave it ``None``. max_batch_records: The most Records of one batch, from 1 to 1,000. Set it to the ``max_batch_records`` of the Store's :py:attr:`temporalio.client.StreamStore.limits` when the Store allows less. ``None`` means 1,000. Raises: - ValueError: ``max_delay`` is negative, or ``max_batch_records`` is below 1 or - above 1,000. + ValueError: ``max_delay`` is negative, ``max_delay`` is set in Workflow code, or + ``max_batch_records`` is below 1 or above 1,000. """ if max_delay is not None and max_delay < timedelta(): raise ValueError("max_delay must not be negative") @@ -86,9 +104,11 @@ def __init__( self._stream = stream self._max_delay = max_delay self._max_batch_records = max_batch_records + self._check_delay() self._buffer: list[BodyT] = [] # When the oldest buffered Record arrived, so its batch starts at most max_delay later. self._first_at = 0.0 + # The timer of a client or an Activity, or the start at the next yield of Workflow code. self._timer: asyncio.Handle | None = None self._writing: asyncio.Task[None] | None = None self._appended = 0 @@ -111,6 +131,7 @@ def append(self, *records: BodyT) -> None: self._raise_error() if not records: raise TypeError("append takes one or more values") + self._check_delay() loop = asyncio.get_running_loop() if not self._buffer: self._first_at = loop.time() @@ -164,10 +185,22 @@ def _raise_error(self) -> None: if self._error is not None: raise self._error + def _check_delay(self) -> None: + if self._max_delay is not None and temporalio.workflow.in_workflow(): + raise ValueError( + "BufferedAppender takes no max_delay in Workflow code, since a delay " + "would put a timer into History. Leave max_delay as None." + ) + def _schedule(self) -> None: if self._writing is not None or not self._buffer: return - if len(self._buffer) >= self._max_batch_records: + if temporalio.workflow.in_workflow(): + # A timer of the Workflow event loop is durable. A start at the next yield + # takes every Record of this activation into one write, and adds no event. + if self._timer is None: + self._timer = asyncio.get_running_loop().call_soon(self._start) + elif len(self._buffer) >= self._max_batch_records: self._start() elif self._timer is None: max_delay = self._max_delay or _DEFAULT_MAX_DELAY diff --git a/temporalio/contrib/streams/_producer.py b/temporalio/contrib/streams/_producer.py index 51518d942c..f94b0d1baf 100644 --- a/temporalio/contrib/streams/_producer.py +++ b/temporalio/contrib/streams/_producer.py @@ -3,7 +3,6 @@ from __future__ import annotations import temporalio.api.stream.v1 -import temporalio.client._stream_store import temporalio.converter._payload_converter import temporalio.nexus.system._support import temporalio.workflow @@ -14,23 +13,23 @@ def current_producer() -> Producer: """Give the Producer that an append or a close of the calling code sends by default. An ``append`` or a ``close`` with no ``producer=`` sends this Producer. In an - Activity or a Local Activity it is the Producer of the current attempt. In any other - code it is the client Producer of this process. It does no I/O. + Activity or a Local Activity it is the Producer of the current attempt. In Workflow + code it is the Workflow's Producer. In any other code it is the client Producer of + this process. It does no I/O. + + On a Store, an append or a close of Workflow code writes as this Producer, with the + Workflow Task that runs it as its epoch. An Activity can keep the result and pass it to a later ``append`` or ``close`` with ``producer=``. That call then writes as this Producer with its epoch, also in a later attempt. Heartbeat details give it back as its proto message, so pass it through :py:func:`as_producer` first. - Raises: - RuntimeError: It runs in Workflow code, which can't call a Stream in this - version. - .. warning:: This API is experimental and subject to change. """ if temporalio.workflow.in_workflow(): - raise temporalio.client._stream_store._workflow_stream_call_error() + return temporalio.nexus.system._support.workflow_producer() return temporalio.nexus.system._support.default_producer() diff --git a/temporalio/exceptions.py b/temporalio/exceptions.py index 3f2df0fc9a..6109f54ea0 100644 --- a/temporalio/exceptions.py +++ b/temporalio/exceptions.py @@ -212,13 +212,16 @@ def category(self) -> ApplicationErrorCategory: class StreamError(ApplicationError): """A failed Stream call. ``reason`` tells the cases apart. - One class covers every failed call in client and Activity code, so match on - ``reason`` and not on the type. The reason is a ``StreamErrorReason``, such as + One class covers every failed call in client, Activity and Workflow code, so match + on ``reason`` and not on the type. The reason is a ``StreamErrorReason``, such as ``STREAM_ERROR_REASON_NOT_FOUND`` (nobody created the Stream), ``STREAM_ERROR_REASON_CLOSED`` (the Stream is closed), or ``STREAM_ERROR_REASON_ALREADY_EXISTS`` (a strict create of a Stream that exists). A Store outage, ``STREAM_ERROR_REASON_STORAGE_UNAVAILABLE``, is the one retryable - reason. + reason. On a Store with a Rust runtime, Core tries a read, an append or a close of + Workflow code again while the Store is unavailable. It waits from 100 ms up to 2 s + between tries, and Workflow code gets this reason only after 30 s. Every other + reason raises on the first try. .. warning:: This API is experimental and subject to change. @@ -249,6 +252,40 @@ def reason(self) -> temporalio.api.enums.v1.StreamErrorReason.ValueType: return self._reason +def _stream_error_from_reason(reason: int, message: str) -> StreamError: + if reason not in temporalio.api.enums.v1.StreamErrorReason.values(): + # A reason that this SDK doesn't know gives the unspecified reason. + reason = _STREAM_ERROR_REASON_UNSPECIFIED + return StreamError( + message, reason=temporalio.api.enums.v1.StreamErrorReason.ValueType(reason) + ) + + +def _stream_error_from_call( # pyright: ignore[reportUnusedFunction] + message: str, details: bytes +) -> StreamError: + # A Store on the bridge fails with the encoded details, as a gRPC status has them. + proto = temporalio.api.stream.v1.StreamErrorDetails.FromString(details) + return _stream_error_from_reason(proto.reason, message) + + +def _stream_error_from_failure( # pyright: ignore[reportUnusedFunction] + err: BaseException, +) -> StreamError | None: + # A Local Activity or a Nexus operation hands back a plain application error, so the + # error is rebuilt from its type and details. + if isinstance(err, StreamError): + return err + if not isinstance(err, ApplicationError) or err.type != "StreamError": + return None + reason: int = _STREAM_ERROR_REASON_UNSPECIFIED + for detail in err.details: + if isinstance(detail, temporalio.api.stream.v1.StreamErrorDetails): + reason = detail.reason + break + return _stream_error_from_reason(reason, err.message) + + class StreamConfigError(RuntimeError): """A Stream call can't find its Store, because of how the Stores are set up. @@ -257,12 +294,50 @@ class StreamConfigError(RuntimeError): * A Stream call is made on a client with no Stream config. This release has no Streams on the server, so every Stream call needs a Store. + * A Workflow's Stream call has a ref that names no Store, on a Worker whose + client has more than one Store and no selector. This fails the Workflow Task. * A :py:class:`temporalio.client.StreamConfig` has no Store, or has two Stores with one name. * A call names no Store on a config with more than one Store and no selector. * A ref names a Store that the config of the client doesn't have. * The selector returns a Store that isn't one of the config's Stores. * A :py:class:`temporalio.client.StreamStore` is given in ``plugins``. + * A Workflow's Stream call has a ref that names a Store that the config of the + Worker's client doesn't have. This fails the Workflow Task. + * This build of temporalio has no Stream runtime. + + A Workflow's Stream call on a Worker whose client has no Stream config doesn't + raise this class. Neither does a read, an append or a close of Workflow code on a + Store written in Python, which has no Rust runtime for Core to call. Workflow code + gets an :py:class:`ApplicationError` with the ``type`` ``StreamConfigError`` and + ``non_retryable`` set, so an uncaught one fails the Workflow. Workflow code can + catch this error. A Workflow that catches it must not go on to make the same call on + a Worker that has a config, since the History then has no record of the first call. + + .. warning:: + This API is experimental and subject to change. + """ + + +class StreamReplayError(TemporalError): + """A replay could not read the Records of a read of Workflow code from its Store again. + + A read under the default :py:attr:`temporalio.client.StreamReplaySource.STORE` keeps + no Record in History, so a replay reads the Records from the Store again. When the + Store doesn't have them, for example after the Stream's Retention, or they differ from + what History says, the replay can't go on. + + By default that fails only the Workflow Task, so a later attempt works once the + Records are back. With this class in ``workflow_failure_exception_types`` of the + Worker, or in ``failure_exception_types`` of ``@workflow.defn``, the Workflow fails + instead. Its failure is an :py:class:`ApplicationError` with the ``type`` + ``StreamReplayError``, and Core raises no instance of this class. + + A Store that doesn't answer is not this error, since the Records may still be + there. A replay tries the Store again for 5 s. If it stays unavailable, only the + Workflow Task fails, with a message that starts with ``StreamReplayUnavailable``. + That holds also with this class in ``workflow_failure_exception_types`` or + ``failure_exception_types``, so a later attempt of the task can read the Records. .. warning:: This API is experimental and subject to change. diff --git a/temporalio/nexus/system/stream_service/workflow/_poll.py b/temporalio/nexus/system/stream_service/workflow/_poll.py new file mode 100644 index 0000000000..a653d84f93 --- /dev/null +++ b/temporalio/nexus/system/stream_service/workflow/_poll.py @@ -0,0 +1,77 @@ +# Generated by nexgen v0.2.12. DO NOT EDIT! + +from __future__ import annotations + +import collections +import collections.abc +import typing + +_PollRequestT = typing.TypeVar("_PollRequestT") +_PollResponseT = typing.TypeVar("_PollResponseT") +_PollItemT = typing.TypeVar("_PollItemT") + + +@typing.final +class PollIterator(typing.Generic[_PollRequestT, _PollItemT]): + """Yields each item of the pages of a poll. + + ``next_request`` is the request to resume with. After an item, the resume helper gives + it. Without a resume helper, it reads the item's page again. After a page's last item, + it is the page helper's request, which is ``None`` after the last page. + """ + + def __init__( + self, + request: _PollRequestT, + pages: collections.abc.AsyncIterator[_PollResponseT], + items: collections.abc.Callable[ + [_PollResponseT], collections.abc.Sequence[_PollItemT] | None + ], + next_page: collections.abc.Callable[ + [_PollRequestT, _PollResponseT], _PollRequestT | None + ], + resume: collections.abc.Callable[[_PollRequestT, _PollItemT], _PollRequestT] + | None, + ) -> None: + async def read( + page_request: _PollRequestT, + ) -> tuple[_PollRequestT | None, collections.deque[_PollItemT]]: + response = await anext(pages) + # The page iterator keeps its requests to itself, so the page helper runs here + # again. It runs before the caller gets an item, so a changed item can't change + # the next request. + return next_page(page_request, response), collections.deque( + items(response) or () + ) + + self.next_request: _PollRequestT | None = request + self._read: collections.abc.Callable[ + [_PollRequestT], + collections.abc.Awaitable[ + tuple[_PollRequestT | None, collections.deque[_PollItemT]] + ], + ] = read + self._resume: ( + collections.abc.Callable[[_PollRequestT, _PollItemT], _PollRequestT] | None + ) = resume + self._page: collections.deque[_PollItemT] = collections.deque() + self._page_request: _PollRequestT = request + self._page_next: _PollRequestT | None = None + + def __aiter__(self) -> PollIterator[_PollRequestT, _PollItemT]: + return self + + async def __anext__(self) -> _PollItemT: + while not self._page: + if self.next_request is None: + raise StopAsyncIteration + self._page_request = self.next_request + self._page_next, self._page = await self._read(self._page_request) + if not self._page: + self.next_request = self._page_next + item = self._page.popleft() + if not self._page: + self.next_request = self._page_next + elif self._resume is not None: + self.next_request = self._resume(self._page_request, item) + return item diff --git a/temporalio/nexus/system/stream_service/workflow/_resources/stream.py b/temporalio/nexus/system/stream_service/workflow/_resources/stream.py index df548d2528..0aa32cc380 100644 --- a/temporalio/nexus/system/stream_service/workflow/_resources/stream.py +++ b/temporalio/nexus/system/stream_service/workflow/_resources/stream.py @@ -2,6 +2,8 @@ from __future__ import annotations +import collections.abc +import datetime import types import typing @@ -20,8 +22,12 @@ DescribeStreamRequest, DescribeStreamResponse, Producer, + ReadRecordsRequest, + ReadRecordsResponse, + StreamRecord, StreamRef, ) +from .._poll import PollIterator BodyT = typing.TypeVar("BodyT") @@ -86,6 +92,103 @@ async def close( handle = await _close_stream(request) await handle + async def read( + self, + from_offset: bytes | None = None, + max_records: int | None = None, + wait: datetime.timedelta | None = None, + max_bytes: int | None = None, + ) -> ReadRecordsResponse[BodyT]: + """Read one page of Records. The poll iterator reads them all.""" + request = ReadRecordsRequest( + ref=self.ref, + from_offset=from_offset, + max_records=max_records, + wait=wait, + max_bytes=max_bytes, + ) + handle = await _read_records(request, self.body_type) + return await handle + + async def iter_read( + self, + from_offset: bytes | None = None, + max_records: int | None = None, + wait: datetime.timedelta | None = None, + max_bytes: int | None = None, + ) -> collections.abc.AsyncGenerator[ReadRecordsResponse[BodyT], None]: + """Yield each response of ``read`` until the poll helper stops. + + Read one page of Records. The poll iterator reads them all. + + Args: + from_offset: Read after this offset. Empty is the beginning. A too high + offset fails. + max_records: About this many Records per page, 1 to 1000. + wait: How long to wait for a new Record before an empty answer. At most 60 + s. + max_bytes: The maximum Record bytes in a page. A larger Record is still + returned alone. + """ + request = ReadRecordsRequest( + ref=self.ref, + from_offset=from_offset, + max_records=max_records, + wait=wait, + max_bytes=max_bytes, + ) + while True: + handle = await _read_records(request, self.body_type) + response = await handle + next_request = _support.next_read_request(request, response) + yield response + if next_request is None: + return + request = next_request + + def iter_read_items( + self, + from_offset: bytes | None = None, + max_records: int | None = None, + wait: datetime.timedelta | None = None, + max_bytes: int | None = None, + ) -> PollIterator[ReadRecordsRequest[BodyT], StreamRecord[BodyT]]: + """Yield each item of ``iter_read``, page by page. + + ``next_request`` resumes after the last item that the iterator yielded. It is + ``None`` after the last page. + + Read one page of Records. The poll iterator reads them all. + + Args: + from_offset: Read after this offset. Empty is the beginning. A too high + offset fails. + max_records: About this many Records per page, 1 to 1000. + wait: How long to wait for a new Record before an empty answer. At most 60 + s. + max_bytes: The maximum Record bytes in a page. A larger Record is still + returned alone. + """ + request = ReadRecordsRequest( + ref=self.ref, + from_offset=from_offset, + max_records=max_records, + wait=wait, + max_bytes=max_bytes, + ) + return PollIterator( + request, + self.iter_read( + from_offset=from_offset, + max_records=max_records, + wait=wait, + max_bytes=max_bytes, + ), + lambda response: response.records, + _support.next_read_request, + _support.resume_read_request, + ) + async def describe( self, ) -> DescribeStreamResponse: @@ -164,6 +267,21 @@ async def _close_stream( ) +async def _read_records( + request: ReadRecordsRequest[BodyT], + body_type: type[BodyT], +) -> temporalio.workflow.NexusOperationHandle[ReadRecordsResponse[BodyT],]: + nexus_client = temporalio.workflow.create_nexus_client( + service="temporal.api.streamservice.v1.StreamService", + endpoint=temporalio.nexus.system.TEMPORAL_SYSTEM_ENDPOINT, + ) + return await nexus_client.start_operation( + operation="ReadRecords", + input=request, + output_type=ReadRecordsResponse[body_type], + ) + + async def _describe_stream( request: DescribeStreamRequest[BodyT], ) -> temporalio.workflow.NexusOperationHandle[DescribeStreamResponse,]: diff --git a/temporalio/testing/_stream_store.py b/temporalio/testing/_stream_store.py new file mode 100644 index 0000000000..42c8500fd1 --- /dev/null +++ b/temporalio/testing/_stream_store.py @@ -0,0 +1,57 @@ +"""A Stream Store in the memory of the process, for tests.""" + +from __future__ import annotations + +from datetime import timedelta + +import temporalio.bridge.runtime +import temporalio.bridge.streams +import temporalio.client._stream_store +import temporalio.runtime + + +class MemoryStreamStore(temporalio.client._stream_store._BridgeStreamStore): + """A Stream Store that keeps its Streams in the memory of this process, for tests. + + It runs the same Stream runtime and rules as every other Store on the bridge. A + failed call raises :py:class:`temporalio.exceptions.StreamError`. + + .. warning:: + This API is experimental and subject to change. + """ + + def __init__( + self, + *, + name: str = "memory", + retention: timedelta | None = None, + producer_idle_ttl: timedelta | None = None, + runtime: temporalio.runtime.Runtime | None = None, + ) -> None: + """Create a memory Store. It builds its Stream runtime at its first use. + + Args: + name: Name of the Store, which the refs of its Streams carry. + retention: How long a closed Stream stays. ``None`` takes the default of + the Stream runtime. + producer_idle_ttl: How long an open Stream stays after the last write of any + Producer. ``None`` keeps an open Stream until its close. + runtime: Runtime whose Tokio threads run the Store. ``None`` takes + :py:meth:`temporalio.runtime.Runtime.default` at the first use. + """ + temporalio.client._stream_store._check_not_negative( + retention=retention, producer_idle_ttl=producer_idle_ttl + ) + super().__init__(name=name, runtime=runtime) + self._retention = retention + self._producer_idle_ttl = producer_idle_ttl + + def _new_stream_runtime( + self, runtime: temporalio.bridge.runtime.Runtime + ) -> temporalio.bridge.streams.StreamRuntime: + return temporalio.bridge.streams.StreamRuntime.new_memory( + runtime, + name=self._name, + retention=self._retention, + producer_idle_ttl=self._producer_idle_ttl, + ) diff --git a/temporalio/worker/_command_aware_visitor.py b/temporalio/worker/_command_aware_visitor.py index 888c06f298..8a42bc9a8a 100644 --- a/temporalio/worker/_command_aware_visitor.py +++ b/temporalio/worker/_command_aware_visitor.py @@ -16,8 +16,13 @@ ResolveNexusOperationStart, ResolveRequestCancelExternalWorkflow, ResolveSignalExternalWorkflow, + ResolveStreamAppend, + ResolveStreamClose, + ResolveStreamRead, + StreamReadEnd, ) from temporalio.bridge.proto.workflow_commands.workflow_commands_pb2 import ( + AppendToStream, CompleteWorkflowExecution, FailWorkflowExecution, ScheduleActivity, @@ -37,6 +42,8 @@ class CommandInfo: command_type: CommandType.ValueType command_seq: int update_protocol_instance_id: str | None = None + stream: bool = False + """Whether the payloads are the bodies of a Stream append or the Records of a read.""" current_command_info: contextvars.ContextVar[CommandInfo | None] = ( @@ -57,6 +64,7 @@ def __init__( skip_search_attributes: bool = False, skip_headers: bool = False, concurrency_limit: int = 1, + skip_stream_bodies: bool = False, ) -> None: """Creates a new command-aware payload visitor. @@ -65,12 +73,53 @@ def __init__( skip_headers: If True, headers are not visited. concurrency_limit: Maximum number of payload visits that may run concurrently during a single call to visit(). Defaults to 1. + skip_stream_bodies: If True, the bodies of a Stream append are not + visited. """ super().__init__( skip_search_attributes=skip_search_attributes, skip_headers=skip_headers, concurrency_limit=concurrency_limit, ) + self.skip_stream_bodies = skip_stream_bodies + + # A Store keeps the bodies of a Workflow's append as a client and an Activity write + # them: with the codec, with no serialization context and with no external storage. So + # every reader decodes them. Each body is one Record, so the codec gets each body alone, + # as for those paths. + async def _visit_coresdk_workflow_commands_AppendToStream( + self, fs: VisitorFunctions, o: AppendToStream + ) -> None: + if self.skip_stream_bodies: + return + with current_command(CommandType.COMMAND_TYPE_UNSPECIFIED, o.seq, stream=True): + for body in o.bodies: + await self._visit_temporal_api_common_v1_Payload(fs, body) + + # The Records of a read come from any writer of the Stream, so the codec decodes them + # with no serialization context, as a client and an Activity do. + async def _visit_coresdk_workflow_activation_ResolveStreamRead( + self, fs: VisitorFunctions, o: ResolveStreamRead + ) -> None: + with current_command(CommandType.COMMAND_TYPE_UNSPECIFIED, o.seq, stream=True): + await super()._visit_coresdk_workflow_activation_ResolveStreamRead(fs, o) + + # Core builds the failure of a Stream read, append or close itself, with no codec, so no + # pass may decode it. + async def _visit_coresdk_workflow_activation_StreamReadEnd( + self, fs: VisitorFunctions, o: StreamReadEnd + ) -> None: + pass + + async def _visit_coresdk_workflow_activation_ResolveStreamAppend( + self, fs: VisitorFunctions, o: ResolveStreamAppend + ) -> None: + pass + + async def _visit_coresdk_workflow_activation_ResolveStreamClose( + self, fs: VisitorFunctions, o: ResolveStreamClose + ) -> None: + pass # Workflow commands with payloads async def _visit_coresdk_workflow_commands_CompleteWorkflowExecution( @@ -218,6 +267,7 @@ def current_command( command_seq: int, *, update_protocol_instance_id: str | None = None, + stream: bool = False, ) -> Iterator[None]: """Context manager for setting command info.""" token = current_command_info.set( @@ -225,6 +275,7 @@ def current_command( command_type=command_type, command_seq=command_seq, update_protocol_instance_id=update_protocol_instance_id, + stream=stream, ) ) try: diff --git a/temporalio/worker/_replayer.py b/temporalio/worker/_replayer.py index 62a27b29cf..c55bb0bb15 100644 --- a/temporalio/worker/_replayer.py +++ b/temporalio/worker/_replayer.py @@ -22,6 +22,11 @@ from ..common import HeaderCodecBehavior from ._interceptor import Interceptor +from ._stream_store import ( + _bridge_read_ahead_bytes, + _bridge_stream_runtimes, + _WorkflowStreamStore, +) from ._worker import load_default_build_id from ._workflow import _WorkflowWorker from ._workflow_instance import ( @@ -56,6 +61,7 @@ def __init__( runtime: temporalio.runtime.Runtime | None = None, disable_safe_workflow_eviction: bool = False, header_codec_behavior: HeaderCodecBehavior = HeaderCodecBehavior.NO_CODEC, + stream_config: temporalio.client.StreamConfig | None = None, ) -> None: """Create a replayer to replay workflows from history. @@ -64,6 +70,22 @@ def __init__( the replayer that were passed to the worker when the workflow originally ran. + ``stream_config`` takes the Stream config of the client that the + worker had, so a replay makes the same commands. A history that records a + create or a describe, which run in a Store's Local Activity, needs it. With no + Stream config, Workflow code gets an error in place of that + call, and the replay fails with a nondeterminism error. A replay calls no method + of its Stores from Python. An append or a close needs no Stream + config, and a replay closes nothing. A read of Workflow code that ran under the + option ``HISTORY`` of ``workflow_read_replay_from`` needs none either. That holds + also when the ref has no Store, since Core takes the Store from the history. A + read that ran under the default, ``StreamReplaySource.STORE``, has no Records in + the history. Core reads them from the Store again, so the replay needs this + config and Stores that still have the Records. Without them the replay fails + with a :py:class:`RuntimeError` whose message has + ``StreamReplayError``, the Stream and the cause. A Store that stays unavailable + for 5 s gives ``StreamReplayUnavailable`` in place of ``StreamReplayError``. + Note, unlike the worker, for the replayer the workflow_task_executor will default to a new thread pool executor with no max_workers set that will be shared across all replay calls and never explicitly shut down. @@ -86,6 +108,7 @@ def __init__( runtime=runtime, disable_safe_workflow_eviction=disable_safe_workflow_eviction, header_codec_behavior=header_codec_behavior, + stream_config=stream_config, ) self._initial_config = self._config.copy() self._default_workflow_logic_flags = set(_DEFAULT_ENABLED_WORKFLOW_LOGIC_FLAGS) @@ -287,6 +310,9 @@ def on_eviction_hook( default_workflow_logic_flags=frozenset( self._default_workflow_logic_flags ), + stream_store=_WorkflowStreamStore.from_config( + self._config.get("stream_config") + ), ) external_storage = data_converter.external_storage storage_driver_types = ( @@ -307,6 +333,10 @@ def on_eviction_hook( # per workflow type nondeterminism_as_workflow_fail=workflow_worker.nondeterminism_as_workflow_fail(), nondeterminism_as_workflow_fail_for_types=workflow_worker.nondeterminism_as_workflow_fail_for_types(), + stream_replay_as_workflow_fail=workflow_worker.stream_replay_as_workflow_fail(), + stream_replay_as_workflow_fail_for_types=( + workflow_worker.stream_replay_as_workflow_fail_for_types() + ), # All values below are ignored but required by Core max_cached_workflows=2, tuner=temporalio.bridge.worker.TunerHolder( @@ -356,6 +386,12 @@ def on_eviction_hook( (plugin.name(), plugin.version()) for plugin in self.plugins ], storage_drivers=storage_driver_types, + stream_runtimes=_bridge_stream_runtimes( + self._config.get("stream_config") + ), + stream_read_ahead_bytes=_bridge_read_ahead_bytes( + self._config.get("stream_config") + ), ), ) bridge_worker_scope = bridge_worker @@ -435,6 +471,7 @@ class ReplayerConfig(TypedDict, total=False): runtime: temporalio.runtime.Runtime | None disable_safe_workflow_eviction: bool header_codec_behavior: HeaderCodecBehavior + stream_config: temporalio.client.StreamConfig | None @dataclass(frozen=True) diff --git a/temporalio/worker/_stream_store.py b/temporalio/worker/_stream_store.py new file mode 100644 index 0000000000..13c1abc29d --- /dev/null +++ b/temporalio/worker/_stream_store.py @@ -0,0 +1,517 @@ +"""How Workflow code reaches the Stream Stores of its Worker's client.""" + +from __future__ import annotations + +import asyncio +import collections +import dataclasses +from collections.abc import Generator, Iterable +from datetime import timedelta +from typing import TYPE_CHECKING, Any + +from nexusrpc import OutputT + +import temporalio.api.enums.v1 +import temporalio.api.failure.v1 +import temporalio.api.stream.v1 +import temporalio.api.streamservice.v1 +import temporalio.bridge.proto.workflow_commands +import temporalio.exceptions +import temporalio.workflow +from temporalio.client._stream_store import ( + _STREAM_OPERATIONS, + StreamConfig, + StreamReplaySource, + StreamStore, + _BridgeStreamStore, + _request_ref, +) +from temporalio.converter import StorageDriverWorkflowInfo + +from ._interceptor import StartNexusOperationInput + +if TYPE_CHECKING: + from ._workflow_instance import _WorkflowInstanceImpl + + +def _no_stream_config_error() -> temporalio.exceptions.ApplicationError: # pyright: ignore[reportUnusedFunction] + # A `StreamConfigError` would fail the Workflow Task, and its retries would fail the + # same way for good. A Worker with no config is a setup that a user has to see, so + # the Workflow fails instead. + return temporalio.exceptions.ApplicationError( + "The client of this Worker has no Stream Store. Give one in " + "stream_config=StreamConfig(stores=[...]).", + type="StreamConfigError", + non_retryable=True, + ) + + +# The Direct calls of Workflow code, by Nexus operation name. Each has the word of an +# error message and the call that an Activity makes in its place. +_DIRECT_CALLS = { + "ReadRecords": ("read", "Read the Stream"), + "AppendRecords": ("append", "Append to the Stream"), + "CloseStream": ("close", "Close the Stream"), +} + + +def _no_direct_call_error( + store: StreamStore, operation: str +) -> temporalio.exceptions.ApplicationError: + # Core makes a Workflow's read, append and close itself, and it can't call a Store + # that runs in Python. Each retry of the task would fail the same way for good, so the + # Workflow fails instead, as for a Worker with no Stream config. + call, advice = _DIRECT_CALLS[operation] + return temporalio.exceptions.ApplicationError( + f"The Stream Store '{store.name}' has no Direct {call}, since it is written in " + f"Python. {advice} in an Activity, or use a Store that runs on the bridge, " + "such as the Redis Store or MemoryStreamStore.", + type="StreamConfigError", + non_retryable=True, + ) + + +def _not_a_workflow_producer_error( + operation: str, +) -> temporalio.exceptions.StreamError: + # Core sets the Workflow Task as the epoch of the Producer, which only a Producer of + # kind Workflow has. + call, _ = _DIRECT_CALLS[operation] + return temporalio.exceptions.StreamError( + f"A Stream {call} of Workflow code writes as a Producer of kind Workflow. " + "Leave out producer=, or give a Producer of kind Workflow.", + reason=temporalio.api.enums.v1.StreamErrorReason.STREAM_ERROR_REASON_INVALID, + ) + + +_STREAM_ERROR_DETAILS_TYPE = ( + temporalio.api.stream.v1.StreamErrorDetails.DESCRIPTOR.full_name +) + + +def _stream_error_from_core( # pyright: ignore[reportUnusedFunction] + failure: temporalio.api.failure.v1.Failure, +) -> temporalio.exceptions.StreamError | None: + """Give the Store error of a failure that Core made, or ``None`` for another one.""" + # Core gives a Store error the token of its reason as its type, and puts the reason in + # its details with no codec. The Local Activity path gives the Store's message alone, + # so the token comes off the message. + info = failure.application_failure_info + for payload in info.details.payloads: + if payload.metadata.get("messageType") != _STREAM_ERROR_DETAILS_TYPE.encode(): + continue + details = temporalio.api.stream.v1.StreamErrorDetails.FromString(payload.data) + message = failure.message.removeprefix(f"{info.type}: ") + return temporalio.exceptions._stream_error_from_reason(details.reason, message) + return None + + +def _start_direct_append( + instance: _WorkflowInstanceImpl, + input: StartNexusOperationInput[Any, OutputT], + request: temporalio.api.streamservice.v1.AppendRecordsRequest, + store_name: str, +) -> temporalio.workflow.NexusOperationHandle[OutputT]: + """Send an append of a Workflow Producer to Core, which makes it in the Workflow Task.""" + payload_converter, _ = instance._system_nexus_converters(input) + append = temporalio.bridge.proto.workflow_commands.AppendToStream( + stream=temporalio.api.stream.v1.StreamRef(id=request.ref.id, store=store_name), + bodies=[record.body for record in request.records], + producer=request.producer, + sequence=request.sequence, + ) + append.producer.workflow.ClearField("epoch") + appended = instance._start_stream_append(append) + + async def result() -> OutputT: + response = await appended + return payload_converter._from_transfer_value( + temporalio.api.streamservice.v1.AppendRecordsResponse( + first_offset=response.first_offset, + last_offset=response.last_offset, + ), + input.output_type, + ) + + return _StreamStoreOperationHandle( + instance.create_task(result(), name=f"stream store: {input.operation_name}") + ) + + +def _start_direct_close( + instance: _WorkflowInstanceImpl, + input: StartNexusOperationInput[Any, OutputT], + request: temporalio.api.streamservice.v1.CloseStreamRequest, + store_name: str, +) -> temporalio.workflow.NexusOperationHandle[OutputT]: + """Send a close of a Workflow Producer to Core. + + Core records the intent in this Workflow Task, closes the Stream after the server took + the task, and gives the result in a later task. So a failed task closes nothing. + """ + payload_converter, _ = instance._system_nexus_converters(input) + close = temporalio.bridge.proto.workflow_commands.CloseStream( + stream=temporalio.api.stream.v1.StreamRef(id=request.ref.id, store=store_name), + producer=request.producer, + ) + close.producer.workflow.ClearField("epoch") + closed = instance._start_stream_close(close) + + async def result() -> OutputT: + await closed + return payload_converter._from_transfer_value( + temporalio.api.streamservice.v1.CloseStreamResponse(), input.output_type + ) + + return _StreamStoreOperationHandle( + instance.create_task(result(), name=f"stream store: {input.operation_name}") + ) + + +def _start_direct_call( + instance: _WorkflowInstanceImpl, + input: StartNexusOperationInput[Any, OutputT], + request: temporalio.api.streamservice.v1.AppendRecordsRequest + | temporalio.api.streamservice.v1.CloseStreamRequest, + store_name: str, +) -> temporalio.workflow.NexusOperationHandle[OutputT]: + """Send an append or a close of Workflow code to Core. + + Core makes these calls itself, with the Workflow Task as the epoch of the Producer. + + Raises: + temporalio.exceptions.StreamError: The Producer is not of kind Workflow. + """ + if request.producer.WhichOneof("kind") != "workflow": + raise _not_a_workflow_producer_error(input.operation_name) + if isinstance(request, temporalio.api.streamservice.v1.AppendRecordsRequest): + return _start_direct_append(instance, input, request, store_name) + return _start_direct_close(instance, input, request, store_name) + + +def _start_replayed_direct_call( # pyright: ignore[reportUnusedFunction] + instance: _WorkflowInstanceImpl, + input: StartNexusOperationInput[Any, OutputT], +) -> temporalio.workflow.NexusOperationHandle[OutputT] | None: + """Replay a Direct call on a Worker with no Stream config, or give ``None``. + + The marker of an append or a close holds its result, and the marker of a read can hold + its Records, so the replay needs no Store. A ref with no Store sends an empty name, and + Core takes the Store of History. + """ + if input.operation_name not in ("AppendRecords", "CloseStream", "ReadRecords"): + return None + payload_converter, _ = instance._system_nexus_converters(input) + request = payload_converter._to_transfer_value(input.input) + if isinstance(request, temporalio.api.streamservice.v1.ReadRecordsRequest): + # A replay of a read whose marker holds its Records reads no Store. + return _start_direct_read( + instance, input, request, request.ref.store, records_in_history=True + ) + if isinstance( + request, + ( + temporalio.api.streamservice.v1.AppendRecordsRequest, + temporalio.api.streamservice.v1.CloseStreamRequest, + ), + ): + return _start_direct_call(instance, input, request, request.ref.store) + return None + + +# A client's read gives at most this many Records in a page, and a `max_records` of zero +# or less gives this many. A read of Workflow code takes the same limits. +_MAX_READ_RECORDS = 1000 +# The longest `wait` of a client's read. A longer one waits this long. +_MAX_READ_WAIT = timedelta(seconds=60) + + +@dataclasses.dataclass(frozen=True) +class _ReadKey: + """What a ``read`` call of Workflow code must match to take an open read.""" + + store: str + stream_id: str + offset: bytes + """The offset that the next page starts after. Empty is the start of the Stream.""" + wait: timedelta | None + + +class _DirectRead: # pyright: ignore[reportUnusedClass] + """One Core read of a Stream, which serves the ``read`` calls of Workflow code. + + Core gives its Records in batches. Each ``read`` call takes one page of them, and the + read then waits for the next call under the offset of that page. + """ + + def __init__(self, seq: int) -> None: + self.seq = seq + self.records: collections.deque[temporalio.api.stream.v1.StreamRecord] = ( + collections.deque() + ) + # Whether Core still reads, with no end and no close. + self.running = True + self.done = False + self.error: BaseException | None = None + # A job and an end wake the call that waits, which is at most one. + self._waiters: list[asyncio.Future[None]] = [] + + def kept(self) -> bool: + """Whether the read can serve a later call.""" + # A failure that no call raised yet stays, so the next call raises it. + return self.running or bool(self.records) or self.error is not None + + def take( + self, + records: Iterable[temporalio.api.stream.v1.StreamRecord], + *, + end: bool, + done: bool, + error: BaseException | None, + ) -> None: + """Keep the Records and the end of one job of Core.""" + self.records.extend(records) + if end: + self.running = False + self.done = done + self.error = error + self._wake() + + def close(self) -> None: + """Mark the read as closed in Core. Its buffered Records stay.""" + self.running = False + self._wake() + + def _wake(self) -> None: + for waiter in self._waiters: + if not waiter.done(): + waiter.set_result(None) + + async def page( + self, max_records: int, max_bytes: int | None + ) -> tuple[list[temporalio.api.stream.v1.StreamRecord], bool]: + """Give the next page and whether the Stream is done, and wait when none is buffered. + + Raises: + temporalio.exceptions.StreamError: The read failed. + """ + while not self.records and self.running: + waiter: asyncio.Future[None] = asyncio.get_running_loop().create_future() + self._waiters.append(waiter) + try: + await waiter + finally: + self._waiters.remove(waiter) + if not self.records and self.error is not None: + error, self.error = self.error, None + raise error + page: list[temporalio.api.stream.v1.StreamRecord] = [] + size = 0 + while self.records and len(page) < max_records: + record_size = self.records[0].ByteSize() + if page and max_bytes is not None and size + record_size > max_bytes: + break + page.append(self.records.popleft()) + size += record_size + return page, self.done and not self.records + + +def _read_limits( + request: temporalio.api.streamservice.v1.ReadRecordsRequest, +) -> tuple[int, timedelta | None, int | None]: + """Give the ``max_records``, ``wait`` and ``max_bytes`` of a client's read. + + Raises: + temporalio.exceptions.StreamError: ``max_bytes`` is negative. + """ + max_records = request.max_records + if max_records <= 0 or max_records > _MAX_READ_RECORDS: + max_records = _MAX_READ_RECORDS + wait: timedelta | None = None + if request.HasField("wait") and request.wait.ToTimedelta() > timedelta(): + wait = min(request.wait.ToTimedelta(), _MAX_READ_WAIT) + if request.max_bytes < 0: + raise temporalio.exceptions.StreamError( + "max_bytes must be 0 or more", + reason=temporalio.api.enums.v1.StreamErrorReason.STREAM_ERROR_REASON_INVALID, + ) + return max_records, wait, request.max_bytes or None + + +def _start_direct_read( + instance: _WorkflowInstanceImpl, + input: StartNexusOperationInput[Any, OutputT], + request: temporalio.api.streamservice.v1.ReadRecordsRequest, + store_name: str, + *, + records_in_history: bool, +) -> temporalio.workflow.NexusOperationHandle[OutputT]: + """Answer one ``read`` call of Workflow code from a Core read of the Stream.""" + instance._assert_not_read_only("start stream read") + payload_converter, _ = instance._system_nexus_converters(input) + max_records, wait, max_bytes = _read_limits(request) + key = _ReadKey( + store=store_name, + stream_id=request.ref.id, + offset=request.from_offset, + wait=wait, + ) + read = instance._take_stream_read(key) + if read is None: + start = temporalio.bridge.proto.workflow_commands.StartStreamRead( + stream=temporalio.api.stream.v1.StreamRef( + id=request.ref.id, store=store_name + ), + from_offset=request.from_offset, + records_in_history=records_in_history, + ) + # The pages come from the calls, so the Core read has no cap and no timeout. + # With no `wait`, an idle timeout of zero gives what is in the Stream. + start.idle_timeout.SetInParent() + start.idle_timeout.FromTimedelta(wait or timedelta()) + read = instance._start_stream_read(start) + + async def result() -> OutputT: + try: + records, done = await read.page(max_records, max_bytes) + except BaseException: + instance._park_stream_read(read, key) + raise + last_offset = records[-1].offset if records else request.from_offset + instance._park_stream_read(read, dataclasses.replace(key, offset=last_offset)) + return payload_converter._from_transfer_value( + temporalio.api.streamservice.v1.ReadRecordsResponse( + records=records, last_offset=last_offset, done=done + ), + input.output_type, + ) + + return _StreamStoreOperationHandle( + instance.create_task(result(), name=f"stream store: {input.operation_name}") + ) + + +def _bridge_read_ahead_bytes(config: StreamConfig | None) -> int | None: # pyright: ignore[reportUnusedFunction] + """Give Core the read-ahead bound of the config, or ``None`` for Core's default.""" + return config.workflow_read_ahead_bytes if config is not None else None + + +def _bridge_stream_runtimes(config: StreamConfig | None) -> list[tuple[str, Any]]: # pyright: ignore[reportUnusedFunction] + """Give Core the bridge's object of each Store's Stream runtime, by Store name.""" + if config is None: + return [] + return [(name, runtime._ref) for name, runtime in config._bridge_stream_runtimes()] + + +def _workflow_target() -> StorageDriverWorkflowInfo: + # External storage builds the same value for a Workflow. + info = temporalio.workflow.info() + return StorageDriverWorkflowInfo( + id=info.workflow_id, + type=info.workflow_type, + run_id=info.run_id, + namespace=info.namespace, + ) + + +@dataclasses.dataclass(frozen=True) +class _WorkflowStreamStore: + """The Worker's Stream Stores as Workflow code reaches them.""" + + config: StreamConfig + """The Stream config of the Worker's client.""" + + @classmethod + def from_config(cls, config: StreamConfig | None) -> _WorkflowStreamStore | None: + """Build it from the Stream config of a client, or give ``None`` when it has none.""" + return cls(config=config) if config is not None else None + + def start_operation( + self, + instance: _WorkflowInstanceImpl, + input: StartNexusOperationInput[Any, OutputT], + ) -> temporalio.workflow.NexusOperationHandle[OutputT]: + """Answer a Stream call of Workflow code with the Store that the call goes to.""" + stream_operation = _STREAM_OPERATIONS[input.operation_name] + payload_converter, _ = instance._system_nexus_converters(input) + request = payload_converter._to_transfer_value(input.input) + ref = _request_ref(input.input) + # The choice runs in Workflow code, so a ref of a Store that the config doesn't + # have raises `StreamConfigError` here, with no I/O, and fails the Workflow Task. + store = self.config._store_for(ref, _workflow_target()) + options = self.config.workflow_call_options + if isinstance(request, temporalio.api.streamservice.v1.ReadRecordsRequest): + instance._assert_not_read_only("start stream read") + if not isinstance(store, _BridgeStreamStore): + raise _no_direct_call_error(store, input.operation_name) + return _start_direct_read( + instance, + input, + request, + store.name, + records_in_history=( + self.config.workflow_read_replay_from is StreamReplaySource.HISTORY + ), + ) + if isinstance( + request, + ( + temporalio.api.streamservice.v1.AppendRecordsRequest, + temporalio.api.streamservice.v1.CloseStreamRequest, + ), + ): + if not isinstance(store, _BridgeStreamStore): + raise _no_direct_call_error(store, input.operation_name) + return _start_direct_call(instance, input, request, store.name) + + # A create and a describe run in the Store's Local Activity. + async def result() -> OutputT: + try: + response = await store.call_from_workflow( + input.operation_name, request, options + ) + except temporalio.exceptions.ApplicationError as err: + # Core hands back a failed Local Activity's error without a wrapper. + stream_error = temporalio.exceptions._stream_error_from_failure(err) + if stream_error is not None: + raise stream_error from None + raise + except temporalio.exceptions.ActivityError as err: + # Workflow code gets the Store's error, not the Local Activity's wrapper. + if err.cause is not None: + stream_error = temporalio.exceptions._stream_error_from_failure( + err.cause + ) + if stream_error is not None: + raise stream_error from None + if isinstance( + err.cause, + ( + temporalio.exceptions.ApplicationError, + temporalio.exceptions.TimeoutError, + ), + ): + raise err.cause + raise + return payload_converter._from_transfer_value( + stream_operation.check_response(store, response), input.output_type + ) + + return _StreamStoreOperationHandle( + instance.create_task(result(), name=f"stream store: {input.operation_name}") + ) + + +class _StreamStoreOperationHandle(temporalio.workflow.NexusOperationHandle[OutputT]): + def __init__(self, task: asyncio.Task[OutputT]) -> None: + self._task = task + + def cancel(self) -> bool: + return self._task.cancel() + + def __await__(self) -> Generator[Any, Any, OutputT]: + return self._task.__await__() + + @property + def operation_token(self) -> str | None: + return None diff --git a/temporalio/worker/_worker.py b/temporalio/worker/_worker.py index e06bb99a4b..a05ed05862 100644 --- a/temporalio/worker/_worker.py +++ b/temporalio/worker/_worker.py @@ -34,6 +34,11 @@ from ._interceptor import Interceptor from ._nexus import _NexusWorker from ._plugin import Plugin +from ._stream_store import ( + _bridge_read_ahead_bytes, + _bridge_stream_runtimes, + _WorkflowStreamStore, +) from ._tuning import WorkerTuner from ._workflow import ( _DEFAULT_WORKFLOW_TASK_EXTERNAL_STORAGE_CONCURRENCY, @@ -498,7 +503,11 @@ def _init_from_config(self, client: temporalio.client.Client, config: WorkerConf or bridge_client.config.runtime or temporalio.runtime.Runtime.default() ) - activities = config.get("activities") + activities = list(config.get("activities") or []) + stream_config = client_config.get("stream_config") + if stream_config is not None and config.get("workflows"): + for store in stream_config.stores: + activities.extend(store.worker_activities()) if activities: _warn_if_activity_executor_max_workers_is_inconsistent(config) self._activity_worker = _ActivityWorker( @@ -578,6 +587,7 @@ def check_activity(activity: str): encode_headers=client_config["header_codec_behavior"] != HeaderCodecBehavior.NO_CODEC, max_workflow_task_external_storage_concurrency=max_workflow_task_external_storage_concurrency, + stream_store=_WorkflowStreamStore.from_config(stream_config), ) tuner = config.get("tuner") @@ -667,7 +677,9 @@ def check_activity(activity: str): enable_workflows=self._workflow_worker is not None, enable_local_activities=self._activity_worker is not None and self._workflow_worker is not None, - enable_remote_activities=self._activity_worker is not None + # The Activities of the Stream Stores run as Local Activities, so + # alone they don't make this worker poll for Activity Tasks. + enable_remote_activities=bool(config.get("activities")) and not config.get("no_remote_activities"), enable_nexus=self._nexus_worker is not None, ), @@ -701,6 +713,13 @@ def check_activity(activity: str): if self._workflow_worker else set() ), + stream_replay_as_workflow_fail=self._workflow_worker is not None + and self._workflow_worker.stream_replay_as_workflow_fail(), + stream_replay_as_workflow_fail_for_types=( + self._workflow_worker.stream_replay_as_workflow_fail_for_types() + if self._workflow_worker + else set() + ), versioning_strategy=versioning_strategy, workflow_task_poller_behavior=_poller_behavior_to_bridge( workflow_task_poller_behavior @@ -716,6 +735,12 @@ def check_activity(activity: str): disable_payload_error_limit=config.get( "disable_payload_error_limit", False ), + # Only Workflow code makes Direct calls, so a Worker with no Workflows + # builds no runtime, as it builds no Store Activity. + stream_runtimes=_bridge_stream_runtimes( + stream_config if self._workflow_worker is not None else None + ), + stream_read_ahead_bytes=_bridge_read_ahead_bytes(stream_config), ), ) @@ -751,13 +776,19 @@ def client(self, value: temporalio.client.Client) -> None: Changing the client will make sure the worker starts using it for the next calls it makes. However, outstanding client calls will still complete with the existing client. The new client cannot be "lazy" and - must be using the same runtime as the current client. + must be using the same runtime and Stream Stores as the current client. """ bridge_client = _extract_bridge_client_for_worker(value) if self._runtime is not bridge_client._runtime: raise ValueError( "New client is not on the same runtime as the existing client" ) + # Running Workflows route their Stream calls by the Store that the worker + # started with, so a new client can't change it. + if _stream_config_stores(value) != _stream_config_stores(self.client): + raise ValueError( + "New client doesn't have the Stream Stores of the existing client" + ) assert bridge_client._bridge_client self._bridge_worker.replace_client(bridge_client._bridge_client) self._config["client"] = value @@ -1013,6 +1044,13 @@ async def __aexit__(self, exc_type: type[BaseException] | None, *args: Any) -> N raise self._async_context_run_exception +def _stream_config_stores( + client: temporalio.client.Client, +) -> list[temporalio.client.StreamStore]: + config = client.config(active_config=True).get("stream_config") + return list(config.stores) if config is not None else [] + + class WorkerConfig(TypedDict, total=False): """TypedDict of config originally passed to :py:class:`Worker`.""" diff --git a/temporalio/worker/_workflow.py b/temporalio/worker/_workflow.py index d8380a6b90..74e40dfd18 100644 --- a/temporalio/worker/_workflow.py +++ b/temporalio/worker/_workflow.py @@ -40,6 +40,7 @@ WorkflowInboundInterceptor, WorkflowInterceptorClassInput, ) +from ._stream_store import _WorkflowStreamStore from ._workflow_instance import ( _DEFAULT_ENABLED_WORKFLOW_LOGIC_FLAGS, PatchActivationInput, @@ -128,6 +129,7 @@ def __init__( encode_headers: bool, max_workflow_task_external_storage_concurrency: int, default_workflow_logic_flags: frozenset[_WorkflowLogicFlag] | None = None, + stream_store: _WorkflowStreamStore | None = None, ) -> None: # Debug mode is enabled if specified or if the TEMPORAL_DEBUG env var is truthy debug_mode = debug_mode or bool(os.environ.get("TEMPORAL_DEBUG")) @@ -197,6 +199,7 @@ def __init__( self._max_workflow_task_external_storage_concurrency = ( max_workflow_task_external_storage_concurrency ) + self._stream_store = stream_store self._throw_after_activation: Exception | None = None # If debug mode is enabled, disable deadlock detection @@ -819,6 +822,7 @@ def _create_workflow_instance( nexus_serialization_context=temporalio.converter._serialization_context._propagated_nexus_serialization_context( init ), + stream_store=self._stream_store, ) if defn.sandboxed: return self._workflow_runner.create_instance(det) @@ -831,6 +835,12 @@ def nondeterminism_as_workflow_fail(self) -> bool: for typ in self._workflow_failure_exception_types ) + def stream_replay_as_workflow_fail(self) -> bool: + return any( + issubclass(temporalio.exceptions.StreamReplayError, typ) + for typ in self._workflow_failure_exception_types + ) + def _set_default_workflow_logic_flag( self, flag: _WorkflowLogicFlag, *, enabled: bool ) -> None: @@ -849,6 +859,16 @@ def nondeterminism_as_workflow_fail_for_types(self) -> set[str]: ) } + def stream_replay_as_workflow_fail_for_types(self) -> set[str]: + return { + k + for k, v in self._workflows.items() + if any( + issubclass(temporalio.exceptions.StreamReplayError, typ) + for typ in v.failure_exception_types + ) + } + class _DeadlockError(Exception): """Exception class for deadlocks. Contains functionality to swap the default traceback for another.""" diff --git a/temporalio/worker/_workflow_instance.py b/temporalio/worker/_workflow_instance.py index b4629a293a..dd159982ad 100644 --- a/temporalio/worker/_workflow_instance.py +++ b/temporalio/worker/_workflow_instance.py @@ -59,10 +59,7 @@ import temporalio.exceptions import temporalio.nexus.system import temporalio.workflow -from temporalio.client._stream_store import ( - _STREAM_SERVICE, - _workflow_stream_call_error, -) +from temporalio.client._stream_store import _STREAM_SERVICE from temporalio.converter import StorageDriverStoreContext, StorageDriverWorkflowInfo from temporalio.nexus.system.stream_service.workflow._system_nexus_interceptor import ( _start_system_nexus_operation as _start_stream_system_nexus_operation, @@ -98,6 +95,14 @@ WorkflowInboundInterceptor, WorkflowOutboundInterceptor, ) +from ._stream_store import ( + _DirectRead, + _no_stream_config_error, + _ReadKey, + _start_replayed_direct_call, + _stream_error_from_core, + _WorkflowStreamStore, +) logger = logging.getLogger(__name__) @@ -195,6 +200,10 @@ class WorkflowInstanceDetails: temporalio.converter.NexusSerializationContext | None ) = None """Serialization context of the Nexus operation handler that started the workflow, if any.""" + stream_store: _WorkflowStreamStore | None = None + """The Worker's Stream Stores as Workflow code reaches them, or ``None`` when the + Worker's client has none. This release has no Streams on the server, so with ``None`` + a Stream call fails the Workflow.""" class WorkflowInstance(ABC): @@ -264,6 +273,17 @@ def get_thread_id(self) -> int | None: return None +def _is_completion( + command: temporalio.bridge.proto.workflow_commands.workflow_commands_pb2.WorkflowCommand, +) -> bool: + return ( + command.HasField("complete_workflow_execution") + or command.HasField("continue_as_new_workflow_execution") + or command.HasField("fail_workflow_execution") + or command.HasField("cancel_workflow_execution") + ) + + class UnsandboxedWorkflowRunner(WorkflowRunner): """Workflow runner that does not do any sandboxing.""" @@ -311,6 +331,7 @@ def __init__(self, det: WorkflowInstanceDetails) -> None: # the Nexus caller, which decodes them with the handler's Nexus context. A retried or cron run # reads the previous run's result and failure with the same context. self._nexus_serialization_context = det.nexus_serialization_context + self._stream_store = det.stream_store if self._nexus_serialization_context: self._result_payload_converter = self._payload_converter_with_context( self._nexus_serialization_context @@ -358,6 +379,19 @@ def __init__(self, det: WorkflowInstanceDetails) -> None: self._pending_nexus_operations: dict[int, _NexusOperationHandle] = {} self._pending_external_signals: dict[int, tuple[asyncio.Future, str]] = {} self._pending_external_cancels: dict[int, tuple[asyncio.Future, str]] = {} + # Core reads that run, by seq. + self._pending_stream_reads: dict[int, _DirectRead] = {} + # Reads that no call waits on, by what the next call must match. Each holds the + # Core read and the Records that it gave. + self._parked_stream_reads: dict[_ReadKey, list[_DirectRead]] = {} + self._pending_stream_appends: dict[ + int, + asyncio.Future[temporalio.bridge.proto.workflow_activation.StreamAppended], + ] = {} + self._pending_stream_closes: dict[int, asyncio.Future[None]] = {} + # Reads that the SDK closed. A job of one of them can still come in the activation + # that the close answers, and is dropped. + self._closed_stream_reads: set[int] = set() # Keyed by type self._curr_seqs: dict[str, int] = {} # TODO(cretz): Any concerns about not sharing this? Maybe the types I @@ -571,6 +605,17 @@ def activate( self._run_once( check_conditions=index == 1 or index == 2, jobs=act.jobs ) + + # A query-only activation sends no command, and the end of the Workflow + # stops every read in Core. + if ( + not job_sets[3] + and not self._deleting + and not any( + map(_is_completion, self._current_completion.successful.commands) + ) + ): + self._close_parked_stream_reads() except Exception as err: # We want some errors during activation, like those that can happen # during payload conversion, to be able to fail the workflow not the @@ -635,17 +680,7 @@ def activate( ) self._current_completion.failed.failure.application_failure_info.SetInParent() - def is_completion( - command: temporalio.bridge.proto.workflow_commands.workflow_commands_pb2.WorkflowCommand, - ): - return ( - command.HasField("complete_workflow_execution") - or command.HasField("continue_as_new_workflow_execution") - or command.HasField("fail_workflow_execution") - or command.HasField("cancel_workflow_execution") - ) - - if any(map(is_completion, self._current_completion.successful.commands)): + if any(map(_is_completion, self._current_completion.successful.commands)): self._warn_if_unfinished_handlers() return self._current_completion @@ -687,6 +722,12 @@ def _apply( self._apply_resolve_signal_external_workflow( job.resolve_signal_external_workflow ) + elif job.HasField("resolve_stream_read"): + self._apply_resolve_stream_read(job.resolve_stream_read) + elif job.HasField("resolve_stream_append"): + self._apply_resolve_stream_append(job.resolve_stream_append) + elif job.HasField("resolve_stream_close"): + self._apply_resolve_stream_close(job.resolve_stream_close) elif job.HasField("signal_workflow"): self._apply_signal_workflow(job.signal_workflow) elif job.HasField("initialize_workflow"): @@ -938,6 +979,64 @@ def _apply_remove_from_cache( for task in self._tasks: task.cancel() + def _apply_resolve_stream_read( + self, job: temporalio.bridge.proto.workflow_activation.ResolveStreamRead + ) -> None: + read = self._pending_stream_reads.get(job.seq) + if read is None: + if job.seq in self._closed_stream_reads: + return + raise RuntimeError(f"Failed finding Stream read for sequence {job.seq}") + error: BaseException | None = None + end = job.HasField("end") + if end: + del self._pending_stream_reads[job.seq] + if job.end.HasField("failure"): + error = _stream_error_from_core(job.end.failure) + if error is None: + error = self._context_free_failure_converter.from_failure( + job.end.failure, self._context_free_payload_converter + ) + read.take(job.records, end=end, done=job.end.done, error=error) + + def _apply_resolve_stream_append( + self, job: temporalio.bridge.proto.workflow_activation.ResolveStreamAppend + ) -> None: + fut = self._pending_stream_appends.pop(job.seq, None) + if fut is None: + raise RuntimeError(f"Failed finding Stream append for sequence {job.seq}") + # Code that stopped waiting for the append has cancelled the future. + if fut.done(): + return + if job.HasField("appended"): + fut.set_result(job.appended) + return + error: BaseException | None = _stream_error_from_core(job.failed) + if error is None: + error = self._context_free_failure_converter.from_failure( + job.failed, self._context_free_payload_converter + ) + fut.set_exception(error) + + def _apply_resolve_stream_close( + self, job: temporalio.bridge.proto.workflow_activation.ResolveStreamClose + ) -> None: + fut = self._pending_stream_closes.pop(job.seq, None) + if fut is None: + raise RuntimeError(f"Failed finding Stream close for sequence {job.seq}") + # Code that stopped waiting for the close has cancelled the future. + if fut.done(): + return + if not job.HasField("failed"): + fut.set_result(None) + return + error: BaseException | None = _stream_error_from_core(job.failed) + if error is None: + error = self._context_free_failure_converter.from_failure( + job.failed, self._context_free_payload_converter + ) + fut.set_exception(error) + def _apply_resolve_activity( self, job: temporalio.bridge.proto.workflow_activation.ResolveActivity ) -> None: @@ -1770,6 +1869,66 @@ async def workflow_start_child_workflow( ) ) + def _start_stream_read( + self, start: temporalio.bridge.proto.workflow_commands.StartStreamRead + ) -> _DirectRead: + start.seq = self._next_seq("stream_read") + self._add_command().start_stream_read.CopyFrom(start) + read = _DirectRead(start.seq) + self._pending_stream_reads[start.seq] = read + return read + + def _take_stream_read(self, key: _ReadKey) -> _DirectRead | None: + reads = self._parked_stream_reads.get(key) + if not reads: + return None + read = reads.pop(0) + if not reads: + del self._parked_stream_reads[key] + return read + + def _park_stream_read(self, read: _DirectRead, key: _ReadKey) -> None: + if read.kept(): + self._parked_stream_reads.setdefault(key, []).append(read) + + def _close_parked_stream_reads(self) -> None: + # A Core read holds its Workflow Task open, so one that no call waits on closes + # before the activation completes. Its Records stay for a later call. + for key, reads in list(self._parked_stream_reads.items()): + for read in reads: + if read.running: + self._add_command().close_stream_read.seq = read.seq + self._closed_stream_reads.add(read.seq) + del self._pending_stream_reads[read.seq] + read.close() + kept = [read for read in reads if read.kept()] + if kept: + self._parked_stream_reads[key] = kept + else: + del self._parked_stream_reads[key] + + def _start_stream_append( + self, append: temporalio.bridge.proto.workflow_commands.AppendToStream + ) -> asyncio.Future[temporalio.bridge.proto.workflow_activation.StreamAppended]: + command = self._add_command() + append.seq = self._next_seq("stream_append") + command.append_to_stream.CopyFrom(append) + fut: asyncio.Future[ + temporalio.bridge.proto.workflow_activation.StreamAppended + ] = self.create_future() + self._pending_stream_appends[append.seq] = fut + return fut + + def _start_stream_close( + self, close: temporalio.bridge.proto.workflow_commands.CloseStream + ) -> asyncio.Future[None]: + command = self._add_command() + close.seq = self._next_seq("stream_close") + command.close_stream.CopyFrom(close) + fut: asyncio.Future[None] = self.create_future() + self._pending_stream_closes[close.seq] = fut + return fut + def workflow_start_local_activity( self, activity: Any, @@ -2312,25 +2471,7 @@ async def operation_handle_fn() -> OutputT: ) if temporalio.nexus.system.is_system_endpoint(input.endpoint): - serialization_context = temporalio.nexus.system._get_serialization_context( - input.service, - input.operation_name, - input.input, - ) - user_payload_converter = self._context_free_payload_converter - user_failure_converter = self._context_free_failure_converter - if serialization_context is not None: - user_payload_converter = self._payload_converter_with_context( - serialization_context - ) - user_failure_converter = self._failure_converter_with_context( - serialization_context - ) - payload_converter = temporalio.nexus.system._get_payload_converter( - user_payload_converter, - user_failure_converter, - ) - failure_converter = user_failure_converter + payload_converter, failure_converter = self._system_nexus_converters(input) else: serialization_context = temporalio.converter.NexusSerializationContext( endpoint=input.endpoint, @@ -2369,12 +2510,48 @@ async def _intercept_system_nexus_operation( async def _schedule_system_nexus_operation( self, input: StartNexusOperationInput[Any, OutputT] ) -> temporalio.workflow.NexusOperationHandle[OutputT]: - # The server has no Streams, and Workflow code reaches no Store. So a Stream call - # stops here, after the interceptors saw it. + # This release has no Streams on the server, so a Worker answers a Stream call with + # a Store of its client. Every other system Nexus operation goes to the server. if input.service == _STREAM_SERVICE.name: - raise _workflow_stream_call_error() + store = self._stream_store + if store is None: + replayed = ( + _start_replayed_direct_call(self, input) + if self._is_replaying + else None + ) + if replayed is None: + raise _no_stream_config_error() + return replayed + return store.start_operation(self, input) return await self._outbound_start_nexus_operation(input) + def _system_nexus_converters( + self, input: StartNexusOperationInput[Any, Any] + ) -> tuple[ + temporalio.nexus.system._SystemNexusPayloadConverter, + temporalio.converter.FailureConverter, + ]: + serialization_context = temporalio.nexus.system._get_serialization_context( + input.service, + input.operation_name, + input.input, + ) + user_payload_converter = self._context_free_payload_converter + user_failure_converter = self._context_free_failure_converter + if serialization_context is not None: + user_payload_converter = self._payload_converter_with_context( + serialization_context + ) + user_failure_converter = self._failure_converter_with_context( + serialization_context + ) + payload_converter = temporalio.nexus.system._get_payload_converter( + user_payload_converter, + user_failure_converter, + ) + return payload_converter, user_failure_converter + #### Miscellaneous helpers #### # These are in alphabetical order. @@ -2541,6 +2718,11 @@ def get_serialization_context( workflow_id=self._info.workflow_id, ) + if command_info.stream: + # Stream bodies take the codec with no context on every path, so a client, an + # Activity and a Workflow read what each other wrote. + return None + if self._nexus_serialization_context and command_info.command_type in ( temporalio.api.enums.v1.command_type_pb2.CommandType.COMMAND_TYPE_COMPLETE_WORKFLOW_EXECUTION, temporalio.api.enums.v1.command_type_pb2.CommandType.COMMAND_TYPE_FAIL_WORKFLOW_EXECUTION, diff --git a/tests/contrib/streams/test_appender_workflow.py b/tests/contrib/streams/test_appender_workflow.py new file mode 100644 index 0000000000..40823d15d8 --- /dev/null +++ b/tests/contrib/streams/test_appender_workflow.py @@ -0,0 +1,230 @@ +from __future__ import annotations + +import asyncio +import uuid +from datetime import timedelta +from typing import Any + +import pytest + +from temporalio import workflow +from temporalio.api.enums.v1 import EventType, StreamErrorReason +from temporalio.api.history.v1 import HistoryEvent +from temporalio.bridge.proto.external_data import StreamAppendEntry, StreamMarkerData +from temporalio.client import Client, WorkflowHandle +from temporalio.contrib.streams import BufferedAppender +from temporalio.exceptions import StreamError +from temporalio.nexus.system.stream_service import StreamRef +from temporalio.nexus.system.stream_service.workflow import create_stream, get_stream +from temporalio.testing import MemoryStreamStore +from temporalio.worker import Replayer +from tests.helpers import new_worker +from tests.nexus.test_stream_store import _with_store + +_BURST = [f"record-{index}" for index in range(40)] + + +async def _yield_a_few_times() -> None: + # A turn of the loop with no timer, so the write starts and waits for Core. + for _ in range(5): + await asyncio.sleep(0) + + +@workflow.defn +class BurstWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = await create_stream(id=stream_id, body_type=str) + appender = BufferedAppender(stream) + for value in _BURST: + appender.append(value) + last_offset = await appender.flush() + assert last_offset is not None + return last_offset.hex() + + +@workflow.defn +class InFlightWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + stream = await create_stream(id=stream_id, body_type=str) + async with BufferedAppender(stream) as appender: + appender.append("a") + await _yield_a_few_times() + # The write of "a" waits for Core, so these go into the next one. + appender.append("b") + appender.append("c", "d") + + +@workflow.defn +class FailedAppendWorkflow: + @workflow.run + async def run(self, stream_id: str) -> list[Any]: + appender = BufferedAppender(get_stream(StreamRef(id=stream_id), body_type=str)) + appender.append("a") + await _yield_a_few_times() + appender.append("b", "c") + try: + await appender.flush() + except StreamError as err: + first = err + else: + return ["flushed"] + try: + appender.append("d") + except StreamError as err: + later = err + else: + return ["appended"] + return [ + StreamErrorReason.Name(first.reason), + later is first, + appender.unwritten, + ] + + +@workflow.defn +class MaxDelayWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = get_stream(StreamRef(id=stream_id), body_type=str) + try: + BufferedAppender(stream, max_delay=timedelta(milliseconds=10)) + except ValueError as err: + return str(err) + return "built" + + +async def _run( + client: Client, workflow_type: type[Any], stream_id: str +) -> tuple[WorkflowHandle[Any, Any], Any]: + async with new_worker(client, workflow_type) as worker: + handle: WorkflowHandle[Any, Any] = await client.start_workflow( + workflow_type.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + execution_timeout=timedelta(seconds=30), + ) + return handle, await handle.result() + + +def _of_type(events: list[HistoryEvent], event_type: EventType.ValueType) -> int: + return sum(1 for event in events if event.event_type == event_type) + + +def _append_entries(events: list[HistoryEvent]) -> list[StreamAppendEntry]: + # Each append that Core makes is one entry in the Stream marker of its Workflow Task. + entries: list[StreamAppendEntry] = [] + for event in events: + attributes = event.marker_recorded_event_attributes + if ( + event.event_type != EventType.EVENT_TYPE_MARKER_RECORDED + or attributes.marker_name != "core_stream" + ): + continue + [payload] = attributes.details["data"].payloads + entries.extend(StreamMarkerData.FromString(payload.data).appends) + return entries + + +async def _batches( + client: Client, stream_id: str, events: list[HistoryEvent] +) -> list[int]: + """Give the number of Records of each append, from its offsets in the marker.""" + stream = client.get_stream(StreamRef(id=stream_id), body_type=str) + offsets = [record.offset async for record in stream.iter_read_items()] + return [ + sum( + 1 for offset in offsets if entry.first_offset <= offset <= entry.last_offset + ) + for entry in _append_entries(events) + ] + + +def _marker_names(events: list[HistoryEvent]) -> list[str]: + return [ + event.marker_recorded_event_attributes.marker_name + for event in events + if event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED + ] + + +async def _bodies(client: Client, stream_id: str) -> list[str]: + stream = client.get_stream(StreamRef(id=stream_id), body_type=str) + return [record.body async for record in stream.iter_read_items()] + + +async def test_appends_of_one_activation_are_one_append(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + handle, last_offset = await _run(stream_client, BurstWorkflow, stream_id) + + events = list((await handle.fetch_history()).events) + assert await _batches(stream_client, stream_id, events) == [40] + assert await _bodies(stream_client, stream_id) == _BURST + described = await stream_client.get_stream( + StreamRef(id=stream_id), body_type=str + ).describe() + assert described.last_offset is not None + assert last_offset == described.last_offset.hex() + # The Local Activity of the create, and the Stream marker of the append. + assert _marker_names(events) == ["core_local_activity", "core_stream"] + assert _of_type(events, EventType.EVENT_TYPE_TIMER_STARTED) == 0 + + +async def test_appends_during_a_workflow_append_join_the_next_one(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + handle, _ = await _run(stream_client, InFlightWorkflow, stream_id) + + events = list((await handle.fetch_history()).events) + assert await _batches(stream_client, stream_id, events) == [1, 3] + assert await _bodies(stream_client, stream_id) == ["a", "b", "c", "d"] + assert _of_type(events, EventType.EVENT_TYPE_TIMER_STARTED) == 0 + + +async def test_a_failed_workflow_append_stops_the_appender(client: Client): + handle, result = await _run( + _with_store(client, MemoryStreamStore()), + FailedAppendWorkflow, + f"stream-{uuid.uuid4()}", + ) + assert result == ["STREAM_ERROR_REASON_NOT_FOUND", True, 3] + # Only the first batch went to the Store, and it failed. + [entry] = _append_entries(list((await handle.fetch_history()).events)) + assert entry.HasField("failure") + + +async def test_replay_of_workflow_appender_writes_nothing(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + handle, _ = await _run(stream_client, InFlightWorkflow, stream_id) + + # A replay of an append that Core made needs a Store that runs on the bridge, as the + # first run had. A new memory Store shows any write, since it has no Stream. + replay_store = MemoryStreamStore() + await Replayer( + workflows=[InFlightWorkflow], + stream_config=_with_store(client, replay_store).config()["stream_config"], + ).replay_workflow(await handle.fetch_history()) + with pytest.raises(StreamError) as err: + await ( + _with_store(client, replay_store) + .get_stream(StreamRef(id=stream_id), body_type=str) + .describe() + ) + assert err.value.reason == StreamErrorReason.STREAM_ERROR_REASON_NOT_FOUND + assert await _bodies(stream_client, stream_id) == ["a", "b", "c", "d"] + + +async def test_workflow_appender_refuses_a_max_delay(client: Client): + _, message = await _run( + _with_store(client, MemoryStreamStore()), + MaxDelayWorkflow, + f"stream-{uuid.uuid4()}", + ) + assert message == ( + "BufferedAppender takes no max_delay in Workflow code, since a delay would put " + "a timer into History. Leave max_delay as None." + ) diff --git a/tests/contrib/streams/test_producer.py b/tests/contrib/streams/test_producer.py index 86b6d922c9..e7b1f19a7f 100644 --- a/tests/contrib/streams/test_producer.py +++ b/tests/contrib/streams/test_producer.py @@ -22,12 +22,13 @@ ProducerKindWorkflowActivity, StreamRecord, StreamRef, + StreamStatus, WorkflowProducer, ) from temporalio.nexus.system.stream_service.client import Stream -from temporalio.testing import ActivityEnvironment +from temporalio.nexus.system.stream_service.workflow import create_stream +from temporalio.testing import ActivityEnvironment, MemoryStreamStore from tests.helpers import new_worker -from tests.helpers.streams import DictStreamStore from tests.nexus.test_stream_store import _create_stream, _with_store _SCHEDULED = datetime(2026, 11, 12, 10, 0, 0, 123456, tzinfo=timezone.utc) @@ -93,7 +94,7 @@ async def _records(stream: Stream[str]) -> list[StreamRecord[str]]: async def test_current_producer_in_other_code_is_the_client_producer(client: Client): - stream = await _create_stream(client, DictStreamStore()) + stream = await _create_stream(client, MemoryStreamStore()) await stream.append("a") [record] = await _records(stream) producer = current_producer() @@ -105,7 +106,7 @@ async def test_current_producer_in_other_code_is_the_client_producer(client: Cli async def test_current_producer_in_an_activity_is_its_attempt_producer( client: Client, kind: str ): - stream = await _create_stream(client, DictStreamStore()) + stream = await _create_stream(client, MemoryStreamStore()) async def append_and_give_producer() -> Producer: await stream.append("a") @@ -122,25 +123,43 @@ async def append_and_give_producer() -> Producer: @workflow.defn class CurrentProducerWorkflow: @workflow.run - async def run(self) -> str: - try: - current_producer() - except RuntimeError as err: - return str(err) - return "no error" + async def run(self, stream_id: str) -> Producer: + stream = await create_stream(id=stream_id, body_type=str) + producer = current_producer() + await stream.append("a", producer=producer) + await stream.close(producer=producer) + return producer -async def test_current_producer_in_workflow_code_raises(client: Client): - async with new_worker(client, CurrentProducerWorkflow) as worker: - message = await client.execute_workflow( +async def test_current_producer_in_workflow_code_is_the_workflow_producer( + client: Client, +): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, CurrentProducerWorkflow) as worker: + handle = await stream_client.start_workflow( CurrentProducerWorkflow.run, + stream_id, id=f"workflow-{uuid.uuid4()}", task_queue=worker.task_queue, ) - assert message == ( - "Workflow code can't call a Stream in this version. Call it from an Activity " - "through activity.client()." + producer = await handle.result() + run_id = handle.result_run_id + assert run_id + assert producer == Producer( + id=f"wf:{run_id}", kind=ProducerKindWorkflow(WorkflowProducer(run_id=run_id)) ) + # On a Store, the append writes as the Workflow, with its Workflow Task as the epoch. + # The close passes the Store's check of that epoch too. + stream = stream_client.get_stream(StreamRef(id=stream_id), body_type=str) + [record] = await _records(stream) + assert record.producer is not None + assert record.producer.id == producer.id + assert isinstance(record.producer.kind, ProducerKindWorkflow) + assert record.producer.kind.value.epoch is not None + assert record.producer.kind.value.epoch.attempt == 1 + described = await stream.describe() + assert described.status == StreamStatus.StreamStatusClosed @pytest.mark.parametrize("kind", ["workflow", *_ACTIVITY_INFOS, "client"]) @@ -195,7 +214,7 @@ async def run(self, stream_id: str, keep_producer: bool) -> None: async def test_activity_retry_appends_as_its_stored_producer( client: Client, keep_producer: bool ): - stream_client = _with_store(client, DictStreamStore()) + stream_client = _with_store(client, MemoryStreamStore()) stream = await stream_client.create_stream( id=f"stream-{uuid.uuid4()}", body_type=str ) diff --git a/tests/nexus/test_stream_direct.py b/tests/nexus/test_stream_direct.py new file mode 100644 index 0000000000..e2bc74c5a5 --- /dev/null +++ b/tests/nexus/test_stream_direct.py @@ -0,0 +1,2849 @@ +"""Reads and appends of a Stream that Core makes inside the Workflow Task.""" + +from __future__ import annotations + +import asyncio +import os +import urllib.parse +import uuid +from collections.abc import AsyncGenerator, Sequence +from dataclasses import dataclass +from datetime import timedelta +from typing import Any, Self + +import pytest + +from temporalio import activity, workflow +from temporalio.api.common.v1 import Payload +from temporalio.api.enums.v1 import EventType, StreamErrorReason +from temporalio.bridge.proto.external_data import ( + StreamCloseEntry, + StreamMarkerData, + StreamReadEntry, +) +from temporalio.client import ( + Client, + StreamConfig, + StreamReplaySource, + WorkflowExecutionStatus, + WorkflowFailureError, + WorkflowHandle, + WorkflowHistory, +) +from temporalio.common import RawValue +from temporalio.converter import ( + DataConverter, + PayloadCodec, + SerializationContext, + WithSerializationContext, + WorkflowSerializationContext, +) +from temporalio.exceptions import ApplicationError, StreamError, StreamReplayError +from temporalio.nexus.system.stream_service import ( + Producer, + ProducerKindWorkflow, + StreamRecord, + StreamRef, + StreamStatus, +) +from temporalio.runtime import MetricBuffer, Runtime, TelemetryConfig +from temporalio.testing import MemoryStreamStore, WorkflowEnvironment +from temporalio.worker import ( + Interceptor, + Replayer, + StartNexusOperationInput, + Worker, + WorkflowInboundInterceptor, + WorkflowInterceptorClassInput, + WorkflowOutboundInterceptor, +) +from tests.helpers import assert_eventually, assert_task_fail_eventually, new_worker +from tests.helpers.streams import DictStreamStore +from tests.test_converter import SimpleCodec + +# A Record body that a test can look for in the bytes of History. +_MARK = "direct-read-mark" + + +def _stream_client( + client: Client, + store: Any = None, + *, + replay_from: StreamReplaySource = StreamReplaySource.STORE, + codec: bool = False, +) -> Client: + config = client.config() + config["stream_config"] = StreamConfig( + stores=[store if store is not None else MemoryStreamStore()], + workflow_read_replay_from=replay_from, + ) + if codec: + config["data_converter"] = DataConverter(payload_codec=SimpleCodec()) + return Client(**config) + + +def _markers(history: WorkflowHistory) -> list[str]: + return [ + event.marker_recorded_event_attributes.marker_name + for event in history.events + if event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED + ] + + +def _stream_markers_per_task(history: WorkflowHistory) -> list[int]: + """Give the number of `core_stream` markers that each completed Workflow Task wrote.""" + counts: list[int] = [] + for event in history.events: + if event.event_type == EventType.EVENT_TYPE_WORKFLOW_TASK_COMPLETED: + counts.append(0) + elif ( + event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED + and event.marker_recorded_event_attributes.marker_name == "core_stream" + ): + counts[-1] += 1 + return counts + + +def _completed_tasks(history: WorkflowHistory) -> int: + return sum( + 1 + for event in history.events + if event.event_type == EventType.EVENT_TYPE_WORKFLOW_TASK_COMPLETED + ) + + +async def _filled_stream( + client: Client, bodies: list[str], *, close: bool = True +) -> StreamRef[str]: + stream = await client.create_stream(id=f"stream-{uuid.uuid4()}", body_type=str) + if bodies: + await stream.append(*bodies) + if close: + await stream.close() + return stream.ref + + +async def _read_all(client: Client, ref: StreamRef[str]) -> list[StreamRecord[str]]: + stream = client.get_stream(ref, body_type=str) + return [record async for record in stream.iter_read_items()] + + +async def _run( + client: Client, + workflow_type: type[Any], + *args: Any, + activities: list[Any] | None = None, + max_cached_workflows: int = 1000, + task_timeout: timedelta | None = None, +) -> tuple[WorkflowHandle[Any, Any], Any]: + async with new_worker( + client, + workflow_type, + activities=activities or [], + max_cached_workflows=max_cached_workflows, + ) as worker: + handle: WorkflowHandle[Any, Any] = await client.start_workflow( + workflow_type.run, + args=list(args), + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + task_timeout=task_timeout, + ) + return handle, await handle.result() + + +# A generator over `iter_read_items` that reads the Stream of an Activity that still runs. + + +@dataclass +class ModelCall: + chunks_id: str + seen_id: str + + +@activity.defn +async def stream_model_output(call: ModelCall) -> None: + client = activity.client() + chunks = client.get_stream(StreamRef(id=call.chunks_id), body_type=str) + seen = client.get_stream(StreamRef(id=call.seen_id), body_type=str) + await chunks.append("Hel", "lo") + # The Activity writes the rest only after the Workflow says that it has the first + # chunks. So the test passes only if Workflow code gets chunks while the Activity runs. + told = False + async for _ in seen.iter_read_items(wait=timedelta(seconds=30)): + told = True + break + if not told: + raise ApplicationError( + "The Workflow got no chunk while the Activity ran", non_retryable=True + ) + await chunks.append(", ", "world") + await chunks.close() + + +async def model_chunks(chunks_id: str) -> AsyncGenerator[str, None]: + """Yield each chunk of the model's Stream, as the model stub of a library does.""" + stream = workflow.get_stream(StreamRef(id=chunks_id), body_type=str) + # The wait is far longer than a pause of the model, so no page comes back empty. + async for record in stream.iter_read_items(wait=timedelta(seconds=30)): + yield record.body + + +@workflow.defn +class LiveModelWorkflow: + @workflow.run + async def run(self, call: ModelCall) -> str: + model = workflow.start_activity( + stream_model_output, call, start_to_close_timeout=timedelta(seconds=60) + ) + seen = workflow.get_stream(StreamRef(id=call.seen_id), body_type=str) + text = "" + async for chunk in model_chunks(call.chunks_id): + text += chunk + if text == "Hello": + await seen.append("the first chunks") + await model + return text + + +async def test_generator_over_iter_read_items_reads_a_running_activity( + client: Client, +): + stream_client = _stream_client(client) + chunks = await _filled_stream(stream_client, [], close=False) + seen = await _filled_stream(stream_client, [], close=False) + handle, text = await _run( + stream_client, + LiveModelWorkflow, + ModelCall(chunks.id, seen.id), + activities=[stream_model_output], + ) + assert text == "Hello, world" + history = await handle.fetch_history() + # The reads and the append write no Local Activity marker, and at most one marker for + # each Workflow Task. + assert "core_local_activity" not in _markers(history) + assert "core_stream" in _markers(history) + assert max(_stream_markers_per_task(history)) == 1 + + +# The pages of a read. + + +@dataclass +class ReadArgs: + stream_id: str + from_offset: str | None = None + wait_ms: int | None = None + max_records: int | None = None + max_bytes: int | None = None + store: str = "memory" + pages: int | None = None + """The most pages to read. ``None`` reads until the iterator ends.""" + + +@dataclass +class ReadResult: + bodies: list[str] + offsets: list[str] + page_sizes: list[int] + last_offsets: list[str | None] + dones: list[bool] + + +def _ms(value: int | None) -> timedelta | None: + return None if value is None else timedelta(milliseconds=value) + + +def _hex(value: bytes | None) -> str | None: + return None if value is None else value.hex() + + +@workflow.defn +class ReadPagesWorkflow: + @workflow.run + async def run(self, args: ReadArgs) -> ReadResult: + # The ref has its Store, so a replay with no Stream config knows it. + ref = StreamRef[str](id=args.stream_id, store=args.store) + stream = workflow.get_stream(ref, body_type=str) + result = ReadResult([], [], [], [], []) + async for page in stream.iter_read( + from_offset=( + bytes.fromhex(args.from_offset) + if args.from_offset is not None + else None + ), + max_records=args.max_records, + wait=_ms(args.wait_ms), + max_bytes=args.max_bytes, + ): + result.bodies.extend(record.body for record in page.records) + result.offsets.extend(record.offset.hex() for record in page.records) + result.page_sizes.append(len(page.records)) + result.last_offsets.append(_hex(page.last_offset)) + result.dones.append(page.done) + if len(result.page_sizes) == args.pages: + break + return result + + +async def _read_pages( + client: Client, args: ReadArgs, *, max_cached_workflows: int = 1000 +) -> ReadResult: + _, result = await _run( + client, ReadPagesWorkflow, args, max_cached_workflows=max_cached_workflows + ) + return result + + +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_closed_stream_ends_the_iterator_with_a_done_page( + client: Client, max_cached_workflows: int +): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a", "b", "c"]) + result = await _read_pages( + stream_client, + ReadArgs(ref.id, wait_ms=30_000), + max_cached_workflows=max_cached_workflows, + ) + assert result.bodies == ["a", "b", "c"] + # The iterator ends after the page with `done`, also with a `wait`. + assert result.dones[-1] is True + assert not any(result.dones[:-1]) + assert result.last_offsets[-1] == result.offsets[-1] + + +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_read_with_max_records_gives_pages_of_two_two_and_one( + client: Client, max_cached_workflows: int +): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a", "b", "c", "d", "e"]) + result = await _read_pages( + stream_client, + ReadArgs(ref.id, max_records=2), + max_cached_workflows=max_cached_workflows, + ) + assert result.bodies == ["a", "b", "c", "d", "e"] + assert result.page_sizes == [2, 2, 1] + assert result.dones == [False, False, True] + # Each page ends at its last Record, and the next one starts after it. + assert result.last_offsets == [ + result.offsets[1], + result.offsets[3], + result.offsets[4], + ] + + +@workflow.defn +class ReadOnePageWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str], wait_ms: int | None) -> list[Any]: + stream = workflow.get_stream(ref, body_type=str) + page = await stream.read(wait=_ms(wait_ms)) + return [[record.body for record in page.records], page.done, page.last_offset] + + +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_read_with_wait_on_a_quiet_stream_gives_an_empty_page( + client: Client, max_cached_workflows: int +): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, [], close=False) + handle, result = await _run( + stream_client, + ReadOnePageWorkflow, + ref, + 1000, + max_cached_workflows=max_cached_workflows, + ) + assert result == [[], False, None] + # The page comes only after the wait. + events = (await handle.fetch_history()).events + elapsed = events[-1].event_time.ToDatetime() - events[0].event_time.ToDatetime() + assert elapsed >= timedelta(seconds=1) + + +async def test_read_with_no_wait_on_an_empty_stream_gives_an_empty_page( + client: Client, +): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, [], close=False) + handle, result = await _run(stream_client, ReadOnePageWorkflow, ref, None) + assert result == [[], False, None] + events = (await handle.fetch_history()).events + # An empty Stream gives no reason to wait. + took = events[-1].event_time.ToDatetime() - events[0].event_time.ToDatetime() + assert took < timedelta(seconds=5) + + +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_iter_read_items_with_no_wait_gives_what_is_there_and_ends( + client: Client, max_cached_workflows: int +): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a", "b", "c"], close=False) + handle, bodies = await _run( + stream_client, + ReadItemsWorkflow, + ref, + None, + max_cached_workflows=max_cached_workflows, + ) + assert bodies == ["a", "b", "c"] + events = (await handle.fetch_history()).events + # The read doesn't wait for the Store, so the run ends long before the first heartbeat. + took = events[-1].event_time.ToDatetime() - events[0].event_time.ToDatetime() + assert took < timedelta(seconds=5) + + +@workflow.defn +class ReadItemsWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str], wait_ms: int | None) -> list[str]: + stream = workflow.get_stream(ref, body_type=str) + return [ + record.body async for record in stream.iter_read_items(wait=_ms(wait_ms)) + ] + + +async def test_page_that_takes_the_last_record_with_a_cap_is_done(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a", "b", "c"]) + # The cap takes the last Record of the closed Stream, so the page is done. + by_records = await _read_pages(stream_client, ReadArgs(ref.id, max_records=3)) + assert (by_records.page_sizes, by_records.dones) == ([3], [True]) + one = await _filled_stream(stream_client, ["a"]) + by_bytes = await _read_pages(stream_client, ReadArgs(one.id, max_bytes=1)) + assert (by_bytes.page_sizes, by_bytes.dones) == ([1], [True]) + # On an open Stream the same page isn't done. + open_ref = await _filled_stream(stream_client, ["a", "b", "c"], close=False) + capped = await _read_pages( + stream_client, ReadArgs(open_ref.id, max_records=3, pages=1) + ) + assert (capped.page_sizes, capped.dones) == ([3], [False]) + + +async def test_page_with_max_bytes_gives_the_first_record_over_the_cap( + client: Client, +): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a", "b", "c"]) + result = await _read_pages(stream_client, ReadArgs(ref.id, max_bytes=1)) + # The first Record of each page always comes, even over the cap. + assert result.page_sizes == [1, 1, 1] + assert result.dones == [False, False, True] + + +@activity.defn +async def append_after_a_quiet_gap(stream_id: str) -> None: + stream = activity.client().get_stream(StreamRef(id=stream_id), body_type=str) + await stream.append("a") + await asyncio.sleep(2) + await stream.append("b") + await stream.close() + + +@workflow.defn +class QuietGapWorkflow: + @workflow.run + async def run(self, stream_id: str) -> list[str]: + writer = workflow.start_activity( + append_after_a_quiet_gap, + stream_id, + start_to_close_timeout=timedelta(seconds=30), + ) + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + got = [ + record.body + async for record in stream.iter_read_items(wait=timedelta(seconds=10)) + ] + await writer + return got + + +async def test_iter_read_items_with_a_wait_reads_across_a_quiet_gap(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, [], close=False) + handle, result = await _run( + stream_client, + QuietGapWorkflow, + ref.id, + activities=[append_after_a_quiet_gap], + ) + assert result == ["a", "b"] + # The gap of 2 s is shorter than the wait, so one Core read gets both Records. + assert len(_read_seqs(await handle.fetch_history())) == 1 + + +# A loop with no await in its body, and one that awaits for each Record. + + +def _read_entries(history: WorkflowHistory) -> list[list[StreamReadEntry]]: + """Give the read entries of the `core_stream` marker of each Workflow Task.""" + entries: list[list[StreamReadEntry]] = [] + for event in history.events: + attributes = event.marker_recorded_event_attributes + if ( + event.event_type != EventType.EVENT_TYPE_MARKER_RECORDED + or attributes.marker_name != "core_stream" + ): + continue + [payload] = attributes.details["data"].payloads + entries.append(list(StreamMarkerData.FromString(payload.data).reads)) + return entries + + +def _read_seqs(history: WorkflowHistory) -> set[int]: + """Give the seq of each Core read that History has.""" + return {entry.seq for task in _read_entries(history) for entry in task} + + +@activity.defn +async def append_five_slowly(stream_id: str) -> None: + stream = activity.client().get_stream(StreamRef(id=stream_id), body_type=str) + for index in range(5): + await asyncio.sleep(0.3) + await stream.append(f"record-{index}") + await stream.close() + + +@workflow.defn +class LoopWithNoAwaitWorkflow: + @workflow.run + async def run(self, stream_id: str) -> list[str]: + writer = workflow.start_activity( + append_five_slowly, stream_id, start_to_close_timeout=timedelta(seconds=30) + ) + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + got = [] + async for record in stream.iter_read_items(wait=timedelta(seconds=10)): + got.append(record.body) + await writer + return got + + +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_loop_with_no_await_keeps_one_core_read( + client: Client, max_cached_workflows: int +): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, [], close=False) + handle, got = await _run( + stream_client, + LoopWithNoAwaitWorkflow, + ref.id, + activities=[append_five_slowly], + max_cached_workflows=max_cached_workflows, + ) + assert got == [f"record-{index}" for index in range(5)] + entries = _read_entries(await handle.fetch_history()) + # The loop asks for the next page in the activation of the last one, so one Core + # read gives every page. + assert len({entry.seq for task in entries for entry in task}) == 1 + assert any(len(task) == 1 and len(task[0].page_counts) > 1 for task in entries) + + +@workflow.defn +class LoopWithAppendWorkflow: + @workflow.run + async def run(self, stream_id: str, out_id: str) -> list[str]: + writer = workflow.start_activity( + append_five_slowly, stream_id, start_to_close_timeout=timedelta(seconds=30) + ) + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + out = workflow.get_stream(StreamRef(id=out_id), body_type=str) + got = [] + async for record in stream.iter_read_items(wait=timedelta(seconds=10)): + got.append(record.body) + await out.append(record.body) + await writer + return got + + +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_loop_that_awaits_an_append_closes_and_starts_the_read( + client: Client, max_cached_workflows: int +): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, [], close=False) + out_ref = await _filled_stream(stream_client, [], close=False) + handle, got = await _run( + stream_client, + LoopWithAppendWorkflow, + ref.id, + out_ref.id, + activities=[append_five_slowly], + max_cached_workflows=max_cached_workflows, + ) + bodies = [f"record-{index}" for index in range(5)] + assert got == bodies + # Each Record went to the other Stream once and in order, whatever the replays. + assert [record.body for record in await _read_all(stream_client, out_ref)] == bodies + # The append ends the activation with no call waiting on the read, so the read + # closes, and the next page starts another one. + assert len(_read_seqs(await handle.fetch_history())) > 1 + + +# The example of the design review: +# +# let mut i = 0; +# while let item = stream.next() { +# workflow.spawn(loop { yield_once.await; i++; }) +# if i == 2 { await execute_activity(...); } +# } +# +# Each Record starts one more task that counts the wakes of the Workflow. So whether the +# Activity starts, and at which Record, depends on the activations that gave the Workflow +# its Records, and not on the Records. + + +class _Wakes: + """Lets a task of a Workflow wait for the next wake of the Workflow. + + Python has no call for it. ``asyncio.sleep(0)`` resumes in the same activation, so a + loop of it never lets the activation end. + """ + + def __init__(self) -> None: + self._new: list[asyncio.Future[None]] = [] + self._parked: list[asyncio.Future[None]] = [] + self._resumed = False + self._watch = asyncio.create_task(workflow.wait_condition(self._on_idle)) + + def _on_idle(self) -> bool: + # The event loop checks a condition each time it has no ready task. That is once at + # the end of a wake, and once more when the tasks that this resumed have run. The + # second check is in the same wake, so it resumes nothing. + if self._resumed: + self._resumed = False + self._parked += self._new + else: + self._resumed = bool(self._parked) + for future in self._parked: + if not future.done(): + future.set_result(None) + self._parked = self._new + self._new = [] + return False + + async def yield_once(self) -> None: + future: asyncio.Future[None] = asyncio.get_running_loop().create_future() + self._new.append(future) + await future + + +@workflow.defn +class CounterTasksWorkflow: + def __init__(self) -> None: + self.i = 0 + + async def count(self, wakes: _Wakes) -> None: + while True: + await wakes.yield_once() + self.i += 1 + + @workflow.run + async def run(self, stream_id: str) -> list[int]: + wakes = _Wakes() + seen: list[int] = [] + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + async for item in stream.iter_read_items(wait=timedelta(seconds=30)): + asyncio.create_task(self.count(wakes)) + seen.append(self.i) + if self.i == 2: + await workflow.execute_activity( + tag, item.body, start_to_close_timeout=timedelta(seconds=10) + ) + return seen + + +async def _read_calls(count: int) -> None: + """Wait until Workflow code has made this many `read` calls.""" + + async def check() -> None: + assert [name for name, _ in _intercepted].count("ReadRecords") >= count + + await assert_eventually(check, interval=timedelta(milliseconds=20)) + + +async def _run_counter_tasks( + client: Client, pages: list[list[str]], *, max_cached_workflows: int = 1000 +) -> tuple[list[int], WorkflowHistory]: + """Run the example, and give it each page of Records in an activation of its own. + + The last page must make the Workflow start its Activity. + """ + _intercepted.clear() + stream = await client.create_stream(id=f"stream-{uuid.uuid4()}", body_type=str) + # The Records that the Stream has when the read starts come in one page. + await stream.append(*pages[0]) + async with new_worker( + client, + CounterTasksWorkflow, + activities=[tag], + interceptors=[StreamCallInterceptor()], + max_cached_workflows=max_cached_workflows, + ) as worker: + handle = await client.start_workflow( + CounterTasksWorkflow.run, + stream.ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + for calls, page in enumerate(pages[1:], start=2): + # The loop asks for its next page in the activation that gave it the page + # before. A Record that comes after that call is in a later activation. + await _read_calls(calls) + await stream.append(*page) + + async def activity_started() -> None: + assert _scheduled_activities(await handle.fetch_history()) == ["tag"] + + await assert_eventually(activity_started) + await stream.close() + seen = await handle.result() + return seen, await handle.fetch_history() + + +_COUNTER_PAGES = [["record-0", "record-1"], ["record-2"], ["record-3"]] + + +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_counter_tasks_start_the_activity_by_the_activations( + client: Client, max_cached_workflows: int +): + stream_client = _stream_client(client) + seen, history = await _run_counter_tasks( + stream_client, _COUNTER_PAGES, max_cached_workflows=max_cached_workflows + ) + # The first two Records start a task each, and both tasks count the next wake. The loop + # reads `i` before the tasks of a wake run, so `i` is 2 at the fourth Record. + assert seen == [0, 0, 0, 2] + # The marker of the Workflow Task that started the Activity has the three pages, and + # each page is in an activation of its own. + [entry] = _read_entries(history)[0] + assert list(entry.page_counts) == [2, 1, 1] + assert all(gap > 0 for gap in entry.page_activations[1:]) + # A replay gives each page in the activation that got it, so `i` is the same at each + # Record. With no cache, the Workflow Tasks after the first one were such replays too. + config = stream_client.config()["stream_config"] + await Replayer( + workflows=[CounterTasksWorkflow], stream_config=config + ).replay_workflow(history) + + +async def test_counter_tasks_with_one_page_start_no_activity(client: Client): + stream_client = _stream_client(client) + bodies = [body for page in _COUNTER_PAGES for body in page] + ref = await _filled_stream(stream_client, bodies) + handle, seen = await _run( + stream_client, CounterTasksWorkflow, ref.id, activities=[tag] + ) + # With the same Records in one activation, no task counts a wake between two of them. + assert seen == [0, 0, 0, 0] + assert _scheduled_activities(await handle.fetch_history()) == [] + + +async def test_replay_with_other_activations_is_a_nondeterminism_error(client: Client): + stream_client = _stream_client(client) + seen, history = await _run_counter_tasks(stream_client, _COUNTER_PAGES) + assert seen == [0, 0, 0, 2] + # Put every page of a read into the activation of its first page. `i` then stays 0, and + # the replay does not start the Activity that History has. + for event in history.events: + attributes = event.marker_recorded_event_attributes + if ( + event.event_type != EventType.EVENT_TYPE_MARKER_RECORDED + or attributes.marker_name != "core_stream" + ): + continue + [payload] = attributes.details["data"].payloads + data = StreamMarkerData.FromString(payload.data) + for entry in data.reads: + gaps = [0] * (len(entry.page_activations) - 1) + del entry.page_activations[1:] + entry.page_activations.extend(gaps) + payload.data = data.SerializeToString() + config = stream_client.config()["stream_config"] + replay = await Replayer( + workflows=[CounterTasksWorkflow], stream_config=config + ).replay_workflow(history, raise_on_replay_failure=False) + assert isinstance(replay.replay_failure, workflow.NondeterminismError) + + +# The Records of one page, taken in several activations. Core records the page +# when it gives it. An Activity between two Records is a later Workflow Task, +# and that task has no marker. + + +@workflow.defn +class ActivityForEachRecordWorkflow: + @workflow.run + async def run(self, stream_id: str) -> list[str]: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + got = [] + async for item in stream.iter_read_items(): + got.append( + await workflow.execute_activity( + tag, item.body, start_to_close_timeout=timedelta(seconds=10) + ) + ) + return got + + +@workflow.defn +class OneRecordPerReadWorkflow: + @workflow.run + async def run(self, stream_id: str) -> list[str]: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + got = [] + offset = None + while True: + page = await stream.read(from_offset=offset, max_records=1) + for record in page.records: + got.append( + await workflow.execute_activity( + tag, + record.body, + start_to_close_timeout=timedelta(seconds=10), + ) + ) + offset = page.last_offset + if page.done or not page.records: + return got + + +async def _one_page_across_activations( + client: Client, workflow_type: type[Any], *, max_cached_workflows: int +) -> None: + ref = await _filled_stream(client, ["a", "b", "c", "d"]) + handle, got = await _run( + client, + workflow_type, + ref.id, + activities=[tag], + max_cached_workflows=max_cached_workflows, + ) + assert got == [f"tagged {body}" for body in "abcd"] + history = await handle.fetch_history() + # The last Activity's result is a task of its own, and that task completes + # the Workflow. It records nothing, so the marker stays in the first task. + assert _completed_tasks(history) == 5 + assert _scheduled_activities(history) == ["tag", "tag", "tag", "tag"] + task = 0 + marker_task = None + for event in history.events: + if event.event_type == EventType.EVENT_TYPE_WORKFLOW_TASK_COMPLETED: + task += 1 + elif ( + event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED + and event.marker_recorded_event_attributes.marker_name == "core_stream" + ): + assert marker_task is None + marker_task = task + assert marker_task == 1 + entries = _read_entries(history) + assert len(entries) == 1 + [entry] = entries[0] + assert list(entry.page_counts) == [4] + assert list(entry.page_activations) == [1] + config = client.config()["stream_config"] + await Replayer(workflows=[workflow_type], stream_config=config).replay_workflow( + history + ) + + +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_one_page_across_activities_keeps_its_marker( + client: Client, max_cached_workflows: int +): + await _one_page_across_activations( + _stream_client(client), + ActivityForEachRecordWorkflow, + max_cached_workflows=max_cached_workflows, + ) + + +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_one_record_reads_of_one_page_keep_its_marker( + client: Client, max_cached_workflows: int +): + await _one_page_across_activations( + _stream_client(client), + OneRecordPerReadWorkflow, + max_cached_workflows=max_cached_workflows, + ) + + +# What `read` checks, and where it can't run. + + +# A client's read clamps each of these, or refuses it, and a Workflow read does the same. +_READ_LIMIT_CASES: list[dict[str, Any]] = [ + {"max_bytes": -1}, + {"max_records": 0}, + {"max_records": -1}, + {"max_records": 5000}, + {"wait": timedelta(seconds=-1)}, + {"wait": timedelta(seconds=120)}, +] + + +@workflow.defn +class ReadLimitsWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> list[Any]: + stream = workflow.get_stream(ref, body_type=str) + results: list[Any] = [] + for arguments in _READ_LIMIT_CASES: + try: + page = await stream.read(**arguments) + results.append(len(page.records)) + except StreamError as err: + results.append([StreamErrorReason.Name(err.reason), err.message]) + return results + + +async def test_read_takes_the_limits_of_a_client_read(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a", "b", "c"]) + _, results = await _run(stream_client, ReadLimitsWorkflow, ref) + stream = stream_client.get_stream(ref, body_type=str) + expected: list[Any] = [] + for arguments in _READ_LIMIT_CASES: + try: + page = await stream.read(**arguments) + expected.append(len(page.records)) + except StreamError as err: + expected.append([StreamErrorReason.Name(err.reason), err.message]) + assert results == expected + assert results[0] == ["STREAM_ERROR_REASON_INVALID", expected[0][1]] + + +@workflow.defn +class LongOffsetWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> list[str]: + stream = workflow.get_stream(ref, body_type=str) + got: list[str] = [] + try: + await stream.read(from_offset=b"o" * 2000) + got.append("page") + except StreamError as err: + got.append(StreamErrorReason.Name(err.reason)) + got.extend([record.body async for record in stream.iter_read_items()]) + return got + + +async def test_read_from_an_offset_over_1024_bytes_raises_invalid(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a", "b"]) + handle, got = await _run(stream_client, LongOffsetWorkflow, ref) + assert got == ["STREAM_ERROR_REASON_INVALID", "a", "b"] + config = stream_client.config()["stream_config"] + await Replayer( + workflows=[LongOffsetWorkflow], stream_config=config + ).replay_workflow(history=await handle.fetch_history()) + + +@workflow.defn +class QueryReadWorkflow: + def __init__(self) -> None: + self.ref: StreamRef[str] | None = None + + @workflow.run + async def run(self, ref: StreamRef[str]) -> None: + self.ref = ref + await workflow.wait_condition(lambda: False) + + @workflow.query + def start_read(self) -> str: + assert self.ref is not None + step = workflow.get_stream(self.ref, body_type=str).read() + try: + step.send(None) + except Exception as err: + return type(err).__name__ + finally: + step.close() + return "started" + + +async def test_read_in_a_query_raises_the_read_only_error(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a"]) + async with new_worker(stream_client, QueryReadWorkflow) as worker: + handle = await stream_client.start_workflow( + QueryReadWorkflow.run, + ref, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert await handle.query(QueryReadWorkflow.start_read) == ( + "ReadOnlyContextError" + ) + await handle.terminate() + + +# Reads that resume from an offset. + + +@workflow.defn +class LoopReadWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> list[str]: + stream = workflow.get_stream(ref, body_type=str) + got: list[str] = [] + # The first loop leaves with `break`, and its next request resumes there. + items = stream.iter_read_items(max_records=4) + async for record in items: + got.append(record.body) + if len(got) == 3: + break + assert items.next_request is not None + last = items.next_request.from_offset + # The last reads go on from there to the end, two Records at a time. + while True: + page = await stream.read(from_offset=last, max_records=2) + got.extend(record.body for record in page.records) + last = page.last_offset + if page.done: + return got + + +async def test_reads_from_an_offset_get_each_record_once(client: Client): + stream_client = _stream_client(client) + bodies = [f"record-{index}" for index in range(10)] + ref = await _filled_stream(stream_client, bodies) + _, got = await _run(stream_client, LoopReadWorkflow, ref) + assert got == bodies + + +# The marker in History. + + +async def _history_of_a_read( + client: Client, replay_from: StreamReplaySource +) -> tuple[WorkflowHistory, StreamRef[str], Client]: + stream_client = _stream_client(client, replay_from=replay_from) + ref = await _filled_stream(stream_client, [f"{_MARK}-1", f"{_MARK}-2"]) + handle, result = await _run(stream_client, ReadPagesWorkflow, ReadArgs(ref.id)) + assert result.bodies == [f"{_MARK}-1", f"{_MARK}-2"] + return await handle.fetch_history(), ref, stream_client + + +def _markers_have_mark(history: WorkflowHistory) -> bool: + # The result of the Workflow holds the bodies, so only the markers count. + return any( + _MARK.encode() in event.SerializeToString() + for event in history.events + if event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED + ) + + +async def test_store_marker_holds_no_body(client: Client): + history, _, _ = await _history_of_a_read(client, StreamReplaySource.STORE) + assert "core_stream" in _markers(history) + assert not _markers_have_mark(history) + + +async def test_history_marker_holds_the_bodies(client: Client): + history, _, _ = await _history_of_a_read(client, StreamReplaySource.HISTORY) + assert "core_stream" in _markers(history) + assert _markers_have_mark(history) + + +# Replay. + + +async def test_replay_of_store_marker_reads_the_store(client: Client): + history, _, stream_client = await _history_of_a_read( + client, StreamReplaySource.STORE + ) + config = stream_client.config()["stream_config"] + await Replayer(workflows=[ReadPagesWorkflow], stream_config=config).replay_workflow( + history + ) + + +async def _failed_replay( + history: WorkflowHistory, ref: StreamRef[str], config: StreamConfig | None +) -> str: + """Replay with and without a raise, and give the text of the failure.""" + replayer = Replayer(workflows=[ReadPagesWorkflow], stream_config=config) + with pytest.raises(RuntimeError) as err: + await replayer.replay_workflow(history) + result = await replayer.replay_workflow(history, raise_on_replay_failure=False) + assert isinstance(result.replay_failure, RuntimeError) + assert str(result.replay_failure) == str(err.value) + message = str(err.value) + assert f"StreamReplayError: Stream `{ref.id}` in Store `memory`" in message + return message + + +async def test_replay_of_store_marker_with_no_stream_config_fails(client: Client): + history, ref, _ = await _history_of_a_read(client, StreamReplaySource.STORE) + message = await _failed_replay(history, ref, None) + assert "this Worker has no Stream runtime for the Store `memory`" in message + + +async def test_replay_of_store_marker_fails_when_records_are_gone(client: Client): + history, ref, _ = await _history_of_a_read(client, StreamReplaySource.STORE) + # A new memory Store of the same name has none of the Records. + message = await _failed_replay( + history, ref, StreamConfig(stores=[MemoryStreamStore()]) + ) + assert "stream_not_found" in message + + +async def test_replay_of_history_marker_needs_no_stream_config(client: Client): + history, _, _ = await _history_of_a_read(client, StreamReplaySource.HISTORY) + await Replayer(workflows=[ReadPagesWorkflow]).replay_workflow(history) + + +# Two reads at once, on a Worker with no cache and on a cached one. + + +@activity.defn +async def tag(body: str) -> str: + return f"tagged {body}" + + +@dataclass +class TwoReadsArgs: + first_id: str + second_id: str + out_id: str + + +@dataclass +class TwoReadsResult: + first: list[str] + second: list[str] + count: int + reads: int + + +@workflow.defn +class TwoReadsWorkflow: + def __init__(self) -> None: + self.count = 0 + self.reads = 0 + + async def _read(self, stream_id: str, out: Any) -> list[str]: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + got: list[str] = [] + last = None + while True: + self.reads += 1 + page = await stream.read(from_offset=last, max_records=2) + for record in page.records: + tagged = await workflow.execute_local_activity( + tag, record.body, start_to_close_timeout=timedelta(seconds=10) + ) + await out.append(tagged) + self.count += 1 + got.append(tagged) + last = page.last_offset + if page.done: + return got + # A timer ends the Workflow Task, so a Worker with no cache replays each read. + await workflow.sleep(0.05) + + @workflow.run + async def run(self, args: TwoReadsArgs) -> TwoReadsResult: + out = workflow.get_stream(StreamRef(id=args.out_id), body_type=str) + first, second = await asyncio.gather( + self._read(args.first_id, out), self._read(args.second_id, out) + ) + return TwoReadsResult(first, second, self.count, self.reads) + + +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_two_reads_with_and_without_a_cache( + client: Client, max_cached_workflows: int +): + stream_client = _stream_client(client) + first = [f"first-{index}" for index in range(5)] + second = [f"second-{index}" for index in range(5)] + out_ref = await _filled_stream(stream_client, [], close=False) + args = TwoReadsArgs( + first_id=(await _filled_stream(stream_client, first)).id, + second_id=(await _filled_stream(stream_client, second)).id, + out_id=out_ref.id, + ) + handle, result = await _run( + stream_client, + TwoReadsWorkflow, + args, + activities=[tag], + max_cached_workflows=max_cached_workflows, + ) + assert result == TwoReadsResult( + first=[f"tagged {body}" for body in first], + second=[f"tagged {body}" for body in second], + count=10, + reads=6, + ) + # Each Record went to the out Stream once, whatever the replays. + out = [record.body for record in await _read_all(stream_client, out_ref)] + assert sorted(out) == sorted(result.first + result.second) + assert _completed_tasks(await handle.fetch_history()) > 2 + + +# A Direct append. + + +@workflow.defn +class AppendWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> list[str | None]: + stream = workflow.get_stream(ref, body_type=str) + appended = await stream.append("a", "b") + # A timer ends the Workflow Task, so a Worker with no cache replays the append. + await workflow.sleep(0.05) + return [_hex(appended.first_offset), _hex(appended.last_offset)] + + +async def test_direct_append(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, [], close=False) + handle, offsets = await _run( + stream_client, AppendWorkflow, ref, max_cached_workflows=0 + ) + records = await _read_all(stream_client, ref) + # The replay of the next Workflow Task appended nothing. + assert [record.body for record in records] == ["a", "b"] + assert offsets == [record.offset.hex() for record in records] + history = await handle.fetch_history() + run_id = handle.result_run_id + started = [ + event.event_id + for event in history.events + if event.event_type == EventType.EVENT_TYPE_WORKFLOW_TASK_STARTED + ] + for record in records: + producer = record.producer + assert isinstance(producer, Producer) + assert producer.id == f"wf:{run_id}" + assert isinstance(producer.kind, ProducerKindWorkflow) + assert producer.kind.value.run_id == run_id + epoch = producer.kind.value.epoch + assert epoch is not None + # The epoch is the first Workflow Task, which made the append. + assert (epoch.started_event_id, epoch.attempt) == (started[0], 1) + assert _markers(history) == ["core_stream"] + # A Replayer appends nothing either. + await Replayer( + workflows=[AppendWorkflow], + stream_config=stream_client.config()["stream_config"], + ).replay_workflow(history) + assert len(await _read_all(stream_client, ref)) == 2 + + +@workflow.defn +class RefusedAppendWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> list[str]: + stream = workflow.get_stream(ref, body_type=str) + try: + await stream.append("a") + except StreamError as err: + # A timer only on the error path, so a replay without the error differs. + await workflow.sleep(0.01) + return [ + StreamErrorReason.Name(err.reason), + err.message, + str(err.non_retryable), + ] + return ["appended"] + + +async def test_refused_append_raises_stream_error_live_and_in_replay(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a"]) + handle, result = await _run(stream_client, RefusedAppendWorkflow, ref) + # The client path gives the same error for the same append. + with pytest.raises(StreamError) as client_err: + await stream_client.get_stream(ref, body_type=str).append("a") + assert result == [ + "STREAM_ERROR_REASON_CLOSED", + client_err.value.message, + "True", + ] + await Replayer( + workflows=[RefusedAppendWorkflow], + stream_config=stream_client.config()["stream_config"], + ).replay_workflow(await handle.fetch_history()) + + +# A Workflow Task that fails once in the middle of a read. + +_fail_read_task = True + + +@workflow.defn(sandboxed=False) +class FailOnceInReadWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str], out_ref: StreamRef[str]) -> list[str]: + stream = workflow.get_stream(ref, body_type=str) + out = workflow.get_stream(out_ref, body_type=str) + got: list[str] = [] + async for record in stream.iter_read_items(): + got.append(record.body) + if record.body == "record-2": + await out.append("seen record-2") + global _fail_read_task + if _fail_read_task: + _fail_read_task = False + raise RuntimeError("Failed the Workflow Task once") + return got + + +async def test_failed_task_in_a_read_gets_each_record_once(client: Client): + global _fail_read_task + _fail_read_task = True + stream_client = _stream_client(client) + bodies = [f"record-{index}" for index in range(5)] + ref = await _filled_stream(stream_client, bodies) + out_ref = await _filled_stream(stream_client, [], close=False) + _, got = await _run(stream_client, FailOnceInReadWorkflow, ref, out_ref) + assert not _fail_read_task + assert got == bodies + # The append of the failed task reached the Store, and its retry appends again. + out = await _read_all(stream_client, out_ref) + assert [record.body for record in out] == ["seen record-2"] * 2 + epochs = [] + for record in out: + assert record.producer is not None + assert isinstance(record.producer.kind, ProducerKindWorkflow) + epoch = record.producer.kind.value.epoch + assert epoch is not None + epochs.append((epoch.started_event_id, epoch.attempt)) + assert epochs[0] < epochs[1] + + +# A Signal while a read waits. + + +@workflow.defn +class SignalStopsReadWorkflow: + def __init__(self) -> None: + self.signaled = False + + @workflow.run + async def run(self, ref: StreamRef[str]) -> list[Any]: + stream = workflow.get_stream(ref, body_type=str) + got: list[str] = [] + + async def read_all() -> None: + async for record in stream.iter_read_items(wait=timedelta(seconds=60)): + got.append(record.body) + + reader = asyncio.create_task(read_all()) + await workflow.wait_condition(lambda: self.signaled) + # The cancel leaves the read with no call that waits on it, so it closes. + reader.cancel() + try: + await reader + except asyncio.CancelledError: + pass + return [got, self.signaled] + + @workflow.signal + def stop(self) -> None: + self.signaled = True + + +async def test_signal_reaches_a_waiting_read(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a"], close=False) + async with new_worker(stream_client, SignalStopsReadWorkflow) as worker: + handle = await stream_client.start_workflow( + SignalStopsReadWorkflow.run, + ref, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + # The heartbeat of an open task comes at 80 % of its timeout. + task_timeout=timedelta(seconds=2), + ) + await asyncio.sleep(0.5) + await handle.signal(SignalStopsReadWorkflow.stop) + result = await asyncio.wait_for(handle.result(), timeout=30) + assert result == [["a"], True] + await Replayer( + workflows=[SignalStopsReadWorkflow], + stream_config=stream_client.config()["stream_config"], + ).replay_workflow(await handle.fetch_history()) + + +# A second loop after a `break`, and the ends of a run with a read open. + + +@activity.defn +async def append_and_close(stream_id: str) -> None: + stream = activity.client().get_stream(StreamRef(id=stream_id), body_type=str) + await stream.append("x", "y") + await stream.close() + + +@workflow.defn +class BreakThenReadAgainWorkflow: + @workflow.run + async def run(self, stream_id: str) -> list[Any]: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + first = stream.iter_read_items(wait=timedelta(seconds=30)) + got_first: list[str] = [] + async for record in first: + got_first.append(record.body) + if len(got_first) == 2: + break + assert first.next_request is not None + # The second loop resumes after the last Record that the first one gave. + second = stream.iter_read( + from_offset=first.next_request.from_offset, wait=timedelta(seconds=30) + ) + writer = workflow.start_activity( + append_and_close, stream_id, start_to_close_timeout=timedelta(seconds=30) + ) + got_second: list[str] = [] + done = False + async for page in second: + got_second.extend(record.body for record in page.records) + done = page.done + await writer + return [got_first, got_second, done] + + +async def test_second_loop_after_a_break_gets_every_record(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a", "b", "c"], close=False) + handle, result = await _run( + stream_client, BreakThenReadAgainWorkflow, ref.id, activities=[append_and_close] + ) + assert result == [["a", "b"], ["c", "x", "y"], True] + await Replayer( + workflows=[BreakThenReadAgainWorkflow], + stream_config=stream_client.config()["stream_config"], + ).replay_workflow(await handle.fetch_history()) + + +@workflow.defn +class CompleteWithOpenReadsWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + first = await stream.read(wait=timedelta(seconds=60)) + # A second read waits in its own task while the Workflow returns. + waiting = asyncio.create_task( + stream.read(from_offset=first.last_offset, wait=timedelta(seconds=60)) + ) + await asyncio.sleep(0) + assert not waiting.done() + return "completed" + + +async def test_workflow_completes_with_open_reads(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a"], close=False) + async with new_worker(stream_client, CompleteWithOpenReadsWorkflow) as worker: + handle = await stream_client.start_workflow( + CompleteWithOpenReadsWorkflow.run, + ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert await asyncio.wait_for(handle.result(), timeout=20) == "completed" + await Replayer( + workflows=[CompleteWithOpenReadsWorkflow], + stream_config=stream_client.config()["stream_config"], + ).replay_workflow(await handle.fetch_history()) + + +@workflow.defn +class CancelDuringReadWorkflow: + @workflow.run + async def run(self, stream_id: str) -> list[Any]: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + got: list[str] = [] + try: + async for record in stream.iter_read_items(wait=timedelta(seconds=60)): + got.append(record.body) + except asyncio.CancelledError: + return [got, "cancelled"] + return [got, "ended"] + + +async def test_cancel_reaches_a_waiting_read(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a"], close=False) + async with new_worker(stream_client, CancelDuringReadWorkflow) as worker: + handle = await stream_client.start_workflow( + CancelDuringReadWorkflow.run, + ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + # The heartbeat of an open task comes at 80 % of its timeout. + task_timeout=timedelta(seconds=2), + ) + await asyncio.sleep(0.5) + await handle.cancel() + result = await asyncio.wait_for(handle.result(), timeout=30) + assert result == [["a"], "cancelled"] + + +@workflow.defn +class ContinueWithOpenReadWorkflow: + @workflow.run + async def run(self, stream_id: str, from_offset: str | None) -> list[str]: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + if from_offset is None: + # The Core read of this page is still open when the run continues. + first = await stream.read(max_records=1, wait=timedelta(seconds=60)) + assert first.last_offset is not None + workflow.continue_as_new(args=[stream_id, first.last_offset.hex()]) + items = stream.iter_read_items(from_offset=bytes.fromhex(from_offset)) + return [record.body async for record in items] + + +async def test_continue_as_new_with_an_open_read(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a", "b", "c"], close=False) + async with new_worker(stream_client, ContinueWithOpenReadWorkflow) as worker: + handle = await stream_client.start_workflow( + ContinueWithOpenReadWorkflow.run, + args=[ref.id, None], + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + result = await asyncio.wait_for(handle.result(), timeout=20) + assert result == ["b", "c"] + first_run = stream_client.get_workflow_handle( + handle.id, run_id=handle.first_execution_run_id + ) + await Replayer( + workflows=[ContinueWithOpenReadWorkflow], + stream_config=stream_client.config()["stream_config"], + ).replay_workflow(await first_run.fetch_history()) + + +# A Store written in Python. + + +@workflow.defn +class PythonStoreAppendWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> None: + await workflow.get_stream(ref, body_type=str).append("a") + + +@workflow.defn +class PythonStoreCloseWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> None: + await workflow.get_stream(ref, body_type=str).close() + + +async def _python_store_failure( + client: Client, workflow_type: type[Any], ref: StreamRef[str] +) -> ApplicationError: + with pytest.raises(WorkflowFailureError) as err: + await _run(client, workflow_type, ref) + cause = err.value.cause + assert isinstance(cause, ApplicationError) + assert cause.type == "StreamConfigError" + assert cause.non_retryable + return cause + + +async def test_python_store_refuses_an_append(client: Client): + stream_client = _stream_client(client, DictStreamStore()) + ref = await _filled_stream(stream_client, [], close=False) + cause = await _python_store_failure(stream_client, PythonStoreAppendWorkflow, ref) + assert cause.message == ( + "The Stream Store 'dict' has no Direct append, since it is written in Python. " + "Append to the Stream in an Activity, or use a Store that runs on the bridge, " + "such as the Redis Store or MemoryStreamStore." + ) + assert await _read_all(stream_client, ref) == [] + + +async def test_python_store_refuses_a_read(client: Client): + stream_client = _stream_client(client, DictStreamStore()) + ref = await _filled_stream(stream_client, ["a"]) + with pytest.raises(WorkflowFailureError) as err: + await _run(stream_client, ReadPagesWorkflow, ReadArgs(ref.id, store="dict")) + cause = err.value.cause + assert isinstance(cause, ApplicationError) + assert cause.type == "StreamConfigError" + assert cause.non_retryable + assert cause.message == ( + "The Stream Store 'dict' has no Direct read, since it is written in Python. " + "Read the Stream in an Activity, or use a Store that runs on the bridge, such " + "as the Redis Store or MemoryStreamStore." + ) + + +# A payload codec. + + +@workflow.defn +class AppendThenReadWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> list[str]: + stream = workflow.get_stream(ref, body_type=str) + await stream.append("a", "b") + return [record.body async for record in stream.iter_read_items()] + + +async def test_payload_codec_covers_direct_append_and_read(client: Client): + store = MemoryStreamStore() + stream_client = _stream_client(client, store, codec=True) + ref = await _filled_stream(stream_client, [], close=False) + _, bodies = await _run(stream_client, AppendThenReadWorkflow, ref) + assert bodies == ["a", "b"] + # A client without the codec reads the bodies as the Store keeps them. + raw = _stream_client(client, store).get_stream( + StreamRef(id=ref.id), body_type=RawValue + ) + stored = [record.body.payload async for record in raw.iter_read_items()] + assert [body.metadata["simple-codec"] for body in stored] == [b"true", b"true"] + + +# A close after a Direct append. + + +@workflow.defn +class AppendThenCloseWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> None: + stream = workflow.get_stream(ref, body_type=str) + await stream.append("a") + await stream.close() + + +async def test_close_after_direct_append(client: Client): + # The append writes as the Workflow with its task as the epoch. The close, from an + # Activity, sends the same Workflow Producer with no epoch, and the Store fences it. + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, [], close=False) + await _run(stream_client, AppendThenCloseWorkflow, ref) + assert [record.body for record in await _read_all(stream_client, ref)] == ["a"] + + +# A measurement. + + +@dataclass +class Burst: + stream_id: str + records: int = 200 + batches: int = 20 + over_ms: int = 2000 + + +@activity.defn +async def write_burst(burst: Burst) -> None: + stream = activity.client().get_stream(StreamRef(id=burst.stream_id), body_type=str) + size = burst.records // burst.batches + for batch in range(burst.batches): + await stream.append( + *(f"record-{batch * size + index}" for index in range(size)) + ) + await asyncio.sleep(burst.over_ms / 1000 / burst.batches) + await stream.close() + + +@workflow.defn +class MeasureWorkflow: + @workflow.run + async def run(self, burst: Burst) -> int: + writer = workflow.start_activity( + write_burst, burst, start_to_close_timeout=timedelta(seconds=60) + ) + stream = workflow.get_stream(StreamRef(id=burst.stream_id), body_type=str) + count = 0 + async for _ in stream.iter_read_items(wait=timedelta(seconds=30)): + count += 1 + await writer + return count + + +@pytest.mark.parametrize( + "replay_from", [StreamReplaySource.STORE, StreamReplaySource.HISTORY] +) +async def test_measure_read_of_200_records_over_2_seconds( + client: Client, replay_from: StreamReplaySource +): + stream_client = _stream_client(client, replay_from=replay_from) + ref = await _filled_stream(stream_client, [], close=False) + handle, count = await _run( + stream_client, MeasureWorkflow, Burst(ref.id), activities=[write_burst] + ) + assert count == 200 + history = await handle.fetch_history() + sizes = [ + event.marker_recorded_event_attributes.ByteSize() + for event in history.events + if event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED + ] + print( + f"\n{replay_from.name}: {_completed_tasks(history)} Workflow Tasks, " + f"{len(sizes)} markers, {sum(sizes)} marker bytes, sizes {sizes}" + ) + + +# Two loops from one offset, and calls on a read that fails. + + +@workflow.defn +class TwoLoopsWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> list[list[str]]: + stream = workflow.get_stream(ref, body_type=str) + + async def drain() -> list[str]: + return [record.body async for record in stream.iter_read_items()] + + # Both loops ask for the first page before either gets it, so each one takes a + # read of its own. + first = asyncio.create_task(drain()) + second = asyncio.create_task(drain()) + return [await first, await second] + + +async def test_two_loops_from_one_offset_each_get_every_record(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a", "b", "c"]) + handle, result = await _run(stream_client, TwoLoopsWorkflow, ref) + assert result == [["a", "b", "c"], ["a", "b", "c"]] + assert len(_read_seqs(await handle.fetch_history())) == 2 + + +@workflow.defn +class FailedReadsWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> list[str]: + stream = workflow.get_stream(ref, body_type=str) + + async def read() -> str: + try: + await stream.read(wait=timedelta(seconds=10)) + except StreamError as err: + return StreamErrorReason.Name(err.reason) + return "page" + + # Two calls wait before the failure comes, and a later call fails too. + first = asyncio.create_task(read()) + second = asyncio.create_task(read()) + return [await first, await second, await read()] + + +async def test_each_read_of_a_missing_stream_raises(client: Client): + stream_client = _stream_client(client) + ref: StreamRef[str] = StreamRef(id=f"stream-{uuid.uuid4()}") + _, result = await _run(stream_client, FailedReadsWorkflow, ref) + assert result == ["STREAM_ERROR_REASON_NOT_FOUND"] * 3 + + +# A codec that uses its serialization context. + + +@dataclass(frozen=True) +class ContextCodec(PayloadCodec, WithSerializationContext): + """Writes the Workflow id of its context into each payload, and decodes only its own.""" + + workflow_id: str = "" + + def with_context(self, context: SerializationContext) -> ContextCodec: + if isinstance(context, WorkflowSerializationContext): + return ContextCodec(context.workflow_id) + return ContextCodec("") + + async def encode(self, payloads: Sequence[Payload]) -> list[Payload]: + return [ + Payload( + metadata={"encoding": b"binary/ctx", "ctx": self.workflow_id.encode()}, + data=payload.SerializeToString(), + ) + for payload in payloads + ] + + async def decode(self, payloads: Sequence[Payload]) -> list[Payload]: + out = [] + for payload in payloads: + if payload.metadata.get("encoding") != b"binary/ctx": + out.append(payload) + continue + if payload.metadata["ctx"] != self.workflow_id.encode(): + raise ApplicationError( + f"Encoded for {payload.metadata['ctx']!r}, not " + f"{self.workflow_id!r}", + non_retryable=True, + ) + out.append(Payload.FromString(payload.data)) + return out + + +@workflow.defn +class AppendThenReadAllWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> list[str]: + stream = workflow.get_stream(ref, body_type=str) + await stream.append("from-workflow") + return [record.body async for record in stream.iter_read_items()] + + +async def test_codec_runs_with_no_context_on_stream_bodies(client: Client): + store = MemoryStreamStore() + config = client.config() + config["stream_config"] = StreamConfig(stores=[store]) + config["data_converter"] = DataConverter(payload_codec=ContextCodec()) + stream_client = Client(**config) + ref = await _filled_stream(stream_client, ["from-client"], close=False) + _, bodies = await _run(stream_client, AppendThenReadAllWorkflow, ref) + assert bodies == ["from-client", "from-workflow"] + # The client decodes with no context, so it reads the Workflow's body too. + assert [record.body for record in await _read_all(stream_client, ref)] == [ + "from-client", + "from-workflow", + ] + raw_config = client.config() + raw_config["stream_config"] = StreamConfig(stores=[store]) + raw = Client(**raw_config).get_stream(StreamRef(id=ref.id), body_type=RawValue) + stored = [record.body.payload async for record in raw.iter_read_items()] + assert [body.metadata["ctx"] for body in stored] == [b"", b""] + + +@workflow.defn +class AppendOneWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> None: + await workflow.get_stream(ref, body_type=str).append("from-workflow") + + +# Replays with no Stream config of calls whose ref names no Store. + + +@workflow.defn +class UnnamedRefReadWorkflow: + @workflow.run + async def run(self, stream_id: str) -> list[str]: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + return [record.body async for record in stream.iter_read_items()] + + +async def _unnamed_read_history( + client: Client, replay_from: StreamReplaySource +) -> tuple[WorkflowHistory, str]: + stream_client = _stream_client(client, replay_from=replay_from) + ref = await _filled_stream(stream_client, ["a", "b"]) + handle, result = await _run(stream_client, UnnamedRefReadWorkflow, ref.id) + assert result == ["a", "b"] + return await handle.fetch_history(), ref.id + + +async def test_history_replay_of_an_unnamed_ref_needs_no_config(client: Client): + history, _ = await _unnamed_read_history(client, StreamReplaySource.HISTORY) + await Replayer(workflows=[UnnamedRefReadWorkflow]).replay_workflow(history) + + +async def test_store_replay_of_an_unnamed_ref_with_no_config_fails(client: Client): + history, stream_id = await _unnamed_read_history(client, StreamReplaySource.STORE) + with pytest.raises(RuntimeError) as err: + await Replayer(workflows=[UnnamedRefReadWorkflow]).replay_workflow(history) + message = str(err.value) + assert f"StreamReplayError: Stream `{stream_id}` in Store `memory`" in message + assert "this Worker has no Stream runtime for the Store `memory`" in message + assert "Nondeterminism" not in message + + +@workflow.defn +class UnnamedRefAppendWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str | None: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + appended = await stream.append("a") + return _hex(appended.last_offset) + + +async def test_replay_of_an_unnamed_append_needs_no_config(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, [], close=False) + handle, _ = await _run(stream_client, UnnamedRefAppendWorkflow, ref.id) + history = await handle.fetch_history() + assert _markers(history) == ["core_stream"] + await Replayer(workflows=[UnnamedRefAppendWorkflow]).replay_workflow(history) + + +# A Record whose body fails to decode. + + +@dataclass +class Point: + x: int + + +@workflow.defn +class BadBodyWorkflow: + @workflow.run + async def run(self, stream_id: str) -> list[Any]: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=Point) + first = await stream.read(max_records=1) + events: list[Any] = [["got", first.records[0].body.x]] + # The page of the bad body raises, and a read from the same offset gets it again. + for _ in range(2): + try: + await stream.read(from_offset=first.last_offset, max_records=1) + events.append(["page", "decoded"]) + except Exception as err: + events.append(["page", type(err).__name__]) + return events + + +async def test_page_with_a_body_that_fails_to_decode_raises_each_time( + client: Client, +): + stream_client = _stream_client(client) + stream = await stream_client.create_stream(id=f"s-{uuid.uuid4()}", body_type=object) + await stream.append({"x": 1}, "not a point", {"x": 3}) + await stream.close() + _, events = await _run(stream_client, BadBodyWorkflow, stream.ref.id) + assert events == [["got", 1], ["page", "TypeError"], ["page", "TypeError"]] + + +# A Worker with no Workflows and a Store with a bad option. + + +@activity.defn +async def noop() -> None: + pass + + +async def test_worker_without_workflows_builds_no_store_runtime(client: Client): + from temporalio.contrib.redis.streams import RedisStreamStore + + config = client.config() + config["stream_config"] = StreamConfig(stores=[RedisStreamStore("not a url")]) + # Such a Worker starts, as it did before Direct calls, since it gives Core no runtime. + async with Worker( + Client(**config), task_queue=f"tq-{uuid.uuid4()}", activities=[noop] + ): + pass + # A Worker with Workflows gives Core the runtime, so it builds it at once. + with pytest.raises(ValueError, match="The Redis URL is wrong"): + Worker( + Client(**config), + task_queue=f"tq-{uuid.uuid4()}", + workflows=[AppendOneWorkflow], + ) + + +# The Direct close. + + +def _close_entries(history: WorkflowHistory) -> list[StreamCloseEntry]: + """Give the close entries of the `core_stream` markers, in order.""" + entries: list[StreamCloseEntry] = [] + for event in history.events: + attributes = event.marker_recorded_event_attributes + if ( + event.event_type != EventType.EVENT_TYPE_MARKER_RECORDED + or attributes.marker_name != "core_stream" + ): + continue + [payload] = attributes.details["data"].payloads + entries.extend(StreamMarkerData.FromString(payload.data).closes) + return entries + + +def _scheduled_activities(history: WorkflowHistory) -> list[str]: + return [ + event.activity_task_scheduled_event_attributes.activity_type.name + for event in history.events + if event.event_type == EventType.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED + ] + + +async def _status(client: Client, ref: StreamRef[str]) -> StreamStatus | None: + return (await client.get_stream(ref, body_type=str).describe()).status + + +@workflow.defn +class AppendMaybeCloseWorkflow: + @workflow.run + async def run(self, stream_id: str, close: bool) -> None: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + await stream.append("a") + if close: + await stream.close() + + +async def test_awaited_close_closes_in_one_more_task(client: Client): + stream_client = _stream_client(client) + closed_ref = await _filled_stream(stream_client, [], close=False) + open_ref = await _filled_stream(stream_client, [], close=False) + closed, _ = await _run(stream_client, AppendMaybeCloseWorkflow, closed_ref.id, True) + left_open, _ = await _run( + stream_client, AppendMaybeCloseWorkflow, open_ref.id, False + ) + assert await _status(stream_client, closed_ref) == StreamStatus.StreamStatusClosed + assert await _status(stream_client, open_ref) == StreamStatus.StreamStatusOpen + history = await closed.fetch_history() + assert _scheduled_activities(history) == [] + assert "core_local_activity" not in _markers(history) + # The task of the close records its intent, and the next one gives the result. + assert _completed_tasks(history) == ( + _completed_tasks(await left_open.fetch_history()) + 1 + ) + intent, result = _close_entries(history) + assert intent.HasField("intent_epoch") and not intent.resolved + assert result.resolved and not result.HasField("failure") + + +@workflow.defn +class FinallyCloseWorkflow: + @workflow.run + async def run(self, stream_id: str, fail: bool) -> str: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + try: + await stream.append("a") + if fail: + raise ApplicationError("failed on purpose", non_retryable=True) + return "returned" + finally: + await stream.close() + + +@pytest.mark.parametrize("fail", [False, True]) +async def test_close_in_finally(client: Client, fail: bool): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, [], close=False) + if fail: + with pytest.raises(WorkflowFailureError) as err: + await _run(stream_client, FinallyCloseWorkflow, ref.id, True) + assert isinstance(err.value.cause, ApplicationError) + assert err.value.cause.message == "failed on purpose" + else: + _, result = await _run(stream_client, FinallyCloseWorkflow, ref.id, False) + assert result == "returned" + assert await _status(stream_client, ref) == StreamStatus.StreamStatusClosed + assert [record.body for record in await _read_all(stream_client, ref)] == ["a"] + + +@workflow.defn +class AppendAfterCloseWorkflow: + @workflow.run + async def run(self, stream_id: str) -> list[str]: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + await stream.append("a") + await stream.close() + try: + await stream.append("b") + except StreamError as err: + return [StreamErrorReason.Name(err.reason), str(err.non_retryable)] + return ["appended"] + + +async def test_append_after_the_close_is_refused(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, [], close=False) + _, result = await _run(stream_client, AppendAfterCloseWorkflow, ref.id) + assert result == ["STREAM_ERROR_REASON_CLOSED", "True"] + assert [record.body for record in await _read_all(stream_client, ref)] == ["a"] + + +_fail_close_task = True + + +@workflow.defn(sandboxed=False) +class FailAfterCloseOnceWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + await stream.append("a") + close = asyncio.create_task(stream.close()) + # A few turns of the loop let the close send its command in this activation. + for _ in range(10): + await asyncio.sleep(0) + global _fail_close_task + if _fail_close_task: + _fail_close_task = False + raise RuntimeError("Failed the Workflow Task once") + await close + + +async def test_failed_task_after_the_close_leaves_the_stream_open(client: Client): + global _fail_close_task + _fail_close_task = True + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, [], close=False) + handle, _ = await _run(stream_client, FailAfterCloseOnceWorkflow, ref.id) + assert not _fail_close_task + # The retry could append, so the failed task closed nothing. + assert [record.body for record in await _read_all(stream_client, ref)] == ["a", "a"] + assert await _status(stream_client, ref) == StreamStatus.StreamStatusClosed + # History has one intent and one result, from the retry. + entries = _close_entries(await handle.fetch_history()) + assert [entry.resolved for entry in entries] == [False, True] + + +@workflow.defn +class CloseTwiceWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + await stream.close() + await stream.close() + return "closed" + + +async def test_close_of_a_closed_stream_succeeds(client: Client): + stream_client = _stream_client(client) + # A client closed this Stream before the Workflow closes it twice. + ref = await _filled_stream(stream_client, ["a"]) + _, result = await _run(stream_client, CloseTwiceWorkflow, ref.id) + assert result == "closed" + open_ref = await _filled_stream(stream_client, ["a"], close=False) + _, result = await _run(stream_client, CloseTwiceWorkflow, open_ref.id) + assert result == "closed" + assert await _status(stream_client, open_ref) == StreamStatus.StreamStatusClosed + + +async def test_close_without_a_cache_and_in_replays(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, [], close=False) + handle, _ = await _run( + stream_client, AppendMaybeCloseWorkflow, ref.id, True, max_cached_workflows=0 + ) + assert [record.body for record in await _read_all(stream_client, ref)] == ["a"] + assert await _status(stream_client, ref) == StreamStatus.StreamStatusClosed + history = await handle.fetch_history() + assert [entry.resolved for entry in _close_entries(history)] == [False, True] + # A Store with the same Stream, still open, shows that a replay closes nothing. + replay_store = MemoryStreamStore() + replay_client = _stream_client(client, replay_store) + await replay_client.create_stream(id=ref.id, body_type=str) + await Replayer( + workflows=[AppendMaybeCloseWorkflow], + stream_config=replay_client.config()["stream_config"], + ).replay_workflow(history) + assert await _status(replay_client, ref) == StreamStatus.StreamStatusOpen + # The ref names no Store, so a Replayer with no Stream config takes it from History. + await Replayer(workflows=[AppendMaybeCloseWorkflow]).replay_workflow(history) + + +@workflow.defn +class CloseNotAwaitedWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + # The task sends the close command, and the Workflow returns at once. + asyncio.create_task(stream.close()) + await asyncio.sleep(0) + return "returned" + + +async def test_close_not_awaited_closes_after_the_workflow_ends(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a"], close=False) + handle, result = await _run(stream_client, CloseNotAwaitedWorkflow, ref.id) + assert result == "returned" + + async def closed() -> bool: + return await _status(stream_client, ref) == StreamStatus.StreamStatusClosed + + for _ in range(50): + if await closed(): + break + await asyncio.sleep(0.1) + assert await closed() + # The task that ends the Workflow has the intent, and no task gives a result. + entries = _close_entries(await handle.fetch_history()) + assert [entry.resolved for entry in entries] == [False] + + +async def test_shutdown_waits_for_a_close_not_awaited(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a"], close=False) + async with new_worker(stream_client, CloseNotAwaitedWorkflow) as worker: + result = await stream_client.execute_workflow( + CloseNotAwaitedWorkflow.run, + ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert result == "returned" + # A graceful shutdown returns only after the Store answered the close in flight, so + # the Stream is closed with no wait here. + assert await _status(stream_client, ref) == StreamStatus.StreamStatusClosed + + +@activity.defn +async def sleep_a_while() -> None: + await asyncio.sleep(2) + + +@workflow.defn +class CancelDuringCloseWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + # The Local Activity holds the task of the close open, so the cancel comes in the + # task that gives the result of the close. + workflow.start_local_activity( + sleep_a_while, start_to_close_timeout=timedelta(seconds=10) + ) + try: + await stream.close() + except asyncio.CancelledError: + return "cancelled" + return "closed" + + +async def test_cancel_while_the_close_waits(client: Client): + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a"], close=False) + async with new_worker( + stream_client, CancelDuringCloseWorkflow, activities=[sleep_a_while] + ) as worker: + handle = await stream_client.start_workflow( + CancelDuringCloseWorkflow.run, + ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + await asyncio.sleep(0.5) + await handle.cancel() + result = await asyncio.wait_for(handle.result(), timeout=30) + # The code gets the cancel, and Core still closed the Stream after the task of the + # intent. + assert result == "cancelled" + assert await _status(stream_client, ref) == StreamStatus.StreamStatusClosed + + +async def test_python_store_refuses_a_close(client: Client): + stream_client = _stream_client(client, DictStreamStore()) + ref = await _filled_stream(stream_client, [], close=False) + cause = await _python_store_failure(stream_client, PythonStoreCloseWorkflow, ref) + assert cause.message == ( + "The Stream Store 'dict' has no Direct close, since it is written in Python. " + "Close the Stream in an Activity, or use a Store that runs on the bridge, " + "such as the Redis Store or MemoryStreamStore." + ) + assert await _status(stream_client, ref) == StreamStatus.StreamStatusOpen + + +# The metrics of the Direct calls. + + +@workflow.defn +class ReadAppendCloseWorkflow: + @workflow.run + async def run(self, source_id: str, out_id: str) -> list[str]: + source = workflow.get_stream(StreamRef(id=source_id), body_type=str) + out = workflow.get_stream(StreamRef(id=out_id), body_type=str) + got = [record.body async for record in source.iter_read_items()] + await out.append(*got) + await out.close() + return got + + +async def test_direct_calls_record_their_metrics(env: WorkflowEnvironment): + buffer = MetricBuffer(10000) + runtime = Runtime(telemetry=TelemetryConfig(metrics=buffer)) + client = await env.connect_client(runtime=runtime) + store = MemoryStreamStore(name="metered", runtime=runtime) + stream_client = _stream_client(client, store) + source = await _filled_stream(stream_client, ["a", "b", "c"]) + out = await _filled_stream(stream_client, [], close=False) + async with new_worker(stream_client, ReadAppendCloseWorkflow) as worker: + result = await stream_client.execute_workflow( + ReadAppendCloseWorkflow.run, + args=[source.id, out.id], + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert result == ["a", "b", "c"] + updates = [ + update + for update in buffer.retrieve_updates() + if update.metric.name.startswith("temporal_stream_") + ] + print("\nStream metrics:", sorted({update.metric.name for update in updates})) + + def values(name: str) -> list[Any]: + return [update.value for update in updates if update.metric.name == name] + + assert sum(values("temporal_stream_read_total")) == 1 + assert sum(values("temporal_stream_read_records")) == 3 + assert sum(values("temporal_stream_append_total")) == 1 + assert sum(values("temporal_stream_close_total")) == 1 + assert len(values("temporal_stream_append_latency")) == 1 + assert len(values("temporal_stream_close_latency")) == 1 + assert values("temporal_stream_read_failed") == [] + assert values("temporal_stream_append_failed") == [] + assert values("temporal_stream_close_failed") == [] + for update in updates: + assert update.attributes["stream_store"] == "metered" + assert update.attributes["workflow_type"] == "ReadAppendCloseWorkflow" + assert update.attributes["task_queue"] == worker.task_queue + + +# The example of the README: a Workflow starts each tool as its call arrives. + + +@dataclass +class ToolCall: + name: str + argument: str + + +@dataclass +class ModelEvent: + kind: str + text: str | None = None + tool_call: ToolCall | None = None + + +_MODEL_EVENTS = [ + ModelEvent(kind="text", text="Let me look."), + ModelEvent(kind="tool_call", tool_call=ToolCall(name="weather", argument="Oslo")), + ModelEvent(kind="text", text="And the time."), + ModelEvent(kind="tool_call", tool_call=ToolCall(name="clock", argument="Oslo")), + ModelEvent(kind="text", text="Done."), +] + + +@activity.defn +async def model_answer(events_id: str) -> None: + events = activity.client().get_stream(StreamRef(id=events_id), body_type=ModelEvent) + for event in _MODEL_EVENTS: + await events.append(event) + # The model takes a while between its events, so a tool can start before it ends. + await asyncio.sleep(0.5) + await events.close() + + +@activity.defn +async def run_tool(call: ToolCall) -> str: + return f"{call.name}({call.argument})" + + +@workflow.defn +class ToolCallsWorkflow: + @workflow.run + async def run(self, events_id: str, status_id: str) -> list[str]: + model = workflow.start_activity( + model_answer, events_id, start_to_close_timeout=timedelta(seconds=30) + ) + events = workflow.get_stream(StreamRef(id=events_id), body_type=ModelEvent) + status = workflow.get_stream(StreamRef(id=status_id), body_type=str) + tools = [] + async for record in events.iter_read_items(wait=timedelta(seconds=10)): + event = record.body + if event.kind == "tool_call": + assert event.tool_call is not None + # The tool starts now, before the model ends its answer. + tools.append( + workflow.start_activity( + run_tool, + event.tool_call, + start_to_close_timeout=timedelta(minutes=1), + ) + ) + await status.append(f"Running {event.tool_call.name}") + results = await asyncio.gather(*tools) + await model + return list(results) + + +async def test_workflow_starts_each_tool_as_its_call_arrives(client: Client): + stream_client = _stream_client(client) + events = await _filled_stream(stream_client, [], close=False) + status = await _filled_stream(stream_client, [], close=False) + handle, results = await _run( + stream_client, + ToolCallsWorkflow, + events.id, + status.id, + activities=[model_answer, run_tool], + ) + assert results == ["weather(Oslo)", "clock(Oslo)"] + assert [record.body for record in await _read_all(stream_client, status)] == [ + "Running weather", + "Running clock", + ] + # The first tool was scheduled while the model Activity still ran. + history = await handle.fetch_history() + scheduled: dict[int, str] = {} + first_tool = None + model_completed = None + for event in history.events: + if event.event_type == EventType.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED: + name = event.activity_task_scheduled_event_attributes.activity_type.name + scheduled[event.event_id] = name + if name == "run_tool" and first_tool is None: + first_tool = event.event_id + elif event.event_type == EventType.EVENT_TYPE_ACTIVITY_TASK_COMPLETED: + attributes = event.activity_task_completed_event_attributes + if scheduled[attributes.scheduled_event_id] == "model_answer": + model_completed = event.event_id + assert first_tool is not None and model_completed is not None + assert first_tool < model_completed + + +# The read-ahead bound. + + +def test_read_ahead_bytes_below_one_raises(): + for bytes_ in (0, -1): + with pytest.raises(ValueError, match="workflow_read_ahead_bytes"): + StreamConfig(stores=[MemoryStreamStore()], workflow_read_ahead_bytes=bytes_) + + +async def test_read_with_a_read_ahead_of_one_byte_gives_every_record(client: Client): + config = client.config() + config["stream_config"] = StreamConfig( + stores=[MemoryStreamStore()], workflow_read_ahead_bytes=1 + ) + stream_client = Client(**config) + bodies = [f"record-{index}" for index in range(20)] + ref = await _filled_stream(stream_client, bodies) + _, result = await _run(stream_client, ReadPagesWorkflow, ReadArgs(ref.id)) + assert result.bodies == bodies + assert result.dones[-1] is True + + +# The cap on the Records of one page that Workflow code gets. + + +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_store_page_over_256_records_comes_in_pages_of_at_most_256( + client: Client, max_cached_workflows: int +): + stream_client = _stream_client(client) + bodies = [f"record-{index}" for index in range(600)] + ref = await _filled_stream(stream_client, bodies) + handle, got = await _run( + stream_client, + ReadItemsWorkflow, + ref, + None, + max_cached_workflows=max_cached_workflows, + ) + assert got == bodies + history = await handle.fetch_history() + counts = [ + count + for task in _read_entries(history) + for entry in task + for count in entry.page_counts + ] + assert sum(counts) == 600 + assert max(counts) == 256 + config = stream_client.config()["stream_config"] + await Replayer(workflows=[ReadItemsWorkflow], stream_config=config).replay_workflow( + history + ) + + +# A replay that lost the Records of a read. + + +class _ReadThenWait: + def __init__(self) -> None: + self.go = False + + async def read_then_wait(self, stream_id: str) -> list[str]: + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + got = [record.body async for record in stream.iter_read_items()] + await workflow.wait_condition(lambda: self.go) + return got + + @workflow.signal + def proceed(self) -> None: + self.go = True + + +@workflow.defn +class ReadThenWaitWorkflow(_ReadThenWait): + @workflow.run + async def run(self, stream_id: str) -> list[str]: + return await self.read_then_wait(stream_id) + + +@workflow.defn(failure_exception_types=[StreamReplayError]) +class ReadThenWaitFailsWorkflow(_ReadThenWait): + @workflow.run + async def run(self, stream_id: str) -> list[str]: + return await self.read_then_wait(stream_id) + + +async def _replay_with_records_gone( + client: Client, workflow_type: type[Any], **worker_options: Any +) -> WorkflowHandle[Any, Any]: + """Read on one Worker, then give the run to a Worker whose Store lost the Records.""" + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a", "b"]) + task_queue = f"tq-{uuid.uuid4()}" + # No cache, so the next Worker replays the History from the start. + async with new_worker( + stream_client, workflow_type, task_queue=task_queue, max_cached_workflows=0 + ): + handle: WorkflowHandle[Any, Any] = await stream_client.start_workflow( + workflow_type.run, + ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=task_queue, + ) + + async def read_is_in_history() -> None: + assert "core_stream" in _markers(await handle.fetch_history()) + + await assert_eventually(read_is_in_history) + # A new memory Store of the same name has none of the Records. + lost_client = _stream_client(client) + worker = new_worker( + lost_client, workflow_type, task_queue=task_queue, **worker_options + ) + async with worker: + await handle.signal("proceed") + if workflow_type is ReadThenWaitWorkflow and not worker_options: + await assert_task_fail_eventually( + handle, message_contains=f"StreamReplayError: Stream `{ref.id}`" + ) + await handle.terminate() + return handle + with pytest.raises(WorkflowFailureError) as err: + await asyncio.wait_for(handle.result(), timeout=30) + cause = err.value.cause + assert isinstance(cause, ApplicationError) + assert cause.type == "StreamReplayError" + assert f"StreamReplayError: Stream `{ref.id}`" in cause.message + return handle + + +async def test_lost_records_fail_the_task_by_default(client: Client): + await _replay_with_records_gone(client, ReadThenWaitWorkflow) + + +async def test_lost_records_fail_the_workflow_with_the_worker_option(client: Client): + await _replay_with_records_gone( + client, + ReadThenWaitWorkflow, + workflow_failure_exception_types=[StreamReplayError], + ) + + +async def test_lost_records_fail_the_workflow_with_the_defn_option(client: Client): + await _replay_with_records_gone(client, ReadThenWaitFailsWorkflow) + + +# A Workflow outbound interceptor sees each page that Workflow code reads. + +_intercepted: list[tuple[str, Any]] = [] + + +class StreamCallInterceptor(Interceptor): + def workflow_interceptor_class( + self, input: WorkflowInterceptorClassInput + ) -> type[WorkflowInboundInterceptor]: + return _StreamCallInbound + + +class _StreamCallInbound(WorkflowInboundInterceptor): + def init(self, outbound: WorkflowOutboundInterceptor) -> None: + super().init(_StreamCallOutbound(outbound)) + + +class _StreamCallOutbound(WorkflowOutboundInterceptor): + async def start_system_nexus_operation( + self, input: StartNexusOperationInput[Any, Any] + ) -> workflow.NexusOperationHandle[Any]: + _intercepted.append((input.operation_name, input.input)) + return await super().start_system_nexus_operation(input) + + +async def test_workflow_interceptor_sees_each_page_read(client: Client): + _intercepted.clear() + stream_client = _stream_client(client) + ref = await _filled_stream(stream_client, ["a", "b", "c", "d", "e"]) + async with new_worker( + stream_client, ReadPagesWorkflow, interceptors=[StreamCallInterceptor()] + ) as worker: + result = await stream_client.execute_workflow( + ReadPagesWorkflow.run, + ReadArgs(ref.id, max_records=2), + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert result.page_sizes == [2, 2, 1] + assert [name for name, _ in _intercepted] == ["ReadRecords"] * 3 + # Each page starts after the last Record of the page before it. + assert [request.from_offset for _, request in _intercepted] == [ + None, + bytes.fromhex(result.offsets[1]), + bytes.fromhex(result.offsets[3]), + ] + + +# A Store outage that Core tries again, made with a proxy in front of Redis. + +_REDIS_URL = os.environ.get("STREAMS_REDIS_URL") + +_needs_redis = pytest.mark.skipif( + not _REDIS_URL, reason="STREAMS_REDIS_URL is not set to a Redis server" +) + + +async def _copy(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None: + try: + while data := await reader.read(65536): + writer.write(data) + await writer.drain() + except ConnectionError: + pass + + +class _StoreOutage: + """A TCP proxy in front of Redis that a test takes down and brings up. + + Down, it drops each open connection and each new one, so each call of the Store + fails with `StorageUnavailable`. Redis itself keeps running, so the tests that + share it in other processes see no outage. `refused` counts the connections that + it dropped while down. + """ + + def __init__(self, url: str) -> None: + self._parts = urllib.parse.urlsplit(url) + self._server: asyncio.Server | None = None + self._writers: set[asyncio.StreamWriter] = set() + self._down = False + self.url = "" + self.refused = 0 + + async def __aenter__(self) -> Self: + self._server = await asyncio.start_server(self._serve, "127.0.0.1", 0) + port = self._server.sockets[0].getsockname()[1] + credentials, at, _ = self._parts.netloc.rpartition("@") + netloc = f"{credentials}{at}127.0.0.1:{port}" + self.url = urllib.parse.urlunsplit(self._parts._replace(netloc=netloc)) + return self + + async def __aexit__(self, *_: object) -> None: + self.take_down() + if self._server: + self._server.close() + + def take_down(self) -> None: + self._down = True + for writer in list(self._writers): + writer.close() + + def bring_up(self) -> None: + self._down = False + + async def _serve( + self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter + ) -> None: + if self._down: + self.refused += 1 + writer.close() + return + try: + redis_reader, redis_writer = await asyncio.open_connection( + self._parts.hostname or "127.0.0.1", self._parts.port or 6379 + ) + except OSError: + writer.close() + return + self._writers.update((writer, redis_writer)) + copies = [ + asyncio.create_task(_copy(reader, redis_writer)), + asyncio.create_task(_copy(redis_reader, writer)), + ] + try: + await asyncio.wait(copies, return_when=asyncio.FIRST_COMPLETED) + finally: + for each in (writer, redis_writer): + self._writers.discard(each) + each.close() + await asyncio.gather(*copies) + + +def _redis_client(client: Client, url: str) -> Client: + from temporalio.contrib.redis.streams import RedisStreamStore + + # A short response timeout keeps a call that the outage cuts from waiting long. + store = RedisStreamStore( + url, + key_prefix=f"test-{uuid.uuid4()}", + response_timeout=timedelta(seconds=1), + ) + return _stream_client(client, store) + + +async def _store_answers(stream: Any) -> None: + # A client call has no retry, so the test waits until the Store answers again. + try: + await stream.describe() + except StreamError as err: + raise AssertionError(f"The Store doesn't answer: {err}") from err + + +@_needs_redis +async def test_append_during_a_short_outage_appends_once(client: Client): + assert _REDIS_URL + async with _StoreOutage(_REDIS_URL) as outage: + stream_client = _redis_client(client, outage.url) + ref = await _filled_stream(stream_client, [], close=False) + stream = stream_client.get_stream(ref, body_type=str) + async with new_worker( + stream_client, AppendWorkflow, max_cached_workflows=0 + ) as worker: + outage.take_down() + handle: WorkflowHandle[Any, Any] = await stream_client.start_workflow( + AppendWorkflow.run, + ref, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + # The client gets the error that Core keeps from Workflow code. + with pytest.raises(StreamError) as err: + await stream.describe() + assert ( + err.value.reason + == StreamErrorReason.STREAM_ERROR_REASON_STORAGE_UNAVAILABLE + ) + + async def task_started() -> None: + events = (await handle.fetch_history()).events + assert any( + event.event_type == EventType.EVENT_TYPE_WORKFLOW_TASK_STARTED + for event in events + ) + + await assert_eventually(task_started) + refused = outage.refused + await asyncio.sleep(1) + # The task waits for the append, which Core tries again. + assert outage.refused > refused + assert _completed_tasks(await handle.fetch_history()) == 0 + outage.bring_up() + offsets = await asyncio.wait_for(handle.result(), timeout=30) + records = await _read_all(stream_client, ref) + assert [record.body for record in records] == ["a", "b"] + assert offsets == [record.offset.hex() for record in records] + + +@workflow.defn +class ReadAcrossAnOutageWorkflow: + def __init__(self) -> None: + self._bodies: list[str] = [] + + @workflow.run + async def run(self, ref: StreamRef[str]) -> list[str]: + stream = workflow.get_stream(ref, body_type=str) + async for record in stream.iter_read_items(wait=timedelta(seconds=10)): + self._bodies.append(record.body) + return self._bodies + + @workflow.query + def bodies(self) -> list[str]: + return self._bodies + + +@_needs_redis +async def test_read_across_a_short_outage_gives_each_record_once(client: Client): + assert _REDIS_URL + async with _StoreOutage(_REDIS_URL) as outage: + stream_client = _redis_client(client, outage.url) + ref = await _filled_stream(stream_client, ["a", "b", "c"], close=False) + stream = stream_client.get_stream(ref, body_type=str) + async with new_worker(stream_client, ReadAcrossAnOutageWorkflow) as worker: + handle = await stream_client.start_workflow( + ReadAcrossAnOutageWorkflow.run, + ref, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + + async def read_three() -> None: + bodies = await handle.query(ReadAcrossAnOutageWorkflow.bodies) + assert bodies == ["a", "b", "c"] + + await assert_eventually(read_three) + # The outage cuts the read that waits for the next Record. + outage.take_down() + await asyncio.sleep(1.5) + # Only the Worker's read asks the Store in the outage. + assert outage.refused > 0 + outage.bring_up() + await assert_eventually(lambda: _store_answers(stream)) + await stream.append("d", "e") + await stream.close() + result = await asyncio.wait_for(handle.result(), timeout=30) + assert result == ["a", "b", "c", "d", "e"] + + +@_needs_redis +@pytest.mark.parametrize("opt_in", ["worker", "defn"]) +async def test_replay_with_its_store_down_fails_only_the_task( + client: Client, opt_in: str +): + assert _REDIS_URL + workflow_type: type[Any] = ( + ReadThenWaitFailsWorkflow if opt_in == "defn" else ReadThenWaitWorkflow + ) + worker_options: dict[str, Any] = ( + {"workflow_failure_exception_types": [StreamReplayError]} + if opt_in == "worker" + else {} + ) + async with _StoreOutage(_REDIS_URL) as outage: + stream_client = _redis_client(client, outage.url) + ref = await _filled_stream(stream_client, ["a", "b"]) + task_queue = f"tq-{uuid.uuid4()}" + # No cache, so the next Worker replays the History and reads the Store. + async with new_worker( + stream_client, workflow_type, task_queue=task_queue, max_cached_workflows=0 + ): + handle: WorkflowHandle[Any, Any] = await stream_client.start_workflow( + workflow_type.run, + ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=task_queue, + ) + + async def read_is_in_history() -> None: + assert "core_stream" in _markers(await handle.fetch_history()) + + await assert_eventually(read_is_in_history) + outage.take_down() + async with new_worker( + stream_client, workflow_type, task_queue=task_queue, **worker_options + ): + await handle.signal("proceed") + + async def task_failed() -> None: + messages = [ + event.workflow_task_failed_event_attributes.failure.message + for event in (await handle.fetch_history()).events + if event.event_type == EventType.EVENT_TYPE_WORKFLOW_TASK_FAILED + ] + assert messages + assert messages[0].startswith( + f"StreamReplayUnavailable: Stream `{ref.id}` in Store `redis`" + ), messages[0] + + # Core tries the Store for 5 s before it fails the task. + await assert_eventually(task_failed, timeout=timedelta(seconds=30)) + # The opt-in doesn't fail the Workflow, since the Records may still be there. + description = await handle.describe() + assert description.status == WorkflowExecutionStatus.RUNNING + outage.bring_up() + result = await asyncio.wait_for(handle.result(), timeout=60) + assert result == ["a", "b"] diff --git a/tests/nexus/test_stream_store.py b/tests/nexus/test_stream_store.py index f9a582519f..fa7aa0af00 100644 --- a/tests/nexus/test_stream_store.py +++ b/tests/nexus/test_stream_store.py @@ -20,6 +20,7 @@ import temporalio.client._stream_store import temporalio.common import temporalio.converter._payload_converter +import temporalio.exceptions import temporalio.nexus.system import temporalio.nexus.system._support import temporalio.workflow @@ -33,7 +34,7 @@ ) from temporalio.client._stream_store import _STREAM_OPERATIONS, _request_ref from temporalio.converter import DataConverter, StorageDriverActivityInfo -from temporalio.exceptions import StreamConfigError, StreamError +from temporalio.exceptions import ApplicationError, StreamConfigError, StreamError from temporalio.nexus.system.stream_service import ( ClientProducer, DescribeStreamRequest, @@ -662,3 +663,32 @@ def test_stream_error_carries_its_reason(): assert StreamError("x").reason == ( StreamErrorReason.STREAM_ERROR_REASON_UNSPECIFIED ) + + +def test_stream_error_from_application_error(): + from_failure = temporalio.exceptions._stream_error_from_failure + rebuilt = from_failure( + ApplicationError( + "the Stream exists", + temporalio.api.stream.v1.StreamErrorDetails( + reason=StreamErrorReason.STREAM_ERROR_REASON_ALREADY_EXISTS + ), + type="StreamError", + ) + ) + assert isinstance(rebuilt, StreamError) + assert rebuilt.reason == StreamErrorReason.STREAM_ERROR_REASON_ALREADY_EXISTS + assert rebuilt.message == "the Stream exists" + unknown = from_failure( + ApplicationError( + "x", + temporalio.api.stream.v1.StreamErrorDetails( + reason=StreamErrorReason.ValueType(999) + ), + type="StreamError", + ) + ) + assert unknown is not None + assert unknown.reason == StreamErrorReason.STREAM_ERROR_REASON_UNSPECIFIED + assert from_failure(ApplicationError("x", type="RuntimeError")) is None + assert from_failure(RuntimeError("x")) is None diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index c1df44daaa..7ee8ca22a7 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -1,84 +1,251 @@ from __future__ import annotations +import asyncio import uuid +from collections.abc import Callable, Sequence from datetime import timedelta +from typing import Any import pytest +from google.protobuf.message import Message +import temporalio.common +import temporalio.exceptions from temporalio import activity, workflow -from temporalio.client import Client +from temporalio.api.enums.v1 import EventType, StreamErrorReason +from temporalio.api.streamservice.v1 import ( + CreateStreamRequest as CreateStreamRequestMessage, +) +from temporalio.api.streamservice.v1 import ( + CreateStreamResponse as CreateStreamResponseMessage, +) +from temporalio.api.streamservice.v1 import ( + DescribeStreamRequest as DescribeStreamRequestMessage, +) +from temporalio.api.streamservice.v1 import ( + DescribeStreamResponse as DescribeStreamResponseMessage, +) +from temporalio.client import ( + Client, + StreamConfig, + StreamSelectContext, + StreamStore, + StreamStoreCallOptions, + WorkflowFailureError, + WorkflowHandle, +) +from temporalio.common import RetryPolicy +from temporalio.converter import DataConverter, StorageDriverWorkflowInfo +from temporalio.exceptions import ApplicationError, StreamError from temporalio.nexus.system import TEMPORAL_SYSTEM_ENDPOINT from temporalio.nexus.system.stream_service import ( + AppendRecordsResponse, + ClientProducer, Producer, + ProducerKindClient, ProducerKindLocalActivity, + ProducerKindWorkflow, ReadRecordsRequest, + StreamIdConflictPolicy, StreamRecord, StreamRef, StreamService, + StreamStatus, ) from temporalio.nexus.system.stream_service.workflow import create_stream, get_stream +from temporalio.nexus.system.stream_service.workflow._resources.stream import ( + Stream as WorkflowStream, +) +from temporalio.testing import MemoryStreamStore +from temporalio.worker import ( + ActivityInboundInterceptor, + ExecuteActivityInput, + Interceptor, + Replayer, + Worker, +) from tests.helpers import assert_task_fail_eventually, new_worker from tests.helpers.streams import DictStreamStore, RecordingStreamStore -from tests.nexus.test_stream_store import _with_store +from tests.test_converter import SimpleCodec +# Workflow code writes these values, so the History shows only whether a Record body +# leaks into it. _BODIES = ["first-record-body", "second-record-body"] +@activity.defn +async def append_values(ref: StreamRef[str], values: list[str]) -> None: + await activity.client().get_stream(ref, body_type=str).append(*values) + + +async def _append_in_local_activity(ref: StreamRef[str], values: list[str]) -> None: + await workflow.execute_local_activity( + append_values, args=[ref, values], start_to_close_timeout=timedelta(seconds=10) + ) + + @workflow.defn -class StreamCallWorkflow: +class AppendWorkflow: @workflow.run - async def run(self, operation: str) -> None: - stream = get_stream(StreamRef(id="stream"), body_type=str) - if operation == "CreateStream": - await create_stream(id="stream", body_type=str) - elif operation == "AppendRecords": + async def run(self, stream_id: str) -> StreamRef[str]: + stream = await create_stream(id=stream_id, body_type=str) + await stream.append(*_BODIES) + return stream.ref + + +@workflow.defn +class LocalActivityAppendWorkflow: + @workflow.run + async def run(self, stream_id: str) -> StreamRef[str]: + stream = await create_stream(id=stream_id, body_type=str) + await _append_in_local_activity(stream.ref, _BODIES) + return stream.ref + + +# Workflow code appends either with the Workflow `Stream` or with a Local Activity of +# its own. Core makes the first append in the Workflow Task as the Workflow, and the +# second writes as the Local Activity that runs it. +_APPEND_WORKFLOWS = pytest.mark.parametrize( + "append_workflow", [AppendWorkflow, LocalActivityAppendWorkflow] +) + + +@workflow.defn +class AppendOffsetsWorkflow: + @workflow.run + async def run(self, stream_id: str) -> AppendRecordsResponse: + stream = await create_stream(id=stream_id, body_type=str) + return await stream.append(*_BODIES) + + +@workflow.defn +class AppendToStreamWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = get_stream(StreamRef(id=stream_id), body_type=str) + try: await stream.append("a") - elif operation == "CloseStream": - await stream.close() - elif operation == "DescribeStream": - await stream.describe() - else: - # The Workflow `Stream` has no `read`, but a Nexus client can start one. - nexus_client = workflow.create_nexus_client( - service=StreamService, endpoint=TEMPORAL_SYSTEM_ENDPOINT - ) - await nexus_client.start_operation( - operation, ReadRecordsRequest(ref=StreamRef[str](id="stream")) + except StreamError as err: + assert isinstance(err, ApplicationError) + return StreamErrorReason.Name(err.reason) + return "appended" + + +@workflow.defn +class ExplicitProducerWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = await create_stream(id=stream_id, body_type=str) + try: + await stream.append( + "a", + producer=Producer(id="mine", kind=ProducerKindClient(ClientProducer())), ) + except StreamError as err: + return f"{StreamErrorReason.Name(err.reason)}: {err.message}" + return "appended" -@pytest.mark.parametrize( - "operation", - ["CreateStream", "AppendRecords", "CloseStream", "DescribeStream", "ReadRecords"], -) -async def test_workflow_stream_call_fails_the_workflow_task( - client: Client, operation: str -): - store = RecordingStreamStore(DictStreamStore()) - stream_client = _with_store(client, store) - async with new_worker(stream_client, StreamCallWorkflow) as worker: - handle = await stream_client.start_workflow( - StreamCallWorkflow.run, - operation, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - execution_timeout=timedelta(seconds=30), - ) - await assert_task_fail_eventually( - handle, - message_contains=( - "Workflow code can't call a Stream in this version. Call it from an " - "Activity through activity.client()." - ), +@workflow.defn +class NexusClientReadWorkflow: + @workflow.run + async def run(self, operation: str) -> str: + # Workflow code can also start the operation of `read` through a Nexus client. + requests: dict[str, Any] = { + "ReadRecords": ReadRecordsRequest(ref=StreamRef[str](id="stream")), + } + nexus_client = workflow.create_nexus_client( + service=StreamService, endpoint=TEMPORAL_SYSTEM_ENDPOINT ) - await handle.terminate() - # The Store of the Worker's client gets no call. - assert store.operations == [] + try: + handle: workflow.NexusOperationHandle[ + Any + ] = await nexus_client.start_operation(operation, requests[operation]) + await handle + except StreamError as err: + return StreamErrorReason.Name(err.reason) + except ApplicationError as err: + assert err.non_retryable + return err.message + return "read" + + +@workflow.defn +class DescribeResultWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = await create_stream(id=stream_id, body_type=str) + try: + await stream.describe() + except StreamError as err: + return f"StreamError {StreamErrorReason.Name(err.reason)}: {err.message}" + except ApplicationError as err: + return f"ApplicationError {err.type}: {err.message}" + except temporalio.exceptions.TimeoutError as err: + return f"TimeoutError {err.type and err.type.name}" + return "described" + + +@workflow.defn +class DescribeStreamWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + stream = get_stream(StreamRef(id=stream_id), body_type=str) + try: + await stream.describe() + except StreamError as err: + assert isinstance(err, ApplicationError) + return StreamErrorReason.Name(err.reason) + return "described" + + +@workflow.defn +class CreateThenContinueAsNewWorkflow: + @workflow.run + async def run(self, stream_id: str, run: int) -> list[bool]: + stream = await create_stream(id=stream_id, body_type=str) + await _append_in_local_activity(stream.ref, [f"run-{run}"]) + if run == 1: + workflow.continue_as_new(args=[stream_id, 2]) + return [bool(stream.created)] + + +@workflow.defn +class DescribeOtherStoreWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + await get_stream( + StreamRef(id=stream_id, store="other"), body_type=str + ).describe() + + +@workflow.defn +class CreateAndDescribeWorkflow: + @workflow.run + async def run(self, stream_id: str) -> StreamRef[str]: + stream = await create_stream(id=stream_id, body_type=str) + await stream.describe() + return stream.ref + + +class CountingStore(RecordingStreamStore): + def __init__(self, name: str) -> None: + super().__init__(MemoryStreamStore(name=name)) @activity.defn -async def append_values(ref: StreamRef[str], values: list[str]) -> None: - await activity.client().get_stream(ref, body_type=str).append(*values) +async def user_activity() -> None: + pass + + +@workflow.defn +class CreateAndActivityWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + await create_stream(id=stream_id, body_type=str) + await workflow.execute_local_activity( + user_activity, start_to_close_timeout=timedelta(seconds=10) + ) _fail_next_task = True @@ -87,12 +254,16 @@ async def append_values(ref: StreamRef[str], values: list[str]) -> None: @workflow.defn(sandboxed=False) class AppendThenFailTaskOnceWorkflow: @workflow.run - async def run(self, ref: StreamRef[str]) -> None: - await workflow.execute_local_activity( - append_values, - args=[ref, _BODIES], - start_to_close_timeout=timedelta(seconds=10), + async def run(self, stream_id: str, in_local_activity: bool) -> None: + stream = await create_stream( + id=stream_id, + body_type=str, + id_conflict_policy=StreamIdConflictPolicy.StreamIdConflictPolicyUseExisting, ) + if in_local_activity: + await _append_in_local_activity(stream.ref, _BODIES) + else: + await stream.append(*_BODIES) global _fail_next_task if _fail_next_task: _fail_next_task = False @@ -116,13 +287,145 @@ async def run(self, stream_id: str) -> None: ) +class FlakyDescribeStore(RecordingStreamStore): + def __init__(self, *, failures: int = 0, delay: timedelta = timedelta()) -> None: + super().__init__(MemoryStreamStore()) + self._failures = failures + self._delay = delay + self.describes = 0 + + async def before_call(self, operation: str, request: Message) -> None: + if operation == "DescribeStream": + self.describes += 1 + await asyncio.sleep(self._delay.total_seconds()) + if self.describes <= self._failures: + raise RuntimeError("describe failed") + + +class FlakyCreateStore(CountingStore): + def __init__(self, name: str) -> None: + super().__init__(name) + self.failed_creates = 0 + + async def before_call(self, operation: str, request: Message) -> None: + if operation == "CreateStream" and not self.failed_creates: + self.failed_creates += 1 + raise RuntimeError("create failed") + + +@workflow.defn +class CloseWorkflow: + @workflow.run + async def run(self, stream_id: str) -> StreamRef[str]: + stream = await create_stream(id=stream_id, body_type=str) + await stream.append("a") + await stream.close() + return stream.ref + + +@workflow.defn +class AsyncWithCloseWorkflow: + @workflow.run + async def run(self, stream_id: str) -> str: + async with create_stream(id=stream_id, body_type=str) as stream: + await stream.append("a") + described = await stream.describe() + assert described.status is not None + return described.status.name + + +_fail_close_intent_task = True + + +@workflow.defn(sandboxed=False) +class CloseIntentThenFailTaskOnceWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + stream = get_stream(StreamRef(id=stream_id), body_type=str) + await stream.append("a") + close = asyncio.create_task(stream.close()) + # A few turns of the loop let the close send its intent in this task. + for _ in range(10): + await asyncio.sleep(0) + global _fail_close_intent_task + if _fail_close_intent_task: + _fail_close_intent_task = False + raise RuntimeError("Failed the Workflow Task once") + await close + + +class RecordingInterceptor(Interceptor): + def __init__(self) -> None: + self.activity_types: list[str] = [] + self.is_local: list[bool] = [] + + def intercept_activity( + self, next: ActivityInboundInterceptor + ) -> ActivityInboundInterceptor: + return _RecordingActivityInbound(next, self) + + +class _RecordingActivityInbound(ActivityInboundInterceptor): + def __init__( + self, next: ActivityInboundInterceptor, recording: RecordingInterceptor + ) -> None: + super().__init__(next) + self._recording = recording + + async def execute_activity(self, input: ExecuteActivityInput) -> Any: + info = activity.info() + self._recording.activity_types.append(info.activity_type) + self._recording.is_local.append(info.is_local) + return await super().execute_activity(input) + + +def _with_store(client: Client, store: StreamStore) -> Client: + return _with_config(client, StreamConfig(stores=[store])) + + +def _with_config(client: Client, stream_config: StreamConfig) -> Client: + config = client.config() + config["stream_config"] = stream_config + return Client(**config) + + +def _two_counting_stores( + contexts: list[StreamSelectContext], +) -> tuple[CountingStore, CountingStore, StreamConfig]: + first = CountingStore("first") + second = CountingStore("second") + + def select(context: StreamSelectContext) -> StreamStore: + contexts.append(context) + return second + + return first, second, StreamConfig(stores=[first, second], store_selector=select) + + +async def _run_create_and_describe( + client: Client, stream_id: str +) -> tuple[WorkflowHandle[CreateAndDescribeWorkflow, StreamRef[str]], StreamRef[str]]: + async with new_worker(client, CreateAndDescribeWorkflow) as worker: + handle = await client.start_workflow( + CreateAndDescribeWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + return handle, await handle.result() + + async def _read_all(client: Client, stream_id: str) -> list[StreamRecord[str]]: stream = client.get_stream(StreamRef(id=stream_id), body_type=str) return [record async for record in stream.read()] -def _local_activity_epoch(producer: Producer) -> tuple[int, int, int]: - # The Workflow Task, then the attempt of the Local Activity. +def _epoch(producer: Producer) -> tuple[int, int, int]: + # The Workflow Task, then the attempt of the Local Activity, which a Workflow lacks. + if isinstance(producer.kind, ProducerKindWorkflow): + task = producer.kind.value.epoch + assert task is not None + return (task.started_event_id, task.attempt, 0) assert isinstance(producer.kind, ProducerKindLocalActivity) local = producer.kind.value.epoch return (local.task.started_event_id, local.task.attempt, local.attempt) @@ -134,40 +437,360 @@ def _producers(records: list[StreamRecord[str]]) -> list[Producer]: return [producer for producer in producers if producer is not None] -async def test_local_activity_append_after_a_failed_workflow_task_has_a_higher_epoch( +async def _run_append_workflow( + client: Client, append_workflow: type[Any] = AppendWorkflow +) -> tuple[WorkflowHandle[Any, StreamRef[str]], StreamRef[str]]: + async with new_worker( + client, append_workflow, activities=[append_values] + ) as worker: + handle: WorkflowHandle[Any, StreamRef[str]] = await client.start_workflow( + append_workflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + return handle, await handle.result() + + +async def _describe_result( + client: Client, + store: StreamStore, + options: StreamStoreCallOptions | None = None, +) -> str: + stream_config = StreamConfig( + stores=[store], workflow_call_options=options or StreamStoreCallOptions() + ) + stream_client = _with_config(client, stream_config) + async with new_worker(stream_client, DescribeResultWorkflow) as worker: + return await stream_client.execute_workflow( + DescribeResultWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + + +@_APPEND_WORKFLOWS +async def test_workflow_stream_uses_store(client: Client, append_workflow: type[Any]): + stream_client = _with_store(client, MemoryStreamStore()) + handle, ref = await _run_append_workflow(stream_client, append_workflow) + + stream = stream_client.get_stream(ref, body_type=str) + records = [record async for record in stream.iter_read_items()] + assert [record.body for record in records] == _BODIES + run_id = handle.result_run_id + assert run_id + first, second = _producers(records) + assert first == second + if append_workflow is AppendWorkflow: + # The Workflow writes as itself, with its first Workflow Task as the epoch. + assert first.id == f"wf:{run_id}" + assert isinstance(first.kind, ProducerKindWorkflow) + assert first.kind.value.run_id == run_id + assert first.kind.value.epoch is not None + assert first.kind.value.epoch.started_event_id > 0 + assert first.kind.value.epoch.attempt == 1 + return + # The create is the run's first Local Activity, so the append is its second. + assert first.id == f"la:{run_id}:2" + assert isinstance(first.kind, ProducerKindLocalActivity) + assert first.kind.value.run_id == run_id + assert first.kind.value.epoch.task.started_event_id > 0 + assert first.kind.value.epoch.attempt == 1 + + +async def test_workflow_stream_uses_a_store_written_from_scratch(client: Client): + store = DictStreamStore() + stream_client = _with_store(client, store) + stream_id = f"stream-{uuid.uuid4()}" + _, ref = await _run_create_and_describe(stream_client, stream_id) + assert ref == StreamRef(id=stream_id, store="dict") + assert list(store.streams) == [stream_id] + + stream = stream_client.get_stream(ref, body_type=str) + await stream.append("a") + assert [record.body async for record in stream.iter_read_items()] == ["a"] + + +@_APPEND_WORKFLOWS +async def test_workflow_stream_store_uses_payload_codec( + client: Client, append_workflow: type[Any] +): + store = MemoryStreamStore() + config = client.config() + config["data_converter"] = DataConverter(payload_codec=SimpleCodec()) + config["stream_config"] = StreamConfig(stores=[store]) + stream_client = Client(**config) + _, ref = await _run_append_workflow(stream_client, append_workflow) + + # A client without the codec reads the bodies as the Store keeps them. + raw = _with_store(client, store).get_stream( + StreamRef(id=ref.id), body_type=temporalio.common.RawValue + ) + stored = [record.body.payload async for record in raw.iter_read_items()] + assert [body.metadata["simple-codec"] for body in stored] == [b"true", b"true"] + stream = stream_client.get_stream(ref, body_type=str) + assert [record.body async for record in stream.iter_read_items()] == _BODIES + + +@_APPEND_WORKFLOWS +async def test_workflow_stream_history_has_no_bodies( + client: Client, append_workflow: type[Any] +): + stream_client = _with_store(client, MemoryStreamStore()) + handle, _ = await _run_append_workflow(stream_client, append_workflow) + events = (await handle.fetch_history()).events + markers = [ + event.marker_recorded_event_attributes.marker_name + for event in events + if event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED + ] + # One marker for the create and one for the append. Core writes its own marker for + # an append that it makes in the Workflow Task. + append_marker = ( + "core_stream" if append_workflow is AppendWorkflow else "core_local_activity" + ) + assert markers == ["core_local_activity", append_marker] + for event in events: + for body in _BODIES: + assert body.encode() not in event.SerializeToString() + + +@_APPEND_WORKFLOWS +async def test_workflow_stream_replay_does_not_call_store( + client: Client, append_workflow: type[Any] +): + stream_client = _with_store(client, MemoryStreamStore()) + handle, ref = await _run_append_workflow(stream_client, append_workflow) + # A replay of an append that Core made needs a Store that runs on the bridge, as the + # first run had. A new memory Store shows any call, since it has no Stream. + replay_store = MemoryStreamStore() + await Replayer( + workflows=[append_workflow], + stream_config=StreamConfig(stores=[replay_store]), + ).replay_workflow(await handle.fetch_history()) + with pytest.raises(StreamError) as err: + await ( + _with_store(client, replay_store).get_stream(ref, body_type=str).describe() + ) + assert err.value.reason == StreamErrorReason.STREAM_ERROR_REASON_NOT_FOUND + + +async def test_workflow_create_without_stream_config_fails_the_workflow( client: Client, +): + async with new_worker(client, CreateAndDescribeWorkflow) as worker: + handle = await client.start_workflow( + CreateAndDescribeWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + execution_timeout=timedelta(seconds=30), + ) + with pytest.raises(WorkflowFailureError) as err: + await asyncio.wait_for(handle.result(), timeout=10) + cause = err.value.cause + assert isinstance(cause, ApplicationError) + assert not isinstance(cause, StreamError) + assert cause.type == "StreamConfigError" + assert cause.non_retryable + assert cause.message == ( + "The client of this Worker has no Stream Store. Give one in " + "stream_config=StreamConfig(stores=[...])." + ) + # A Replayer with no Stream config fails the Workflow the same way. + await Replayer(workflows=[CreateAndDescribeWorkflow]).replay_workflow( + await handle.fetch_history() + ) + + +async def test_workflow_read_through_a_nexus_client_needs_a_runtime( + client: Client, +): + async def read_with(store: StreamStore) -> str: + stream_client = _with_store(client, store) + async with new_worker(stream_client, NexusClientReadWorkflow) as worker: + return await stream_client.execute_workflow( + NexusClientReadWorkflow.run, + "ReadRecords", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + + # Core reads a Store with a Rust runtime, as for `read`. Nobody created the Stream. + assert await read_with(MemoryStreamStore()) == "STREAM_ERROR_REASON_NOT_FOUND" + assert await read_with(DictStreamStore()) == ( + "The Stream Store 'dict' has no Direct read, since it is written in Python. " + "Read the Stream in an Activity, or use a Store that runs on the bridge, such " + "as the Redis Store or MemoryStreamStore." + ) + + +def test_workflow_stream_has_the_calls_of_a_client_stream(): + for name in ( + "append", + "close", + "describe", + "read", + "iter_read", + "iter_read_items", + "__aenter__", + "__aexit__", + ): + assert hasattr(WorkflowStream, name) + assert not hasattr(WorkflowStream, "iter") + + +async def test_workflow_stream_store_error_reaches_workflow_code(client: Client): + store = FlakyDescribeStore(failures=1) + result = await _describe_result(client, store) + assert result == "ApplicationError RuntimeError: describe failed" + assert store.describes == 1 + + +async def test_workflow_describe_of_unknown_stream_raises_stream_error(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + async with new_worker(stream_client, DescribeStreamWorkflow) as worker: + reason = await stream_client.execute_workflow( + DescribeStreamWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert reason == "STREAM_ERROR_REASON_NOT_FOUND" + + +async def test_strict_create_in_each_run_of_a_chain_succeeds(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker( + stream_client, CreateThenContinueAsNewWorkflow, activities=[append_values] + ) as worker: + handle = await stream_client.start_workflow( + CreateThenContinueAsNewWorkflow.run, + args=[stream_id, 1], + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert await handle.result() == [True] + first_run_id = handle.first_execution_run_id + last_run = await stream_client.get_workflow_handle(handle.id).describe() + assert first_run_id + assert last_run.run_id != first_run_id + records = await _read_all(stream_client, stream_id) + assert [record.body for record in records] == ["run-1", "run-2"] + first, second = _producers(records) + assert isinstance(first.kind, ProducerKindLocalActivity) + assert isinstance(second.kind, ProducerKindLocalActivity) + assert first.kind.value.run_id == first_run_id + assert second.kind.value.run_id == last_run.run_id + + +@_APPEND_WORKFLOWS +async def test_client_append_after_workflow_append_keeps_offset_order( + client: Client, append_workflow: type[Any] +): + stream_client = _with_store(client, MemoryStreamStore()) + _, ref = await _run_append_workflow(stream_client, append_workflow) + stream = stream_client.get_stream(ref, body_type=str) + appended = await stream.append("from-client") + records = await _read_all(stream_client, ref.id) + assert [record.body for record in records] == [*_BODIES, "from-client"] + offsets = [record.offset for record in records] + assert offsets == sorted(offsets) + assert len(set(offsets)) == len(offsets) + assert offsets[-1] == appended.last_offset + kinds = [type(producer.kind) for producer in _producers(records)] + workflow_kind = ( + ProducerKindWorkflow + if append_workflow is AppendWorkflow + else ProducerKindLocalActivity + ) + assert kinds == [workflow_kind, workflow_kind, ProducerKindClient] + + +async def test_workflow_stream_store_call_options_retry(client: Client): + options = StreamStoreCallOptions( + retry_policy=RetryPolicy( + initial_interval=timedelta(milliseconds=10), maximum_attempts=2 + ) + ) + store = FlakyDescribeStore(failures=1) + assert await _describe_result(client, store, options) == "described" + assert store.describes == 2 + + +async def test_workflow_stream_store_call_options_timeout(client: Client): + options = StreamStoreCallOptions(start_to_close_timeout=timedelta(milliseconds=200)) + store = FlakyDescribeStore(delay=timedelta(seconds=5)) + result = await _describe_result(client, store, options) + assert result == "TimeoutError START_TO_CLOSE" + + +async def test_worker_keeps_its_stream_stores(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + async with new_worker(stream_client, AppendWorkflow) as worker: + with pytest.raises(ValueError, match="Stream Stores"): + worker.client = client + worker.client = stream_client + + +async def test_activity_interceptors_see_stream_store_activity(client: Client): + interceptor = RecordingInterceptor() + stream_client = _with_store(client, MemoryStreamStore()) + async with new_worker( + stream_client, + CreateAndActivityWorkflow, + activities=[user_activity], + interceptors=[interceptor], + ) as worker: + await stream_client.execute_workflow( + CreateAndActivityWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + # The create runs as the SDK's Local Activity, before the Workflow's own Activity. + assert interceptor.activity_types == [ + "__temporal_stream_store_call:memory", + "user_activity", + ] + + +@pytest.mark.parametrize("in_local_activity", [False, True]) +async def test_workflow_task_retry_appends_with_higher_epoch( + client: Client, in_local_activity: bool ): global _fail_next_task _fail_next_task = True - stream_client = _with_store(client, DictStreamStore()) - stream = await stream_client.create_stream( - id=f"stream-{uuid.uuid4()}", body_type=str - ) + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" async with new_worker( stream_client, AppendThenFailTaskOnceWorkflow, activities=[append_values] ) as worker: await stream_client.execute_workflow( AppendThenFailTaskOnceWorkflow.run, - stream.ref, + args=[stream_id, in_local_activity], id=f"workflow-{uuid.uuid4()}", task_queue=worker.task_queue, ) - records = await _read_all(stream_client, stream.ref.id) - # The failed Workflow Task records no marker, so its retry runs the Local Activity - # again and writes a second copy. + records = await _read_all(stream_client, stream_id) + # The failed Workflow Task records no marker, so its retry appends again and writes + # a second copy. assert [record.body for record in records] == [*_BODIES, *_BODIES] producers = _producers(records) assert len({producer.id for producer in producers}) == 1 - first, second, third, fourth = ( - _local_activity_epoch(producer) for producer in producers - ) + if not in_local_activity: + assert producers[0].id.startswith("wf:") + + first, second, third, fourth = (_epoch(producer) for producer in producers) assert first == second assert third == fourth assert third > first async def test_user_local_activity_append_has_local_activity_producer(client: Client): - stream_client = _with_store(client, DictStreamStore()) + stream_client = _with_store(client, MemoryStreamStore()) stream = await stream_client.create_stream( id=f"stream-{uuid.uuid4()}", body_type=str ) @@ -190,3 +813,390 @@ async def test_user_local_activity_append_has_local_activity_producer(client: Cl assert producer.kind.value.run_id == run_id assert producer.kind.value.epoch.task.started_event_id > 0 assert producer.kind.value.epoch.attempt == 1 + + +async def test_workflow_ref_of_another_store_fails_the_task(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + async with new_worker(stream_client, DescribeOtherStoreWorkflow) as worker: + handle = await stream_client.start_workflow( + DescribeOtherStoreWorkflow.run, + f"stream-{uuid.uuid4()}", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + execution_timeout=timedelta(seconds=30), + ) + await assert_task_fail_eventually( + handle, message_contains="names the Stream Store 'other'" + ) + await handle.terminate() + + +async def test_workflow_create_without_store_on_several_stores_fails_the_task( + client: Client, +): + stream_config = StreamConfig( + stores=[CountingStore("first"), CountingStore("second")] + ) + stream_client = _with_config(client, stream_config) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, CreateAndDescribeWorkflow) as worker: + handle = await stream_client.start_workflow( + CreateAndDescribeWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + execution_timeout=timedelta(seconds=30), + ) + await assert_task_fail_eventually( + handle, + message_contains=( + "StreamConfig has more than one Store and no store_selector, and the " + f"call on the Stream '{stream_id}' names no Store. Name one with " + "store=, or set StreamConfig.store_selector." + ), + ) + await handle.terminate() + + +async def test_workflow_create_goes_to_the_selected_store(client: Client): + contexts: list[StreamSelectContext] = [] + first, second, stream_config = _two_counting_stores(contexts) + stream_id = f"stream-{uuid.uuid4()}" + handle, ref = await _run_create_and_describe( + _with_config(client, stream_config), stream_id + ) + assert ref.store == "second" + assert first.operations == [] + assert second.operations == ["CreateStream", "DescribeStream"] + # The describe goes by the Store that the ref names, so the selector runs once. + assert contexts == [ + StreamSelectContext( + id=stream_id, + target=StorageDriverWorkflowInfo( + id=handle.id, + type="CreateAndDescribeWorkflow", + run_id=handle.result_run_id, + namespace=client.namespace, + ), + ) + ] + + +async def test_workflow_replay_calls_no_store(client: Client): + _, _, stream_config = _two_counting_stores([]) + stream_id = f"stream-{uuid.uuid4()}" + handle, _ = await _run_create_and_describe( + _with_config(client, stream_config), stream_id + ) + contexts: list[StreamSelectContext] = [] + first, second, replay_config = _two_counting_stores(contexts) + await Replayer( + workflows=[CreateAndDescribeWorkflow], stream_config=replay_config + ).replay_workflow(await handle.fetch_history()) + # The selector runs in Workflow code, so a replay runs it again with the Workflow. + assert [context.id for context in contexts] == [stream_id] + target = contexts[0].target + assert isinstance(target, StorageDriverWorkflowInfo) + assert (target.id, target.run_id) == (handle.id, handle.result_run_id) + assert first.operations == [] + assert second.operations == [] + + +async def test_workflow_create_with_several_stores_takes_the_config_options( + client: Client, +): + first = CountingStore("first") + second = FlakyCreateStore("second") + stream_config = StreamConfig( + stores=[first, second], + store_selector=lambda _: second, + workflow_call_options=StreamStoreCallOptions( + retry_policy=RetryPolicy( + initial_interval=timedelta(milliseconds=10), maximum_attempts=2 + ) + ), + ) + # The default options make one attempt, so only the config's retry policy lets + # the create pass after its first attempt fails. + _, ref = await _run_create_and_describe( + _with_config(client, stream_config), f"stream-{uuid.uuid4()}" + ) + assert ref.store == "second" + assert first.operations == [] + assert second.operations == ["CreateStream", "CreateStream", "DescribeStream"] + + +class OwnActivityStore(CountingStore): + """A Store that brings its own Activity for the calls of Workflow code.""" + + def __init__(self) -> None: + super().__init__("own") + self.workflow_calls: list[str] = [] + + def worker_activities(self) -> Sequence[Callable[..., Any]]: + @activity.defn(name="own_store_call") + async def own_store_call(operation: str, request: bytes) -> bytes: + if operation == "CreateStream": + created = await self.create_stream( + CreateStreamRequestMessage.FromString(request) + ) + return created.SerializeToString() + described = await self.describe_stream( + DescribeStreamRequestMessage.FromString(request) + ) + return described.SerializeToString() + + return [own_store_call] + + async def call_from_workflow( + self, operation: str, request: Message, options: StreamStoreCallOptions + ) -> Message: + self.workflow_calls.append(operation) + response = await workflow.execute_local_activity( + "own_store_call", + args=[operation, request.SerializeToString()], + result_type=bytes, + start_to_close_timeout=options.start_to_close_timeout, + ) + if operation == "CreateStream": + return CreateStreamResponseMessage.FromString(response) + return DescribeStreamResponseMessage.FromString(response) + + +async def test_workflow_call_goes_through_the_store_own_activity(client: Client): + store = OwnActivityStore() + interceptor = RecordingInterceptor() + stream_client = _with_store(client, store) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker( + stream_client, CreateAndDescribeWorkflow, interceptors=[interceptor] + ) as worker: + ref = await stream_client.execute_workflow( + CreateAndDescribeWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert ref == StreamRef(id=stream_id, store="own") + assert store.workflow_calls == ["CreateStream", "DescribeStream"] + assert store.operations == ["CreateStream", "DescribeStream"] + assert interceptor.activity_types == ["own_store_call", "own_store_call"] + + +async def test_workflow_append_returns_offsets_of_records_in_order(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, AppendOffsetsWorkflow) as worker: + appended = await stream_client.execute_workflow( + AppendOffsetsWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + records = await _read_all(stream_client, stream_id) + assert [record.body for record in records] == _BODIES + assert [appended.first_offset, appended.last_offset] == [ + record.offset for record in records + ] + + +async def test_workflow_append_to_closed_stream_raises_stream_error(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream = await stream_client.create_stream( + id=f"stream-{uuid.uuid4()}", body_type=str + ) + await stream.close() + async with new_worker(stream_client, AppendToStreamWorkflow) as worker: + reason = await stream_client.execute_workflow( + AppendToStreamWorkflow.run, + stream.ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert reason == "STREAM_ERROR_REASON_CLOSED" + + +async def test_workflow_append_with_a_producer_of_another_kind_fails(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, ExplicitProducerWorkflow) as worker: + result = await stream_client.execute_workflow( + ExplicitProducerWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + # Core writes only as the Workflow's Producer, so the append writes nothing. + assert result == ( + "STREAM_ERROR_REASON_INVALID: A Stream append of Workflow code writes as a " + "Producer of kind Workflow. Leave out producer=, or give a Producer of kind " + "Workflow." + ) + assert await _read_all(stream_client, stream_id) == [] + + +async def _describe_status(client: Client, stream_id: str) -> StreamStatus | None: + stream = client.get_stream(StreamRef(id=stream_id), body_type=str) + return (await stream.describe()).status + + +async def test_workflow_async_with_closes_the_stream(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, AsyncWithCloseWorkflow) as worker: + status = await stream_client.execute_workflow( + AsyncWithCloseWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert status == StreamStatus.StreamStatusOpen.name + assert await _describe_status(stream_client, stream_id) == ( + StreamStatus.StreamStatusClosed + ) + records = await _read_all(stream_client, stream_id) + assert [record.body for record in records] == ["a"] + + +@pytest.mark.parametrize("no_remote_activities", [False, True]) +async def test_worker_with_workflows_only_runs_the_close( + client: Client, no_remote_activities: bool +): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with Worker( + stream_client, + task_queue=str(uuid.uuid4()), + workflows=[CloseWorkflow], + no_remote_activities=no_remote_activities, + ) as worker: + await stream_client.execute_workflow( + CloseWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert await _describe_status(stream_client, stream_id) == ( + StreamStatus.StreamStatusClosed + ) + + +async def test_failed_workflow_task_after_direct_close_leaves_the_stream_open( + client: Client, +): + global _fail_close_intent_task + _fail_close_intent_task = True + stream_client = _with_store(client, MemoryStreamStore()) + stream = await stream_client.create_stream( + id=f"stream-{uuid.uuid4()}", body_type=str + ) + async with new_worker(stream_client, CloseIntentThenFailTaskOnceWorkflow) as worker: + await stream_client.execute_workflow( + CloseIntentThenFailTaskOnceWorkflow.run, + stream.ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + # Core makes the close with no Activity. The failed task recorded no intent, so it + # closed nothing, and the retried task appends again before it closes the Stream. + records = await _read_all(stream_client, stream.ref.id) + assert [record.body for record in records] == ["a", "a"] + assert await _describe_status(stream_client, stream.ref.id) == ( + StreamStatus.StreamStatusClosed + ) + + +@workflow.defn +class TwoAppendsWorkflow: + @workflow.run + async def run(self, stream_id: str, sleep_between: bool) -> None: + stream = await create_stream(id=stream_id, body_type=str) + await stream.append("a") + if sleep_between: + # A timer ends the Workflow Task, so the next append runs in a later one. + await workflow.sleep(timedelta(milliseconds=10)) + await stream.append("b") + + +@workflow.defn +class GatheredAppendsWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + stream = await create_stream(id=stream_id, body_type=str) + await asyncio.gather(stream.append("a"), stream.append("b"), stream.append("c")) + + +@workflow.defn +class GatheredAppendAndCloseWorkflow: + @workflow.run + async def run(self, stream_id: str) -> None: + stream = await create_stream(id=stream_id, body_type=str) + await asyncio.gather(stream.append("a"), stream.close()) + + +async def _run_two_appends( + client: Client, store: StreamStore, sleep_between: bool +) -> tuple[str, list[StreamRecord[str]]]: + stream_client = _with_store(client, store) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, TwoAppendsWorkflow) as worker: + handle = await stream_client.start_workflow( + TwoAppendsWorkflow.run, + args=[stream_id, sleep_between], + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + await handle.result() + assert handle.result_run_id + return handle.result_run_id, await _read_all(stream_client, stream_id) + + +async def test_workflow_appends_in_one_task_write_as_one_producer(client: Client): + run_id, records = await _run_two_appends( + client, MemoryStreamStore(), sleep_between=False + ) + assert [record.body for record in records] == ["a", "b"] + first, second = _producers(records) + assert first == second + assert first.id == f"wf:{run_id}" + + +async def test_workflow_append_in_a_later_task_has_a_higher_epoch(client: Client): + run_id, records = await _run_two_appends( + client, MemoryStreamStore(), sleep_between=True + ) + first, second = _producers(records) + assert first.id == second.id == f"wf:{run_id}" + assert _epoch(second) > _epoch(first) + + +async def test_workflow_appends_started_together_write_in_call_order(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, GatheredAppendsWorkflow) as worker: + await stream_client.execute_workflow( + GatheredAppendsWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + # Core runs the appends of one Stream one at a time, in the order of the calls. + records = await _read_all(stream_client, stream_id) + assert [record.body for record in records] == ["a", "b", "c"] + + +async def test_workflow_close_started_with_an_append_comes_after_it(client: Client): + stream_client = _with_store(client, MemoryStreamStore()) + stream_id = f"stream-{uuid.uuid4()}" + async with new_worker(stream_client, GatheredAppendAndCloseWorkflow) as worker: + await stream_client.execute_workflow( + GatheredAppendAndCloseWorkflow.run, + stream_id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + records = await _read_all(stream_client, stream_id) + assert [record.body for record in records] == ["a"] + assert await _describe_status(stream_client, stream_id) == ( + StreamStatus.StreamStatusClosed + ) From 28509b38eba8e520a0552eb650a790c9810573f8 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Tue, 17 Nov 2026 15:46:14 -0700 Subject: [PATCH 26/35] Added a page cap and a least gap to the Workflow read of a Stream. Core gave the Worker two read settings, so `StreamConfig` passes them on with the same checks. A gap trades latency for fewer activations of Workflow code. --- changelog/added/patient-river-heron.md | 2 + temporalio/bridge/sdk-core | 2 +- temporalio/bridge/src/worker.rs | 13 ++- temporalio/bridge/worker.py | 5 ++ temporalio/client/_stream_store.py | 37 ++++++++- temporalio/contrib/streams/README.md | 6 ++ temporalio/worker/_replayer.py | 8 ++ temporalio/worker/_stream_store.py | 10 +++ temporalio/worker/_worker.py | 4 + tests/nexus/test_stream_direct.py | 105 +++++++++++++++++++++++++ 10 files changed, 188 insertions(+), 4 deletions(-) diff --git a/changelog/added/patient-river-heron.md b/changelog/added/patient-river-heron.md index a0dc05af38..db5b9d2e51 100644 --- a/changelog/added/patient-river-heron.md +++ b/changelog/added/patient-river-heron.md @@ -4,6 +4,8 @@ A Workflow's append runs in Core inside the Workflow Task, with no Activity. It A Workflow's close runs in Core, with no Activity, and spans two Workflow Tasks. The task that calls `close` records the intent, Core closes the Stream after the server took that task, and the next task gives the result, so a failed task closes nothing. Await the close. A graceful shutdown of the Worker waits at most 10 s for a close that the code doesn't await, and a Worker that dies when the Workflow ends loses it. An append and a close of Workflow code always carry the Workflow Task as their epoch. An append is at-least-once. After a Worker crash or an eviction, the rerun of its task can write a second copy with the epoch of the new task. Core records metrics for the Direct calls of a Stream, such as `temporal_stream_read_total`, `temporal_stream_read_records` and `temporal_stream_close_latency`, each with the attribute `stream_store`, and `temporal_stream_close_abandoned` for a close that a graceful shutdown gave up. Add `StreamConfig.workflow_read_ahead_bytes`, the most bytes of one read's Records that wait in the Worker for Workflow code, 4 MiB by default. +Add `StreamConfig.workflow_read_page_records`, the most Records of one read that Workflow code gets in one activation. It is 256 by default and must be 1 to 1000. +Add `StreamConfig.workflow_read_min_gap`, the least time between two batches of one read. It has no gap by default and must be at most 1 s. A gap trades latency for fewer activations of Workflow code. Add `temporalio.exceptions.StreamReplayError`. With it in `workflow_failure_exception_types`, a replay that can't read the Records of a read from the Store again fails the Workflow, and not only the Workflow Task. Core tries a read, an append or a close of Workflow code again while its Store is unavailable, from 100 ms up to 2 s apart, for at most 30 s. Only then does Workflow code get the retryable `StreamError`. A replay tries its Store for 5 s. If the Store stays down, only the Workflow Task fails, with `StreamReplayUnavailable`, also with `StreamReplayError` in `workflow_failure_exception_types`. In Workflow code, `temporalio.contrib.streams.current_producer` gives the Workflow's Producer, and `BufferedAppender` writes the appends of one activation as one append, with no timer. diff --git a/temporalio/bridge/sdk-core b/temporalio/bridge/sdk-core index 3bec8fc0eb..829d645721 160000 --- a/temporalio/bridge/sdk-core +++ b/temporalio/bridge/sdk-core @@ -1 +1 @@ -Subproject commit 3bec8fc0eb42f47f2b250f21773448491a921afc +Subproject commit 829d645721baf2bb8cb6f9d0020bcdbfe779a68a diff --git a/temporalio/bridge/src/worker.rs b/temporalio/bridge/src/worker.rs index 48e3b77c30..56b6fc838f 100644 --- a/temporalio/bridge/src/worker.rs +++ b/temporalio/bridge/src/worker.rs @@ -72,7 +72,10 @@ pub struct WorkerConfig { /// The most bytes of one read's Records that wait for Workflow code. `None` keeps Core's /// default. stream_read_ahead_bytes: Option, - /// The least time between two batches of one read. `None` keeps Core's default. + /// The most Records of one read in one activation. `None` keeps Core's default. + stream_read_page_records: Option, + /// The least time between two batches of one read. `None` keeps Core's default of no gap. + stream_read_min_gap: Option, /// Whether a replay that lost the Records of a read fails the Workflow, for every type or /// for the types listed. stream_replay_as_workflow_fail: bool, @@ -743,6 +746,14 @@ fn convert_worker_config( Some(bytes) => stream_runtimes.with_read_ahead_bytes(bytes), None => stream_runtimes, }; + let stream_runtimes = match conf.stream_read_page_records { + Some(records) => stream_runtimes.with_read_page_records(records), + None => stream_runtimes, + }; + let stream_runtimes = match conf.stream_read_min_gap { + Some(gap) => stream_runtimes.with_read_min_gap(gap), + None => stream_runtimes, + }; let mut workflow_failure_errors = HashSet::new(); if conf.nondeterminism_as_workflow_fail { workflow_failure_errors.insert(WorkflowErrorType::Nondeterminism); diff --git a/temporalio/bridge/worker.py b/temporalio/bridge/worker.py index 8b14a05595..5347487a6f 100644 --- a/temporalio/bridge/worker.py +++ b/temporalio/bridge/worker.py @@ -8,6 +8,7 @@ import dataclasses from collections.abc import Awaitable, Callable, Sequence from dataclasses import dataclass +from datetime import timedelta from typing import ( Any, TypeAlias, @@ -66,6 +67,10 @@ class WorkerConfig: """The bridge's object of each Store's Stream runtime, by Store name.""" stream_read_ahead_bytes: int | None = None """The most bytes of one read's Records that wait for Workflow code, or Core's default.""" + stream_read_page_records: int | None = None + """The most Records of one read in one activation, or Core's default.""" + stream_read_min_gap: timedelta | None = None + """The least time between two batches of one read, or Core's default of no gap.""" stream_replay_as_workflow_fail: bool = False """Whether a replay that lost the Records of a read fails the Workflow.""" stream_replay_as_workflow_fail_for_types: set[str] = dataclasses.field( diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index 024f4213fc..41af0e1f55 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -595,6 +595,23 @@ class StreamConfig: for a Worker with little memory, since each open read can hold this much. """ + workflow_read_page_records: int | None = None + """The most Records of one read that Workflow code gets in one activation. + + Core keeps the Records over it and gives them in the next activation, with no wait. + ``None`` takes Core's default of 256. The value must be 1 to 1000. A replay gives + each page as History has it, whatever this value is. + """ + + workflow_read_min_gap: timedelta | None = None + """The least time between two batches of one read of Workflow code. + + A read gives its first batch immediately. The Records that come in the gap after a + batch come together in the next batch. This trades latency for fewer activations of + Workflow code, since a Record can wait up to one gap. ``None`` and zero mean no gap, + the default. The value must be zero to 1 s. A replay ignores it. + """ + _store_map: dict[str, StreamStore] = dataclasses.field( init=False, repr=False, compare=False ) @@ -604,14 +621,30 @@ def __post_init__(self) -> None: """Check the Stores and build the name-keyed map of them. Raises :py:class:`temporalio.exceptions.StreamConfigError` if there is no - Store, or if two Stores share a name, and ``ValueError`` if - :py:attr:`workflow_read_ahead_bytes` is below 1. + Store, or if two Stores share a name. Raises ``ValueError`` if + :py:attr:`workflow_read_ahead_bytes` is below 1, if + :py:attr:`workflow_read_page_records` is not 1 to 1000, or if + :py:attr:`workflow_read_min_gap` is not zero to 1 s. """ if ( self.workflow_read_ahead_bytes is not None and self.workflow_read_ahead_bytes < 1 ): raise ValueError("workflow_read_ahead_bytes must be at least 1") + if self.workflow_read_page_records is not None and not ( + 1 <= self.workflow_read_page_records <= 1000 + ): + raise ValueError( + "workflow_read_page_records must be 1 to 1000, " + f"got {self.workflow_read_page_records}" + ) + if self.workflow_read_min_gap is not None and not ( + timedelta(0) <= self.workflow_read_min_gap <= timedelta(seconds=1) + ): + raise ValueError( + "workflow_read_min_gap must be zero to 1 second, " + f"got {self.workflow_read_min_gap}" + ) if not self.stores: raise StreamConfigError("StreamConfig.stores must have at least one Store") store_map: dict[str, StreamStore] = {} diff --git a/temporalio/contrib/streams/README.md b/temporalio/contrib/streams/README.md index 2dff451391..d93115dc0a 100644 --- a/temporalio/contrib/streams/README.md +++ b/temporalio/contrib/streams/README.md @@ -115,6 +115,12 @@ waits until the code takes them. `workflow_read_ahead_bytes` of `StreamConfig` c bound, for large Records or a Worker with little memory. A read gives Workflow code its Records as soon as no activation of the Workflow is pending. A page that Workflow code gets in one activation has at most 256 Records, and the rest come in the next activation with no wait. +`workflow_read_page_records` changes that cap, from 1 to 1000. + +`workflow_read_min_gap` of `StreamConfig` sets the least time between two batches of one read. +It has no gap by default, and it must be at most 1 s. A read gives its first batch immediately. +The Records that come in the gap come together in the next batch. This trades latency for fewer +activations of Workflow code. A replay ignores both settings. An append and a close of Workflow code always write as the Workflow, with the Workflow Task as their epoch. A `producer=` of another kind raises `StreamError` with the reason diff --git a/temporalio/worker/_replayer.py b/temporalio/worker/_replayer.py index c55bb0bb15..0c9faf6638 100644 --- a/temporalio/worker/_replayer.py +++ b/temporalio/worker/_replayer.py @@ -24,6 +24,8 @@ from ._interceptor import Interceptor from ._stream_store import ( _bridge_read_ahead_bytes, + _bridge_read_min_gap, + _bridge_read_page_records, _bridge_stream_runtimes, _WorkflowStreamStore, ) @@ -392,6 +394,12 @@ def on_eviction_hook( stream_read_ahead_bytes=_bridge_read_ahead_bytes( self._config.get("stream_config") ), + stream_read_page_records=_bridge_read_page_records( + self._config.get("stream_config") + ), + stream_read_min_gap=_bridge_read_min_gap( + self._config.get("stream_config") + ), ), ) bridge_worker_scope = bridge_worker diff --git a/temporalio/worker/_stream_store.py b/temporalio/worker/_stream_store.py index 13c1abc29d..9fe2335d07 100644 --- a/temporalio/worker/_stream_store.py +++ b/temporalio/worker/_stream_store.py @@ -396,6 +396,16 @@ def _bridge_read_ahead_bytes(config: StreamConfig | None) -> int | None: # pyri return config.workflow_read_ahead_bytes if config is not None else None +def _bridge_read_page_records(config: StreamConfig | None) -> int | None: # pyright: ignore[reportUnusedFunction] + """Give Core the page cap of a read in the config, or ``None`` for Core's default.""" + return config.workflow_read_page_records if config is not None else None + + +def _bridge_read_min_gap(config: StreamConfig | None) -> timedelta | None: # pyright: ignore[reportUnusedFunction] + """Give Core the least gap between two batches of a read, or ``None`` for no gap.""" + return config.workflow_read_min_gap if config is not None else None + + def _bridge_stream_runtimes(config: StreamConfig | None) -> list[tuple[str, Any]]: # pyright: ignore[reportUnusedFunction] """Give Core the bridge's object of each Store's Stream runtime, by Store name.""" if config is None: diff --git a/temporalio/worker/_worker.py b/temporalio/worker/_worker.py index a05ed05862..e54ebef1cd 100644 --- a/temporalio/worker/_worker.py +++ b/temporalio/worker/_worker.py @@ -36,6 +36,8 @@ from ._plugin import Plugin from ._stream_store import ( _bridge_read_ahead_bytes, + _bridge_read_min_gap, + _bridge_read_page_records, _bridge_stream_runtimes, _WorkflowStreamStore, ) @@ -741,6 +743,8 @@ def check_activity(activity: str): stream_config if self._workflow_worker is not None else None ), stream_read_ahead_bytes=_bridge_read_ahead_bytes(stream_config), + stream_read_page_records=_bridge_read_page_records(stream_config), + stream_read_min_gap=_bridge_read_min_gap(stream_config), ), ) diff --git a/tests/nexus/test_stream_direct.py b/tests/nexus/test_stream_direct.py index e2bc74c5a5..8ec405eb26 100644 --- a/tests/nexus/test_stream_direct.py +++ b/tests/nexus/test_stream_direct.py @@ -2450,6 +2450,111 @@ async def test_store_page_over_256_records_comes_in_pages_of_at_most_256( ) +# The page cap and the least gap of a read, two settings of the Worker. + + +def _client_with(client: Client, **settings: Any) -> Client: + config = client.config() + config["stream_config"] = StreamConfig(stores=[MemoryStreamStore()], **settings) + return Client(**config) + + +def _page_counts(history: WorkflowHistory) -> list[int]: + return [ + count + for task in _read_entries(history) + for entry in task + for count in entry.page_counts + ] + + +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_page_cap_of_10_gives_pages_of_at_most_10( + client: Client, max_cached_workflows: int +): + stream_client = _client_with(client, workflow_read_page_records=10) + bodies = [f"record-{index}" for index in range(35)] + ref = await _filled_stream(stream_client, bodies) + handle, got = await _run( + stream_client, + ReadItemsWorkflow, + ref, + None, + max_cached_workflows=max_cached_workflows, + ) + assert got == bodies + history = await handle.fetch_history() + counts = _page_counts(history) + assert sum(counts) == 35 + assert max(counts) == 10 + config = stream_client.config()["stream_config"] + await Replayer(workflows=[ReadItemsWorkflow], stream_config=config).replay_workflow( + history + ) + + +def test_page_cap_out_of_bounds_raises(): + for records in (0, 1001): + with pytest.raises(ValueError, match="workflow_read_page_records"): + StreamConfig( + stores=[MemoryStreamStore()], workflow_read_page_records=records + ) + + +def test_min_gap_out_of_bounds_raises(): + for gap in (timedelta(seconds=2), timedelta(milliseconds=-1)): + with pytest.raises(ValueError, match="workflow_read_min_gap"): + StreamConfig(stores=[MemoryStreamStore()], workflow_read_min_gap=gap) + + +@activity.defn +async def append_five_20ms_apart(stream_id: str) -> None: + stream = activity.client().get_stream(StreamRef(id=stream_id), body_type=str) + for index in range(5): + await stream.append(f"record-{index}") + await asyncio.sleep(0.02) + await stream.close() + + +@workflow.defn +class GappedReadWorkflow: + @workflow.run + async def run(self, stream_id: str) -> ReadResult: + writer = workflow.start_activity( + append_five_20ms_apart, + stream_id, + start_to_close_timeout=timedelta(seconds=30), + ) + stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) + result = ReadResult([], [], [], [], []) + async for page in stream.iter_read(wait=timedelta(seconds=10)): + result.bodies.extend(record.body for record in page.records) + result.page_sizes.append(len(page.records)) + result.dones.append(page.done) + await writer + return result + + +async def test_min_gap_of_200ms_joins_records_that_come_20ms_apart(client: Client): + stream_client = _client_with( + client, workflow_read_min_gap=timedelta(milliseconds=200) + ) + ref = await _filled_stream(stream_client, [], close=False) + handle, result = await _run( + stream_client, + GappedReadWorkflow, + ref.id, + activities=[append_five_20ms_apart], + ) + assert result.bodies == [f"record-{index}" for index in range(5)] + assert len(result.page_sizes) < 5 + assert result.dones[-1] is True + config = stream_client.config()["stream_config"] + await Replayer( + workflows=[GappedReadWorkflow], stream_config=config + ).replay_workflow(await handle.fetch_history()) + + # A replay that lost the Records of a read. From d5c0e57386b8182ee8a25c22e2e44676c496c9ae Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Tue, 17 Nov 2026 16:23:03 -0700 Subject: [PATCH 27/35] Made Core call a Stream Store written in Python for Workflow code. A read, an append and a close of Workflow code go through Core, which records the marker. Core gets a runtime that runs the Python Store's calls on the Worker's event loop, so the refusal of such a Store is gone. --- changelog/added/patient-river-heron.md | 2 +- temporalio/bridge/sdk-core | 2 +- temporalio/bridge/src/lib.rs | 32 +++ temporalio/bridge/src/streams.rs | 207 ++++++++++++++++++- temporalio/bridge/streams.py | 91 +++++++++ temporalio/client/_stream_store.py | 62 ++++-- temporalio/contrib/streams/README.md | 28 ++- temporalio/exceptions.py | 18 +- temporalio/worker/_replayer.py | 12 +- temporalio/worker/_stream_store.py | 165 +++++++++++++-- temporalio/worker/_worker.py | 17 +- tests/helpers/streams.py | 106 +++------- tests/nexus/test_stream_direct.py | 234 +++++++++++++++++----- tests/nexus/test_stream_store_workflow.py | 11 +- 14 files changed, 787 insertions(+), 200 deletions(-) create mode 100644 temporalio/bridge/streams.py diff --git a/changelog/added/patient-river-heron.md b/changelog/added/patient-river-heron.md index db5b9d2e51..f08a60bc6d 100644 --- a/changelog/added/patient-river-heron.md +++ b/changelog/added/patient-river-heron.md @@ -1,4 +1,4 @@ -Workflow code creates a Stream, appends Records to it, reads it, describes it and closes it, with `workflow.create_stream` and `workflow.get_stream`. A create and a describe run in the Store's Local Activity, with the options of `StreamConfig.workflow_call_options`. Core makes a read, an append and a close itself, with no Activity, on a Store with a Rust runtime such as the Redis Store or `MemoryStreamStore`. On a Store written in Python, a read, an append and a close of Workflow code fail the Workflow. `Replayer` takes the `stream_config` of the Worker's client. +Workflow code creates a Stream, appends Records to it, reads it, describes it and closes it, with `workflow.create_stream` and `workflow.get_stream`. A create and a describe run in the Store's Local Activity, with the options of `StreamConfig.workflow_call_options`. Core makes a read, an append and a close itself, with no Activity, on the Redis Store, on `MemoryStreamStore` and on a Store written in Python. Core calls a Store written in Python as a task on the Worker's event loop, never on a Workflow thread. For Workflow code, such a Store waits up to a read's `wait`, ends that wait on a cancel, gives offsets of 1 to 1024 bytes that compare in order, fences a lower epoch of a Producer, writes a resent batch once, and raises `StreamError` with a reason. Any other exception counts as an outage, which Core tries again for up to 30 s. `Replayer` takes the `stream_config` of the Worker's client. The Workflow `Stream` has `read`, `iter_read` and `iter_read_items`, so Workflow code reads a Stream with the same calls as a client. Core reads the Store inside the Workflow Task. A page that Workflow code gets in one activation has at most 256 Records, and the rest come in the next activation with no wait. A loop that asks for its next page in the same activation keeps one read open, and a read that no code waits on closes before the activation completes, so it never holds a Workflow Task open. With a `wait`, a quiet Stream gives empty pages, so a loop on a Stream that can be quiet for long uses `iter_read` and leaves after an empty page. A call that waits on a quiet Stream holds its Workflow Task open, with one Workflow Task about every 8 s. The calls of one Workflow Task write one marker in History. `StreamConfig.workflow_read_replay_from` decides whether the marker holds the Records (`StreamReplaySource.HISTORY`) or only their offsets and a digest, which a replay reads from the Store again (`StreamReplaySource.STORE`, the default). With `HISTORY`, one Workflow Task's marker holds at most 1 MiB of Records, and the read goes on in the next task. A Workflow's append runs in Core inside the Workflow Task, with no Activity. It writes as the Workflow, with its Workflow Task as the epoch. A `producer=` of another kind raises `StreamError` with the reason `STREAM_ERROR_REASON_INVALID`. A Workflow's close runs in Core, with no Activity, and spans two Workflow Tasks. The task that calls `close` records the intent, Core closes the Stream after the server took that task, and the next task gives the result, so a failed task closes nothing. Await the close. A graceful shutdown of the Worker waits at most 10 s for a close that the code doesn't await, and a Worker that dies when the Workflow ends loses it. An append and a close of Workflow code always carry the Workflow Task as their epoch. An append is at-least-once. After a Worker crash or an eviction, the rerun of its task can write a second copy with the epoch of the new task. diff --git a/temporalio/bridge/sdk-core b/temporalio/bridge/sdk-core index 829d645721..c90f015fd0 160000 --- a/temporalio/bridge/sdk-core +++ b/temporalio/bridge/sdk-core @@ -1 +1 @@ -Subproject commit 829d645721baf2bb8cb6f9d0020bcdbfe779a68a +Subproject commit c90f015fd067978ce630025c48d84e0f07318f12 diff --git a/temporalio/bridge/src/lib.rs b/temporalio/bridge/src/lib.rs index a4fda26db6..64dff02650 100644 --- a/temporalio/bridge/src/lib.rs +++ b/temporalio/bridge/src/lib.rs @@ -6,6 +6,7 @@ mod client_rpc_generated; mod envconfig; mod metric; mod runtime; +mod streams; mod testing; mod worker; @@ -48,6 +49,15 @@ fn temporal_sdk_bridge(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(init_runtime, m)?)?; m.add_function(wrap_pyfunction!(raise_in_thread, m)?)?; + // Stream stuff + m.add("StreamCallError", py.get_type::())?; + #[cfg(feature = "streams")] + { + m.add_class::()?; + m.add_function(wrap_pyfunction!(new_memory_stream_runtime, m)?)?; + m.add_function(wrap_pyfunction!(new_python_stream_runtime, m)?)?; + } + // Testing stuff m.add_class::()?; m.add_function(wrap_pyfunction!(start_dev_server, m)?)?; @@ -104,6 +114,28 @@ fn raise_in_thread(py: Python, thread_id: std::os::raw::c_long, exc: &Bound<'_, runtime::raise_in_thread(py, thread_id, exc) } +#[cfg(feature = "streams")] +#[pyfunction] +#[pyo3(signature = (runtime_ref, store_name, retention=None, producer_idle_ttl=None))] +fn new_memory_stream_runtime( + runtime_ref: &runtime::RuntimeRef, + store_name: String, + retention: Option, + producer_idle_ttl: Option, +) -> PyResult { + streams::new_memory_stream_runtime(runtime_ref, store_name, retention, producer_idle_ttl) +} + +#[cfg(feature = "streams")] +#[pyfunction] +fn new_python_stream_runtime( + runtime_ref: &runtime::RuntimeRef, + store_name: String, + caller: Py, +) -> PyResult { + streams::new_python_stream_runtime(runtime_ref, store_name, caller) +} + #[pyfunction] fn start_dev_server<'a>( py: Python<'a>, diff --git a/temporalio/bridge/src/streams.rs b/temporalio/bridge/src/streams.rs index c575fd7916..16a189c9e7 100644 --- a/temporalio/bridge/src/streams.rs +++ b/temporalio/bridge/src/streams.rs @@ -1,6 +1,6 @@ -//! Bindings to the Stream runtime of the `temporalio-streams` crate, on the memory Store. Python -//! calls the five Stream service operations by name, with the encoded request, and gets the encoded -//! response. +//! Bindings to the Stream runtime of the `temporalio-streams` crate, on the memory Store or on a +//! Store written in Python. Python calls the five Stream service operations by name, with the +//! encoded request, and gets the encoded response. use pyo3::exceptions::PyException; #[cfg(feature = "streams")] @@ -67,3 +67,204 @@ pub fn new_memory_stream_runtime( ); Ok(StreamRuntimeRef { streams, runtime }) } + +/// Builds the runtime on a Store written in Python. Core calls it for Workflow code through the +/// runtime, which checks each request and answer as for a Store in Rust. `caller` runs each call +/// of the Store on the Worker's event loop, never on a Workflow thread. +#[cfg(feature = "streams")] +pub fn new_python_stream_runtime( + runtime_ref: &runtime::RuntimeRef, + store_name: String, + caller: Py, +) -> PyResult { + let runtime = runtime_ref.runtime.clone(); + runtime.assert_same_process("create a Stream runtime")?; + let meter = runtime.core.telemetry().get_temporal_metric_meter(); + let store = std::sync::Arc::new(python_store::PythonStore { caller }); + let streams = temporalio_streams::StreamRuntime::with_operations(store_name, store, meter); + Ok(StreamRuntimeRef { streams, runtime }) +} + +#[cfg(feature = "streams")] +mod python_store { + use futures::channel::oneshot; + use prost::Message; + use pyo3::prelude::*; + use pyo3::types::PyBytes; + use std::sync::Mutex; + use std::time::Duration; + use temporalio_streams::{ + operations::OperationStore, + proto::{ + AppendRecordsRequest, AppendRecordsResponse, CloseStreamRequest, CloseStreamResponse, + CreateStreamRequest, CreateStreamResponse, DescribeStreamRequest, + DescribeStreamResponse, ReadRecordsRequest, ReadRecordsResponse, + }, + Reason, StreamError, StreamResult, + }; + + /// The reasons that a Stream failure can carry to Workflow code. + const REASONS: [Reason; 15] = [ + Reason::NotFound, + Reason::Expired, + Reason::Closed, + Reason::Fenced, + Reason::SequenceTooLow, + Reason::SeqDivergent, + Reason::NotOwner, + Reason::FormatTooNew, + Reason::OffsetTooHigh, + Reason::TooLarge, + Reason::Invalid, + Reason::StorageUnavailable, + Reason::Unsupported, + Reason::AlreadyExists, + Reason::StorageCorrupt, + ]; + + /// The longest pause after an empty page that came before its wait ended. A Store that + /// ignores `wait` then gets a read about ten times a second, and not in a busy loop. + const EMPTY_PAGE_PAUSE: Duration = Duration::from_millis(100); + + /// The encoded response, or the reason and the message of a failure. + type Answer = Result, (i32, String)>; + + /// A Store written in Python. Each call runs as a task on the Worker's event loop. + pub(crate) struct PythonStore { + /// The Python object that starts a call on the event loop and gives a canceller. + pub(crate) caller: Py, + } + + /// What Python calls with the answer of one call. + #[pyclass(frozen)] + struct StoreCallDone { + tx: Mutex>>, + } + + #[pymethods] + impl StoreCallDone { + fn __call__(&self, response: Option<&[u8]>, reason: i32, message: String) { + let tx = self.tx.lock().ok().and_then(|mut tx| tx.take()); + if let Some(tx) = tx { + // Core may have stopped the call, and then nobody waits for its answer. + let _ = tx.send(match response { + Some(bytes) => Ok(bytes.to_vec()), + None => Err((reason, message)), + }); + } + } + } + + /// Cancels the Python task of a call that Core stopped, so that no task stays on the loop. + struct CancelOnDrop(Option>); + + impl Drop for CancelOnDrop { + fn drop(&mut self) { + if let Some(cancel) = self.0.take() { + // An interpreter that is shutting down has no task left to cancel. + let _ = Python::try_attach(|py| { + if let Err(err) = cancel.call0(py) { + tracing::warn!("Failed to cancel a call to a Stream Store: {err}"); + } + }); + } + } + } + + fn reason(value: i32) -> Option { + REASONS + .into_iter() + .find(|reason| reason.proto() as i32 == value) + } + + impl PythonStore { + async fn call( + &self, + operation: &'static str, + request: Req, + ) -> StreamResult { + let (tx, rx) = oneshot::channel(); + let done = StoreCallDone { + tx: Mutex::new(Some(tx)), + }; + let cancel = Python::attach(|py| { + let request = PyBytes::new(py, &request.encode_to_vec()); + self.caller + .call_method1(py, "start", (operation, request, done)) + }) + .map_err(|err| { + StreamError::storage_unavailable(format!( + "The Worker could not call the Stream Store: {err}" + )) + })?; + let mut canceller = CancelOnDrop(Some(cancel)); + let answer = rx.await; + // The task ended, so there is nothing to cancel. + canceller.0 = None; + match answer { + Ok(Ok(bytes)) => Resp::decode(bytes.as_slice()).map_err(|err| { + StreamError::storage_corrupt(format!( + "The Stream Store gave a bad {operation} response: {err}" + )) + }), + Ok(Err((value, message))) => match reason(value) { + Some(reason) => Err(StreamError::new(reason, message)), + None => Err(StreamError::storage_unavailable(format!( + "The Stream Store failed with the unknown reason {value}: {message}" + ))), + }, + Err(_) => Err(StreamError::storage_unavailable( + "The event loop of the Worker stopped the call to the Stream Store", + )), + } + } + } + + #[async_trait::async_trait] + impl OperationStore for PythonStore { + async fn create_stream( + &self, + request: CreateStreamRequest, + ) -> StreamResult { + self.call("CreateStream", request).await + } + + async fn append_records( + &self, + request: AppendRecordsRequest, + ) -> StreamResult { + self.call("AppendRecords", request).await + } + + async fn read_records( + &self, + request: ReadRecordsRequest, + ) -> StreamResult { + let wait = request + .wait + .and_then(|wait| Duration::try_from(wait).ok()) + .unwrap_or_default(); + let started = tokio::time::Instant::now(); + let page: ReadRecordsResponse = self.call("ReadRecords", request).await?; + let left = wait.saturating_sub(started.elapsed()); + if page.records.is_empty() && !page.done && !left.is_zero() { + tokio::time::sleep(left.min(EMPTY_PAGE_PAUSE)).await; + } + Ok(page) + } + + async fn close_stream( + &self, + request: CloseStreamRequest, + ) -> StreamResult { + self.call("CloseStream", request).await + } + + async fn describe_stream( + &self, + request: DescribeStreamRequest, + ) -> StreamResult { + self.call("DescribeStream", request).await + } + } +} diff --git a/temporalio/bridge/streams.py b/temporalio/bridge/streams.py new file mode 100644 index 0000000000..b4c418d693 --- /dev/null +++ b/temporalio/bridge/streams.py @@ -0,0 +1,91 @@ +"""The Stream runtime that this bridge build links. (unstable) + +Nothing in this module should be considered stable. The API may change. +""" + +from __future__ import annotations + +from datetime import timedelta +from typing import Any + +import temporalio.bridge.runtime +import temporalio.bridge.temporal_sdk_bridge +import temporalio.exceptions + +StreamCallError: type[Exception] = temporalio.bridge.temporal_sdk_bridge.StreamCallError + + +class StreamRuntime: + """The runtime of the ``temporalio-streams`` crate on one Store.""" + + @staticmethod + def new_memory( + runtime: temporalio.bridge.runtime.Runtime, + *, + name: str, + retention: timedelta | None, + producer_idle_ttl: timedelta | None, + ) -> StreamRuntime: + """Build the runtime on a new memory Store. + + Raises: + temporalio.exceptions.StreamConfigError: The bridge build has no Stream + runtime. + """ + new = getattr( + temporalio.bridge.temporal_sdk_bridge, "new_memory_stream_runtime", None + ) + if new is None: + raise temporalio.exceptions.StreamConfigError( + "This build of temporalio has no Stream runtime" + ) + return StreamRuntime(new(runtime._ref, name, retention, producer_idle_ttl)) + + @staticmethod + def new_python( + runtime: temporalio.bridge.runtime.Runtime, + *, + name: str, + caller: Any, + ) -> StreamRuntime: + """Build the runtime on a Store written in Python, which Core calls for Workflow code. + + ``caller`` runs each call of the Store on the Worker's event loop. + + Raises: + temporalio.exceptions.StreamConfigError: The bridge build has no Stream + runtime. + """ + new = getattr( + temporalio.bridge.temporal_sdk_bridge, "new_python_stream_runtime", None + ) + if new is None: + raise temporalio.exceptions.StreamConfigError( + "This build of temporalio has no Stream runtime" + ) + return StreamRuntime(new(runtime._ref, name, caller)) + + @staticmethod + def has_runtime() -> bool: + """Whether the bridge build has the Stream runtime.""" + return hasattr( + temporalio.bridge.temporal_sdk_bridge, "new_python_stream_runtime" + ) + + def __init__(self, ref: Any) -> None: + """Wrap the bridge's runtime object. Use ``new_memory`` or ``new_python`` instead.""" + self._ref = ref + + async def call(self, operation: str, request: bytes) -> bytes: + """Run one Stream operation on an encoded request and return the encoded response. + + Raises: + temporalio.exceptions.StreamError: The call failed. + """ + try: + return await self._ref.call(operation, request) + except StreamCallError as err: + _, message, details = err.args + raise temporalio.exceptions._stream_error_from_call( + message, details + ) from None diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index 41af0e1f55..c3f15b590c 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -147,18 +147,42 @@ class StreamStore(ABC): Store of a package, such as ``RedisStreamStore`` of ``temporalio-streams-redis``, or :py:class:`temporalio.testing.MemoryStreamStore` in tests. - The SDK's Stores run on the bridge, in Rust, and this class isn't meant to be - subclassed in Python. A Store that is written in Python works for a client and an - Activity, for example as a double in a test. Core can't call it, so Workflow code has - no Direct call on it. A read, an append and a close of Workflow code then fail the - Workflow. A create and a describe still run in the Store's Local Activity. - - Such a Store implements :py:attr:`name` and the five operations. Each operation - takes and gives a message of - ``temporalio.api.streamservice.v1``. The bodies of the Records arrive as payloads - that the SDK already encoded with the data converter and the payload codec, so a - Store keeps payloads and not the user's types. A failed call raises - :py:class:`temporalio.exceptions.StreamError` with a reason. + The SDK's Stores run on the bridge, in Rust. A Store can also be written in Python, + by a subclass that implements :py:attr:`name` and the five operations. Each + operation takes and gives a message of ``temporalio.api.streamservice.v1``. The + bodies of the Records arrive as payloads that the SDK already encoded with the data + converter and the payload codec, so a Store keeps payloads and not the user's types. + A failed call raises :py:class:`temporalio.exceptions.StreamError` with a reason. + + Such a Store works for client, Activity and Workflow code. Core makes a read, an + append and a close of Workflow code, as it does on a Store in Rust. It calls the + Store's method as a task on the Worker's event loop, never on a Workflow thread, and + records the result in History. A create and a describe of Workflow code run in the + Store's Local Activity. For Workflow code, a Store must do these things. + + * Each method is ``async`` and never blocks the event loop, since the calls of every + Workflow on the Worker run there. + * :py:meth:`read_records` waits up to the request's ``wait`` when it has no Record + after ``from_offset``, and gives an empty page after the wait. An append or a + close wakes it. Core cancels the task of a read that it stops, for example when + Workflow code is cancelled or the Worker shuts down, so the wait must end on a + cancel. + * Each Record has an offset of 1 to 1024 bytes. Offsets of one Stream compare in + order as bytes. A page that gives the last Record of a closed Stream is ``done``. + * :py:meth:`append_records` gives the offsets of the first and the last Record. A + Workflow appends as a Producer of kind Workflow, with its Workflow Task as the + epoch. The Store refuses a batch of a lower epoch of that Producer with the reason + ``STREAM_ERROR_REASON_FENCED``. It writes a batch that comes again with the same + Producer, epoch and ``sequence`` once, since Core sends it again after an outage. + * :py:meth:`close_stream` of a closed Stream succeeds, since Core may send a close + again. + * A replay of a read under :py:attr:`StreamReplaySource.STORE` reads the Records + again, so the Store keeps them with the same offsets. + * An outage raises :py:class:`temporalio.exceptions.StreamError` with the reason + ``STREAM_ERROR_REASON_STORAGE_UNAVAILABLE``. Core then tries a call of Workflow + code again for up to 30 s. Every other reason reaches Workflow code on the first + try. Any other exception, or a ``StreamError`` with no reason, counts as an outage, + and the Worker logs it. Its message tells the type of the exception. .. warning:: This API is experimental and subject to change. @@ -494,8 +518,7 @@ class StreamConfig: Activity. So Workflow code needs no Worker that polls for Activity Tasks. Workflow code reads with the same ``read``, ``iter_read`` and ``iter_read_items`` as - a client. On a Store with a Rust runtime, Core reads the Store inside the Workflow - Task. A page that Workflow code gets in one activation has at most 256 Records, and + a client. Core reads the Store inside the Workflow Task. A page that Workflow code gets in one activation has at most 256 Records, and the rest come in the next activation with no wait. A loop that asks for its next page in the same activation keeps one read open in Core. A read that no code waits on closes before the activation completes, so it never holds a Workflow Task open. @@ -528,10 +551,9 @@ class StreamConfig: as long as the Workflow can replay. * A replay on a Worker with no cache reads the Store again on every Workflow Task. - On a Store written in Python, a read, an append and a close of Workflow code fail - the Workflow with a non-retryable - :py:class:`temporalio.exceptions.ApplicationError`. Code in a Query handler or an - Update validator can't read. + Core calls a Store written in Python on the Worker's event loop, with the same rules. + :py:class:`StreamStore` says what such a Store must do for Workflow code. Code in a + Query handler or an Update validator can't read. .. warning:: This API is experimental and subject to change. @@ -691,8 +713,8 @@ def _bridge_stream_runtimes( ) -> list[tuple[str, temporalio.bridge.streams.StreamRuntime]]: """Give the Stream runtime of each Store that runs on the bridge, by Store name. - Core reads and appends a Stream of Workflow code through these runtimes. A Store - that a user wrote in Python has none. + Core reads and appends a Stream of Workflow code through these runtimes. The + Worker builds the runtime of a Store written in Python itself. """ return [ (store.name, store._get_stream_runtime()) diff --git a/temporalio/contrib/streams/README.md b/temporalio/contrib/streams/README.md index d93115dc0a..fa7d7dee8e 100644 --- a/temporalio/contrib/streams/README.md +++ b/temporalio/contrib/streams/README.md @@ -46,10 +46,10 @@ replacement of the first attempt's Records. ## Reads and appends in Workflow code The helpers run on the Streams API, so they follow how Workflow code reads, appends and -closes. On a Store with a Rust runtime, such as the Redis Store or `MemoryStreamStore`, Core -reads, appends and closes a Stream itself, with no Activity. Workflow code reads with the same -`read`, `iter_read` and `iter_read_items` as a client. It gets the same pages, but each page has -at most 256 Records. +closes. On every Store, such as the Redis Store, `MemoryStreamStore` or a Store written in +Python, Core reads, appends and closes a Stream itself, with no Activity. Workflow code reads +with the same `read`, `iter_read` and `iter_read_items` as a client. It gets the same pages, but +each page has at most 256 Records. Here the Workflow reads the events of a model's answer while the model Activity still runs, and starts each tool as its call arrives. @@ -152,8 +152,24 @@ after the server took that task. A graceful shutdown of the Worker waits for suc at most 10 s. Only a Worker that dies there, or a Store that doesn't answer in that time, loses the close, and the Stream stays open. -A Store isn't meant to be written in Python, since such a Store has no Rust runtime. On one, -a read, an append and a close of Workflow code fail the Workflow. +A Store can be written in Python, as a subclass of `StreamStore`, and it works for Workflow +code too. Core makes a read, an append and a close of Workflow code on it with the same marker +and rules as on a Store in Rust. It calls the Store's method as a task on the Worker's event +loop, never on a Workflow thread. For Workflow code, such a Store must do these things. + +- Each method is `async` and never blocks the event loop. +- `read_records` waits up to the request's `wait` when it has no Record after `from_offset`, and + an append or a close wakes it. The wait must end on a cancel, since Core cancels the task of a + read that it stops. +- Each Record has an offset of 1 to 1024 bytes, and offsets compare in order as bytes. A page + that gives the last Record of a closed Stream is `done`. +- `append_records` refuses a lower epoch of a Producer with `STREAM_ERROR_REASON_FENCED`, and + writes a batch that comes again with the same Producer, epoch and `sequence` once. +- `close_stream` of a closed Stream succeeds. +- The Store keeps the Records with their offsets for as long as a replay reads them. +- An outage raises `StreamError` with `STREAM_ERROR_REASON_STORAGE_UNAVAILABLE`, and Core tries + the call again for up to 30 s. Any other exception counts as an outage too, and its type is in + the message that Workflow code gets. ## Metrics diff --git a/temporalio/exceptions.py b/temporalio/exceptions.py index 6109f54ea0..f854061d68 100644 --- a/temporalio/exceptions.py +++ b/temporalio/exceptions.py @@ -218,10 +218,9 @@ class StreamError(ApplicationError): ``STREAM_ERROR_REASON_CLOSED`` (the Stream is closed), or ``STREAM_ERROR_REASON_ALREADY_EXISTS`` (a strict create of a Stream that exists). A Store outage, ``STREAM_ERROR_REASON_STORAGE_UNAVAILABLE``, is the one retryable - reason. On a Store with a Rust runtime, Core tries a read, an append or a close of - Workflow code again while the Store is unavailable. It waits from 100 ms up to 2 s - between tries, and Workflow code gets this reason only after 30 s. Every other - reason raises on the first try. + reason. Core tries a read, an append or a close of Workflow code again while the + Store is unavailable. It waits from 100 ms up to 2 s between tries, and Workflow + code gets this reason only after 30 s. Every other reason raises on the first try. .. warning:: This API is experimental and subject to change. @@ -308,11 +307,12 @@ class StreamConfigError(RuntimeError): A Workflow's Stream call on a Worker whose client has no Stream config doesn't raise this class. Neither does a read, an append or a close of Workflow code on a - Store written in Python, which has no Rust runtime for Core to call. Workflow code - gets an :py:class:`ApplicationError` with the ``type`` ``StreamConfigError`` and - ``non_retryable`` set, so an uncaught one fails the Workflow. Workflow code can - catch this error. A Workflow that catches it must not go on to make the same call on - a Worker that has a config, since the History then has no record of the first call. + Store written in Python, in a build of temporalio with no Stream runtime for Core to + call it. Workflow code gets an :py:class:`ApplicationError` with the ``type`` + ``StreamConfigError`` and ``non_retryable`` set, so an uncaught one fails the + Workflow. Workflow code can catch this error. A Workflow that catches it must not go + on to make the same call on a Worker that has a config, since the History then has + no record of the first call. .. warning:: This API is experimental and subject to change. diff --git a/temporalio/worker/_replayer.py b/temporalio/worker/_replayer.py index 0c9faf6638..17e630fc4a 100644 --- a/temporalio/worker/_replayer.py +++ b/temporalio/worker/_replayer.py @@ -26,7 +26,7 @@ _bridge_read_ahead_bytes, _bridge_read_min_gap, _bridge_read_page_records, - _bridge_stream_runtimes, + _core_stream_runtimes, _WorkflowStreamStore, ) from ._worker import load_default_build_id @@ -323,6 +323,12 @@ def on_eviction_hook( else set() ) + # A replay reads a Store written in Python on this loop. + core_stream_runtimes = _core_stream_runtimes( + self._config.get("stream_config"), runtime + ) + core_stream_runtimes.bind(asyncio.get_running_loop()) + # Create bridge worker bridge_worker, pusher = temporalio.bridge.worker.Worker.for_replay( runtime._core_runtime, @@ -388,9 +394,7 @@ def on_eviction_hook( (plugin.name(), plugin.version()) for plugin in self.plugins ], storage_drivers=storage_driver_types, - stream_runtimes=_bridge_stream_runtimes( - self._config.get("stream_config") - ), + stream_runtimes=core_stream_runtimes.runtimes, stream_read_ahead_bytes=_bridge_read_ahead_bytes( self._config.get("stream_config") ), diff --git a/temporalio/worker/_stream_store.py b/temporalio/worker/_stream_store.py index 9fe2335d07..612a21f031 100644 --- a/temporalio/worker/_stream_store.py +++ b/temporalio/worker/_stream_store.py @@ -5,7 +5,8 @@ import asyncio import collections import dataclasses -from collections.abc import Generator, Iterable +import logging +from collections.abc import Callable, Generator, Iterable from datetime import timedelta from typing import TYPE_CHECKING, Any @@ -16,7 +17,9 @@ import temporalio.api.stream.v1 import temporalio.api.streamservice.v1 import temporalio.bridge.proto.workflow_commands +import temporalio.bridge.streams import temporalio.exceptions +import temporalio.runtime import temporalio.workflow from temporalio.client._stream_store import ( _STREAM_OPERATIONS, @@ -33,6 +36,8 @@ if TYPE_CHECKING: from ._workflow_instance import _WorkflowInstanceImpl +logger = logging.getLogger(__name__) + def _no_stream_config_error() -> temporalio.exceptions.ApplicationError: # pyright: ignore[reportUnusedFunction] # A `StreamConfigError` would fail the Workflow Task, and its retries would fail the @@ -55,17 +60,21 @@ def _no_stream_config_error() -> temporalio.exceptions.ApplicationError: # pyri } +# Core calls a Store written in Python through the Stream runtime of the bridge, which a +# build of temporalio may leave out. +_CORE_CALLS_PYTHON_STORES = temporalio.bridge.streams.StreamRuntime.has_runtime() + + def _no_direct_call_error( store: StreamStore, operation: str ) -> temporalio.exceptions.ApplicationError: - # Core makes a Workflow's read, append and close itself, and it can't call a Store - # that runs in Python. Each retry of the task would fail the same way for good, so the - # Workflow fails instead, as for a Worker with no Stream config. + # Core makes a Workflow's read, append and close itself, and a build with no Stream + # runtime gives Core no way to call the Store. Each retry of the task would fail the + # same way for good, so the Workflow fails instead, as for a Worker with no config. call, advice = _DIRECT_CALLS[operation] return temporalio.exceptions.ApplicationError( - f"The Stream Store '{store.name}' has no Direct {call}, since it is written in " - f"Python. {advice} in an Activity, or use a Store that runs on the bridge, " - "such as the Redis Store or MemoryStreamStore.", + f"The Stream Store '{store.name}' has no Direct {call}, since this build of " + f"temporalio has no Stream runtime. {advice} in an Activity.", type="StreamConfigError", non_retryable=True, ) @@ -406,11 +415,141 @@ def _bridge_read_min_gap(config: StreamConfig | None) -> timedelta | None: # py return config.workflow_read_min_gap if config is not None else None -def _bridge_stream_runtimes(config: StreamConfig | None) -> list[tuple[str, Any]]: # pyright: ignore[reportUnusedFunction] - """Give Core the bridge's object of each Store's Stream runtime, by Store name.""" +_STORAGE_UNAVAILABLE = ( + temporalio.api.enums.v1.StreamErrorReason.STREAM_ERROR_REASON_STORAGE_UNAVAILABLE +) + + +class _CoreStoreCaller: + """Runs the calls that Core makes on a Store written in Python. + + Core calls :py:meth:`start` from a thread of the bridge. Each call runs as a task on + the Worker's event loop, as a call of client or Activity code does, and never on a + Workflow thread. The bridge cancels the task when Core stops the call. + """ + + def __init__(self, store: StreamStore) -> None: + self._store = store + self._loop: asyncio.AbstractEventLoop | None = None + + def bind(self, loop: asyncio.AbstractEventLoop) -> None: + """Run the later calls on ``loop``, the event loop of the Worker.""" + self._loop = loop + + def start( + self, + operation: str, + request: bytes, + done: Callable[[bytes | None, int, str], None], + ) -> Callable[[], None]: + """Start one call on the event loop, and give what cancels it. + + ``done`` gets the encoded response, or ``None`` with the reason and the message + of the failure. + """ + loop = self._loop + if loop is None: + raise RuntimeError("The Worker of the Stream Store isn't running") + call = self._call(operation, request, done) + try: + future = asyncio.run_coroutine_threadsafe(call, loop) + except BaseException: + call.close() + raise + + def cancel() -> None: + # A closed loop has no task left, and its cancel would raise. + if not loop.is_closed(): + future.cancel() + + return cancel + + async def _call( + self, + operation: str, + request: bytes, + done: Callable[[bytes | None, int, str], None], + ) -> None: + stream_operation = _STREAM_OPERATIONS[operation] + name = self._store.name + try: + response = await stream_operation.call( + self._store, stream_operation.request_type.FromString(request) + ) + except temporalio.exceptions.StreamError as err: + if ( + err.reason + != temporalio.api.enums.v1.StreamErrorReason.STREAM_ERROR_REASON_UNSPECIFIED + ): + done(None, err.reason, err.message) + return + done( + None, + _STORAGE_UNAVAILABLE, + f"The Stream Store '{name}' raised a StreamError with no reason: " + f"{err.message}", + ) + except Exception as err: + # Workflow code gets only the message, so the Worker logs the traceback. + logger.warning( + "The Stream Store '%s' raised in %s", name, operation, exc_info=True + ) + done( + None, + _STORAGE_UNAVAILABLE, + f"The Stream Store '{name}' raised {type(err).__name__}: {err}", + ) + else: + done(response.SerializeToString(), 0, "") + + +@dataclasses.dataclass(frozen=True) +class _CoreStreamRuntimes: + """The Stream runtimes that a Worker gives Core, and the callers of its Python Stores.""" + + runtimes: list[tuple[str, Any]] + """The bridge's object of each Store's Stream runtime, by Store name.""" + + callers: list[_CoreStoreCaller] + + def bind(self, loop: asyncio.AbstractEventLoop) -> None: + """Run the calls of each Store written in Python on ``loop``.""" + for caller in self.callers: + caller.bind(loop) + + +def _core_stream_runtimes( # pyright: ignore[reportUnusedFunction] + config: StreamConfig | None, runtime: temporalio.runtime.Runtime +) -> _CoreStreamRuntimes: + """Give Core a Stream runtime for each Store of the config. + + A Store on the bridge has its own runtime. A Store written in Python gets one on + ``runtime`` that calls it on the event loop that :py:meth:`_CoreStreamRuntimes.bind` + gives. + """ if config is None: - return [] - return [(name, runtime._ref) for name, runtime in config._bridge_stream_runtimes()] + return _CoreStreamRuntimes(runtimes=[], callers=[]) + runtimes = [ + (name, stream_runtime._ref) + for name, stream_runtime in config._bridge_stream_runtimes() + ] + callers: list[_CoreStoreCaller] = [] + if _CORE_CALLS_PYTHON_STORES: + for store in config.stores: + if isinstance(store, _BridgeStreamStore): + continue + caller = _CoreStoreCaller(store) + stream_runtime = temporalio.bridge.streams.StreamRuntime.new_python( + runtime._core_runtime, name=store.name, caller=caller + ) + runtimes.append((store.name, stream_runtime._ref)) + callers.append(caller) + return _CoreStreamRuntimes(runtimes=runtimes, callers=callers) + + +def _core_calls(store: StreamStore) -> bool: + """Whether Core can call ``store`` for a read, an append and a close.""" + return isinstance(store, _BridgeStreamStore) or _CORE_CALLS_PYTHON_STORES def _workflow_target() -> StorageDriverWorkflowInfo: @@ -452,7 +591,7 @@ def start_operation( options = self.config.workflow_call_options if isinstance(request, temporalio.api.streamservice.v1.ReadRecordsRequest): instance._assert_not_read_only("start stream read") - if not isinstance(store, _BridgeStreamStore): + if not _core_calls(store): raise _no_direct_call_error(store, input.operation_name) return _start_direct_read( instance, @@ -470,7 +609,7 @@ def start_operation( temporalio.api.streamservice.v1.CloseStreamRequest, ), ): - if not isinstance(store, _BridgeStreamStore): + if not _core_calls(store): raise _no_direct_call_error(store, input.operation_name) return _start_direct_call(instance, input, request, store.name) diff --git a/temporalio/worker/_worker.py b/temporalio/worker/_worker.py index e54ebef1cd..4983f9a0f0 100644 --- a/temporalio/worker/_worker.py +++ b/temporalio/worker/_worker.py @@ -38,7 +38,7 @@ _bridge_read_ahead_bytes, _bridge_read_min_gap, _bridge_read_page_records, - _bridge_stream_runtimes, + _core_stream_runtimes, _WorkflowStreamStore, ) from ._tuning import WorkerTuner @@ -654,6 +654,13 @@ def check_activity(activity: str): driver.type() for driver in self._storage_drivers } + # Only Workflow code makes Direct calls, so a Worker with no Workflows builds no + # runtime, as it builds no Store Activity. + self._core_stream_runtimes = _core_stream_runtimes( + stream_config if self._workflow_worker is not None else None, + self._runtime, + ) + # Create bridge worker last. We have empirically observed that if it is # created before an error is raised from the activity worker # constructor, a deadlock/hang will occur presumably while trying to @@ -737,11 +744,7 @@ def check_activity(activity: str): disable_payload_error_limit=config.get( "disable_payload_error_limit", False ), - # Only Workflow code makes Direct calls, so a Worker with no Workflows - # builds no runtime, as it builds no Store Activity. - stream_runtimes=_bridge_stream_runtimes( - stream_config if self._workflow_worker is not None else None - ), + stream_runtimes=self._core_stream_runtimes.runtimes, stream_read_ahead_bytes=_bridge_read_ahead_bytes(stream_config), stream_read_page_records=_bridge_read_page_records(stream_config), stream_read_min_gap=_bridge_read_min_gap(stream_config), @@ -867,6 +870,8 @@ async def _run(self): if self._started: raise RuntimeError("Already started") self._started = True + # Core calls a Store written in Python on the loop that runs the Worker. + self._core_stream_runtimes.bind(asyncio.get_running_loop()) # Eagerly validate which will do a namespace check in Core. try: diff --git a/tests/helpers/streams.py b/tests/helpers/streams.py index f2a3aad34e..b06179a476 100644 --- a/tests/helpers/streams.py +++ b/tests/helpers/streams.py @@ -3,25 +3,18 @@ from __future__ import annotations import asyncio -import contextlib import dataclasses from collections.abc import Awaitable, Callable from typing import TypeVar from google.protobuf.message import Message -from google.protobuf.timestamp_pb2 import Timestamp from temporalio.api.enums.v1 import ( StreamErrorReason, StreamIdConflictPolicy, StreamStatus, ) -from temporalio.api.stream.v1 import ( - Producer, - StreamRecord, - StreamRef, - WorkflowTaskEpoch, -) +from temporalio.api.stream.v1 import StreamRecord, StreamRef from temporalio.api.streamservice.v1 import ( AppendRecordsRequest, AppendRecordsResponse, @@ -92,64 +85,35 @@ async def describe_stream( ) -@dataclasses.dataclass -class _LastBatch: - epoch: tuple[int, ...] - sequence: int - response: AppendRecordsResponse - - @dataclasses.dataclass class _DictStream: request_id: str records: list[StreamRecord] = dataclasses.field(default_factory=list) closed: bool = False - # The id of a Producer is unique in one Stream. - last_batches: dict[str, _LastBatch] = dataclasses.field(default_factory=dict) + changed: asyncio.Event = dataclasses.field(default_factory=asyncio.Event) + + def wake(self) -> None: + """Wake the reads that wait, and give later reads a new event.""" + self.changed.set() + self.changed = asyncio.Event() def _offset(index: int) -> bytes: return index.to_bytes(8, "big") -def _epoch(producer: Producer) -> tuple[int, ...]: - """Give a key that orders the epochs of one Producer, as ``Producer`` documents it.""" - kind = producer.WhichOneof("kind") - task = WorkflowTaskEpoch() - scheduled = Timestamp() - attempt = 0 - if kind == "workflow": - task = producer.workflow.epoch - elif kind == "local_activity": - task = producer.local_activity.epoch.task - attempt = producer.local_activity.epoch.attempt - elif kind in ("workflow_activity", "standalone_activity"): - scheduled = getattr(producer, kind).epoch.attempt_scheduled_time - attempt = getattr(producer, kind).epoch.attempt - return ( - task.started_event_id, - task.attempt, - task.started_time.seconds, - task.started_time.nanos, - scheduled.seconds, - scheduled.nanos, - attempt, - ) - - class DictStreamStore(StreamStore): - """A Store that keeps its Streams in a dict, for the tests. + """A Store written from scratch, with no bridge, that keeps its Streams in a dict. - It has no Retention and no expiry, and it checks no request. It resends the answer - of a batch that comes again with the same Producer, epoch and sequence, and it - fences a Producer whose epoch is lower than its newest one. A read waits for a new - Record up to the ``wait`` of its request. + It has enough of each operation for the tests, and no dedupe and no Retention. A + read with a ``wait`` waits for an append or a close, as Core needs for Workflow + code. :py:attr:`waiting` counts the reads that wait, so a test sees one that stays. """ def __init__(self, name: str = "dict") -> None: self._name = name self.streams: dict[str, _DictStream] = {} - self._changed = asyncio.Condition() + self.waiting = 0 @property def name(self) -> str: @@ -164,10 +128,6 @@ def _stream(self, ref: StreamRef) -> _DictStream: ) return stream - async def _notify(self) -> None: - async with self._changed: - self._changed.notify_all() - async def create_stream(self, request: CreateStreamRequest) -> CreateStreamResponse: ref = StreamRef(id=request.stream_ref.id, store=self.name) stream = self.streams.get(ref.id) @@ -190,22 +150,11 @@ async def append_records( self, request: AppendRecordsRequest ) -> AppendRecordsResponse: stream = self._stream(request.ref) - epoch = _epoch(request.producer) - last = stream.last_batches.get(request.producer.id) - previous = last if last is not None and last.epoch == epoch else None - sequence = request.sequence or (previous.sequence + 1 if previous else 1) - if previous is not None and sequence == previous.sequence: - return previous.response if stream.closed: raise StreamError( f"The Stream '{request.ref.id}' is closed", reason=StreamErrorReason.STREAM_ERROR_REASON_CLOSED, ) - if last is not None and epoch < last.epoch: - raise StreamError( - "The epoch is lower than the newest one of the Producer", - reason=StreamErrorReason.STREAM_ERROR_REASON_FENCED, - ) first = len(stream.records) + 1 for record in request.records: stream.records.append( @@ -215,26 +164,24 @@ async def append_records( offset=_offset(len(stream.records) + 1), ) ) - response = AppendRecordsResponse( + if request.records: + stream.wake() + return AppendRecordsResponse( first_offset=_offset(first), last_offset=_offset(len(stream.records)) ) - stream.last_batches[request.producer.id] = _LastBatch(epoch, sequence, response) - await self._notify() - return response async def read_records(self, request: ReadRecordsRequest) -> ReadRecordsResponse: stream = self._stream(request.ref) start = int.from_bytes(request.from_offset, "big") if request.from_offset else 0 - wait = request.wait.ToTimedelta() - if wait and start >= len(stream.records) and not stream.closed: - async with self._changed: - with contextlib.suppress(TimeoutError): - await asyncio.wait_for( - self._changed.wait_for( - lambda: len(stream.records) > start or stream.closed - ), - wait.total_seconds(), - ) + wait = request.wait.ToTimedelta().total_seconds() + if start >= len(stream.records) and not stream.closed and wait > 0: + self.waiting += 1 + try: + await asyncio.wait_for(stream.changed.wait(), wait) + except TimeoutError: + pass + finally: + self.waiting -= 1 page = stream.records[start : start + (request.max_records or 1000)] last_offset = page[-1].offset if page else request.from_offset return ReadRecordsResponse( @@ -244,8 +191,9 @@ async def read_records(self, request: ReadRecordsRequest) -> ReadRecordsResponse ) async def close_stream(self, request: CloseStreamRequest) -> CloseStreamResponse: - self._stream(request.ref).closed = True - await self._notify() + stream = self._stream(request.ref) + stream.closed = True + stream.wake() return CloseStreamResponse() async def describe_stream( diff --git a/tests/nexus/test_stream_direct.py b/tests/nexus/test_stream_direct.py index 8ec405eb26..9c55d977d2 100644 --- a/tests/nexus/test_stream_direct.py +++ b/tests/nexus/test_stream_direct.py @@ -16,6 +16,12 @@ from temporalio import activity, workflow from temporalio.api.common.v1 import Payload from temporalio.api.enums.v1 import EventType, StreamErrorReason +from temporalio.api.streamservice.v1 import ( + AppendRecordsRequest, + AppendRecordsResponse, + ReadRecordsRequest, + ReadRecordsResponse, +) from temporalio.bridge.proto.external_data import ( StreamCloseEntry, StreamMarkerData, @@ -58,7 +64,7 @@ WorkflowOutboundInterceptor, ) from tests.helpers import assert_eventually, assert_task_fail_eventually, new_worker -from tests.helpers.streams import DictStreamStore +from tests.helpers.streams import DictStreamStore, RecordingStreamStore from tests.test_converter import SimpleCodec # A Record body that a test can look for in the bytes of History. @@ -1533,57 +1539,195 @@ async def test_continue_as_new_with_an_open_read(client: Client): @workflow.defn -class PythonStoreAppendWorkflow: +class PythonStoreAppendCloseWorkflow: @workflow.run async def run(self, ref: StreamRef[str]) -> None: - await workflow.get_stream(ref, body_type=str).append("a") + stream = workflow.get_stream(ref, body_type=str) + await stream.append("a", "b") + await stream.close() -@workflow.defn -class PythonStoreCloseWorkflow: - @workflow.run - async def run(self, ref: StreamRef[str]) -> None: - await workflow.get_stream(ref, body_type=str).close() +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_python_store_takes_a_workflow_append_and_close( + client: Client, max_cached_workflows: int +): + store = DictStreamStore() + stream_client = _stream_client(client, store) + ref = await _filled_stream(stream_client, [], close=False) + assert ref.store == "dict" + handle, _ = await _run( + stream_client, + PythonStoreAppendCloseWorkflow, + ref, + max_cached_workflows=max_cached_workflows, + ) + assert [record.body for record in await _read_all(stream_client, ref)] == [ + "a", + "b", + ] + assert await _status(stream_client, ref) == StreamStatus.StreamStatusClosed + # The Store gets the Workflow's Producer with the Workflow Task as its epoch. + producer = store.streams[ref.id].records[0].producer + assert producer.WhichOneof("kind") == "workflow" + assert producer.workflow.run_id == handle.result_run_id + assert producer.workflow.epoch.started_event_id > 0 + # Core made the calls, with no Local Activity. + history = await handle.fetch_history() + assert "core_local_activity" not in _markers(history) + assert "core_stream" in _markers(history) -async def _python_store_failure( - client: Client, workflow_type: type[Any], ref: StreamRef[str] -) -> ApplicationError: - with pytest.raises(WorkflowFailureError) as err: - await _run(client, workflow_type, ref) - cause = err.value.cause - assert isinstance(cause, ApplicationError) - assert cause.type == "StreamConfigError" - assert cause.non_retryable - return cause +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_python_store_gives_a_workflow_the_records_of_a_running_activity( + client: Client, max_cached_workflows: int +): + store = DictStreamStore() + stream_client = _stream_client(client, store) + chunks = await _filled_stream(stream_client, [], close=False) + seen = await _filled_stream(stream_client, [], close=False) + _, text = await _run( + stream_client, + LiveModelWorkflow, + ModelCall(chunks.id, seen.id), + activities=[stream_model_output], + max_cached_workflows=max_cached_workflows, + ) + assert text == "Hello, world" + assert store.waiting == 0 -async def test_python_store_refuses_an_append(client: Client): - stream_client = _stream_client(client, DictStreamStore()) +@pytest.mark.parametrize("max_cached_workflows", [1000, 0]) +async def test_python_store_read_with_wait_on_a_quiet_stream_gives_an_empty_page( + client: Client, max_cached_workflows: int +): + store = DictStreamStore() + stream_client = _stream_client(client, store) ref = await _filled_stream(stream_client, [], close=False) - cause = await _python_store_failure(stream_client, PythonStoreAppendWorkflow, ref) - assert cause.message == ( - "The Stream Store 'dict' has no Direct append, since it is written in Python. " - "Append to the Stream in an Activity, or use a Store that runs on the bridge, " - "such as the Redis Store or MemoryStreamStore." + handle, result = await _run( + stream_client, + ReadOnePageWorkflow, + ref, + 1000, + max_cached_workflows=max_cached_workflows, ) - assert await _read_all(stream_client, ref) == [] + assert result == [[], False, None] + events = (await handle.fetch_history()).events + elapsed = events[-1].event_time.ToDatetime() - events[0].event_time.ToDatetime() + assert elapsed >= timedelta(seconds=1) + assert store.waiting == 0 -async def test_python_store_refuses_a_read(client: Client): - stream_client = _stream_client(client, DictStreamStore()) - ref = await _filled_stream(stream_client, ["a"]) - with pytest.raises(WorkflowFailureError) as err: - await _run(stream_client, ReadPagesWorkflow, ReadArgs(ref.id, store="dict")) - cause = err.value.cause - assert isinstance(cause, ApplicationError) - assert cause.type == "StreamConfigError" - assert cause.non_retryable - assert cause.message == ( - "The Stream Store 'dict' has no Direct read, since it is written in Python. " - "Read the Stream in an Activity, or use a Store that runs on the bridge, such " - "as the Redis Store or MemoryStreamStore." +async def test_python_store_read_replays_from_the_store(client: Client): + store = DictStreamStore() + stream_client = _stream_client(client, store) + ref = await _filled_stream(stream_client, ["a", "b", "c"]) + handle, result = await _run( + stream_client, ReadPagesWorkflow, ReadArgs(ref.id, store="dict") ) + assert result.bodies == ["a", "b", "c"] + # History holds no Record, so the replay reads them from the Python Store again. + recording = RecordingStreamStore(store) + await Replayer( + workflows=[ReadPagesWorkflow], + stream_config=StreamConfig(stores=[recording]), + ).replay_workflow(await handle.fetch_history()) + assert "ReadRecords" in recording.operations + + +class ClosedOnAppendStore(DictStreamStore): + async def append_records( + self, request: AppendRecordsRequest + ) -> AppendRecordsResponse: + raise StreamError( + "The Store closed the Stream", + reason=StreamErrorReason.STREAM_ERROR_REASON_CLOSED, + ) + + +class BrokenReadStore(DictStreamStore): + async def read_records(self, request: ReadRecordsRequest) -> ReadRecordsResponse: + raise RuntimeError("the read broke") + + +@workflow.defn +class PythonStoreErrorWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str], call: str) -> list[Any]: + stream = workflow.get_stream(ref, body_type=str) + try: + if call == "append": + await stream.append("a") + else: + await stream.read() + except StreamError as err: + return [StreamErrorReason.Name(err.reason), err.message] + return ["no error"] + + +async def test_python_store_error_reaches_workflow_code_with_its_reason( + client: Client, +): + stream_client = _stream_client(client, ClosedOnAppendStore()) + ref = await _filled_stream(stream_client, [], close=False) + _, result = await _run(stream_client, PythonStoreErrorWorkflow, ref, "append") + assert result == ["STREAM_ERROR_REASON_CLOSED", "The Store closed the Stream"] + + +async def test_python_store_exception_is_a_stream_failure_and_the_worker_goes_on( + client: Client, +): + stream_client = _stream_client(client, BrokenReadStore()) + ref = await _filled_stream(stream_client, [], close=False) + async with new_worker( + stream_client, PythonStoreErrorWorkflow, PythonStoreAppendCloseWorkflow + ) as worker: + # Core tries a Store that fails again for 30 s, as for an unavailable Store. + result = await stream_client.execute_workflow( + PythonStoreErrorWorkflow.run, + args=[ref, "read"], + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert result == [ + "STREAM_ERROR_REASON_STORAGE_UNAVAILABLE", + "The Stream Store 'dict' raised RuntimeError: the read broke", + ] + # The same Worker serves the next Workflow. + await stream_client.execute_workflow( + PythonStoreAppendCloseWorkflow.run, + ref, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + assert await _status(stream_client, ref) == StreamStatus.StreamStatusClosed + + +async def test_python_store_read_that_a_cancel_stops_leaves_no_task(client: Client): + store = DictStreamStore() + stream_client = _stream_client(client, store) + ref = await _filled_stream(stream_client, ["a"], close=False) + async with new_worker(stream_client, CancelDuringReadWorkflow) as worker: + handle = await stream_client.start_workflow( + CancelDuringReadWorkflow.run, + ref.id, + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + + async def read_waits() -> None: + assert store.waiting == 1 + + await assert_eventually(read_waits) + await handle.cancel() + assert await asyncio.wait_for(handle.result(), timeout=30) == [ + ["a"], + "cancelled", + ] + + async def no_read_waits() -> None: + assert store.waiting == 0 + + await assert_eventually(no_read_waits) # A payload codec. @@ -2224,18 +2368,6 @@ async def test_cancel_while_the_close_waits(client: Client): assert await _status(stream_client, ref) == StreamStatus.StreamStatusClosed -async def test_python_store_refuses_a_close(client: Client): - stream_client = _stream_client(client, DictStreamStore()) - ref = await _filled_stream(stream_client, [], close=False) - cause = await _python_store_failure(stream_client, PythonStoreCloseWorkflow, ref) - assert cause.message == ( - "The Stream Store 'dict' has no Direct close, since it is written in Python. " - "Close the Stream in an Activity, or use a Store that runs on the bridge, " - "such as the Redis Store or MemoryStreamStore." - ) - assert await _status(stream_client, ref) == StreamStatus.StreamStatusOpen - - # The metrics of the Direct calls. diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index 7ee8ca22a7..f24eaa3ebd 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -604,7 +604,7 @@ async def test_workflow_create_without_stream_config_fails_the_workflow( ) -async def test_workflow_read_through_a_nexus_client_needs_a_runtime( +async def test_workflow_read_through_a_nexus_client_goes_through_core( client: Client, ): async def read_with(store: StreamStore) -> str: @@ -617,13 +617,10 @@ async def read_with(store: StreamStore) -> str: task_queue=worker.task_queue, ) - # Core reads a Store with a Rust runtime, as for `read`. Nobody created the Stream. + # Core reads the Store, as for `read`, also one written in Python. Nobody created + # the Stream. assert await read_with(MemoryStreamStore()) == "STREAM_ERROR_REASON_NOT_FOUND" - assert await read_with(DictStreamStore()) == ( - "The Stream Store 'dict' has no Direct read, since it is written in Python. " - "Read the Stream in an Activity, or use a Store that runs on the bridge, such " - "as the Redis Store or MemoryStreamStore." - ) + assert await read_with(DictStreamStore()) == "STREAM_ERROR_REASON_NOT_FOUND" def test_workflow_stream_has_the_calls_of_a_client_stream(): From e535849e2d5580d1b82eb50906b64058bcc2de49 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Tue, 17 Nov 2026 16:31:57 -0700 Subject: [PATCH 28/35] Stopped retrying an unexpected exception of a Python Stream Store. A bug in a Store never gets better, so a retry only delayed the error. Such an exception reaches Workflow code on the first try as a non-retryable `STORAGE_CORRUPT`, and an outage still retries. --- changelog/added/patient-river-heron.md | 2 +- temporalio/bridge/src/streams.rs | 2 +- temporalio/client/_stream_store.py | 6 ++-- temporalio/contrib/streams/README.md | 4 +-- temporalio/worker/_stream_store.py | 10 +++--- tests/nexus/test_stream_direct.py | 49 +++++++++++++++++++++++--- 6 files changed, 58 insertions(+), 15 deletions(-) diff --git a/changelog/added/patient-river-heron.md b/changelog/added/patient-river-heron.md index f08a60bc6d..a6218197af 100644 --- a/changelog/added/patient-river-heron.md +++ b/changelog/added/patient-river-heron.md @@ -1,4 +1,4 @@ -Workflow code creates a Stream, appends Records to it, reads it, describes it and closes it, with `workflow.create_stream` and `workflow.get_stream`. A create and a describe run in the Store's Local Activity, with the options of `StreamConfig.workflow_call_options`. Core makes a read, an append and a close itself, with no Activity, on the Redis Store, on `MemoryStreamStore` and on a Store written in Python. Core calls a Store written in Python as a task on the Worker's event loop, never on a Workflow thread. For Workflow code, such a Store waits up to a read's `wait`, ends that wait on a cancel, gives offsets of 1 to 1024 bytes that compare in order, fences a lower epoch of a Producer, writes a resent batch once, and raises `StreamError` with a reason. Any other exception counts as an outage, which Core tries again for up to 30 s. `Replayer` takes the `stream_config` of the Worker's client. +Workflow code creates a Stream, appends Records to it, reads it, describes it and closes it, with `workflow.create_stream` and `workflow.get_stream`. A create and a describe run in the Store's Local Activity, with the options of `StreamConfig.workflow_call_options`. Core makes a read, an append and a close itself, with no Activity, on the Redis Store, on `MemoryStreamStore` and on a Store written in Python. Core calls a Store written in Python as a task on the Worker's event loop, never on a Workflow thread. For Workflow code, such a Store waits up to a read's `wait`, ends that wait on a cancel, gives offsets of 1 to 1024 bytes that compare in order, fences a lower epoch of a Producer, writes a resent batch once, and raises `StreamError` with a reason. Core tries a call again for up to 30 s only on `STREAM_ERROR_REASON_STORAGE_UNAVAILABLE`. Any other exception reaches Workflow code on the first try, as a non-retryable `STREAM_ERROR_REASON_STORAGE_CORRUPT` with its type in the message. `Replayer` takes the `stream_config` of the Worker's client. The Workflow `Stream` has `read`, `iter_read` and `iter_read_items`, so Workflow code reads a Stream with the same calls as a client. Core reads the Store inside the Workflow Task. A page that Workflow code gets in one activation has at most 256 Records, and the rest come in the next activation with no wait. A loop that asks for its next page in the same activation keeps one read open, and a read that no code waits on closes before the activation completes, so it never holds a Workflow Task open. With a `wait`, a quiet Stream gives empty pages, so a loop on a Stream that can be quiet for long uses `iter_read` and leaves after an empty page. A call that waits on a quiet Stream holds its Workflow Task open, with one Workflow Task about every 8 s. The calls of one Workflow Task write one marker in History. `StreamConfig.workflow_read_replay_from` decides whether the marker holds the Records (`StreamReplaySource.HISTORY`) or only their offsets and a digest, which a replay reads from the Store again (`StreamReplaySource.STORE`, the default). With `HISTORY`, one Workflow Task's marker holds at most 1 MiB of Records, and the read goes on in the next task. A Workflow's append runs in Core inside the Workflow Task, with no Activity. It writes as the Workflow, with its Workflow Task as the epoch. A `producer=` of another kind raises `StreamError` with the reason `STREAM_ERROR_REASON_INVALID`. A Workflow's close runs in Core, with no Activity, and spans two Workflow Tasks. The task that calls `close` records the intent, Core closes the Stream after the server took that task, and the next task gives the result, so a failed task closes nothing. Await the close. A graceful shutdown of the Worker waits at most 10 s for a close that the code doesn't await, and a Worker that dies when the Workflow ends loses it. An append and a close of Workflow code always carry the Workflow Task as their epoch. An append is at-least-once. After a Worker crash or an eviction, the rerun of its task can write a second copy with the epoch of the new task. diff --git a/temporalio/bridge/src/streams.rs b/temporalio/bridge/src/streams.rs index 16a189c9e7..bb45bbde86 100644 --- a/temporalio/bridge/src/streams.rs +++ b/temporalio/bridge/src/streams.rs @@ -209,7 +209,7 @@ mod python_store { }), Ok(Err((value, message))) => match reason(value) { Some(reason) => Err(StreamError::new(reason, message)), - None => Err(StreamError::storage_unavailable(format!( + None => Err(StreamError::storage_corrupt(format!( "The Stream Store failed with the unknown reason {value}: {message}" ))), }, diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index c3f15b590c..25c57e47ed 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -181,8 +181,10 @@ class StreamStore(ABC): * An outage raises :py:class:`temporalio.exceptions.StreamError` with the reason ``STREAM_ERROR_REASON_STORAGE_UNAVAILABLE``. Core then tries a call of Workflow code again for up to 30 s. Every other reason reaches Workflow code on the first - try. Any other exception, or a ``StreamError`` with no reason, counts as an outage, - and the Worker logs it. Its message tells the type of the exception. + try. Any other exception, or a ``StreamError`` with no reason, is a fault that a + retry can't fix. Workflow code gets it on the first try as a non-retryable + ``STREAM_ERROR_REASON_STORAGE_CORRUPT``, whose message tells the type of the + exception, and the Worker logs it. .. warning:: This API is experimental and subject to change. diff --git a/temporalio/contrib/streams/README.md b/temporalio/contrib/streams/README.md index fa7d7dee8e..4556d65600 100644 --- a/temporalio/contrib/streams/README.md +++ b/temporalio/contrib/streams/README.md @@ -168,8 +168,8 @@ loop, never on a Workflow thread. For Workflow code, such a Store must do these - `close_stream` of a closed Stream succeeds. - The Store keeps the Records with their offsets for as long as a replay reads them. - An outage raises `StreamError` with `STREAM_ERROR_REASON_STORAGE_UNAVAILABLE`, and Core tries - the call again for up to 30 s. Any other exception counts as an outage too, and its type is in - the message that Workflow code gets. + the call again for up to 30 s. Any other exception is not tried again. Workflow code gets it + as a non-retryable `STREAM_ERROR_REASON_STORAGE_CORRUPT`, with its type in the message. ## Metrics diff --git a/temporalio/worker/_stream_store.py b/temporalio/worker/_stream_store.py index 612a21f031..eca5eb9e47 100644 --- a/temporalio/worker/_stream_store.py +++ b/temporalio/worker/_stream_store.py @@ -415,8 +415,10 @@ def _bridge_read_min_gap(config: StreamConfig | None) -> timedelta | None: # py return config.workflow_read_min_gap if config is not None else None -_STORAGE_UNAVAILABLE = ( - temporalio.api.enums.v1.StreamErrorReason.STREAM_ERROR_REASON_STORAGE_UNAVAILABLE +# A Store that raises something other than a `StreamError` with a reason has a fault that a +# retry reads again, as with corrupt data, so Core gives the failure on the first try. +_STORE_FAULT = ( + temporalio.api.enums.v1.StreamErrorReason.STREAM_ERROR_REASON_STORAGE_CORRUPT ) @@ -485,7 +487,7 @@ async def _call( return done( None, - _STORAGE_UNAVAILABLE, + _STORE_FAULT, f"The Stream Store '{name}' raised a StreamError with no reason: " f"{err.message}", ) @@ -496,7 +498,7 @@ async def _call( ) done( None, - _STORAGE_UNAVAILABLE, + _STORE_FAULT, f"The Stream Store '{name}' raised {type(err).__name__}: {err}", ) else: diff --git a/tests/nexus/test_stream_direct.py b/tests/nexus/test_stream_direct.py index 9c55d977d2..fac0a44961 100644 --- a/tests/nexus/test_stream_direct.py +++ b/tests/nexus/test_stream_direct.py @@ -1660,7 +1660,7 @@ async def run(self, ref: StreamRef[str], call: str) -> list[Any]: else: await stream.read() except StreamError as err: - return [StreamErrorReason.Name(err.reason), err.message] + return [StreamErrorReason.Name(err.reason), err.message, err.non_retryable] return ["no error"] @@ -1670,7 +1670,44 @@ async def test_python_store_error_reaches_workflow_code_with_its_reason( stream_client = _stream_client(client, ClosedOnAppendStore()) ref = await _filled_stream(stream_client, [], close=False) _, result = await _run(stream_client, PythonStoreErrorWorkflow, ref, "append") - assert result == ["STREAM_ERROR_REASON_CLOSED", "The Store closed the Stream"] + assert result == [ + "STREAM_ERROR_REASON_CLOSED", + "The Store closed the Stream", + True, + ] + + +class UnavailableTwiceStore(DictStreamStore): + """A Store whose first two appends fail with an outage.""" + + def __init__(self) -> None: + super().__init__() + self.failed_appends = 0 + + async def append_records( + self, request: AppendRecordsRequest + ) -> AppendRecordsResponse: + if self.failed_appends < 2: + self.failed_appends += 1 + raise StreamError( + "The Store is down", + reason=StreamErrorReason.STREAM_ERROR_REASON_STORAGE_UNAVAILABLE, + ) + return await super().append_records(request) + + +async def test_python_store_outage_is_tried_again(client: Client): + store = UnavailableTwiceStore() + stream_client = _stream_client(client, store) + ref = await _filled_stream(stream_client, [], close=False) + _, offsets = await _run(stream_client, AppendWorkflow, ref) + # Core tried the append again after each outage, and the third try wrote it. + assert store.failed_appends == 2 + assert offsets == [(1).to_bytes(8, "big").hex(), (2).to_bytes(8, "big").hex()] + assert [record.body for record in await _read_all(stream_client, ref)] == [ + "a", + "b", + ] async def test_python_store_exception_is_a_stream_failure_and_the_worker_goes_on( @@ -1681,16 +1718,18 @@ async def test_python_store_exception_is_a_stream_failure_and_the_worker_goes_on async with new_worker( stream_client, PythonStoreErrorWorkflow, PythonStoreAppendCloseWorkflow ) as worker: - # Core tries a Store that fails again for 30 s, as for an unavailable Store. - result = await stream_client.execute_workflow( + handle = await stream_client.start_workflow( PythonStoreErrorWorkflow.run, args=[ref, "read"], id=f"workflow-{uuid.uuid4()}", task_queue=worker.task_queue, ) + # A retry can't fix the Store, so Core gives the failure on the first try. + result = await asyncio.wait_for(handle.result(), timeout=10) assert result == [ - "STREAM_ERROR_REASON_STORAGE_UNAVAILABLE", + "STREAM_ERROR_REASON_STORAGE_CORRUPT", "The Stream Store 'dict' raised RuntimeError: the read broke", + True, ] # The same Worker serves the next Workflow. await stream_client.execute_workflow( From b3c8a611003e1ddd10807733cda4fc6f2d59a119 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Tue, 17 Nov 2026 18:37:08 -0700 Subject: [PATCH 29/35] Fitted the Workflow reads of a Stream to the new names of a polled read. The Workflow `Stream` has `read`, `read_pages` and `read_page` from commit 07b565b6 of the nexgen fork. A Stream call no longer reaches the generic hook, so the page count test uses `start_read_records`. --- .github/workflows/ci.yml | 5 +- changelog/added/patient-river-heron.md | 2 +- scripts/gen_nexus_system_api.py | 6 +- temporalio/client/_stream_store.py | 6 +- temporalio/contrib/streams/README.md | 8 +- .../system/stream_service/workflow/_poll.py | 77 ----------- .../workflow/_resources/stream.py | 14 +- temporalio/worker/_stream_store.py | 8 +- .../contrib/streams/test_appender_workflow.py | 4 +- tests/nexus/test_stream_direct.py | 121 +++++++++--------- tests/nexus/test_stream_store_workflow.py | 16 +-- 11 files changed, 94 insertions(+), 173 deletions(-) delete mode 100644 temporalio/nexus/system/stream_service/workflow/_poll.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f121ae1dd3..da1b43d780 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -227,8 +227,9 @@ jobs: - run: uv sync --all-extras - run: cargo install --locked nexgen --version 0.2.8 --features advanced --force # The Stream package needs `@nexus.helper` with `sync=true`, which only a nexgen fork has. - # This commit of the fork also has the poll iterators of the Workflow target. - - run: cargo install --locked --git https://github.com/moetemp/nexgen --rev 2785462ecdaba5da7a27051fbaaccbaee26ae9e0 --features advanced --root "$RUNNER_TEMP/nexgen-stream" nexgen + # This commit of the fork also has the poll iterators of the Workflow target and the read, + # read pages and read page methods of a poll. + - run: cargo install --locked --git https://github.com/moetemp/nexgen --rev 07b565b6d4339dc8a3f67ce2e9e72ba4925d7f9c --features advanced --root "$RUNNER_TEMP/nexgen-stream" nexgen - run: echo "NEX_GEN_STREAM_BIN=$RUNNER_TEMP/nexgen-stream/bin/nexgen" >> "$GITHUB_ENV" - run: poe build-develop - run: poe gen-protos diff --git a/changelog/added/patient-river-heron.md b/changelog/added/patient-river-heron.md index a6218197af..296756d6f8 100644 --- a/changelog/added/patient-river-heron.md +++ b/changelog/added/patient-river-heron.md @@ -1,5 +1,5 @@ Workflow code creates a Stream, appends Records to it, reads it, describes it and closes it, with `workflow.create_stream` and `workflow.get_stream`. A create and a describe run in the Store's Local Activity, with the options of `StreamConfig.workflow_call_options`. Core makes a read, an append and a close itself, with no Activity, on the Redis Store, on `MemoryStreamStore` and on a Store written in Python. Core calls a Store written in Python as a task on the Worker's event loop, never on a Workflow thread. For Workflow code, such a Store waits up to a read's `wait`, ends that wait on a cancel, gives offsets of 1 to 1024 bytes that compare in order, fences a lower epoch of a Producer, writes a resent batch once, and raises `StreamError` with a reason. Core tries a call again for up to 30 s only on `STREAM_ERROR_REASON_STORAGE_UNAVAILABLE`. Any other exception reaches Workflow code on the first try, as a non-retryable `STREAM_ERROR_REASON_STORAGE_CORRUPT` with its type in the message. `Replayer` takes the `stream_config` of the Worker's client. -The Workflow `Stream` has `read`, `iter_read` and `iter_read_items`, so Workflow code reads a Stream with the same calls as a client. Core reads the Store inside the Workflow Task. A page that Workflow code gets in one activation has at most 256 Records, and the rest come in the next activation with no wait. A loop that asks for its next page in the same activation keeps one read open, and a read that no code waits on closes before the activation completes, so it never holds a Workflow Task open. With a `wait`, a quiet Stream gives empty pages, so a loop on a Stream that can be quiet for long uses `iter_read` and leaves after an empty page. A call that waits on a quiet Stream holds its Workflow Task open, with one Workflow Task about every 8 s. The calls of one Workflow Task write one marker in History. `StreamConfig.workflow_read_replay_from` decides whether the marker holds the Records (`StreamReplaySource.HISTORY`) or only their offsets and a digest, which a replay reads from the Store again (`StreamReplaySource.STORE`, the default). With `HISTORY`, one Workflow Task's marker holds at most 1 MiB of Records, and the read goes on in the next task. +The Workflow `Stream` has `read`, `read_pages` and `read_page`, so Workflow code reads a Stream with the same calls as a client. Core reads the Store inside the Workflow Task. A page that Workflow code gets in one activation has at most 256 Records, and the rest come in the next activation with no wait. A loop that asks for its next page in the same activation keeps one read open, and a read that no code waits on closes before the activation completes, so it never holds a Workflow Task open. With a `wait`, a quiet Stream gives empty pages, so a loop on a Stream that can be quiet for long uses `read_pages` and leaves after an empty page. A call that waits on a quiet Stream holds its Workflow Task open, with one Workflow Task about every 8 s. The calls of one Workflow Task write one marker in History. `StreamConfig.workflow_read_replay_from` decides whether the marker holds the Records (`StreamReplaySource.HISTORY`) or only their offsets and a digest, which a replay reads from the Store again (`StreamReplaySource.STORE`, the default). With `HISTORY`, one Workflow Task's marker holds at most 1 MiB of Records, and the read goes on in the next task. A Workflow's append runs in Core inside the Workflow Task, with no Activity. It writes as the Workflow, with its Workflow Task as the epoch. A `producer=` of another kind raises `StreamError` with the reason `STREAM_ERROR_REASON_INVALID`. A Workflow's close runs in Core, with no Activity, and spans two Workflow Tasks. The task that calls `close` records the intent, Core closes the Stream after the server took that task, and the next task gives the result, so a failed task closes nothing. Await the close. A graceful shutdown of the Worker waits at most 10 s for a close that the code doesn't await, and a Worker that dies when the Workflow ends loses it. An append and a close of Workflow code always carry the Workflow Task as their epoch. An append is at-least-once. After a Worker crash or an eviction, the rerun of its task can write a second copy with the epoch of the new task. Core records metrics for the Direct calls of a Stream, such as `temporal_stream_read_total`, `temporal_stream_read_records` and `temporal_stream_close_latency`, each with the attribute `stream_store`, and `temporal_stream_close_abandoned` for a close that a graceful shutdown gave up. diff --git a/scripts/gen_nexus_system_api.py b/scripts/gen_nexus_system_api.py index b62c423ef8..224330d63a 100644 --- a/scripts/gen_nexus_system_api.py +++ b/scripts/gen_nexus_system_api.py @@ -30,8 +30,8 @@ # The Stream package needs a newer nexgen, which rejects the other WITs. One pin is enough # again once the api WITs and a nexgen release agree. The Stream WIT uses `@nexus.helper` with # `sync=true` and `@nexus.support-type`, which only the fork https://github.com/moetemp/nexgen -# has. Its commit 2785462e also has the poll iterators of the Workflow target. So -# NEX_GEN_STREAM_BIN points at a build of that commit. +# has. Its commit 07b565b6 also has the poll iterators of the Workflow target and the read, read +# pages and read page methods of a poll. So NEX_GEN_STREAM_BIN points at a build of that commit. NEX_GEN_STREAM_VERSION = "0.2.12" proto_files = [ gen_protos.api_proto_dir @@ -89,7 +89,7 @@ def nex_gen_stream_command() -> list[str]: bin_path = os.environ.get("NEX_GEN_STREAM_BIN") if not bin_path: raise RuntimeError( - "NEX_GEN_STREAM_BIN must point at a nexgen build of commit 2785462e " + "NEX_GEN_STREAM_BIN must point at a nexgen build of commit 07b565b6 " "of https://github.com/moetemp/nexgen" ) version = subprocess.check_output([bin_path, "--version"], text=True).strip() diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index 25c57e47ed..a07945d3ec 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -519,7 +519,7 @@ class StreamConfig: closes nothing. A create and a describe of Workflow code run in the Store's Local Activity. So Workflow code needs no Worker that polls for Activity Tasks. - Workflow code reads with the same ``read``, ``iter_read`` and ``iter_read_items`` as + Workflow code reads with the same ``read``, ``read_pages`` and ``read_page`` as a client. Core reads the Store inside the Workflow Task. A page that Workflow code gets in one activation has at most 256 Records, and the rest come in the next activation with no wait. A loop that asks for its next page in the same activation keeps one read open in Core. A read that no code waits on closes before @@ -527,7 +527,7 @@ class StreamConfig: .. code-block:: python - async for record in events.iter_read_items(wait=timedelta(seconds=10)): + async for record in events.read(wait=timedelta(seconds=10)): if record.body.kind == "tool_call": tools.append( workflow.start_activity( @@ -540,7 +540,7 @@ class StreamConfig: The loop ends after the page that is ``done``, when the Stream is closed and the read gave its last Record. With a ``wait``, a quiet Stream gives empty pages, and the loop goes on. So a loop on a Stream that can be quiet for long uses - ``iter_read`` and leaves after an empty page. A read has these costs. + ``read_pages`` and leaves after an empty page. A read has these costs. * A call keeps its Workflow Task open while it waits for its page. A Signal, an Update, a cancel or an Activity result reaches the Workflow at the next Workflow diff --git a/temporalio/contrib/streams/README.md b/temporalio/contrib/streams/README.md index 4556d65600..a95e3bea49 100644 --- a/temporalio/contrib/streams/README.md +++ b/temporalio/contrib/streams/README.md @@ -48,7 +48,7 @@ replacement of the first attempt's Records. The helpers run on the Streams API, so they follow how Workflow code reads, appends and closes. On every Store, such as the Redis Store, `MemoryStreamStore` or a Store written in Python, Core reads, appends and closes a Stream itself, with no Activity. Workflow code reads -with the same `read`, `iter_read` and `iter_read_items` as a client. It gets the same pages, but +with the same `read`, `read_pages` and `read_page` as a client. It gets the same pages, but each page has at most 256 Records. Here the Workflow reads the events of a model's answer while the model Activity still runs, @@ -58,7 +58,7 @@ and starts each tool as its call arrives. events = workflow.get_stream(events_ref, body_type=ModelEvent) status = workflow.get_stream(status_ref, body_type=str) tools = [] -async for record in events.iter_read_items(wait=timedelta(seconds=10)): +async for record in events.read(wait=timedelta(seconds=10)): event = record.body if event.kind == "tool_call": # The tool starts now, before the model ends its answer. @@ -74,12 +74,12 @@ results = await asyncio.gather(*tools) The loop ends after the page that is `done`, when the Stream is closed and the read gave its last Record. With no `wait`, a call gives what is in the Stream, and the loop ends at the first empty page. With a `wait`, a call on a quiet Stream gives an empty page after the wait, and -the loop asks again. So a loop on a Stream that can be quiet for long uses `iter_read` and +the loop asks again. So a loop on a Stream that can be quiet for long uses `read_pages` and leaves after an empty page. A later loop from the `last_offset` of that page goes on there. ```python last = None -async for page in tokens.iter_read(from_offset=last, wait=timedelta(seconds=30)): +async for page in tokens.read_pages(from_offset=last, wait=timedelta(seconds=30)): for record in page.records: await shown.append(record.body.text) last = page.last_offset diff --git a/temporalio/nexus/system/stream_service/workflow/_poll.py b/temporalio/nexus/system/stream_service/workflow/_poll.py deleted file mode 100644 index a653d84f93..0000000000 --- a/temporalio/nexus/system/stream_service/workflow/_poll.py +++ /dev/null @@ -1,77 +0,0 @@ -# Generated by nexgen v0.2.12. DO NOT EDIT! - -from __future__ import annotations - -import collections -import collections.abc -import typing - -_PollRequestT = typing.TypeVar("_PollRequestT") -_PollResponseT = typing.TypeVar("_PollResponseT") -_PollItemT = typing.TypeVar("_PollItemT") - - -@typing.final -class PollIterator(typing.Generic[_PollRequestT, _PollItemT]): - """Yields each item of the pages of a poll. - - ``next_request`` is the request to resume with. After an item, the resume helper gives - it. Without a resume helper, it reads the item's page again. After a page's last item, - it is the page helper's request, which is ``None`` after the last page. - """ - - def __init__( - self, - request: _PollRequestT, - pages: collections.abc.AsyncIterator[_PollResponseT], - items: collections.abc.Callable[ - [_PollResponseT], collections.abc.Sequence[_PollItemT] | None - ], - next_page: collections.abc.Callable[ - [_PollRequestT, _PollResponseT], _PollRequestT | None - ], - resume: collections.abc.Callable[[_PollRequestT, _PollItemT], _PollRequestT] - | None, - ) -> None: - async def read( - page_request: _PollRequestT, - ) -> tuple[_PollRequestT | None, collections.deque[_PollItemT]]: - response = await anext(pages) - # The page iterator keeps its requests to itself, so the page helper runs here - # again. It runs before the caller gets an item, so a changed item can't change - # the next request. - return next_page(page_request, response), collections.deque( - items(response) or () - ) - - self.next_request: _PollRequestT | None = request - self._read: collections.abc.Callable[ - [_PollRequestT], - collections.abc.Awaitable[ - tuple[_PollRequestT | None, collections.deque[_PollItemT]] - ], - ] = read - self._resume: ( - collections.abc.Callable[[_PollRequestT, _PollItemT], _PollRequestT] | None - ) = resume - self._page: collections.deque[_PollItemT] = collections.deque() - self._page_request: _PollRequestT = request - self._page_next: _PollRequestT | None = None - - def __aiter__(self) -> PollIterator[_PollRequestT, _PollItemT]: - return self - - async def __anext__(self) -> _PollItemT: - while not self._page: - if self.next_request is None: - raise StopAsyncIteration - self._page_request = self.next_request - self._page_next, self._page = await self._read(self._page_request) - if not self._page: - self.next_request = self._page_next - item = self._page.popleft() - if not self._page: - self.next_request = self._page_next - elif self._resume is not None: - self.next_request = self._resume(self._page_request, item) - return item diff --git a/temporalio/nexus/system/stream_service/workflow/_resources/stream.py b/temporalio/nexus/system/stream_service/workflow/_resources/stream.py index 0aa32cc380..3786698d1a 100644 --- a/temporalio/nexus/system/stream_service/workflow/_resources/stream.py +++ b/temporalio/nexus/system/stream_service/workflow/_resources/stream.py @@ -13,6 +13,7 @@ import temporalio.nexus.system._support as _support import temporalio.workflow +from ..._poll import PollIterator from ...models import ( AppendRecord, AppendRecordsRequest, @@ -27,7 +28,6 @@ StreamRecord, StreamRef, ) -from .._poll import PollIterator BodyT = typing.TypeVar("BodyT") @@ -92,7 +92,7 @@ async def close( handle = await _close_stream(request) await handle - async def read( + async def read_page( self, from_offset: bytes | None = None, max_records: int | None = None, @@ -110,14 +110,14 @@ async def read( handle = await _read_records(request, self.body_type) return await handle - async def iter_read( + async def read_pages( self, from_offset: bytes | None = None, max_records: int | None = None, wait: datetime.timedelta | None = None, max_bytes: int | None = None, ) -> collections.abc.AsyncGenerator[ReadRecordsResponse[BodyT], None]: - """Yield each response of ``read`` until the poll helper stops. + """Yield each response of ``read_page`` until the poll helper stops. Read one page of Records. The poll iterator reads them all. @@ -146,14 +146,14 @@ async def iter_read( return request = next_request - def iter_read_items( + def read( self, from_offset: bytes | None = None, max_records: int | None = None, wait: datetime.timedelta | None = None, max_bytes: int | None = None, ) -> PollIterator[ReadRecordsRequest[BodyT], StreamRecord[BodyT]]: - """Yield each item of ``iter_read``, page by page. + """Yield each item of ``read_pages``, page by page. ``next_request`` resumes after the last item that the iterator yielded. It is ``None`` after the last page. @@ -178,7 +178,7 @@ def iter_read_items( ) return PollIterator( request, - self.iter_read( + self.read_pages( from_offset=from_offset, max_records=max_records, wait=wait, diff --git a/temporalio/worker/_stream_store.py b/temporalio/worker/_stream_store.py index eca5eb9e47..e1945153f0 100644 --- a/temporalio/worker/_stream_store.py +++ b/temporalio/worker/_stream_store.py @@ -237,7 +237,7 @@ def _start_replayed_direct_call( # pyright: ignore[reportUnusedFunction] @dataclasses.dataclass(frozen=True) class _ReadKey: - """What a ``read`` call of Workflow code must match to take an open read.""" + """What a ``read_page`` call of Workflow code must match to take an open read.""" store: str stream_id: str @@ -247,9 +247,9 @@ class _ReadKey: class _DirectRead: # pyright: ignore[reportUnusedClass] - """One Core read of a Stream, which serves the ``read`` calls of Workflow code. + """One Core read of a Stream, which serves the ``read_page`` calls of Workflow code. - Core gives its Records in batches. Each ``read`` call takes one page of them, and the + Core gives its Records in batches. Each ``read_page`` call takes one page of them, and the read then waits for the next call under the offset of that page. """ @@ -355,7 +355,7 @@ def _start_direct_read( *, records_in_history: bool, ) -> temporalio.workflow.NexusOperationHandle[OutputT]: - """Answer one ``read`` call of Workflow code from a Core read of the Stream.""" + """Answer one ``read_page`` call of Workflow code from a Core read of the Stream.""" instance._assert_not_read_only("start stream read") payload_converter, _ = instance._system_nexus_converters(input) max_records, wait, max_bytes = _read_limits(request) diff --git a/tests/contrib/streams/test_appender_workflow.py b/tests/contrib/streams/test_appender_workflow.py index 40823d15d8..6a8c5fbaf7 100644 --- a/tests/contrib/streams/test_appender_workflow.py +++ b/tests/contrib/streams/test_appender_workflow.py @@ -133,7 +133,7 @@ async def _batches( ) -> list[int]: """Give the number of Records of each append, from its offsets in the marker.""" stream = client.get_stream(StreamRef(id=stream_id), body_type=str) - offsets = [record.offset async for record in stream.iter_read_items()] + offsets = [record.offset async for record in stream.read()] return [ sum( 1 for offset in offsets if entry.first_offset <= offset <= entry.last_offset @@ -152,7 +152,7 @@ def _marker_names(events: list[HistoryEvent]) -> list[str]: async def _bodies(client: Client, stream_id: str) -> list[str]: stream = client.get_stream(StreamRef(id=stream_id), body_type=str) - return [record.body async for record in stream.iter_read_items()] + return [record.body async for record in stream.read()] async def test_appends_of_one_activation_are_one_append(client: Client): diff --git a/tests/nexus/test_stream_direct.py b/tests/nexus/test_stream_direct.py index fac0a44961..ba4e6fac60 100644 --- a/tests/nexus/test_stream_direct.py +++ b/tests/nexus/test_stream_direct.py @@ -45,6 +45,7 @@ WorkflowSerializationContext, ) from temporalio.exceptions import ApplicationError, StreamError, StreamReplayError +from temporalio.nexus.system import stream_service from temporalio.nexus.system.stream_service import ( Producer, ProducerKindWorkflow, @@ -57,7 +58,6 @@ from temporalio.worker import ( Interceptor, Replayer, - StartNexusOperationInput, Worker, WorkflowInboundInterceptor, WorkflowInterceptorClassInput, @@ -131,7 +131,7 @@ async def _filled_stream( async def _read_all(client: Client, ref: StreamRef[str]) -> list[StreamRecord[str]]: stream = client.get_stream(ref, body_type=str) - return [record async for record in stream.iter_read_items()] + return [record async for record in stream.read()] async def _run( @@ -158,7 +158,7 @@ async def _run( return handle, await handle.result() -# A generator over `iter_read_items` that reads the Stream of an Activity that still runs. +# A generator over `read` that reads the Stream of an Activity that still runs. @dataclass @@ -176,7 +176,7 @@ async def stream_model_output(call: ModelCall) -> None: # The Activity writes the rest only after the Workflow says that it has the first # chunks. So the test passes only if Workflow code gets chunks while the Activity runs. told = False - async for _ in seen.iter_read_items(wait=timedelta(seconds=30)): + async for _ in seen.read(wait=timedelta(seconds=30)): told = True break if not told: @@ -191,7 +191,7 @@ async def model_chunks(chunks_id: str) -> AsyncGenerator[str, None]: """Yield each chunk of the model's Stream, as the model stub of a library does.""" stream = workflow.get_stream(StreamRef(id=chunks_id), body_type=str) # The wait is far longer than a pause of the model, so no page comes back empty. - async for record in stream.iter_read_items(wait=timedelta(seconds=30)): + async for record in stream.read(wait=timedelta(seconds=30)): yield record.body @@ -212,7 +212,7 @@ async def run(self, call: ModelCall) -> str: return text -async def test_generator_over_iter_read_items_reads_a_running_activity( +async def test_generator_over_read_reads_a_running_activity( client: Client, ): stream_client = _stream_client(client) @@ -273,7 +273,7 @@ async def run(self, args: ReadArgs) -> ReadResult: ref = StreamRef[str](id=args.stream_id, store=args.store) stream = workflow.get_stream(ref, body_type=str) result = ReadResult([], [], [], [], []) - async for page in stream.iter_read( + async for page in stream.read_pages( from_offset=( bytes.fromhex(args.from_offset) if args.from_offset is not None @@ -347,7 +347,7 @@ class ReadOnePageWorkflow: @workflow.run async def run(self, ref: StreamRef[str], wait_ms: int | None) -> list[Any]: stream = workflow.get_stream(ref, body_type=str) - page = await stream.read(wait=_ms(wait_ms)) + page = await stream.read_page(wait=_ms(wait_ms)) return [[record.body for record in page.records], page.done, page.last_offset] @@ -385,7 +385,7 @@ async def test_read_with_no_wait_on_an_empty_stream_gives_an_empty_page( @pytest.mark.parametrize("max_cached_workflows", [1000, 0]) -async def test_iter_read_items_with_no_wait_gives_what_is_there_and_ends( +async def test_read_with_no_wait_gives_what_is_there_and_ends( client: Client, max_cached_workflows: int ): stream_client = _stream_client(client) @@ -409,9 +409,7 @@ class ReadItemsWorkflow: @workflow.run async def run(self, ref: StreamRef[str], wait_ms: int | None) -> list[str]: stream = workflow.get_stream(ref, body_type=str) - return [ - record.body async for record in stream.iter_read_items(wait=_ms(wait_ms)) - ] + return [record.body async for record in stream.read(wait=_ms(wait_ms))] async def test_page_that_takes_the_last_record_with_a_cap_is_done(client: Client): @@ -461,15 +459,12 @@ async def run(self, stream_id: str) -> list[str]: start_to_close_timeout=timedelta(seconds=30), ) stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) - got = [ - record.body - async for record in stream.iter_read_items(wait=timedelta(seconds=10)) - ] + got = [record.body async for record in stream.read(wait=timedelta(seconds=10))] await writer return got -async def test_iter_read_items_with_a_wait_reads_across_a_quiet_gap(client: Client): +async def test_read_with_a_wait_reads_across_a_quiet_gap(client: Client): stream_client = _stream_client(client) ref = await _filled_stream(stream_client, [], close=False) handle, result = await _run( @@ -524,7 +519,7 @@ async def run(self, stream_id: str) -> list[str]: ) stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) got = [] - async for record in stream.iter_read_items(wait=timedelta(seconds=10)): + async for record in stream.read(wait=timedelta(seconds=10)): got.append(record.body) await writer return got @@ -561,7 +556,7 @@ async def run(self, stream_id: str, out_id: str) -> list[str]: stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) out = workflow.get_stream(StreamRef(id=out_id), body_type=str) got = [] - async for record in stream.iter_read_items(wait=timedelta(seconds=10)): + async for record in stream.read(wait=timedelta(seconds=10)): got.append(record.body) await out.append(record.body) await writer @@ -655,7 +650,7 @@ async def run(self, stream_id: str) -> list[int]: wakes = _Wakes() seen: list[int] = [] stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) - async for item in stream.iter_read_items(wait=timedelta(seconds=30)): + async for item in stream.read(wait=timedelta(seconds=30)): asyncio.create_task(self.count(wakes)) seen.append(self.i) if self.i == 2: @@ -666,7 +661,7 @@ async def run(self, stream_id: str) -> list[int]: async def _read_calls(count: int) -> None: - """Wait until Workflow code has made this many `read` calls.""" + """Wait until Workflow code has made this many `read_page` calls.""" async def check() -> None: assert [name for name, _ in _intercepted].count("ReadRecords") >= count @@ -790,7 +785,7 @@ class ActivityForEachRecordWorkflow: async def run(self, stream_id: str) -> list[str]: stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) got = [] - async for item in stream.iter_read_items(): + async for item in stream.read(): got.append( await workflow.execute_activity( tag, item.body, start_to_close_timeout=timedelta(seconds=10) @@ -807,7 +802,7 @@ async def run(self, stream_id: str) -> list[str]: got = [] offset = None while True: - page = await stream.read(from_offset=offset, max_records=1) + page = await stream.read_page(from_offset=offset, max_records=1) for record in page.records: got.append( await workflow.execute_activity( @@ -883,7 +878,7 @@ async def test_one_record_reads_of_one_page_keep_its_marker( ) -# What `read` checks, and where it can't run. +# What `read_page` checks, and where it can't run. # A client's read clamps each of these, or refuses it, and a Workflow read does the same. @@ -905,7 +900,7 @@ async def run(self, ref: StreamRef[str]) -> list[Any]: results: list[Any] = [] for arguments in _READ_LIMIT_CASES: try: - page = await stream.read(**arguments) + page = await stream.read_page(**arguments) results.append(len(page.records)) except StreamError as err: results.append([StreamErrorReason.Name(err.reason), err.message]) @@ -920,7 +915,7 @@ async def test_read_takes_the_limits_of_a_client_read(client: Client): expected: list[Any] = [] for arguments in _READ_LIMIT_CASES: try: - page = await stream.read(**arguments) + page = await stream.read_page(**arguments) expected.append(len(page.records)) except StreamError as err: expected.append([StreamErrorReason.Name(err.reason), err.message]) @@ -935,11 +930,11 @@ async def run(self, ref: StreamRef[str]) -> list[str]: stream = workflow.get_stream(ref, body_type=str) got: list[str] = [] try: - await stream.read(from_offset=b"o" * 2000) + await stream.read_page(from_offset=b"o" * 2000) got.append("page") except StreamError as err: got.append(StreamErrorReason.Name(err.reason)) - got.extend([record.body async for record in stream.iter_read_items()]) + got.extend([record.body async for record in stream.read()]) return got @@ -967,7 +962,7 @@ async def run(self, ref: StreamRef[str]) -> None: @workflow.query def start_read(self) -> str: assert self.ref is not None - step = workflow.get_stream(self.ref, body_type=str).read() + step = workflow.get_stream(self.ref, body_type=str).read_page() try: step.send(None) except Exception as err: @@ -1003,7 +998,7 @@ async def run(self, ref: StreamRef[str]) -> list[str]: stream = workflow.get_stream(ref, body_type=str) got: list[str] = [] # The first loop leaves with `break`, and its next request resumes there. - items = stream.iter_read_items(max_records=4) + items = stream.read(max_records=4) async for record in items: got.append(record.body) if len(got) == 3: @@ -1012,7 +1007,7 @@ async def run(self, ref: StreamRef[str]) -> list[str]: last = items.next_request.from_offset # The last reads go on from there to the end, two Records at a time. while True: - page = await stream.read(from_offset=last, max_records=2) + page = await stream.read_page(from_offset=last, max_records=2) got.extend(record.body for record in page.records) last = page.last_offset if page.done: @@ -1144,7 +1139,7 @@ async def _read(self, stream_id: str, out: Any) -> list[str]: last = None while True: self.reads += 1 - page = await stream.read(from_offset=last, max_records=2) + page = await stream.read_page(from_offset=last, max_records=2) for record in page.records: tagged = await workflow.execute_local_activity( tag, record.body, start_to_close_timeout=timedelta(seconds=10) @@ -1297,7 +1292,7 @@ async def run(self, ref: StreamRef[str], out_ref: StreamRef[str]) -> list[str]: stream = workflow.get_stream(ref, body_type=str) out = workflow.get_stream(out_ref, body_type=str) got: list[str] = [] - async for record in stream.iter_read_items(): + async for record in stream.read(): got.append(record.body) if record.body == "record-2": await out.append("seen record-2") @@ -1345,7 +1340,7 @@ async def run(self, ref: StreamRef[str]) -> list[Any]: got: list[str] = [] async def read_all() -> None: - async for record in stream.iter_read_items(wait=timedelta(seconds=60)): + async for record in stream.read(wait=timedelta(seconds=60)): got.append(record.body) reader = asyncio.create_task(read_all()) @@ -1400,7 +1395,7 @@ class BreakThenReadAgainWorkflow: @workflow.run async def run(self, stream_id: str) -> list[Any]: stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) - first = stream.iter_read_items(wait=timedelta(seconds=30)) + first = stream.read(wait=timedelta(seconds=30)) got_first: list[str] = [] async for record in first: got_first.append(record.body) @@ -1408,7 +1403,7 @@ async def run(self, stream_id: str) -> list[Any]: break assert first.next_request is not None # The second loop resumes after the last Record that the first one gave. - second = stream.iter_read( + second = stream.read_pages( from_offset=first.next_request.from_offset, wait=timedelta(seconds=30) ) writer = workflow.start_activity( @@ -1441,10 +1436,10 @@ class CompleteWithOpenReadsWorkflow: @workflow.run async def run(self, stream_id: str) -> str: stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) - first = await stream.read(wait=timedelta(seconds=60)) + first = await stream.read_page(wait=timedelta(seconds=60)) # A second read waits in its own task while the Workflow returns. waiting = asyncio.create_task( - stream.read(from_offset=first.last_offset, wait=timedelta(seconds=60)) + stream.read_page(from_offset=first.last_offset, wait=timedelta(seconds=60)) ) await asyncio.sleep(0) assert not waiting.done() @@ -1475,7 +1470,7 @@ async def run(self, stream_id: str) -> list[Any]: stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) got: list[str] = [] try: - async for record in stream.iter_read_items(wait=timedelta(seconds=60)): + async for record in stream.read(wait=timedelta(seconds=60)): got.append(record.body) except asyncio.CancelledError: return [got, "cancelled"] @@ -1507,10 +1502,10 @@ async def run(self, stream_id: str, from_offset: str | None) -> list[str]: stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) if from_offset is None: # The Core read of this page is still open when the run continues. - first = await stream.read(max_records=1, wait=timedelta(seconds=60)) + first = await stream.read_page(max_records=1, wait=timedelta(seconds=60)) assert first.last_offset is not None workflow.continue_as_new(args=[stream_id, first.last_offset.hex()]) - items = stream.iter_read_items(from_offset=bytes.fromhex(from_offset)) + items = stream.read(from_offset=bytes.fromhex(from_offset)) return [record.body async for record in items] @@ -1658,7 +1653,7 @@ async def run(self, ref: StreamRef[str], call: str) -> list[Any]: if call == "append": await stream.append("a") else: - await stream.read() + await stream.read_page() except StreamError as err: return [StreamErrorReason.Name(err.reason), err.message, err.non_retryable] return ["no error"] @@ -1778,7 +1773,7 @@ class AppendThenReadWorkflow: async def run(self, ref: StreamRef[str]) -> list[str]: stream = workflow.get_stream(ref, body_type=str) await stream.append("a", "b") - return [record.body async for record in stream.iter_read_items()] + return [record.body async for record in stream.read()] async def test_payload_codec_covers_direct_append_and_read(client: Client): @@ -1791,7 +1786,7 @@ async def test_payload_codec_covers_direct_append_and_read(client: Client): raw = _stream_client(client, store).get_stream( StreamRef(id=ref.id), body_type=RawValue ) - stored = [record.body.payload async for record in raw.iter_read_items()] + stored = [record.body.payload async for record in raw.read()] assert [body.metadata["simple-codec"] for body in stored] == [b"true", b"true"] @@ -1848,7 +1843,7 @@ async def run(self, burst: Burst) -> int: ) stream = workflow.get_stream(StreamRef(id=burst.stream_id), body_type=str) count = 0 - async for _ in stream.iter_read_items(wait=timedelta(seconds=30)): + async for _ in stream.read(wait=timedelta(seconds=30)): count += 1 await writer return count @@ -1888,7 +1883,7 @@ async def run(self, ref: StreamRef[str]) -> list[list[str]]: stream = workflow.get_stream(ref, body_type=str) async def drain() -> list[str]: - return [record.body async for record in stream.iter_read_items()] + return [record.body async for record in stream.read()] # Both loops ask for the first page before either gets it, so each one takes a # read of its own. @@ -1913,7 +1908,7 @@ async def run(self, ref: StreamRef[str]) -> list[str]: async def read() -> str: try: - await stream.read(wait=timedelta(seconds=10)) + await stream.read_page(wait=timedelta(seconds=10)) except StreamError as err: return StreamErrorReason.Name(err.reason) return "page" @@ -1976,7 +1971,7 @@ class AppendThenReadAllWorkflow: async def run(self, ref: StreamRef[str]) -> list[str]: stream = workflow.get_stream(ref, body_type=str) await stream.append("from-workflow") - return [record.body async for record in stream.iter_read_items()] + return [record.body async for record in stream.read()] async def test_codec_runs_with_no_context_on_stream_bodies(client: Client): @@ -1996,7 +1991,7 @@ async def test_codec_runs_with_no_context_on_stream_bodies(client: Client): raw_config = client.config() raw_config["stream_config"] = StreamConfig(stores=[store]) raw = Client(**raw_config).get_stream(StreamRef(id=ref.id), body_type=RawValue) - stored = [record.body.payload async for record in raw.iter_read_items()] + stored = [record.body.payload async for record in raw.read()] assert [body.metadata["ctx"] for body in stored] == [b"", b""] @@ -2015,7 +2010,7 @@ class UnnamedRefReadWorkflow: @workflow.run async def run(self, stream_id: str) -> list[str]: stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) - return [record.body async for record in stream.iter_read_items()] + return [record.body async for record in stream.read()] async def _unnamed_read_history( @@ -2074,12 +2069,12 @@ class BadBodyWorkflow: @workflow.run async def run(self, stream_id: str) -> list[Any]: stream = workflow.get_stream(StreamRef(id=stream_id), body_type=Point) - first = await stream.read(max_records=1) + first = await stream.read_page(max_records=1) events: list[Any] = [["got", first.records[0].body.x]] # The page of the bad body raises, and a read from the same offset gets it again. for _ in range(2): try: - await stream.read(from_offset=first.last_offset, max_records=1) + await stream.read_page(from_offset=first.last_offset, max_records=1) events.append(["page", "decoded"]) except Exception as err: events.append(["page", type(err).__name__]) @@ -2416,7 +2411,7 @@ class ReadAppendCloseWorkflow: async def run(self, source_id: str, out_id: str) -> list[str]: source = workflow.get_stream(StreamRef(id=source_id), body_type=str) out = workflow.get_stream(StreamRef(id=out_id), body_type=str) - got = [record.body async for record in source.iter_read_items()] + got = [record.body async for record in source.read()] await out.append(*got) await out.close() return got @@ -2513,7 +2508,7 @@ async def run(self, events_id: str, status_id: str) -> list[str]: events = workflow.get_stream(StreamRef(id=events_id), body_type=ModelEvent) status = workflow.get_stream(StreamRef(id=status_id), body_type=str) tools = [] - async for record in events.iter_read_items(wait=timedelta(seconds=10)): + async for record in events.read(wait=timedelta(seconds=10)): event = record.body if event.kind == "tool_call": assert event.tool_call is not None @@ -2698,7 +2693,7 @@ async def run(self, stream_id: str) -> ReadResult: ) stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) result = ReadResult([], [], [], [], []) - async for page in stream.iter_read(wait=timedelta(seconds=10)): + async for page in stream.read_pages(wait=timedelta(seconds=10)): result.bodies.extend(record.body for record in page.records) result.page_sizes.append(len(page.records)) result.dones.append(page.done) @@ -2735,7 +2730,7 @@ def __init__(self) -> None: async def read_then_wait(self, stream_id: str) -> list[str]: stream = workflow.get_stream(StreamRef(id=stream_id), body_type=str) - got = [record.body async for record in stream.iter_read_items()] + got = [record.body async for record in stream.read()] await workflow.wait_condition(lambda: self.go) return got @@ -2836,11 +2831,13 @@ def init(self, outbound: WorkflowOutboundInterceptor) -> None: class _StreamCallOutbound(WorkflowOutboundInterceptor): - async def start_system_nexus_operation( - self, input: StartNexusOperationInput[Any, Any] - ) -> workflow.NexusOperationHandle[Any]: - _intercepted.append((input.operation_name, input.input)) - return await super().start_system_nexus_operation(input) + async def start_read_records( + self, + request: stream_service.ReadRecordsRequest[Any], + output_type: type[stream_service.ReadRecordsResponse[Any]] | None = None, + ) -> workflow.NexusOperationHandle[stream_service.ReadRecordsResponse[Any]]: + _intercepted.append(("ReadRecords", request)) + return await super().start_read_records(request, output_type) async def test_workflow_interceptor_sees_each_page_read(client: Client): @@ -3023,7 +3020,7 @@ def __init__(self) -> None: @workflow.run async def run(self, ref: StreamRef[str]) -> list[str]: stream = workflow.get_stream(ref, body_type=str) - async for record in stream.iter_read_items(wait=timedelta(seconds=10)): + async for record in stream.read(wait=timedelta(seconds=10)): self._bodies.append(record.body) return self._bodies diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index f24eaa3ebd..ee4185a01d 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -150,7 +150,7 @@ async def run(self, stream_id: str) -> str: class NexusClientReadWorkflow: @workflow.run async def run(self, operation: str) -> str: - # Workflow code can also start the operation of `read` through a Nexus client. + # Workflow code can also start the operation of `read_page` through a Nexus client. requests: dict[str, Any] = { "ReadRecords": ReadRecordsRequest(ref=StreamRef[str](id="stream")), } @@ -476,7 +476,7 @@ async def test_workflow_stream_uses_store(client: Client, append_workflow: type[ handle, ref = await _run_append_workflow(stream_client, append_workflow) stream = stream_client.get_stream(ref, body_type=str) - records = [record async for record in stream.iter_read_items()] + records = [record async for record in stream.read()] assert [record.body for record in records] == _BODIES run_id = handle.result_run_id assert run_id @@ -509,7 +509,7 @@ async def test_workflow_stream_uses_a_store_written_from_scratch(client: Client) stream = stream_client.get_stream(ref, body_type=str) await stream.append("a") - assert [record.body async for record in stream.iter_read_items()] == ["a"] + assert [record.body async for record in stream.read()] == ["a"] @_APPEND_WORKFLOWS @@ -527,10 +527,10 @@ async def test_workflow_stream_store_uses_payload_codec( raw = _with_store(client, store).get_stream( StreamRef(id=ref.id), body_type=temporalio.common.RawValue ) - stored = [record.body.payload async for record in raw.iter_read_items()] + stored = [record.body.payload async for record in raw.read()] assert [body.metadata["simple-codec"] for body in stored] == [b"true", b"true"] stream = stream_client.get_stream(ref, body_type=str) - assert [record.body async for record in stream.iter_read_items()] == _BODIES + assert [record.body async for record in stream.read()] == _BODIES @_APPEND_WORKFLOWS @@ -617,7 +617,7 @@ async def read_with(store: StreamStore) -> str: task_queue=worker.task_queue, ) - # Core reads the Store, as for `read`, also one written in Python. Nobody created + # Core reads the Store, as for `read_page`, also one written in Python. Nobody created # the Stream. assert await read_with(MemoryStreamStore()) == "STREAM_ERROR_REASON_NOT_FOUND" assert await read_with(DictStreamStore()) == "STREAM_ERROR_REASON_NOT_FOUND" @@ -629,8 +629,8 @@ def test_workflow_stream_has_the_calls_of_a_client_stream(): "close", "describe", "read", - "iter_read", - "iter_read_items", + "read_pages", + "read_page", "__aenter__", "__aexit__", ): From 3205ffcee0f680aa4a8a6d915e8acef846752280 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Wed, 18 Nov 2026 01:46:40 -0700 Subject: [PATCH 30/35] Removed the Redis cases from the Workflow Stream tests. The Redis Store moved to its own package, with the tests of its outages. The test of a Worker with no Workflows keeps its proof on a small Store of the bridge that fails to build its runtime. --- changelog/added/patient-river-heron.md | 2 +- temporalio/client/_stream_store.py | 15 +- tests/nexus/test_stream_direct.py | 285 ++----------------------- 3 files changed, 26 insertions(+), 276 deletions(-) diff --git a/changelog/added/patient-river-heron.md b/changelog/added/patient-river-heron.md index 296756d6f8..a473d3033b 100644 --- a/changelog/added/patient-river-heron.md +++ b/changelog/added/patient-river-heron.md @@ -1,4 +1,4 @@ -Workflow code creates a Stream, appends Records to it, reads it, describes it and closes it, with `workflow.create_stream` and `workflow.get_stream`. A create and a describe run in the Store's Local Activity, with the options of `StreamConfig.workflow_call_options`. Core makes a read, an append and a close itself, with no Activity, on the Redis Store, on `MemoryStreamStore` and on a Store written in Python. Core calls a Store written in Python as a task on the Worker's event loop, never on a Workflow thread. For Workflow code, such a Store waits up to a read's `wait`, ends that wait on a cancel, gives offsets of 1 to 1024 bytes that compare in order, fences a lower epoch of a Producer, writes a resent batch once, and raises `StreamError` with a reason. Core tries a call again for up to 30 s only on `STREAM_ERROR_REASON_STORAGE_UNAVAILABLE`. Any other exception reaches Workflow code on the first try, as a non-retryable `STREAM_ERROR_REASON_STORAGE_CORRUPT` with its type in the message. `Replayer` takes the `stream_config` of the Worker's client. +Workflow code creates a Stream, appends Records to it, reads it, describes it and closes it, with `workflow.create_stream` and `workflow.get_stream`. A create and a describe run in the Store's Local Activity, with the options of `StreamConfig.workflow_call_options`. Core makes a read, an append and a close itself, with no Activity, on `MemoryStreamStore` and on a Store written in Python, such as `RedisStreamStore` of the `temporalio-streams-redis` package. Core calls a Store written in Python as a task on the Worker's event loop, never on a Workflow thread. For Workflow code, such a Store waits up to a read's `wait`, ends that wait on a cancel, gives offsets of 1 to 1024 bytes that compare in order, fences a lower epoch of a Producer, writes a resent batch once, and raises `StreamError` with a reason. Core tries a call again for up to 30 s only on `STREAM_ERROR_REASON_STORAGE_UNAVAILABLE`. Any other exception reaches Workflow code on the first try, as a non-retryable `STREAM_ERROR_REASON_STORAGE_CORRUPT` with its type in the message. `Replayer` takes the `stream_config` of the Worker's client. The Workflow `Stream` has `read`, `read_pages` and `read_page`, so Workflow code reads a Stream with the same calls as a client. Core reads the Store inside the Workflow Task. A page that Workflow code gets in one activation has at most 256 Records, and the rest come in the next activation with no wait. A loop that asks for its next page in the same activation keeps one read open, and a read that no code waits on closes before the activation completes, so it never holds a Workflow Task open. With a `wait`, a quiet Stream gives empty pages, so a loop on a Stream that can be quiet for long uses `read_pages` and leaves after an empty page. A call that waits on a quiet Stream holds its Workflow Task open, with one Workflow Task about every 8 s. The calls of one Workflow Task write one marker in History. `StreamConfig.workflow_read_replay_from` decides whether the marker holds the Records (`StreamReplaySource.HISTORY`) or only their offsets and a digest, which a replay reads from the Store again (`StreamReplaySource.STORE`, the default). With `HISTORY`, one Workflow Task's marker holds at most 1 MiB of Records, and the read goes on in the next task. A Workflow's append runs in Core inside the Workflow Task, with no Activity. It writes as the Workflow, with its Workflow Task as the epoch. A `producer=` of another kind raises `StreamError` with the reason `STREAM_ERROR_REASON_INVALID`. A Workflow's close runs in Core, with no Activity, and spans two Workflow Tasks. The task that calls `close` records the intent, Core closes the Stream after the server took that task, and the next task gives the result, so a failed task closes nothing. Await the close. A graceful shutdown of the Worker waits at most 10 s for a close that the code doesn't await, and a Worker that dies when the Workflow ends loses it. An append and a close of Workflow code always carry the Workflow Task as their epoch. An append is at-least-once. After a Worker crash or an eviction, the rerun of its task can write a second copy with the epoch of the new task. diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index a07945d3ec..814767ef6c 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -147,15 +147,16 @@ class StreamStore(ABC): Store of a package, such as ``RedisStreamStore`` of ``temporalio-streams-redis``, or :py:class:`temporalio.testing.MemoryStreamStore` in tests. - The SDK's Stores run on the bridge, in Rust. A Store can also be written in Python, - by a subclass that implements :py:attr:`name` and the five operations. Each - operation takes and gives a message of ``temporalio.api.streamservice.v1``. The - bodies of the Records arrive as payloads that the SDK already encoded with the data - converter and the payload codec, so a Store keeps payloads and not the user's types. - A failed call raises :py:class:`temporalio.exceptions.StreamError` with a reason. + The memory Store runs on the bridge, in Rust. Every other Store is a subclass, + written in Python, that implements :py:attr:`name` and the five operations. It can + call its own native code, as the Redis Store does. Each operation takes and gives a + message of ``temporalio.api.streamservice.v1``. The bodies of the Records arrive as + payloads that the SDK already encoded with the data converter and the payload codec, + so a Store keeps payloads and not the user's types. A failed call raises + :py:class:`temporalio.exceptions.StreamError` with a reason. Such a Store works for client, Activity and Workflow code. Core makes a read, an - append and a close of Workflow code, as it does on a Store in Rust. It calls the + append and a close of Workflow code, as it does on the memory Store. It calls the Store's method as a task on the Worker's event loop, never on a Workflow thread, and records the result in History. A create and a describe of Workflow code run in the Store's Local Activity. For Workflow code, a Store must do these things. diff --git a/tests/nexus/test_stream_direct.py b/tests/nexus/test_stream_direct.py index ba4e6fac60..a8df4a555f 100644 --- a/tests/nexus/test_stream_direct.py +++ b/tests/nexus/test_stream_direct.py @@ -3,16 +3,16 @@ from __future__ import annotations import asyncio -import os -import urllib.parse import uuid from collections.abc import AsyncGenerator, Sequence from dataclasses import dataclass from datetime import timedelta -from typing import Any, Self +from typing import Any import pytest +import temporalio.bridge.runtime +import temporalio.bridge.streams from temporalio import activity, workflow from temporalio.api.common.v1 import Payload from temporalio.api.enums.v1 import EventType, StreamErrorReason @@ -31,11 +31,11 @@ Client, StreamConfig, StreamReplaySource, - WorkflowExecutionStatus, WorkflowFailureError, WorkflowHandle, WorkflowHistory, ) +from temporalio.client._stream_store import _BridgeStreamStore from temporalio.common import RawValue from temporalio.converter import ( DataConverter, @@ -2100,18 +2100,26 @@ async def noop() -> None: pass -async def test_worker_without_workflows_builds_no_store_runtime(client: Client): - from temporalio.contrib.redis.streams import RedisStreamStore +class BadOptionStore(_BridgeStreamStore): + def __init__(self) -> None: + super().__init__(name="bad-option", runtime=None) + + def _new_stream_runtime( + self, runtime: temporalio.bridge.runtime.Runtime + ) -> temporalio.bridge.streams.StreamRuntime: + raise ValueError("The Store option is wrong") + +async def test_worker_without_workflows_builds_no_store_runtime(client: Client): config = client.config() - config["stream_config"] = StreamConfig(stores=[RedisStreamStore("not a url")]) + config["stream_config"] = StreamConfig(stores=[BadOptionStore()]) # Such a Worker starts, as it did before Direct calls, since it gives Core no runtime. async with Worker( Client(**config), task_queue=f"tq-{uuid.uuid4()}", activities=[noop] ): pass - # A Worker with Workflows gives Core the runtime, so it builds it at once. - with pytest.raises(ValueError, match="The Redis URL is wrong"): + # A Worker with Workflows gives Core the runtime, so it builds it immediately. + with pytest.raises(ValueError, match="The Store option is wrong"): Worker( Client(**config), task_queue=f"tq-{uuid.uuid4()}", @@ -2861,262 +2869,3 @@ async def test_workflow_interceptor_sees_each_page_read(client: Client): bytes.fromhex(result.offsets[1]), bytes.fromhex(result.offsets[3]), ] - - -# A Store outage that Core tries again, made with a proxy in front of Redis. - -_REDIS_URL = os.environ.get("STREAMS_REDIS_URL") - -_needs_redis = pytest.mark.skipif( - not _REDIS_URL, reason="STREAMS_REDIS_URL is not set to a Redis server" -) - - -async def _copy(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None: - try: - while data := await reader.read(65536): - writer.write(data) - await writer.drain() - except ConnectionError: - pass - - -class _StoreOutage: - """A TCP proxy in front of Redis that a test takes down and brings up. - - Down, it drops each open connection and each new one, so each call of the Store - fails with `StorageUnavailable`. Redis itself keeps running, so the tests that - share it in other processes see no outage. `refused` counts the connections that - it dropped while down. - """ - - def __init__(self, url: str) -> None: - self._parts = urllib.parse.urlsplit(url) - self._server: asyncio.Server | None = None - self._writers: set[asyncio.StreamWriter] = set() - self._down = False - self.url = "" - self.refused = 0 - - async def __aenter__(self) -> Self: - self._server = await asyncio.start_server(self._serve, "127.0.0.1", 0) - port = self._server.sockets[0].getsockname()[1] - credentials, at, _ = self._parts.netloc.rpartition("@") - netloc = f"{credentials}{at}127.0.0.1:{port}" - self.url = urllib.parse.urlunsplit(self._parts._replace(netloc=netloc)) - return self - - async def __aexit__(self, *_: object) -> None: - self.take_down() - if self._server: - self._server.close() - - def take_down(self) -> None: - self._down = True - for writer in list(self._writers): - writer.close() - - def bring_up(self) -> None: - self._down = False - - async def _serve( - self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter - ) -> None: - if self._down: - self.refused += 1 - writer.close() - return - try: - redis_reader, redis_writer = await asyncio.open_connection( - self._parts.hostname or "127.0.0.1", self._parts.port or 6379 - ) - except OSError: - writer.close() - return - self._writers.update((writer, redis_writer)) - copies = [ - asyncio.create_task(_copy(reader, redis_writer)), - asyncio.create_task(_copy(redis_reader, writer)), - ] - try: - await asyncio.wait(copies, return_when=asyncio.FIRST_COMPLETED) - finally: - for each in (writer, redis_writer): - self._writers.discard(each) - each.close() - await asyncio.gather(*copies) - - -def _redis_client(client: Client, url: str) -> Client: - from temporalio.contrib.redis.streams import RedisStreamStore - - # A short response timeout keeps a call that the outage cuts from waiting long. - store = RedisStreamStore( - url, - key_prefix=f"test-{uuid.uuid4()}", - response_timeout=timedelta(seconds=1), - ) - return _stream_client(client, store) - - -async def _store_answers(stream: Any) -> None: - # A client call has no retry, so the test waits until the Store answers again. - try: - await stream.describe() - except StreamError as err: - raise AssertionError(f"The Store doesn't answer: {err}") from err - - -@_needs_redis -async def test_append_during_a_short_outage_appends_once(client: Client): - assert _REDIS_URL - async with _StoreOutage(_REDIS_URL) as outage: - stream_client = _redis_client(client, outage.url) - ref = await _filled_stream(stream_client, [], close=False) - stream = stream_client.get_stream(ref, body_type=str) - async with new_worker( - stream_client, AppendWorkflow, max_cached_workflows=0 - ) as worker: - outage.take_down() - handle: WorkflowHandle[Any, Any] = await stream_client.start_workflow( - AppendWorkflow.run, - ref, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - # The client gets the error that Core keeps from Workflow code. - with pytest.raises(StreamError) as err: - await stream.describe() - assert ( - err.value.reason - == StreamErrorReason.STREAM_ERROR_REASON_STORAGE_UNAVAILABLE - ) - - async def task_started() -> None: - events = (await handle.fetch_history()).events - assert any( - event.event_type == EventType.EVENT_TYPE_WORKFLOW_TASK_STARTED - for event in events - ) - - await assert_eventually(task_started) - refused = outage.refused - await asyncio.sleep(1) - # The task waits for the append, which Core tries again. - assert outage.refused > refused - assert _completed_tasks(await handle.fetch_history()) == 0 - outage.bring_up() - offsets = await asyncio.wait_for(handle.result(), timeout=30) - records = await _read_all(stream_client, ref) - assert [record.body for record in records] == ["a", "b"] - assert offsets == [record.offset.hex() for record in records] - - -@workflow.defn -class ReadAcrossAnOutageWorkflow: - def __init__(self) -> None: - self._bodies: list[str] = [] - - @workflow.run - async def run(self, ref: StreamRef[str]) -> list[str]: - stream = workflow.get_stream(ref, body_type=str) - async for record in stream.read(wait=timedelta(seconds=10)): - self._bodies.append(record.body) - return self._bodies - - @workflow.query - def bodies(self) -> list[str]: - return self._bodies - - -@_needs_redis -async def test_read_across_a_short_outage_gives_each_record_once(client: Client): - assert _REDIS_URL - async with _StoreOutage(_REDIS_URL) as outage: - stream_client = _redis_client(client, outage.url) - ref = await _filled_stream(stream_client, ["a", "b", "c"], close=False) - stream = stream_client.get_stream(ref, body_type=str) - async with new_worker(stream_client, ReadAcrossAnOutageWorkflow) as worker: - handle = await stream_client.start_workflow( - ReadAcrossAnOutageWorkflow.run, - ref, - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - - async def read_three() -> None: - bodies = await handle.query(ReadAcrossAnOutageWorkflow.bodies) - assert bodies == ["a", "b", "c"] - - await assert_eventually(read_three) - # The outage cuts the read that waits for the next Record. - outage.take_down() - await asyncio.sleep(1.5) - # Only the Worker's read asks the Store in the outage. - assert outage.refused > 0 - outage.bring_up() - await assert_eventually(lambda: _store_answers(stream)) - await stream.append("d", "e") - await stream.close() - result = await asyncio.wait_for(handle.result(), timeout=30) - assert result == ["a", "b", "c", "d", "e"] - - -@_needs_redis -@pytest.mark.parametrize("opt_in", ["worker", "defn"]) -async def test_replay_with_its_store_down_fails_only_the_task( - client: Client, opt_in: str -): - assert _REDIS_URL - workflow_type: type[Any] = ( - ReadThenWaitFailsWorkflow if opt_in == "defn" else ReadThenWaitWorkflow - ) - worker_options: dict[str, Any] = ( - {"workflow_failure_exception_types": [StreamReplayError]} - if opt_in == "worker" - else {} - ) - async with _StoreOutage(_REDIS_URL) as outage: - stream_client = _redis_client(client, outage.url) - ref = await _filled_stream(stream_client, ["a", "b"]) - task_queue = f"tq-{uuid.uuid4()}" - # No cache, so the next Worker replays the History and reads the Store. - async with new_worker( - stream_client, workflow_type, task_queue=task_queue, max_cached_workflows=0 - ): - handle: WorkflowHandle[Any, Any] = await stream_client.start_workflow( - workflow_type.run, - ref.id, - id=f"workflow-{uuid.uuid4()}", - task_queue=task_queue, - ) - - async def read_is_in_history() -> None: - assert "core_stream" in _markers(await handle.fetch_history()) - - await assert_eventually(read_is_in_history) - outage.take_down() - async with new_worker( - stream_client, workflow_type, task_queue=task_queue, **worker_options - ): - await handle.signal("proceed") - - async def task_failed() -> None: - messages = [ - event.workflow_task_failed_event_attributes.failure.message - for event in (await handle.fetch_history()).events - if event.event_type == EventType.EVENT_TYPE_WORKFLOW_TASK_FAILED - ] - assert messages - assert messages[0].startswith( - f"StreamReplayUnavailable: Stream `{ref.id}` in Store `redis`" - ), messages[0] - - # Core tries the Store for 5 s before it fails the task. - await assert_eventually(task_failed, timeout=timedelta(seconds=30)) - # The opt-in doesn't fail the Workflow, since the Records may still be there. - description = await handle.describe() - assert description.status == WorkflowExecutionStatus.RUNNING - outage.bring_up() - result = await asyncio.wait_for(handle.result(), timeout=60) - assert result == ["a", "b"] From 754639ad50e844fedd06be5e4b4c50c1780a377c Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Wed, 18 Nov 2026 03:22:53 -0700 Subject: [PATCH 31/35] Ran the Workflow Stream tests on a Store written in Python. The next commit takes the memory Store out of the bridge. `DictStreamStore` now has the wait of a read that Core needs, with the resend and the fence of a batch. The test that compared the limits of a client read with a Workflow read leaves, since a client read of a Python Store has no check. --- .../contrib/streams/test_appender_workflow.py | 18 +-- tests/contrib/streams/test_producer.py | 11 +- tests/helpers/streams.py | 71 ++++++++-- tests/nexus/test_stream_direct.py | 121 +++--------------- tests/nexus/test_stream_store_workflow.py | 81 ++++++------ 5 files changed, 135 insertions(+), 167 deletions(-) diff --git a/tests/contrib/streams/test_appender_workflow.py b/tests/contrib/streams/test_appender_workflow.py index 6a8c5fbaf7..7766f8cd3e 100644 --- a/tests/contrib/streams/test_appender_workflow.py +++ b/tests/contrib/streams/test_appender_workflow.py @@ -16,9 +16,9 @@ from temporalio.exceptions import StreamError from temporalio.nexus.system.stream_service import StreamRef from temporalio.nexus.system.stream_service.workflow import create_stream, get_stream -from temporalio.testing import MemoryStreamStore from temporalio.worker import Replayer from tests.helpers import new_worker +from tests.helpers.streams import DictStreamStore from tests.nexus.test_stream_store import _with_store _BURST = [f"record-{index}" for index in range(40)] @@ -156,7 +156,7 @@ async def _bodies(client: Client, stream_id: str) -> list[str]: async def test_appends_of_one_activation_are_one_append(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" handle, last_offset = await _run(stream_client, BurstWorkflow, stream_id) @@ -174,7 +174,7 @@ async def test_appends_of_one_activation_are_one_append(client: Client): async def test_appends_during_a_workflow_append_join_the_next_one(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" handle, _ = await _run(stream_client, InFlightWorkflow, stream_id) @@ -186,7 +186,7 @@ async def test_appends_during_a_workflow_append_join_the_next_one(client: Client async def test_a_failed_workflow_append_stops_the_appender(client: Client): handle, result = await _run( - _with_store(client, MemoryStreamStore()), + _with_store(client, DictStreamStore()), FailedAppendWorkflow, f"stream-{uuid.uuid4()}", ) @@ -197,13 +197,13 @@ async def test_a_failed_workflow_append_stops_the_appender(client: Client): async def test_replay_of_workflow_appender_writes_nothing(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" handle, _ = await _run(stream_client, InFlightWorkflow, stream_id) - # A replay of an append that Core made needs a Store that runs on the bridge, as the - # first run had. A new memory Store shows any write, since it has no Stream. - replay_store = MemoryStreamStore() + # A replay of an append that Core made needs a Store, as the first run had. A new + # Store shows any write, since it has no Stream. + replay_store = DictStreamStore() await Replayer( workflows=[InFlightWorkflow], stream_config=_with_store(client, replay_store).config()["stream_config"], @@ -220,7 +220,7 @@ async def test_replay_of_workflow_appender_writes_nothing(client: Client): async def test_workflow_appender_refuses_a_max_delay(client: Client): _, message = await _run( - _with_store(client, MemoryStreamStore()), + _with_store(client, DictStreamStore()), MaxDelayWorkflow, f"stream-{uuid.uuid4()}", ) diff --git a/tests/contrib/streams/test_producer.py b/tests/contrib/streams/test_producer.py index e7b1f19a7f..0d6386da44 100644 --- a/tests/contrib/streams/test_producer.py +++ b/tests/contrib/streams/test_producer.py @@ -27,8 +27,9 @@ ) from temporalio.nexus.system.stream_service.client import Stream from temporalio.nexus.system.stream_service.workflow import create_stream -from temporalio.testing import ActivityEnvironment, MemoryStreamStore +from temporalio.testing import ActivityEnvironment from tests.helpers import new_worker +from tests.helpers.streams import DictStreamStore from tests.nexus.test_stream_store import _create_stream, _with_store _SCHEDULED = datetime(2026, 11, 12, 10, 0, 0, 123456, tzinfo=timezone.utc) @@ -94,7 +95,7 @@ async def _records(stream: Stream[str]) -> list[StreamRecord[str]]: async def test_current_producer_in_other_code_is_the_client_producer(client: Client): - stream = await _create_stream(client, MemoryStreamStore()) + stream = await _create_stream(client, DictStreamStore()) await stream.append("a") [record] = await _records(stream) producer = current_producer() @@ -106,7 +107,7 @@ async def test_current_producer_in_other_code_is_the_client_producer(client: Cli async def test_current_producer_in_an_activity_is_its_attempt_producer( client: Client, kind: str ): - stream = await _create_stream(client, MemoryStreamStore()) + stream = await _create_stream(client, DictStreamStore()) async def append_and_give_producer() -> Producer: await stream.append("a") @@ -134,7 +135,7 @@ async def run(self, stream_id: str) -> Producer: async def test_current_producer_in_workflow_code_is_the_workflow_producer( client: Client, ): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" async with new_worker(stream_client, CurrentProducerWorkflow) as worker: handle = await stream_client.start_workflow( @@ -214,7 +215,7 @@ async def run(self, stream_id: str, keep_producer: bool) -> None: async def test_activity_retry_appends_as_its_stored_producer( client: Client, keep_producer: bool ): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream = await stream_client.create_stream( id=f"stream-{uuid.uuid4()}", body_type=str ) diff --git a/tests/helpers/streams.py b/tests/helpers/streams.py index b06179a476..2cacb22772 100644 --- a/tests/helpers/streams.py +++ b/tests/helpers/streams.py @@ -8,13 +8,19 @@ from typing import TypeVar from google.protobuf.message import Message +from google.protobuf.timestamp_pb2 import Timestamp from temporalio.api.enums.v1 import ( StreamErrorReason, StreamIdConflictPolicy, StreamStatus, ) -from temporalio.api.stream.v1 import StreamRecord, StreamRef +from temporalio.api.stream.v1 import ( + Producer, + StreamRecord, + StreamRef, + WorkflowTaskEpoch, +) from temporalio.api.streamservice.v1 import ( AppendRecordsRequest, AppendRecordsResponse, @@ -85,11 +91,20 @@ async def describe_stream( ) +@dataclasses.dataclass +class _LastBatch: + epoch: tuple[int, ...] + sequence: int + response: AppendRecordsResponse + + @dataclasses.dataclass class _DictStream: request_id: str records: list[StreamRecord] = dataclasses.field(default_factory=list) closed: bool = False + # The id of a Producer is unique in one Stream. + last_batches: dict[str, _LastBatch] = dataclasses.field(default_factory=dict) changed: asyncio.Event = dataclasses.field(default_factory=asyncio.Event) def wake(self) -> None: @@ -102,12 +117,39 @@ def _offset(index: int) -> bytes: return index.to_bytes(8, "big") +def _epoch(producer: Producer) -> tuple[int, ...]: + """Give a key that orders the epochs of one Producer, as ``Producer`` documents it.""" + kind = producer.WhichOneof("kind") + task = WorkflowTaskEpoch() + scheduled = Timestamp() + attempt = 0 + if kind == "workflow": + task = producer.workflow.epoch + elif kind == "local_activity": + task = producer.local_activity.epoch.task + attempt = producer.local_activity.epoch.attempt + elif kind in ("workflow_activity", "standalone_activity"): + scheduled = getattr(producer, kind).epoch.attempt_scheduled_time + attempt = getattr(producer, kind).epoch.attempt + return ( + task.started_event_id, + task.attempt, + task.started_time.seconds, + task.started_time.nanos, + scheduled.seconds, + scheduled.nanos, + attempt, + ) + + class DictStreamStore(StreamStore): - """A Store written from scratch, with no bridge, that keeps its Streams in a dict. + """A Store that keeps its Streams in a dict, for the tests. - It has enough of each operation for the tests, and no dedupe and no Retention. A - read with a ``wait`` waits for an append or a close, as Core needs for Workflow - code. :py:attr:`waiting` counts the reads that wait, so a test sees one that stays. + It has no Retention and no expiry, and it checks no request. It resends the answer + of a batch that comes again with the same Producer, epoch and sequence, and it + fences a Producer whose epoch is lower than its newest one. A read with a ``wait`` + waits for an append or a close, as Core needs for Workflow code. + :py:attr:`waiting` counts the reads that wait, so a test sees one that stays. """ def __init__(self, name: str = "dict") -> None: @@ -150,11 +192,22 @@ async def append_records( self, request: AppendRecordsRequest ) -> AppendRecordsResponse: stream = self._stream(request.ref) + epoch = _epoch(request.producer) + last = stream.last_batches.get(request.producer.id) + previous = last if last is not None and last.epoch == epoch else None + sequence = request.sequence or (previous.sequence + 1 if previous else 1) + if previous is not None and sequence == previous.sequence: + return previous.response if stream.closed: raise StreamError( f"The Stream '{request.ref.id}' is closed", reason=StreamErrorReason.STREAM_ERROR_REASON_CLOSED, ) + if last is not None and epoch < last.epoch: + raise StreamError( + "The epoch is lower than the newest one of the Producer", + reason=StreamErrorReason.STREAM_ERROR_REASON_FENCED, + ) first = len(stream.records) + 1 for record in request.records: stream.records.append( @@ -164,11 +217,13 @@ async def append_records( offset=_offset(len(stream.records) + 1), ) ) - if request.records: - stream.wake() - return AppendRecordsResponse( + response = AppendRecordsResponse( first_offset=_offset(first), last_offset=_offset(len(stream.records)) ) + stream.last_batches[request.producer.id] = _LastBatch(epoch, sequence, response) + if request.records: + stream.wake() + return response async def read_records(self, request: ReadRecordsRequest) -> ReadRecordsResponse: stream = self._stream(request.ref) diff --git a/tests/nexus/test_stream_direct.py b/tests/nexus/test_stream_direct.py index a8df4a555f..d5abd8427f 100644 --- a/tests/nexus/test_stream_direct.py +++ b/tests/nexus/test_stream_direct.py @@ -11,8 +11,6 @@ import pytest -import temporalio.bridge.runtime -import temporalio.bridge.streams from temporalio import activity, workflow from temporalio.api.common.v1 import Payload from temporalio.api.enums.v1 import EventType, StreamErrorReason @@ -35,7 +33,6 @@ WorkflowHandle, WorkflowHistory, ) -from temporalio.client._stream_store import _BridgeStreamStore from temporalio.common import RawValue from temporalio.converter import ( DataConverter, @@ -54,11 +51,10 @@ StreamStatus, ) from temporalio.runtime import MetricBuffer, Runtime, TelemetryConfig -from temporalio.testing import MemoryStreamStore, WorkflowEnvironment +from temporalio.testing import WorkflowEnvironment from temporalio.worker import ( Interceptor, Replayer, - Worker, WorkflowInboundInterceptor, WorkflowInterceptorClassInput, WorkflowOutboundInterceptor, @@ -80,7 +76,7 @@ def _stream_client( ) -> Client: config = client.config() config["stream_config"] = StreamConfig( - stores=[store if store is not None else MemoryStreamStore()], + stores=[store if store is not None else DictStreamStore()], workflow_read_replay_from=replay_from, ) if codec: @@ -243,7 +239,7 @@ class ReadArgs: wait_ms: int | None = None max_records: int | None = None max_bytes: int | None = None - store: str = "memory" + store: str = "dict" pages: int | None = None """The most pages to read. ``None`` reads until the iterator ends.""" @@ -881,48 +877,6 @@ async def test_one_record_reads_of_one_page_keep_its_marker( # What `read_page` checks, and where it can't run. -# A client's read clamps each of these, or refuses it, and a Workflow read does the same. -_READ_LIMIT_CASES: list[dict[str, Any]] = [ - {"max_bytes": -1}, - {"max_records": 0}, - {"max_records": -1}, - {"max_records": 5000}, - {"wait": timedelta(seconds=-1)}, - {"wait": timedelta(seconds=120)}, -] - - -@workflow.defn -class ReadLimitsWorkflow: - @workflow.run - async def run(self, ref: StreamRef[str]) -> list[Any]: - stream = workflow.get_stream(ref, body_type=str) - results: list[Any] = [] - for arguments in _READ_LIMIT_CASES: - try: - page = await stream.read_page(**arguments) - results.append(len(page.records)) - except StreamError as err: - results.append([StreamErrorReason.Name(err.reason), err.message]) - return results - - -async def test_read_takes_the_limits_of_a_client_read(client: Client): - stream_client = _stream_client(client) - ref = await _filled_stream(stream_client, ["a", "b", "c"]) - _, results = await _run(stream_client, ReadLimitsWorkflow, ref) - stream = stream_client.get_stream(ref, body_type=str) - expected: list[Any] = [] - for arguments in _READ_LIMIT_CASES: - try: - page = await stream.read_page(**arguments) - expected.append(len(page.records)) - except StreamError as err: - expected.append([StreamErrorReason.Name(err.reason), err.message]) - assert results == expected - assert results[0] == ["STREAM_ERROR_REASON_INVALID", expected[0][1]] - - @workflow.defn class LongOffsetWorkflow: @workflow.run @@ -1080,21 +1034,21 @@ async def _failed_replay( assert isinstance(result.replay_failure, RuntimeError) assert str(result.replay_failure) == str(err.value) message = str(err.value) - assert f"StreamReplayError: Stream `{ref.id}` in Store `memory`" in message + assert f"StreamReplayError: Stream `{ref.id}` in Store `dict`" in message return message async def test_replay_of_store_marker_with_no_stream_config_fails(client: Client): history, ref, _ = await _history_of_a_read(client, StreamReplaySource.STORE) message = await _failed_replay(history, ref, None) - assert "this Worker has no Stream runtime for the Store `memory`" in message + assert "this Worker has no Stream runtime for the Store `dict`" in message async def test_replay_of_store_marker_fails_when_records_are_gone(client: Client): history, ref, _ = await _history_of_a_read(client, StreamReplaySource.STORE) - # A new memory Store of the same name has none of the Records. + # A new Store of the same name has none of the Records. message = await _failed_replay( - history, ref, StreamConfig(stores=[MemoryStreamStore()]) + history, ref, StreamConfig(stores=[DictStreamStore()]) ) assert "stream_not_found" in message @@ -1777,7 +1731,7 @@ async def run(self, ref: StreamRef[str]) -> list[str]: async def test_payload_codec_covers_direct_append_and_read(client: Client): - store = MemoryStreamStore() + store = DictStreamStore() stream_client = _stream_client(client, store, codec=True) ref = await _filled_stream(stream_client, [], close=False) _, bodies = await _run(stream_client, AppendThenReadWorkflow, ref) @@ -1975,7 +1929,7 @@ async def run(self, ref: StreamRef[str]) -> list[str]: async def test_codec_runs_with_no_context_on_stream_bodies(client: Client): - store = MemoryStreamStore() + store = DictStreamStore() config = client.config() config["stream_config"] = StreamConfig(stores=[store]) config["data_converter"] = DataConverter(payload_codec=ContextCodec()) @@ -2033,8 +1987,8 @@ async def test_store_replay_of_an_unnamed_ref_with_no_config_fails(client: Clien with pytest.raises(RuntimeError) as err: await Replayer(workflows=[UnnamedRefReadWorkflow]).replay_workflow(history) message = str(err.value) - assert f"StreamReplayError: Stream `{stream_id}` in Store `memory`" in message - assert "this Worker has no Stream runtime for the Store `memory`" in message + assert f"StreamReplayError: Stream `{stream_id}` in Store `dict`" in message + assert "this Worker has no Stream runtime for the Store `dict`" in message assert "Nondeterminism" not in message @@ -2092,41 +2046,6 @@ async def test_page_with_a_body_that_fails_to_decode_raises_each_time( assert events == [["got", 1], ["page", "TypeError"], ["page", "TypeError"]] -# A Worker with no Workflows and a Store with a bad option. - - -@activity.defn -async def noop() -> None: - pass - - -class BadOptionStore(_BridgeStreamStore): - def __init__(self) -> None: - super().__init__(name="bad-option", runtime=None) - - def _new_stream_runtime( - self, runtime: temporalio.bridge.runtime.Runtime - ) -> temporalio.bridge.streams.StreamRuntime: - raise ValueError("The Store option is wrong") - - -async def test_worker_without_workflows_builds_no_store_runtime(client: Client): - config = client.config() - config["stream_config"] = StreamConfig(stores=[BadOptionStore()]) - # Such a Worker starts, as it did before Direct calls, since it gives Core no runtime. - async with Worker( - Client(**config), task_queue=f"tq-{uuid.uuid4()}", activities=[noop] - ): - pass - # A Worker with Workflows gives Core the runtime, so it builds it immediately. - with pytest.raises(ValueError, match="The Store option is wrong"): - Worker( - Client(**config), - task_queue=f"tq-{uuid.uuid4()}", - workflows=[AppendOneWorkflow], - ) - - # The Direct close. @@ -2309,7 +2228,7 @@ async def test_close_without_a_cache_and_in_replays(client: Client): history = await handle.fetch_history() assert [entry.resolved for entry in _close_entries(history)] == [False, True] # A Store with the same Stream, still open, shows that a replay closes nothing. - replay_store = MemoryStreamStore() + replay_store = DictStreamStore() replay_client = _stream_client(client, replay_store) await replay_client.create_stream(id=ref.id, body_type=str) await Replayer( @@ -2429,7 +2348,7 @@ async def test_direct_calls_record_their_metrics(env: WorkflowEnvironment): buffer = MetricBuffer(10000) runtime = Runtime(telemetry=TelemetryConfig(metrics=buffer)) client = await env.connect_client(runtime=runtime) - store = MemoryStreamStore(name="metered", runtime=runtime) + store = DictStreamStore(name="metered") stream_client = _stream_client(client, store) source = await _filled_stream(stream_client, ["a", "b", "c"]) out = await _filled_stream(stream_client, [], close=False) @@ -2575,13 +2494,13 @@ async def test_workflow_starts_each_tool_as_its_call_arrives(client: Client): def test_read_ahead_bytes_below_one_raises(): for bytes_ in (0, -1): with pytest.raises(ValueError, match="workflow_read_ahead_bytes"): - StreamConfig(stores=[MemoryStreamStore()], workflow_read_ahead_bytes=bytes_) + StreamConfig(stores=[DictStreamStore()], workflow_read_ahead_bytes=bytes_) async def test_read_with_a_read_ahead_of_one_byte_gives_every_record(client: Client): config = client.config() config["stream_config"] = StreamConfig( - stores=[MemoryStreamStore()], workflow_read_ahead_bytes=1 + stores=[DictStreamStore()], workflow_read_ahead_bytes=1 ) stream_client = Client(**config) bodies = [f"record-{index}" for index in range(20)] @@ -2629,7 +2548,7 @@ async def test_store_page_over_256_records_comes_in_pages_of_at_most_256( def _client_with(client: Client, **settings: Any) -> Client: config = client.config() - config["stream_config"] = StreamConfig(stores=[MemoryStreamStore()], **settings) + config["stream_config"] = StreamConfig(stores=[DictStreamStore()], **settings) return Client(**config) @@ -2670,15 +2589,13 @@ async def test_page_cap_of_10_gives_pages_of_at_most_10( def test_page_cap_out_of_bounds_raises(): for records in (0, 1001): with pytest.raises(ValueError, match="workflow_read_page_records"): - StreamConfig( - stores=[MemoryStreamStore()], workflow_read_page_records=records - ) + StreamConfig(stores=[DictStreamStore()], workflow_read_page_records=records) def test_min_gap_out_of_bounds_raises(): for gap in (timedelta(seconds=2), timedelta(milliseconds=-1)): with pytest.raises(ValueError, match="workflow_read_min_gap"): - StreamConfig(stores=[MemoryStreamStore()], workflow_read_min_gap=gap) + StreamConfig(stores=[DictStreamStore()], workflow_read_min_gap=gap) @activity.defn @@ -2783,7 +2700,7 @@ async def read_is_in_history() -> None: assert "core_stream" in _markers(await handle.fetch_history()) await assert_eventually(read_is_in_history) - # A new memory Store of the same name has none of the Records. + # A new Store of the same name has none of the Records. lost_client = _stream_client(client) worker = new_worker( lost_client, workflow_type, task_queue=task_queue, **worker_options diff --git a/tests/nexus/test_stream_store_workflow.py b/tests/nexus/test_stream_store_workflow.py index ee4185a01d..01984d4604 100644 --- a/tests/nexus/test_stream_store_workflow.py +++ b/tests/nexus/test_stream_store_workflow.py @@ -56,7 +56,6 @@ from temporalio.nexus.system.stream_service.workflow._resources.stream import ( Stream as WorkflowStream, ) -from temporalio.testing import MemoryStreamStore from temporalio.worker import ( ActivityInboundInterceptor, ExecuteActivityInput, @@ -230,7 +229,7 @@ async def run(self, stream_id: str) -> StreamRef[str]: class CountingStore(RecordingStreamStore): def __init__(self, name: str) -> None: - super().__init__(MemoryStreamStore(name=name)) + super().__init__(DictStreamStore(name=name)) @activity.defn @@ -289,7 +288,7 @@ async def run(self, stream_id: str) -> None: class FlakyDescribeStore(RecordingStreamStore): def __init__(self, *, failures: int = 0, delay: timedelta = timedelta()) -> None: - super().__init__(MemoryStreamStore()) + super().__init__(DictStreamStore()) self._failures = failures self._delay = delay self.describes = 0 @@ -472,7 +471,7 @@ async def _describe_result( @_APPEND_WORKFLOWS async def test_workflow_stream_uses_store(client: Client, append_workflow: type[Any]): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) handle, ref = await _run_append_workflow(stream_client, append_workflow) stream = stream_client.get_stream(ref, body_type=str) @@ -516,7 +515,7 @@ async def test_workflow_stream_uses_a_store_written_from_scratch(client: Client) async def test_workflow_stream_store_uses_payload_codec( client: Client, append_workflow: type[Any] ): - store = MemoryStreamStore() + store = DictStreamStore() config = client.config() config["data_converter"] = DataConverter(payload_codec=SimpleCodec()) config["stream_config"] = StreamConfig(stores=[store]) @@ -537,7 +536,7 @@ async def test_workflow_stream_store_uses_payload_codec( async def test_workflow_stream_history_has_no_bodies( client: Client, append_workflow: type[Any] ): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) handle, _ = await _run_append_workflow(stream_client, append_workflow) events = (await handle.fetch_history()).events markers = [ @@ -560,11 +559,11 @@ async def test_workflow_stream_history_has_no_bodies( async def test_workflow_stream_replay_does_not_call_store( client: Client, append_workflow: type[Any] ): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) handle, ref = await _run_append_workflow(stream_client, append_workflow) - # A replay of an append that Core made needs a Store that runs on the bridge, as the - # first run had. A new memory Store shows any call, since it has no Stream. - replay_store = MemoryStreamStore() + # A replay of an append that Core made needs a Store, as the first run had. A new + # Store shows any call, since it has no Stream. + replay_store = DictStreamStore() await Replayer( workflows=[append_workflow], stream_config=StreamConfig(stores=[replay_store]), @@ -607,20 +606,16 @@ async def test_workflow_create_without_stream_config_fails_the_workflow( async def test_workflow_read_through_a_nexus_client_goes_through_core( client: Client, ): - async def read_with(store: StreamStore) -> str: - stream_client = _with_store(client, store) - async with new_worker(stream_client, NexusClientReadWorkflow) as worker: - return await stream_client.execute_workflow( - NexusClientReadWorkflow.run, - "ReadRecords", - id=f"workflow-{uuid.uuid4()}", - task_queue=worker.task_queue, - ) - - # Core reads the Store, as for `read_page`, also one written in Python. Nobody created - # the Stream. - assert await read_with(MemoryStreamStore()) == "STREAM_ERROR_REASON_NOT_FOUND" - assert await read_with(DictStreamStore()) == "STREAM_ERROR_REASON_NOT_FOUND" + stream_client = _with_store(client, DictStreamStore()) + async with new_worker(stream_client, NexusClientReadWorkflow) as worker: + result = await stream_client.execute_workflow( + NexusClientReadWorkflow.run, + "ReadRecords", + id=f"workflow-{uuid.uuid4()}", + task_queue=worker.task_queue, + ) + # Core reads the Store, as for `read_page`. Nobody created the Stream. + assert result == "STREAM_ERROR_REASON_NOT_FOUND" def test_workflow_stream_has_the_calls_of_a_client_stream(): @@ -646,7 +641,7 @@ async def test_workflow_stream_store_error_reaches_workflow_code(client: Client) async def test_workflow_describe_of_unknown_stream_raises_stream_error(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) async with new_worker(stream_client, DescribeStreamWorkflow) as worker: reason = await stream_client.execute_workflow( DescribeStreamWorkflow.run, @@ -658,7 +653,7 @@ async def test_workflow_describe_of_unknown_stream_raises_stream_error(client: C async def test_strict_create_in_each_run_of_a_chain_succeeds(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" async with new_worker( stream_client, CreateThenContinueAsNewWorkflow, activities=[append_values] @@ -687,7 +682,7 @@ async def test_strict_create_in_each_run_of_a_chain_succeeds(client: Client): async def test_client_append_after_workflow_append_keeps_offset_order( client: Client, append_workflow: type[Any] ): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) _, ref = await _run_append_workflow(stream_client, append_workflow) stream = stream_client.get_stream(ref, body_type=str) appended = await stream.append("from-client") @@ -725,7 +720,7 @@ async def test_workflow_stream_store_call_options_timeout(client: Client): async def test_worker_keeps_its_stream_stores(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) async with new_worker(stream_client, AppendWorkflow) as worker: with pytest.raises(ValueError, match="Stream Stores"): worker.client = client @@ -734,7 +729,7 @@ async def test_worker_keeps_its_stream_stores(client: Client): async def test_activity_interceptors_see_stream_store_activity(client: Client): interceptor = RecordingInterceptor() - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) async with new_worker( stream_client, CreateAndActivityWorkflow, @@ -749,7 +744,7 @@ async def test_activity_interceptors_see_stream_store_activity(client: Client): ) # The create runs as the SDK's Local Activity, before the Workflow's own Activity. assert interceptor.activity_types == [ - "__temporal_stream_store_call:memory", + "__temporal_stream_store_call:dict", "user_activity", ] @@ -760,7 +755,7 @@ async def test_workflow_task_retry_appends_with_higher_epoch( ): global _fail_next_task _fail_next_task = True - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" async with new_worker( stream_client, AppendThenFailTaskOnceWorkflow, activities=[append_values] @@ -787,7 +782,7 @@ async def test_workflow_task_retry_appends_with_higher_epoch( async def test_user_local_activity_append_has_local_activity_producer(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream = await stream_client.create_stream( id=f"stream-{uuid.uuid4()}", body_type=str ) @@ -813,7 +808,7 @@ async def test_user_local_activity_append_has_local_activity_producer(client: Cl async def test_workflow_ref_of_another_store_fails_the_task(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) async with new_worker(stream_client, DescribeOtherStoreWorkflow) as worker: handle = await stream_client.start_workflow( DescribeOtherStoreWorkflow.run, @@ -981,7 +976,7 @@ async def test_workflow_call_goes_through_the_store_own_activity(client: Client) async def test_workflow_append_returns_offsets_of_records_in_order(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" async with new_worker(stream_client, AppendOffsetsWorkflow) as worker: appended = await stream_client.execute_workflow( @@ -998,7 +993,7 @@ async def test_workflow_append_returns_offsets_of_records_in_order(client: Clien async def test_workflow_append_to_closed_stream_raises_stream_error(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream = await stream_client.create_stream( id=f"stream-{uuid.uuid4()}", body_type=str ) @@ -1014,7 +1009,7 @@ async def test_workflow_append_to_closed_stream_raises_stream_error(client: Clie async def test_workflow_append_with_a_producer_of_another_kind_fails(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" async with new_worker(stream_client, ExplicitProducerWorkflow) as worker: result = await stream_client.execute_workflow( @@ -1038,7 +1033,7 @@ async def _describe_status(client: Client, stream_id: str) -> StreamStatus | Non async def test_workflow_async_with_closes_the_stream(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" async with new_worker(stream_client, AsyncWithCloseWorkflow) as worker: status = await stream_client.execute_workflow( @@ -1059,7 +1054,7 @@ async def test_workflow_async_with_closes_the_stream(client: Client): async def test_worker_with_workflows_only_runs_the_close( client: Client, no_remote_activities: bool ): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" async with Worker( stream_client, @@ -1083,7 +1078,7 @@ async def test_failed_workflow_task_after_direct_close_leaves_the_stream_open( ): global _fail_close_intent_task _fail_close_intent_task = True - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream = await stream_client.create_stream( id=f"stream-{uuid.uuid4()}", body_type=str ) @@ -1150,7 +1145,7 @@ async def _run_two_appends( async def test_workflow_appends_in_one_task_write_as_one_producer(client: Client): run_id, records = await _run_two_appends( - client, MemoryStreamStore(), sleep_between=False + client, DictStreamStore(), sleep_between=False ) assert [record.body for record in records] == ["a", "b"] first, second = _producers(records) @@ -1160,7 +1155,7 @@ async def test_workflow_appends_in_one_task_write_as_one_producer(client: Client async def test_workflow_append_in_a_later_task_has_a_higher_epoch(client: Client): run_id, records = await _run_two_appends( - client, MemoryStreamStore(), sleep_between=True + client, DictStreamStore(), sleep_between=True ) first, second = _producers(records) assert first.id == second.id == f"wf:{run_id}" @@ -1168,7 +1163,7 @@ async def test_workflow_append_in_a_later_task_has_a_higher_epoch(client: Client async def test_workflow_appends_started_together_write_in_call_order(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" async with new_worker(stream_client, GatheredAppendsWorkflow) as worker: await stream_client.execute_workflow( @@ -1183,7 +1178,7 @@ async def test_workflow_appends_started_together_write_in_call_order(client: Cli async def test_workflow_close_started_with_an_append_comes_after_it(client: Client): - stream_client = _with_store(client, MemoryStreamStore()) + stream_client = _with_store(client, DictStreamStore()) stream_id = f"stream-{uuid.uuid4()}" async with new_worker(stream_client, GatheredAppendAndCloseWorkflow) as worker: await stream_client.execute_workflow( From a54e5ac1427807d27b8a20ff34163d3380a01b0a Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Wed, 18 Nov 2026 03:31:43 -0700 Subject: [PATCH 32/35] Left only the adapter for a Python Store in the bridge. Core's Stream crate has one `StreamStore` trait and no Store of its own, so the bridge keeps only the adapter that calls a Store written in Python. The `streams` cargo feature goes, since Core links the crate in every build. --- changelog/added/chattering-ledger-otter.md | 2 +- changelog/added/patient-river-heron.md | 2 +- temporalio/bridge/Cargo.lock | 81 +++++++++++- temporalio/bridge/Cargo.toml | 2 + temporalio/bridge/sdk-core | 2 +- temporalio/bridge/src/lib.rs | 25 +--- temporalio/bridge/src/streams.rs | 113 ++--------------- temporalio/bridge/src/worker.rs | 9 +- temporalio/bridge/streams.py | 68 +---------- temporalio/client/_stream_store.py | 136 ++------------------- temporalio/contrib/streams/README.md | 14 +-- temporalio/exceptions.py | 17 +-- temporalio/testing/_stream_store.py | 57 --------- temporalio/worker/_stream_store.py | 69 +++-------- tests/nexus/test_stream_direct.py | 4 + 15 files changed, 148 insertions(+), 453 deletions(-) delete mode 100644 temporalio/testing/_stream_store.py diff --git a/changelog/added/chattering-ledger-otter.md b/changelog/added/chattering-ledger-otter.md index acb0a8fba7..a165dffa36 100644 --- a/changelog/added/chattering-ledger-otter.md +++ b/changelog/added/chattering-ledger-otter.md @@ -1,3 +1,3 @@ -Add the experimental Streams API. Client and Activity code create a Stream, append Records to it, read it and close it. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `RedisStreamStore` of the `temporalio-streams-redis` package, or `temporalio.testing.MemoryStreamStore` in tests. +Add the experimental Streams API. Client and Activity code create a Stream, append Records to it, read it and close it. A client takes its Stream Stores in `stream_config=StreamConfig(stores=[...])`, such as `RedisStreamStore` of the `temporalio-streams-redis` package, or `MemoryStreamStore` of the `temporalio-streams-memory` package in tests. Add `temporalio.contrib.streams.current_producer` and `as_producer`. An Activity can store its Producer in its heartbeat details and pass it to the appends of its retry. The retry's Records then continue the first attempt's epoch, so a reader does not take them as a replacement of the first attempt's Records. A Workflow outbound interceptor sees a Stream operation only in its operation-specific method, such as `start_append_records`, and not in `start_system_nexus_operation`. An unknown Stream operation raises `ValueError`. diff --git a/changelog/added/patient-river-heron.md b/changelog/added/patient-river-heron.md index a473d3033b..f9747e768b 100644 --- a/changelog/added/patient-river-heron.md +++ b/changelog/added/patient-river-heron.md @@ -1,4 +1,4 @@ -Workflow code creates a Stream, appends Records to it, reads it, describes it and closes it, with `workflow.create_stream` and `workflow.get_stream`. A create and a describe run in the Store's Local Activity, with the options of `StreamConfig.workflow_call_options`. Core makes a read, an append and a close itself, with no Activity, on `MemoryStreamStore` and on a Store written in Python, such as `RedisStreamStore` of the `temporalio-streams-redis` package. Core calls a Store written in Python as a task on the Worker's event loop, never on a Workflow thread. For Workflow code, such a Store waits up to a read's `wait`, ends that wait on a cancel, gives offsets of 1 to 1024 bytes that compare in order, fences a lower epoch of a Producer, writes a resent batch once, and raises `StreamError` with a reason. Core tries a call again for up to 30 s only on `STREAM_ERROR_REASON_STORAGE_UNAVAILABLE`. Any other exception reaches Workflow code on the first try, as a non-retryable `STREAM_ERROR_REASON_STORAGE_CORRUPT` with its type in the message. `Replayer` takes the `stream_config` of the Worker's client. +Workflow code creates a Stream, appends Records to it, reads it, describes it and closes it, with `workflow.create_stream` and `workflow.get_stream`. A create and a describe run in the Store's Local Activity, with the options of `StreamConfig.workflow_call_options`. Core makes a read, an append and a close itself, with no Activity. It calls the Store as a task on the Worker's event loop, never on a Workflow thread. For Workflow code, a Store waits up to a read's `wait`, ends that wait on a cancel, gives offsets of 1 to 1024 bytes that compare in order, fences a lower epoch of a Producer, writes a resent batch once, and raises `StreamError` with a reason. Core tries a call again for up to 30 s only on `STREAM_ERROR_REASON_STORAGE_UNAVAILABLE`. Any other exception reaches Workflow code on the first try, as a non-retryable `STREAM_ERROR_REASON_STORAGE_CORRUPT` with its type in the message. `Replayer` takes the `stream_config` of the Worker's client. The Workflow `Stream` has `read`, `read_pages` and `read_page`, so Workflow code reads a Stream with the same calls as a client. Core reads the Store inside the Workflow Task. A page that Workflow code gets in one activation has at most 256 Records, and the rest come in the next activation with no wait. A loop that asks for its next page in the same activation keeps one read open, and a read that no code waits on closes before the activation completes, so it never holds a Workflow Task open. With a `wait`, a quiet Stream gives empty pages, so a loop on a Stream that can be quiet for long uses `read_pages` and leaves after an empty page. A call that waits on a quiet Stream holds its Workflow Task open, with one Workflow Task about every 8 s. The calls of one Workflow Task write one marker in History. `StreamConfig.workflow_read_replay_from` decides whether the marker holds the Records (`StreamReplaySource.HISTORY`) or only their offsets and a digest, which a replay reads from the Store again (`StreamReplaySource.STORE`, the default). With `HISTORY`, one Workflow Task's marker holds at most 1 MiB of Records, and the read goes on in the next task. A Workflow's append runs in Core inside the Workflow Task, with no Activity. It writes as the Workflow, with its Workflow Task as the epoch. A `producer=` of another kind raises `StreamError` with the reason `STREAM_ERROR_REASON_INVALID`. A Workflow's close runs in Core, with no Activity, and spans two Workflow Tasks. The task that calls `close` records the intent, Core closes the Stream after the server took that task, and the next task gives the result, so a failed task closes nothing. Await the close. A graceful shutdown of the Worker waits at most 10 s for a close that the code doesn't await, and a Worker that dies when the Workflow ends loses it. An append and a close of Workflow code always carry the Workflow Task as their epoch. An append is at-least-once. After a Worker crash or an eviction, the rerun of its task can write a second copy with the epoch of the new task. diff --git a/temporalio/bridge/Cargo.lock b/temporalio/bridge/Cargo.lock index 8a76ca598a..bae80dc51c 100644 --- a/temporalio/bridge/Cargo.lock +++ b/temporalio/bridge/Cargo.lock @@ -181,6 +181,15 @@ version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bon" version = "3.10.3" @@ -265,7 +274,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ "cfg-if", - "cpufeatures", + "cpufeatures 0.3.1", "rand_core 0.10.1", ] @@ -343,6 +352,15 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "cpufeatures" version = "0.3.1" @@ -376,6 +394,16 @@ version = "0.8.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "darling" version = "0.24.1" @@ -433,6 +461,16 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "dirs" version = "7.0.0" @@ -728,6 +766,16 @@ dependencies = [ "slab", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "gethostname" version = "1.1.0" @@ -2390,6 +2438,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -2593,6 +2652,7 @@ dependencies = [ "temporalio-client", "temporalio-common", "temporalio-sdk-core", + "temporalio-streams", "tokio", "tokio-rustls", "tokio-stream", @@ -2777,6 +2837,19 @@ dependencies = [ "zip", ] +[[package]] +name = "temporalio-streams" +version = "0.1.0" +dependencies = [ + "async-trait", + "prost", + "prost-wkt-types", + "sha2", + "temporalio-common", + "thiserror", + "tonic", +] + [[package]] name = "termtree" version = "0.5.1" @@ -3142,6 +3215,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c" +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + [[package]] name = "typetag" version = "0.2.23" diff --git a/temporalio/bridge/Cargo.toml b/temporalio/bridge/Cargo.toml index 4b23ae6ce9..a6bb863c45 100644 --- a/temporalio/bridge/Cargo.toml +++ b/temporalio/bridge/Cargo.toml @@ -72,6 +72,8 @@ temporalio-sdk-core = { version = "0.11.0", path = "./sdk-core/crates/sdk-core", "envconfig", "prometheus", ] } +# The Stream runtime, from the submodule. +temporalio-streams = { path = "./sdk-core/crates/streams" } tokio = "1.26" # Matches the sdk-core client crate's rustls stack; used to build the custom # server certificate verifier for ClientTlsConfig.verification_server_name. diff --git a/temporalio/bridge/sdk-core b/temporalio/bridge/sdk-core index c90f015fd0..55c1b6cf3f 160000 --- a/temporalio/bridge/sdk-core +++ b/temporalio/bridge/sdk-core @@ -1 +1 @@ -Subproject commit c90f015fd067978ce630025c48d84e0f07318f12 +Subproject commit 55c1b6cf3f9d3fde109fbe6ebb62ba0d4db909ef diff --git a/temporalio/bridge/src/lib.rs b/temporalio/bridge/src/lib.rs index 64dff02650..3de2b73069 100644 --- a/temporalio/bridge/src/lib.rs +++ b/temporalio/bridge/src/lib.rs @@ -50,13 +50,8 @@ fn temporal_sdk_bridge(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(raise_in_thread, m)?)?; // Stream stuff - m.add("StreamCallError", py.get_type::())?; - #[cfg(feature = "streams")] - { - m.add_class::()?; - m.add_function(wrap_pyfunction!(new_memory_stream_runtime, m)?)?; - m.add_function(wrap_pyfunction!(new_python_stream_runtime, m)?)?; - } + m.add_class::()?; + m.add_function(wrap_pyfunction!(new_python_stream_runtime, m)?)?; // Testing stuff m.add_class::()?; @@ -114,26 +109,12 @@ fn raise_in_thread(py: Python, thread_id: std::os::raw::c_long, exc: &Bound<'_, runtime::raise_in_thread(py, thread_id, exc) } -#[cfg(feature = "streams")] -#[pyfunction] -#[pyo3(signature = (runtime_ref, store_name, retention=None, producer_idle_ttl=None))] -fn new_memory_stream_runtime( - runtime_ref: &runtime::RuntimeRef, - store_name: String, - retention: Option, - producer_idle_ttl: Option, -) -> PyResult { - streams::new_memory_stream_runtime(runtime_ref, store_name, retention, producer_idle_ttl) -} - -#[cfg(feature = "streams")] #[pyfunction] fn new_python_stream_runtime( runtime_ref: &runtime::RuntimeRef, - store_name: String, caller: Py, ) -> PyResult { - streams::new_python_stream_runtime(runtime_ref, store_name, caller) + streams::new_python_stream_runtime(runtime_ref, caller) } #[pyfunction] diff --git a/temporalio/bridge/src/streams.rs b/temporalio/bridge/src/streams.rs index bb45bbde86..2625d40a45 100644 --- a/temporalio/bridge/src/streams.rs +++ b/temporalio/bridge/src/streams.rs @@ -1,91 +1,30 @@ -//! Bindings to the Stream runtime of the `temporalio-streams` crate, on the memory Store or on a -//! Store written in Python. Python calls the five Stream service operations by name, with the -//! encoded request, and gets the encoded response. +//! The adapter that lets Core call a Stream Store written in Python for Workflow code. -use pyo3::exceptions::PyException; -#[cfg(feature = "streams")] use pyo3::prelude::*; -#[cfg(feature = "streams")] use crate::runtime; -// The exception args are the gRPC code, the message and the encoded details, as in `RPCError`. -pyo3::create_exception!(temporal_sdk_bridge, StreamCallError, PyException); - -/// The crate's runtime object. Each Store on the bridge holds one. -#[cfg(feature = "streams")] +/// The crate's runtime object. Each Store written in Python has one. #[pyclass(frozen)] pub struct StreamRuntimeRef { pub(crate) streams: std::sync::Arc, - runtime: runtime::Runtime, -} - -#[cfg(feature = "streams")] -#[pymethods] -impl StreamRuntimeRef { - /// Runs one Stream operation on an encoded request. The future gives the encoded response. - fn call<'p>( - &self, - py: Python<'p>, - operation: String, - request: Vec, - ) -> PyResult> { - self.runtime - .assert_same_process("call the Stream runtime")?; - let streams = self.streams.clone(); - self.runtime.future_into_py(py, async move { - streams.call(&operation, &request).await.map_err(call_error) - }) - } -} - -#[cfg(feature = "streams")] -fn call_error(err: temporalio_streams::StreamError) -> PyErr { - StreamCallError::new_err((err.code() as u32, err.message.clone(), err.details())) -} - -/// Builds the runtime on a new memory Store. A `None` duration takes the crate's default. -#[cfg(feature = "streams")] -pub fn new_memory_stream_runtime( - runtime_ref: &runtime::RuntimeRef, - store_name: String, - retention: Option, - producer_idle_ttl: Option, -) -> PyResult { - let runtime = runtime_ref.runtime.clone(); - runtime.assert_same_process("create a Stream runtime")?; - // The runtime can spawn Tokio tasks, which need an entered Tokio runtime. - let _guard = runtime.core.tokio_handle().enter(); - let meter = runtime.core.telemetry().get_temporal_metric_meter(); - let streams = temporalio_streams::MemoryStreamRuntime::new( - store_name, - temporalio_streams::MemoryStreamOptions { - retention, - producer_idle_ttl, - }, - meter, - ); - Ok(StreamRuntimeRef { streams, runtime }) } -/// Builds the runtime on a Store written in Python. Core calls it for Workflow code through the -/// runtime, which checks each request and answer as for a Store in Rust. `caller` runs each call -/// of the Store on the Worker's event loop, never on a Workflow thread. -#[cfg(feature = "streams")] +/// Builds the runtime on a Store written in Python. Core calls the Store through it for Workflow +/// code, and the runtime checks each request and answer. `caller` runs each call of the Store on +/// the Worker's event loop, never on a Workflow thread. pub fn new_python_stream_runtime( runtime_ref: &runtime::RuntimeRef, - store_name: String, caller: Py, ) -> PyResult { - let runtime = runtime_ref.runtime.clone(); + let runtime = &runtime_ref.runtime; runtime.assert_same_process("create a Stream runtime")?; let meter = runtime.core.telemetry().get_temporal_metric_meter(); let store = std::sync::Arc::new(python_store::PythonStore { caller }); - let streams = temporalio_streams::StreamRuntime::with_operations(store_name, store, meter); - Ok(StreamRuntimeRef { streams, runtime }) + let streams = temporalio_streams::StreamRuntime::new(store, meter); + Ok(StreamRuntimeRef { streams }) } -#[cfg(feature = "streams")] mod python_store { use futures::channel::oneshot; use prost::Message; @@ -94,34 +33,14 @@ mod python_store { use std::sync::Mutex; use std::time::Duration; use temporalio_streams::{ - operations::OperationStore, proto::{ AppendRecordsRequest, AppendRecordsResponse, CloseStreamRequest, CloseStreamResponse, CreateStreamRequest, CreateStreamResponse, DescribeStreamRequest, DescribeStreamResponse, ReadRecordsRequest, ReadRecordsResponse, }, - Reason, StreamError, StreamResult, + Reason, StreamError, StreamResult, StreamStore, }; - /// The reasons that a Stream failure can carry to Workflow code. - const REASONS: [Reason; 15] = [ - Reason::NotFound, - Reason::Expired, - Reason::Closed, - Reason::Fenced, - Reason::SequenceTooLow, - Reason::SeqDivergent, - Reason::NotOwner, - Reason::FormatTooNew, - Reason::OffsetTooHigh, - Reason::TooLarge, - Reason::Invalid, - Reason::StorageUnavailable, - Reason::Unsupported, - Reason::AlreadyExists, - Reason::StorageCorrupt, - ]; - /// The longest pause after an empty page that came before its wait ended. A Store that /// ignores `wait` then gets a read about ten times a second, and not in a busy loop. const EMPTY_PAGE_PAUSE: Duration = Duration::from_millis(100); @@ -171,12 +90,6 @@ mod python_store { } } - fn reason(value: i32) -> Option { - REASONS - .into_iter() - .find(|reason| reason.proto() as i32 == value) - } - impl PythonStore { async fn call( &self, @@ -207,9 +120,9 @@ mod python_store { "The Stream Store gave a bad {operation} response: {err}" )) }), - Ok(Err((value, message))) => match reason(value) { - Some(reason) => Err(StreamError::new(reason, message)), - None => Err(StreamError::storage_corrupt(format!( + Ok(Err((value, message))) => match Reason::try_from(value) { + Ok(reason) => Err(StreamError::new(reason, message)), + Err(_) => Err(StreamError::storage_corrupt(format!( "The Stream Store failed with the unknown reason {value}: {message}" ))), }, @@ -221,7 +134,7 @@ mod python_store { } #[async_trait::async_trait] - impl OperationStore for PythonStore { + impl StreamStore for PythonStore { async fn create_stream( &self, request: CreateStreamRequest, diff --git a/temporalio/bridge/src/worker.rs b/temporalio/bridge/src/worker.rs index 56b6fc838f..c4f8848d99 100644 --- a/temporalio/bridge/src/worker.rs +++ b/temporalio/bridge/src/worker.rs @@ -65,9 +65,8 @@ pub struct WorkerConfig { plugins: Vec<(String, String)>, storage_drivers: HashSet, disable_payload_error_limit: bool, - /// The Stream runtime of each Store that has one, by Store name. Core reads and appends a - /// Stream of Workflow code through them. - #[cfg(feature = "streams")] + /// The Stream runtime of each Store, by Store name. Core reads and appends a Stream of + /// Workflow code through them. stream_runtimes: Vec<(String, Py)>, /// The most bytes of one read's Records that wait for Workflow code. `None` keeps Core's /// default. @@ -733,15 +732,11 @@ fn convert_worker_config( ) -> PyResult { let converted_tuner = convert_tuner_holder(conf.tuner, task_locals)?; let converted_versioning_strategy = convert_versioning_strategy(conf.versioning_strategy); - // A build with no Stream runtime has no Store that Core can call, so Core gets none. - #[cfg(feature = "streams")] let stream_runtimes = temporalio_sdk_core::StreamRuntimes::new( conf.stream_runtimes .into_iter() .map(|(store, runtime)| (store, runtime.get().streams.clone())), ); - #[cfg(not(feature = "streams"))] - let stream_runtimes = temporalio_sdk_core::StreamRuntimes::default(); let stream_runtimes = match conf.stream_read_ahead_bytes { Some(bytes) => stream_runtimes.with_read_ahead_bytes(bytes), None => stream_runtimes, diff --git a/temporalio/bridge/streams.py b/temporalio/bridge/streams.py index b4c418d693..7e43276815 100644 --- a/temporalio/bridge/streams.py +++ b/temporalio/bridge/streams.py @@ -1,91 +1,35 @@ -"""The Stream runtime that this bridge build links. (unstable) +"""The Stream runtime that Core uses to call a Stream Store written in Python. (unstable) Nothing in this module should be considered stable. The API may change. """ from __future__ import annotations -from datetime import timedelta from typing import Any import temporalio.bridge.runtime import temporalio.bridge.temporal_sdk_bridge -import temporalio.exceptions - -StreamCallError: type[Exception] = temporalio.bridge.temporal_sdk_bridge.StreamCallError class StreamRuntime: - """The runtime of the ``temporalio-streams`` crate on one Store.""" - - @staticmethod - def new_memory( - runtime: temporalio.bridge.runtime.Runtime, - *, - name: str, - retention: timedelta | None, - producer_idle_ttl: timedelta | None, - ) -> StreamRuntime: - """Build the runtime on a new memory Store. - - Raises: - temporalio.exceptions.StreamConfigError: The bridge build has no Stream - runtime. - """ - new = getattr( - temporalio.bridge.temporal_sdk_bridge, "new_memory_stream_runtime", None - ) - if new is None: - raise temporalio.exceptions.StreamConfigError( - "This build of temporalio has no Stream runtime" - ) - return StreamRuntime(new(runtime._ref, name, retention, producer_idle_ttl)) + """The runtime of the ``temporalio-streams`` crate on one Store written in Python.""" @staticmethod def new_python( runtime: temporalio.bridge.runtime.Runtime, *, - name: str, caller: Any, ) -> StreamRuntime: """Build the runtime on a Store written in Python, which Core calls for Workflow code. ``caller`` runs each call of the Store on the Worker's event loop. - - Raises: - temporalio.exceptions.StreamConfigError: The bridge build has no Stream - runtime. """ - new = getattr( - temporalio.bridge.temporal_sdk_bridge, "new_python_stream_runtime", None - ) - if new is None: - raise temporalio.exceptions.StreamConfigError( - "This build of temporalio has no Stream runtime" + return StreamRuntime( + temporalio.bridge.temporal_sdk_bridge.new_python_stream_runtime( + runtime._ref, caller ) - return StreamRuntime(new(runtime._ref, name, caller)) - - @staticmethod - def has_runtime() -> bool: - """Whether the bridge build has the Stream runtime.""" - return hasattr( - temporalio.bridge.temporal_sdk_bridge, "new_python_stream_runtime" ) def __init__(self, ref: Any) -> None: - """Wrap the bridge's runtime object. Use ``new_memory`` or ``new_python`` instead.""" + """Wrap the bridge's runtime object. Use ``new_python`` instead.""" self._ref = ref - - async def call(self, operation: str, request: bytes) -> bytes: - """Run one Stream operation on an encoded request and return the encoded response. - - Raises: - temporalio.exceptions.StreamError: The call failed. - """ - try: - return await self._ref.call(operation, request) - except StreamCallError as err: - _, message, details = err.args - raise temporalio.exceptions._stream_error_from_call( - message, details - ) from None diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index 814767ef6c..2d264e6b2d 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -5,7 +5,7 @@ from abc import ABC, abstractmethod from collections.abc import Awaitable, Callable, Sequence from datetime import timedelta -from typing import TYPE_CHECKING, Any +from typing import Any import google.protobuf.message import nexusrpc @@ -13,18 +13,13 @@ import temporalio.activity import temporalio.api.streamservice.v1 import temporalio.bridge._visitor -import temporalio.bridge.runtime import temporalio.common import temporalio.converter import temporalio.nexus.system.stream_service -import temporalio.runtime import temporalio.workflow from temporalio.converter import StorageDriverActivityInfo, StorageDriverWorkflowInfo from temporalio.exceptions import StreamConfigError -if TYPE_CHECKING: - import temporalio.bridge.streams - _MAX_RECORD_BYTES = 1536 * 1024 _MAX_BATCH_BYTES = 4 * 1024 * 1024 _MAX_BATCH_RECORDS = 1000 @@ -145,21 +140,20 @@ class StreamStore(ABC): A client with a Store sends each Stream operation to it. This release has no Streams on the server, so a client needs a Store for any Stream operation. Use a Store of a package, such as ``RedisStreamStore`` of ``temporalio-streams-redis``, or - :py:class:`temporalio.testing.MemoryStreamStore` in tests. + ``MemoryStreamStore`` of ``temporalio-streams-memory`` in tests. - The memory Store runs on the bridge, in Rust. Every other Store is a subclass, - written in Python, that implements :py:attr:`name` and the five operations. It can - call its own native code, as the Redis Store does. Each operation takes and gives a - message of ``temporalio.api.streamservice.v1``. The bodies of the Records arrive as - payloads that the SDK already encoded with the data converter and the payload codec, - so a Store keeps payloads and not the user's types. A failed call raises - :py:class:`temporalio.exceptions.StreamError` with a reason. + A Store is a subclass, written in Python, that implements :py:attr:`name` and the + five operations. It can call its own native code, as the Redis Store does. Each + operation takes and gives a message of ``temporalio.api.streamservice.v1``. The + bodies of the Records arrive as payloads that the SDK already encoded with the data + converter and the payload codec, so a Store keeps payloads and not the user's types. + A failed call raises :py:class:`temporalio.exceptions.StreamError` with a reason. Such a Store works for client, Activity and Workflow code. Core makes a read, an - append and a close of Workflow code, as it does on the memory Store. It calls the - Store's method as a task on the Worker's event loop, never on a Workflow thread, and - records the result in History. A create and a describe of Workflow code run in the - Store's Local Activity. For Workflow code, a Store must do these things. + append and a close of Workflow code. It calls the Store's method as a task on the + Worker's event loop, never on a Workflow thread, and records the result in History. + A create and a describe of Workflow code run in the Store's Local Activity. For + Workflow code, a Store must do these things. * Each method is ``async`` and never blocks the event loop, since the calls of every Workflow on the Worker run there. @@ -296,92 +290,6 @@ async def call_from_workflow( return stream_operation.response_type.FromString(response) -class _BridgeStreamStore(StreamStore): # pyright: ignore[reportUnusedClass] - """A Store that runs on the Stream runtime of the bridge. - - It builds the runtime at its first call, or when a Worker with Workflows gives it to - Core, so the Store can be built before the Runtime and its Tokio threads exist. - """ - - def __init__( - self, - *, - name: str, - runtime: temporalio.runtime.Runtime | None, - ) -> None: - self._name = name - self._runtime = runtime - self._stream_runtime: temporalio.bridge.streams.StreamRuntime | None = None - - @property - def name(self) -> str: - """Name of the Store.""" - return self._name - - @abstractmethod - def _new_stream_runtime( - self, runtime: temporalio.bridge.runtime.Runtime - ) -> temporalio.bridge.streams.StreamRuntime: ... - - async def create_stream( - self, request: temporalio.api.streamservice.v1.CreateStreamRequest - ) -> temporalio.api.streamservice.v1.CreateStreamResponse: - """See base class.""" - return await self._call_bridge("CreateStream", request) - - async def append_records( - self, request: temporalio.api.streamservice.v1.AppendRecordsRequest - ) -> temporalio.api.streamservice.v1.AppendRecordsResponse: - """See base class.""" - return await self._call_bridge("AppendRecords", request) - - async def read_records( - self, request: temporalio.api.streamservice.v1.ReadRecordsRequest - ) -> temporalio.api.streamservice.v1.ReadRecordsResponse: - """See base class.""" - return await self._call_bridge("ReadRecords", request) - - async def close_stream( - self, request: temporalio.api.streamservice.v1.CloseStreamRequest - ) -> temporalio.api.streamservice.v1.CloseStreamResponse: - """See base class.""" - return await self._call_bridge("CloseStream", request) - - async def describe_stream( - self, request: temporalio.api.streamservice.v1.DescribeStreamRequest - ) -> temporalio.api.streamservice.v1.DescribeStreamResponse: - """See base class.""" - return await self._call_bridge("DescribeStream", request) - - def _get_stream_runtime(self) -> temporalio.bridge.streams.StreamRuntime: - """Give the Stream runtime of the bridge, and build it at the first use. - - Raises: - temporalio.exceptions.StreamConfigError: This build of temporalio has no - Stream runtime, or not this Store. - """ - if self._stream_runtime is None: - runtime = self._runtime or temporalio.runtime.Runtime.default() - self._stream_runtime = self._new_stream_runtime(runtime._core_runtime) - return self._stream_runtime - - async def _call_bridge( - self, operation: str, request: google.protobuf.message.Message - ) -> Any: - """Answer one Stream operation with the Stream runtime of the bridge. - - Raises: - temporalio.exceptions.StreamError: The call failed. - temporalio.exceptions.StreamConfigError: This build of temporalio has no - Stream runtime. - """ - # The bridge takes and gives the messages in their wire form. - response = await self._get_stream_runtime().call( - operation, request.SerializeToString() - ) - return _STREAM_OPERATIONS[operation].response_type.FromString(response) - - @dataclasses.dataclass(frozen=True) class _StreamOperation: """How a Store answers one operation of the Stream service.""" @@ -711,20 +619,6 @@ def _get_store_by_name(self, name: str) -> StreamStore: ) return store - def _bridge_stream_runtimes( - self, - ) -> list[tuple[str, temporalio.bridge.streams.StreamRuntime]]: - """Give the Stream runtime of each Store that runs on the bridge, by Store name. - - Core reads and appends a Stream of Workflow code through these runtimes. The - Worker builds the runtime of a Store written in Python itself. - """ - return [ - (store.name, store._get_stream_runtime()) - for store in self.stores - if isinstance(store, _BridgeStreamStore) - ] - def _store_for( self, ref: temporalio.nexus.system.stream_service.StreamRef[Any], @@ -772,12 +666,6 @@ def _refuse_store_plugins( # pyright: ignore[reportUnusedFunction] ) -def _check_not_negative(**durations: timedelta | None) -> None: # pyright: ignore[reportUnusedFunction] - for label, value in durations.items(): - if value is not None and value < timedelta(): - raise ValueError(f"{label} must not be negative") - - def _stream_service() -> nexusrpc.ServiceDefinition: service = nexusrpc.get_service_definition( temporalio.nexus.system.stream_service.StreamService diff --git a/temporalio/contrib/streams/README.md b/temporalio/contrib/streams/README.md index a95e3bea49..8e34164fc4 100644 --- a/temporalio/contrib/streams/README.md +++ b/temporalio/contrib/streams/README.md @@ -46,10 +46,9 @@ replacement of the first attempt's Records. ## Reads and appends in Workflow code The helpers run on the Streams API, so they follow how Workflow code reads, appends and -closes. On every Store, such as the Redis Store, `MemoryStreamStore` or a Store written in -Python, Core reads, appends and closes a Stream itself, with no Activity. Workflow code reads -with the same `read`, `read_pages` and `read_page` as a client. It gets the same pages, but -each page has at most 256 Records. +closes. On every Store, such as the Redis Store, Core reads, appends and closes a Stream +itself, with no Activity. Workflow code reads with the same `read`, `read_pages` and +`read_page` as a client. It gets the same pages, but each page has at most 256 Records. Here the Workflow reads the events of a model's answer while the model Activity still runs, and starts each tool as its call arrives. @@ -152,10 +151,9 @@ after the server took that task. A graceful shutdown of the Worker waits for suc at most 10 s. Only a Worker that dies there, or a Store that doesn't answer in that time, loses the close, and the Stream stays open. -A Store can be written in Python, as a subclass of `StreamStore`, and it works for Workflow -code too. Core makes a read, an append and a close of Workflow code on it with the same marker -and rules as on a Store in Rust. It calls the Store's method as a task on the Worker's event -loop, never on a Workflow thread. For Workflow code, such a Store must do these things. +A Store is a subclass of `StreamStore`, written in Python. Core makes a read, an append and a +close of Workflow code on it. It calls the Store's method as a task on the Worker's event loop, +never on a Workflow thread. For Workflow code, a Store must do these things. - Each method is `async` and never blocks the event loop. - `read_records` waits up to the request's `wait` when it has no Record after `from_offset`, and diff --git a/temporalio/exceptions.py b/temporalio/exceptions.py index f854061d68..6954c08cde 100644 --- a/temporalio/exceptions.py +++ b/temporalio/exceptions.py @@ -260,14 +260,6 @@ def _stream_error_from_reason(reason: int, message: str) -> StreamError: ) -def _stream_error_from_call( # pyright: ignore[reportUnusedFunction] - message: str, details: bytes -) -> StreamError: - # A Store on the bridge fails with the encoded details, as a gRPC status has them. - proto = temporalio.api.stream.v1.StreamErrorDetails.FromString(details) - return _stream_error_from_reason(proto.reason, message) - - def _stream_error_from_failure( # pyright: ignore[reportUnusedFunction] err: BaseException, ) -> StreamError | None: @@ -303,14 +295,11 @@ class StreamConfigError(RuntimeError): * A :py:class:`temporalio.client.StreamStore` is given in ``plugins``. * A Workflow's Stream call has a ref that names a Store that the config of the Worker's client doesn't have. This fails the Workflow Task. - * This build of temporalio has no Stream runtime. A Workflow's Stream call on a Worker whose client has no Stream config doesn't - raise this class. Neither does a read, an append or a close of Workflow code on a - Store written in Python, in a build of temporalio with no Stream runtime for Core to - call it. Workflow code gets an :py:class:`ApplicationError` with the ``type`` - ``StreamConfigError`` and ``non_retryable`` set, so an uncaught one fails the - Workflow. Workflow code can catch this error. A Workflow that catches it must not go + raise this class. Workflow code gets an :py:class:`ApplicationError` with the + ``type`` ``StreamConfigError`` and ``non_retryable`` set, so an uncaught one fails + the Workflow. Workflow code can catch this error. A Workflow that catches it must not go on to make the same call on a Worker that has a config, since the History then has no record of the first call. diff --git a/temporalio/testing/_stream_store.py b/temporalio/testing/_stream_store.py deleted file mode 100644 index 42c8500fd1..0000000000 --- a/temporalio/testing/_stream_store.py +++ /dev/null @@ -1,57 +0,0 @@ -"""A Stream Store in the memory of the process, for tests.""" - -from __future__ import annotations - -from datetime import timedelta - -import temporalio.bridge.runtime -import temporalio.bridge.streams -import temporalio.client._stream_store -import temporalio.runtime - - -class MemoryStreamStore(temporalio.client._stream_store._BridgeStreamStore): - """A Stream Store that keeps its Streams in the memory of this process, for tests. - - It runs the same Stream runtime and rules as every other Store on the bridge. A - failed call raises :py:class:`temporalio.exceptions.StreamError`. - - .. warning:: - This API is experimental and subject to change. - """ - - def __init__( - self, - *, - name: str = "memory", - retention: timedelta | None = None, - producer_idle_ttl: timedelta | None = None, - runtime: temporalio.runtime.Runtime | None = None, - ) -> None: - """Create a memory Store. It builds its Stream runtime at its first use. - - Args: - name: Name of the Store, which the refs of its Streams carry. - retention: How long a closed Stream stays. ``None`` takes the default of - the Stream runtime. - producer_idle_ttl: How long an open Stream stays after the last write of any - Producer. ``None`` keeps an open Stream until its close. - runtime: Runtime whose Tokio threads run the Store. ``None`` takes - :py:meth:`temporalio.runtime.Runtime.default` at the first use. - """ - temporalio.client._stream_store._check_not_negative( - retention=retention, producer_idle_ttl=producer_idle_ttl - ) - super().__init__(name=name, runtime=runtime) - self._retention = retention - self._producer_idle_ttl = producer_idle_ttl - - def _new_stream_runtime( - self, runtime: temporalio.bridge.runtime.Runtime - ) -> temporalio.bridge.streams.StreamRuntime: - return temporalio.bridge.streams.StreamRuntime.new_memory( - runtime, - name=self._name, - retention=self._retention, - producer_idle_ttl=self._producer_idle_ttl, - ) diff --git a/temporalio/worker/_stream_store.py b/temporalio/worker/_stream_store.py index e1945153f0..35c6839142 100644 --- a/temporalio/worker/_stream_store.py +++ b/temporalio/worker/_stream_store.py @@ -26,7 +26,6 @@ StreamConfig, StreamReplaySource, StreamStore, - _BridgeStreamStore, _request_ref, ) from temporalio.converter import StorageDriverWorkflowInfo @@ -51,33 +50,9 @@ def _no_stream_config_error() -> temporalio.exceptions.ApplicationError: # pyri ) -# The Direct calls of Workflow code, by Nexus operation name. Each has the word of an -# error message and the call that an Activity makes in its place. -_DIRECT_CALLS = { - "ReadRecords": ("read", "Read the Stream"), - "AppendRecords": ("append", "Append to the Stream"), - "CloseStream": ("close", "Close the Stream"), -} - - -# Core calls a Store written in Python through the Stream runtime of the bridge, which a -# build of temporalio may leave out. -_CORE_CALLS_PYTHON_STORES = temporalio.bridge.streams.StreamRuntime.has_runtime() - - -def _no_direct_call_error( - store: StreamStore, operation: str -) -> temporalio.exceptions.ApplicationError: - # Core makes a Workflow's read, append and close itself, and a build with no Stream - # runtime gives Core no way to call the Store. Each retry of the task would fail the - # same way for good, so the Workflow fails instead, as for a Worker with no config. - call, advice = _DIRECT_CALLS[operation] - return temporalio.exceptions.ApplicationError( - f"The Stream Store '{store.name}' has no Direct {call}, since this build of " - f"temporalio has no Stream runtime. {advice} in an Activity.", - type="StreamConfigError", - non_retryable=True, - ) +# The word of an error message for each write that Core makes for Workflow code, by Nexus +# operation name. +_DIRECT_WRITES = {"AppendRecords": "append", "CloseStream": "close"} def _not_a_workflow_producer_error( @@ -85,7 +60,7 @@ def _not_a_workflow_producer_error( ) -> temporalio.exceptions.StreamError: # Core sets the Workflow Task as the epoch of the Producer, which only a Producer of # kind Workflow has. - call, _ = _DIRECT_CALLS[operation] + call = _DIRECT_WRITES[operation] return temporalio.exceptions.StreamError( f"A Stream {call} of Workflow code writes as a Producer of kind Workflow. " "Leave out producer=, or give a Producer of kind Workflow.", @@ -525,35 +500,23 @@ def _core_stream_runtimes( # pyright: ignore[reportUnusedFunction] ) -> _CoreStreamRuntimes: """Give Core a Stream runtime for each Store of the config. - A Store on the bridge has its own runtime. A Store written in Python gets one on - ``runtime`` that calls it on the event loop that :py:meth:`_CoreStreamRuntimes.bind` - gives. + Each runtime is on ``runtime`` and calls its Store on the event loop that + :py:meth:`_CoreStreamRuntimes.bind` gives. """ if config is None: return _CoreStreamRuntimes(runtimes=[], callers=[]) - runtimes = [ - (name, stream_runtime._ref) - for name, stream_runtime in config._bridge_stream_runtimes() - ] + runtimes: list[tuple[str, Any]] = [] callers: list[_CoreStoreCaller] = [] - if _CORE_CALLS_PYTHON_STORES: - for store in config.stores: - if isinstance(store, _BridgeStreamStore): - continue - caller = _CoreStoreCaller(store) - stream_runtime = temporalio.bridge.streams.StreamRuntime.new_python( - runtime._core_runtime, name=store.name, caller=caller - ) - runtimes.append((store.name, stream_runtime._ref)) - callers.append(caller) + for store in config.stores: + caller = _CoreStoreCaller(store) + stream_runtime = temporalio.bridge.streams.StreamRuntime.new_python( + runtime._core_runtime, caller=caller + ) + runtimes.append((store.name, stream_runtime._ref)) + callers.append(caller) return _CoreStreamRuntimes(runtimes=runtimes, callers=callers) -def _core_calls(store: StreamStore) -> bool: - """Whether Core can call ``store`` for a read, an append and a close.""" - return isinstance(store, _BridgeStreamStore) or _CORE_CALLS_PYTHON_STORES - - def _workflow_target() -> StorageDriverWorkflowInfo: # External storage builds the same value for a Workflow. info = temporalio.workflow.info() @@ -593,8 +556,6 @@ def start_operation( options = self.config.workflow_call_options if isinstance(request, temporalio.api.streamservice.v1.ReadRecordsRequest): instance._assert_not_read_only("start stream read") - if not _core_calls(store): - raise _no_direct_call_error(store, input.operation_name) return _start_direct_read( instance, input, @@ -611,8 +572,6 @@ def start_operation( temporalio.api.streamservice.v1.CloseStreamRequest, ), ): - if not _core_calls(store): - raise _no_direct_call_error(store, input.operation_name) return _start_direct_call(instance, input, request, store.name) # A create and a describe run in the Store's Local Activity. diff --git a/tests/nexus/test_stream_direct.py b/tests/nexus/test_stream_direct.py index d5abd8427f..2233b27139 100644 --- a/tests/nexus/test_stream_direct.py +++ b/tests/nexus/test_stream_direct.py @@ -2380,6 +2380,10 @@ def values(name: str) -> list[Any]: assert values("temporal_stream_append_failed") == [] assert values("temporal_stream_close_failed") == [] for update in updates: + # The Stream runtime counts the reads that wait on the Store, with the default + # attributes of the meter and no more. + if update.metric.name == "temporal_stream_blocking_reads_waiting": + continue assert update.attributes["stream_store"] == "metered" assert update.attributes["workflow_type"] == "ReadAppendCloseWorkflow" assert update.attributes["task_queue"] == worker.task_queue From 340a1a78632dda93e9585944612d6233920d10f1 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Wed, 18 Nov 2026 11:27:33 -0700 Subject: [PATCH 33/35] Checked a Workflow batch against the limits of its Store. --- temporalio/bridge/sdk-core | 2 +- temporalio/bridge/src/lib.rs | 11 ++++++- temporalio/bridge/src/streams.rs | 22 +++++++++++-- temporalio/bridge/streams.py | 11 +++++-- temporalio/client/_stream_store.py | 3 +- temporalio/worker/_stream_store.py | 2 +- tests/helpers/streams.py | 11 +++++-- tests/nexus/test_stream_direct.py | 50 ++++++++++++++++++++++++++++++ 8 files changed, 102 insertions(+), 10 deletions(-) diff --git a/temporalio/bridge/sdk-core b/temporalio/bridge/sdk-core index 55c1b6cf3f..5d84833a12 160000 --- a/temporalio/bridge/sdk-core +++ b/temporalio/bridge/sdk-core @@ -1 +1 @@ -Subproject commit 55c1b6cf3f9d3fde109fbe6ebb62ba0d4db909ef +Subproject commit 5d84833a12a13cd74866331f32b8d3d16ef2bb00 diff --git a/temporalio/bridge/src/lib.rs b/temporalio/bridge/src/lib.rs index 3de2b73069..d12d2e769b 100644 --- a/temporalio/bridge/src/lib.rs +++ b/temporalio/bridge/src/lib.rs @@ -113,8 +113,17 @@ fn raise_in_thread(py: Python, thread_id: std::os::raw::c_long, exc: &Bound<'_, fn new_python_stream_runtime( runtime_ref: &runtime::RuntimeRef, caller: Py, + max_record_bytes: usize, + max_batch_bytes: usize, + max_batch_records: usize, ) -> PyResult { - streams::new_python_stream_runtime(runtime_ref, caller) + streams::new_python_stream_runtime( + runtime_ref, + caller, + max_record_bytes, + max_batch_bytes, + max_batch_records, + ) } #[pyfunction] diff --git a/temporalio/bridge/src/streams.rs b/temporalio/bridge/src/streams.rs index 2625d40a45..c272633017 100644 --- a/temporalio/bridge/src/streams.rs +++ b/temporalio/bridge/src/streams.rs @@ -1,6 +1,8 @@ //! The adapter that lets Core call a Stream Store written in Python for Workflow code. +use pyo3::exceptions::PyValueError; use pyo3::prelude::*; +use temporalio_streams::record::BatchLimits; use crate::runtime; @@ -12,15 +14,24 @@ pub struct StreamRuntimeRef { /// Builds the runtime on a Store written in Python. Core calls the Store through it for Workflow /// code, and the runtime checks each request and answer. `caller` runs each call of the Store on -/// the Worker's event loop, never on a Workflow thread. +/// the Worker's event loop, never on a Workflow thread. The three numbers are the batch limits of +/// the Store, and the runtime refuses a batch over them before the Store sees it. pub fn new_python_stream_runtime( runtime_ref: &runtime::RuntimeRef, caller: Py, + max_record_bytes: usize, + max_batch_bytes: usize, + max_batch_records: usize, ) -> PyResult { let runtime = &runtime_ref.runtime; runtime.assert_same_process("create a Stream runtime")?; + let limits = BatchLimits::default() + .with_max_record_bytes(max_record_bytes) + .and_then(|limits| limits.with_max_batch_bytes(max_batch_bytes)) + .and_then(|limits| limits.with_max_batch_records(max_batch_records)) + .map_err(|err| PyValueError::new_err(err.message))?; let meter = runtime.core.telemetry().get_temporal_metric_meter(); - let store = std::sync::Arc::new(python_store::PythonStore { caller }); + let store = std::sync::Arc::new(python_store::PythonStore { caller, limits }); let streams = temporalio_streams::StreamRuntime::new(store, meter); Ok(StreamRuntimeRef { streams }) } @@ -38,6 +49,7 @@ mod python_store { CreateStreamRequest, CreateStreamResponse, DescribeStreamRequest, DescribeStreamResponse, ReadRecordsRequest, ReadRecordsResponse, }, + record::BatchLimits, Reason, StreamError, StreamResult, StreamStore, }; @@ -52,6 +64,8 @@ mod python_store { pub(crate) struct PythonStore { /// The Python object that starts a call on the event loop and gives a canceller. pub(crate) caller: Py, + /// The batch limits of the Store, which the runtime reads once. + pub(crate) limits: BatchLimits, } /// What Python calls with the answer of one call. @@ -179,5 +193,9 @@ mod python_store { ) -> StreamResult { self.call("DescribeStream", request).await } + + fn limits(&self) -> BatchLimits { + self.limits + } } } diff --git a/temporalio/bridge/streams.py b/temporalio/bridge/streams.py index 7e43276815..ca6a4aa204 100644 --- a/temporalio/bridge/streams.py +++ b/temporalio/bridge/streams.py @@ -9,6 +9,7 @@ import temporalio.bridge.runtime import temporalio.bridge.temporal_sdk_bridge +import temporalio.client class StreamRuntime: @@ -19,14 +20,20 @@ def new_python( runtime: temporalio.bridge.runtime.Runtime, *, caller: Any, + limits: temporalio.client.StreamBatchLimits, ) -> StreamRuntime: """Build the runtime on a Store written in Python, which Core calls for Workflow code. - ``caller`` runs each call of the Store on the Worker's event loop. + ``caller`` runs each call of the Store on the Worker's event loop. Core refuses a + batch over ``limits`` before the Store sees it. """ return StreamRuntime( temporalio.bridge.temporal_sdk_bridge.new_python_stream_runtime( - runtime._ref, caller + runtime._ref, + caller, + limits.max_record_bytes, + limits.max_batch_bytes, + limits.max_batch_records, ) ) diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index 2d264e6b2d..7c04085ce9 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -196,7 +196,8 @@ def limits(self) -> StreamBatchLimits: """The limits of one batch on this Store. The default is the largest batch that any Store may take. A Store that allows less - overrides this property, and refuses a larger batch itself. + overrides this property, and refuses a larger batch itself. Core refuses a larger + batch of Workflow code before the Store sees it. """ return StreamBatchLimits() diff --git a/temporalio/worker/_stream_store.py b/temporalio/worker/_stream_store.py index 35c6839142..9c14707b65 100644 --- a/temporalio/worker/_stream_store.py +++ b/temporalio/worker/_stream_store.py @@ -510,7 +510,7 @@ def _core_stream_runtimes( # pyright: ignore[reportUnusedFunction] for store in config.stores: caller = _CoreStoreCaller(store) stream_runtime = temporalio.bridge.streams.StreamRuntime.new_python( - runtime._core_runtime, caller=caller + runtime._core_runtime, caller=caller, limits=store.limits ) runtimes.append((store.name, stream_runtime._ref)) callers.append(caller) diff --git a/tests/helpers/streams.py b/tests/helpers/streams.py index 2cacb22772..ba0593bed6 100644 --- a/tests/helpers/streams.py +++ b/tests/helpers/streams.py @@ -33,7 +33,7 @@ ReadRecordsRequest, ReadRecordsResponse, ) -from temporalio.client import StreamStore +from temporalio.client import StreamBatchLimits, StreamStore from temporalio.exceptions import StreamError _RequestT = TypeVar("_RequestT", bound=Message) @@ -152,8 +152,11 @@ class DictStreamStore(StreamStore): :py:attr:`waiting` counts the reads that wait, so a test sees one that stays. """ - def __init__(self, name: str = "dict") -> None: + def __init__( + self, name: str = "dict", *, limits: StreamBatchLimits | None = None + ) -> None: self._name = name + self._limits = limits if limits is not None else StreamBatchLimits() self.streams: dict[str, _DictStream] = {} self.waiting = 0 @@ -161,6 +164,10 @@ def __init__(self, name: str = "dict") -> None: def name(self) -> str: return self._name + @property + def limits(self) -> StreamBatchLimits: + return self._limits + def _stream(self, ref: StreamRef) -> _DictStream: stream = self.streams.get(ref.id) if stream is None: diff --git a/tests/nexus/test_stream_direct.py b/tests/nexus/test_stream_direct.py index 2233b27139..34497767cb 100644 --- a/tests/nexus/test_stream_direct.py +++ b/tests/nexus/test_stream_direct.py @@ -27,6 +27,7 @@ ) from temporalio.client import ( Client, + StreamBatchLimits, StreamConfig, StreamReplaySource, WorkflowFailureError, @@ -1659,6 +1660,55 @@ async def test_python_store_outage_is_tried_again(client: Client): ] +class CountingAppendStore(DictStreamStore): + """A Store that allows at most 2 Records in a batch and keeps the size of each append.""" + + def __init__(self) -> None: + super().__init__(limits=StreamBatchLimits(max_batch_records=2)) + self.batches: list[int] = [] + + async def append_records( + self, request: AppendRecordsRequest + ) -> AppendRecordsResponse: + self.batches.append(len(request.records)) + return await super().append_records(request) + + +@workflow.defn +class BatchOverTheLimitWorkflow: + @workflow.run + async def run(self, ref: StreamRef[str]) -> str: + stream = workflow.get_stream(ref, body_type=str) + try: + await stream.append("a", "b", "c") + reason = "appended" + except StreamError as err: + reason = StreamErrorReason.Name(err.reason) + await stream.append("d", "e") + return reason + + +async def test_a_workflow_batch_over_the_limits_of_its_store_is_refused( + client: Client, +): + store = CountingAppendStore() + stream_client = _stream_client(client, store) + ref = await _filled_stream(stream_client, [], close=False) + handle, reason = await _run(stream_client, BatchOverTheLimitWorkflow, ref) + assert reason == "STREAM_ERROR_REASON_TOO_LARGE" + # Core refused the batch of 3 before the Store saw it, and the batch of 2 went through. + assert store.batches == [2] + assert [record.body for record in await _read_all(stream_client, ref)] == [ + "d", + "e", + ] + await Replayer( + workflows=[BatchOverTheLimitWorkflow], + stream_config=stream_client.config()["stream_config"], + ).replay_workflow(await handle.fetch_history()) + assert store.batches == [2] + + async def test_python_store_exception_is_a_stream_failure_and_the_worker_goes_on( client: Client, ): From 809c59881168a0aa31a0834318c4c96eabe00fa3 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Wed, 18 Nov 2026 17:03:30 -0700 Subject: [PATCH 34/35] Limited BufferedAppender to client and Activity code. Workflow code passes its Records to one `append` call, and a flush by time has no meaning there. --- changelog/added/patient-river-heron.md | 2 +- temporalio/contrib/streams/README.md | 16 +- temporalio/contrib/streams/__init__.py | 4 +- temporalio/contrib/streams/_appender.py | 60 ++---- .../contrib/streams/test_appender_workflow.py | 194 +----------------- 5 files changed, 36 insertions(+), 240 deletions(-) diff --git a/changelog/added/patient-river-heron.md b/changelog/added/patient-river-heron.md index f9747e768b..b24b71f251 100644 --- a/changelog/added/patient-river-heron.md +++ b/changelog/added/patient-river-heron.md @@ -8,4 +8,4 @@ Add `StreamConfig.workflow_read_page_records`, the most Records of one read that Add `StreamConfig.workflow_read_min_gap`, the least time between two batches of one read. It has no gap by default and must be at most 1 s. A gap trades latency for fewer activations of Workflow code. Add `temporalio.exceptions.StreamReplayError`. With it in `workflow_failure_exception_types`, a replay that can't read the Records of a read from the Store again fails the Workflow, and not only the Workflow Task. Core tries a read, an append or a close of Workflow code again while its Store is unavailable, from 100 ms up to 2 s apart, for at most 30 s. Only then does Workflow code get the retryable `StreamError`. A replay tries its Store for 5 s. If the Store stays down, only the Workflow Task fails, with `StreamReplayUnavailable`, also with `StreamReplayError` in `workflow_failure_exception_types`. -In Workflow code, `temporalio.contrib.streams.current_producer` gives the Workflow's Producer, and `BufferedAppender` writes the appends of one activation as one append, with no timer. +In Workflow code, `temporalio.contrib.streams.current_producer` gives the Workflow's Producer. `BufferedAppender` is for client and Activity code, and it raises `RuntimeError` in Workflow code. diff --git a/temporalio/contrib/streams/README.md b/temporalio/contrib/streams/README.md index 8e34164fc4..315b694fb0 100644 --- a/temporalio/contrib/streams/README.md +++ b/temporalio/contrib/streams/README.md @@ -2,15 +2,13 @@ > This package is experimental. Its API can change in a later release. -`temporalio.contrib.streams` holds helpers that sit on top of the Streams API of the SDK. A -helper takes a client `Stream` or a Workflow `Stream`. +`temporalio.contrib.streams` holds helpers that sit on top of the Streams API of the SDK. ## `BufferedAppender` `BufferedAppender` batches the appends of one Stream. `append` returns immediately, and the -appender writes the buffered Records as one batch. In a client or an Activity a batch waits -at most `max_delay`. In Workflow code a batch starts when the code next yields, so it adds no -timer to History. +appender writes the buffered Records as one batch. A batch waits at most `max_delay`. The +appender is for client and Activity code. Workflow code passes its Records to one `append` call. ```python from temporalio.contrib.streams import BufferedAppender @@ -45,10 +43,10 @@ replacement of the first attempt's Records. ## Reads and appends in Workflow code -The helpers run on the Streams API, so they follow how Workflow code reads, appends and -closes. On every Store, such as the Redis Store, Core reads, appends and closes a Stream -itself, with no Activity. Workflow code reads with the same `read`, `read_pages` and -`read_page` as a client. It gets the same pages, but each page has at most 256 Records. +This section says how Workflow code reads, appends and closes a Stream. On every Store, such +as the Redis Store, Core reads, appends and closes a Stream itself, with no Activity. Workflow +code reads with the same `read`, `read_pages` and `read_page` as a client. It gets the same +pages, but each page has at most 256 Records. Here the Workflow reads the events of a model's answer while the model Activity still runs, and starts each tool as its call arrives. diff --git a/temporalio/contrib/streams/__init__.py b/temporalio/contrib/streams/__init__.py index ac9a7c6392..547a5c4f43 100644 --- a/temporalio/contrib/streams/__init__.py +++ b/temporalio/contrib/streams/__init__.py @@ -1,7 +1,7 @@ """Helpers that sit on top of the Streams API. -:py:class:`BufferedAppender` batches the appends of one Stream. It takes a client Stream or a -Workflow Stream. :py:func:`current_producer` gives the Producer that an append of the +:py:class:`BufferedAppender` batches the appends of one Stream. It is for client and Activity +code. :py:func:`current_producer` gives the Producer that an append of the calling code sends by default, and :py:func:`as_producer` gives back a Producer that code stored. diff --git a/temporalio/contrib/streams/_appender.py b/temporalio/contrib/streams/_appender.py index 75a6213e05..1c49172556 100644 --- a/temporalio/contrib/streams/_appender.py +++ b/temporalio/contrib/streams/_appender.py @@ -9,18 +9,13 @@ import asyncio from datetime import timedelta from types import TracebackType -from typing import TYPE_CHECKING, Generic, TypeVar +from typing import Generic, TypeVar from typing_extensions import Self import temporalio.nexus.system.stream_service.client import temporalio.workflow -if TYPE_CHECKING: - # The Workflow Stream class is in a private module of the generated code. Only its type - # is needed, since the appender calls the same `append` on both Streams. - import temporalio.nexus.system.stream_service.workflow._resources.stream - BodyT = TypeVar("BodyT") # The Store refuses an append of more Records than this. Many small Records fit under its byte @@ -38,20 +33,13 @@ class BufferedAppender(Generic[BodyT]): Records appended during a write wait for the next batch, and they keep their order. A batch holds at most ``max_batch_records`` Records. - It takes a client Stream or a Workflow Stream. When a write starts depends on where - :py:meth:`append` runs, not on the type of the Stream: - - * In a client or an Activity, the appender writes the buffered Records at most - ``max_delay`` after the first of them, or immediately when they fill a batch. - * In Workflow code, a timer would go into History, so the appender uses none. The first - write starts when the calling code next yields, so the appends of one activation go as - one write. Each write is the Workflow ``append`` of the Stream, which Core makes inside - the Workflow Task with no Activity. A replay takes the results from History and writes - nothing. + The appender writes the buffered Records at most ``max_delay`` after the first of them, or + immediately when they fill a batch. It is for client and Activity code. Workflow code can't + use it, and passes its Records to one ``append`` call of the Stream. Use it in ``async with``, which writes the remaining Records at the end of the block, or await :py:meth:`flush`. Without ``async with`` or a :py:meth:`flush`, the Records still in - the buffer are lost. The end of an Activity attempt or of a Workflow doesn't write them. + the buffer are lost. The end of an Activity attempt doesn't write them. ``await flush()`` is the backpressure point. A caller that appends faster than the Store accepts Records must await it from time to time, so the buffer doesn't grow without bound. @@ -68,10 +56,7 @@ class BufferedAppender(Generic[BodyT]): def __init__( self, - stream: temporalio.nexus.system.stream_service.client.Stream[BodyT] - | temporalio.nexus.system.stream_service.workflow._resources.stream.Stream[ - BodyT - ], + stream: temporalio.nexus.system.stream_service.client.Stream[BodyT], *, max_delay: timedelta | None = None, max_batch_records: int | None = None, @@ -79,19 +64,24 @@ def __init__( """Create an appender. It starts no write until the first append. Args: - stream: The Stream to append to, a client Stream or a Workflow Stream. + stream: The Stream to append to, from a client or from the client of an Activity. max_delay: The longest time that a Record waits in the buffer before its batch - starts to write, in a client or an Activity. It must not be negative. - ``None`` means 100 milliseconds. Workflow code must leave it ``None``. + starts to write. It must not be negative. ``None`` means 100 milliseconds. max_batch_records: The most Records of one batch, from 1 to 1,000. Set it to the ``max_batch_records`` of the Store's :py:attr:`temporalio.client.StreamStore.limits` when the Store allows less. ``None`` means 1,000. Raises: - ValueError: ``max_delay`` is negative, ``max_delay`` is set in Workflow code, or - ``max_batch_records`` is below 1 or above 1,000. + RuntimeError: The appender is built in Workflow code. + ValueError: ``max_delay`` is negative, or ``max_batch_records`` is below 1 or + above 1,000. """ + if temporalio.workflow.in_workflow(): + raise RuntimeError( + "BufferedAppender is for client and Activity code. In Workflow code, pass " + "the Records to one append call." + ) if max_delay is not None and max_delay < timedelta(): raise ValueError("max_delay must not be negative") if max_batch_records is None: @@ -104,11 +94,10 @@ def __init__( self._stream = stream self._max_delay = max_delay self._max_batch_records = max_batch_records - self._check_delay() self._buffer: list[BodyT] = [] # When the oldest buffered Record arrived, so its batch starts at most max_delay later. self._first_at = 0.0 - # The timer of a client or an Activity, or the start at the next yield of Workflow code. + # The timer that starts the next batch. self._timer: asyncio.Handle | None = None self._writing: asyncio.Task[None] | None = None self._appended = 0 @@ -131,7 +120,6 @@ def append(self, *records: BodyT) -> None: self._raise_error() if not records: raise TypeError("append takes one or more values") - self._check_delay() loop = asyncio.get_running_loop() if not self._buffer: self._first_at = loop.time() @@ -185,22 +173,10 @@ def _raise_error(self) -> None: if self._error is not None: raise self._error - def _check_delay(self) -> None: - if self._max_delay is not None and temporalio.workflow.in_workflow(): - raise ValueError( - "BufferedAppender takes no max_delay in Workflow code, since a delay " - "would put a timer into History. Leave max_delay as None." - ) - def _schedule(self) -> None: if self._writing is not None or not self._buffer: return - if temporalio.workflow.in_workflow(): - # A timer of the Workflow event loop is durable. A start at the next yield - # takes every Record of this activation into one write, and adds no event. - if self._timer is None: - self._timer = asyncio.get_running_loop().call_soon(self._start) - elif len(self._buffer) >= self._max_batch_records: + if len(self._buffer) >= self._max_batch_records: self._start() elif self._timer is None: max_delay = self._max_delay or _DEFAULT_MAX_DELAY diff --git a/tests/contrib/streams/test_appender_workflow.py b/tests/contrib/streams/test_appender_workflow.py index 7766f8cd3e..f7cc6f1ce1 100644 --- a/tests/contrib/streams/test_appender_workflow.py +++ b/tests/contrib/streams/test_appender_workflow.py @@ -1,96 +1,27 @@ from __future__ import annotations -import asyncio import uuid from datetime import timedelta from typing import Any -import pytest - from temporalio import workflow -from temporalio.api.enums.v1 import EventType, StreamErrorReason -from temporalio.api.history.v1 import HistoryEvent -from temporalio.bridge.proto.external_data import StreamAppendEntry, StreamMarkerData from temporalio.client import Client, WorkflowHandle from temporalio.contrib.streams import BufferedAppender -from temporalio.exceptions import StreamError from temporalio.nexus.system.stream_service import StreamRef -from temporalio.nexus.system.stream_service.workflow import create_stream, get_stream -from temporalio.worker import Replayer +from temporalio.nexus.system.stream_service.workflow import get_stream from tests.helpers import new_worker from tests.helpers.streams import DictStreamStore from tests.nexus.test_stream_store import _with_store -_BURST = [f"record-{index}" for index in range(40)] - - -async def _yield_a_few_times() -> None: - # A turn of the loop with no timer, so the write starts and waits for Core. - for _ in range(5): - await asyncio.sleep(0) - @workflow.defn -class BurstWorkflow: - @workflow.run - async def run(self, stream_id: str) -> str: - stream = await create_stream(id=stream_id, body_type=str) - appender = BufferedAppender(stream) - for value in _BURST: - appender.append(value) - last_offset = await appender.flush() - assert last_offset is not None - return last_offset.hex() - - -@workflow.defn -class InFlightWorkflow: - @workflow.run - async def run(self, stream_id: str) -> None: - stream = await create_stream(id=stream_id, body_type=str) - async with BufferedAppender(stream) as appender: - appender.append("a") - await _yield_a_few_times() - # The write of "a" waits for Core, so these go into the next one. - appender.append("b") - appender.append("c", "d") - - -@workflow.defn -class FailedAppendWorkflow: - @workflow.run - async def run(self, stream_id: str) -> list[Any]: - appender = BufferedAppender(get_stream(StreamRef(id=stream_id), body_type=str)) - appender.append("a") - await _yield_a_few_times() - appender.append("b", "c") - try: - await appender.flush() - except StreamError as err: - first = err - else: - return ["flushed"] - try: - appender.append("d") - except StreamError as err: - later = err - else: - return ["appended"] - return [ - StreamErrorReason.Name(first.reason), - later is first, - appender.unwritten, - ] - - -@workflow.defn -class MaxDelayWorkflow: +class AppenderWorkflow: @workflow.run async def run(self, stream_id: str) -> str: stream = get_stream(StreamRef(id=stream_id), body_type=str) try: - BufferedAppender(stream, max_delay=timedelta(milliseconds=10)) - except ValueError as err: + BufferedAppender(stream) # type: ignore[arg-type] + except RuntimeError as err: return str(err) return "built" @@ -109,122 +40,13 @@ async def _run( return handle, await handle.result() -def _of_type(events: list[HistoryEvent], event_type: EventType.ValueType) -> int: - return sum(1 for event in events if event.event_type == event_type) - - -def _append_entries(events: list[HistoryEvent]) -> list[StreamAppendEntry]: - # Each append that Core makes is one entry in the Stream marker of its Workflow Task. - entries: list[StreamAppendEntry] = [] - for event in events: - attributes = event.marker_recorded_event_attributes - if ( - event.event_type != EventType.EVENT_TYPE_MARKER_RECORDED - or attributes.marker_name != "core_stream" - ): - continue - [payload] = attributes.details["data"].payloads - entries.extend(StreamMarkerData.FromString(payload.data).appends) - return entries - - -async def _batches( - client: Client, stream_id: str, events: list[HistoryEvent] -) -> list[int]: - """Give the number of Records of each append, from its offsets in the marker.""" - stream = client.get_stream(StreamRef(id=stream_id), body_type=str) - offsets = [record.offset async for record in stream.read()] - return [ - sum( - 1 for offset in offsets if entry.first_offset <= offset <= entry.last_offset - ) - for entry in _append_entries(events) - ] - - -def _marker_names(events: list[HistoryEvent]) -> list[str]: - return [ - event.marker_recorded_event_attributes.marker_name - for event in events - if event.event_type == EventType.EVENT_TYPE_MARKER_RECORDED - ] - - -async def _bodies(client: Client, stream_id: str) -> list[str]: - stream = client.get_stream(StreamRef(id=stream_id), body_type=str) - return [record.body async for record in stream.read()] - - -async def test_appends_of_one_activation_are_one_append(client: Client): - stream_client = _with_store(client, DictStreamStore()) - stream_id = f"stream-{uuid.uuid4()}" - handle, last_offset = await _run(stream_client, BurstWorkflow, stream_id) - - events = list((await handle.fetch_history()).events) - assert await _batches(stream_client, stream_id, events) == [40] - assert await _bodies(stream_client, stream_id) == _BURST - described = await stream_client.get_stream( - StreamRef(id=stream_id), body_type=str - ).describe() - assert described.last_offset is not None - assert last_offset == described.last_offset.hex() - # The Local Activity of the create, and the Stream marker of the append. - assert _marker_names(events) == ["core_local_activity", "core_stream"] - assert _of_type(events, EventType.EVENT_TYPE_TIMER_STARTED) == 0 - - -async def test_appends_during_a_workflow_append_join_the_next_one(client: Client): - stream_client = _with_store(client, DictStreamStore()) - stream_id = f"stream-{uuid.uuid4()}" - handle, _ = await _run(stream_client, InFlightWorkflow, stream_id) - - events = list((await handle.fetch_history()).events) - assert await _batches(stream_client, stream_id, events) == [1, 3] - assert await _bodies(stream_client, stream_id) == ["a", "b", "c", "d"] - assert _of_type(events, EventType.EVENT_TYPE_TIMER_STARTED) == 0 - - -async def test_a_failed_workflow_append_stops_the_appender(client: Client): - handle, result = await _run( - _with_store(client, DictStreamStore()), - FailedAppendWorkflow, - f"stream-{uuid.uuid4()}", - ) - assert result == ["STREAM_ERROR_REASON_NOT_FOUND", True, 3] - # Only the first batch went to the Store, and it failed. - [entry] = _append_entries(list((await handle.fetch_history()).events)) - assert entry.HasField("failure") - - -async def test_replay_of_workflow_appender_writes_nothing(client: Client): - stream_client = _with_store(client, DictStreamStore()) - stream_id = f"stream-{uuid.uuid4()}" - handle, _ = await _run(stream_client, InFlightWorkflow, stream_id) - - # A replay of an append that Core made needs a Store, as the first run had. A new - # Store shows any write, since it has no Stream. - replay_store = DictStreamStore() - await Replayer( - workflows=[InFlightWorkflow], - stream_config=_with_store(client, replay_store).config()["stream_config"], - ).replay_workflow(await handle.fetch_history()) - with pytest.raises(StreamError) as err: - await ( - _with_store(client, replay_store) - .get_stream(StreamRef(id=stream_id), body_type=str) - .describe() - ) - assert err.value.reason == StreamErrorReason.STREAM_ERROR_REASON_NOT_FOUND - assert await _bodies(stream_client, stream_id) == ["a", "b", "c", "d"] - - -async def test_workflow_appender_refuses_a_max_delay(client: Client): +async def test_workflow_code_cannot_build_an_appender(client: Client): _, message = await _run( _with_store(client, DictStreamStore()), - MaxDelayWorkflow, + AppenderWorkflow, f"stream-{uuid.uuid4()}", ) assert message == ( - "BufferedAppender takes no max_delay in Workflow code, since a delay would put " - "a timer into History. Leave max_delay as None." + "BufferedAppender is for client and Activity code. In Workflow code, pass the " + "Records to one append call." ) From 46fa50222067c57e33b959e778b0a0935785a6b5 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Wed, 18 Nov 2026 17:15:38 -0700 Subject: [PATCH 35/35] Made the fence a choice of the Store in the Store rules. A Store doesn't have to refuse a lower epoch of a Producer. Each Record has what a reader needs to tell the attempts apart. --- changelog/added/patient-river-heron.md | 2 +- temporalio/client/_stream_store.py | 11 ++++++----- temporalio/contrib/streams/README.md | 5 +++-- temporalio/contrib/streams/__init__.py | 5 ++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/changelog/added/patient-river-heron.md b/changelog/added/patient-river-heron.md index b24b71f251..81f5177f33 100644 --- a/changelog/added/patient-river-heron.md +++ b/changelog/added/patient-river-heron.md @@ -1,4 +1,4 @@ -Workflow code creates a Stream, appends Records to it, reads it, describes it and closes it, with `workflow.create_stream` and `workflow.get_stream`. A create and a describe run in the Store's Local Activity, with the options of `StreamConfig.workflow_call_options`. Core makes a read, an append and a close itself, with no Activity. It calls the Store as a task on the Worker's event loop, never on a Workflow thread. For Workflow code, a Store waits up to a read's `wait`, ends that wait on a cancel, gives offsets of 1 to 1024 bytes that compare in order, fences a lower epoch of a Producer, writes a resent batch once, and raises `StreamError` with a reason. Core tries a call again for up to 30 s only on `STREAM_ERROR_REASON_STORAGE_UNAVAILABLE`. Any other exception reaches Workflow code on the first try, as a non-retryable `STREAM_ERROR_REASON_STORAGE_CORRUPT` with its type in the message. `Replayer` takes the `stream_config` of the Worker's client. +Workflow code creates a Stream, appends Records to it, reads it, describes it and closes it, with `workflow.create_stream` and `workflow.get_stream`. A create and a describe run in the Store's Local Activity, with the options of `StreamConfig.workflow_call_options`. Core makes a read, an append and a close itself, with no Activity. It calls the Store as a task on the Worker's event loop, never on a Workflow thread. For Workflow code, a Store waits up to a read's `wait`, ends that wait on a cancel, gives offsets of 1 to 1024 bytes that compare in order, writes a resent batch once, and raises `StreamError` with a reason. A Store can refuse a lower epoch of a Producer with `STREAM_ERROR_REASON_FENCED`, and it doesn't have to. Core tries a call again for up to 30 s only on `STREAM_ERROR_REASON_STORAGE_UNAVAILABLE`. Any other exception reaches Workflow code on the first try, as a non-retryable `STREAM_ERROR_REASON_STORAGE_CORRUPT` with its type in the message. `Replayer` takes the `stream_config` of the Worker's client. The Workflow `Stream` has `read`, `read_pages` and `read_page`, so Workflow code reads a Stream with the same calls as a client. Core reads the Store inside the Workflow Task. A page that Workflow code gets in one activation has at most 256 Records, and the rest come in the next activation with no wait. A loop that asks for its next page in the same activation keeps one read open, and a read that no code waits on closes before the activation completes, so it never holds a Workflow Task open. With a `wait`, a quiet Stream gives empty pages, so a loop on a Stream that can be quiet for long uses `read_pages` and leaves after an empty page. A call that waits on a quiet Stream holds its Workflow Task open, with one Workflow Task about every 8 s. The calls of one Workflow Task write one marker in History. `StreamConfig.workflow_read_replay_from` decides whether the marker holds the Records (`StreamReplaySource.HISTORY`) or only their offsets and a digest, which a replay reads from the Store again (`StreamReplaySource.STORE`, the default). With `HISTORY`, one Workflow Task's marker holds at most 1 MiB of Records, and the read goes on in the next task. A Workflow's append runs in Core inside the Workflow Task, with no Activity. It writes as the Workflow, with its Workflow Task as the epoch. A `producer=` of another kind raises `StreamError` with the reason `STREAM_ERROR_REASON_INVALID`. A Workflow's close runs in Core, with no Activity, and spans two Workflow Tasks. The task that calls `close` records the intent, Core closes the Stream after the server took that task, and the next task gives the result, so a failed task closes nothing. Await the close. A graceful shutdown of the Worker waits at most 10 s for a close that the code doesn't await, and a Worker that dies when the Workflow ends loses it. An append and a close of Workflow code always carry the Workflow Task as their epoch. An append is at-least-once. After a Worker crash or an eviction, the rerun of its task can write a second copy with the epoch of the new task. diff --git a/temporalio/client/_stream_store.py b/temporalio/client/_stream_store.py index 7c04085ce9..d7db546af1 100644 --- a/temporalio/client/_stream_store.py +++ b/temporalio/client/_stream_store.py @@ -166,9 +166,10 @@ class StreamStore(ABC): order as bytes. A page that gives the last Record of a closed Stream is ``done``. * :py:meth:`append_records` gives the offsets of the first and the last Record. A Workflow appends as a Producer of kind Workflow, with its Workflow Task as the - epoch. The Store refuses a batch of a lower epoch of that Producer with the reason - ``STREAM_ERROR_REASON_FENCED``. It writes a batch that comes again with the same - Producer, epoch and ``sequence`` once, since Core sends it again after an outage. + epoch. The Store writes a batch that comes again with the same Producer, epoch and + ``sequence`` once, since Core sends it again after an outage. A Store can refuse a + batch of a lower epoch of that Producer with the reason + ``STREAM_ERROR_REASON_FENCED``. It doesn't have to. * :py:meth:`close_stream` of a closed Stream succeeds, since Core may send a close again. * A replay of a read under :py:attr:`StreamReplaySource.STORE` reads the Records @@ -418,8 +419,8 @@ class StreamConfig: such a call raises :py:class:`temporalio.exceptions.StreamConfigError`. An append or a close of Workflow code writes as the Workflow, with the Workflow Task - that runs it as its epoch. So the Store fences a late write of a failed Workflow - Task. A ``producer=`` of another kind raises + that runs it as its epoch. So a Store that fences refuses a late write of a failed + Workflow Task. A ``producer=`` of another kind raises :py:class:`temporalio.exceptions.StreamError` with the reason ``STREAM_ERROR_REASON_INVALID``. Core keeps the appends and the close of one Workflow on one Stream in the order of the calls. diff --git a/temporalio/contrib/streams/README.md b/temporalio/contrib/streams/README.md index 315b694fb0..1478e8f78c 100644 --- a/temporalio/contrib/streams/README.md +++ b/temporalio/contrib/streams/README.md @@ -159,8 +159,9 @@ never on a Workflow thread. For Workflow code, a Store must do these things. read that it stops. - Each Record has an offset of 1 to 1024 bytes, and offsets compare in order as bytes. A page that gives the last Record of a closed Stream is `done`. -- `append_records` refuses a lower epoch of a Producer with `STREAM_ERROR_REASON_FENCED`, and - writes a batch that comes again with the same Producer, epoch and `sequence` once. +- `append_records` writes a batch that comes again with the same Producer, epoch and `sequence` + once. It can refuse a lower epoch of a Producer with `STREAM_ERROR_REASON_FENCED`, and it + doesn't have to. - `close_stream` of a closed Stream succeeds. - The Store keeps the Records with their offsets for as long as a replay reads them. - An outage raises `StreamError` with `STREAM_ERROR_REASON_STORAGE_UNAVAILABLE`, and Core tries diff --git a/temporalio/contrib/streams/__init__.py b/temporalio/contrib/streams/__init__.py index 547a5c4f43..b3680baa79 100644 --- a/temporalio/contrib/streams/__init__.py +++ b/temporalio/contrib/streams/__init__.py @@ -1,9 +1,8 @@ """Helpers that sit on top of the Streams API. :py:class:`BufferedAppender` batches the appends of one Stream. It is for client and Activity -code. :py:func:`current_producer` gives the Producer that an append of the -calling code sends by default, and :py:func:`as_producer` gives back a Producer that code -stored. +code. :py:func:`current_producer` gives the Producer that an append of the calling code sends by +default, and :py:func:`as_producer` gives back a Producer that code stored. .. warning:: This package is experimental and may change in future versions.