amaters-cluster 0.2.0

Consensus layer for AmateRS (Ukehi)
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
//! Core types for Raft consensus

use std::collections::HashSet;
use std::path::PathBuf;
use std::time::Duration;

/// Node identifier
pub type NodeId = u64;

/// Raft term number
pub type Term = u64;

/// Log entry index (1-indexed, 0 means no entry)
pub type LogIndex = u64;

/// A membership change request for dynamic cluster reconfiguration
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MembershipChange {
    /// Add a new node to the cluster
    AddNode {
        /// The node ID to add
        node_id: NodeId,
        /// The network address of the node
        address: String,
    },
    /// Remove an existing node from the cluster
    RemoveNode {
        /// The node ID to remove
        node_id: NodeId,
    },
}

/// Tracks cluster members with their addresses and a monotonically increasing version
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClusterConfig {
    /// Map of node IDs to their network addresses
    members: Vec<(NodeId, String)>,
    /// Monotonically increasing version number for this configuration
    version: u64,
}

impl ClusterConfig {
    /// Create a new cluster config with the given members and version
    pub fn new(members: Vec<(NodeId, String)>, version: u64) -> Self {
        Self { members, version }
    }

    /// Get the list of member node IDs
    pub fn member_ids(&self) -> HashSet<NodeId> {
        self.members.iter().map(|(id, _)| *id).collect()
    }

    /// Get all members as (node_id, address) pairs
    pub fn members(&self) -> &[(NodeId, String)] {
        &self.members
    }

    /// Get the version of this configuration
    pub fn version(&self) -> u64 {
        self.version
    }

    /// Check if a node is a member
    pub fn contains(&self, node_id: NodeId) -> bool {
        self.members.iter().any(|(id, _)| *id == node_id)
    }

    /// Get the majority quorum size for this config
    pub fn quorum_size(&self) -> usize {
        self.members.len() / 2 + 1
    }

    /// Get the number of members
    pub fn len(&self) -> usize {
        self.members.len()
    }

    /// Check if the config has no members
    pub fn is_empty(&self) -> bool {
        self.members.is_empty()
    }

    /// Add a member to the config, returning a new config with incremented version
    pub fn with_added_member(&self, node_id: NodeId, address: String) -> Self {
        let mut members = self.members.clone();
        if !self.contains(node_id) {
            members.push((node_id, address));
        }
        Self {
            members,
            version: self.version + 1,
        }
    }

    /// Remove a member from the config, returning a new config with incremented version
    pub fn without_member(&self, node_id: NodeId) -> Self {
        let members: Vec<_> = self
            .members
            .iter()
            .filter(|(id, _)| *id != node_id)
            .cloned()
            .collect();
        Self {
            members,
            version: self.version + 1,
        }
    }
}

/// The state of cluster configuration during membership changes.
///
/// Implements the Raft joint consensus protocol (Section 6):
/// - `Stable`: Normal operation with a single configuration
/// - `Joint`: Transitional state requiring majority from BOTH old and new configs
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfigState {
    /// Normal operation with a single configuration
    Stable(ClusterConfig),
    /// Joint consensus: decisions require majority of both old and new configs
    Joint {
        /// The old (current) configuration
        old: ClusterConfig,
        /// The new (target) configuration
        new: ClusterConfig,
    },
}

impl ConfigState {
    /// Create a new stable config state
    pub fn new_stable(members: Vec<(NodeId, String)>) -> Self {
        ConfigState::Stable(ClusterConfig::new(members, 0))
    }

    /// Get all unique member node IDs across both configs (if joint)
    pub fn all_member_ids(&self) -> HashSet<NodeId> {
        match self {
            ConfigState::Stable(config) => config.member_ids(),
            ConfigState::Joint { old, new } => {
                let mut ids = old.member_ids();
                ids.extend(new.member_ids());
                ids
            }
        }
    }

    /// Check if we are in joint consensus
    pub fn is_joint(&self) -> bool {
        matches!(self, ConfigState::Joint { .. })
    }

    /// Get the current version (max of both configs if joint)
    pub fn version(&self) -> u64 {
        match self {
            ConfigState::Stable(config) => config.version(),
            ConfigState::Joint { old, new } => old.version().max(new.version()),
        }
    }

    /// Check if a given set of responding nodes forms a quorum.
    ///
    /// During joint consensus, a quorum requires majority in BOTH the old
    /// and new configurations independently.
    pub fn has_quorum(&self, responding_nodes: &HashSet<NodeId>) -> bool {
        match self {
            ConfigState::Stable(config) => {
                let count = config.member_ids().intersection(responding_nodes).count();
                count >= config.quorum_size()
            }
            ConfigState::Joint { old, new } => {
                let old_count = old.member_ids().intersection(responding_nodes).count();
                let new_count = new.member_ids().intersection(responding_nodes).count();
                old_count >= old.quorum_size() && new_count >= new.quorum_size()
            }
        }
    }

    /// Get the stable config (only valid if not in joint state)
    pub fn stable_config(&self) -> Option<&ClusterConfig> {
        match self {
            ConfigState::Stable(config) => Some(config),
            ConfigState::Joint { .. } => None,
        }
    }

    /// Get all members as (node_id, address) pairs
    pub fn all_members(&self) -> Vec<(NodeId, String)> {
        match self {
            ConfigState::Stable(config) => config.members().to_vec(),
            ConfigState::Joint { old, new } => {
                let mut seen = HashSet::new();
                let mut result = Vec::new();
                for (id, addr) in old.members().iter().chain(new.members().iter()) {
                    if seen.insert(*id) {
                        result.push((*id, addr.clone()));
                    }
                }
                result
            }
        }
    }
}

/// Raft node state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeState {
    /// Follower state - passive, responds to RPCs
    Follower,
    /// Candidate state - requesting votes for leadership
    Candidate,
    /// Leader state - handles client requests and replicates log
    Leader,
}

impl NodeState {
    /// Get the state name as a string
    pub fn as_str(&self) -> &'static str {
        match self {
            NodeState::Follower => "Follower",
            NodeState::Candidate => "Candidate",
            NodeState::Leader => "Leader",
        }
    }
}

/// Configuration for a Raft node
#[derive(Debug, Clone)]
pub struct RaftConfig {
    /// This node's ID
    pub node_id: NodeId,
    /// List of all peer node IDs (including this node)
    pub peers: Vec<NodeId>,
    /// Election timeout range (min, max) in milliseconds
    pub election_timeout_range: (u64, u64),
    /// Heartbeat interval in milliseconds
    pub heartbeat_interval: u64,
    /// Maximum number of entries to send in a single AppendEntries RPC
    pub max_entries_per_message: usize,
    /// Whether to enable log compaction
    pub enable_compaction: bool,
    /// Snapshot threshold (number of log entries before triggering snapshot)
    pub snapshot_threshold: u64,
    /// Maximum number of snapshots to retain on disk
    pub max_snapshots: usize,
    /// Directory for storing snapshots (None = snapshots disabled on disk)
    pub snapshot_dir: Option<PathBuf>,
    /// Directory for Raft persistent state and log (None = in-memory only)
    pub persistence_dir: Option<PathBuf>,
    /// Directory for segment-based WAL replay on startup (None = WAL replay disabled)
    pub wal_dir: Option<PathBuf>,
    /// Whether to fsync after every persistent write (default: true)
    pub sync_on_write: bool,
}

impl RaftConfig {
    /// Create a new Raft configuration with sensible defaults
    pub fn new(node_id: NodeId, peers: Vec<NodeId>) -> Self {
        Self {
            node_id,
            peers,
            election_timeout_range: (150, 300),
            heartbeat_interval: 50,
            max_entries_per_message: 100,
            enable_compaction: true,
            snapshot_threshold: 10000,
            max_snapshots: 3,
            snapshot_dir: None,
            persistence_dir: None,
            wal_dir: None,
            sync_on_write: true,
        }
    }

    /// Get a random election timeout within the configured range
    pub fn random_election_timeout(&self) -> Duration {
        use std::collections::hash_map::RandomState;
        use std::hash::BuildHasher;

        let (min, max) = self.election_timeout_range;
        let range = max - min;

        // Use current time as seed for randomization
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0);

        let random_value = RandomState::new().hash_one(now);

        let timeout_ms = min + (random_value % range);
        Duration::from_millis(timeout_ms)
    }

    /// Get the heartbeat interval
    pub fn heartbeat_interval(&self) -> Duration {
        Duration::from_millis(self.heartbeat_interval)
    }

    /// Validate the configuration
    pub fn validate(&self) -> Result<(), String> {
        // Check that node_id is in peers list
        if !self.peers.contains(&self.node_id) {
            return Err(format!("Node ID {} not found in peers list", self.node_id));
        }

        // Check for odd number of nodes (for quorum)
        if self.peers.len() % 2 == 0 {
            return Err(format!(
                "Raft requires odd number of nodes, got {}",
                self.peers.len()
            ));
        }

        // Check minimum nodes
        if self.peers.len() < 3 {
            return Err(format!(
                "Raft requires at least 3 nodes for fault tolerance, got {}",
                self.peers.len()
            ));
        }

        // Check election timeout range
        let (min, max) = self.election_timeout_range;
        if min >= max {
            return Err(format!(
                "Election timeout min ({}) must be less than max ({})",
                min, max
            ));
        }

        // Check heartbeat interval vs election timeout
        if self.heartbeat_interval >= min {
            return Err(format!(
                "Heartbeat interval ({}) must be less than election timeout min ({})",
                self.heartbeat_interval, min
            ));
        }

        Ok(())
    }

    /// Calculate the quorum size (majority)
    pub fn quorum_size(&self) -> usize {
        self.peers.len() / 2 + 1
    }
}

/// Configuration for heartbeat-based failure detection
#[derive(Debug, Clone)]
pub struct HeartbeatConfig {
    /// Interval between heartbeat sends in milliseconds
    pub interval_ms: u64,
    /// Time in milliseconds after which a peer is considered potentially failed
    pub timeout_ms: u64,
    /// Number of consecutive missed heartbeats before declaring failure
    pub max_missed: u32,
}

impl HeartbeatConfig {
    /// Create a new heartbeat configuration
    pub fn new(interval_ms: u64, timeout_ms: u64, max_missed: u32) -> Self {
        Self {
            interval_ms,
            timeout_ms,
            max_missed,
        }
    }

    /// Create a default heartbeat configuration
    /// Default: 100ms interval, 500ms timeout, 3 missed max
    pub fn default_config() -> Self {
        Self {
            interval_ms: 100,
            timeout_ms: 500,
            max_missed: 3,
        }
    }

    /// Validate the configuration
    pub fn validate(&self) -> Result<(), String> {
        if self.interval_ms == 0 {
            return Err("Heartbeat interval must be > 0".to_string());
        }
        if self.timeout_ms == 0 {
            return Err("Heartbeat timeout must be > 0".to_string());
        }
        if self.timeout_ms <= self.interval_ms {
            return Err(format!(
                "Heartbeat timeout ({}) must be greater than interval ({})",
                self.timeout_ms, self.interval_ms
            ));
        }
        if self.max_missed == 0 {
            return Err("max_missed must be > 0".to_string());
        }
        Ok(())
    }
}

impl Default for HeartbeatConfig {
    fn default() -> Self {
        Self::default_config()
    }
}

/// A packed monotonically increasing fencing token that uniquely identifies a write epoch.
///
/// Encoded as a single `u64`:
/// - High 32 bits = Raft term (capped at `u32::MAX` for compactness; terms exceeding `u32::MAX`
///   are exceedingly unlikely in any realistic deployment).
/// - Low 32 bits  = per-term monotonic sequence number.
///
/// Each time a node becomes leader it resets the sequence to zero and bumps the term
/// component via [`FencingToken::new_leader_term`].  Storage backends and followers use
/// the token to reject stale writes from former leaders: a write is stale when its
/// token's term is less than the current term, or the term matches but the sequence
/// has been superseded by a higher-sequence write in the same term.
///
/// # Format
///
/// ```text
/// [term: 32 bits][seq: 32 bits]
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct FencingToken(pub u64);

impl FencingToken {
    /// Pack `term` and `seq` into a new fencing token.
    pub fn new(term: u32, seq: u32) -> Self {
        Self(((term as u64) << 32) | (seq as u64))
    }

    /// Extract the term component (high 32 bits).
    pub fn term(self) -> u32 {
        (self.0 >> 32) as u32
    }

    /// Extract the sequence component (low 32 bits).
    pub fn seq(self) -> u32 {
        self.0 as u32
    }

    /// Return the raw `u64` representation.
    pub fn raw(self) -> u64 {
        self.0
    }

    /// Return a new token with the sequence number incremented by one,
    /// keeping the term unchanged.
    ///
    /// Wraps on `u32` overflow (extremely unlikely in practice).
    pub fn bump_seq(self) -> Self {
        let term = self.term();
        let seq = self.seq().wrapping_add(1);
        Self::new(term, seq)
    }

    /// Return a fresh token for a new leader term; the sequence number resets to 0.
    pub fn new_leader_term(term: u32) -> Self {
        Self::new(term, 0)
    }
}

/// Events emitted by the failure detector
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FailureEvent {
    /// A node has been detected as failed (missed too many heartbeats)
    NodeFailed {
        /// The node that failed
        node_id: NodeId,
        /// Number of consecutive missed heartbeats
        missed_count: u32,
        /// Duration since last successful heartbeat
        last_seen_ago_ms: u64,
    },
    /// A previously failed node has recovered (heartbeat received again)
    NodeRecovered {
        /// The node that recovered
        node_id: NodeId,
    },
}

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

    #[test]
    fn test_node_state_as_str() {
        assert_eq!(NodeState::Follower.as_str(), "Follower");
        assert_eq!(NodeState::Candidate.as_str(), "Candidate");
        assert_eq!(NodeState::Leader.as_str(), "Leader");
    }

    #[test]
    fn test_raft_config_new() {
        let config = RaftConfig::new(1, vec![1, 2, 3]);
        assert_eq!(config.node_id, 1);
        assert_eq!(config.peers, vec![1, 2, 3]);
        assert_eq!(config.election_timeout_range, (150, 300));
        assert_eq!(config.heartbeat_interval, 50);
    }

    #[test]
    fn test_raft_config_validate_valid() {
        let config = RaftConfig::new(1, vec![1, 2, 3]);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_raft_config_validate_node_not_in_peers() {
        let config = RaftConfig::new(4, vec![1, 2, 3]);
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_raft_config_validate_even_number_of_nodes() {
        let config = RaftConfig::new(1, vec![1, 2, 3, 4]);
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_raft_config_validate_too_few_nodes() {
        let config = RaftConfig::new(1, vec![1]);
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_raft_config_quorum_size() {
        let config = RaftConfig::new(1, vec![1, 2, 3]);
        assert_eq!(config.quorum_size(), 2);

        let config = RaftConfig::new(1, vec![1, 2, 3, 4, 5]);
        assert_eq!(config.quorum_size(), 3);
    }

    #[test]
    fn test_random_election_timeout() {
        let config = RaftConfig::new(1, vec![1, 2, 3]);
        let timeout1 = config.random_election_timeout();
        let timeout2 = config.random_election_timeout();

        // Both should be within range
        assert!(timeout1.as_millis() >= 150);
        assert!(timeout1.as_millis() <= 300);
        assert!(timeout2.as_millis() >= 150);
        assert!(timeout2.as_millis() <= 300);
    }

    // ── ClusterConfig tests ─────────────────────────────────────────

    #[test]
    fn test_cluster_config_new() {
        let members = vec![(1, "addr1".to_string()), (2, "addr2".to_string())];
        let cfg = ClusterConfig::new(members.clone(), 0);
        assert_eq!(cfg.len(), 2);
        assert_eq!(cfg.version(), 0);
        assert!(cfg.contains(1));
        assert!(cfg.contains(2));
        assert!(!cfg.contains(3));
    }

    #[test]
    fn test_cluster_config_quorum() {
        let members = vec![(1, "a".into()), (2, "b".into()), (3, "c".into())];
        let cfg = ClusterConfig::new(members, 0);
        assert_eq!(cfg.quorum_size(), 2); // 3/2 + 1 = 2
    }

    #[test]
    fn test_cluster_config_add_remove() {
        let members = vec![(1, "a".into()), (2, "b".into()), (3, "c".into())];
        let cfg = ClusterConfig::new(members, 0);

        let cfg2 = cfg.with_added_member(4, "d".into());
        assert_eq!(cfg2.len(), 4);
        assert!(cfg2.contains(4));
        assert_eq!(cfg2.version(), 1);

        let cfg3 = cfg2.without_member(2);
        assert_eq!(cfg3.len(), 3);
        assert!(!cfg3.contains(2));
        assert_eq!(cfg3.version(), 2);
    }

    #[test]
    fn test_cluster_config_add_existing_is_noop() {
        let members = vec![(1, "a".into()), (2, "b".into())];
        let cfg = ClusterConfig::new(members, 0);
        let cfg2 = cfg.with_added_member(1, "a2".into());
        // Should still have 2 members (not duplicated)
        assert_eq!(cfg2.len(), 2);
    }

    // ── ConfigState tests ───────────────────────────────────────────

    #[test]
    fn test_config_state_stable_quorum() {
        let members = vec![(1, "a".into()), (2, "b".into()), (3, "c".into())];
        let cs = ConfigState::new_stable(members);
        assert!(!cs.is_joint());

        let mut responding = HashSet::new();
        responding.insert(1);
        assert!(!cs.has_quorum(&responding)); // 1 of 3 -- no quorum

        responding.insert(2);
        assert!(cs.has_quorum(&responding)); // 2 of 3 -- quorum
    }

    #[test]
    fn test_config_state_joint_quorum() {
        let old = ClusterConfig::new(vec![(1, "a".into()), (2, "b".into()), (3, "c".into())], 0);
        let new = ClusterConfig::new(
            vec![
                (1, "a".into()),
                (2, "b".into()),
                (3, "c".into()),
                (4, "d".into()),
            ],
            1,
        );
        let cs = ConfigState::Joint {
            old: old.clone(),
            new: new.clone(),
        };
        assert!(cs.is_joint());

        // Need majority of old (2/3) AND new (3/4)
        let mut r = HashSet::new();
        r.insert(1);
        r.insert(2);
        // old: 2/3 ok, new: 2/4 not ok
        assert!(!cs.has_quorum(&r));

        r.insert(3);
        // old: 3/3 ok, new: 3/4 ok
        assert!(cs.has_quorum(&r));
    }

    #[test]
    fn test_config_state_all_members() {
        let old = ClusterConfig::new(vec![(1, "a".into()), (2, "b".into()), (3, "c".into())], 0);
        let new = ClusterConfig::new(vec![(1, "a".into()), (2, "b".into()), (4, "d".into())], 1);
        let cs = ConfigState::Joint { old, new };
        let members = cs.all_members();
        let ids: HashSet<NodeId> = members.iter().map(|(id, _)| *id).collect();
        assert_eq!(ids.len(), 4); // 1, 2, 3, 4
        assert!(ids.contains(&3));
        assert!(ids.contains(&4));
    }

    #[test]
    fn test_config_state_version() {
        let cs = ConfigState::new_stable(vec![(1, "a".into())]);
        assert_eq!(cs.version(), 0);
    }

    // ── HeartbeatConfig tests ───────────────────────────────────────

    #[test]
    fn test_heartbeat_config_new() {
        let config = HeartbeatConfig::new(100, 500, 3);
        assert_eq!(config.interval_ms, 100);
        assert_eq!(config.timeout_ms, 500);
        assert_eq!(config.max_missed, 3);
    }

    #[test]
    fn test_heartbeat_config_default() {
        let config = HeartbeatConfig::default();
        assert_eq!(config.interval_ms, 100);
        assert_eq!(config.timeout_ms, 500);
        assert_eq!(config.max_missed, 3);
    }

    #[test]
    fn test_heartbeat_config_validate_ok() {
        let config = HeartbeatConfig::new(100, 500, 3);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_heartbeat_config_validate_zero_interval() {
        let config = HeartbeatConfig::new(0, 500, 3);
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_heartbeat_config_validate_zero_timeout() {
        let config = HeartbeatConfig::new(100, 0, 3);
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_heartbeat_config_validate_timeout_less_than_interval() {
        let config = HeartbeatConfig::new(100, 50, 3);
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_heartbeat_config_validate_timeout_equal_interval() {
        let config = HeartbeatConfig::new(100, 100, 3);
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_heartbeat_config_validate_zero_max_missed() {
        let config = HeartbeatConfig::new(100, 500, 0);
        assert!(config.validate().is_err());
    }

    // ── FailureEvent tests ──────────────────────────────────────────

    #[test]
    fn test_failure_event_node_failed_eq() {
        let a = FailureEvent::NodeFailed {
            node_id: 2,
            missed_count: 3,
            last_seen_ago_ms: 500,
        };
        let b = FailureEvent::NodeFailed {
            node_id: 2,
            missed_count: 3,
            last_seen_ago_ms: 500,
        };
        assert_eq!(a, b);
    }

    #[test]
    fn test_failure_event_node_recovered_eq() {
        let a = FailureEvent::NodeRecovered { node_id: 2 };
        let b = FailureEvent::NodeRecovered { node_id: 2 };
        assert_eq!(a, b);
    }

    #[test]
    fn test_failure_event_ne() {
        let a = FailureEvent::NodeFailed {
            node_id: 2,
            missed_count: 3,
            last_seen_ago_ms: 500,
        };
        let b = FailureEvent::NodeRecovered { node_id: 2 };
        assert_ne!(a, b);
    }
}