pub struct GossipHandler { /* private fields */ }Expand description
Authoritative owner of PeerState transitions for the
gossip plane.
The handler holds an Arc<ServerPool> and feeds the
per-peer phi-accrual failure detectors as gossip frames
arrive. A periodic tick re-evaluates phi for every non-local
peer and toggles PeerState between Normal and Down based
on the configured threshold:
- a peer is
Normalonce at least one heartbeat has been recorded ANDphi(now) <= threshold, - a peer is
Downwhen no heartbeat has ever been recorded ORphi(now) > threshold.
The handler is the single place that mutates peer.state
once gossip is wired; the supervisor loop that owns the TCP
link no longer publishes peer-state transitions of its own.
§Examples
use std::sync::Arc;
use dynomite::cluster::gossip::GossipHandler;
use dynomite::cluster::peer::{Peer, PeerEndpoint};
use dynomite::cluster::pool::{PoolConfig, ServerPool};
use dynomite::hashkit::DynToken;
let cfg = PoolConfig::default();
let local = Peer::new(
0, PeerEndpoint::tcp("h".into(), 1), "r".into(), "d".into(),
vec![DynToken::from_u32(0)], true, true, false,
);
let pool = Arc::new(ServerPool::new(cfg, vec![local]));
let handler = GossipHandler::new(pool);
assert!((handler.threshold() - 8.0).abs() < f64::EPSILON);Implementations§
Source§impl GossipHandler
impl GossipHandler
Sourcepub fn new(pool: Arc<ServerPool>) -> Self
pub fn new(pool: Arc<ServerPool>) -> Self
Build a fresh handler over pool using the default
phi-accrual threshold (crate::cluster::failure_detector::DEFAULT_THRESHOLD).
Sourcepub fn with_failure_metrics(self, metrics: Arc<FailureMetrics>) -> Self
pub fn with_failure_metrics(self, metrics: Arc<FailureMetrics>) -> Self
Attach a crate::stats::FailureMetrics handle.
When set, Self::evaluate emits a
peer_state_transitions_total counter tick and a
peer_state_current gauge update for every transition
it applies, plus a gossip_phi_score gauge update for
every non-local peer regardless of whether its state
changed. Default behaviour is unchanged when no metrics
handle is supplied.
Sourcepub fn with_events(self, events: Arc<EventManager>) -> Self
pub fn with_events(self, events: Arc<EventManager>) -> Self
Attach an EventManager handle.
When set, every peer-state transition the handler
applies surfaces a ClusterEvent::PeerUp or
ClusterEvent::PeerDown payload on the manager’s
broadcast. Default behaviour is unchanged when no event
manager is supplied.
Sourcepub fn events(&self) -> Option<&Arc<EventManager>>
pub fn events(&self) -> Option<&Arc<EventManager>>
Borrow the installed event manager, if any.
Sourcepub fn with_threshold(self, threshold: f64) -> Self
pub fn with_threshold(self, threshold: f64) -> Self
Override the phi threshold (default 8.0).
Sourcepub fn with_interval(self, interval: Duration) -> Self
pub fn with_interval(self, interval: Duration) -> Self
Override the gossip interval used by the periodic tick when the handler is driven by the binary’s run loop. The in-process tests do not depend on this value.
Sourcepub fn pool(&self) -> &Arc<ServerPool> ⓘ
pub fn pool(&self) -> &Arc<ServerPool> ⓘ
Borrow the underlying pool.
Sourcepub fn record_heartbeat_pname(&self, pname: &str, now: Instant)
pub fn record_heartbeat_pname(&self, pname: &str, now: Instant)
Record an inbound gossip heartbeat from the peer
identified by pname (a host:port string matching the
peer’s crate::cluster::peer::PeerEndpoint::pname).
Mutates the peer’s failure detector and immediately
promotes the peer’s state to PeerState::Normal when
phi(now) is below the threshold; this gives gossip a
snappy first-contact transition without waiting for the
next periodic tick.
Unknown pnames are ignored.
Sourcepub fn record_heartbeat_idx(&self, peer_idx: u32, now: Instant)
pub fn record_heartbeat_idx(&self, peer_idx: u32, now: Instant)
Record an inbound gossip heartbeat against a known peer index. Used by tests and by callers that already resolved the originating peer.
Sourcepub fn evaluate(&self, now: Instant) -> Vec<(u32, PeerState)>
pub fn evaluate(&self, now: Instant) -> Vec<(u32, PeerState)>
Walk every non-local peer and reconcile its PeerState
with the failure detector’s current view of phi(now).
Returns the list of (peer_idx, new_state) transitions
the call applied (handy in tests).
This is the failure-detector tick the binary runs on a periodic timer. Calling it never panics and it never blocks on I/O.
Sourcepub fn mark_down_pname(&self, pname: &str)
pub fn mark_down_pname(&self, pname: &str)
Mark the peer identified by pname as PeerState::Down
without consulting the failure detector. Used by the
gossip-shutdown path so the dispatcher can short-circuit
routing to a peer that announced its own departure.
Sourcepub fn reset_detector(&self, peer_idx: u32)
pub fn reset_detector(&self, peer_idx: u32)
Reset the per-peer failure detector. Used when a peer is removed and re-added so historical jitter does not bias the new suspicion value.