infinite-db 0.4.0

A spatial-graph database using n-dimensional curves and hyperedges for engineering logic.
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
//! Per-subscriber derivation completeness watermarks (Phase 6: per-session vectors).
//!
//! Uses the outstanding-set / contiguous-prefix pattern from [`RevisionWatermark`]:
//! register on bus submit, retire on successful apply, watermark = predecessor of
//! lowest outstanding (or highest retired when empty). Failed derivations stay
//! outstanding and block advancement.

use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::sync::Arc;

use parking_lot::Mutex;

use crate::engine::session::VersionVector;
use crate::infinitedb_core::address::RevisionId;
use crate::infinitedb_core::hlc::{SessionId, GLOBAL_SESSION};

const MAX_FAILED_RECORDS: usize = 64;

/// Record of a derivation event that could not be applied.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FailedDerivation {
    pub revision: RevisionId,
    pub error: String,
}

#[derive(Debug)]
struct DerivationWatermarkState {
    outstanding: BTreeSet<RevisionId>,
    registered_high: RevisionId,
    failed: VecDeque<FailedDerivation>,
}

/// Highest revision through which derived rows are durably complete.
fn compute_complete(state: &DerivationWatermarkState) -> RevisionId {
    if state.outstanding.is_empty() {
        state.registered_high
    } else {
        state
            .outstanding
            .first()
            .copied()
            .unwrap_or(RevisionId::ZERO)
            .predecessor()
    }
}

/// Tracks in-flight derivation events for one session stream.
#[derive(Debug)]
pub struct DerivationWatermark {
    state: Mutex<DerivationWatermarkState>,
}

impl DerivationWatermark {
    pub fn new(initial: RevisionId) -> Self {
        Self {
            state: Mutex::new(DerivationWatermarkState {
                outstanding: BTreeSet::new(),
                registered_high: initial,
                failed: VecDeque::new(),
            }),
        }
    }

    /// Register an event revision when submitted to the bus.
    pub fn register(&self, revision: RevisionId) {
        let mut state = self.state.lock();
        state.outstanding.insert(revision);
        if revision > state.registered_high {
            state.registered_high = revision;
        }
    }

    /// Retire after derived rows are durably applied.
    pub fn retire(&self, revision: RevisionId) {
        self.state.lock().outstanding.remove(&revision);
    }

    /// Record a permanent failure; revision stays outstanding and blocks advancement.
    pub fn record_failure(&self, revision: RevisionId, error: impl Into<String>) {
        let mut state = self.state.lock();
        if state.failed.len() >= MAX_FAILED_RECORDS {
            state.failed.pop_front();
        }
        state.failed.push_back(FailedDerivation {
            revision,
            error: error.into(),
        });
    }

    pub fn get(&self) -> RevisionId {
        compute_complete(&self.state.lock())
    }

    pub fn failed_derivations(&self) -> Vec<FailedDerivation> {
        self.state.lock().failed.iter().cloned().collect()
    }

    pub fn outstanding_count(&self) -> usize {
        self.state.lock().outstanding.len()
    }
}

/// Per-session derivation watermarks for one subscriber (Phase 6).
#[derive(Debug)]
pub struct DerivationSessionWatermark {
    sessions: Mutex<BTreeMap<u32, Arc<DerivationWatermark>>>,
}

impl DerivationSessionWatermark {
    pub fn new(initial: RevisionId) -> Self {
        let mut map = BTreeMap::new();
        map.insert(
            GLOBAL_SESSION,
            Arc::new(DerivationWatermark::new(initial)),
        );
        Self {
            sessions: Mutex::new(map),
        }
    }

    fn ensure(&self, session: SessionId) -> Arc<DerivationWatermark> {
        if let Some(wm) = self.sessions.lock().get(&session.0) {
            return Arc::clone(wm);
        }
        let mut map = self.sessions.lock();
        map.entry(session.0)
            .or_insert_with(|| Arc::new(DerivationWatermark::new(RevisionId::ZERO)))
            .clone()
    }

    pub fn register(&self, session: SessionId, revision: RevisionId) {
        self.ensure(session).register(revision);
    }

    pub fn retire(&self, session: SessionId, revision: RevisionId) {
        self.ensure(session).retire(revision);
    }

    pub fn record_failure(
        &self,
        session: SessionId,
        revision: RevisionId,
        error: impl Into<String>,
    ) {
        self.ensure(session).record_failure(revision, error);
    }

    pub fn get(&self, session: SessionId) -> RevisionId {
        self.sessions
            .lock()
            .get(&session.0)
            .map(|wm| wm.get())
            .unwrap_or(RevisionId::ZERO)
    }

    pub fn capture_vector(&self) -> VersionVector {
        VersionVector(
            self.sessions
                .lock()
                .iter()
                .map(|(&sid, wm)| (SessionId(sid), wm.get()))
                .collect(),
        )
    }

    pub fn total_outstanding(&self) -> usize {
        self.sessions
            .lock()
            .values()
            .map(|wm| wm.outstanding_count())
            .sum()
    }

    pub fn outstanding_for_session(&self, session: SessionId) -> usize {
        self.sessions
            .lock()
            .get(&session.0)
            .map(|wm| wm.outstanding_count())
            .unwrap_or(0)
    }

    pub fn failed_derivations(&self) -> Vec<FailedDerivation> {
        let mut out = Vec::new();
        for wm in self.sessions.lock().values() {
            out.extend(wm.failed_derivations());
        }
        out
    }

    /// Lag estimate for backpressure within one session stream.
    pub fn lag_for_session(&self, session: SessionId, head: RevisionId) -> u64 {
        let complete = self.get(session);
        let outstanding = self.outstanding_for_session(session) as u64;
        if head <= complete {
            return outstanding;
        }
        if head.session() == session.0 {
            return outstanding.saturating_add(
                head.sequence()
                    .saturating_sub(complete.sequence()) as u64,
            );
        }
        outstanding
    }
}

/// Named subscriber with its own per-session derivation watermark.
#[derive(Debug)]
pub struct SubscriberWatermark {
    pub id: &'static str,
    pub watermark: DerivationSessionWatermark,
}

impl SubscriberWatermark {
    pub fn new(id: &'static str, initial: RevisionId) -> Self {
        Self {
            id,
            watermark: DerivationSessionWatermark::new(initial),
        }
    }
}

/// Registry of subscriber watermarks.
#[derive(Debug)]
pub struct WatermarkRegistry {
    subscribers: Mutex<Vec<SubscriberWatermark>>,
}

impl WatermarkRegistry {
    pub fn new() -> Self {
        Self {
            subscribers: Mutex::new(Vec::new()),
        }
    }

    pub fn register(&self, id: &'static str, initial: RevisionId) {
        self.subscribers
            .lock()
            .push(SubscriberWatermark::new(id, initial));
    }

    pub fn register_for_session(&self, session: SessionId, revision: RevisionId) {
        for sub in self.subscribers.lock().iter() {
            sub.watermark.register(session, revision);
        }
    }

    pub fn retire_for_session(&self, session: SessionId, revision: RevisionId) {
        for sub in self.subscribers.lock().iter() {
            sub.watermark.retire(session, revision);
        }
    }

    pub fn record_failure_for_session(
        &self,
        session: SessionId,
        revision: RevisionId,
        error: impl Into<String>,
    ) {
        let message = error.into();
        for sub in self.subscribers.lock().iter() {
            sub.watermark
                .record_failure(session, revision, &message);
        }
    }

    pub fn get_vector(&self, id: &str) -> Option<VersionVector> {
        self.subscribers
            .lock()
            .iter()
            .find(|s| s.id == id)
            .map(|s| s.watermark.capture_vector())
    }

    /// Minimum complete-through revision across subscribers for one session.
    pub fn min_complete_for_session(&self, session: SessionId) -> RevisionId {
        self.subscribers
            .lock()
            .iter()
            .map(|s| s.watermark.get(session))
            .min()
            .unwrap_or(RevisionId::ZERO)
    }

    pub fn lag_for_session(&self, session: SessionId, head: RevisionId) -> u64 {
        self.subscribers
            .lock()
            .iter()
            .map(|s| s.watermark.lag_for_session(session, head))
            .max()
            .unwrap_or(0)
    }

    pub fn failed_derivations(&self) -> Vec<FailedDerivation> {
        let mut out = Vec::new();
        for sub in self.subscribers.lock().iter() {
            out.extend(sub.watermark.failed_derivations());
        }
        out
    }

    pub fn total_outstanding(&self) -> usize {
        self.subscribers
            .lock()
            .iter()
            .map(|sub| sub.watermark.total_outstanding())
            .sum()
    }

    /// Per-session minimum complete revision across all subscribers.
    pub fn min_vector(&self) -> VersionVector {
        let mut merged = BTreeMap::new();
        for sub in self.subscribers.lock().iter() {
            for (session, rev) in sub.watermark.capture_vector().0 {
                merged
                    .entry(session)
                    .and_modify(|m: &mut RevisionId| *m = (*m).min(rev))
                    .or_insert(rev);
            }
        }
        VersionVector(merged)
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::infinitedb_core::hlc::HlcStamp;

    #[test]
    fn complete_waits_on_outstanding_gap() {
        let wm = DerivationWatermark::new(RevisionId::ZERO);
        wm.register(RevisionId::legacy(1));
        wm.register(RevisionId::legacy(2));
        wm.register(RevisionId::legacy(3));
        wm.retire(RevisionId::legacy(1));
        wm.retire(RevisionId::legacy(3));
        assert_eq!(wm.get(), RevisionId::legacy(1));
        wm.retire(RevisionId::legacy(2));
        assert_eq!(wm.get(), RevisionId::legacy(3));
    }

    #[test]
    fn failure_blocks_advancement() {
        let wm = DerivationWatermark::new(RevisionId::ZERO);
        wm.register(RevisionId::legacy(1));
        wm.register(RevisionId::legacy(2));
        wm.retire(RevisionId::legacy(1));
        wm.record_failure(RevisionId::legacy(2), "sink error");
        assert_eq!(wm.get(), RevisionId::legacy(1));
        assert_eq!(wm.failed_derivations().len(), 1);
    }

    #[test]
    fn out_of_order_retire_does_not_skip() {
        let wm = DerivationWatermark::new(RevisionId::ZERO);
        wm.register(RevisionId::legacy(1));
        wm.register(RevisionId::legacy(2));
        wm.retire(RevisionId::legacy(2));
        assert_eq!(wm.get(), RevisionId::ZERO);
        wm.retire(RevisionId::legacy(1));
        assert_eq!(wm.get(), RevisionId::legacy(2));
    }

    #[test]
    fn per_session_watermark_independent_advance() {
        let wm = DerivationSessionWatermark::new(RevisionId::ZERO);
        let s1 = SessionId(1);
        let s2 = SessionId(2);
        let r1a = RevisionId::from_stamp(HlcStamp {
            physical_ms: 1,
            logical: 0,
            session: s1.0,
            sequence: 1,
        });
        let r1b = RevisionId::from_stamp(HlcStamp {
            physical_ms: 1,
            logical: 0,
            session: s1.0,
            sequence: 2,
        });
        let r2a = RevisionId::from_stamp(HlcStamp {
            physical_ms: 1,
            logical: 0,
            session: s2.0,
            sequence: 1,
        });
        wm.register(s1, r1a);
        wm.register(s1, r1b);
        wm.register(s2, r2a);
        wm.retire(s2, r2a);
        assert_eq!(wm.get(s2), r2a);
        assert_eq!(wm.get(s1), r1a.predecessor());
        wm.retire(s1, r1a);
        assert_eq!(wm.get(s1), r1a);
        let vec = wm.capture_vector();
        assert_eq!(vec.get(s1), Some(r1a));
        assert_eq!(vec.get(s2), Some(r2a));
    }
}