mocra-dag 0.4.1

Generic distributed DAG execution engine for the mocra crawler framework (zero crawler coupling).
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
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::sync::Arc;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DagError {
    DuplicateNode(String),
    NodeNotFound(String),
    PrecedingNodeNotFound(String),
    CycleDetected,
    EmptyGraph,
    MissingNodeCompute(String),
    NodeExecutionFailed {
        node_id: String,
        reason: String,
    },
    RetryExhausted {
        node_id: String,
        attempts: usize,
        max_retries: usize,
        retryable: bool,
        last_error: String,
    },
    TaskJoinFailed {
        node_id: String,
        reason: String,
    },
    RunAlreadyInProgress {
        lock_key: String,
    },
    RunGuardAcquireFailed {
        lock_key: String,
        reason: String,
    },
    RunGuardRenewFailed {
        lock_key: String,
        reason: String,
    },
    RunGuardReleaseFailed {
        lock_key: String,
        reason: String,
    },
    MissingRunFencingToken {
        resource: String,
    },
    FencingTokenRejected {
        resource: String,
        token: u64,
        reason: String,
    },
    InvalidPayloadEnvelope(String),
    UnsupportedPayloadVersion(u8),
    ReservedControlNode(String),
    InvalidPrecedingControlNode(String),
    RemoteDispatchNotConfigured(String),
    NodeTimeout {
        node_id: String,
        timeout_ms: u64,
    },
    ExecutionTimeout {
        run_id: String,
        timeout_ms: u64,
    },
    ExecutionIncomplete {
        run_id: String,
        unfinished_nodes: usize,
    },
    InvalidStateTransition {
        node_id: String,
        from: DagNodeStatus,
        to: DagNodeStatus,
    },
}

impl Display for DagError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            DagError::DuplicateNode(id) => write!(f, "duplicate node id: {id}"),
            DagError::NodeNotFound(id) => write!(f, "node not found: {id}"),
            DagError::PrecedingNodeNotFound(id) => write!(f, "preceding node not found: {id}"),
            DagError::CycleDetected => write!(f, "cycle detected in dag"),
            DagError::EmptyGraph => write!(f, "dag graph is empty"),
            DagError::MissingNodeCompute(id) => {
                write!(f, "missing compute function for node: {id}")
            }
            DagError::NodeExecutionFailed { node_id, reason } => {
                write!(f, "node execution failed: node={node_id} reason={reason}")
            }
            DagError::RetryExhausted {
                node_id,
                attempts,
                max_retries,
                retryable,
                last_error,
            } => {
                write!(
                    f,
                    "node retry exhausted: node={node_id} attempts={attempts} max_retries={max_retries} retryable={retryable} last_error={last_error}"
                )
            }
            DagError::TaskJoinFailed { node_id, reason } => {
                write!(f, "node task join failed: node={node_id} reason={reason}")
            }
            DagError::RunAlreadyInProgress { lock_key } => {
                write!(f, "dag run already in progress: lock_key={lock_key}")
            }
            DagError::RunGuardAcquireFailed { lock_key, reason } => {
                write!(
                    f,
                    "dag run guard acquire failed: lock_key={lock_key} reason={reason}"
                )
            }
            DagError::RunGuardRenewFailed { lock_key, reason } => {
                write!(
                    f,
                    "dag run guard renew failed: lock_key={lock_key} reason={reason}"
                )
            }
            DagError::RunGuardReleaseFailed { lock_key, reason } => {
                write!(
                    f,
                    "dag run guard release failed: lock_key={lock_key} reason={reason}"
                )
            }
            DagError::MissingRunFencingToken { resource } => {
                write!(f, "missing run fencing token for resource: {resource}")
            }
            DagError::FencingTokenRejected {
                resource,
                token,
                reason,
            } => {
                write!(
                    f,
                    "fencing token rejected: resource={resource} token={token} reason={reason}"
                )
            }
            DagError::InvalidPayloadEnvelope(reason) => {
                write!(f, "invalid payload envelope: {reason}")
            }
            DagError::UnsupportedPayloadVersion(version) => {
                write!(f, "unsupported payload version: {version}")
            }
            DagError::ReservedControlNode(id) => {
                write!(f, "node id is reserved as control node: {id}")
            }
            DagError::InvalidPrecedingControlNode(id) => {
                write!(f, "invalid preceding control node: {id}")
            }
            DagError::RemoteDispatchNotConfigured(id) => {
                write!(f, "remote dispatch not configured for node: {id}")
            }
            DagError::NodeTimeout {
                node_id,
                timeout_ms,
            } => {
                write!(
                    f,
                    "node execution timeout: node={node_id} timeout_ms={timeout_ms}"
                )
            }
            DagError::ExecutionTimeout { run_id, timeout_ms } => {
                write!(
                    f,
                    "dag execution timeout: run_id={run_id} timeout_ms={timeout_ms}"
                )
            }
            DagError::ExecutionIncomplete {
                run_id,
                unfinished_nodes,
            } => {
                write!(
                    f,
                    "dag execution incomplete: run_id={run_id} unfinished_nodes={unfinished_nodes}"
                )
            }
            DagError::InvalidStateTransition { node_id, from, to } => {
                write!(
                    f,
                    "invalid node state transition: node={node_id} from={from:?} to={to:?}"
                )
            }
        }
    }
}

impl Error for DagError {}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TaskPayload {
    pub bytes: Vec<u8>,
    pub content_type: Option<String>,
    pub encoding: Option<String>,
    pub metadata: HashMap<String, String>,
}

impl TaskPayload {
    const ENVELOPE_MAGIC: [u8; 4] = *b"MDAG";
    const ENVELOPE_VERSION: u8 = 1;

    pub fn from_bytes(bytes: Vec<u8>) -> Self {
        Self {
            bytes,
            content_type: None,
            encoding: None,
            metadata: HashMap::new(),
        }
    }

    pub fn with_content_type(mut self, content_type: impl AsRef<str>) -> Self {
        self.content_type = Some(content_type.as_ref().to_string());
        self
    }

    pub fn with_encoding(mut self, encoding: impl AsRef<str>) -> Self {
        self.encoding = Some(encoding.as_ref().to_string());
        self
    }

    pub fn with_meta(mut self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self {
        self.metadata
            .insert(key.as_ref().to_string(), value.as_ref().to_string());
        self
    }

    pub fn to_envelope_bytes(&self) -> Result<Vec<u8>, DagError> {
        fn to_u16_len(len: usize, field: &str) -> Result<u16, DagError> {
            u16::try_from(len)
                .map_err(|_| DagError::InvalidPayloadEnvelope(format!("{field} too large")))
        }
        fn to_u32_len(len: usize, field: &str) -> Result<u32, DagError> {
            u32::try_from(len)
                .map_err(|_| DagError::InvalidPayloadEnvelope(format!("{field} too large")))
        }

        let ct_bytes = self.content_type.as_deref().unwrap_or("").as_bytes();
        let enc_bytes = self.encoding.as_deref().unwrap_or("").as_bytes();

        let ct_len = to_u16_len(ct_bytes.len(), "content_type")?;
        let enc_len = to_u16_len(enc_bytes.len(), "encoding")?;
        let meta_count = to_u16_len(self.metadata.len(), "metadata_count")?;
        let data_len = to_u32_len(self.bytes.len(), "bytes")?;

        let mut out = Vec::new();
        out.extend_from_slice(&Self::ENVELOPE_MAGIC);
        out.push(Self::ENVELOPE_VERSION);
        out.extend_from_slice(&ct_len.to_le_bytes());
        out.extend_from_slice(&enc_len.to_le_bytes());
        out.extend_from_slice(&meta_count.to_le_bytes());
        out.extend_from_slice(&data_len.to_le_bytes());
        out.extend_from_slice(ct_bytes);
        out.extend_from_slice(enc_bytes);

        let mut keys: Vec<&String> = self.metadata.keys().collect();
        keys.sort();

        for key in keys {
            let value = self
                .metadata
                .get(key)
                .ok_or_else(|| DagError::InvalidPayloadEnvelope("metadata key missing".into()))?;
            let k = key.as_bytes();
            let v = value.as_bytes();
            let k_len = to_u16_len(k.len(), "metadata_key")?;
            let v_len = to_u16_len(v.len(), "metadata_value")?;
            out.extend_from_slice(&k_len.to_le_bytes());
            out.extend_from_slice(&v_len.to_le_bytes());
            out.extend_from_slice(k);
            out.extend_from_slice(v);
        }

        out.extend_from_slice(&self.bytes);
        Ok(out)
    }

    pub fn from_envelope_bytes(input: &[u8]) -> Result<Self, DagError> {
        struct Cursor<'a> {
            buf: &'a [u8],
            pos: usize,
        }
        impl<'a> Cursor<'a> {
            fn new(buf: &'a [u8]) -> Self {
                Self { buf, pos: 0 }
            }
            fn read_exact(&mut self, n: usize) -> Result<&'a [u8], DagError> {
                if self.pos + n > self.buf.len() {
                    return Err(DagError::InvalidPayloadEnvelope("unexpected eof".into()));
                }
                let s = &self.buf[self.pos..self.pos + n];
                self.pos += n;
                Ok(s)
            }
            fn read_u8(&mut self) -> Result<u8, DagError> {
                Ok(self.read_exact(1)?[0])
            }
            fn read_u16(&mut self) -> Result<u16, DagError> {
                let b = self.read_exact(2)?;
                Ok(u16::from_le_bytes([b[0], b[1]]))
            }
            fn read_u32(&mut self) -> Result<u32, DagError> {
                let b = self.read_exact(4)?;
                Ok(u32::from_le_bytes([b[0], b[1], b[2], b[3]]))
            }
            fn read_str(&mut self, len: usize) -> Result<String, DagError> {
                let b = self.read_exact(len)?;
                std::str::from_utf8(b)
                    .map(|s| s.to_string())
                    .map_err(|_| DagError::InvalidPayloadEnvelope("invalid utf8".into()))
            }
        }

        let mut c = Cursor::new(input);
        let magic = c.read_exact(4)?;
        if magic != Self::ENVELOPE_MAGIC {
            return Err(DagError::InvalidPayloadEnvelope("bad magic".into()));
        }

        let version = c.read_u8()?;
        if version != Self::ENVELOPE_VERSION {
            return Err(DagError::UnsupportedPayloadVersion(version));
        }

        let ct_len = c.read_u16()? as usize;
        let enc_len = c.read_u16()? as usize;
        let meta_count = c.read_u16()? as usize;
        let data_len = c.read_u32()? as usize;

        let content_type = {
            let s = c.read_str(ct_len)?;
            if s.is_empty() { None } else { Some(s) }
        };
        let encoding = {
            let s = c.read_str(enc_len)?;
            if s.is_empty() { None } else { Some(s) }
        };

        let mut metadata = HashMap::new();
        for _ in 0..meta_count {
            let k_len = c.read_u16()? as usize;
            let v_len = c.read_u16()? as usize;
            let key = c.read_str(k_len)?;
            let value = c.read_str(v_len)?;
            metadata.insert(key, value);
        }

        let bytes = c.read_exact(data_len)?.to_vec();
        if c.pos != input.len() {
            return Err(DagError::InvalidPayloadEnvelope("trailing bytes".into()));
        }

        Ok(Self {
            bytes,
            content_type,
            encoding,
            metadata,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DagNodeStatus {
    Pending,
    Ready,
    Running,
    Succeeded,
    Failed,
}

#[derive(Debug, Clone)]
pub struct NodeExecutionContext {
    pub run_id: String,
    pub run_fencing_token: Option<u64>,
    pub node_id: String,
    pub attempt: usize,
    pub upstream_nodes: Vec<String>,
    pub upstream_outputs: HashMap<String, TaskPayload>,
    pub layer_index: usize,
}

#[async_trait]
pub trait DagNodeTrait: Send + Sync {
    async fn start(&self, context: NodeExecutionContext) -> Result<TaskPayload, DagError>;
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum DagErrorCode {
    DuplicateNode,
    NodeNotFound,
    PrecedingNodeNotFound,
    CycleDetected,
    EmptyGraph,
    MissingNodeCompute,
    NodeExecutionFailed,
    RetryExhausted,
    TaskJoinFailed,
    RunAlreadyInProgress,
    RunGuardAcquireFailed,
    RunGuardRenewFailed,
    RunGuardReleaseFailed,
    MissingRunFencingToken,
    FencingTokenRejected,
    InvalidPayloadEnvelope,
    UnsupportedPayloadVersion,
    ReservedControlNode,
    InvalidPrecedingControlNode,
    RemoteDispatchNotConfigured,
    NodeTimeout,
    ExecutionTimeout,
    ExecutionIncomplete,
    InvalidStateTransition,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum DagErrorClass {
    Retryable,
    NonRetryable,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DagNodeSyncState {
    pub run_id: String,
    pub run_fencing_token: Option<u64>,
    pub node_id: String,
    pub stage: String,
    pub placement: String,
    pub worker_group: Option<String>,
    pub worker_id: Option<String>,
    pub attempt: usize,
    pub layer_index: usize,
    pub idempotency_key: Option<String>,
    pub deadline_ms: Option<u64>,
    pub dispatch_latency_ms: Option<u64>,
    pub error_code: Option<DagErrorCode>,
    pub error_class: Option<DagErrorClass>,
    pub error: Option<String>,
    pub timestamp_ms: u64,
}

#[derive(Debug, Clone, Default)]
pub struct DagRunResumeState {
    pub run_id: String,
    pub run_fencing_token: Option<u64>,
    pub succeeded_outputs: HashMap<String, TaskPayload>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NodePlacement {
    Local,
    Remote { worker_group: String },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DagNodeRetryMode {
    AllErrors,
    RetryableOnly,
}

#[derive(Debug, Clone)]
pub struct DagNodeExecutionPolicy {
    pub max_retries: usize,
    pub timeout_ms: Option<u64>,
    pub retry_backoff_ms: u64,
    pub idempotency_key: Option<String>,
    pub retry_mode: DagNodeRetryMode,
    pub circuit_breaker_failure_threshold: Option<usize>,
    pub circuit_breaker_open_ms: u64,
}

impl Default for DagNodeExecutionPolicy {
    fn default() -> Self {
        Self {
            max_retries: 0,
            timeout_ms: None,
            retry_backoff_ms: 0,
            idempotency_key: None,
            retry_mode: DagNodeRetryMode::AllErrors,
            circuit_breaker_failure_threshold: None,
            circuit_breaker_open_ms: 0,
        }
    }
}

#[async_trait]
pub trait DagNodeDispatcher: Send + Sync {
    async fn dispatch(
        &self,
        node_id: &str,
        placement: &NodePlacement,
        executor: Arc<dyn DagNodeTrait>,
        context: NodeExecutionContext,
    ) -> Result<TaskPayload, DagError>;
}

#[async_trait]
pub trait DagFencingStore: Send + Sync {
    async fn commit(
        &self,
        resource: &str,
        token: u64,
        node_id: &str,
        payload: &TaskPayload,
    ) -> Result<(), DagError>;
}

#[async_trait]
pub trait DagRunStateStore: Send + Sync {
    async fn load(&self, run_key: &str) -> Result<Option<DagRunResumeState>, DagError>;
    async fn save(&self, run_key: &str, state: &DagRunResumeState) -> Result<(), DagError>;
    async fn clear(&self, run_key: &str) -> Result<(), DagError>;
}

#[derive(Debug, Clone)]
pub struct DagRunGuardAcquireOutcome {
    pub acquired: bool,
    pub fencing_token: Option<u64>,
}

#[async_trait]
pub trait DagRunGuard: Send + Sync {
    async fn try_acquire(
        &self,
        lock_key: &str,
        owner: &str,
        ttl_ms: u64,
    ) -> Result<DagRunGuardAcquireOutcome, DagError>;
    async fn renew(&self, lock_key: &str, owner: &str, ttl_ms: u64) -> Result<bool, DagError>;
    async fn release(&self, lock_key: &str, owner: &str) -> Result<(), DagError>;
}

#[derive(Default)]
pub struct LocalNodeDispatcher;

#[async_trait]
impl DagNodeDispatcher for LocalNodeDispatcher {
    async fn dispatch(
        &self,
        node_id: &str,
        placement: &NodePlacement,
        executor: Arc<dyn DagNodeTrait>,
        context: NodeExecutionContext,
    ) -> Result<TaskPayload, DagError> {
        match placement {
            NodePlacement::Local => executor.start(context).await,
            NodePlacement::Remote { .. } => {
                Err(DagError::RemoteDispatchNotConfigured(node_id.to_string()))
            }
        }
    }
}

#[derive(Clone)]
pub struct DagNodeRecord {
    pub id: String,
    pub predecessors: Vec<String>,
    pub status: DagNodeStatus,
    pub placement: NodePlacement,
    pub execution_policy: DagNodeExecutionPolicy,
    pub executor: Option<Arc<dyn DagNodeTrait>>,
    pub result: Option<TaskPayload>,
    pub metadata: HashMap<String, String>,
    pub error: Option<String>,
}