oxirs-stream 0.2.4

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
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
//! # Consistency Protocols
//!
//! Configurable consistency levels for stream operations, including:
//!
//! - [`StreamConsistencyLevel`]: Consistency semantics (Eventual, ReadYourWrites, etc.)
//! - [`ConsistencyConfig`]: Configuration for read/write consistency with retries
//! - [`VersionedValue`]: A value tagged with version, timestamp, and origin node
//! - [`ConsistencyManager`]: Enforces monotonic-read and monotonic-write guarantees per session
//! - [`EventualConsistencyBuffer`]: Batches writes for eventual synchronisation

use std::collections::HashMap;

// ─── StreamConsistencyLevel ───────────────────────────────────────────────────

/// Consistency semantics for stream read and write operations.
///
/// These map to well-known distributed-systems consistency models.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StreamConsistencyLevel {
    /// Replicas converge eventually; no ordering guarantees.
    Eventual,
    /// A client always reads values it previously wrote.
    ReadYourWrites,
    /// Read responses are non-decreasing in version for a session.
    MonotonicRead,
    /// Write responses are non-decreasing in version for a session.
    MonotonicWrite,
    /// All reads reflect all preceding writes globally.
    Strong,
    /// Operations appear instantaneous and globally ordered.
    Linearizable,
}

// ─── ConsistencyConfig ────────────────────────────────────────────────────────

/// Configures read/write consistency, timeouts, and retry behaviour.
#[derive(Debug, Clone)]
pub struct ConsistencyConfig {
    pub read_level: StreamConsistencyLevel,
    pub write_level: StreamConsistencyLevel,
    pub timeout_ms: u64,
    pub retry_count: usize,
}

impl Default for ConsistencyConfig {
    fn default() -> Self {
        Self {
            read_level: StreamConsistencyLevel::Eventual,
            write_level: StreamConsistencyLevel::Eventual,
            timeout_ms: 1000,
            retry_count: 3,
        }
    }
}

// ─── VersionedValue ───────────────────────────────────────────────────────────

/// A value tagged with a logical version, wall-clock timestamp, and originating node.
#[derive(Debug, Clone)]
pub struct VersionedValue<T> {
    pub value: T,
    pub version: u64,
    pub timestamp: i64,
    pub node_id: String,
}

impl<T> VersionedValue<T> {
    pub fn new(value: T, version: u64, timestamp: i64, node_id: impl Into<String>) -> Self {
        Self {
            value,
            version,
            timestamp,
            node_id: node_id.into(),
        }
    }
}

// ─── ConsistencyManager ───────────────────────────────────────────────────────

/// Enforces per-session monotonic-read and monotonic-write guarantees.
///
/// Sessions are identified by arbitrary string tokens (e.g., client IDs).
pub struct ConsistencyManager {
    config: ConsistencyConfig,
    /// session_id → minimum version that reads must satisfy (MonotonicRead)
    monotonic_read_version: HashMap<String, u64>,
    /// session_id → last write version (MonotonicWrite)
    monotonic_write_version: HashMap<String, u64>,
}

impl ConsistencyManager {
    /// Create a new manager with the given configuration.
    pub fn new(config: ConsistencyConfig) -> Self {
        Self {
            config,
            monotonic_read_version: HashMap::new(),
            monotonic_write_version: HashMap::new(),
        }
    }

    /// Determine whether a read of `value` is acceptable for `session_id`.
    ///
    /// - `Eventual` / `ReadYourWrites` / `Strong` / `Linearizable`: always `true`
    /// - `MonotonicRead`: `value.version >= session's last-seen version`
    pub fn can_read<T>(&mut self, session_id: &str, value: &VersionedValue<T>) -> bool {
        match self.config.read_level {
            StreamConsistencyLevel::MonotonicRead => {
                let min_ver = self
                    .monotonic_read_version
                    .get(session_id)
                    .copied()
                    .unwrap_or(0);
                value.version >= min_ver
            }
            _ => true,
        }
    }

    /// Record that `session_id` has observed `value` (update monotonic-read floor).
    pub fn after_read<T>(&mut self, session_id: &str, value: &VersionedValue<T>) {
        let entry = self
            .monotonic_read_version
            .entry(session_id.to_string())
            .or_insert(0);
        if value.version > *entry {
            *entry = value.version;
        }
    }

    /// Determine whether a write of `value` is valid for `session_id`.
    ///
    /// - `MonotonicWrite`: `value.version >= session's last-write version`
    /// - everything else: always `true`
    pub fn can_write<T>(&mut self, session_id: &str, value: &VersionedValue<T>) -> bool {
        match self.config.write_level {
            StreamConsistencyLevel::MonotonicWrite => {
                let last_ver = self
                    .monotonic_write_version
                    .get(session_id)
                    .copied()
                    .unwrap_or(0);
                value.version >= last_ver
            }
            _ => true,
        }
    }

    /// Record that `session_id` performed a write at `value`'s version.
    pub fn after_write<T>(&mut self, session_id: &str, value: &VersionedValue<T>) {
        let entry = self
            .monotonic_write_version
            .entry(session_id.to_string())
            .or_insert(0);
        if value.version > *entry {
            *entry = value.version;
        }
    }

    /// Return `true` if `value.version` is strictly behind `current_version`
    /// (i.e., the value is stale).
    pub fn is_stale<T>(&self, value: &VersionedValue<T>, current_version: u64) -> bool {
        value.version < current_version
    }

    /// Reference to the active configuration.
    pub fn config(&self) -> &ConsistencyConfig {
        &self.config
    }

    /// How many sessions are tracked for monotonic-read.
    pub fn monotonic_read_session_count(&self) -> usize {
        self.monotonic_read_version.len()
    }

    /// How many sessions are tracked for monotonic-write.
    pub fn monotonic_write_session_count(&self) -> usize {
        self.monotonic_write_version.len()
    }
}

// ─── EventualConsistencyBuffer ────────────────────────────────────────────────

/// Buffers writes for eventual propagation, flushing when `max_lag_ms` has passed.
pub struct EventualConsistencyBuffer {
    pending: Vec<(String, Vec<u8>)>,
    max_lag_ms: u64,
    last_sync_ms: i64,
}

impl EventualConsistencyBuffer {
    /// Create a buffer that flushes after at most `max_lag_ms` milliseconds.
    pub fn new(max_lag_ms: u64) -> Self {
        Self {
            pending: Vec::new(),
            max_lag_ms,
            last_sync_ms: current_ms(),
        }
    }

    /// Stage a key-value write for eventual propagation.
    pub fn buffer(&mut self, key: &str, value: Vec<u8>) {
        self.pending.push((key.to_string(), value));
    }

    /// Return `true` when the lag since last sync exceeds `max_lag_ms`.
    pub fn should_sync(&self, now_ms: i64) -> bool {
        now_ms - self.last_sync_ms >= self.max_lag_ms as i64
    }

    /// Drain all pending writes and reset the sync timer.
    pub fn drain(&mut self) -> Vec<(String, Vec<u8>)> {
        self.last_sync_ms = current_ms();
        std::mem::take(&mut self.pending)
    }

    /// Number of writes currently buffered.
    pub fn pending_count(&self) -> usize {
        self.pending.len()
    }
}

/// Helper: current wall-clock time in milliseconds.
fn current_ms() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as i64)
        .unwrap_or(0)
}

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

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

    fn make_value<T>(value: T, version: u64) -> VersionedValue<T> {
        VersionedValue::new(value, version, 1_700_000_000_000, "node-1")
    }

    // ── ConsistencyConfig ──────────────────────────────────────────────────────

    #[test]
    fn test_default_config() {
        let cfg = ConsistencyConfig::default();
        assert_eq!(cfg.read_level, StreamConsistencyLevel::Eventual);
        assert_eq!(cfg.write_level, StreamConsistencyLevel::Eventual);
        assert_eq!(cfg.timeout_ms, 1000);
        assert_eq!(cfg.retry_count, 3);
    }

    #[test]
    fn test_custom_config() {
        let cfg = ConsistencyConfig {
            read_level: StreamConsistencyLevel::Strong,
            write_level: StreamConsistencyLevel::Linearizable,
            timeout_ms: 500,
            retry_count: 5,
        };
        assert_eq!(cfg.read_level, StreamConsistencyLevel::Strong);
        assert_eq!(cfg.write_level, StreamConsistencyLevel::Linearizable);
    }

    // ── VersionedValue ─────────────────────────────────────────────────────────

    #[test]
    fn test_versioned_value_fields() {
        let vv = make_value("hello", 42);
        assert_eq!(vv.value, "hello");
        assert_eq!(vv.version, 42);
        assert_eq!(vv.node_id, "node-1");
    }

    // ── ConsistencyManager (Eventual) ─────────────────────────────────────────

    #[test]
    fn test_eventual_always_allows_read_and_write() {
        let mut mgr = ConsistencyManager::new(ConsistencyConfig::default());
        let vv = make_value(1u32, 1);
        assert!(mgr.can_read("s1", &vv));
        assert!(mgr.can_write("s1", &vv));
    }

    // ── ConsistencyManager (MonotonicRead) ────────────────────────────────────

    #[test]
    fn test_monotonic_read_initial_allows_any_version() {
        let mut mgr = ConsistencyManager::new(ConsistencyConfig {
            read_level: StreamConsistencyLevel::MonotonicRead,
            ..Default::default()
        });
        let vv = make_value(0u32, 0);
        assert!(mgr.can_read("sess", &vv));
    }

    #[test]
    fn test_monotonic_read_blocks_regression() {
        let mut mgr = ConsistencyManager::new(ConsistencyConfig {
            read_level: StreamConsistencyLevel::MonotonicRead,
            ..Default::default()
        });
        let v5 = make_value(0u32, 5);
        mgr.after_read("sess", &v5);

        // version 4 is a regression – must be blocked
        let v4 = make_value(0u32, 4);
        assert!(!mgr.can_read("sess", &v4));

        // version 5 is same level – must be allowed
        assert!(mgr.can_read("sess", &v5));

        // version 6 advances – must be allowed
        let v6 = make_value(0u32, 6);
        assert!(mgr.can_read("sess", &v6));
    }

    #[test]
    fn test_monotonic_read_sessions_independent() {
        let mut mgr = ConsistencyManager::new(ConsistencyConfig {
            read_level: StreamConsistencyLevel::MonotonicRead,
            ..Default::default()
        });
        let v10 = make_value(0u32, 10);
        mgr.after_read("session-A", &v10);

        // session-B has no history; version 1 is fine
        let v1 = make_value(0u32, 1);
        assert!(mgr.can_read("session-B", &v1));
    }

    #[test]
    fn test_after_read_tracks_max_version() {
        let mut mgr = ConsistencyManager::new(ConsistencyConfig {
            read_level: StreamConsistencyLevel::MonotonicRead,
            ..Default::default()
        });
        mgr.after_read("s", &make_value(0u32, 3));
        mgr.after_read("s", &make_value(0u32, 7));
        mgr.after_read("s", &make_value(0u32, 5)); // lower – should be ignored

        // After seeing version 7, the monotonic floor is 7.
        // v7 (== floor) must be allowed
        let v7 = make_value(0u32, 7);
        assert!(mgr.can_read("s", &v7));
        // v8 (> floor) must be allowed
        let v8 = make_value(0u32, 8);
        assert!(mgr.can_read("s", &v8));
        // v4 (< floor) must be blocked
        let v4 = make_value(0u32, 4);
        assert!(!mgr.can_read("s", &v4));
        // v6 (< floor) must also be blocked
        let v6 = make_value(0u32, 6);
        assert!(!mgr.can_read("s", &v6));
    }

    // ── ConsistencyManager (MonotonicWrite) ───────────────────────────────────

    #[test]
    fn test_monotonic_write_initial_allows_any_version() {
        let mut mgr = ConsistencyManager::new(ConsistencyConfig {
            write_level: StreamConsistencyLevel::MonotonicWrite,
            ..Default::default()
        });
        let vv = make_value(0u32, 0);
        assert!(mgr.can_write("sess", &vv));
    }

    #[test]
    fn test_monotonic_write_blocks_regression() {
        let mut mgr = ConsistencyManager::new(ConsistencyConfig {
            write_level: StreamConsistencyLevel::MonotonicWrite,
            ..Default::default()
        });
        let v5 = make_value(0u32, 5);
        mgr.after_write("sess", &v5);

        let v4 = make_value(0u32, 4);
        assert!(!mgr.can_write("sess", &v4));

        let v5b = make_value(0u32, 5);
        assert!(mgr.can_write("sess", &v5b));

        let v6 = make_value(0u32, 6);
        assert!(mgr.can_write("sess", &v6));
    }

    // ── ConsistencyManager (is_stale) ─────────────────────────────────────────

    #[test]
    fn test_is_stale_behind_current() {
        let mgr = ConsistencyManager::new(ConsistencyConfig::default());
        let vv = make_value(0u32, 4);
        assert!(mgr.is_stale(&vv, 10));
    }

    #[test]
    fn test_is_not_stale_at_current() {
        let mgr = ConsistencyManager::new(ConsistencyConfig::default());
        let vv = make_value(0u32, 10);
        assert!(!mgr.is_stale(&vv, 10));
    }

    #[test]
    fn test_is_not_stale_ahead_current() {
        let mgr = ConsistencyManager::new(ConsistencyConfig::default());
        let vv = make_value(0u32, 12);
        assert!(!mgr.is_stale(&vv, 10));
    }

    // ── ConsistencyManager (session counts) ───────────────────────────────────

    #[test]
    fn test_session_tracking_counts() {
        let mut mgr = ConsistencyManager::new(ConsistencyConfig {
            read_level: StreamConsistencyLevel::MonotonicRead,
            write_level: StreamConsistencyLevel::MonotonicWrite,
            ..Default::default()
        });
        assert_eq!(mgr.monotonic_read_session_count(), 0);
        assert_eq!(mgr.monotonic_write_session_count(), 0);

        mgr.after_read("r-sess", &make_value(0u32, 1));
        mgr.after_write("w-sess", &make_value(0u32, 1));

        assert_eq!(mgr.monotonic_read_session_count(), 1);
        assert_eq!(mgr.monotonic_write_session_count(), 1);
    }

    // ── EventualConsistencyBuffer ──────────────────────────────────────────────

    #[test]
    fn test_buffer_initial_empty() {
        let buf = EventualConsistencyBuffer::new(500);
        assert_eq!(buf.pending_count(), 0);
    }

    #[test]
    fn test_buffer_and_count() {
        let mut buf = EventualConsistencyBuffer::new(500);
        buf.buffer("key1", b"val1".to_vec());
        buf.buffer("key2", b"val2".to_vec());
        assert_eq!(buf.pending_count(), 2);
    }

    #[test]
    fn test_buffer_drain() {
        let mut buf = EventualConsistencyBuffer::new(500);
        buf.buffer("k", b"v".to_vec());
        let drained = buf.drain();
        assert_eq!(drained.len(), 1);
        assert_eq!(drained[0].0, "k");
        assert_eq!(drained[0].1, b"v");
        assert_eq!(buf.pending_count(), 0);
    }

    #[test]
    fn test_should_sync_past_deadline() {
        let buf = EventualConsistencyBuffer::new(100);
        let far_future = current_ms() + 10_000; // 10 seconds in the future
        assert!(buf.should_sync(far_future));
    }

    #[test]
    fn test_should_not_sync_before_deadline() {
        let buf = EventualConsistencyBuffer::new(60_000); // 60 second lag
        let now = current_ms();
        assert!(!buf.should_sync(now));
    }

    #[test]
    fn test_drain_resets_timer() {
        let mut buf = EventualConsistencyBuffer::new(100);
        buf.buffer("x", b"1".to_vec());
        let _ = buf.drain();
        // immediately after drain the timer is reset; now_ms == last_sync_ms ≈ now
        let now = current_ms();
        assert!(!buf.should_sync(now));
    }
}