reddb-io-server 1.33.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
use super::commit::StoreWalAction;
use super::*;
use std::cell::Cell;
use std::collections::{HashSet, VecDeque};
use std::sync::atomic::{AtomicBool, AtomicUsize};

thread_local! {
    static BOOT_SYSTEM_INFO_UPDATE: Cell<bool> = const { Cell::new(false) };
}

const CACHE_COLLECTION: &str = "red.cache.blob_l2";
const EPOCH_KEY: &str = "__result_data_epoch_v1";
const HISTORY_LIMIT: usize = 256;

#[derive(Default)]
pub(super) struct ResultCacheEpochState {
    epoch: AtomicU64,
    id: AtomicU64,
    pending: AtomicUsize,
    poisoned: AtomicBool,
    format_fenced: AtomicBool,
    snapshot_format_current: AtomicBool,
    // None means a publication whose effects cannot be limited to collections.
    history: parking_lot::Mutex<VecDeque<(u64, Option<HashSet<String>>)>>,
    #[cfg(test)]
    on_pending: parking_lot::Mutex<Option<Arc<dyn Fn() + Send + Sync>>>,
}

pub(super) struct ResultCacheEpochGuard {
    state: Arc<ResultCacheEpochState>,
    committed: bool,
}

impl ResultCacheEpochGuard {
    pub(super) fn commit(mut self) {
        self.committed = true;
    }
}

impl Drop for ResultCacheEpochGuard {
    fn drop(&mut self) {
        if !self.committed {
            self.state.poisoned.store(true, Ordering::SeqCst);
        }
        self.state.pending.fetch_sub(1, Ordering::SeqCst);
    }
}

impl UnifiedStore {
    pub(crate) fn result_cache_format_fenced(&self) -> bool {
        self.result_cache_epoch
            .format_fenced
            .load(Ordering::Acquire)
    }

    pub(super) fn note_result_cache_snapshot_version(&self, version: u32) {
        self.result_cache_epoch
            .snapshot_format_current
            .store(version >= reddb_file::STORE_VERSION_V13, Ordering::Release);
    }

    pub(crate) fn result_cache_snapshot_format_current(&self) -> bool {
        self.result_cache_epoch
            .snapshot_format_current
            .load(Ordering::Acquire)
    }

    /// Publish the epoch marker once after a checkpoint so both embedded
    /// superblocks name the fenced snapshot before cached results are written.
    pub(crate) fn seal_result_cache_snapshot(&self) -> Result<(), StoreError> {
        let _publication = self.vault_publication_lock.lock();
        let id = EntityId::new(self.result_cache_epoch.id.load(Ordering::SeqCst));
        let mut entity = self
            .try_get(CACHE_COLLECTION, id, usize::MAX)?
            .ok_or_else(|| {
                StoreError::Io(std::io::Error::other("result-cache epoch marker missing"))
            })?;
        entity.data = Self::result_cache_epoch_data(
            self.result_cache_epoch.epoch.load(Ordering::SeqCst),
            true,
        );
        let action =
            StoreWalAction::upsert_entity(CACHE_COLLECTION, &entity, None, self.format_version());
        self.apply_replayed_action(&action, true)?;
        self.finish_paged_write([action])?;
        self.result_cache_epoch
            .format_fenced
            .store(true, Ordering::Release);
        Ok(())
    }
    #[cfg(test)]
    pub(crate) fn set_result_cache_pending_hook(&self, hook: Option<Arc<dyn Fn() + Send + Sync>>) {
        *self.result_cache_epoch.on_pending.lock() = hook;
    }

    // Boot-only process telemetry changes on every restart but cannot affect
    // persisted user-table results. System-collection results are not saved.
    pub(crate) fn with_boot_system_info_update<T>(&self, f: impl FnOnce() -> T) -> T {
        struct Reset(bool);
        impl Drop for Reset {
            fn drop(&mut self) {
                BOOT_SYSTEM_INFO_UPDATE.with(|flag| flag.set(self.0));
            }
        }
        let previous = BOOT_SYSTEM_INFO_UPDATE.with(|flag| flag.replace(true));
        let _reset = Reset(previous);
        f()
    }

    fn read_result_cache_epoch_marker(&self) -> Result<Option<(EntityId, u64, bool)>, StoreError> {
        let Some(manager) = self.get_collection(CACHE_COLLECTION) else {
            return Ok(None);
        };
        let row = manager.try_project_key_entity(EPOCH_KEY, None, true, usize::MAX, |entity| {
            (
                entity.id,
                entity
                    .data
                    .as_row()
                    .and_then(|row| row.get_field("value"))
                    .cloned(),
                matches!(
                    entity
                        .data
                        .as_row()
                        .and_then(|row| row.get_field("format_fenced")),
                    Some(Value::Boolean(true))
                ),
            )
        })?;
        match row {
            None => Ok(None),
            Some((id, Some(Value::UnsignedInteger(epoch)), fenced)) if epoch > 0 => {
                Ok(Some((id, epoch, fenced)))
            }
            Some(_) => Err(StoreError::Io(std::io::Error::other(
                "invalid result-cache epoch marker",
            ))),
        }
    }

    /// Load only after WAL replay, before any caller can mutate the store.
    pub(crate) fn load_result_cache_epoch(&self) -> Result<(), StoreError> {
        let _publication = self.vault_publication_lock.lock();
        self.result_cache_epoch.history.lock().clear();
        if let Some((id, epoch, marker_fenced)) = self.read_result_cache_epoch_marker()? {
            self.result_cache_epoch.id.store(id.raw(), Ordering::SeqCst);
            self.result_cache_epoch.epoch.store(epoch, Ordering::SeqCst);
            let format_fenced = if let Some(pager) = self.pager() {
                pager
                    .header()
                    .map_err(|error| StoreError::Io(std::io::Error::other(error.to_string())))?
                    .version
                    >= reddb_file::file_format::PAGE_RESULT_CACHE_FILE_VERSION
            } else {
                self.result_cache_snapshot_format_current()
            };
            self.result_cache_epoch
                .format_fenced
                .store(marker_fenced && format_fenced, Ordering::Release);
        } else {
            self.result_cache_epoch.epoch.store(0, Ordering::SeqCst);
            self.result_cache_epoch.id.store(0, Ordering::SeqCst);
            self.result_cache_epoch
                .format_fenced
                .store(false, Ordering::Release);
        }
        Ok(())
    }

    /// None fences caching while a publication is pending or its outcome is uncertain.
    pub(crate) fn result_cache_epoch(&self) -> Option<u64> {
        let state = &self.result_cache_epoch;
        if state.poisoned.load(Ordering::SeqCst) || state.pending.load(Ordering::SeqCst) != 0 {
            return None;
        }
        let epoch = state.epoch.load(Ordering::SeqCst);
        if state.poisoned.load(Ordering::SeqCst)
            || state.pending.load(Ordering::SeqCst) != 0
            || state.epoch.load(Ordering::SeqCst) != epoch
        {
            return None;
        }
        (epoch != 0).then_some(epoch)
    }

    /// A pending publication can be read across only after checking an
    /// individual result's dependencies with `result_cache_unchanged_since`.
    pub(crate) fn result_cache_read_epoch(&self) -> Option<u64> {
        let state = &self.result_cache_epoch;
        let _history = state.history.lock();
        if state.poisoned.load(Ordering::SeqCst) {
            return None;
        }
        let epoch = state.epoch.load(Ordering::SeqCst);
        (epoch != 0).then_some(epoch)
    }

    pub(crate) fn result_cache_unchanged_since(
        &self,
        epoch: u64,
        scopes: &HashSet<String>,
    ) -> bool {
        let state = &self.result_cache_epoch;
        let history = state.history.lock();
        if state.poisoned.load(Ordering::SeqCst) {
            return false;
        }
        let current = state.epoch.load(Ordering::SeqCst);
        if epoch == 0 || epoch > current {
            return false;
        }
        if epoch == current {
            return true;
        }
        if scopes.is_empty() {
            return false;
        }
        let mut next = epoch + 1;
        for (version, changed) in history.iter().filter(|(version, _)| *version > epoch) {
            if *version != next
                || changed.as_ref().is_none_or(|changed| {
                    changed.iter().any(|collection| {
                        scopes.iter().any(|scope| {
                            scope.eq_ignore_ascii_case(collection)
                                || scope.rsplit('.').next().is_some_and(|name| {
                                    collection
                                        .rsplit('.')
                                        .next()
                                        .is_some_and(|other| name.eq_ignore_ascii_case(other))
                                })
                        })
                    })
                })
            {
                return false;
            }
            next = version.saturating_add(1);
        }
        next == current.saturating_add(1)
    }

    /// Activate epoch tracking before a result can be persisted. Cache rows
    /// written by older versions have another payload format and are ignored.
    pub(crate) fn ensure_result_cache_epoch(&self) -> Result<u64, StoreError> {
        if let Some(epoch) = self.result_cache_epoch() {
            return Ok(epoch);
        }
        if self.result_cache_epoch.epoch.load(Ordering::SeqCst) != 0
            || self.result_cache_epoch.poisoned.load(Ordering::SeqCst)
            || self.result_cache_epoch.pending.load(Ordering::SeqCst) != 0
        {
            return Err(StoreError::Io(std::io::Error::other(
                "result-cache epoch unavailable",
            )));
        }
        let _publication = self.vault_publication_lock.lock();
        if let Some(epoch) = self.result_cache_epoch() {
            return Ok(epoch);
        }
        if self.result_cache_epoch.epoch.load(Ordering::SeqCst) != 0
            || self.result_cache_epoch.poisoned.load(Ordering::SeqCst)
            || self.result_cache_epoch.pending.load(Ordering::SeqCst) != 0
        {
            return Err(StoreError::Io(std::io::Error::other(
                "result-cache epoch unavailable",
            )));
        }
        if self.is_read_only() {
            return Err(StoreError::Io(std::io::Error::other(
                "read-only result-cache epoch",
            )));
        }
        if let Some((id, epoch, _)) = self.read_result_cache_epoch_marker()? {
            self.result_cache_epoch.id.store(id.raw(), Ordering::SeqCst);
            self.result_cache_epoch.epoch.store(epoch, Ordering::SeqCst);
            return Ok(epoch);
        }
        let id = self.next_entity_id();
        let entity = Self::result_cache_epoch_entity(id, 1);
        self.publish_rows(super::row_publication::RowPublication {
            inserts: vec![(CACHE_COLLECTION.to_string(), entity, None)],
            deletes: Vec::new(),
        })?;
        self.sync_control_metadata()?;
        self.result_cache_epoch.id.store(id.raw(), Ordering::SeqCst);
        self.result_cache_epoch.epoch.store(1, Ordering::SeqCst);
        Ok(1)
    }

    fn result_cache_epoch_entity(id: EntityId, epoch: u64) -> UnifiedEntity {
        UnifiedEntity::new(
            id,
            EntityKind::TableRow {
                table: Arc::from(CACHE_COLLECTION),
                row_id: 0,
            },
            Self::result_cache_epoch_data(epoch, false),
        )
    }

    fn result_cache_epoch_data(epoch: u64, fenced: bool) -> EntityData {
        EntityData::Row(RowData::with_names(
            vec![
                Value::text(EPOCH_KEY),
                Value::UnsignedInteger(epoch),
                Value::Boolean(fenced),
            ],
            vec!["key".into(), "value".into(), "format_fenced".into()],
        ))
    }

    pub(super) fn note_in_memory_result_mutation(
        &self,
        collection: &str,
    ) -> Result<(), StoreError> {
        if self.result_cache_epoch.epoch.load(Ordering::SeqCst) == 0 {
            return Ok(());
        }
        let _publication = self.vault_publication_lock.lock();
        let mut actions = vec![StoreWalAction::DeleteEntityRecord {
            collection: collection.to_string(),
            entity_id: 0,
        }];
        if let Some(guard) = self.append_result_cache_epoch_action(&mut actions)? {
            guard.commit();
        }
        Ok(())
    }

    /// Called with the publication fence held. Its guard must be committed
    /// only after the data and marker WAL batch is known durable.
    pub(super) fn append_result_cache_epoch_action(
        &self,
        actions: &mut Vec<StoreWalAction>,
    ) -> Result<Option<ResultCacheEpochGuard>, StoreError> {
        let state = &self.result_cache_epoch;
        let epoch = state.epoch.load(Ordering::SeqCst);
        if BOOT_SYSTEM_INFO_UPDATE.with(Cell::get)
            || epoch == 0
            || !actions.iter().any(|action| match action {
                StoreWalAction::CreateCollection { name }
                | StoreWalAction::DropCollection { name } => name != CACHE_COLLECTION,
                StoreWalAction::UpsertEntityRecord { collection, .. }
                | StoreWalAction::DeleteEntityRecord { collection, .. }
                | StoreWalAction::BulkUpsertEntityRecords { collection, .. }
                | StoreWalAction::RefreshCollection { collection, .. } => {
                    collection != CACHE_COLLECTION
                }
                StoreWalAction::SetAuxMetadata { .. } => true,
            })
        {
            return Ok(None);
        }
        let mut history = state.history.lock();
        state.pending.fetch_add(1, Ordering::SeqCst);
        let guard = ResultCacheEpochGuard {
            state: Arc::clone(state),
            committed: false,
        };
        let next = epoch
            .checked_add(1)
            .ok_or_else(|| StoreError::Io(std::io::Error::other("result-cache epoch exhausted")))?;
        let id = EntityId::new(state.id.load(Ordering::SeqCst));
        let Some(mut entity) = self.try_get(CACHE_COLLECTION, id, usize::MAX)? else {
            return Err(StoreError::Io(std::io::Error::other(
                "result-cache epoch marker disappeared",
            )));
        };
        let fenced = matches!(
            entity
                .data
                .as_row()
                .and_then(|row| row.get_field("format_fenced")),
            Some(Value::Boolean(true))
        );
        entity.data = Self::result_cache_epoch_data(next, fenced);
        let action =
            StoreWalAction::upsert_entity(CACHE_COLLECTION, &entity, None, self.format_version());
        self.apply_replayed_action(&action, true)?;
        let mut changed = HashSet::new();
        let scoped = actions.iter().all(|action| {
            let collection = match action {
                StoreWalAction::UpsertEntityRecord { collection, .. }
                | StoreWalAction::DeleteEntityRecord { collection, .. }
                | StoreWalAction::BulkUpsertEntityRecords { collection, .. }
                | StoreWalAction::RefreshCollection { collection, .. } => collection,
                StoreWalAction::CreateCollection { .. }
                | StoreWalAction::DropCollection { .. }
                | StoreWalAction::SetAuxMetadata { .. } => return false,
            };
            if collection == CACHE_COLLECTION {
                return true;
            }
            let canonical =
                reddb_engine::storage::system_collection_names::canonical_or_self(collection);
            if canonical.starts_with("red.") || collection.len() > 256 || changed.len() >= 16 {
                return false;
            }
            changed.insert(collection.clone());
            true
        });
        history.push_back((next, scoped.then_some(changed)));
        while history.len() > HISTORY_LIMIT {
            history.pop_front();
        }
        state.epoch.store(next, Ordering::SeqCst);
        actions.push(action);
        drop(history);
        #[cfg(test)]
        let hook = state.on_pending.lock().clone();
        #[cfg(test)]
        if let Some(hook) = hook {
            hook();
        }
        Ok(Some(guard))
    }
}

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

    #[test]
    fn pending_publication_only_fences_results_with_matching_dependencies() {
        let store = UnifiedStore::new();
        assert_eq!(store.ensure_result_cache_epoch().expect("initial epoch"), 1);
        let _publication = store.vault_publication_lock.lock();
        let mut actions = vec![StoreWalAction::DeleteEntityRecord {
            collection: "noise".into(),
            entity_id: 7,
        }];
        let guard = store
            .append_result_cache_epoch_action(&mut actions)
            .unwrap()
            .unwrap();
        assert_eq!(store.result_cache_epoch(), None);
        assert_eq!(store.result_cache_read_epoch(), Some(2));
        assert!(store.result_cache_unchanged_since(1, &HashSet::from(["items".into()])));
        assert!(!store.result_cache_unchanged_since(1, &HashSet::from(["noise".into()])));
        guard.commit();
        let mut actions = vec![StoreWalAction::SetAuxMetadata { bytes: vec![] }];
        let guard = store
            .append_result_cache_epoch_action(&mut actions)
            .unwrap()
            .unwrap();
        assert!(!store.result_cache_unchanged_since(1, &HashSet::from(["items".into()])));
        drop(guard);
        assert_eq!(store.result_cache_read_epoch(), None);
        assert!(!store.result_cache_unchanged_since(3, &HashSet::from(["items".into()])));
    }

    #[test]
    fn missing_history_fails_closed_and_mixed_batches_fence_each_dependency() {
        let store = UnifiedStore::new();
        store.ensure_result_cache_epoch().unwrap();
        let _publication = store.vault_publication_lock.lock();
        for _ in 0..=HISTORY_LIMIT {
            let mut actions = vec![StoreWalAction::DeleteEntityRecord {
                collection: "noise".into(),
                entity_id: 7,
            }];
            store
                .append_result_cache_epoch_action(&mut actions)
                .unwrap()
                .unwrap()
                .commit();
        }
        assert!(!store.result_cache_unchanged_since(1, &HashSet::from(["items".into()])));
        let epoch = store.result_cache_epoch().unwrap();
        let mut actions = vec![
            StoreWalAction::DeleteEntityRecord {
                collection: "noise".into(),
                entity_id: 1,
            },
            StoreWalAction::DeleteEntityRecord {
                collection: "items".into(),
                entity_id: 2,
            },
        ];
        let guard = store
            .append_result_cache_epoch_action(&mut actions)
            .unwrap()
            .unwrap();
        assert!(!store.result_cache_unchanged_since(epoch, &HashSet::from(["items".into()])));
        assert!(store.result_cache_unchanged_since(epoch, &HashSet::from(["other".into()])));
        guard.commit();
    }

    #[test]
    fn pending_or_failed_epoch_publication_disables_result_cache() {
        let store = UnifiedStore::new();
        assert_eq!(store.ensure_result_cache_epoch().expect("initial epoch"), 1);
        let _publication = store.vault_publication_lock.lock();
        let mut actions = vec![StoreWalAction::CreateCollection {
            name: "items".into(),
        }];
        let guard = store
            .append_result_cache_epoch_action(&mut actions)
            .expect("prepare")
            .expect("guard");
        assert_eq!(
            store.result_cache_epoch(),
            None,
            "pending data must not use the cache"
        );
        guard.commit();
        assert_eq!(store.result_cache_epoch(), Some(2));
        let mut actions = vec![StoreWalAction::CreateCollection {
            name: "items".into(),
        }];
        let guard = store
            .append_result_cache_epoch_action(&mut actions)
            .expect("prepare")
            .expect("guard");
        drop(guard);
        assert_eq!(
            store.result_cache_epoch(),
            None,
            "an uncertain commit must fence the cache"
        );
        assert!(store.ensure_result_cache_epoch().is_err());
    }

    #[test]
    fn boot_system_telemetry_does_not_advance_data_epoch() {
        let store = UnifiedStore::new();
        assert_eq!(store.ensure_result_cache_epoch().expect("initial epoch"), 1);
        store.with_boot_system_info_update(|| {
            store
                .try_set_config_tree("red.system", &crate::json!({"pid": 7}))
                .expect("boot telemetry");
        });
        assert_eq!(store.result_cache_epoch(), Some(1));
        store
            .try_set_config_tree("red.config", &crate::json!({"key": "new"}))
            .expect("user config");
        assert!(store.result_cache_epoch().is_some_and(|epoch| epoch > 1));
    }

    #[test]
    fn deferred_transaction_persists_epoch_in_the_same_wal_batch() {
        let directory = tempfile::tempdir().expect("temporary directory");
        let path = directory.path().join("epoch-transaction.rdb");
        {
            let store = UnifiedStore::open(&path).expect("store");
            assert_eq!(store.ensure_result_cache_epoch().expect("initial epoch"), 1);
            UnifiedStore::begin_deferred_store_wal_capture();
            store
                .insert_auto(
                    "items",
                    UnifiedEntity::table_row(EntityId::new(0), "items", 0, vec![Value::Integer(7)]),
                )
                .expect("stage row");
            let actions = UnifiedStore::take_deferred_store_wal_capture();
            assert_eq!(
                store.result_cache_epoch(),
                Some(1),
                "uncommitted actions must not advance durable epoch"
            );
            store
                .append_deferred_store_wal_actions(actions)
                .expect("commit batch");
            assert_eq!(store.result_cache_epoch(), Some(2));
        }
        let store = UnifiedStore::open(&path).expect("reopen");
        assert_eq!(store.result_cache_epoch(), Some(2));
        assert_eq!(store.get_collection("items").expect("items").count(), 1);
    }

    #[test]
    fn internal_blob_publication_does_not_advance_data_epoch() {
        let store = UnifiedStore::new();
        assert_eq!(store.ensure_result_cache_epoch().expect("initial epoch"), 1);
        let _publication = store.vault_publication_lock.lock();
        let mut actions = vec![StoreWalAction::CreateCollection {
            name: CACHE_COLLECTION.into(),
        }];
        assert!(store
            .append_result_cache_epoch_action(&mut actions)
            .expect("prepare")
            .is_none());
        assert_eq!(store.result_cache_epoch(), Some(1));
    }
}