Class PartitionTable

java.lang.Object
com.loomcache.server.cluster.PartitionTable

public class PartitionTable extends Object
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 Details

  • Constructor Details

    • PartitionTable

      public PartitionTable(int instanceNumber)
  • Method Details

    • getOwner

      public @Nullable String getOwner(int slotId)
      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

      public static int slotForKey(String key)
      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

      public Set<Integer> getSlotsForNode(String nodeId)
      Get all slots owned by a specific node.
      Parameters:
      nodeId - the node ID
      Returns:
      immutable set of slot IDs owned by this node
    • getSlotOwnership

      public Map<Integer,String> getSlotOwnership()
      Get a snapshot of current slot ownership.
      Returns:
      immutable map of slotId → nodeId
    • reassignSlots

      public void reassignSlots(List<String> members)
      Recompute all slot ownership based on cluster topology. Called when nodes join or leave.

      Algorithm:

      1. Clear ring
      2. Add all new member nodes to ring
      3. For each slot 0-16383, hash "slot:{slotId}" and assign to primary owner
      4. Atomically update the ownership snapshot
      Parameters:
      members - list of active member node IDs (from ClusterState)
    • previewReassignment

      public Map<Integer,String> previewReassignment(List<String> members)
    • replaceOwnership

      public void replaceOwnership(Map<Integer,String> newOwnership)
    • canonicalizeMembers

      public static List<String> canonicalizeMembers(Collection<String> members)
    • getDistribution

      public Map<String,Integer> getDistribution()
      Get distribution of slots across nodes.
      Returns:
      map of nodeId → slot count
    • 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 table
      newOwnership - new partition table
      Returns:
      map of nodeId → set of slots that this node should receive