reddb-io-server 1.34.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
//! Restore a checkpoint's first page images before loading its published trees.
use super::*;
use crate::storage::engine::{Page, PAGE_SIZE};
use reddb_file::page_ownership_journal::{encode, pending_epoch, PageOwnershipRecord};

fn invalid(message: &str) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidData, message)
}

fn page_image(page_id: u32, bytes: &[u8]) -> io::Result<Page> {
    let image: [u8; PAGE_SIZE] = bytes
        .try_into()
        .map_err(|_| invalid("checkpoint before-image has the wrong page size"))?;
    let page = Page::from_bytes(image);
    if page.page_id() != page_id {
        return Err(invalid(
            "checkpoint before-image has the wrong page identity",
        ));
    }
    page.verify_checksum()
        .map_err(|error| invalid(&error.to_string()))?;
    page.page_type()
        .map_err(|error| invalid(&error.to_string()))?;
    Ok(page)
}

impl StoreCommitCoordinator {
    pub(crate) fn restore_checkpoint_page_images(&self, store: &UnifiedStore) -> io::Result<()> {
        Self::restore_checkpoint_page_images_from_path(store, &self.wal_path)
    }

    pub(crate) fn restore_checkpoint_page_images_from_path(
        store: &UnifiedStore,
        wal_path: &Path,
    ) -> io::Result<()> {
        if !wal_path.exists() {
            return Ok(());
        }
        let Some(pager) = &store.pager else {
            return Ok(());
        };
        let header = pager
            .header()
            .map_err(|error| invalid(&error.to_string()))?;
        if header.version < reddb_file::file_format::PAGE_CHECKPOINT_UNDO_FILE_VERSION {
            return Ok(());
        }

        // Validate the entire journal and every image before changing a page.
        // Keep page identities only; a second streaming pass restores images
        // one at a time instead of retaining their payloads in recovery RAM.
        let mut journal = Vec::new();
        let mut active_epoch = None;
        let mut images = roaring::RoaringBitmap::new();
        for item in WalReader::open(wal_path)?.iter() {
            let (_, record) = match item {
                Ok(record) => record,
                Err(error)
                    if error.kind() == io::ErrorKind::UnexpectedEof && journal.is_empty() =>
                {
                    return Ok(());
                }
                Err(error) => {
                    return Err(invalid(&format!("incomplete checkpoint undo WAL: {error}")))
                }
            };
            match record {
                WalRecord::PageOwnership { record } => {
                    journal.extend_from_slice(&encode(record)?);
                    match record {
                        PageOwnershipRecord::Begin { epoch } => {
                            active_epoch = Some(epoch);
                            images.clear();
                        }
                        PageOwnershipRecord::Finish { .. } => {
                            active_epoch = None;
                            images.clear();
                        }
                        _ => {}
                    }
                }
                WalRecord::FullPageImage {
                    tx_id,
                    page_id,
                    ckpt_epoch,
                    data,
                } => {
                    if tx_id != 0
                        || active_epoch != Some(ckpt_epoch)
                        || page_id < reddb_engine::storage::pager_io::FIRST_ALLOCATABLE_PAGE
                        || page_id >= header.page_count
                        || !images.insert(page_id)
                    {
                        return Err(invalid("invalid checkpoint before-image ownership"));
                    }
                    page_image(page_id, &data)?;
                }
                _ => {}
            }
        }
        let Some(pending) = pending_epoch(&journal).map_err(|error| {
            invalid(&format!(
                "page ownership journal requires recovery: {error}"
            ))
        })?
        else {
            return Ok(());
        };
        if images.is_empty() {
            return Ok(());
        }
        if images
            .iter()
            .any(|page| pending.reserved_pages.contains(&page))
        {
            return Err(invalid("checkpoint before-image aliases a new reservation"));
        }
        let published_epoch = store
            .published_ownership_epoch()
            .map_err(|error| invalid(&error.to_string()))?;
        if published_epoch > pending.epoch {
            return Err(invalid("checkpoint undo precedes the published epoch"));
        }
        if published_epoch == pending.epoch {
            return Ok(());
        }

        #[cfg(test)]
        if let Ok(fault) = std::env::var("REDDB_TEST_PAGE_UNDO_IO") {
            use crate::storage::engine::pager;
            match fault.as_str() {
                "empty" => pager::test_fail_next_pager_write(None, 0),
                "partial" => pager::test_fail_next_pager_write(None, 17),
                "sync" => pager::test_fail_pager_sync_after(0),
                _ => panic!("unknown undo recovery I/O fault {fault}"),
            }
        }

        for item in WalReader::open(wal_path)?.iter() {
            let (_, record) = item?;
            if let WalRecord::FullPageImage {
                page_id,
                ckpt_epoch,
                data,
                ..
            } = record
            {
                if ckpt_epoch == pending.epoch {
                    let image = page_image(page_id, &data)?;
                    if pager.is_read_only() {
                        pager.restore_read_only_page(page_id, image)
                    } else {
                        pager.write_page(page_id, image)
                    }
                    .map_err(|error| invalid(&error.to_string()))?;
                    #[cfg(test)]
                    if !pager.is_read_only()
                        && std::env::var_os("REDDB_TEST_EXIT_DURING_PAGE_UNDO").is_some()
                    {
                        pager.sync().map_err(|error| invalid(&error.to_string()))?;
                        std::process::exit(194);
                    }
                }
            }
        }
        // Restoration must survive another crash before allocator recovery can
        // release newly allocated children/overflow pages from the failed epoch.
        if pager.is_read_only() {
            return Ok(());
        }
        pager.sync().map_err(|error| invalid(&error.to_string()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    const CHILD: &str = "REDDB_CHECKPOINT_PAGE_UNDO_CHILD";
    const TEST: &str = "storage::unified::store::commit::checkpoint_undo::tests::overflow_checkpoint_undo_survives_interrupted_recovery_and_publication";

    fn run_child(command: &mut std::process::Command) -> std::process::Output {
        let mut child = command
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
            .expect("recovery child");
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
        while child.try_wait().expect("child status").is_none() {
            if std::time::Instant::now() >= deadline {
                child.kill().expect("stop stuck recovery");
                let output = child.wait_with_output().expect("stuck recovery output");
                panic!(
                    "recovery made no progress: {}",
                    String::from_utf8_lossy(&output.stderr)
                );
            }
            std::thread::sleep(std::time::Duration::from_millis(10));
        }
        child.wait_with_output().expect("recovery child output")
    }

    fn row(id: u64, generation: u64) -> UnifiedEntity {
        let mut random = id + generation * 137;
        let payload: String = (0..24 * 1024)
            .map(|_| {
                random ^= random << 13;
                random ^= random >> 7;
                random ^= random << 17;
                char::from(b'!' + (random % 90) as u8)
            })
            .collect();
        UnifiedEntity::table_row(EntityId::new(id), "rows", id, vec![Value::text(payload)])
    }

    #[test]
    fn overflow_checkpoint_undo_survives_interrupted_recovery_and_publication() {
        let config = || UnifiedStoreConfig::default().with_page_cache_slots(4);
        if let Some(path) = std::env::var_os(CHILD) {
            if std::env::var_os("REDDB_TEST_PAGE_UNDO_IO").is_some()
                || std::env::var_os("REDDB_TEST_PAGE_REPLAY_IO").is_some()
            {
                let error = match UnifiedStore::open_with_config(path, config()) {
                    Ok(_) => panic!("undo recovery must propagate its I/O failure"),
                    Err(error) => error,
                };
                assert!(error.to_string().contains("injected pager"), "{error}");
                std::process::exit(196);
            }
            let store = UnifiedStore::open_with_config(path, config()).expect("child open");
            if std::env::var_os("REDDB_TEST_EXIT_DURING_PAGE_UNDO").is_some() {
                panic!("recovery interruption was not reached");
            }
            for id in 1..=32 {
                store
                    .insert_auto("rows", row(id, 1))
                    .expect("seed overflow row");
            }
            store.persist().expect("baseline");
            let manager = store.get_collection("rows").expect("rows");
            let mut actions = Vec::new();
            for id in 1..=32 {
                manager.update(row(id, 2)).expect("changed row");
                let entity = manager.get(EntityId::new(id)).expect("post-image");
                actions.push(StoreWalAction::upsert_entity(
                    "rows",
                    &entity,
                    None,
                    store.format_version(),
                ));
            }
            let references = store
                .commit
                .as_ref()
                .expect("WAL")
                .append_checkpoint_actions(&actions)
                .expect("durable rows");
            store.remember_checkpoint_rows(actions, references);
            let fault = std::env::var("REDDB_PAGE_UNDO_FAULT").expect("fault");
            std::env::set_var(&fault, "1");
            store.persist().expect("injected checkpoint exit");
            panic!("checkpoint interruption was not reached");
        }
        for (fault, exit_code) in [
            ("REDDB_TEST_EXIT_AFTER_SPARSE_BATCH", 193),
            ("REDDB_TEST_EXIT_AFTER_UNDO_CATALOG_PUBLICATION", 195),
        ] {
            let directory = tempfile::tempdir().expect("temporary database");
            let path = directory.path().join("undo.rdb");
            let run = |recovery: bool| {
                let mut command =
                    std::process::Command::new(std::env::current_exe().expect("test binary"));
                command
                    .args(["--exact", TEST, "--nocapture"])
                    .env(CHILD, &path)
                    .env("REDDB_PAGE_UNDO_FAULT", fault);
                if recovery {
                    command.env("REDDB_TEST_EXIT_DURING_PAGE_UNDO", "1");
                }
                run_child(&mut command)
            };
            let result = run(false);
            assert_eq!(
                result.status.code(),
                Some(exit_code),
                "{}",
                String::from_utf8_lossy(&result.stderr)
            );
            let images = WalReader::open(reddb_file::layout::unified_wal_path(&path))
                .expect("WAL")
                .iter()
                .filter(|item| matches!(item, Ok((_, WalRecord::FullPageImage { .. }))))
                .count();
            assert!(images > 0, "the fixture must overwrite published pages");
            let original_wal =
                std::fs::read(reddb_file::layout::unified_wal_path(&path)).expect("preserved WAL");
            let original_pages = std::fs::read(&path).expect("preserved pages");
            for _ in 0..2 {
                let reader = UnifiedStore::open_read_only_with_config(
                    &path,
                    UnifiedStoreConfig::default().with_page_cache_slots(512),
                )
                .expect("read-only checkpoint undo and cold boot");
                assert_eq!(reader.get_collection("rows").expect("rows").count(), 32);
                for id in 1..=32 {
                    assert_eq!(
                        reader
                            .get("rows", EntityId::new(id))
                            .expect("row")
                            .data
                            .as_row()
                            .expect("row")
                            .columns,
                        row(id, 2).data.as_row().expect("expected row").columns
                    );
                }
                drop(reader);
                assert_eq!(
                    std::fs::read(&path).expect("unchanged pages"),
                    original_pages
                );
                assert_eq!(
                    std::fs::read(reddb_file::layout::unified_wal_path(&path))
                        .expect("unchanged WAL"),
                    original_wal
                );
            }
            let isolated = directory.path().join("isolated-recovery");
            crate::storage::compact::recover_paged_wal(&path, &isolated)
                .expect("recover interrupted checkpoint on a copy");
            assert_eq!(
                std::fs::read(&path).expect("unchanged pages"),
                original_pages
            );
            assert_eq!(
                std::fs::read(reddb_file::layout::unified_wal_path(&path)).expect("unchanged WAL"),
                original_wal
            );
            let recovered = UnifiedStore::open_with_config(isolated.join("undo.rdb"), config())
                .expect("open isolated recovery");
            assert_eq!(recovered.get_collection("rows").expect("rows").count(), 32);
            for id in 1..=32 {
                assert_eq!(
                    recovered
                        .get("rows", EntityId::new(id))
                        .expect("row")
                        .data
                        .as_row()
                        .expect("row")
                        .columns,
                    row(id, 2).data.as_row().expect("expected row").columns
                );
            }
            drop(recovered);
            if exit_code == 193 {
                let wal_path = reddb_file::layout::unified_wal_path(&path);
                let baseline_wal = std::fs::read(&wal_path).expect("baseline WAL");
                let baseline_pages = std::fs::read(&path).expect("baseline pages");
                let (image_offset, original_image) = WalReader::open(&wal_path)
                    .expect("WAL")
                    .iter()
                    .filter_map(|item| match item.expect("valid record") {
                        (offset, image @ WalRecord::FullPageImage { .. }) => Some((offset, image)),
                        _ => None,
                    })
                    .last()
                    .expect("before-image");
                for corruption in 0..6 {
                    let mut image = original_image.clone();
                    if let WalRecord::FullPageImage {
                        tx_id,
                        page_id,
                        ckpt_epoch,
                        data,
                    } = &mut image
                    {
                        match corruption {
                            0 => *tx_id = 9,
                            1 => *page_id = 1,
                            2 => *ckpt_epoch += 1,
                            3 => {
                                data.pop();
                            }
                            4 => data[64] ^= 1,
                            5 => {} // Duplicate a valid first image.
                            _ => unreachable!(),
                        }
                    }
                    let mut invalid_wal = baseline_wal.clone();
                    if corruption == 5 {
                        invalid_wal.extend(image.encode());
                    } else {
                        let offset = usize::try_from(image_offset).expect("fixture offset");
                        invalid_wal.splice(
                            offset..offset + original_image.encode().len(),
                            image.encode(),
                        );
                    }
                    std::fs::write(&wal_path, invalid_wal).expect("inject invalid image");
                    let invalid_wal_before = std::fs::read(&wal_path).expect("invalid WAL");
                    assert!(
                        UnifiedStore::open_read_only_with_config(&path, config()).is_err(),
                        "read-only corruption {corruption}"
                    );
                    assert_eq!(
                        std::fs::read(&wal_path).expect("unchanged invalid WAL"),
                        invalid_wal_before
                    );
                    assert!(
                        UnifiedStore::open_with_config(&path, config()).is_err(),
                        "corruption {corruption}"
                    );
                    assert_eq!(
                        std::fs::read(&path).expect("unchanged pages"),
                        baseline_pages,
                        "validate all images before restoring any page"
                    );
                }
                let mut truncated = baseline_wal.clone();
                let frame = original_image.encode();
                truncated.extend_from_slice(&frame[..frame.len() / 2]);
                std::fs::write(&wal_path, truncated).expect("incomplete undo WAL");
                assert!(UnifiedStore::open_with_config(&path, config()).is_err());
                assert_eq!(
                    std::fs::read(&path).expect("unchanged pages"),
                    baseline_pages
                );
                std::fs::write(&wal_path, baseline_wal).expect("restore valid WAL");
                for fault in ["empty", "partial", "sync"] {
                    let wal = std::fs::read(&wal_path).expect("undo WAL before I/O failure");
                    let output = run_child(
                        std::process::Command::new(std::env::current_exe().expect("test binary"))
                            .args(["--exact", TEST, "--nocapture"])
                            .env(CHILD, &path)
                            .env("REDDB_TEST_PAGE_UNDO_IO", fault),
                    );
                    assert_eq!(
                        output.status.code(),
                        Some(196),
                        "{fault}: {}",
                        String::from_utf8_lossy(&output.stderr)
                    );
                    assert_eq!(
                        std::fs::read(&wal_path).expect("retained undo WAL"),
                        wal,
                        "failed undo must not consume its before-images"
                    );
                }
                let replay_pages = std::fs::read(&path).expect("replay fixture pages");
                let replay_wal = std::fs::read(&wal_path).expect("replay fixture WAL");
                for fault in ["empty", "partial", "sync"] {
                    let replay_directory = tempfile::tempdir().expect("isolated replay fixture");
                    let replay_path = replay_directory.path().join("replay.rdb");
                    let replay_wal_path = reddb_file::layout::unified_wal_path(&replay_path);
                    std::fs::create_dir_all(replay_wal_path.parent().expect("WAL parent"))
                        .expect("replay support directory");
                    std::fs::write(&replay_path, &replay_pages).expect("copy replay pages");
                    std::fs::write(&replay_wal_path, &replay_wal).expect("copy replay WAL");
                    for _ in 0..2 {
                        let output = run_child(
                            std::process::Command::new(
                                std::env::current_exe().expect("test binary"),
                            )
                            .args(["--exact", TEST, "--nocapture"])
                            .env(CHILD, &replay_path)
                            .env("REDDB_TEST_PAGE_REPLAY_IO", fault),
                        );
                        assert_eq!(
                            output.status.code(),
                            Some(196),
                            "replay/{fault}: {}",
                            String::from_utf8_lossy(&output.stderr)
                        );
                        assert!(
                            std::fs::read(&replay_wal_path)
                                .expect("retained replay WAL")
                                .starts_with(&replay_wal),
                            "replay may append ownership evidence but must retain committed WAL"
                        );
                    }
                    for _ in 0..2 {
                        let recovered = UnifiedStore::open_with_config(&replay_path, config())
                            .expect("recover interrupted replay publication");
                        assert_eq!(recovered.get_collection("rows").expect("rows").count(), 32);
                        for id in 1..=32 {
                            assert_eq!(
                                recovered
                                    .get("rows", EntityId::new(id))
                                    .expect("replayed row")
                                    .data
                                    .as_row()
                                    .expect("row")
                                    .columns,
                                row(id, 2).data.as_row().expect("expected row").columns
                            );
                        }
                        recovered.persist().expect("publish recovered replay");
                    }
                }
                for _ in 0..2 {
                    let result = run(true);
                    assert_eq!(
                        result.status.code(),
                        Some(194),
                        "{}",
                        String::from_utf8_lossy(&result.stderr)
                    );
                }
            }
            for _ in 0..3 {
                let store =
                    UnifiedStore::open_with_config(&path, config()).expect("recover checkpoint");
                assert_eq!(store.get_collection("rows").expect("rows").count(), 32);
                for id in 1..=32 {
                    assert_eq!(
                        store
                            .get("rows", EntityId::new(id))
                            .expect("recovered row")
                            .data
                            .as_row()
                            .expect("row")
                            .columns,
                        row(id, 2).data.as_row().expect("expected row").columns
                    );
                }
                store.persist().expect("retirement after recovery");
            }
        }
    }
}