mace-kv 0.2.0

A pure-Rust lightweight alternative to RocksDB (in most cases)
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
//! fail-stop invariant checking.
//!
//! `must_ok!`, `must_exist!` and `must_true!` (with `hot_true!` as the debug/`extra_check`
//! wrapper) report the failed invariant, its caller, the captured arguments and a backtrace
//! on stderr, flush every stream, and then abort the process.
//!
//! an invariant failure is not recoverable: unwinding would run destructors and hand control
//! back to code that assumes the invariant held, so the failure is fail-stop. the report is
//! emitted before the abort so an operator keeps the evidence, and the release profile
//! already aborts on panic (`panic = "abort"`), which this makes uniform across profiles.

use backtrace::Backtrace;
use std::fmt::Display;
use std::io::Write;
use std::panic::Location;

#[cold]
#[inline(never)]
#[track_caller]
pub fn must_ok_failed<E>(target: &str, message: &str, args: &[String], err: &E) -> !
where
    E: Display,
{
    abort_after_report("must_ok", target, message, args, Some(err))
}

#[cold]
#[inline(never)]
#[track_caller]
pub fn must_exist_failed(target: &str, message: &str, args: &[String]) -> ! {
    abort_after_report::<std::convert::Infallible>("must_exist", target, message, args, None)
}

#[cold]
#[inline(never)]
#[track_caller]
pub fn must_true_failed(target: &str, message: &str, args: &[String]) -> ! {
    abort_after_report::<std::convert::Infallible>("must_true", target, message, args, None)
}

#[cold]
#[inline(never)]
#[track_caller]
fn emit_failure<E>(kind: &str, target: &str, message: &str, args: &[String], err: Option<&E>)
where
    E: Display,
{
    let caller = Location::caller();
    eprintln!("fatal invariant failure");
    eprintln!("check={kind}");
    eprintln!("target={target}");
    eprintln!("caller={}:{}", caller.file(), caller.line());
    eprintln!("context={message}");
    for arg in args {
        eprintln!("arg={arg}");
    }
    if let Some(err) = err {
        eprintln!("error={err}");
    }
    // the process aborts instead of unwinding, so no panic hook runs: the report must
    // carry its own backtrace
    emit_backtrace(caller);
}

/// reports the failed invariant and then aborts the process
///
/// an invariant failure is not recoverable: unwinding would run user destructors and could
/// hand control back to code that assumes the invariant held. the report - the failed
/// check, its caller, the message, the captured arguments, the error and a backtrace - is
/// printed first, and the failure line names the check and its message, because no panic
/// hook runs on this path
#[cold]
#[inline(never)]
#[track_caller]
fn abort_after_report<E>(
    kind: &str,
    target: &str,
    message: &str,
    args: &[String],
    err: Option<&E>,
) -> !
where
    E: Display,
{
    emit_failure(kind, target, message, args, err);
    // the message must survive the abort even when the backtrace could not be symbolized,
    // so it is also the last line of the report
    eprintln!("fatal: {kind} failed: {message}");
    // stderr writes are unbuffered, but stdout is line buffered, and the process is about
    // to die without unwinding: flush both so the report and the program's own output
    // survive the abort
    let _ = std::io::stdout().flush();
    let _ = std::io::stderr().flush();
    std::process::abort()
}

fn emit_backtrace(caller: &Location<'_>) {
    let bt = Backtrace::new();
    let mut frames = Vec::new();

    for frame in bt.frames() {
        for symbol in frame.symbols() {
            let Some(name) = symbol.name() else {
                continue;
            };
            let name = name.to_string();
            if frames.is_empty() && is_internal_backtrace_symbol(&name) {
                continue;
            }
            frames.push((
                name,
                symbol.filename().map(|x| x.display().to_string()),
                symbol.lineno(),
                symbol.colno(),
            ));
            break;
        }
    }

    if frames.is_empty() {
        return;
    }

    eprintln!("backtrace:");
    eprintln!("   0: {}", frames[0].0);
    eprintln!(
        "             at {}:{}:{}",
        caller.file(),
        caller.line(),
        caller.column()
    );
    for (idx, (name, file, line, col)) in frames.into_iter().enumerate().skip(1) {
        eprintln!("   {idx}: {name}");
        if let (Some(file), Some(line)) = (file, line) {
            if let Some(col) = col {
                eprintln!("             at {file}:{line}:{col}");
            } else {
                eprintln!("             at {file}:{line}");
            }
        }
    }
}

fn is_internal_backtrace_symbol(name: &str) -> bool {
    name.contains("::emit_backtrace")
        || name.contains("::emit_failure")
        || name.contains("::abort_after_report")
        || name.contains("::must_true_failed")
        || name.contains("::must_exist_failed")
        || name.contains("::must_ok_failed")
        || name.starts_with("backtrace::")
}

#[macro_export]
macro_rules! must_ok {
    ($expr:expr $(,)?) => {{
        match $expr {
            Ok(value) => value,
            Err(err) => $crate::error::must_ok_failed("statement", stringify!($expr), &[], &err),
        }
    }};
    ($expr:expr, $fmt:literal $(,)?) => {{
        match $expr {
            Ok(value) => value,
            Err(err) => $crate::error::must_ok_failed("statement", &format!($fmt), &[], &err),
        }
    }};
    ($expr:expr, $fmt:literal, $($arg:expr),+ $(,)?) => {{
        match $expr {
            Ok(value) => value,
            Err(err) => $crate::error::must_ok_failed(
                "statement",
                &format!($fmt, $($arg),+),
                &[$(format!("{}={:?}", stringify!($arg), $arg)),+],
                &err,
            ),
        }
    }};
}

#[macro_export]
macro_rules! must_exist {
    ($expr:expr $(,)?) => {{
        match $expr {
            Some(value) => value,
            None => $crate::error::must_exist_failed("statement", stringify!($expr), &[]),
        }
    }};
    ($expr:expr, $fmt:literal $(,)?) => {{
        match $expr {
            Some(value) => value,
            None => $crate::error::must_exist_failed("statement", &format!($fmt), &[]),
        }
    }};
    ($expr:expr, $fmt:literal, $($arg:expr),+ $(,)?) => {{
        match $expr {
            Some(value) => value,
            None => $crate::error::must_exist_failed(
                "statement",
                &format!($fmt, $($arg),+),
                &[$(format!("{}={:?}", stringify!($arg), $arg)),+],
            ),
        }
    }};
}

#[macro_export]
macro_rules! hot_true {
    ($($tt:tt)*) => {{
        #[cfg(any(debug_assertions, feature = "extra_check"))]
        {
            $crate::must_true!($($tt)*);
        }
    }};
}

#[macro_export]
macro_rules! must_true {
    (eq $left:expr, $right:expr $(,)?) => {{
        match (&$left, &$right) {
            (left_val, right_val) => {
                if *left_val != *right_val {
                    $crate::error::must_true_failed(
                        "statement",
                        "assert_eq failed",
                        &[
                            format!("left={:?}", left_val),
                            format!("right={:?}", right_val),
                        ],
                    );
                }
            }
        }
    }};
    (eq $left:expr, $right:expr, $fmt:literal $(,)?) => {{
        match (&$left, &$right) {
            (left_val, right_val) => {
                if *left_val != *right_val {
                    $crate::error::must_true_failed(
                        "statement",
                        &format!($fmt),
                        &[
                            format!("left={:?}", left_val),
                            format!("right={:?}", right_val),
                        ],
                    );
                }
            }
        }
    }};
    (eq $left:expr, $right:expr, $fmt:literal, $($arg:expr),+ $(,)?) => {{
        match (&$left, &$right) {
            (left_val, right_val) => {
                if *left_val != *right_val {
                    $crate::error::must_true_failed(
                        "statement",
                        &format!($fmt, $($arg),+),
                        &[
                            $(format!("{}={:?}", stringify!($arg), $arg)),+,
                            format!("left={:?}", left_val),
                            format!("right={:?}", right_val),
                        ],
                    );
                }
            }
        }
    }};
    (ne $left:expr, $right:expr $(,)?) => {{
        match (&$left, &$right) {
            (left_val, right_val) => {
                if *left_val == *right_val {
                    $crate::error::must_true_failed(
                        "statement",
                        "assert_ne failed",
                        &[
                            format!("left={:?}", left_val),
                            format!("right={:?}", right_val),
                        ],
                    );
                }
            }
        }
    }};
    (ne $left:expr, $right:expr, $fmt:literal $(,)?) => {{
        match (&$left, &$right) {
            (left_val, right_val) => {
                if *left_val == *right_val {
                    $crate::error::must_true_failed(
                        "statement",
                        &format!($fmt),
                        &[
                            format!("left={:?}", left_val),
                            format!("right={:?}", right_val),
                        ],
                    );
                }
            }
        }
    }};
    (ne $left:expr, $right:expr, $fmt:literal, $($arg:expr),+ $(,)?) => {{
        match (&$left, &$right) {
            (left_val, right_val) => {
                if *left_val == *right_val {
                    $crate::error::must_true_failed(
                        "statement",
                        &format!($fmt, $($arg),+),
                        &[
                            $(format!("{}={:?}", stringify!($arg), $arg)),+,
                            format!("left={:?}", left_val),
                            format!("right={:?}", right_val),
                        ],
                    );
                }
            }
        }
    }};
    ($expr:expr $(,)?) => {{
        match $expr {
            true => (),
            false => $crate::error::must_true_failed("statement", stringify!($expr), &[]),
        }
    }};
    ($expr:expr, $fmt:literal $(,)?) => {{
        match $expr {
            true => (),
            false => $crate::error::must_true_failed("statement", &format!($fmt), &[]),
        }
    }};
    ($expr:expr, $fmt:literal, $($arg:expr),+ $(,)?) => {{
        match $expr {
            true => (),
            false => $crate::error::must_true_failed(
                "statement",
                &format!($fmt, $($arg),+),
                &[$(format!("{}={:?}", stringify!($arg), $arg)),+],
            ),
        }
    }};
}

#[cfg(test)]
pub(crate) mod test_support {
    use std::process::{Command, Output};

    /// set for the child process of an abort test
    pub(crate) const ENV_CHILD: &str = "MACE_ABORT_TEST_CHILD";

    /// whether this process is the child of an abort test
    pub(crate) fn is_abort_child() -> bool {
        std::env::var(ENV_CHILD).ok().as_deref() == Some("1")
    }

    /// runs one test of this binary in a child process and returns its output
    ///
    /// a fatal invariant failure aborts the process, so a test that pins that behavior
    /// cannot observe the outcome in-process. the abort also skips the child's `Drop`, so
    /// the child is given a temp root this parent process removes once the child has exited
    pub(crate) fn run_child_test(name: &str) -> Output {
        let child_root = crate::RandomPath::tmp();
        std::fs::create_dir_all(&*child_root).expect("child temp root must be created");
        let exe = std::env::current_exe().expect("current exe");
        let mut cmd = match cargo_runner() {
            Some(parts) => {
                let mut it = parts.into_iter();
                let mut cmd = Command::new(it.next().expect("runner must not be empty"));
                cmd.args(it);
                cmd.arg(exe);
                cmd
            }
            None => Command::new(exe),
        };
        cmd.arg("--exact")
            .arg(name)
            .arg("--nocapture")
            .env(ENV_CHILD, "1")
            .env(crate::RandomPath::TMP_ROOT_ENV, &*child_root)
            .output()
            .expect("child test must run")
    }

    /// the runner configured for a cross-compiled test target, if any
    fn cargo_runner() -> Option<Vec<String>> {
        let arch = std::env::consts::ARCH
            .to_ascii_uppercase()
            .replace('-', "_");
        let mut preferred: Vec<(String, String)> = Vec::new();
        let mut all: Vec<(String, String)> = Vec::new();
        for (key, value) in std::env::vars() {
            if !key.starts_with("CARGO_TARGET_") || !key.ends_with("_RUNNER") {
                continue;
            }
            if value.trim().is_empty() {
                continue;
            }
            if key.contains(&format!("_{arch}_")) {
                preferred.push((key.clone(), value.clone()));
            }
            all.push((key, value));
        }
        preferred.sort_by(|a, b| a.0.cmp(&b.0));
        all.sort_by(|a, b| a.0.cmp(&b.0));
        let chosen = preferred.into_iter().next().or_else(|| {
            if all.len() == 1 {
                all.into_iter().next()
            } else {
                None
            }
        })?;
        let parts: Vec<String> = chosen.1.split_whitespace().map(str::to_string).collect();
        (!parts.is_empty()).then_some(parts)
    }

    /// asserts that the child aborted and printed the expected report
    pub(crate) fn assert_aborted(output: &Output, expected: &[&str]) {
        #[cfg(unix)]
        {
            use std::os::unix::process::ExitStatusExt;
            assert_eq!(
                output.status.signal(),
                Some(6),
                "the child must abort, got {:?}",
                output.status
            );
        }
        #[cfg(not(unix))]
        assert!(
            !output.status.success(),
            "the child must fail, got {:?}",
            output.status
        );
        let stderr = String::from_utf8_lossy(&output.stderr);
        for needle in expected {
            assert!(
                stderr.contains(needle),
                "the report must contain {needle:?} before the abort, got:\n{stderr}"
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::test_support::{assert_aborted, is_abort_child, run_child_test};

    /// a fatal invariant failure prints the report and then aborts the process, so the
    /// report is what an operator sees and no destructor runs afterwards
    #[test]
    fn a_fatal_failure_aborts_the_process() {
        let output = run_child_test("error::tests::fatal_failure_child");
        assert_aborted(
            &output,
            &[
                "fatal invariant failure",
                "check=must_true",
                "context=child invariant",
                "backtrace:",
                // the failure line carries the message and is printed by the abort path
                "fatal: must_true failed: child invariant",
            ],
        );
    }

    /// the aborting side of the test above: it runs only in the child process
    #[test]
    fn fatal_failure_child() {
        if !is_abort_child() {
            return;
        }
        crate::must_true!(false, "child invariant");
    }
}