public class ClusterLock extends Object implements XClusterLock, org.apache.curator.framework.state.ConnectionStateListener
Constructor and Description |
---|
ClusterLock(org.apache.curator.framework.CuratorFramework client,
String lockPath)
Constructs a cluster lock instance.
|
Modifier and Type | Method and Description |
---|---|
void |
acquire()
Acquires the cluster lock.
|
boolean |
acquire(long time,
TimeUnit timeUnit)
Acquire the lock - blocks until it's available or the given time expires.
|
String |
getLockPath()
Get the path used for acquiring the distributed lock.
|
org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex |
getUnderlyingLock() |
boolean |
isAcquiredInThisProcess()
Whether this lock is acquired by a thread in this process.
|
void |
release()
Perform one release of the mutex if the calling thread is the same thread that acquired it.
|
void |
stateChanged(org.apache.curator.framework.CuratorFramework client,
org.apache.curator.framework.state.ConnectionState newState)
State changes will be notified to this method.
|
boolean |
tryAcquire()
Tries to acquire the lock when called.
|
public ClusterLock(org.apache.curator.framework.CuratorFramework client, String lockPath)
client
- CuratorFramework
clientlockPath
- the path on which the lock will be acquiredpublic void acquire() throws Exception
acquire
in interface XClusterLock
Exception
- in connection interruptions and underlying distributed coordination provider's errorspublic boolean tryAcquire() throws Exception
tryAcquire
in interface XClusterLock
Exception
- in zookeeper errors and connection errorspublic boolean acquire(long time, TimeUnit timeUnit) throws Exception
acquire
in interface XClusterLock
time
- time to waittimeUnit
- time unitException
- connection interruptionspublic void release() throws Exception
This method is not synchronized to avoid dead locks. That is, if a thread is waiting to acquire the lock in
acquire()
method, if this method was synchronized, even the owning thread cannot release the lock.
We have an edge case where the distributed lock is being acquired by a thread, another thread can call this method and release the lock. Developers has to be careful on this situation. No thread level lock is being used at the moment as we have requirements to release this lock by other threads.
release
in interface XClusterLock
Exception
- interruptions, current thread does not own the lockUnreliableLockException
- if the connection was lost/session expired
while the lock was acquired and before being releasedpublic boolean isAcquiredInThisProcess()
isAcquiredInThisProcess
in interface XClusterLock
public String getLockPath()
getLockPath
in interface XClusterLock
public org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex getUnderlyingLock()
public void stateChanged(org.apache.curator.framework.CuratorFramework client, org.apache.curator.framework.state.ConnectionState newState)
ConnectionState.LOST
, we cannot guarantee the reliability of the lock unless we
receive a subsequent ConnectionState.RECONNECTED
.stateChanged
in interface org.apache.curator.framework.state.ConnectionStateListener
client
- curator clientnewState
- new connection stateCopyright © 2016–2019 AdroitLogic. All rights reserved.