net-mesh 0.23.0

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
//! Replica state-machine validation — Phase C pre-scaffolding for
//! `docs/plans/REDEX_DISTRIBUTED_PLAN.md` §3.
//!
//! Pure-logic layer over the four-state model
//! ([`ReplicaRole::{Leader, Replica, Candidate, Idle}`] from the
//! wire crate, reused as the state-machine view). Pins which
//! transitions are valid + which signal class drives each. The
//! [`ReplicationCoordinator`] (Phase C) holds a `Cell<ReplicaRole>`
//! and routes every transition through
//! [`StateTransition::apply`] so the coordinator can't accidentally
//! advance a state combination the plan doesn't enumerate.
//!
//! Transition matrix (plan §3):
//!
//! | From      | To        | Trigger                                                |
//! |-----------|-----------|--------------------------------------------------------|
//! | `Idle`    | `Replica` | Capability filter selected this node                   |
//! | `Replica` | `Candidate` | 3 consecutive missed leader heartbeats              |
//! | `Candidate` | `Leader`  | Won the deterministic `elect()`                    |
//! | `Candidate` | `Replica` | Lost the deterministic `elect()`                   |
//! | `Leader`  | `Idle`    | Graceful relinquish (admin / `leader_pinned` migration) |
//! | `Replica` | `Idle`    | Disk pressure withdrawal under `UnderCapacity::Withdraw` |
//! | `*`       | `Idle`    | Channel close                                          |
//!
//! Self-transitions and any other pair are invalid. Phase F's DST
//! harness models the same matrix; this module's tests pin every
//! valid cell + reject every invalid one.

use super::replication::ReplicaRole;

/// Signal class that drives a state transition. Distinct from the
/// (from, to) pair because some pairs are reachable via more than
/// one signal (e.g. `Idle` is reachable via graceful-relinquish OR
/// channel-close — the operator-facing audit cares which).
///
/// Pinned at scaffolding time so Phase C wires signal-keyed
/// metrics (`election_thrash_total` counts only `MissedHeartbeats`-
/// driven transitions, not channel-close ones).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransitionSignal {
    /// Capability filter / `PlacementFilter` selected this node as
    /// a replica. Drives `Idle → Replica`.
    CapabilitySelected,
    /// 3 consecutive leader heartbeats missed within the §6
    /// hysteresis window. Drives `Replica → Candidate`.
    MissedHeartbeats,
    /// `elect()` computed this node as the winner. Drives
    /// `Candidate → Leader`.
    ElectionWon,
    /// `elect()` computed a peer as the winner. Drives
    /// `Candidate → Replica`.
    ElectionLost,
    /// Admin command (`leader_pinned` migration / explicit
    /// relinquish). Drives `Leader → Idle`.
    GracefulRelinquish,
    /// Local disk pressure tripped `UnderCapacity::Withdraw`.
    /// Drives `Replica → Idle`.
    DiskPressureWithdraw,
    /// Disk-pressure withdraw from `Leader`. Lets the transition
    /// metric label this case as disk-pressure rather than the
    /// `ChannelClose` fallback the FSM used pre-fix, which made
    /// operator dashboards triage it as "graceful channel close."
    LeaderDiskPressureWithdraw,
    /// Disk-pressure withdraw from `Candidate`. Same labeling
    /// rationale as `LeaderDiskPressureWithdraw`.
    CandidateDiskPressureWithdraw,
    /// Channel was closed. Drives `* → Idle` from any state.
    ChannelClose,
    /// This Leader observed another peer also claiming Leader for
    /// the same channel (inbound Heartbeat with `role=Leader`) and
    /// lost the deterministic tiebreak (lower tail_seq, or equal
    /// tail with greater node id). Drives `Leader → Replica` so a
    /// partition-heal converges to one leader rather than leaving
    /// both partitions claiming authority indefinitely.
    PeerLeaderObserved,
}

/// Result of validating + applying a state transition.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum StateTransitionError {
    /// `(from, to)` pair is not in the plan §3 matrix.
    #[error("invalid replica state transition: {from:?} → {to:?}")]
    InvalidPair {
        /// State the transition started from.
        from: ReplicaRole,
        /// Attempted target state.
        to: ReplicaRole,
    },
    /// Transition pair is valid in some signal context but not for
    /// the supplied [`TransitionSignal`]. Surfaces the kind of
    /// "you used MissedHeartbeats to go Leader → Idle" misuse.
    #[error("transition {from:?} → {to:?} not permitted under signal {signal:?}")]
    SignalMismatch {
        /// State the transition started from.
        from: ReplicaRole,
        /// Attempted target state.
        to: ReplicaRole,
        /// Signal class supplied to [`StateTransition::apply`].
        signal: TransitionSignal,
    },
}

/// One step of the state machine. Holds the validated `(from, to,
/// signal)` triple; the coordinator constructs one via
/// [`Self::apply`] and uses it to atomically write the new role +
/// log the signal.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StateTransition {
    /// State the transition started from.
    pub from: ReplicaRole,
    /// State the transition ended at.
    pub to: ReplicaRole,
    /// Signal class that drove the transition.
    pub signal: TransitionSignal,
}

impl StateTransition {
    /// Validate + construct a `StateTransition`. Returns a typed
    /// error when `(from, to, signal)` lies outside the plan §3
    /// matrix.
    ///
    /// The plan's `Any → Idle on channel close` rule is encoded as:
    /// `signal == ChannelClose` permits transition from any state
    /// to `Idle`, including no-op `Idle → Idle` (a redundant close
    /// is harmless; pinning it as valid lets shutdown code be
    /// idempotent).
    pub fn apply(
        from: ReplicaRole,
        to: ReplicaRole,
        signal: TransitionSignal,
    ) -> Result<Self, StateTransitionError> {
        if signal == TransitionSignal::ChannelClose && to == ReplicaRole::Idle {
            // Channel close from any state, including no-op self-
            // transition. Always permitted.
            return Ok(Self { from, to, signal });
        }
        let permitted = matches!(
            (from, to, signal),
            (
                ReplicaRole::Idle,
                ReplicaRole::Replica,
                TransitionSignal::CapabilitySelected,
            ) | (
                ReplicaRole::Replica,
                ReplicaRole::Candidate,
                TransitionSignal::MissedHeartbeats,
            ) | (
                ReplicaRole::Candidate,
                ReplicaRole::Leader,
                TransitionSignal::ElectionWon,
            ) | (
                ReplicaRole::Candidate,
                ReplicaRole::Replica,
                TransitionSignal::ElectionLost,
            ) | (
                ReplicaRole::Leader,
                ReplicaRole::Idle,
                TransitionSignal::GracefulRelinquish,
            ) | (
                ReplicaRole::Replica,
                ReplicaRole::Idle,
                TransitionSignal::DiskPressureWithdraw,
            ) | (
                ReplicaRole::Leader,
                ReplicaRole::Idle,
                TransitionSignal::LeaderDiskPressureWithdraw,
            ) | (
                ReplicaRole::Candidate,
                ReplicaRole::Idle,
                TransitionSignal::CandidateDiskPressureWithdraw,
            ) | (
                ReplicaRole::Leader,
                ReplicaRole::Replica,
                TransitionSignal::PeerLeaderObserved,
            )
        );
        if !permitted {
            // Distinguish "pair not in matrix at all" from
            // "pair valid but wrong signal" so operator-facing
            // diagnostics surface the misuse cleanly.
            if pair_is_valid_for_some_signal(from, to) {
                return Err(StateTransitionError::SignalMismatch { from, to, signal });
            }
            return Err(StateTransitionError::InvalidPair { from, to });
        }
        Ok(Self { from, to, signal })
    }
}

/// True iff some [`TransitionSignal`] would permit the given
/// `(from, to)` pair. Used inside [`StateTransition::apply`] to
/// surface "you supplied the wrong signal" separately from
/// "this pair is impossible."
fn pair_is_valid_for_some_signal(from: ReplicaRole, to: ReplicaRole) -> bool {
    if to == ReplicaRole::Idle {
        // Channel-close drives `* → Idle` from any state.
        return true;
    }
    matches!(
        (from, to),
        (ReplicaRole::Idle, ReplicaRole::Replica)
            | (ReplicaRole::Replica, ReplicaRole::Candidate)
            | (ReplicaRole::Candidate, ReplicaRole::Leader)
            | (ReplicaRole::Candidate, ReplicaRole::Replica)
            | (ReplicaRole::Leader, ReplicaRole::Replica)
    )
}

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

    #[test]
    fn idle_to_replica_via_capability_selected() {
        let t = StateTransition::apply(
            ReplicaRole::Idle,
            ReplicaRole::Replica,
            TransitionSignal::CapabilitySelected,
        )
        .expect("plan §3 valid pair");
        assert_eq!(t.from, ReplicaRole::Idle);
        assert_eq!(t.to, ReplicaRole::Replica);
        assert_eq!(t.signal, TransitionSignal::CapabilitySelected);
    }

    #[test]
    fn replica_to_candidate_via_missed_heartbeats() {
        StateTransition::apply(
            ReplicaRole::Replica,
            ReplicaRole::Candidate,
            TransitionSignal::MissedHeartbeats,
        )
        .expect("plan §3 valid pair");
    }

    #[test]
    fn candidate_to_leader_via_election_won() {
        StateTransition::apply(
            ReplicaRole::Candidate,
            ReplicaRole::Leader,
            TransitionSignal::ElectionWon,
        )
        .expect("plan §3 valid pair");
    }

    #[test]
    fn candidate_to_replica_via_election_lost() {
        StateTransition::apply(
            ReplicaRole::Candidate,
            ReplicaRole::Replica,
            TransitionSignal::ElectionLost,
        )
        .expect("plan §3 valid pair");
    }

    #[test]
    fn leader_to_idle_via_graceful_relinquish() {
        StateTransition::apply(
            ReplicaRole::Leader,
            ReplicaRole::Idle,
            TransitionSignal::GracefulRelinquish,
        )
        .expect("plan §3 valid pair");
    }

    #[test]
    fn replica_to_idle_via_disk_pressure() {
        StateTransition::apply(
            ReplicaRole::Replica,
            ReplicaRole::Idle,
            TransitionSignal::DiskPressureWithdraw,
        )
        .expect("plan §3 valid pair");
    }

    #[test]
    fn channel_close_terminates_from_any_state() {
        for from in [
            ReplicaRole::Leader,
            ReplicaRole::Replica,
            ReplicaRole::Candidate,
            ReplicaRole::Idle,
        ] {
            StateTransition::apply(from, ReplicaRole::Idle, TransitionSignal::ChannelClose)
                .unwrap_or_else(|_| panic!("ChannelClose must drive {from:?} → Idle"));
        }
    }

    #[test]
    fn rejects_invalid_pair_idle_to_leader() {
        let err = StateTransition::apply(
            ReplicaRole::Idle,
            ReplicaRole::Leader,
            TransitionSignal::ElectionWon,
        )
        .expect_err("Idle → Leader is not in the matrix");
        assert!(matches!(
            err,
            StateTransitionError::InvalidPair {
                from: ReplicaRole::Idle,
                to: ReplicaRole::Leader,
            }
        ));
    }

    #[test]
    fn rejects_invalid_pair_replica_to_leader() {
        // The plan forces a transient `Candidate` between Replica
        // and Leader — the state machine refuses a direct hop.
        let err = StateTransition::apply(
            ReplicaRole::Replica,
            ReplicaRole::Leader,
            TransitionSignal::ElectionWon,
        )
        .expect_err("Replica → Leader bypasses Candidate");
        assert!(matches!(err, StateTransitionError::InvalidPair { .. }));
    }

    #[test]
    fn rejects_invalid_pair_leader_to_candidate() {
        // Leader doesn't lose leadership by re-running election —
        // leader-loss detection lives on the Replica side. A leader
        // that detects its own membership shrunk transitions
        // directly via GracefulRelinquish (admin) or ChannelClose;
        // never via Candidate.
        let err = StateTransition::apply(
            ReplicaRole::Leader,
            ReplicaRole::Candidate,
            TransitionSignal::MissedHeartbeats,
        )
        .expect_err("Leader → Candidate is not in the matrix");
        assert!(matches!(err, StateTransitionError::InvalidPair { .. }));
    }

    #[test]
    fn rejects_pair_valid_but_signal_mismatch() {
        // `Idle → Replica` is a valid pair, but the only signal
        // that drives it is CapabilitySelected. Any other signal
        // surfaces as SignalMismatch (not InvalidPair) so the
        // operator-facing log says "you used the wrong signal,"
        // not "this transition is impossible."
        let err = StateTransition::apply(
            ReplicaRole::Idle,
            ReplicaRole::Replica,
            TransitionSignal::ElectionWon,
        )
        .expect_err("wrong signal for Idle → Replica");
        assert!(matches!(
            err,
            StateTransitionError::SignalMismatch {
                from: ReplicaRole::Idle,
                to: ReplicaRole::Replica,
                signal: TransitionSignal::ElectionWon,
            }
        ));
    }

    #[test]
    fn rejects_self_transitions_via_normal_signals() {
        // No `Leader → Leader` / `Replica → Replica` / etc. via
        // the normal signals — those would mask transition logic.
        // The exception is `Idle → Idle` via ChannelClose
        // (idempotent shutdown), which the dedicated test pins.
        for state in [
            ReplicaRole::Leader,
            ReplicaRole::Replica,
            ReplicaRole::Candidate,
            ReplicaRole::Idle,
        ] {
            for signal in [
                TransitionSignal::CapabilitySelected,
                TransitionSignal::MissedHeartbeats,
                TransitionSignal::ElectionWon,
                TransitionSignal::ElectionLost,
                TransitionSignal::GracefulRelinquish,
                TransitionSignal::DiskPressureWithdraw,
            ] {
                let result = StateTransition::apply(state, state, signal);
                assert!(
                    result.is_err(),
                    "self-transition {state:?} → {state:?} via {signal:?} must be rejected",
                );
            }
        }
    }

    #[test]
    fn channel_close_idle_to_idle_is_valid_idempotent_shutdown() {
        // Channel-close on an already-Idle replica is a no-op but
        // must not error — shutdown code paths can be redundant
        // under failure-injection.
        StateTransition::apply(
            ReplicaRole::Idle,
            ReplicaRole::Idle,
            TransitionSignal::ChannelClose,
        )
        .expect("ChannelClose on Idle is idempotent");
    }

    #[test]
    fn channel_close_with_wrong_target_rejected() {
        // ChannelClose drives to Idle and only to Idle.
        let err = StateTransition::apply(
            ReplicaRole::Replica,
            ReplicaRole::Leader,
            TransitionSignal::ChannelClose,
        )
        .expect_err("ChannelClose target must be Idle");
        assert!(matches!(err, StateTransitionError::InvalidPair { .. }));
    }

    #[test]
    fn matrix_exhaustive_negative_coverage() {
        // For every (from, to) pair, exhaustively cycle through
        // every signal and assert: at most ONE (signal, pair)
        // combination is valid (excluding the Channel-Close-to-Idle
        // family which permits any from-state). Pins the matrix
        // shape — drift would surface here.
        const ROLES: [ReplicaRole; 4] = [
            ReplicaRole::Leader,
            ReplicaRole::Replica,
            ReplicaRole::Candidate,
            ReplicaRole::Idle,
        ];
        const SIGNALS: [TransitionSignal; 7] = [
            TransitionSignal::CapabilitySelected,
            TransitionSignal::MissedHeartbeats,
            TransitionSignal::ElectionWon,
            TransitionSignal::ElectionLost,
            TransitionSignal::GracefulRelinquish,
            TransitionSignal::DiskPressureWithdraw,
            TransitionSignal::ChannelClose,
        ];

        let mut valid_pairs = 0;
        for from in ROLES {
            for to in ROLES {
                let mut signal_hits = 0;
                for signal in SIGNALS {
                    if StateTransition::apply(from, to, signal).is_ok() {
                        signal_hits += 1;
                    }
                }
                if signal_hits > 0 {
                    valid_pairs += 1;
                }
                if to == ReplicaRole::Idle {
                    // ChannelClose adds one hit on every from→Idle
                    // pair. The base specific-signal pair (e.g.
                    // Leader→Idle via GracefulRelinquish,
                    // Replica→Idle via DiskPressureWithdraw) adds
                    // another for some from-states. Cap is 2.
                    assert!(
                        signal_hits <= 2,
                        "{from:?} → Idle has too many valid signals: {signal_hits}",
                    );
                } else {
                    assert!(
                        signal_hits <= 1,
                        "{from:?} → {to:?} has too many valid signals: {signal_hits}",
                    );
                }
            }
        }
        // 7 valid (from, to) pairs total per plan §3:
        // Idle→Replica, Replica→Candidate, Candidate→Leader,
        // Candidate→Replica, Leader→Idle, Replica→Idle, Idle→Idle.
        // Plus Candidate→Idle is reachable via ChannelClose. So 8.
        assert_eq!(valid_pairs, 8, "expected 8 reachable (from, to) pairs");
    }
}