net-mesh 0.27.0-beta.1

High-performance, schema-agnostic, backend-agnostic event bus
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
//! Partition detection and healing.
//!
//! Detects when a mass failure is actually a network partition (asymmetric
//! visibility), tracks partition state, and detects healing when nodes
//! from the other side reappear.

use std::time::Instant;

use super::correlation::{CorrelationVerdict, FailureCause};
use crate::adapter::net::state::horizon::ObservedHorizon;
use crate::adapter::net::subnet::SubnetId;

/// Lifecycle phase of a detected partition.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PartitionPhase {
    /// Partition suspected but not confirmed.
    Suspected,
    /// Partition confirmed (other side is alive but unreachable).
    Confirmed,
    /// Partition healing — some nodes reappearing.
    Healing {
        /// Nodes from the other side that have reappeared.
        reappeared: Vec<u64>,
    },
    /// Partition healed, reconciliation needed.
    Healed,
}

/// Record of a detected partition.
#[derive(Debug, Clone)]
pub struct PartitionRecord {
    /// Unique partition ID (timestamp-based).
    id: u64,
    /// When the partition was detected.
    detected_at: Instant,
    /// Nodes we can still reach.
    our_side: Vec<u64>,
    /// Nodes we lost.
    other_side: Vec<u64>,
    /// Subnet where the failure was concentrated (if identified).
    partition_subnet: Option<SubnetId>,
    /// Current phase.
    phase: PartitionPhase,
    /// Snapshot of our ObservedHorizon at partition time (reconciliation baseline).
    our_horizon_at_split: ObservedHorizon,
}

impl PartitionRecord {
    /// Get the partition ID.
    #[inline]
    pub fn id(&self) -> u64 {
        self.id
    }

    /// Get our side nodes.
    pub fn our_side(&self) -> &[u64] {
        &self.our_side
    }

    /// Get the other side nodes.
    pub fn other_side(&self) -> &[u64] {
        &self.other_side
    }

    /// Get the partition subnet.
    pub fn partition_subnet(&self) -> Option<SubnetId> {
        self.partition_subnet
    }

    /// Get the current phase.
    pub fn phase(&self) -> &PartitionPhase {
        &self.phase
    }

    /// Get the horizon snapshot at split time.
    pub fn horizon_at_split(&self) -> &ObservedHorizon {
        &self.our_horizon_at_split
    }

    /// How long the partition has been active.
    pub fn duration(&self) -> std::time::Duration {
        self.detected_at.elapsed()
    }

    /// Fraction of the other side that has reappeared.
    pub fn healing_progress(&self) -> f32 {
        if self.other_side.is_empty() {
            return 1.0;
        }
        match &self.phase {
            PartitionPhase::Healing { reappeared } => {
                reappeared.len() as f32 / self.other_side.len() as f32
            }
            PartitionPhase::Healed => 1.0,
            _ => 0.0,
        }
    }
}

/// Partition detector.
///
/// Tracks active partitions and detects healing when nodes reappear.
pub struct PartitionDetector {
    /// Active partition records.
    active_partitions: Vec<PartitionRecord>,
    /// Healing threshold — fraction of other_side that must reappear
    /// for the partition to be considered healed.
    healing_threshold: f32,
    /// Counter for generating partition IDs.
    next_id: u64,
}

impl PartitionDetector {
    /// Create a new partition detector.
    pub fn new() -> Self {
        Self {
            active_partitions: Vec::new(),
            healing_threshold: 0.50,
            next_id: 1,
        }
    }

    /// Set the healing threshold (fraction of other_side that must reappear).
    pub fn with_healing_threshold(mut self, threshold: f32) -> Self {
        self.healing_threshold = threshold;
        self
    }

    /// Attempt to detect a partition from a correlation verdict.
    ///
    /// Only creates a partition record for `MassFailure` with `SubnetFailure` cause.
    /// Returns the partition ID if created.
    pub fn detect(
        &mut self,
        verdict: &CorrelationVerdict,
        healthy_nodes: &[u64],
        current_horizon: &ObservedHorizon,
    ) -> Option<u64> {
        let (failed_nodes, cause) = match verdict {
            CorrelationVerdict::MassFailure {
                failed_nodes,
                suspected_cause,
                ..
            } => (failed_nodes, suspected_cause),
            _ => return None,
        };

        let partition_subnet = match cause {
            FailureCause::SubnetFailure { subnet, .. } => Some(*subnet),
            _ => return None, // broad outage, not a partition
        };

        // Pre-fix `self.next_id += 1` panicked in debug
        // and wrapped to 0 in release at u64::MAX, reusing partition
        // ID 0 — `confirm` / `find_mut` would then operate on the
        // wrong record. Astronomical in practice (a node would have
        // to create ~1.8e19 partition records, which is absurd
        // even for very long uptimes), but cheap to guard against
        // a wrap that would silently corrupt partition tracking.
        let id = self.next_id;
        self.next_id = self.next_id.checked_add(1).unwrap_or_else(|| {
            // Saturate by leaving next_id at u64::MAX. The next
            // detection call will see `id = u64::MAX` again,
            // re-issue it, and stay at u64::MAX. Two records with
            // the same id is a recoverable failure mode (the
            // operator notices duplicate partition tracking) where
            // wraparound to 0 is silent.
            tracing::error!(
                "partition next_id reached u64::MAX; saturating to avoid \
                 wrap-to-0 collisions with active records"
            );
            u64::MAX
        });

        let record = PartitionRecord {
            id,
            detected_at: Instant::now(),
            our_side: healthy_nodes.to_vec(),
            other_side: failed_nodes.clone(),
            partition_subnet,
            phase: PartitionPhase::Suspected,
            our_horizon_at_split: current_horizon.clone(),
        };

        self.active_partitions.push(record);
        Some(id)
    }

    /// Confirm a partition (e.g., received gossip that other side is alive).
    pub fn confirm(&mut self, partition_id: u64) -> bool {
        if let Some(record) = self.find_mut(partition_id) {
            if record.phase == PartitionPhase::Suspected {
                record.phase = PartitionPhase::Confirmed;
                return true;
            }
        }
        false
    }

    /// Record that a node has recovered (reappeared after failure).
    ///
    /// If the node was in any partition's `other_side`, transitions
    /// the partition toward healing.
    ///
    /// # Overlapping partitions
    ///
    /// A single node id can appear in `other_side` of
    /// multiple active partition records (e.g., a noisy detector
    /// classified one physical outage into two records). This
    /// function intentionally walks **all** matching records and
    /// updates each independently — each record is the source of
    /// truth for its own healing state.
    ///
    /// Downstream consumers that fire side-effecting healing
    /// actions per partition (replica rebalance, alert dispatch)
    /// must be idempotent over `(partition_id, recovered_node)`
    /// pairs, otherwise overlapping records will double-count
    /// one physical recovery. The detector layer is the place to
    /// prevent overlaps; this layer is just bookkeeping.
    pub fn on_node_recovery(&mut self, node_id: u64) {
        for record in &mut self.active_partitions {
            if !record.other_side.contains(&node_id) {
                continue;
            }

            match &mut record.phase {
                PartitionPhase::Suspected | PartitionPhase::Confirmed => {
                    record.phase = PartitionPhase::Healing {
                        reappeared: vec![node_id],
                    };
                }
                PartitionPhase::Healing { reappeared } => {
                    if !reappeared.contains(&node_id) {
                        reappeared.push(node_id);
                    }
                }
                PartitionPhase::Healed => {}
            }

            // Check if healed (after any phase transition).
            // Guard against an empty `other_side`: the ratio
            // computation would be `0 / 0 = NaN`, and `NaN >=
            // threshold` is always false, so the partition could
            // never auto-heal. The current control flow makes
            // empty `other_side` unreachable inside this branch
            // (the `contains(&node_id)` filter above eliminates
            // empties before any phase enters `Healing`), but
            // future refactors that mutate `other_side` after a
            // `Healing` transition would silently expose the
            // bug. Treat an empty `other_side` as already-healed
            // — there is no remaining side to wait on.
            if let PartitionPhase::Healing { reappeared } = &record.phase {
                if record.other_side.is_empty() {
                    record.phase = PartitionPhase::Healed;
                } else {
                    let ratio = reappeared.len() as f32 / record.other_side.len() as f32;
                    if ratio >= self.healing_threshold {
                        record.phase = PartitionPhase::Healed;
                    }
                }
            }
        }
    }

    /// Take all partitions that have healed (drains them from active list).
    pub fn take_healed(&mut self) -> Vec<PartitionRecord> {
        let mut healed = Vec::new();
        self.active_partitions.retain(|r| {
            if r.phase == PartitionPhase::Healed {
                healed.push(r.clone());
                false
            } else {
                true
            }
        });
        healed
    }

    /// Number of active partitions.
    pub fn active_count(&self) -> usize {
        self.active_partitions.len()
    }

    /// Get an active partition by ID.
    pub fn get(&self, partition_id: u64) -> Option<&PartitionRecord> {
        self.active_partitions.iter().find(|r| r.id == partition_id)
    }

    fn find_mut(&mut self, partition_id: u64) -> Option<&mut PartitionRecord> {
        self.active_partitions
            .iter_mut()
            .find(|r| r.id == partition_id)
    }
}

impl Default for PartitionDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for PartitionDetector {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PartitionDetector")
            .field("active_partitions", &self.active_partitions.len())
            .field("healing_threshold", &self.healing_threshold)
            .finish()
    }
}

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

    fn make_verdict_subnet(failed: Vec<u64>, subnet: SubnetId) -> CorrelationVerdict {
        CorrelationVerdict::MassFailure {
            failed_nodes: failed,
            failure_ratio: 0.5,
            suspected_cause: FailureCause::SubnetFailure {
                subnet,
                affected_ratio: 1.0,
            },
        }
    }

    fn make_verdict_broad(failed: Vec<u64>) -> CorrelationVerdict {
        CorrelationVerdict::MassFailure {
            failed_nodes: failed,
            failure_ratio: 0.5,
            suspected_cause: FailureCause::BroadOutage,
        }
    }

    #[test]
    fn test_detect_partition() {
        let mut det = PartitionDetector::new();
        let horizon = ObservedHorizon::new();
        let verdict = make_verdict_subnet(vec![1, 2, 3], SubnetId::new(&[2]));

        let id = det.detect(&verdict, &[4, 5, 6], &horizon);
        assert!(id.is_some());
        assert_eq!(det.active_count(), 1);

        let record = det.get(id.unwrap()).unwrap();
        assert_eq!(record.other_side(), &[1, 2, 3]);
        assert_eq!(record.our_side(), &[4, 5, 6]);
        assert_eq!(record.phase(), &PartitionPhase::Suspected);
    }

    #[test]
    fn test_no_partition_for_broad_outage() {
        let mut det = PartitionDetector::new();
        let horizon = ObservedHorizon::new();
        let verdict = make_verdict_broad(vec![1, 2, 3]);

        let id = det.detect(&verdict, &[4, 5, 6], &horizon);
        assert!(id.is_none());
        assert_eq!(det.active_count(), 0);
    }

    #[test]
    fn test_no_partition_for_independent() {
        let mut det = PartitionDetector::new();
        let horizon = ObservedHorizon::new();
        let verdict = CorrelationVerdict::Independent {
            failed_nodes: vec![1],
        };

        let id = det.detect(&verdict, &[2, 3], &horizon);
        assert!(id.is_none());
    }

    #[test]
    fn test_confirm() {
        let mut det = PartitionDetector::new();
        let horizon = ObservedHorizon::new();
        let verdict = make_verdict_subnet(vec![1, 2], SubnetId::new(&[2]));

        let id = det.detect(&verdict, &[3, 4], &horizon).unwrap();
        assert!(det.confirm(id));
        assert_eq!(det.get(id).unwrap().phase(), &PartitionPhase::Confirmed);
    }

    #[test]
    fn test_healing() {
        let mut det = PartitionDetector::new().with_healing_threshold(0.50);
        let horizon = ObservedHorizon::new();
        let verdict = make_verdict_subnet(vec![1, 2, 3, 4], SubnetId::new(&[2]));

        let id = det.detect(&verdict, &[5, 6], &horizon).unwrap();

        // First recovery — not healed yet
        det.on_node_recovery(1);
        assert!(matches!(
            det.get(id).unwrap().phase(),
            PartitionPhase::Healing { .. }
        ));

        // Second recovery — 2/4 = 50% >= threshold
        det.on_node_recovery(2);
        assert_eq!(det.get(id).unwrap().phase(), &PartitionPhase::Healed);
    }

    #[test]
    fn test_take_healed() {
        let mut det = PartitionDetector::new().with_healing_threshold(0.50);
        let horizon = ObservedHorizon::new();
        let verdict = make_verdict_subnet(vec![1, 2], SubnetId::new(&[2]));

        det.detect(&verdict, &[3, 4], &horizon);

        det.on_node_recovery(1); // 1/2 = 50% >= threshold → healed

        let healed = det.take_healed();
        assert_eq!(healed.len(), 1);
        assert_eq!(det.active_count(), 0);
    }

    #[test]
    fn test_healing_progress() {
        let mut det = PartitionDetector::new().with_healing_threshold(0.75);
        let horizon = ObservedHorizon::new();
        let verdict = make_verdict_subnet(vec![1, 2, 3, 4], SubnetId::new(&[2]));

        let id = det.detect(&verdict, &[5], &horizon).unwrap();
        assert_eq!(det.get(id).unwrap().healing_progress(), 0.0);

        det.on_node_recovery(1);
        assert_eq!(det.get(id).unwrap().healing_progress(), 0.25);

        det.on_node_recovery(2);
        assert_eq!(det.get(id).unwrap().healing_progress(), 0.50);
    }

    #[test]
    fn test_duplicate_recovery_ignored() {
        let mut det = PartitionDetector::new().with_healing_threshold(0.75);
        let horizon = ObservedHorizon::new();
        let verdict = make_verdict_subnet(vec![1, 2, 3, 4], SubnetId::new(&[2]));

        let id = det.detect(&verdict, &[5], &horizon).unwrap();

        det.on_node_recovery(1);
        det.on_node_recovery(1); // duplicate
        assert_eq!(det.get(id).unwrap().healing_progress(), 0.25); // still 1/4
    }

    /// PartitionRecord exposes a handful of pure getters that the
    /// operator dashboards + healing logic read on every tick. The
    /// existing tests cover `our_side`/`other_side`/`phase`; the
    /// rest (id, partition_subnet, horizon_at_split, duration)
    /// were not pinned. A getter that returns the wrong field —
    /// say `partition_subnet()` accidentally returning `id` after
    /// a refactor — would silently mis-render every partition
    /// dashboard row without any test surfacing the swap.
    #[test]
    fn partition_record_getters_return_the_right_fields() {
        let mut det = PartitionDetector::new();
        let horizon = ObservedHorizon::new();
        let subnet = SubnetId::new(&[2]);
        let verdict = make_verdict_subnet(vec![1, 2, 3], subnet);

        let id = det.detect(&verdict, &[4, 5, 6], &horizon).unwrap();
        let record = det.get(id).unwrap();

        assert_eq!(record.id(), id);
        assert_eq!(record.partition_subnet(), Some(subnet));
        // horizon_at_split was the empty ObservedHorizon we
        // passed in — ObservedHorizon doesn't impl PartialEq, so
        // we observe via entity_count() (0 for the empty horizon)
        // as the simplest invariant.
        assert_eq!(record.horizon_at_split().entity_count(), 0);
        // duration() reflects time since detection. We only
        // need a sanity ceiling — the contract is "returns a
        // forward-going Duration relative to detect()," not a
        // specific tight bound. A 60s ceiling tolerates loaded
        // CI boxes while still catching a regression that
        // returns `Duration::MAX` or an unrelated wall-clock
        // value.
        let d = record.duration();
        assert!(
            d < std::time::Duration::from_secs(60),
            "duration sanity ceiling exceeded: {d:?}",
        );
    }

    /// Phase-transition guard: confirm() returning false when the
    /// partition is already past Suspected (e.g. confirming
    /// twice). Pre-confirm the partition once, then re-confirm —
    /// the second call must return false and leave the phase
    /// untouched. A regression that unconditionally returns true
    /// would let callers "re-confirm" healed partitions, breaking
    /// the state-machine invariants downstream code relies on.
    #[test]
    fn confirm_returns_false_on_already_confirmed_partition() {
        let mut det = PartitionDetector::new();
        let horizon = ObservedHorizon::new();
        let verdict = make_verdict_subnet(vec![1, 2], SubnetId::new(&[2]));
        let id = det.detect(&verdict, &[3, 4], &horizon).unwrap();

        assert!(det.confirm(id), "first confirm must succeed");
        assert_eq!(det.get(id).unwrap().phase(), &PartitionPhase::Confirmed);

        // Second confirm: already past Suspected → false, no
        // phase change.
        assert!(!det.confirm(id), "second confirm must reject");
        assert_eq!(det.get(id).unwrap().phase(), &PartitionPhase::Confirmed);

        // Confirming a non-existent partition: also false.
        assert!(!det.confirm(u64::MAX));
    }

    #[test]
    fn partition_detector_default_matches_new() {
        let a: PartitionDetector = Default::default();
        let b = PartitionDetector::new();
        assert_eq!(a.active_count(), b.active_count());
    }

    #[test]
    fn partition_detector_debug_includes_counts() {
        let det = PartitionDetector::new().with_healing_threshold(0.5);
        let s = format!("{:?}", det);
        assert!(s.contains("PartitionDetector"));
        assert!(s.contains("active_partitions: 0"));
        assert!(s.contains("healing_threshold"));
    }
}