ringlet-hash 0.2.2

Consistent hashing with weighted nodes and cluster migration previews
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Consistent hashing for assigning hashable keys to nodes on a ring.
//!
//! Each physical node is represented by `virtual_node_base_count * replica_multiplier`
//! virtual nodes. Adding or removing a physical node generally changes the owner of
//! only part of the key space. Use [`ConsistentRing::get_nodes`] when a key needs
//! several distinct physical nodes. The multiplier is a relative placement
//! weight, not a data replica count; `get_nodes` only selects owners and can
//! return fewer than requested. Callers must check capacity, replica policy,
//! and actual distribution.
//!
//! All processes that share a ring must use the same node identifiers, weights,
//! hash algorithm and seed, and [`Hash`] representation for keys and virtual
//! node identifiers. Otherwise they may disagree about ownership. Do not use
//! [`DefaultHasher`](std::collections::hash_map::DefaultHasher) for a shared or
//! persisted ring: its algorithm and output are not guaranteed across Rust
//! releases. [`Hash`] output is also not guaranteed across compiler versions or
//! platforms. Keep those environments aligned, or use a separately specified
//! encoding and hashing scheme for long-lived, cross-platform placement.
//!
//! [`ConsistentRing`] borrows each physical node and stores its identifier and
//! weight once; virtual positions refer to those entries by index. The caller
//! must keep node values alive while the ring uses them. [`ClusterManager`]
//! provides checked membership changes over a ring.
//!
//! For background, see [Karger et al.'s original paper](https://people.csail.mit.edu/karger/Papers/web.pdf)
//! and [Dynamo's discussion of virtual nodes and replica owners](https://www.amazon.science/publications/dynamo-amazons-highly-available-key-value-store).
//!
//! # Example
//!
//! ```
//! extern crate ringlet_hash;
//! use ringlet_hash::{ConsistentRing, RingError, RingConfig};
//! use xxhash_rust::xxh3::Xxh3Builder;
//!
//! let config = 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");
//! let mut ring = ConsistentRing::try_new(config)?;
//! ring.add_node(&server_a, "a", 1)?;
//! ring.add_node(&server_b, "b", 1)?;
//!
//! let owner = ring.get_node(&"customer-42").unwrap();
//! assert!(["server-a", "server-b"].contains(&owner.as_str()));
//! assert_eq!(ring.get_nodes(&"customer-42", 2).len(), 2);
//! # Ok::<(), RingError>(())
//! ```

use std::{
    collections::HashSet,
    hash::{BuildHasher, Hash},
};

mod cluster_manager;

pub use cluster_manager::ClusterManager;

/// A virtual node placed at a hash position on the ring.
///
/// Its index refers to one physical node owned by [`ConsistentRing`].
#[derive(Debug, Clone)]
pub struct VirtualRingNode {
    hash: u64,
    physical_node_index: usize,
}

/// A borrowed physical node stored once, regardless of its number of virtual positions.
#[derive(Debug, Clone)]
pub struct PhysicalRingNode<'a, T> {
    identifier: String,
    value: &'a T,
    weight: usize,
}

impl<'a, T> PhysicalRingNode<'a, T> {
    /// Returns the unique identifier used to place this node on the ring.
    pub fn identifier(&self) -> &str {
        &self.identifier
    }

    /// Returns the node value.
    pub fn value(&self) -> &T {
        self.value
    }

    /// Returns the multiplier used to determine its virtual node count.
    pub fn weight(&self) -> usize {
        self.weight
    }
}

/// A proposed token interval to move when adding or removing a physical node.
///
/// Hash positions wrap around at [`u64::MAX`], so `start_hash` can be greater
/// than `end_hash`.
#[derive(Debug, Clone)]
pub struct TokenRangeMigration<T> {
    /// Hash at the start of the interval.
    pub start_hash: u64,
    /// Hash at the end of the interval.
    pub end_hash: u64,
    /// Owner of the interval before the membership change.
    pub from_node: T,
    /// Owner of the interval after the membership change.
    pub to_node: T,
}

/// Settings used to construct a [`ConsistentRing`].
#[derive(Clone)]
pub struct RingConfig<BH: BuildHasher> {
    /// Number of virtual nodes created for a physical node with weight 1.
    /// Must be greater than zero. `50` is an illustrative starting point;
    /// measure placement with representative keys and tune for your cluster.
    pub virtual_node_base_count: usize,
    /// Hash builder used for both keys and virtual node identifiers.
    /// Use a named algorithm with a fixed seed for shared or persisted rings;
    /// `DefaultHasher` has no stable output guarantee across Rust releases.
    pub build_hasher: BH,
}

/// A consistent hashing ring of physical nodes of type `T`.
///
/// The ring borrows each physical value and stores its identifier and weight
/// once. Virtual positions refer to it by index. Values must outlive the ring.
/// [`len`](Self::len) counts virtual nodes, not physical nodes. Ordinary
/// membership changes and lookups accept values that do not implement [`Clone`];
/// migration previews require it because they return owned node values.
#[derive(Clone)]
pub struct ConsistentRing<'a, T, BH: BuildHasher> {
    nodes: Vec<VirtualRingNode>,
    physical_nodes: Vec<PhysicalRingNode<'a, T>>,
    virtual_node_base_count: usize,
    build_hasher: BH,
}

/// Error returned for an invalid ring configuration or node weight.
#[derive(Debug, Clone)]
pub enum RingError {
    /// The configured base virtual node count is zero.
    EmptyRing(&'static str),
    /// A node was added or previewed with a zero replica multiplier.
    ZeroWeight(&'static str),
    /// A physical node with this identifier is already registered.
    NodeAlreadyExists(String),
    /// No physical node has this identifier.
    NodeNotFound(String),
}

impl<'a, T, BH: BuildHasher> ConsistentRing<'a, T, BH> {
    /// Creates an empty ring with the supplied configuration.
    ///
    /// Returns [`RingError::EmptyRing`] if `virtual_node_base_count` is zero.
    pub fn try_new(config: RingConfig<BH>) -> Result<Self, RingError> {
        if config.virtual_node_base_count == 0 {
            return Err(RingError::EmptyRing("Virtual nodes count must be > 0"));
        }
        Ok(Self {
            nodes: Vec::new(),
            physical_nodes: Vec::new(),
            virtual_node_base_count: config.virtual_node_base_count,
            build_hasher: config.build_hasher,
        })
    }

    fn hash_key<K: Hash>(&self, key: &K) -> u64 {
        self.build_hasher.hash_one(key)
    }

    fn format_virtual_node_key(&self, identifier: &str, i: usize) -> String {
        format!("{identifier}#virtual-node-{i}")
    }

    /// Adds a physical node and returns its identifier.
    ///
    /// The node gets `virtual_node_base_count * replica_multiplier` positions.
    /// The multiplier is a relative placement weight, not a replica count;
    /// the caller must decide whether it matches the node's actual capacity.
    /// The product is not checked for overflow or capped for memory use.
    /// Returns [`RingError::NodeAlreadyExists`] for a duplicate identifier and
    /// [`RingError::ZeroWeight`] for a zero multiplier.
    pub fn add_node(
        &mut self,
        node: &'a T,
        identifier: &str,
        replica_multiplier: usize,
    ) -> Result<String, RingError> {
        if replica_multiplier == 0 {
            return Err(RingError::ZeroWeight("Replica multiplier must be > 0"));
        }
        if self.contains_identifier(identifier) {
            return Err(RingError::NodeAlreadyExists(identifier.to_owned()));
        }

        let virtual_nodes_count = self.virtual_node_base_count * replica_multiplier;
        let physical_node_index = self.physical_nodes.len();

        for i in 0..virtual_nodes_count {
            let virtual_node_key = self.format_virtual_node_key(identifier, i);
            let hash = self.hash_key(&virtual_node_key);

            self.nodes.push(VirtualRingNode {
                hash,
                physical_node_index,
            });
        }

        self.nodes.sort_unstable_by_key(|ring_node| ring_node.hash);
        self.physical_nodes.push(PhysicalRingNode {
            identifier: identifier.to_owned(),
            value: node,
            weight: replica_multiplier,
        });

        Ok(identifier.to_string())
    }

    /// Removes a physical node and all its virtual positions; an unknown identifier has no effect.
    pub fn remove_node(&mut self, identifier: &str) {
        if let Some(index) = self.physical_node_index(identifier) {
            self.nodes.retain(|node| node.physical_node_index != index);
            for node in &mut self.nodes {
                if node.physical_node_index > index {
                    node.physical_node_index -= 1;
                }
            }
            self.physical_nodes.remove(index);
        }
    }

    fn physical_node_index(&self, identifier: &str) -> Option<usize> {
        self.physical_nodes
            .iter()
            .position(|node| node.identifier == identifier)
    }

    /// Returns whether a physical node with this identifier is registered.
    pub fn contains_identifier(&self, identifier: &str) -> bool {
        self.physical_node_index(identifier).is_some()
    }

    /// Returns the physical nodes and their weights in insertion order.
    pub fn physical_nodes(&self) -> &[PhysicalRingNode<'a, T>] {
        &self.physical_nodes
    }

    fn get_node_by_hash(&self, hash: u64) -> Option<&'a T> {
        if self.nodes.is_empty() {
            return None;
        }

        let start_index = self.find_start_index(hash);

        Some(self.physical_nodes[self.nodes[start_index].physical_node_index].value)
    }

    /// Returns the index of the first virtual node at or after `target_hash`.
    ///
    /// The search wraps to index 0 beyond the last token. An empty ring also
    /// returns 0; check [`is_empty`](Self::is_empty) before indexing.
    pub fn find_start_index(&self, target_hash: u64) -> usize {
        match self
            .nodes
            .binary_search_by(|ring_node| ring_node.hash.cmp(&target_hash))
        {
            Ok(exact_match) => exact_match,
            Err(some_position) => {
                if some_position == self.nodes.len() {
                    0
                } else {
                    some_position
                }
            }
        }
    }

    /// Returns up to `count` distinct physical nodes for a key, in ring order.
    ///
    /// The first result is the primary owner returned by [`get_node`](Self::get_node).
    /// The result is empty for an empty ring or a zero count, and may contain
    /// fewer than `count` nodes when the ring has fewer distinct identifiers.
    /// The caller must check the returned length and store any desired copies.
    pub fn get_nodes<K: Hash>(&self, key: &K, count: usize) -> Vec<&'a T> {
        if self.is_empty() || count == 0 {
            return Vec::new();
        }

        let target_hash = self.hash_key(key);

        let start_index = self.find_start_index(target_hash);

        let mut result = Vec::new();
        let mut seen_indices = HashSet::new();

        for i in 0..self.len() {
            let current_index = (start_index + i) % self.len();
            let ring_node = &self.nodes[current_index];

            if seen_indices.insert(ring_node.physical_node_index) {
                result.push(self.physical_nodes[ring_node.physical_node_index].value);
                if result.len() == count {
                    break;
                }
            }
        }

        result
    }

    /// Previews token ranges associated with adding a node without changing this ring.
    ///
    /// Each returned entry identifies an existing owner at a new virtual token
    /// and the proposed destination. An empty ring has no source to migrate
    /// from, so it returns an empty list. A zero multiplier returns
    /// [`RingError::ZeroWeight`].
    pub fn calculate_migration_on_add(
        &self,
        new_node: &'a T,
        new_identifier: &str,
        new_replica_multiplier: usize,
    ) -> Result<Vec<TokenRangeMigration<T>>, RingError>
    where
        T: Clone,
        BH: Clone,
    {
        let mut future_ring = self.clone();

        future_ring.add_node(new_node, new_identifier, new_replica_multiplier)?;
        let new_index = future_ring.physical_nodes.len() - 1;

        let mut migrations = Vec::new();
        for i in 0..future_ring.len() {
            let current_virtual_node = &future_ring.nodes[i];

            if current_virtual_node.physical_node_index == new_index {
                let old_node = self.get_node_by_hash(current_virtual_node.hash);

                if let Some(from_node) = old_node {
                    let previous_virtual_node = if i == 0 { future_ring.len() - 1 } else { i - 1 };

                    let start_hash = future_ring.nodes[previous_virtual_node].hash;
                    let end_hash = current_virtual_node.hash;

                    migrations.push(TokenRangeMigration {
                        start_hash,
                        end_hash,
                        from_node: from_node.clone(),
                        to_node: new_node.clone(),
                    });
                }
            }
        }

        Ok(migrations)
    }

    /// Previews token ranges that would leave `identifier` on removal.
    ///
    /// Each range is `(start_hash, end_hash]` on the hash ring, wrapping at
    /// [`u64::MAX`]. The ring is unchanged. Returns an empty list when the
    /// identifier is absent or when removal would leave no destination node.
    pub fn calculate_migration_on_remove(&self, identifier: &str) -> Vec<TokenRangeMigration<T>>
    where
        T: Clone,
        BH: Clone,
    {
        if self.nodes.is_empty() {
            return Vec::new();
        }

        let mut future_ring = self.clone();
        future_ring.remove_node(identifier);
        if future_ring.is_empty() {
            return Vec::new();
        }

        let Some(removed_index) = self.physical_node_index(identifier) else {
            return Vec::new();
        };

        self.nodes
            .iter()
            .enumerate()
            .filter(|(_, node)| node.physical_node_index == removed_index)
            .map(|(index, node)| {
                let previous_index = if index == 0 {
                    self.nodes.len() - 1
                } else {
                    index - 1
                };

                TokenRangeMigration {
                    start_hash: self.nodes[previous_index].hash,
                    end_hash: node.hash,
                    from_node: self.physical_nodes[removed_index].value.clone(),
                    to_node: future_ring
                        .get_node_by_hash(node.hash)
                        .expect("a destination exists after removal")
                        .clone(),
                }
            })
            .collect()
    }

    /// Returns the primary owner of `key`, or `None` for an empty ring.
    pub fn get_node<K: Hash>(&self, key: &K) -> Option<&'a T> {
        self.get_nodes(key, 1).into_iter().next()
    }

    /// Returns the number of virtual nodes in the ring.
    pub fn len(&self) -> usize {
        self.nodes.len()
    }

    /// Returns whether the ring has no virtual nodes.
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }
}