Class PartitionTable
java.lang.Object
com.loomcache.server.cluster.PartitionTable
Maps 16384 partition slots to owner node IDs using consistent hashing.
NOTE (v1): In the current CP-by-default full-replication model, partitioning is NOT used for data placement — all nodes hold all data via a single Raft group. This class is a placeholder for v2 multi-Raft partitioned sharding (like CockroachDB), where each partition gets its own Raft group with independent leader election and failover.
Architecture:
- Total slots: 16384 (0-16383)
- Each slot is owned by a single node determined by ConsistentHashRing
- Thread-safe reads via volatile reference to snapshot
- Bulk reassignment via
reassignSlots(List)when topology changes
Thread safety: Uses AtomicReference to hold an immutable snapshot of the partition map. Reads are O(1) without locks. Writes (topology changes) are exclusive.
- Since:
- 1.0
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncanonicalizeMembers(Collection<String> members) Compute the delta (slots to migrate) between old and new membership.Get distribution of slots across nodes.@Nullable StringgetOwner(int slotId) Get the owner node ID for a given slot.Get a snapshot of current slot ownership.getSlotsForNode(String nodeId) Get all slots owned by a specific node.previewReassignment(List<String> members) voidreassignSlots(List<String> members) Recompute all slot ownership based on cluster topology.voidreplaceOwnership(Map<Integer, String> newOwnership) static intslotForKey(String key) Canonical mapping from a user key to its partition slot.
-
Field Details
-
TOTAL_SLOTS
public static final int TOTAL_SLOTS- See Also:
-
-
Constructor Details
-
PartitionTable
public PartitionTable(int instanceNumber)
-
-
Method Details
-
getOwner
Get the owner node ID for a given slot.- Parameters:
slotId- slot identifier (0-16383)- Returns:
- node ID owning this slot, or null if unknown
-
slotForKey
Canonical mapping from a user key to its partition slot. Kept in one place so every call site uses the same hash (server-side migration pipeline, smart-client routing, and tests).- Parameters:
key- user-visible data key- Returns:
- slot id in [0,
TOTAL_SLOTS)
-
getSlotsForNode
-
getSlotOwnership
-
reassignSlots
Recompute all slot ownership based on cluster topology. Called when nodes join or leave.Algorithm:
- Clear ring
- Add all new member nodes to ring
- For each slot 0-16383, hash "slot:{slotId}" and assign to primary owner
- Atomically update the ownership snapshot
- Parameters:
members- list of active member node IDs (from ClusterState)
-
previewReassignment
-
replaceOwnership
-
canonicalizeMembers
-
getDistribution
-
computeMigrationDelta
public static Map<String, Set<Integer>> computeMigrationDelta(Map<Integer, String> oldOwnership, Map<Integer, String> newOwnership) Compute the delta (slots to migrate) between old and new membership.- Parameters:
oldOwnership- old partition tablenewOwnership- new partition table- Returns:
- map of nodeId → set of slots that this node should receive
-