com.bigdata.zookeeper
Class ZNodeLockWatcher.ZLockImpl

java.lang.Object
  extended by com.bigdata.zookeeper.ZNodeLockWatcher.ZLockImpl
All Implemented Interfaces:
ZLock
Enclosing class:
ZNodeLockWatcher

protected static class ZNodeLockWatcher.ZLockImpl
extends Object
implements ZLock

ZLock implementation class. The lock is realized as an EPHEMERAL SEQUENTIAL child of the lock node. If the child is in the first position of the lexically sorted children then it holds the lock.

Note: Assuming it has the correct ACL, any thread in any process MAY release the lock, NOT just the one that acquired it. This is done by deleting the ephemeral znode corresponding to the process holding the lock.

Version:
$Id$
Author:
Bryan Thompson
TODO:
The lock is not reentrant (you can not re-acquire it by requesting it while already held in the same or a different thread).

Field Summary
protected  List<org.apache.zookeeper.data.ACL> acl
           
protected  org.apache.zookeeper.ZooKeeper zookeeper
           
protected  String zpath
          The zpath of the lock node (the parent node whose ephemeral sequential children represent the queue of processes contending for the lock).
 
Constructor Summary
protected ZNodeLockWatcher.ZLockImpl(org.apache.zookeeper.ZooKeeper zookeeper, String zpath, List<org.apache.zookeeper.data.ACL> acl)
           
 
Method Summary
 void destroyLock()
          Creates a marker node (a sibling of the lock node) to prevent new children from being added to the queue and then deletes all children in the queue in reverse lexical order so as to not trigger cascades of watchers and finally deletes the lock node itself and then the marker node.
 String getChild()
          The znode (not zpath) of the child and null iff the child does not hold the lock.
 String getZPath()
          The zpath of the lock node.
 boolean isLockHeld()
          Return true if the lock is currently held (this verifies that the zchild for the lock is in fact the leader in the queue of ephemeral znodes contending for the lock).
 void lock()
          Acquire the lock (blocking).
 void lock(long timeout, TimeUnit unit)
          Creates a new lock request (an EPHEMERAL SEQUENTIAL znode that is a child of the lock node) and awaits up to the timeout for the ZLock to be granted.
 String toString()
          Non-blocking representation of lock state.
 void unlock()
          Note: In the case where the lock was stolen by deleting the zchild node this method logs a warning but does not throw an exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

zookeeper

protected final org.apache.zookeeper.ZooKeeper zookeeper

zpath

protected final String zpath
The zpath of the lock node (the parent node whose ephemeral sequential children represent the queue of processes contending for the lock).


acl

protected final List<org.apache.zookeeper.data.ACL> acl
Constructor Detail

ZNodeLockWatcher.ZLockImpl

protected ZNodeLockWatcher.ZLockImpl(org.apache.zookeeper.ZooKeeper zookeeper,
                                     String zpath,
                                     List<org.apache.zookeeper.data.ACL> acl)
Parameters:
zookeeper -
zpath - The zpath of the lock node.
acl -
Method Detail

toString

public String toString()
Non-blocking representation of lock state.

Overrides:
toString in class Object

getZPath

public String getZPath()
The zpath of the lock node.


getChild

public String getChild()
                throws InterruptedException
The znode (not zpath) of the child and null iff the child does not hold the lock.

Throws:
InterruptedException

lock

public void lock()
          throws org.apache.zookeeper.KeeperException,
                 InterruptedException
Description copied from interface: ZLock
Acquire the lock (blocking).

Specified by:
lock in interface ZLock
Throws:
org.apache.zookeeper.KeeperException - if the lock node or the ephemeral child could not be created.
org.apache.zookeeper.KeeperException.SessionExpiredException - If the ZooKeeper client's session was expired. note that this exception is non-recoverable for a given ZooKeeper instance. However, if the application handles the exception according to its own semantics then it MAY obtain a new ZooKeeper instance associated with a new session.
InterruptedException - if the thread was interrupted while awaiting the lock.

isLockHeld

public boolean isLockHeld()
                   throws org.apache.zookeeper.KeeperException,
                          InterruptedException
Description copied from interface: ZLock
Return true if the lock is currently held (this verifies that the zchild for the lock is in fact the leader in the queue of ephemeral znodes contending for the lock).

Specified by:
isLockHeld in interface ZLock
Returns:
true iff the lock is held.
Throws:
org.apache.zookeeper.KeeperException
org.apache.zookeeper.KeeperException.SessionExpiredException - If the ZooKeeper client's session was expired. note that this exception is non-recoverable for a given ZooKeeper instance. However, if the application handles the exception according to its own semantics then it MAY obtain a new ZooKeeper instance associated with a new session.
InterruptedException

lock

public void lock(long timeout,
                 TimeUnit unit)
          throws org.apache.zookeeper.KeeperException,
                 InterruptedException,
                 TimeoutException
Description copied from interface: ZLock
Creates a new lock request (an EPHEMERAL SEQUENTIAL znode that is a child of the lock node) and awaits up to the timeout for the ZLock to be granted.

Specified by:
lock in interface ZLock
Parameters:
timeout - The timeout.
unit - The unit in which that timeout is expressed.
Throws:
org.apache.zookeeper.KeeperException
org.apache.zookeeper.KeeperException.SessionExpiredException - If the ZooKeeper client's session was expired. note that this exception is non-recoverable for a given ZooKeeper instance. However, if the application handles the exception according to its own semantics then it MAY obtain a new ZooKeeper instance associated with a new session.
InterruptedException - if the thread was interrupted while awaiting the lock.
TimeoutException - if the timeout noticeably expired before the lock was granted.

unlock

public void unlock()
            throws org.apache.zookeeper.KeeperException,
                   InterruptedException
Note: In the case where the lock was stolen by deleting the zchild node this method logs a warning but does not throw an exception.

Specified by:
unlock in interface ZLock
Throws:
org.apache.zookeeper.KeeperException
InterruptedException

destroyLock

public void destroyLock()
                 throws org.apache.zookeeper.KeeperException,
                        InterruptedException
Creates a marker node (a sibling of the lock node) to prevent new children from being added to the queue and then deletes all children in the queue in reverse lexical order so as to not trigger cascades of watchers and finally deletes the lock node itself and then the marker node.

Specified by:
destroyLock in interface ZLock
Throws:
org.apache.zookeeper.KeeperException
InterruptedException
TODO:
write unit tests for this., verify unlock() succeeds after destroyLock(), consider allowing even if you do not hold the lock., consider what happens if there is a transient disconnect.


Copyright © 2006-2009 SYSTAP, LLC. All Rights Reserved.