Class NonReentrantMutex

java.lang.Object
com.complexible.common.curator.framework.locks.NonReentrantMutex
All Implemented Interfaces:
InterProcessTxLock

public class NonReentrantMutex extends Object implements InterProcessTxLock
Since:
4.3
Author:
Scott Fines
  • Constructor Details

    • NonReentrantMutex

      public NonReentrantMutex(org.apache.curator.framework.CuratorFramework client, String path)
      Parameters:
      client - client
      path - 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 - client
      path - the path to lock
      driver - lock driver
  • Method Details

    • acquire

      public void acquire(UUID theTxId) throws Exception
      Acquire the mutex - blocking until it's available. Each call to acquire must be balanced by a call to release(UUID)
      Specified by:
      acquire in interface InterProcessTxLock
      Parameters:
      theTxId -
      Throws:
      Exception - ZK errors, connection interruptions
    • acquire

      public boolean acquire(UUID theTxId, long theTime, TimeUnit theTimeUnit) throws Exception
      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 to release(UUID)
      Specified by:
      acquire in interface InterProcessTxLock
      Parameters:
      theTxId -
      theTime - time to wait
      theTimeUnit - time unit
      Returns:
      true if the mutex was acquired, false if not
      Throws:
      Exception - ZK errors, connection interruptions
    • isAcquired

      public boolean isAcquired(UUID theTxId)
      Specified by:
      isAcquired in interface InterProcessTxLock
      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: InterProcessTxLock
      Whether 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:
      isHeldInProcess in interface InterProcessTxLock
    • releaseAll

      public void releaseAll() throws Exception
      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:
      releaseAll in interface InterProcessTxLock
      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:
      invalidateAll in interface InterProcessTxLock
    • release

      public void release(UUID theTxId) throws Exception
      Release the lock held for a transaction.
      Specified by:
      release in interface InterProcessTxLock
      Parameters:
      theTxId -
      Throws:
      IllegalMonitorStateException - if we hold no lock for this transaction
      Exception
    • releaseIfHeld

      public boolean releaseIfHeld(UUID theTxId) throws Exception
      Atomic, unlike the default implementation
      Specified by:
      releaseIfHeld in interface InterProcessTxLock
      Returns:
      true if the lock was held and is now released
      Throws:
      Exception
    • getParticipantNodes

      public Collection<String> getParticipantNodes() throws IOException
      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()