armdb 0.7.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
use std::path::{Path, PathBuf};

use crate::sync::{self, Mutex};

use crate::error::{DbError, DbResult};
use crate::fixed::config::FixedConfig;
use crate::fixed::shard::FixedShardInner;
use crate::meta::{Backend, DbMeta, META_VERSION};

/// Wrapper that pairs a shard id with a mutex-protected `FixedShardInner`.
#[allow(dead_code)]
pub(crate) struct FixedShard {
    pub id: u8,
    pub inner: Mutex<FixedShardInner>,
}

/// Multi-shard engine for the fixed-slot storage backend.
///
/// Manages the lifecycle of all shards, the `db.meta` file, and
/// open/close coordination.
pub(crate) struct FixedEngine {
    path: PathBuf,
    /// Owned outright, not behind an `Arc`: nothing may hold shards without also
    /// holding the lease below. Bitcask needs `Arc<Shards>` because replication
    /// clones the shard set out of the engine and outlives it; FixedStore never
    /// does — its replication takes `Arc<FixedEngine>`, carrying the lease along.
    /// Keeping the `Vec` unshared means a future `.clone()` into a thread has
    /// nothing to clone, so the invariant cannot regress silently.
    shards: Vec<FixedShard>,
    config: FixedConfig,
    /// Exclusive lease on the collection directory.
    ///
    /// A plain field is enough here (unlike Bitcask): `Fixed` holds
    /// `Arc<FixedEngine>` and `fixed_engine_access()` clones the whole engine,
    /// so every holder keeps the lease. Declared last so the shards flush and
    /// close before the lock is released.
    _lock: std::fs::File,
}

// ── db.meta I/O ───────────────────────────────────────────────────

fn write_meta(path: &Path, config: &FixedConfig) -> DbResult<()> {
    let meta = DbMeta {
        version: META_VERSION,
        backend: Backend::FixedStore,
        shard_count: config.shard_count as u8,
        shard_prefix_bits: config.shard_prefix_bits as u8,
        flags: 0,
    };

    let file = std::fs::File::create(path)?;
    use std::io::Write;
    (&file).write_all(&meta.encode())?;
    file.sync_all()?;

    if let Some(parent) = path.parent() {
        let dir = std::fs::File::open(parent)?;
        dir.sync_all()?;
    }
    Ok(())
}

fn validate_meta(path: &Path, config: &FixedConfig) -> DbResult<()> {
    let bytes = std::fs::read(path)?;
    let meta = DbMeta::decode(&bytes)?;

    if meta.backend != Backend::FixedStore {
        return Err(DbError::FormatMismatch(format!(
            "db.meta: expected FixedStore backend, found {:?}",
            meta.backend
        )));
    }
    if meta.shard_count as usize != config.shard_count {
        return Err(DbError::FormatMismatch(format!(
            "db.meta: shard_count mismatch: stored {}, config {}",
            meta.shard_count, config.shard_count
        )));
    }
    if meta.shard_prefix_bits as usize != config.shard_prefix_bits {
        return Err(DbError::FormatMismatch(format!(
            "db.meta: shard_prefix_bits mismatch: stored {}, config {}",
            meta.shard_prefix_bits, config.shard_prefix_bits
        )));
    }
    Ok(())
}

// ── FixedEngine ───────────────────────────────────────────────────

impl FixedEngine {
    /// Open or create a fixed-slot database at the given path.
    ///
    /// 1. Validates configuration.
    /// 2. Creates the root directory if needed.
    /// 3. Reads/writes `db.meta`.
    /// 4. Opens every shard (`shard_000/`, `shard_001/`, ...).
    #[tracing::instrument(skip(path,config), fields(path = %path.as_ref().display()))]
    pub fn open(
        path: impl AsRef<Path>,
        config: FixedConfig,
        key_len: u16,
        value_len: u16,
    ) -> DbResult<Self> {
        let path = path.as_ref().to_path_buf();
        config.validate()?;

        tracing::info!(shards = config.shard_count, "opening fixed-slot database");

        std::fs::create_dir_all(&path)?;

        // Before db.meta is read or written, so validation and recovery run
        // under the lease.
        let _lock = crate::lock::acquire(&path)?;

        // db.meta: validate or create
        let meta_path = path.join("db.meta");
        if meta_path.exists() {
            validate_meta(&meta_path, &config)?;
        } else {
            write_meta(&meta_path, &config)?;
        }

        // Open each shard
        let mut shards = Vec::with_capacity(config.shard_count);
        for i in 0..config.shard_count {
            let shard_dir = path.join(format!("shard_{i:03}"));
            let inner = FixedShardInner::open(&shard_dir, i as u8, key_len, value_len, &config)?;
            shards.push(FixedShard {
                id: i as u8,
                inner: Mutex::new(inner),
            });
        }

        tracing::info!("fixed-slot database opened");
        Ok(Self {
            path,
            shards,
            config,
            _lock,
        })
    }

    /// Reference to the shard list. Deliberately a slice, not the owning
    /// container: a caller must not be able to take shards away from the lease.
    #[allow(dead_code)]
    pub fn shards(&self) -> &[FixedShard] {
        &self.shards
    }

    /// Reference to the engine configuration.
    #[allow(dead_code)]
    pub fn config(&self) -> &FixedConfig {
        &self.config
    }

    /// Root path of the database.
    #[allow(dead_code)]
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Sync all shard data files to disk.
    pub fn flush(&self) -> DbResult<()> {
        tracing::debug!("flushing all fixed-slot shards");
        for shard in self.shards.iter() {
            sync::lock(&shard.inner).sync()?;
        }
        Ok(())
    }

    /// Install SPSC producers into each shard's `replication_tx`.
    /// Called by `FixedReplicationServer` on the first follower accept.
    ///
    /// # Single-active-server contract
    ///
    /// Each call installs fresh producers, **replacing** any previously
    /// installed ones (F-06 — mirrors the Bitcask side, see
    /// `ConstTree::start_replication_server`). This makes restart-on-the-same-
    /// engine safe: stopping a server and starting a new one no longer panics
    /// the acceptor thread. Run at most one active `FixedReplicationServer` per
    /// engine at a time — a second concurrent server would orphan the first's
    /// in-flight producer, whose consumer then drains an empty ring and
    /// silently stops forwarding.
    ///
    /// Panics only if the producers slice length mismatches `shard_count`.
    #[cfg(feature = "replication")]
    #[allow(dead_code)]
    pub fn install_replication_producers(
        &self,
        producers: Vec<rtrb::Producer<crate::fixed_replication::FixedReplicationEvent>>,
    ) {
        assert_eq!(
            producers.len(),
            self.shards.len(),
            "producer count must match shard count"
        );
        for (shard, tx) in self.shards.iter().zip(producers) {
            let mut inner = sync::lock(&shard.inner);
            // Replace unconditionally; any prior producer is dropped here.
            inner.replication_tx = Some(tx);
        }
    }

    /// Perform a clean shutdown of every shard.
    pub fn close(&self) -> DbResult<()> {
        tracing::info!("closing fixed-slot database");
        for shard in self.shards.iter() {
            sync::lock(&shard.inner).clean_shutdown()?;
        }
        tracing::info!("fixed-slot database closed");
        Ok(())
    }
}

#[cfg(feature = "replication")]
impl crate::fixed_replication::FixedEngineAccess for FixedEngine {
    fn shard_count(&self) -> usize {
        self.shards.len()
    }

    fn key_len(&self) -> usize {
        sync::lock(&self.shards[0].inner).key_len() as usize
    }

    fn value_len(&self) -> usize {
        sync::lock(&self.shards[0].inner).value_len() as usize
    }

    fn slot_size(&self) -> u16 {
        sync::lock(&self.shards[0].inner).slot_size
    }

    fn current_slot_count(&self, shard_id: usize) -> u32 {
        sync::lock(&self.shards[shard_id].inner).slot_count()
    }

    fn shard_prefix_bits(&self) -> u8 {
        self.config.shard_prefix_bits as u8
    }

    fn read_shard_chunk(
        &self,
        shard_id: usize,
        start_slot: u32,
        count: usize,
    ) -> crate::error::DbResult<Vec<u8>> {
        let inner = sync::lock(&self.shards[shard_id].inner);
        let slot_size = inner.slot_size as usize;
        let mut buf = vec![0u8; count * slot_size];
        let offset = crate::fixed::shard::HEADER_SIZE + start_slot as u64 * slot_size as u64;
        inner.read_chunk_at(offset, &mut buf)?;
        Ok(buf)
    }

    fn install_replication_producers(
        &self,
        producers: Vec<rtrb::Producer<crate::fixed_replication::FixedReplicationEvent>>,
    ) {
        FixedEngine::install_replication_producers(self, producers);
    }

    fn update_min_replicated_version(&self, _shard_id: usize, _version: u32) {
        // Metric-only hook; no-op for v1. A future version can track per-shard
        // atomic gauges for dashboard visibility.
    }

    fn take_dropped(&self, shard_id: usize) -> bool {
        // F-02: read-and-clear the per-shard drop flag under the shard lock.
        sync::lock(&self.shards[shard_id].inner)
            .replication_dropped
            .swap(false, std::sync::atomic::Ordering::Relaxed)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    fn test_config() -> FixedConfig {
        FixedConfig {
            shard_count: 2,
            grow_step: 64,
            ..FixedConfig::test()
        }
    }

    #[test]
    fn test_engine_open_creates_shards() {
        let dir = tempdir().unwrap();
        let db_path = dir.path().join("testdb");
        let config = test_config();

        let engine = FixedEngine::open(&db_path, config.clone(), 8, 32).unwrap();

        // db.meta exists
        assert!(db_path.join("db.meta").exists());

        // Shard directories exist
        for i in 0..config.shard_count {
            assert!(
                db_path.join(format!("shard_{i:03}")).exists(),
                "shard_{i:03} directory should exist"
            );
        }

        // Correct number of shards
        assert_eq!(engine.shards().len(), config.shard_count);

        // Each shard has the right id
        for (i, shard) in engine.shards().iter().enumerate() {
            assert_eq!(shard.id, i as u8);
        }
    }

    #[cfg(feature = "replication")]
    #[test]
    fn test_f02_take_dropped_reads_and_clears() {
        use crate::fixed_replication::FixedEngineAccess;
        use std::sync::atomic::Ordering;

        let dir = tempdir().unwrap();
        let db_path = dir.path().join("testdb");
        let engine = FixedEngine::open(&db_path, test_config(), 8, 32).unwrap();

        // Nothing dropped yet.
        assert!(!FixedEngineAccess::take_dropped(&engine, 0));

        // Write path flags a drop on shard 0.
        sync::lock(&engine.shards()[0].inner)
            .replication_dropped
            .store(true, Ordering::Relaxed);

        // First read observes and clears; the second sees it cleared.
        assert!(FixedEngineAccess::take_dropped(&engine, 0));
        assert!(!FixedEngineAccess::take_dropped(&engine, 0));
    }

    #[test]
    fn test_engine_reopen() {
        let dir = tempdir().unwrap();
        let db_path = dir.path().join("testdb");
        let config = test_config();

        // Open and close
        {
            let engine = FixedEngine::open(&db_path, config.clone(), 8, 32).unwrap();
            engine.close().unwrap();
        }

        // Reopen — must succeed with same config
        {
            let engine = FixedEngine::open(&db_path, config.clone(), 8, 32).unwrap();
            assert_eq!(engine.shards().len(), config.shard_count);
            engine.close().unwrap();
        }
    }

    #[test]
    fn fixedstore_rejects_bitcask_meta() {
        let dir = tempdir().unwrap();
        let db_path = dir.path().join("testdb");
        let config = test_config();

        // Create the database directory and write a valid Bitcask db.meta
        // (backend byte = 0). shard_count/shard_prefix_bits match test_config()
        // so the backend check fires before any shard mismatch check.
        std::fs::create_dir_all(&db_path).unwrap();
        let meta_path = db_path.join("db.meta");
        let bitcask_meta: [u8; 16] = [
            b'A',
            b'R',
            b'M',
            b'D', // magic
            2,    // version
            0,    // backend: 0 = Bitcask
            config.shard_count as u8,
            config.shard_prefix_bits as u8,
            0, // flags
            0,
            0,
            0,
            0,
            0,
            0,
            0, // reserved
        ];
        std::fs::write(&meta_path, bitcask_meta).unwrap();

        let result = FixedEngine::open(&db_path, config, 8, 32);
        assert!(result.is_err());
        let err = result.err().unwrap();
        assert!(
            matches!(err, DbError::FormatMismatch(_)),
            "expected FormatMismatch, got: {err}"
        );
    }

    #[test]
    fn test_engine_shard_count_mismatch() {
        let dir = tempdir().unwrap();
        let db_path = dir.path().join("testdb");

        // Open with 2 shards
        {
            let config = FixedConfig {
                shard_count: 2,
                grow_step: 64,
                ..FixedConfig::test()
            };
            let engine = FixedEngine::open(&db_path, config, 8, 32).unwrap();
            engine.close().unwrap();
        }

        // Reopen with 4 shards — should fail
        {
            let config = FixedConfig {
                shard_count: 4,
                grow_step: 64,
                ..FixedConfig::test()
            };
            let result = FixedEngine::open(&db_path, config, 8, 32);
            assert!(result.is_err());
            let msg = result.err().unwrap().to_string();
            assert!(
                msg.contains("shard_count mismatch"),
                "expected shard_count mismatch error, got: {msg}"
            );
        }
    }
}