pub struct ClusterState {
pub nodes: HashMap<NodeId, ClusterNode>,
pub local_id: NodeId,
pub config_epoch: u64,
pub slot_map: SlotMap,
pub state: ClusterHealth,
}Expand description
The complete state of the cluster as seen by a node.
Fields§
§nodes: HashMap<NodeId, ClusterNode>All known nodes in the cluster, indexed by ID.
local_id: NodeIdThis node’s ID.
config_epoch: u64Current configuration epoch (increases on topology changes).
slot_map: SlotMapSlot-to-node mapping.
state: ClusterHealthCluster state: ok, fail, or unknown.
Implementations§
Source§impl ClusterState
impl ClusterState
Sourcepub fn single_node(local_node: ClusterNode) -> Self
pub fn single_node(local_node: ClusterNode) -> Self
Creates a new cluster state for a single-node cluster.
Sourcepub fn new(local_id: NodeId) -> Self
pub fn new(local_id: NodeId) -> Self
Creates a new empty cluster state (for joining an existing cluster).
Sourcepub fn local_node(&self) -> Option<&ClusterNode>
pub fn local_node(&self) -> Option<&ClusterNode>
Returns the local node.
Sourcepub fn local_node_mut(&mut self) -> Option<&mut ClusterNode>
pub fn local_node_mut(&mut self) -> Option<&mut ClusterNode>
Returns a mutable reference to the local node.
Sourcepub fn add_node(&mut self, node: ClusterNode)
pub fn add_node(&mut self, node: ClusterNode)
Adds a node to the cluster.
Sourcepub fn remove_node(&mut self, node_id: NodeId) -> Option<ClusterNode>
pub fn remove_node(&mut self, node_id: NodeId) -> Option<ClusterNode>
Removes a node from the cluster.
Sourcepub fn slot_owner(&self, slot: u16) -> Option<&ClusterNode>
pub fn slot_owner(&self, slot: u16) -> Option<&ClusterNode>
Returns the node that owns the given slot.
Sourcepub fn primaries(&self) -> impl Iterator<Item = &ClusterNode>
pub fn primaries(&self) -> impl Iterator<Item = &ClusterNode>
Returns all primary nodes.
Sourcepub fn replicas(&self) -> impl Iterator<Item = &ClusterNode>
pub fn replicas(&self) -> impl Iterator<Item = &ClusterNode>
Returns all replica nodes.
Sourcepub fn replicas_of(
&self,
primary_id: NodeId,
) -> impl Iterator<Item = &ClusterNode>
pub fn replicas_of( &self, primary_id: NodeId, ) -> impl Iterator<Item = &ClusterNode>
Returns replicas of a specific primary.
Sourcepub fn update_health(&mut self)
pub fn update_health(&mut self)
Computes and updates the cluster health state.
Sourcepub fn promote_replica(&mut self, replica_id: NodeId) -> Result<(), String>
pub fn promote_replica(&mut self, replica_id: NodeId) -> Result<(), String>
Promotes a replica to primary, transferring slots from its current primary.
Performs the full state transition for a failover:
- Transfers all slots from the old primary to the promoted replica.
- Demotes the old primary to a replica of the new primary.
- Updates replica lists on both nodes.
- Bumps the global config epoch.
Returns an error if the target is not a replica with a configured primary.
Sourcepub fn cluster_info(&self) -> String
pub fn cluster_info(&self) -> String
Generates the response for CLUSTER INFO command.
Sourcepub fn cluster_nodes(&self) -> String
pub fn cluster_nodes(&self) -> String
Generates the response for CLUSTER NODES command.
Sourcepub fn moved_redirect(
&self,
slot: u16,
) -> Result<(u16, SocketAddr), ClusterError>
pub fn moved_redirect( &self, slot: u16, ) -> Result<(u16, SocketAddr), ClusterError>
Generates MOVED redirect information for a slot.
Sourcepub fn to_nodes_conf(&self, incarnation: u64) -> String
pub fn to_nodes_conf(&self, incarnation: u64) -> String
Serializes the cluster state to the nodes.conf format.
The output consists of a comment header, a vars line with the current
epoch and gossip incarnation, followed by one line per node in the
standard CLUSTER NODES format.
Sourcepub fn from_nodes_conf(data: &str) -> Result<(Self, u64), ConfigParseError>
pub fn from_nodes_conf(data: &str) -> Result<(Self, u64), ConfigParseError>
Parses a nodes.conf file and reconstructs the cluster state.
Returns the reconstructed state and the saved gossip incarnation number.
Instant fields on nodes are initialized to Instant::now() since
wall-clock timestamps aren’t persisted.