Class ServiceLock
- java.lang.Object
-
- org.apache.accumulo.core.fate.zookeeper.ServiceLock
-
- All Implemented Interfaces:
org.apache.zookeeper.Watcher
public class ServiceLock extends Object implements org.apache.zookeeper.Watcher
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ServiceLock.AccumuloLockWatcher
static class
ServiceLock.LockLossReason
static interface
ServiceLock.LockWatcher
static class
ServiceLock.ServiceLockPath
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.zookeeper.ZooKeeper
zooKeeper
-
Constructor Summary
Constructors Constructor Description ServiceLock(org.apache.zookeeper.ZooKeeper zookeeper, ServiceLock.ServiceLockPath path, UUID uuid)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
deleteLock(ZooReaderWriter zk, ServiceLock.ServiceLockPath path)
static boolean
deleteLock(ZooReaderWriter zk, ServiceLock.ServiceLockPath path, String lockData)
static String
findLowestPrevPrefix(List<String> children, String ephemeralNode)
Given a pre-sorted set of children ephemeral nodes where the node name is of the form "zlock#UUID#sequenceNumber", find the ephemeral node that sorts before the ephemeralNode parameter with the lowest sequence numberstatic byte[]
getLockData(ZooCache zc, ServiceLock.ServiceLockPath path, ZooCache.ZcStat stat)
static byte[]
getLockData(org.apache.zookeeper.ZooKeeper zk, ServiceLock.ServiceLockPath path)
ZooUtil.LockID
getLockID()
String
getLockName()
String
getLockPath()
long
getSessionId()
static long
getSessionId(ZooCache zc, ServiceLock.ServiceLockPath path)
String
getWatching()
boolean
isLocked()
static boolean
isLockHeld(ZooCache zc, ZooUtil.LockID lid)
void
lock(ServiceLock.AccumuloLockWatcher lw, byte[] data)
static ServiceLock.ServiceLockPath
path(String path)
void
process(org.apache.zookeeper.WatchedEvent event)
void
replaceLockData(byte[] b)
boolean
tryLock(ServiceLock.LockWatcher lw, byte[] data)
boolean
tryToCancelAsyncLockOrUnlock()
void
unlock()
static List<String>
validateAndSort(ServiceLock.ServiceLockPath path, List<String> children)
Sort list of ephemeral nodes by their sequence number.boolean
wasLockAcquired()
indicates if the lock was acquired in the past....
-
-
-
Constructor Detail
-
ServiceLock
public ServiceLock(org.apache.zookeeper.ZooKeeper zookeeper, ServiceLock.ServiceLockPath path, UUID uuid)
-
-
Method Detail
-
path
public static ServiceLock.ServiceLockPath path(String path)
-
tryLock
public boolean tryLock(ServiceLock.LockWatcher lw, byte[] data) throws org.apache.zookeeper.KeeperException, InterruptedException
- Throws:
org.apache.zookeeper.KeeperException
InterruptedException
-
validateAndSort
public static List<String> validateAndSort(ServiceLock.ServiceLockPath path, List<String> children)
Sort list of ephemeral nodes by their sequence number. Any ephemeral nodes that are not of the correct form will sort last.- Parameters:
children
- list of ephemeral nodes- Returns:
- list of ephemeral nodes that have valid formats, sorted by sequence number
-
findLowestPrevPrefix
public static String findLowestPrevPrefix(List<String> children, String ephemeralNode)
Given a pre-sorted set of children ephemeral nodes where the node name is of the form "zlock#UUID#sequenceNumber", find the ephemeral node that sorts before the ephemeralNode parameter with the lowest sequence number- Parameters:
children
- list of sequential ephemera nodes, already sortedephemeralNode
- starting node for the search- Returns:
- next lowest prefix with the lowest sequence number
-
lock
public void lock(ServiceLock.AccumuloLockWatcher lw, byte[] data)
-
tryToCancelAsyncLockOrUnlock
public boolean tryToCancelAsyncLockOrUnlock() throws InterruptedException, org.apache.zookeeper.KeeperException
- Throws:
InterruptedException
org.apache.zookeeper.KeeperException
-
unlock
public void unlock() throws InterruptedException, org.apache.zookeeper.KeeperException
- Throws:
InterruptedException
org.apache.zookeeper.KeeperException
-
getWatching
public String getWatching()
- Returns:
- path of node that this lock is watching
-
getLockPath
public String getLockPath()
-
getLockName
public String getLockName()
-
getLockID
public ZooUtil.LockID getLockID()
-
wasLockAcquired
public boolean wasLockAcquired()
indicates if the lock was acquired in the past.... helps discriminate between the case where the lock was never held, or held and lost....- Returns:
- true if the lock was acquired, otherwise false.
-
isLocked
public boolean isLocked()
-
replaceLockData
public void replaceLockData(byte[] b) throws org.apache.zookeeper.KeeperException, InterruptedException
- Throws:
org.apache.zookeeper.KeeperException
InterruptedException
-
process
public void process(org.apache.zookeeper.WatchedEvent event)
- Specified by:
process
in interfaceorg.apache.zookeeper.Watcher
-
isLockHeld
public static boolean isLockHeld(ZooCache zc, ZooUtil.LockID lid)
-
getLockData
public static byte[] getLockData(org.apache.zookeeper.ZooKeeper zk, ServiceLock.ServiceLockPath path) throws org.apache.zookeeper.KeeperException, InterruptedException
- Throws:
org.apache.zookeeper.KeeperException
InterruptedException
-
getLockData
public static byte[] getLockData(ZooCache zc, ServiceLock.ServiceLockPath path, ZooCache.ZcStat stat)
-
getSessionId
public static long getSessionId(ZooCache zc, ServiceLock.ServiceLockPath path)
-
getSessionId
public long getSessionId() throws org.apache.zookeeper.KeeperException, InterruptedException
- Throws:
org.apache.zookeeper.KeeperException
InterruptedException
-
deleteLock
public static void deleteLock(ZooReaderWriter zk, ServiceLock.ServiceLockPath path) throws InterruptedException, org.apache.zookeeper.KeeperException
- Throws:
InterruptedException
org.apache.zookeeper.KeeperException
-
deleteLock
public static boolean deleteLock(ZooReaderWriter zk, ServiceLock.ServiceLockPath path, String lockData) throws InterruptedException, org.apache.zookeeper.KeeperException
- Throws:
InterruptedException
org.apache.zookeeper.KeeperException
-
-