dynomite-engine 0.0.2

Embeddable Dynamo-style distributed replication engine: token-ring partitioning, gossip cluster, hinted handoff, anti-entropy, RediSearch FT.* surface.
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
//! Plaintext, postmortem-shaped dump of one node's state.
//!
//! The output mirrors what an operator wants to attach to a bug
//! report: build info, the sanitized config, the ring view, the
//! peer table, queue depths, gossip churn, recent events, and a
//! coarse memory / file-descriptor accounting block. The shape is
//! deliberately ASCII-only and grep-friendly: each section opens
//! with a `=== <name> ===` header followed by `key=value` lines or
//! per-row entries; no JSON, no YAML, no smart quotes.
//!
//! [`ClusterInfoSnapshot`] is the value type: every field is owned
//! and `Clone`. [`format_text`] renders a snapshot to any
//! [`std::io::Write`].
//!
//! Two helpers exist to obtain a snapshot:
//!
//! * [`ClusterInfoSnapshot::synthetic`] - hand-built for tests.
//! * [`gather_from_handle`] (re-exported below) - assembles a
//!   snapshot from a live [`crate::embed::ServerHandle`] plus a
//!   [`RecentEvents`] ring buffer; this is what dynomited wires
//!   up at boot.
//!
//! The snapshot deliberately does not include any secret fields:
//! Redis `requirepass` values, AUTH passwords, and the contents
//! of TLS / reconciliation key files are omitted. Paths to those
//! files are included so an operator can see whether the node is
//! configured for secure mode without leaking the material.

use std::collections::VecDeque;
use std::fmt::Write as _;
use std::io::{self, Write};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

use parking_lot::Mutex;

use crate::cluster::peer::PeerState;
use crate::conf::ConfPool;
use crate::embed::ServerHandle;
use crate::stats::PoolField;

/// Maximum number of recent events retained in the ring buffer.
///
/// # Examples
///
/// ```
/// assert_eq!(dynomite::admin::cluster_info::MAX_RECENT_EVENTS, 50);
/// ```
pub const MAX_RECENT_EVENTS: usize = 50;

/// One section of the dump expressed as a name plus an ordered
/// list of `(key, value)` pairs. Sections always render the same
/// way, so authors can append a new field without touching the
/// formatter.
///
/// # Examples
///
/// ```
/// use dynomite::admin::cluster_info::Section;
/// let s = Section::with_pairs("build", &[("version", "0.0.1")]);
/// assert_eq!(s.name(), "build");
/// assert_eq!(s.pairs().len(), 1);
/// ```
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Section {
    name: String,
    pairs: Vec<(String, String)>,
}

impl Section {
    /// Build a section with the supplied name and list of
    /// `(key, value)` pairs. Both keys and values are copied
    /// into owned strings so the section may outlive its inputs.
    ///
    /// # Examples
    ///
    /// ```
    /// use dynomite::admin::cluster_info::Section;
    /// let s = Section::with_pairs("hello", &[("k", "v")]);
    /// assert_eq!(s.pairs()[0].1, "v");
    /// ```
    #[must_use]
    pub fn with_pairs(name: &str, pairs: &[(&str, &str)]) -> Self {
        Self {
            name: name.to_string(),
            pairs: pairs
                .iter()
                .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
                .collect(),
        }
    }

    /// Section name (without the framing `===` markers).
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Borrow the ordered pair list.
    #[must_use]
    pub fn pairs(&self) -> &[(String, String)] {
        &self.pairs
    }

    /// Append a `(key, value)` pair to the section.
    pub fn push<K: Into<String>, V: Into<String>>(&mut self, key: K, value: V) {
        self.pairs.push((key.into(), value.into()));
    }
}

/// One free-form row inside the `peers` or `recent_events`
/// section. The text has already been rendered to an ASCII line.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct RowSection {
    name: String,
    rows: Vec<String>,
}

impl RowSection {
    /// Build an empty row-section.
    #[must_use]
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            rows: Vec::new(),
        }
    }

    /// Section name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Borrow the row list.
    #[must_use]
    pub fn rows(&self) -> &[String] {
        &self.rows
    }

    /// Append a row.
    pub fn push<S: Into<String>>(&mut self, row: S) {
        self.rows.push(row.into());
    }
}

/// One entry in the recent-events ring buffer.
///
/// Stored as a flat tuple so the producer (the gossip / lifecycle
/// taps) does not have to depend on the cluster-info module to
/// emit events.
///
/// # Examples
///
/// ```
/// use dynomite::admin::cluster_info::RecentEvent;
/// let e = RecentEvent::new(1_700_000_000, "peer_up", "arnold");
/// assert_eq!(e.kind, "peer_up");
/// assert_eq!(e.detail, "arnold");
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RecentEvent {
    /// Wall-clock time (seconds since the unix epoch).
    pub ts_secs: u64,
    /// Short event kind: `peer_up`, `peer_down`, `gossip_round`,
    /// `config_reloaded`, `restart`. Free-form ASCII; consumers
    /// match by exact string.
    pub kind: String,
    /// Optional payload. ASCII; spaces are allowed.
    pub detail: String,
}

impl RecentEvent {
    /// Build a new event.
    #[must_use]
    pub fn new<K: Into<String>, D: Into<String>>(ts_secs: u64, kind: K, detail: D) -> Self {
        Self {
            ts_secs,
            kind: kind.into(),
            detail: detail.into(),
        }
    }
}

/// Bounded ring buffer of recent lifecycle events.
///
/// The ring is held inside an [`Arc<Mutex<...>>`] so the gossip
/// task and the dump endpoint can share it without contention on
/// the steady-state path. New entries push to the back; once the
/// length reaches [`MAX_RECENT_EVENTS`] the oldest entry is
/// dropped.
///
/// # Examples
///
/// ```
/// use dynomite::admin::cluster_info::{RecentEvent, RecentEvents};
/// let log = RecentEvents::new();
/// log.push(RecentEvent::new(1, "restart", ""));
/// assert_eq!(log.snapshot().len(), 1);
/// ```
#[derive(Clone, Debug, Default)]
pub struct RecentEvents {
    inner: Arc<Mutex<VecDeque<RecentEvent>>>,
}

impl RecentEvents {
    /// Construct a fresh, empty ring.
    #[must_use]
    pub fn new() -> Self {
        Self {
            inner: Arc::new(Mutex::new(VecDeque::with_capacity(MAX_RECENT_EVENTS))),
        }
    }

    /// Append `event`, dropping the oldest entry when the ring
    /// is full.
    pub fn push(&self, event: RecentEvent) {
        let mut guard = self.inner.lock();
        if guard.len() == MAX_RECENT_EVENTS {
            guard.pop_front();
        }
        guard.push_back(event);
    }

    /// Cloned snapshot of the ring contents (oldest first).
    #[must_use]
    pub fn snapshot(&self) -> Vec<RecentEvent> {
        self.inner.lock().iter().cloned().collect()
    }

    /// Number of events currently buffered.
    #[must_use]
    pub fn len(&self) -> usize {
        self.inner.lock().len()
    }

    /// True when the ring has no entries.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.inner.lock().is_empty()
    }
}

/// Owned snapshot ready to be rendered.
///
/// Sections are stored in the canonical render order:
/// `build`, `config`, `ring`, `peers`, `queues`, `gossip`,
/// `recent_events`, `memory`, `fds`. Producers should fill every
/// section even when the underlying data is unavailable; the
/// formatter happily emits empty sections so the resulting text
/// has a stable shape across releases.
///
/// # Examples
///
/// ```
/// use dynomite::admin::cluster_info::ClusterInfoSnapshot;
/// let snap = ClusterInfoSnapshot::synthetic();
/// assert_eq!(snap.build.name(), "build");
/// ```
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ClusterInfoSnapshot {
    /// Build identity: version, git sha, build profile.
    pub build: Section,
    /// Sanitized configuration view.
    pub config: Section,
    /// Ring distribution / vnode counts.
    pub ring: Section,
    /// Per-peer rows.
    pub peers: RowSection,
    /// Queue-depth gauges.
    pub queues: Section,
    /// Gossip churn metrics.
    pub gossip: Section,
    /// Bounded ring of lifecycle events.
    pub recent_events: RowSection,
    /// Memory accounting block.
    pub memory: Section,
    /// Open file-descriptor accounting block.
    pub fds: Section,
}

impl ClusterInfoSnapshot {
    /// Hand-built snapshot useful for unit tests and the doc
    /// example. Every section is populated with a single
    /// representative entry.
    ///
    /// # Examples
    ///
    /// ```
    /// use dynomite::admin::cluster_info::{format_text, ClusterInfoSnapshot};
    /// let snap = ClusterInfoSnapshot::synthetic();
    /// let mut out = Vec::new();
    /// format_text(&snap, &mut out).unwrap();
    /// let text = String::from_utf8(out).unwrap();
    /// assert!(text.contains("=== build ==="));
    /// ```
    #[must_use]
    pub fn synthetic() -> Self {
        let mut snap = Self {
            build: Section::with_pairs(
                "build",
                &[
                    ("version", "0.0.1"),
                    ("git_sha", "unknown"),
                    ("build_profile", "debug"),
                ],
            ),
            config: Section::with_pairs(
                "config",
                &[
                    ("data_store", "redis"),
                    ("listen", "0.0.0.0:8102"),
                    ("dyn_listen", "0.0.0.0:8101"),
                    ("rack", "rack-1"),
                    ("dc", "dc-1"),
                ],
            ),
            ring: Section::with_pairs(
                "ring",
                &[
                    ("distribution", "vnode"),
                    ("vnodes", "1"),
                    ("tokens_per_node", "1"),
                ],
            ),
            peers: RowSection::new("peers"),
            queues: Section::with_pairs(
                "queues",
                &[
                    ("dispatcher_inflight", "0"),
                    ("backend_supervisor_pending", "0"),
                    ("hint_store_size", "0"),
                ],
            ),
            gossip: Section::with_pairs(
                "gossip",
                &[
                    ("churn_total", "0"),
                    ("phi_alarms_total", "0"),
                    ("heartbeats_sent", "0"),
                    ("heartbeats_received", "0"),
                ],
            ),
            recent_events: RowSection::new("recent_events"),
            memory: Section::with_pairs("memory", &[("rss_bytes", "unavailable")]),
            fds: Section::with_pairs("fds", &[("total", "unavailable")]),
        };
        snap.peers.push(
            "peer_id=local dc=dc-1 rack=rack-1 status=Normal phi=0.00 last_seen_ms=0 tokens=1",
        );
        snap.recent_events
            .push("1970-01-01T00:00:00Z restart local-bootstrap");
        snap
    }
}

/// Render `snap` to `w` as ASCII text. Each section is preceded
/// by a `=== <name> ===` header line, then either `key=value`
/// pairs (one per line) or free-form rows. Sections render in
/// the canonical order documented on
/// [`ClusterInfoSnapshot`]; each section ends with a single
/// trailing blank line so consumers can split on `\n\n`.
///
/// # Errors
///
/// Returns the underlying [`std::io::Error`] when the writer
/// rejects a write.
///
/// # Examples
///
/// ```
/// use dynomite::admin::cluster_info::{format_text, ClusterInfoSnapshot};
/// let snap = ClusterInfoSnapshot::synthetic();
/// let mut out = Vec::new();
/// format_text(&snap, &mut out).unwrap();
/// let text = String::from_utf8(out).unwrap();
/// for header in [
///     "=== build ===",
///     "=== config ===",
///     "=== ring ===",
///     "=== peers ===",
///     "=== queues ===",
///     "=== gossip ===",
///     "=== recent_events ===",
///     "=== memory ===",
///     "=== fds ===",
/// ] {
///     assert!(text.contains(header), "missing {header}");
/// }
/// ```
pub fn format_text<W: Write>(snap: &ClusterInfoSnapshot, w: &mut W) -> io::Result<()> {
    write_pair_section(w, &snap.build)?;
    write_pair_section(w, &snap.config)?;
    write_pair_section(w, &snap.ring)?;
    write_row_section(w, &snap.peers)?;
    write_pair_section(w, &snap.queues)?;
    write_pair_section(w, &snap.gossip)?;
    write_row_section(w, &snap.recent_events)?;
    write_pair_section(w, &snap.memory)?;
    write_pair_section(w, &snap.fds)?;
    Ok(())
}

fn write_pair_section<W: Write>(w: &mut W, s: &Section) -> io::Result<()> {
    writeln!(w, "=== {} ===", s.name())?;
    for (k, v) in &s.pairs {
        writeln!(w, "{k}={v}")?;
    }
    writeln!(w)
}

fn write_row_section<W: Write>(w: &mut W, s: &RowSection) -> io::Result<()> {
    writeln!(w, "=== {} ===", s.name())?;
    for r in &s.rows {
        writeln!(w, "{r}")?;
    }
    writeln!(w)
}

// ----- snapshot builders -------------------------------------------------

/// Compile-time crate version.
#[must_use]
pub fn build_version() -> &'static str {
    env!("CARGO_PKG_VERSION")
}

/// Best-effort short git SHA. Looks at the `DYNOMITE_GIT_SHA`
/// environment variable at compile time and falls back to
/// `unknown` when unset. The dynomited build script (if any) is
/// expected to populate the variable; the engine library does
/// not require it.
#[must_use]
pub fn build_git_sha() -> &'static str {
    option_env!("DYNOMITE_GIT_SHA").unwrap_or("unknown")
}

/// Returns `release` when compiled with optimisations, `debug`
/// otherwise.
#[must_use]
pub fn build_profile() -> &'static str {
    if cfg!(debug_assertions) {
        "debug"
    } else {
        "release"
    }
}

/// Assemble the `build` section.
#[must_use]
pub fn build_section() -> Section {
    Section::with_pairs(
        "build",
        &[
            ("version", build_version()),
            ("git_sha", build_git_sha()),
            ("build_profile", build_profile()),
        ],
    )
}

/// Set of YAML field names whose values must never appear in the
/// dump. Paths to key files are fine; the file contents and
/// passwords are not.
const SECRET_FIELDS: &[&str] = &["redis_requirepass"];

/// True when `key` names a sensitive configuration field.
#[must_use]
pub fn is_secret_config_field(key: &str) -> bool {
    SECRET_FIELDS.contains(&key)
}

/// Build the sanitized `config` section from a [`ConfPool`].
///
/// Only fields useful in postmortems are included; secret
/// material (Redis AUTH password) is masked as `redacted`. The
/// emission order is fixed so the rendered text is reproducible.
///
/// # Examples
///
/// ```
/// use dynomite::admin::cluster_info::config_section;
/// use dynomite::conf::ConfPool;
/// let mut p = ConfPool::default();
/// p.apply_defaults();
/// let s = config_section(&p);
/// assert_eq!(s.name(), "config");
/// // No secret value ever leaks.
/// for (_, v) in s.pairs() {
///     assert!(!v.contains("super-secret-password"));
/// }
/// ```
#[must_use]
pub fn config_section(pool: &ConfPool) -> Section {
    let mut s = Section::with_pairs("config", &[]);
    push_config_listeners(&mut s, pool);
    push_config_topology(&mut s, pool);
    push_config_security(&mut s, pool);
    push_config_runtime(&mut s, pool);
    s
}

fn push_config_listeners(s: &mut Section, pool: &ConfPool) {
    let listen = pool
        .listen
        .as_ref()
        .map_or_else(|| "unset".to_string(), endpoint_to_string);
    let dyn_listen = pool
        .dyn_listen
        .as_ref()
        .map_or_else(|| "unset".to_string(), endpoint_to_string);
    let stats_listen = pool
        .stats_listen
        .as_ref()
        .map_or_else(|| "unset".to_string(), endpoint_to_string);
    let data_store = match pool.data_store {
        Some(0) => "redis".to_string(),
        Some(1) => "memcache".to_string(),
        Some(2) => "noxu".to_string(),
        Some(n) => format!("unknown({n})"),
        None => "unset".to_string(),
    };
    s.push("data_store", data_store);
    s.push("listen", listen);
    s.push("dyn_listen", dyn_listen);
    s.push("stats_listen", stats_listen);
}

fn push_config_topology(s: &mut Section, pool: &ConfPool) {
    s.push("rack", pool.rack.clone().unwrap_or_else(|| "unset".into()));
    s.push(
        "dc",
        pool.datacenter.clone().unwrap_or_else(|| "unset".into()),
    );
    s.push("env", pool.env.clone().unwrap_or_else(|| "unset".into()));
    s.push(
        "distribution",
        pool.resolved_distribution().as_str().to_string(),
    );
    s.push(
        "hash",
        pool.hash
            .map_or_else(|| "unset".to_string(), |h| h.as_str().to_string()),
    );
}

fn push_config_security(s: &mut Section, pool: &ConfPool) {
    s.push(
        "secure_server_option",
        pool.secure_server_option
            .clone()
            .unwrap_or_else(|| "unset".into()),
    );
    s.push(
        "pem_key_file",
        pool.pem_key_file.clone().unwrap_or_else(|| "unset".into()),
    );
    s.push(
        "recon_key_file",
        pool.recon_key_file
            .clone()
            .unwrap_or_else(|| "unset".into()),
    );
    s.push(
        "recon_iv_file",
        pool.recon_iv_file.clone().unwrap_or_else(|| "unset".into()),
    );
    s.push(
        "redis_requirepass",
        if pool.redis_requirepass.is_some() {
            "redacted".to_string()
        } else {
            "unset".to_string()
        },
    );
}

fn push_config_runtime(s: &mut Section, pool: &ConfPool) {
    s.push(
        "timeout_ms",
        pool.timeout
            .map_or_else(|| "unset".to_string(), |n| n.to_string()),
    );
    s.push(
        "preconnect",
        pool.preconnect
            .map_or_else(|| "unset".to_string(), |b| b.to_string()),
    );
    s.push(
        "auto_eject_hosts",
        pool.auto_eject_hosts
            .map_or_else(|| "unset".to_string(), |b| b.to_string()),
    );
    s.push(
        "enable_hinted_handoff",
        pool.enable_hinted_handoff
            .map_or_else(|| "unset".to_string(), |b| b.to_string()),
    );
    s.push(
        "enable_gossip",
        pool.enable_gossip
            .map_or_else(|| "unset".to_string(), |b| b.to_string()),
    );
    s.push(
        "read_consistency",
        pool.read_consistency
            .clone()
            .unwrap_or_else(|| "unset".into()),
    );
    s.push(
        "write_consistency",
        pool.write_consistency
            .clone()
            .unwrap_or_else(|| "unset".into()),
    );
}

fn endpoint_to_string(l: &crate::conf::ConfListen) -> String {
    format!("{}:{}", l.name(), l.port())
}

/// Build the `ring` section from the resolved distribution and
/// the live ring snapshot.
#[must_use]
pub fn ring_section(distribution: &str, ring_entries: usize, tokens_per_node: usize) -> Section {
    let mut s = Section::with_pairs("ring", &[]);
    s.push("distribution", distribution);
    s.push("vnodes", ring_entries.to_string());
    s.push("tokens_per_node", tokens_per_node.to_string());
    s
}

/// Format a single peer row.
#[must_use]
pub fn peer_row(
    peer_id: &str,
    dc: &str,
    rack: &str,
    state: PeerState,
    phi: f64,
    last_seen_ms: i64,
    tokens: usize,
) -> String {
    format!(
        "peer_id={peer_id} dc={dc} rack={rack} status={status} phi={phi:.2} \
         last_seen_ms={last_seen_ms} tokens={tokens}",
        status = state.name(),
    )
}

/// Build the `queues` section from a stats snapshot.
#[must_use]
pub fn queues_section(snap: &crate::stats::Snapshot) -> Section {
    let mut s = Section::with_pairs("queues", &[]);
    let dispatcher = snap.pool.metrics[PoolField::DnodeClientInQueue.index()]
        + snap.pool.metrics[PoolField::DnodeClientOutQueue.index()];
    let backend = snap.server.metrics[crate::stats::ServerField::InQueue.index()]
        + snap.server.metrics[crate::stats::ServerField::OutQueue.index()];
    let hint_store = snap.pool.metrics[PoolField::PeerInQueue.index()]
        + snap.pool.metrics[PoolField::RemotePeerInQueue.index()];
    s.push("dispatcher_inflight", dispatcher.to_string());
    s.push("backend_supervisor_pending", backend.to_string());
    s.push("hint_store_size", hint_store.to_string());
    s
}

/// Build the `gossip` section from a stats snapshot. Only the
/// counters tracked today are surfaced; missing fields are
/// emitted as `unavailable`.
#[must_use]
pub fn gossip_section(snap: &crate::stats::Snapshot) -> Section {
    let mut s = Section::with_pairs("gossip", &[]);
    let churn = snap.pool.metrics[PoolField::StatsCount.index()];
    // Phi >= 8.0 is the conviction threshold defined in
    // failure_detector::DEFAULT_THRESHOLD; we surface a count
    // of peers currently above it as the alarm gauge.
    let alarms: i64 = snap
        .failure
        .peer_phi
        .iter()
        .filter(|p| p.phi >= 8.0)
        .count()
        .try_into()
        .unwrap_or(i64::MAX);
    let transitions: i64 = snap
        .failure
        .peer_state_transitions
        .iter()
        .map(|t| i64::try_from(t.count).unwrap_or(i64::MAX))
        .sum();
    s.push("churn_total", transitions.to_string());
    s.push("phi_alarms_total", alarms.to_string());
    s.push("stats_count", churn.to_string());
    s.push("heartbeats_sent", "unavailable".to_string());
    s.push("heartbeats_received", "unavailable".to_string());
    s
}

/// Build the `memory` section by scraping `/proc/self/status` on
/// Linux. Other platforms emit `unavailable`.
#[must_use]
pub fn memory_section(dyn_memory: i64) -> Section {
    let mut s = Section::with_pairs("memory", &[]);
    let rss = read_proc_status_kb("VmRSS").map_or_else(
        || "unavailable".to_string(),
        |kb| (kb.saturating_mul(1024)).to_string(),
    );
    let vms = read_proc_status_kb("VmSize").map_or_else(
        || "unavailable".to_string(),
        |kb| (kb.saturating_mul(1024)).to_string(),
    );
    s.push("rss_bytes", rss);
    s.push("vms_bytes", vms);
    s.push("hint_store_bytes", "unavailable".to_string());
    s.push("mbuf_pool_bytes", dyn_memory.to_string());
    s
}

/// Build the `fds` section by counting `/proc/self/fd` on Linux.
/// Other platforms emit `unavailable`.
#[must_use]
pub fn fds_section() -> Section {
    let mut s = Section::with_pairs("fds", &[]);
    let total = match std::fs::read_dir("/proc/self/fd") {
        Ok(rd) => rd.count().to_string(),
        Err(_) => "unavailable".to_string(),
    };
    s.push("total", total);
    s.push("listening", "unavailable".to_string());
    s.push("peer_links", "unavailable".to_string());
    s
}

/// Read `/proc/self/status` on Linux and parse the value (in
/// kilobytes) of the named line. Returns `None` on any error
/// or when the field is absent.
fn read_proc_status_kb(field: &str) -> Option<u64> {
    let s = std::fs::read_to_string("/proc/self/status").ok()?;
    for line in s.lines() {
        let Some(rest) = line.strip_prefix(field) else {
            continue;
        };
        // Accept either `Field:\tNNN kB` or `Field: NNN kB`.
        let rest = rest.trim_start_matches(':').trim();
        let mut it = rest.split_whitespace();
        let n_str = it.next()?;
        return n_str.parse::<u64>().ok();
    }
    None
}

/// Render an iso-8601 ASCII UTC timestamp from `ts_secs`.
/// Tolerates pre-1970 inputs by emitting `1970-01-01T00:00:00Z`.
#[must_use]
pub fn render_timestamp(ts_secs: u64) -> String {
    let mut out = String::with_capacity(20);
    let (y, mo, d, h, mi, se) = secs_to_components(ts_secs);
    let _ = write!(out, "{y:04}-{mo:02}-{d:02}T{h:02}:{mi:02}:{se:02}Z");
    out
}

/// Decompose `ts_secs` into `(year, month, day, hour, minute,
/// second)` UTC components without a third-party calendar
/// dependency. Years through `9999` are representable; inputs
/// past that horizon saturate.
fn secs_to_components(ts_secs: u64) -> (u64, u64, u64, u64, u64, u64) {
    let secs_per_day: u64 = 86_400;
    let days = ts_secs / secs_per_day;
    let day_rem = ts_secs % secs_per_day;
    let hour = day_rem / 3_600;
    let minute = (day_rem % 3_600) / 60;
    let second = day_rem % 60;
    let (year, month, day) = days_to_civil(days);
    (year, month, day, hour, minute, second)
}

/// Howard Hinnant's `civil_from_days` adapted to non-negative
/// inputs and `u64` arithmetic. Returns a `(year, month, day)`
/// triple; the algorithm is documented at
/// <https://howardhinnant.github.io/date_algorithms.html>.
fn days_to_civil(days_since_epoch: u64) -> (u64, u64, u64) {
    let serial = days_since_epoch + 719_468;
    let era = serial / 146_097;
    let day_of_era = serial - era * 146_097;
    let year_of_era =
        (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
    let mut year = year_of_era + era * 400;
    let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
    let month_phase = (5 * day_of_year + 2) / 153;
    let day = day_of_year - (153 * month_phase + 2) / 5 + 1;
    let month = if month_phase < 10 {
        month_phase + 3
    } else {
        month_phase.saturating_sub(9)
    };
    if month <= 2 {
        year += 1;
    }
    (year, month, day)
}

/// Render the `recent_events` section from the snapshot of a
/// [`RecentEvents`] ring.
#[must_use]
pub fn recent_events_section(events: &[RecentEvent]) -> RowSection {
    let mut sec = RowSection::new("recent_events");
    for e in events {
        let detail = if e.detail.is_empty() {
            String::new()
        } else {
            format!(" {}", e.detail)
        };
        sec.push(format!(
            "{} {}{}",
            render_timestamp(e.ts_secs),
            e.kind,
            detail
        ));
    }
    sec
}

/// Assemble a snapshot from a live [`ServerHandle`] plus a
/// shared [`RecentEvents`] ring.
///
/// This is the call dynomited makes when the operator hits
/// `GET /cluster-info.txt`: it folds together the build, config,
/// ring, peers, queues, gossip, recent-events, memory, and fds
/// sections so the response shape is independent of the binary
/// layer.
#[must_use]
pub fn gather_from_handle(
    handle: &ServerHandle,
    pool: &ConfPool,
    events: &RecentEvents,
) -> ClusterInfoSnapshot {
    let stats = handle.stats();
    let ring = handle.ring();
    let peers_view = handle.peers();
    let tokens_per_node = peers_view
        .iter()
        .find(|p| p.is_local)
        .map_or(0, |p| p.tokens.len());
    let dist = pool.resolved_distribution().as_str();
    let mut peer_rows = RowSection::new("peers");
    let _now_secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| d.as_secs());
    for p in &peers_view {
        // PeerSnapshot doesn't carry the gossip last-seen
        // timestamp today; emit 0 so the field is present but
        // honestly empty. Updating PeerSnapshot to surface
        // `last_state_ts_secs()` is tracked as a follow-up.
        let last_seen_ms: i64 = 0;
        let row = peer_row(
            &peer_label(p),
            &p.dc,
            &p.rack,
            p.state,
            phi_for(&stats, p.idx),
            last_seen_ms,
            p.tokens.len(),
        );
        peer_rows.push(row);
    }
    ClusterInfoSnapshot {
        build: build_section(),
        config: config_section(pool),
        ring: ring_section(dist, ring.entries.len(), tokens_per_node),
        peers: peer_rows,
        queues: queues_section(&stats),
        gossip: gossip_section(&stats),
        recent_events: recent_events_section(&events.snapshot()),
        memory: memory_section(stats.dyn_memory),
        fds: fds_section(),
    }
}

fn peer_label(p: &crate::embed::PeerSnapshot) -> String {
    if p.is_local {
        "local".to_string()
    } else {
        format!("{}:{}", p.host, p.port)
    }
}

fn phi_for(stats: &crate::stats::Snapshot, peer_idx: u32) -> f64 {
    stats
        .failure
        .peer_phi
        .iter()
        .find(|p| p.peer_idx == peer_idx)
        .map_or(0.0, |p| p.phi)
}

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

    #[test]
    fn synthetic_snapshot_renders_all_sections() {
        let snap = ClusterInfoSnapshot::synthetic();
        let mut buf = Vec::new();
        format_text(&snap, &mut buf).expect("format");
        let text = String::from_utf8(buf).expect("ascii");
        for header in [
            "=== build ===",
            "=== config ===",
            "=== ring ===",
            "=== peers ===",
            "=== queues ===",
            "=== gossip ===",
            "=== recent_events ===",
            "=== memory ===",
            "=== fds ===",
        ] {
            assert!(text.contains(header), "missing {header}");
        }
        assert!(text.is_ascii());
    }

    #[test]
    fn config_section_redacts_requirepass() {
        let mut pool = ConfPool::default();
        pool.apply_defaults();
        pool.redis_requirepass = Some("super-secret-password".into());
        let sec = config_section(&pool);
        let pair = sec
            .pairs()
            .iter()
            .find(|(k, _)| k == "redis_requirepass")
            .expect("present");
        assert_eq!(pair.1, "redacted");
        let mut buf = Vec::new();
        let snap = ClusterInfoSnapshot {
            config: sec,
            ..ClusterInfoSnapshot::synthetic()
        };
        format_text(&snap, &mut buf).unwrap();
        let text = String::from_utf8(buf).unwrap();
        assert!(!text.contains("super-secret-password"));
    }

    #[test]
    fn ring_buffer_drops_oldest_when_full() {
        let log = RecentEvents::new();
        for i in 0..(MAX_RECENT_EVENTS + 5) {
            log.push(RecentEvent::new(i as u64, "tick", ""));
        }
        let snap = log.snapshot();
        assert_eq!(snap.len(), MAX_RECENT_EVENTS);
        assert_eq!(snap.first().unwrap().ts_secs, 5);
        assert_eq!(snap.last().unwrap().ts_secs, (MAX_RECENT_EVENTS + 4) as u64);
    }

    #[test]
    fn timestamp_renders_known_epoch() {
        assert_eq!(render_timestamp(0), "1970-01-01T00:00:00Z");
        // 2026-05-27T00:00:00Z
        assert_eq!(render_timestamp(1_779_840_000), "2026-05-27T00:00:00Z");
    }

    #[test]
    fn is_secret_reports_known_fields() {
        assert!(is_secret_config_field("redis_requirepass"));
        assert!(!is_secret_config_field("listen"));
    }

    #[test]
    fn peer_row_is_ascii_and_well_formed() {
        let row = peer_row(
            "arnold",
            "dc-arnold",
            "rack-1",
            PeerState::Normal,
            0.42,
            120,
            3,
        );
        assert!(row.is_ascii());
        assert!(row.contains("phi=0.42"));
        assert!(row.contains("status=NORMAL"));
        assert!(row.contains("tokens=3"));
    }

    #[test]
    fn build_profile_is_one_of_two_values() {
        let p = build_profile();
        assert!(p == "debug" || p == "release");
    }
}