armdb 0.12.0

sharded bitcask key-value storage optimized for NVMe
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
#![cfg(all(
    feature = "test-faults",
    feature = "armour",
    feature = "schema",
    feature = "typed-tree",
    feature = "rapira-codec"
))]

use std::sync::Arc;

use armdb::armour::Db;
use armdb::schema::{
    CollectionKind, Finding, RepairOptions, SchemaRegistry, debug_fail_next_namespace_repair,
    repair, validate,
};
use armdb::{
    ClaimSink, CollectionMeta, Config, DbResult, FormatMigration, IndexTargets, NoHook, Projected,
    RapiraCodec, UniqueIndex, UniqueTargets,
};
use armour_core::{Fuid, GetRefs, GetType};
use rapira::Rapira;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

armour_core::const_hasher_or!(
    format_unique_user,
    "FMUUSR",
    "dev-key-format-unique",
    brand = "user"
);
type UserId = Fuid<format_unique_user::Hasher>;

fn id(n: u64) -> UserId {
    Fuid::from_be_bytes(n.to_be_bytes())
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, FromBytes, IntoBytes, Immutable, KnownLayout)]
#[repr(C)]
pub struct UsernameKey([u8; 24]);
armdb::impl_key_zerocopy!(
    UsernameKey,
    armour_core::KeyScheme::Typed(&[armour_core::KeyType::Array(24)])
);
impl UsernameKey {
    fn new(s: &str) -> Self {
        let mut key = [0; 24];
        key[..s.len()].copy_from_slice(s.as_bytes());
        Self(key)
    }
}

armdb::unique_index!(
    FormatUsernames,
    key = UsernameKey,
    name = "format_usernames"
);

#[derive(Clone, Debug, GetRefs, GetType, Rapira)]
struct UserV1 {
    username: String,
}
impl CollectionMeta for UserV1 {
    type SelfId = UserId;
    const NAME: &'static str = "format_unique_users";
    const VERSION: u16 = 1;
}

#[derive(Clone, Debug, GetRefs, GetType, Rapira)]
struct UserV2 {
    username: String,
    migrated: bool,
}

#[derive(Clone, Debug, GetRefs, GetType, Rapira)]
struct LegacyUser {
    username: Option<String>,
}

impl CollectionMeta for LegacyUser {
    type SelfId = UserId;
    const NAME: &'static str = "format_unique_legacy_users";
}

#[derive(Clone, Debug, GetRefs, GetType, Rapira)]
struct ForeignUser;

impl CollectionMeta for ForeignUser {
    type SelfId = UserId;
    const NAME: &'static str = "format_unique_foreign_users";
}

const USER_FORMAT: FormatMigration = armdb::format_step!(
    1,
    UserV1 => UserV2,
    RapiraCodec,
    |old| Ok::<_, armdb::DbError>(Some(UserV2 { username: old.username, migrated: true }))
);
impl CollectionMeta for UserV2 {
    type SelfId = UserId;
    const NAME: &'static str = "format_unique_users";
    const VERSION: u16 = 2;
    const FORMAT_MIGRATIONS: &'static [FormatMigration] = &[USER_FORMAT];
}

fn open_usernames(db: &Arc<Db>) -> Arc<UniqueIndex<FormatUsernames>> {
    db.open_unique_index(armdb::FixedConfig::test())
        .expect("open namespace")
}

fn open_v1(
    db: &Arc<Db>,
    usernames: &Arc<UniqueIndex<FormatUsernames>>,
) -> Arc<armdb::typed_map!(UserV1, RapiraCodec, Projected<UserId, UserV1, NoHook>)> {
    let namespace = Arc::clone(usernames);
    let hook =
        Projected::<UserId, UserV1, NoHook>::new(IndexTargets::new(), |_id, _user, _sink| Ok(()))
            .and_unique(
                UniqueTargets::new().with(usernames),
                move |_id, user: &UserV1, sink: &mut ClaimSink<'_>| {
                    sink.claim(&namespace, UsernameKey::new(&user.username))
                },
            );
    db.open_typed_map(Config::test(), hook, &[])
        .expect("open v1")
}

fn open_v2(
    db: &Arc<Db>,
    usernames: &Arc<UniqueIndex<FormatUsernames>>,
) -> Arc<armdb::typed_map!(UserV2, RapiraCodec, Projected<UserId, UserV2, NoHook>)> {
    let namespace = Arc::clone(usernames);
    let hook =
        Projected::<UserId, UserV2, NoHook>::new(IndexTargets::new(), |_id, _user, _sink| Ok(()))
            .and_unique(
                UniqueTargets::new().with(usernames),
                move |_id, user: &UserV2, sink: &mut ClaimSink<'_>| {
                    sink.claim(&namespace, UsernameKey::new(&user.username))
                },
            );
    db.open_typed_map(Config::test(), hook, &[])
        .expect("open v2")
}

#[test]
fn format_migration_unique_drift_requires_registry_repair_before_it_clears() {
    let dir = tempfile::tempdir().expect("tempdir");
    {
        let db = Arc::new(Db::open_test(dir.path()).expect("open v1 db"));
        let usernames = open_usernames(&db);
        let users = open_v1(&db, &usernames);
        db.tx()
            .put(
                &users,
                &id(1),
                &UserV1 {
                    username: "rail".into(),
                },
            )
            .expect("stage")
            .commit()
            .expect("commit");
        assert!(
            usernames.contains(&UsernameKey::new("rail")),
            "sanity: claim exists"
        );
        db.close().expect("close v1");
    }

    let db = Arc::new(Db::open_test(dir.path()).expect("open v2 db"));
    let usernames = open_usernames(&db);
    let users = open_v2(&db, &usernames);
    assert_eq!(users.get(&id(1)).expect("migrated row").username, "rail");
    assert!(
        db.unique_dirty(UserV2::NAME),
        "format migration records conservative drift"
    );

    let mut registry = SchemaRegistry::new();
    registry.register_external(usernames.name(), CollectionKind::UniqueIndex);
    let namespace = Arc::clone(&usernames);
    let _ = registry.unique_index::<UserV2, _>(
        UniqueTargets::new().with(&usernames),
        move |_, user: &UserV2, sink: &mut ClaimSink<'_>| -> DbResult<()> {
            sink.claim(&namespace, UsernameKey::new(&user.username))
        },
    );
    registry.register_entity(&users);
    let registry = registry.finish().expect("registry");

    let report = validate(&db, &registry);
    assert!(report.findings.iter().any(|finding| matches!(
        finding,
        Finding::UniqueDirty { collection } if collection == UserV2::NAME
    ) && finding.is_error()));

    repair(&db, &registry, RepairOptions::default()).expect("repair");
    assert!(
        !db.unique_dirty(UserV2::NAME),
        "complete registered repair clears marker"
    );
}

#[test]
fn seeded_unique_dirty_repair_failure_keeps_dirty_marker() {
    let dir = tempfile::tempdir().expect("tempdir");
    let db = Arc::new(Db::open_test(dir.path()).expect("open db"));
    let usernames = open_usernames(&db);
    let users = open_v1(&db, &usernames);
    db.db_info_update(|info| {
        info.collections
            .get_mut(UserV1::NAME)
            .expect("opened source")
            .unique_dirty = true;
    })
    .expect("mark dirty");

    let mut registry = SchemaRegistry::new();
    registry.register_external(usernames.name(), CollectionKind::UniqueIndex);
    let namespace = Arc::clone(&usernames);
    let _ = registry.unique_index::<UserV1, _>(
        UniqueTargets::new().with(&usernames),
        move |_, user: &UserV1, sink: &mut ClaimSink<'_>| -> DbResult<()> {
            sink.claim(&namespace, UsernameKey::new(&user.username))
        },
    );
    registry.register_entity(&users);
    let registry = registry.finish().expect("registry");

    debug_fail_next_namespace_repair();
    let err = repair(&db, &registry, RepairOptions::default()).expect_err("injected failure");
    assert!(format!("{err}").contains("injected namespace repair failure"));
    assert!(
        db.unique_dirty(UserV1::NAME),
        "failed repair must not retire marker"
    );
}

#[test]
fn seeded_unique_dirty_corrupt_claim_keeps_dirty_marker() {
    let dir = tempfile::tempdir().expect("tempdir");
    let db = Arc::new(Db::open_test(dir.path()).expect("open db"));
    let usernames = open_usernames(&db);
    let users = open_v1(&db, &usernames);
    db.tx()
        .put(
            &users,
            &id(1),
            &UserV1 {
                username: "rail".into(),
            },
        )
        .expect("stage")
        .commit()
        .expect("commit");
    armdb::testing::force_put(
        &usernames,
        &UsernameKey::new("rail"),
        armdb::testing::corrupt_unique_row(),
    )
    .expect("corrupt claimed namespace row");
    db.db_info_update(|info| {
        info.collections
            .get_mut(UserV1::NAME)
            .expect("opened source")
            .unique_dirty = true;
    })
    .expect("mark dirty");

    let mut registry = SchemaRegistry::new();
    registry.register_external(usernames.name(), CollectionKind::UniqueIndex);
    let namespace = Arc::clone(&usernames);
    let _ = registry.unique_index::<UserV1, _>(
        UniqueTargets::new().with(&usernames),
        move |_, user: &UserV1, sink: &mut ClaimSink<'_>| -> DbResult<()> {
            sink.claim(&namespace, UsernameKey::new(&user.username))
        },
    );
    registry.register_entity(&users);
    let registry = registry.finish().expect("registry");

    repair(&db, &registry, RepairOptions::default()).expect("repair returns finding state");
    assert!(
        db.unique_dirty(UserV1::NAME),
        "a corrupt namespace row covering this source's claim must block marker retirement"
    );
}

#[test]
fn seeded_unique_dirty_renamed_username_stale_claim_keeps_dirty_until_deleted() {
    let dir = tempfile::tempdir().expect("tempdir");
    let db = Arc::new(Db::open_test(dir.path()).expect("open db"));
    let usernames = open_usernames(&db);
    let users = db
        .open_typed_map::<UserV1, RapiraCodec, _>(Config::test(), NoHook, &[])
        .expect("open users without enforcing hook");
    users
        .put(
            &id(1),
            UserV1 {
                username: "new-name".into(),
            },
        )
        .expect("write renamed source row");
    let owner = armdb::testing::unique_owner_for::<UserV1>(armdb::Key::as_bytes(&id(1)));
    armdb::testing::force_put(
        &usernames,
        &UsernameKey::new("old-name"),
        armdb::testing::unique_row_for(owner),
    )
    .expect("seed stale old username claim");
    db.db_info_update(|info| {
        info.collections
            .get_mut(UserV1::NAME)
            .expect("opened source")
            .unique_dirty = true;
    })
    .expect("mark unique dirty");

    let mut registry = SchemaRegistry::new();
    registry.register_external(usernames.name(), CollectionKind::UniqueIndex);
    registry.complete_namespace(&usernames);
    let namespace = Arc::clone(&usernames);
    let _ = registry.unique_index::<UserV1, _>(
        UniqueTargets::new().with(&usernames),
        move |_, user: &UserV1, sink: &mut ClaimSink<'_>| -> DbResult<()> {
            sink.claim(&namespace, UsernameKey::new(&user.username))
        },
    );
    registry.register_entity(&users);
    let registry = registry.finish().expect("registry");

    let report = repair(&db, &registry, RepairOptions::default()).expect("default repair");
    assert!(usernames.contains(&UsernameKey::new("new-name")));
    assert!(usernames.contains(&UsernameKey::new("old-name")));
    assert_eq!(report.per_namespace[0].declined_orphans, 1, "{report:?}");
    assert!(
        db.unique_dirty(UserV1::NAME),
        "an unresolved stale claim must retain its source's repair obligation"
    );

    repair(
        &db,
        &registry,
        RepairOptions {
            delete_orphans: true,
        },
    )
    .expect("authoritative destructive repair");
    assert!(!usernames.contains(&UsernameKey::new("old-name")));
    assert!(
        !db.unique_dirty(UserV1::NAME),
        "the marker clears only after the authoritative namespace is clean"
    );
}

#[test]
fn seeded_unique_dirty_three_source_conflict_keeps_every_source_dirty() {
    let dir = tempfile::tempdir().expect("tempdir");
    let db = Arc::new(Db::open_test(dir.path()).expect("open db"));
    let usernames = open_usernames(&db);
    let users = db
        .open_typed_map::<UserV1, RapiraCodec, _>(Config::test(), NoHook, &[])
        .expect("open current users");
    let legacy = db
        .open_typed_map::<LegacyUser, RapiraCodec, _>(Config::test(), NoHook, &[])
        .expect("open legacy users");
    let foreign = db
        .open_typed_map::<ForeignUser, RapiraCodec, _>(Config::test(), NoHook, &[])
        .expect("open foreign users");
    users
        .put(
            &id(1),
            UserV1 {
                username: "rail".into(),
            },
        )
        .expect("write current claimant");
    legacy
        .put(
            &id(2),
            LegacyUser {
                username: Some("rail".into()),
            },
        )
        .expect("write second claimant");
    let legacy_key = id(9);
    let foreign_owner =
        armdb::testing::unique_owner_for::<ForeignUser>(armdb::Key::as_bytes(&legacy_key));
    armdb::testing::force_put(
        &usernames,
        &UsernameKey::new("rail"),
        armdb::testing::unique_row_for(foreign_owner),
    )
    .expect("seed foreign owner");
    db.db_info_update(|info| {
        info.collections
            .get_mut(UserV1::NAME)
            .expect("current source")
            .unique_dirty = true;
        info.collections
            .get_mut(LegacyUser::NAME)
            .expect("second claimant")
            .unique_dirty = true;
        info.collections
            .get_mut(ForeignUser::NAME)
            .expect("foreign source")
            .unique_dirty = true;
    })
    .expect("mark both sources dirty");

    let mut registry = SchemaRegistry::new();
    registry.register_external(usernames.name(), CollectionKind::UniqueIndex);
    registry.complete_namespace(&usernames);
    let user_ns = Arc::clone(&usernames);
    let _ = registry.unique_index::<UserV1, _>(
        UniqueTargets::new().with(&usernames),
        move |_, user: &UserV1, sink: &mut ClaimSink<'_>| -> DbResult<()> {
            sink.claim(&user_ns, UsernameKey::new(&user.username))
        },
    );
    let legacy_ns = Arc::clone(&usernames);
    let _ = registry.unique_index::<LegacyUser, _>(
        UniqueTargets::new().with(&usernames),
        move |_, user: &LegacyUser, sink: &mut ClaimSink<'_>| -> DbResult<()> {
            if let Some(username) = &user.username {
                sink.claim(&legacy_ns, UsernameKey::new(username))?;
            }
            Ok(())
        },
    );
    registry.register_entity(&users);
    registry.register_entity(&legacy);
    registry.register_entity(&foreign);
    let registry = registry.finish().expect("registry");

    let report = repair(
        &db,
        &registry,
        RepairOptions {
            delete_orphans: true,
        },
    )
    .expect("repair reports conflict");

    let stats = report
        .per_namespace
        .iter()
        .find(|stats| stats.namespace == usernames.name())
        .expect("namespace stats");
    assert_eq!(stats.conflicts.len(), 1, "{report:?}");
    assert!(
        db.unique_dirty(UserV1::NAME),
        "first claimant remains unresolved"
    );
    assert!(
        db.unique_dirty(LegacyUser::NAME),
        "second claimant remains unresolved"
    );
    assert!(
        db.unique_dirty(ForeignUser::NAME),
        "the registered dirty foreign stored owner also remains unresolved"
    );
}