ursula-config 0.5.0

Ursula configuration types and TOML loading.
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#[cfg(test)]
mod human_tests {
    use std::time::Duration;

    use crate::human::HumanDuration;
    use crate::human::HumanSize;

    #[test]
    fn human_duration_parses_valid_inputs() {
        let cases: &[(&str, Duration)] = &[
            ("30s", Duration::from_secs(30)),
            ("250ms", Duration::from_millis(250)),
            ("2h", Duration::from_secs(7200)),
            ("1d", Duration::from_secs(86400)),
        ];
        for (input, expected) in cases {
            assert_eq!(
                input.parse::<HumanDuration>().unwrap().as_duration(),
                *expected,
                "input {input:?}"
            );
        }
    }

    #[test]
    fn human_duration_rejects_invalid_inputs() {
        for input in ["1.5ms", "30x", "-1s"] {
            assert!(
                input.parse::<HumanDuration>().is_err(),
                "input {input:?} should be rejected"
            );
        }
    }

    #[test]
    fn human_duration_from_toml_values() {
        let raw: toml::Value = toml::Value::Integer(30000);
        let dur: HumanDuration = raw.try_into().unwrap();
        assert_eq!(dur.as_duration(), Duration::from_millis(30000));

        let raw: toml::Value = toml::Value::String("5m".into());
        let dur: HumanDuration = raw.try_into().unwrap();
        assert_eq!(dur.as_duration(), Duration::from_secs(300));
    }

    #[test]
    fn human_duration_display_roundtrip() {
        let dur = HumanDuration::sec(60);
        assert_eq!(dur.to_string(), "1m");
        assert_eq!(
            dur.to_string()
                .parse::<HumanDuration>()
                .unwrap()
                .as_duration(),
            Duration::from_secs(60)
        );
    }

    #[test]
    fn human_size_parses_valid_inputs() {
        let cases: &[(&str, u64)] = &[
            ("100B", 100),
            ("1KiB", 1024),
            ("1MiB", 1024 * 1024),
            ("1GiB", 1024 * 1024 * 1024),
            ("256MiB", 256 * 1024 * 1024),
            ("1.5GiB", (1.5 * 1024.0 * 1024.0 * 1024.0) as u64),
        ];
        for (input, expected) in cases {
            assert_eq!(
                input.parse::<HumanSize>().unwrap().as_bytes(),
                *expected,
                "input {input:?}"
            );
        }
    }

    #[test]
    fn human_size_rejects_invalid_inputs() {
        for input in ["30x", "-1MiB", "99999999999999999999GiB"] {
            assert!(
                input.parse::<HumanSize>().is_err(),
                "input {input:?} should be rejected"
            );
        }
    }

    #[test]
    fn human_size_from_toml_values() {
        let raw: toml::Value = toml::Value::Integer(67108864);
        let size: HumanSize = raw.try_into().unwrap();
        assert_eq!(size.as_bytes(), 67108864);

        let raw: toml::Value = toml::Value::String("128MiB".into());
        let size: HumanSize = raw.try_into().unwrap();
        assert_eq!(size.as_bytes(), 128 * 1024 * 1024);
    }

    #[test]
    fn human_size_display_roundtrip() {
        let size = HumanSize::gib(1);
        assert_eq!(size.to_string(), "1GiB");
        assert_eq!(
            size.to_string().parse::<HumanSize>().unwrap().as_bytes(),
            1024 * 1024 * 1024
        );
    }
}

#[cfg(test)]
mod config_tests {
    use crate::config::UrsulaConfig;

    #[test]
    fn deserialize_minimal_config() {
        let toml = r#"
[server]
listen = "0.0.0.0:4437"

[runtime]
core_count = 16

[raft]
group_count = 256

[raft.wal]
backend = "disk"
path = "/var/lib/ursula"

[[raft.peers]]
node_id = 1
url = "http://10.0.0.1:4437"

[storage.cold]
backend = "s3"
flush_interval = "30s"
flush_size = "64MiB"

[storage.cold.s3]
bucket = "my-bucket"
region = "us-east-1"

[storage.snapshot]
backend = "s3"
"#;
        let config: UrsulaConfig = toml::from_str(toml).expect("valid config");
        assert_eq!(config.server.listen, "0.0.0.0:4437");
        assert_eq!(config.runtime.core_count, 16);
        // node_id is not in the file — it comes from CLI --node-id at runtime
        assert_eq!(config.raft.node_id, 0); // serde default
        assert_eq!(config.raft.group_count, 256);
        use crate::config::ColdBackend;
        use crate::config::RaftSnapshotBackend;
        use crate::config::WalBackend;
        assert_eq!(config.raft.wal.backend, WalBackend::Disk);
        assert_eq!(config.storage.cold.backend, ColdBackend::S3);
        assert_eq!(config.storage.snapshot.backend, RaftSnapshotBackend::S3);
        assert_eq!(config.raft.snapshot_build_max_concurrency, 1);
        assert_eq!(
            config.storage.cold.s3.as_ref().unwrap().bucket,
            Some("my-bucket".into())
        );
    }

    #[test]
    fn raft_snapshot_build_concurrency_is_configurable() {
        let config: UrsulaConfig = toml::from_str(
            r#"
[raft]
snapshot_build_max_concurrency = 2
"#,
        )
        .expect("snapshot_build_max_concurrency parses");

        assert_eq!(config.raft.snapshot_build_max_concurrency, 2);
    }

    #[test]
    fn raft_snapshot_log_retention_is_bounded_and_configurable() {
        let default = UrsulaConfig::default();
        assert_eq!(default.raft.snapshot_logs_since_last, 5_000);
        assert_eq!(default.raft.snapshot_pressure_unpurged_logs, 65_536);
        assert_eq!(default.raft.snapshot_pressure_max_groups_per_tick, 16);
        assert_eq!(default.raft.max_in_snapshot_log_to_keep, 64);

        let config: UrsulaConfig = toml::from_str(
            r#"
[raft]
snapshot_logs_since_last = 20000
snapshot_pressure_unpurged_logs = 131072
snapshot_pressure_max_groups_per_tick = 32
max_in_snapshot_log_to_keep = 128
"#,
        )
        .expect("max_in_snapshot_log_to_keep parses");

        assert_eq!(config.raft.snapshot_logs_since_last, 20_000);
        assert_eq!(config.raft.snapshot_pressure_unpurged_logs, 131_072);
        assert_eq!(config.raft.snapshot_pressure_max_groups_per_tick, 32);
        assert_eq!(config.raft.max_in_snapshot_log_to_keep, 128);
    }

    #[test]
    fn snapshot_drive_interval_is_optional_and_zero_is_explicit_disable() {
        use crate::human::HumanDuration;

        let omitted: UrsulaConfig = toml::from_str(
            r#"
[storage.snapshot]
backend = "s3"
"#,
        )
        .expect("omitted drive_interval parses");
        assert_eq!(omitted.storage.snapshot.drive_interval, None);

        let disabled: UrsulaConfig = toml::from_str(
            r#"
[storage.snapshot]
backend = "s3"
drive_interval = "0s"
"#,
        )
        .expect("explicit zero drive_interval parses");
        assert_eq!(
            disabled.storage.snapshot.drive_interval,
            Some(HumanDuration::milli(0))
        );

        let explicit: UrsulaConfig = toml::from_str(
            r#"
[storage.snapshot]
backend = "s3"
drive_interval = "45s"
"#,
        )
        .expect("explicit non-zero drive_interval parses");
        assert_eq!(
            explicit.storage.snapshot.drive_interval,
            Some(HumanDuration::sec(45))
        );
    }

    #[test]
    fn cold_cache_config_has_omitted_zero_and_custom_states() {
        use crate::human::HumanSize;

        let omitted: UrsulaConfig = toml::from_str(
            r#"
[storage.cold]
backend = "memory"
"#,
        )
        .expect("omitted cache parses");
        assert_eq!(omitted.storage.cold.cache, None);

        let disabled: UrsulaConfig = toml::from_str(
            r#"
[storage.cold]
backend = "memory"

[storage.cold.cache]
max_size = "0B"
"#,
        )
        .expect("zero cache parses");
        assert_eq!(
            disabled.storage.cold.cache.unwrap().max_size,
            HumanSize::bytes(0)
        );

        let custom: UrsulaConfig = toml::from_str(
            r#"
[storage.cold]
backend = "memory"

[storage.cold.cache]
max_size = "12MiB"
block_size = "2MiB"
readahead_blocks = 7
"#,
        )
        .expect("custom cache parses");
        let cache = custom.storage.cold.cache.unwrap();
        assert_eq!(cache.max_size, HumanSize::mib(12));
        assert_eq!(cache.block_size, HumanSize::mib(2));
        assert_eq!(cache.readahead_blocks, 7);
    }
}

#[cfg(test)]
mod load_tests {
    use std::io::Write;

    use crate::config::WalBackend;
    use crate::load::load_config;
    use crate::preset::Preset;

    /// Write `contents` to a fresh temp file with the given suffix.
    fn temp_config(suffix: &str, contents: &str) -> tempfile::NamedTempFile {
        let mut tmp = tempfile::NamedTempFile::with_suffix(suffix).unwrap();
        write!(tmp, "{contents}").unwrap();
        tmp
    }

    fn available_cores() -> usize {
        std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(4)
    }

    #[test]
    fn load_minimal_config_without_preset() {
        let tmp = temp_config(
            ".toml",
            r#"
[server]
listen = "127.0.0.1:4437"

[runtime]
core_count = 4

[raft]
group_count = 16

[raft.wal]
backend = "memory"
"#,
        );
        let config = load_config(Some(tmp.path()), None, Some(1)).unwrap();
        assert_eq!(config.runtime.core_count, 4);
        assert_eq!(config.raft.node_id, 1);
        assert_eq!(config.raft.wal.backend, WalBackend::Memory);
    }

    #[test]
    fn preset_tiny_overrides_defaults() {
        let tmp = temp_config(
            ".toml",
            r#"
[server]
listen = "127.0.0.1:4437"
"#,
        );
        let config = load_config(Some(tmp.path()), Some(Preset::Tiny), Some(1)).unwrap();
        // preset no longer overrides core_count
        assert_eq!(config.runtime.core_count, available_cores());
        assert_eq!(config.raft.group_count, 64); // from tiny preset
        assert_eq!(config.raft.wal.backend, WalBackend::Memory); // from tiny preset
    }

    #[test]
    fn user_config_overrides_preset() {
        let tmp = temp_config(
            ".toml",
            r#"
[runtime]
core_count = 2
"#,
        );
        let config = load_config(Some(tmp.path()), Some(Preset::Tiny), Some(1)).unwrap();
        assert_eq!(config.runtime.core_count, 2); // user overrides preset's 4
        assert_eq!(config.raft.group_count, 64); // preset still applies
    }

    #[test]
    fn validation_rejects_disk_without_path() {
        let tmp = temp_config(
            ".toml",
            r#"
[raft.wal]
backend = "disk"
"#,
        );
        let err = load_config(Some(tmp.path()), None, Some(1)).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("raft.wal.path"),
            "error should mention raft.wal.path: {msg}"
        );
    }

    #[test]
    fn validation_rejects_volatile_multi_peer_without_opt_in() {
        let tmp = temp_config(
            ".toml",
            r#"
[raft.wal]
backend = "memory"

[[raft.peers]]
node_id = 1
url = "http://127.0.0.1:4437"

[[raft.peers]]
node_id = 2
url = "http://127.0.0.1:4438"
"#,
        );
        let err = load_config(Some(tmp.path()), None, Some(1)).unwrap_err();
        assert!(
            err.to_string().contains("allow_volatile_multi_peer"),
            "error should name the explicit opt-in: {err}"
        );
    }

    #[test]
    fn volatile_multi_peer_explicit_opt_in_is_accepted() {
        let tmp = temp_config(
            ".toml",
            r#"
[raft.wal]
backend = "memory"
allow_volatile_multi_peer = true

[[raft.peers]]
node_id = 1
url = "http://127.0.0.1:4437"

[[raft.peers]]
node_id = 2
url = "http://127.0.0.1:4438"
"#,
        );
        let config = load_config(Some(tmp.path()), None, Some(1)).expect("explicit opt-in");
        assert!(config.raft.wal.allow_volatile_multi_peer);
    }

    #[test]
    fn validation_rejects_disk_pressure_resume_at_or_below_minimum() {
        let tmp = temp_config(
            ".toml",
            r#"
[raft.wal]
backend = "disk"
path = "/tmp/ursula-wal"
min_available_size = "1GiB"
resume_available_size = "512MiB"
"#,
        );
        let err = load_config(Some(tmp.path()), None, Some(1)).unwrap_err();
        assert!(
            err.to_string().contains("resume_available_size"),
            "error should name the invalid watermark: {err}"
        );
    }

    #[test]
    fn validation_rejects_s3_without_bucket() {
        let tmp = temp_config(
            ".toml",
            r#"
[storage.cold]
backend = "s3"
"#,
        );
        let err = load_config(Some(tmp.path()), None, Some(1)).unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("bucket"), "error should mention bucket: {msg}");
    }

    #[test]
    fn nested_table_merge() {
        let tmp = temp_config(
            ".toml",
            r#"
[storage.cold.cache]
max_size = "128MiB"
"#,
        );
        let config = load_config(Some(tmp.path()), Some(Preset::Tiny), Some(1)).unwrap();
        // tiny preset sets cache.max_size = "64MiB", user overrides to "128MiB"
        assert_eq!(
            config
                .storage
                .cold
                .cache
                .as_ref()
                .unwrap()
                .max_size
                .as_bytes(),
            128 * 1024 * 1024
        );
        // but tiny preset also sets flush_size = "4MiB", which should still apply
        assert_eq!(config.storage.cold.flush_size.as_bytes(), 4 * 1024 * 1024);
    }

    #[test]
    fn array_replacement_not_append() {
        let tmp = temp_config(
            ".toml",
            r#"
[raft.wal]
allow_volatile_multi_peer = true

[[raft.peers]]
node_id = 1
url = "http://10.0.0.1:4437"

[[raft.peers]]
node_id = 2
url = "http://10.0.0.2:4437"
"#,
        );
        let config = load_config(Some(tmp.path()), None, Some(1)).unwrap();
        assert_eq!(config.raft.peers.len(), 2);
        assert_eq!(config.raft.peers[0].node_id, 1);
        assert_eq!(config.raft.peers[1].node_id, 2);
    }

    #[test]
    fn node_id_from_cli_overrides_file() {
        let tmp = temp_config(
            ".toml",
            r#"
[raft]
node_id = 1
"#,
        );
        // CLI --node-id 42 overrides file's node_id = 1
        let config = load_config(Some(tmp.path()), None, Some(42)).unwrap();
        assert_eq!(config.raft.node_id, 42);
    }

    #[test]
    fn validation_rejects_missing_node_id() {
        let tmp = temp_config(
            ".toml",
            r#"
[server]
listen = "127.0.0.1:4437"
"#,
        );
        let err = load_config(Some(tmp.path()), None, None).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("node_id") && msg.contains("--node-id"),
            "error should mention --node-id: {msg}"
        );
    }

    /// Verify that a Default config survives a serde round-trip without
    /// information loss.  This is the foundation that makes `merge_tables`
    /// safe: preset defaults are serialised to a TOML table, merged with
    /// the user's partial TOML, and then deserialised back.
    #[test]
    fn preset_roundtrip_equality() {
        use crate::UrsulaConfig;
        use crate::preset::Preset;

        let preset = Preset::Standard;
        let original = UrsulaConfig::from(preset);

        // 1. Serialise preset to TOML AST
        let value = toml::Value::try_from(&original).expect("serialise");
        let table = value.as_table().cloned().expect("is table");

        // 2. Merge with an empty user table (no-op)
        let mut merged = table.clone();
        crate::load::merge_tables_for_test(&mut merged, toml::Table::new());

        // 3. Deserialise back
        let restored: UrsulaConfig = merged.try_into().expect("deserialise");

        // Scalar fields must be identical
        assert_eq!(original.server.listen, restored.server.listen);
        assert_eq!(original.runtime.core_count, restored.runtime.core_count);
        assert_eq!(original.raft.group_count, restored.raft.group_count);
        assert_eq!(
            original.raft.rejoin_probe.as_duration(),
            restored.raft.rejoin_probe.as_duration()
        );
        assert_eq!(
            original.storage.cold.flush_size.as_bytes(),
            restored.storage.cold.flush_size.as_bytes()
        );
        assert_eq!(
            original
                .storage
                .cold
                .cache
                .as_ref()
                .unwrap()
                .max_size
                .as_bytes(),
            restored
                .storage
                .cold
                .cache
                .as_ref()
                .unwrap()
                .max_size
                .as_bytes()
        );
    }

    #[test]
    fn preset_alone_without_config_file() {
        let config = load_config(None, Some(Preset::Tiny), Some(1)).unwrap();
        assert_eq!(config.runtime.core_count, available_cores());
        assert_eq!(config.raft.group_count, 64);
        assert_eq!(config.raft.node_id, 1);
        assert_eq!(
            config
                .storage
                .cold
                .cache
                .as_ref()
                .unwrap()
                .max_size
                .as_bytes(),
            64 * 1024 * 1024
        );
    }

    #[test]
    fn presets_match_legacy_profiles() {
        let mib = |n: u64| n * 1024 * 1024;
        // (preset, name, (live_read_waiters, group_count, uncommitted_bytes,
        //  http_inflight_bytes, flush_bytes, flush_concurrency, hot_bytes))
        let cases = [
            (
                Preset::Tiny,
                "tiny",
                (Some(8_192), 64, mib(8), mib(64), mib(4), 2, mib(8)),
            ),
            (
                Preset::Standard,
                "standard",
                (Some(65_536), 256, mib(64), mib(256), mib(8), 4, mib(64)),
            ),
        ];
        for (preset, name, expected) in cases {
            let config = load_config(None, Some(preset), Some(1)).unwrap();
            assert_eq!(
                config.runtime.core_count,
                available_cores(),
                "preset {name}: core_count"
            );
            let actual = (
                config.runtime.live_read_max_waiters_per_core,
                config.raft.group_count,
                config
                    .raft
                    .max_uncommitted_size_per_group
                    .unwrap()
                    .as_bytes(),
                config.server.http_inflight_body_size.as_bytes(),
                config.storage.cold.flush_size.as_bytes(),
                config.storage.cold.flush_max_concurrency,
                config
                    .storage
                    .cold
                    .max_hot_size_per_group
                    .unwrap()
                    .as_bytes(),
            );
            // Tuple order: (live_read_waiters, group_count, uncommitted,
            // http_inflight, flush_size, flush_concurrency, max_hot_size).
            assert_eq!(actual, expected, "preset {name}");
        }
    }
    #[test]
    fn s3_server_side_encryption_defaults_to_aes256() {
        use crate::config::S3ServerSideEncryption;
        use crate::config::UrsulaConfig;
        let config: UrsulaConfig = toml::from_str(
            r#"
[storage.cold]
backend = "s3"

[storage.cold.s3]
bucket = "my-bucket"
"#,
        )
        .expect("valid config");
        let s3 = config.storage.cold.s3.expect("s3 config");
        assert_eq!(s3.server_side_encryption, S3ServerSideEncryption::Aes256);
        assert_eq!(s3.kms_key_id, None);
    }

    #[test]
    fn s3_server_side_encryption_parses_all_modes() {
        use crate::config::S3ServerSideEncryption;
        use crate::config::UrsulaConfig;
        for (value, expected) in [
            ("aes256", S3ServerSideEncryption::Aes256),
            ("aws-kms", S3ServerSideEncryption::AwsKms),
            ("none", S3ServerSideEncryption::None),
        ] {
            let toml = format!(
                r#"
[storage.cold]
backend = "s3"

[storage.cold.s3]
bucket = "my-bucket"
server_side_encryption = "{value}"
"#
            );
            let config: UrsulaConfig = toml::from_str(&toml).expect("valid config");
            let s3 = config.storage.cold.s3.expect("s3 config");
            assert_eq!(s3.server_side_encryption, expected, "mode {value}");
        }
    }

    #[test]
    fn s3_kms_key_id_round_trips() {
        use crate::config::S3ServerSideEncryption;
        use crate::config::UrsulaConfig;
        let config: UrsulaConfig = toml::from_str(
            r#"
[storage.cold]
backend = "s3"

[storage.cold.s3]
bucket = "my-bucket"
server_side_encryption = "aws-kms"
kms_key_id = "arn:aws:kms:us-east-1:111122223333:key/test"
"#,
        )
        .expect("valid config");
        let s3 = config.storage.cold.s3.expect("s3 config");
        assert_eq!(s3.server_side_encryption, S3ServerSideEncryption::AwsKms);
        assert_eq!(
            s3.kms_key_id.as_deref(),
            Some("arn:aws:kms:us-east-1:111122223333:key/test")
        );
    }
}