pub struct ClusterManager {
pub state: Arc<RwLock<SharedState>>,
pub nodes: Arc<RwLock<HashMap<Arc<str>, NodeInfo>>>,
}
Expand description
Central manager for cluster operations and node tracking.
The ClusterManager is responsible for maintaining the state of the entire cluster, including tracking which nodes are currently active, managing node registration and removal, and providing information about the cluster’s current composition.
It uses thread-safe data structures to allow concurrent access from multiple parts of the application, particularly important in a distributed system where node changes can happen at any time.
§Fields
state
- Shared state that includes information about the current node and clusternodes
- Thread-safe map of all known nodes in the cluster, indexed by their address
Fields§
§state: Arc<RwLock<SharedState>>
Shared state containing information about the current node and overall cluster
nodes: Arc<RwLock<HashMap<Arc<str>, NodeInfo>>>
Thread-safe map of all nodes in the cluster, keyed by node address
Implementations§
Source§impl ClusterManager
impl ClusterManager
Sourcepub fn new(state: Arc<RwLock<SharedState>>) -> Self
pub fn new(state: Arc<RwLock<SharedState>>) -> Self
Creates a new instance of the ClusterManager.
Initializes a new cluster manager with the provided shared state and an empty nodes map. This is typically called during application startup to establish the cluster management subsystem.
§Arguments
state
- Shared state containing information about the current node
§Returns
A new ClusterManager instance ready to track and manage cluster nodes
Sourcepub async fn register_node(&self, node: NodeInfo)
pub async fn register_node(&self, node: NodeInfo)
Registers a new node in the cluster.
This method adds a node to the cluster’s node registry if it doesn’t already exist. After registration, it updates the shared state with the new cluster size. The method uses colorized output to make debugging in the console easier.
§Arguments
node
- Information about the node to register
§Side Effects
- Updates the internal nodes map if the node is new
- Updates the cluster size in the shared state
- Prints diagnostic information to console
Sourcepub async fn remove_node(&self, node_uid: Arc<str>)
pub async fn remove_node(&self, node_uid: Arc<str>)
Removes a node from the cluster.
This method removes a node from the cluster’s node registry if it exists. After removal, it updates the shared state with the new cluster size. The method includes debug logging to track node removal operations.
§Arguments
node_uid
- Unique identifier of the node to remove, typically its address
§Side Effects
- Updates the internal nodes map by removing the specified node
- Updates the cluster size in the shared state
- Logs diagnostic information about the removal operation
Sourcepub async fn get_nodes(&self) -> Vec<NodeInfo>
pub async fn get_nodes(&self) -> Vec<NodeInfo>
Retrieves a list of all known nodes in the cluster.
This method provides a snapshot of all the nodes currently registered in the cluster manager. It’s useful for operations that need to iterate over all nodes or display cluster status information.
§Returns
A vector containing information about all known nodes in the cluster
Sourcepub async fn get_nodes_and_self(&self) -> Vec<NodeInfo>
pub async fn get_nodes_and_self(&self) -> Vec<NodeInfo>
Retrieves a list of all known nodes plus the current node.
This method provides a complete view of the cluster including the current node. It’s particularly useful for operations that need a complete picture of the cluster, such as leader election or quorum calculations.
§Returns
A vector containing information about all nodes in the cluster, including the current node
§Side Effects
- Prints diagnostic information about the current node and known nodes to console
Sourcepub async fn is_node_alive(&self, node_uid: Arc<str>) -> bool
pub async fn is_node_alive(&self, node_uid: Arc<str>) -> bool
Checks if a specific node is currently active in the cluster.
This method determines if a node is still considered active within the cluster by checking if it exists in the nodes registry. It’s useful for operations that need to verify a node’s presence before attempting to communicate with it.
§Arguments
node_uid
- Unique identifier of the node to check, typically its address
§Returns
true
if the node is active in the cluster, false
otherwise
Source§impl ClusterManager
impl ClusterManager
Sourcepub async fn discover_peers(
&self,
config: &ServerConfig,
my_port: u16,
) -> Result<()>
pub async fn discover_peers( &self, config: &ServerConfig, my_port: u16, ) -> Result<()>
Sourcepub(crate) async fn connect_to_peer(
&self,
client: &Client,
node_address: &str,
) -> Result<()>
pub(crate) async fn connect_to_peer( &self, client: &Client, node_address: &str, ) -> Result<()>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ClusterManager
impl !RefUnwindSafe for ClusterManager
impl Send for ClusterManager
impl Sync for ClusterManager
impl Unpin for ClusterManager
impl !UnwindSafe for ClusterManager
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
§fn into_collection<A>(self) -> SmallVec<A>where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A>where
A: Array<Item = T>,
self
into a collection.fn mapped<U, F, A>(self, f: F) -> SmallVec<A>where
F: FnMut(T) -> U,
A: Array<Item = U>,
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§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute
] value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
[Quirk
] value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition
] value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);