Class LockManager
byte[]
key ranges that ensures isolation and serialization while allowing concurrent
access by multiple threads to a single underlying byte[]
key/value store.
This implementation is straightforward: read locks can overlap, but write locks may not, and all locks owned by the same owner remain in force until all are released at the same time.
Instances are configured with a monitor object which is used for all internal locking and inter-thread wait/notify handshaking (by default, this instance). A user-supplied monitor object may be provided via the constructor.
Two timeout values are supported:
- The wait timeout (specified as a parameter to
lock()
) limits how long a thread will wait on a lock held by another thread before giving up - The hold timeout limits how long a thread may hold
on to a contested lock before being forced to release all its locks; after that, the
next call to
lock
orrelease
will fail
Note that if the hold timeout is set to zero (unlimited), then an application bug that leaks locks will result in those locks never being released.
-
Nested Class Summary
-
Constructor Summary
ConstructorDescriptionConvenience constructor.LockManager
(Object lockObject) Primary constructor. -
Method Summary
Modifier and TypeMethodDescriptionlong
checkHoldTimeout
(LockOwner owner) Check whether the hold timeout has expired for the given lock owner and if not return the amount of time remaining.long
Get the hold timeout configured for this instance.boolean
Determine if the given lock owner holds a lock on the specified range.Acquire a lock on behalf of the specified owner.boolean
Release all locks held by the specified owner.void
setHoldTimeout
(long holdTimeout) Set the hold timeout for this instance.
-
Constructor Details
-
LockManager
public LockManager()Convenience constructor. Equivalent toLockManager(null)
. -
LockManager
Primary constructor.- Parameters:
lockObject
- Java object used to synchronize field access and inter-thread wait/notify handshake, or null to use this instance
-
-
Method Details
-
getHoldTimeout
public long getHoldTimeout()Get the hold timeout configured for this instance.The hold timeout limits how long a thread may hold on to a contested lock before being forced to release all of its locks; after that, the next call to
lock
orrelease
will fail.- Returns:
- hold timeout in milliseconds
-
setHoldTimeout
public void setHoldTimeout(long holdTimeout) Set the hold timeout for this instance. Default is zero (unlimited).- Parameters:
holdTimeout
- how long a thread may hold a contested lock beforeLockManager.LockResult.HOLD_TIMEOUT_EXPIRED
will be returned bylock()
orrelease()
in milliseconds, or zero for unlimited- Throws:
IllegalArgumentException
- ifholdTimeout
is negative
-
lock
public LockManager.LockResult lock(LockOwner owner, byte[] minKey, byte[] maxKey, boolean write, long waitTimeout) throws InterruptedException Acquire a lock on behalf of the specified owner.This method will block for up to
waitTimeout
milliseconds if the lock is held by another thread, after which pointLockManager.LockResult.WAIT_TIMEOUT_EXPIRED
is returned. The configured locking object will be used for inter-thread wait/notify handshaking.If
owner
already holds one or more locks, but the hold timeout has expired, thenLockManager.LockResult.HOLD_TIMEOUT_EXPIRED
is returned and all of the other locks are will have already been automatically released.Once a lock is successfully acquired, it stays acquired until all locks are released together via
release()
.- Parameters:
owner
- lock ownerminKey
- minimum key (inclusive); must not be nullmaxKey
- maximum key (exclusive), or null for no maximumwrite
- true for a write lock, false for a read lockwaitTimeout
- how long to wait before returningLockManager.LockResult.WAIT_TIMEOUT_EXPIRED
in milliseconds, or zero for unlimited- Returns:
- a
LockManager.LockResult
- Throws:
InterruptedException
- if the current thread is interrupted while waiting for the lockIllegalArgumentException
- ifowner
,minKey
, orrange
is nullIllegalArgumentException
- ifminKey > maxKey
IllegalArgumentException
- ifwaitTimeout
is negative
-
isLocked
Determine if the given lock owner holds a lock on the specified range.- Parameters:
owner
- lock ownerminKey
- minimum key (inclusive); must not be nullmaxKey
- maximum key (exclusive), or null for no maximumwrite
- if range must be write locked; if false, may be either read or write locked- Returns:
- true if the range is locked for writes by
owner
-
release
Release all locks held by the specified owner.If the owner's hold timeout has already expired, then all locks will have already been released and false is returned.
Does nothing (and returns true) if
owner
does not own any locks.- Parameters:
owner
- lock owner- Returns:
- true if successful, false if
owner
's hold timeout expired - Throws:
IllegalArgumentException
- ifowner
is null
-
checkHoldTimeout
Check whether the hold timeout has expired for the given lock owner and if not return the amount of time remaining.If the owner's hold timeout has expired, then
-1
is returned and any locks previously held byowner
will have been automatically released.- Parameters:
owner
- lock owner- Returns:
- milliseconds until
owner
's hold timeout expires, zero ifowner
has no hold timeout (e.g., nothing is locked or hold timeout disabled), or -1 ifowner
's hold timeout has expired - Throws:
IllegalArgumentException
- ifowner
is null
-