Class BambooClusterLockServiceImpl
- java.lang.Object
-
- com.atlassian.beehive.db.DatabaseClusterLockService
-
- com.atlassian.bamboo.beehive.BambooClusterLockServiceImpl
-
- All Implemented Interfaces:
BambooClusterLockService
,com.atlassian.beehive.ClusterLockService
,com.atlassian.beehive.core.ManagedClusterLockService
public class BambooClusterLockServiceImpl extends com.atlassian.beehive.db.DatabaseClusterLockService implements BambooClusterLockService
Supports mutual exclusion operations in a cluster environment.- Since:
- 9.5
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_LOCK_TIMEOUT
static String
MULTIPLE_LOCK_NAME_SEPARATOR
-
Constructor Summary
Constructors Constructor Description BambooClusterLockServiceImpl(@NotNull BambooClusterLockDao bambooClusterLockDao, @NotNull ClusterNodeHeartbeatDao clusterNodeHeartbeatDao)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static @NotNull String
getReadLockName(@NotNull String lockName)
static @NotNull String
getReadLockNameForNode(@NotNull String lockName, @NotNull String nodeId)
static @NotNull String
getWriteLockName(@NotNull String lockName)
void
lockForReading(String lockName, long lockTimeoutSeconds)
Locks for READING the lock identified by the given name.void
lockForReading(String lockName, @NotNull String nodeId, long lockTimeoutSeconds)
void
lockForWriting(String lockName, long lockTimeoutSeconds)
Locks for READING the lock identified by the given name.void
releaseLock(@NotNull String lockName)
Release ownership of a named lock by current cluster node.void
releaseLocksHeldByNode()
Releases any locks held by the this node.void
releaseReadLock(@NotNull String lockName)
Release READ ownership of a named lock by current cluster node.void
releaseWriteLock(@NotNull String lockName)
Release READ ownership of a named lock by current cluster node.void
shutdown()
Shuts the service down, stopping all accessory jobs (e..g: lock renewer) for all locks.boolean
tryAcquireLock(@NotNull String lockName, long lockTimeoutSeconds)
Attempts to acquire ownership of a named lock by current cluster node.boolean
tryAcquireReadLock(@NotNull String lockName, long lockTimeoutSeconds)
Attempts to acquire READ ownership of a named lock by current cluster node.boolean
tryAcquireWriteLock(@NotNull String lockName, long lockTimeoutSeconds)
AttemptS to acquire WRITE ownership of a named lock by current cluster node.void
unlockForReading(String lockName)
Unlocks the READING part of the lock identified by the given name.void
unlockForReading(String lockName, @NotNull String nodeId)
void
unlockForWriting(String lockName)
Unlocks the WRITING part of the lock identified by the given name.-
Methods inherited from class com.atlassian.beehive.db.DatabaseClusterLockService
getAllKnownClusterLocks, getLockForName, getStatusesOfAllHeldClusterLocks, resetDatabaseState
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.atlassian.bamboo.beehive.BambooClusterLockService
resetDatabaseState
-
-
-
-
Field Detail
-
DEFAULT_LOCK_TIMEOUT
public static final int DEFAULT_LOCK_TIMEOUT
- See Also:
- Constant Field Values
-
MULTIPLE_LOCK_NAME_SEPARATOR
public static final String MULTIPLE_LOCK_NAME_SEPARATOR
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BambooClusterLockServiceImpl
public BambooClusterLockServiceImpl(@NotNull @NotNull BambooClusterLockDao bambooClusterLockDao, @NotNull @NotNull ClusterNodeHeartbeatDao clusterNodeHeartbeatDao)
-
-
Method Detail
-
releaseLocksHeldByNode
public void releaseLocksHeldByNode()
Description copied from interface:BambooClusterLockService
Releases any locks held by the this node.- Specified by:
releaseLocksHeldByNode
in interfaceBambooClusterLockService
-
tryAcquireLock
public boolean tryAcquireLock(@NotNull @NotNull String lockName, long lockTimeoutSeconds)
Description copied from interface:BambooClusterLockService
Attempts to acquire ownership of a named lock by current cluster node. A lock should be treated as eligible to be acquired when one of the following is true:- It has no node set as an owner
- It has the current node set as an owner
- It has an other node set as an owner, but its last update time is older than the specified timeout
- Specified by:
tryAcquireLock
in interfaceBambooClusterLockService
- Parameters:
lockName
- the globally unique id of the locklockTimeoutSeconds
- time (in seconds) after which the lock can be taken over- Returns:
- true if we acquired the lock, false if the locks already has a valid owner.
-
releaseLock
public void releaseLock(@NotNull @NotNull String lockName)
Description copied from interface:BambooClusterLockService
Release ownership of a named lock by current cluster node.- Specified by:
releaseLock
in interfaceBambooClusterLockService
- Parameters:
lockName
- the globally unique id of the lock
-
lockForWriting
public void lockForWriting(@Nonnull String lockName, long lockTimeoutSeconds)
Description copied from interface:BambooClusterLockService
Locks for READING the lock identified by the given name. A WRITE lock should be treated as eligible to be acquired when one of the following is true:- It has no node set as an owner of correspondent WRITE lock
- It has no node set as an owner of any correspondent READ locks
- It has nodes set as owners of correspondent READ/WRITE locks, but their last update times are older than the specified time
BambooClusterLockService.tryAcquireWriteLock(String, long)
.Important: this is a NON-REENTRANT implementation, i.e., if the node tries to acquire a lock that it already owns, then it will not be granted. Rationale: reentrant implementation requires mechanism to track re-entrancies in order to prevent releasing the lock before all operations are completed. It can be considered in the future.
There is no limit for the number of nodes that can acquire READ lock simultaneously, but only one can acquire the correspondent WRITE.
Make sure that
BambooClusterLockService.unlockForWriting(String)
is called in a try...finally block.*
- Specified by:
lockForWriting
in interfaceBambooClusterLockService
- Parameters:
lockName
- the globally unique id of the locklockTimeoutSeconds
- time (in seconds) after which an existing WRITE lock doesn't block the READ ones anymore
-
unlockForWriting
public void unlockForWriting(@Nonnull String lockName)
Description copied from interface:BambooClusterLockService
Unlocks the WRITING part of the lock identified by the given name.- Specified by:
unlockForWriting
in interfaceBambooClusterLockService
- Parameters:
lockName
- the globally unique id of the lock
-
lockForReading
public void lockForReading(@Nonnull String lockName, long lockTimeoutSeconds)
Description copied from interface:BambooClusterLockService
Locks for READING the lock identified by the given name. A READ lock should be treated as eligible to be acquired when one of the following is true:- It has no node set as an owner of correspondent WRITE lock
- It has an node set as an owner of correspondent WRITE lock, but its last update time is older than the specified time
BambooClusterLockService.tryAcquireReadLock(String, long)
.Important: this is a NON-REENTRANT implementation, i.e., if the node tries to acquire a lock that it already owns, then it will not be granted. Rationale: reentrant implementation requires mechanism to track re-entrancies in order to prevent releasing the lock before all operations are completed. It can be considered in the future.
There is no limit for the number of nodes that can acquire READ lock simultaneously, but only one can acquire the correspondent WRITE (see
BambooClusterLockService.lockForWriting(String, long)
.Make sure that
BambooClusterLockService.unlockForReading(String)
is called in a try...finally block.*
- Specified by:
lockForReading
in interfaceBambooClusterLockService
- Parameters:
lockName
- the globally unique id of the locklockTimeoutSeconds
- time (in seconds) after which an existing WRITE lock doesn't block the READ ones anymore
-
lockForReading
@VisibleForTesting public void lockForReading(@Nonnull String lockName, @NotNull @NotNull String nodeId, long lockTimeoutSeconds)
-
unlockForReading
public void unlockForReading(@Nonnull String lockName)
Description copied from interface:BambooClusterLockService
Unlocks the READING part of the lock identified by the given name.- Specified by:
unlockForReading
in interfaceBambooClusterLockService
- Parameters:
lockName
- the globally unique id of the lock
-
unlockForReading
@VisibleForTesting public void unlockForReading(@Nonnull String lockName, @NotNull @NotNull String nodeId)
-
tryAcquireReadLock
public boolean tryAcquireReadLock(@NotNull @NotNull String lockName, long lockTimeoutSeconds)
Description copied from interface:BambooClusterLockService
Attempts to acquire READ ownership of a named lock by current cluster node. A READ lock should be treated as eligible to be acquired when one of the following is true:- It has no node set as an owner of correspondent WRITE lock
- It has an node set as an owner of correspondent WRITE lock, but its last update time is older than the specified time
There is no limit for the number of nodes that can acquire READ lock simultaneously, but only one can acquire the correspondent WRITE (see
BambooClusterLockService.tryAcquireWriteLock(String,long)
.Make sure that
BambooClusterLockService.releaseReadLock(String)
is called properly.There is no central entity managing a waiting list and granting permissions based on some criterion. Therefore, clients will be granted or denied immediately and it's up to them to try again.
- Specified by:
tryAcquireReadLock
in interfaceBambooClusterLockService
- Parameters:
lockName
- the globally unique id of the locklockTimeoutSeconds
- time (in seconds) after which an existing WRITE lock doesn't block the READ ones- Returns:
- true if we acquired the READ lock, false otherwise.
-
releaseReadLock
public void releaseReadLock(@NotNull @NotNull String lockName)
Description copied from interface:BambooClusterLockService
Release READ ownership of a named lock by current cluster node.- Specified by:
releaseReadLock
in interfaceBambooClusterLockService
- Parameters:
lockName
- the globally unique id of the lock
-
tryAcquireWriteLock
public boolean tryAcquireWriteLock(@NotNull @NotNull String lockName, long lockTimeoutSeconds)
Description copied from interface:BambooClusterLockService
AttemptS to acquire WRITE ownership of a named lock by current cluster node. A WRITE lock should be treated as eligible to be acquired when one of the following is true:- It has no node set as an owner of correspondent WRITE lock
- It has no node set as an owner of correspondent READ locks
- It has nodes set as owners of correspondent READ/WRITE locks, but their last update times are older than the specified time
There is no limit for the number of nodes that can acquire READ lock simultaneously, but only one can acquire the correspondent WRITE.
Make sure that
BambooClusterLockService.releaseReadLock(String)
is called properly.There is no central entity managing a waiting list and granting permissions based on some criterion. Therefore, clients will be granted or denied immediately and it's up to them to try again.
- Specified by:
tryAcquireWriteLock
in interfaceBambooClusterLockService
- Parameters:
lockName
- the globally unique id of the locklockTimeoutSeconds
- time (in seconds) after which the WRITE lock can be taken over- Returns:
- true if we acquired the WRITE lock, false otherwise.
-
releaseWriteLock
public void releaseWriteLock(@NotNull @NotNull String lockName)
Description copied from interface:BambooClusterLockService
Release READ ownership of a named lock by current cluster node.- Specified by:
releaseWriteLock
in interfaceBambooClusterLockService
- Parameters:
lockName
- the globally unique id of the lock
-
shutdown
@PreDestroy public void shutdown()
Description copied from interface:BambooClusterLockService
Shuts the service down, stopping all accessory jobs (e..g: lock renewer) for all locks.- Specified by:
shutdown
in interfaceBambooClusterLockService
- Overrides:
shutdown
in classcom.atlassian.beehive.db.DatabaseClusterLockService
-
getReadLockName
@NotNull @VisibleForTesting public static @NotNull String getReadLockName(@NotNull @NotNull String lockName)
-
getReadLockNameForNode
@NotNull @VisibleForTesting public static @NotNull String getReadLockNameForNode(@NotNull @NotNull String lockName, @NotNull @NotNull String nodeId)
-
-