pub struct SwimState { /* private fields */ }Expand description
The pure SWIM + Lifeguard state machine for one node.
Deterministic and I/O-free: every method takes a logical tick or an explicit event and returns the peer-state transitions to apply. See the module docs for the protocol and the two-layer rationale.
Implementations§
Source§impl SwimState
impl SwimState
Sourcepub fn new(me: usize, n: usize, cfg: SwimConfig) -> Self
pub fn new(me: usize, n: usize, cfg: SwimConfig) -> Self
Build a state machine for node me in a group of n
members. Every other member starts Status::Alive at
incarnation 0 (the join-time optimistic assumption; a
genuinely dead member is discovered by the first failed
probe).
§Panics
Panics if me >= n; a node must be a member of its own group.
Sourcepub fn my_incarnation(&self) -> Incarnation
pub fn my_incarnation(&self) -> Incarnation
This node’s current incarnation.
Sourcepub fn nack_score(&self) -> u32
pub fn nack_score(&self) -> u32
Current Lifeguard nack score (0 means “I believe I am healthy”).
Sourcepub fn status(&self, member: usize) -> Status
pub fn status(&self, member: usize) -> Status
The internal SWIM status this node holds for member.
Returns Status::Alive for me (a node always considers
itself alive) and for any unknown index.
Sourcepub fn incarnation(&self, member: usize) -> Incarnation
pub fn incarnation(&self, member: usize) -> Incarnation
The incarnation this node holds for member.
Sourcepub fn member_state(&self, member: usize) -> PeerState
pub fn member_state(&self, member: usize) -> PeerState
Project the internal SWIM status onto the engine-wide
PeerState the dispatcher consumes.
Status::Alive->PeerState::NormalStatus::Suspect->PeerState::Down(a suspect is removed from routing immediately; refutation restores it, exactly as phi-accrual’s transient down/up does)Status::Dead->PeerState::Down
Mapping suspect to Down is deliberate: routing to a
suspected-unreachable peer wastes a request, and a refutation
promotes it straight back to Normal. The DST accuracy
invariant is about the confirmed-dead status, not the
transient suspect window.
Sourcepub fn on_probe(
&mut self,
tick: Tick,
target: usize,
result: ProbeResult,
) -> Vec<(u32, PeerState)>
pub fn on_probe( &mut self, tick: Tick, target: usize, result: ProbeResult, ) -> Vec<(u32, PeerState)>
Record the outcome of a probe period initiated by this node
against target at logical tick.
ProbeResult::Acked-> target confirmed alive; refresh its status and decay this node’s nack score.ProbeResult::IndirectAcked-> target alive, but this node’s direct path failed: raise the nack score (Lifeguard self-awareness) and keep the target alive.ProbeResult::Failed-> begin (or reinforce) suspicion of the target.
Returns any peer-state transitions the caller should apply.
Sourcepub fn on_update(&mut self, tick: Tick, update: Update) -> Vec<(u32, PeerState)>
pub fn on_update(&mut self, tick: Tick, update: Update) -> Vec<(u32, PeerState)>
Merge an incoming membership update that piggybacked on ping / ack traffic. Returns any peer-state transitions.
The merge rule is the SWIM precedence order:
- A higher incarnation always wins.
- At equal incarnation,
DeadbeatsSuspectbeatsAlive(a worse belief overrides a better one at the same incarnation, which is how a suspicion spreads). - An update about this node that suspects or kills it is
refuted: this node bumps its own incarnation above the
update and the refutation (a fresh
Aliveat the higher incarnation) is what the caller disseminates. Refutation is skipped whenSwimConfig::refutation_enabledis false (negative-control only).
Sourcepub fn tick(&mut self, tick: Tick) -> Vec<(u32, PeerState)>
pub fn tick(&mut self, tick: Tick) -> Vec<(u32, PeerState)>
Advance logical time to tick and confirm any suspicions
whose (dogpile- and NS-adjusted) deadline has passed. Returns
the peer-state transitions produced (suspect -> dead is not a
PeerState change, since both map to Down; the list is
non-empty only when a member first crosses into Down).
Sourcepub fn confirm_deadline(&self, member: usize) -> Option<Tick>
pub fn confirm_deadline(&self, member: usize) -> Option<Tick>
The tick at which a suspicion of member will confirm dead,
given the current suspector count and this node’s nack score.
Returns None if the member is not currently suspect.