dora-coordinator 1.1.0

`dora` goal is to be a low latency, composable, and distributed data flow.
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use crate::DaemonConnections;

use dora_core::{descriptor::DescriptorExt, uhlc::HLC};
use dora_message::{
    BuildId, SessionId,
    common::DaemonId,
    coordinator_to_daemon::{DaemonCoordinatorEvent, SpawnDataflowNodes, Timestamped},
    daemon_to_coordinator::DaemonCoordinatorReply,
    descriptor::{Deploy, Descriptor, ResolvedNode},
    id::NodeId,
};
use eyre::{ContextCompat, WrapErr, bail, eyre};
use std::{
    collections::{BTreeMap, BTreeSet},
    path::PathBuf,
};
use uuid::{NoContext, Timestamp, Uuid};

#[allow(clippy::too_many_arguments)]
#[tracing::instrument(skip(daemon_connections, clock))]
pub(super) async fn spawn_dataflow(
    build_id: Option<BuildId>,
    session_id: SessionId,
    dataflow: Descriptor,
    local_working_dir: Option<PathBuf>,
    daemon_connections: &mut DaemonConnections,
    clock: &HLC,
    uv: bool,
    write_events_to: Option<PathBuf>,
) -> eyre::Result<SpawnedDataflow> {
    let nodes = dataflow.resolve_aliases_and_set_defaults()?;
    // Neither the CLI nor the daemon validates a `dora start` descriptor
    // (only `dora run` goes through `check_dataflow`), so check the timing
    // fields here, before any daemon is contacted: the daemon feeds them to
    // `Duration::from_secs_f64`, which panics on a negative, non-finite, or
    // overflowing value and would take down every dataflow on that daemon.
    for node in nodes.values() {
        dora_core::descriptor::validate::check_node_timing_values(node)?;
    }
    let uuid = Uuid::new_v7(Timestamp::now(NoContext));

    // Resolve each node to its target daemon, then group by daemon.
    let mut nodes_by_daemon: BTreeMap<DaemonId, Vec<&ResolvedNode>> = BTreeMap::new();
    for node in nodes.values() {
        let daemon_id = resolve_daemon(daemon_connections, node.deploy.as_ref())?;
        nodes_by_daemon.entry(daemon_id).or_default().push(node);
    }

    let mut daemons = BTreeSet::new();
    let mut node_to_daemon = BTreeMap::new();

    for (daemon_id, nodes_on_daemon) in &nodes_by_daemon {
        let spawn_nodes = nodes_on_daemon.iter().map(|n| n.id.clone()).collect();
        tracing::debug!(
            "Spawning dataflow `{uuid}` on daemon `{daemon_id}` (nodes: {spawn_nodes:?})"
        );

        let spawn_command = SpawnDataflowNodes {
            build_id,
            session_id,
            dataflow_id: uuid,
            local_working_dir: local_working_dir.clone(),
            nodes: nodes.clone(),
            dataflow_descriptor: dataflow.clone(),
            spawn_nodes,
            uv,
            write_events_to: write_events_to.clone(),
            artifact_base_url: None,
        };
        let message = serde_json::to_vec(&Timestamped {
            inner: DaemonCoordinatorEvent::Spawn(spawn_command),
            timestamp: clock.new_timestamp(),
        })?;

        let daemon_connection = daemon_connections
            .get_mut(daemon_id)
            .wrap_err_with(|| format!("no daemon connection for daemon `{daemon_id}`"))?;

        let spawn_result: eyre::Result<()> = async {
            let reply_raw = daemon_connection
                .send_and_receive(&message)
                .await
                .wrap_err("failed to send/receive spawn message")?;
            match serde_json::from_slice(&reply_raw)
                .wrap_err("failed to deserialize spawn reply from daemon")?
            {
                DaemonCoordinatorReply::TriggerSpawnResult(result) => result
                    .map_err(|e| eyre!(e))
                    .wrap_err("daemon returned an error")?,
                _ => bail!("unexpected reply"),
            }
            Ok(())
        }
        .await;

        if let Err(spawn_err) = spawn_result {
            // The failing daemon was never inserted into `daemons`, so rollback
            // only targets daemons that confirmed a successful spawn.
            debug_assert!(
                !daemons.contains(daemon_id),
                "failing daemon should not be in the spawned set"
            );
            // Rollback: stop dataflow on daemons that already started successfully.
            let rollback_errors =
                rollback_spawned_daemons(uuid, &daemons, daemon_connections, clock).await;
            if rollback_errors.is_empty() {
                return Err(spawn_err.wrap_err(format!(
                    "spawn failed on daemon `{daemon_id}`; \
                     rolled back {n} previously-started daemon(s)",
                    n = daemons.len()
                )));
            } else {
                let rollback_summary = rollback_errors
                    .iter()
                    .map(|(id, e)| format!("  {id}: {e}"))
                    .collect::<Vec<_>>()
                    .join("\n");
                return Err(spawn_err.wrap_err(format!(
                    "spawn failed on daemon `{daemon_id}`; \
                     rollback attempted on {n} previously-started daemon(s), \
                     {f} stop(s) failed:\n{rollback_summary}",
                    n = daemons.len(),
                    f = rollback_errors.len(),
                )));
            }
        }

        daemons.insert(daemon_id.clone());
        for node in nodes_on_daemon {
            node_to_daemon.insert(node.id.clone(), daemon_id.clone());
        }
    }

    tracing::info!("successfully triggered dataflow spawn `{uuid}`",);

    Ok(SpawnedDataflow {
        uuid,
        daemons,
        nodes,
        node_to_daemon,
    })
}

/// Resolve which daemon should run a node based on its deploy config.
/// Priority: machine > labels > unnamed.
pub(super) fn resolve_daemon(
    connections: &DaemonConnections,
    deploy: Option<&Deploy>,
) -> eyre::Result<DaemonId> {
    match deploy {
        Some(Deploy {
            machine: Some(machine),
            ..
        }) => connections
            .get_matching_daemon_id(machine)
            .cloned()
            .wrap_err_with(|| format!("no matching daemon for machine id `{machine}`")),
        Some(d) if !d.labels.is_empty() => connections
            .get_matching_daemon_by_labels(&d.labels)
            .cloned()
            .wrap_err_with(|| format!("no daemon matches labels {:?}", d.labels)),
        _ => connections
            .unnamed()
            .next()
            .cloned()
            .wrap_err("no unnamed daemon connections"),
    }
}

#[derive(Debug)]
pub struct SpawnedDataflow {
    pub uuid: Uuid,
    pub daemons: BTreeSet<DaemonId>,
    pub nodes: BTreeMap<NodeId, ResolvedNode>,
    pub node_to_daemon: BTreeMap<NodeId, DaemonId>,
}

/// Best-effort compensating rollback: send `StopDataflow` to each daemon that
/// already acknowledged a successful spawn. Returns a list of `(daemon_id, error)`
/// for any daemons where the stop itself failed.
///
/// # Preconditions
///
/// `spawned_daemons` must only contain daemon IDs that the coordinator confirmed
/// as successfully spawned during the current `spawn_dataflow` call.
///
/// # Design notes
///
/// - Uses `force: true` because nodes have not reached a ready state during a
///   partial spawn, so graceful shutdown hooks are unnecessary.
/// - Stops are issued sequentially because `&mut DaemonConnections` cannot
///   hand out multiple mutable references. Each call is individually bounded
///   by `TCP_READ_TIMEOUT`.
/// - Daemon-supplied error strings are truncated to prevent oversized messages
///   propagating to clients.
async fn rollback_spawned_daemons(
    dataflow_id: Uuid,
    spawned_daemons: &BTreeSet<DaemonId>,
    daemon_connections: &mut DaemonConnections,
    clock: &HLC,
) -> Vec<(DaemonId, String)> {
    if spawned_daemons.is_empty() {
        return Vec::new();
    }
    tracing::warn!(
        "rolling back dataflow `{dataflow_id}` on {} daemon(s) after partial spawn failure",
        spawned_daemons.len()
    );

    let stop_message = match serde_json::to_vec(&Timestamped {
        inner: DaemonCoordinatorEvent::StopDataflow {
            dataflow_id,
            grace_duration: None,
            force: true,
        },
        timestamp: clock.new_timestamp(),
    }) {
        Ok(m) => m,
        Err(e) => {
            // Serialization failure is unlikely but fatal for rollback.
            return spawned_daemons
                .iter()
                .map(|id| (id.clone(), format!("failed to serialize stop message: {e}")))
                .collect();
        }
    };

    // NOTE: We cannot use `join_all` here because each `send_and_receive`
    // requires `&mut DaemonConnection`, and `DaemonConnections` cannot hand out
    // multiple mutable references concurrently. Each call is individually bounded
    // by `TCP_READ_TIMEOUT`, so worst-case wall time is N × timeout.
    let mut errors = Vec::new();
    for daemon_id in spawned_daemons {
        // The `?` stays inside this async block intentionally: one daemon's
        // failure must not abort rollback for the remaining daemons.
        let result: eyre::Result<()> = async {
            let conn = daemon_connections
                .get_mut(daemon_id)
                .wrap_err("no daemon connection")?;
            let reply_raw = conn
                .send_and_receive(&stop_message)
                .await
                .wrap_err("failed to send/receive stop message")?;
            match serde_json::from_slice(&reply_raw).wrap_err("failed to deserialize stop reply")? {
                DaemonCoordinatorReply::StopResult(r) => {
                    r.map_err(|e| eyre!(e)).wrap_err("daemon stop failed")?;
                }
                other => bail!("unexpected reply during rollback: {other:?}"),
            }
            Ok(())
        }
        .await;

        match result {
            Ok(()) => {
                tracing::info!(
                    "rollback stop succeeded for daemon `{daemon_id}` \
                     on dataflow `{dataflow_id}`"
                );
            }
            Err(e) => {
                tracing::error!(
                    "rollback stop failed for daemon `{daemon_id}` \
                     on dataflow `{dataflow_id}`: {e:#}"
                );
                let msg = format!("{e:#}");
                let msg = crate::state::truncate_str(&msg, MAX_ROLLBACK_ERROR_BYTES);
                errors.push((daemon_id.clone(), msg.to_owned()));
            }
        }
    }
    errors
}

/// Maximum bytes for a single daemon error string in rollback summaries.
const MAX_ROLLBACK_ERROR_BYTES: usize = 1024;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::DaemonConnection;
    use std::collections::HashMap;
    use std::sync::Arc;
    use tokio::sync::{Mutex, mpsc};

    /// Create a mock `DaemonConnection` + a background task that auto-replies.
    ///
    /// The mock extracts the WsRequest correlation `id` and the
    /// `DaemonCoordinatorEvent` variant name from each incoming message,
    /// passes the variant to `reply_fn`, and resolves the pending oneshot.
    ///
    /// It also asserts that the params deserialize into a real
    /// `Timestamped<DaemonCoordinatorEvent>`, so a malformed frame fails here
    /// rather than being reduced to an empty variant name and silently taking
    /// the `reply_fn` fallback branch.
    fn mock_daemon(
        reply_fn: impl Fn(&str) -> DaemonCoordinatorReply + Send + 'static,
    ) -> DaemonConnection {
        let (tx, mut rx) = mpsc::channel::<String>(16);
        let pending: Arc<Mutex<HashMap<Uuid, tokio::sync::oneshot::Sender<String>>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let pending_clone = pending.clone();

        tokio::spawn(async move {
            while let Some(msg) = rx.recv().await {
                let v: serde_json::Value = serde_json::from_str(&msg).unwrap();
                let id: Uuid = v["id"].as_str().unwrap().parse().unwrap();

                // The whole frame must be a well-formed daemon command, not just
                // something whose `inner` happens to have one key. This runs in
                // the mock's task, so a failure surfaces as this panic plus the
                // caller timing out waiting for a reply that never comes.
                serde_json::from_value::<Timestamped<DaemonCoordinatorEvent>>(v["params"].clone())
                    .expect("coordinator sent a malformed Timestamped<DaemonCoordinatorEvent>");

                // Extract variant name from `params.inner`. The params field
                // is embedded as raw JSON (not a string). For externally-tagged
                // enum variants like `StopDataflow { .. }`, serde produces
                // `{"StopDataflow": {...}}`. For newtype variants like
                // `Spawn(...)`, serde produces `{"Spawn": {...}}`.
                let inner = &v["params"]["inner"];
                let variant = if let Some(obj) = inner.as_object() {
                    obj.keys().next().cloned().unwrap_or_default()
                } else if let Some(s) = inner.as_str() {
                    s.to_owned()
                } else {
                    String::new()
                };

                let reply = reply_fn(&variant);
                let reply_json = serde_json::to_string(&reply).unwrap();

                let sender = pending_clone.lock().await.remove(&id);
                if let Some(tx) = sender {
                    let _ = tx.send(reply_json);
                }
            }
        });

        DaemonConnection::new(tx, pending, BTreeMap::new())
    }

    #[tokio::test]
    async fn rollback_sends_stop_to_spawned_daemons() {
        let clock = HLC::default();
        let dataflow_id = Uuid::new_v4();

        let stop_received = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let stop_flag = stop_received.clone();

        let conn = mock_daemon(move |variant| {
            assert_eq!(
                variant, "StopDataflow",
                "expected StopDataflow, got {variant}"
            );
            stop_flag.store(true, std::sync::atomic::Ordering::SeqCst);
            DaemonCoordinatorReply::StopResult(Ok(()))
        });

        let daemon_id = DaemonId::new(Some("rollback-test".to_string()));
        let mut connections = DaemonConnections::default();
        connections.add(daemon_id.clone(), conn);

        let spawned = BTreeSet::from([daemon_id]);
        let errors =
            rollback_spawned_daemons(dataflow_id, &spawned, &mut connections, &clock).await;

        assert!(errors.is_empty(), "rollback should succeed: {errors:?}");
        assert!(
            stop_received.load(std::sync::atomic::Ordering::SeqCst),
            "daemon should have received StopDataflow"
        );
    }

    #[tokio::test]
    async fn rollback_reports_daemon_stop_failure() {
        let clock = HLC::default();
        let dataflow_id = Uuid::new_v4();

        let conn = mock_daemon(|variant| {
            assert_eq!(variant, "StopDataflow");
            DaemonCoordinatorReply::StopResult(Err("stop rejected".into()))
        });

        let daemon_id = DaemonId::new(Some("fail-stop".to_string()));
        let mut connections = DaemonConnections::default();
        connections.add(daemon_id.clone(), conn);

        let spawned = BTreeSet::from([daemon_id.clone()]);
        let errors =
            rollback_spawned_daemons(dataflow_id, &spawned, &mut connections, &clock).await;

        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].0, daemon_id);
        assert!(
            errors[0].1.contains("stop rejected"),
            "error should contain daemon message, got: {}",
            errors[0].1
        );
    }

    #[tokio::test]
    async fn rollback_empty_set_is_noop() {
        let clock = HLC::default();
        let mut connections = DaemonConnections::default();
        let errors =
            rollback_spawned_daemons(Uuid::new_v4(), &BTreeSet::new(), &mut connections, &clock)
                .await;
        assert!(errors.is_empty());
    }

    #[tokio::test]
    async fn spawn_failure_triggers_rollback_on_previous_daemons() {
        let clock = HLC::default();

        let first_stop_received = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let first_stop_flag = first_stop_received.clone();

        // First daemon: spawn succeeds, stop (rollback) succeeds.
        let first_conn = mock_daemon(move |variant| match variant {
            "Spawn" => DaemonCoordinatorReply::TriggerSpawnResult(Ok(())),
            "StopDataflow" => {
                first_stop_flag.store(true, std::sync::atomic::Ordering::SeqCst);
                DaemonCoordinatorReply::StopResult(Ok(()))
            }
            other => panic!("unexpected variant on first daemon: {other}"),
        });

        // Second daemon: spawn fails.
        let second_conn = mock_daemon(|variant| match variant {
            "Spawn" => {
                DaemonCoordinatorReply::TriggerSpawnResult(Err("node binary missing".into()))
            }
            other => panic!("unexpected variant on second daemon: {other}"),
        });

        // Use machine IDs that sort deterministically: "aaa" < "bbb" in BTreeMap.
        let first_id = DaemonId::new(Some("aaa".to_string()));

        let mut connections = DaemonConnections::default();
        connections.add(first_id.clone(), first_conn);
        connections.add(DaemonId::new(Some("bbb".to_string())), second_conn);

        // Minimal dataflow with two nodes deployed to different machines.
        let dataflow: Descriptor = serde_json::from_str(
            r#"{
                "nodes": [
                    {
                        "id": "node-a",
                        "path": "/tmp/dummy-a",
                        "deploy": { "machine": "aaa" },
                        "outputs": ["out"]
                    },
                    {
                        "id": "node-b",
                        "path": "/tmp/dummy-b",
                        "deploy": { "machine": "bbb" },
                        "inputs": { "in": "node-a/out" }
                    }
                ]
            }"#,
        )
        .unwrap();

        let result = spawn_dataflow(
            None,
            SessionId::generate(),
            dataflow,
            None,
            &mut connections,
            &clock,
            false,
            None,
        )
        .await;

        assert!(result.is_err(), "spawn should fail");
        let err_msg = format!("{:#}", result.unwrap_err());
        assert!(
            err_msg.contains("node binary missing"),
            "error should contain root cause, got: {err_msg}"
        );
        assert!(
            first_stop_received.load(std::sync::atomic::Ordering::SeqCst),
            "first daemon should have received StopDataflow for rollback"
        );
    }

    /// A `dora start` descriptor is never run through `check_dataflow`, so
    /// `spawn_dataflow` must reject a timing value the daemon would panic on
    /// (`Duration::from_secs_f64`) before sending `Spawn` to any daemon.
    #[tokio::test]
    async fn spawn_rejects_invalid_timing_before_contacting_daemons() {
        let clock = HLC::default();
        for (node_field, input_field) in [
            (r#""restart_delay": -1.0,"#, ""),
            (r#""health_check_timeout": 1e300,"#, ""),
            ("", r#", "input_timeout": -0.5"#),
        ] {
            let conn = mock_daemon(|variant| panic!("daemon must not be contacted: {variant}"));
            let mut connections = DaemonConnections::default();
            connections.add(DaemonId::new(None), conn);

            let dataflow: Descriptor = serde_json::from_str(&format!(
                r#"{{
                    "nodes": [
                        {{ "id": "src", "path": "/tmp/dummy-a", "outputs": ["out"] }},
                        {{
                            "id": "sink",
                            "path": "/tmp/dummy-b",
                            {node_field}
                            "inputs": {{ "in": {{ "source": "src/out"{input_field} }} }}
                        }}
                    ]
                }}"#
            ))
            .unwrap();

            let err = spawn_dataflow(
                None,
                SessionId::generate(),
                dataflow,
                None,
                &mut connections,
                &clock,
                false,
                None,
            )
            .await
            .err()
            .unwrap_or_else(|| panic!("spawn should fail for `{node_field}{input_field}`"));
            let msg = format!("{err:#}");
            assert!(
                msg.contains("sink"),
                "error should name the offending node, got: {msg}"
            );
        }
    }

    /// The spawn-path check guards against the `Duration::from_secs_f64`
    /// panic only; it must not newly reject a descriptor that `dora start`
    /// accepts on 1.0.x. A dynamic node carrying a `startup_timeout` is the
    /// case in point: `check_node_timing` rejects it as a descriptor-policy
    /// matter, but the daemon ignores the value for a node it does not spawn
    /// (`check_node_health` skips nodes with no process), so it never
    /// panicked and must still start.
    #[tokio::test]
    async fn spawn_accepts_a_dynamic_node_with_a_startup_timeout() {
        let clock = HLC::default();

        let spawn_received = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let spawn_flag = spawn_received.clone();
        let conn = mock_daemon(move |variant| match variant {
            "Spawn" => {
                spawn_flag.store(true, std::sync::atomic::Ordering::SeqCst);
                DaemonCoordinatorReply::TriggerSpawnResult(Ok(()))
            }
            other => panic!("unexpected variant: {other}"),
        });
        let mut connections = DaemonConnections::default();
        connections.add(DaemonId::new(None), conn);

        let dataflow: Descriptor = serde_json::from_str(
            r#"{
                "nodes": [
                    {
                        "id": "dyn-src",
                        "path": "dynamic",
                        "startup_timeout": 30.0,
                        "outputs": ["out"]
                    },
                    {
                        "id": "sink",
                        "path": "/tmp/dummy-b",
                        "inputs": { "in": "dyn-src/out" }
                    }
                ]
            }"#,
        )
        .unwrap();

        let result = spawn_dataflow(
            None,
            SessionId::generate(),
            dataflow,
            None,
            &mut connections,
            &clock,
            false,
            None,
        )
        .await;

        assert!(
            result.is_ok(),
            "a dynamic node's ignored `startup_timeout` must not block `dora start`, got: {:#}",
            result.err().unwrap()
        );
        assert!(
            spawn_received.load(std::sync::atomic::Ordering::SeqCst),
            "daemon should have been contacted with Spawn"
        );
    }
}