|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.bigdata.zookeeper.AbstractZNodeConditionWatcher
com.bigdata.zookeeper.ZNodeLockWatcher
ZNodeLockWatcher.ZLockImpl
public class ZNodeLockWatcher
Watcher supporting distributed synchronous lock pattern.
A lock is just a node with CreateMode.EPHEMERAL_SEQUENTIAL children
modeling a queue of processes obeying a contract on the conditions under
which a process may proceed. The process whose child is ordered first in the
queue holds the lock. To release the lock the process simply deletes its node
in the queue. Each process seeking a lock creates a new child that watches
the previous child in the ordered queue. When it receives a
WatchedEvent (NodeDeleted) for that child, it has reached the head of
the queue.
Any processes wishing to contend for the lock must ensure the creation of the
lock node. This is a CreateMode.PERSISTENT znode. The SEQUENTIAL flag
COULD be used if processes have another means to communicate the identity of
the lock node. Lacking such a mechanism, the lock node must have a zpath
which can be computed by any process seeking to contend for the lock. That is
normally accomplished by naming the lock node for a resource with a well
known identity.
This class only watches the previous child in the queue (if any). Zookeeper does not permit you to delete the parent without deleting the children, so you will always the queue purged of children before the lock node itself is deleted. If the child deleted from the queue, the next child in the queue will be granted the lock.
Destroying the lock node will not cause the lock to be persistently destroyed if the conditions still exist under which processes will contend for that lock as some process will simply re-create the lock node and the queue will become re-populated over time.
While a queue may seem excessive for one time operations, it provides failover if the process holding the lock dies.
getLock(ZooKeeper, String, List),
ZNodeLockWatcher.ZLockImpl| Nested Class Summary | |
|---|---|
protected static class |
ZNodeLockWatcher.ZLockImpl
Deprecated. ZLock implementation class. |
| Nested classes/interfaces inherited from interface org.apache.zookeeper.Watcher |
|---|
org.apache.zookeeper.Watcher.Event |
| Field Summary | |
|---|---|
protected static boolean |
DEBUG
Deprecated. |
protected static boolean |
INFO
Deprecated. |
static String |
INVALID
Deprecated. The suffix for a marker that is a sibling of the lock node. |
protected static org.apache.log4j.Logger |
log
Deprecated. |
| Fields inherited from class com.bigdata.zookeeper.AbstractZNodeConditionWatcher |
|---|
zookeeper, zpath |
| Constructor Summary | |
|---|---|
protected |
ZNodeLockWatcher(org.apache.zookeeper.ZooKeeper zookeeper,
String zpath,
String zchild)
Deprecated. |
| Method Summary | |
|---|---|
protected void |
clearWatch()
Deprecated. Clear any watches. |
static ZNodeLockWatcher.ZLockImpl |
getLock(org.apache.zookeeper.ZooKeeper zookeeper,
String zpath,
List<org.apache.zookeeper.data.ACL> acl)
Deprecated. Replaced by ZLockImpl |
protected boolean |
isCancelled()
Deprecated. This implementation always returns false but may be
overriden to permit cancellation of
AbstractZNodeConditionWatcher.awaitCondition(long, TimeUnit). |
protected boolean |
isConditionSatisified()
Deprecated. Obtains the children of the lock node. |
protected boolean |
isConditionSatisified(org.apache.zookeeper.WatchedEvent event)
Deprecated. Implementation must inspect the event and determine if the conditions are satisified. |
protected void |
toString(StringBuilder sb)
Deprecated. Subclasses may extend this method to add additional state into the representation generated by AbstractZNodeConditionWatcher.toString(). |
| Methods inherited from class com.bigdata.zookeeper.AbstractZNodeConditionWatcher |
|---|
_resumeWatch, awaitCondition, awaitCondition, process, success, toString |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected static final org.apache.log4j.Logger log
protected static final boolean INFO
protected static final boolean DEBUG
public static final transient String INVALID
isConditionSatisified() logic will not allow a
lock to be granted if this marker is found. Likewise, new children are
not permitted into the queue when this marker is present.
Note: If you are monitoring a znode whose children a lock nodes, then a
child ending with this string IS NOT a lock node!
| Constructor Detail |
|---|
protected ZNodeLockWatcher(org.apache.zookeeper.ZooKeeper zookeeper,
String zpath,
String zchild)
| Method Detail |
|---|
protected boolean isCancelled()
AbstractZNodeConditionWatcherfalse but may be
overriden to permit cancellation of
AbstractZNodeConditionWatcher.awaitCondition(long, TimeUnit).
isCancelled in class AbstractZNodeConditionWatcherprotected void toString(StringBuilder sb)
AbstractZNodeConditionWatcherAbstractZNodeConditionWatcher.toString().
Note: This implementation MUST be safe (no exceptions) and non-blocking.
toString in class AbstractZNodeConditionWatcher
protected void clearWatch()
throws org.apache.zookeeper.KeeperException,
InterruptedException
AbstractZNodeConditionWatcher
clearWatch in class AbstractZNodeConditionWatcherorg.apache.zookeeper.KeeperException
InterruptedException
protected boolean isConditionSatisified(org.apache.zookeeper.WatchedEvent event)
throws org.apache.zookeeper.KeeperException,
InterruptedException
AbstractZNodeConditionWatcher
isConditionSatisified in class AbstractZNodeConditionWatcherevent - The WatchedEvent
true if the event satisified the condition.
org.apache.zookeeper.KeeperException
InterruptedException
protected boolean isConditionSatisified()
throws org.apache.zookeeper.KeeperException,
InterruptedException
true. Otherwise, set
a watch on the prior child in the queue.
If the lock node has been deleted then set the cancelled flag
(the entire queue was destroyed, so we cancel any process contending for
a lock).
If the zchild has been deleted then set the cancelled flag (this
let's you break the lock by deleting the child).
If the client is not connected, then an exception will be thrown and the
watch will resume with the next WatchedEvent.
isConditionSatisified in class AbstractZNodeConditionWatchertrue iff the process holds the lock
org.apache.zookeeper.KeeperException
InterruptedException
public static ZNodeLockWatcher.ZLockImpl getLock(org.apache.zookeeper.ZooKeeper zookeeper,
String zpath,
List<org.apache.zookeeper.data.ACL> acl)
throws org.apache.zookeeper.KeeperException,
InterruptedException
ZLockImpl
This method will create a new lock node if the znode identified by the zpath does not exist. However, it will reject the request if the lock node has been invalidated pending the destruction of the lock node and its queue. In design patterns where the lock node may be destroyed, this places the responsibility on the caller to verify that the lock node is pre-existing if they wish to avoid re-creating a lock node which has been destroyed.
zookeeper - zpath - The path identifying the lock node.acl -
org.apache.zookeeper.KeeperException
InterruptedException
ZLockNodeInvalidatedException - if the lock node has been invalidated but not yet destroyed.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||