Struct ReplicaNode
pub struct ReplicaNode { /* private fields */ }Expand description
One replica of a shard group.
Implementations§
§impl ReplicaNode
impl ReplicaNode
pub async fn start(cfg: ReplicaConfig) -> Result<Self>
pub async fn start(cfg: ReplicaConfig) -> Result<Self>
Starts the replica: recovers its protocol log, re-applies committed batches to its engine directory, and joins the group.
pub fn observer(&self) -> ReplicaObserver
pub fn status(&self) -> NodeStatus
pub async fn wait_ready(&mut self)
pub async fn wait_ready(&mut self)
Resolves once this replica is primary with everything committed and applied, so an engine opened now sees the whole committed log.
pub async fn wait_lost(&mut self)
pub async fn wait_lost(&mut self)
Resolves once this replica is no longer a ready primary.
pub fn standby_config(&self) -> Config
pub fn standby_config(&self) -> Config
The engine configuration for this replica while it is primary. Call
it after wait_ready: its storage refuses writes
once this replica stops being the ready primary.
The engine has the group’s shard count: the one its log was written
with, or for a new group one per core up to eight. Keep it: records
of a shard the engine does not open would never be replayed.
A hot-standby engine configuration: an engine opened with it on a
backup (or at any time) opens at once with every shard standing by
(threads, runtimes, open run stores, registered workflows), and its
shards start the moment this replica becomes the ready primary: each
recovers from the committed log batches the I/O thread holds in
memory and the files it already has open, with no engine to open.
Calls wait meanwhile. Like an engine from
engine_config it stops when this replica
loses the primary role: shut it down and open another standby
engine. Log groups only (a segment group’s shard refuses to stand by).
loop {
let engine = Engine::open(node.standby_config()).await?; // at once
register_workflows(&engine);
node.wait_ready().await; // promoted: the shards are running
node.wait_lost().await;
let _ = engine.shutdown().await;
}pub fn engine_config(&self) -> Config
pub fn lease_valid(&self) -> bool
pub fn lease_valid(&self) -> bool
Whether this replica is the ready primary and holds a read lease now:
a quorum answered its ping recently enough that no other member can
lead a newer view yet, so reads of its engine (queries, results,
inspect, list) see every write the group acknowledged. The lease is
renewed with every heartbeat and lapses about
Timing::lease_ms after the last quorum of answers.
pub async fn confirm_reads(&self) -> Result<()>
pub async fn confirm_reads(&self) -> Result<()>
For a read that must not be stale: returns at once under a lease, else once a no-op commits through a quorum (one round trip). Fails if this replica is not the ready primary or no quorum answers; the read must then not be served from this replica.
pub fn membership(&self) -> (u64, Vec<(ReplicaId, String)>)
pub fn membership(&self) -> (u64, Vec<(ReplicaId, String)>)
The membership epoch this replica is in, and its members with their addresses, in order (the first leads the epoch’s first view).
pub async fn change_members(
&self,
members: Vec<(ReplicaId, String)>,
timeout: Duration,
) -> Result<u64>
pub async fn change_members( &self, members: Vec<(ReplicaId, String)>, timeout: Duration, ) -> Result<u64>
Changes the group’s membership to members (each ID with its
address), from any member: the primary proposes it as the last entry
of the current epoch; once it commits, the new members that hold the
log change to the next epoch with a view change, members left out
retire, and new members (started with ReplicaConfig::join) fetch
the log. A quorum of the new members must be members now, so one
change adds or removes members, or replaces a minority: replace a
dead member by adding its successor in its place. Returns the new
epoch once this replica has entered it (a removed replica enters it
to retire). Like a failover, the change stops the primary’s engine:
it reopens on the first member of the new list (the current primary
when it stays).
pub fn reads(&self) -> ReadGuard
pub fn reads(&self) -> ReadGuard
A handle that answers Self::lease_valid and
Self::confirm_reads from other tasks.
pub fn kill(self)
pub fn kill(self)
Stops the replica at once, as a crash would (in-flight work is lost unless a quorum already holds it).