Interface FencedLockManager
- All Superinterfaces:
Lifecycle
- All Known Implementing Classes:
DBFencedLockManager
This library provides a Distributed Locking Manager based of the Fenced Locking concept
described here
The
Only one
The implementation has been on supporting intra-service (i.e. across different deployed instances of the same service) Lock support through database based implementations (
In a service oriented architecture it's common for all deployed instances of a given service (e.g. a Sales service) to share the same underlying database(s). As long as the different deployed (Sales) services instances can share the same underlying database, then you use the
If you need cross-service lock support, e.g. across instances of different services (such as across Sales, Billing and Shipping services), then you need to use a dedicated distributed locking service such as Zookeeper.
To coordinate this properly it's important that each
Per default, it uses the local machine hostname as
The
FencedLockManager is responsible for obtaining and managing distributed FencedLock's, which are named exclusive locks.Only one
FencedLockManager instance can acquire a FencedLock at a time.The implementation has been on supporting intra-service (i.e. across different deployed instances of the same service) Lock support through database based implementations (
MongoFencedLockManager and PostgresqlFencedLockManager).In a service oriented architecture it's common for all deployed instances of a given service (e.g. a Sales service) to share the same underlying database(s). As long as the different deployed (Sales) services instances can share the same underlying database, then you use the
FencedLockManager concept for handling distributed locks across all deployed (Sales service)
instances in the cluster.If you need cross-service lock support, e.g. across instances of different services (such as across Sales, Billing and Shipping services), then you need to use a dedicated distributed locking service such as Zookeeper.
To coordinate this properly it's important that each
getLockManagerInstanceId()
is unique across the cluster.Per default, it uses the local machine hostname as
getLockManagerInstanceId() value.-
Method Summary
Modifier and TypeMethodDescriptionacquireLock(LockName lockName) Acquire the lock on this JVM Node.voidacquireLockAsync(LockName lockName, LockCallback lockCallback) Asynchronously try to acquire a lock by the given name and call theLockCallback.lockAcquired(FencedLock)when the lock is acquired
To stop the background acquiring process, you need to callcancelAsyncLockAcquiring(LockName)with the same lockNamevoidcancelAsyncLockAcquiring(LockName lockName) Cancel a previously started asynchronous lock acquiring background process
IF this JVM node had acquired aFencedLockthen this lock will be released AND theLockCallback.lockReleased(FencedLock)will be called on theLockCallbackinstance that was supplied to theacquireLockAsync(LockName, LockCallback)
Otherwise only the background lock acquiring process will be stopped.Get the instance id that distinguishes differentFencedLockManagerinstances from each other
For local JVM testing you can assign a unique instance id to allow multipleFencedLockManager's to compete for Locks.
In production the hostname should be returnedbooleanisLockAcquired(LockName lockName) Is the lock acquiredbooleanIs the lock already acquired by another JVM nodebooleanisLockedByThisLockManagerInstance(LockName lockName) Is the lock already acquired by this JVM nodelookupLock(LockName lockName) Lookup a locktryAcquireLock(LockName lockName) Try to acquire the lock using this Lock Manager instancetryAcquireLock(LockName lockName, Duration timeout) Try to acquire the lock on this JVM Node
-
Method Details
-
lookupLock
Lookup a lock- Parameters:
lockName- the name of the lock- Returns:
- an
Optionalwith the locks current state orOptional.empty()if the lock doesn't exist
-
tryAcquireLock
Try to acquire the lock using this Lock Manager instance- Parameters:
lockName- the name of the lock- Returns:
- An Optional with the
FencedLockIF it was acquired by theFencedLockManageron this JVM Node, otherwise it return an empty Optional
-
tryAcquireLock
Try to acquire the lock on this JVM Node- Parameters:
lockName- the name of the locktimeout- timeout for the lock acquiring- Returns:
- An Optional with the
FencedLockIF it was acquired by theFencedLockManageron this JVM Node, otherwise it return an empty Optional
-
acquireLock
Acquire the lock on this JVM Node. If the lock already is acquired by another JVM Node, then this method will wait until the lock can be acquired by this JVM node- Parameters:
lockName- the name of the lock- Returns:
- The
FencedLockwhen can be acquired by this JVM Node
-
isLockAcquired
Is the lock acquired- Parameters:
lockName- the name of the lock- Returns:
- true if the lock is acquired, otherwise false
-
isLockedByThisLockManagerInstance
Is the lock already acquired by this JVM node- Parameters:
lockName- the lock name- Returns:
- true of the lock is acquired by this JVM node
-
isLockAcquiredByAnotherLockManagerInstance
Is the lock already acquired by another JVM node- Parameters:
lockName- the name of the lock- Returns:
- true of the lock is acquired by this JVM node
-
acquireLockAsync
Asynchronously try to acquire a lock by the given name and call theLockCallback.lockAcquired(FencedLock)when the lock is acquired
To stop the background acquiring process, you need to callcancelAsyncLockAcquiring(LockName)with the same lockName- Parameters:
lockName- the name of the locklockCallback- the callback that's notified when a lock is acquired
-
cancelAsyncLockAcquiring
Cancel a previously started asynchronous lock acquiring background process
IF this JVM node had acquired aFencedLockthen this lock will be released AND theLockCallback.lockReleased(FencedLock)will be called on theLockCallbackinstance that was supplied to theacquireLockAsync(LockName, LockCallback)
Otherwise only the background lock acquiring process will be stopped.- Parameters:
lockName- the name of the lock
-
getLockManagerInstanceId
String getLockManagerInstanceId()Get the instance id that distinguishes differentFencedLockManagerinstances from each other
For local JVM testing you can assign a unique instance id to allow multipleFencedLockManager's to compete for Locks.
In production the hostname should be returned- Returns:
- the instance id
-