Class ConsistentHashRing
Architecture:
- Each physical node gets VNODES_PER_NODE (256) virtual positions on the ring
- Ring is a sorted TreeMap<Integer, String> mapping hash → nodeId
- Lookup: hash(key) → find next clockwise vnode → that node is the owner
- Backup: the NEXT node clockwise after the primary (different physical node)
Adding a node: 256 new vnodes on ring, ~1/N data migrates. Removing a node: 256 vnodes removed, data shifts to next clockwise.
Thread safety: ReadWriteLock allows concurrent reads during lookups and exclusive access during topology changes.
- Since:
- 1.0
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConsistentHashRing(int instanceNumber) ConsistentHashRing(int instanceNumber, PartitionGroupConfig partitionGroupConfig) ConsistentHashRing(int instanceNumber, PartitionGroupType partitionGroupType) -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a physical node to the ring.@Nullable StringgetBackupNode(String key) Find the BACKUP owner of a key.Calculate the distribution of vnodes across physical nodes.getKeysOwnedBy(String nodeId, Set<String> allKeys) Get all keys that should be owned by a specific node.@Nullable NodeInfogetNodeInfo(String nodeId) Get metadata for a specific node, or null if not on the ring.getNodes()Snapshot of all physical nodes currently on the ring.@Nullable String[]Get both primary and backup for a key.@Nullable StringgetPrimaryNode(String key) Find the PRIMARY owner of a key.intNumber of distinct physical nodes on the ring.voidremoveNode(String nodeId) Remove a physical node and all its virtual positions from the ring.intringSize()Total number of virtual nodes on the ring.
-
Field Details
-
VNODES_PER_NODE
public static final int VNODES_PER_NODE- See Also:
-
-
Constructor Details
-
ConsistentHashRing
public ConsistentHashRing(int instanceNumber) -
ConsistentHashRing
-
ConsistentHashRing
-
-
Method Details
-
partitionGroupType
-
partitionGroupConfig
-
addNode
Add a physical node to the ring.Places VNODES_PER_NODE (256) virtual positions on the consistent hash ring. If the node is already on the ring, this is a no-op and logged as a warning.
- Parameters:
node- the node to add (must not be null)- Throws:
IllegalArgumentException- if node is null
-
removeNode
Remove a physical node and all its virtual positions from the ring.After removal, all keys that were owned by this node will migrate to the next node clockwise on the ring. If the node is not on the ring, this is a no-op and logged as a warning.
- Parameters:
nodeId- the ID of the node to remove (must not be null)- Throws:
IllegalArgumentException- if nodeId is null
-
getPrimaryNode
-
getBackupNode
Find the BACKUP owner of a key.Returns the next DIFFERENT physical node clockwise after the primary. This ensures primary and backup are always on different physical nodes, providing resilience against single-node failure.
- Parameters:
key- the key to hash and locate (must not be null)- Returns:
- the backup node ID, or null if invalid input: '<' 2 nodes on ring
-
getOwners
-
getKeysOwnedBy
-
getDistribution
-
ringSize
public int ringSize()Total number of virtual nodes on the ring. -
nodeCount
public int nodeCount()Number of distinct physical nodes on the ring. -
getNodes
Snapshot of all physical nodes currently on the ring. -
getNodeInfo
-