pub struct ReplicationGroup { /* private fields */ }Expand description
A simulated Raft cluster of TsdbRaftNodes.
All message delivery, election timeout tracking, and commit-index
advancement are driven by calls to ReplicationGroup::tick.
Implementations§
Source§impl ReplicationGroup
impl ReplicationGroup
Sourcepub fn new(ids: &[&str], timeout_override: Option<&[u32]>) -> Self
pub fn new(ids: &[&str], timeout_override: Option<&[u32]>) -> Self
Create a group with the given node IDs and (optionally distinct) election timeouts.
Pass timeout_override = None to assign uniform timeouts (10 ticks).
Pass a slice of per-node timeouts to make elections deterministic.
Sourcepub fn cluster_size(&self) -> usize
pub fn cluster_size(&self) -> usize
Return the number of nodes in the group.
Sourcepub fn node(&self, id: &str) -> Option<&TsdbRaftNode>
pub fn node(&self, id: &str) -> Option<&TsdbRaftNode>
Return a reference to a node by ID.
Sourcepub fn node_mut(&mut self, id: &str) -> Option<&mut TsdbRaftNode>
pub fn node_mut(&mut self, id: &str) -> Option<&mut TsdbRaftNode>
Return a mutable reference to a node by ID.
Sourcepub fn leader_id(&self) -> Option<String>
pub fn leader_id(&self) -> Option<String>
Find the current leader, if any.
Returns None if no node is in the Leader role.
Sourcepub fn leader_count(&self) -> usize
pub fn leader_count(&self) -> usize
Return the number of nodes currently acting as leader.
In a healthy cluster this should always be <= 1.
Sourcepub fn quorum_size(&self) -> usize
pub fn quorum_size(&self) -> usize
Return the quorum size (majority) for this cluster.
Sourcepub fn propose(&mut self, cmd: TsdbCommand) -> TsdbResult<(String, u64)>
pub fn propose(&mut self, cmd: TsdbCommand) -> TsdbResult<(String, u64)>
Propose a write command on the current leader.
Returns (leader_id, log_index) or an error if there is no leader.
Sourcepub fn propose_write_entry(
&mut self,
entry: &WriteEntry,
) -> TsdbResult<(String, u64)>
pub fn propose_write_entry( &mut self, entry: &WriteEntry, ) -> TsdbResult<(String, u64)>
Propose a WriteEntry via the current leader (convenience wrapper).
Returns (leader_id, log_index) or an error.
Sourcepub fn propose_and_commit(
&mut self,
entry: &WriteEntry,
max_ticks: u32,
) -> TsdbResult<(String, u64)>
pub fn propose_and_commit( &mut self, entry: &WriteEntry, max_ticks: u32, ) -> TsdbResult<(String, u64)>
Propose a WriteEntry and wait for quorum commit by ticking.
Returns (leader_id, log_index) after up to max_ticks ticks.
Returns an error if the entry is not committed within max_ticks.
Sourcepub fn tick(&mut self)
pub fn tick(&mut self)
Advance the simulation by one logical tick.
Each tick:
- Sends leader heartbeats first (so they enter the bus).
- Delivers all pending messages from the bus.
- Increments election timers on followers/candidates.
- Advances commit indices on the leader.
The order matters: heartbeats are sent first so they can be delivered in the same tick, preventing unnecessary election timeouts.
Sourcepub fn pending_messages(&self) -> usize
pub fn pending_messages(&self) -> usize
Return the number of pending messages on the bus.
Auto Trait Implementations§
impl Freeze for ReplicationGroup
impl RefUnwindSafe for ReplicationGroup
impl Send for ReplicationGroup
impl Sync for ReplicationGroup
impl Unpin for ReplicationGroup
impl UnsafeUnpin for ReplicationGroup
impl UnwindSafe for ReplicationGroup
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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