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 final int
    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)
    Constructor.
    DistributedLock(org.apache.zookeeper.ZooKeeper zkClient, String lockPath, Iterable<org.apache.zookeeper.data.ACL> acl)
    Creates a distributed lock using the given zkClient to coordinate locking.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Lock.
    boolean
    tryLock(long timeout, TimeUnit unit)
    Try to lock.
    void
    Unlock.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • 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:
  • Constructor Details

    • DistributedLock

      public DistributedLock(org.apache.zookeeper.ZooKeeper zkClient, String lockPath)
      Constructor.
      Parameters:
      zkClient - ZooKeeper client object
      lockPath - 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 given zkClient 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 Details

    • lock

      public void lock() throws IOException
      Lock.
      Throws:
      IOException - if an error occurs
    • tryLock

      public boolean tryLock(long timeout, TimeUnit unit) throws EoulsanException
      Try to lock.
      Parameters:
      timeout - the timeout
      unit - timeout unit
      Returns:
      true if lock is successful
      Throws:
      EoulsanException - if an error occurs while trying to lock
    • unlock

      public void unlock() throws IOException
      Unlock.
      Throws:
      IOException - if an error occurs while unlocking