Class DBFencedLockManager<UOW extends UnitOfWork,LOCK extends DBFencedLock>
- java.lang.Object
-
- dk.cloudcreate.essentials.components.foundation.fencedlock.DBFencedLockManager<UOW,LOCK>
-
- All Implemented Interfaces:
FencedLockManager,Lifecycle
public class DBFencedLockManager<UOW extends UnitOfWork,LOCK extends DBFencedLock> extends Object implements FencedLockManager
-
-
Field Summary
Fields Modifier and Type Field Description protected org.slf4j.Loggerlogprotected intsyncAcquireLockPauseIntervalMstryAcquireLock(LockName)/tryAcquireLock(LockName, Duration)andacquireLock(LockName)pause interval between retriesprotected UnitOfWorkFactory<? extends UOW>unitOfWorkFactory
-
Constructor Summary
Constructors Modifier Constructor Description protectedDBFencedLockManager(FencedLockStorage<UOW,LOCK> lockStorage, UnitOfWorkFactory<? extends UOW> unitOfWorkFactory, Optional<String> lockManagerInstanceId, Duration lockTimeOut, Duration lockConfirmationInterval, Optional<EventBus> eventBus)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FencedLockacquireLock(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 callFencedLockManager.cancelAsyncLockAcquiring(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 theFencedLockManager.acquireLockAsync(LockName, LockCallback)
Otherwise only the background lock acquiring process will be stopped.voiddeleteAllLocksInDB()longgetInitialTokenValue()StringgetLockManagerInstanceId()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 returnedLonggetUninitializedTokenValue()booleanisLockAcquired(LockName lockName)Is the lock acquiredbooleanisLockAcquiredByAnotherLockManagerInstance(LockName lockName)Is the lock already acquired by another JVM nodebooleanisLockedByThisLockManagerInstance(LockName lockName)Is the lock already acquired by this JVM nodebooleanisStarted()Returns true if the process is startedOptional<FencedLock>lookupLock(LockName lockName)Lookup a lockprotected voidnotify(FencedLockEvents event)voidpause()protected voidreleaseLock(LOCK lock)Internal method only to be called by subclasses ofDBFencedLockManagerandDBFencedLockvoidresume()voidstart()Start the processing.voidstop()Stop the processing.StringtoString()Optional<FencedLock>tryAcquireLock(LockName lockName)Try to acquire the lock using this Lock Manager instanceOptional<FencedLock>tryAcquireLock(LockName lockName, Duration timeout)Try to acquire the lock on this JVM Node
-
-
-
Field Detail
-
log
protected final org.slf4j.Logger log
-
unitOfWorkFactory
protected final UnitOfWorkFactory<? extends UOW extends UnitOfWork> unitOfWorkFactory
-
syncAcquireLockPauseIntervalMs
protected int syncAcquireLockPauseIntervalMs
tryAcquireLock(LockName)/tryAcquireLock(LockName, Duration)andacquireLock(LockName)pause interval between retries
-
-
Constructor Detail
-
DBFencedLockManager
protected DBFencedLockManager(FencedLockStorage<UOW,LOCK> lockStorage, UnitOfWorkFactory<? extends UOW> unitOfWorkFactory, Optional<String> lockManagerInstanceId, Duration lockTimeOut, Duration lockConfirmationInterval, Optional<EventBus> eventBus)
- Parameters:
lockStorage- the lock storage used for the lock managerunitOfWorkFactory- theUnitOfWorkfactorylockManagerInstanceId- The unique name for this lock manager instance. If leftOptional.empty()then the machines hostname is usedlockTimeOut- the period betweenFencedLock.getLockLastConfirmedTimestamp()and the current time before the lock is marked as timed outlockConfirmationInterval- how often should the locks be confirmed. MUST is less than thelockTimeOuteventBus- optionalLocalEventBuswhereFencedLockEventswill be published
-
-
Method Detail
-
start
public void start()
Description copied from interface:LifecycleStart the processing. This operation must be idempotent, such that duplicate calls toLifecycle.start()for an already started process (whereLifecycle.isStarted()returns true) is ignored
-
notify
protected void notify(FencedLockEvents event)
-
pause
public void pause()
-
resume
public void resume()
-
releaseLock
protected void releaseLock(LOCK lock)
Internal method only to be called by subclasses ofDBFencedLockManagerandDBFencedLock- Parameters:
lock- the lock to be released
-
lookupLock
public Optional<FencedLock> lookupLock(LockName lockName)
Description copied from interface:FencedLockManagerLookup a lock- Specified by:
lookupLockin interfaceFencedLockManager- Parameters:
lockName- the name of the lock- Returns:
- an
Optionalwith the locks current state orOptional.empty()if the lock doesn't exist
-
stop
public void stop()
Description copied from interface:LifecycleStop the processing. This operation must be idempotent, such that duplicate calls toLifecycle.stop()for an already stopped process (whereLifecycle.isStarted()returns false) is ignored
-
isStarted
public boolean isStarted()
Description copied from interface:LifecycleReturns true if the process is started
-
tryAcquireLock
public Optional<FencedLock> tryAcquireLock(LockName lockName)
Description copied from interface:FencedLockManagerTry to acquire the lock using this Lock Manager instance- Specified by:
tryAcquireLockin interfaceFencedLockManager- 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
public Optional<FencedLock> tryAcquireLock(LockName lockName, Duration timeout)
Description copied from interface:FencedLockManagerTry to acquire the lock on this JVM Node- Specified by:
tryAcquireLockin interfaceFencedLockManager- 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
public FencedLock acquireLock(LockName lockName)
Description copied from interface:FencedLockManagerAcquire 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- Specified by:
acquireLockin interfaceFencedLockManager- Parameters:
lockName- the name of the lock- Returns:
- The
FencedLockwhen can be acquired by this JVM Node
-
isLockAcquired
public boolean isLockAcquired(LockName lockName)
Description copied from interface:FencedLockManagerIs the lock acquired- Specified by:
isLockAcquiredin interfaceFencedLockManager- Parameters:
lockName- the name of the lock- Returns:
- true if the lock is acquired, otherwise false
-
isLockedByThisLockManagerInstance
public boolean isLockedByThisLockManagerInstance(LockName lockName)
Description copied from interface:FencedLockManagerIs the lock already acquired by this JVM node- Specified by:
isLockedByThisLockManagerInstancein interfaceFencedLockManager- Parameters:
lockName- the lock name- Returns:
- true of the lock is acquired by this JVM node
-
isLockAcquiredByAnotherLockManagerInstance
public boolean isLockAcquiredByAnotherLockManagerInstance(LockName lockName)
Description copied from interface:FencedLockManagerIs the lock already acquired by another JVM node- Specified by:
isLockAcquiredByAnotherLockManagerInstancein interfaceFencedLockManager- Parameters:
lockName- the name of the lock- Returns:
- true of the lock is acquired by this JVM node
-
acquireLockAsync
public void acquireLockAsync(LockName lockName, LockCallback lockCallback)
Description copied from interface:FencedLockManagerAsynchronously 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 callFencedLockManager.cancelAsyncLockAcquiring(LockName)with the same lockName- Specified by:
acquireLockAsyncin interfaceFencedLockManager- Parameters:
lockName- the name of the locklockCallback- the callback that's notified when a lock is acquired
-
cancelAsyncLockAcquiring
public void cancelAsyncLockAcquiring(LockName lockName)
Description copied from interface:FencedLockManagerCancel 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 theFencedLockManager.acquireLockAsync(LockName, LockCallback)
Otherwise only the background lock acquiring process will be stopped.- Specified by:
cancelAsyncLockAcquiringin interfaceFencedLockManager- Parameters:
lockName- the name of the lock
-
getLockManagerInstanceId
public String getLockManagerInstanceId()
Description copied from interface:FencedLockManagerGet 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- Specified by:
getLockManagerInstanceIdin interfaceFencedLockManager- Returns:
- the instance id
-
getUninitializedTokenValue
public Long getUninitializedTokenValue()
-
getInitialTokenValue
public long getInitialTokenValue()
-
deleteAllLocksInDB
public void deleteAllLocksInDB()
-
-