Skip to main content

ClusterState

Struct ClusterState 

Source
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: NodeId

This node’s ID.

§config_epoch: u64

Current configuration epoch (increases on topology changes).

§slot_map: SlotMap

Slot-to-node mapping.

§state: ClusterHealth

Cluster state: ok, fail, or unknown.

Implementations§

Source§

impl ClusterState

Source

pub fn single_node(local_node: ClusterNode) -> Self

Creates a new cluster state for a single-node cluster.

Source

pub fn new(local_id: NodeId) -> Self

Creates a new empty cluster state (for joining an existing cluster).

Source

pub fn local_node(&self) -> Option<&ClusterNode>

Returns the local node.

Source

pub fn local_node_mut(&mut self) -> Option<&mut ClusterNode>

Returns a mutable reference to the local node.

Source

pub fn add_node(&mut self, node: ClusterNode)

Adds a node to the cluster.

Source

pub fn remove_node(&mut self, node_id: NodeId) -> Option<ClusterNode>

Removes a node from the cluster.

Source

pub fn slot_owner(&self, slot: u16) -> Option<&ClusterNode>

Returns the node that owns the given slot.

Source

pub fn owns_slot(&self, slot: u16) -> bool

Returns true if the local node owns the given slot.

Source

pub fn primaries(&self) -> impl Iterator<Item = &ClusterNode>

Returns all primary nodes.

Source

pub fn replicas(&self) -> impl Iterator<Item = &ClusterNode>

Returns all replica nodes.

Source

pub fn replicas_of( &self, primary_id: NodeId, ) -> impl Iterator<Item = &ClusterNode>

Returns replicas of a specific primary.

Source

pub fn update_health(&mut self)

Computes and updates the cluster health state.

Source

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.

Source

pub fn cluster_info(&self) -> String

Generates the response for CLUSTER INFO command.

Source

pub fn cluster_nodes(&self) -> String

Generates the response for CLUSTER NODES command.

Source

pub fn moved_redirect( &self, slot: u16, ) -> Result<(u16, SocketAddr), ClusterError>

Generates MOVED redirect information for a slot.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for ClusterState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> OptionalSend for T
where T: Send + ?Sized,

Source§

impl<T> OptionalSync for T
where T: Sync + ?Sized,