Class DistributedLock
java.lang.Object
fr.ens.biologie.genomique.eoulsan.util.locker.DistributedLock
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
FieldsModifier and TypeFieldDescriptionstatic final intThe magic version number that allows any mutation to always succeed regardless of actual version number. -
Constructor Summary
ConstructorsConstructorDescriptionDistributedLock(org.apache.zookeeper.ZooKeeper zkClient, String lockPath) Constructor.DistributedLock(org.apache.zookeeper.ZooKeeper zkClient, String lockPath, Iterable<org.apache.zookeeper.data.ACL> acl) Creates a distributed lock using the givenzkClientto coordinate locking. -
Method Summary
-
Field Details
-
ANY_VERSION
public static final int ANY_VERSIONThe magic version number that allows any mutation to always succeed regardless of actual version number.- See Also:
-
-
Constructor Details
-
DistributedLock
Constructor.- Parameters:
zkClient- ZooKeeper client objectlockPath- lock path
-
DistributedLock
public DistributedLock(org.apache.zookeeper.ZooKeeper zkClient, String lockPath, Iterable<org.apache.zookeeper.data.ACL> acl) Creates a distributed lock using the givenzkClientto 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 Details
-
lock
Lock.- Throws:
IOException- if an error occurs
-
tryLock
Try to lock.- Parameters:
timeout- the timeoutunit- timeout unit- Returns:
- true if lock is successful
- Throws:
EoulsanException- if an error occurs while trying to lock
-
unlock
Unlock.- Throws:
IOException- if an error occurs while unlocking
-