hawdb-storage 0.9.0

An embedded Rust graph database for local data-plane workloads
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
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Test-support-only capture of real project IO and physical crash images.
//!
//! Attach before opening a database in an empty, already durable project
//! directory. Descendant project installations inherit the recorder, allowing
//! their first directory installation to be observed. Capture at an acknowledgement boundary while the host excludes
//! concurrent database operations. Capture verifies the visible native tree
//! against the model, so unexpected visible changes cannot silently qualify.
//! A snapshot survives later handle closure and its additional flushes.
//!
//! Qualification assumes completed file/directory barriers preserve bytes/names
//! and same-directory replacement is atomic. On Windows the directory-flush
//! assumption is conditional: native execution does not establish filesystem or
//! device namespace durability. See `docs/BRANCH_POWER_LOSS_QUALIFICATION.md`.

pub mod image;

use crate::file_descriptors::{absolute_path_ref, FileOpenContext, ProjectFileDescriptors};
use image::{CrashImage, CrashPlan, ImageEngine, ImageLimits, InodeId, UncoveredWrite};
use std::collections::{BTreeMap, BTreeSet};
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard};

#[derive(Debug)]
pub struct PowerLossModel {
    core: Arc<ModelCore>,
    // Keep the project registry entry alive even across database closure.
    project: ProjectFileDescriptors,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IoEvent {
    Rename,
    Remove,
    HardLink,
    CreateFile,
    CreateDirectory,
    Write,
    Truncate,
    FileSync,
    DirectorySync,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ObservationBoundary {
    Before,
    After,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ObservationPoint {
    pub event: IoEvent,
    pub relative_path: PathBuf,
    pub boundary: ObservationBoundary,
    pub skip_matches: usize,
    pub include_descendants: bool,
    /// Replace the single retained image at each matching event. Useful when
    /// bootstrap performs several checkpoint renames before its final selector.
    pub keep_last: bool,
}

#[derive(Debug)]
struct Observation {
    point: ObservationPoint,
    snapshot: Option<PowerLossSnapshot>,
}

#[derive(Debug)]
pub(crate) struct ModelCore {
    root: PathBuf,
    engine: Mutex<ImageEngine>,
    observation: Mutex<Option<Observation>>,
}

#[derive(Debug, Clone)]
pub struct PowerLossSnapshot {
    engine: ImageEngine,
    observed_path: Option<PathBuf>,
}

impl PowerLossModel {
    pub fn attach(project: &ProjectFileDescriptors, limits: ImageLimits) -> io::Result<Self> {
        if !cfg!(any(unix, windows)) {
            return Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "power-loss capture requires Unix or Windows native IO",
            ));
        }
        let engine = ImageEngine::new(limits)?;
        let mut slot = project.state.power_loss.lock().map_err(|_| poisoned())?;
        let metrics = project.metrics();
        if slot.upgrade().is_some() || metrics.open != 0 || metrics.reserved != 0 {
            return Err(invalid(
                "attach power-loss capture before acquiring project handles",
            ));
        }
        if std::fs::read_dir(project.root())?.next().is_some() {
            return Err(invalid(
                "power-loss capture requires an empty project directory",
            ));
        }
        let core = Arc::new(ModelCore {
            root: project.root().to_path_buf(),
            engine: Mutex::new(engine),
            observation: Mutex::new(None),
        });
        *slot = Arc::downgrade(&core);
        Ok(Self {
            core,
            project: project.clone(),
        })
    }

    pub fn capture(&self) -> io::Result<PowerLossSnapshot> {
        let engine = self.core.lock()?;
        verify_native_tree(
            &self.core.root,
            &engine.crash(&engine.persist_all_plan())?,
            &self.project.io_context(),
        )?;
        Ok(PowerLossSnapshot {
            engine: engine.clone(),
            observed_path: None,
        })
    }

    pub fn project(&self) -> &ProjectFileDescriptors {
        &self.project
    }

    /// Retain at most one image at an actual namespace operation boundary.
    /// No callback runs under storage locks, and later barriers cannot advance
    /// the captured image. A subsequent final capture verifies IO coverage.
    pub fn observe(&self, point: ObservationPoint) -> io::Result<()> {
        let _engine = self.core.lock()?;
        if point.relative_path.as_os_str().is_empty()
            || !point
                .relative_path
                .components()
                .all(|component| matches!(component, std::path::Component::Normal(_)))
        {
            return Err(invalid(
                "observation path must be normalized and project-relative",
            ));
        }
        let mut observation = self.core.observation.lock().map_err(|_| poisoned())?;
        if observation.is_some() {
            return Err(invalid(
                "take the previous observation before arming another",
            ));
        }
        *observation = Some(Observation {
            point,
            snapshot: None,
        });
        Ok(())
    }

    pub fn take_observation(&self) -> io::Result<Option<PowerLossSnapshot>> {
        let _engine = self.core.lock()?;
        let mut observation = self.core.observation.lock().map_err(|_| poisoned())?;
        match observation
            .as_mut()
            .and_then(|observation| observation.snapshot.take())
        {
            Some(snapshot) => {
                *observation = None;
                Ok(Some(snapshot))
            }
            None => Ok(None),
        }
    }
}

impl PowerLossSnapshot {
    pub fn observed_path(&self) -> Option<&Path> {
        self.observed_path.as_deref()
    }
    pub fn crash(&self, plan: &CrashPlan) -> io::Result<CrashImage> {
        self.engine.crash(plan)
    }

    pub fn persist_all_plan(&self) -> CrashPlan {
        self.engine.persist_all_plan()
    }

    pub fn uncovered_writes(&self, relative_path: &Path) -> io::Result<Vec<UncoveredWrite>> {
        let inode = self.engine.inode(relative_path)?;
        Ok(self
            .engine
            .uncovered_writes()
            .filter(|write| write.inode == inode)
            .collect())
    }
}

impl ModelCore {
    pub(crate) fn lock(&self) -> io::Result<MutexGuard<'_, ImageEngine>> {
        self.engine.lock().map_err(|_| poisoned())
    }

    pub(crate) fn relative(&self, path: &Path) -> io::Result<PathBuf> {
        self.relative_path(path)?
            .ok_or_else(|| invalid("captured IO escaped the project directory"))
    }

    fn relative_path(&self, path: &Path) -> io::Result<Option<PathBuf>> {
        let absolute = absolute_path_ref(path)?;
        if let Ok(relative) = absolute.strip_prefix(&self.root) {
            return Ok(Some(relative.to_path_buf()));
        }
        // The descriptor registry also accepts a lexical root alias, e.g.
        // macOS /var -> /private/var. Resolve the nearest existing ancestor
        // before appending new filenames; those do not canonicalize yet.
        let mut ancestor = absolute.as_ref();
        let mut suffix = Vec::new();
        let mut resolved = loop {
            match std::fs::canonicalize(ancestor) {
                Ok(resolved) => break resolved,
                Err(error) if error.kind() == io::ErrorKind::NotFound => {
                    suffix.push(ancestor.file_name().ok_or(error)?);
                    ancestor = ancestor
                        .parent()
                        .ok_or_else(|| invalid("captured path has no existing ancestor"))?;
                }
                Err(error) => return Err(error),
            }
        };
        for component in suffix.iter().rev() {
            resolved.push(component);
        }
        Ok(resolved
            .strip_prefix(&self.root)
            .ok()
            .map(Path::to_path_buf))
    }

    /// Admit a complete model change before native mutation, then publish the
    /// model change only after native success. The mutex covers both steps.
    pub(crate) fn mutate_observed<T>(
        &self,
        event: Option<(IoEvent, &Path)>,
        simulate: impl FnOnce(&mut ImageEngine) -> io::Result<()>,
        native: impl FnOnce() -> io::Result<T>,
    ) -> io::Result<T> {
        let mut engine = self.lock()?;
        let mut candidate = engine.clone();
        simulate(&mut candidate)?;
        self.observe_at(&engine, event, ObservationBoundary::Before)?;
        let value = native()?;
        *engine = candidate;
        self.observe_at(&engine, event, ObservationBoundary::After)?;
        Ok(value)
    }

    fn observe_at(
        &self,
        engine: &ImageEngine,
        event: Option<(IoEvent, &Path)>,
        boundary: ObservationBoundary,
    ) -> io::Result<()> {
        let Some((event, path)) = event else {
            return Ok(());
        };
        let mut observation = self.observation.lock().map_err(|_| poisoned())?;
        if let Some(observation) = observation.as_mut()
            && (observation.snapshot.is_none() || observation.point.keep_last)
            && observation.point.event == event
            && (observation.point.relative_path == path
                || observation.point.include_descendants
                    && path.starts_with(&observation.point.relative_path))
            && observation.point.boundary == boundary
        {
            if observation.point.skip_matches > 0 {
                observation.point.skip_matches -= 1;
            } else {
                observation.snapshot = Some(PowerLossSnapshot {
                    engine: engine.clone(),
                    observed_path: Some(path.to_path_buf()),
                });
            }
        }
        Ok(())
    }
}

pub(crate) fn for_path(
    context: &FileOpenContext,
    path: &Path,
) -> io::Result<Option<Arc<ModelCore>>> {
    let core = context
        .state
        .power_loss
        .lock()
        .map_err(|_| poisoned())?
        .upgrade();
    match core {
        Some(core) if core.relative_path(path)?.is_some() => Ok(Some(core)),
        _ => Ok(None),
    }
}

pub(crate) fn namespace<T>(
    context: &FileOpenContext,
    path: &Path,
    event: Option<IoEvent>,
    simulate: impl FnOnce(&mut ImageEngine, &Path) -> io::Result<()>,
    native: impl FnOnce() -> io::Result<T>,
) -> io::Result<T> {
    let Some(core) = for_path(context, path)? else {
        return native();
    };
    let relative = core.relative(path)?;
    core.mutate_observed(
        event.map(|event| (event, relative.as_path())),
        |engine| simulate(engine, &relative),
        native,
    )
}

pub(crate) fn two_paths<T>(
    source_context: &FileOpenContext,
    destination_context: &FileOpenContext,
    source: &Path,
    destination: &Path,
    event: Option<IoEvent>,
    simulate: impl FnOnce(&mut ImageEngine, &Path, &Path) -> io::Result<()>,
    native: impl FnOnce() -> io::Result<T>,
) -> io::Result<T> {
    match (
        for_path(source_context, source)?,
        for_path(destination_context, destination)?,
    ) {
        (None, None) => native(),
        (Some(core), Some(other)) if Arc::ptr_eq(&core, &other) => {
            let source = core.relative(source)?;
            let destination = core.relative(destination)?;
            core.mutate_observed(
                event.map(|event| (event, destination.as_path())),
                |engine| simulate(engine, &source, &destination),
                native,
            )
        }
        _ => Err(invalid(
            "cross-project namespace changes are outside the captured fault model",
        )),
    }
}

pub(crate) fn create_directories(engine: &mut ImageEngine, path: &Path) -> io::Result<()> {
    let mut prefix = PathBuf::new();
    for component in path.components() {
        prefix.push(component);
        match engine.inode(&prefix) {
            Ok(inode) if engine.is_directory(inode) => {}
            Ok(_) => {
                return Err(io::Error::new(
                    io::ErrorKind::NotADirectory,
                    "captured create_dir_all found a file",
                ))
            }
            Err(error) if error.kind() == io::ErrorKind::NotFound => {
                engine.create_directory(&prefix)?;
            }
            Err(error) => return Err(error),
        }
    }
    Ok(())
}

fn verify_native_tree(
    root: &Path,
    image: &CrashImage,
    context: &FileOpenContext,
) -> io::Result<()> {
    let mut native_files = BTreeSet::new();
    let mut native_directories = BTreeSet::new();
    let mut directories = vec![root.to_path_buf()];
    let mut native_by_inode: BTreeMap<InodeId, identity::NativeIdentity> = BTreeMap::new();
    let mut inode_by_native = BTreeMap::new();
    while let Some(directory) = directories.pop() {
        native_directories.insert(
            directory
                .strip_prefix(root)
                .map_err(|_| invalid("native directory escaped root"))?
                .to_path_buf(),
        );
        let _directory_permit =
            context.acquire(crate::file_descriptors::DescriptorKind::Transient)?;
        for entry in std::fs::read_dir(directory)? {
            let entry = entry?;
            let path = entry.path();
            let metadata = entry.metadata()?;
            identity::require_direct_entry(&metadata)?;
            let kind = metadata.file_type();
            if kind.is_dir() {
                directories.push(path);
                continue;
            }
            if !kind.is_file() {
                return Err(invalid("captured project contains a nonregular file"));
            }
            let relative = path
                .strip_prefix(root)
                .map_err(|_| invalid("native path escaped root"))?;
            let expected = image
                .bytes(relative)
                .ok_or_else(|| invalid("native file has no captured IO history"))?;
            let _file_permit =
                context.acquire(crate::file_descriptors::DescriptorKind::Transient)?;
            let mut file = std::fs::File::open(&path)?;
            let identity = identity::of_file(&file)?;
            if file.metadata()?.len() != expected.len() as u64 {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("uncaptured native length at {}", relative.display()),
                ));
            }
            let mut bytes = Vec::new();
            // Windows byte-range locks also cover bytes beyond EOF. Empty
            // lease files need only the handle's identity and length checked;
            // probing EOF through a second handle would violate their lock.
            if !expected.is_empty() {
                file.by_ref()
                    .take((expected.len() as u64).saturating_add(1))
                    .read_to_end(&mut bytes)?;
            }
            if bytes.as_slice() != expected {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("uncaptured native bytes at {}", relative.display()),
                ));
            }
            let inode = image
                .file_inode(relative)
                .ok_or_else(|| invalid("captured inode is absent"))?;
            if let Some(prior) = inode_by_native.insert(identity, inode)
                && prior != inode
            {
                return Err(invalid(
                    "native hard link has separate captured inode identities",
                ));
            }
            if let Some(prior) = native_by_inode.insert(inode, identity)
                && prior != identity
            {
                return Err(invalid(
                    "captured hard-link identity differs from native identity",
                ));
            }
            native_files.insert(relative.to_path_buf());
        }
    }
    let captured_files = image.paths().map(Path::to_path_buf).collect();
    let captured_directories = image.directory_paths().map(Path::to_path_buf).collect();
    if native_files != captured_files || native_directories != captured_directories {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!(
                "captured namespace differs from native namespace: missing file {:?}, missing directory {:?}, unexpected directory {:?}",
                captured_files.difference(&native_files).next(),
                captured_directories.difference(&native_directories).next(),
                native_directories.difference(&captured_directories).next(),
            ),
        ));
    }
    Ok(())
}

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

fn poisoned() -> io::Error {
    invalid("power-loss capture was interrupted while recording IO")
}

mod identity;
mod native;
pub(crate) use native::NativeTrace;

#[cfg(all(test, any(unix, windows)))]
mod tests;