/build/source/nativelink-scheduler/src/simple_scheduler_state_manager.rs
Line | Count | Source |
1 | | // Copyright 2024 The NativeLink Authors. All rights reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | use core::ops::Bound; |
16 | | use core::time::Duration; |
17 | | use std::string::ToString; |
18 | | use std::sync::{Arc, Weak}; |
19 | | |
20 | | use async_lock::Mutex; |
21 | | use async_trait::async_trait; |
22 | | use futures::{StreamExt, TryStreamExt, stream}; |
23 | | use nativelink_error::{Code, Error, ResultExt, make_err}; |
24 | | use nativelink_metric::MetricsComponent; |
25 | | use nativelink_util::action_messages::{ |
26 | | ActionInfo, ActionResult, ActionStage, ActionState, ActionUniqueQualifier, ExecutionMetadata, |
27 | | OperationId, WorkerId, |
28 | | }; |
29 | | use nativelink_util::instant_wrapper::InstantWrapper; |
30 | | use nativelink_util::known_platform_property_provider::KnownPlatformPropertyProvider; |
31 | | use nativelink_util::operation_state_manager::{ |
32 | | ActionStateResult, ActionStateResultStream, ClientStateManager, MatchingEngineStateManager, |
33 | | OperationFilter, OperationStageFlags, OrderDirection, UpdateOperationType, WorkerStateManager, |
34 | | }; |
35 | | use nativelink_util::origin_event::OriginMetadata; |
36 | | use tracing::{info, warn}; |
37 | | |
38 | | use super::awaited_action_db::{ |
39 | | AwaitedAction, AwaitedActionDb, AwaitedActionSubscriber, SortedAwaitedActionState, |
40 | | }; |
41 | | |
42 | | /// Maximum number of times an update to the database |
43 | | /// can fail before giving up. |
44 | | const MAX_UPDATE_RETRIES: usize = 5; |
45 | | |
46 | | /// Simple struct that implements the `ActionStateResult` trait and always returns an error. |
47 | | struct ErrorActionStateResult(Error); |
48 | | |
49 | | #[async_trait] |
50 | | impl ActionStateResult for ErrorActionStateResult { |
51 | 0 | async fn as_state(&self) -> Result<(Arc<ActionState>, Option<OriginMetadata>), Error> { |
52 | 0 | Err(self.0.clone()) |
53 | 0 | } |
54 | | |
55 | 0 | async fn changed(&mut self) -> Result<(Arc<ActionState>, Option<OriginMetadata>), Error> { |
56 | 0 | Err(self.0.clone()) |
57 | 0 | } |
58 | | |
59 | 0 | async fn as_action_info(&self) -> Result<(Arc<ActionInfo>, Option<OriginMetadata>), Error> { |
60 | 0 | Err(self.0.clone()) |
61 | 0 | } |
62 | | } |
63 | | |
64 | | struct ClientActionStateResult<U, T, I, NowFn> |
65 | | where |
66 | | U: AwaitedActionSubscriber, |
67 | | T: AwaitedActionDb, |
68 | | I: InstantWrapper, |
69 | | NowFn: Fn() -> I + Clone + Send + Unpin + Sync + 'static, |
70 | | { |
71 | | inner: MatchingEngineActionStateResult<U, T, I, NowFn>, |
72 | | } |
73 | | |
74 | | impl<U, T, I, NowFn> ClientActionStateResult<U, T, I, NowFn> |
75 | | where |
76 | | U: AwaitedActionSubscriber, |
77 | | T: AwaitedActionDb, |
78 | | I: InstantWrapper, |
79 | | NowFn: Fn() -> I + Clone + Send + Unpin + Sync + 'static, |
80 | | { |
81 | 534 | const fn new( |
82 | 534 | sub: U, |
83 | 534 | simple_scheduler_state_manager: Weak<SimpleSchedulerStateManager<T, I, NowFn>>, |
84 | 534 | no_event_action_timeout: Duration, |
85 | 534 | now_fn: NowFn, |
86 | 534 | ) -> Self { |
87 | 534 | Self { |
88 | 534 | inner: MatchingEngineActionStateResult::new( |
89 | 534 | sub, |
90 | 534 | simple_scheduler_state_manager, |
91 | 534 | no_event_action_timeout, |
92 | 534 | now_fn, |
93 | 534 | ), |
94 | 534 | } |
95 | 534 | } |
96 | | } |
97 | | |
98 | | #[async_trait] |
99 | | impl<U, T, I, NowFn> ActionStateResult for ClientActionStateResult<U, T, I, NowFn> |
100 | | where |
101 | | U: AwaitedActionSubscriber, |
102 | | T: AwaitedActionDb, |
103 | | I: InstantWrapper, |
104 | | NowFn: Fn() -> I + Clone + Send + Unpin + Sync + 'static, |
105 | | { |
106 | 12 | async fn as_state(&self) -> Result<(Arc<ActionState>, Option<OriginMetadata>), Error> { |
107 | 6 | self.inner.as_state().await |
108 | 12 | } |
109 | | |
110 | 100 | async fn changed(&mut self) -> Result<(Arc<ActionState>, Option<OriginMetadata>), Error> { |
111 | 50 | self.inner.changed().await |
112 | 96 | } |
113 | | |
114 | 0 | async fn as_action_info(&self) -> Result<(Arc<ActionInfo>, Option<OriginMetadata>), Error> { |
115 | 0 | self.inner.as_action_info().await |
116 | 0 | } |
117 | | } |
118 | | |
119 | | struct MatchingEngineActionStateResult<U, T, I, NowFn> |
120 | | where |
121 | | U: AwaitedActionSubscriber, |
122 | | T: AwaitedActionDb, |
123 | | I: InstantWrapper, |
124 | | NowFn: Fn() -> I + Clone + Send + Unpin + Sync + 'static, |
125 | | { |
126 | | awaited_action_sub: U, |
127 | | simple_scheduler_state_manager: Weak<SimpleSchedulerStateManager<T, I, NowFn>>, |
128 | | no_event_action_timeout: Duration, |
129 | | now_fn: NowFn, |
130 | | } |
131 | | impl<U, T, I, NowFn> MatchingEngineActionStateResult<U, T, I, NowFn> |
132 | | where |
133 | | U: AwaitedActionSubscriber, |
134 | | T: AwaitedActionDb, |
135 | | I: InstantWrapper, |
136 | | NowFn: Fn() -> I + Clone + Send + Unpin + Sync + 'static, |
137 | | { |
138 | 584 | const fn new( |
139 | 584 | awaited_action_sub: U, |
140 | 584 | simple_scheduler_state_manager: Weak<SimpleSchedulerStateManager<T, I, NowFn>>, |
141 | 584 | no_event_action_timeout: Duration, |
142 | 584 | now_fn: NowFn, |
143 | 584 | ) -> Self { |
144 | 584 | Self { |
145 | 584 | awaited_action_sub, |
146 | 584 | simple_scheduler_state_manager, |
147 | 584 | no_event_action_timeout, |
148 | 584 | now_fn, |
149 | 584 | } |
150 | 584 | } |
151 | | } |
152 | | |
153 | | #[async_trait] |
154 | | impl<U, T, I, NowFn> ActionStateResult for MatchingEngineActionStateResult<U, T, I, NowFn> |
155 | | where |
156 | | U: AwaitedActionSubscriber, |
157 | | T: AwaitedActionDb, |
158 | | I: InstantWrapper, |
159 | | NowFn: Fn() -> I + Clone + Send + Unpin + Sync + 'static, |
160 | | { |
161 | 76 | async fn as_state(&self) -> Result<(Arc<ActionState>, Option<OriginMetadata>), Error> { |
162 | 38 | let awaited_action = self |
163 | 38 | .awaited_action_sub |
164 | 38 | .borrow() |
165 | 38 | .await |
166 | 38 | .err_tip(|| "In MatchingEngineActionStateResult::as_state")?0 ; |
167 | 38 | Ok(( |
168 | 38 | awaited_action.state().clone(), |
169 | 38 | awaited_action.maybe_origin_metadata().cloned(), |
170 | 38 | )) |
171 | 76 | } |
172 | | |
173 | 100 | async fn changed(&mut self) -> Result<(Arc<ActionState>, Option<OriginMetadata>), Error> { |
174 | 50 | let mut timeout_attempts = 0; |
175 | | loop { |
176 | 60 | tokio::select! { |
177 | 60 | awaited_action_result46 = self.awaited_action_sub.changed() => { |
178 | 46 | return awaited_action_result |
179 | 46 | .err_tip(|| "In MatchingEngineActionStateResult::changed") |
180 | 46 | .map(|v| (v.state().clone(), v.maybe_origin_metadata().cloned())); |
181 | | } |
182 | 60 | () = (self.now_fn)().sleep(self.no_event_action_timeout) => { |
183 | 10 | // Timeout happened, do additional checks below. |
184 | 10 | } |
185 | | } |
186 | | |
187 | 10 | let awaited_action = self |
188 | 10 | .awaited_action_sub |
189 | 10 | .borrow() |
190 | 10 | .await |
191 | 10 | .err_tip(|| "In MatchingEngineActionStateResult::changed")?0 ; |
192 | | |
193 | 10 | if matches!1 (awaited_action.state().stage, ActionStage::Queued) { |
194 | | // Actions in queued state do not get periodically updated, |
195 | | // so we don't need to timeout them. |
196 | 9 | continue; |
197 | 1 | } |
198 | | |
199 | 1 | let simple_scheduler_state_manager = self |
200 | 1 | .simple_scheduler_state_manager |
201 | 1 | .upgrade() |
202 | 1 | .err_tip(|| format!("Failed to upgrade weak reference to SimpleSchedulerStateManager in MatchingEngineActionStateResult::changed at attempt: {timeout_attempts}"0 ))?0 ; |
203 | | |
204 | 1 | warn!( |
205 | | ?awaited_action, |
206 | 1 | "OperationId {} / {} timed out after {} seconds issuing a retry", |
207 | 1 | awaited_action.operation_id(), |
208 | 1 | awaited_action.state().client_operation_id, |
209 | 1 | self.no_event_action_timeout.as_secs_f32(), |
210 | | ); |
211 | | |
212 | 1 | simple_scheduler_state_manager |
213 | 1 | .timeout_operation_id(awaited_action.operation_id()) |
214 | 1 | .await |
215 | 1 | .err_tip(|| "In MatchingEngineActionStateResult::changed")?0 ; |
216 | | |
217 | 1 | if timeout_attempts >= MAX_UPDATE_RETRIES { Branch (217:16): [True: 0, False: 0]
Branch (217:16): [True: 0, False: 0]
Branch (217:16): [Folded - Ignored]
Branch (217:16): [True: 0, False: 0]
Branch (217:16): [True: 0, False: 1]
Branch (217:16): [True: 0, False: 0]
|
218 | 0 | return Err(make_err!( |
219 | 0 | Code::Internal, |
220 | 0 | "Failed to update action after {} retries with no error set in MatchingEngineActionStateResult::changed - {} {:?}", |
221 | 0 | MAX_UPDATE_RETRIES, |
222 | 0 | awaited_action.operation_id(), |
223 | 0 | awaited_action.state().stage, |
224 | 0 | )); |
225 | 1 | } |
226 | 1 | timeout_attempts += 1; |
227 | | } |
228 | 96 | } |
229 | | |
230 | 100 | async fn as_action_info(&self) -> Result<(Arc<ActionInfo>, Option<OriginMetadata>), Error> { |
231 | 50 | let awaited_action = self |
232 | 50 | .awaited_action_sub |
233 | 50 | .borrow() |
234 | 50 | .await |
235 | 50 | .err_tip(|| "In MatchingEngineActionStateResult::as_action_info")?0 ; |
236 | 50 | Ok(( |
237 | 50 | awaited_action.action_info().clone(), |
238 | 50 | awaited_action.maybe_origin_metadata().cloned(), |
239 | 50 | )) |
240 | 100 | } |
241 | | } |
242 | | |
243 | | /// `SimpleSchedulerStateManager` is responsible for maintaining the state of the scheduler. |
244 | | /// Scheduler state includes the actions that are queued, active, and recently completed. |
245 | | /// It also includes the workers that are available to execute actions based on allocation |
246 | | /// strategy. |
247 | | #[derive(MetricsComponent)] |
248 | | pub(crate) struct SimpleSchedulerStateManager<T, I, NowFn> |
249 | | where |
250 | | T: AwaitedActionDb, |
251 | | I: InstantWrapper, |
252 | | NowFn: Fn() -> I + Clone + Send + Unpin + Sync + 'static, |
253 | | { |
254 | | /// Database for storing the state of all actions. |
255 | | #[metric(group = "action_db")] |
256 | | action_db: T, |
257 | | |
258 | | /// Maximum number of times a job can be retried. |
259 | | // TODO(palfrey) This should be a scheduler decorator instead |
260 | | // of always having it on every SimpleScheduler. |
261 | | #[metric(help = "Maximum number of times a job can be retried")] |
262 | | max_job_retries: usize, |
263 | | |
264 | | /// Duration after which an action is considered to be timed out if |
265 | | /// no event is received. |
266 | | #[metric( |
267 | | help = "Duration after which an action is considered to be timed out if no event is received" |
268 | | )] |
269 | | no_event_action_timeout: Duration, |
270 | | |
271 | | /// Mark operation as timed out if the worker has not updated in this duration. |
272 | | /// This is used to prevent operations from being stuck in the queue forever |
273 | | /// if it is not being processed by any worker. |
274 | | client_action_timeout: Duration, |
275 | | |
276 | | // A lock to ensure only one timeout operation is running at a time |
277 | | // on this service. |
278 | | timeout_operation_mux: Mutex<()>, |
279 | | |
280 | | /// Weak reference to self. |
281 | | // We use a weak reference to reduce the risk of a memory leak from |
282 | | // future changes. If this becomes some kind of performance issue, |
283 | | // we can consider using a strong reference. |
284 | | weak_self: Weak<Self>, |
285 | | |
286 | | /// Function to get the current time. |
287 | | now_fn: NowFn, |
288 | | } |
289 | | |
290 | | impl<T, I, NowFn> SimpleSchedulerStateManager<T, I, NowFn> |
291 | | where |
292 | | T: AwaitedActionDb, |
293 | | I: InstantWrapper, |
294 | | NowFn: Fn() -> I + Clone + Send + Unpin + Sync + 'static, |
295 | | { |
296 | 23 | pub(crate) fn new( |
297 | 23 | max_job_retries: usize, |
298 | 23 | no_event_action_timeout: Duration, |
299 | 23 | client_action_timeout: Duration, |
300 | 23 | action_db: T, |
301 | 23 | now_fn: NowFn, |
302 | 23 | ) -> Arc<Self> { |
303 | 23 | Arc::new_cyclic(|weak_self| Self { |
304 | 23 | action_db, |
305 | 23 | max_job_retries, |
306 | 23 | no_event_action_timeout, |
307 | 23 | client_action_timeout, |
308 | 23 | timeout_operation_mux: Mutex::new(()), |
309 | 23 | weak_self: weak_self.clone(), |
310 | 23 | now_fn, |
311 | 23 | }) |
312 | 23 | } |
313 | | |
314 | 554 | async fn apply_filter_predicate( |
315 | 554 | &self, |
316 | 554 | awaited_action: &AwaitedAction, |
317 | 554 | filter: &OperationFilter, |
318 | 554 | ) -> bool { |
319 | | // Note: The caller must filter `client_operation_id`. |
320 | | |
321 | 554 | if awaited_action.last_client_keepalive_timestamp() + self.client_action_timeout Branch (321:12): [True: 0, False: 0]
Branch (321:12): [True: 0, False: 0]
Branch (321:12): [Folded - Ignored]
Branch (321:12): [True: 0, False: 6]
Branch (321:12): [True: 0, False: 546]
Branch (321:12): [True: 0, False: 2]
|
322 | 554 | < (self.now_fn)().now() |
323 | | { |
324 | 0 | if !awaited_action.state().stage.is_finished() { Branch (324:16): [True: 0, False: 0]
Branch (324:16): [True: 0, False: 0]
Branch (324:16): [Folded - Ignored]
Branch (324:16): [True: 0, False: 0]
Branch (324:16): [True: 0, False: 0]
Branch (324:16): [True: 0, False: 0]
|
325 | 0 | let mut state = awaited_action.state().as_ref().clone(); |
326 | 0 | state.stage = ActionStage::Completed(ActionResult { |
327 | 0 | error: Some(make_err!( |
328 | 0 | Code::DeadlineExceeded, |
329 | 0 | "Operation timed out {} seconds of having no more clients listening", |
330 | 0 | self.client_action_timeout.as_secs_f32(), |
331 | 0 | )), |
332 | 0 | ..ActionResult::default() |
333 | 0 | }); |
334 | 0 | let mut new_awaited_action = awaited_action.clone(); |
335 | 0 | new_awaited_action.worker_set_state(Arc::new(state), (self.now_fn)().now()); |
336 | 0 | if let Err(err) = self Branch (336:24): [True: 0, False: 0]
Branch (336:24): [True: 0, False: 0]
Branch (336:24): [Folded - Ignored]
Branch (336:24): [True: 0, False: 0]
Branch (336:24): [True: 0, False: 0]
Branch (336:24): [True: 0, False: 0]
|
337 | 0 | .action_db |
338 | 0 | .update_awaited_action(new_awaited_action) |
339 | 0 | .await |
340 | | { |
341 | 0 | warn!( |
342 | 0 | "Failed to update action to timed out state after client keepalive timeout. This is ok if multiple schedulers tried to set the state at the same time: {err}", |
343 | | ); |
344 | 0 | } |
345 | 0 | } |
346 | 0 | return false; |
347 | 554 | } |
348 | | |
349 | 554 | if let Some(operation_id0 ) = &filter.operation_id { Branch (349:16): [True: 0, False: 0]
Branch (349:16): [True: 0, False: 0]
Branch (349:16): [Folded - Ignored]
Branch (349:16): [True: 0, False: 6]
Branch (349:16): [True: 0, False: 546]
Branch (349:16): [True: 0, False: 2]
|
350 | 0 | if operation_id != awaited_action.operation_id() { Branch (350:16): [True: 0, False: 0]
Branch (350:16): [True: 0, False: 0]
Branch (350:16): [Folded - Ignored]
Branch (350:16): [True: 0, False: 0]
Branch (350:16): [True: 0, False: 0]
Branch (350:16): [True: 0, False: 0]
|
351 | 0 | return false; |
352 | 0 | } |
353 | 554 | } |
354 | | |
355 | 554 | if filter.worker_id.is_some() && filter.worker_id.as_ref()0 != awaited_action.worker_id() { Branch (355:12): [True: 0, False: 0]
Branch (355:42): [True: 0, False: 0]
Branch (355:12): [True: 0, False: 0]
Branch (355:42): [True: 0, False: 0]
Branch (355:12): [Folded - Ignored]
Branch (355:42): [Folded - Ignored]
Branch (355:12): [True: 0, False: 6]
Branch (355:42): [True: 0, False: 0]
Branch (355:12): [True: 0, False: 546]
Branch (355:42): [True: 0, False: 0]
Branch (355:12): [True: 0, False: 2]
Branch (355:42): [True: 0, False: 0]
|
356 | 0 | return false; |
357 | 554 | } |
358 | | |
359 | | { |
360 | 554 | if let Some(filter_unique_key0 ) = &filter.unique_key { Branch (360:20): [True: 0, False: 0]
Branch (360:20): [True: 0, False: 0]
Branch (360:20): [Folded - Ignored]
Branch (360:20): [True: 0, False: 6]
Branch (360:20): [True: 0, False: 546]
Branch (360:20): [True: 0, False: 2]
|
361 | 0 | match &awaited_action.action_info().unique_qualifier { |
362 | 0 | ActionUniqueQualifier::Cacheable(unique_key) => { |
363 | 0 | if filter_unique_key != unique_key { Branch (363:28): [True: 0, False: 0]
Branch (363:28): [True: 0, False: 0]
Branch (363:28): [Folded - Ignored]
Branch (363:28): [True: 0, False: 0]
Branch (363:28): [True: 0, False: 0]
Branch (363:28): [True: 0, False: 0]
|
364 | 0 | return false; |
365 | 0 | } |
366 | | } |
367 | | ActionUniqueQualifier::Uncacheable(_) => { |
368 | 0 | return false; |
369 | | } |
370 | | } |
371 | 554 | } |
372 | 554 | if let Some(action_digest0 ) = filter.action_digest { Branch (372:20): [True: 0, False: 0]
Branch (372:20): [True: 0, False: 0]
Branch (372:20): [Folded - Ignored]
Branch (372:20): [True: 0, False: 6]
Branch (372:20): [True: 0, False: 546]
Branch (372:20): [True: 0, False: 2]
|
373 | 0 | if action_digest != awaited_action.action_info().digest() { Branch (373:20): [True: 0, False: 0]
Branch (373:20): [True: 0, False: 0]
Branch (373:20): [Folded - Ignored]
Branch (373:20): [True: 0, False: 0]
Branch (373:20): [True: 0, False: 0]
Branch (373:20): [True: 0, False: 0]
|
374 | 0 | return false; |
375 | 0 | } |
376 | 554 | } |
377 | | } |
378 | | |
379 | | { |
380 | 554 | let last_worker_update_timestamp = awaited_action.last_worker_updated_timestamp(); |
381 | 554 | if let Some(worker_update_before0 ) = filter.worker_update_before { Branch (381:20): [True: 0, False: 0]
Branch (381:20): [True: 0, False: 0]
Branch (381:20): [Folded - Ignored]
Branch (381:20): [True: 0, False: 6]
Branch (381:20): [True: 0, False: 546]
Branch (381:20): [True: 0, False: 2]
|
382 | 0 | if worker_update_before < last_worker_update_timestamp { Branch (382:20): [True: 0, False: 0]
Branch (382:20): [True: 0, False: 0]
Branch (382:20): [Folded - Ignored]
Branch (382:20): [True: 0, False: 0]
Branch (382:20): [True: 0, False: 0]
Branch (382:20): [True: 0, False: 0]
|
383 | 0 | return false; |
384 | 0 | } |
385 | 554 | } |
386 | 554 | if let Some(completed_before0 ) = filter.completed_before { Branch (386:20): [True: 0, False: 0]
Branch (386:20): [True: 0, False: 0]
Branch (386:20): [Folded - Ignored]
Branch (386:20): [True: 0, False: 6]
Branch (386:20): [True: 0, False: 546]
Branch (386:20): [True: 0, False: 2]
|
387 | 0 | if awaited_action.state().stage.is_finished() Branch (387:20): [True: 0, False: 0]
Branch (387:20): [True: 0, False: 0]
Branch (387:20): [Folded - Ignored]
Branch (387:20): [True: 0, False: 0]
Branch (387:20): [True: 0, False: 0]
Branch (387:20): [True: 0, False: 0]
|
388 | 0 | && completed_before < last_worker_update_timestamp Branch (388:24): [True: 0, False: 0]
Branch (388:24): [True: 0, False: 0]
Branch (388:24): [Folded - Ignored]
Branch (388:24): [True: 0, False: 0]
Branch (388:24): [True: 0, False: 0]
Branch (388:24): [True: 0, False: 0]
|
389 | | { |
390 | 0 | return false; |
391 | 0 | } |
392 | 554 | } |
393 | 554 | if filter.stages != OperationStageFlags::Any { Branch (393:16): [True: 0, False: 0]
Branch (393:16): [True: 0, False: 0]
Branch (393:16): [Folded - Ignored]
Branch (393:16): [True: 5, False: 1]
Branch (393:16): [True: 543, False: 3]
Branch (393:16): [True: 2, False: 0]
|
394 | 550 | let stage_flag = match awaited_action.state().stage { |
395 | 0 | ActionStage::Unknown => OperationStageFlags::Any, |
396 | 0 | ActionStage::CacheCheck => OperationStageFlags::CacheCheck, |
397 | 550 | ActionStage::Queued => OperationStageFlags::Queued, |
398 | 0 | ActionStage::Executing => OperationStageFlags::Executing, |
399 | | ActionStage::Completed(_) | ActionStage::CompletedFromCache(_) => { |
400 | 0 | OperationStageFlags::Completed |
401 | | } |
402 | | }; |
403 | 550 | if !filter.stages.intersects(stage_flag) { Branch (403:20): [True: 0, False: 0]
Branch (403:20): [True: 0, False: 0]
Branch (403:20): [Folded - Ignored]
Branch (403:20): [True: 0, False: 5]
Branch (403:20): [True: 0, False: 543]
Branch (403:20): [True: 0, False: 2]
|
404 | 0 | return false; |
405 | 550 | } |
406 | 4 | } |
407 | | } |
408 | | |
409 | 554 | true |
410 | 554 | } |
411 | | |
412 | | /// Let the scheduler know that an operation has timed out from |
413 | | /// the client side (ie: worker has not updated in a while). |
414 | 1 | async fn timeout_operation_id(&self, operation_id: &OperationId) -> Result<(), Error> { |
415 | | // Ensure that only one timeout operation is running at a time. |
416 | | // Failing to do this could result in the same operation being |
417 | | // timed out multiple times at the same time. |
418 | | // Note: We could implement this on a per-operation_id basis, but it is quite |
419 | | // complex to manage the locks. |
420 | 1 | let _lock = self.timeout_operation_mux.lock().await; |
421 | | |
422 | 1 | let awaited_action_subscriber = self |
423 | 1 | .action_db |
424 | 1 | .get_by_operation_id(operation_id) |
425 | 1 | .await |
426 | 1 | .err_tip(|| "In SimpleSchedulerStateManager::timeout_operation_id")?0 |
427 | 1 | .err_tip(|| {0 |
428 | 0 | format!("Operation id {operation_id} does not exist in SimpleSchedulerStateManager::timeout_operation_id") |
429 | 0 | })?; |
430 | | |
431 | 1 | let awaited_action = awaited_action_subscriber |
432 | 1 | .borrow() |
433 | 1 | .await |
434 | 1 | .err_tip(|| "In SimpleSchedulerStateManager::timeout_operation_id")?0 ; |
435 | | |
436 | | // If the action is not executing, we should not timeout the action. |
437 | 1 | if !matches!0 (awaited_action.state().stage, ActionStage::Executing) { Branch (437:12): [True: 0, False: 0]
Branch (437:12): [True: 0, False: 0]
Branch (437:12): [Folded - Ignored]
Branch (437:12): [True: 0, False: 0]
Branch (437:12): [True: 0, False: 1]
Branch (437:12): [True: 0, False: 0]
|
438 | 0 | return Ok(()); |
439 | 1 | } |
440 | | |
441 | 1 | let worker_should_update_before = awaited_action |
442 | 1 | .last_worker_updated_timestamp() |
443 | 1 | .checked_add(self.no_event_action_timeout) |
444 | 1 | .ok_or_else(|| {0 |
445 | 0 | make_err!( |
446 | 0 | Code::Internal, |
447 | | "Timestamp overflow for operation {operation_id} in SimpleSchedulerStateManager::timeout_operation_id" |
448 | | ) |
449 | 0 | })?; |
450 | 1 | if worker_should_update_before >= (self.now_fn)().now() { Branch (450:12): [True: 0, False: 0]
Branch (450:12): [True: 0, False: 0]
Branch (450:12): [Folded - Ignored]
Branch (450:12): [True: 0, False: 0]
Branch (450:12): [True: 0, False: 1]
Branch (450:12): [True: 0, False: 0]
|
451 | | // The action was updated recently, we should not timeout the action. |
452 | | // This is to prevent timing out actions that have recently been updated |
453 | | // (like multiple clients timeout the same action at the same time). |
454 | 0 | return Ok(()); |
455 | 1 | } |
456 | | |
457 | 1 | self.assign_operation( |
458 | 1 | operation_id, |
459 | 1 | Err(make_err!( |
460 | 1 | Code::DeadlineExceeded, |
461 | 1 | "Operation timed out after {} seconds", |
462 | 1 | self.no_event_action_timeout.as_secs_f32(), |
463 | 1 | )), |
464 | 1 | ) |
465 | 1 | .await |
466 | 1 | } |
467 | | |
468 | 49 | async fn inner_update_operation( |
469 | 49 | &self, |
470 | 49 | operation_id: &OperationId, |
471 | 49 | maybe_worker_id: Option<&WorkerId>, |
472 | 49 | update: UpdateOperationType, |
473 | 49 | ) -> Result<(), Error> { |
474 | 49 | let mut last_err = None; |
475 | 50 | for _ in 0..MAX_UPDATE_RETRIES { |
476 | 50 | let maybe_awaited_action_subscriber = self |
477 | 50 | .action_db |
478 | 50 | .get_by_operation_id(operation_id) |
479 | 50 | .await |
480 | 50 | .err_tip(|| "In SimpleSchedulerStateManager::update_operation")?0 ; |
481 | 50 | let Some(awaited_action_subscriber49 ) = maybe_awaited_action_subscriber else { Branch (481:17): [True: 0, False: 0]
Branch (481:17): [True: 0, False: 0]
Branch (481:17): [Folded - Ignored]
Branch (481:17): [True: 8, False: 0]
Branch (481:17): [True: 39, False: 0]
Branch (481:17): [True: 2, False: 1]
|
482 | | // No action found. It is ok if the action was not found. It |
483 | | // probably means that the action was dropped, but worker was |
484 | | // still processing it. |
485 | 1 | return Ok(()); |
486 | | }; |
487 | | |
488 | 49 | let mut awaited_action = awaited_action_subscriber |
489 | 49 | .borrow() |
490 | 49 | .await |
491 | 49 | .err_tip(|| "In SimpleSchedulerStateManager::update_operation")?0 ; |
492 | | |
493 | | // Make sure the worker id matches the awaited action worker id. |
494 | | // This might happen if the worker sending the update is not the |
495 | | // worker that was assigned. |
496 | 49 | if awaited_action.worker_id().is_some() Branch (496:16): [True: 0, False: 0]
Branch (496:16): [True: 0, False: 0]
Branch (496:16): [Folded - Ignored]
Branch (496:16): [True: 4, False: 4]
Branch (496:16): [True: 13, False: 26]
Branch (496:16): [True: 0, False: 2]
|
497 | 17 | && maybe_worker_id.is_some() Branch (497:20): [True: 0, False: 0]
Branch (497:20): [True: 0, False: 0]
Branch (497:20): [Folded - Ignored]
Branch (497:20): [True: 4, False: 0]
Branch (497:20): [True: 12, False: 1]
Branch (497:20): [True: 0, False: 0]
|
498 | 16 | && maybe_worker_id != awaited_action.worker_id() Branch (498:20): [True: 0, False: 0]
Branch (498:20): [True: 0, False: 0]
Branch (498:20): [Folded - Ignored]
Branch (498:20): [True: 0, False: 4]
Branch (498:20): [True: 0, False: 12]
Branch (498:20): [True: 0, False: 0]
|
499 | | { |
500 | | // If another worker is already assigned to the action, another |
501 | | // worker probably picked up the action. We should not update the |
502 | | // action in this case and abort this operation. |
503 | 0 | let err = make_err!( |
504 | 0 | Code::Aborted, |
505 | | "Worker ids do not match - {:?} != {:?} for {:?}", |
506 | | maybe_worker_id, |
507 | 0 | awaited_action.worker_id(), |
508 | | awaited_action, |
509 | | ); |
510 | 0 | info!( |
511 | 0 | "Worker ids do not match - {:?} != {:?} for {:?}. This is probably due to another worker picking up the action.", |
512 | | maybe_worker_id, |
513 | 0 | awaited_action.worker_id(), |
514 | | awaited_action, |
515 | | ); |
516 | 0 | return Err(err); |
517 | 49 | } |
518 | | |
519 | | // Make sure we don't update an action that is already completed. |
520 | 49 | if awaited_action.state().stage.is_finished() { Branch (520:16): [True: 0, False: 0]
Branch (520:16): [True: 0, False: 0]
Branch (520:16): [Folded - Ignored]
Branch (520:16): [True: 0, False: 8]
Branch (520:16): [True: 0, False: 39]
Branch (520:16): [True: 0, False: 2]
|
521 | 0 | return Err(make_err!( |
522 | 0 | Code::Internal, |
523 | 0 | "Action {operation_id:?} is already completed with state {:?} - maybe_worker_id: {:?}", |
524 | 0 | awaited_action.state().stage, |
525 | 0 | maybe_worker_id, |
526 | 0 | )); |
527 | 49 | } |
528 | | |
529 | 49 | let stage = match &update { |
530 | | UpdateOperationType::KeepAlive => { |
531 | 0 | awaited_action.worker_keep_alive((self.now_fn)().now()); |
532 | 0 | return self |
533 | 0 | .action_db |
534 | 0 | .update_awaited_action(awaited_action) |
535 | 0 | .await |
536 | 0 | .err_tip(|| "Failed to send KeepAlive in SimpleSchedulerStateManager::update_operation"); |
537 | | } |
538 | 37 | UpdateOperationType::UpdateWithActionStage(stage) => stage.clone(), |
539 | 12 | UpdateOperationType::UpdateWithError(err) => { |
540 | | // Don't count a backpressure failure as an attempt for an action. |
541 | 12 | let due_to_backpressure = err.code == Code::ResourceExhausted; |
542 | 12 | if !due_to_backpressure { Branch (542:24): [True: 0, False: 0]
Branch (542:24): [True: 0, False: 0]
Branch (542:24): [Folded - Ignored]
Branch (542:24): [True: 4, False: 0]
Branch (542:24): [True: 8, False: 0]
Branch (542:24): [True: 0, False: 0]
|
543 | 12 | awaited_action.attempts += 1; |
544 | 12 | }0 |
545 | | |
546 | 12 | if awaited_action.attempts > self.max_job_retries { Branch (546:24): [True: 0, False: 0]
Branch (546:24): [True: 0, False: 0]
Branch (546:24): [Folded - Ignored]
Branch (546:24): [True: 1, False: 3]
Branch (546:24): [True: 1, False: 7]
Branch (546:24): [True: 0, False: 0]
|
547 | 2 | ActionStage::Completed(ActionResult { |
548 | 2 | execution_metadata: ExecutionMetadata { |
549 | 2 | worker: maybe_worker_id.map_or_else(String::default, ToString::to_string), |
550 | 2 | ..ExecutionMetadata::default() |
551 | 2 | }, |
552 | 2 | error: Some(err.clone().merge(make_err!( |
553 | 2 | Code::Internal, |
554 | 2 | "Job cancelled because it attempted to execute too many times {} > {} times {}", |
555 | 2 | awaited_action.attempts, |
556 | 2 | self.max_job_retries, |
557 | 2 | format!("for operation_id: {operation_id}, maybe_worker_id: {maybe_worker_id:?}"), |
558 | 2 | ))), |
559 | 2 | ..ActionResult::default() |
560 | 2 | }) |
561 | | } else { |
562 | 10 | ActionStage::Queued |
563 | | } |
564 | | } |
565 | 0 | UpdateOperationType::UpdateWithDisconnect => ActionStage::Queued, |
566 | | }; |
567 | 49 | let now = (self.now_fn)().now(); |
568 | 49 | if matches!39 (stage, ActionStage::Queued) { |
569 | 10 | // If the action is queued, we need to unset the worker id regardless of |
570 | 10 | // which worker sent the update. |
571 | 10 | awaited_action.set_worker_id(None, now); |
572 | 39 | } else { |
573 | 39 | awaited_action.set_worker_id(maybe_worker_id.cloned(), now); |
574 | 39 | } |
575 | 49 | awaited_action.worker_set_state( |
576 | 49 | Arc::new(ActionState { |
577 | 49 | stage, |
578 | 49 | // Client id is not known here, it is the responsibility of |
579 | 49 | // the the subscriber impl to replace this with the |
580 | 49 | // correct client id. |
581 | 49 | client_operation_id: operation_id.clone(), |
582 | 49 | action_digest: awaited_action.action_info().digest(), |
583 | 49 | }), |
584 | 49 | now, |
585 | | ); |
586 | | |
587 | 49 | let update_action_result = self |
588 | 49 | .action_db |
589 | 49 | .update_awaited_action(awaited_action) |
590 | 49 | .await |
591 | 49 | .err_tip(|| "In SimpleSchedulerStateManager::update_operation"); |
592 | 49 | if let Err(err2 ) = update_action_result { Branch (592:20): [True: 0, False: 0]
Branch (592:20): [True: 0, False: 0]
Branch (592:20): [Folded - Ignored]
Branch (592:20): [True: 0, False: 8]
Branch (592:20): [True: 0, False: 39]
Branch (592:20): [True: 2, False: 0]
|
593 | | // We use Aborted to signal that the action was not |
594 | | // updated due to the data being set was not the latest |
595 | | // but can be retried. |
596 | 2 | if err.code == Code::Aborted { Branch (596:20): [True: 0, False: 0]
Branch (596:20): [True: 0, False: 0]
Branch (596:20): [Folded - Ignored]
Branch (596:20): [True: 0, False: 0]
Branch (596:20): [True: 0, False: 0]
Branch (596:20): [True: 1, False: 1]
|
597 | 1 | last_err = Some(err); |
598 | 1 | continue; |
599 | 1 | } |
600 | 1 | return Err(err); |
601 | 47 | } |
602 | 47 | return Ok(()); |
603 | | } |
604 | 0 | Err(last_err.unwrap_or_else(|| { |
605 | 0 | make_err!( |
606 | 0 | Code::Internal, |
607 | | "Failed to update action after {} retries with no error set", |
608 | | MAX_UPDATE_RETRIES, |
609 | | ) |
610 | 0 | })) |
611 | 49 | } |
612 | | |
613 | 30 | async fn inner_add_operation( |
614 | 30 | &self, |
615 | 30 | new_client_operation_id: OperationId, |
616 | 30 | action_info: Arc<ActionInfo>, |
617 | 30 | ) -> Result<T::Subscriber, Error> { |
618 | 30 | self.action_db |
619 | 30 | .add_action( |
620 | 30 | new_client_operation_id, |
621 | 30 | action_info, |
622 | 30 | self.no_event_action_timeout, |
623 | 30 | ) |
624 | 30 | .await |
625 | 30 | .err_tip(|| "In SimpleSchedulerStateManager::add_operation") |
626 | 30 | } |
627 | | |
628 | 615 | async fn inner_filter_operations<'a, F>( |
629 | 615 | &'a self, |
630 | 615 | filter: OperationFilter, |
631 | 615 | to_action_state_result: F, |
632 | 615 | ) -> Result<ActionStateResultStream<'a>, Error> |
633 | 615 | where |
634 | 615 | F: Fn(T::Subscriber) -> Box<dyn ActionStateResult> + Send + Sync + 'a, |
635 | 615 | { |
636 | 611 | const fn sorted_awaited_action_state_for_flags( |
637 | 611 | stage: OperationStageFlags, |
638 | 611 | ) -> Option<SortedAwaitedActionState> { |
639 | 611 | match stage { |
640 | 0 | OperationStageFlags::CacheCheck => Some(SortedAwaitedActionState::CacheCheck), |
641 | 611 | OperationStageFlags::Queued => Some(SortedAwaitedActionState::Queued), |
642 | 0 | OperationStageFlags::Executing => Some(SortedAwaitedActionState::Executing), |
643 | 0 | OperationStageFlags::Completed => Some(SortedAwaitedActionState::Completed), |
644 | 0 | _ => None, |
645 | | } |
646 | 611 | } |
647 | | |
648 | 615 | if let Some(operation_id0 ) = &filter.operation_id { Branch (648:16): [True: 0, False: 0]
Branch (648:16): [True: 0, False: 0]
Branch (648:16): [True: 0, False: 0]
Branch (648:16): [True: 0, False: 0]
Branch (648:16): [Folded - Ignored]
Branch (648:16): [True: 0, False: 1]
Branch (648:16): [True: 0, False: 17]
Branch (648:16): [True: 0, False: 503]
Branch (648:16): [True: 0, False: 90]
Branch (648:16): [True: 0, False: 0]
Branch (648:16): [True: 0, False: 4]
|
649 | 0 | let maybe_subscriber = self |
650 | 0 | .action_db |
651 | 0 | .get_by_operation_id(operation_id) |
652 | 0 | .await |
653 | 0 | .err_tip(|| "In SimpleSchedulerStateManager::filter_operations")?; |
654 | 0 | let Some(subscriber) = maybe_subscriber else { Branch (654:17): [True: 0, False: 0]
Branch (654:17): [True: 0, False: 0]
Branch (654:17): [True: 0, False: 0]
Branch (654:17): [True: 0, False: 0]
Branch (654:17): [Folded - Ignored]
Branch (654:17): [True: 0, False: 0]
Branch (654:17): [True: 0, False: 0]
Branch (654:17): [True: 0, False: 0]
Branch (654:17): [True: 0, False: 0]
Branch (654:17): [True: 0, False: 0]
Branch (654:17): [True: 0, False: 0]
|
655 | 0 | return Ok(Box::pin(stream::empty())); |
656 | | }; |
657 | 0 | let awaited_action = subscriber |
658 | 0 | .borrow() |
659 | 0 | .await |
660 | 0 | .err_tip(|| "In SimpleSchedulerStateManager::filter_operations")?; |
661 | 0 | if !self.apply_filter_predicate(&awaited_action, &filter).await { Branch (661:16): [True: 0, False: 0]
Branch (661:16): [True: 0, False: 0]
Branch (661:16): [True: 0, False: 0]
Branch (661:16): [True: 0, False: 0]
Branch (661:16): [Folded - Ignored]
Branch (661:16): [True: 0, False: 0]
Branch (661:16): [True: 0, False: 0]
Branch (661:16): [True: 0, False: 0]
Branch (661:16): [True: 0, False: 0]
Branch (661:16): [True: 0, False: 0]
Branch (661:16): [True: 0, False: 0]
|
662 | 0 | return Ok(Box::pin(stream::empty())); |
663 | 0 | } |
664 | 0 | return Ok(Box::pin(stream::once(async move { |
665 | 0 | to_action_state_result(subscriber) |
666 | 0 | }))); |
667 | 615 | } |
668 | 615 | if let Some(client_operation_id4 ) = &filter.client_operation_id { Branch (668:16): [True: 0, False: 0]
Branch (668:16): [True: 0, False: 0]
Branch (668:16): [True: 0, False: 0]
Branch (668:16): [True: 0, False: 0]
Branch (668:16): [Folded - Ignored]
Branch (668:16): [True: 1, False: 0]
Branch (668:16): [True: 0, False: 17]
Branch (668:16): [True: 3, False: 500]
Branch (668:16): [True: 0, False: 90]
Branch (668:16): [True: 0, False: 0]
Branch (668:16): [True: 0, False: 4]
|
669 | 4 | let maybe_subscriber = self |
670 | 4 | .action_db |
671 | 4 | .get_awaited_action_by_id(client_operation_id) |
672 | 4 | .await |
673 | 4 | .err_tip(|| "In SimpleSchedulerStateManager::filter_operations")?0 ; |
674 | 4 | let Some(subscriber) = maybe_subscriber else { Branch (674:17): [True: 0, False: 0]
Branch (674:17): [True: 0, False: 0]
Branch (674:17): [True: 0, False: 0]
Branch (674:17): [True: 0, False: 0]
Branch (674:17): [Folded - Ignored]
Branch (674:17): [True: 1, False: 0]
Branch (674:17): [True: 0, False: 0]
Branch (674:17): [True: 3, False: 0]
Branch (674:17): [True: 0, False: 0]
Branch (674:17): [True: 0, False: 0]
Branch (674:17): [True: 0, False: 0]
|
675 | 0 | return Ok(Box::pin(stream::empty())); |
676 | | }; |
677 | 4 | let awaited_action = subscriber |
678 | 4 | .borrow() |
679 | 4 | .await |
680 | 4 | .err_tip(|| "In SimpleSchedulerStateManager::filter_operations")?0 ; |
681 | 4 | if !self.apply_filter_predicate(&awaited_action, &filter).await { Branch (681:16): [True: 0, False: 0]
Branch (681:16): [True: 0, False: 0]
Branch (681:16): [True: 0, False: 0]
Branch (681:16): [True: 0, False: 0]
Branch (681:16): [Folded - Ignored]
Branch (681:16): [True: 0, False: 1]
Branch (681:16): [True: 0, False: 0]
Branch (681:16): [True: 0, False: 3]
Branch (681:16): [True: 0, False: 0]
Branch (681:16): [True: 0, False: 0]
Branch (681:16): [True: 0, False: 0]
|
682 | 0 | return Ok(Box::pin(stream::empty())); |
683 | 4 | } |
684 | 4 | return Ok(Box::pin(stream::once(async move { |
685 | 4 | to_action_state_result(subscriber) |
686 | 4 | }))); |
687 | 611 | } |
688 | | |
689 | 611 | let Some(sorted_awaited_action_state) = Branch (689:13): [True: 0, False: 0]
Branch (689:13): [True: 0, False: 0]
Branch (689:13): [True: 0, False: 0]
Branch (689:13): [True: 0, False: 0]
Branch (689:13): [Folded - Ignored]
Branch (689:13): [True: 0, False: 0]
Branch (689:13): [True: 17, False: 0]
Branch (689:13): [True: 500, False: 0]
Branch (689:13): [True: 90, False: 0]
Branch (689:13): [True: 0, False: 0]
Branch (689:13): [True: 4, False: 0]
|
690 | 611 | sorted_awaited_action_state_for_flags(filter.stages) |
691 | | else { |
692 | 0 | let mut all_items: Vec<_> = self |
693 | 0 | .action_db |
694 | 0 | .get_all_awaited_actions() |
695 | 0 | .await |
696 | 0 | .err_tip(|| "In SimpleSchedulerStateManager::filter_operations")? |
697 | 0 | .and_then(|awaited_action_subscriber| async move { |
698 | 0 | let awaited_action = awaited_action_subscriber |
699 | 0 | .borrow() |
700 | 0 | .await |
701 | 0 | .err_tip(|| "In SimpleSchedulerStateManager::filter_operations")?; |
702 | 0 | Ok((awaited_action_subscriber, awaited_action)) |
703 | 0 | }) |
704 | 0 | .try_filter_map(|(subscriber, awaited_action)| { |
705 | 0 | let filter = filter.clone(); |
706 | 0 | async move { |
707 | 0 | if self.apply_filter_predicate(&awaited_action, &filter).await { Branch (707:28): [True: 0, False: 0]
Branch (707:28): [True: 0, False: 0]
Branch (707:28): [True: 0, False: 0]
Branch (707:28): [True: 0, False: 0]
Branch (707:28): [Folded - Ignored]
Branch (707:28): [True: 0, False: 0]
Branch (707:28): [True: 0, False: 0]
Branch (707:28): [True: 0, False: 0]
Branch (707:28): [True: 0, False: 0]
Branch (707:28): [True: 0, False: 0]
Branch (707:28): [True: 0, False: 0]
|
708 | 0 | Ok(Some((subscriber, awaited_action.sort_key()))) |
709 | | } else { |
710 | 0 | Ok(None) |
711 | | } |
712 | 0 | } |
713 | 0 | }) |
714 | 0 | .try_collect() |
715 | 0 | .await |
716 | 0 | .err_tip(|| "In SimpleSchedulerStateManager::filter_operations")?; |
717 | 0 | match filter.order_by_priority_direction { |
718 | 0 | Some(OrderDirection::Asc) => all_items.sort_unstable_by(|(_, a), (_, b)| a.cmp(b)), |
719 | 0 | Some(OrderDirection::Desc) => all_items.sort_unstable_by(|(_, a), (_, b)| b.cmp(a)), |
720 | 0 | None => {} |
721 | | } |
722 | 0 | return Ok(Box::pin(stream::iter( |
723 | 0 | all_items |
724 | 0 | .into_iter() |
725 | 0 | .map(move |(subscriber, _)| to_action_state_result(subscriber)), |
726 | | ))); |
727 | | }; |
728 | | |
729 | 611 | let desc = matches!500 ( |
730 | 111 | filter.order_by_priority_direction, |
731 | | Some(OrderDirection::Desc) |
732 | | ); |
733 | 611 | let stream = self |
734 | 611 | .action_db |
735 | 611 | .get_range_of_actions( |
736 | 611 | sorted_awaited_action_state, |
737 | 611 | Bound::Unbounded, |
738 | 611 | Bound::Unbounded, |
739 | 611 | desc, |
740 | 611 | ) |
741 | 611 | .await |
742 | 611 | .err_tip(|| "In SimpleSchedulerStateManager::filter_operations")?0 |
743 | 611 | .and_then(|awaited_action_subscriber| async move {550 |
744 | 550 | let awaited_action = awaited_action_subscriber |
745 | 550 | .borrow() |
746 | 550 | .await |
747 | 550 | .err_tip(|| "In SimpleSchedulerStateManager::filter_operations")?0 ; |
748 | 550 | Ok((awaited_action_subscriber, awaited_action)) |
749 | 1.10k | }) |
750 | 611 | .try_filter_map(move |(subscriber, awaited_action)| {550 |
751 | 550 | let filter = filter.clone(); |
752 | 550 | async move { |
753 | 550 | if self.apply_filter_predicate(&awaited_action, &filter).await { Branch (753:24): [True: 0, False: 0]
Branch (753:24): [True: 0, False: 0]
Branch (753:24): [True: 0, False: 0]
Branch (753:24): [True: 0, False: 0]
Branch (753:24): [Folded - Ignored]
Branch (753:24): [True: 0, False: 0]
Branch (753:24): [True: 5, False: 0]
Branch (753:24): [True: 500, False: 0]
Branch (753:24): [True: 43, False: 0]
Branch (753:24): [True: 0, False: 0]
Branch (753:24): [True: 2, False: 0]
|
754 | 550 | Ok(Some(subscriber)) |
755 | | } else { |
756 | 0 | Ok(None) |
757 | | } |
758 | 550 | } |
759 | 550 | }) |
760 | 611 | .map(move |result| -> Box<dyn ActionStateResult> {550 |
761 | 550 | result.map_or_else( |
762 | 0 | |e| -> Box<dyn ActionStateResult> { Box::new(ErrorActionStateResult(e)) }, |
763 | 550 | |v| -> Box<dyn ActionStateResult> { to_action_state_result(v) }, |
764 | | ) |
765 | 550 | }); |
766 | 611 | Ok(Box::pin(stream)) |
767 | 615 | } |
768 | | } |
769 | | |
770 | | #[async_trait] |
771 | | impl<T, I, NowFn> ClientStateManager for SimpleSchedulerStateManager<T, I, NowFn> |
772 | | where |
773 | | T: AwaitedActionDb, |
774 | | I: InstantWrapper, |
775 | | NowFn: Fn() -> I + Clone + Send + Unpin + Sync + 'static, |
776 | | { |
777 | | async fn add_action( |
778 | | &self, |
779 | | client_operation_id: OperationId, |
780 | | action_info: Arc<ActionInfo>, |
781 | 60 | ) -> Result<Box<dyn ActionStateResult>, Error> { |
782 | 30 | let sub = self |
783 | 30 | .inner_add_operation(client_operation_id, action_info.clone()) |
784 | 30 | .await?0 ; |
785 | | |
786 | 30 | Ok(Box::new(ClientActionStateResult::new( |
787 | 30 | sub, |
788 | 30 | self.weak_self.clone(), |
789 | 30 | self.no_event_action_timeout, |
790 | 30 | self.now_fn.clone(), |
791 | 30 | ))) |
792 | 60 | } |
793 | | |
794 | | async fn filter_operations<'a>( |
795 | | &'a self, |
796 | | filter: OperationFilter, |
797 | 1.00k | ) -> Result<ActionStateResultStream<'a>, Error> { |
798 | 504 | self.inner_filter_operations(filter, move |rx| { |
799 | 504 | Box::new(ClientActionStateResult::new( |
800 | 504 | rx, |
801 | 504 | self.weak_self.clone(), |
802 | 504 | self.no_event_action_timeout, |
803 | 504 | self.now_fn.clone(), |
804 | 504 | )) |
805 | 504 | }) |
806 | 504 | .await |
807 | 1.00k | } |
808 | | |
809 | 0 | fn as_known_platform_property_provider(&self) -> Option<&dyn KnownPlatformPropertyProvider> { |
810 | 0 | None |
811 | 0 | } |
812 | | } |
813 | | |
814 | | #[async_trait] |
815 | | impl<T, I, NowFn> WorkerStateManager for SimpleSchedulerStateManager<T, I, NowFn> |
816 | | where |
817 | | T: AwaitedActionDb, |
818 | | I: InstantWrapper, |
819 | | NowFn: Fn() -> I + Clone + Send + Unpin + Sync + 'static, |
820 | | { |
821 | | async fn update_operation( |
822 | | &self, |
823 | | operation_id: &OperationId, |
824 | | worker_id: &WorkerId, |
825 | | update: UpdateOperationType, |
826 | 32 | ) -> Result<(), Error> { |
827 | 16 | self.inner_update_operation(operation_id, Some(worker_id), update) |
828 | 16 | .await |
829 | 32 | } |
830 | | } |
831 | | |
832 | | #[async_trait] |
833 | | impl<T, I, NowFn> MatchingEngineStateManager for SimpleSchedulerStateManager<T, I, NowFn> |
834 | | where |
835 | | T: AwaitedActionDb, |
836 | | I: InstantWrapper, |
837 | | NowFn: Fn() -> I + Clone + Send + Unpin + Sync + 'static, |
838 | | { |
839 | | async fn filter_operations<'a>( |
840 | | &'a self, |
841 | | filter: OperationFilter, |
842 | 222 | ) -> Result<ActionStateResultStream<'a>, Error> { |
843 | 111 | self.inner_filter_operations(filter, |rx| {50 |
844 | 50 | Box::new(MatchingEngineActionStateResult::new( |
845 | 50 | rx, |
846 | 50 | self.weak_self.clone(), |
847 | 50 | self.no_event_action_timeout, |
848 | 50 | self.now_fn.clone(), |
849 | 50 | )) |
850 | 50 | }) |
851 | 111 | .await |
852 | 222 | } |
853 | | |
854 | | async fn assign_operation( |
855 | | &self, |
856 | | operation_id: &OperationId, |
857 | | worker_id_or_reason_for_unassign: Result<&WorkerId, Error>, |
858 | 66 | ) -> Result<(), Error> { |
859 | 33 | let (maybe_worker_id, update) = match worker_id_or_reason_for_unassign { |
860 | 32 | Ok(worker_id) => ( |
861 | 32 | Some(worker_id), |
862 | 32 | UpdateOperationType::UpdateWithActionStage(ActionStage::Executing), |
863 | 32 | ), |
864 | 1 | Err(err) => (None, UpdateOperationType::UpdateWithError(err)), |
865 | | }; |
866 | 33 | self.inner_update_operation(operation_id, maybe_worker_id, update) |
867 | 33 | .await |
868 | 66 | } |
869 | | } |