@temporalio/core-bridge 1.6.0 → 1.7.1

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 (138) hide show
  1. package/Cargo.lock +520 -456
  2. package/lib/index.d.ts +8 -6
  3. package/lib/index.js.map +1 -1
  4. package/package.json +8 -3
  5. package/releases/aarch64-apple-darwin/index.node +0 -0
  6. package/releases/aarch64-unknown-linux-gnu/index.node +0 -0
  7. package/releases/x86_64-apple-darwin/index.node +0 -0
  8. package/releases/x86_64-pc-windows-msvc/index.node +0 -0
  9. package/releases/x86_64-unknown-linux-gnu/index.node +0 -0
  10. package/sdk-core/.buildkite/docker/Dockerfile +2 -2
  11. package/sdk-core/.buildkite/docker/docker-compose.yaml +1 -1
  12. package/sdk-core/.buildkite/pipeline.yml +1 -1
  13. package/sdk-core/.github/workflows/heavy.yml +1 -0
  14. package/sdk-core/README.md +13 -7
  15. package/sdk-core/client/src/lib.rs +27 -9
  16. package/sdk-core/client/src/metrics.rs +17 -8
  17. package/sdk-core/client/src/raw.rs +3 -3
  18. package/sdk-core/core/Cargo.toml +3 -4
  19. package/sdk-core/core/src/abstractions/take_cell.rs +28 -0
  20. package/sdk-core/core/src/abstractions.rs +197 -18
  21. package/sdk-core/core/src/core_tests/activity_tasks.rs +137 -45
  22. package/sdk-core/core/src/core_tests/child_workflows.rs +6 -5
  23. package/sdk-core/core/src/core_tests/determinism.rs +212 -2
  24. package/sdk-core/core/src/core_tests/local_activities.rs +183 -36
  25. package/sdk-core/core/src/core_tests/queries.rs +32 -14
  26. package/sdk-core/core/src/core_tests/workers.rs +8 -5
  27. package/sdk-core/core/src/core_tests/workflow_tasks.rs +340 -51
  28. package/sdk-core/core/src/ephemeral_server/mod.rs +110 -8
  29. package/sdk-core/core/src/internal_flags.rs +141 -0
  30. package/sdk-core/core/src/lib.rs +14 -9
  31. package/sdk-core/core/src/replay/mod.rs +16 -27
  32. package/sdk-core/core/src/telemetry/metrics.rs +69 -35
  33. package/sdk-core/core/src/telemetry/mod.rs +38 -14
  34. package/sdk-core/core/src/telemetry/prometheus_server.rs +19 -13
  35. package/sdk-core/core/src/test_help/mod.rs +65 -13
  36. package/sdk-core/core/src/worker/activities/activity_heartbeat_manager.rs +119 -160
  37. package/sdk-core/core/src/worker/activities/activity_task_poller_stream.rs +89 -0
  38. package/sdk-core/core/src/worker/activities/local_activities.rs +122 -6
  39. package/sdk-core/core/src/worker/activities.rs +347 -173
  40. package/sdk-core/core/src/worker/client/mocks.rs +22 -2
  41. package/sdk-core/core/src/worker/client.rs +18 -2
  42. package/sdk-core/core/src/worker/mod.rs +137 -44
  43. package/sdk-core/core/src/worker/workflow/history_update.rs +132 -51
  44. package/sdk-core/core/src/worker/workflow/machines/activity_state_machine.rs +207 -166
  45. package/sdk-core/core/src/worker/workflow/machines/cancel_external_state_machine.rs +6 -7
  46. package/sdk-core/core/src/worker/workflow/machines/cancel_workflow_state_machine.rs +6 -7
  47. package/sdk-core/core/src/worker/workflow/machines/child_workflow_state_machine.rs +157 -82
  48. package/sdk-core/core/src/worker/workflow/machines/complete_workflow_state_machine.rs +12 -12
  49. package/sdk-core/core/src/worker/workflow/machines/continue_as_new_workflow_state_machine.rs +6 -7
  50. package/sdk-core/core/src/worker/workflow/machines/fail_workflow_state_machine.rs +13 -15
  51. package/sdk-core/core/src/worker/workflow/machines/local_activity_state_machine.rs +170 -60
  52. package/sdk-core/core/src/worker/workflow/machines/mod.rs +24 -16
  53. package/sdk-core/core/src/worker/workflow/machines/modify_workflow_properties_state_machine.rs +6 -8
  54. package/sdk-core/core/src/worker/workflow/machines/patch_state_machine.rs +320 -204
  55. package/sdk-core/core/src/worker/workflow/machines/signal_external_state_machine.rs +10 -13
  56. package/sdk-core/core/src/worker/workflow/machines/timer_state_machine.rs +15 -23
  57. package/sdk-core/core/src/worker/workflow/machines/upsert_search_attributes_state_machine.rs +187 -46
  58. package/sdk-core/core/src/worker/workflow/machines/workflow_machines.rs +237 -111
  59. package/sdk-core/core/src/worker/workflow/machines/workflow_task_state_machine.rs +13 -13
  60. package/sdk-core/core/src/worker/workflow/managed_run/managed_wf_test.rs +10 -6
  61. package/sdk-core/core/src/worker/workflow/managed_run.rs +81 -62
  62. package/sdk-core/core/src/worker/workflow/mod.rs +341 -79
  63. package/sdk-core/core/src/worker/workflow/run_cache.rs +18 -11
  64. package/sdk-core/core/src/worker/workflow/wft_extraction.rs +15 -3
  65. package/sdk-core/core/src/worker/workflow/workflow_stream/saved_wf_inputs.rs +2 -0
  66. package/sdk-core/core/src/worker/workflow/workflow_stream.rs +75 -52
  67. package/sdk-core/core-api/Cargo.toml +0 -1
  68. package/sdk-core/core-api/src/lib.rs +13 -7
  69. package/sdk-core/core-api/src/telemetry.rs +4 -6
  70. package/sdk-core/core-api/src/worker.rs +5 -0
  71. package/sdk-core/fsm/rustfsm_procmacro/src/lib.rs +80 -55
  72. package/sdk-core/fsm/rustfsm_trait/src/lib.rs +22 -68
  73. package/sdk-core/histories/ends_empty_wft_complete.bin +0 -0
  74. package/sdk-core/histories/old_change_marker_format.bin +0 -0
  75. package/sdk-core/protos/api_upstream/.github/CODEOWNERS +2 -1
  76. package/sdk-core/protos/api_upstream/Makefile +1 -1
  77. package/sdk-core/protos/api_upstream/temporal/api/command/v1/message.proto +5 -17
  78. package/sdk-core/protos/api_upstream/temporal/api/common/v1/message.proto +11 -0
  79. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/command_type.proto +1 -6
  80. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/event_type.proto +6 -6
  81. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/failed_cause.proto +5 -0
  82. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/update.proto +22 -6
  83. package/sdk-core/protos/api_upstream/temporal/api/history/v1/message.proto +48 -19
  84. package/sdk-core/protos/api_upstream/temporal/api/namespace/v1/message.proto +2 -0
  85. package/sdk-core/protos/api_upstream/temporal/api/operatorservice/v1/request_response.proto +3 -0
  86. package/sdk-core/protos/api_upstream/temporal/api/{enums/v1/interaction_type.proto → protocol/v1/message.proto} +29 -11
  87. package/sdk-core/protos/api_upstream/temporal/api/sdk/v1/task_complete_metadata.proto +63 -0
  88. package/sdk-core/protos/api_upstream/temporal/api/update/v1/message.proto +111 -0
  89. package/sdk-core/protos/api_upstream/temporal/api/workflowservice/v1/request_response.proto +59 -28
  90. package/sdk-core/protos/api_upstream/temporal/api/workflowservice/v1/service.proto +2 -2
  91. package/sdk-core/protos/local/temporal/sdk/core/activity_result/activity_result.proto +7 -8
  92. package/sdk-core/protos/local/temporal/sdk/core/activity_task/activity_task.proto +10 -7
  93. package/sdk-core/protos/local/temporal/sdk/core/child_workflow/child_workflow.proto +19 -30
  94. package/sdk-core/protos/local/temporal/sdk/core/common/common.proto +1 -0
  95. package/sdk-core/protos/local/temporal/sdk/core/core_interface.proto +1 -0
  96. package/sdk-core/protos/local/temporal/sdk/core/external_data/external_data.proto +8 -0
  97. package/sdk-core/protos/local/temporal/sdk/core/workflow_activation/workflow_activation.proto +65 -60
  98. package/sdk-core/protos/local/temporal/sdk/core/workflow_commands/workflow_commands.proto +85 -84
  99. package/sdk-core/protos/local/temporal/sdk/core/workflow_completion/workflow_completion.proto +9 -3
  100. package/sdk-core/sdk/Cargo.toml +1 -1
  101. package/sdk-core/sdk/src/lib.rs +21 -5
  102. package/sdk-core/sdk/src/workflow_context/options.rs +7 -1
  103. package/sdk-core/sdk/src/workflow_context.rs +24 -17
  104. package/sdk-core/sdk/src/workflow_future.rs +9 -3
  105. package/sdk-core/sdk-core-protos/src/history_builder.rs +114 -89
  106. package/sdk-core/sdk-core-protos/src/history_info.rs +6 -1
  107. package/sdk-core/sdk-core-protos/src/lib.rs +205 -64
  108. package/sdk-core/test-utils/src/canned_histories.rs +106 -296
  109. package/sdk-core/test-utils/src/lib.rs +32 -5
  110. package/sdk-core/tests/heavy_tests.rs +10 -43
  111. package/sdk-core/tests/integ_tests/ephemeral_server_tests.rs +25 -3
  112. package/sdk-core/tests/integ_tests/heartbeat_tests.rs +5 -3
  113. package/sdk-core/tests/integ_tests/metrics_tests.rs +218 -16
  114. package/sdk-core/tests/integ_tests/polling_tests.rs +3 -8
  115. package/sdk-core/tests/integ_tests/queries_tests.rs +4 -2
  116. package/sdk-core/tests/integ_tests/visibility_tests.rs +34 -23
  117. package/sdk-core/tests/integ_tests/workflow_tests/activities.rs +97 -81
  118. package/sdk-core/tests/integ_tests/workflow_tests/cancel_external.rs +1 -0
  119. package/sdk-core/tests/integ_tests/workflow_tests/cancel_wf.rs +1 -0
  120. package/sdk-core/tests/integ_tests/workflow_tests/child_workflows.rs +80 -3
  121. package/sdk-core/tests/integ_tests/workflow_tests/continue_as_new.rs +5 -1
  122. package/sdk-core/tests/integ_tests/workflow_tests/determinism.rs +1 -0
  123. package/sdk-core/tests/integ_tests/workflow_tests/local_activities.rs +25 -3
  124. package/sdk-core/tests/integ_tests/workflow_tests/modify_wf_properties.rs +2 -4
  125. package/sdk-core/tests/integ_tests/workflow_tests/patches.rs +30 -0
  126. package/sdk-core/tests/integ_tests/workflow_tests/replay.rs +64 -0
  127. package/sdk-core/tests/integ_tests/workflow_tests/resets.rs +1 -0
  128. package/sdk-core/tests/integ_tests/workflow_tests/signals.rs +4 -0
  129. package/sdk-core/tests/integ_tests/workflow_tests/stickyness.rs +3 -1
  130. package/sdk-core/tests/integ_tests/workflow_tests/timers.rs +7 -2
  131. package/sdk-core/tests/integ_tests/workflow_tests/upsert_search_attrs.rs +6 -7
  132. package/sdk-core/tests/integ_tests/workflow_tests.rs +8 -8
  133. package/sdk-core/tests/main.rs +16 -25
  134. package/sdk-core/tests/runner.rs +11 -9
  135. package/src/conversions.rs +14 -8
  136. package/src/runtime.rs +9 -8
  137. package/ts/index.ts +8 -6
  138. package/sdk-core/protos/api_upstream/temporal/api/interaction/v1/message.proto +0 -87
@@ -2,7 +2,8 @@ use super::{
2
2
  workflow_machines::MachineResponse, Cancellable, EventInfo, NewMachineWithCommand,
3
3
  OnEventWrapper, WFMachinesAdapter, WFMachinesError,
4
4
  };
5
- use rustfsm::{fsm, TransitionResult};
5
+ use crate::worker::workflow::machines::HistEventData;
6
+ use rustfsm::{fsm, StateMachine, TransitionResult};
6
7
  use std::convert::TryFrom;
7
8
  use temporal_sdk_core_protos::{
8
9
  coresdk::{
@@ -60,10 +61,7 @@ pub(super) fn new_external_cancel(
60
61
  only_child: bool,
61
62
  reason: String,
62
63
  ) -> NewMachineWithCommand {
63
- let mut s = CancelExternalMachine {
64
- state: Created {}.into(),
65
- shared_state: SharedState { seq },
66
- };
64
+ let mut s = CancelExternalMachine::from_parts(Created {}.into(), SharedState { seq });
67
65
  OnEventWrapper::on_event_mut(&mut s, CancelExternalMachineEvents::Schedule)
68
66
  .expect("Scheduling cancel external wf command doesn't fail");
69
67
  let cmd_attrs = command::Attributes::RequestCancelExternalWorkflowExecutionCommandAttributes(
@@ -145,10 +143,11 @@ impl TryFrom<CommandType> for CancelExternalMachineEvents {
145
143
  }
146
144
  }
147
145
 
148
- impl TryFrom<HistoryEvent> for CancelExternalMachineEvents {
146
+ impl TryFrom<HistEventData> for CancelExternalMachineEvents {
149
147
  type Error = WFMachinesError;
150
148
 
151
- fn try_from(e: HistoryEvent) -> Result<Self, Self::Error> {
149
+ fn try_from(e: HistEventData) -> Result<Self, Self::Error> {
150
+ let e = e.event;
152
151
  Ok(match e.event_type() {
153
152
  EventType::ExternalWorkflowExecutionCancelRequested => {
154
153
  Self::ExternalWorkflowExecutionCancelRequested
@@ -2,7 +2,8 @@ use super::{
2
2
  workflow_machines::MachineResponse, Cancellable, EventInfo, HistoryEvent,
3
3
  NewMachineWithCommand, OnEventWrapper, WFMachinesAdapter, WFMachinesError,
4
4
  };
5
- use rustfsm::{fsm, TransitionResult};
5
+ use crate::worker::workflow::machines::HistEventData;
6
+ use rustfsm::{fsm, StateMachine, TransitionResult};
6
7
  use std::convert::TryFrom;
7
8
  use temporal_sdk_core_protos::{
8
9
  coresdk::workflow_commands::CancelWorkflowExecution,
@@ -33,10 +34,7 @@ pub(super) enum CancelWorkflowMachineError {}
33
34
  pub(super) enum CancelWorkflowCommand {}
34
35
 
35
36
  pub(super) fn cancel_workflow(attribs: CancelWorkflowExecution) -> NewMachineWithCommand {
36
- let mut machine = CancelWorkflowMachine {
37
- state: Created {}.into(),
38
- shared_state: (),
39
- };
37
+ let mut machine = CancelWorkflowMachine::from_parts(Created {}.into(), ());
40
38
  OnEventWrapper::on_event_mut(&mut machine, CancelWorkflowMachineEvents::Schedule)
41
39
  .expect("Scheduling continue as new machine doesn't fail");
42
40
  let command = Command {
@@ -72,10 +70,11 @@ impl Created {
72
70
  }
73
71
  }
74
72
 
75
- impl TryFrom<HistoryEvent> for CancelWorkflowMachineEvents {
73
+ impl TryFrom<HistEventData> for CancelWorkflowMachineEvents {
76
74
  type Error = WFMachinesError;
77
75
 
78
- fn try_from(e: HistoryEvent) -> Result<Self, Self::Error> {
76
+ fn try_from(e: HistEventData) -> Result<Self, Self::Error> {
77
+ let e = e.event;
79
78
  Ok(match EventType::from_i32(e.event_type) {
80
79
  Some(EventType::WorkflowExecutionCanceled) => Self::WorkflowExecutionCanceled,
81
80
  _ => {
@@ -2,7 +2,11 @@ use super::{
2
2
  workflow_machines::MachineResponse, Cancellable, EventInfo, NewMachineWithCommand,
3
3
  OnEventWrapper, WFMachinesAdapter, WFMachinesError,
4
4
  };
5
- use rustfsm::{fsm, MachineError, TransitionResult};
5
+ use crate::{
6
+ internal_flags::CoreInternalFlags,
7
+ worker::workflow::{machines::HistEventData, InternalFlagsRef},
8
+ };
9
+ use rustfsm::{fsm, MachineError, StateMachine, TransitionResult};
6
10
  use std::convert::{TryFrom, TryInto};
7
11
  use temporal_sdk_core_protos::{
8
12
  coresdk::{
@@ -41,7 +45,7 @@ fsm! {
41
45
 
42
46
  Created --(Schedule, on_schedule) --> StartCommandCreated;
43
47
  StartCommandCreated --(CommandStartChildWorkflowExecution) --> StartCommandCreated;
44
- StartCommandCreated --(StartChildWorkflowExecutionInitiated(i64),
48
+ StartCommandCreated --(StartChildWorkflowExecutionInitiated(ChildWorkflowInitiatedData),
45
49
  shared on_start_child_workflow_execution_initiated) --> StartEventRecorded;
46
50
  StartCommandCreated --(Cancel, shared on_cancelled) --> Cancelled;
47
51
 
@@ -70,6 +74,8 @@ fsm! {
70
74
 
71
75
  // Ignore any spurious cancellations after resolution
72
76
  Cancelled --(Cancel) --> Cancelled;
77
+ Cancelled --(ChildWorkflowExecutionCancelled,
78
+ on_child_workflow_execution_cancelled) --> Cancelled;
73
79
  Failed --(Cancel) --> Failed;
74
80
  StartFailed --(Cancel) --> StartFailed;
75
81
  TimedOut --(Cancel) --> TimedOut;
@@ -99,8 +105,38 @@ pub(super) enum ChildWorkflowCommand {
99
105
  IssueCancelAfterStarted { reason: String },
100
106
  }
101
107
 
108
+ pub(super) struct ChildWorkflowInitiatedData {
109
+ event_id: i64,
110
+ wf_type: String,
111
+ wf_id: String,
112
+ last_task_in_history: bool,
113
+ }
114
+
102
115
  #[derive(Default, Clone)]
103
- pub(super) struct Cancelled {}
116
+ pub(super) struct Cancelled {
117
+ seen_cancelled_event: bool,
118
+ }
119
+
120
+ impl Cancelled {
121
+ pub(super) fn on_child_workflow_execution_cancelled(
122
+ self,
123
+ ) -> ChildWorkflowMachineTransition<Cancelled> {
124
+ if self.seen_cancelled_event {
125
+ ChildWorkflowMachineTransition::Err(WFMachinesError::Fatal(
126
+ "Child workflow has already seen a ChildWorkflowExecutionCanceledEvent, and now \
127
+ another is being applied! This is a bug, please report."
128
+ .to_string(),
129
+ ))
130
+ } else {
131
+ ChildWorkflowMachineTransition::ok(
132
+ [],
133
+ Cancelled {
134
+ seen_cancelled_event: true,
135
+ },
136
+ )
137
+ }
138
+ }
139
+ }
104
140
 
105
141
  #[derive(Default, Clone)]
106
142
  pub(super) struct Completed {}
@@ -123,44 +159,50 @@ pub(super) struct StartCommandCreated {}
123
159
  impl StartCommandCreated {
124
160
  pub(super) fn on_start_child_workflow_execution_initiated(
125
161
  self,
126
- state: SharedState,
127
- initiated_event_id: i64,
162
+ state: &mut SharedState,
163
+ event_dat: ChildWorkflowInitiatedData,
128
164
  ) -> ChildWorkflowMachineTransition<StartEventRecorded> {
129
- ChildWorkflowMachineTransition::ok_shared(
130
- vec![],
131
- StartEventRecorded::default(),
132
- SharedState {
133
- initiated_event_id,
134
- ..state
135
- },
136
- )
165
+ if state.internal_flags.borrow_mut().try_use(
166
+ CoreInternalFlags::IdAndTypeDeterminismChecks,
167
+ event_dat.last_task_in_history,
168
+ ) {
169
+ if event_dat.wf_id != state.workflow_id {
170
+ return TransitionResult::Err(WFMachinesError::Nondeterminism(format!(
171
+ "Child workflow id of scheduled event '{}' does not \
172
+ match child workflow id of activity command '{}'",
173
+ event_dat.wf_id, state.workflow_id
174
+ )));
175
+ }
176
+ if event_dat.wf_type != state.workflow_type {
177
+ return TransitionResult::Err(WFMachinesError::Nondeterminism(format!(
178
+ "Child workflow type of scheduled event '{}' does not \
179
+ match child workflow type of activity command '{}'",
180
+ event_dat.wf_type, state.workflow_type
181
+ )));
182
+ }
183
+ }
184
+ state.initiated_event_id = event_dat.event_id;
185
+ ChildWorkflowMachineTransition::default()
137
186
  }
138
187
 
139
188
  pub(super) fn on_cancelled(
140
189
  self,
141
- state: SharedState,
190
+ state: &mut SharedState,
142
191
  ) -> ChildWorkflowMachineTransition<Cancelled> {
143
- let state = SharedState {
144
- cancelled_before_sent: true,
145
- ..state
146
- };
147
- ChildWorkflowMachineTransition::ok_shared(
148
- vec![ChildWorkflowCommand::StartCancel(Failure {
149
- message: "Child Workflow execution cancelled before scheduled".to_owned(),
150
- cause: Some(Box::new(Failure {
151
- failure_info: Some(FailureInfo::CanceledFailureInfo(
152
- failure::CanceledFailureInfo {
153
- ..Default::default()
154
- },
155
- )),
156
- ..Default::default()
157
- })),
158
- failure_info: failure_info_from_state(&state, RetryState::NonRetryableFailure),
192
+ state.cancelled_before_sent = true;
193
+ ChildWorkflowMachineTransition::commands(vec![ChildWorkflowCommand::StartCancel(Failure {
194
+ message: "Child Workflow execution cancelled before scheduled".to_owned(),
195
+ cause: Some(Box::new(Failure {
196
+ failure_info: Some(FailureInfo::CanceledFailureInfo(
197
+ failure::CanceledFailureInfo {
198
+ ..Default::default()
199
+ },
200
+ )),
159
201
  ..Default::default()
160
- })],
161
- Cancelled::default(),
162
- state,
163
- )
202
+ })),
203
+ failure_info: failure_info_from_state(state, RetryState::NonRetryableFailure),
204
+ ..Default::default()
205
+ })])
164
206
  }
165
207
  }
166
208
 
@@ -170,20 +212,14 @@ pub(super) struct StartEventRecorded {}
170
212
  impl StartEventRecorded {
171
213
  pub(super) fn on_child_workflow_execution_started(
172
214
  self,
173
- state: SharedState,
215
+ state: &mut SharedState,
174
216
  event: ChildWorkflowExecutionStartedEvent,
175
217
  ) -> ChildWorkflowMachineTransition<Started> {
176
- ChildWorkflowMachineTransition::ok_shared(
177
- vec![ChildWorkflowCommand::Start(
178
- event.workflow_execution.clone(),
179
- )],
180
- Started::default(),
181
- SharedState {
182
- started_event_id: event.started_event_id,
183
- run_id: event.workflow_execution.run_id,
184
- ..state
185
- },
186
- )
218
+ state.started_event_id = event.started_event_id;
219
+ state.run_id = event.workflow_execution.run_id.clone();
220
+ ChildWorkflowMachineTransition::commands(vec![ChildWorkflowCommand::Start(
221
+ event.workflow_execution,
222
+ )])
187
223
  }
188
224
  pub(super) fn on_start_child_workflow_execution_failed(
189
225
  self,
@@ -214,13 +250,13 @@ impl Started {
214
250
  }
215
251
  fn on_child_workflow_execution_failed(
216
252
  self,
217
- state: SharedState,
253
+ state: &mut SharedState,
218
254
  attrs: ChildWorkflowExecutionFailedEventAttributes,
219
255
  ) -> ChildWorkflowMachineTransition<Failed> {
220
256
  ChildWorkflowMachineTransition::ok(
221
257
  vec![ChildWorkflowCommand::Fail(Failure {
222
258
  message: "Child Workflow execution failed".to_owned(),
223
- failure_info: failure_info_from_state(&state, attrs.retry_state()),
259
+ failure_info: failure_info_from_state(state, attrs.retry_state()),
224
260
  cause: attrs.failure.map(Box::new),
225
261
  ..Default::default()
226
262
  })],
@@ -229,7 +265,7 @@ impl Started {
229
265
  }
230
266
  fn on_child_workflow_execution_timed_out(
231
267
  self,
232
- state: SharedState,
268
+ state: &mut SharedState,
233
269
  retry_state: RetryState,
234
270
  ) -> ChildWorkflowMachineTransition<TimedOut> {
235
271
  ChildWorkflowMachineTransition::ok(
@@ -245,18 +281,23 @@ impl Started {
245
281
  )),
246
282
  ..Default::default()
247
283
  })),
248
- failure_info: failure_info_from_state(&state, retry_state),
284
+ failure_info: failure_info_from_state(state, retry_state),
249
285
  ..Default::default()
250
286
  })],
251
287
  TimedOut::default(),
252
288
  )
253
289
  }
254
290
  fn on_child_workflow_execution_cancelled(self) -> ChildWorkflowMachineTransition<Cancelled> {
255
- ChildWorkflowMachineTransition::ok(vec![ChildWorkflowCommand::Cancel], Cancelled::default())
291
+ ChildWorkflowMachineTransition::ok(
292
+ vec![ChildWorkflowCommand::Cancel],
293
+ Cancelled {
294
+ seen_cancelled_event: true,
295
+ },
296
+ )
256
297
  }
257
298
  fn on_child_workflow_execution_terminated(
258
299
  self,
259
- state: SharedState,
300
+ state: &mut SharedState,
260
301
  ) -> ChildWorkflowMachineTransition<Terminated> {
261
302
  ChildWorkflowMachineTransition::ok(
262
303
  vec![ChildWorkflowCommand::Fail(Failure {
@@ -268,7 +309,7 @@ impl Started {
268
309
  )),
269
310
  ..Default::default()
270
311
  })),
271
- failure_info: failure_info_from_state(&state, RetryState::NonRetryableFailure),
312
+ failure_info: failure_info_from_state(state, RetryState::NonRetryableFailure),
272
313
  ..Default::default()
273
314
  })],
274
315
  Terminated::default(),
@@ -276,7 +317,7 @@ impl Started {
276
317
  }
277
318
  fn on_cancelled(
278
319
  self,
279
- state: SharedState,
320
+ state: &mut SharedState,
280
321
  ) -> ChildWorkflowMachineTransition<StartedOrCancelled> {
281
322
  let dest = match state.cancel_type {
282
323
  ChildWorkflowCancellationType::Abandon | ChildWorkflowCancellationType::TryCancel => {
@@ -299,7 +340,7 @@ pub(super) struct Terminated {}
299
340
  #[derive(Default, Clone)]
300
341
  pub(super) struct TimedOut {}
301
342
 
302
- #[derive(Default, Clone, Debug)]
343
+ #[derive(Clone, Debug)]
303
344
  pub(super) struct SharedState {
304
345
  initiated_event_id: i64,
305
346
  started_event_id: i64,
@@ -310,38 +351,40 @@ pub(super) struct SharedState {
310
351
  workflow_type: String,
311
352
  cancelled_before_sent: bool,
312
353
  cancel_type: ChildWorkflowCancellationType,
313
- }
314
-
315
- /// Creates a new child workflow state machine and a command to start it on the server.
316
- pub(super) fn new_child_workflow(attribs: StartChildWorkflowExecution) -> NewMachineWithCommand {
317
- let (wf, add_cmd) = ChildWorkflowMachine::new_scheduled(attribs);
318
- NewMachineWithCommand {
319
- command: add_cmd,
320
- machine: wf.into(),
321
- }
354
+ internal_flags: InternalFlagsRef,
322
355
  }
323
356
 
324
357
  impl ChildWorkflowMachine {
325
358
  /// Create a new child workflow and immediately schedule it.
326
- pub(crate) fn new_scheduled(attribs: StartChildWorkflowExecution) -> (Self, Command) {
327
- let mut s = Self {
328
- state: Created {}.into(),
329
- shared_state: SharedState {
359
+ pub(super) fn new_scheduled(
360
+ attribs: StartChildWorkflowExecution,
361
+ internal_flags: InternalFlagsRef,
362
+ ) -> NewMachineWithCommand {
363
+ let mut s = Self::from_parts(
364
+ Created {}.into(),
365
+ SharedState {
330
366
  lang_sequence_number: attribs.seq,
331
367
  workflow_id: attribs.workflow_id.clone(),
332
368
  workflow_type: attribs.workflow_type.clone(),
333
369
  namespace: attribs.namespace.clone(),
334
370
  cancel_type: attribs.cancellation_type(),
335
- ..Default::default()
371
+ internal_flags,
372
+ run_id: "".to_string(),
373
+ initiated_event_id: 0,
374
+ started_event_id: 0,
375
+ cancelled_before_sent: false,
336
376
  },
337
- };
377
+ );
338
378
  OnEventWrapper::on_event_mut(&mut s, ChildWorkflowMachineEvents::Schedule)
339
379
  .expect("Scheduling child workflows doesn't fail");
340
380
  let cmd = Command {
341
381
  command_type: CommandType::StartChildWorkflowExecution as i32,
342
382
  attributes: Some(attribs.into()),
343
383
  };
344
- (s, cmd)
384
+ NewMachineWithCommand {
385
+ command: cmd,
386
+ machine: s.into(),
387
+ }
345
388
  }
346
389
 
347
390
  fn resolve_cancelled_msg(&self) -> ResolveChildWorkflowExecution {
@@ -372,13 +415,31 @@ impl ChildWorkflowMachine {
372
415
  }
373
416
  }
374
417
 
375
- impl TryFrom<HistoryEvent> for ChildWorkflowMachineEvents {
418
+ impl TryFrom<HistEventData> for ChildWorkflowMachineEvents {
376
419
  type Error = WFMachinesError;
377
420
 
378
- fn try_from(e: HistoryEvent) -> Result<Self, Self::Error> {
421
+ fn try_from(e: HistEventData) -> Result<Self, Self::Error> {
422
+ let last_task_in_history = e.current_task_is_last_in_history;
423
+ let e = e.event;
379
424
  Ok(match EventType::from_i32(e.event_type) {
380
425
  Some(EventType::StartChildWorkflowExecutionInitiated) => {
381
- Self::StartChildWorkflowExecutionInitiated(e.event_id)
426
+ if let Some(
427
+ history_event::Attributes::StartChildWorkflowExecutionInitiatedEventAttributes(
428
+ attrs,
429
+ ),
430
+ ) = e.attributes
431
+ {
432
+ Self::StartChildWorkflowExecutionInitiated(ChildWorkflowInitiatedData {
433
+ event_id: e.event_id,
434
+ wf_type: attrs.workflow_type.unwrap_or_default().name,
435
+ wf_id: attrs.workflow_id,
436
+ last_task_in_history,
437
+ })
438
+ } else {
439
+ return Err(WFMachinesError::Fatal(
440
+ "StartChildWorkflowExecutionInitiated attributes were unset".to_string(),
441
+ ));
442
+ }
382
443
  }
383
444
  Some(EventType::StartChildWorkflowExecutionFailed) => {
384
445
  if let Some(
@@ -658,11 +719,12 @@ fn convert_payloads(
658
719
  mod test {
659
720
  use super::*;
660
721
  use crate::{
661
- replay::TestHistoryBuilder, test_help::canned_histories, worker::workflow::ManagedWFFunc,
722
+ internal_flags::InternalFlags, replay::TestHistoryBuilder, test_help::canned_histories,
723
+ worker::workflow::ManagedWFFunc,
662
724
  };
663
725
  use anyhow::anyhow;
664
726
  use rstest::{fixture, rstest};
665
- use std::mem::discriminant;
727
+ use std::{cell::RefCell, mem::discriminant, rc::Rc};
666
728
  use temporal_sdk::{
667
729
  CancellableFuture, ChildWorkflowOptions, WfContext, WorkflowFunction, WorkflowResult,
668
730
  };
@@ -838,19 +900,32 @@ mod test {
838
900
  #[test]
839
901
  fn cancels_ignored_terminal() {
840
902
  for state in [
841
- ChildWorkflowMachineState::Cancelled(Cancelled {}),
903
+ ChildWorkflowMachineState::Cancelled(Cancelled {
904
+ seen_cancelled_event: false,
905
+ }),
842
906
  Failed {}.into(),
843
907
  StartFailed {}.into(),
844
908
  TimedOut {}.into(),
845
909
  Completed {}.into(),
846
910
  ] {
847
- let mut s = ChildWorkflowMachine {
848
- state: state.clone(),
849
- shared_state: Default::default(),
850
- };
911
+ let mut s = ChildWorkflowMachine::from_parts(
912
+ state.clone(),
913
+ SharedState {
914
+ initiated_event_id: 0,
915
+ started_event_id: 0,
916
+ lang_sequence_number: 0,
917
+ namespace: "".to_string(),
918
+ workflow_id: "".to_string(),
919
+ run_id: "".to_string(),
920
+ workflow_type: "".to_string(),
921
+ cancelled_before_sent: false,
922
+ cancel_type: Default::default(),
923
+ internal_flags: Rc::new(RefCell::new(InternalFlags::new(&Default::default()))),
924
+ },
925
+ );
851
926
  let cmds = s.cancel().unwrap();
852
927
  assert_eq!(cmds.len(), 0);
853
- assert_eq!(discriminant(&state), discriminant(&s.state));
928
+ assert_eq!(discriminant(&state), discriminant(s.state()));
854
929
  }
855
930
  }
856
931
  }
@@ -2,7 +2,8 @@ use super::{
2
2
  workflow_machines::MachineResponse, Cancellable, EventInfo, NewMachineWithCommand,
3
3
  OnEventWrapper, WFMachinesAdapter, WFMachinesError,
4
4
  };
5
- use rustfsm::{fsm, TransitionResult};
5
+ use crate::worker::workflow::machines::HistEventData;
6
+ use rustfsm::{fsm, StateMachine, TransitionResult};
6
7
  use std::convert::TryFrom;
7
8
  use temporal_sdk_core_protos::{
8
9
  coresdk::workflow_commands::CompleteWorkflowExecution,
@@ -18,9 +19,9 @@ fsm! {
18
19
  name CompleteWorkflowMachine;
19
20
  command CompleteWFCommand;
20
21
  error WFMachinesError;
21
- shared_state CompleteWorkflowExecution;
22
+ shared_state ();
22
23
 
23
- Created --(Schedule, shared on_schedule) --> CompleteWorkflowCommandCreated;
24
+ Created --(Schedule, on_schedule) --> CompleteWorkflowCommandCreated;
24
25
 
25
26
  CompleteWorkflowCommandCreated --(CommandCompleteWorkflowExecution)
26
27
  --> CompleteWorkflowCommandCreated;
@@ -45,10 +46,7 @@ pub(super) fn complete_workflow(attribs: CompleteWorkflowExecution) -> NewMachin
45
46
  impl CompleteWorkflowMachine {
46
47
  /// Create a new WF machine and schedule it
47
48
  pub(crate) fn new_scheduled(attribs: CompleteWorkflowExecution) -> (Self, Command) {
48
- let mut s = Self {
49
- state: Created {}.into(),
50
- shared_state: attribs,
51
- };
49
+ let mut s = Self::from_parts(Created { attribs }.into(), ());
52
50
  let cmd =
53
51
  match OnEventWrapper::on_event_mut(&mut s, CompleteWorkflowMachineEvents::Schedule)
54
52
  .expect("Scheduling complete wf machines doesn't fail")
@@ -61,10 +59,11 @@ impl CompleteWorkflowMachine {
61
59
  }
62
60
  }
63
61
 
64
- impl TryFrom<HistoryEvent> for CompleteWorkflowMachineEvents {
62
+ impl TryFrom<HistEventData> for CompleteWorkflowMachineEvents {
65
63
  type Error = WFMachinesError;
66
64
 
67
- fn try_from(e: HistoryEvent) -> Result<Self, Self::Error> {
65
+ fn try_from(e: HistEventData) -> Result<Self, Self::Error> {
66
+ let e = e.event;
68
67
  Ok(match e.event_type() {
69
68
  EventType::WorkflowExecutionCompleted => Self::WorkflowExecutionCompleted,
70
69
  _ => {
@@ -88,16 +87,17 @@ impl TryFrom<CommandType> for CompleteWorkflowMachineEvents {
88
87
  }
89
88
 
90
89
  #[derive(Default, Clone)]
91
- pub(super) struct Created {}
90
+ pub(super) struct Created {
91
+ attribs: CompleteWorkflowExecution,
92
+ }
92
93
 
93
94
  impl Created {
94
95
  pub(super) fn on_schedule(
95
96
  self,
96
- dat: CompleteWorkflowExecution,
97
97
  ) -> CompleteWorkflowMachineTransition<CompleteWorkflowCommandCreated> {
98
98
  let cmd = Command {
99
99
  command_type: CommandType::CompleteWorkflowExecution as i32,
100
- attributes: Some(dat.into()),
100
+ attributes: Some(self.attribs.into()),
101
101
  };
102
102
  TransitionResult::commands(vec![CompleteWFCommand::AddCommand(cmd)])
103
103
  }
@@ -2,7 +2,8 @@ use super::{
2
2
  Cancellable, EventInfo, HistoryEvent, MachineResponse, NewMachineWithCommand, OnEventWrapper,
3
3
  WFMachinesAdapter, WFMachinesError,
4
4
  };
5
- use rustfsm::{fsm, TransitionResult};
5
+ use crate::worker::workflow::machines::HistEventData;
6
+ use rustfsm::{fsm, StateMachine, TransitionResult};
6
7
  use std::convert::TryFrom;
7
8
  use temporal_sdk_core_protos::{
8
9
  coresdk::workflow_commands::ContinueAsNewWorkflowExecution,
@@ -30,10 +31,7 @@ fsm! {
30
31
  pub(super) enum ContinueAsNewWorkflowCommand {}
31
32
 
32
33
  pub(super) fn continue_as_new(attribs: ContinueAsNewWorkflowExecution) -> NewMachineWithCommand {
33
- let mut machine = ContinueAsNewWorkflowMachine {
34
- state: Created {}.into(),
35
- shared_state: (),
36
- };
34
+ let mut machine = ContinueAsNewWorkflowMachine::from_parts(Created {}.into(), ());
37
35
  OnEventWrapper::on_event_mut(&mut machine, ContinueAsNewWorkflowMachineEvents::Schedule)
38
36
  .expect("Scheduling continue as new machine doesn't fail");
39
37
  let command = Command {
@@ -69,10 +67,11 @@ impl From<ContinueAsNewWorkflowCommandCreated> for ContinueAsNewWorkflowCommandR
69
67
  }
70
68
  }
71
69
 
72
- impl TryFrom<HistoryEvent> for ContinueAsNewWorkflowMachineEvents {
70
+ impl TryFrom<HistEventData> for ContinueAsNewWorkflowMachineEvents {
73
71
  type Error = WFMachinesError;
74
72
 
75
- fn try_from(e: HistoryEvent) -> Result<Self, Self::Error> {
73
+ fn try_from(e: HistEventData) -> Result<Self, Self::Error> {
74
+ let e = e.event;
76
75
  Ok(match e.event_type() {
77
76
  EventType::WorkflowExecutionContinuedAsNew => Self::WorkflowExecutionContinuedAsNew,
78
77
  _ => {
@@ -2,7 +2,8 @@ use super::{
2
2
  workflow_machines::MachineResponse, Cancellable, EventInfo, NewMachineWithCommand,
3
3
  OnEventWrapper, WFMachinesAdapter, WFMachinesError,
4
4
  };
5
- use rustfsm::{fsm, TransitionResult};
5
+ use crate::worker::workflow::machines::HistEventData;
6
+ use rustfsm::{fsm, StateMachine, TransitionResult};
6
7
  use std::convert::TryFrom;
7
8
  use temporal_sdk_core_protos::{
8
9
  coresdk::workflow_commands::FailWorkflowExecution,
@@ -17,9 +18,9 @@ fsm! {
17
18
  pub(super) name FailWorkflowMachine;
18
19
  command FailWFCommand;
19
20
  error WFMachinesError;
20
- shared_state FailWorkflowExecution;
21
+ shared_state ();
21
22
 
22
- Created --(Schedule, shared on_schedule) --> FailWorkflowCommandCreated;
23
+ Created --(Schedule, on_schedule) --> FailWorkflowCommandCreated;
23
24
 
24
25
  FailWorkflowCommandCreated --(CommandFailWorkflowExecution) --> FailWorkflowCommandCreated;
25
26
  FailWorkflowCommandCreated --(WorkflowExecutionFailed) --> FailWorkflowCommandRecorded;
@@ -42,10 +43,7 @@ pub(super) fn fail_workflow(attribs: FailWorkflowExecution) -> NewMachineWithCom
42
43
  impl FailWorkflowMachine {
43
44
  /// Create a new WF machine and schedule it
44
45
  pub(crate) fn new_scheduled(attribs: FailWorkflowExecution) -> (Self, ProtoCommand) {
45
- let mut s = Self {
46
- state: Created {}.into(),
47
- shared_state: attribs,
48
- };
46
+ let mut s = Self::from_parts(Created { attribs }.into(), ());
49
47
  let cmd = match OnEventWrapper::on_event_mut(&mut s, FailWorkflowMachineEvents::Schedule)
50
48
  .expect("Scheduling fail wf machines doesn't fail")
51
49
  .pop()
@@ -58,16 +56,15 @@ impl FailWorkflowMachine {
58
56
  }
59
57
 
60
58
  #[derive(Default, Clone)]
61
- pub(super) struct Created {}
59
+ pub(super) struct Created {
60
+ attribs: FailWorkflowExecution,
61
+ }
62
62
 
63
63
  impl Created {
64
- pub(super) fn on_schedule(
65
- self,
66
- dat: FailWorkflowExecution,
67
- ) -> FailWorkflowMachineTransition<FailWorkflowCommandCreated> {
64
+ pub(super) fn on_schedule(self) -> FailWorkflowMachineTransition<FailWorkflowCommandCreated> {
68
65
  let cmd = ProtoCommand {
69
66
  command_type: CommandType::FailWorkflowExecution as i32,
70
- attributes: Some(dat.into()),
67
+ attributes: Some(self.attribs.into()),
71
68
  };
72
69
  TransitionResult::commands(vec![FailWFCommand::AddCommand(cmd)])
73
70
  }
@@ -85,10 +82,11 @@ impl From<FailWorkflowCommandCreated> for FailWorkflowCommandRecorded {
85
82
  }
86
83
  }
87
84
 
88
- impl TryFrom<HistoryEvent> for FailWorkflowMachineEvents {
85
+ impl TryFrom<HistEventData> for FailWorkflowMachineEvents {
89
86
  type Error = WFMachinesError;
90
87
 
91
- fn try_from(e: HistoryEvent) -> Result<Self, Self::Error> {
88
+ fn try_from(e: HistEventData) -> Result<Self, Self::Error> {
89
+ let e = e.event;
92
90
  Ok(match e.event_type() {
93
91
  EventType::WorkflowExecutionFailed => Self::WorkflowExecutionFailed,
94
92
  _ => {