com.twitter.common.zookeeper
Class DistributedLockImpl

java.lang.Object
  extended by com.twitter.common.zookeeper.DistributedLockImpl
All Implemented Interfaces:
DistributedLock

@ThreadSafe
public class DistributedLockImpl
extends Object
implements 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!


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.twitter.common.zookeeper.DistributedLock
DistributedLock.LockingException
 
Constructor Summary
DistributedLockImpl(ZooKeeperClient zkClient, String lockPath)
          Equivalent to DistributedLockImpl(ZooKeeperClient, String, Iterable) with a default wide open acl (ZooDefs.Ids#OPEN_ACL_UNSAFE).
DistributedLockImpl(ZooKeeperClient zkClient, String lockPath, Iterable<org.apache.zookeeper.data.ACL> acl)
          Creates a distributed lock using the given zkClient to coordinate locking.
 
Method Summary
 void lock()
           
 boolean tryLock(long timeout, TimeUnit unit)
           
 void unlock()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DistributedLockImpl

public DistributedLockImpl(ZooKeeperClient zkClient,
                           String lockPath)
Equivalent to DistributedLockImpl(ZooKeeperClient, String, Iterable) with a default wide open acl (ZooDefs.Ids#OPEN_ACL_UNSAFE).


DistributedLockImpl

public DistributedLockImpl(ZooKeeperClient 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 Detail

lock

public void lock()
          throws DistributedLock.LockingException
Specified by:
lock in interface DistributedLock
Throws:
DistributedLock.LockingException

tryLock

public boolean tryLock(long timeout,
                       TimeUnit unit)
Specified by:
tryLock in interface DistributedLock

unlock

public void unlock()
            throws DistributedLock.LockingException
Specified by:
unlock in interface DistributedLock
Throws:
DistributedLock.LockingException