hawdb 0.41.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
// 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 hawdb::{
    EmbeddedQueryError, HawDBEmbedded, HawDBEmbeddedOpenOptions, IoConcurrencyBudget,
    ProcessMemoryCapabilities, ProcessMemoryPolicy, ProcessMemoryPolicyConfig,
    ProcessMemorySnapshot, RuntimeAdmissionCode, RuntimeGovernor, RuntimeGovernorConfig,
    RuntimeMemorySnapshot, RuntimeResourceBudget, RuntimeResourceSnapshot, RuntimeWorkRequest,
    Value,
};
use std::num::{NonZeroU64, NonZeroUsize};
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

const MIB: u64 = 1024 * 1024;
static NEXT_PATH: AtomicU64 = AtomicU64::new(0);

struct TestDirectory(PathBuf);

impl TestDirectory {
    fn new() -> Self {
        Self(std::env::temp_dir().join(format!(
            "hawdb-process-memory-{}-{}",
            std::process::id(),
            NEXT_PATH.fetch_add(1, Ordering::Relaxed),
        )))
    }

    fn options(&self) -> HawDBEmbeddedOpenOptions {
        let mut config = hawdb::DatabaseConfig {
            max_wal_record_bytes: Some(1024 * 1024),
            max_read_result_rows: Some(8),
            max_read_result_payload_bytes: Some(64 * 1024),
            ..hawdb::DatabaseConfig::default()
        };
        config.mutation_limits.max_affected_rows = NonZeroUsize::new(8).unwrap();
        config.mutation_limits.max_operations = NonZeroUsize::new(8).unwrap();
        config.mutation_limits.max_result_rows = NonZeroUsize::new(8).unwrap();
        config.mutation_limits.max_result_payload_bytes = NonZeroUsize::new(64 * 1024).unwrap();
        HawDBEmbeddedOpenOptions::new(self.0.join("graph"))
            .with_config(config)
            .with_resource_snapshot(RuntimeResourceSnapshot::from_parts(
                RuntimeResourceBudget::from_limits(NonZeroUsize::new(2).unwrap(), None, None),
                RuntimeMemorySnapshot::from_limits(
                    Some(512 * MIB),
                    Some(512 * MIB),
                    None,
                    None,
                    None,
                ),
            ))
            .with_runtime_governor_config(RuntimeGovernorConfig {
                memory_budget_bytes: Some(64 * MIB),
                result_budget_bytes: 64 * 1024,
                ..RuntimeGovernorConfig::shared_host()
            })
    }
}

impl Drop for TestDirectory {
    fn drop(&mut self) {
        if self.0.exists() {
            std::fs::remove_dir_all(&self.0).unwrap();
        }
    }
}

fn policy() -> ProcessMemoryPolicy {
    let policy = ProcessMemoryPolicy::new(
        ProcessMemoryPolicyConfig::new(NonZeroU64::new(64 * MIB).unwrap())
            .with_recovery_headroom(8 * MIB)
            .with_sample_max_age(Duration::from_secs(60)),
    );
    policy.update(sample(16 * MIB));
    policy
}

fn resources() -> RuntimeResourceSnapshot {
    RuntimeResourceSnapshot::from_parts(
        RuntimeResourceBudget::from_limits(NonZeroUsize::MIN, None, None),
        RuntimeMemorySnapshot::from_limits(Some(1_000), Some(1_000), None, None, None),
    )
}

fn sample(resident_bytes: u64) -> ProcessMemorySnapshot {
    ProcessMemorySnapshot {
        capabilities: ProcessMemoryCapabilities {
            resident_memory: true,
            total_page_faults: false,
            split_page_faults: false,
        },
        resident_bytes,
        peak_resident_bytes: resident_bytes,
        total_page_faults: None,
        minor_page_faults: None,
        major_page_faults: None,
    }
}

#[test]
fn public_policy_coordinates_rss_headroom_across_embedded_governors() {
    let policy = ProcessMemoryPolicy::new(ProcessMemoryPolicyConfig::new(
        NonZeroU64::new(100).unwrap(),
    ));
    policy.update(sample(20));
    let first = RuntimeGovernor::new_with_process_memory_policy(
        RuntimeGovernorConfig::shared_host(),
        resources(),
        IoConcurrencyBudget::new(1, 1),
        policy.clone(),
    );
    let second = RuntimeGovernor::new_with_process_memory_policy(
        RuntimeGovernorConfig::shared_host(),
        resources(),
        IoConcurrencyBudget::new(1, 1),
        policy,
    );

    let permit = first
        .try_admit(RuntimeWorkRequest::foreground_query(60, 0).with_blocking(false))
        .unwrap();
    let error = second
        .try_admit(RuntimeWorkRequest::foreground_query(30, 0).with_blocking(false))
        .unwrap_err();
    assert_eq!(error.code, RuntimeAdmissionCode::MemorySaturated);
    assert!(error.is_retryable());

    drop(permit);
    assert!(second
        .try_admit(RuntimeWorkRequest::foreground_query(30, 0).with_blocking(false))
        .is_ok());
}

#[test]
fn embedded_queries_reject_rss_pressure_before_mutation_and_recover_with_hysteresis() {
    let directory = TestDirectory::new();
    let policy = policy();
    let mut embedded =
        HawDBEmbedded::open_with_process_memory_policy(directory.options(), policy.clone())
            .unwrap();
    embedded
        .query_admitted("CREATE (:Probe {value: 1})")
        .unwrap();
    let resources = embedded.runtime_governor().snapshot();

    policy.update(sample(65 * MIB));
    let error = embedded
        .query_admitted("CREATE (:Probe {value: 2})")
        .unwrap_err();
    assert!(matches!(error, EmbeddedQueryError::Admission(error)
        if error.code == RuntimeAdmissionCode::MemorySaturated && error.is_retryable()));
    assert_eq!(policy.snapshot().available_bytes, Some(0));
    assert_eq!(
        embedded.runtime_governor().snapshot().limits,
        resources.limits
    );

    policy.update(sample(60 * MIB));
    assert!(policy.snapshot().admission_paused);
    assert!(matches!(
        embedded.query_admitted("MATCH ("),
        Err(EmbeddedQueryError::Admission(_))
    ));
    policy.update(sample(16 * MIB));
    assert!(!policy.snapshot().admission_paused);
    let rows = embedded
        .query_admitted("MATCH (p:Probe) RETURN p.value AS value")
        .unwrap()
        .rows;
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].get("value"), Some(&Value::Int(1)));
    assert_eq!(policy.snapshot().unobserved_reserved_bytes, 0);
}

#[test]
fn embedded_refresh_preserves_pinned_resources_and_host_owned_rss_samples() {
    let directory = TestDirectory::new();
    let policy = policy();
    let mut embedded =
        HawDBEmbedded::open_with_process_memory_policy(directory.options(), policy.clone())
            .unwrap();
    let initial = embedded.runtime_governor().snapshot();
    let policy_before = policy.snapshot();
    assert!(initial.resources_pinned);
    assert!(!embedded.refresh_runtime_resources());
    assert_eq!(
        embedded.runtime_governor().snapshot().resources,
        initial.resources
    );
    assert_eq!(
        embedded.runtime_resources().memory,
        initial.resources.memory
    );
    assert_eq!(policy.snapshot(), policy_before);

    let updated = RuntimeResourceSnapshot::from_parts(
        initial.resources.cpu,
        RuntimeMemorySnapshot::from_limits(Some(512 * MIB), Some(32 * MIB), None, None, None),
    );
    assert!(embedded.update_runtime_resources(updated));
    assert!(!embedded.refresh_runtime_resources());
    assert_eq!(embedded.runtime_governor().snapshot().resources, updated);
    assert!(embedded.runtime_governor().snapshot().resources_pinned);
    assert_eq!(policy.snapshot(), policy_before);
}

#[test]
fn embedded_resource_report_observes_shared_governor_updates_without_facade_refresh() {
    let directory = TestDirectory::new();
    let embedded = HawDBEmbedded::open_with_options(directory.options()).unwrap();
    let initial = embedded.runtime_resources();
    let updated = RuntimeResourceSnapshot::from_parts(initial.cpu, resources().memory);
    let shared = embedded.runtime_governor().clone();
    assert!(shared.update_resources(updated));
    assert_eq!(embedded.runtime_resources().memory, updated.memory);
    assert_eq!(embedded.runtime_resources().cpu, updated.cpu);
    assert_eq!(embedded.runtime_resources().storage_io, initial.storage_io);
    assert_eq!(
        embedded.runtime_resources().storage_device,
        initial.storage_device
    );
}

#[test]
fn embedded_instances_share_reservations_without_charging_observed_growth_twice() {
    let first_directory = TestDirectory::new();
    let second_directory = TestDirectory::new();
    let policy = policy();
    let first =
        HawDBEmbedded::open_with_process_memory_policy(first_directory.options(), policy.clone())
            .unwrap();
    let mut second =
        HawDBEmbedded::open_with_process_memory_policy(second_directory.options(), policy.clone())
            .unwrap();
    // Opening persistent handles may outlive the synthetic sample on a slow
    // host. This assertion starts with fresh feedback, as a real host supplies.
    policy.update(sample(16 * MIB));
    let governor = first.runtime_governor();
    let permit = governor
        .try_admit(
            RuntimeWorkRequest::new(
                hawdb::RuntimeWorkPriority::Foreground,
                hawdb::RuntimeWorkKind::Control,
            )
            .with_memory_bytes(48 * MIB),
        )
        .unwrap();
    assert!(matches!(
        second.query_admitted("CREATE (:Probe {value: 1})"),
        Err(EmbeddedQueryError::Admission(_))
    ));

    policy.update(sample(48 * MIB));
    assert_eq!(policy.snapshot().unobserved_reserved_bytes, 16 * MIB);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 48 * MIB);
    assert!(matches!(
        second.query_admitted("MATCH ("),
        Err(EmbeddedQueryError::Admission(_))
    ));
    drop(permit);
    assert_eq!(policy.snapshot().unobserved_reserved_bytes, 0);
    assert_eq!(policy.snapshot().sampled_resident_bytes, Some(48 * MIB));
    policy.update(sample(40 * MIB));
    assert!(second
        .query_admitted("MATCH (p:Probe) RETURN p.value AS value")
        .unwrap()
        .rows
        .is_empty());
}

#[test]
fn embedded_missing_unsupported_and_stale_samples_fail_closed() {
    for mode in 0..3 {
        let directory = TestDirectory::new();
        let policy = ProcessMemoryPolicy::new(
            ProcessMemoryPolicyConfig::new(NonZeroU64::new(64 * MIB).unwrap()).with_sample_max_age(
                if mode == 2 {
                    Duration::ZERO
                } else {
                    Duration::from_secs(60)
                },
            ),
        );
        if mode == 1 {
            policy.update(ProcessMemorySnapshot {
                capabilities: ProcessMemoryCapabilities::default(),
                ..sample(0)
            });
        } else if mode == 2 {
            policy.update(sample(0));
            while policy.snapshot().sample_is_current {
                std::thread::yield_now();
            }
        }
        let mut embedded =
            HawDBEmbedded::open_with_process_memory_policy(directory.options(), policy.clone())
                .unwrap();
        assert!(
            matches!(embedded.query_admitted("CREATE (:Probe {value: 1})"), Err(EmbeddedQueryError::Admission(error))
            if error.code == RuntimeAdmissionCode::MemoryPressure && error.is_retryable())
        );
        assert!(embedded
            .database_mut()
            .query("MATCH (p:Probe) RETURN p.value AS value")
            .unwrap()
            .rows
            .is_empty());
        assert_eq!(policy.snapshot().unobserved_reserved_bytes, 0);
    }
}

#[cfg(feature = "tokio-runtime")]
mod asynchronous {
    use super::*;
    use hawdb::{
        HawDBTokioEmbedded, HawDBTokioEmbeddedError, RuntimeCancellationReason,
        RuntimeCancellationToken, RuntimeTaskContext, TokioRuntimeConfig, TokioTaskError,
    };
    use std::time::Instant;

    fn config() -> TokioRuntimeConfig {
        TokioRuntimeConfig {
            resource_refresh_interval: Duration::ZERO,
            ..TokioRuntimeConfig::default()
        }
    }

    fn after_queued(
        governor: RuntimeGovernor,
        action: impl FnOnce() + Send + 'static,
    ) -> std::thread::JoinHandle<()> {
        std::thread::spawn(move || {
            let deadline = Instant::now() + Duration::from_secs(10);
            while governor.snapshot().queued_admission_waiters == 0 {
                assert!(
                    Instant::now() < deadline,
                    "query never queued for RSS admission"
                );
                std::thread::sleep(Duration::from_millis(1));
            }
            action();
        })
    }

    #[test]
    fn owned_and_borrowed_tokio_wait_for_host_samples_and_cancel_without_mutation() {
        let host_directory = TestDirectory::new();
        let borrowed_directory = TestDirectory::new();
        let policy = policy();
        let owned = HawDBTokioEmbedded::from_owned(
            HawDBEmbedded::open_with_process_memory_policy(
                host_directory.options(),
                policy.clone(),
            )
            .unwrap(),
            config(),
        )
        .unwrap();
        let borrowed = HawDBTokioEmbedded::from_borrowed(
            HawDBEmbedded::open_with_process_memory_policy(
                borrowed_directory.options(),
                policy.clone(),
            )
            .unwrap(),
            owned.runtime().handle().clone(),
            config(),
        );
        for embedded in [&owned, &borrowed] {
            policy.clear_sample();
            let governor = embedded.runtime().governor().clone();
            let cancellation = RuntimeCancellationToken::new();
            let cancel = cancellation.clone();
            let trigger = after_queued(governor.clone(), move || {
                cancel.cancel();
            });
            let result = owned
                .runtime()
                .block_on(embedded.query(
                    "CREATE (:Probe {value: 1})",
                    RuntimeTaskContext::new(
                        cancellation,
                        Some(Instant::now() + Duration::from_secs(10)),
                    ),
                ))
                .unwrap();
            trigger.join().unwrap();
            assert!(matches!(
                result,
                Err(HawDBTokioEmbeddedError::Task(TokioTaskError::Stopped(
                    RuntimeCancellationReason::Cancelled
                )))
            ));
            assert_eq!(governor.snapshot().queued_admission_waiters, 0);
            assert_eq!(governor.snapshot().admitted_memory_bytes, 0);

            policy.update(sample(65 * MIB));
            let recovery = policy.clone();
            let trigger = after_queued(governor.clone(), move || {
                recovery.update(sample(16 * MIB));
            });
            let rows = owned
                .runtime()
                .block_on(embedded.query(
                    "MATCH (p:Probe) RETURN p.value AS value",
                    RuntimeTaskContext::with_timeout(Duration::from_secs(10)),
                ))
                .unwrap()
                .unwrap()
                .rows;
            trigger.join().unwrap();
            assert!(rows.is_empty());
            assert_eq!(governor.snapshot().queued_admission_waiters, 0);
            assert_eq!(policy.snapshot().unobserved_reserved_bytes, 0);
            assert_eq!(
                governor.snapshot().resources,
                host_directory.options().resource_snapshot.unwrap()
            );
            assert!(!embedded.refresh_runtime_resources());
            assert_eq!(policy.snapshot().sampled_resident_bytes, Some(16 * MIB));
        }
    }

    #[test]
    #[cfg(any(target_os = "linux", target_os = "macos", windows))]
    fn native_rss_changes_embedded_admission_without_cgroups() {
        const TEST: &str = "asynchronous::native_rss_changes_embedded_admission_without_cgroups";
        const CHILD: &str = "HAWDB_TEST_PROCESS_MEMORY_CHILD";
        if std::env::var_os(CHILD).is_none() {
            let output = std::process::Command::new(std::env::current_exe().unwrap())
                .args([TEST, "--exact", "--nocapture"])
                .env(CHILD, "1")
                .output()
                .unwrap();
            assert!(
                output.status.success(),
                "native RSS child failed: {}{}",
                String::from_utf8_lossy(&output.stdout),
                String::from_utf8_lossy(&output.stderr)
            );
            assert!(String::from_utf8_lossy(&output.stdout).contains("hawdb-process-rss-native-v1"));
            print!("{}", String::from_utf8_lossy(&output.stdout));
            return;
        }
        let warm_directory = TestDirectory::new();
        {
            let mut warm = HawDBEmbedded::open_with_options(warm_directory.options()).unwrap();
            warm.query_admitted("CREATE (:Probe {value: 1})").unwrap();
            warm.query_admitted("MATCH (p:Probe) RETURN p.value AS value")
                .unwrap();
        }
        let baseline = ProcessMemorySnapshot::capture().unwrap();
        assert!(baseline.capabilities.resident_memory);
        let limit = baseline.resident_bytes + 32 * MIB;
        let policy = ProcessMemoryPolicy::from_current_process(
            ProcessMemoryPolicyConfig::new(NonZeroU64::new(limit).unwrap())
                .with_recovery_headroom(8 * MIB)
                .with_sample_max_age(Duration::from_secs(60)),
        )
        .unwrap();
        let sync_directory = TestDirectory::new();
        let tokio_directory = TestDirectory::new();
        let mut synchronous = HawDBEmbedded::open_with_process_memory_policy(
            sync_directory.options(),
            policy.clone(),
        )
        .unwrap();
        let asynchronous = HawDBTokioEmbedded::from_owned(
            HawDBEmbedded::open_with_process_memory_policy(
                tokio_directory.options(),
                policy.clone(),
            )
            .unwrap(),
            config(),
        )
        .unwrap();
        synchronous
            .query_admitted("CREATE (:Probe {value: 1})")
            .unwrap();
        asynchronous
            .runtime()
            .block_on(asynchronous.query(
                "CREATE (:Probe {value: 1})",
                RuntimeTaskContext::with_timeout(Duration::from_secs(10)),
            ))
            .unwrap()
            .unwrap();
        policy.refresh_from_host().unwrap();
        let before = policy.snapshot().sampled_resident_bytes.unwrap();
        assert!(before + 8 * MIB < limit, "insufficient baseline headroom");

        // An anonymous mapping makes release observable independently of allocator
        // caching. The production policy only observes RSS; it never reclaims memory.
        let mut allocation =
            memmap2::MmapMut::map_anon(usize::try_from(64 * MIB).unwrap()).unwrap();
        let mut random = 7_u64;
        for value in allocation.iter_mut() {
            random ^= random << 13;
            random ^= random >> 7;
            random ^= random << 17;
            *value = random as u8;
        }
        std::hint::black_box(&allocation);
        policy.refresh_from_host().unwrap();
        let elevated = policy.snapshot().sampled_resident_bytes.unwrap();
        assert!(
            elevated >= before + 16 * MIB,
            "bounded allocation did not raise current RSS"
        );
        assert!(policy.snapshot().admission_paused);
        assert!(
            matches!(synchronous.query_admitted("CREATE (:Probe {value: 2})"), Err(EmbeddedQueryError::Admission(error)) if error.is_retryable())
        );
        let rejected = asynchronous
            .runtime()
            .block_on(asynchronous.query(
                "CREATE (:Probe {value: 2})",
                RuntimeTaskContext::with_timeout(Duration::from_millis(100)),
            ))
            .unwrap();
        assert!(matches!(
            rejected,
            Err(HawDBTokioEmbeddedError::Task(TokioTaskError::Stopped(
                RuntimeCancellationReason::DeadlineExceeded
            )))
        ));

        drop(allocation);
        let deadline = Instant::now() + Duration::from_secs(30);
        loop {
            policy.refresh_from_host().unwrap();
            if !policy.snapshot().admission_paused {
                break;
            }
            assert!(
                Instant::now() < deadline,
                "native RSS did not recover after bounded allocation release: {:?}",
                policy.snapshot()
            );
            std::thread::sleep(Duration::from_millis(10));
        }
        let recovered = policy.snapshot().sampled_resident_bytes.unwrap();
        assert_eq!(
            synchronous
                .query_admitted("MATCH (p:Probe) RETURN p.value AS value")
                .unwrap()
                .rows
                .len(),
            1
        );
        assert_eq!(
            asynchronous
                .runtime()
                .block_on(asynchronous.query(
                    "MATCH (p:Probe) RETURN p.value AS value",
                    RuntimeTaskContext::with_timeout(Duration::from_secs(10)),
                ))
                .unwrap()
                .unwrap()
                .rows
                .len(),
            1
        );
        assert_eq!(policy.snapshot().unobserved_reserved_bytes, 0);
        println!("hawdb-process-rss-native-v1 os={} baseline_bytes={before} elevated_bytes={elevated} recovered_bytes={recovered} limit_bytes={limit} allocation_bytes={}", std::env::consts::OS, 64 * MIB);
    }
}