oxirs-stream 0.3.0

Real-time streaming support with Kafka/NATS/MQTT/OPC-UA I/O, RDF Patch, and SPARQL Update delta
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
//! Watermark propagation across operator topologies.
//!
//! A streaming pipeline is a directed acyclic graph of operators.  Watermarks
//! flow from sources through operators to sinks.  At each operator, the
//! *output* watermark is the **minimum** of all *input* watermarks — this is
//! the contract that lets downstream operators close windows safely.
//!
//! This module provides:
//!
//! * [`OperatorId`] — typed wrapper around an operator name.
//! * [`OperatorWatermarkAggregator`] — per-operator aggregator that
//!   combines multiple input watermarks into one output watermark using the
//!   minimum rule.  Built on top of [`super::WatermarkAligner`].
//! * [`WatermarkPropagator`] — operator-graph-wide propagator that drives
//!   updates from upstream sources to downstream sinks while enforcing
//!   monotonicity per output edge.  Returns
//!   [`StreamError::WatermarkViolation`] if any operator emits a non-monotonic
//!   watermark.
//!
//! Contract
//!
//! 1. **Per-operator output non-decreasing**: each operator's emitted
//!    watermark must be ≥ its previously emitted watermark.
//! 2. **Aggregation is min**: an operator with N upstream sources sees the
//!    minimum of the N inputs as its output watermark.
//! 3. **Cycles are not supported**: the topology must be acyclic.
//!
//! Returns
//!
//! * `Ok(Some(global_watermark))` after a successful update.
//! * `Ok(None)` if no operator output watermark could yet be determined
//!   (e.g. a downstream operator without all of its upstream inputs reporting).
//! * `Err(StreamError::WatermarkViolation { … })` on monotonicity violation.

use std::collections::HashMap;

use crate::error::StreamError;

use super::WatermarkAligner;

// ─── OperatorId ──────────────────────────────────────────────────────────────

/// Unique identifier of an operator within a topology.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OperatorId(pub String);

impl OperatorId {
    /// Construct a new identifier.
    pub fn new(name: impl Into<String>) -> Self {
        Self(name.into())
    }

    /// Borrow the underlying string.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl<S: Into<String>> From<S> for OperatorId {
    fn from(s: S) -> Self {
        OperatorId(s.into())
    }
}

// ─── Operator-level aggregator ───────────────────────────────────────────────

/// Aggregates per-input watermarks for a single operator.
///
/// Each operator wraps a [`WatermarkAligner`] keyed by upstream operator id.
/// The output watermark is the global minimum across all inputs and is
/// monotonically non-decreasing.
pub struct OperatorWatermarkAggregator {
    operator_id: OperatorId,
    inputs: WatermarkAligner,
    last_emitted: Option<i64>,
}

impl OperatorWatermarkAggregator {
    /// Create a new aggregator for `operator_id`.
    pub fn new(operator_id: impl Into<OperatorId>) -> Self {
        Self {
            operator_id: operator_id.into(),
            inputs: WatermarkAligner::new(),
            last_emitted: None,
        }
    }

    /// Update the watermark received from `input` upstream.
    ///
    /// Returns the new operator output watermark if all inputs have reported
    /// at least once.  If any input is missing returns `None`.
    pub fn observe(
        &mut self,
        input: &OperatorId,
        watermark_ms: i64,
        expected_inputs: usize,
    ) -> Result<Option<i64>, StreamError> {
        self.inputs.update(input.as_str(), watermark_ms);

        if self.inputs.source_count() < expected_inputs {
            return Ok(None);
        }

        let candidate = self.inputs.global_watermark();
        if let Some(prev) = self.last_emitted {
            if candidate < prev {
                return Err(StreamError::WatermarkViolation {
                    operator_id: self.operator_id.0.clone(),
                    reason: format!("candidate output watermark {candidate} < previous {prev}"),
                });
            }
        }
        self.last_emitted = Some(candidate);
        Ok(Some(candidate))
    }

    /// Most recently emitted output watermark, if any.
    pub fn last_emitted(&self) -> Option<i64> {
        self.last_emitted
    }

    /// Operator identifier.
    pub fn operator_id(&self) -> &OperatorId {
        &self.operator_id
    }
}

// ─── Topology-level propagator ───────────────────────────────────────────────

/// Topology-wide watermark propagator.
///
/// The topology is described by `add_edge(upstream, downstream)`.  Source
/// watermarks are pushed via [`WatermarkPropagator::push_source`]; the
/// propagator then fans them through the topology and returns the resulting
/// global watermark — the minimum across all sinks.
pub struct WatermarkPropagator {
    // upstream → downstream edges
    downstream_of: HashMap<OperatorId, Vec<OperatorId>>,
    // downstream → upstream edges (so we can compute expected_inputs)
    upstream_of: HashMap<OperatorId, Vec<OperatorId>>,
    // every operator known to the topology
    operators: HashMap<OperatorId, OperatorWatermarkAggregator>,
    // sinks (no downstream).
    sinks: Vec<OperatorId>,
}

impl Default for WatermarkPropagator {
    fn default() -> Self {
        Self::new()
    }
}

impl WatermarkPropagator {
    /// Create an empty propagator.
    pub fn new() -> Self {
        Self {
            downstream_of: HashMap::new(),
            upstream_of: HashMap::new(),
            operators: HashMap::new(),
            sinks: Vec::new(),
        }
    }

    /// Register an operator.  Idempotent.
    pub fn add_operator(&mut self, operator: OperatorId) {
        self.operators
            .entry(operator.clone())
            .or_insert_with(|| OperatorWatermarkAggregator::new(operator));
    }

    /// Register a directed edge `upstream → downstream`.
    ///
    /// Both endpoints are auto-added if not already present.
    pub fn add_edge(&mut self, upstream: OperatorId, downstream: OperatorId) {
        self.add_operator(upstream.clone());
        self.add_operator(downstream.clone());
        self.downstream_of
            .entry(upstream.clone())
            .or_default()
            .push(downstream.clone());
        self.upstream_of
            .entry(downstream)
            .or_default()
            .push(upstream);
        self.recompute_sinks();
    }

    fn recompute_sinks(&mut self) {
        self.sinks = self
            .operators
            .keys()
            .filter(|op| !self.downstream_of.contains_key(*op))
            .cloned()
            .collect();
    }

    /// Push a source watermark.  The source is treated as having a single
    /// virtual upstream (itself) so the aggregator's monotonicity logic
    /// applies directly.
    ///
    /// Returns the new global watermark (minimum across all sinks), or
    /// `Ok(None)` if any sink is still missing inputs.
    pub fn push_source(
        &mut self,
        source: &OperatorId,
        watermark_ms: i64,
    ) -> Result<Option<i64>, StreamError> {
        // Treat the source as both the operator and its own only upstream.
        if !self.operators.contains_key(source) {
            self.add_operator(source.clone());
        }
        let agg = self
            .operators
            .get_mut(source)
            .expect("source aggregator just added");
        agg.observe(source, watermark_ms, 1)?;

        // BFS down the graph, propagating watermarks.
        let mut frontier: Vec<OperatorId> =
            self.downstream_of.get(source).cloned().unwrap_or_default();

        while let Some(op) = frontier.pop() {
            // Compute the new candidate watermark for `op` from all its
            // upstreams' last_emitted.  If any upstream hasn't emitted yet,
            // skip — `op` cannot move yet.
            let upstreams: Vec<OperatorId> = self.upstream_of.get(&op).cloned().unwrap_or_default();
            if upstreams.is_empty() {
                continue;
            }

            // Collect (upstream_id, watermark_value) snapshots first so we
            // don't overlap a mutable borrow on `self.operators`.
            let mut readings: Vec<(OperatorId, i64)> = Vec::with_capacity(upstreams.len());
            let mut all_ready = true;
            for u in &upstreams {
                match self.operators.get(u).and_then(|a| a.last_emitted()) {
                    Some(v) => readings.push((u.clone(), v)),
                    None => {
                        all_ready = false;
                        break;
                    }
                }
            }
            if !all_ready {
                continue;
            }

            // Feed each upstream's current watermark into `op`'s aggregator.
            let n_inputs = upstreams.len();
            for (u, wm) in &readings {
                let agg = self
                    .operators
                    .get_mut(&op)
                    .expect("operator known to topology");
                agg.observe(u, *wm, n_inputs)?;
            }

            // Schedule descendants.
            if let Some(ds) = self.downstream_of.get(&op) {
                frontier.extend(ds.iter().cloned());
            }
        }

        Ok(self.global_watermark())
    }

    /// The global watermark: the minimum across all sinks' last_emitted.
    /// `None` if any sink hasn't emitted yet.
    pub fn global_watermark(&self) -> Option<i64> {
        if self.sinks.is_empty() {
            // No sinks ⇒ global = min across all operators that have emitted.
            let mut min_v: Option<i64> = None;
            for (_, agg) in self.operators.iter() {
                let v = agg.last_emitted()?;
                min_v = Some(min_v.map(|m| m.min(v)).unwrap_or(v));
            }
            return min_v;
        }
        let mut min_v: Option<i64> = None;
        for sink in &self.sinks {
            let v = self.operators.get(sink).and_then(|a| a.last_emitted())?;
            min_v = Some(min_v.map(|m| m.min(v)).unwrap_or(v));
        }
        min_v
    }

    /// Return the last-emitted output watermark for `op`, if any.
    pub fn watermark_of(&self, op: &OperatorId) -> Option<i64> {
        self.operators.get(op).and_then(|a| a.last_emitted())
    }

    /// Number of operators in the topology.
    pub fn operator_count(&self) -> usize {
        self.operators.len()
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn op(name: &str) -> OperatorId {
        OperatorId::new(name)
    }

    // ── OperatorWatermarkAggregator ──────────────────────────────────────────

    #[test]
    fn aggregator_emits_min_across_inputs() {
        let mut agg = OperatorWatermarkAggregator::new("merge");
        let a = op("a");
        let b = op("b");
        // Only one input reported → not enough yet.
        assert_eq!(agg.observe(&a, 1_000, 2).unwrap(), None);
        // Both inputs reported → emit min.
        let out = agg.observe(&b, 800, 2).unwrap();
        assert_eq!(out, Some(800));
    }

    #[test]
    fn aggregator_is_monotonic() {
        let mut agg = OperatorWatermarkAggregator::new("op");
        let a = op("a");
        agg.observe(&a, 5_000, 1).unwrap();
        // Lower watermark must violate monotonicity.
        let err = agg.observe(&a, 4_000, 1).expect_err("monotonic");
        match err {
            StreamError::WatermarkViolation { operator_id, .. } => {
                assert_eq!(operator_id, "op");
            }
            other => panic!("expected WatermarkViolation, got {other:?}"),
        }
    }

    #[test]
    fn aggregator_equal_watermark_is_ok() {
        let mut agg = OperatorWatermarkAggregator::new("op");
        let a = op("a");
        agg.observe(&a, 1_000, 1).unwrap();
        // Equal is allowed.
        assert_eq!(agg.observe(&a, 1_000, 1).unwrap(), Some(1_000));
    }

    // ── WatermarkPropagator ──────────────────────────────────────────────────

    #[test]
    fn propagator_single_source_to_single_sink() {
        let mut p = WatermarkPropagator::new();
        let s = op("source");
        let snk = op("sink");
        p.add_edge(s.clone(), snk.clone());

        let g = p.push_source(&s, 1_000).unwrap();
        assert_eq!(g, Some(1_000));
        assert_eq!(p.watermark_of(&s), Some(1_000));
        assert_eq!(p.watermark_of(&snk), Some(1_000));
    }

    #[test]
    fn propagator_two_sources_take_min() {
        // src_a → join, src_b → join, join → sink
        let mut p = WatermarkPropagator::new();
        let a = op("a");
        let b = op("b");
        let j = op("j");
        let s = op("sink");
        p.add_edge(a.clone(), j.clone());
        p.add_edge(b.clone(), j.clone());
        p.add_edge(j.clone(), s.clone());

        // First source reports 1000.
        let g = p.push_source(&a, 1_000).unwrap();
        // join cannot move yet (b not reported).
        assert_eq!(p.watermark_of(&j), None);
        assert_eq!(g, None);

        // Second source reports 700.
        let g = p.push_source(&b, 700).unwrap();
        // join takes min(1000, 700) = 700.
        assert_eq!(p.watermark_of(&j), Some(700));
        assert_eq!(p.watermark_of(&s), Some(700));
        assert_eq!(g, Some(700));
    }

    #[test]
    fn propagator_global_watermark_is_min_across_sinks() {
        // src → sink_a (sink_a is one sink)
        // src → sink_b (sink_b is another sink)
        let mut p = WatermarkPropagator::new();
        let s = op("src");
        let sa = op("sa");
        let sb = op("sb");
        p.add_edge(s.clone(), sa.clone());
        p.add_edge(s.clone(), sb.clone());

        let g = p.push_source(&s, 5_000).unwrap();
        assert_eq!(g, Some(5_000));
        assert_eq!(p.watermark_of(&sa), Some(5_000));
        assert_eq!(p.watermark_of(&sb), Some(5_000));
    }

    #[test]
    fn propagator_is_monotonic_across_topology() {
        let mut p = WatermarkPropagator::new();
        let s = op("src");
        let snk = op("snk");
        p.add_edge(s.clone(), snk.clone());
        p.push_source(&s, 1_000).unwrap();
        // Decrease must fail.
        let err = p.push_source(&s, 500).expect_err("monotonic");
        assert!(matches!(err, StreamError::WatermarkViolation { .. }));
    }

    #[test]
    fn propagator_no_topology_treats_each_op_as_sink() {
        let mut p = WatermarkPropagator::new();
        let s = op("solo");
        // No edges; "solo" is its own sink.
        let g = p.push_source(&s, 42).unwrap();
        assert_eq!(g, Some(42));
        assert_eq!(p.operator_count(), 1);
    }
}