Class GroupLockManager

java.lang.Object
com.loomcache.server.sharding.GroupLockManager

public class GroupLockManager extends Object
Manages cross-group lock acquisition with deadlock prevention.

Groups are always locked in ascending ID order and unlocked in reverse order. This sorted-order strategy prevents deadlocks across concurrent cross-group transactions that may involve overlapping groups.

Each group is represented by a ReentrantLock created on demand and cached for the lifetime of this manager.

Since:
2.0
  • Constructor Details

    • GroupLockManager

      public GroupLockManager()
  • Method Details

    • lockGroups

      public boolean lockGroups(List<Integer> groupIds, Duration timeout) throws InterruptedException
      Acquire locks on the specified groups in sorted ascending order.

      Blocks up to the given timeout per group. If any lock acquisition fails, all previously acquired locks are released in reverse order.

      Parameters:
      groupIds - the group IDs to lock (need not be sorted — will be sorted internally)
      timeout - maximum time to wait for each lock
      Returns:
      true if all locks were acquired, false if any timed out
      Throws:
      InterruptedException - if the thread is interrupted while waiting
    • unlockGroups

      public void unlockGroups()
      Release all locks currently held by this thread, in reverse acquisition order.
    • heldLockCount

      public int heldLockCount()
      Get the number of groups currently locked by this thread.
      Returns:
      the count of held group locks