rocketmq-common 0.9.0

Rust implementation of Apache rocketmq common
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Consistent hash router implementation
//!
//! To hash Node objects to a hash ring with a certain amount of virtual nodes.
//! Method `route_node` will return a Node instance which the object key should be
//! allocated to according to consistent hash algorithm.

use std::collections::BTreeMap;
use std::sync::Arc;

use md5::Digest;
use rocketmq_error::RocketMQError;
use rocketmq_error::RocketMQResult;

use super::hash_function::HashFunction;
use super::node::Node;
use super::virtual_node::VirtualNode;

/// Consistent hash router that maps keys to nodes on a hash ring
///
/// This implementation uses virtual nodes to improve key distribution
/// across physical nodes. Each physical node is represented by multiple
/// virtual nodes on the ring.
///
/// # Type Parameters
/// * `T` - The type of physical nodes, must implement `Node` and `Clone` traits
///
/// # Example
/// ```
/// use rocketmq_common::common::consistenthash::ConsistentHashRouter;
/// use rocketmq_common::common::consistenthash::Node;
///
/// #[derive(Clone)]
/// struct ServerNode {
///     address: String,
/// }
///
/// impl Node for ServerNode {
///     fn get_key(&self) -> &str {
///         &self.address
///     }
/// }
///
/// let nodes = vec![
///     ServerNode {
///         address: "192.168.1.1:8080".to_string(),
///     },
///     ServerNode {
///         address: "192.168.1.2:8080".to_string(),
///     },
/// ];
///
/// let router = ConsistentHashRouter::new(nodes, 10);
/// let target_node = router.route_node("my-key").unwrap();
/// println!("Key 'my-key' routes to: {}", target_node.get_key());
/// ```
#[derive(Clone)]
pub struct ConsistentHashRouter<T: Node + Clone> {
    /// The hash ring mapping hash values to virtual nodes
    ring: BTreeMap<i64, VirtualNode<T>>,
    /// Hash function used to hash keys
    hash_function: Arc<dyn HashFunction>,
}

impl<T: Node + Clone> ConsistentHashRouter<T> {
    /// Create a new consistent hash router with the default MD5 hash function
    ///
    /// # Arguments
    /// * `physical_nodes` - Collection of physical nodes to add to the ring
    /// * `virtual_node_count` - Number of virtual nodes per physical node
    ///
    /// # Returns
    /// A new ConsistentHashRouter instance
    ///
    /// # Example
    /// ```
    /// use rocketmq_common::common::consistenthash::ConsistentHashRouter;
    /// use rocketmq_common::common::consistenthash::Node;
    ///
    /// #[derive(Clone)]
    /// struct ServerNode {
    ///     name: String,
    /// }
    ///
    /// impl Node for ServerNode {
    ///     fn get_key(&self) -> &str {
    ///         &self.name
    ///     }
    /// }
    ///
    /// let nodes = vec![
    ///     ServerNode {
    ///         name: "node1".to_string(),
    ///     },
    ///     ServerNode {
    ///         name: "node2".to_string(),
    ///     },
    /// ];
    ///
    /// let router = ConsistentHashRouter::new(nodes, 150);
    /// ```
    pub fn new(physical_nodes: Vec<T>, virtual_node_count: i32) -> Self {
        Self::new_with_hash_function(physical_nodes, virtual_node_count, Arc::new(MD5Hash::new()))
    }

    /// Create a new consistent hash router and return a typed error when the configuration is
    /// invalid.
    pub fn try_new(physical_nodes: Vec<T>, virtual_node_count: i32) -> RocketMQResult<Self> {
        Self::try_new_with_hash_function(physical_nodes, virtual_node_count, Arc::new(MD5Hash::new()))
    }

    /// Create a new consistent hash router with a custom hash function
    ///
    /// # Arguments
    /// * `physical_nodes` - Collection of physical nodes to add to the ring
    /// * `virtual_node_count` - Number of virtual nodes per physical node
    /// * `hash_function` - Custom hash function to use
    ///
    /// # Returns
    /// A new ConsistentHashRouter instance
    ///
    /// # Invalid configuration
    /// This method returns an empty router when `virtual_node_count` is negative. Use
    /// [`Self::try_new_with_hash_function`] when invalid configuration must be reported to the
    /// caller.
    pub fn new_with_hash_function(
        physical_nodes: Vec<T>,
        virtual_node_count: i32,
        hash_function: Arc<dyn HashFunction>,
    ) -> Self {
        if virtual_node_count < 0 {
            return Self {
                ring: BTreeMap::new(),
                hash_function,
            };
        }

        let mut router = Self {
            ring: BTreeMap::new(),
            hash_function,
        };

        for node in physical_nodes {
            router.add_node(node, virtual_node_count);
        }

        router
    }

    /// Create a new consistent hash router with a custom hash function and return a typed error
    /// when `virtual_node_count` is negative.
    pub fn try_new_with_hash_function(
        physical_nodes: Vec<T>,
        virtual_node_count: i32,
        hash_function: Arc<dyn HashFunction>,
    ) -> RocketMQResult<Self> {
        if virtual_node_count < 0 {
            return Err(RocketMQError::illegal_argument(format!(
                "illegal virtual node counts: {virtual_node_count}"
            )));
        }

        let mut router = Self {
            ring: BTreeMap::new(),
            hash_function,
        };

        for node in physical_nodes {
            router.try_add_node(node, virtual_node_count)?;
        }

        Ok(router)
    }

    /// Add a physical node to the hash ring with virtual nodes
    ///
    /// # Arguments
    /// * `physical_node` - Physical node to add to the hash ring
    /// * `virtual_node_count` - The number of virtual nodes for this physical node. Value should be
    ///   greater than or equal to 0
    ///
    /// # Invalid configuration
    /// This method is a no-op when `virtual_node_count` is negative. Use [`Self::try_add_node`]
    /// when invalid configuration must be reported to the caller.
    ///
    /// # Example
    /// ```
    /// use rocketmq_common::common::consistenthash::ConsistentHashRouter;
    /// use rocketmq_common::common::consistenthash::Node;
    ///
    /// #[derive(Clone)]
    /// struct ServerNode {
    ///     name: String,
    /// }
    ///
    /// impl Node for ServerNode {
    ///     fn get_key(&self) -> &str {
    ///         &self.name
    ///     }
    /// }
    ///
    /// let mut router = ConsistentHashRouter::new(vec![], 0);
    /// let node = ServerNode {
    ///     name: "node1".to_string(),
    /// };
    /// router.add_node(node, 100);
    /// ```
    pub fn add_node(&mut self, physical_node: T, virtual_node_count: i32) {
        let _ = self.try_add_node(physical_node, virtual_node_count);
    }

    /// Add a physical node to the hash ring, returning a typed error when `virtual_node_count` is
    /// negative.
    pub fn try_add_node(&mut self, physical_node: T, virtual_node_count: i32) -> RocketMQResult<()> {
        if virtual_node_count < 0 {
            return Err(RocketMQError::illegal_argument(format!(
                "illegal virtual node counts: {virtual_node_count}"
            )));
        }

        let existing_replicas = self.get_existing_replicas(&physical_node);
        for i in 0..virtual_node_count {
            let virtual_node = VirtualNode::new(physical_node.clone(), i + existing_replicas);
            let hash = self.hash_function.hash(virtual_node.get_key());
            self.ring.insert(hash, virtual_node);
        }

        Ok(())
    }

    /// Remove a physical node from the hash ring
    ///
    /// This removes all virtual nodes associated with the given physical node.
    ///
    /// # Arguments
    /// * `physical_node` - The physical node to remove
    ///
    /// # Example
    /// ```
    /// use rocketmq_common::common::consistenthash::ConsistentHashRouter;
    /// use rocketmq_common::common::consistenthash::Node;
    ///
    /// #[derive(Clone)]
    /// struct ServerNode {
    ///     name: String,
    /// }
    ///
    /// impl Node for ServerNode {
    ///     fn get_key(&self) -> &str {
    ///         &self.name
    ///     }
    /// }
    ///
    /// let node = ServerNode {
    ///     name: "node1".to_string(),
    /// };
    /// let mut router = ConsistentHashRouter::new(vec![node.clone()], 10);
    /// router.remove_node(&node);
    /// assert!(router.route_node("any-key").is_none());
    /// ```
    pub fn remove_node(&mut self, physical_node: &T) {
        self.ring
            .retain(|_, virtual_node| !virtual_node.is_virtual_node_of(physical_node));
    }

    /// Route a key to a node on the hash ring
    ///
    /// This method finds the nearest node in clockwise direction on the hash ring
    /// for the given key using consistent hashing algorithm.
    ///
    /// # Arguments
    /// * `object_key` - The object key to find a nearest node for
    ///
    /// # Returns
    /// * `Some(T)` - The physical node that the key should route to
    /// * `None` - If the ring is empty
    ///
    /// # Example
    /// ```
    /// use rocketmq_common::common::consistenthash::ConsistentHashRouter;
    /// use rocketmq_common::common::consistenthash::Node;
    ///
    /// #[derive(Clone)]
    /// struct ServerNode {
    ///     name: String,
    /// }
    ///
    /// impl Node for ServerNode {
    ///     fn get_key(&self) -> &str {
    ///         &self.name
    ///     }
    /// }
    ///
    /// let nodes = vec![
    ///     ServerNode {
    ///         name: "node1".to_string(),
    ///     },
    ///     ServerNode {
    ///         name: "node2".to_string(),
    ///     },
    /// ];
    ///
    /// let router = ConsistentHashRouter::new(nodes, 10);
    /// let node = router.route_node("my-key");
    /// assert!(node.is_some());
    /// ```
    pub fn route_node_ref(&self, object_key: &str) -> Option<&T> {
        if self.ring.is_empty() {
            return None;
        }

        let hash_val = self.hash_function.hash(object_key);

        // Find the first node with hash >= hash_val (tailMap in Java)
        let virtual_node = self
            .ring
            .range(hash_val..)
            .next()
            .or_else(|| self.ring.iter().next())
            .map(|(_, vnode)| vnode)?;

        Some(virtual_node.physical_node())
    }

    pub fn route_node(&self, object_key: &str) -> Option<T> {
        self.route_node_ref(object_key).cloned()
    }

    /// Get the number of existing virtual nodes for a physical node
    ///
    /// # Arguments
    /// * `physical_node` - The physical node to count replicas for
    ///
    /// # Returns
    /// The count of virtual nodes for the given physical node
    pub fn get_existing_replicas(&self, physical_node: &T) -> i32 {
        self.ring
            .values()
            .filter(|vnode| vnode.is_virtual_node_of(physical_node))
            .count() as i32
    }

    /// Get the total number of virtual nodes in the ring
    ///
    /// # Returns
    /// The total count of virtual nodes
    pub fn size(&self) -> usize {
        self.ring.len()
    }

    /// Check if the ring is empty
    ///
    /// # Returns
    /// `true` if there are no nodes in the ring
    pub fn is_empty(&self) -> bool {
        self.ring.is_empty()
    }
}

/// Default MD5 hash function implementation
///
/// This is the default hash function used by ConsistentHashRouter.
/// It computes MD5 digest and uses the first 4 bytes as a long hash value.
#[derive(Debug, Clone)]
pub struct MD5Hash;

impl MD5Hash {
    /// Create a new MD5Hash instance
    pub fn new() -> Self {
        Self
    }
}

impl Default for MD5Hash {
    fn default() -> Self {
        Self::new()
    }
}

impl HashFunction for MD5Hash {
    /// Hash a key using MD5 algorithm
    ///
    /// Takes the first 4 bytes of the MD5 digest and combines them
    /// into a 32-bit signed integer value.
    ///
    /// # Arguments
    /// * `key` - The key to hash
    ///
    /// # Returns
    /// A 64-bit hash value derived from the MD5 digest
    fn hash(&self, key: &str) -> i64 {
        let mut hasher = md5::Md5::new();
        hasher.update(key.as_bytes());
        let result = hasher.finalize();

        let mut h: i64 = 0;
        for i in 0..4 {
            h <<= 8;
            h |= (result[i] as i64) & 0xFF;
        }
        h
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, Clone, PartialEq)]
    struct TestNode {
        key: String,
    }

    impl TestNode {
        fn new(key: &str) -> Self {
            Self { key: key.to_string() }
        }
    }

    impl Node for TestNode {
        fn get_key(&self) -> &str {
            &self.key
        }
    }

    #[test]
    fn test_new_router() {
        let nodes = vec![TestNode::new("node1"), TestNode::new("node2")];
        let router = ConsistentHashRouter::new(nodes, 10);
        assert_eq!(router.size(), 20); // 2 nodes * 10 virtual nodes each
    }

    #[test]
    fn test_add_node() {
        let mut router = ConsistentHashRouter::new(vec![], 0);
        assert!(router.is_empty());

        router.add_node(TestNode::new("node1"), 5);
        assert_eq!(router.size(), 5);

        router.add_node(TestNode::new("node2"), 5);
        assert_eq!(router.size(), 10);
    }

    #[test]
    fn test_add_node_incremental() {
        let mut router = ConsistentHashRouter::new(vec![], 0);
        let node = TestNode::new("node1");

        router.add_node(node.clone(), 5);
        assert_eq!(router.get_existing_replicas(&node), 5);

        // Adding more virtual nodes should append, not replace
        router.add_node(node.clone(), 3);
        assert_eq!(router.get_existing_replicas(&node), 8);
    }

    #[test]
    fn test_add_node_negative_count_is_noop_without_panic() {
        let mut router = ConsistentHashRouter::new(vec![], 0);
        router.add_node(TestNode::new("node1"), -1);
        assert!(router.is_empty());
    }

    #[test]
    fn test_try_add_node_negative_count_returns_error() {
        let mut router = ConsistentHashRouter::new(vec![], 0);
        let error = router
            .try_add_node(TestNode::new("node1"), -1)
            .expect_err("negative virtual node count should error");

        assert!(error.to_string().contains("illegal virtual node counts: -1"));
        assert!(router.is_empty());
    }

    #[test]
    fn test_try_new_negative_count_returns_error() {
        let Err(error) = ConsistentHashRouter::try_new(vec![TestNode::new("node1")], -1) else {
            panic!("negative virtual node count should error");
        };

        assert!(error.to_string().contains("illegal virtual node counts: -1"));
    }

    #[test]
    fn test_new_negative_count_is_empty_without_panic() {
        let router = ConsistentHashRouter::new(vec![TestNode::new("node1")], -1);

        assert!(router.is_empty());
    }

    #[test]
    fn test_remove_node() {
        let node1 = TestNode::new("node1");
        let node2 = TestNode::new("node2");
        let mut router = ConsistentHashRouter::new(vec![node1.clone(), node2.clone()], 10);
        assert_eq!(router.size(), 20);

        router.remove_node(&node1);
        assert_eq!(router.size(), 10);
        assert_eq!(router.get_existing_replicas(&node1), 0);
        assert_eq!(router.get_existing_replicas(&node2), 10);
    }

    #[test]
    fn test_route_node_empty() {
        let router: ConsistentHashRouter<TestNode> = ConsistentHashRouter::new(vec![], 0);
        assert!(router.route_node("any-key").is_none());
    }

    #[test]
    fn test_route_node_single() {
        let node = TestNode::new("node1");
        let router = ConsistentHashRouter::new(vec![node.clone()], 10);
        let routed = router.route_node("test-key").unwrap();
        assert_eq!(routed.get_key(), "node1");
    }

    #[test]
    fn test_route_node_consistency() {
        let nodes = vec![TestNode::new("node1"), TestNode::new("node2"), TestNode::new("node3")];
        let router = ConsistentHashRouter::new(nodes, 10);

        // Same key should always route to the same node
        let key = "consistent-key";
        let node1 = router.route_node(key).unwrap();
        let node2 = router.route_node(key).unwrap();
        assert_eq!(node1.get_key(), node2.get_key());
    }

    #[test]
    fn test_route_node_ref_matches_owned_route() {
        let nodes = vec![TestNode::new("node1"), TestNode::new("node2"), TestNode::new("node3")];
        let router = ConsistentHashRouter::new(nodes, 10);

        let key = "borrowed-route-key";
        let routed_ref = router.route_node_ref(key).unwrap();
        let routed_owned = router.route_node(key).unwrap();

        assert_eq!(routed_ref.get_key(), routed_owned.get_key());
    }

    #[test]
    fn test_route_node_distribution() {
        let nodes = vec![TestNode::new("node1"), TestNode::new("node2"), TestNode::new("node3")];
        let router = ConsistentHashRouter::new(nodes, 100);

        // Test that different keys can route to different nodes
        let mut routed_nodes = std::collections::HashSet::new();
        for i in 0..100 {
            let key = format!("key-{}", i);
            if let Some(node) = router.route_node(&key) {
                routed_nodes.insert(node.get_key().to_string());
            }
        }

        // With 100 keys and 3 nodes with 100 virtual nodes each,
        // we should see distribution across nodes
        assert!(routed_nodes.len() > 1);
    }

    #[test]
    fn test_get_existing_replicas() {
        let node1 = TestNode::new("node1");
        let node2 = TestNode::new("node2");
        let router = ConsistentHashRouter::new(vec![node1.clone(), node2.clone()], 15);

        assert_eq!(router.get_existing_replicas(&node1), 15);
        assert_eq!(router.get_existing_replicas(&node2), 15);

        let node3 = TestNode::new("node3");
        assert_eq!(router.get_existing_replicas(&node3), 0);
    }

    #[test]
    fn test_md5_hash() {
        let hash_fn = MD5Hash::new();
        let hash1 = hash_fn.hash("test-key");
        let hash2 = hash_fn.hash("test-key");
        assert_eq!(hash1, hash2); // Same key should produce same hash

        let hash3 = hash_fn.hash("different-key");
        assert_ne!(hash1, hash3); // Different keys should (likely) produce different hashes
    }

    #[test]
    fn test_md5_hash_deterministic() {
        let hash_fn = MD5Hash::new();
        // Test that hash is deterministic
        for _ in 0..10 {
            let hash = hash_fn.hash("deterministic-test");
            assert_eq!(hash, hash_fn.hash("deterministic-test"));
        }
    }
}