org.apache.hadoop.hbase.zookeeper.lock
Class ZKInterProcessLockBase

java.lang.Object
  extended by org.apache.hadoop.hbase.zookeeper.lock.ZKInterProcessLockBase
All Implemented Interfaces:
InterProcessLock
Direct Known Subclasses:
ZKInterProcessReadLock, ZKInterProcessWriteLock

@InterfaceAudience.Private
public abstract class ZKInterProcessLockBase
extends Object
implements InterProcessLock

ZooKeeper based HLock implementation. Based on the Shared Locks recipe. (see: ZooKeeper Recipes and Solutions )


Nested Class Summary
protected static class ZKInterProcessLockBase.AcquiredLock
          Represents information about a lock held by this thread.
protected static class ZKInterProcessLockBase.ZNodeComparator
           
 
Nested classes/interfaces inherited from interface org.apache.hadoop.hbase.InterProcessLock
InterProcessLock.MetadataHandler
 
Field Summary
protected  AtomicReference<ZKInterProcessLockBase.AcquiredLock> acquiredLock
           
protected  String childZNode
           
protected  String fullyQualifiedZNode
           
protected  InterProcessLock.MetadataHandler handler
           
protected  byte[] metadata
           
protected  String parentLockNode
           
protected static String READ_LOCK_CHILD_NODE_PREFIX
          ZNode prefix used by processes acquiring reader locks
protected static String WRITE_LOCK_CHILD_NODE_PREFIX
          ZNode prefix used by processes acquiring writer locks
protected  ZooKeeperWatcher zkWatcher
           
 
Constructor Summary
protected ZKInterProcessLockBase(ZooKeeperWatcher zkWatcher, String parentLockNode, byte[] metadata, InterProcessLock.MetadataHandler handler, String childNode)
          Called by implementing classes.
 
Method Summary
 void acquire()
          Acquire the lock, waiting indefinitely until the lock is released or the thread is interrupted.
protected abstract  String getLockPath(String myZNode, List<String> children)
          Determine based on a list of children under a ZNode, whether or not a process which created a specified ZNode has obtained a lock.
protected  boolean handleLockMetadata(String lockZNode)
          Process metadata stored in a ZNode using a callback
protected  boolean handleLockMetadata(String lockZNode, InterProcessLock.MetadataHandler handler)
          Process metadata stored in a ZNode using a callback object passed to this instance.
protected  boolean isChildOfSameType(String child)
          Check if a child znode represents the same type(read or write) of lock
protected static boolean isChildReadLock(String child)
          Check if a child znode represents a read lock.
protected static boolean isChildWriteLock(String child)
          Check if a child znode represents a write lock.
 void reapAllLocks()
          If supported, attempts to reap all the locks of this type by forcefully deleting the locks (both held and attempted).
 void reapExpiredLocks(long timeout)
          Will delete all lock znodes of this type (either read or write) which are "expired" according to timeout.
 void release()
          Release the lock.
 boolean tryAcquire(long timeoutMs)
          Acquire the lock within a wait time.
protected  void updateAcquiredLock(String createdZNode)
          Update state as to indicate that a lock is held
 void visitLocks(InterProcessLock.MetadataHandler handler)
          Visits the locks (both held and attempted) with the given MetadataHandler.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

READ_LOCK_CHILD_NODE_PREFIX

protected static final String READ_LOCK_CHILD_NODE_PREFIX
ZNode prefix used by processes acquiring reader locks

See Also:
Constant Field Values

WRITE_LOCK_CHILD_NODE_PREFIX

protected static final String WRITE_LOCK_CHILD_NODE_PREFIX
ZNode prefix used by processes acquiring writer locks

See Also:
Constant Field Values

zkWatcher

protected final ZooKeeperWatcher zkWatcher

parentLockNode

protected final String parentLockNode

fullyQualifiedZNode

protected final String fullyQualifiedZNode

childZNode

protected final String childZNode

metadata

protected final byte[] metadata

handler

protected final InterProcessLock.MetadataHandler handler

acquiredLock

protected final AtomicReference<ZKInterProcessLockBase.AcquiredLock> acquiredLock
Constructor Detail

ZKInterProcessLockBase

protected ZKInterProcessLockBase(ZooKeeperWatcher zkWatcher,
                                 String parentLockNode,
                                 byte[] metadata,
                                 InterProcessLock.MetadataHandler handler,
                                 String childNode)
Called by implementing classes.

Parameters:
zkWatcher -
parentLockNode - The lock ZNode path
metadata -
handler -
childNode - The prefix for child nodes created under the parent
Method Detail

acquire

public void acquire()
             throws IOException,
                    InterruptedException
Acquire the lock, waiting indefinitely until the lock is released or the thread is interrupted.

Specified by:
acquire in interface InterProcessLock
Throws:
IOException - If there is an unrecoverable error releasing the lock
InterruptedException - If current thread is interrupted while waiting for the lock

tryAcquire

public boolean tryAcquire(long timeoutMs)
                   throws IOException,
                          InterruptedException
Acquire the lock within a wait time.

Specified by:
tryAcquire in interface InterProcessLock
Parameters:
timeoutMs - The maximum time (in milliseconds) to wait for the lock, -1 to wait indefinitely
Returns:
True if the lock was acquired, false if waiting time elapsed before the lock was acquired
Throws:
IOException - If there is an unrecoverable error talking talking (e.g., when talking to a lock service) when acquiring the lock
InterruptedException - If the thread is interrupted while waiting to acquire the lock

isChildReadLock

protected static boolean isChildReadLock(String child)
Check if a child znode represents a read lock.

Parameters:
child - The child znode we want to check.
Returns:
whether the child znode represents a read lock

isChildWriteLock

protected static boolean isChildWriteLock(String child)
Check if a child znode represents a write lock.

Parameters:
child - The child znode we want to check.
Returns:
whether the child znode represents a write lock

isChildOfSameType

protected boolean isChildOfSameType(String child)
Check if a child znode represents the same type(read or write) of lock

Parameters:
child - The child znode we want to check.
Returns:
whether the child znode represents the same type(read or write) of lock

updateAcquiredLock

protected void updateAcquiredLock(String createdZNode)
                           throws IOException
Update state as to indicate that a lock is held

Parameters:
createdZNode - The lock znode
Throws:
IOException - If an unrecoverable ZooKeeper error occurs

release

public void release()
             throws IOException,
                    InterruptedException
Release the lock.

Specified by:
release in interface InterProcessLock
Throws:
IOException - If there is an unrecoverable error releasing the lock
InterruptedException - If the thread is interrupted while releasing the lock

handleLockMetadata

protected boolean handleLockMetadata(String lockZNode)
Process metadata stored in a ZNode using a callback

Parameters:
lockZNode - The node holding the metadata
Returns:
True if metadata was ready and processed, false otherwise.

handleLockMetadata

protected boolean handleLockMetadata(String lockZNode,
                                     InterProcessLock.MetadataHandler handler)
Process metadata stored in a ZNode using a callback object passed to this instance.

Parameters:
lockZNode - The node holding the metadata
handler - the metadata handler
Returns:
True if metadata was ready and processed, false on exception.

reapAllLocks

public void reapAllLocks()
                  throws IOException
Description copied from interface: InterProcessLock
If supported, attempts to reap all the locks of this type by forcefully deleting the locks (both held and attempted). Lock reaping is different than coordinated lock revocation in that, there is no coordination, and the behavior is undefined if the lock holder is still alive. Calling this should have the same affect as calling InterProcessLock.reapExpiredLocks(long) with timeout=0.

Specified by:
reapAllLocks in interface InterProcessLock
Throws:
IOException - If there is an unrecoverable error reaping the locks

reapExpiredLocks

public void reapExpiredLocks(long timeout)
                      throws IOException
Will delete all lock znodes of this type (either read or write) which are "expired" according to timeout. Assumption is that the clock skew between zookeeper and this servers is negligible. Referred in zk recipe as "Revocable Shared Locks with Freaking Laser Beams". (http://zookeeper.apache.org/doc/trunk/recipes.html).

Specified by:
reapExpiredLocks in interface InterProcessLock
Throws:
IOException - If there is an unrecoverable error reaping the locks

visitLocks

public void visitLocks(InterProcessLock.MetadataHandler handler)
                throws IOException
Visits the locks (both held and attempted) with the given MetadataHandler.

Specified by:
visitLocks in interface InterProcessLock
Throws:
InterruptedException - If there is an unrecoverable error
IOException

getLockPath

protected abstract String getLockPath(String myZNode,
                                      List<String> children)
                               throws IOException
Determine based on a list of children under a ZNode, whether or not a process which created a specified ZNode has obtained a lock. If a lock is not obtained, return the path that we should watch awaiting its deletion. Otherwise, return null. This method is abstract as the logic for determining whether or not a lock is obtained depends on the type of lock being implemented.

Parameters:
myZNode - The ZNode created by the process attempting to acquire a lock
children - List of all child ZNodes under the lock's parent ZNode
Returns:
The path to watch, or null if myZNode can represent a correctly acquired lock.
Throws:
IOException


Copyright © 2015 The Apache Software Foundation. All rights reserved.