Class NonReentrantMutex
- All Implemented Interfaces:
InterProcessTxLock
- Since:
- 4.3
- Author:
- Scott Fines
-
Constructor Summary
ConstructorsConstructorDescriptionNonReentrantMutex(org.apache.curator.framework.CuratorFramework client, String path) NonReentrantMutex(org.apache.curator.framework.CuratorFramework client, String path, org.apache.curator.framework.recipes.locks.LockInternalsDriver driver) -
Method Summary
Modifier and TypeMethodDescriptionvoidAcquire the mutex - blocking until it's available.booleanAcquire the mutex - blocks until it's available or the given time expires.protected byte[]Return a sorted list of all current nodes participating in the lockvoidDiscard any local record of held locks, without touching ZooKeeper.booleanisAcquired(UUID theTxId) booleanWhether this process holds the lock for any transaction, by its own records.voidRelease the lock held for a transaction.voidRelease all locks for all transactions that we've created.booleanreleaseIfHeld(UUID theTxId) Atomic, unlike the default implementation
-
Constructor Details
-
NonReentrantMutex
- Parameters:
client- clientpath- the path to lock
-
NonReentrantMutex
public NonReentrantMutex(org.apache.curator.framework.CuratorFramework client, String path, org.apache.curator.framework.recipes.locks.LockInternalsDriver driver) - Parameters:
client- clientpath- the path to lockdriver- lock driver
-
-
Method Details
-
acquire
Acquire the mutex - blocking until it's available. Each call to acquire must be balanced by a call torelease(UUID)- Specified by:
acquirein interfaceInterProcessTxLock- Parameters:
theTxId-- Throws:
Exception- ZK errors, connection interruptions
-
acquire
Acquire the mutex - blocks until it's available or the given time expires. Each call to acquire that returns true must be balanced by a call torelease(UUID)- Specified by:
acquirein interfaceInterProcessTxLock- Parameters:
theTxId-theTime- time to waittheTimeUnit- time unit- Returns:
- true if the mutex was acquired, false if not
- Throws:
Exception- ZK errors, connection interruptions
-
isAcquired
- Specified by:
isAcquiredin interfaceInterProcessTxLock- Parameters:
theTxId- the transaction id to check- Returns:
- true if there is a lock for the specified transaction id.
-
isHeldInProcess
public boolean isHeldInProcess()Description copied from interface:InterProcessTxLockWhether this process holds the lock for any transaction, by its own records. The default reports false; implementations that record locks must override it.- Specified by:
isHeldInProcessin interfaceInterProcessTxLock
-
releaseAll
Release all locks for all transactions that we've created.Every lock is attempted even if some fail -- leaving the rest held is worse than a partial failure. The first failure is thrown at the end, the others suppressed.
- Specified by:
releaseAllin interfaceInterProcessTxLock- Throws:
Exception
-
invalidateAll
public void invalidateAll()Discard any local record of held locks, without touching ZooKeeper.Called when this node's ZooKeeper session is lost. Lock nodes are ephemeral, so the session ending is itself the release -- unlike
InterProcessTxLock.releaseAll(), there is nothing left to delete.The default does nothing. Implementations that cache lock state must override it.
Without this,
isAcquired(java.util.UUID)keeps answering true for locks ZooKeeper released with the session, and a joining node can take the exclusive lease while a transaction believes it is protected.Discards lock state created by a previous Curator session. LOST is queued and delivered on Curator's dispatch thread, so the connection may be already repaired by the time we see it. Comparing indices errs in one direction only: a lock granted by the live connection always carries the current index and is kept, while one recorded against an older connection is dropped.
Best-effort: Curator drops queued state events when its 25-slot queue overflows, so a LOST may never reach us at all.
- Specified by:
invalidateAllin interfaceInterProcessTxLock
-
release
Release the lock held for a transaction.- Specified by:
releasein interfaceInterProcessTxLock- Parameters:
theTxId-- Throws:
IllegalMonitorStateException- if we hold no lock for this transactionException
-
releaseIfHeld
Atomic, unlike the default implementation- Specified by:
releaseIfHeldin interfaceInterProcessTxLock- Returns:
- true if the lock was held and is now released
- Throws:
Exception
-
getParticipantNodes
Return a sorted list of all current nodes participating in the lock- Returns:
- list of nodes
- Throws:
IOException- ZK errors, interruptions, etc.
-
getLockNodeBytes
protected byte[] getLockNodeBytes()
-