Class DistributedLock
- java.lang.Object
-
- fr.ens.biologie.genomique.eoulsan.util.locker.DistributedLock
-
@ThreadSafe public class DistributedLock extends Object
Distributed locking via ZooKeeper. Assuming there are N clients that all try to acquire a lock, the algorithm works as follows. Each host creates an ephemeral|sequential node, and requests a list of children for the lock node. Due to the nature of sequential, all the ids are increasing in order, therefore the client with the least ID according to natural ordering will hold the lock. Every other client watches the id immediately preceding its own id and checks for the lock in case of notification. The client holding the lock does the work and finally deletes the node, thereby triggering the next client in line to acquire the lock. Deadlocks are possible but avoided in most cases because if a client drops dead while holding the lock, the ZK session should timeout and since the node is ephemeral, it will be removed in such a case. Deadlocks could occur if the the worker thread on a client hangs but the zk-client thread is still alive. There could be an external monitor client that ensures that alerts are triggered if the least-id ephemeral node is present past a time-out. Note: Locking attempts will fail in case session expires!- Author:
- Florian Leibert
-
-
Field Summary
Fields Modifier and Type Field Description static int
ANY_VERSION
The magic version number that allows any mutation to always succeed regardless of actual version number.
-
Constructor Summary
Constructors Constructor Description DistributedLock(org.apache.zookeeper.ZooKeeper zkClient, String lockPath)
DistributedLock(org.apache.zookeeper.ZooKeeper zkClient, String lockPath, Iterable<org.apache.zookeeper.data.ACL> acl)
Creates a distributed lock using the givenzkClient
to coordinate locking.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
lock()
boolean
tryLock(long timeout, TimeUnit unit)
void
unlock()
-
-
-
Field Detail
-
ANY_VERSION
public static final int ANY_VERSION
The magic version number that allows any mutation to always succeed regardless of actual version number.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DistributedLock
public DistributedLock(org.apache.zookeeper.ZooKeeper zkClient, String lockPath)
-
DistributedLock
public DistributedLock(org.apache.zookeeper.ZooKeeper zkClient, String lockPath, Iterable<org.apache.zookeeper.data.ACL> acl)
Creates a distributed lock using the givenzkClient
to coordinate locking.- Parameters:
zkClient
- The ZooKeeper client to use.lockPath
- The path used to manage the lock under.acl
- The acl to apply to newly created lock nodes.
-
-
Method Detail
-
lock
public void lock() throws IOException
- Throws:
IOException
-
tryLock
public boolean tryLock(long timeout, TimeUnit unit) throws EoulsanException
- Throws:
EoulsanException
-
unlock
public void unlock() throws IOException
- Throws:
IOException
-
-