@temporalio/core-bridge 0.23.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/Cargo.lock +118 -15
  2. package/Cargo.toml +2 -1
  3. package/LICENSE.md +1 -1
  4. package/README.md +1 -1
  5. package/index.d.ts +47 -18
  6. package/package.json +7 -7
  7. package/releases/aarch64-apple-darwin/index.node +0 -0
  8. package/releases/aarch64-unknown-linux-gnu/index.node +0 -0
  9. package/releases/x86_64-apple-darwin/index.node +0 -0
  10. package/releases/x86_64-pc-windows-msvc/index.node +0 -0
  11. package/releases/x86_64-unknown-linux-gnu/index.node +0 -0
  12. package/sdk-core/.buildkite/docker/docker-compose.yaml +4 -2
  13. package/sdk-core/ARCHITECTURE.md +9 -7
  14. package/sdk-core/README.md +5 -1
  15. package/sdk-core/arch_docs/diagrams/workflow_internals.svg +1 -0
  16. package/sdk-core/bridge-ffi/src/wrappers.rs +0 -3
  17. package/sdk-core/client/src/lib.rs +26 -8
  18. package/sdk-core/client/src/raw.rs +166 -54
  19. package/sdk-core/client/src/retry.rs +9 -4
  20. package/sdk-core/client/src/workflow_handle/mod.rs +4 -2
  21. package/sdk-core/core/Cargo.toml +2 -0
  22. package/sdk-core/core/src/abstractions.rs +137 -16
  23. package/sdk-core/core/src/core_tests/activity_tasks.rs +258 -63
  24. package/sdk-core/core/src/core_tests/child_workflows.rs +1 -2
  25. package/sdk-core/core/src/core_tests/determinism.rs +2 -2
  26. package/sdk-core/core/src/core_tests/local_activities.rs +8 -7
  27. package/sdk-core/core/src/core_tests/queries.rs +146 -60
  28. package/sdk-core/core/src/core_tests/replay_flag.rs +1 -1
  29. package/sdk-core/core/src/core_tests/workers.rs +39 -23
  30. package/sdk-core/core/src/core_tests/workflow_cancels.rs +1 -1
  31. package/sdk-core/core/src/core_tests/workflow_tasks.rs +387 -280
  32. package/sdk-core/core/src/lib.rs +6 -4
  33. package/sdk-core/core/src/pollers/poll_buffer.rs +16 -10
  34. package/sdk-core/core/src/protosext/mod.rs +6 -6
  35. package/sdk-core/core/src/retry_logic.rs +1 -1
  36. package/sdk-core/core/src/telemetry/metrics.rs +21 -7
  37. package/sdk-core/core/src/telemetry/mod.rs +18 -4
  38. package/sdk-core/core/src/test_help/mod.rs +341 -109
  39. package/sdk-core/core/src/worker/activities/activity_heartbeat_manager.rs +18 -9
  40. package/sdk-core/core/src/worker/activities/local_activities.rs +19 -16
  41. package/sdk-core/core/src/worker/activities.rs +156 -29
  42. package/sdk-core/core/src/worker/client.rs +1 -0
  43. package/sdk-core/core/src/worker/mod.rs +132 -659
  44. package/sdk-core/core/src/{workflow → worker/workflow}/bridge.rs +1 -1
  45. package/sdk-core/core/src/{workflow → worker/workflow}/driven_workflow.rs +1 -1
  46. package/sdk-core/core/src/{workflow → worker/workflow}/history_update.rs +16 -2
  47. package/sdk-core/core/src/{workflow → worker/workflow}/machines/activity_state_machine.rs +39 -4
  48. package/sdk-core/core/src/{workflow → worker/workflow}/machines/cancel_external_state_machine.rs +5 -2
  49. package/sdk-core/core/src/{workflow → worker/workflow}/machines/cancel_workflow_state_machine.rs +1 -1
  50. package/sdk-core/core/src/{workflow → worker/workflow}/machines/child_workflow_state_machine.rs +2 -4
  51. package/sdk-core/core/src/{workflow → worker/workflow}/machines/complete_workflow_state_machine.rs +0 -0
  52. package/sdk-core/core/src/{workflow → worker/workflow}/machines/continue_as_new_workflow_state_machine.rs +1 -1
  53. package/sdk-core/core/src/{workflow → worker/workflow}/machines/fail_workflow_state_machine.rs +0 -0
  54. package/sdk-core/core/src/{workflow → worker/workflow}/machines/local_activity_state_machine.rs +2 -5
  55. package/sdk-core/core/src/{workflow → worker/workflow}/machines/mod.rs +1 -1
  56. package/sdk-core/core/src/{workflow → worker/workflow}/machines/mutable_side_effect_state_machine.rs +0 -0
  57. package/sdk-core/core/src/{workflow → worker/workflow}/machines/patch_state_machine.rs +1 -1
  58. package/sdk-core/core/src/{workflow → worker/workflow}/machines/side_effect_state_machine.rs +0 -0
  59. package/sdk-core/core/src/{workflow → worker/workflow}/machines/signal_external_state_machine.rs +4 -2
  60. package/sdk-core/core/src/{workflow → worker/workflow}/machines/timer_state_machine.rs +1 -2
  61. package/sdk-core/core/src/{workflow → worker/workflow}/machines/transition_coverage.rs +1 -1
  62. package/sdk-core/core/src/{workflow → worker/workflow}/machines/upsert_search_attributes_state_machine.rs +5 -7
  63. package/sdk-core/core/src/{workflow → worker/workflow}/machines/workflow_machines/local_acts.rs +2 -2
  64. package/sdk-core/core/src/{workflow → worker/workflow}/machines/workflow_machines.rs +40 -16
  65. package/sdk-core/core/src/{workflow → worker/workflow}/machines/workflow_task_state_machine.rs +0 -0
  66. package/sdk-core/core/src/worker/workflow/managed_run/managed_wf_test.rs +198 -0
  67. package/sdk-core/core/src/worker/workflow/managed_run.rs +627 -0
  68. package/sdk-core/core/src/worker/workflow/mod.rs +1115 -0
  69. package/sdk-core/core/src/worker/workflow/run_cache.rs +143 -0
  70. package/sdk-core/core/src/worker/workflow/wft_poller.rs +88 -0
  71. package/sdk-core/core/src/worker/workflow/workflow_stream.rs +936 -0
  72. package/sdk-core/core-api/src/errors.rs +3 -10
  73. package/sdk-core/core-api/src/lib.rs +2 -1
  74. package/sdk-core/core-api/src/worker.rs +26 -2
  75. package/sdk-core/etc/dynamic-config.yaml +2 -0
  76. package/sdk-core/integ-with-otel.sh +1 -1
  77. package/sdk-core/protos/api_upstream/Makefile +4 -4
  78. package/sdk-core/protos/api_upstream/api-linter.yaml +2 -0
  79. package/sdk-core/protos/api_upstream/buf.yaml +8 -9
  80. package/sdk-core/protos/api_upstream/temporal/api/cluster/v1/message.proto +83 -0
  81. package/sdk-core/protos/api_upstream/temporal/api/command/v1/message.proto +7 -1
  82. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/cluster.proto +40 -0
  83. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/failed_cause.proto +3 -0
  84. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/reset.proto +3 -1
  85. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/schedule.proto +60 -0
  86. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/workflow.proto +3 -0
  87. package/sdk-core/protos/api_upstream/temporal/api/errordetails/v1/message.proto +32 -4
  88. package/sdk-core/protos/api_upstream/temporal/api/history/v1/message.proto +69 -19
  89. package/sdk-core/protos/api_upstream/temporal/api/namespace/v1/message.proto +13 -0
  90. package/sdk-core/protos/api_upstream/temporal/api/operatorservice/v1/request_response.proto +163 -0
  91. package/sdk-core/protos/api_upstream/temporal/api/operatorservice/v1/service.proto +97 -0
  92. package/sdk-core/protos/api_upstream/temporal/api/schedule/v1/message.proto +300 -0
  93. package/sdk-core/protos/api_upstream/temporal/api/workflow/v1/message.proto +25 -0
  94. package/sdk-core/protos/api_upstream/temporal/api/workflowservice/v1/request_response.proto +180 -3
  95. package/sdk-core/protos/api_upstream/temporal/api/workflowservice/v1/service.proto +53 -3
  96. package/sdk-core/protos/local/temporal/sdk/core/activity_result/activity_result.proto +2 -2
  97. package/sdk-core/protos/local/temporal/sdk/core/activity_task/activity_task.proto +6 -5
  98. package/sdk-core/protos/local/temporal/sdk/core/bridge/bridge.proto +0 -1
  99. package/sdk-core/protos/local/temporal/sdk/core/child_workflow/child_workflow.proto +2 -1
  100. package/sdk-core/protos/local/temporal/sdk/core/common/common.proto +0 -64
  101. package/sdk-core/protos/local/temporal/sdk/core/core_interface.proto +2 -1
  102. package/sdk-core/protos/local/temporal/sdk/core/workflow_activation/workflow_activation.proto +11 -8
  103. package/sdk-core/protos/local/temporal/sdk/core/workflow_commands/workflow_commands.proto +30 -25
  104. package/sdk-core/sdk/src/activity_context.rs +12 -5
  105. package/sdk-core/sdk/src/app_data.rs +37 -0
  106. package/sdk-core/sdk/src/lib.rs +76 -43
  107. package/sdk-core/sdk/src/workflow_context/options.rs +8 -6
  108. package/sdk-core/sdk/src/workflow_context.rs +14 -19
  109. package/sdk-core/sdk/src/workflow_future.rs +11 -6
  110. package/sdk-core/sdk-core-protos/src/history_builder.rs +19 -5
  111. package/sdk-core/sdk-core-protos/src/history_info.rs +11 -6
  112. package/sdk-core/sdk-core-protos/src/lib.rs +74 -176
  113. package/sdk-core/test-utils/src/lib.rs +85 -72
  114. package/sdk-core/tests/integ_tests/heartbeat_tests.rs +11 -9
  115. package/sdk-core/tests/integ_tests/polling_tests.rs +12 -0
  116. package/sdk-core/tests/integ_tests/queries_tests.rs +39 -22
  117. package/sdk-core/tests/integ_tests/workflow_tests/activities.rs +49 -4
  118. package/sdk-core/tests/integ_tests/workflow_tests/appdata_propagation.rs +61 -0
  119. package/sdk-core/tests/integ_tests/workflow_tests/cancel_wf.rs +1 -1
  120. package/sdk-core/tests/integ_tests/workflow_tests/local_activities.rs +74 -13
  121. package/sdk-core/tests/integ_tests/workflow_tests/replay.rs +19 -0
  122. package/sdk-core/tests/integ_tests/workflow_tests/resets.rs +1 -1
  123. package/sdk-core/tests/integ_tests/workflow_tests/upsert_search_attrs.rs +6 -3
  124. package/sdk-core/tests/integ_tests/workflow_tests.rs +10 -23
  125. package/sdk-core/tests/load_tests.rs +8 -3
  126. package/sdk-core/tests/main.rs +2 -1
  127. package/src/conversions.rs +47 -39
  128. package/src/errors.rs +10 -21
  129. package/src/lib.rs +342 -325
  130. package/sdk-core/core/src/pending_activations.rs +0 -173
  131. package/sdk-core/core/src/worker/wft_delivery.rs +0 -81
  132. package/sdk-core/core/src/workflow/mod.rs +0 -478
  133. package/sdk-core/core/src/workflow/workflow_tasks/cache_manager.rs +0 -194
  134. package/sdk-core/core/src/workflow/workflow_tasks/concurrency_manager.rs +0 -418
  135. package/sdk-core/core/src/workflow/workflow_tasks/mod.rs +0 -989
@@ -0,0 +1,1115 @@
1
+ //! This module and its submodules implement Core's logic for managing workflows (which is the
2
+ //! lion's share of the complexity in Core). See the `ARCHITECTURE.md` file in the repo root for
3
+ //! a diagram of the internals.
4
+
5
+ mod bridge;
6
+ mod driven_workflow;
7
+ mod history_update;
8
+ mod machines;
9
+ mod managed_run;
10
+ mod run_cache;
11
+ pub(crate) mod wft_poller;
12
+ mod workflow_stream;
13
+
14
+ pub(crate) use bridge::WorkflowBridge;
15
+ pub(crate) use driven_workflow::{DrivenWorkflow, WorkflowFetcher};
16
+ pub(crate) use history_update::{HistoryPaginator, HistoryUpdate};
17
+ pub(crate) use machines::WFMachinesError;
18
+ #[cfg(test)]
19
+ pub(crate) use managed_run::ManagedWFFunc;
20
+
21
+ use crate::{
22
+ abstractions::OwnedMeteredSemPermit,
23
+ protosext::{legacy_query_failure, ValidPollWFTQResponse, WorkflowActivationExt},
24
+ telemetry::VecDisplayer,
25
+ worker::{
26
+ activities::{ActivitiesFromWFTsHandle, PermittedTqResp},
27
+ workflow::{
28
+ managed_run::{ManagedRun, WorkflowManager},
29
+ wft_poller::validate_wft,
30
+ workflow_stream::{LocalInput, LocalInputs, WFStream},
31
+ },
32
+ LocalActRequest, LocalActivityResolution,
33
+ },
34
+ MetricsContext, WorkerClientBag,
35
+ };
36
+ use futures::{stream::BoxStream, Stream, StreamExt};
37
+ use std::{
38
+ fmt::{Debug, Display, Formatter},
39
+ future::Future,
40
+ ops::DerefMut,
41
+ result,
42
+ sync::Arc,
43
+ time::{Duration, Instant},
44
+ };
45
+ use temporal_client::WorkflowTaskCompletion;
46
+ use temporal_sdk_core_api::errors::{CompleteWfError, PollWfError};
47
+ use temporal_sdk_core_protos::{
48
+ coresdk::{
49
+ workflow_activation::{
50
+ remove_from_cache::EvictionReason, QueryWorkflow, WorkflowActivation,
51
+ },
52
+ workflow_commands::*,
53
+ workflow_completion,
54
+ workflow_completion::{
55
+ workflow_activation_completion, Failure, WorkflowActivationCompletion,
56
+ },
57
+ },
58
+ temporal::api::{
59
+ command::v1::{command::Attributes, Command as ProtoCommand, Command},
60
+ enums::v1::WorkflowTaskFailedCause,
61
+ taskqueue::v1::StickyExecutionAttributes,
62
+ workflowservice::v1::PollActivityTaskQueueResponse,
63
+ },
64
+ TaskToken,
65
+ };
66
+ use tokio::{
67
+ sync::{
68
+ mpsc::{unbounded_channel, UnboundedSender},
69
+ oneshot,
70
+ },
71
+ task,
72
+ task::{JoinError, JoinHandle},
73
+ };
74
+ use tokio_stream::wrappers::UnboundedReceiverStream;
75
+ use tokio_util::sync::CancellationToken;
76
+ use tracing::Span;
77
+
78
+ pub(crate) const LEGACY_QUERY_ID: &str = "legacy_query";
79
+
80
+ type Result<T, E = WFMachinesError> = result::Result<T, E>;
81
+ type BoxedActivationStream = BoxStream<'static, Result<ActivationOrAuto, PollWfError>>;
82
+
83
+ /// Centralizes all state related to workflows and workflow tasks
84
+ pub(crate) struct Workflows {
85
+ local_tx: UnboundedSender<LocalInput>,
86
+ processing_task: tokio::sync::Mutex<Option<JoinHandle<()>>>,
87
+ activation_stream: tokio::sync::Mutex<(
88
+ BoxedActivationStream,
89
+ // Used to indicate polling may begin
90
+ Option<oneshot::Sender<()>>,
91
+ )>,
92
+ client: Arc<WorkerClientBag>,
93
+ /// Will be populated when this worker is using a cache and should complete WFTs with a sticky
94
+ /// queue.
95
+ sticky_attrs: Option<StickyExecutionAttributes>,
96
+ /// If set, can be used to reserve activity task slots for eager-return of new activity tasks.
97
+ activity_tasks_handle: Option<ActivitiesFromWFTsHandle>,
98
+ }
99
+
100
+ pub(super) struct WorkflowBasics {
101
+ pub max_cached_workflows: usize,
102
+ pub max_outstanding_wfts: usize,
103
+ pub shutdown_token: CancellationToken,
104
+ pub metrics: MetricsContext,
105
+ }
106
+
107
+ impl Workflows {
108
+ pub(super) fn new(
109
+ basics: WorkflowBasics,
110
+ sticky_attrs: Option<StickyExecutionAttributes>,
111
+ client: Arc<WorkerClientBag>,
112
+ wft_stream: impl Stream<Item = Result<ValidPollWFTQResponse, tonic::Status>> + Send + 'static,
113
+ local_activity_request_sink: impl Fn(Vec<LocalActRequest>) -> Vec<LocalActivityResolution>
114
+ + Send
115
+ + Sync
116
+ + 'static,
117
+ activity_tasks_handle: Option<ActivitiesFromWFTsHandle>,
118
+ ) -> Self {
119
+ let (local_tx, local_rx) = unbounded_channel();
120
+ let shutdown_tok = basics.shutdown_token.clone();
121
+ let mut stream = WFStream::build(
122
+ basics,
123
+ wft_stream,
124
+ UnboundedReceiverStream::new(local_rx),
125
+ client.clone(),
126
+ local_activity_request_sink,
127
+ );
128
+ let (activation_tx, activation_rx) = unbounded_channel();
129
+ let (start_polling_tx, start_polling_rx) = oneshot::channel();
130
+ // We must spawn a task to constantly poll the activation stream, because otherwise
131
+ // activation completions would not cause anything to happen until the next poll.
132
+ let processing_task = task::spawn(async move {
133
+ // However, we want to avoid plowing ahead until we've been asked to poll at least once.
134
+ // This supports activity-only workers.
135
+ let do_poll = tokio::select! {
136
+ sp = start_polling_rx => {
137
+ sp.is_ok()
138
+ }
139
+ _ = shutdown_tok.cancelled() => {
140
+ false
141
+ }
142
+ };
143
+ if !do_poll {
144
+ return;
145
+ }
146
+ while let Some(act) = stream.next().await {
147
+ activation_tx
148
+ .send(act)
149
+ .expect("Activation processor channel not dropped");
150
+ }
151
+ });
152
+ Self {
153
+ local_tx,
154
+ processing_task: tokio::sync::Mutex::new(Some(processing_task)),
155
+ activation_stream: tokio::sync::Mutex::new((
156
+ UnboundedReceiverStream::new(activation_rx).boxed(),
157
+ Some(start_polling_tx),
158
+ )),
159
+ client,
160
+ sticky_attrs,
161
+ activity_tasks_handle,
162
+ }
163
+ }
164
+
165
+ pub async fn next_workflow_activation(&self) -> Result<WorkflowActivation, PollWfError> {
166
+ loop {
167
+ let r = {
168
+ let mut lock = self.activation_stream.lock().await;
169
+ let (ref mut stream, ref mut beginner) = lock.deref_mut();
170
+ if let Some(beginner) = beginner.take() {
171
+ let _ = beginner.send(());
172
+ }
173
+ stream.next().await.unwrap_or(Err(PollWfError::ShutDown))?
174
+ };
175
+ Span::current().record("run_id", &r.run_id());
176
+ match r {
177
+ ActivationOrAuto::LangActivation(act) | ActivationOrAuto::ReadyForQueries(act) => {
178
+ debug!(activation=%act, "Sending activation to lang");
179
+ break Ok(act);
180
+ }
181
+ ActivationOrAuto::Autocomplete { run_id } => {
182
+ self.activation_completed(WorkflowActivationCompletion {
183
+ run_id,
184
+ status: Some(workflow_completion::Success::from_variants(vec![]).into()),
185
+ })
186
+ .await?;
187
+ }
188
+ }
189
+ }
190
+ }
191
+
192
+ /// Queue an activation completion for processing, returning a future that will resolve with
193
+ /// the outcome of that completion. See [ActivationCompletedOutcome].
194
+ ///
195
+ /// Returns the most-recently-processed event number for the run
196
+ pub async fn activation_completed(
197
+ &self,
198
+ completion: WorkflowActivationCompletion,
199
+ ) -> Result<usize, CompleteWfError> {
200
+ let is_empty_completion = completion.is_empty();
201
+ let completion = validate_completion(completion)?;
202
+ let run_id = completion.run_id().to_string();
203
+ let (tx, rx) = oneshot::channel();
204
+ let was_sent = self.send_local(WFActCompleteMsg {
205
+ completion,
206
+ response_tx: tx,
207
+ });
208
+ if !was_sent {
209
+ if is_empty_completion {
210
+ // Empty complete which is likely an evict reply, we can just ignore.
211
+ return Ok(0);
212
+ }
213
+ panic!(
214
+ "A non-empty completion was not processed. Workflow processing may have \
215
+ terminated unexpectedly. This is a bug."
216
+ );
217
+ }
218
+
219
+ let completion_outcome = rx
220
+ .await
221
+ .expect("Send half of activation complete response not dropped");
222
+ let mut wft_from_complete = None;
223
+ let reported_wft_to_server = match completion_outcome.outcome {
224
+ ActivationCompleteOutcome::ReportWFTSuccess(report) => match report {
225
+ ServerCommandsWithWorkflowInfo {
226
+ task_token,
227
+ action:
228
+ ActivationAction::WftComplete {
229
+ mut commands,
230
+ query_responses,
231
+ force_new_wft,
232
+ },
233
+ } => {
234
+ let reserved_act_permits =
235
+ self.reserve_activity_slots_for_outgoing_commands(commands.as_mut_slice());
236
+ debug!(commands=%commands.display(), query_responses=%query_responses.display(),
237
+ "Sending responses to server");
238
+ let mut completion = WorkflowTaskCompletion {
239
+ task_token,
240
+ commands,
241
+ query_responses,
242
+ sticky_attributes: None,
243
+ return_new_workflow_task: true,
244
+ force_create_new_workflow_task: force_new_wft,
245
+ };
246
+ let sticky_attrs = self.sticky_attrs.clone();
247
+ // Do not return new WFT if we would not cache, because returned new WFTs are
248
+ // always partial.
249
+ if sticky_attrs.is_none() {
250
+ completion.return_new_workflow_task = false;
251
+ }
252
+ completion.sticky_attributes = sticky_attrs;
253
+
254
+ self.handle_wft_reporting_errs(&run_id, || async {
255
+ let maybe_wft = self.client.complete_workflow_task(completion).await?;
256
+ if let Some(wft) = maybe_wft.workflow_task {
257
+ wft_from_complete = Some(validate_wft(wft)?);
258
+ }
259
+ self.handle_eager_activities(
260
+ reserved_act_permits,
261
+ maybe_wft.activity_tasks,
262
+ );
263
+ Ok(())
264
+ })
265
+ .await?;
266
+ true
267
+ }
268
+ ServerCommandsWithWorkflowInfo {
269
+ task_token,
270
+ action: ActivationAction::RespondLegacyQuery { result },
271
+ } => {
272
+ self.respond_legacy_query(task_token, result).await?;
273
+ true
274
+ }
275
+ },
276
+ ActivationCompleteOutcome::ReportWFTFail(outcome) => match outcome {
277
+ FailedActivationWFTReport::Report(tt, cause, failure) => {
278
+ warn!(run_id=%run_id, failure=?failure, "Failing workflow task");
279
+ self.handle_wft_reporting_errs(&run_id, || async {
280
+ self.client
281
+ .fail_workflow_task(tt, cause, failure.failure.map(Into::into))
282
+ .await
283
+ })
284
+ .await?;
285
+ true
286
+ }
287
+ FailedActivationWFTReport::ReportLegacyQueryFailure(task_token, failure) => {
288
+ warn!(run_id=%run_id, failure=?failure, "Failing legacy query request");
289
+ self.respond_legacy_query(task_token, legacy_query_failure(failure))
290
+ .await?;
291
+ true
292
+ }
293
+ },
294
+ ActivationCompleteOutcome::DoNothing => false,
295
+ };
296
+
297
+ self.post_activation(PostActivationMsg {
298
+ run_id,
299
+ reported_wft_to_server,
300
+ wft_from_complete,
301
+ });
302
+
303
+ Ok(completion_outcome.most_recently_processed_event)
304
+ }
305
+
306
+ /// Tell workflow that a local activity has finished with the provided result
307
+ pub fn notify_of_local_result(&self, run_id: impl Into<String>, resolved: LocalResolution) {
308
+ self.send_local(LocalResolutionMsg {
309
+ run_id: run_id.into(),
310
+ res: resolved,
311
+ });
312
+ }
313
+
314
+ /// Request eviction of a workflow
315
+ pub fn request_eviction(
316
+ &self,
317
+ run_id: impl Into<String>,
318
+ message: impl Into<String>,
319
+ reason: EvictionReason,
320
+ ) {
321
+ self.send_local(RequestEvictMsg {
322
+ run_id: run_id.into(),
323
+ message: message.into(),
324
+ reason,
325
+ });
326
+ }
327
+
328
+ /// Query the state of workflow management. Can return `None` if workflow state is shut down.
329
+ pub fn get_state_info(&self) -> impl Future<Output = Option<WorkflowStateInfo>> {
330
+ let (tx, rx) = oneshot::channel();
331
+ self.send_local(GetStateInfoMsg { response_tx: tx });
332
+ async move { rx.await.ok() }
333
+ }
334
+
335
+ pub async fn shutdown(&self) -> Result<(), JoinError> {
336
+ let maybe_jh = self.processing_task.lock().await.take();
337
+ if let Some(jh) = maybe_jh {
338
+ // This acts as a final wake up in case the stream is still alive and wouldn't otherwise
339
+ // receive another message. It allows it to shut itself down.
340
+ let _ = self.get_state_info();
341
+ jh.await
342
+ } else {
343
+ Ok(())
344
+ }
345
+ }
346
+
347
+ /// Must be called after every activation completion has finished
348
+ fn post_activation(&self, msg: PostActivationMsg) {
349
+ self.send_local(msg);
350
+ }
351
+
352
+ /// Handle server errors from either completing or failing a workflow task. Returns any errors
353
+ /// that can't be automatically handled.
354
+ async fn handle_wft_reporting_errs<T, Fut>(
355
+ &self,
356
+ run_id: &str,
357
+ completer: impl FnOnce() -> Fut,
358
+ ) -> Result<(), CompleteWfError>
359
+ where
360
+ Fut: Future<Output = Result<T, tonic::Status>>,
361
+ {
362
+ let mut should_evict = None;
363
+ let res = match completer().await {
364
+ Err(err) => {
365
+ match err.code() {
366
+ // Silence unhandled command errors since the lang SDK cannot do anything about
367
+ // them besides poll again, which it will do anyway.
368
+ tonic::Code::InvalidArgument if err.message() == "UnhandledCommand" => {
369
+ debug!(error = %err, run_id, "Unhandled command response when completing");
370
+ should_evict = Some(EvictionReason::UnhandledCommand);
371
+ Ok(())
372
+ }
373
+ tonic::Code::NotFound => {
374
+ warn!(error = %err, run_id, "Task not found when completing");
375
+ should_evict = Some(EvictionReason::TaskNotFound);
376
+ Ok(())
377
+ }
378
+ _ => Err(err),
379
+ }
380
+ }
381
+ _ => Ok(()),
382
+ };
383
+ if let Some(reason) = should_evict {
384
+ self.request_eviction(run_id, "Error reporting WFT to server", reason);
385
+ }
386
+ res.map_err(Into::into)
387
+ }
388
+
389
+ /// Sends a message to the workflow processing stream. Returns true if the message was sent
390
+ /// successfully.
391
+ fn send_local(&self, msg: impl Into<LocalInputs>) -> bool {
392
+ let msg = msg.into();
393
+ let print_err = !matches!(msg, LocalInputs::GetStateInfo(_));
394
+ if let Err(e) = self.local_tx.send(LocalInput {
395
+ input: msg,
396
+ span: Span::current(),
397
+ }) {
398
+ if print_err {
399
+ warn!(
400
+ "Tried to interact with workflow state after it shut down. This may be benign \
401
+ when processing evictions during shutdown. When sending {:?}",
402
+ e.0.input
403
+ )
404
+ }
405
+ false
406
+ } else {
407
+ true
408
+ }
409
+ }
410
+
411
+ /// Process eagerly returned activities from WFT completion
412
+ fn handle_eager_activities(
413
+ &self,
414
+ reserved_act_permits: Vec<OwnedMeteredSemPermit>,
415
+ eager_acts: Vec<PollActivityTaskQueueResponse>,
416
+ ) {
417
+ if let Some(at_handle) = self.activity_tasks_handle.as_ref() {
418
+ let excess_reserved = reserved_act_permits.len().saturating_sub(eager_acts.len());
419
+ if excess_reserved > 0 {
420
+ debug!(
421
+ "Server returned {excess_reserved} fewer activities for \
422
+ eager execution than we requested"
423
+ );
424
+ } else if eager_acts.len() > reserved_act_permits.len() {
425
+ // If we somehow got more activities from server than we asked for, server did
426
+ // something wrong.
427
+ error!(
428
+ "Server sent more activities for eager execution than we requested! They will \
429
+ be dropped and eventually time out. Please report this, as it is a server bug."
430
+ )
431
+ }
432
+ let with_permits = reserved_act_permits
433
+ .into_iter()
434
+ .zip(eager_acts.into_iter())
435
+ .map(|(permit, resp)| PermittedTqResp { permit, resp });
436
+ if with_permits.len() > 0 {
437
+ debug!(
438
+ "Adding {} activity tasks received from WFT complete",
439
+ with_permits.len()
440
+ );
441
+ at_handle.add_tasks(with_permits);
442
+ }
443
+ } else if !eager_acts.is_empty() {
444
+ panic!(
445
+ "Requested eager activity execution but this worker has no activity task \
446
+ manager! This is an internal bug, Core should not have asked for tasks."
447
+ )
448
+ }
449
+ }
450
+
451
+ /// Attempt to reserve activity slots for activities we could eagerly execute on
452
+ /// this worker.
453
+ ///
454
+ /// Returns the number of activity slots that were reserved
455
+ fn reserve_activity_slots_for_outgoing_commands(
456
+ &self,
457
+ commands: &mut [Command],
458
+ ) -> Vec<OwnedMeteredSemPermit> {
459
+ let mut reserved = vec![];
460
+ if let Some(at_handle) = self.activity_tasks_handle.as_ref() {
461
+ for cmd in commands {
462
+ if let Some(Attributes::ScheduleActivityTaskCommandAttributes(attrs)) =
463
+ cmd.attributes.as_mut()
464
+ {
465
+ // If request_eager_execution was already false, that means lang explicitly
466
+ // told us it didn't want to eagerly execute for some reason. So, we only
467
+ // ever turn *off* eager execution if a slot is not available.
468
+ if attrs.request_eager_execution {
469
+ if let Some(p) = at_handle.reserve_slot() {
470
+ reserved.push(p);
471
+ } else {
472
+ attrs.request_eager_execution = false;
473
+ }
474
+ }
475
+ }
476
+ }
477
+ }
478
+ reserved
479
+ }
480
+
481
+ /// Wraps responding to legacy queries. Handles ignore-able failures.
482
+ async fn respond_legacy_query(
483
+ &self,
484
+ tt: TaskToken,
485
+ res: QueryResult,
486
+ ) -> Result<(), tonic::Status> {
487
+ match self.client.respond_legacy_query(tt, res).await {
488
+ Ok(_) => Ok(()),
489
+ Err(e) if e.code() == tonic::Code::NotFound => {
490
+ warn!(error=?e,"Query not found when attempting to respond to it");
491
+ Ok(())
492
+ }
493
+ Err(e) => Err(e),
494
+ }
495
+ }
496
+ }
497
+
498
+ /// Manages access to a specific workflow run, and contains various bookkeeping information that the
499
+ /// [WFStream] may need to access quickly.
500
+ #[derive(Debug)]
501
+ struct ManagedRunHandle {
502
+ /// If set, the WFT this run is currently/will be processing.
503
+ wft: Option<OutstandingTask>,
504
+ /// An outstanding activation to lang
505
+ activation: Option<OutstandingActivation>,
506
+ /// If set, it indicates there is a buffered poll response from the server that applies to this
507
+ /// run. This can happen when lang takes too long to complete a task and the task times out, for
508
+ /// example. Upon next completion, the buffered response will be removed and can be made ready
509
+ /// to be returned from polling
510
+ buffered_resp: Option<PermittedWFT>,
511
+ /// True if this machine has seen an event which ends the execution
512
+ have_seen_terminal_event: bool,
513
+ /// The most recently processed event id this machine has seen. 0 means it has seen nothing.
514
+ most_recently_processed_event_number: usize,
515
+ /// Is set true when the machines indicate that there is additional known work to be processed
516
+ more_pending_work: bool,
517
+ /// Is set if an eviction has been requested for this run
518
+ trying_to_evict: Option<RequestEvictMsg>,
519
+ /// Set to true if the last action we tried to take to this run has been processed (ie: the
520
+ /// [RunUpdateResponse] for it has been seen.
521
+ last_action_acked: bool,
522
+ /// For sending work to the machines
523
+ run_actions_tx: UnboundedSender<RunAction>,
524
+ /// Handle to the task where the actual machines live
525
+ handle: JoinHandle<()>,
526
+ metrics: MetricsContext,
527
+ }
528
+ impl ManagedRunHandle {
529
+ fn new(
530
+ wfm: WorkflowManager,
531
+ activations_tx: UnboundedSender<RunUpdateResponse>,
532
+ local_activity_request_sink: LocalActivityRequestSink,
533
+ metrics: MetricsContext,
534
+ ) -> Self {
535
+ let (run_actions_tx, run_actions_rx) = unbounded_channel();
536
+ let managed = ManagedRun::new(wfm, activations_tx, local_activity_request_sink);
537
+ let handle = tokio::task::spawn(managed.run(run_actions_rx));
538
+ Self {
539
+ wft: None,
540
+ activation: None,
541
+ buffered_resp: None,
542
+ have_seen_terminal_event: false,
543
+ most_recently_processed_event_number: 0,
544
+ more_pending_work: false,
545
+ trying_to_evict: None,
546
+ last_action_acked: true,
547
+ handle,
548
+ metrics,
549
+ run_actions_tx,
550
+ }
551
+ }
552
+
553
+ fn incoming_wft(&mut self, wft: NewIncomingWFT) {
554
+ if self.wft.is_some() {
555
+ error!("Trying to send a new WFT for a run which already has one!");
556
+ }
557
+ self.send_run_action(RunActions::NewIncomingWFT(wft));
558
+ }
559
+ fn check_more_activations(&mut self) {
560
+ // No point in checking for more activations if we have not acked the last update, or
561
+ // if there's already an outstanding activation.
562
+ if self.last_action_acked && self.activation.is_none() {
563
+ self.send_run_action(RunActions::CheckMoreWork {
564
+ want_to_evict: self.trying_to_evict.clone(),
565
+ has_pending_queries: self
566
+ .wft
567
+ .as_ref()
568
+ .map(|wft| !wft.pending_queries.is_empty())
569
+ .unwrap_or_default(),
570
+ });
571
+ }
572
+ }
573
+ fn send_completion(&mut self, c: RunActivationCompletion) {
574
+ self.send_run_action(RunActions::ActivationCompletion(c));
575
+ }
576
+ fn send_local_resolution(&mut self, r: LocalResolution) {
577
+ self.send_run_action(RunActions::LocalResolution(r));
578
+ }
579
+
580
+ fn insert_outstanding_activation(&mut self, act: &ActivationOrAuto) {
581
+ let act_type = match &act {
582
+ ActivationOrAuto::LangActivation(act) | ActivationOrAuto::ReadyForQueries(act) => {
583
+ if act.is_legacy_query() {
584
+ OutstandingActivation::LegacyQuery
585
+ } else {
586
+ OutstandingActivation::Normal {
587
+ contains_eviction: act.eviction_index().is_some(),
588
+ num_jobs: act.jobs.len(),
589
+ }
590
+ }
591
+ }
592
+ ActivationOrAuto::Autocomplete { .. } => OutstandingActivation::Autocomplete,
593
+ };
594
+ if let Some(old_act) = self.activation {
595
+ // This is a panic because we have screwed up core logic if this is violated. It must be
596
+ // upheld.
597
+ panic!(
598
+ "Attempted to insert a new outstanding activation {:?}, but there already was \
599
+ one outstanding: {:?}",
600
+ act, old_act
601
+ );
602
+ }
603
+ self.activation = Some(act_type);
604
+ }
605
+
606
+ fn send_run_action(&mut self, action: RunActions) {
607
+ self.last_action_acked = false;
608
+ self.run_actions_tx
609
+ .send(RunAction {
610
+ action,
611
+ trace_span: Span::current(),
612
+ })
613
+ .expect("Receive half of run actions not dropped");
614
+ }
615
+
616
+ /// Returns true if the managed run has any form of pending work
617
+ /// If `ignore_evicts` is true, pending evictions do not count as pending work.
618
+ /// If `ignore_buffered` is true, buffered workflow tasks do not count as pending work.
619
+ fn has_any_pending_work(&self, ignore_evicts: bool, ignore_buffered: bool) -> bool {
620
+ let evict_work = if ignore_evicts {
621
+ false
622
+ } else {
623
+ self.trying_to_evict.is_some()
624
+ };
625
+ let act_work = if ignore_evicts {
626
+ if let Some(ref act) = self.activation {
627
+ !act.has_only_eviction()
628
+ } else {
629
+ false
630
+ }
631
+ } else {
632
+ self.activation.is_some()
633
+ };
634
+ let buffered = if ignore_buffered {
635
+ false
636
+ } else {
637
+ self.buffered_resp.is_some()
638
+ };
639
+ self.wft.is_some()
640
+ || buffered
641
+ || !self.last_action_acked
642
+ || self.more_pending_work
643
+ || act_work
644
+ || evict_work
645
+ }
646
+
647
+ /// Returns true if the handle is currently processing a WFT which contains a legacy query.
648
+ fn pending_work_is_legacy_query(&self) -> bool {
649
+ // Either we know because there is a pending legacy query, or it's already been drained and
650
+ // sent as an activation.
651
+ matches!(self.activation, Some(OutstandingActivation::LegacyQuery))
652
+ || self
653
+ .wft
654
+ .as_ref()
655
+ .map(|t| t.has_pending_legacy_query())
656
+ .unwrap_or_default()
657
+ }
658
+ }
659
+
660
+ #[derive(Debug, derive_more::Display)]
661
+ enum ActivationOrAuto {
662
+ LangActivation(WorkflowActivation),
663
+ /// This type should only be filled with an empty activation which is ready to have queries
664
+ /// inserted into the joblist
665
+ ReadyForQueries(WorkflowActivation),
666
+ Autocomplete {
667
+ run_id: String,
668
+ },
669
+ }
670
+ impl ActivationOrAuto {
671
+ pub fn run_id(&self) -> &str {
672
+ match self {
673
+ ActivationOrAuto::LangActivation(act) => &act.run_id,
674
+ ActivationOrAuto::Autocomplete { run_id, .. } => run_id,
675
+ ActivationOrAuto::ReadyForQueries(act) => &act.run_id,
676
+ }
677
+ }
678
+ }
679
+
680
+ #[derive(Debug)]
681
+ pub(crate) struct PermittedWFT {
682
+ wft: ValidPollWFTQResponse,
683
+ permit: OwnedMeteredSemPermit,
684
+ }
685
+
686
+ #[derive(Debug)]
687
+ pub(crate) struct OutstandingTask {
688
+ pub info: WorkflowTaskInfo,
689
+ pub hit_cache: bool,
690
+ /// Set if the outstanding task has quer(ies) which must be fulfilled upon finishing replay
691
+ pub pending_queries: Vec<QueryWorkflow>,
692
+ pub start_time: Instant,
693
+ /// The WFT permit owned by this task, ensures we don't exceed max concurrent WFT, and makes
694
+ /// sure the permit is automatically freed when we delete the task.
695
+ pub permit: OwnedMeteredSemPermit,
696
+ }
697
+
698
+ impl OutstandingTask {
699
+ pub fn has_pending_legacy_query(&self) -> bool {
700
+ self.pending_queries
701
+ .iter()
702
+ .any(|q| q.query_id == LEGACY_QUERY_ID)
703
+ }
704
+ }
705
+
706
+ #[derive(Copy, Clone, Debug)]
707
+ pub(crate) enum OutstandingActivation {
708
+ /// A normal activation with a joblist
709
+ Normal {
710
+ /// True if there is an eviction in the joblist
711
+ contains_eviction: bool,
712
+ /// Number of jobs in the activation
713
+ num_jobs: usize,
714
+ },
715
+ /// An activation for a legacy query
716
+ LegacyQuery,
717
+ /// A fake activation which is never sent to lang, but used internally
718
+ Autocomplete,
719
+ }
720
+
721
+ impl OutstandingActivation {
722
+ pub(crate) const fn has_only_eviction(self) -> bool {
723
+ matches!(
724
+ self,
725
+ OutstandingActivation::Normal {
726
+ contains_eviction: true,
727
+ num_jobs: nj
728
+ }
729
+ if nj == 1)
730
+ }
731
+ pub(crate) const fn has_eviction(self) -> bool {
732
+ matches!(
733
+ self,
734
+ OutstandingActivation::Normal {
735
+ contains_eviction: true,
736
+ ..
737
+ }
738
+ )
739
+ }
740
+ }
741
+
742
+ /// Contains important information about a given workflow task that we need to memorize while
743
+ /// lang handles it.
744
+ #[derive(Clone, Debug)]
745
+ pub struct WorkflowTaskInfo {
746
+ pub task_token: TaskToken,
747
+ pub attempt: u32,
748
+ }
749
+
750
+ #[derive(Debug)]
751
+ pub enum FailedActivationWFTReport {
752
+ Report(TaskToken, WorkflowTaskFailedCause, Failure),
753
+ ReportLegacyQueryFailure(TaskToken, Failure),
754
+ }
755
+
756
+ #[derive(Debug)]
757
+ pub(crate) struct ServerCommandsWithWorkflowInfo {
758
+ pub task_token: TaskToken,
759
+ pub action: ActivationAction,
760
+ }
761
+
762
+ #[derive(Debug)]
763
+ pub(crate) enum ActivationAction {
764
+ /// We should respond that the workflow task is complete
765
+ WftComplete {
766
+ commands: Vec<ProtoCommand>,
767
+ query_responses: Vec<QueryResult>,
768
+ force_new_wft: bool,
769
+ },
770
+ /// We should respond to a legacy query request
771
+ RespondLegacyQuery { result: QueryResult },
772
+ }
773
+
774
+ #[derive(Debug, Eq, PartialEq, Hash)]
775
+ pub(crate) enum EvictionRequestResult {
776
+ EvictionRequested(Option<u32>),
777
+ NotFound,
778
+ EvictionAlreadyRequested(Option<u32>),
779
+ }
780
+
781
+ #[derive(Debug)]
782
+ #[allow(dead_code)] // Not always used in non-test
783
+ pub(crate) struct WorkflowStateInfo {
784
+ pub cached_workflows: usize,
785
+ pub outstanding_wft: usize,
786
+ pub available_wft_permits: usize,
787
+ }
788
+
789
+ #[derive(Debug)]
790
+ struct WFActCompleteMsg {
791
+ completion: ValidatedCompletion,
792
+ response_tx: oneshot::Sender<ActivationCompleteResult>,
793
+ }
794
+ #[derive(Debug)]
795
+ struct LocalResolutionMsg {
796
+ run_id: String,
797
+ res: LocalResolution,
798
+ }
799
+ #[derive(Debug)]
800
+ struct PostActivationMsg {
801
+ run_id: String,
802
+ reported_wft_to_server: bool,
803
+ wft_from_complete: Option<ValidPollWFTQResponse>,
804
+ }
805
+ #[derive(Debug, Clone)]
806
+ struct RequestEvictMsg {
807
+ run_id: String,
808
+ message: String,
809
+ reason: EvictionReason,
810
+ }
811
+ #[derive(Debug)]
812
+ struct GetStateInfoMsg {
813
+ response_tx: oneshot::Sender<WorkflowStateInfo>,
814
+ }
815
+
816
+ /// Each activation completion produces one of these
817
+ #[derive(Debug)]
818
+ struct ActivationCompleteResult {
819
+ most_recently_processed_event: usize,
820
+ outcome: ActivationCompleteOutcome,
821
+ }
822
+ /// What needs to be done after calling [Workflows::activation_completed]
823
+ #[derive(Debug)]
824
+ enum ActivationCompleteOutcome {
825
+ /// The WFT must be reported as successful to the server using the contained information.
826
+ ReportWFTSuccess(ServerCommandsWithWorkflowInfo),
827
+ /// The WFT must be reported as failed to the server using the contained information.
828
+ ReportWFTFail(FailedActivationWFTReport),
829
+ /// There's nothing to do right now. EX: The workflow needs to keep replaying.
830
+ DoNothing,
831
+ }
832
+ #[derive(Debug)]
833
+ struct FulfillableActivationComplete {
834
+ result: ActivationCompleteResult,
835
+ resp_chan: oneshot::Sender<ActivationCompleteResult>,
836
+ }
837
+ impl FulfillableActivationComplete {
838
+ fn fulfill(self) {
839
+ let _ = self.resp_chan.send(self.result);
840
+ }
841
+ }
842
+
843
+ fn validate_completion(
844
+ completion: WorkflowActivationCompletion,
845
+ ) -> Result<ValidatedCompletion, CompleteWfError> {
846
+ match completion.status {
847
+ Some(workflow_activation_completion::Status::Successful(success)) => {
848
+ // Convert to wf commands
849
+ let commands = success
850
+ .commands
851
+ .into_iter()
852
+ .map(|c| c.try_into())
853
+ .collect::<Result<Vec<_>, EmptyWorkflowCommandErr>>()
854
+ .map_err(|_| CompleteWfError::MalformedWorkflowCompletion {
855
+ reason: "At least one workflow command in the completion contained \
856
+ an empty variant"
857
+ .to_owned(),
858
+ run_id: completion.run_id.clone(),
859
+ })?;
860
+
861
+ if commands.len() > 1
862
+ && commands.iter().any(
863
+ |c| matches!(c, WFCommand::QueryResponse(q) if q.query_id == LEGACY_QUERY_ID),
864
+ )
865
+ {
866
+ return Err(CompleteWfError::MalformedWorkflowCompletion {
867
+ reason: "Workflow completion had a legacy query response along with other \
868
+ commands. This is not allowed and constitutes an error in the \
869
+ lang SDK"
870
+ .to_owned(),
871
+ run_id: completion.run_id,
872
+ });
873
+ }
874
+
875
+ Ok(ValidatedCompletion::Success {
876
+ run_id: completion.run_id,
877
+ commands,
878
+ })
879
+ }
880
+ Some(workflow_activation_completion::Status::Failed(failure)) => {
881
+ Ok(ValidatedCompletion::Fail {
882
+ run_id: completion.run_id,
883
+ failure,
884
+ })
885
+ }
886
+ None => Err(CompleteWfError::MalformedWorkflowCompletion {
887
+ reason: "Workflow completion had empty status field".to_owned(),
888
+ run_id: completion.run_id,
889
+ }),
890
+ }
891
+ }
892
+
893
+ #[derive(Debug)]
894
+ enum ValidatedCompletion {
895
+ Success {
896
+ run_id: String,
897
+ commands: Vec<WFCommand>,
898
+ },
899
+ Fail {
900
+ run_id: String,
901
+ failure: Failure,
902
+ },
903
+ }
904
+
905
+ impl ValidatedCompletion {
906
+ pub fn run_id(&self) -> &str {
907
+ match self {
908
+ ValidatedCompletion::Success { run_id, .. } => run_id,
909
+ ValidatedCompletion::Fail { run_id, .. } => run_id,
910
+ }
911
+ }
912
+ }
913
+
914
+ /// Input to run tasks, sent to [ManagedRun]s via [ManagedRunHandle]s
915
+ #[derive(Debug)]
916
+ struct RunAction {
917
+ action: RunActions,
918
+ trace_span: Span,
919
+ }
920
+ #[derive(Debug)]
921
+ enum RunActions {
922
+ NewIncomingWFT(NewIncomingWFT),
923
+ ActivationCompletion(RunActivationCompletion),
924
+ CheckMoreWork {
925
+ want_to_evict: Option<RequestEvictMsg>,
926
+ has_pending_queries: bool,
927
+ },
928
+ LocalResolution(LocalResolution),
929
+ HeartbeatTimeout,
930
+ }
931
+ #[derive(Debug)]
932
+ struct NewIncomingWFT {
933
+ /// This field is only populated if the machines already exist. Otherwise the machines
934
+ /// are instantiated with the workflow history.
935
+ history_update: Option<HistoryUpdate>,
936
+ /// Wft start time
937
+ start_time: Instant,
938
+ }
939
+ #[derive(Debug)]
940
+ struct RunActivationCompletion {
941
+ task_token: TaskToken,
942
+ start_time: Instant,
943
+ commands: Vec<WFCommand>,
944
+ activation_was_eviction: bool,
945
+ activation_was_only_eviction: bool,
946
+ has_pending_query: bool,
947
+ query_responses: Vec<QueryResult>,
948
+ /// Used to notify the worker when the completion is done processing and the completion can
949
+ /// unblock. Must always be `Some` when initialized.
950
+ resp_chan: Option<oneshot::Sender<ActivationCompleteResult>>,
951
+ }
952
+
953
+ /// A response from a [ManagedRun] held by a [ManagedRunHandle]
954
+ #[derive(Debug)]
955
+ struct RunUpdateResponse {
956
+ kind: RunUpdateResponseKind,
957
+ span: Span,
958
+ }
959
+ #[derive(Debug, derive_more::Display)]
960
+ enum RunUpdateResponseKind {
961
+ Good(GoodRunUpdate),
962
+ Fail(FailRunUpdate),
963
+ }
964
+
965
+ #[derive(Debug)]
966
+ struct GoodRunUpdate {
967
+ run_id: String,
968
+ outgoing_activation: Option<ActivationOrAuto>,
969
+ fulfillable_complete: Option<FulfillableActivationComplete>,
970
+ have_seen_terminal_event: bool,
971
+ /// Is true if there are more jobs that need to be sent to lang
972
+ more_pending_work: bool,
973
+ most_recently_processed_event_number: usize,
974
+ /// Is true if this update was in response to a new WFT
975
+ in_response_to_wft: bool,
976
+ }
977
+ impl Display for GoodRunUpdate {
978
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
979
+ write!(
980
+ f,
981
+ "GoodRunUpdate(run_id: {}, outgoing_activation: {}, more_pending_work: {})",
982
+ self.run_id,
983
+ if let Some(og) = self.outgoing_activation.as_ref() {
984
+ format!("{}", og)
985
+ } else {
986
+ "None".to_string()
987
+ },
988
+ self.more_pending_work
989
+ )
990
+ }
991
+ }
992
+ #[derive(Debug)]
993
+ pub(crate) struct FailRunUpdate {
994
+ run_id: String,
995
+ err: WFMachinesError,
996
+ /// This is populated if the run update failed while processing a completion - and thus we
997
+ /// must respond down it when handling the failure.
998
+ completion_resp: Option<oneshot::Sender<ActivationCompleteResult>>,
999
+ }
1000
+ impl Display for FailRunUpdate {
1001
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1002
+ write!(
1003
+ f,
1004
+ "FailRunUpdate(run_id: {}, error: {:?})",
1005
+ self.run_id, self.err
1006
+ )
1007
+ }
1008
+ }
1009
+ #[derive(Debug)]
1010
+ pub struct OutgoingServerCommands {
1011
+ pub commands: Vec<ProtoCommand>,
1012
+ pub replaying: bool,
1013
+ }
1014
+
1015
+ #[derive(Debug)]
1016
+ pub(crate) enum LocalResolution {
1017
+ LocalActivity(LocalActivityResolution),
1018
+ }
1019
+
1020
+ #[derive(thiserror::Error, Debug, derive_more::From)]
1021
+ #[error("Lang provided workflow command with empty variant")]
1022
+ pub struct EmptyWorkflowCommandErr;
1023
+
1024
+ /// [DrivenWorkflow]s respond with these when called, to indicate what they want to do next.
1025
+ /// EX: Create a new timer, complete the workflow, etc.
1026
+ #[derive(Debug, derive_more::From, derive_more::Display)]
1027
+ #[allow(clippy::large_enum_variant)]
1028
+ pub enum WFCommand {
1029
+ /// Returned when we need to wait for the lang sdk to send us something
1030
+ NoCommandsFromLang,
1031
+ AddActivity(ScheduleActivity),
1032
+ AddLocalActivity(ScheduleLocalActivity),
1033
+ RequestCancelActivity(RequestCancelActivity),
1034
+ RequestCancelLocalActivity(RequestCancelLocalActivity),
1035
+ AddTimer(StartTimer),
1036
+ CancelTimer(CancelTimer),
1037
+ CompleteWorkflow(CompleteWorkflowExecution),
1038
+ FailWorkflow(FailWorkflowExecution),
1039
+ QueryResponse(QueryResult),
1040
+ ContinueAsNew(ContinueAsNewWorkflowExecution),
1041
+ CancelWorkflow(CancelWorkflowExecution),
1042
+ SetPatchMarker(SetPatchMarker),
1043
+ AddChildWorkflow(StartChildWorkflowExecution),
1044
+ CancelUnstartedChild(CancelUnstartedChildWorkflowExecution),
1045
+ RequestCancelExternalWorkflow(RequestCancelExternalWorkflowExecution),
1046
+ SignalExternalWorkflow(SignalExternalWorkflowExecution),
1047
+ CancelSignalWorkflow(CancelSignalWorkflow),
1048
+ UpsertSearchAttributes(UpsertWorkflowSearchAttributes),
1049
+ }
1050
+
1051
+ impl TryFrom<WorkflowCommand> for WFCommand {
1052
+ type Error = EmptyWorkflowCommandErr;
1053
+
1054
+ fn try_from(c: WorkflowCommand) -> result::Result<Self, Self::Error> {
1055
+ match c.variant.ok_or(EmptyWorkflowCommandErr)? {
1056
+ workflow_command::Variant::StartTimer(s) => Ok(Self::AddTimer(s)),
1057
+ workflow_command::Variant::CancelTimer(s) => Ok(Self::CancelTimer(s)),
1058
+ workflow_command::Variant::ScheduleActivity(s) => Ok(Self::AddActivity(s)),
1059
+ workflow_command::Variant::RequestCancelActivity(s) => {
1060
+ Ok(Self::RequestCancelActivity(s))
1061
+ }
1062
+ workflow_command::Variant::CompleteWorkflowExecution(c) => {
1063
+ Ok(Self::CompleteWorkflow(c))
1064
+ }
1065
+ workflow_command::Variant::FailWorkflowExecution(s) => Ok(Self::FailWorkflow(s)),
1066
+ workflow_command::Variant::RespondToQuery(s) => Ok(Self::QueryResponse(s)),
1067
+ workflow_command::Variant::ContinueAsNewWorkflowExecution(s) => {
1068
+ Ok(Self::ContinueAsNew(s))
1069
+ }
1070
+ workflow_command::Variant::CancelWorkflowExecution(s) => Ok(Self::CancelWorkflow(s)),
1071
+ workflow_command::Variant::SetPatchMarker(s) => Ok(Self::SetPatchMarker(s)),
1072
+ workflow_command::Variant::StartChildWorkflowExecution(s) => {
1073
+ Ok(Self::AddChildWorkflow(s))
1074
+ }
1075
+ workflow_command::Variant::RequestCancelExternalWorkflowExecution(s) => {
1076
+ Ok(Self::RequestCancelExternalWorkflow(s))
1077
+ }
1078
+ workflow_command::Variant::SignalExternalWorkflowExecution(s) => {
1079
+ Ok(Self::SignalExternalWorkflow(s))
1080
+ }
1081
+ workflow_command::Variant::CancelSignalWorkflow(s) => Ok(Self::CancelSignalWorkflow(s)),
1082
+ workflow_command::Variant::CancelUnstartedChildWorkflowExecution(s) => {
1083
+ Ok(Self::CancelUnstartedChild(s))
1084
+ }
1085
+ workflow_command::Variant::ScheduleLocalActivity(s) => Ok(Self::AddLocalActivity(s)),
1086
+ workflow_command::Variant::RequestCancelLocalActivity(s) => {
1087
+ Ok(Self::RequestCancelLocalActivity(s))
1088
+ }
1089
+ workflow_command::Variant::UpsertWorkflowSearchAttributes(s) => {
1090
+ Ok(Self::UpsertSearchAttributes(s))
1091
+ }
1092
+ }
1093
+ }
1094
+ }
1095
+
1096
+ #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
1097
+ enum CommandID {
1098
+ Timer(u32),
1099
+ Activity(u32),
1100
+ LocalActivity(u32),
1101
+ ChildWorkflowStart(u32),
1102
+ SignalExternal(u32),
1103
+ CancelExternal(u32),
1104
+ }
1105
+
1106
+ /// Details remembered from the workflow execution started event that we may need to recall later.
1107
+ /// Is a subset of `WorkflowExecutionStartedEventAttributes`, but avoids holding on to huge fields.
1108
+ #[derive(Debug, Clone)]
1109
+ pub struct WorkflowStartedInfo {
1110
+ workflow_task_timeout: Option<Duration>,
1111
+ workflow_execution_timeout: Option<Duration>,
1112
+ }
1113
+
1114
+ type LocalActivityRequestSink =
1115
+ Arc<dyn Fn(Vec<LocalActRequest>) -> Vec<LocalActivityResolution> + Send + Sync>;