Class ConsistentHash<T>
java.lang.Object
cloud.opencode.base.hash.consistent.ConsistentHash<T>
- Type Parameters:
T- node data type | 节点数据类型
- All Implemented Interfaces:
NodeLocator<T>
Consistent hash ring implementation
一致性哈希环实现
Implements consistent hashing with virtual nodes for improved load distribution. Supports dynamic node addition/removal with minimal key migration.
实现带虚拟节点的一致性哈希以改善负载分布。 支持动态节点添加/删除,最小化键迁移。
Features | 主要功能:
- Virtual nodes for better distribution - 虚拟节点以获得更好的分布
- Weight-based virtual node count - 基于权重的虚拟节点数量
- Optional concurrent access support - 可选的并发访问支持
- Distribution statistics - 分布统计
Usage Examples | 使用示例:
ConsistentHash<String> ring = ConsistentHash.<String>builder()
.virtualNodeCount(150)
.addNode("server1", "192.168.1.1")
.addNode("server2", "192.168.1.2", 2)
.concurrent(true)
.build();
String server = ring.get("user_123");
List<String> replicas = ring.get("data_key", 3);
Security | 安全性:
- Thread-safe: Optional (use concurrent(true)) - 线程安全: 可选(使用concurrent(true))
Performance | 性能特性:
- Time complexity: O(log n) for lookup where n = virtual nodes - 查找 O(log n), n为虚拟节点数
- Space complexity: O(n * v) where v = virtual replicas - O(n * v), v为虚拟副本数
- Since:
- JDK 25, opencode-base-hash V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a node with default weight 添加默认权重的节点voidAdds a weighted node 添加带权重的节点static <T> ConsistentHashBuilder<T> builder()Creates a builder 创建构建器voidclear()Clears all nodes 清空所有节点Gets the node for a key 获取键对应的节点Gets multiple nodes for a key (for replicas) 获取键对应的多个节点(用于副本)getDistribution(Collection<?> keys) Gets key distribution across nodes 获取键在节点间的分布intgetMigrationCount(String nodeId, Collection<?> keys) Gets the migration count if a node is removed 获取如果移除节点需要迁移的键数量intGets the physical node count 获取物理节点数量getNodes()Gets all physical nodes 获取所有物理节点getVirtualNode(long hashValue) Gets the virtual node at the specified position 获取指定位置的虚拟节点intGets the total number of virtual nodes 获取虚拟节点总数booleanisEmpty()Checks if the ring is empty 检查环是否为空locate(long hashValue) Locates the node for a given hash value 为给定的哈希值定位节点locateAll(long hashValue, int count) Locates multiple nodes for replica placement 为副本放置定位多个节点voidremoveNode(String nodeId) Removes a node 移除节点
-
Method Details
-
get
-
get
-
locate
Description copied from interface:NodeLocatorLocates the node for a given hash value 为给定的哈希值定位节点- Specified by:
locatein interfaceNodeLocator<T>- Parameters:
hashValue- hash value | 哈希值- Returns:
- the node data, or null if ring is empty | 节点数据,如果环为空则返回null
-
locateAll
Description copied from interface:NodeLocatorLocates multiple nodes for replica placement 为副本放置定位多个节点Returns nodes in clockwise order from the hash position. Duplicate physical nodes are excluded.
按哈希位置的顺时针顺序返回节点。排除重复的物理节点。
- Specified by:
locateAllin interfaceNodeLocator<T>- Parameters:
hashValue- hash value | 哈希值count- number of nodes to return | 要返回的节点数- Returns:
- list of node data | 节点数据列表
-
getVirtualNode
Description copied from interface:NodeLocatorGets the virtual node at the specified position 获取指定位置的虚拟节点- Specified by:
getVirtualNodein interfaceNodeLocator<T>- Parameters:
hashValue- hash value | 哈希值- Returns:
- virtual node, or null if ring is empty | 虚拟节点,如果环为空则返回null
-
isEmpty
public boolean isEmpty()Description copied from interface:NodeLocatorChecks if the ring is empty 检查环是否为空- Specified by:
isEmptyin interfaceNodeLocator<T>- Returns:
- true if empty | 如果为空返回true
-
getVirtualNodeCount
public int getVirtualNodeCount()Description copied from interface:NodeLocatorGets the total number of virtual nodes 获取虚拟节点总数- Specified by:
getVirtualNodeCountin interfaceNodeLocator<T>- Returns:
- virtual node count | 虚拟节点数量
-
addNode
-
addNode
-
removeNode
-
getNodes
-
getNodeCount
public int getNodeCount()Gets the physical node count 获取物理节点数量- Returns:
- node count | 节点数量
-
clear
public void clear()Clears all nodes 清空所有节点 -
getDistribution
Gets key distribution across nodes 获取键在节点间的分布- Parameters:
keys- collection of keys | 键集合- Returns:
- map of node id to key count | 节点ID到键数量的映射
-
getMigrationCount
Gets the migration count if a node is removed 获取如果移除节点需要迁移的键数量- Parameters:
nodeId- node id | 节点IDkeys- collection of keys | 键集合- Returns:
- number of keys that would be migrated | 需要迁移的键数量
-
builder
Creates a builder 创建构建器- Type Parameters:
T- node data type | 节点数据类型- Returns:
- builder | 构建器
-