pub struct RaftControlPlane { /* private fields */ }Expand description
An openraft-based control plane.
Implementations§
Source§impl RaftControlPlane
impl RaftControlPlane
Sourcepub async fn start_single_node(
node_id: NodeId,
dir: impl AsRef<Path>,
) -> Result<Self, ControlError>
pub async fn start_single_node( node_id: NodeId, dir: impl AsRef<Path>, ) -> Result<Self, ControlError>
Starts a single-node Raft control plane.
Two redb files live under dir: sm.redb (the state machine) and log.redb (the Raft
log) — both the state machine and the log are persisted, so the whole control plane is
self-contained and recoverable after a crash.
Sourcepub async fn start_cluster_node(
node_id: NodeId,
dir: impl AsRef<Path>,
http_addr: impl Into<String>,
) -> Result<Self, ControlError>
pub async fn start_cluster_node( node_id: NodeId, dir: impl AsRef<Path>, http_addr: impl Into<String>, ) -> Result<Self, ControlError>
Starts a cluster node: the HTTP network plus its own HTTP server (exposing the Raft RPC
and /cluster/join).
dir: the directory for the redb state machine + log.http_addr: the address this node binds and advertises (e.g.127.0.0.1:7001), i.e. the address that identifies it within the cluster.
Sourcepub async fn start_cluster_node_with(
node_id: NodeId,
dir: impl AsRef<Path>,
http_addr: impl Into<String>,
tuning: RaftTuning,
) -> Result<Self, ControlError>
pub async fn start_cluster_node_with( node_id: NodeId, dir: impl AsRef<Path>, http_addr: impl Into<String>, tuning: RaftTuning, ) -> Result<Self, ControlError>
Same as start_cluster_node, but with customizable Raft timing
(RaftTuning) for high-latency / wide-area clusters.
Sourcepub async fn init_cluster(
&self,
members: BTreeMap<NodeId, String>,
) -> Result<(), ControlError>
pub async fn init_cluster( &self, members: BTreeMap<NodeId, String>, ) -> Result<(), ControlError>
Initializes a new cluster (the given {id: addr} becomes the initial voting set; normally
called exactly once, on the first core node).
Sourcepub async fn add_learner(
&self,
node_id: NodeId,
addr: impl Into<String>,
) -> Result<(), ControlError>
pub async fn add_learner( &self, node_id: NodeId, addr: impl Into<String>, ) -> Result<(), ControlError>
Adds a node as a learner (plan A: a worker starts out as a learner).
Sourcepub async fn change_membership(
&self,
voters: BTreeSet<NodeId>,
) -> Result<(), ControlError>
pub async fn change_membership( &self, voters: BTreeSet<NodeId>, ) -> Result<(), ControlError>
Changes the voting membership (plan A: a small voting core of 3~5; learners/workers are unaffected).
Sourcepub async fn join_cluster(
seed_addr: &str,
node_id: NodeId,
my_addr: &str,
) -> Result<(), ControlError>
pub async fn join_cluster( seed_addr: &str, node_id: NodeId, my_addr: &str, ) -> Result<(), ControlError>
Sends a join request from this node to a seed node (adding itself to the cluster as a
learner).
“Register against any node to join”: seed_addr is the address of any known node in the
cluster (it should be the current leader).
Sourcepub fn raft(&self) -> &MocraRaft
pub fn raft(&self) -> &MocraRaft
The underlying openraft handle (membership changes / status queries).
Sourcepub fn members(&self) -> Vec<(NodeId, String)>
pub fn members(&self) -> Vec<(NodeId, String)>
All members currently configured in the cluster (voters + learners) and their addresses.
Based on the Raft membership configuration (strongly consistent) rather than live liveness probing — good enough for approximate distributed semantics such as “spread rate limits across the member count” and “partition ownership”.
Sourcepub fn member_count(&self) -> usize
pub fn member_count(&self) -> usize
Total number of cluster members (voters + learners). At least 1.
Sourcepub fn voter_count(&self) -> usize
pub fn voter_count(&self) -> usize
The number of members in the voting core (plan A’s small voting set, typically 3~5).
Sourcepub fn current_leader(&self) -> Option<NodeId>
pub fn current_leader(&self) -> Option<NodeId>
The currently known leader (returns None when unknown / during a leader election).
Sourcepub fn status(&self) -> ClusterStatus
pub fn status(&self) -> ClusterStatus
A cluster status snapshot (observability / operational monitoring: leader, term, applied position, member count).
Sourcepub fn owned_partitions(&self) -> Vec<u32>
pub fn owned_partitions(&self) -> Vec<u32>
The partitions this node is responsible for under the current membership view (rendezvous assignment, default partition count).
Sourcepub fn owns_key(&self, key: &str) -> bool
pub fn owns_key(&self, key: &str) -> bool
Whether a given account / session key is handled by this node (rendezvous assignment, default partition count).
No negotiation needed: membership comes from Raft’s strongly consistent view and the hash is deterministic, so every node reaches the same conclusion.
Sourcepub async fn acquire_partition(
&self,
partition: u32,
ttl_ms: u64,
) -> Result<Option<u64>, ControlError>
pub async fn acquire_partition( &self, partition: u32, ttl_ms: u64, ) -> Result<Option<u64>, ControlError>
Claims a partition’s ownership lease and returns a monotonic fencing token.
For situations that need a strong guarantee at the moment membership changes: while two
nodes’ views briefly disagree, only the node holding the newest token can safely handle the
partition; downstream writes from a stale owner can be rejected on their smaller token.
Steady-state ownership is decided by owns_key; this method layers a
Raft-backed strong guarantee on top of it.
Sourcepub async fn release_partition(
&self,
partition: u32,
) -> Result<(), ControlError>
pub async fn release_partition( &self, partition: u32, ) -> Result<(), ControlError>
Releases the partition ownership lease (only if it is still held by this node).
Sourcepub async fn shutdown(&self) -> Result<(), ControlError>
pub async fn shutdown(&self) -> Result<(), ControlError>
Gracefully shuts down this node’s Raft runtime: stops background tasks and releases storage (including the redb file lock).
Call it before crash recovery / restart to make sure the redb database handle is released, so reopening the same directory no longer reports “Database already open”.
Sourcepub async fn wait_leader(&self, timeout: Duration) -> Result<(), ControlError>
pub async fn wait_leader(&self, timeout: Duration) -> Result<(), ControlError>
Waits for this node to become the leader (near-instant on a single node; a multi-node leader election takes one election-timeout window).
Trait Implementations§
Source§impl Clone for RaftControlPlane
impl Clone for RaftControlPlane
Source§fn clone(&self) -> RaftControlPlane
fn clone(&self) -> RaftControlPlane
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more