Class PartitionRebalancer
java.lang.Object
com.loomcache.server.sharding.PartitionRebalancer
Handles partition migration between Raft groups when groups are added or removed.
Rebalancing algorithm:
- Compute ideal assignment: partition
igoes to groupi % targetGroups - Compare with current assignment to find moves
- Execute moves sequentially: copy data, then update routing
- Fence old writes by updating the partition router atomically
Consistency during migration (two-phase copy with routing fence):
- Copy: snapshot partition entries and copy to target group
- Fence: atomically update routing so new writes go to target
- Sweep: re-scan source for entries written during the copy phase
- Cleanup: delete migrated entries from source (safe: routing already points to target)
If migration fails, the plan is rolled back to the last successfully completed move. Progress is tracked and can be queried by monitoring tools.
Thread Safety: Only one migration can run at a time, enforced by a reentrant lock. Progress queries are lock-free via AtomicReference.
- Since:
- 2.0
-
Constructor Summary
ConstructorsConstructorDescriptionPartitionRebalancer(PartitionRouter router, ShardedDataStructureRegistry shardedRegistry) Creates a new rebalancer for the given router and sharded registry. -
Method Summary
Modifier and TypeMethodDescriptioncalculatePlan(int currentGroups, int targetGroups) Calculates a rebalance plan to move from the current group count to the target.voidExecutes a migration plan, moving data between groups and updating routing.Returns the current migration progress.
-
Constructor Details
-
PartitionRebalancer
Creates a new rebalancer for the given router and sharded registry.- Parameters:
router- the partition router to update (must not be null)shardedRegistry- the sharded registry containing group data (must not be null)
-
-
Method Details
-
calculatePlan
Calculates a rebalance plan to move from the current group count to the target.The plan computes the ideal round-robin assignment for
targetGroupsand determines which partitions need to move from their current group.- Parameters:
currentGroups- the current number of Raft groups (must be > 0)targetGroups- the desired number of Raft groups (must be > 0)- Returns:
- the rebalance plan (never null, may be empty if no moves needed)
-
executeMigration
Executes a migration plan, moving data between groups and updating routing.Moves are executed sequentially. For each move:
- Data from the source group is copied to the destination group
- The partition router mapping is updated
- Progress is updated atomically
If a move fails, the migration stops and the state is left at the last successfully completed move. The router reflects all completed moves.
- Parameters:
plan- the rebalance plan to execute (must not be null)
-
getProgress
Returns the current migration progress.- Returns:
- the progress snapshot (never null)
-