reddb-io-server 1.28.1

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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! Paced table-history maintenance for standalone embedded runtimes.
use std::time::{Duration, Instant};

use super::RedDBRuntime;
use crate::storage::profile::DeployProfile;
use crate::storage::unified::store::MvccReclaimCursor;

#[derive(Default)]
pub(super) struct MvccMaintenance {
    current: Option<MvccReclaimCursor>,
    after_collection: Option<String>,
    retry_after: Option<Instant>,
    last_warning: Option<Instant>,
}

impl RedDBRuntime {
    /// Foreground pressure and the periodic worker share one paced cursor.
    /// Never wait behind another collector while a mutation holds row locks.
    pub(super) fn try_reclaim_history_tick(&self) -> bool {
        let Some(mut state) = self.inner.mvcc_maintenance.try_lock() else {
            return false;
        };
        self.reclaim_history_tick(&mut state)
    }

    fn reclaim_history_tick(&self, state: &mut MvccMaintenance) -> bool {
        let options = self.inner.db.options();
        // Replicated profiles need a distributed reader/replica low-water
        // mark before local physical history can be reclaimed automatically.
        if options.read_only
            || options.storage_profile.deploy_profile != DeployProfile::Embedded
            || options.replication.role != crate::replication::ReplicationRole::Standalone
            || self.inner.lifecycle.phase() != super::lifecycle::Phase::Ready
            || !self.config_bool("runtime.mvcc.autovacuum.enabled", true)
        {
            state.current = None;
            return false;
        }
        // A collector must not append physical GC to an unrelated, possibly
        // aborted DML event capture. The periodic worker can resume this cursor.
        if crate::storage::UnifiedStore::deferred_store_wal_capture_active() {
            return false;
        }
        if state.retry_after.is_some_and(|at| Instant::now() < at) {
            return false;
        }
        let store = self.inner.db.store();
        if state
            .current
            .as_ref()
            .is_some_and(|cursor| store.get_collection(&cursor.collection).is_none())
        {
            state.current = None;
        }
        if state.current.is_none() {
            let mut collections = store.list_collections();
            collections.sort_unstable();
            if collections.is_empty() {
                return false;
            }
            let index = state.after_collection.as_ref().map_or(0, |after| {
                collections.partition_point(|name| name <= after) % collections.len()
            });
            state.current = store.mvcc_reclaim_cursor(collections[index].clone());
        }
        let Some(cursor) = state.current.as_mut() else {
            return false;
        };
        // Native GC retains one leaf and decodes one current row at a time.
        // Charge encoded/decoded workspace before taking the publication fence.
        let _workspace = if store.config().resident_memory_bytes_max.is_some() {
            let Some(reservation) =
                self.try_reserve_query_buffer(store.mvcc_reclaim_read_limit().saturating_mul(2))
            else {
                return false;
            };
            Some(reservation)
        } else {
            None
        };
        let scan_budget = self
            .config_u64("runtime.mvcc.autovacuum.scan_rows_per_tick", 4096)
            .clamp(1, 4096) as usize;
        let reclaim_budget = self
            .config_u64("runtime.mvcc.autovacuum.reclaim_rows_per_tick", 256)
            .clamp(1, 256) as usize;
        let result = store.reclaim_mvcc_tick(
            cursor,
            &self.snapshot_manager(),
            self.mvcc_vacuum_cutoff_xid(),
            scan_budget,
            reclaim_budget,
        );
        match result {
            Ok(reclaimed) => {
                state.retry_after = None;
                if reclaimed > 0 {
                    tracing::debug!(collection = %cursor.collection, reclaimed_versions = reclaimed,
                        "paced MVCC history reclamation");
                }
                if cursor.finished() {
                    state.after_collection = Some(cursor.collection.clone());
                    state.current = None;
                }
                true
            }
            Err(error) => {
                state.retry_after = Some(Instant::now() + Duration::from_secs(5));
                if state
                    .last_warning
                    .is_none_or(|at| at.elapsed() >= Duration::from_secs(60))
                {
                    tracing::warn!(%error, "MVCC history reclamation deferred");
                    state.last_warning = Some(Instant::now());
                }
                // An unsuccessful WAL-backed batch is not recorded as progress.
                state.current = None;
                false
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{RedDBOptions, RedDBRuntime};

    #[cfg(unix)]
    #[test]
    fn constant_live_data_churn_reuses_pages_after_cold_gc() {
        use reddb_types::Value;
        use std::os::unix::fs::MetadataExt;
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("churn.rdb");
        let mut runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(&path)
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .unwrap();
        // Scheduling is part of this fixture: no sleeps or races against the
        // periodic worker. Every cycle drives the same bounded production GC.
        drop(runtime.background_tasks.take());
        runtime
            .execute_query("CREATE TABLE churn_rows (id INTEGER PRIMARY KEY, payload TEXT)")
            .unwrap();
        let payload = |generation: usize, id: usize| {
            let mut state = (generation * 32 + id) as u64 + 1;
            // Exercise small inline, near-inline and overflow values in every
            // fixed cycle without making CI depend on bulk-throughput speed.
            let bytes = [180, 4 * 1024, 64 * 1024][id % 3];
            (0..bytes)
                .map(|_| {
                    state ^= state << 13;
                    state ^= state >> 7;
                    state ^= state << 17;
                    (b'a' + (state % 26) as u8) as char
                })
                .collect::<String>()
        };
        let allocated = || {
            [&path, &reddb_file::layout::unified_wal_path(&path)]
                .iter()
                .map(|p| std::fs::metadata(p).map_or(0, |m| m.blocks() * 512))
                .sum::<u64>()
        };
        let mut warmup_allocated = 0;
        let mut peak_after_warmup = 0;
        for generation in 0..120 {
            for start in (0..32).step_by(8) {
                runtime
                    .execute_query(&format!(
                        "DELETE FROM churn_rows WHERE id >= {start} AND id < {}",
                        start + 8,
                    ))
                    .unwrap();
            }
            for start in (0..32).step_by(8) {
                let values = (start..start + 8)
                    .map(|id| format!("({id}, '{}')", payload(generation, id)))
                    .collect::<Vec<_>>()
                    .join(",");
                runtime
                    .execute_query(&format!(
                        "INSERT INTO churn_rows (id, payload) VALUES {values}"
                    ))
                    .unwrap();
            }
            // Force the regression's hard case: obsolete versions are cold,
            // and the cleanup must find them without a resident-only scan.
            runtime.checkpoint().unwrap();
            let store = runtime.db().store();
            let mut cursor = store.mvcc_reclaim_cursor("churn_rows".into()).unwrap();
            let mut reclaimed = 0;
            for _ in 0..128 {
                let count = store
                    .reclaim_mvcc_tick(
                        &mut cursor,
                        &runtime.snapshot_manager(),
                        runtime.mvcc_vacuum_cutoff_xid(),
                        64,
                        8,
                    )
                    .unwrap();
                assert!(count <= 8, "bounded deletion work");
                reclaimed += count;
                if cursor.finished() {
                    break;
                }
            }
            assert!(
                cursor.finished(),
                "bounded sweep must converge at cycle {generation}"
            );
            assert_eq!(
                reclaimed,
                if generation == 0 { 0 } else { 32 },
                "cycle {generation}"
            );
            drop(cursor);
            runtime.checkpoint().unwrap();
            let result = runtime
                .execute_query("SELECT COUNT(*) AS n FROM churn_rows")
                .unwrap();
            assert_eq!(
                result.result.records[0].get("n").unwrap().display_string(),
                "32"
            );
            if generation == 19 {
                warmup_allocated = allocated();
            }
            if generation >= 20 {
                peak_after_warmup = peak_after_warmup.max(allocated());
            }
            if generation % 20 == 19 {
                eprintln!(
                    "churn_cycles={} allocated_bytes={} reclaimed_versions={reclaimed}",
                    generation + 1,
                    allocated()
                );
            }
        }
        let growth = peak_after_warmup.saturating_sub(warmup_allocated);
        eprintln!("churn_cycles=120 warmup_allocated={warmup_allocated} peak_after_warmup={peak_after_warmup} growth_bytes={growth}");
        assert!(
            growth <= 64 * 1024 * 1024,
            "storage grew {growth} bytes with constant live data"
        );
        drop(runtime);
        let runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(&path)
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .unwrap();
        for id in 0..32 {
            let result = runtime
                .execute_query(&format!("SELECT payload FROM churn_rows WHERE id = {id}"))
                .unwrap();
            assert_eq!(result.result.records.len(), 1);
            assert_eq!(
                result.result.records[0].get("payload"),
                Some(&Value::text(payload(119, id).as_str()))
            );
        }
    }

    #[test]
    fn cold_history_reclamation_waits_for_a_retained_reader() {
        let directory = tempfile::tempdir().unwrap();
        let mut runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(directory.path().join("reader.rdb"))
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .unwrap();
        drop(runtime.background_tasks.take());
        runtime
            .execute_query("CREATE TABLE cold_history (id INTEGER)")
            .unwrap();
        runtime
            .execute_query("INSERT INTO cold_history (id) VALUES (1)")
            .unwrap();
        let (_, retention) = runtime.snapshot_manager().fresh_retained_snapshot();
        runtime
            .execute_query("DELETE FROM cold_history WHERE id = 1")
            .unwrap();
        runtime.checkpoint().unwrap();
        let store = runtime.db().store();
        let manager = store.get_collection("cold_history").unwrap();
        assert!(manager.has_backing());
        let sweep = || {
            let mut cursor = store.mvcc_reclaim_cursor("cold_history".into()).unwrap();
            let mut reclaimed = 0;
            for _ in 0..8 {
                reclaimed += store
                    .reclaim_mvcc_tick(
                        &mut cursor,
                        &runtime.snapshot_manager(),
                        runtime.mvcc_vacuum_cutoff_xid(),
                        1,
                        1,
                    )
                    .unwrap();
                if cursor.finished() {
                    break;
                }
            }
            assert!(cursor.finished());
            reclaimed
        };
        assert_eq!(sweep(), 0);
        assert_eq!(
            manager.count(),
            1,
            "old reader still needs the cold preimage"
        );
        drop(retention);
        assert_eq!(sweep(), 1);
        assert_eq!(manager.count(), 0);
    }

    #[test]
    fn cold_gc_skips_live_payloads_using_the_visibility_projection() {
        use crate::storage::{EntityId, StoreError};
        use reddb_engine::storage::collection_read::{
            CollectionCursor, CollectionRead, EncodedRecord,
        };
        use std::sync::Arc;
        struct RefusePayload;
        impl CollectionRead for RefusePayload {
            fn get_record(
                &self,
                _: EntityId,
                _: usize,
            ) -> Result<Option<EncodedRecord>, StoreError> {
                Err(StoreError::Serialization(
                    "GC read an unneeded live payload".into(),
                ))
            }
            fn cursor(&self) -> Result<Box<dyn CollectionCursor + '_>, StoreError> {
                Err(StoreError::Serialization("GC scanned live payloads".into()))
            }
            fn owned_cursor(&self) -> Result<Box<dyn CollectionCursor>, StoreError> {
                Err(StoreError::Serialization(
                    "GC retained live payloads".into(),
                ))
            }
        }
        let directory = tempfile::tempdir().unwrap();
        let mut runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(directory.path().join("inventory.rdb"))
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .unwrap();
        drop(runtime.background_tasks.take());
        runtime
            .execute_query("CREATE TABLE live_rows (id INTEGER, payload TEXT)")
            .unwrap();
        runtime
            .execute_query("INSERT INTO live_rows (id, payload) VALUES (1, 'retained')")
            .unwrap();
        runtime.checkpoint().unwrap();
        let store = runtime.db().store();
        assert_eq!(
            store.try_count_from_index("live_rows", None).unwrap(),
            Some(1)
        );
        let manager = store.get_collection("live_rows").unwrap();
        let root = manager.backing_root_page().unwrap();
        let count = manager.count() as u64;
        let (source, _) = manager.current_backing_overlay().unwrap();
        manager.install_backing(
            Arc::new(RefusePayload),
            root,
            store.format_version(),
            1024,
            count,
        );
        let mut cursor = store.mvcc_reclaim_cursor("live_rows".into()).unwrap();
        for _ in 0..8 {
            assert_eq!(
                store
                    .reclaim_mvcc_tick(
                        &mut cursor,
                        &runtime.snapshot_manager(),
                        runtime.mvcc_vacuum_cutoff_xid(),
                        1,
                        1
                    )
                    .unwrap(),
                0
            );
            if cursor.finished() {
                break;
            }
        }
        assert!(cursor.finished());
        assert_eq!(manager.count(), 1);
        manager.install_backing(source, root, store.format_version(), 1024, count);
    }

    #[test]
    fn paused_cold_gc_does_not_force_a_checkpoint_to_copy_live_history() {
        use crate::storage::{EntityId, StoreError};
        use reddb_engine::storage::collection_read::{
            CollectionCursor, CollectionRead, EncodedRecord,
        };
        use std::sync::{
            atomic::{AtomicUsize, Ordering},
            Arc,
        };
        struct CountScans {
            inner: Arc<dyn CollectionRead + Send + Sync>,
            scans: Arc<AtomicUsize>,
        }
        impl CollectionRead for CountScans {
            fn get_record(
                &self,
                id: EntityId,
                bytes: usize,
            ) -> Result<Option<EncodedRecord>, StoreError> {
                self.inner.get_record(id, bytes)
            }
            fn cursor(&self) -> Result<Box<dyn CollectionCursor + '_>, StoreError> {
                self.scans.fetch_add(1, Ordering::Relaxed);
                self.inner.cursor()
            }
            fn owned_cursor(&self) -> Result<Box<dyn CollectionCursor>, StoreError> {
                self.scans.fetch_add(1, Ordering::Relaxed);
                self.inner.owned_cursor()
            }
        }
        let directory = tempfile::tempdir().unwrap();
        let mut runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(directory.path().join("pins.rdb"))
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .unwrap();
        drop(runtime.background_tasks.take());
        runtime
            .execute_query("CREATE TABLE append_rows (id INTEGER, payload TEXT)")
            .unwrap();
        runtime
            .execute_query(
                "INSERT INTO append_rows (id, payload) VALUES (1, 'old one'), (2, 'old two')",
            )
            .unwrap();
        runtime.checkpoint().unwrap();
        let store = runtime.db().store();
        let manager = store.get_collection("append_rows").unwrap();
        let root = manager.backing_root_page().unwrap();
        let count = manager.count() as u64;
        let (source, _) = manager.current_backing_overlay().unwrap();
        let scans = Arc::new(AtomicUsize::new(0));
        manager.install_backing(
            Arc::new(CountScans {
                inner: source,
                scans: Arc::clone(&scans),
            }),
            root,
            store.format_version(),
            1024 * 1024,
            count,
        );
        let mut cursor = store.mvcc_reclaim_cursor("append_rows".into()).unwrap();
        assert_eq!(
            store
                .reclaim_mvcc_tick(
                    &mut cursor,
                    &runtime.snapshot_manager(),
                    runtime.mvcc_vacuum_cutoff_xid(),
                    1,
                    1
                )
                .unwrap(),
            0
        );
        assert!(
            !cursor.finished(),
            "maintenance is deliberately paused mid-inventory"
        );
        runtime
            .execute_query("INSERT INTO append_rows (id, payload) VALUES (3, 'new')")
            .unwrap();
        runtime.checkpoint().unwrap();
        assert_eq!(
            scans.load(Ordering::Relaxed),
            0,
            "a paused GC tick must not force live history through full replacement"
        );
        for _ in 0..8 {
            assert_eq!(
                store
                    .reclaim_mvcc_tick(
                        &mut cursor,
                        &runtime.snapshot_manager(),
                        runtime.mvcc_vacuum_cutoff_xid(),
                        1,
                        1
                    )
                    .unwrap(),
                0
            );
            if cursor.finished() {
                break;
            }
        }
        assert!(
            cursor.finished(),
            "resume from the current generation after checkpoint"
        );
        assert_eq!(manager.count(), 3);
        drop(cursor);
    }

    fn fixture() -> RedDBRuntime {
        let mut runtime = RedDBRuntime::with_options(
            RedDBOptions::in_memory().with_memory_budget(2 * 1024 * 1024),
        )
        .expect("bounded runtime");
        drop(runtime.background_tasks.take());
        runtime
            .execute_query("CREATE TABLE pressure_history (id INTEGER)")
            .expect("table");
        runtime
            .vcs_set_versioned("pressure_history", false)
            .expect("no VCS history");
        runtime
            .execute_query("INSERT INTO pressure_history (id) VALUES (1)")
            .expect("seed row");
        runtime
    }

    fn physical_count(runtime: &RedDBRuntime) -> usize {
        runtime
            .db()
            .store()
            .get_collection("pressure_history")
            .expect("manager")
            .count()
    }

    fn collect(runtime: &RedDBRuntime) {
        for _ in 0..64 {
            runtime.try_reclaim_history_tick();
        }
    }

    #[test]
    fn foreground_history_reclamation_waits_for_a_retained_reader() {
        let runtime = fixture();
        let (_snapshot, retention) = runtime.snapshot_manager().fresh_retained_snapshot();
        runtime
            .execute_query("DELETE FROM pressure_history WHERE id = 1")
            .expect("delete");
        collect(&runtime);
        assert_eq!(
            physical_count(&runtime),
            1,
            "old reader still needs the preimage"
        );
        drop(retention);
        collect(&runtime);
        assert_eq!(
            physical_count(&runtime),
            0,
            "released reader permits reclamation"
        );
    }

    #[test]
    fn foreground_history_reclamation_does_not_join_a_deferred_wal_capture() {
        use crate::storage::UnifiedStore;
        struct CaptureGuard;
        impl Drop for CaptureGuard {
            fn drop(&mut self) {
                if UnifiedStore::deferred_store_wal_capture_active() {
                    drop(UnifiedStore::take_deferred_store_wal_capture());
                }
            }
        }
        let runtime = fixture();
        runtime
            .execute_query("DELETE FROM pressure_history WHERE id = 1")
            .expect("delete");
        UnifiedStore::begin_deferred_store_wal_capture();
        let guard = CaptureGuard;
        for _ in 0..64 {
            assert!(!runtime.try_reclaim_history_tick());
        }
        assert_eq!(physical_count(&runtime), 1);
        let actions = UnifiedStore::take_deferred_store_wal_capture();
        assert!(
            actions.is_empty(),
            "physical GC cannot join a caller's rollback journal"
        );
        drop(guard);
        collect(&runtime);
        assert_eq!(physical_count(&runtime), 0);
    }

    #[test]
    fn foreground_history_reclamation_respects_autovacuum_disable() {
        let runtime = fixture();
        runtime
            .execute_query("SET CONFIG runtime.mvcc.autovacuum.enabled = false")
            .expect("disable automatic GC");
        runtime
            .execute_query("DELETE FROM pressure_history WHERE id = 1")
            .expect("delete");
        for _ in 0..64 {
            assert!(!runtime.try_reclaim_history_tick());
        }
        assert_eq!(physical_count(&runtime), 1);
    }
}