hawdb 0.33.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
599
600
601
602
603
604
605
606
607
// 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.

//! One admitted branch bundle, with recoverable deferred admission.
//!
//! A pending project contains identities/configuration and its shared FD domain.
//! It contains no empty dataset that could accidentally answer a data query.
//! Failed admission is never cached as success, and only a fully validated
//! candidate enters the cell. Readers keep the original runtime's snapshot pins.

use super::{
    automatic_checkpoint::{Control, Owner, State, Suspension},
    branch_lifecycle::{BranchLifecycleError, BranchSelection},
    configure_relational_fast_paths, configure_search_projection_changefeed,
    search_projection_consumer, CascadesOptimizer, Catalog, Database, DatabaseConfig,
    DatabaseInstanceIdentity, DurabilityPolicy, GraphStore, LocalQosScheduler,
    OptimizerPlanningCache, PlanCache, ReaderPins, SharedState, TelemetrySink,
};
use crate::error::{HawDBError, Result};
use crate::store::LiveHeadroomCommit;
use hawdb_storage::branch_project::CapturedProject;
use hawdb_storage::file_descriptors::{FileDescriptorMetrics, FileOpenContext};
use hawdb_storage::relational::RelationalLiveOverlayShortfall;
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex, MutexGuard, OnceLock};

mod guards;
pub(super) use guards::SharedBranchRuntime;
use guards::{RuntimeRead, RuntimeWrite};

#[derive(Debug)]
pub(super) struct AdmittedBranchRuntime {
    pub(super) catalog: Catalog,
    pub(super) store: GraphStore,
    pub(super) branch_selection: Option<BranchSelection>,
    pub(super) optimizer: CascadesOptimizer,
    pub(super) plan_cache: Arc<SharedState<PlanCache>>,
    pub(super) relational_plan_template_cache:
        Arc<crate::relational_sql::RelationalPlanTemplateCache>,
    pub(super) optimizer_planning_cache: Arc<SharedState<OptimizerPlanningCache>>,
    pub(super) reader_pins: Arc<Mutex<ReaderPins>>,
    pub(super) projection_consumers: search_projection_consumer::ConsumerRegistry,
}

#[derive(Debug)]
pub(super) struct DeferredBranchAdmission {
    pub(super) config: DatabaseConfig,
    pub(super) durability: DurabilityPolicy,
    pub(super) scheduler: LocalQosScheduler,
    pub(super) telemetry: Option<Arc<dyn TelemetrySink>>,
    pub(super) governor: Option<hawdb_qos::RuntimeGovernor>,
}

#[derive(Debug)]
pub(super) struct BranchRuntimeCell {
    admitted: Arc<SharedBranchRuntime>,
    pending: Option<DeferredBranchAdmission>,
    project: Option<CapturedProject>,
    admission: Mutex<()>,
    instance_identity: DatabaseInstanceIdentity,
    // Mutations and background selector publication share this barrier.
    // Reads borrow the stable frontend and only try to adopt a ready handoff.
    publication: Arc<Control>,
    automatic: OnceLock<Option<Owner>>,
    automatic_start: Mutex<()>,
}

#[derive(Debug)]
pub(super) struct AdmittedBranchRuntimeMut<'a> {
    runtime: RuntimeWrite<'a>,
    publication: Option<MutexGuard<'a, State>>,
    control: &'a Control,
}

impl Drop for AdmittedBranchRuntimeMut<'_> {
    fn drop(&mut self) {
        let retired = self.publication.take().and_then(|mut state| {
            self.control
                .submit(&mut state, &self.runtime.store, &self.runtime.catalog)
        });
        // The closure released its mutex guard. Captured-source destruction
        // must not extend the foreground publication critical section.
        drop(retired);
    }
}

impl Deref for AdmittedBranchRuntimeMut<'_> {
    type Target = AdmittedBranchRuntime;

    fn deref(&self) -> &Self::Target {
        &self.runtime
    }
}

impl DerefMut for AdmittedBranchRuntimeMut<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.runtime
    }
}

/// Read execution may update local caches, but callers cannot access mutable
/// live storage or the catalog without the mutation/publication guard.
#[derive(Debug)]
pub(super) struct AdmittedBranchRuntimeRead<'a> {
    runtime: RuntimeWrite<'a>,
}

impl AdmittedBranchRuntimeRead<'_> {
    pub(super) fn catalog(&self) -> &Catalog {
        &self.runtime.catalog
    }

    pub(super) fn store(&self) -> &GraphStore {
        &self.runtime.store
    }

    pub(super) fn execute(
        &mut self,
        request: crate::executor::ExecutionRequest<'_>,
        external: &mut dyn crate::executor::ExternalReadOperator,
    ) -> Result<crate::executor::ProfiledQueryRows> {
        if crate::executor::requires_write_access(request.plan()) {
            return Err(HawDBError::Execution(
                "read runtime access does not permit storage writes".into(),
            ));
        }
        let runtime = &mut *self.runtime;
        crate::executor::execute_with_request(
            request,
            crate::executor::ExecutionResources::new(
                &mut runtime.catalog,
                &mut runtime.store,
                external,
            ),
        )
    }
}

impl BranchRuntimeCell {
    pub(super) fn admitted(runtime: AdmittedBranchRuntime) -> Self {
        let admitted = Arc::new(SharedBranchRuntime::new(Some(runtime)));
        let publication = Arc::new(Control::default());
        publication.attach_frontend(&admitted);
        Self {
            admitted,
            pending: None,
            project: None,
            admission: Mutex::new(()),
            publication,
            automatic: OnceLock::new(),
            automatic_start: Mutex::new(()),
            instance_identity: DatabaseInstanceIdentity::new(),
        }
    }

    pub(super) fn deferred(pending: DeferredBranchAdmission, project: CapturedProject) -> Self {
        let admitted = Arc::new(SharedBranchRuntime::default());
        let publication = Arc::new(Control::default());
        publication.attach_frontend(&admitted);
        Self {
            admitted,
            pending: Some(pending),
            project: Some(project),
            admission: Mutex::new(()),
            publication,
            automatic: OnceLock::new(),
            automatic_start: Mutex::new(()),
            instance_identity: DatabaseInstanceIdentity::new(),
        }
    }

    pub(super) fn instance_identity(&self) -> DatabaseInstanceIdentity {
        self.instance_identity.clone()
    }

    pub(super) fn get(&self) -> Result<RuntimeRead<'_>> {
        self.publication.ensure_healthy()?;
        if self.admitted.read(&self.publication)?.is_some() {
            if let Some(pending) = &self.pending {
                self.arm_automatic(
                    &pending.config,
                    pending.scheduler.clone(),
                    pending.governor.clone(),
                )?;
            }
            return self.admitted.read(&self.publication)?.ok_or_else(|| {
                HawDBError::StorageIntegrity("completed branch admission has no runtime".into())
            });
        }
        let admission = self.admission.lock().map_err(|_| {
            HawDBError::StorageIntegrity("deferred branch admission lock is poisoned".into())
        })?;
        if let Some(runtime) = self.admitted.read(&self.publication)? {
            return Ok(runtime);
        }
        let pending = self.pending.as_ref().ok_or_else(|| {
            HawDBError::StorageIntegrity("database has no branch admission identity".into())
        })?;
        let project = self.project.as_ref().ok_or_else(|| {
            HawDBError::StorageIntegrity("database has no captured project".into())
        })?;
        let candidate = recover_default_branch(pending, project)?;
        self.admitted.initialize(candidate)?;
        drop(admission);
        self.arm_automatic(
            &pending.config,
            pending.scheduler.clone(),
            pending.governor.clone(),
        )?;
        let runtime = self.admitted.read(&self.publication)?.ok_or_else(|| {
            HawDBError::StorageIntegrity("completed branch admission has no runtime".into())
        })?;
        if let Some(telemetry) = &pending.telemetry {
            record_recovery_telemetry(&runtime.store, telemetry.as_ref());
        }
        Ok(runtime)
    }

    pub(super) fn get_mut(&mut self) -> Result<AdmittedBranchRuntimeMut<'_>> {
        self.get()?;
        let mut publication =
            Self::publication_barrier(&self.publication, self.needs_publication_barrier())?;
        let mut runtime = self.admitted.write(&self.publication)?;
        if let Some(state) = &mut publication {
            self.publication.adopt(state, &mut runtime.store)?;
        }
        Ok(AdmittedBranchRuntimeMut {
            runtime,
            publication,
            control: &self.publication,
        })
    }

    /// Like [`Self::get_mut`], but first waits for the automatic owner to
    /// drain a live overlay that cannot hold `demand` beside what it retains.
    fn get_mut_for_headroom(
        &mut self,
        demand: Option<RelationalLiveOverlayShortfall>,
    ) -> Result<AdmittedBranchRuntimeMut<'_>> {
        self.get()?;
        let mut publication = if self.needs_publication_barrier() {
            Some(self.publication.lock_frontend_for_headroom(demand)?)
        } else {
            None
        };
        let mut runtime = self.admitted.write(&self.publication)?;
        if let Some(state) = &mut publication {
            self.publication.adopt(state, &mut runtime.store)?;
        }
        Ok(AdmittedBranchRuntimeMut {
            runtime,
            publication,
            control: &self.publication,
        })
    }

    /// Commits `inputs` through `commit`. A commit that fits an empty
    /// relational live overlay but not its remaining headroom is rejected
    /// before WAL append with nothing applied; it releases the runtime and
    /// the publication barrier, declares its size, waits there for the
    /// automatic owner's drain, then commits the same inputs again (#1479).
    ///
    /// The wait reuses the bounded live-overlay capacity waits. It ends with
    /// the original rejection when no owner can drain (no background
    /// maintenance, a suspended, stopped or failed owner), and with the
    /// retryable `BranchBusy` when the drain stops progressing or a wait
    /// completed no checkpoint while the owner can still drain (a deferred or
    /// failed attempt). An exhausted operation circuit can still drain: a
    /// later commit that needs headroom re-arms it (#1618).
    /// `task_context` is checked before the wait and again after it, once the
    /// drained generation is adopted and immediately before the retry, so a
    /// task cancelled or past its deadline while waiting applies nothing.
    pub(super) fn commit_with_live_headroom<I>(
        &mut self,
        mut inputs: I,
        task_context: Option<&hawdb_core::RuntimeTaskContext>,
        mut commit: impl FnMut(&mut AdmittedBranchRuntime, I) -> Result<LiveHeadroomCommit<I>>,
    ) -> Result<crate::store::MutationSummary> {
        let mut demand = None;
        let mut observed_checkpoints = None;
        loop {
            let outcome = {
                let mut access = self.get_mut_for_headroom(demand)?;
                if demand.is_some() {
                    // The drain wait can outlast the caller's cancellation or
                    // deadline. Check again after adopting the drained
                    // generation, immediately before the retry appends WAL;
                    // a stopped task drops its inputs unapplied, and dropping
                    // `access` releases the runtime and publication barrier.
                    super::query_runtime::query_runtime_checkpoint(task_context)?;
                }
                commit(&mut access, inputs)
            }?;
            match outcome {
                LiveHeadroomCommit::Committed(summary) => return Ok(summary),
                LiveHeadroomCommit::NeedsHeadroom {
                    shortfall,
                    inputs: retained,
                    error,
                } => {
                    let completed = self.publication.completed_checkpoints();
                    if !self.needs_publication_barrier()
                        || !self.publication.can_drain_for_headroom()
                    {
                        return Err(error);
                    }
                    if observed_checkpoints == Some(completed) {
                        // The owner can still drain, but this wait completed
                        // no checkpoint (a deferred or failed attempt). A
                        // later retry can fit, so report the retryable busy
                        // result; nothing was applied (#1637).
                        return Err(HawDBError::BranchBusy {
                            resource: "automatic checkpoint capacity",
                        });
                    }
                    super::query_runtime::query_runtime_checkpoint(task_context)?;
                    observed_checkpoints = Some(completed);
                    demand = Some(shortfall);
                    inputs = retained;
                }
            }
        }
    }

    pub(super) fn get_read(&mut self) -> Result<AdmittedBranchRuntimeRead<'_>> {
        self.get()?;
        let mut runtime = self.admitted.write(&self.publication)?;
        self.publication.adopt_for_read(&mut runtime.store)?;
        // &mut self exclusively owns the frontend. Read execution may update
        // its local caches without holding the owner's publication mutex.
        Ok(AdmittedBranchRuntimeRead { runtime })
    }

    pub(super) fn peek(&self) -> Option<RuntimeRead<'_>> {
        self.admitted.read(&self.publication).ok().flatten()
    }

    pub(super) fn peek_mut(&mut self) -> Option<AdmittedBranchRuntimeMut<'_>> {
        let mut publication =
            Self::publication_barrier(&self.publication, self.needs_publication_barrier()).ok()?;
        let mut runtime = self.admitted.write(&self.publication).ok()?;
        if let Some(state) = &mut publication {
            self.publication.adopt(state, &mut runtime.store).ok()?;
        }
        Some(AdmittedBranchRuntimeMut {
            runtime,
            publication,
            control: &self.publication,
        })
    }

    fn needs_publication_barrier(&self) -> bool {
        cfg!(all(
            feature = "background-maintenance",
            not(target_arch = "wasm32")
        )) && self
            .peek()
            .is_some_and(|runtime| runtime.store.storage_recovery_report().durable)
    }

    fn publication_barrier(
        publication: &Control,
        needed: bool,
    ) -> Result<Option<MutexGuard<'_, State>>> {
        if !needed {
            return Ok(None);
        }
        publication.lock_frontend().map(Some)
    }

    pub(super) fn arm_automatic(
        &self,
        config: &DatabaseConfig,
        scheduler: LocalQosScheduler,
        governor: Option<hawdb_qos::RuntimeGovernor>,
    ) -> Result<()> {
        if self.automatic.get().is_some() {
            return Ok(());
        }
        let _start = self.automatic_start.lock().map_err(|_| {
            HawDBError::StorageIntegrity("checkpoint startup lock is poisoned".into())
        })?;
        if self.automatic.get().is_some() {
            return Ok(());
        }
        let runtime = self.peek().ok_or_else(|| {
            HawDBError::StorageIntegrity(
                "checkpoint owner requires completed branch admission".into(),
            )
        })?;
        let owner = Owner::start(
            Arc::clone(&self.publication),
            &runtime.store,
            &runtime.catalog,
            Arc::clone(&runtime.reader_pins),
            config,
            scheduler,
            governor,
        )?;
        self.automatic
            .set(owner)
            .map_err(|_| HawDBError::StorageIntegrity("checkpoint owner started twice".into()))
    }

    pub(super) fn checkpoint_control(&self) -> Arc<Control> {
        Arc::clone(&self.publication)
    }

    #[cfg(all(test, feature = "background-maintenance", not(target_arch = "wasm32")))]
    pub(super) fn checkpoint_control_for_test(&self) -> Arc<Control> {
        self.checkpoint_control()
    }

    #[cfg(all(test, feature = "background-maintenance", not(target_arch = "wasm32")))]
    pub(super) fn shared_runtime_for_test(&self) -> Arc<SharedBranchRuntime> {
        Arc::clone(&self.admitted)
    }

    pub(super) fn automatic_checkpoint_report(
        &self,
    ) -> Result<Option<super::AutomaticCheckpointReport>> {
        self.publication.report()
    }

    pub(super) fn suspend_automatic_checkpoint(&mut self) -> Result<Option<Suspension>> {
        self.get()?;
        if self.automatic.get().is_none_or(Option::is_none) {
            return Ok(None);
        }
        let suspension = self.publication.suspend()?;
        // Selection can have completed while suspension was requested. Adopt
        // it before waiting for the worker's off-gate retirement to finish.
        drop(self.get_mut()?);
        suspension.wait_idle()?;
        Ok(Some(suspension))
    }

    pub(super) fn set_automatic_governor(
        &self,
        governor: hawdb_qos::RuntimeGovernor,
    ) -> Result<()> {
        self.publication.set_governor(governor)
    }

    pub(super) fn release_suspended_checkpoint_source(&self) -> Result<()> {
        self.publication.release_suspended_source()
    }

    pub(super) fn captured_project(&self) -> Option<&CapturedProject> {
        self.project.as_ref()
    }

    pub(super) fn retain_project_capture(&mut self, project: Option<CapturedProject>) {
        self.project = project;
    }

    pub(super) fn pending_mut(&mut self) -> Option<&mut DeferredBranchAdmission> {
        self.pending.as_mut()
    }

    pub(super) fn file_descriptor_metrics(&self) -> Option<FileDescriptorMetrics> {
        self.peek()
            .and_then(|runtime| runtime.store.file_descriptor_metrics())
            .or_else(|| {
                self.project
                    .as_ref()
                    .map(|project| project.file_descriptors().metrics())
            })
    }

    pub(super) fn file_descriptor_context(&self) -> Option<FileOpenContext> {
        self.peek()
            .and_then(|runtime| runtime.store.file_descriptor_context())
            .or_else(|| {
                self.project
                    .as_ref()
                    .map(|project| project.file_descriptors().io_context())
            })
    }

    pub(super) fn reserve_target_admission_resources(
        &self,
    ) -> Result<hawdb_storage::file_descriptors::DescriptorReservation> {
        if let Some(runtime) = self.peek() {
            return runtime.store.reserve_branch_admission_resources();
        }
        let project = self.project.as_ref().ok_or_else(|| {
            HawDBError::StorageIntegrity("database has no project admission domain".into())
        })?;
        project.require_original_namespace()?;
        GraphStore::reserve_project_branch_admission_resources(project.file_descriptors())
    }

    pub(super) fn into_admitted(mut self) -> Result<AdmittedBranchRuntime> {
        if let Some(Some(owner)) = self.automatic.get_mut() {
            owner.stop();
        }
        if self.peek().is_some() {
            drop(self.get_mut()?);
        }
        self.admitted
            .take()?
            .map(|runtime| *runtime)
            .ok_or_else(|| {
                HawDBError::StorageIntegrity("candidate branch runtime was not admitted".into())
            })
    }
}

impl Drop for BranchRuntimeCell {
    fn drop(&mut self) {
        if let Some(Some(owner)) = self.automatic.get_mut() {
            owner.stop();
        }
    }
}

pub(super) fn record_recovery_telemetry(store: &GraphStore, telemetry: &dyn TelemetrySink) {
    let recovery = store.storage_recovery_report();
    if recovery.durable {
        telemetry.record_kernel(super::KernelTelemetry {
            operation: super::KernelTelemetryOperation::Recovery,
            success: true,
            elapsed_micros: 0,
            item_count: recovery.replayed_wal_entries,
            byte_count: recovery.replayed_wal_bytes,
            fsync_micros: 0,
            generation: recovery.wal_generation,
        });
    }
}

fn recover_default_branch(
    pending: &DeferredBranchAdmission,
    project: &CapturedProject,
) -> Result<AdmittedBranchRuntime> {
    let _descriptor_operation = project
        .file_descriptors()
        .io_context()
        .internal_operation()?;
    let resources =
        GraphStore::reserve_project_branch_admission_resources(project.file_descriptors())?;
    project.require_original_namespace()?;
    let metadata = project.metadata()?;
    let record = metadata.main().clone();
    drop(metadata);
    let root = project.file_descriptors().root();
    let admitted =
        GraphStore::admit_captured_branch(hawdb_storage::store::CapturedBranchAdmissionRequest {
            project,
            branch_id: record.id,
            expected_metadata_revision: record.metadata_revision,
            durability: pending.durability,
            replay_config: pending.config.wal_replay_config(),
            read_only: pending.config.read_only,
        })
        .map_err(|error| HawDBError::from(BranchLifecycleError::Admission(error)))?;
    let (mut store, schema) = admitted.into_parts();
    // Branch admission has finished and its native owners are retained by the
    // store. Release the admission IO wave before recovery can require its own
    // checkpoint publication reservation.
    drop(resources);
    configure_search_projection_changefeed(&mut store, &pending.config);
    configure_relational_fast_paths(&mut store, &pending.config);
    let mut candidate = Database::new_with_config(pending.config.clone());
    {
        let mut runtime_access = candidate.runtime.get_mut()?;
        let runtime = &mut *runtime_access;
        runtime.store = store;
        runtime.catalog = schema;
        runtime.branch_selection = Some(BranchSelection { record });
    }
    candidate.project_root_path = Some(root.to_path_buf());
    candidate.durability = pending.durability;
    candidate.local_qos_scheduler = pending.scheduler.clone();
    if let Some(governor) = &pending.governor {
        candidate.set_runtime_governor(governor.clone());
    }
    if !pending.config.read_only {
        candidate.complete_required_relational_row_checkpoint("default branch recovery")?;
    }
    candidate.apply_engine_system_schema()?;
    {
        let mut runtime_access = candidate.runtime.get_mut()?;
        let runtime = &mut *runtime_access;
        runtime.projection_consumers = search_projection_consumer::ConsumerRegistry::load(
            runtime.store.search_projection_registry_root(),
            runtime.store.search_projection_database_identity(),
        );
    }
    crate::store::StoreTelemetry::set_telemetry_sink(
        &mut candidate.runtime.get_mut()?.store,
        pending.telemetry.clone(),
    );
    candidate.runtime.into_admitted()
}