apalis-workflow 0.1.0-rc.7

A flexible and composable task workflow engine for rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use apalis_core::backend::BackendExt;
use apalis_core::backend::codec::Codec;
use apalis_core::task::builder::TaskBuilder;
use apalis_core::task::metadata::Meta;
use apalis_core::task::status::Status;
use apalis_core::{
    backend::WaitForCompletion,
    error::BoxDynError,
    task::{Task, metadata::MetadataExt, task_id::TaskId},
};
use futures::future::BoxFuture;
use futures::{FutureExt, Sink, SinkExt, StreamExt};
use petgraph::Direction;
use petgraph::graph::NodeIndex;
use std::collections::HashMap;
use std::fmt::Debug;
use tower::Service;

use crate::DagExecutor;
use crate::dag::context::DagFlowContext;
use crate::dag::error::{DagFlowError, DagServiceError};
use crate::dag::response::DagExecutionResponse;
use crate::id_generator::GenerateId;

/// Service that manages the execution of a DAG workflow
pub struct RootDagService<B>
where
    B: BackendExt,
{
    executor: DagExecutor<B>,
    backend: B,
}

impl<B> std::fmt::Debug for RootDagService<B>
where
    B: BackendExt,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RootDagService")
            .field("executor", &"<DagExecutor>")
            .field("backend", &"<Backend>")
            .finish()
    }
}

impl<B> RootDagService<B>
where
    B: BackendExt,
{
    pub(crate) fn new(executor: DagExecutor<B>, backend: B) -> Self {
        Self { executor, backend }
    }
}

impl<B> Clone for RootDagService<B>
where
    B: BackendExt + Clone,
{
    fn clone(&self) -> Self {
        Self {
            executor: self.executor.clone(),
            backend: self.backend.clone(),
        }
    }
}

/// Determine if the previous node is the designated predecessor in a fan-in scenario
fn find_designated_fan_in_handler(
    incoming_nodes: &[NodeIndex],
) -> Result<&NodeIndex, DagFlowError> {
    let designated_handler = incoming_nodes.iter().max_by_key(|n| n.index());
    designated_handler.ok_or(DagFlowError::Service(DagServiceError::MissingFaninHandler))
}

impl<B, Err, CdcErr, MetaError, IdType> Service<Task<B::Compact, B::Context, B::IdType>>
    for RootDagService<B>
where
    B: BackendExt<Error = Err, IdType = IdType>
        + Send
        + Sync
        + 'static
        + Clone
        + WaitForCompletion<DagExecutionResponse<B::Compact, IdType>>,
    IdType: GenerateId + Send + Sync + 'static + PartialEq + Debug + Clone,
    B::Compact: Send + Sync + 'static + Clone,
    B::Context:
        Send + Sync + Default + MetadataExt<DagFlowContext<B::IdType>, Error = MetaError> + 'static,
    Err: std::error::Error + Send + Sync + 'static,
    B: Sink<Task<B::Compact, B::Context, B::IdType>, Error = Err> + Unpin,
    B::Codec: Codec<Vec<B::Compact>, Compact = B::Compact, Error = CdcErr>
        + 'static
        + Codec<DagExecutionResponse<B::Compact, B::IdType>, Compact = B::Compact, Error = CdcErr>,
    CdcErr: Into<BoxDynError>,
    MetaError: Into<BoxDynError> + Send + Sync + 'static,
{
    type Response = DagExecutionResponse<B::Compact, B::IdType>;
    type Error = DagFlowError;
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(
        &mut self,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        self.executor.poll_ready(cx)
    }

    fn call(&mut self, mut req: Task<B::Compact, B::Context, B::IdType>) -> Self::Future {
        let mut executor = self.executor.clone();
        let mut backend = self.backend.clone();
        let start_nodes = executor.start_nodes.clone();
        let end_nodes = executor.end_nodes.clone();
        async move {
            let ctx = req.extract::<Meta<DagFlowContext<B::IdType>>>().await;
            let (response, context) = if let Ok(Meta(context)) = ctx {
                #[cfg(feature = "tracing")]
                tracing::debug!(
                    node = ?context.current_node,
                    "Extracted DagFlowContext for task"
                );
                let incoming_nodes = executor
                    .graph
                    .neighbors_directed(context.current_node, Direction::Incoming)
                    .collect::<Vec<_>>();
                match incoming_nodes.len() {
                    // Entry node
                    0 if start_nodes.len() == 1 => {
                        let response = executor.call(req).await?;
                        (response, context)
                    }
                    // Entry node with multiple start nodes
                    0 if start_nodes.len() > 1 => {
                        let response = executor.call(req).await?;
                        (response, context)
                    }
                    // Single incoming node, proceed normally
                    1 => {
                        let response = executor.call(req).await?;
                        (response, context)
                    }
                    // Multiple incoming nodes, fan-in scenario
                    _ => {
                        let dependency_task_ids = context.get_dependency_task_ids(&incoming_nodes);
                        #[cfg(feature = "tracing")]
                        tracing::debug!(
                            prev_node = ?context.prev_node,
                            node = ?context.current_node,
                            deps = ?dependency_task_ids,
                            "Fanning in from multiple dependencies",
                        );

                        let prev_node = context
                            .prev_node
                            .ok_or(DagFlowError::Service(DagServiceError::MissingPreviousNode))?;

                        if *find_designated_fan_in_handler(&incoming_nodes)? != prev_node {
                            return Ok(DagExecutionResponse::WaitingForDependencies {
                                pending_dependencies: dependency_task_ids,
                            });
                        }

                        let results = backend
                            .wait_for(dependency_task_ids.values().cloned().collect::<Vec<_>>())
                            .collect::<Vec<_>>()
                            .await
                            .into_iter()
                            .collect::<Result<Vec<_>, _>>()
                            .map_err(|e| DagFlowError::Backend(e.into()))?;
                        if results.iter().all(|s| matches!(s.status, Status::Done)) {
                            let sorted_results = {
                                // Match the order of incoming_nodes by matching NodeIndex
                                let res = incoming_nodes
                                    .iter()
                                    .rev()
                                    .map(|node_index| {
                                        let task_id = context
                                            .node_task_ids
                                            .iter()
                                            .find(|(n, _)| *n == node_index)
                                            .map(|(_, task_id)| task_id)
                                            .ok_or(DagFlowError::Service(
                                                DagServiceError::MissingIncomingTaskId,
                                            ))?;
                                        let task_result = results
                                            .iter()
                                            .find(|r| &r.task_id == task_id)
                                            .ok_or(DagFlowError::Service(
                                                DagServiceError::MissingTaskIdResult(format!(
                                                    "{:?}",
                                                    task_id.inner()
                                                )),
                                            ))?;
                                        Ok(task_result)
                                    })
                                    .collect::<Result<Vec<_>, DagFlowError>>();
                                match res {
                                    Ok(v) => v,
                                    Err(_) => {
                                        return Ok(DagExecutionResponse::WaitingForDependencies {
                                            pending_dependencies: dependency_task_ids,
                                        });
                                    }
                                }
                            };
                            let res = sorted_results
                                .iter()
                                .map(|s| match &s.result {
                                    Ok(val) => match val {
                                        DagExecutionResponse::FanOut { response, .. } => {
                                            Ok(response.clone())
                                        }
                                        DagExecutionResponse::EnqueuedNext { result }
                                        | DagExecutionResponse::Complete { result } => {
                                            Ok(result.clone())
                                        }
                                        _ => Err(DagFlowError::Service(
                                            DagServiceError::InvalidFanInDependencyResult,
                                        )),
                                    },
                                    Err(e) => Err(DagFlowError::Service(
                                        DagServiceError::DependencyTaskFailed(e.as_str().into()),
                                    )),
                                })
                                .collect::<Result<Vec<_>, _>>()?;
                            let encoded_input = B::Codec::encode(&res)
                                .map_err(|e| DagFlowError::Codec(e.into()))?;

                            let req = req.map(|_| encoded_input); // Replace args with fan-in input
                            let response = executor.call(req).await?;
                            (response, context)
                        } else {
                            return Err(DagFlowError::Service(
                                DagServiceError::DependencyTaskFailed(
                                    "An adjacent node failed. Terminating".into(),
                                ),
                            ));
                        }
                    }
                }
            } else {
                #[cfg(feature = "tracing")]
                tracing::debug!("Extracting DagFlowContext for task without meta");
                // if no metadata, we assume its an entry task
                if start_nodes.len() == 1 {
                    #[cfg(feature = "tracing")]
                    tracing::debug!("Single start node detected, proceeding with execution");
                    let context = DagFlowContext::new(req.parts.task_id.clone());
                    req.parts
                        .ctx
                        .inject(context.clone())
                        .map_err(|e| DagFlowError::Metadata(e.into()))?;
                    let response = executor.call(req).await?;
                    #[cfg(feature = "tracing")]
                    tracing::debug!(node = ?context.current_node, "Execution complete at node");
                    (response, context)
                } else {
                    let new_node_task_ids = fan_out_entry_nodes(
                        &executor,
                        &backend,
                        &DagFlowContext::new(req.parts.task_id.clone()),
                        &req.args,
                    )
                    .await?;
                    return Ok(DagExecutionResponse::EntryFanOut {
                        node_task_ids: new_node_task_ids,
                    });
                }
            };
            // At this point we know a node was executed and we have its context
            // We need to figure out the outgoing nodes and enqueue tasks for them
            let current_node = context.current_node;
            let outgoing_nodes = executor
                .graph
                .neighbors_directed(current_node, Direction::Outgoing)
                .collect::<Vec<_>>();

            match outgoing_nodes.len() {
                0 => {
                    assert!(
                        end_nodes.contains(&current_node),
                        "Current node is not an end node"
                    );
                    // This was an end node
                    return Ok(DagExecutionResponse::Complete { result: response });
                }
                1 => {
                    // Single outgoing node, enqueue task for it
                    let next_node = outgoing_nodes[0];
                    let mut new_context = context.clone();
                    new_context.prev_node = Some(current_node);
                    new_context.current_node = next_node;
                    new_context.current_position += 1;
                    new_context.is_initial = false;

                    let task = TaskBuilder::new(response.clone())
                        .with_task_id(TaskId::new(B::IdType::generate()))
                        .meta(new_context)
                        .build();
                    backend
                        .send(task)
                        .await
                        .map_err(|e| DagFlowError::Backend(e.into()))?;
                }
                _ => {
                    // Multiple outgoing nodes, fan out
                    let mut new_context = context.clone();
                    new_context.prev_node = Some(current_node);
                    new_context.current_position += 1;
                    new_context.is_initial = false;

                    let next_task_ids = fan_out_next_nodes(
                        &executor,
                        outgoing_nodes,
                        &backend,
                        &new_context,
                        &response,
                    )
                    .await?;
                    return Ok(DagExecutionResponse::FanOut {
                        response,
                        node_task_ids: next_task_ids,
                    });
                }
            }
            Ok(DagExecutionResponse::EnqueuedNext { result: response })
        }
        .boxed()
    }
}

async fn fan_out_next_nodes<B, Err, CdcErr>(
    _executor: &DagExecutor<B>,
    outgoing_nodes: Vec<NodeIndex>,
    backend: &B,
    context: &DagFlowContext<B::IdType>,
    input: &B::Compact,
) -> Result<HashMap<NodeIndex, TaskId<B::IdType>>, DagFlowError>
where
    B::IdType: GenerateId + Send + Sync + 'static + PartialEq,
    B::Compact: Send + Sync + 'static + Clone,
    B::Context: Send + Sync + Default + MetadataExt<DagFlowContext<B::IdType>> + 'static,
    B: Sink<Task<B::Compact, B::Context, B::IdType>, Error = Err> + Unpin,
    Err: std::error::Error + Send + Sync + 'static,
    B: BackendExt<Error = Err> + Send + Sync + 'static + Clone,
    B::Codec: Codec<Vec<B::Compact>, Compact = B::Compact, Error = CdcErr>,
    CdcErr: Into<BoxDynError>,
{
    let mut enqueue_futures = vec![];
    let next_nodes = outgoing_nodes
        .iter()
        .map(|node| (*node, TaskId::new(B::IdType::generate())))
        .collect::<HashMap<NodeIndex, TaskId<B::IdType>>>();
    let mut node_task_ids = next_nodes.clone();
    node_task_ids.extend(context.node_task_ids.clone());
    for outgoing_node in outgoing_nodes.into_iter() {
        let task_id = next_nodes
            .get(&outgoing_node)
            .ok_or(DagFlowError::Service(DagServiceError::MissingNextNode))?
            .clone();
        let task = TaskBuilder::new(input.clone())
            .with_task_id(task_id)
            .meta(DagFlowContext {
                prev_node: context.prev_node,
                current_node: outgoing_node,
                completed_nodes: context.completed_nodes.clone(),
                node_task_ids: node_task_ids.clone(),
                current_position: context.current_position + 1,
                is_initial: context.is_initial,
                root_task_id: context.root_task_id.clone(),
            })
            .build();
        let mut b = backend.clone();
        enqueue_futures.push(
            async move {
                b.send(task)
                    .await
                    .map_err(|e| DagFlowError::Backend(e.into()))?;
                Ok::<(), DagFlowError>(())
            }
            .boxed(),
        );
    }
    futures::future::try_join_all(enqueue_futures).await?;
    Ok(next_nodes)
}

async fn fan_out_entry_nodes<B, Err, CdcErr>(
    executor: &DagExecutor<B>,
    backend: &B,
    context: &DagFlowContext<B::IdType>,
    input: &B::Compact,
) -> Result<HashMap<NodeIndex, TaskId<B::IdType>>, DagFlowError>
where
    B::IdType: GenerateId + Send + Sync + 'static + PartialEq + Debug,
    B::Compact: Send + Sync + 'static + Clone,
    B::Context: Send + Sync + Default + MetadataExt<DagFlowContext<B::IdType>> + 'static,
    B: Sink<Task<B::Compact, B::Context, B::IdType>, Error = Err> + Unpin,
    Err: std::error::Error + Send + Sync + 'static,
    B: BackendExt<Error = Err> + Send + Sync + 'static + Clone,
    B::Codec: Codec<Vec<B::Compact>, Compact = B::Compact, Error = CdcErr>,
    CdcErr: Into<BoxDynError>,
{
    let values: Vec<B::Compact> =
        B::Codec::decode(input).map_err(|e: CdcErr| DagFlowError::Codec(e.into()))?;
    let start_nodes = executor.start_nodes.clone();
    if values.len() != start_nodes.len() {
        return Err(DagFlowError::InputCountMismatch {
            expected: start_nodes.len(),
            actual: values.len(),
        });
    }
    let mut enqueue_futures = vec![];
    let next_nodes = start_nodes
        .iter()
        .map(|node| (*node, TaskId::new(B::IdType::generate())))
        .collect::<HashMap<NodeIndex, TaskId<B::IdType>>>();
    let mut node_task_ids = next_nodes.clone();
    node_task_ids.extend(context.node_task_ids.clone());
    for (outgoing_node, input) in start_nodes.into_iter().zip(values) {
        let task_id = next_nodes
            .get(&outgoing_node)
            .ok_or(DagFlowError::Service(DagServiceError::MissingNextNode))?;
        let task = TaskBuilder::new(input)
            .with_task_id(task_id.clone())
            .meta(DagFlowContext {
                prev_node: None,
                current_node: outgoing_node,
                completed_nodes: Default::default(),
                node_task_ids: node_task_ids.clone(),
                current_position: context.current_position,
                is_initial: true,
                root_task_id: context.root_task_id.clone(),
            })
            .build();
        let mut b = backend.clone();
        enqueue_futures.push(
            async move {
                b.send(task)
                    .await
                    .map_err(|e| DagFlowError::Backend(BoxDynError::from(e)))?;
                Ok::<(), DagFlowError>(())
            }
            .boxed(),
        );
    }
    futures::future::try_join_all(enqueue_futures).await?;
    Ok(next_nodes)
}