pub struct ConsistentHashRouter<T: Node + Clone> { /* private fields */ }Expand description
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 implementNodeandClonetraits
§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());Implementations§
Source§impl<T: Node + Clone> ConsistentHashRouter<T>
impl<T: Node + Clone> ConsistentHashRouter<T>
Sourcepub fn new(physical_nodes: Vec<T>, virtual_node_count: i32) -> Self
pub fn new(physical_nodes: Vec<T>, virtual_node_count: i32) -> Self
Create a new consistent hash router with the default MD5 hash function
§Arguments
physical_nodes- Collection of physical nodes to add to the ringvirtual_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);Sourcepub fn try_new(
physical_nodes: Vec<T>,
virtual_node_count: i32,
) -> RocketMQResult<Self>
pub fn try_new( physical_nodes: Vec<T>, virtual_node_count: i32, ) -> RocketMQResult<Self>
Create a new consistent hash router and return a typed error when the configuration is invalid.
Sourcepub fn new_with_hash_function(
physical_nodes: Vec<T>,
virtual_node_count: i32,
hash_function: Arc<dyn HashFunction>,
) -> Self
pub fn new_with_hash_function( physical_nodes: Vec<T>, virtual_node_count: i32, hash_function: Arc<dyn HashFunction>, ) -> Self
Create a new consistent hash router with a custom hash function
§Arguments
physical_nodes- Collection of physical nodes to add to the ringvirtual_node_count- Number of virtual nodes per physical nodehash_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.
Sourcepub fn try_new_with_hash_function(
physical_nodes: Vec<T>,
virtual_node_count: i32,
hash_function: Arc<dyn HashFunction>,
) -> RocketMQResult<Self>
pub fn try_new_with_hash_function( physical_nodes: Vec<T>, virtual_node_count: i32, hash_function: Arc<dyn HashFunction>, ) -> RocketMQResult<Self>
Create a new consistent hash router with a custom hash function and return a typed error
when virtual_node_count is negative.
Sourcepub fn add_node(&mut self, physical_node: T, virtual_node_count: i32)
pub fn add_node(&mut self, physical_node: T, virtual_node_count: i32)
Add a physical node to the hash ring with virtual nodes
§Arguments
physical_node- Physical node to add to the hash ringvirtual_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);Sourcepub fn try_add_node(
&mut self,
physical_node: T,
virtual_node_count: i32,
) -> RocketMQResult<()>
pub fn try_add_node( &mut self, physical_node: T, virtual_node_count: i32, ) -> RocketMQResult<()>
Add a physical node to the hash ring, returning a typed error when virtual_node_count is
negative.
Sourcepub fn remove_node(&mut self, physical_node: &T)
pub fn remove_node(&mut self, physical_node: &T)
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());Sourcepub fn route_node_ref(&self, object_key: &str) -> Option<&T>
pub fn route_node_ref(&self, object_key: &str) -> Option<&T>
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 toNone- 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());