Skip to content

Commit 35b476d

Browse files
drewhoskins-temporaldrewhoskinsdandavison
authored
Workflow Update and Signal handlers concurrency sample (#123)
* Atomic message handlers sample * Remove resize jobs to reduce code size * Misc polish * Add test * Format code * Continue as new * Formatting * Feedback, readme, restructure files and directories * Format * More feedback. Add test-continue-as-new flag. * Feedback; throw ApplicationFailures from update handlers * Formatting * __init__.py * Fix lint issues * Dan Feedback * More typehints * s/atomic/safe/ * Fix and demo idempotency * Compatibility with 3.8 * More feedback * Re-add tests * Fix flaky test * Improve update and tests * list -> set to fix a test * Return a struct rather than a raw value from the list for better hygiene * Remove test dependency on race conditions between health check and adding nodes. * Ruff linting * Use consistent verbs, improve health check * poe format * Minor sample improvements * Skip update tests under Java test server --------- Co-authored-by: Drew Hoskins <[email protected]> Co-authored-by: Dan Davison <[email protected]>
1 parent 5ae603e commit 35b476d

File tree

12 files changed

+620
-12
lines changed

12 files changed

+620
-12
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Some examples require extra dependencies. See each sample's directory for specif
6767
* [polling](polling) - Recommended implementation of an activity that needs to periodically poll an external resource waiting its successful completion.
6868
* [prometheus](prometheus) - Configure Prometheus metrics on clients/workers.
6969
* [pydantic_converter](pydantic_converter) - Data converter for using Pydantic models.
70+
* [safe_message_handlers](updates_and_signals/safe_message_handlers/) - Safely handling updates and signals.
7071
* [schedules](schedules) - Demonstrates a Workflow Execution that occurs according to a schedule.
7172
* [sentry](sentry) - Report errors to Sentry.
7273
* [worker_specific_task_queues](worker_specific_task_queues) - Use unique task queues to ensure activities run on specific workers.

Diff for: polling/frequent/README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ To run, first see [README.md](../../README.md) for prerequisites.
88

99
Then, run the following from this directory to run the sample:
1010

11-
```bash
12-
poetry run python run_worker.py
13-
poetry run python run_frequent.py
14-
```
11+
poetry run python run_worker.py
12+
13+
Then, in another terminal, run the following to execute the workflow:
14+
15+
poetry run python run_frequent.py
1516

1617
The Workflow will continue to poll the service and heartbeat on every iteration until it succeeds.
1718

Diff for: polling/infrequent/README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ To run, first see [README.md](../../README.md) for prerequisites.
1313

1414
Then, run the following from this directory to run the sample:
1515

16-
```bash
17-
poetry run python run_worker.py
18-
poetry run python run_infrequent.py
19-
```
16+
poetry run python run_worker.py
17+
18+
Then, in another terminal, run the following to execute the workflow:
19+
20+
poetry run python run_infrequent.py
21+
2022

2123
Since the test service simulates being _down_ for four polling attempts and then returns _OK_ on the fifth poll attempt, the Workflow will perform four Activity retries with a 60-second poll interval, and then return the service result on the successful fifth attempt.
2224

Diff for: polling/periodic_sequence/README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ To run, first see [README.md](../../README.md) for prerequisites.
88

99
Then, run the following from this directory to run the sample:
1010

11-
```bash
12-
poetry run python run_worker.py
13-
poetry run python run_periodic.py
14-
```
11+
poetry run python run_worker.py
12+
13+
Then, in another terminal, run the following to execute the workflow:
14+
15+
poetry run python run_periodic.py
16+
1517

1618
This will start a Workflow and Child Workflow to periodically poll an Activity.
1719
The Parent Workflow is not aware about the Child Workflow calling Continue-As-New, and it gets notified when it completes (or fails).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import asyncio
2+
import uuid
3+
4+
import pytest
5+
from temporalio.client import Client, WorkflowUpdateFailedError
6+
from temporalio.exceptions import ApplicationError
7+
from temporalio.testing import WorkflowEnvironment
8+
from temporalio.worker import Worker
9+
10+
from updates_and_signals.safe_message_handlers.activities import (
11+
assign_nodes_to_job,
12+
find_bad_nodes,
13+
unassign_nodes_for_job,
14+
)
15+
from updates_and_signals.safe_message_handlers.workflow import (
16+
ClusterManagerAssignNodesToJobInput,
17+
ClusterManagerDeleteJobInput,
18+
ClusterManagerInput,
19+
ClusterManagerWorkflow,
20+
)
21+
22+
23+
async def test_safe_message_handlers(client: Client, env: WorkflowEnvironment):
24+
if env.supports_time_skipping:
25+
pytest.skip(
26+
"Java test server: https://github.com/temporalio/sdk-java/issues/1903"
27+
)
28+
task_queue = f"tq-{uuid.uuid4()}"
29+
async with Worker(
30+
client,
31+
task_queue=task_queue,
32+
workflows=[ClusterManagerWorkflow],
33+
activities=[assign_nodes_to_job, unassign_nodes_for_job, find_bad_nodes],
34+
):
35+
cluster_manager_handle = await client.start_workflow(
36+
ClusterManagerWorkflow.run,
37+
ClusterManagerInput(),
38+
id=f"ClusterManagerWorkflow-{uuid.uuid4()}",
39+
task_queue=task_queue,
40+
)
41+
await cluster_manager_handle.signal(ClusterManagerWorkflow.start_cluster)
42+
43+
allocation_updates = []
44+
for i in range(6):
45+
allocation_updates.append(
46+
cluster_manager_handle.execute_update(
47+
ClusterManagerWorkflow.assign_nodes_to_job,
48+
ClusterManagerAssignNodesToJobInput(
49+
total_num_nodes=2, job_name=f"task-{i}"
50+
),
51+
)
52+
)
53+
results = await asyncio.gather(*allocation_updates)
54+
for result in results:
55+
assert len(result.nodes_assigned) == 2
56+
57+
await asyncio.sleep(1)
58+
59+
deletion_updates = []
60+
for i in range(6):
61+
deletion_updates.append(
62+
cluster_manager_handle.execute_update(
63+
ClusterManagerWorkflow.delete_job,
64+
ClusterManagerDeleteJobInput(job_name=f"task-{i}"),
65+
)
66+
)
67+
await asyncio.gather(*deletion_updates)
68+
69+
await cluster_manager_handle.signal(ClusterManagerWorkflow.shutdown_cluster)
70+
71+
result = await cluster_manager_handle.result()
72+
assert result.num_currently_assigned_nodes == 0
73+
74+
75+
async def test_update_idempotency(client: Client, env: WorkflowEnvironment):
76+
if env.supports_time_skipping:
77+
pytest.skip(
78+
"Java test server: https://github.com/temporalio/sdk-java/issues/1903"
79+
)
80+
task_queue = f"tq-{uuid.uuid4()}"
81+
async with Worker(
82+
client,
83+
task_queue=task_queue,
84+
workflows=[ClusterManagerWorkflow],
85+
activities=[assign_nodes_to_job, unassign_nodes_for_job, find_bad_nodes],
86+
):
87+
cluster_manager_handle = await client.start_workflow(
88+
ClusterManagerWorkflow.run,
89+
ClusterManagerInput(),
90+
id=f"ClusterManagerWorkflow-{uuid.uuid4()}",
91+
task_queue=task_queue,
92+
)
93+
94+
await cluster_manager_handle.signal(ClusterManagerWorkflow.start_cluster)
95+
96+
result_1 = await cluster_manager_handle.execute_update(
97+
ClusterManagerWorkflow.assign_nodes_to_job,
98+
ClusterManagerAssignNodesToJobInput(
99+
total_num_nodes=5, job_name="jobby-job"
100+
),
101+
)
102+
# simulate that in calling it twice, the operation is idempotent
103+
result_2 = await cluster_manager_handle.execute_update(
104+
ClusterManagerWorkflow.assign_nodes_to_job,
105+
ClusterManagerAssignNodesToJobInput(
106+
total_num_nodes=5, job_name="jobby-job"
107+
),
108+
)
109+
# the second call should not assign more nodes (it may return fewer if the health check finds bad nodes
110+
# in between the two signals.)
111+
assert result_1.nodes_assigned >= result_2.nodes_assigned
112+
113+
114+
async def test_update_failure(client: Client, env: WorkflowEnvironment):
115+
if env.supports_time_skipping:
116+
pytest.skip(
117+
"Java test server: https://github.com/temporalio/sdk-java/issues/1903"
118+
)
119+
task_queue = f"tq-{uuid.uuid4()}"
120+
async with Worker(
121+
client,
122+
task_queue=task_queue,
123+
workflows=[ClusterManagerWorkflow],
124+
activities=[assign_nodes_to_job, unassign_nodes_for_job, find_bad_nodes],
125+
):
126+
cluster_manager_handle = await client.start_workflow(
127+
ClusterManagerWorkflow.run,
128+
ClusterManagerInput(),
129+
id=f"ClusterManagerWorkflow-{uuid.uuid4()}",
130+
task_queue=task_queue,
131+
)
132+
133+
await cluster_manager_handle.signal(ClusterManagerWorkflow.start_cluster)
134+
135+
await cluster_manager_handle.execute_update(
136+
ClusterManagerWorkflow.assign_nodes_to_job,
137+
ClusterManagerAssignNodesToJobInput(
138+
total_num_nodes=24, job_name="big-task"
139+
),
140+
)
141+
try:
142+
# Try to assign too many nodes
143+
await cluster_manager_handle.execute_update(
144+
ClusterManagerWorkflow.assign_nodes_to_job,
145+
ClusterManagerAssignNodesToJobInput(
146+
total_num_nodes=3, job_name="little-task"
147+
),
148+
)
149+
except WorkflowUpdateFailedError as e:
150+
assert isinstance(e.cause, ApplicationError)
151+
assert e.cause.message == "Cannot assign 3 nodes; have only 1 available"
152+
finally:
153+
await cluster_manager_handle.signal(ClusterManagerWorkflow.shutdown_cluster)
154+
result = await cluster_manager_handle.result()
155+
assert result.num_currently_assigned_nodes + result.num_bad_nodes == 24

Diff for: updates_and_signals/__init__.py

Whitespace-only changes.

Diff for: updates_and_signals/safe_message_handlers/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Atomic message handlers
2+
3+
This sample shows off important techniques for handling signals and updates, aka messages. In particular, it illustrates how message handlers can interleave or not be completed before the workflow completes, and how you can manage that.
4+
5+
* Here, using workflow.wait_condition, signal and update handlers will only operate when the workflow is within a certain state--between cluster_started and cluster_shutdown.
6+
* You can run start_workflow with an initializer signal that you want to run before anything else other than the workflow's constructor. This pattern is known as "signal-with-start."
7+
* Message handlers can block and their actions can be interleaved with one another and with the main workflow. This can easily cause bugs, so we use a lock to protect shared state from interleaved access.
8+
* Message handlers should also finish before the workflow run completes. One option is to use a lock.
9+
* An "Entity" workflow, i.e. a long-lived workflow, periodically "continues as new". It must do this to prevent its history from growing too large, and it passes its state to the next workflow. You can check `workflow.info().is_continue_as_new_suggested()` to see when it's time. Just make sure message handlers have finished before doing so.
10+
* Message handlers can be made idempotent. See update `ClusterManager.assign_nodes_to_job`.
11+
12+
To run, first see [README.md](../../README.md) for prerequisites.
13+
14+
Then, run the following from this directory to run the worker:
15+
\
16+
poetry run python worker.py
17+
18+
Then, in another terminal, run the following to execute the workflow:
19+
20+
poetry run python starter.py
21+
22+
This will start a worker to run your workflow and activities, then start a ClusterManagerWorkflow and put it through its paces.

Diff for: updates_and_signals/safe_message_handlers/__init__.py

Whitespace-only changes.
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import asyncio
2+
from dataclasses import dataclass
3+
from typing import List, Set
4+
5+
from temporalio import activity
6+
7+
8+
@dataclass
9+
class AssignNodesToJobInput:
10+
nodes: List[str]
11+
job_name: str
12+
13+
14+
@activity.defn
15+
async def assign_nodes_to_job(input: AssignNodesToJobInput) -> None:
16+
print(f"Assigning nodes {input.nodes} to job {input.job_name}")
17+
await asyncio.sleep(0.1)
18+
19+
20+
@dataclass
21+
class UnassignNodesForJobInput:
22+
nodes: List[str]
23+
job_name: str
24+
25+
26+
@activity.defn
27+
async def unassign_nodes_for_job(input: UnassignNodesForJobInput) -> None:
28+
print(f"Deallocating nodes {input.nodes} from job {input.job_name}")
29+
await asyncio.sleep(0.1)
30+
31+
32+
@dataclass
33+
class FindBadNodesInput:
34+
nodes_to_check: Set[str]
35+
36+
37+
@activity.defn
38+
async def find_bad_nodes(input: FindBadNodesInput) -> Set[str]:
39+
await asyncio.sleep(0.1)
40+
bad_nodes = set([n for n in input.nodes_to_check if int(n) % 5 == 0])
41+
if bad_nodes:
42+
print(f"Found bad nodes: {bad_nodes}")
43+
else:
44+
print("No new bad nodes found.")
45+
return bad_nodes

Diff for: updates_and_signals/safe_message_handlers/starter.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import argparse
2+
import asyncio
3+
import logging
4+
import uuid
5+
from typing import Optional
6+
7+
from temporalio import common
8+
from temporalio.client import Client, WorkflowHandle
9+
10+
from updates_and_signals.safe_message_handlers.workflow import (
11+
ClusterManagerAssignNodesToJobInput,
12+
ClusterManagerDeleteJobInput,
13+
ClusterManagerInput,
14+
ClusterManagerWorkflow,
15+
)
16+
17+
18+
async def do_cluster_lifecycle(wf: WorkflowHandle, delay_seconds: Optional[int] = None):
19+
20+
await wf.signal(ClusterManagerWorkflow.start_cluster)
21+
22+
print("Assigning jobs to nodes...")
23+
allocation_updates = []
24+
for i in range(6):
25+
allocation_updates.append(
26+
wf.execute_update(
27+
ClusterManagerWorkflow.assign_nodes_to_job,
28+
ClusterManagerAssignNodesToJobInput(
29+
total_num_nodes=2, job_name=f"task-{i}"
30+
),
31+
)
32+
)
33+
await asyncio.gather(*allocation_updates)
34+
35+
print(f"Sleeping for {delay_seconds} second(s)")
36+
if delay_seconds:
37+
await asyncio.sleep(delay_seconds)
38+
39+
print("Deleting jobs...")
40+
deletion_updates = []
41+
for i in range(6):
42+
deletion_updates.append(
43+
wf.execute_update(
44+
ClusterManagerWorkflow.delete_job,
45+
ClusterManagerDeleteJobInput(job_name=f"task-{i}"),
46+
)
47+
)
48+
await asyncio.gather(*deletion_updates)
49+
50+
await wf.signal(ClusterManagerWorkflow.shutdown_cluster)
51+
52+
53+
async def main(should_test_continue_as_new: bool):
54+
# Connect to Temporal
55+
client = await Client.connect("localhost:7233")
56+
57+
print("Starting cluster")
58+
cluster_manager_handle = await client.start_workflow(
59+
ClusterManagerWorkflow.run,
60+
ClusterManagerInput(test_continue_as_new=should_test_continue_as_new),
61+
id=f"ClusterManagerWorkflow-{uuid.uuid4()}",
62+
task_queue="safe-message-handlers-task-queue",
63+
id_reuse_policy=common.WorkflowIDReusePolicy.TERMINATE_IF_RUNNING,
64+
)
65+
delay_seconds = 10 if should_test_continue_as_new else 1
66+
await do_cluster_lifecycle(cluster_manager_handle, delay_seconds=delay_seconds)
67+
result = await cluster_manager_handle.result()
68+
print(
69+
f"Cluster shut down successfully."
70+
f" It had {result.num_currently_assigned_nodes} nodes assigned at the end."
71+
)
72+
73+
74+
if __name__ == "__main__":
75+
logging.basicConfig(level=logging.INFO)
76+
parser = argparse.ArgumentParser(description="Atomic message handlers")
77+
parser.add_argument(
78+
"--test-continue-as-new",
79+
help="Make the ClusterManagerWorkflow continue as new before shutting down",
80+
action="store_true",
81+
default=False,
82+
)
83+
args = parser.parse_args()
84+
asyncio.run(main(args.test_continue_as_new))

0 commit comments

Comments
 (0)