pub struct MasterTracker { /* private fields */ }Expand description
Tracks the current known master of the replication group.
All methods are safe to call concurrently from multiple threads. Reads use a shared lock; writes use an exclusive lock.
When a PhiAccrualDetector is attached (via MasterTracker::with_phi),
is_master_alive uses the continuous φ suspicion level instead of the
binary heartbeat timeout. This adapts automatically to network jitter.
Implementations§
Source§impl MasterTracker
impl MasterTracker
Sourcepub fn new(heartbeat_timeout: Duration) -> Self
pub fn new(heartbeat_timeout: Duration) -> Self
Create a new tracker with the given heartbeat timeout.
Uses binary heartbeat timeout for liveness detection. Call
with_phi to switch to phi accrual detection.
Sourcepub fn with_phi(self, detector: PhiAccrualDetector) -> Self
pub fn with_phi(self, detector: PhiAccrualDetector) -> Self
Attach a phi accrual failure detector.
When attached, is_master_alive returns
phi_detector.is_available() instead of the binary timeout check.
Sourcepub fn set_master(&self, name: &str, term: u64)
pub fn set_master(&self, name: &str, term: u64)
Set the current master and its term unconditionally.
Also records a heartbeat at the current time.
Sourcepub fn clear_master(&self)
pub fn clear_master(&self)
Clear the current master.
After this call, get_master returns None.
Sourcepub fn get_master(&self) -> Option<String>
pub fn get_master(&self) -> Option<String>
Returns the name of the current master, if known.
Sourcepub fn record_heartbeat(&self)
pub fn record_heartbeat(&self)
Record a heartbeat from the master at the current time.
Sourcepub fn is_master_alive(&self) -> bool
pub fn is_master_alive(&self) -> bool
Returns true if a master is set and it is still considered alive.
When a phi detector is configured, uses the continuous suspicion level
(phi.is_available()). Otherwise falls back to binary heartbeat
timeout (elapsed < heartbeat_timeout).
Sourcepub fn phi(&self) -> Option<f64>
pub fn phi(&self) -> Option<f64>
Returns the current phi suspicion value, or None if no phi detector
is configured.
Sourcepub fn time_since_heartbeat(&self) -> Option<Duration>
pub fn time_since_heartbeat(&self) -> Option<Duration>
Returns the duration since the last heartbeat, or None if no
heartbeat has been recorded.
Sourcepub fn update_master(&self, name: &str, term: u64) -> bool
pub fn update_master(&self, name: &str, term: u64) -> bool
Update the master only if term is greater than or equal to the
current term.
This ensures that stale election results (from older terms) cannot overwrite a more recent master.
Returns true if the master was updated, false if the update was
rejected due to a stale term.
Sourcepub fn heartbeat_timeout(&self) -> Duration
pub fn heartbeat_timeout(&self) -> Duration
Returns the configured heartbeat timeout.
Auto Trait Implementations§
impl !Freeze for MasterTracker
impl !RefUnwindSafe for MasterTracker
impl Send for MasterTracker
impl Sync for MasterTracker
impl Unpin for MasterTracker
impl UnsafeUnpin for MasterTracker
impl UnwindSafe for MasterTracker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more