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
// 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.

//! Row-page dirty budget and required schema checkpoints (#1593).
//!
//! The dirty budget charges encoded page bytes, not the 1 MiB v1 slot, and a
//! resource or budget refusal of a required schema checkpoint leaves it
//! pending instead of poisoning the handle. Budgets are forced small through
//! the test-only publication override so the cases stay a few thousand rows;
//! the 131k/300k-row production cases are measured with the release harness.

use super::*;
use hawdb_storage::config::{RelationalIndexMode, StorageResidencyMode};
use hawdb_storage::relational::RelationalRowPagePublicationConfig;
use hawdb_storage::store::relational_row_pages::set_relational_row_publication_config_override_for_test;
use std::num::NonZeroU64;
use std::path::{Path, PathBuf};

/// Twelve 256-row pages: 12 MiB of v1 slots, about 0.7 MiB encoded.
const ROWS: i64 = 3_000;
const BODY_BYTES: usize = 180;

fn config(residency: StorageResidencyMode, index: RelationalIndexMode) -> DatabaseConfig {
    DatabaseConfig {
        storage_residency_mode: residency,
        relational_index_mode: index,
        automatic_checkpoint_max_age: std::time::Duration::from_secs(3600),
        ..Default::default()
    }
}

fn resident() -> DatabaseConfig {
    config(
        StorageResidencyMode::Materialized,
        RelationalIndexMode::Materialized,
    )
}

/// Resident rows that also publish persistent index generations, which an
/// out-of-core reopen with authoritative indexes requires.
fn resident_shadow() -> DatabaseConfig {
    config(
        StorageResidencyMode::Materialized,
        RelationalIndexMode::Shadow,
    )
}

fn out_of_core() -> DatabaseConfig {
    config(
        StorageResidencyMode::OutOfCore,
        RelationalIndexMode::Authoritative,
    )
}

/// Restricts the canonical checkpoint of `path` to `bytes` encoded dirty
/// bytes for the lifetime of the guard.
struct BudgetOverride(PathBuf);

impl BudgetOverride {
    fn new(path: &Path, bytes: u64) -> Self {
        let config = RelationalRowPagePublicationConfig {
            max_dirty_bytes: NonZeroU64::new(bytes).unwrap(),
            ..Default::default()
        };
        set_relational_row_publication_config_override_for_test(path, Some(config));
        Self(path.to_path_buf())
    }
}

impl Drop for BudgetOverride {
    fn drop(&mut self) {
        set_relational_row_publication_config_override_for_test(&self.0, None);
    }
}

fn load(db: &mut Database, table: &str, rows: i64) {
    db.query_sql(&format!(
        "CREATE TABLE {table} (id BIGINT PRIMARY KEY, body TEXT)"
    ))
    .unwrap();
    for start in (0..rows).step_by(500) {
        let values = (start..(start + 500).min(rows))
            .map(|id| format!("({id}, '{}')", "x".repeat(BODY_BYTES)))
            .collect::<Vec<_>>()
            .join(", ");
        db.query_sql(&format!("INSERT INTO {table} (id, body) VALUES {values}"))
            .unwrap();
    }
}

fn int(db: &mut Database, sql: &str) -> i64 {
    match *db.query_sql(sql).unwrap().rows[0].values().next().unwrap() {
        Value::Int(value) => value,
        ref other => panic!("{sql}: {other:?}"),
    }
}

fn required(db: &Database) -> bool {
    db.runtime
        .get()
        .unwrap()
        .store
        .relational_row_schema_checkpoint_required()
}

fn deferred(db: &Database) -> Option<String> {
    db.runtime
        .get()
        .unwrap()
        .store
        .relational_row_schema_checkpoint_deferred()
        .map(str::to_string)
}

/// Reads and writes through an ordinary handle, then checks the totals.
fn exercise(db: &mut Database, expected_t0: i64) {
    assert_eq!(int(db, "SELECT count(*) FROM t0"), expected_t0);
    db.query_sql("INSERT INTO t1 (id) VALUES (1)").unwrap();
    db.query_sql("UPDATE t0 SET body = 'updated' WHERE id = 7")
        .unwrap();
    db.query_sql(&format!(
        "INSERT INTO t0 (id, body) VALUES ({}, 'new')",
        expected_t0 + 1_000_000
    ))
    .unwrap();
    assert_eq!(int(db, "SELECT count(*) FROM t0"), expected_t0 + 1);
    assert_eq!(int(db, "SELECT count(*) FROM t1 WHERE id = 1"), 1);
    db.query_sql(&format!(
        "DELETE FROM t0 WHERE id = {}",
        expected_t0 + 1_000_000
    ))
    .unwrap();
    db.query_sql("DELETE FROM t1 WHERE id = 1").unwrap();
}

#[test]
fn schema_rebuild_charges_encoded_page_bytes() {
    let path = unique_test_dir("row_budget_rebuild");
    // Twelve full v1 slots exceed 2 MiB; their encoded pages do not.
    let _budget = BudgetOverride::new(&path, 2 * 1024 * 1024);
    let mut db = Database::open_with_config(&path, resident()).unwrap();
    load(&mut db, "t0", ROWS);
    db.query_sql("CREATE TABLE t1 (id BIGINT PRIMARY KEY)")
        .unwrap();
    // CREATE TABLE writes no existing page; ALTER rewrites all of t0.
    db.query_sql("ALTER TABLE t0 ADD COLUMN extra TEXT")
        .unwrap();
    assert!(!required(&db), "the schema checkpoint must complete");
    exercise(&mut db, ROWS);
    drop(db);

    let mut db = Database::open_with_config(&path, resident()).unwrap();
    assert!(!required(&db));
    exercise(&mut db, ROWS);
}

fn checkpoint_epoch(db: &Database) -> Option<u64> {
    db.storage_reclamation_watermark().unwrap().checkpoint_epoch
}

fn can_wait(db: &Database) -> bool {
    db.runtime
        .get()
        .unwrap()
        .store
        .relational_row_schema_checkpoint_can_wait()
}

/// DDL that only rewrites empty tables commits without a checkpoint until a
/// row is written to such a table; DDL that rewrites existing rows still
/// checkpoints in its commit (#1942).
#[test]
fn empty_table_ddl_waits_and_indexing_existing_rows_checkpoints() {
    let path = unique_test_dir("row_budget_empty_ddl_waits");
    let mut db = Database::open_with_config(&path, resident()).unwrap();
    load(&mut db, "t0", ROWS);
    db.checkpoint().unwrap();
    let checkpoint = checkpoint_epoch(&db);

    db.query_sql("CREATE TABLE t1 (id BIGINT PRIMARY KEY, body TEXT)")
        .unwrap();
    db.query_sql("CREATE INDEX t1_body ON t1 (body)").unwrap();
    db.query_sql("ALTER TABLE t1 ADD COLUMN extra TEXT")
        .unwrap();
    assert!(required(&db) && can_wait(&db));
    assert_eq!(
        checkpoint_epoch(&db),
        checkpoint,
        "empty-table DDL checkpointed"
    );
    // Reads, and writes to other tables, keep it waiting.
    db.query_sql("UPDATE t0 SET body = 'updated' WHERE id = 7")
        .unwrap();
    db.query_sql("INSERT INTO t0 (id, body) VALUES (-1, 'new')")
        .unwrap();
    db.query_sql("DELETE FROM t0 WHERE id = -1").unwrap();
    assert_eq!(int(&mut db, "SELECT count(*) FROM t0"), ROWS);
    assert_eq!(int(&mut db, "SELECT count(*) FROM t1"), 0);
    assert!(can_wait(&db));
    assert_eq!(checkpoint_epoch(&db), checkpoint);

    // The first row in the new table ends the wait with one checkpoint.
    db.query_sql("INSERT INTO t1 (id, body) VALUES (10, 'one'), (20, 'two')")
        .unwrap();
    assert!(!required(&db), "the schema checkpoint must complete");
    let checkpoint = checkpoint_epoch(&db);
    assert_ne!(checkpoint, None);
    assert_eq!(
        int(&mut db, "SELECT count(*) FROM t1 WHERE body = 'two'"),
        1
    );

    // Creating a table and filling it in one commit rewrites rows.
    let mut tx = db.begin_transaction().unwrap();
    tx.query_sql("CREATE TABLE t2 (id BIGINT PRIMARY KEY)")
        .unwrap();
    tx.query_sql("INSERT INTO t2 (id) VALUES (1)").unwrap();
    tx.commit().unwrap();
    assert!(!required(&db), "the schema checkpoint must complete");
    assert_ne!(checkpoint_epoch(&db), checkpoint);
    let checkpoint = checkpoint_epoch(&db);

    // An index on a non-empty table checkpoints in its commit.
    db.query_sql("CREATE TABLE t3 (id BIGINT PRIMARY KEY)")
        .unwrap();
    assert!(can_wait(&db));
    assert_eq!(checkpoint_epoch(&db), checkpoint);
    db.query_sql("CREATE INDEX t0_body ON t0 (body)").unwrap();
    assert!(!required(&db), "the schema checkpoint must complete");
    assert_ne!(checkpoint_epoch(&db), checkpoint);
    assert_eq!(
        int(&mut db, "SELECT count(*) FROM t0 WHERE body = 'updated'"),
        1
    );
    drop(db);

    let mut db = Database::open_with_config(&path, resident()).unwrap();
    assert!(!required(&db));
    assert_eq!(
        int(&mut db, "SELECT count(*) FROM t1 WHERE body = 'one'"),
        1
    );
    assert_eq!(int(&mut db, "SELECT count(*) FROM t2"), 1);
    assert_eq!(int(&mut db, "SELECT count(*) FROM t3"), 0);
    assert_eq!(
        int(&mut db, "SELECT count(*) FROM t0 WHERE body = 'updated'"),
        1
    );
    exercise(&mut db, ROWS);
}

/// Persistent index views and out-of-core reopen wait for the schema
/// checkpoint, so DDL with published index generations keeps it in the
/// commit (#1942).
#[test]
fn empty_table_ddl_checkpoints_when_indexes_are_persistent() {
    let path = unique_test_dir("row_budget_empty_ddl_shadow");
    let mut db = Database::open_with_config(&path, resident_shadow()).unwrap();
    load(&mut db, "t0", 600);
    db.checkpoint().unwrap();
    let checkpoint = checkpoint_epoch(&db);
    db.query_sql("CREATE TABLE t1 (id BIGINT PRIMARY KEY)")
        .unwrap();
    assert!(!required(&db), "the schema checkpoint must complete");
    assert_ne!(checkpoint_epoch(&db), checkpoint);
    drop(db);
    let mut db = Database::open_with_config(&path, out_of_core()).unwrap();
    assert_eq!(int(&mut db, "SELECT count(*) FROM t1"), 0);
}

#[test]
fn spread_update_checkpoint_charges_encoded_page_bytes() {
    let path = unique_test_dir("row_budget_spread_update");
    let _budget = BudgetOverride::new(&path, 2 * 1024 * 1024);
    let mut db = Database::open_with_config(&path, resident()).unwrap();
    load(&mut db, "t0", ROWS);
    db.checkpoint().unwrap();
    // One row in each of the twelve pages, in one transaction.
    let mut tx = db.begin_transaction().unwrap();
    for id in (0..ROWS).step_by(256) {
        tx.query_sql(&format!("UPDATE t0 SET body = 'z' WHERE id = {id}"))
            .unwrap();
    }
    tx.commit().unwrap();
    db.checkpoint().unwrap();
    let updated = (0..ROWS).step_by(256).count() as i64;
    assert_eq!(
        int(&mut db, "SELECT count(*) FROM t0 WHERE body = 'z'"),
        updated
    );
    drop(db);

    let mut db = Database::open_with_config(&path, resident()).unwrap();
    assert_eq!(
        int(&mut db, "SELECT count(*) FROM t0 WHERE body = 'z'"),
        updated
    );
    assert_eq!(int(&mut db, "SELECT count(*) FROM t0"), ROWS);
}

#[test]
fn budget_refusal_defers_the_schema_checkpoint_without_poisoning() {
    let path = unique_test_dir("row_budget_deferred");
    let budget = BudgetOverride::new(&path, 64 * 1024);
    let mut db = Database::open_with_config(&path, resident()).unwrap();
    load(&mut db, "t0", ROWS);

    // The DDL commits; its schema checkpoint does not fit and stays pending.
    db.query_sql("CREATE TABLE t1 (id BIGINT PRIMARY KEY)")
        .unwrap();
    // CREATE TABLE writes no existing page; ALTER rewrites all of t0.
    db.query_sql("ALTER TABLE t0 ADD COLUMN extra TEXT")
        .unwrap();
    assert!(required(&db));
    let reason = deferred(&db).expect("the refused checkpoint is recorded");
    assert!(reason.contains("exceeding limit"), "{reason}");

    // Resident reads and writes keep working, and later commits do not
    // retry or poison.
    exercise(&mut db, ROWS);
    db.query_sql("INSERT INTO t0 (id, body) VALUES (-1, 'after')")
        .unwrap();
    assert_eq!(deferred(&db).as_deref(), Some(reason.as_str()));

    // An explicit checkpoint retries and reports the refusal without
    // poisoning.
    let error = db.checkpoint().unwrap_err();
    assert!(
        !matches!(error, HawDBError::StorageIntegrity(_)),
        "{error:?}"
    );
    assert_eq!(int(&mut db, "SELECT count(*) FROM t0"), ROWS + 1);
    drop(db);

    // Reopen attempts the checkpoint once, still cannot fit it, and opens
    // with the checkpoint pending rather than refusing.
    let mut db = Database::open_with_config(&path, resident()).unwrap();
    assert!(required(&db));
    assert!(deferred(&db).is_some());
    exercise(&mut db, ROWS + 1);
    drop(db);

    // This on-disk state is the one a poisoned handle leaves on main: the
    // last checkpoint precedes the DDL and the WAL holds it. With the
    // default budget, recovery completes the checkpoint.
    drop(budget);
    let mut db = Database::open_with_config(&path, resident()).unwrap();
    assert!(!required(&db), "recovery must complete the checkpoint");
    assert!(deferred(&db).is_none());
    exercise(&mut db, ROWS + 1);
    assert_eq!(int(&mut db, "SELECT count(*) FROM t0 WHERE id = -1"), 1);
    drop(db);

    let mut db = Database::open_with_config(&path, resident()).unwrap();
    assert!(!required(&db));
    exercise(&mut db, ROWS + 1);
}

#[test]
fn out_of_core_reopen_of_a_pending_schema_checkpoint_heals_after_a_resident_reopen() {
    let path = unique_test_dir("row_budget_out_of_core");
    {
        let mut db = Database::open_with_config(&path, resident_shadow()).unwrap();
        load(&mut db, "t0", ROWS);
        db.checkpoint().unwrap();
    }
    // Out-of-core handles reject schema-changing transactions before the
    // WAL, so a pending schema checkpoint comes from a resident handle.
    let budget = BudgetOverride::new(&path, 64 * 1024);
    {
        let mut db = Database::open_with_config(&path, resident_shadow()).unwrap();
        db.query_sql("CREATE TABLE t1 (id BIGINT PRIMARY KEY)")
            .unwrap();
        // CREATE TABLE writes no existing page; ALTER rewrites all of t0.
        db.query_sql("ALTER TABLE t0 ADD COLUMN extra TEXT")
            .unwrap();
        assert!(required(&db));
        assert_eq!(int(&mut db, "SELECT count(*) FROM t0"), ROWS);
    }

    // Out-of-core recovery replays only canonical checkpoints plus
    // authoritative WAL, so it refuses the uncheckpointed schema change with
    // a recovery error, not an integrity failure.
    let error = Database::open_with_config(&path, out_of_core())
        .and_then(|mut db| db.query_sql("SELECT count(*) FROM t0"))
        .unwrap_err();
    assert!(
        !matches!(error, HawDBError::StorageIntegrity(_)),
        "{error:?}"
    );

    // A resident reopen with the default budget completes the checkpoint;
    // out-of-core handles then serve the new schema.
    drop(budget);
    let mut db = Database::open_with_config(&path, resident_shadow()).unwrap();
    assert!(!required(&db));
    exercise(&mut db, ROWS);
    drop(db);
    let mut db = Database::open_with_config(&path, out_of_core()).unwrap();
    assert!(!required(&db));
    assert_eq!(int(&mut db, "SELECT count(*) FROM t0"), ROWS);
    assert_eq!(int(&mut db, "SELECT count(*) FROM t1"), 0);
}