Skip to main content

ClusterManager

Struct ClusterManager 

Source
pub struct ClusterManager<'a, T, BH: BuildHasher> { /* private fields */ }
Expand description

Manages physical node membership through a ConsistentRing.

The ring borrows each physical value and stores its weight once. Callers must keep values alive while the manager uses them. This type provides checked membership changes and delegates placement to the ring.

§Example

use xxhash_rust::xxh3::Xxh3Builder;
use ringlet_hash::{ClusterManager, RingConfig};

let mut cluster = ClusterManager::try_new(RingConfig {
    virtual_node_base_count: 50,
    build_hasher: Xxh3Builder::new().with_seed(0),
})?;

let server_a = String::from("server-a");
let server_b = String::from("server-b");
cluster.add_node(&server_a, "a", 1)?;
cluster.add_node(&server_b, "b", 2)?;

let moves = cluster.calculate_migration_on_remove("a")?;
assert!(!moves.is_empty());

cluster.remove_node("a")?;
assert_eq!(cluster.get_node(&"customer-42"), Some(&server_b));

Ok::<(), ringlet_hash::RingError>(())

Implementations§

Source§

impl<'a, T, BH: BuildHasher> ClusterManager<'a, T, BH>

Source

pub fn try_new(config: RingConfig<BH>) -> Result<Self, RingError>

Creates a manager with an empty ring.

Returns RingError::EmptyRing if the base virtual node count is zero.

Source

pub fn nodes(&self) -> &[PhysicalRingNode<'a, T>]

Returns physical nodes and weights in insertion order.

Source

pub fn ring(&self) -> &ConsistentRing<'a, T, BH>

Returns the ring for read-only lookups and token inspection.

Source

pub fn add_node( &mut self, node: &'a T, identifier: &str, replica_multiplier: usize, ) -> Result<(), RingError>

Adds a physical node with the given identifier and replica multiplier.

The multiplier is a relative placement weight, not a replica count. The caller must choose a weight that reflects the node’s capacity. Returns RingError::NodeAlreadyExists for a duplicate identifier or RingError::ZeroWeight for a zero multiplier.

Source

pub fn remove_node(&mut self, identifier: &str) -> Result<(), RingError>

Removes a physical node and all its virtual nodes.

Returns RingError::NodeNotFound for an unknown identifier.

Source

pub fn calculate_migration_on_add( &self, new_node: &'a T, identifier: &str, replica_multiplier: usize, ) -> Result<Vec<TokenRangeMigration<T>>, RingError>
where T: Clone, BH: Clone,

Previews migration ranges for adding a node without changing membership.

Returns RingError::NodeAlreadyExists for a duplicate identifier or RingError::ZeroWeight for a zero multiplier.

Source

pub fn calculate_migration_on_remove( &self, identifier: &str, ) -> Result<Vec<TokenRangeMigration<T>>, RingError>
where T: Clone, BH: Clone,

Previews migration ranges for removing a node without changing membership.

Returns RingError::NodeNotFound for an unknown identifier. Removing the last node has no migration destination and returns an empty list.

Source

pub fn get_node<K: Hash>(&self, key: &K) -> Option<&'a T>

Returns the primary owner of key, or None for an empty cluster.

Source

pub fn get_nodes<K: Hash>(&self, key: &K, count: usize) -> Vec<&'a T>

Returns up to count distinct physical owners for key. The caller must check the returned length and store any desired copies.

Source

pub fn len(&self) -> usize

Returns the number of registered physical nodes.

Source

pub fn is_empty(&self) -> bool

Returns whether no physical nodes are registered.

Trait Implementations§

Source§

impl<'a, T: Clone, BH: Clone + BuildHasher> Clone for ClusterManager<'a, T, BH>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<'a, T, BH> Freeze for ClusterManager<'a, T, BH>
where ConsistentRing<'a, T, BH>: Freeze,

§

impl<'a, T, BH> RefUnwindSafe for ClusterManager<'a, T, BH>
where ConsistentRing<'a, T, BH>: RefUnwindSafe,

§

impl<'a, T, BH> Send for ClusterManager<'a, T, BH>
where ConsistentRing<'a, T, BH>: Send,

§

impl<'a, T, BH> Sync for ClusterManager<'a, T, BH>
where ConsistentRing<'a, T, BH>: Sync,

§

impl<'a, T, BH> Unpin for ClusterManager<'a, T, BH>
where ConsistentRing<'a, T, BH>: Unpin,

§

impl<'a, T, BH> UnsafeUnpin for ClusterManager<'a, T, BH>
where ConsistentRing<'a, T, BH>: UnsafeUnpin,

§

impl<'a, T, BH> UnwindSafe for ClusterManager<'a, T, BH>
where ConsistentRing<'a, T, BH>: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.