hawdb-storage 0.5.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
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
// 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.

use super::doctor::DatabaseDoctor;
use super::{
    file_checksum, load_published_canonical_adjacency, load_published_property_projection,
    store_id_for_path, DurableManifest, GraphManifestOpenBudget, GraphStore, RecoveryMode,
    SegmentCache, StorageResidencyMode, WalReplayConfig, MANIFEST_FILE,
};
use crate::error::{HawDBError, Result};
use hawdb_storage::derived_repair::{plan_identity, validate_options, validate_plan};
pub use hawdb_storage::derived_repair::{
    DerivedArtifactBranchSource, DerivedArtifactHealth, DerivedArtifactHealthReport,
    DerivedArtifactHealthState, DerivedArtifactKind, DerivedArtifactRebuildOptions,
    DerivedArtifactRepairPlan, DerivedArtifactRepairReport, DERIVED_ARTIFACT_REPAIR_PROTOCOL,
};
use std::path::{Path, PathBuf};
use std::sync::Arc;

#[path = "derived_repair/audit.rs"]
#[doc(hidden)]
pub mod audit;
#[path = "derived_repair/publication.rs"]
pub(super) mod publication;
use audit::{
    finalize_repair, load_matching_pending_record, load_single_pending_record, prepare_repair,
    validate_pending_record,
};

struct DerivedInspection {
    store: GraphStore,
    recovered_catalog: crate::schema::Catalog,
    health: DerivedArtifactHealthReport,
    manifest: DurableManifest,
}

impl DatabaseDoctor {
    pub fn derived_artifact_health(
        path: impl AsRef<Path>,
        options: DerivedArtifactRebuildOptions,
    ) -> Result<DerivedArtifactHealthReport> {
        validate_options(options)?;
        let directory = super::doctor::repair_directory(path.as_ref())?;
        let path = directory.as_path();
        let _files = super::doctor::repair_file_descriptors(path)?;
        if let Some(record) = load_single_pending_record(path)? {
            validate_pending_record(path, &record)?;
            let inspection = inspect(path, record.plan.options)?;
            return Ok(inspection.health);
        }
        Ok(inspect(path, options)?.health)
    }

    pub fn plan_derived_artifact_rebuild(
        path: impl AsRef<Path>,
        options: DerivedArtifactRebuildOptions,
    ) -> Result<DerivedArtifactRepairPlan> {
        validate_options(options)?;
        let directory = super::doctor::repair_directory(path.as_ref())?;
        let path = directory.as_path();
        let _files = super::doctor::repair_file_descriptors(path)?;
        if let Some(record) = load_single_pending_record(path)? {
            validate_pending_record(path, &record)?;
            return Ok(record.plan);
        }
        let inspection = inspect(path, options)?;
        plan_from_inspection(&inspection, options)
    }

    pub fn apply_derived_artifact_rebuild(
        path: impl AsRef<Path>,
        plan: &DerivedArtifactRepairPlan,
    ) -> Result<DerivedArtifactRepairReport> {
        validate_plan(plan)?;
        let directory = super::doctor::repair_directory(path.as_ref())?;
        let path = directory.as_path();
        let _files = super::doctor::repair_file_descriptors(path)?;
        let pending = load_matching_pending_record(path, &plan.plan_id)?;
        if let Some(record) = &pending {
            validate_pending_record(path, record)?;
            if record.plan != *plan {
                return Err(HawDBError::Storage(
                    "pending derived repair plan differs from the requested plan".into(),
                ));
            }
        }
        let mut inspection = inspect(path, plan.options)?;
        if let Some(record) = &pending
            && inspection.manifest.checkpoint_epoch == record.plan.target_generation
            && inspection.manifest.checkpoint_commit_epoch == plan.source_commit_epoch
            && inspection.store.commit_epoch == plan.source_commit_epoch
            && !inspection.health.repair_required
        {
            // Keep the UUID lease through validation and audit completion.
            return finalize_repair(path, record.clone(), true);
        }
        if pending.is_none() && plan_from_inspection(&inspection, plan.options)? != *plan {
            return Err(HawDBError::Storage(
                "derived artifact repair plan no longer matches the current database state"
                    .to_string(),
            ));
        }
        validate_source_identity(path, plan)?;
        if inspection.manifest.checkpoint_epoch != plan.source_generation
            || inspection.store.commit_epoch != plan.source_commit_epoch
            || inspection.health.source_node_count != plan.source_node_count
            || inspection.health.source_relationship_count != plan.source_relationship_count
            || inspection.health.source_logical_bytes != plan.source_logical_bytes
            || inspection
                .store
                .durable
                .as_ref()
                .unwrap()
                .next_checkpoint_generation()?
                != plan.target_generation
        {
            return Err(HawDBError::Storage(
                "derived repair source no longer matches the planned recovery".into(),
            ));
        }
        let resumed = pending.is_some();
        let prepared = match pending {
            Some(record) => record,
            None => prepare_repair(path, plan)?,
        };
        validate_pending_record(path, &prepared)?;
        if resumed && plan.branch.is_some() {
            audit::archive_unpublished_checkpoint(
                path,
                &prepared,
                inspection.store.durable.as_ref().unwrap().root_path(),
            )?;
        }
        if plan.branch.is_some() {
            inspection
                .store
                .authorize_derived_repair_publication(Arc::new(
                    publication::DerivedRepairPublication::from_prepared(path, prepared.clone()),
                ))?;
        }
        inspection.store.enable_derived_repair_writes()?;
        let build_config = build_config(plan.options)?;
        inspection
            .store
            .checkpoint_with_reader_epoch_and_build_config(
                &inspection.recovered_catalog,
                None,
                build_config,
            )?;
        let durable = inspection.store.durable.as_ref().unwrap();
        let manifest = match &plan.branch {
            Some(source) => DurableManifest::load(&published_branch(path, source)?.manifest_path)?,
            None => DurableManifest::load(durable.manifest_path())?,
        };
        if manifest.checkpoint_epoch != plan.target_generation
            || manifest.checkpoint_commit_epoch != plan.source_commit_epoch
            || assess_artifacts(
                durable.root_path(),
                &inspection.store,
                manifest,
                plan.options,
            )?
            .iter()
            .any(|artifact| artifact.state == DerivedArtifactHealthState::RepairRequired)
        {
            return Err(HawDBError::Storage(
                "derived artifact rebuild did not publish one healthy target generation; pending audit was retained"
                    .to_string(),
            ));
        }
        let prepared = if plan.branch.is_some() {
            load_matching_pending_record(path, &plan.plan_id)?.ok_or_else(|| {
                HawDBError::Storage("published branch repair lost its pending audit".into())
            })?
        } else {
            prepared
        };
        finalize_repair(path, prepared, resumed)
    }
}

pub(crate) fn reject_pending_derived_artifact_repair(path: &Path) -> Result<()> {
    let pending = audit::pending_record_paths_at_directory(path)?;
    if pending.is_empty() {
        return Ok(());
    }
    Err(HawDBError::Storage(format!(
        "database has {} interrupted derived artifact repair record(s); finish the repair with DatabaseDoctor before opening the database",
        pending.len()
    )))
}

fn inspect(path: &Path, options: DerivedArtifactRebuildOptions) -> Result<DerivedInspection> {
    let replay = WalReplayConfig {
        recovery_mode: RecoveryMode::Strict,
        max_entries: Some(options.max_wal_replay_entries),
        max_bytes: Some(options.max_wal_replay_bytes),
        segment_cache_capacity_bytes: options.segment_cache_capacity_bytes,
        max_graph_manifest_open_bytes: options.max_graph_manifest_open_bytes,
        residency_mode: StorageResidencyMode::OutOfCore,
        max_out_of_core_delta_bytes: Some(options.max_source_logical_bytes),
        ..WalReplayConfig::default()
    };
    let (store, recovered_catalog) = GraphStore::open_for_derived_repair(path, replay)?;
    let durable = store.durable.as_ref().expect("repair source is durable");
    let manifest = DurableManifest::load(durable.manifest_path())?;
    manifest.validate()?;
    let (node_count, relationship_count, logical_bytes) =
        validate_canonical_source(&store, options)?;
    let artifacts = assess_artifacts(durable.root_path(), &store, manifest, options)?;
    let repair_required = artifacts
        .iter()
        .any(|artifact| artifact.state == DerivedArtifactHealthState::RepairRequired);
    Ok(DerivedInspection {
        store,
        recovered_catalog,
        health: DerivedArtifactHealthReport {
            protocol: DERIVED_ARTIFACT_REPAIR_PROTOCOL.to_string(),
            checkpoint_generation: manifest.checkpoint_epoch,
            checkpoint_commit_epoch: manifest.checkpoint_commit_epoch,
            canonical_source_validated: true,
            source_node_count: node_count,
            source_relationship_count: relationship_count,
            source_logical_bytes: logical_bytes,
            artifacts,
            repair_required,
        },
        manifest,
    })
}

fn validate_canonical_source(
    store: &GraphStore,
    options: DerivedArtifactRebuildOptions,
) -> Result<(u64, u64, u64)> {
    let logical_bytes = store.estimated_logical_record_bytes();
    if logical_bytes > options.max_source_logical_bytes {
        return Err(HawDBError::Storage(format!(
            "derived repair source byte admission rejected {logical_bytes} bytes under the {} byte limit",
            options.max_source_logical_bytes
        )));
    }
    let mut node_count = 0u64;
    for node in store.node_records_owned() {
        node.map_err(|error| HawDBError::StorageIntegrity(error.to_string()))?;
        node_count = node_count.saturating_add(1);
        if node_count > options.max_source_records {
            return Err(HawDBError::Storage(format!(
                "derived repair source record admission exceeded {} records",
                options.max_source_records
            )));
        }
    }
    let mut relationship_count = 0u64;
    for relationship in store.relationship_records_owned() {
        relationship.map_err(|error| HawDBError::StorageIntegrity(error.to_string()))?;
        relationship_count = relationship_count.saturating_add(1);
        if node_count.saturating_add(relationship_count) > options.max_source_records {
            return Err(HawDBError::Storage(format!(
                "derived repair source record admission exceeded {} records",
                options.max_source_records
            )));
        }
    }
    Ok((node_count, relationship_count, logical_bytes))
}

fn assess_artifacts(
    path: &Path,
    store: &GraphStore,
    manifest: DurableManifest,
    options: DerivedArtifactRebuildOptions,
) -> Result<Vec<DerivedArtifactHealth>> {
    if manifest.checkpoint_generation.is_none() {
        return Ok(Vec::new());
    }
    let canonical_relationship_count = store
        .canonical_base
        .as_ref()
        .map(|reader| reader.manifest().relationship_count)
        .unwrap_or_default();
    Ok(vec![
        assess_one(DerivedArtifactKind::CanonicalAdjacency, || {
            validate_branch_derived_objects(store, DerivedArtifactKind::CanonicalAdjacency)?;
            let cache = Arc::new(SegmentCache::new(options.segment_cache_capacity_bytes));
            let mut open_budget =
                GraphManifestOpenBudget::new(options.max_graph_manifest_open_bytes);
            let reader = load_published_canonical_adjacency(
                path,
                manifest,
                cache,
                store_id_for_path(path)?,
                &mut open_budget,
            )?
            .ok_or_else(|| {
                HawDBError::Storage("canonical adjacency publication is missing".to_string())
            })?;
            if reader.relationship_count() != canonical_relationship_count {
                return Err(HawDBError::Storage(
                    "canonical adjacency relationship count mismatch".to_string(),
                ));
            }
            reader
                .deep_scrub()
                .map(|_| ())
                .map_err(|error| HawDBError::StorageIntegrity(error.to_string()))
        })?,
        assess_one(DerivedArtifactKind::PersistentPropertyProjection, || {
            validate_branch_derived_objects(
                store,
                DerivedArtifactKind::PersistentPropertyProjection,
            )?;
            let cache = Arc::new(SegmentCache::new(options.segment_cache_capacity_bytes));
            let mut open_budget =
                GraphManifestOpenBudget::new(options.max_graph_manifest_open_bytes);
            let reader = load_published_property_projection(
                path,
                manifest,
                cache,
                store_id_for_path(path)?,
                &mut open_budget,
            )?
            .ok_or_else(|| {
                HawDBError::Storage(
                    "persistent property projection publication is missing".to_string(),
                )
            })?;
            reader
                .deep_scrub()
                .map(|_| ())
                .map_err(|error| HawDBError::StorageIntegrity(error.to_string()))
        })?,
    ])
}

pub(super) fn rebuildable_artifact_kind(
    path: &str,
    generation: u64,
) -> Option<DerivedArtifactKind> {
    [
        DerivedArtifactKind::CanonicalAdjacency,
        DerivedArtifactKind::PersistentPropertyProjection,
    ]
    .into_iter()
    .find(|kind| {
        audit::target_files(*kind, generation)
            .iter()
            .any(|name| name == path)
    })
}

fn validate_branch_derived_objects(store: &GraphStore, kind: DerivedArtifactKind) -> Result<()> {
    let durable = store.durable.as_ref().expect("repair source is durable");
    let Some(branch) = &durable.branch_runtime else {
        return Ok(());
    };
    let objects =
        crate::immutable_object::ImmutableObjectStore::open(branch.immutable_store_root())
            .map_err(HawDBError::from_storage_error)?;
    let references = branch
        .root()
        .checkpoint_bindings
        .iter()
        .filter(|binding| {
            rebuildable_artifact_kind(&binding.relative_path, durable.checkpoint_epoch)
                == Some(kind)
        })
        .map(|binding| binding.reference)
        .collect::<std::collections::BTreeSet<_>>();
    for reference in references {
        objects
            .read(reference)
            .map_err(HawDBError::from_storage_error)?;
    }
    Ok(())
}

fn assess_one(
    kind: DerivedArtifactKind,
    check: impl FnOnce() -> Result<()>,
) -> Result<DerivedArtifactHealth> {
    match check() {
        Ok(()) => Ok(DerivedArtifactHealth {
            kind,
            state: DerivedArtifactHealthState::Healthy,
            reason_code: None,
        }),
        Err(error @ HawDBError::FileDescriptors(_)) => Err(error),
        Err(_) => Ok(DerivedArtifactHealth {
            kind,
            state: DerivedArtifactHealthState::RepairRequired,
            reason_code: Some("artifact_missing_corrupt_or_inconsistent".to_string()),
        }),
    }
}

fn plan_from_inspection(
    inspection: &DerivedInspection,
    options: DerivedArtifactRebuildOptions,
) -> Result<DerivedArtifactRepairPlan> {
    let targets = inspection
        .health
        .artifacts
        .iter()
        .filter(|artifact| artifact.state == DerivedArtifactHealthState::RepairRequired)
        .map(|artifact| artifact.kind)
        .collect::<Vec<_>>();
    if targets.is_empty() {
        return Err(HawDBError::Storage(
            "derived artifact doctor found no repair-required artifact".to_string(),
        ));
    }
    let estimated_temporary_bytes = inspection
        .health
        .source_logical_bytes
        .saturating_mul(8)
        .max(64 * 1024 * 1024);
    if estimated_temporary_bytes > options.max_temporary_bytes {
        return Err(HawDBError::Storage(format!(
            "derived repair temporary byte admission rejected {estimated_temporary_bytes} estimated bytes under the {} byte limit",
            options.max_temporary_bytes
        )));
    }
    let durable = inspection
        .store
        .durable
        .as_ref()
        .expect("repair source is durable");
    let manifest_identity = file_checksum(durable.manifest_path())?;
    let wal_identity = file_checksum(&durable.wal_path)?;
    let branch = durable
        .branch_runtime
        .as_ref()
        .map(|branch| {
            let head = file_checksum(branch.head_path())?;
            Ok::<_, HawDBError>(DerivedArtifactBranchSource {
                project_id: hawdb_core::Uuid::from_bytes(branch.head.project_id),
                branch_id: hawdb_core::Uuid::from_bytes(branch.head.branch_id),
                metadata_revision: branch.metadata_revision(),
                head_len: head.0,
                head_crc32c: head.1,
                head_sha256: head.2.to_string(),
            })
        })
        .transpose()?;
    let mut plan = DerivedArtifactRepairPlan {
        protocol: DERIVED_ARTIFACT_REPAIR_PROTOCOL.to_string(),
        plan_id: String::new(),
        branch,
        source_generation: inspection.manifest.checkpoint_epoch,
        target_generation: durable.next_checkpoint_generation()?,
        source_commit_epoch: inspection.store.commit_epoch,
        manifest_len: manifest_identity.0,
        manifest_crc32c: manifest_identity.1,
        manifest_sha256: manifest_identity.2.to_string(),
        wal_len: wal_identity.0,
        wal_crc32c: wal_identity.1,
        wal_sha256: wal_identity.2.to_string(),
        source_node_count: inspection.health.source_node_count,
        source_relationship_count: inspection.health.source_relationship_count,
        source_logical_bytes: inspection.health.source_logical_bytes,
        estimated_temporary_bytes,
        targets,
        options,
    };
    plan.plan_id = plan_identity(&plan);
    Ok(plan)
}

pub(crate) use hawdb_storage::derived_repair::build_config;

fn validate_source_identity(path: &Path, plan: &DerivedArtifactRepairPlan) -> Result<()> {
    let (manifest_path, wal_path) = match &plan.branch {
        Some(source) => {
            let published = published_branch(path, source)?;
            let head = file_checksum(&path.join("branch.head"))?;
            if head.0 != source.head_len
                || head.1 != source.head_crc32c
                || head.2.to_string() != source.head_sha256
            {
                return Err(HawDBError::Storage(
                    "derived artifact repair branch head changed after planning".into(),
                ));
            }
            (published.manifest_path, published.wal_path)
        }
        None => {
            let manifest_path = path.join(MANIFEST_FILE);
            let manifest = DurableManifest::load(&manifest_path)?;
            (manifest_path, manifest.wal_path(path))
        }
    };
    let manifest = file_checksum(&manifest_path)?;
    let wal = file_checksum(&wal_path)?;
    if manifest.0 != plan.manifest_len
        || manifest.1 != plan.manifest_crc32c
        || manifest.2.to_string() != plan.manifest_sha256
        || wal.0 != plan.wal_len
        || wal.1 != plan.wal_crc32c
        || wal.2.to_string() != plan.wal_sha256
    {
        return Err(HawDBError::Storage(
            "derived artifact repair source identity changed after planning".to_string(),
        ));
    }
    Ok(())
}

struct PublishedRepairBranch {
    head: crate::branch_head::BranchHead,
    root: crate::sealed_root::SealedRoot,
    objects: crate::immutable_object::ImmutableObjectStore,
    manifest_path: PathBuf,
    wal_path: PathBuf,
}

/// Resolves only the durable selector and its metadata objects. Disposable
/// runtime manifests never decide whether a branch repair was committed.
fn published_branch(
    path: &Path,
    source: &DerivedArtifactBranchSource,
) -> Result<PublishedRepairBranch> {
    let id = source.branch_id.to_string();
    let branches = path
        .parent()
        .filter(|parent| {
            parent.file_name().is_some_and(|name| name == "branches")
                && path.file_name().is_some_and(|name| name == id.as_str())
        })
        .ok_or_else(|| {
            HawDBError::StorageIntegrity("repair path does not identify its branch UUID".into())
        })?;
    let catalog = crate::branch_catalog::read_catalog(&branches.join("catalog.hawdb"))?;
    let record = catalog
        .branches
        .iter()
        .find(|record| record.id.as_uuid() == source.branch_id)
        .ok_or_else(|| {
            HawDBError::StorageIntegrity("repair branch is absent from the catalog".into())
        })?;
    if catalog.project_id.as_uuid() != source.project_id
        || record.metadata_revision != source.metadata_revision
        || record.state != crate::branch_catalog::BranchState::Ready
    {
        return Err(HawDBError::StorageIntegrity(
            "repair branch catalog identity changed".into(),
        ));
    }
    let head = crate::branch_head::read_branch_head(&path.join("branch.head"))
        .map_err(HawDBError::from_storage_error)?;
    if head.project_id != *source.project_id.as_bytes()
        || head.branch_id != *source.branch_id.as_bytes()
    {
        return Err(HawDBError::StorageIntegrity(
            "repair branch head identity changed".into(),
        ));
    }
    let objects = crate::immutable_object::ImmutableObjectStore::open(branches.join("objects"))
        .map_err(HawDBError::from_storage_error)?;
    let root = crate::sealed_root::SealedRoot::decode(
        &objects
            .read(head.sealed_root)
            .map_err(HawDBError::from_storage_error)?,
    )
    .map_err(HawDBError::from_storage_error)?;
    if root.commit_epoch != head.logical_commit_epoch
        || root.replay_end_lsn() != head.active_wal.replay_start_lsn
    {
        return Err(HawDBError::StorageIntegrity(
            "repair branch root does not match its head".into(),
        ));
    }
    objects
        .read(root.durable_manifest)
        .map_err(HawDBError::from_storage_error)?;
    let manifest_path = objects.object_path(root.durable_manifest);
    let wal_path = path.join(crate::artifact_files::wal_generation_file(
        head.active_wal.generation,
    ));
    Ok(PublishedRepairBranch {
        head,
        root,
        objects,
        manifest_path,
        wal_path,
    })
}