Skip to main content

ConsistentHashRing

Struct ConsistentHashRing 

Source
pub struct ConsistentHashRing { /* private fields */ }
Expand description

A consistent hash ring for routing documents to shards.

The ring uses virtual nodes to achieve better load distribution across shards. Each physical shard is represented by multiple virtual nodes on the ring, which helps ensure that documents are distributed evenly.

§Thread Safety

The ring itself is Clone and can be wrapped in Arc<RwLock<_>> for thread-safe access with dynamic updates, or Arc<_> for read-only concurrent access.

§Example

use phago_distributed::hashing::ConsistentHashRing;
use phago_core::types::DocumentId;

let ring = ConsistentHashRing::new(3);
let doc_id = DocumentId::from_seed(42);
let shard = ring.get_shard(&doc_id);
println!("Document maps to shard: {}", shard);

Implementations§

Source§

impl ConsistentHashRing

Source

pub fn new(num_shards: u32) -> Self

Create a new hash ring with the specified number of shards.

Each shard will be represented by VIRTUAL_NODES_PER_SHARD virtual nodes on the ring for better distribution.

§Arguments
  • num_shards - The number of physical shards to distribute across
§Panics

Panics if num_shards is 0.

Source

pub fn with_virtual_nodes(num_shards: u32, virtual_nodes: u32) -> Self

Create a new hash ring with custom virtual nodes per shard.

More virtual nodes generally result in better distribution but increase memory usage and lookup time slightly.

§Arguments
  • num_shards - The number of physical shards
  • virtual_nodes - Number of virtual nodes per shard
Source

pub fn get_shard(&self, doc_id: &DocumentId) -> ShardId

Get the shard ID for a document.

This operation is O(log n) where n is the total number of virtual nodes.

§Arguments
  • doc_id - The document ID to route
§Returns

The shard ID that should store this document.

Source

pub fn get_shard_for_key<K: Hash>(&self, key: &K) -> ShardId

Get the shard ID for an arbitrary key.

This is useful for routing non-document data to shards.

§Arguments
  • key - Any hashable key
Source

pub fn add_shard(&mut self, shard_id: ShardId)

Add a new shard to the ring.

This will redistribute approximately 1 / (n+1) of the keys from existing shards to the new shard, where n is the current number of shards.

§Arguments
  • shard_id - The shard ID to add
Source

pub fn remove_shard(&mut self, shard_id: ShardId)

Remove a shard from the ring.

Documents previously assigned to this shard will be redistributed to the next shard in the ring (clockwise).

§Arguments
  • shard_id - The shard ID to remove
Source

pub fn shard_count(&self) -> u32

Get the number of shards.

Source

pub fn all_shards(&self) -> Vec<ShardId>

Get all shard IDs in the ring.

Returns a sorted, deduplicated list of all shard IDs.

Source

pub fn virtual_nodes_per_shard(&self) -> u32

Get the number of virtual nodes per shard.

Source

pub fn total_virtual_nodes(&self) -> usize

Get the total number of virtual nodes in the ring.

Source

pub fn get_replica_shards( &self, doc_id: &DocumentId, replica_count: usize, ) -> Vec<ShardId>

Get replica shards for a document.

Returns the primary shard plus replica_count additional shards that should store replicas of the document.

§Arguments
  • doc_id - The document ID
  • replica_count - Number of additional replicas (excluding primary)
§Returns

A vector of shard IDs, with the primary shard first.

Trait Implementations§

Source§

impl Clone for ConsistentHashRing

Source§

fn clone(&self) -> ConsistentHashRing

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
Source§

impl Debug for ConsistentHashRing

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for ConsistentHashRing

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext 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> 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 = 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