org.jboss.cache.lock
Class MVCCLockManager

java.lang.Object
  extended by org.jboss.cache.lock.AbstractLockManager
      extended by org.jboss.cache.lock.FqnLockManager
          extended by org.jboss.cache.lock.MVCCLockManager
All Implemented Interfaces:
LockManager

public class MVCCLockManager
extends FqnLockManager

This lock manager acquires and releases locks based on the Fqn passed in and not on the node itself. The main benefit here is that locks can be acquired and held even for nonexistent nodes.

Uses lock striping so that a fixed number of locks are maintained for the entire cache, and not a single lock per node.

This implementation only acquires exclusive WRITE locks, and throws an exception if you attempt to use it to acquire a READ lock. JBoss Cache's MVCC design doesn't use read locks at all.

The concept of lock owners is implicit in this implementation and any owners passed in as parameters (where required) will be ignored. See OwnableReentrantLock for details on how ownership is determined.

Since:
3.0
Author:
Manik Surtani (manik AT jboss DOT org)
See Also:
OwnableReentrantLock

Field Summary
 
Fields inherited from class org.jboss.cache.lock.AbstractLockManager
configuration, lockAcquisitionTimeout
 
Constructor Summary
MVCCLockManager()
           
 
Method Summary
 int getConcurrencyLevel()
           
 int getNumberOfLocksAvailable()
           
 int getNumberOfLocksHeld()
           
 Collection<Object> getReadOwners(Fqn f)
          Retrieves the read lock owners, if any, for the current Fqn.
 Object getWriteOwner(Fqn f)
          Retrieves the write lock owner, if any, for the current Fqn.
 void injectDependencies(DataContainer dataContainer, CacheSPI cache, TransactionManager transactionManager, InvocationContextContainer invocationContextContainer)
           
 boolean isLocked(Fqn fqn)
          Returns true if the node is locked (either for reading or writing) by anyone, and false otherwise.
 boolean isLocked(NodeSPI n, LockType lockType)
          Returns true if the node is locked (either for reading or writing) by anyone, and false otherwise.
 boolean lock(Fqn fqn, LockType lockType, Object owner)
          Acquires a lock of type lockType, for a given owner, on a specific Node in the cache, denoted by fqn.
 boolean lock(Fqn fqn, LockType lockType, Object owner, long timeoutMillis)
          Acquires a lock of type lockType, for a given owner, on a specific Node in the cache, denoted by fqn.
 boolean lockAll(NodeSPI node, LockType lockType, Object owner)
          Locks the node and all child nodes, acquiring lock of type specified for the owner specified.
 boolean lockAll(NodeSPI node, LockType lockType, Object owner, long timeout)
          Locks the node and all child nodes, acquiring lock of type specified for the owner specified.
 boolean lockAll(NodeSPI node, LockType lockType, Object owner, long timeout, boolean excludeInternalFqns)
          Locks the node and all child nodes, acquiring lock of type specified for the owner specified.
 boolean lockAllAndRecord(Fqn fqn, LockType lockType, InvocationContext ctx)
          Locks the node and all child nodes, acquiring lock of type specified for the owner specified.
 boolean lockAllAndRecord(NodeSPI node, LockType lockType, InvocationContext ctx)
          Locks the node and all child nodes, acquiring lock of type specified for the owner specified.
 boolean lockAndRecord(Fqn fqn, LockType lockType, InvocationContext ctx)
          Acquires a lock of type lockType, on a specific Node in the cache, denoted by fqn.
 boolean ownsLock(Fqn fqn, LockType lockType, Object owner)
          Tests whether a given owner owns a lock of lockType on a particular Fqn.
 boolean ownsLock(Fqn fqn, Object owner)
          Tests whether a given owner owns any sort of lock on a particular Fqn.
 String printLockInfo()
          Prints lock information for all locks.
 String printLockInfo(NodeSPI node)
          Prints lock information about a node (and its children) to a String.
 void setInternalFqns()
           
 void startLockManager()
           
 void unlock(Fqn fqn, Object owner)
          Releases the lock passed in, held by the specified owner
 void unlock(InvocationContext ctx)
          Releases locks present in an invocation context and transaction entry, if one is available.
 void unlockAll(NodeSPI node)
          Releases locks on a given node and all children for all owners.
 void unlockAll(NodeSPI<?,?> node, Object owner)
          Releases locks on a given node and all children for a given owner.
 
Methods inherited from class org.jboss.cache.lock.FqnLockManager
getReadOwners, getWriteOwner, isLocked, lock, lock, lockAndRecord, ownsLock, unlock
 
Methods inherited from class org.jboss.cache.lock.AbstractLockManager
getLockOwner, injectConfiguration, setLockAcquisitionTimeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MVCCLockManager

public MVCCLockManager()
Method Detail

injectDependencies

public void injectDependencies(DataContainer dataContainer,
                               CacheSPI cache,
                               TransactionManager transactionManager,
                               InvocationContextContainer invocationContextContainer)

startLockManager

public void startLockManager()

setInternalFqns

public void setInternalFqns()

lock

public boolean lock(Fqn fqn,
                    LockType lockType,
                    Object owner)
             throws InterruptedException
Description copied from interface: LockManager
Acquires a lock of type lockType, for a given owner, on a specific Node in the cache, denoted by fqn. This method will try for Configuration.getLockAcquisitionTimeout() milliseconds and give up if it is unable to acquire the required lock.

Parameters:
fqn - Fqn to lock
lockType - type of lock to acquire
owner - owner to acquire the lock for
Returns:
true if the lock was acquired, false otherwise.
Throws:
InterruptedException

lock

public boolean lock(Fqn fqn,
                    LockType lockType,
                    Object owner,
                    long timeoutMillis)
             throws InterruptedException
Description copied from interface: LockManager
Acquires a lock of type lockType, for a given owner, on a specific Node in the cache, denoted by fqn. This method will try for timeout milliseconds and give up if it is unable to acquire the required lock.

Parameters:
fqn - Fqn to lock
lockType - type of lock to acquire
owner - owner to acquire the lock for
timeoutMillis - maximum length of time to wait for (in millis)
Returns:
true if the lock was acquired, false otherwise.
Throws:
InterruptedException

lockAndRecord

public boolean lockAndRecord(Fqn fqn,
                             LockType lockType,
                             InvocationContext ctx)
                      throws InterruptedException
Description copied from interface: LockManager
Acquires a lock of type lockType, on a specific Node in the cache, denoted by fqn. This method will try for a period of time and give up if it is unable to acquire the required lock. The period of time is specified in Option.getLockAcquisitionTimeout() and, if this is unset, the default timeout set in Configuration.getLockAcquisitionTimeout() is used.

In addition, any locks acquired are added to the context OR transaction entry using InvocationContext.addLock(Object).

The owner for the lock is determined by passing the invocation context to LockManager.getLockOwner(org.jboss.cache.InvocationContext).

Parameters:
fqn - Fqn to lock
lockType - type of lock to acquire
ctx - invocation context associated with this invocation
Returns:
true if the lock was acquired, false otherwise.
Throws:
InterruptedException

unlock

public void unlock(Fqn fqn,
                   Object owner)
Description copied from interface: LockManager
Releases the lock passed in, held by the specified owner

Parameters:
fqn - Fqn of the node to unlock
owner - lock owner

unlock

public void unlock(InvocationContext ctx)
Description copied from interface: LockManager
Releases locks present in an invocation context and transaction entry, if one is available.

Locks are released in reverse order of which they are acquired and registered.

Lock owner is determined by passing the invocation context to LockManager.getLockOwner(org.jboss.cache.InvocationContext)

Parameters:
ctx - invocation context to inspect

lockAll

public boolean lockAll(NodeSPI node,
                       LockType lockType,
                       Object owner)
                throws InterruptedException
Description copied from interface: LockManager
Locks the node and all child nodes, acquiring lock of type specified for the owner specified. If only some locks are acquired, all locks are released and the method returns false.

This method will try for Configuration.getLockAcquisitionTimeout() milliseconds and give up if it is unable to acquire the required lock.

Parameters:
node - Node to lock
lockType - type of lock to acquire
owner - owner to acquire the lock for
Returns:
true if the lock was acquired, false otherwise.
Throws:
InterruptedException

lockAll

public boolean lockAll(NodeSPI node,
                       LockType lockType,
                       Object owner,
                       long timeout)
                throws InterruptedException
Description copied from interface: LockManager
Locks the node and all child nodes, acquiring lock of type specified for the owner specified. If only some locks are acquired, all locks are released and the method returns false. Internal Fqns are included as well.

Parameters:
node - Node to lock
lockType - type of lock to acquire
owner - owner to acquire the lock for
timeout - maximum length of time to wait for (in millis)
Returns:
true if all locks were acquired, false otherwise.
Throws:
InterruptedException

lockAll

public boolean lockAll(NodeSPI node,
                       LockType lockType,
                       Object owner,
                       long timeout,
                       boolean excludeInternalFqns)
                throws InterruptedException
Description copied from interface: LockManager
Locks the node and all child nodes, acquiring lock of type specified for the owner specified. If only some locks are acquired, all locks are released and the method returns false.

Parameters:
node - Node to lock
lockType - type of lock to acquire
owner - owner to acquire the lock for
timeout - maximum length of time to wait for (in millis)
excludeInternalFqns - if true, any Fqns that are internal are excluded.
Returns:
true if all locks were acquired, false otherwise.
Throws:
InterruptedException

lockAllAndRecord

public boolean lockAllAndRecord(NodeSPI node,
                                LockType lockType,
                                InvocationContext ctx)
                         throws InterruptedException
Description copied from interface: LockManager
Locks the node and all child nodes, acquiring lock of type specified for the owner specified. If only some locks are acquired, all locks are released and the method returns false.

In addition, any locks acquired are added to the context OR transaction entry using InvocationContext.addLock(Object).

The owner for the lock is determined by passing the invocation context to LockManager.getLockOwner(org.jboss.cache.InvocationContext).

Parameters:
node - Node to lock
lockType - type of lock to acquire
ctx - invocation context associated with this invocation
Returns:
true if all locks were acquired, false otherwise.
Throws:
InterruptedException

lockAllAndRecord

public boolean lockAllAndRecord(Fqn fqn,
                                LockType lockType,
                                InvocationContext ctx)
                         throws InterruptedException
Description copied from interface: LockManager
Locks the node and all child nodes, acquiring lock of type specified for the owner specified. If only some locks are acquired, all locks are released and the method returns false.

In addition, any locks acquired are added to the context OR transaction entry using InvocationContext.addLock(Object).

The owner for the lock is determined by passing the invocation context to LockManager.getLockOwner(org.jboss.cache.InvocationContext).

Parameters:
fqn - Node to lock
lockType - type of lock to acquire
ctx - invocation context associated with this invocation
Returns:
true if all locks were acquired, false otherwise.
Throws:
InterruptedException

unlockAll

public void unlockAll(NodeSPI<?,?> node,
                      Object owner)
Description copied from interface: LockManager
Releases locks on a given node and all children for a given owner.

Parameters:
node - node to unlock
owner - lock owner

unlockAll

public void unlockAll(NodeSPI node)
Description copied from interface: LockManager
Releases locks on a given node and all children for all owners. Use with care.

Parameters:
node - node to unlock

ownsLock

public boolean ownsLock(Fqn fqn,
                        LockType lockType,
                        Object owner)
Description copied from interface: LockManager
Tests whether a given owner owns a lock of lockType on a particular Fqn.

Parameters:
fqn - fqn to test
lockType - type of lock to test for
owner - owner
Returns:
true if the owner does own the specified lock type on the specified node, false otherwise.

ownsLock

public boolean ownsLock(Fqn fqn,
                        Object owner)
Description copied from interface: LockManager
Tests whether a given owner owns any sort of lock on a particular Fqn.

Parameters:
fqn - fqn to test
owner - owner
Returns:
true if the owner does own the specified lock type on the specified node, false otherwise.

isLocked

public boolean isLocked(Fqn fqn)
Description copied from interface: LockManager
Returns true if the node is locked (either for reading or writing) by anyone, and false otherwise.

Parameters:
fqn - node to inspect
Returns:
true of locked; false if not.

isLocked

public boolean isLocked(NodeSPI n,
                        LockType lockType)
Description copied from interface: LockManager
Returns true if the node is locked (either for reading or writing) by anyone, and false otherwise.

Parameters:
n - node to inspect
lockType - lockType to test for
Returns:
true of locked; false if not.

getWriteOwner

public Object getWriteOwner(Fqn f)
Description copied from interface: LockManager
Retrieves the write lock owner, if any, for the current Fqn.

Parameters:
f - Fqn to inspect
Returns:
the owner of the lock, or null if not locked.

getReadOwners

public Collection<Object> getReadOwners(Fqn f)
Description copied from interface: LockManager
Retrieves the read lock owners, if any, for the current Fqn.

Parameters:
f - Fqn to inspect
Returns:
a collection of read lock owners, or an empty collection if not locked.

printLockInfo

public String printLockInfo(NodeSPI node)
Description copied from interface: LockManager
Prints lock information about a node (and its children) to a String.

Parameters:
node - node to inspect

printLockInfo

public String printLockInfo()
Description copied from interface: LockManager
Prints lock information for all locks.

Returns:
lock information

getConcurrencyLevel

public int getConcurrencyLevel()

getNumberOfLocksHeld

public int getNumberOfLocksHeld()

getNumberOfLocksAvailable

public int getNumberOfLocksAvailable()


Copyright © 2009 JBoss, a division of Red Hat. All Rights Reserved.