homestar-runtime 0.3.0

Homestar runtime implementation
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! [Scheduler] module for executing a series of tasks for a given
//! [Workflow].
//!
//! [Scheduler]: TaskScheduler
//! [Workflow]: homestar_workflow::Workflow

use crate::{
    db::{Connection, Database},
    workflow::{self, IndexedResources, Resource, Vertex},
    Db,
};
use anyhow::{anyhow, Result};
use dagga::Node;
use fnv::FnvHashSet;
use futures::future::BoxFuture;
use homestar_invocation::{task, Pointer};
use homestar_wasm::io::Arg;
use homestar_workflow::LinkMap;
use indexmap::IndexMap;
use libipld::Cid;
use std::{str::FromStr, sync::Arc};
use tokio::sync::RwLock;
use tracing::debug;

/// Type alias for a [Dag] set of batched nodes.
///
/// [Dag]: dagga::Dag
type Schedule<'a> = Vec<Vec<Node<Vertex<'a>, usize>>>;

/// Type for [instruction]-based, batched, execution graph and set of task
/// resources.
///
/// [instruction]: homestar_invocation::task::Instruction
#[derive(Debug, Clone, Default)]
pub(crate) struct ExecutionGraph<'a> {
    /// A built-up [Dag] [Schedule] of batches.
    ///
    /// [Dag]: dagga::Dag
    ///
    pub(crate) awaiting: workflow::Promises,
    pub(crate) schedule: Schedule<'a>,
    /// Vector of [resources] to fetch for executing functions in [Workflow].
    ///
    /// [resources]: Resource
    /// [Workflow]: homestar_workflow::Workflow
    pub(crate) indexed_resources: IndexedResources,
}

/// Scheduler for a series of tasks, including what's run,
/// what's left to run, and data structures to track resources
/// and what's been executed in memory.
#[allow(dead_code)]
#[derive(Debug, Clone, Default)]
pub(crate) struct TaskScheduler<'a> {
    /// In-memory map of task/instruction results.
    pub(crate) linkmap: Arc<RwLock<LinkMap<task::Result<Arg>>>>,
    /// [ExecutionGraph] of what's been run so far for a [Workflow] of `batched`
    /// [Tasks].
    ///
    /// [Workflow]: homestar_workflow::Workflow
    /// [Tasks]: homestar_invocation::Task
    pub(crate) ran: Option<Schedule<'a>>,

    /// [ExecutionGraph] of what's left to run for a [Workflow] of `batched`
    /// [Tasks].
    ///
    /// [Workflow]: homestar_workflow::Workflow
    /// [Tasks]: homestar_invocation::Task
    pub(crate) run: Schedule<'a>,

    /// Set of Cids to possibly fetch from the DHT.
    pub(crate) promises_to_resolve: Arc<FnvHashSet<Cid>>,

    /// Step/batch to resume from.
    pub(crate) resume_step: Option<usize>,

    /// Resources that tasks within a [Workflow] rely on, retrieved
    /// through over the network, ahead-of-time.
    ///
    /// This is transferred from the [ExecutionGraph] for executing the
    /// schedule by a worker.
    ///
    /// [Workflow]: homestar_workflow::Workflow
    pub(crate) resources: Arc<RwLock<IndexMap<Resource, Vec<u8>>>>,
}

/// Scheduler context containing the a schedule for executing tasks.
pub(crate) struct SchedulerContext<'a> {
    /// Scheduler for a series of tasks, including what's run.
    pub(crate) scheduler: TaskScheduler<'a>,
}

impl<'a> TaskScheduler<'a> {
    /// Initialize Task Scheduler for a [Workflow]
    ///
    /// The scheduler will attempt to find already-executed tasks (via [Receipts])
    /// either in the database or through a [Swarm]/DHT query on a short(er)
    /// timeout.
    ///
    /// [Receipts]: crate::Receipt
    /// [Swarm]: crate::network::swarm
    /// [Workflow]: homestar_workflow::Workflow
    pub(crate) async fn init<F>(
        mut graph: Arc<ExecutionGraph<'a>>,
        conn: &mut Connection,
        fetch_fn: F,
    ) -> Result<SchedulerContext<'a>>
    where
        F: FnOnce(FnvHashSet<Resource>) -> BoxFuture<'a, Result<IndexMap<Resource, Vec<u8>>>>,
    {
        let mut_graph = Arc::make_mut(&mut graph);
        let schedule = &mut mut_graph.schedule;
        let schedule_length = schedule.len();

        // Gather all CIDs to resolve
        let mut cids_to_resolve = Vec::new();
        // Gather all resources to fetch
        let mut resources_to_fetch = Vec::new();
        let mut linkmap = LinkMap::<task::Result<Arg>>::default();

        let mut last_idx = 0;
        for (idx, vec) in schedule.iter().enumerate().rev() {
            let pointers: Result<Vec<_>, _> = vec
                .iter()
                .map(|node| {
                    let cid = Cid::from_str(node.name())?;
                    if let Some(resource) = mut_graph.indexed_resources.get(&cid) {
                        for rsc in resource.iter() {
                            resources_to_fetch.push((cid, rsc.clone()));
                        }
                    } else {
                        return Err(anyhow!("Resource not found for instruction {cid}"));
                    }
                    Ok(Pointer::new(cid))
                })
                .collect();

            if let Ok(pointers) = pointers {
                if let Ok(found) = Db::find_instruction_pointers(&pointers, conn) {
                    for receipt in found.iter() {
                        resources_to_fetch.retain(|(cid, _)| *cid != receipt.instruction().cid());
                        linkmap.insert(receipt.instruction().cid(), receipt.output_as_arg());
                    }

                    if found.len() == vec.len() {
                        last_idx = idx + 1;
                        break;
                    }
                } else {
                    debug!("Receipt not available in the database");
                }
            }
        }

        // Add all CIDs not resolved to the list of CIDs to resolve.
        cids_to_resolve.extend(resources_to_fetch.iter().map(|(cid, _)| *cid));

        // Fetch resources from the DHT as a unique set.
        let resources_to_fetch: FnvHashSet<Resource> =
            resources_to_fetch.into_iter().map(|(_, rsc)| rsc).collect();
        let fetched_resources = fetch_fn(resources_to_fetch).await?;

        // Filter out promises/awaits outside of the workflow that
        // have been already resolved and store them in our in-memory
        // cache (linkmap).
        let promises_as_pointers =
            mut_graph
                .awaiting
                .iter()
                .fold(
                    vec![],
                    |mut acc, (in_or_out_flow, cid)| match in_or_out_flow {
                        workflow::Origin::InFlow => acc,
                        workflow::Origin::OutFlow => {
                            acc.push(Pointer::new(*cid));
                            acc
                        }
                    },
                );
        if let Ok(found) = Db::find_instruction_pointers(&promises_as_pointers, conn) {
            for receipt in found.iter() {
                cids_to_resolve.retain(|cid| *cid != receipt.instruction().cid());
                linkmap.insert(receipt.instruction().cid(), receipt.output_as_arg());
            }
        }

        // Convert the list of CIDs to resolve into a unique set.
        let promises_to_resolve: FnvHashSet<Cid> = cids_to_resolve.into_iter().collect();

        let (ran, run, resume_step) = if last_idx > 0 {
            let pivot = schedule.split_off(last_idx);
            if last_idx >= schedule_length || last_idx == 0 {
                (Some(schedule.to_vec()), pivot, None)
            } else {
                (Some(schedule.to_vec()), pivot, Some(last_idx))
            }
        } else {
            (None, schedule.to_vec(), None)
        };

        Ok(SchedulerContext {
            scheduler: Self {
                linkmap: Arc::new(RwLock::new(linkmap)),
                promises_to_resolve: Arc::new(promises_to_resolve),
                ran,
                run,
                resume_step,
                resources: Arc::new(fetched_resources.into()),
            },
        })
    }

    /// Get the number of tasks that have already ran in the [Workflow].
    ///
    /// [Workflow]: homestar_workflow::Workflow
    #[allow(dead_code)]
    pub(crate) fn ran_length(&self) -> usize {
        self.ran
            .as_ref()
            .map(|ran| ran.iter().flatten().collect::<Vec<_>>().len())
            .unwrap_or_default()
    }

    /// Get the number of tasks left to run in the [Workflow].
    ///
    /// [Workflow]: homestar_workflow::Workflow
    #[allow(dead_code)]
    pub(crate) fn run_length(&self) -> usize {
        self.run.iter().flatten().collect::<Vec<_>>().len()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{test_utils::db::MemoryDb, workflow, Receipt};
    use futures::FutureExt;
    use homestar_invocation::{
        authority::UcanPrf,
        ipld::DagCbor,
        task::{instruction::RunInstruction, Resources},
        test_utils, Invocation, Receipt as InvocationReceipt, Task,
    };
    use homestar_workflow::Workflow;
    use libipld::Ipld;

    #[homestar_runtime_proc_macro::db_async_test]
    fn initialize_task_scheduler() {
        let settings = TestSettings::load();
        let config = Resources::default();
        let (instruction1, instruction2, _) = test_utils::related_wasm_instructions::<Arg>();
        let task1 = Task::new(
            RunInstruction::Expanded(instruction1.clone()),
            config.clone().into(),
            UcanPrf::default(),
        );
        let task2 = Task::new(
            RunInstruction::Expanded(instruction2.clone()),
            config.into(),
            UcanPrf::default(),
        );

        let db = MemoryDb::setup_connection_pool(&settings.node, None).unwrap();
        let mut conn = db.conn().unwrap();
        let workflow = Workflow::new(vec![task1.clone(), task2.clone()]);
        let fetch_fn = |_rscs: FnvHashSet<Resource>| {
            {
                async {
                    let mut index_map = IndexMap::new();
                    index_map.insert(Resource::Url(instruction1.resource().to_owned()), vec![]);
                    index_map.insert(Resource::Url(instruction2.resource().to_owned()), vec![]);

                    Ok(index_map)
                }
            }
            .boxed()
        };

        let builder = workflow::Builder::new(workflow);
        let graph = builder.graph().unwrap();

        let scheduler_ctx = TaskScheduler::init(graph.into(), &mut conn, fetch_fn)
            .await
            .unwrap();

        let ctx = scheduler_ctx.scheduler;

        assert!(ctx.linkmap.read().await.is_empty());
        assert!(ctx.ran.is_none());
        assert_eq!(ctx.run.len(), 2);
        assert_eq!(ctx.resume_step, None);
    }

    #[homestar_runtime_proc_macro::db_async_test]
    fn initialize_task_scheduler_with_receipted_instruction() {
        let settings = TestSettings::load();
        let config = Resources::default();
        let (instruction1, instruction2, _) = test_utils::related_wasm_instructions::<Arg>();
        let task1 = Task::new(
            RunInstruction::Expanded(instruction1.clone()),
            config.clone().into(),
            UcanPrf::default(),
        );
        let task2 = Task::new(
            RunInstruction::Expanded(instruction2.clone()),
            config.into(),
            UcanPrf::default(),
        );

        let invocation_receipt = InvocationReceipt::new(
            Invocation::new(task1.clone()).try_into().unwrap(),
            task::Result::Ok(Ipld::Integer(4)),
            Ipld::Null,
            None,
            UcanPrf::default(),
        );
        let receipt = Receipt::try_with(
            instruction1.clone().try_into().unwrap(),
            &invocation_receipt,
        )
        .unwrap();

        let db = MemoryDb::setup_connection_pool(&settings.node, None).unwrap();
        let mut conn = db.conn().unwrap();
        let stored_receipt = MemoryDb::store_receipt(receipt.clone(), &mut conn).unwrap();

        assert_eq!(receipt, stored_receipt.unwrap());

        let workflow = Workflow::new(vec![task1.clone(), task2.clone()]);
        let fetch_fn = |_rscs: FnvHashSet<Resource>| {
            {
                async {
                    let mut index_map = IndexMap::new();
                    index_map.insert(Resource::Url(instruction1.resource().to_owned()), vec![]);
                    index_map.insert(Resource::Url(instruction2.resource().to_owned()), vec![]);

                    Ok(index_map)
                }
            }
            .boxed()
        };

        let builder = workflow::Builder::new(workflow);
        let graph = builder.graph().unwrap();

        let scheduler_ctx = TaskScheduler::init(graph.into(), &mut conn, fetch_fn)
            .await
            .unwrap();

        let ctx = scheduler_ctx.scheduler;
        let ran = ctx.ran.as_ref().unwrap();

        assert_eq!(ctx.linkmap.read().await.len(), 1);
        assert!(ctx
            .linkmap
            .read()
            .await
            .contains_key(&instruction1.clone().to_cid().unwrap()));
        assert_eq!(ran.len(), 1);
        assert_eq!(ctx.run.len(), 1);
        assert_eq!(ctx.resume_step, Some(1));
    }

    #[homestar_runtime_proc_macro::db_async_test]
    fn initialize_task_scheduler_with_all_receipted_instruction() {
        let settings = TestSettings::load();
        let config = Resources::default();
        let (instruction1, instruction2, _) = test_utils::related_wasm_instructions::<Arg>();

        let task1 = Task::new(
            RunInstruction::Expanded(instruction1.clone()),
            config.clone().into(),
            UcanPrf::default(),
        );

        let task2 = Task::new(
            RunInstruction::Expanded(instruction2.clone()),
            config.into(),
            UcanPrf::default(),
        );

        let invocation_receipt1 = InvocationReceipt::new(
            Invocation::new(task1.clone()).try_into().unwrap(),
            task::Result::Ok(Ipld::Integer(4)),
            Ipld::Null,
            None,
            UcanPrf::default(),
        );
        let receipt1 = Receipt::try_with(
            instruction1.clone().try_into().unwrap(),
            &invocation_receipt1,
        )
        .unwrap();

        let invocation_receipt2 = InvocationReceipt::new(
            Invocation::new(task2.clone()).try_into().unwrap(),
            task::Result::Ok(Ipld::Integer(44)),
            Ipld::Null,
            None,
            UcanPrf::default(),
        );
        let receipt2 = Receipt::try_with(
            instruction2.clone().try_into().unwrap(),
            &invocation_receipt2,
        )
        .unwrap();

        let db = MemoryDb::setup_connection_pool(&settings.node, None).unwrap();
        let mut conn = db.conn().unwrap();
        let rows_inserted = MemoryDb::store_receipts(vec![receipt1, receipt2], &mut conn).unwrap();
        assert_eq!(2, rows_inserted);

        let workflow = Workflow::new(vec![task1.clone(), task2.clone()]);
        let fetch_fn = |_rscs: FnvHashSet<Resource>| {
            async {
                let mut index_map = IndexMap::new();
                index_map.insert(Resource::Url(instruction1.resource().to_owned()), vec![]);
                index_map.insert(Resource::Url(instruction2.resource().to_owned()), vec![]);
                Ok(index_map)
            }
            .boxed()
        };

        let builder = workflow::Builder::new(workflow);
        let graph = builder.graph().unwrap();

        let scheduler_ctx = TaskScheduler::init(graph.into(), &mut conn, fetch_fn)
            .await
            .unwrap();

        let ctx = scheduler_ctx.scheduler;
        let ran = ctx.ran.as_ref().unwrap();

        assert_eq!(ctx.linkmap.read().await.len(), 1);
        assert!(!ctx
            .linkmap
            .read()
            .await
            .contains_key(&instruction1.clone().to_cid().unwrap()));
        assert!(ctx
            .linkmap
            .read()
            .await
            .contains_key(&instruction2.clone().to_cid().unwrap()));
        assert_eq!(ran.len(), 2);
        assert!(ctx.run.is_empty());
        assert_eq!(ctx.resume_step, None);
    }

    #[test]
    fn duplicate_task_no_nonce() {
        let config = Resources::default();
        let (instruction1, instruction2, _) = test_utils::related_wasm_instructions::<Arg>();

        let task1 = Task::new(
            RunInstruction::Expanded(instruction1.clone()),
            config.clone().into(),
            UcanPrf::default(),
        );

        let task2 = Task::new(
            RunInstruction::Expanded(instruction2.clone()),
            config.into(),
            UcanPrf::default(),
        );

        let workflow = Workflow::new(vec![task1.clone(), task2.clone(), task1.clone()]);
        let builder = workflow::Builder::new(workflow);
        let graph = builder.graph();
        assert!(graph.is_err());
        assert_eq!(
            graph.unwrap_err().to_string(),
            "workflow cannot contain duplicate tasks: use a nonce (nnc field) to ensure uniqueness"
        );
    }

    #[test]
    fn duplicate_task_with_nonce() {
        let config = Resources::default();
        let (instruction1, _) = test_utils::wasm_instruction_with_nonce::<Arg>();
        let (instruction2, _) = test_utils::wasm_instruction_with_nonce::<Arg>();

        let task1 = Task::new(
            RunInstruction::Expanded(instruction1.clone()),
            config.clone().into(),
            UcanPrf::default(),
        );

        let task2 = Task::new(
            RunInstruction::Expanded(instruction2.clone()),
            config.into(),
            UcanPrf::default(),
        );

        let workflow = Workflow::new(vec![task1.clone(), task2.clone()]);
        let builder = workflow::Builder::new(workflow);
        let graph = builder.graph();
        assert!(graph.is_ok());
    }
}