aedb 0.2.9

Embedded Rust storage engine with transactional commits, WAL durability, and snapshot-consistent reads
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
mod integrity;
pub mod replay;
pub mod scanner;

use crate::backup::sha256_hex;
use crate::catalog::Catalog;
use crate::checkpoint::loader::load_checkpoint_bytes_with_key;
use crate::commit::tx::{IdempotencyKey, IdempotencyRecord};
use crate::config::{AedbConfig, RecoveryMode, StorageMode};
use crate::error::{AedbError, RecoveryIntegrityDiagnostic, RecoveryIntegrityKind};
use crate::manifest::atomic::load_manifest_signed_mode;
use crate::manifest::schema::Manifest;
use crate::recovery::integrity::{is_valid_sha256_hex, sha256_prefix_hex};
use crate::recovery::replay::{ReplaySegmentsRequest, replay_segments};
use crate::recovery::scanner::scan_segments;
use crate::storage::keyspace::Keyspace;
use crate::storage::kv_segment::KvSegmentStore;
use crate::storage::value_store::PersistentValueStore;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tracing::{info, warn};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecoveredState {
    pub keyspace: Keyspace,
    pub catalog: Catalog,
    pub current_seq: u64,
    pub idempotency: HashMap<IdempotencyKey, IdempotencyRecord>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecoveryOutcome {
    pub state: RecoveredState,
    pub warnings: Vec<RecoveryIntegrityDiagnostic>,
}

type LoadedCheckpoint = (
    Keyspace,
    Catalog,
    u64,
    HashMap<IdempotencyKey, IdempotencyRecord>,
);

pub fn recover(data_dir: &Path) -> Result<RecoveredState, AedbError> {
    recover_with_config(data_dir, &AedbConfig::default())
}

pub fn recover_with_config(
    data_dir: &Path,
    config: &AedbConfig,
) -> Result<RecoveredState, AedbError> {
    recover_with_config_and_diagnostics(data_dir, config).map(|outcome| outcome.state)
}

pub fn recover_with_config_and_diagnostics(
    data_dir: &Path,
    config: &AedbConfig,
) -> Result<RecoveryOutcome, AedbError> {
    recover_with_optional_target(data_dir, config, None)
}

pub fn recover_at_seq(data_dir: &Path, target_seq: u64) -> Result<RecoveredState, AedbError> {
    recover_at_seq_with_config(data_dir, target_seq, &AedbConfig::default())
}

pub fn recover_at_seq_with_config(
    data_dir: &Path,
    target_seq: u64,
    config: &AedbConfig,
) -> Result<RecoveredState, AedbError> {
    recover_with_optional_target(data_dir, config, Some(target_seq)).map(|outcome| outcome.state)
}

fn recover_with_optional_target(
    data_dir: &Path,
    config: &AedbConfig,
    target_seq: Option<u64>,
) -> Result<RecoveryOutcome, AedbError> {
    info!("recovery: load manifest");
    let manifest =
        load_manifest_signed_mode(data_dir, config.hmac_key(), config.strict_recovery())?;
    let explicit_manifest = has_explicit_manifest(data_dir);
    let replay_to = effective_replay_target(target_seq, &manifest, explicit_manifest);
    let mut keyspace = Keyspace::with_backend(config.primary_index_backend);
    let mut catalog = Catalog::default();
    let mut seq = 0u64;
    let mut idempotency = HashMap::new();

    if let Some((ks, cat, loaded_seq, checkpoint_idempotency)) =
        load_latest_valid_checkpoint(&manifest, data_dir, config, replay_to)?
    {
        keyspace = ks;
        keyspace.set_backend(config.primary_index_backend);
        catalog = cat;
        seq = replay_to
            .map(|target_seq| loaded_seq.min(target_seq))
            .unwrap_or(loaded_seq);
        idempotency = checkpoint_idempotency;
    }

    info!("recovery: scan segments");
    let (segments, replay_limits) =
        segment_paths_for_replay(data_dir, &manifest, explicit_manifest, config)?;
    info!("recovery: replay segments");
    let replayed = replay_segments(ReplaySegmentsRequest {
        segments: &segments,
        from_seq_exclusive: seq,
        to_seq_inclusive: replay_to,
        segment_replay_byte_limits: Some(&replay_limits.limits),
        hash_chain_required: config.hash_chain_required,
        strict_recovery: config.strict_recovery(),
        keyspace: &mut keyspace,
        catalog: &mut catalog,
        idempotency: &mut idempotency,
    })?;
    attach_configured_value_store(&mut keyspace, config, data_dir)?;
    Ok(RecoveryOutcome {
        state: RecoveredState {
            keyspace,
            catalog,
            current_seq: replayed,
            idempotency,
        },
        warnings: replay_limits.warnings,
    })
}

fn effective_replay_target(
    target_seq: Option<u64>,
    manifest: &Manifest,
    explicit_manifest: bool,
) -> Option<u64> {
    match (target_seq, explicit_manifest) {
        (Some(target_seq), true) => Some(target_seq.min(manifest.durable_seq)),
        (Some(target_seq), false) => Some(target_seq),
        (None, true) => Some(manifest.durable_seq),
        (None, false) => None,
    }
}

fn attach_configured_value_store(
    keyspace: &mut Keyspace,
    config: &AedbConfig,
    data_dir: &Path,
) -> Result<(), AedbError> {
    if matches!(config.storage_mode, StorageMode::DiskBacked) {
        let store = Arc::new(PersistentValueStore::open_with_hot_cache_bytes(
            data_dir,
            config.persistent_value_hot_cache_bytes,
        )?);
        let segment_store = Arc::new(KvSegmentStore::open_with_block_cache_bytes(
            data_dir,
            config.kv_segment_block_cache_bytes,
        )?);
        keyspace
            .attach_persistent_value_store(store, config.persistent_value_inline_threshold_bytes)?;
        keyspace.attach_kv_segment_store(segment_store);
        keyspace.spill_kv_values_to_memory_target(config.max_memory_estimate_bytes)?;
        keyspace.flush_kv_to_segments_to_memory_target(config.max_memory_estimate_bytes)?;
    } else {
        keyspace.detach_persistent_value_store();
    }
    Ok(())
}

fn has_explicit_manifest(data_dir: &Path) -> bool {
    data_dir.join("manifest.json").exists() || data_dir.join("manifest.json.prev").exists()
}

#[derive(Debug, Default)]
struct ReplaySelection {
    limits: HashMap<PathBuf, u64>,
    warnings: Vec<RecoveryIntegrityDiagnostic>,
}

fn segment_paths_for_replay(
    data_dir: &Path,
    manifest: &Manifest,
    explicit_manifest: bool,
    config: &AedbConfig,
) -> Result<(Vec<PathBuf>, ReplaySelection), AedbError> {
    if !explicit_manifest {
        return Ok((scan_segments(data_dir)?, ReplaySelection::default()));
    }

    let mut segments = manifest.segments.clone();
    segments.sort_by_key(|segment| segment.segment_seq);
    let mut segment_paths = Vec::with_capacity(segments.len());
    let mut replay_selection = ReplaySelection::default();
    let active_segment_seq = manifest.active_segment_seq;
    for segment in segments {
        let is_active_segment = segment.segment_seq == active_segment_seq;
        let path = data_dir.join(&segment.filename);
        if !path.exists() {
            let diagnostic = manifest_segment_diagnostic(
                RecoveryIntegrityKind::ManifestWalSegmentMissing,
                data_dir,
                manifest,
                &segment,
                config.recovery_mode,
                SegmentDiagnosticDetails::default(),
            );
            handle_manifest_segment_diagnostic(&mut replay_selection, diagnostic)?;
            continue;
        }
        let actual_size_bytes = std::fs::metadata(&path)?.len();
        if segment.size_bytes == 0 {
            if config.strict_recovery() && !is_active_segment {
                let diagnostic = manifest_segment_diagnostic(
                    RecoveryIntegrityKind::ManifestWalSegmentMissingSizeMetadata,
                    data_dir,
                    manifest,
                    &segment,
                    config.recovery_mode,
                    SegmentDiagnosticDetails::with_actual_size(actual_size_bytes),
                );
                handle_manifest_segment_diagnostic(&mut replay_selection, diagnostic)?;
            }
            warn!(
                filename = %segment.filename,
                "recovery: wal segment size metadata missing, skipping strict size check"
            );
        } else {
            if actual_size_bytes < segment.size_bytes {
                let diagnostic = manifest_segment_diagnostic(
                    RecoveryIntegrityKind::ManifestWalSegmentTruncated,
                    data_dir,
                    manifest,
                    &segment,
                    config.recovery_mode,
                    SegmentDiagnosticDetails::with_actual_size(actual_size_bytes),
                );
                handle_manifest_segment_diagnostic(&mut replay_selection, diagnostic)?;
                continue;
            }
            if actual_size_bytes > segment.size_bytes
                && !is_active_segment
                && config.strict_recovery()
            {
                let diagnostic = manifest_segment_diagnostic(
                    RecoveryIntegrityKind::ManifestWalSegmentSizeMismatch,
                    data_dir,
                    manifest,
                    &segment,
                    config.recovery_mode,
                    SegmentDiagnosticDetails::with_actual_size(actual_size_bytes),
                );
                handle_manifest_segment_diagnostic(&mut replay_selection, diagnostic)?;
            }
        }
        if segment.sha256_hex.is_empty() {
            if config.strict_recovery() && !is_active_segment {
                let diagnostic = manifest_segment_diagnostic(
                    RecoveryIntegrityKind::ManifestWalSegmentMissingChecksumMetadata,
                    data_dir,
                    manifest,
                    &segment,
                    config.recovery_mode,
                    SegmentDiagnosticDetails::with_actual_size(actual_size_bytes),
                );
                handle_manifest_segment_diagnostic(&mut replay_selection, diagnostic)?;
            }
            warn!(
                filename = %segment.filename,
                "recovery: wal segment sha256 metadata missing, skipping integrity check in permissive mode"
            );
        } else if !is_valid_sha256_hex(&segment.sha256_hex) {
            if config.strict_recovery() && !is_active_segment {
                let diagnostic = manifest_segment_diagnostic(
                    RecoveryIntegrityKind::ManifestWalSegmentInvalidChecksumMetadata,
                    data_dir,
                    manifest,
                    &segment,
                    config.recovery_mode,
                    SegmentDiagnosticDetails::with_actual_size(actual_size_bytes),
                );
                handle_manifest_segment_diagnostic(&mut replay_selection, diagnostic)?;
            }
            warn!(
                filename = %segment.filename,
                "recovery: invalid wal segment sha256 metadata, skipping integrity check"
            );
        } else {
            let hash_len = if segment.size_bytes > 0 {
                segment.size_bytes
            } else {
                actual_size_bytes
            };
            let actual = sha256_prefix_hex(&path, hash_len)?;
            if actual != segment.sha256_hex {
                let diagnostic = manifest_segment_diagnostic(
                    RecoveryIntegrityKind::ManifestWalSegmentChecksumMismatch,
                    data_dir,
                    manifest,
                    &segment,
                    config.recovery_mode,
                    SegmentDiagnosticDetails {
                        actual_size_bytes: Some(actual_size_bytes),
                        actual_sha256_hex: Some(actual),
                    },
                );
                return Err(recovery_integrity_error(diagnostic));
            }
        }
        let replay_limit_path = path.clone();
        segment_paths.push(path);
        if segment.size_bytes > 0 {
            replay_selection
                .limits
                .insert(replay_limit_path, segment.size_bytes);
        }
    }
    Ok((segment_paths, replay_selection))
}

#[derive(Default)]
struct SegmentDiagnosticDetails {
    actual_size_bytes: Option<u64>,
    actual_sha256_hex: Option<String>,
}

impl SegmentDiagnosticDetails {
    fn with_actual_size(actual_size_bytes: u64) -> Self {
        Self {
            actual_size_bytes: Some(actual_size_bytes),
            ..Self::default()
        }
    }
}

fn manifest_segment_diagnostic(
    kind: RecoveryIntegrityKind,
    data_dir: &Path,
    manifest: &Manifest,
    segment: &crate::manifest::schema::SegmentMeta,
    recovery_mode: RecoveryMode,
    details: SegmentDiagnosticDetails,
) -> RecoveryIntegrityDiagnostic {
    RecoveryIntegrityDiagnostic {
        kind,
        segment_filename: segment.filename.clone(),
        manifest_path: data_dir.join("manifest.json"),
        wal_dir: data_dir.to_path_buf(),
        recovery_mode,
        durable_seq: manifest.durable_seq,
        visible_seq: manifest.visible_seq,
        active_segment_seq: manifest.active_segment_seq,
        segment_seq: segment.segment_seq,
        latest_checkpoint_seq: manifest.checkpoints.iter().map(|cp| cp.seq).max(),
        expected_size_bytes: (segment.size_bytes > 0).then_some(segment.size_bytes),
        actual_size_bytes: details.actual_size_bytes,
        expected_sha256_hex: (!segment.sha256_hex.is_empty()).then(|| segment.sha256_hex.clone()),
        actual_sha256_hex: details.actual_sha256_hex,
    }
}

fn handle_manifest_segment_diagnostic(
    replay_selection: &mut ReplaySelection,
    diagnostic: RecoveryIntegrityDiagnostic,
) -> Result<(), AedbError> {
    match diagnostic.recovery_mode {
        RecoveryMode::Strict => Err(recovery_integrity_error(diagnostic)),
        RecoveryMode::Permissive => {
            warn!(
                kind = %diagnostic.kind,
                filename = %diagnostic.segment_filename,
                "recovery: skipping manifest-referenced wal segment in permissive mode"
            );
            replay_selection.warnings.push(diagnostic);
            Ok(())
        }
    }
}

fn recovery_integrity_error(diagnostic: RecoveryIntegrityDiagnostic) -> AedbError {
    AedbError::RecoveryIntegrity {
        diagnostic: Box::new(diagnostic),
    }
}

fn load_latest_valid_checkpoint(
    manifest: &crate::manifest::schema::Manifest,
    data_dir: &Path,
    config: &AedbConfig,
    max_seq: Option<u64>,
) -> Result<Option<LoadedCheckpoint>, AedbError> {
    let mut candidates = 0usize;
    let mut failures = 0usize;
    for cp in manifest.checkpoints.iter().rev() {
        if let Some(limit) = max_seq
            && cp.seq > limit
        {
            continue;
        }
        let cp_path = data_dir.join(&cp.filename);
        if !cp_path.exists() {
            continue;
        }
        candidates = candidates.saturating_add(1);
        // Read the payload once and reuse it for both the manifest-digest check
        // and decoding, instead of streaming the file for a hash and then
        // reading it a second time inside the loader.
        let bytes = fs::read(&cp_path)?;
        let mut manifest_hash_verified = false;
        if cp.sha256_hex.is_empty() {
            if config.strict_recovery() {
                failures = failures.saturating_add(1);
                warn!(
                    filename = %cp.filename,
                    seq = cp.seq,
                    "recovery: checkpoint metadata missing sha256 in strict mode"
                );
                continue;
            }
        } else if !is_valid_sha256_hex(&cp.sha256_hex) {
            failures = failures.saturating_add(1);
            warn!(
                filename = %cp.filename,
                seq = cp.seq,
                "recovery: checkpoint metadata has invalid sha256"
            );
            continue;
        } else {
            let actual = sha256_hex(&bytes);
            if actual != cp.sha256_hex {
                failures = failures.saturating_add(1);
                warn!(
                    filename = %cp.filename,
                    seq = cp.seq,
                    "recovery: checkpoint sha256 mismatch"
                );
                continue;
            }
            manifest_hash_verified = true;
        }
        info!("recovery: load checkpoint {}", cp.filename);
        // Skip the loader's internal trailer re-hash when the full payload was
        // already verified against the manifest digest above.
        match load_checkpoint_bytes_with_key(
            &bytes,
            config.checkpoint_key(),
            !manifest_hash_verified,
        ) {
            Ok(loaded) => return Ok(Some(loaded)),
            Err(err) => {
                failures = failures.saturating_add(1);
                warn!(
                    filename = %cp.filename,
                    seq = cp.seq,
                    error = %err,
                    "recovery: checkpoint load failed, trying older checkpoint"
                );
            }
        }
    }
    if config.strict_recovery() && candidates > 0 && failures == candidates {
        return Err(AedbError::Validation(
            "all referenced checkpoints failed to load in strict recovery mode".into(),
        ));
    }
    Ok(None)
}

#[cfg(test)]
#[path = "tests.rs"]
mod tests;