astrs-node-api 0.1.0

The AstRS node API: handshake, typed pub/sub, zero-copy allocation and event streams.
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
//! [`Event`] and [`EventStream`] — what a node's loop reads (blueprint §9.1).
//!
//! ```no_run
//! # use astrs_node_api::prelude::*;
//! # fn run(events: &mut EventStream) {
//! while let Some(event) = events.recv() {
//!     match event {
//!         Event::Input { id, data, meta } => { let _ = (id, data, meta); }
//!         Event::Stop(_) => break,
//!         _ => {}
//!     }
//! }
//! # }
//! ```
//!
//! # Two lanes, one stream
//!
//! Events reach the stream on one of two paths, and the difference is
//! observable (§11.3):
//!
//! | Lane | Events | Behaviour |
//! |---|---|---|
//! | **Control** | `Stop`, `Reload`, `AllInputsClosed`, `NodeFailed`, `Restarted`, `ParamUpdate`, `ParamDeleted`, `ExtDropped`, `Error`, merged external events | Unbounded FIFO, delivered **before** any data |
//! | **Data** | `Input`, `InputClosed`, `InputRecovered` | Per-input bounded queues under the manifest's `queue_size`/`queue_policy`, round-robin across inputs |
//!
//! That is why a `Stop` never waits behind a backlog of camera frames, and
//! why an `InputClosed` *does* wait behind that input's own queued frames —
//! it has to, or a node would see "this input is finished" before the frames
//! that came first.
//!
//! # Fusing
//!
//! After [`Event::Stop`] the stream fuses: it is delivered once, and every
//! later `recv` returns `None` (§9.1). A node that ignores `Stop` and keeps
//! looping therefore terminates rather than spinning.
//!
//! # Queue policy is applied *here*
//!
//! The daemon does not decide what a node drops. Each input owns an
//! [`astrs_scheduler::InputQueue`] on the node side, so `queue_size: 1` means
//! "the node always sees the newest frame" no matter how the daemon batched
//! them, and eviction immunity (§11.2) protects correlated messages from ever
//! being the one dropped.

pub mod queued;
pub mod source;
pub mod stream;

use astrs_wire::messages::control::types::ParamScope;
use astrs_wire::metadata::Parameter;
use astrs_wire::{
    DataId, ExtensionKey, Metadata, NodeExitCause, NodeId, OperatorId, ParamKey, PortRef,
    RouteCloseReason, StopCause,
};

use crate::payload::Payload;

pub use queued::QueuedEvent;
pub use source::EventSource;
pub use stream::{EventStream, Stream};

/// One thing that happened to this node.
///
/// [`Event::Input`] carries exactly the three fields blueprint §9.1
/// destructures — `{ id, data, meta }` — so the example there compiles
/// verbatim. The producer port behind an input is a property of the node's
/// wiring rather than of the message, and is available from
/// [`crate::Node::input_source`].
#[derive(Debug)]
#[non_exhaustive]
pub enum Event {
    /// A message arrived on one of the node's inputs.
    Input {
        /// The input it arrived on.
        id: DataId,
        /// The metadata beside it, with AstRS-internal keys already
        /// stripped (§6.1).
        meta: Metadata,
        /// The payload — decoded lazily, and zero-copy when the route was
        /// upgraded (§6.3).
        data: Payload,
    },
    /// An input will receive nothing further.
    InputClosed {
        /// The input that closed.
        id: DataId,
        /// The producer port that stopped.
        source: PortRef,
        /// Why it closed.
        reason: RouteCloseReason,
    },
    /// A closed input is live again, because its producer restarted (§12).
    InputRecovered {
        /// The input that recovered.
        id: DataId,
        /// The producer port that came back.
        source: PortRef,
        /// The producer's new incarnation.
        generation: u64,
    },
    /// Finish up and exit. The stream fuses after this.
    Stop(StopCause),
    /// Reload the node's code, or one operator inside it (§9.3).
    Reload {
        /// The operator to reload, or `None` for the whole node.
        operator: Option<OperatorId>,
        /// The artefact to load, when it moved.
        path: Option<String>,
    },
    /// Every input of this node has closed.
    AllInputsClosed,
    /// A parameter this node reads was written (§17 `param set`).
    ParamUpdate {
        /// The scope it was written in.
        scope: ParamScope,
        /// The key.
        key: ParamKey,
        /// The new value.
        value: Parameter,
    },
    /// A parameter this node reads was deleted.
    ParamDeleted {
        /// The scope it was deleted from.
        scope: ParamScope,
        /// The key.
        key: ParamKey,
    },
    /// A peer node failed (§12).
    NodeFailed {
        /// The node that failed.
        peer: NodeId,
        /// How it failed.
        cause: NodeExitCause,
    },
    /// A peer node was restarted under its restart policy (§12).
    Restarted {
        /// The node that came back.
        peer: NodeId,
        /// Its new incarnation.
        generation: u64,
    },
    /// An extension entry this node owned was dropped.
    ExtDropped {
        /// The key that went away.
        key: ExtensionKey,
        /// Why.
        reason: String,
    },
    /// The session hit a condition the node should know about but that does
    /// not end it — a queue overflow, an undecodable frame, a route the node
    /// could not map.
    Error(String),
}

impl Event {
    /// The input this event concerns, when it concerns one.
    #[must_use]
    pub const fn input(&self) -> Option<&DataId> {
        match self {
            Self::Input { id, .. }
            | Self::InputClosed { id, .. }
            | Self::InputRecovered { id, .. } => Some(id),
            _ => None,
        }
    }

    /// Whether this event delivers data.
    #[must_use]
    pub const fn is_input(&self) -> bool {
        matches!(self, Self::Input { .. })
    }

    /// Whether this event fuses the stream.
    #[must_use]
    pub const fn is_stop(&self) -> bool {
        matches!(self, Self::Stop(_))
    }

    /// Whether this event travels on the control lane (§11.3).
    #[must_use]
    pub const fn is_control(&self) -> bool {
        !matches!(
            self,
            Self::Input { .. } | Self::InputClosed { .. } | Self::InputRecovered { .. }
        )
    }

    /// Whether this event reports a fault rather than normal progress.
    #[must_use]
    pub fn is_fault(&self) -> bool {
        match self {
            Self::NodeFailed { .. } | Self::Error(_) => true,
            Self::InputClosed { reason, .. } => !reason.is_expected(),
            _ => false,
        }
    }

    /// The metadata this event carries, when it carries any.
    #[must_use]
    pub const fn metadata(&self) -> Option<&Metadata> {
        match self {
            Self::Input { meta, .. } => Some(meta),
            _ => None,
        }
    }

    /// The payload this event carries, when it carries one.
    #[must_use]
    pub const fn payload(&self) -> Option<&Payload> {
        match self {
            Self::Input { data, .. } => Some(data),
            _ => None,
        }
    }

    /// Consumes the event and returns its payload and metadata, if it is an
    /// input.
    #[must_use]
    pub fn into_input(self) -> Option<(DataId, Metadata, Payload)> {
        match self {
            Self::Input { id, meta, data } => Some((id, meta, data)),
            _ => None,
        }
    }

    /// A short, stable slug for metrics labels and structured logs.
    #[must_use]
    pub const fn kind_name(&self) -> &'static str {
        match self {
            Self::Input { .. } => "input",
            Self::InputClosed { .. } => "input_closed",
            Self::InputRecovered { .. } => "input_recovered",
            Self::Stop(_) => "stop",
            Self::Reload { .. } => "reload",
            Self::AllInputsClosed => "all_inputs_closed",
            Self::ParamUpdate { .. } => "param_update",
            Self::ParamDeleted { .. } => "param_deleted",
            Self::NodeFailed { .. } => "node_failed",
            Self::Restarted { .. } => "restarted",
            Self::ExtDropped { .. } => "ext_dropped",
            Self::Error(_) => "error",
        }
    }
}

impl core::fmt::Display for Event {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Input { id, data, .. } => write!(f, "input {id} ({data})"),
            Self::InputClosed { id, reason, .. } => write!(f, "input {id} closed: {reason}"),
            Self::InputRecovered { id, generation, .. } => {
                write!(f, "input {id} recovered at generation {generation}")
            }
            Self::Stop(cause) => write!(f, "stop: {cause}"),
            Self::Reload { operator, .. } => match operator {
                Some(operator) => write!(f, "reload operator {operator}"),
                None => f.write_str("reload node"),
            },
            Self::AllInputsClosed => f.write_str("all inputs closed"),
            Self::ParamUpdate { scope, key, value } => write!(f, "{scope}: {key} = {value}"),
            Self::ParamDeleted { scope, key } => write!(f, "{scope}: {key} deleted"),
            Self::NodeFailed { peer, cause } => write!(f, "peer {peer} failed: {cause}"),
            Self::Restarted { peer, generation } => {
                write!(f, "peer {peer} restarted as generation {generation}")
            }
            Self::ExtDropped { key, reason } => write!(f, "extension {key} dropped: {reason}"),
            Self::Error(message) => write!(f, "error: {message}"),
        }
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

    use super::*;
    use astrs_time::HlcTimestamp;

    fn input() -> Event {
        Event::Input {
            id: DataId::new("frames").unwrap(),
            meta: Metadata::new(HlcTimestamp::new(1, 0)),
            data: Payload::inline(vec![1, 2, 3]),
        }
    }

    fn every_variant() -> Vec<Event> {
        vec![
            input(),
            Event::InputClosed {
                id: DataId::new("frames").unwrap(),
                source: PortRef::from_parts("camera", "image").unwrap(),
                reason: RouteCloseReason::ProducerFinished,
            },
            Event::InputRecovered {
                id: DataId::new("frames").unwrap(),
                source: PortRef::from_parts("camera", "image").unwrap(),
                generation: 2,
            },
            Event::Stop(StopCause::Requested),
            Event::Reload {
                operator: None,
                path: None,
            },
            Event::AllInputsClosed,
            Event::ParamUpdate {
                scope: ParamScope::Global,
                key: ParamKey::new("gain").unwrap(),
                value: Parameter::from(2.0),
            },
            Event::ParamDeleted {
                scope: ParamScope::Global,
                key: ParamKey::new("gain").unwrap(),
            },
            Event::NodeFailed {
                peer: NodeId::new("camera").unwrap(),
                cause: NodeExitCause::Success,
            },
            Event::Restarted {
                peer: NodeId::new("camera").unwrap(),
                generation: 1,
            },
            Event::ExtDropped {
                key: ExtensionKey::user("calibration").unwrap(),
                reason: "ttl".to_owned(),
            },
            Event::Error("queue overflow".to_owned()),
        ]
    }

    #[test]
    fn every_variant_renders_and_classifies() {
        for event in every_variant() {
            assert!(!event.to_string().is_empty(), "{event:?}");
            assert!(!event.kind_name().is_empty());
        }
    }

    #[test]
    fn kind_names_are_unique() {
        let mut names: Vec<&str> = every_variant().iter().map(Event::kind_name).collect();
        let total = names.len();
        names.sort_unstable();
        names.dedup();
        assert_eq!(names.len(), total);
    }

    #[test]
    fn the_control_lane_is_everything_that_is_not_an_input() {
        for event in every_variant() {
            let data_lane = matches!(
                event,
                Event::Input { .. } | Event::InputClosed { .. } | Event::InputRecovered { .. }
            );
            assert_eq!(event.is_control(), !data_lane, "{event}");
        }
    }

    #[test]
    fn inputs_expose_their_parts() {
        let event = input();
        assert!(event.is_input());
        assert!(!event.is_stop());
        assert_eq!(event.input().map(DataId::as_str), Some("frames"));
        assert!(event.metadata().is_some());
        assert_eq!(event.payload().map(Payload::len), Some(3));

        let (id, meta, data) = input().into_input().unwrap();
        assert_eq!(id.as_str(), "frames");
        assert_eq!(meta.timestamp, HlcTimestamp::new(1, 0));
        assert_eq!(data.len(), 3);
        assert!(Event::AllInputsClosed.into_input().is_none());
    }

    #[test]
    fn stop_is_the_only_fusing_event() {
        for event in every_variant() {
            assert_eq!(event.is_stop(), matches!(event, Event::Stop(_)));
        }
    }

    #[test]
    fn faults_are_reported_as_faults() {
        assert!(Event::Error(String::new()).is_fault());
        assert!(
            Event::NodeFailed {
                peer: NodeId::new("x").unwrap(),
                cause: NodeExitCause::Success
            }
            .is_fault()
        );
        assert!(!input().is_fault());
        assert!(
            !Event::InputClosed {
                id: DataId::new("a").unwrap(),
                source: PortRef::from_parts("b", "c").unwrap(),
                reason: RouteCloseReason::ProducerFinished,
            }
            .is_fault(),
            "a producer finishing is not a fault"
        );
    }
}