@InterfaceAudience.Private public abstract class ZKInterProcessLockBase extends Object implements InterProcessLock
Modifier and Type | Class and Description |
---|---|
protected static class |
ZKInterProcessLockBase.AcquiredLock
Represents information about a lock held by this thread.
|
protected static class |
ZKInterProcessLockBase.ZNodeComparator |
InterProcessLock.MetadataHandler
Modifier and Type | Field and Description |
---|---|
protected AtomicReference<ZKInterProcessLockBase.AcquiredLock> |
acquiredLock |
protected String |
childZNode |
protected String |
fullyQualifiedZNode |
protected InterProcessLock.MetadataHandler |
handler |
private static org.apache.commons.logging.Log |
LOG |
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 |
Modifier | Constructor and Description |
---|---|
protected |
ZKInterProcessLockBase(ZooKeeperWatcher zkWatcher,
String parentLockNode,
byte[] metadata,
InterProcessLock.MetadataHandler handler,
String childNode)
Called by implementing classes.
|
Modifier and Type | Method and Description |
---|---|
void |
acquire()
Acquire the lock, waiting indefinitely until the lock is released or
the thread is interrupted.
|
private String |
createLockZNode() |
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.
|
private static final org.apache.commons.logging.Log LOG
protected static final String READ_LOCK_CHILD_NODE_PREFIX
protected static final String WRITE_LOCK_CHILD_NODE_PREFIX
protected final ZooKeeperWatcher zkWatcher
protected final String parentLockNode
protected final String fullyQualifiedZNode
protected final String childZNode
protected final byte[] metadata
protected final InterProcessLock.MetadataHandler handler
protected final AtomicReference<ZKInterProcessLockBase.AcquiredLock> acquiredLock
protected ZKInterProcessLockBase(ZooKeeperWatcher zkWatcher, String parentLockNode, byte[] metadata, InterProcessLock.MetadataHandler handler, String childNode)
zkWatcher
- parentLockNode
- The lock ZNode pathmetadata
- handler
- childNode
- The prefix for child nodes created under the parentpublic void acquire() throws IOException, InterruptedException
acquire
in interface InterProcessLock
IOException
- If there is an unrecoverable error releasing the lockInterruptedException
- If current thread is interrupted while
waiting for the lockpublic boolean tryAcquire(long timeoutMs) throws IOException, InterruptedException
tryAcquire
in interface InterProcessLock
timeoutMs
- The maximum time (in milliseconds) to wait for the lock,
-1 to wait indefinitelyIOException
- If there is an unrecoverable error talking talking
(e.g., when talking to a lock service) when acquiring
the lockInterruptedException
- If the thread is interrupted while waiting to
acquire the lockprivate String createLockZNode() throws org.apache.zookeeper.KeeperException
org.apache.zookeeper.KeeperException
protected static boolean isChildReadLock(String child)
child
- The child znode we want to check.protected static boolean isChildWriteLock(String child)
child
- The child znode we want to check.protected boolean isChildOfSameType(String child)
child
- The child znode we want to check.protected void updateAcquiredLock(String createdZNode) throws IOException
createdZNode
- The lock znodeIOException
- If an unrecoverable ZooKeeper error occurspublic void release() throws IOException, InterruptedException
release
in interface InterProcessLock
IOException
- If there is an unrecoverable error releasing the lockInterruptedException
- If the thread is interrupted while releasing
the lockprotected boolean handleLockMetadata(String lockZNode)
lockZNode
- The node holding the metadataprotected boolean handleLockMetadata(String lockZNode, InterProcessLock.MetadataHandler handler)
lockZNode
- The node holding the metadatahandler
- the metadata handlerpublic void reapAllLocks() throws IOException
InterProcessLock
InterProcessLock.reapExpiredLocks(long)
with timeout=0.reapAllLocks
in interface InterProcessLock
IOException
- If there is an unrecoverable error reaping the lockspublic void reapExpiredLocks(long timeout) throws IOException
reapExpiredLocks
in interface InterProcessLock
IOException
- If there is an unrecoverable error reaping the lockspublic void visitLocks(InterProcessLock.MetadataHandler handler) throws IOException
visitLocks
in interface InterProcessLock
IOException
- If there is an unrecoverable errorprotected abstract String getLockPath(String myZNode, List<String> children) throws IOException
myZNode
- The ZNode created by the process attempting to acquire
a lockchildren
- List of all child ZNodes under the lock's parent ZNodeIOException
Copyright © 2007–2019 The Apache Software Foundation. All rights reserved.