@temporalio/core-bridge 1.5.2 → 1.6.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 (153) hide show
  1. package/Cargo.lock +255 -48
  2. package/package.json +4 -4
  3. package/releases/aarch64-apple-darwin/index.node +0 -0
  4. package/releases/aarch64-unknown-linux-gnu/index.node +0 -0
  5. package/releases/x86_64-apple-darwin/index.node +0 -0
  6. package/releases/x86_64-pc-windows-msvc/index.node +0 -0
  7. package/releases/x86_64-unknown-linux-gnu/index.node +0 -0
  8. package/sdk-core/.buildkite/pipeline.yml +1 -3
  9. package/sdk-core/.cargo/config.toml +5 -2
  10. package/sdk-core/.github/workflows/heavy.yml +28 -0
  11. package/sdk-core/Cargo.toml +1 -1
  12. package/sdk-core/README.md +9 -5
  13. package/sdk-core/client/src/lib.rs +211 -36
  14. package/sdk-core/client/src/raw.rs +1 -1
  15. package/sdk-core/client/src/retry.rs +32 -20
  16. package/sdk-core/core/Cargo.toml +23 -9
  17. package/sdk-core/core/src/abstractions.rs +11 -0
  18. package/sdk-core/core/src/core_tests/activity_tasks.rs +6 -5
  19. package/sdk-core/core/src/core_tests/local_activities.rs +263 -22
  20. package/sdk-core/core/src/core_tests/queries.rs +2 -2
  21. package/sdk-core/core/src/core_tests/workflow_tasks.rs +249 -5
  22. package/sdk-core/core/src/ephemeral_server/mod.rs +5 -6
  23. package/sdk-core/core/src/lib.rs +2 -0
  24. package/sdk-core/core/src/protosext/mod.rs +1 -1
  25. package/sdk-core/core/src/telemetry/log_export.rs +1 -1
  26. package/sdk-core/core/src/telemetry/mod.rs +23 -8
  27. package/sdk-core/core/src/test_help/mod.rs +8 -1
  28. package/sdk-core/core/src/worker/activities/local_activities.rs +259 -125
  29. package/sdk-core/core/src/worker/activities.rs +3 -2
  30. package/sdk-core/core/src/worker/mod.rs +53 -26
  31. package/sdk-core/core/src/worker/workflow/bridge.rs +1 -3
  32. package/sdk-core/core/src/worker/workflow/driven_workflow.rs +3 -5
  33. package/sdk-core/core/src/worker/workflow/history_update.rs +835 -277
  34. package/sdk-core/core/src/worker/workflow/machines/activity_state_machine.rs +9 -17
  35. package/sdk-core/core/src/worker/workflow/machines/cancel_external_state_machine.rs +3 -5
  36. package/sdk-core/core/src/worker/workflow/machines/cancel_workflow_state_machine.rs +1 -2
  37. package/sdk-core/core/src/worker/workflow/machines/child_workflow_state_machine.rs +3 -5
  38. package/sdk-core/core/src/worker/workflow/machines/complete_workflow_state_machine.rs +1 -2
  39. package/sdk-core/core/src/worker/workflow/machines/continue_as_new_workflow_state_machine.rs +1 -2
  40. package/sdk-core/core/src/worker/workflow/machines/fail_workflow_state_machine.rs +1 -2
  41. package/sdk-core/core/src/worker/workflow/machines/local_activity_state_machine.rs +73 -51
  42. package/sdk-core/core/src/worker/workflow/machines/mod.rs +3 -3
  43. package/sdk-core/core/src/worker/workflow/machines/modify_workflow_properties_state_machine.rs +4 -4
  44. package/sdk-core/core/src/worker/workflow/machines/patch_state_machine.rs +1 -2
  45. package/sdk-core/core/src/worker/workflow/machines/signal_external_state_machine.rs +3 -5
  46. package/sdk-core/core/src/worker/workflow/machines/timer_state_machine.rs +6 -7
  47. package/sdk-core/core/src/worker/workflow/machines/transition_coverage.rs +2 -2
  48. package/sdk-core/core/src/worker/workflow/machines/upsert_search_attributes_state_machine.rs +4 -4
  49. package/sdk-core/core/src/worker/workflow/machines/workflow_machines/local_acts.rs +6 -17
  50. package/sdk-core/core/src/worker/workflow/machines/workflow_machines.rs +89 -58
  51. package/sdk-core/core/src/worker/workflow/machines/workflow_task_state_machine.rs +4 -7
  52. package/sdk-core/core/src/worker/workflow/managed_run/managed_wf_test.rs +21 -9
  53. package/sdk-core/core/src/worker/workflow/managed_run.rs +1021 -360
  54. package/sdk-core/core/src/worker/workflow/mod.rs +306 -346
  55. package/sdk-core/core/src/worker/workflow/run_cache.rs +29 -53
  56. package/sdk-core/core/src/worker/workflow/wft_extraction.rs +125 -0
  57. package/sdk-core/core/src/worker/workflow/wft_poller.rs +1 -4
  58. package/sdk-core/core/src/worker/workflow/workflow_stream/saved_wf_inputs.rs +115 -0
  59. package/sdk-core/core/src/worker/workflow/workflow_stream/tonic_status_serde.rs +24 -0
  60. package/sdk-core/core/src/worker/workflow/workflow_stream.rs +444 -714
  61. package/sdk-core/core-api/Cargo.toml +2 -0
  62. package/sdk-core/core-api/src/errors.rs +1 -34
  63. package/sdk-core/core-api/src/lib.rs +6 -2
  64. package/sdk-core/core-api/src/worker.rs +14 -1
  65. package/sdk-core/etc/deps.svg +115 -140
  66. package/sdk-core/etc/regen-depgraph.sh +5 -0
  67. package/sdk-core/fsm/rustfsm_procmacro/src/lib.rs +6 -6
  68. package/sdk-core/fsm/rustfsm_trait/src/lib.rs +7 -3
  69. package/sdk-core/histories/evict_while_la_running_no_interference-16_history.bin +0 -0
  70. package/sdk-core/protos/api_upstream/Makefile +5 -5
  71. package/sdk-core/protos/api_upstream/build/go.mod +7 -0
  72. package/sdk-core/protos/api_upstream/build/go.sum +5 -0
  73. package/sdk-core/protos/api_upstream/build/tools.go +29 -0
  74. package/sdk-core/protos/api_upstream/go.mod +6 -0
  75. package/sdk-core/protos/api_upstream/temporal/api/batch/v1/message.proto +9 -2
  76. package/sdk-core/protos/api_upstream/temporal/api/command/v1/message.proto +12 -19
  77. package/sdk-core/protos/api_upstream/temporal/api/common/v1/message.proto +2 -2
  78. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/batch_operation.proto +3 -2
  79. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/command_type.proto +3 -2
  80. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/common.proto +3 -2
  81. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/event_type.proto +3 -3
  82. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/failed_cause.proto +20 -2
  83. package/sdk-core/protos/api_upstream/temporal/api/{update/v1/message.proto → enums/v1/interaction_type.proto} +11 -18
  84. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/namespace.proto +2 -2
  85. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/query.proto +2 -2
  86. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/reset.proto +2 -2
  87. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/schedule.proto +2 -2
  88. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/task_queue.proto +2 -2
  89. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/update.proto +2 -13
  90. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/workflow.proto +2 -2
  91. package/sdk-core/protos/api_upstream/temporal/api/errordetails/v1/message.proto +2 -2
  92. package/sdk-core/protos/api_upstream/temporal/api/failure/v1/message.proto +2 -2
  93. package/sdk-core/protos/api_upstream/temporal/api/filter/v1/message.proto +2 -2
  94. package/sdk-core/protos/api_upstream/temporal/api/history/v1/message.proto +13 -19
  95. package/sdk-core/protos/api_upstream/temporal/api/interaction/v1/message.proto +87 -0
  96. package/sdk-core/protos/api_upstream/temporal/api/namespace/v1/message.proto +2 -2
  97. package/sdk-core/protos/api_upstream/temporal/api/operatorservice/v1/request_response.proto +2 -2
  98. package/sdk-core/protos/api_upstream/temporal/api/operatorservice/v1/service.proto +2 -2
  99. package/sdk-core/protos/api_upstream/temporal/api/query/v1/message.proto +2 -2
  100. package/sdk-core/protos/api_upstream/temporal/api/replication/v1/message.proto +2 -2
  101. package/sdk-core/protos/api_upstream/temporal/api/schedule/v1/message.proto +2 -2
  102. package/sdk-core/protos/api_upstream/temporal/api/taskqueue/v1/message.proto +2 -2
  103. package/sdk-core/protos/api_upstream/temporal/api/version/v1/message.proto +2 -2
  104. package/sdk-core/protos/api_upstream/temporal/api/workflow/v1/message.proto +2 -2
  105. package/sdk-core/protos/api_upstream/temporal/api/workflowservice/v1/request_response.proto +13 -8
  106. package/sdk-core/protos/api_upstream/temporal/api/workflowservice/v1/service.proto +2 -2
  107. package/sdk-core/protos/local/temporal/sdk/core/workflow_activation/workflow_activation.proto +2 -0
  108. package/sdk-core/protos/testsrv_upstream/temporal/api/testservice/v1/request_response.proto +2 -2
  109. package/sdk-core/protos/testsrv_upstream/temporal/api/testservice/v1/service.proto +2 -2
  110. package/sdk-core/sdk/Cargo.toml +4 -3
  111. package/sdk-core/sdk/src/lib.rs +87 -21
  112. package/sdk-core/sdk/src/workflow_future.rs +7 -12
  113. package/sdk-core/sdk-core-protos/Cargo.toml +5 -2
  114. package/sdk-core/sdk-core-protos/build.rs +36 -2
  115. package/sdk-core/sdk-core-protos/src/history_builder.rs +26 -19
  116. package/sdk-core/sdk-core-protos/src/history_info.rs +4 -0
  117. package/sdk-core/sdk-core-protos/src/lib.rs +78 -34
  118. package/sdk-core/sdk-core-protos/src/task_token.rs +12 -2
  119. package/sdk-core/test-utils/Cargo.toml +3 -1
  120. package/sdk-core/test-utils/src/histfetch.rs +1 -1
  121. package/sdk-core/test-utils/src/lib.rs +50 -18
  122. package/sdk-core/test-utils/src/wf_input_saver.rs +50 -0
  123. package/sdk-core/test-utils/src/workflows.rs +29 -0
  124. package/sdk-core/tests/fuzzy_workflow.rs +130 -0
  125. package/sdk-core/tests/{load_tests.rs → heavy_tests.rs} +114 -7
  126. package/sdk-core/tests/integ_tests/heartbeat_tests.rs +5 -2
  127. package/sdk-core/tests/integ_tests/metrics_tests.rs +1 -1
  128. package/sdk-core/tests/integ_tests/polling_tests.rs +1 -39
  129. package/sdk-core/tests/integ_tests/queries_tests.rs +2 -127
  130. package/sdk-core/tests/integ_tests/visibility_tests.rs +52 -5
  131. package/sdk-core/tests/integ_tests/workflow_tests/activities.rs +74 -1
  132. package/sdk-core/tests/integ_tests/workflow_tests/cancel_wf.rs +5 -13
  133. package/sdk-core/tests/integ_tests/workflow_tests/continue_as_new.rs +1 -1
  134. package/sdk-core/tests/integ_tests/workflow_tests/determinism.rs +2 -10
  135. package/sdk-core/tests/integ_tests/workflow_tests/local_activities.rs +69 -197
  136. package/sdk-core/tests/integ_tests/workflow_tests/patches.rs +4 -28
  137. package/sdk-core/tests/integ_tests/workflow_tests/replay.rs +12 -7
  138. package/sdk-core/tests/integ_tests/workflow_tests/signals.rs +14 -14
  139. package/sdk-core/tests/integ_tests/workflow_tests/stickyness.rs +3 -19
  140. package/sdk-core/tests/integ_tests/workflow_tests/timers.rs +3 -19
  141. package/sdk-core/tests/integ_tests/workflow_tests/upsert_search_attrs.rs +1 -1
  142. package/sdk-core/tests/integ_tests/workflow_tests.rs +5 -6
  143. package/sdk-core/tests/main.rs +2 -12
  144. package/sdk-core/tests/runner.rs +71 -34
  145. package/sdk-core/tests/wf_input_replay.rs +32 -0
  146. package/sdk-core/bridge-ffi/Cargo.toml +0 -24
  147. package/sdk-core/bridge-ffi/LICENSE.txt +0 -23
  148. package/sdk-core/bridge-ffi/build.rs +0 -25
  149. package/sdk-core/bridge-ffi/include/sdk-core-bridge.h +0 -224
  150. package/sdk-core/bridge-ffi/src/lib.rs +0 -746
  151. package/sdk-core/bridge-ffi/src/wrappers.rs +0 -221
  152. package/sdk-core/protos/local/temporal/sdk/core/bridge/bridge.proto +0 -210
  153. package/sdk-core/sdk/src/conversions.rs +0 -8
@@ -1,12 +1,18 @@
1
1
  use crate::{
2
- replay::{HistoryInfo, TestHistoryBuilder},
3
- worker::client::WorkerClient,
2
+ protosext::ValidPollWFTQResponse,
3
+ worker::{
4
+ client::WorkerClient,
5
+ workflow::{CacheMissFetchReq, PermittedWFT, PreparedWFT},
6
+ },
4
7
  };
5
- use futures::{future::BoxFuture, stream, stream::BoxStream, FutureExt, Stream, StreamExt};
8
+ use futures::{future::BoxFuture, FutureExt, Stream};
9
+ use itertools::Itertools;
6
10
  use std::{
7
11
  collections::VecDeque,
8
12
  fmt::Debug,
9
13
  future::Future,
14
+ mem,
15
+ mem::transmute,
10
16
  pin::Pin,
11
17
  sync::Arc,
12
18
  task::{Context, Poll},
@@ -14,44 +20,93 @@ use std::{
14
20
  use temporal_sdk_core_protos::temporal::api::{
15
21
  enums::v1::EventType,
16
22
  history::v1::{History, HistoryEvent},
17
- workflowservice::v1::GetWorkflowExecutionHistoryResponse,
18
23
  };
19
24
  use tracing::Instrument;
20
25
 
21
- /// A slimmed down version of a poll workflow task response which includes just the info needed
22
- /// by [WorkflowManager]. History events are expected to be consumed from it and applied to the
23
- /// state machines.
26
+ lazy_static::lazy_static! {
27
+ static ref EMPTY_FETCH_ERR: tonic::Status
28
+ = tonic::Status::data_loss("Fetched empty history page");
29
+ static ref EMPTY_TASK_ERR: tonic::Status
30
+ = tonic::Status::data_loss("Received an empty workflow task with no queries or history");
31
+ }
32
+
33
+ /// Represents one or more complete WFT sequences. History events are expected to be consumed from
34
+ /// it and applied to the state machines via [HistoryUpdate::take_next_wft_sequence]
35
+ #[cfg_attr(
36
+ feature = "save_wf_inputs",
37
+ derive(serde::Serialize, serde::Deserialize)
38
+ )]
24
39
  pub struct HistoryUpdate {
25
- events: BoxStream<'static, Result<HistoryEvent, tonic::Status>>,
26
- /// It is useful to be able to look ahead up to one workflow task beyond the currently
27
- /// requested one. The initial (possibly only) motivation for this being to be able to
28
- /// pre-emptively notify lang about patch markers so that calls to `changed` do not need to
29
- /// be async.
30
- buffered: VecDeque<HistoryEvent>,
31
- pub previous_started_event_id: i64,
40
+ events: Vec<HistoryEvent>,
41
+ /// The event ID of the last started WFT, as according to the WFT which this update was
42
+ /// extracted from. Hence, while processing multiple logical WFTs during replay which were part
43
+ /// of one large history fetched from server, multiple updates may have the same value here.
44
+ pub previous_wft_started_id: i64,
45
+ /// True if this update contains the final WFT in history, and no more attempts to extract
46
+ /// additional updates should be made.
47
+ has_last_wft: bool,
32
48
  }
33
49
  impl Debug for HistoryUpdate {
34
50
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35
- write!(
36
- f,
37
- "HistoryUpdate(previous_started_event_id: {})",
38
- self.previous_started_event_id
39
- )
51
+ if self.is_real() {
52
+ write!(
53
+ f,
54
+ "HistoryUpdate(previous_started_event_id: {}, length: {}, first_event_id: {:?})",
55
+ self.previous_wft_started_id,
56
+ self.events.len(),
57
+ self.events.first().map(|e| e.event_id)
58
+ )
59
+ } else {
60
+ write!(f, "DummyHistoryUpdate")
61
+ }
62
+ }
63
+ }
64
+ impl HistoryUpdate {
65
+ /// Sometimes it's useful to take an update out of something without needing to use an option
66
+ /// field. Use this to replace the field with an empty update.
67
+ pub fn dummy() -> Self {
68
+ Self {
69
+ events: vec![],
70
+ previous_wft_started_id: -1,
71
+ has_last_wft: false,
72
+ }
73
+ }
74
+ pub fn is_real(&self) -> bool {
75
+ self.previous_wft_started_id >= 0
76
+ }
77
+ pub fn first_event_id(&self) -> Option<i64> {
78
+ self.events.get(0).map(|e| e.event_id)
40
79
  }
41
80
  }
42
81
 
82
+ #[derive(Debug)]
83
+ pub enum NextWFT {
84
+ ReplayOver,
85
+ WFT(Vec<HistoryEvent>),
86
+ NeedFetch,
87
+ }
88
+
89
+ #[derive(derive_more::DebugCustom)]
90
+ #[debug(fmt = "HistoryPaginator(run_id: {run_id})")]
91
+ #[cfg_attr(
92
+ feature = "save_wf_inputs",
93
+ derive(serde::Serialize, serde::Deserialize),
94
+ serde(default = "HistoryPaginator::fake_deserialized")
95
+ )]
43
96
  pub struct HistoryPaginator {
44
- // Potentially this could actually be a ref w/ lifetime here
97
+ pub(crate) wf_id: String,
98
+ pub(crate) run_id: String,
99
+ pub(crate) previous_wft_started_id: i64,
100
+
101
+ #[cfg_attr(feature = "save_wf_inputs", serde(skip))]
45
102
  client: Arc<dyn WorkerClient>,
103
+ #[cfg_attr(feature = "save_wf_inputs", serde(skip))]
46
104
  event_queue: VecDeque<HistoryEvent>,
47
- wf_id: String,
48
- run_id: String,
105
+ #[cfg_attr(feature = "save_wf_inputs", serde(skip))]
49
106
  next_page_token: NextPageToken,
50
- open_history_request:
51
- Option<BoxFuture<'static, Result<GetWorkflowExecutionHistoryResponse, tonic::Status>>>,
52
107
  /// These are events that should be returned once pagination has finished. This only happens
53
108
  /// during cache misses, where we got a partial task but need to fetch history from the start.
54
- /// We use this to apply any
109
+ #[cfg_attr(feature = "save_wf_inputs", serde(skip))]
55
110
  final_events: Vec<HistoryEvent>,
56
111
  }
57
112
 
@@ -77,8 +132,68 @@ impl From<Vec<u8>> for NextPageToken {
77
132
  }
78
133
 
79
134
  impl HistoryPaginator {
80
- pub(crate) fn new(
135
+ /// Use a new poll response to create a new [WFTPaginator], returning it and the
136
+ /// [PreparedWFT] extracted from it that can be fed into workflow state.
137
+ pub(super) async fn from_poll(
138
+ wft: ValidPollWFTQResponse,
139
+ client: Arc<dyn WorkerClient>,
140
+ ) -> Result<(Self, PreparedWFT), tonic::Status> {
141
+ let empty_hist = wft.history.events.is_empty();
142
+ let npt = if empty_hist {
143
+ NextPageToken::FetchFromStart
144
+ } else {
145
+ wft.next_page_token.into()
146
+ };
147
+ let mut paginator = HistoryPaginator::new(
148
+ wft.history,
149
+ wft.previous_started_event_id,
150
+ wft.workflow_execution.workflow_id.clone(),
151
+ wft.workflow_execution.run_id.clone(),
152
+ npt,
153
+ client,
154
+ );
155
+ if empty_hist && wft.legacy_query.is_none() && wft.query_requests.is_empty() {
156
+ return Err(EMPTY_TASK_ERR.clone());
157
+ }
158
+ let update = if empty_hist {
159
+ HistoryUpdate::from_events([], wft.previous_started_event_id, true).0
160
+ } else {
161
+ paginator.extract_next_update().await?
162
+ };
163
+ let prepared = PreparedWFT {
164
+ task_token: wft.task_token,
165
+ attempt: wft.attempt,
166
+ execution: wft.workflow_execution,
167
+ workflow_type: wft.workflow_type,
168
+ legacy_query: wft.legacy_query,
169
+ query_requests: wft.query_requests,
170
+ update,
171
+ };
172
+ Ok((paginator, prepared))
173
+ }
174
+
175
+ pub(super) async fn from_fetchreq(
176
+ mut req: CacheMissFetchReq,
177
+ client: Arc<dyn WorkerClient>,
178
+ ) -> Result<PermittedWFT, tonic::Status> {
179
+ let mut paginator = Self {
180
+ wf_id: req.original_wft.work.execution.workflow_id.clone(),
181
+ run_id: req.original_wft.work.execution.run_id.clone(),
182
+ previous_wft_started_id: req.original_wft.work.update.previous_wft_started_id,
183
+ client,
184
+ event_queue: Default::default(),
185
+ next_page_token: NextPageToken::FetchFromStart,
186
+ final_events: vec![],
187
+ };
188
+ let first_update = paginator.extract_next_update().await?;
189
+ req.original_wft.work.update = first_update;
190
+ req.original_wft.paginator = paginator;
191
+ Ok(req.original_wft)
192
+ }
193
+
194
+ fn new(
81
195
  initial_history: History,
196
+ previous_wft_started_id: i64,
82
197
  wf_id: String,
83
198
  run_id: String,
84
199
  next_page_token: impl Into<NextPageToken>,
@@ -97,20 +212,107 @@ impl HistoryPaginator {
97
212
  wf_id,
98
213
  run_id,
99
214
  next_page_token,
100
- open_history_request: None,
101
215
  final_events,
216
+ previous_wft_started_id,
217
+ }
218
+ }
219
+
220
+ #[cfg(feature = "save_wf_inputs")]
221
+ pub(super) fn fake_deserialized() -> HistoryPaginator {
222
+ use crate::worker::client::mocks::mock_manual_workflow_client;
223
+ HistoryPaginator {
224
+ client: Arc::new(mock_manual_workflow_client()),
225
+ event_queue: Default::default(),
226
+ wf_id: "".to_string(),
227
+ run_id: "".to_string(),
228
+ next_page_token: NextPageToken::FetchFromStart,
229
+ final_events: vec![],
230
+ previous_wft_started_id: -2,
102
231
  }
103
232
  }
104
233
 
105
- fn extend_queue_with_new_page(&mut self, resp: GetWorkflowExecutionHistoryResponse) {
106
- self.next_page_token = resp.next_page_token.into();
234
+ /// Return at least the next two WFT sequences (as determined by the passed-in ID) as a
235
+ /// [HistoryUpdate]. Two sequences supports the required peek-ahead during replay without
236
+ /// unnecessary back-and-forth.
237
+ ///
238
+ /// If there are already enough events buffered in memory, they will all be returned. Including
239
+ /// possibly (likely, during replay) more than just the next two WFTs.
240
+ ///
241
+ /// If there are insufficient events to constitute two WFTs, then we will fetch pages until
242
+ /// we have two, or until we are at the end of history.
243
+ pub(crate) async fn extract_next_update(&mut self) -> Result<HistoryUpdate, tonic::Status> {
244
+ loop {
245
+ self.get_next_page().await?;
246
+ let current_events = mem::take(&mut self.event_queue);
247
+ if current_events.is_empty() {
248
+ // If next page fetching happened, and we still ended up with no events, something
249
+ // is wrong. We're expecting there to be more events to be able to extract this
250
+ // update, but server isn't giving us any. We have no choice except to give up and
251
+ // evict.
252
+ error!(
253
+ "We expected to be able to fetch more events but server says there are none"
254
+ );
255
+ return Err(EMPTY_FETCH_ERR.clone());
256
+ }
257
+ let first_event_id = current_events.front().unwrap().event_id;
258
+ // If there are some events at the end of the fetched events which represent only a
259
+ // portion of a complete WFT, retain them to be used in the next extraction.
260
+ let no_more = matches!(self.next_page_token, NextPageToken::Done);
261
+ let (update, extra) =
262
+ HistoryUpdate::from_events(current_events, self.previous_wft_started_id, no_more);
263
+ let extra_eid_same = extra
264
+ .first()
265
+ .map(|e| e.event_id == first_event_id)
266
+ .unwrap_or_default();
267
+ self.event_queue = extra.into();
268
+ if !no_more && extra_eid_same {
269
+ // There was not a meaningful WFT in the whole page. We must fetch more
270
+ continue;
271
+ }
272
+ return Ok(update);
273
+ }
274
+ }
275
+
276
+ /// Fetches the next page and adds it to the internal queue. Returns true if a fetch was
277
+ /// performed, false if there is no next page.
278
+ async fn get_next_page(&mut self) -> Result<bool, tonic::Status> {
279
+ let history = loop {
280
+ let npt = match mem::replace(&mut self.next_page_token, NextPageToken::Done) {
281
+ // If there's no open request and the last page token we got was empty, we're done.
282
+ NextPageToken::Done => return Ok(false),
283
+ NextPageToken::FetchFromStart => vec![],
284
+ NextPageToken::Next(v) => v,
285
+ };
286
+ debug!(run_id=%self.run_id, "Fetching new history page");
287
+ let fetch_res = self
288
+ .client
289
+ .get_workflow_execution_history(self.wf_id.clone(), Some(self.run_id.clone()), npt)
290
+ .instrument(span!(tracing::Level::TRACE, "fetch_history_in_paginator"))
291
+ .await?;
292
+
293
+ self.next_page_token = fetch_res.next_page_token.into();
294
+
295
+ let history_is_empty = fetch_res
296
+ .history
297
+ .as_ref()
298
+ .map(|h| h.events.is_empty())
299
+ .unwrap_or(true);
300
+ if history_is_empty && matches!(&self.next_page_token, NextPageToken::Next(_)) {
301
+ // If the fetch returned an empty history, but there *was* a next page token,
302
+ // immediately try to get that.
303
+ continue;
304
+ }
305
+ // Async doesn't love recursion so we do this instead.
306
+ break fetch_res.history;
307
+ };
308
+
107
309
  self.event_queue
108
- .extend(resp.history.map(|h| h.events).unwrap_or_default());
310
+ .extend(history.map(|h| h.events).unwrap_or_default());
109
311
  if matches!(&self.next_page_token, NextPageToken::Done) {
110
312
  // If finished, we need to extend the queue with the final events, skipping any
111
313
  // which are already present.
112
314
  if let Some(last_event_id) = self.event_queue.back().map(|e| e.event_id) {
113
- let final_events = std::mem::take(&mut self.final_events);
315
+ let final_events = mem::take(&mut self.final_events);
114
316
  self.event_queue.extend(
115
317
  final_events
116
318
  .into_iter()
@@ -118,63 +320,127 @@ impl HistoryPaginator {
118
320
  );
119
321
  }
120
322
  };
323
+ Ok(true)
324
+ }
325
+ }
326
+
327
+ #[pin_project::pin_project]
328
+ struct StreamingHistoryPaginator {
329
+ inner: HistoryPaginator,
330
+ #[pin]
331
+ open_history_request: Option<BoxFuture<'static, Result<(), tonic::Status>>>,
332
+ }
333
+
334
+ impl StreamingHistoryPaginator {
335
+ // Kept since can be used for history downloading
336
+ #[cfg(test)]
337
+ pub fn new(inner: HistoryPaginator) -> Self {
338
+ Self {
339
+ inner,
340
+ open_history_request: None,
341
+ }
121
342
  }
122
343
  }
123
344
 
124
- impl Stream for HistoryPaginator {
345
+ impl Stream for StreamingHistoryPaginator {
125
346
  type Item = Result<HistoryEvent, tonic::Status>;
126
347
 
127
- fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
128
- if let Some(e) = self.event_queue.pop_front() {
348
+ fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
349
+ let mut this = self.project();
350
+
351
+ if let Some(e) = this.inner.event_queue.pop_front() {
129
352
  return Poll::Ready(Some(Ok(e)));
130
353
  }
131
- let history_req = if let Some(req) = self.open_history_request.as_mut() {
132
- req
133
- } else {
134
- let npt = match std::mem::replace(&mut self.next_page_token, NextPageToken::Done) {
135
- // If there's no open request and the last page token we got was empty, we're done.
136
- NextPageToken::Done => return Poll::Ready(None),
137
- NextPageToken::FetchFromStart => vec![],
138
- NextPageToken::Next(v) => v,
139
- };
140
- debug!(run_id=%self.run_id, "Fetching new history page");
141
- let gw = self.client.clone();
142
- let wid = self.wf_id.clone();
143
- let rid = self.run_id.clone();
144
- let resp_fut = async move {
145
- gw.get_workflow_execution_history(wid, Some(rid), npt)
146
- .instrument(span!(tracing::Level::TRACE, "fetch_history_in_paginator"))
147
- .await
148
- };
149
- self.open_history_request.insert(resp_fut.boxed())
150
- };
354
+ if this.open_history_request.is_none() {
355
+ // SAFETY: This is safe because the inner paginator cannot be dropped before the future,
356
+ // and the future won't be moved from out of this struct.
357
+ this.open_history_request.set(Some(unsafe {
358
+ transmute(HistoryPaginator::get_next_page(this.inner).boxed())
359
+ }));
360
+ }
361
+ let history_req = this.open_history_request.as_mut().as_pin_mut().unwrap();
151
362
 
152
- return match Future::poll(history_req.as_mut(), cx) {
363
+ match Future::poll(history_req, cx) {
153
364
  Poll::Ready(resp) => {
154
- self.open_history_request = None;
365
+ this.open_history_request.set(None);
155
366
  match resp {
156
367
  Err(neterr) => Poll::Ready(Some(Err(neterr))),
157
- Ok(resp) => {
158
- self.extend_queue_with_new_page(resp);
159
- Poll::Ready(self.event_queue.pop_front().map(Ok))
160
- }
368
+ Ok(_) => Poll::Ready(this.inner.event_queue.pop_front().map(Ok)),
161
369
  }
162
370
  }
163
371
  Poll::Pending => Poll::Pending,
164
- };
372
+ }
165
373
  }
166
374
  }
167
375
 
168
376
  impl HistoryUpdate {
169
- pub fn new(history_iterator: HistoryPaginator, previous_wft_started_id: i64) -> Self {
170
- Self {
171
- events: history_iterator.fuse().boxed(),
172
- buffered: VecDeque::new(),
173
- previous_started_event_id: previous_wft_started_id,
377
+ /// Create an instance of an update directly from events. If the passed in event iterator has a
378
+ /// partial WFT sequence at the end, all events after the last complete WFT sequence (ending
379
+ /// with WFT started) are returned back to the caller, since the history update only works in
380
+ /// terms of complete WFT sequences.
381
+ pub fn from_events<I: IntoIterator<Item = HistoryEvent>>(
382
+ events: I,
383
+ previous_wft_started_id: i64,
384
+ has_last_wft: bool,
385
+ ) -> (Self, Vec<HistoryEvent>)
386
+ where
387
+ <I as IntoIterator>::IntoIter: Send + 'static,
388
+ {
389
+ let mut all_events: Vec<_> = events.into_iter().collect();
390
+ let mut last_end =
391
+ find_end_index_of_next_wft_seq(all_events.as_slice(), previous_wft_started_id);
392
+ if matches!(last_end, NextWFTSeqEndIndex::Incomplete(_)) {
393
+ return if has_last_wft {
394
+ (
395
+ Self {
396
+ events: all_events,
397
+ previous_wft_started_id,
398
+ has_last_wft,
399
+ },
400
+ vec![],
401
+ )
402
+ } else {
403
+ (
404
+ Self {
405
+ events: vec![],
406
+ previous_wft_started_id,
407
+ has_last_wft,
408
+ },
409
+ all_events,
410
+ )
411
+ };
412
+ }
413
+ while let NextWFTSeqEndIndex::Complete(next_end_ix) = last_end {
414
+ let next_end_eid = all_events[next_end_ix].event_id;
415
+ // To save skipping all events at the front of this slice, only pass the relevant
416
+ // portion, but that means the returned index must be adjusted, hence the addition.
417
+ let next_end = find_end_index_of_next_wft_seq(&all_events[next_end_ix..], next_end_eid)
418
+ .add(next_end_ix);
419
+ if matches!(next_end, NextWFTSeqEndIndex::Incomplete(_)) {
420
+ break;
421
+ }
422
+ last_end = next_end;
174
423
  }
424
+ let remaining_events = if all_events.is_empty() {
425
+ vec![]
426
+ } else {
427
+ all_events.split_off(last_end.index() + 1)
428
+ };
429
+
430
+ (
431
+ Self {
432
+ events: all_events,
433
+ previous_wft_started_id,
434
+ has_last_wft,
435
+ },
436
+ remaining_events,
437
+ )
175
438
  }
176
439
 
177
- /// Create an instance of an update directly from events - should only be used for replaying.
440
+ /// Create an instance of an update directly from events. The passed in events *must* consist
441
+ /// of one or more complete WFT sequences. IE: The event iterator must not end in the middle
442
+ /// of a WFT sequence.
443
+ #[cfg(test)]
178
444
  pub fn new_from_events<I: IntoIterator<Item = HistoryEvent>>(
179
445
  events: I,
180
446
  previous_wft_started_id: i64,
@@ -183,309 +449,395 @@ impl HistoryUpdate {
183
449
  <I as IntoIterator>::IntoIter: Send + 'static,
184
450
  {
185
451
  Self {
186
- events: stream::iter(events.into_iter().map(Ok)).boxed(),
187
- buffered: VecDeque::new(),
188
- previous_started_event_id: previous_wft_started_id,
452
+ events: events.into_iter().collect(),
453
+ previous_wft_started_id,
454
+ has_last_wft: true,
189
455
  }
190
456
  }
191
457
 
192
- /// Given a workflow task started id, return all events starting at that number (inclusive) to
193
- /// the next WFT started event (inclusive). If there is no subsequent WFT started event,
194
- /// remaining history is returned.
458
+ /// Given a workflow task started id, return all events starting at that number (exclusive) to
459
+ /// the next WFT started event (inclusive).
195
460
  ///
196
- /// Events are *consumed* by this process, to keep things efficient in workflow machines, and
197
- /// the function may call out to server to fetch more pages if they are known to exist and
198
- /// needed to complete the WFT sequence.
461
+ /// Events are *consumed* by this process, to keep things efficient in workflow machines.
199
462
  ///
200
- /// Always buffers the WFT sequence *after* the returned one as well, if it is available.
201
- ///
202
- /// Can return a tonic error in the event that fetching additional history was needed and failed
203
- pub async fn take_next_wft_sequence(
204
- &mut self,
205
- from_wft_started_id: i64,
206
- ) -> Result<Vec<HistoryEvent>, tonic::Status> {
207
- let (next_wft_events, maybe_bonus_events) = self
208
- .take_next_wft_sequence_impl(from_wft_started_id)
209
- .await?;
210
- if !maybe_bonus_events.is_empty() {
211
- self.buffered.extend(maybe_bonus_events);
463
+ /// If we are out of WFT sequences that can be yielded by this update, it will return an empty
464
+ /// vec, indicating more pages will need to be fetched.
465
+ pub fn take_next_wft_sequence(&mut self, from_wft_started_id: i64) -> NextWFT {
466
+ // First, drop any events from the queue which are earlier than the passed-in id.
467
+ if let Some(ix_first_relevant) = self.starting_index_after_skipping(from_wft_started_id) {
468
+ self.events.drain(0..ix_first_relevant);
212
469
  }
213
-
214
- if let Some(last_event_id) = next_wft_events.last().map(|he| he.event_id) {
215
- // Always attempt to fetch the *next* WFT sequence as well, to buffer it for lookahead
216
- let (buffer_these_events, maybe_bonus_events) =
217
- self.take_next_wft_sequence_impl(last_event_id).await?;
218
- self.buffered.extend(buffer_these_events);
219
- if !maybe_bonus_events.is_empty() {
220
- self.buffered.extend(maybe_bonus_events);
470
+ let next_wft_ix = find_end_index_of_next_wft_seq(&self.events, from_wft_started_id);
471
+ match next_wft_ix {
472
+ NextWFTSeqEndIndex::Incomplete(siz) => {
473
+ if self.has_last_wft {
474
+ if siz == 0 {
475
+ NextWFT::ReplayOver
476
+ } else {
477
+ NextWFT::WFT(self.events.drain(0..=siz).collect())
478
+ }
479
+ } else {
480
+ if siz != 0 {
481
+ panic!(
482
+ "HistoryUpdate was created with an incomplete WFT. This is an SDK bug."
483
+ );
484
+ }
485
+ NextWFT::NeedFetch
486
+ }
487
+ }
488
+ NextWFTSeqEndIndex::Complete(next_wft_ix) => {
489
+ NextWFT::WFT(self.events.drain(0..=next_wft_ix).collect())
221
490
  }
222
491
  }
223
-
224
- Ok(next_wft_events)
225
492
  }
226
493
 
227
494
  /// Lets the caller peek ahead at the next WFT sequence that will be returned by
228
- /// [take_next_wft_sequence]. Will always return an empty iterator if that has not been called
229
- /// first. May also return an empty iterator or incomplete sequence if we are at the end of
230
- /// history.
231
- pub fn peek_next_wft_sequence(&self) -> impl Iterator<Item = &HistoryEvent> {
232
- self.buffered.iter()
233
- }
234
-
235
- /// Retrieve the next WFT sequence, first from buffered events and then from the real stream.
236
- /// Returns (events up to the next logical wft sequence, extra events that were taken but
237
- /// should be re-appended to the end of the buffer).
238
- async fn take_next_wft_sequence_impl(
239
- &mut self,
240
- from_event_id: i64,
241
- ) -> Result<(Vec<HistoryEvent>, Vec<HistoryEvent>), tonic::Status> {
242
- let mut events_to_next_wft_started: Vec<HistoryEvent> = vec![];
243
-
244
- // This flag tracks if, while determining events to be returned, we have seen the next
245
- // logically significant WFT started event which follows the one that was passed in as a
246
- // parameter. If a WFT fails, times out, or is devoid of commands (ie: a heartbeat) it is
247
- // not significant. So we will stop returning events (exclusive) as soon as we see an event
248
- // following a WFT started that is *not* failed, timed out, or completed with a command.
249
- let mut next_wft_state = NextWftState::NotSeen;
250
- let mut should_pop = |e: &HistoryEvent| {
251
- if e.event_id <= from_event_id {
252
- return true;
253
- } else if e.event_type() == EventType::WorkflowTaskStarted {
254
- next_wft_state = NextWftState::Seen;
255
- return true;
256
- }
495
+ /// [take_next_wft_sequence]. Will always return the first available WFT sequence if that has
496
+ /// not been called first. May also return an empty iterator or incomplete sequence if we are at
497
+ /// the end of history.
498
+ pub fn peek_next_wft_sequence(&self, from_wft_started_id: i64) -> &[HistoryEvent] {
499
+ let ix_first_relevant = self
500
+ .starting_index_after_skipping(from_wft_started_id)
501
+ .unwrap_or_default();
502
+ let relevant_events = &self.events[ix_first_relevant..];
503
+ if relevant_events.is_empty() {
504
+ return relevant_events;
505
+ }
506
+ let ix_end = find_end_index_of_next_wft_seq(relevant_events, from_wft_started_id).index();
507
+ &relevant_events[0..=ix_end]
508
+ }
257
509
 
258
- match next_wft_state {
259
- NextWftState::Seen => {
260
- // Must ignore failures and timeouts
261
- if e.event_type() == EventType::WorkflowTaskFailed
262
- || e.event_type() == EventType::WorkflowTaskTimedOut
263
- {
264
- next_wft_state = NextWftState::NotSeen;
265
- return true;
266
- } else if e.event_type() == EventType::WorkflowTaskCompleted {
267
- next_wft_state = NextWftState::SeenCompleted;
268
- return true;
269
- }
270
- false
271
- }
272
- NextWftState::SeenCompleted => {
273
- // If we've seen the WFT be completed, and this event is another scheduled, then
274
- // this was an empty heartbeat we should ignore.
275
- if e.event_type() == EventType::WorkflowTaskScheduled {
276
- next_wft_state = NextWftState::NotSeen;
277
- return true;
278
- }
279
- // Otherwise, we're done here
280
- false
281
- }
282
- NextWftState::NotSeen => true,
510
+ /// Returns true if this update has the next needed WFT sequence, false if events will need to
511
+ /// be fetched in order to create a complete update with the entire next WFT sequence.
512
+ pub fn can_take_next_wft_sequence(&self, from_wft_started_id: i64) -> bool {
513
+ let next_wft_ix = find_end_index_of_next_wft_seq(&self.events, from_wft_started_id);
514
+ if let NextWFTSeqEndIndex::Incomplete(_) = next_wft_ix {
515
+ if !self.has_last_wft {
516
+ return false;
283
517
  }
284
- };
518
+ }
519
+ true
520
+ }
285
521
 
286
- // Fetch events from the buffer first, then from the network
287
- let mut event_q = stream::iter(self.buffered.drain(..).map(Ok)).chain(&mut self.events);
522
+ fn starting_index_after_skipping(&self, from_wft_started_id: i64) -> Option<usize> {
523
+ self.events
524
+ .iter()
525
+ .find_position(|e| e.event_id > from_wft_started_id)
526
+ .map(|(ix, _)| ix)
527
+ }
528
+ }
288
529
 
289
- let mut extra_e = vec![];
290
- let mut last_seen_id = None;
291
- while let Some(e) = event_q.next().await {
292
- let e = e?;
530
+ #[derive(Debug, Copy, Clone)]
531
+ enum NextWFTSeqEndIndex {
532
+ /// The next WFT sequence is completely contained within the passed-in iterator
533
+ Complete(usize),
534
+ /// The next WFT sequence is not found within the passed-in iterator, and the contained
535
+ /// value is the last index of the iterator.
536
+ Incomplete(usize),
537
+ }
538
+ impl NextWFTSeqEndIndex {
539
+ fn index(self) -> usize {
540
+ match self {
541
+ NextWFTSeqEndIndex::Complete(ix) | NextWFTSeqEndIndex::Incomplete(ix) => ix,
542
+ }
543
+ }
544
+ fn add(self, val: usize) -> Self {
545
+ match self {
546
+ NextWFTSeqEndIndex::Complete(ix) => NextWFTSeqEndIndex::Complete(ix + val),
547
+ NextWFTSeqEndIndex::Incomplete(ix) => NextWFTSeqEndIndex::Incomplete(ix + val),
548
+ }
549
+ }
550
+ }
551
+
552
+ /// Discovers the index of the last event in next WFT sequence within the passed-in slice
553
+ fn find_end_index_of_next_wft_seq(
554
+ events: &[HistoryEvent],
555
+ from_event_id: i64,
556
+ ) -> NextWFTSeqEndIndex {
557
+ if events.is_empty() {
558
+ return NextWFTSeqEndIndex::Incomplete(0);
559
+ }
560
+ let mut last_index = 0;
561
+ let mut saw_any_non_wft_event = false;
562
+ for (ix, e) in events.iter().enumerate() {
563
+ last_index = ix;
564
+
565
+ // It's possible to have gotten a new history update without eviction (ex: unhandled
566
+ // command on completion), where we may need to skip events we already handled.
567
+ if e.event_id <= from_event_id {
568
+ continue;
569
+ }
570
+
571
+ if !matches!(
572
+ e.event_type(),
573
+ EventType::WorkflowTaskFailed
574
+ | EventType::WorkflowTaskTimedOut
575
+ | EventType::WorkflowTaskScheduled
576
+ | EventType::WorkflowTaskStarted
577
+ | EventType::WorkflowTaskCompleted
578
+ ) {
579
+ saw_any_non_wft_event = true;
580
+ }
581
+ if e.is_final_wf_execution_event() {
582
+ return NextWFTSeqEndIndex::Complete(last_index);
583
+ }
293
584
 
294
- // This little block prevents us from infinitely fetching work from the server in the
295
- // event that, for whatever reason, it keeps returning stuff we've already seen.
296
- if let Some(last_id) = last_seen_id {
297
- if e.event_id <= last_id {
298
- error!("Server returned history event IDs that went backwards!");
299
- break;
585
+ if e.event_type() == EventType::WorkflowTaskStarted {
586
+ if let Some(next_event) = events.get(ix + 1) {
587
+ let et = next_event.event_type();
588
+ // If the next event is WFT timeout or fail, or abrupt WF execution end, that
589
+ // doesn't conclude a WFT sequence.
590
+ if matches!(
591
+ et,
592
+ EventType::WorkflowTaskFailed
593
+ | EventType::WorkflowTaskTimedOut
594
+ | EventType::WorkflowExecutionTimedOut
595
+ | EventType::WorkflowExecutionTerminated
596
+ | EventType::WorkflowExecutionCanceled
597
+ ) {
598
+ continue;
300
599
  }
301
- }
302
- last_seen_id = Some(e.event_id);
303
-
304
- // It's possible to have gotten a new history update without eviction (ex: unhandled
305
- // command on completion), where we may need to skip events we already handled.
306
- if e.event_id > from_event_id {
307
- if !should_pop(&e) {
308
- if next_wft_state == NextWftState::SeenCompleted {
309
- // We have seen the wft completed event, but decided to exit. We don't
310
- // want to return that event as part of this sequence, so include it for
311
- // re-buffering along with the event we're currently on.
312
- extra_e.push(
313
- events_to_next_wft_started
314
- .pop()
315
- .expect("There is an element here by definition"),
316
- );
600
+ // If we've never seen an interesting event and the next two events are a completion
601
+ // followed immediately again by scheduled, then this is a WFT heartbeat and also
602
+ // doesn't conclude the sequence.
603
+ else if et == EventType::WorkflowTaskCompleted {
604
+ if let Some(next_next_event) = events.get(ix + 2) {
605
+ if next_next_event.event_type() == EventType::WorkflowTaskScheduled {
606
+ continue;
607
+ } else {
608
+ saw_any_non_wft_event = true;
609
+ }
317
610
  }
318
- extra_e.push(e);
319
- break;
320
611
  }
321
- events_to_next_wft_started.push(e);
612
+ }
613
+ if saw_any_non_wft_event {
614
+ return NextWFTSeqEndIndex::Complete(ix);
322
615
  }
323
616
  }
324
-
325
- Ok((events_to_next_wft_started, extra_e))
326
617
  }
327
- }
328
618
 
329
- #[derive(Eq, PartialEq, Debug)]
330
- enum NextWftState {
331
- NotSeen,
332
- Seen,
333
- SeenCompleted,
619
+ NextWFTSeqEndIndex::Incomplete(last_index)
334
620
  }
335
621
 
336
- impl From<HistoryInfo> for HistoryUpdate {
337
- fn from(v: HistoryInfo) -> Self {
338
- Self::new_from_events(v.events().to_vec(), v.previous_started_event_id())
622
+ #[cfg(test)]
623
+ pub mod tests {
624
+ use super::*;
625
+ use crate::{
626
+ replay::{HistoryInfo, TestHistoryBuilder},
627
+ test_help::canned_histories,
628
+ worker::client::mocks::mock_workflow_client,
629
+ };
630
+ use futures_util::TryStreamExt;
631
+ use temporal_sdk_core_protos::temporal::api::workflowservice::v1::GetWorkflowExecutionHistoryResponse;
632
+
633
+ impl From<HistoryInfo> for HistoryUpdate {
634
+ fn from(v: HistoryInfo) -> Self {
635
+ Self::new_from_events(v.events().to_vec(), v.previous_started_event_id())
636
+ }
339
637
  }
340
- }
341
638
 
342
- pub trait TestHBExt {
343
- fn as_history_update(&self) -> HistoryUpdate;
344
- }
639
+ pub trait TestHBExt {
640
+ fn as_history_update(&self) -> HistoryUpdate;
641
+ }
345
642
 
346
- impl TestHBExt for TestHistoryBuilder {
347
- fn as_history_update(&self) -> HistoryUpdate {
348
- self.get_full_history_info().unwrap().into()
643
+ impl TestHBExt for TestHistoryBuilder {
644
+ fn as_history_update(&self) -> HistoryUpdate {
645
+ self.get_full_history_info().unwrap().into()
646
+ }
349
647
  }
350
- }
351
648
 
352
- #[cfg(test)]
353
- pub mod tests {
354
- use super::*;
355
- use crate::{test_help::canned_histories, worker::client::mocks::mock_workflow_client};
649
+ impl NextWFT {
650
+ fn unwrap_events(self) -> Vec<HistoryEvent> {
651
+ match self {
652
+ NextWFT::WFT(e) => e,
653
+ o => panic!("Must be complete WFT: {o:?}"),
654
+ }
655
+ }
656
+ }
356
657
 
357
- #[tokio::test]
358
- async fn consumes_standard_wft_sequence() {
658
+ fn next_check_peek(update: &mut HistoryUpdate, from_id: i64) -> Vec<HistoryEvent> {
659
+ let seq_peeked = update.peek_next_wft_sequence(from_id).to_vec();
660
+ let seq = update.take_next_wft_sequence(from_id).unwrap_events();
661
+ assert_eq!(seq, seq_peeked);
662
+ seq
663
+ }
664
+
665
+ #[test]
666
+ fn consumes_standard_wft_sequence() {
359
667
  let timer_hist = canned_histories::single_timer("t");
360
668
  let mut update = timer_hist.as_history_update();
361
- let seq_1 = update.take_next_wft_sequence(0).await.unwrap();
669
+ let seq_1 = next_check_peek(&mut update, 0);
362
670
  assert_eq!(seq_1.len(), 3);
363
671
  assert_eq!(seq_1.last().unwrap().event_id, 3);
364
- let seq_2 = update.take_next_wft_sequence(3).await.unwrap();
672
+ let seq_2_peeked = update.peek_next_wft_sequence(0).to_vec();
673
+ let seq_2 = next_check_peek(&mut update, 3);
674
+ assert_eq!(seq_2, seq_2_peeked);
365
675
  assert_eq!(seq_2.len(), 5);
366
676
  assert_eq!(seq_2.last().unwrap().event_id, 8);
367
677
  }
368
678
 
369
- #[tokio::test]
370
- async fn skips_wft_failed() {
679
+ #[test]
680
+ fn skips_wft_failed() {
371
681
  let failed_hist = canned_histories::workflow_fails_with_reset_after_timer("t", "runid");
372
682
  let mut update = failed_hist.as_history_update();
373
- let seq_1 = update.take_next_wft_sequence(0).await.unwrap();
683
+ let seq_1 = next_check_peek(&mut update, 0);
374
684
  assert_eq!(seq_1.len(), 3);
375
685
  assert_eq!(seq_1.last().unwrap().event_id, 3);
376
- let seq_2 = update.take_next_wft_sequence(3).await.unwrap();
686
+ let seq_2 = next_check_peek(&mut update, 3);
377
687
  assert_eq!(seq_2.len(), 8);
378
688
  assert_eq!(seq_2.last().unwrap().event_id, 11);
379
689
  }
380
690
 
381
- #[tokio::test]
382
- async fn skips_wft_timeout() {
691
+ #[test]
692
+ fn skips_wft_timeout() {
383
693
  let failed_hist = canned_histories::wft_timeout_repro();
384
694
  let mut update = failed_hist.as_history_update();
385
- let seq_1 = update.take_next_wft_sequence(0).await.unwrap();
695
+ let seq_1 = next_check_peek(&mut update, 0);
386
696
  assert_eq!(seq_1.len(), 3);
387
697
  assert_eq!(seq_1.last().unwrap().event_id, 3);
388
- let seq_2 = update.take_next_wft_sequence(3).await.unwrap();
698
+ let seq_2 = next_check_peek(&mut update, 3);
389
699
  assert_eq!(seq_2.len(), 11);
390
700
  assert_eq!(seq_2.last().unwrap().event_id, 14);
391
701
  }
392
702
 
393
- #[tokio::test]
394
- async fn skips_events_before_desired_wft() {
703
+ #[test]
704
+ fn skips_events_before_desired_wft() {
395
705
  let timer_hist = canned_histories::single_timer("t");
396
706
  let mut update = timer_hist.as_history_update();
397
707
  // We haven't processed the first 3 events, but we should still only get the second sequence
398
- let seq_2 = update.take_next_wft_sequence(3).await.unwrap();
708
+ let seq_2 = update.take_next_wft_sequence(3).unwrap_events();
399
709
  assert_eq!(seq_2.len(), 5);
400
710
  assert_eq!(seq_2.last().unwrap().event_id, 8);
401
711
  }
402
712
 
403
- #[tokio::test]
404
- async fn history_ends_abruptly() {
713
+ #[test]
714
+ fn history_ends_abruptly() {
405
715
  let mut timer_hist = canned_histories::single_timer("t");
406
716
  timer_hist.add_workflow_execution_terminated();
407
717
  let mut update = timer_hist.as_history_update();
408
- let seq_2 = update.take_next_wft_sequence(3).await.unwrap();
409
- assert_eq!(seq_2.len(), 5);
410
- assert_eq!(seq_2.last().unwrap().event_id, 8);
718
+ let seq_2 = update.take_next_wft_sequence(3).unwrap_events();
719
+ assert_eq!(seq_2.len(), 6);
720
+ assert_eq!(seq_2.last().unwrap().event_id, 9);
411
721
  }
412
722
 
413
- #[tokio::test]
414
- async fn heartbeats_skipped() {
723
+ #[test]
724
+ fn heartbeats_skipped() {
415
725
  let mut t = TestHistoryBuilder::default();
416
726
  t.add_by_type(EventType::WorkflowExecutionStarted);
417
727
  t.add_full_wf_task();
418
- t.add_full_wf_task();
728
+ t.add_full_wf_task(); // wft started 6
419
729
  t.add_get_event_id(EventType::TimerStarted, None);
730
+ t.add_full_wf_task(); // wft started 10
420
731
  t.add_full_wf_task();
421
732
  t.add_full_wf_task();
422
- t.add_full_wf_task();
423
- t.add_full_wf_task();
733
+ t.add_full_wf_task(); // wft started 19
424
734
  t.add_get_event_id(EventType::TimerStarted, None);
425
- t.add_full_wf_task();
735
+ t.add_full_wf_task(); // wft started 23
426
736
  t.add_we_signaled("whee", vec![]);
427
737
  t.add_full_wf_task();
428
738
  t.add_workflow_execution_completed();
429
739
 
430
740
  let mut update = t.as_history_update();
431
- let seq = update.take_next_wft_sequence(0).await.unwrap();
741
+ let seq = next_check_peek(&mut update, 0);
432
742
  assert_eq!(seq.len(), 6);
433
- let seq = update.take_next_wft_sequence(6).await.unwrap();
743
+ let seq = next_check_peek(&mut update, 6);
434
744
  assert_eq!(seq.len(), 13);
435
- let seq = update.take_next_wft_sequence(19).await.unwrap();
745
+ let seq = next_check_peek(&mut update, 19);
436
746
  assert_eq!(seq.len(), 4);
437
- let seq = update.take_next_wft_sequence(23).await.unwrap();
747
+ let seq = next_check_peek(&mut update, 23);
438
748
  assert_eq!(seq.len(), 4);
439
- let seq = update.take_next_wft_sequence(27).await.unwrap();
749
+ let seq = next_check_peek(&mut update, 27);
440
750
  assert_eq!(seq.len(), 2);
441
751
  }
442
752
 
443
- #[tokio::test]
444
- async fn paginator_fetches_new_pages() {
445
- // Note that this test triggers the "event ids that went backwards" error, acceptably.
446
- // Can be fixed by having mock not return earlier events.
447
- let wft_count = 500;
448
- let long_hist = canned_histories::long_sequential_timers(wft_count);
449
- let initial_hist = long_hist.get_history_info(10).unwrap();
450
- let prev_started = initial_hist.previous_started_event_id();
753
+ #[test]
754
+ fn heartbeat_marker_end() {
755
+ let mut t = TestHistoryBuilder::default();
756
+ t.add_by_type(EventType::WorkflowExecutionStarted);
757
+ t.add_full_wf_task();
758
+ t.add_full_wf_task();
759
+ t.add_local_activity_result_marker(1, "1", "done".into());
760
+ t.add_workflow_execution_completed();
761
+
762
+ let mut update = t.as_history_update();
763
+ let seq = next_check_peek(&mut update, 3);
764
+ // completed, sched, started
765
+ assert_eq!(seq.len(), 3);
766
+ let seq = next_check_peek(&mut update, 6);
767
+ assert_eq!(seq.len(), 3);
768
+ }
769
+
770
+ fn paginator_setup(history: TestHistoryBuilder, chunk_size: usize) -> HistoryPaginator {
771
+ let full_hist = history.get_full_history_info().unwrap().into_events();
772
+ let initial_hist = full_hist.chunks(chunk_size).next().unwrap().to_vec();
451
773
  let mut mock_client = mock_workflow_client();
452
774
 
453
- let mut npt = 2;
775
+ let mut npt = 1;
454
776
  mock_client
455
777
  .expect_get_workflow_execution_history()
456
778
  .returning(move |_, _, passed_npt| {
457
779
  assert_eq!(passed_npt, vec![npt]);
458
- let history = long_hist.get_history_info(10 * npt as usize).unwrap();
780
+ let mut hist_chunks = full_hist.chunks(chunk_size).peekable();
781
+ let next_chunks = hist_chunks.nth(npt.into()).unwrap_or_default();
459
782
  npt += 1;
783
+ let next_page_token = if hist_chunks.peek().is_none() {
784
+ vec![]
785
+ } else {
786
+ vec![npt]
787
+ };
460
788
  Ok(GetWorkflowExecutionHistoryResponse {
461
- history: Some(history.into()),
789
+ history: Some(History {
790
+ events: next_chunks.into(),
791
+ }),
462
792
  raw_history: vec![],
463
- next_page_token: vec![npt],
793
+ next_page_token,
464
794
  archived: false,
465
795
  })
466
796
  });
467
797
 
468
- let mut update = HistoryUpdate::new(
469
- HistoryPaginator::new(
470
- initial_hist.into(),
471
- "wfid".to_string(),
472
- "runid".to_string(),
473
- vec![2], // Start at page "2"
474
- Arc::new(mock_client),
475
- ),
476
- prev_started,
798
+ HistoryPaginator::new(
799
+ History {
800
+ events: initial_hist,
801
+ },
802
+ 0,
803
+ "wfid".to_string(),
804
+ "runid".to_string(),
805
+ vec![1],
806
+ Arc::new(mock_client),
807
+ )
808
+ }
809
+
810
+ #[rstest::rstest]
811
+ #[tokio::test]
812
+ async fn paginator_extracts_updates(#[values(10, 11, 12, 13, 14)] chunk_size: usize) {
813
+ let wft_count = 100;
814
+ let mut paginator = paginator_setup(
815
+ canned_histories::long_sequential_timers(wft_count),
816
+ chunk_size,
477
817
  );
818
+ let mut update = paginator.extract_next_update().await.unwrap();
478
819
 
479
- let seq = update.take_next_wft_sequence(0).await.unwrap();
820
+ let seq = update.take_next_wft_sequence(0).unwrap_events();
480
821
  assert_eq!(seq.len(), 3);
481
822
 
482
823
  let mut last_event_id = 3;
483
824
  let mut last_started_id = 3;
484
- for _ in 1..wft_count {
485
- let seq = update
486
- .take_next_wft_sequence(last_started_id)
487
- .await
488
- .unwrap();
825
+ for i in 1..wft_count {
826
+ let seq = {
827
+ match update.take_next_wft_sequence(last_started_id) {
828
+ NextWFT::WFT(seq) => seq,
829
+ NextWFT::NeedFetch => {
830
+ update = paginator.extract_next_update().await.unwrap();
831
+ update
832
+ .take_next_wft_sequence(last_started_id)
833
+ .unwrap_events()
834
+ }
835
+ NextWFT::ReplayOver => {
836
+ assert_eq!(i, wft_count - 1);
837
+ break;
838
+ }
839
+ }
840
+ };
489
841
  for e in &seq {
490
842
  last_event_id += 1;
491
843
  assert_eq!(e.event_id, last_event_id);
@@ -495,10 +847,124 @@ pub mod tests {
495
847
  }
496
848
  }
497
849
 
850
+ #[tokio::test]
851
+ async fn paginator_streams() {
852
+ let wft_count = 10;
853
+ let paginator = StreamingHistoryPaginator::new(paginator_setup(
854
+ canned_histories::long_sequential_timers(wft_count),
855
+ 10,
856
+ ));
857
+ let everything: Vec<_> = paginator.try_collect().await.unwrap();
858
+ assert_eq!(everything.len(), (wft_count + 1) * 5);
859
+ everything.iter().fold(1, |event_id, e| {
860
+ assert_eq!(event_id, e.event_id);
861
+ e.event_id + 1
862
+ });
863
+ }
864
+
865
+ fn three_wfts_then_heartbeats() -> TestHistoryBuilder {
866
+ let mut t = TestHistoryBuilder::default();
867
+ // Start with two complete normal WFTs
868
+ t.add_by_type(EventType::WorkflowExecutionStarted);
869
+ t.add_full_wf_task(); // wft start - 3
870
+ t.add_get_event_id(EventType::TimerStarted, None);
871
+ t.add_full_wf_task(); // wft start - 7
872
+ t.add_get_event_id(EventType::TimerStarted, None);
873
+ t.add_full_wf_task(); // wft start - 11
874
+ for _ in 1..50 {
875
+ // Add a bunch of heartbeats with no commands, which count as one task
876
+ t.add_full_wf_task();
877
+ }
878
+ t.add_workflow_execution_completed();
879
+ t
880
+ }
881
+
882
+ #[tokio::test]
883
+ async fn needs_fetch_if_ending_in_middle_of_wft_seq() {
884
+ let t = three_wfts_then_heartbeats();
885
+ let mut ends_in_middle_of_seq = t.as_history_update().events;
886
+ ends_in_middle_of_seq.truncate(19);
887
+ // The update should contain the first two complete WFTs, ending on the 8th event which
888
+ // is WFT started. The remaining events should be returned. False flags means the creator
889
+ // knows there are more events, so we should return need fetch
890
+ let (mut update, remaining) = HistoryUpdate::from_events(ends_in_middle_of_seq, 0, false);
891
+ assert_eq!(remaining[0].event_id, 8);
892
+ assert_eq!(remaining.last().unwrap().event_id, 19);
893
+ let seq = update.take_next_wft_sequence(0).unwrap_events();
894
+ assert_eq!(seq.last().unwrap().event_id, 3);
895
+ let seq = update.take_next_wft_sequence(3).unwrap_events();
896
+ assert_eq!(seq.last().unwrap().event_id, 7);
897
+ let next = update.take_next_wft_sequence(7);
898
+ assert_matches!(next, NextWFT::NeedFetch);
899
+ }
900
+
901
+ // Like the above, but if the history happens to be cut off at a wft boundary, (even though
902
+ // there may have been many heartbeats after we have no way of knowing about), it's going to
903
+ // count events 7-20 as a WFT since there is started, completed, timer command, ..heartbeats..
904
+ #[tokio::test]
905
+ async fn needs_fetch_after_complete_seq_with_heartbeats() {
906
+ let t = three_wfts_then_heartbeats();
907
+ let mut ends_in_middle_of_seq = t.as_history_update().events;
908
+ ends_in_middle_of_seq.truncate(20);
909
+ let (mut update, remaining) = HistoryUpdate::from_events(ends_in_middle_of_seq, 0, false);
910
+ assert!(remaining.is_empty());
911
+ let seq = update.take_next_wft_sequence(0).unwrap_events();
912
+ assert_eq!(seq.last().unwrap().event_id, 3);
913
+ let seq = update.take_next_wft_sequence(3).unwrap_events();
914
+ assert_eq!(seq.last().unwrap().event_id, 7);
915
+ let seq = update.take_next_wft_sequence(7).unwrap_events();
916
+ assert_eq!(seq.last().unwrap().event_id, 20);
917
+ let next = update.take_next_wft_sequence(20);
918
+ assert_matches!(next, NextWFT::NeedFetch);
919
+ }
920
+
921
+ #[rstest::rstest]
922
+ #[tokio::test]
923
+ async fn paginator_works_with_wft_over_multiple_pages(
924
+ #[values(10, 11, 12, 13, 14)] chunk_size: usize,
925
+ ) {
926
+ let t = three_wfts_then_heartbeats();
927
+ let mut paginator = paginator_setup(t, chunk_size);
928
+ let mut update = paginator.extract_next_update().await.unwrap();
929
+ let mut last_id = 0;
930
+ loop {
931
+ let seq = update.take_next_wft_sequence(last_id);
932
+ match seq {
933
+ NextWFT::WFT(seq) => {
934
+ last_id = seq.last().unwrap().event_id;
935
+ }
936
+ NextWFT::NeedFetch => {
937
+ update = paginator.extract_next_update().await.unwrap();
938
+ }
939
+ NextWFT::ReplayOver => break,
940
+ }
941
+ }
942
+ assert_eq!(last_id, 160);
943
+ }
944
+
945
+ #[tokio::test]
946
+ async fn task_just_before_heartbeat_chain_is_taken() {
947
+ let t = three_wfts_then_heartbeats();
948
+ let mut update = t.as_history_update();
949
+ let seq = update.take_next_wft_sequence(0).unwrap_events();
950
+ assert_eq!(seq.last().unwrap().event_id, 3);
951
+ let seq = update.take_next_wft_sequence(3).unwrap_events();
952
+ assert_eq!(seq.last().unwrap().event_id, 7);
953
+ let seq = update.take_next_wft_sequence(7).unwrap_events();
954
+ assert_eq!(seq.last().unwrap().event_id, 158);
955
+ let seq = update.take_next_wft_sequence(158).unwrap_events();
956
+ assert_eq!(seq.last().unwrap().event_id, 160);
957
+ assert_eq!(
958
+ seq.last().unwrap().event_type(),
959
+ EventType::WorkflowExecutionCompleted
960
+ );
961
+ }
962
+
498
963
  #[tokio::test]
499
964
  async fn handles_cache_misses() {
500
965
  let timer_hist = canned_histories::single_timer("t");
501
966
  let partial_task = timer_hist.get_one_wft(2).unwrap();
967
+ let prev_started_wft_id = partial_task.previous_started_event_id();
502
968
  let mut history_from_get: GetWorkflowExecutionHistoryResponse =
503
969
  timer_hist.get_history_info(2).unwrap().into();
504
970
  // Chop off the last event, which is WFT started, which server doesn't return in get
@@ -509,24 +975,116 @@ pub mod tests {
509
975
  .expect_get_workflow_execution_history()
510
976
  .returning(move |_, _, _| Ok(history_from_get.clone()));
511
977
 
512
- let mut update = HistoryUpdate::new(
513
- HistoryPaginator::new(
514
- partial_task.into(),
515
- "wfid".to_string(),
516
- "runid".to_string(),
517
- // A cache miss means we'll try to fetch from start
518
- NextPageToken::FetchFromStart,
519
- Arc::new(mock_client),
520
- ),
521
- 1,
978
+ let mut paginator = HistoryPaginator::new(
979
+ partial_task.into(),
980
+ prev_started_wft_id,
981
+ "wfid".to_string(),
982
+ "runid".to_string(),
983
+ // A cache miss means we'll try to fetch from start
984
+ NextPageToken::FetchFromStart,
985
+ Arc::new(mock_client),
522
986
  );
987
+ let mut update = paginator.extract_next_update().await.unwrap();
523
988
  // We expect if we try to take the first task sequence that the first event is the first
524
989
  // event in the sequence.
525
- let seq = update.take_next_wft_sequence(0).await.unwrap();
990
+ let seq = update.take_next_wft_sequence(0).unwrap_events();
526
991
  assert_eq!(seq[0].event_id, 1);
527
- let seq = update.take_next_wft_sequence(3).await.unwrap();
992
+ let seq = update.take_next_wft_sequence(3).unwrap_events();
528
993
  // Verify anything extra (which should only ever be WFT started) was re-appended to the
529
994
  // end of the event iteration after fetching the old history.
530
995
  assert_eq!(seq.last().unwrap().event_id, 8);
531
996
  }
997
+
998
+ #[test]
999
+ fn la_marker_chunking() {
1000
+ let mut t = TestHistoryBuilder::default();
1001
+ t.add_by_type(EventType::WorkflowExecutionStarted);
1002
+ t.add_full_wf_task();
1003
+ t.add_we_signaled("whatever", vec![]);
1004
+ t.add_full_wf_task(); // started - 7
1005
+ t.add_local_activity_result_marker(1, "hi", Default::default());
1006
+ let act_s = t.add_activity_task_scheduled("1");
1007
+ let act_st = t.add_activity_task_started(act_s);
1008
+ t.add_activity_task_completed(act_s, act_st, Default::default());
1009
+ t.add_workflow_task_scheduled_and_started();
1010
+ t.add_workflow_task_timed_out();
1011
+ t.add_workflow_task_scheduled_and_started();
1012
+ t.add_workflow_task_timed_out();
1013
+ t.add_workflow_task_scheduled_and_started();
1014
+
1015
+ let mut update = t.as_history_update();
1016
+ let seq = next_check_peek(&mut update, 0);
1017
+ assert_eq!(seq.len(), 3);
1018
+ let seq = next_check_peek(&mut update, 3);
1019
+ assert_eq!(seq.len(), 4);
1020
+ let seq = next_check_peek(&mut update, 7);
1021
+ assert_eq!(seq.len(), 13);
1022
+ }
1023
+
1024
+ #[tokio::test]
1025
+ async fn handles_blank_fetch_response() {
1026
+ let timer_hist = canned_histories::single_timer("t");
1027
+ let partial_task = timer_hist.get_one_wft(2).unwrap();
1028
+ let prev_started_wft_id = partial_task.previous_started_event_id();
1029
+ let mut mock_client = mock_workflow_client();
1030
+ mock_client
1031
+ .expect_get_workflow_execution_history()
1032
+ .returning(move |_, _, _| Ok(Default::default()));
1033
+
1034
+ let mut paginator = HistoryPaginator::new(
1035
+ partial_task.into(),
1036
+ prev_started_wft_id,
1037
+ "wfid".to_string(),
1038
+ "runid".to_string(),
1039
+ // A cache miss means we'll try to fetch from start
1040
+ NextPageToken::FetchFromStart,
1041
+ Arc::new(mock_client),
1042
+ );
1043
+ let err = paginator.extract_next_update().await.unwrap_err();
1044
+ assert_matches!(err.code(), tonic::Code::DataLoss);
1045
+ }
1046
+
1047
+ #[tokio::test]
1048
+ async fn handles_empty_page_with_next_token() {
1049
+ let timer_hist = canned_histories::single_timer("t");
1050
+ let partial_task = timer_hist.get_one_wft(2).unwrap();
1051
+ let prev_started_wft_id = partial_task.previous_started_event_id();
1052
+ let full_resp: GetWorkflowExecutionHistoryResponse =
1053
+ timer_hist.get_full_history_info().unwrap().into();
1054
+ let mut mock_client = mock_workflow_client();
1055
+ mock_client
1056
+ .expect_get_workflow_execution_history()
1057
+ .returning(move |_, _, _| {
1058
+ Ok(GetWorkflowExecutionHistoryResponse {
1059
+ history: Some(History { events: vec![] }),
1060
+ raw_history: vec![],
1061
+ next_page_token: vec![2],
1062
+ archived: false,
1063
+ })
1064
+ })
1065
+ .times(1);
1066
+ mock_client
1067
+ .expect_get_workflow_execution_history()
1068
+ .returning(move |_, _, _| Ok(full_resp.clone()))
1069
+ .times(1);
1070
+
1071
+ let mut paginator = HistoryPaginator::new(
1072
+ partial_task.into(),
1073
+ prev_started_wft_id,
1074
+ "wfid".to_string(),
1075
+ "runid".to_string(),
1076
+ // A cache miss means we'll try to fetch from start
1077
+ NextPageToken::FetchFromStart,
1078
+ Arc::new(mock_client),
1079
+ );
1080
+ let mut update = paginator.extract_next_update().await.unwrap();
1081
+ let seq = update.take_next_wft_sequence(0).unwrap_events();
1082
+ assert_eq!(seq.last().unwrap().event_id, 3);
1083
+ let seq = update.take_next_wft_sequence(3).unwrap_events();
1084
+ assert_eq!(seq.last().unwrap().event_id, 8);
1085
+ assert_matches!(update.take_next_wft_sequence(8), NextWFT::ReplayOver);
1086
+ }
1087
+
1088
+ // TODO: Test we dont re-feed pointless updates if fetching returns <= events we already
1089
+ // processed
532
1090
  }