/** * Create a new LockHolder. This is done *after* the distributed lock was acquired. * * @param lockName */ public void createLock(final String lockName, final GlobalLock originalLock) { final String requestId = getRequestId(); if (requestId == null) { return; } synchronized (lockTable) { LockHolder lockHolder = lockTable.get(lockName); if (lockHolder != null) { throw new IllegalStateException(String.format("ReentrantLock createLock %s : lock already current request = %s, owner request = %s", lockName, requestId, lockHolder.getRequestId())); } if (lockHolder == null) { lockHolder = new LockHolder(requestId, originalLock); lockTable.put(lockName, lockHolder); } lockHolder.increment(); } }
/** * Release a lock. This is always called when the resources are feed * * @param lockName * @return true if nobody still holds the reentrant lock (and thefeore distributed lock can be freed) */ public boolean releaseLock(final String lockName) { // In case there no requestId set, this was not a 'reentrant' lock, so nothing to do but we need to return true // so distributed lock can be released // final String requestId = getRequestId(); if (requestId == null) { return true; } synchronized (lockTable) { final LockHolder lockHolder = lockTable.get(lockName); if (lockHolder == null) { throw new IllegalStateException(String.format("ReentrantLock releaseLock %s : cannot find lock in the table, current request = %s", lockName, requestId)); } if (!lockHolder.getRequestId().equals(requestId)) { throw new IllegalStateException(String.format("ReentrantLock releaseLock %s : current request = %s, owner request = %s", lockName, requestId, lockHolder.getRequestId())); } final boolean free = lockHolder.decrement(); if (free) { lockTable.remove(lockName); } return free; } }
/** * Atomically increment the refCount lock if we are already the owner of that lock. * * @param lockName * @return the ReentrantLockState: lock is FREE, or we already hold it (and incremented the refCount) or it held by somebody else */ public TryAcquireLockState tryAcquireLockForExistingOwner(final String lockName) { synchronized (lockTable) { final LockHolder lockHolder = lockTable.get(lockName); if (lockHolder == null) { return new TryAcquireLockState(ReentrantLockState.FREE); } final String maybeNullRequestId = getRequestId(); if (maybeNullRequestId == null || !lockHolder.getRequestId().equals(maybeNullRequestId)) { return new TryAcquireLockState(ReentrantLockState.HELD_NOT_OWNER); } else { // Increment value before we return while we hold the lockTable lock. lockHolder.increment(); return new TryAcquireLockState(ReentrantLockState.HELD_OWNER, lockHolder.getOriginalLock()); } } }