pub struct AdaptiveConsistentHash { /* private fields */ }Expand description
An adaptive consistent hash ring supporting weighted nodes and automatic rebalancing.
§Example
use oxirs_cluster::adaptive_consistent_hash::{AdaptiveConsistentHash, HashNode};
let mut ring = AdaptiveConsistentHash::new(150);
ring.add_node(HashNode::with_weight("node-a", 2)).unwrap();
ring.add_node(HashNode::new("node-b")).unwrap();
let owner = ring.get("my-rdf-subject").unwrap();
assert!(owner == "node-a" || owner == "node-b");Implementations§
Source§impl AdaptiveConsistentHash
impl AdaptiveConsistentHash
Sourcepub fn new(base_vnodes: u32) -> Self
pub fn new(base_vnodes: u32) -> Self
Create a new empty ring.
base_vnodes controls the number of virtual nodes per unit of weight.
A value of 100–300 gives good load distribution for small–medium clusters.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Return the number of physical nodes in the ring.
Sourcepub fn virtual_node_count(&self) -> usize
pub fn virtual_node_count(&self) -> usize
Return the total number of virtual nodes (ring points).
Sourcepub fn add_node(&mut self, node: HashNode) -> Result<Vec<RebalanceEvent>>
pub fn add_node(&mut self, node: HashNode) -> Result<Vec<RebalanceEvent>>
Add a node to the ring.
If a node with the same ID already exists, it is first removed and re-inserted with the new weight (update semantics).
Returns a list of rebalance events describing which ranges move.
Sourcepub fn remove_node(&mut self, node_id: &str) -> Result<Vec<RebalanceEvent>>
pub fn remove_node(&mut self, node_id: &str) -> Result<Vec<RebalanceEvent>>
Remove a node from the ring.
Returns the rebalance events (which node takes over which ranges).
Sourcepub fn update_weight(
&mut self,
node_id: &str,
new_weight: u32,
) -> Result<Vec<RebalanceEvent>>
pub fn update_weight( &mut self, node_id: &str, new_weight: u32, ) -> Result<Vec<RebalanceEvent>>
Update the weight of an existing node, triggering rebalancing.
Sourcepub fn get(&self, key: &str) -> Option<&str>
pub fn get(&self, key: &str) -> Option<&str>
Return the ID of the node responsible for the given key.
Returns None if the ring is empty.
Sourcepub fn get_replicas<'a>(&'a self, key: &str, n: usize) -> Vec<&'a str>
pub fn get_replicas<'a>(&'a self, key: &str, n: usize) -> Vec<&'a str>
Return up to n distinct nodes in replica order for the given key.
Used for replication: the first node is the primary, subsequent nodes are replicas.
Sourcepub fn load_stats(&self) -> Vec<NodeLoadStats>
pub fn load_stats(&self) -> Vec<NodeLoadStats>
Compute per-node load statistics.
Sourcepub fn max_load_imbalance(&self) -> f64
pub fn max_load_imbalance(&self) -> f64
Return the maximum imbalance ratio across all nodes.
A ratio of 1.0 means perfectly balanced. Higher values indicate imbalance.
Auto Trait Implementations§
impl Freeze for AdaptiveConsistentHash
impl RefUnwindSafe for AdaptiveConsistentHash
impl Send for AdaptiveConsistentHash
impl Sync for AdaptiveConsistentHash
impl Unpin for AdaptiveConsistentHash
impl UnsafeUnpin for AdaptiveConsistentHash
impl UnwindSafe for AdaptiveConsistentHash
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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 moreimpl<T> MaybeDebug for T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.