pub struct ConsistentHashRing { /* private fields */ }Expand description
Consistent hash ring with virtual nodes for even load distribution.
Virtual nodes (replicas per real node) help achieve more uniform key
distribution even with small cluster sizes.
§Example
use oxibonsai_runtime::distributed::ConsistentHashRing;
let mut ring = ConsistentHashRing::new(150);
ring.add_node("node-a");
ring.add_node("node-b");
let target = ring.get_node("my-request-key");
assert!(target.is_some());Implementations§
Source§impl ConsistentHashRing
impl ConsistentHashRing
Sourcepub fn new(replicas: usize) -> Self
pub fn new(replicas: usize) -> Self
Create a new empty ring.
replicas controls how many virtual nodes are placed on the ring per
real node. Higher values give better distribution at the cost of memory.
A value of 100–200 is typical.
Sourcepub fn add_node(&mut self, node_id: &str)
pub fn add_node(&mut self, node_id: &str)
Add a node to the ring by inserting replicas virtual nodes.
Virtual node keys are "<node_id>#<i>" for i in 0..replicas.
After insertion the internal slice is re-sorted.
Sourcepub fn remove_node(&mut self, node_id: &str)
pub fn remove_node(&mut self, node_id: &str)
Remove all virtual nodes belonging to node_id from the ring.
Sourcepub fn get_node(&self, key: &str) -> Option<&str>
pub fn get_node(&self, key: &str) -> Option<&str>
Route a key to the first virtual node at or after its hash position.
Returns None if the ring is empty.
Sourcepub fn get_nodes(&self, key: &str, count: usize) -> Vec<&str>
pub fn get_nodes(&self, key: &str, count: usize) -> Vec<&str>
Route a key and return up to count distinct real nodes in ring order.
Useful for replication — returns the first count unique node IDs
encountered walking clockwise from the key’s position.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Number of distinct real nodes currently on the ring.
Sourcepub fn vnode_count(&self) -> usize
pub fn vnode_count(&self) -> usize
Total number of virtual nodes on the ring (replicas × node_count).
Auto Trait Implementations§
impl Freeze for ConsistentHashRing
impl RefUnwindSafe for ConsistentHashRing
impl Send for ConsistentHashRing
impl Sync for ConsistentHashRing
impl Unpin for ConsistentHashRing
impl UnsafeUnpin for ConsistentHashRing
impl UnwindSafe for ConsistentHashRing
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
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 more