hawdb-storage 0.10.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
use super::*;
use crate::background::{CheckpointOperationError, CheckpointWorkError, CheckpointWorkProbe};
use hawdb_core::{RuntimeMemoryError, RuntimeTaskContext, Value};
use hawdb_qos::{
    IoConcurrencyBudget, LocalQosPolicy, LocalQosScheduler, RuntimeGovernor, RuntimeGovernorConfig,
    RuntimeMemorySnapshot, RuntimeResourceBudget, RuntimeResourceSnapshot, RuntimeWorkRequest,
    WorkClass, WorkRequest,
};
use std::num::NonZeroUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;

fn governor(bytes: u64) -> RuntimeGovernor {
    RuntimeGovernor::new(
        RuntimeGovernorConfig {
            memory_budget_bytes: Some(bytes),
            background_task_limit: Some(NonZeroUsize::MIN),
            ..RuntimeGovernorConfig::shared_host()
        },
        RuntimeResourceSnapshot::from_parts(
            RuntimeResourceBudget::from_limits(NonZeroUsize::MIN, None, None),
            RuntimeMemorySnapshot::from_limits(Some(1 << 30), Some(1 << 30), None, None, None),
        ),
        IoConcurrencyBudget::new(2, 1),
    )
}

fn local() -> LocalQosScheduler {
    LocalQosScheduler::new(LocalQosPolicy {
        max_background_operations: Some(1),
        max_total_background_operations: Some(1),
        ..Default::default()
    })
}

fn used(task: &RuntimeTaskContext, ceiling: u64) -> u64 {
    match task.reserve_working_memory(ceiling) {
        Err(RuntimeMemoryError::ReservationExceeded {
            available_bytes, ..
        }) => ceiling - available_bytes,
        _ => panic!("the exact-ceiling probe must read the real governor ledger"),
    }
}

fn definition(bytes: usize) -> ProjectedGraphDefinition {
    ProjectedGraphDefinition {
        node_labels: vec!["Memory".into(), "中文".into()],
        rel_types: vec!["LINKS".into()],
        relationship_predicates: BTreeMap::from([(
            "LINKS".into(),
            crate::projection::ProjectedRelationshipPredicate::Eq {
                property: "body".into(),
                value: Value::Binary((0..bytes).map(|i| (i % 256) as u8).collect()),
            },
        )]),
    }
}

fn cycle(nodes: usize) -> ProjectedGraphArtifactData {
    ProjectedGraphArtifactData::new(
        (0..nodes).map(|id| NodeId(id as u64 + 1)).collect(),
        (0..=nodes).collect(),
        (0..nodes).map(|id| (id + 1) % nodes).collect(),
        (0..=nodes).collect(),
        (0..nodes).map(|id| (id + nodes - 1) % nodes).collect(),
    )
    .unwrap()
}

fn array_bytes(data: &ProjectedGraphArtifactData) -> usize {
    data.nodes.capacity() * std::mem::size_of::<NodeId>()
        + (data.csr_offsets.capacity()
            + data.csr_targets.capacity()
            + data.csc_offsets.capacity()
            + data.csc_sources.capacity())
            * std::mem::size_of::<usize>()
}

#[test]
fn checkpoint_units_projection_artifact_related_empty_roots_allocate_nothing() {
    let source = encode_projected_graph_artifacts(19, 23, []);
    let (epoch, artifacts) = decode(&source, &CheckpointWorkContext::default()).unwrap();
    assert_eq!(epoch, 23);
    let governor = governor(1);
    let permit = governor
        .try_admit(RuntimeWorkRequest::background_maintenance(1))
        .unwrap();
    let task = permit.bind_task_context(RuntimeTaskContext::default());
    let work = CheckpointWorkContext::new(task.clone());
    let observation = crate::test_allocator::AllocationObservation::start_all();
    let root = artifacts.into_root(&work).unwrap();
    let empty = CheckpointProjectedGraphRoot::empty(&work).unwrap();
    let snapshot = root.clone();
    assert!(root.get("missing").is_none());
    assert_eq!(root.values().count(), 0);
    assert!(empty.get("missing").is_none());
    assert_eq!(snapshot.values().count(), 0);
    assert_eq!(
        observation.finish(),
        0,
        "empty root preparation and cloning must not create any heap allocation"
    );
    assert_eq!(used(&task, 1), 0);
    drop(work);
    drop(task);
    drop(permit);
    assert_eq!(governor.snapshot().active_background_tasks, 0);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
    drop(root);
    drop(empty);
    drop(snapshot);
}

#[test]
fn checkpoint_units_projection_artifact_related_arrays_and_root_charge_actual_capacities() {
    let expected = cycle(8193);
    let definition = definition(0);
    let source = encode_projected_graph_artifacts(19, 23, [("g", &definition, expected.clone())]);
    let ceiling = 16 * 1024 * 1024;
    let governor = governor(ceiling);
    let permit = governor
        .try_admit(RuntimeWorkRequest::background_maintenance(ceiling))
        .unwrap();
    let task = permit.bind_task_context(RuntimeTaskContext::default());
    let work = CheckpointWorkContext::new(task.clone());
    let (epoch, artifacts) = decode(&source, &work).unwrap();
    assert_eq!(epoch, 23);
    let data = &artifacts.get("g").unwrap().data;
    assert_eq!(*data, expected);
    let actual_bytes = array_bytes(data);
    assert!(actual_bytes > 5 * 64 * 1024);
    assert!(
        used(&task, ceiling) >= actual_bytes as u64,
        "every retained array capacity needs its own admission"
    );
    let before_root = used(&task, ceiling);
    let root = artifacts.into_root(&work).unwrap();
    let root_bytes = std::mem::size_of::<BTreeMap<String, ProjectedGraphArtifact>>()
        + 2 * std::mem::size_of::<std::sync::atomic::AtomicUsize>();
    assert!(
        used(&task, ceiling) >= before_root + root_bytes as u64,
        "root cell is admitted off gate"
    );
    let snapshot = root.clone();
    assert!(root.data.shares_storage_with(&snapshot.data));
    assert_eq!(root.get("g").unwrap().data, expected);
    drop(root);
    assert!(used(&task, ceiling) >= actual_bytes as u64);
    drop(snapshot);
    assert_eq!(used(&task, ceiling), 0);
    drop(work);
    drop(task);
    drop(permit);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn checkpoint_units_projection_artifact_related_removed_result_retains_inventory() {
    let definition = definition(257 * 1024 + 3);
    let data = cycle(17);
    let source = encode_projected_graph_artifacts(19, 23, [("g", &definition, data.clone())]);
    let ceiling = 32 * 1024 * 1024;
    let governor = governor(ceiling);
    let permit = governor
        .try_admit(RuntimeWorkRequest::background_maintenance(ceiling))
        .unwrap();
    let work = CheckpointWorkContext::new(permit.bind_task_context(RuntimeTaskContext::default()));
    let (_, mut artifacts) = decode(&source, &work).unwrap();
    let removed = artifacts.remove("g").unwrap();
    assert!(artifacts.is_empty());
    drop(artifacts);
    drop(work);
    drop(permit);
    assert_eq!(governor.snapshot().active_background_tasks, 0);
    assert_eq!(governor.snapshot().admitted_memory_bytes, ceiling);
    assert_eq!(removed.definition, definition);
    assert_eq!(removed.data, data);
    drop(removed);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn checkpoint_units_projection_artifact_related_root_and_invalidation_deny_one_byte_and_fully_retry(
) {
    let definition = definition(513);
    let data = cycle(17);
    let source = encode_projected_graph_artifacts(19, 23, [("g", &definition, data.clone())]);
    let denied = governor(1);
    let permit = denied
        .try_admit(RuntimeWorkRequest::background_maintenance(1))
        .unwrap();
    let task = permit.bind_task_context(RuntimeTaskContext::default());
    let work = CheckpointWorkContext::new(task.clone());
    let (_, artifacts) = decode(&source, &CheckpointWorkContext::default()).unwrap();
    assert!(matches!(
        work.classify(|work| artifacts.into_root(work)),
        Err(CheckpointOperationError::Work(CheckpointWorkError::Memory(
            RuntimeMemoryError::ReservationExceeded { .. }
        )))
    ));
    assert_eq!(used(&task, 1), 0);
    let (_, artifacts) = decode(&source, &CheckpointWorkContext::default()).unwrap();
    let mut root = artifacts
        .into_root(&CheckpointWorkContext::default())
        .unwrap();
    let snapshot = root.clone();
    assert!(matches!(
        work.classify(|work| root.invalidate("g", work)),
        Err(CheckpointOperationError::Work(CheckpointWorkError::Memory(
            RuntimeMemoryError::ReservationExceeded { .. }
        )))
    ));
    assert_eq!(root.get("g").unwrap().definition, definition);
    assert_eq!(root.get("g").unwrap().data, data);
    assert!(root.data.shares_storage_with(&snapshot.data));
    assert_eq!(used(&task, 1), 0);
    drop(work);
    drop(task);
    drop(permit);
    assert_eq!(denied.snapshot().admitted_memory_bytes, 0);
    let ceiling = 64 * 1024;
    let governor = governor(ceiling);
    let permit = governor
        .try_admit(RuntimeWorkRequest::background_maintenance(ceiling))
        .unwrap();
    let task = permit.bind_task_context(RuntimeTaskContext::default());
    let work = CheckpointWorkContext::new(task.clone());
    root.invalidate("g", &work).unwrap();
    assert!(root.get("g").is_none());
    assert_eq!(snapshot.get("g").unwrap().data, data);
    assert!(used(&task, ceiling) > 0);
    drop(root);
    assert_eq!(used(&task, ceiling), 0);
    let (_, artifacts) = decode(&source, &CheckpointWorkContext::default()).unwrap();
    let root = artifacts.into_root(&work).unwrap();
    assert_eq!(root.get("g").unwrap().definition, definition);
    assert_eq!(root.get("g").unwrap().data, data);
    assert!(used(&task, ceiling) > 0);
    drop(root);
    assert_eq!(used(&task, ceiling), 0);
    drop(work);
    drop(task);
    drop(permit);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn checkpoint_units_projection_artifact_related_invalidation_keeps_snapshot_and_payload_admission()
{
    let definition = definition(257 * 1024 + 3);
    let data = cycle(17);
    let source = encode_projected_graph_artifacts(
        19,
        23,
        [
            ("g", &definition, data.clone()),
            ("other", &definition, data.clone()),
        ],
    );
    let source_ceiling = 32 * 1024 * 1024;
    let source_governor = governor(source_ceiling);
    let source_permit = source_governor
        .try_admit(RuntimeWorkRequest::background_maintenance(source_ceiling))
        .unwrap();
    let source_work =
        CheckpointWorkContext::new(source_permit.bind_task_context(RuntimeTaskContext::default()));
    let (_, artifacts) = decode(&source, &source_work).unwrap();
    let mut root = artifacts.into_root(&source_work).unwrap();
    let snapshot = root.clone();
    let other = std::ptr::from_ref(root.get("other").unwrap());
    drop(source_work);
    drop(source_permit);
    let mutation_ceiling = 64 * 1024;
    let mutation_governor = governor(mutation_ceiling);
    let mutation_permit = mutation_governor
        .try_admit(RuntimeWorkRequest::background_maintenance(mutation_ceiling))
        .unwrap();
    let task = mutation_permit.bind_task_context(RuntimeTaskContext::default());
    let work = CheckpointWorkContext::new(task.clone());
    let observation = crate::test_allocator::AllocationObservation::start();
    root.invalidate("g", &work).unwrap();
    assert_eq!(
        observation.finish(),
        0,
        "invalidation must share the wide payloads"
    );
    assert!(root.data.shares_storage_with(&snapshot.data));
    assert!(root.get("g").is_none());
    assert_eq!(root.values().count(), 1);
    assert_eq!(std::ptr::from_ref(root.get("other").unwrap()), other);
    assert_eq!(snapshot.get("g").unwrap().definition, definition);
    assert_eq!(snapshot.values().count(), 2);
    let mutation_usage = used(&task, mutation_ceiling);
    assert!(mutation_usage > 0, "invalidation metadata must be admitted");
    root.invalidate("g", &work).unwrap();
    root.invalidate("missing", &work).unwrap();
    assert_eq!(used(&task, mutation_ceiling), mutation_usage);
    drop(work);
    drop(task);
    drop(mutation_permit);
    assert_eq!(
        mutation_governor.snapshot().admitted_memory_bytes,
        mutation_ceiling
    );
    assert_eq!(
        source_governor.snapshot().admitted_memory_bytes,
        source_ceiling
    );
    drop(root);
    assert_eq!(mutation_governor.snapshot().admitted_memory_bytes, 0);
    assert_eq!(
        source_governor.snapshot().admitted_memory_bytes,
        source_ceiling
    );
    assert_eq!(snapshot.get("g").unwrap().data, data);
    drop(snapshot);
    assert_eq!(source_governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn checkpoint_units_projection_artifact_related_invalidation_cancels_every_unit_without_mutating_source(
) {
    let definition = definition(0);
    let data = cycle(1);
    let source = encode_projected_graph_artifacts(
        19,
        23,
        (0..129)
            .map(|_| data.clone())
            .enumerate()
            .map(|(id, data)| (format!("g{id:03}"), data))
            .collect::<Vec<_>>()
            .iter()
            .map(|(name, data)| (name.as_str(), &definition, data.clone())),
    );
    let (_, artifacts) = decode(&source, &CheckpointWorkContext::default()).unwrap();
    let mut original = artifacts
        .into_root(&CheckpointWorkContext::default())
        .unwrap();
    for id in (0..128).step_by(2) {
        original
            .invalidate(&format!("g{id:03}"), &CheckpointWorkContext::default())
            .unwrap();
    }
    let local = local();
    let probe = Arc::new(CheckpointWorkProbe::default());
    let baseline = probe.context(local.clone());
    let mut complete = original.clone();
    complete.invalidate("g128", &baseline).unwrap();
    let units = probe.completed.load(Ordering::SeqCst);
    assert!(units > 3);
    assert_eq!(complete.values().count(), 64);
    drop(baseline);
    probe.assert_released(&local);
    for stop in 0..=units {
        let ceiling = 64 * 1024;
        let governor = governor(ceiling);
        let permit = governor
            .try_admit(RuntimeWorkRequest::background_maintenance(ceiling))
            .unwrap();
        let task = permit.bind_task_context(RuntimeTaskContext::default());
        let probe = Arc::new(CheckpointWorkProbe::default());
        probe.cancel_after.store(stop, Ordering::SeqCst);
        local.set_telemetry_sink(Some(probe.clone()));
        let work = CheckpointWorkContext::new(permit.bind_task_context(
            RuntimeTaskContext::without_deadline(probe.cancellation.clone()),
        ))
        .with_scheduler(local.clone());
        let held = (stop == 0).then(|| {
            local
                .try_start(WorkRequest::background(WorkClass::Mutation, 1))
                .unwrap()
        });
        let mut root = original.clone();
        let result = work.classify(|work| root.invalidate("g128", work));
        assert!(
            matches!(result, Err(CheckpointOperationError::Work(CheckpointWorkError::Admission(_))) if stop == 0)
                || matches!(result, Err(CheckpointOperationError::Work(CheckpointWorkError::Stopped(_))) if stop > 0)
        );
        assert_eq!(root.values().count(), 65);
        assert_eq!(root.get("g128").unwrap().data, data);
        assert!(root.data.shares_storage_with(&original.data));
        assert_eq!(used(&task, ceiling), 0);
        drop(held);
        drop(work);
        probe.assert_released(&local);
        local.set_telemetry_sink(None);
        let retry = CheckpointWorkContext::new(task.clone()).with_scheduler(local.clone());
        root.invalidate("g128", &retry).unwrap();
        assert!(root.get("g128").is_none());
        assert_eq!(root.values().count(), 64);
        assert!(original.get("g128").is_some());
        drop(root);
        assert_eq!(used(&task, ceiling), 0);
        drop(retry);
        drop(task);
        drop(permit);
        assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
    }
}

#[test]
fn checkpoint_units_projection_artifact_related_decode_cancels_every_unit_and_fully_retries() {
    let definition = definition(513);
    let data = cycle(17);
    let source = encode_projected_graph_artifacts(19, 23, [("g", &definition, data.clone())]);
    let local = local();
    let probe = Arc::new(CheckpointWorkProbe::default());
    let baseline = probe.context(local.clone());
    let (_, artifacts) = decode(&source, &baseline).unwrap();
    let root = artifacts.into_root(&baseline).unwrap();
    assert_eq!(root.get("g").unwrap().data, data);
    let units = probe.completed.load(Ordering::SeqCst);
    assert!(units > 100);
    drop(root);
    drop(baseline);
    probe.assert_released(&local);
    for stop in 0..=units {
        let ceiling = 4 * 1024 * 1024;
        let governor = governor(ceiling);
        let permit = governor
            .try_admit(RuntimeWorkRequest::background_maintenance(ceiling))
            .unwrap();
        let task = permit.bind_task_context(RuntimeTaskContext::default());
        let probe = Arc::new(CheckpointWorkProbe::default());
        probe.cancel_after.store(stop, Ordering::SeqCst);
        local.set_telemetry_sink(Some(probe.clone()));
        let work = CheckpointWorkContext::new(permit.bind_task_context(
            RuntimeTaskContext::without_deadline(probe.cancellation.clone()),
        ))
        .with_scheduler(local.clone());
        let held = (stop == 0).then(|| {
            local
                .try_start(WorkRequest::background(WorkClass::Mutation, 1))
                .unwrap()
        });
        let result = work.classify(|work| {
            decode(&source, work).and_then(|(_, artifacts)| artifacts.into_root(work))
        });
        assert!(
            matches!(result, Err(CheckpointOperationError::Work(CheckpointWorkError::Admission(_))) if stop == 0)
                || matches!(result, Err(CheckpointOperationError::Work(CheckpointWorkError::Stopped(_))) if stop > 0)
        );
        assert_eq!(used(&task, ceiling), 0);
        drop(held);
        drop(work);
        probe.assert_released(&local);
        local.set_telemetry_sink(None);
        let retry = CheckpointWorkContext::new(task.clone()).with_scheduler(local.clone());
        let (epoch, artifacts) = decode(&source, &retry).unwrap();
        assert_eq!(epoch, 23);
        let root = artifacts.into_root(&retry).unwrap();
        assert_eq!(root.get("g").unwrap().definition, definition);
        assert_eq!(root.get("g").unwrap().data, data);
        drop(root);
        assert_eq!(used(&task, ceiling), 0);
        drop(retry);
        drop(task);
        drop(permit);
        assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
    }
}