[jboss-cvs] JBossCache/src/org/jboss/cache/lock ...

Elias Ross genman at noderunner.net
Sun Nov 19 22:53:55 EST 2006


  User: genman  
  Date: 06/11/19 22:53:55

  Modified:    src/org/jboss/cache/lock    IdentityLock.java LockUtil.java
  Added:       src/org/jboss/cache/lock    NodeLock.java
  Log:
  JBCACHE-867, Move methods from the ProxyImpl to Node, refactor Node classes as well
  
  Revision  Changes    Path
  1.15      +177 -15   JBossCache/src/org/jboss/cache/lock/IdentityLock.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: IdentityLock.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/lock/IdentityLock.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -b -r1.14 -r1.15
  --- IdentityLock.java	30 Aug 2006 19:09:56 -0000	1.14
  +++ IdentityLock.java	20 Nov 2006 03:53:55 -0000	1.15
  @@ -9,11 +9,17 @@
   import EDU.oswego.cs.dl.util.concurrent.Sync;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +import org.jboss.cache.DataNode;
   import org.jboss.cache.Fqn;
  +import org.jboss.cache.Node;
  +import org.jboss.cache.NodeImpl;
  +import org.jboss.cache.NodeSPI;
   
   import java.util.ArrayList;
   import java.util.Collection;
  +import java.util.HashSet;
   import java.util.Iterator;
  +import java.util.Map;
   import java.util.Set;
   
   /**
  @@ -55,25 +61,29 @@
    *
    * @author <a href="mailto:bela at jboss.org">Bela Ban</a> Apr 11, 2003
    * @author Ben Wang July 2003
  - * @version $Revision: 1.14 $
  + * @version $Revision: 1.15 $
    */
  -public class IdentityLock
  +public class IdentityLock implements NodeLock
   {
   
  +   /**
  +    * Initialized property for debugging "print_lock_details"
  +    */
  +   private boolean PRINT_LOCK_DETAILS = Boolean.getBoolean("print_lock_details");
  +
      private static final Log log = LogFactory.getLog(IdentityLock.class);
      private static boolean trace = log.isTraceEnabled();
      private final LockStrategy lock_;
      private final LockMap map_;
      private final boolean mustReacquireRead_;
  -   private Fqn fqn_;
  -
  +   private Node node;
   
      /**
       * Creates a new Identity lock based on the lock level set in the {@link LockStrategyFactory}
       */
  -   public IdentityLock(Fqn fqn)
  +   public IdentityLock(Node node)
      {
  -      this(LockStrategyFactory.getLockStrategy(), fqn);
  +      this(LockStrategyFactory.getLockStrategy(), node);
         log.trace("Using default lock level");
      }
   
  @@ -82,26 +92,35 @@
       *
       * @param level
       */
  -   public IdentityLock(IsolationLevel level, Fqn fqn)
  +   public IdentityLock(IsolationLevel level, Node node)
      {
  -      this(LockStrategyFactory.getLockStrategy(level), fqn);
  -
  +      this(LockStrategyFactory.getLockStrategy(level), node);
      }
   
  -   private IdentityLock(LockStrategy strategy, Fqn fqn)
  +   private IdentityLock(LockStrategy strategy, Node node)
      {
         lock_ = strategy;
         mustReacquireRead_ = strategy instanceof LockStrategyReadCommitted;
         map_ = new LockMap();
  -      fqn_ = fqn == null ? Fqn.ROOT : fqn;
  +      this.node = node;
  +   }
  +
  +   /**
  +    * Returns the node for this lock, may be <code>null</code>.
  +    */
  +   public Node getNode()
  +   {
  +      return node;
      }
   
      /**
  -    * Returns the FQN for this lock, may be <code>null</code>.
  +    * Returns the FQN this lock, may be <code>null</code>.
       */
      public Fqn getFqn()
      {
  -      return fqn_;
  +      if (node == null)
  +         return null;
  +      return node.getFqn();
      }
   
      /**
  @@ -139,6 +158,21 @@
       */
      public boolean acquireWriteLock(Object caller, long timeout) throws LockingException, TimeoutException, InterruptedException
      {
  +      if (trace)
  +      {
  +         log.trace(new StringBuffer("acquiring WL: fqn=").append(getFqn()).append(", caller=").append(caller).
  +                 append(", lock=").append(toString(PRINT_LOCK_DETAILS)));
  +      }
  +      boolean flag = acquireWriteLock0(caller, timeout);
  +      if (trace)
  +      {
  +         log.trace(new StringBuffer("acquired WL: fqn=").append(getFqn()).append(", caller=").append(caller).
  +                 append(", lock=").append(toString(PRINT_LOCK_DETAILS)));
  +      }
  +      return flag;
  +   }
  +   
  +   private boolean acquireWriteLock0(Object caller, long timeout) throws LockingException, TimeoutException, InterruptedException {
         if (caller == null)
         {
            throw new IllegalArgumentException("acquireWriteLock(): null caller");
  @@ -215,7 +249,23 @@
       * @throws LockingException
       * @throws TimeoutException
       */
  -   public boolean acquireReadLock(Object caller, long timeout)
  +   public boolean acquireReadLock(Object caller, long timeout) throws LockingException, TimeoutException, InterruptedException
  +   {
  +      if (trace)
  +      {
  +         log.trace(new StringBuffer("acquiring RL: fqn=").append(getFqn()).append(", caller=").append(caller).
  +                 append(", lock=").append(toString(PRINT_LOCK_DETAILS)));
  +      }
  +      boolean flag = acquireReadLock0(caller, timeout);
  +      if (trace)
  +      {
  +         log.trace(new StringBuffer("acquired RL: fqn=").append(getFqn()).append(", caller=").append(caller).
  +                 append(", lock=").append(toString(PRINT_LOCK_DETAILS)));
  +      }
  +      return flag;
  +   }
  +   
  +   private boolean acquireReadLock0(Object caller, long timeout)
              throws LockingException, TimeoutException, InterruptedException
      {
         boolean rc;
  @@ -298,7 +348,6 @@
       */
      public void releaseAll()
      {
  -      Collection col;
         try
         {
            if ((map_.writerOwner()) != null)
  @@ -411,4 +460,117 @@
            sb.append(" (").append(lock_.toString()).append(')');
         }
      }
  +   
  +   public boolean acquire(Object caller, long timeout, NodeLock.LockType lock_type) throws LockingException, TimeoutException, InterruptedException
  +   {
  +      try
  +      {
  +         if (lock_type == NodeLock.LockType.NONE)
  +         {
  +            return true;
  +         }
  +         else if (lock_type == NodeLock.LockType.READ)
  +         {
  +            return acquireReadLock(caller, timeout);
  +         }
  +         else
  +         {
  +            return acquireWriteLock(caller, timeout);
  +         }
  +      }
  +      catch (UpgradeException e)
  +      {
  +         StringBuffer buf = new StringBuffer("failure upgrading lock: fqn=").append(getFqn()).append(", caller=").append(caller).
  +                 append(", lock=").append(toString(true));
  +         if (trace)
  +         {
  +            log.trace(buf.toString());
  +         }
  +         throw new UpgradeException(buf.toString(), e);
  +      }
  +      catch (LockingException e)
  +      {
  +         StringBuffer buf = new StringBuffer("failure acquiring lock: fqn=").append(getFqn()).append(", caller=").append(caller).
  +                 append(", lock=").append(toString(true));
  +         if (trace)
  +         {
  +            log.trace(buf.toString());
  +         }
  +         throw new LockingException(buf.toString(), e);
  +      }
  +      catch (TimeoutException e)
  +      {
  +         StringBuffer buf = new StringBuffer("failure acquiring lock: fqn=").append(getFqn()).append(", caller=").append(caller).
  +                 append(", lock=").append(toString(true));
  +         if (trace)
  +         {
  +            log.trace(buf.toString());
  +         }
  +         throw new TimeoutException(buf.toString(), e);
  +      }
  +   }
  +
  +   public Set acquireAll(Object caller, long timeout, LockType lock_type)
  +      throws LockingException, TimeoutException, InterruptedException
  +   {
  +      DataNode tmp;
  +      boolean acquired;
  +      Set retval = new HashSet();
  +
  +      if (lock_type == LockType.NONE)
  +      {
  +         return retval;
  +      }
  +      acquired = acquire(caller, timeout, lock_type);
  +      if (acquired)
  +      {
  +         retval.add(this);
  +      }
  +
  +      for (Node n : node.getChildren())
  +      {
  +         retval.addAll(n.getNodeSPI().getLock().acquireAll(caller, timeout, lock_type));
  +      }
  +      return retval;
  +   }
  +   
  +   public void releaseAll(Object owner)
  +   {
  +      for (Node n: node.getChildren()) {
  +         n.getNodeSPI().getLock().releaseAll(owner);
  +      }
  +      release(owner);
  +   }
  +
  +   private void printIndent(StringBuffer sb, int indent)
  +   {
  +      if (sb != null)
  +      {
  +         for (int i = 0; i < indent; i++)
  +         {
  +            sb.append(" ");
  +         }
  +      }
  +   }
  +
  +   public void printLockInfo(StringBuffer sb, int indent)
  +   {
  +      boolean locked = isLocked();
  +
  +      printIndent(sb, indent);
  +      sb.append(Fqn.SEPARATOR).append(node.getFqn().getLast());
  +      if (locked)
  +      {
  +         sb.append("\t(");
  +         toString(sb);
  +         sb.append(")");
  +      }
  +
  +      for (Node n : node.getChildren())
  +      {
  +         sb.append("\n");
  +         n.getNodeSPI().getLock().printLockInfo(sb, indent + 4);
  +      }
  +   }
  +   
   }
  
  
  
  1.4       +12 -13    JBossCache/src/org/jboss/cache/lock/LockUtil.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: LockUtil.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/lock/LockUtil.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -b -r1.3 -r1.4
  --- LockUtil.java	30 Aug 2006 19:09:56 -0000	1.3
  +++ LockUtil.java	20 Nov 2006 03:53:55 -0000	1.4
  @@ -2,8 +2,8 @@
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  -import org.jboss.cache.DataNode;
   import org.jboss.cache.GlobalTransaction;
  +import org.jboss.cache.Node;
   import org.jboss.cache.TransactionTable;
   import org.jboss.cache.TreeCache;
   import org.jboss.cache.statetransfer.StateTransferManager;
  @@ -23,7 +23,7 @@
         public static final int STATUS_BROKEN = Integer.MIN_VALUE;
      }
   
  -   public static boolean breakTransactionLock(IdentityLock lock,
  +   public static boolean breakTransactionLock(NodeLock lock,
                                                 GlobalTransaction gtx,
                                                 boolean localTx,
                                                 TreeCache cache)
  @@ -61,13 +61,13 @@
       * @param lockChildren <code>true</code> if this method should be recursively
       *                     applied to <code>node</code>'s children.
       */
  -   public static void forceAcquireLock(DataNode node,
  +   public static void forceAcquireLock(Node node,
                                          Object newOwner,
                                          TreeCache cache,
                                          boolean lockChildren)
      {
   
  -      IdentityLock lock = node.getLock();
  +      NodeLock lock = node.getNodeSPI().getLock();
         boolean acquired = lock.isOwner(newOwner);
   
         if (!acquired && log.isDebugEnabled())
  @@ -113,7 +113,7 @@
               // Seems no one is holding a lock and it's there for the taking.
               try
               {
  -               acquired = node.acquire(newOwner, 1, DataNode.LockType.READ);
  +               acquired = lock.acquire(newOwner, 1, NodeLock.LockType.READ);
               }
               catch (Exception ignored)
               {
  @@ -122,12 +122,11 @@
         }
   
         // Recursively unlock children
  -      if (lockChildren && node.hasChildren())
  +      if (lockChildren)
         {
  -         Collection children = node.getChildren().values();
  -         for (Iterator it = children.iterator(); it.hasNext();)
  +         for (Node n: node.getChildren())
            {
  -            forceAcquireLock((DataNode) it.next(), newOwner, cache, true);
  +            forceAcquireLock(n, newOwner, cache, true);
            }
         }
      }
  @@ -142,8 +141,8 @@
       * @param curOwner the current owner
       * @param newOwner the new owner
       */
  -   private static boolean acquireLockFromOwner(DataNode node,
  -                                               IdentityLock lock,
  +   private static boolean acquireLockFromOwner(Node node,
  +                                               NodeLock lock,
                                                  Object curOwner,
                                                  Object newOwner,
                                                  TransactionTable tx_table,
  @@ -184,7 +183,7 @@
   
            try
            {
  -            acquired = node.acquire(newOwner, 1, DataNode.LockType.READ);
  +            acquired = lock.acquire(newOwner, 1, NodeLock.LockType.READ);
            }
            catch (Exception ignore)
            {
  @@ -222,7 +221,7 @@
       *         if the lock held by gtx was forcibly broken.
       */
      private static int breakTransactionLock(GlobalTransaction gtx,
  -                                           IdentityLock lock,
  +                                           NodeLock lock,
                                              TransactionTable tx_table,
                                              TransactionManager tm,
                                              boolean localTx,
  
  
  
  1.1      date: 2006/11/20 03:53:55;  author: genman;  state: Exp;JBossCache/src/org/jboss/cache/lock/NodeLock.java
  
  Index: NodeLock.java
  ===================================================================
  package org.jboss.cache.lock;
  
  import java.util.Set;
  
  
  /**
   * Interface for a lock for nodes. 
   */
  public interface NodeLock
  {
  
     public enum LockType
     {
        NONE, READ, WRITE
     }
  
     /**
      * Returns a copy of the reader lock owner in List. 
      * Size is zero is not available. Note that this list
      * is synchronized.
      *
      * @return Set of readers
      */
     Set getReaderOwners();
  
     /**
      * Returns the writer lock owner object. Null if not available.
      *
      * @return Object owner
      */
     Object getWriterOwner();
  
     /**
      * Acquires a write lock with a timeout of <code>timeout</code> milliseconds.
      * Note that if the current owner owns a read lock, it will be upgraded
      * automatically. However, if upgrade fails, i.e., timeout, the read lock will
      * be released automatically.
      *
      * @param caller  Can't be null.
      * @param timeout
      * @return boolean True if lock was acquired and was not held before, false if lock was held
      * @throws LockingException
      * @throws TimeoutException
      */
     boolean acquireWriteLock(Object caller, long timeout) throws LockingException, TimeoutException,
           InterruptedException;
  
     /**
      * Acquires a read lock with a timeout period of <code>timeout</code> milliseconds.
      *
      * @param caller  Can't be null.
      * @param timeout
      * @return boolean True if lock was acquired and was not held before, false if lock was held
      * @throws LockingException
      * @throws TimeoutException
      */
     boolean acquireReadLock(Object caller, long timeout) throws LockingException, TimeoutException, InterruptedException;
  
     /**
      * Releases the lock held by the owner.
      *
      * @param caller Can't be null.
      */
     void release(Object caller);
  
     /**
      * Releases all locks associated with this instance.
      */
     void releaseAll();
  
     /**
      * Releases all locks with this owner.
      */
     void releaseAll(Object owner);
  
     /**
      * Check if there is a read lock.
      */
     boolean isReadLocked();
  
     /**
      * Check if there is a write lock.
      */
     boolean isWriteLocked();
  
     /**
      * Check if there is a read or write lock
      */
     boolean isLocked();
  
     /**
      * Returns true if the object is the lock owner.
      */
     boolean isOwner(Object o);
  
     boolean acquire(Object caller, long timeout, NodeLock.LockType lock_type) throws LockingException, TimeoutException,
           InterruptedException;
  
     Set acquireAll(Object caller, long timeout, NodeLock.LockType lock_type) throws LockingException, TimeoutException,
           InterruptedException;
  
     void printLockInfo(StringBuffer sb, int indent);
  
  }
  
  



More information about the jboss-cvs-commits mailing list