Skip to content

Commit fc6b8d3

Browse files
committed
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.
1 parent 7841bf0 commit fc6b8d3

5 files changed

Lines changed: 306 additions & 62 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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.
1+
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.

‎temporalio/client/_stream_store.py‎

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929

3030
@dataclasses.dataclass(frozen=True)
3131
class StreamStoreCallOptions:
32-
"""Options of the Local Activity that calls a Stream Store for Workflow code.
32+
"""Options of the Activity that calls a Stream Store for Workflow code.
3333
3434
Workflow code can't do I/O, so the default
35-
:py:meth:`StreamStore.call_from_workflow` calls its Store from a Local Activity.
35+
:py:meth:`StreamStore.call_from_workflow` calls its Store from an Activity. A close
36+
runs it as a regular Activity, and every other operation runs it as a Local
37+
Activity. Both get these options.
3638
3739
A :py:class:`StreamConfig` holds them in
3840
:py:attr:`StreamConfig.workflow_call_options`, and every Store of that config
@@ -137,8 +139,9 @@ def worker_activities(self) -> Sequence[Callable[..., Any]]:
137139
"""The Activities that a Worker registers for this Store when it runs Workflows.
138140
139141
The default is one Activity, named for this Store, that
140-
:py:meth:`call_from_workflow` runs as a Local Activity. A Store that overrides
141-
one of the two methods overrides the other to match.
142+
:py:meth:`call_from_workflow` runs as a Local Activity, or as a regular Activity
143+
for a close. A Store that overrides one of the two methods overrides the other to
144+
match.
142145
"""
143146
return [_store_call_activity(self)]
144147

@@ -151,13 +154,15 @@ async def call_from_workflow(
151154
"""Answer one Stream operation for Workflow code.
152155
153156
It runs in Workflow code, so it does no I/O itself. The default runs the
154-
Activity of :py:meth:`worker_activities` as a Local Activity, with the timeouts
155-
and the retry policy of ``options``. A replay takes the result from History and
156-
calls no method of this Store.
157+
Activity of :py:meth:`worker_activities` with the timeouts and the retry policy
158+
of ``options``. A close runs it as a regular Activity on the Workflow's Task
159+
Queue, after the Workflow Task completes. Every other operation runs it as a
160+
Local Activity. A replay takes the result from History and calls no method of
161+
this Store.
157162
158163
Args:
159-
operation: The Nexus operation name, ``CreateStream``, ``AppendRecords``
160-
or ``DescribeStream`` in this release.
164+
operation: The Nexus operation name, ``CreateStream``, ``AppendRecords``,
165+
``CloseStream`` or ``DescribeStream`` in this release.
161166
request: The request message of ``temporalio.api.streamservice.v1``.
162167
options: The :py:attr:`StreamConfig.workflow_call_options` of the config.
163168
@@ -167,14 +172,29 @@ async def call_from_workflow(
167172
stream_operation = _STREAM_OPERATIONS[operation]
168173
# Workflow code runs no payload codec, so the Activity encodes the bodies of an
169174
# append before the Store gets them.
170-
response = await temporalio.workflow.execute_local_activity(
171-
_store_call_activity_name(self.name),
172-
args=[operation, request.SerializeToString()],
173-
result_type=bytes,
174-
schedule_to_close_timeout=options.schedule_to_close_timeout,
175-
start_to_close_timeout=options.start_to_close_timeout,
176-
retry_policy=options.retry_policy,
177-
)
175+
if operation == "CloseStream":
176+
# A Local Activity reaches the Store before its Workflow Task completes. If
177+
# that task then failed, the Stream would stay closed and the retry of the
178+
# task could not append. The server schedules a regular Activity only when
179+
# the task completes.
180+
response = await temporalio.workflow.execute_activity(
181+
_store_call_activity_name(self.name),
182+
args=[operation, request.SerializeToString()],
183+
result_type=bytes,
184+
task_queue=temporalio.workflow.info().task_queue,
185+
schedule_to_close_timeout=options.schedule_to_close_timeout,
186+
start_to_close_timeout=options.start_to_close_timeout,
187+
retry_policy=options.retry_policy,
188+
)
189+
else:
190+
response = await temporalio.workflow.execute_local_activity(
191+
_store_call_activity_name(self.name),
192+
args=[operation, request.SerializeToString()],
193+
result_type=bytes,
194+
schedule_to_close_timeout=options.schedule_to_close_timeout,
195+
start_to_close_timeout=options.start_to_close_timeout,
196+
retry_policy=options.retry_policy,
197+
)
178198
return stream_operation.response_type.FromString(response)
179199

180200

@@ -315,10 +335,6 @@ def check_response(
315335
"close_stream",
316336
temporalio.api.streamservice.v1.CloseStreamRequest,
317337
temporalio.api.streamservice.v1.CloseStreamResponse,
318-
workflow_refusal=(
319-
"Workflow code can't close a Stream on a Store in this release. "
320-
"Call an Activity that closes it through its client."
321-
),
322338
),
323339
"DescribeStream": _StreamOperation(
324340
"describe_stream",
@@ -411,6 +427,10 @@ class StreamConfig:
411427
Store of that name. A call whose ref names no Store, such as a create, goes to
412428
the one Store, or to the Store that :py:attr:`store_selector` returns.
413429
430+
A Worker with Workflows and this config polls for Activity Tasks, since a Workflow's
431+
close runs the Store's Activity as a regular Activity. With ``no_remote_activities``
432+
on that Worker, another Worker of the Task Queue must run the close.
433+
414434
.. warning::
415435
This API is experimental and subject to change.
416436
"""
@@ -431,8 +451,8 @@ class StreamConfig:
431451
``None`` and there is one Store, every new Stream goes to that Store.
432452
433453
For a call of Workflow code, the selector runs in Workflow code, also on a
434-
replay. So it must give the same Store for the same context. The Local Activity
435-
of the chosen Store is in History, and another answer on a replay is a
454+
replay. So it must give the same Store for the same context. The Activity of the
455+
chosen Store is in History, and another answer on a replay is a
436456
non-determinism error. A Stream that code builds from an id only goes through the
437457
selector on each call, so the selector must also give the same Store for the same
438458
id.
@@ -443,8 +463,9 @@ class StreamConfig:
443463
)
444464
"""Options of a Store's call for Workflow code, for every Store of this config.
445465
446-
:py:meth:`StreamStore.call_from_workflow` gets them. The default runs its Local
447-
Activity with their timeouts and retry policy.
466+
:py:meth:`StreamStore.call_from_workflow` gets them. The default runs the Store's
467+
Activity with their timeouts and retry policy, as a Local Activity, or as a regular
468+
Activity for a close.
448469
"""
449470

450471
_store_map: dict[str, StreamStore] = dataclasses.field(

‎temporalio/worker/_worker.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,14 +668,15 @@ def check_activity(activity: str):
668668
# are not running an activity worker at all. Otherwise shutdown
669669
# will not proceed properly.
670670
no_remote_activities=config.get("no_remote_activities")
671-
or not config.get("activities"),
671+
or self._activity_worker is None,
672672
task_types=temporalio.bridge.worker.WorkerTaskTypes(
673673
enable_workflows=self._workflow_worker is not None,
674674
enable_local_activities=self._activity_worker is not None
675675
and self._workflow_worker is not None,
676-
# The Activities of the Stream Stores run as Local Activities, so
677-
# alone they don't make this worker poll for Activity Tasks.
678-
enable_remote_activities=bool(config.get("activities"))
676+
# A Workflow's Stream close runs a Store's Activity as a regular
677+
# Activity, so the Activities of the Stream Stores alone make this
678+
# worker poll for Activity Tasks.
679+
enable_remote_activities=self._activity_worker is not None
679680
and not config.get("no_remote_activities"),
680681
enable_nexus=self._nexus_worker is not None,
681682
),

‎tests/contrib/redis/test_streams.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ async def run(self, stream_id: str) -> StreamRef[str]:
7474
return stream.ref
7575

7676

77+
@workflow.defn
78+
class AppendAndCloseWorkflow:
79+
@workflow.run
80+
async def run(self, stream_id: str) -> StreamRef[str]:
81+
stream = await create_stream(id=stream_id, body_type=str)
82+
await stream.append("a", "b")
83+
await stream.close()
84+
return stream.ref
85+
86+
7787
@activity.defn
7888
async def append_in_activity(stream_id: str) -> None:
7989
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(
140150
assert record.producer.kind.value.run_id == run_id
141151

142152

153+
async def test_workflow_appends_and_closes(client: Client):
154+
stream_client = _with_store(client, _store())
155+
async with new_worker(stream_client, AppendAndCloseWorkflow) as worker:
156+
handle = await stream_client.start_workflow(
157+
AppendAndCloseWorkflow.run,
158+
f"stream-{uuid.uuid4()}",
159+
id=f"workflow-{uuid.uuid4()}",
160+
task_queue=worker.task_queue,
161+
)
162+
ref = await handle.result()
163+
164+
stream = stream_client.get_stream(ref, body_type=str)
165+
records = [record async for record in stream.iter_read_items()]
166+
assert [record.body for record in records] == ["a", "b"]
167+
run_id = handle.result_run_id
168+
for record in records:
169+
assert record.producer is not None
170+
assert isinstance(record.producer.kind, ProducerKindLocalActivity)
171+
assert record.producer.kind.value.run_id == run_id
172+
described = await stream.describe()
173+
assert described.status == StreamStatus.StreamStatusClosed
174+
err = await _stream_error(stream.append("c"))
175+
assert err.reason == StreamErrorReason.STREAM_ERROR_REASON_CLOSED
176+
177+
143178
async def test_activity_appends_with_activity_producer(client: Client):
144179
stream_client = _with_store(client, _store())
145180
stream = await _create_stream(stream_client)

0 commit comments

Comments
 (0)