-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy path_worker_versioning.py
More file actions
278 lines (224 loc) · 9.88 KB
/
Copy path_worker_versioning.py
File metadata and controls
278 lines (224 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""Client support for accessing Temporal."""
from __future__ import annotations
from abc import ABC, abstractmethod
from collections.abc import (
Mapping,
Sequence,
)
from dataclasses import dataclass
from enum import Enum
import temporalio.api.enums.v1
import temporalio.api.workflowservice.v1
@dataclass(frozen=True)
class WorkerBuildIdVersionSets:
"""Represents the sets of compatible Build ID versions associated with some Task Queue, as
fetched by :py:meth:`Client.get_worker_build_id_compatibility`.
"""
version_sets: Sequence[BuildIdVersionSet]
"""All version sets that were fetched for this task queue."""
def default_set(self) -> BuildIdVersionSet:
"""Returns the default version set for this task queue."""
return self.version_sets[-1]
def default_build_id(self) -> str:
"""Returns the default Build ID for this task queue."""
return self.default_set().default()
@staticmethod
def _from_proto(
resp: temporalio.api.workflowservice.v1.GetWorkerBuildIdCompatibilityResponse,
) -> WorkerBuildIdVersionSets:
return WorkerBuildIdVersionSets(
version_sets=[
BuildIdVersionSet(mvs.build_ids) for mvs in resp.major_version_sets
]
)
@dataclass(frozen=True)
class BuildIdVersionSet:
"""A set of Build IDs which are compatible with each other."""
build_ids: Sequence[str]
"""All Build IDs contained in the set."""
def default(self) -> str:
"""Returns the default Build ID for this set."""
return self.build_ids[-1]
class BuildIdOp(ABC):
"""Base class for Build ID operations as used by
:py:meth:`Client.update_worker_build_id_compatibility`.
"""
@abstractmethod
def _as_partial_proto(
self,
) -> temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest:
"""Returns a partial request with the operation populated. Caller must populate
non-operation fields. This is done b/c there's no good way to assign a non-primitive message
as the operation after initializing the request.
"""
...
@dataclass(frozen=True)
class BuildIdOpAddNewDefault(BuildIdOp):
"""Adds a new Build Id into a new set, which will be used as the default set for
the queue. This means all new workflows will start on this Build Id.
"""
build_id: str
def _as_partial_proto(
self,
) -> temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest:
return (
temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest(
add_new_build_id_in_new_default_set=self.build_id
)
)
@dataclass(frozen=True)
class BuildIdOpAddNewCompatible(BuildIdOp):
"""Adds a new Build Id into an existing compatible set. The newly added ID becomes
the default for that compatible set, and thus new workflow tasks for workflows which have been
executing on workers in that set will now start on this new Build Id.
"""
build_id: str
"""The Build Id to add to the compatible set."""
existing_compatible_build_id: str
"""A Build Id which must already be defined on the task queue, and is used to find the
compatible set to add the new id to.
"""
promote_set: bool = False
"""If set to true, the targeted set will also be promoted to become the overall default set for
the queue."""
def _as_partial_proto(
self,
) -> temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest:
return temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest(
add_new_compatible_build_id=temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest.AddNewCompatibleVersion(
new_build_id=self.build_id,
existing_compatible_build_id=self.existing_compatible_build_id,
make_set_default=self.promote_set,
)
)
@dataclass(frozen=True)
class BuildIdOpPromoteSetByBuildId(BuildIdOp):
"""Promotes a set of compatible Build Ids to become the current default set for the task queue.
Any Build Id in the set may be used to target it.
"""
build_id: str
"""A Build Id which must already be defined on the task queue, and is used to find the
compatible set to promote."""
def _as_partial_proto(
self,
) -> temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest:
return (
temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest(
promote_set_by_build_id=self.build_id
)
)
@dataclass(frozen=True)
class BuildIdOpPromoteBuildIdWithinSet(BuildIdOp):
"""Promotes a Build Id within an existing set to become the default ID for that set."""
build_id: str
def _as_partial_proto(
self,
) -> temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest:
return (
temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest(
promote_build_id_within_set=self.build_id
)
)
@dataclass(frozen=True)
class BuildIdOpMergeSets(BuildIdOp):
"""Merges two sets into one set, thus declaring all the Build Ids in both as compatible with one
another. The default of the primary set is maintained as the merged set's overall default.
"""
primary_build_id: str
"""A Build Id which and is used to find the primary set to be merged."""
secondary_build_id: str
"""A Build Id which and is used to find the secondary set to be merged."""
def _as_partial_proto(
self,
) -> temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest:
return temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest(
merge_sets=temporalio.api.workflowservice.v1.UpdateWorkerBuildIdCompatibilityRequest.MergeSets(
primary_set_build_id=self.primary_build_id,
secondary_set_build_id=self.secondary_build_id,
)
)
@dataclass(frozen=True)
class WorkerTaskReachability:
"""Contains information about the reachability of some Build IDs"""
build_id_reachability: Mapping[str, BuildIdReachability]
"""Maps Build IDs to information about their reachability"""
@staticmethod
def _from_proto(
resp: temporalio.api.workflowservice.v1.GetWorkerTaskReachabilityResponse,
) -> WorkerTaskReachability:
mapping = dict()
for bid_reach in resp.build_id_reachability:
tq_mapping = dict()
unretrieved = set()
for tq_reach in bid_reach.task_queue_reachability:
if tq_reach.reachability == [
temporalio.api.enums.v1.TaskReachability.TASK_REACHABILITY_UNSPECIFIED
]:
unretrieved.add(tq_reach.task_queue)
continue
tq_mapping[tq_reach.task_queue] = [
TaskReachabilityType._from_proto(r) for r in tq_reach.reachability
]
mapping[bid_reach.build_id] = BuildIdReachability(
task_queue_reachability=tq_mapping,
unretrieved_task_queues=frozenset(unretrieved),
)
return WorkerTaskReachability(build_id_reachability=mapping)
@dataclass(frozen=True)
class BuildIdReachability:
"""Contains information about the reachability of a specific Build ID"""
task_queue_reachability: Mapping[str, Sequence[TaskReachabilityType]]
"""Maps Task Queue names to the reachability status of the Build ID on that queue. If the value
is an empty list, the Build ID is not reachable on that queue.
"""
unretrieved_task_queues: frozenset[str]
"""If any Task Queues could not be retrieved because the server limits the number that can be
queried at once, they will be listed here.
"""
class TaskReachabilityType(Enum):
"""Enumerates how a task might reach certain kinds of workflows"""
NEW_WORKFLOWS = 1
EXISTING_WORKFLOWS = 2
OPEN_WORKFLOWS = 3
CLOSED_WORKFLOWS = 4
@staticmethod
def _from_proto(
reachability: temporalio.api.enums.v1.TaskReachability.ValueType,
) -> TaskReachabilityType:
if (
reachability
== temporalio.api.enums.v1.TaskReachability.TASK_REACHABILITY_NEW_WORKFLOWS
):
return TaskReachabilityType.NEW_WORKFLOWS
elif (
reachability
== temporalio.api.enums.v1.TaskReachability.TASK_REACHABILITY_EXISTING_WORKFLOWS
):
return TaskReachabilityType.EXISTING_WORKFLOWS
elif (
reachability
== temporalio.api.enums.v1.TaskReachability.TASK_REACHABILITY_OPEN_WORKFLOWS
):
return TaskReachabilityType.OPEN_WORKFLOWS
elif (
reachability
== temporalio.api.enums.v1.TaskReachability.TASK_REACHABILITY_CLOSED_WORKFLOWS
):
return TaskReachabilityType.CLOSED_WORKFLOWS
else:
raise ValueError(f"Cannot convert reachability type: {reachability}")
def _to_proto(self) -> temporalio.api.enums.v1.TaskReachability.ValueType:
if self == TaskReachabilityType.NEW_WORKFLOWS:
return (
temporalio.api.enums.v1.TaskReachability.TASK_REACHABILITY_NEW_WORKFLOWS
)
elif self == TaskReachabilityType.EXISTING_WORKFLOWS:
return temporalio.api.enums.v1.TaskReachability.TASK_REACHABILITY_EXISTING_WORKFLOWS
elif self == TaskReachabilityType.OPEN_WORKFLOWS:
return temporalio.api.enums.v1.TaskReachability.TASK_REACHABILITY_OPEN_WORKFLOWS
elif self == TaskReachabilityType.CLOSED_WORKFLOWS:
return temporalio.api.enums.v1.TaskReachability.TASK_REACHABILITY_CLOSED_WORKFLOWS
else:
return (
temporalio.api.enums.v1.TaskReachability.TASK_REACHABILITY_UNSPECIFIED
)