[jbosscache-commits] JBoss Cache SVN: r5560 - in core/trunk/src/main/java/org/jboss/cache: interceptors and 1 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Mon Apr 14 13:08:26 EDT 2008


Author: mircea.markus
Date: 2008-04-14 13:08:26 -0400 (Mon, 14 Apr 2008)
New Revision: 5560

Modified:
   core/trunk/src/main/java/org/jboss/cache/InvocationContext.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticCreateIfNotExistsInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/PessimisticLockInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/TxInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/invocation/InterceptorChain.java
Log:
JBCACHE-1222 - bug fixing - released locks and optimistic add

Modified: core/trunk/src/main/java/org/jboss/cache/InvocationContext.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/InvocationContext.java	2008-04-14 15:39:38 UTC (rev 5559)
+++ core/trunk/src/main/java/org/jboss/cache/InvocationContext.java	2008-04-14 17:08:26 UTC (rev 5560)
@@ -190,36 +190,39 @@
     */
    public void clearInvocationLocksAcquired()
    {
-      {
-         Transaction tx = getTransaction();
-         if (tx == null || !TxUtil.isValid(tx))
-         { // no TX
-            List<NodeLock> locks = getInvocationLocksAcquired();
-            if (log.isTraceEnabled())
-               log.trace("Attempting to release locks on current thread.  Locks for the invocation is " + locks);
+      if (isSupressLocking()) return;
+      if (!isValidTransaction())
+      { // no TX
+         List<NodeLock> locks = getInvocationLocksAcquired();
+         if (log.isTraceEnabled())
+            log.trace("Attempting to release locks on current thread.  Locks for the invocation is " + locks);
 
-            if (locks != null && locks.size() > 0)
+         if (locks != null && locks.size() > 0)
+         {
+            Thread currentThread = Thread.currentThread();
+            try
             {
-               Thread currentThread = Thread.currentThread();
-               try
+               // make sure we release locks in *reverse* order!
+               for (int i = locks.size() - 1; i > -1; i--)
                {
-                  // make sure we release locks in *reverse* order!
-                  for (int i = locks.size() - 1; i > -1; i--)
-                  {
-                     NodeLock nl = locks.get(i);
-                     if (log.isTraceEnabled()) log.trace("releasing lock for " + nl.getFqn() + ": " + nl);
-                     nl.release(currentThread);
-                  }
+                  NodeLock nl = locks.get(i);
+                  if (log.isTraceEnabled()) log.trace("releasing lock for " + nl.getFqn() + ": " + nl);
+                  nl.release(currentThread);
                }
-               finally
-               {
-                  invocationLocks = null;
-               }
             }
+            finally
+            {
+               invocationLocks = null;
+            }
          }
       }
    }
 
+   public boolean isSupressLocking()
+   {
+      return getOptionOverrides() != null && getOptionOverrides().isSuppressLocking();
+   }
+
    /**
     * If set to true, the invocation is assumed to have originated locally.  If set to false,
     * assumed to have originated from a remote cache.

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticCreateIfNotExistsInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticCreateIfNotExistsInterceptor.java	2008-04-14 15:39:38 UTC (rev 5559)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticCreateIfNotExistsInterceptor.java	2008-04-14 17:08:26 UTC (rev 5560)
@@ -152,7 +152,17 @@
          {
             // first test that it exists in the workspace and has been created in thix tx!
             WorkspaceNode peekInWorkspace = workspace.getNode(Fqn.fromRelativeElements(workspaceNode.getFqn(), childName));
-            if (peekInWorkspace.isDeleted()) undeleteWorkspaceNode(peekInWorkspace, workspaceNode);
+            if (peekInWorkspace != null && peekInWorkspace.isCreated())
+            {
+               // exists in workspace and has just been created.
+               currentNode = peekInWorkspace.getNode();
+               if (peekInWorkspace.isDeleted())
+               {
+                  peekInWorkspace.markAsDeleted(false);
+                  // add in parent again
+                  workspaceNode.addChild(peekInWorkspace);
+               }
+            }
          }
 
          if (currentNode == null)

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/PessimisticLockInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/PessimisticLockInterceptor.java	2008-04-14 15:39:38 UTC (rev 5559)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/PessimisticLockInterceptor.java	2008-04-14 17:08:26 UTC (rev 5560)
@@ -20,7 +20,6 @@
 import org.jboss.cache.lock.IsolationLevel;
 import org.jboss.cache.lock.LockManager;
 import org.jboss.cache.lock.NodeLock;
-import org.jboss.cache.marshall.MethodDeclarations;
 import org.jboss.cache.transaction.GlobalTransaction;
 import org.jboss.cache.transaction.TransactionEntry;
 import org.jboss.cache.transaction.TransactionTable;
@@ -28,14 +27,6 @@
 import java.util.LinkedList;
 import java.util.List;
 
-/*
-* todo refactorings ideas
-*      - thre are many places in code that handles that coputes the lock owners: either GTX or Thread.local. The
-*      lockOwner can be abstractised  as a LockOwner that can be extended by CurrentThreadLock owner and
-       GlobalTransaction owner. This would make the code nicer.
-*/
-
-
 /**
  * An interceptor that handles locking. When a TX is associated, we register
  * for TX completion and unlock the locks acquired within the scope of the TX.
@@ -49,7 +40,6 @@
 {
    private TransactionTable txTable;
    private CacheData cacheData;
-   private NodeSPI rootNode;
    private LockManager lockManager;
    private Configuration configuration;
 
@@ -72,19 +62,19 @@
    @Override
    public Object handlePutDataMapCommand(InvocationContext ctx, PutDataMapCommand command) throws Throwable
    {
-      return handlePutMethod(ctx, command);
+      return handlePutCommand(ctx, command);
    }
 
    @Override
    public Object handlePutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable
    {
-      return handlePutMethod(ctx, command);
+      return handlePutCommand(ctx, command);
    }
 
-   private Object handlePutMethod(InvocationContext ctx, CacheDataCommand command)
+   private Object handlePutCommand(InvocationContext ctx, CacheDataCommand command)
          throws Throwable
    {
-      if ((supressLocking(ctx)) || configuration.getIsolationLevel() == IsolationLevel.NONE)
+      if ((ctx.isSupressLocking()) || configuration.getIsolationLevel() == IsolationLevel.NONE)
       {
          if (trace) log.trace("Suppressing locking, creating nodes if necessary");
          int treeNodeSize = command.getFqn().size();
@@ -98,43 +88,15 @@
             lockManager.manageReverseRemove(ctx.getGlobalTransaction(), childNode, true, null);
             n = childNode;
          }
-      }
-      else
+      } else
       {
          lockManager.acquireLocksWithTimeout(ctx, command.getFqn(), NodeLock.LockType.WRITE, true, false, false, true, null, false);
       }
-
       Object retVal = invokeNextInterceptor(ctx, command);
-
-      // we NEED to unlock stuff!!! - Manik
-
-      if (ctx.getGlobalTransaction() == null && ctx.getTransaction() == null)
-      {
-         Thread current = Thread.currentThread();
-         // we are not transactional.
-         try
-         {
-            for (NodeLock nl : ctx.getInvocationLocksAcquired()) nl.release(current);
-         }
-         finally
-         {
-            ctx.clearInvocationLocksAcquired();
-         }
-      }
-
+      ctx.clearInvocationLocksAcquired();
       return retVal;
    }
 
-   protected boolean skipMethodCall(InvocationContext ctx)
-   {
-      return (supressLocking(ctx) && !MethodDeclarations.isPutMethod(ctx.getMethodCall().getMethodId()));
-   }
-
-   private boolean supressLocking(InvocationContext ctx)
-   {
-      return ctx.getOptionOverrides() != null && ctx.getOptionOverrides().isSuppressLocking();
-   }
-
    public Object handlePrepareCommand(InvocationContext ctx, PrepareCommand command) throws Throwable
    {
       // 2-phase commit prepares are no-ops here.
@@ -166,8 +128,7 @@
       if (entry == null)
       {
          log.error("entry for transaction " + command.getGlobalTransaction() + " not found (transaction has possibly already been rolled back)");
-      }
-      else
+      } else
       {
          for (Fqn fqn : entry.getRemovedNodes())
          {
@@ -187,7 +148,7 @@
 
    public Object handleMoveCommand(InvocationContext ctx, MoveCommand command) throws Throwable
    {
-      if (supressLocking(ctx)) return invokeNextInterceptor(ctx, command);
+      if (ctx.isSupressLocking()) return invokeNextInterceptor(ctx, command);
       long timeout = ctx.getContextLockAcquisitionTimeout(lockAcquisitionTimeout);
       // this call will ensure the node gets a WL and it's current parent gets RL.
       if (trace) log.trace("Attempting to get WL on node to be moved [" + command.getFrom() + "]");
@@ -214,12 +175,13 @@
       {
          n.getLock().releaseAll(Thread.currentThread());
       }
+      ctx.clearInvocationLocksAcquired();
       return retValue;
    }
 
    public Object handleRemoveNodeCommand(InvocationContext ctx, RemoveNodeCommand command) throws Throwable
    {
-      if (supressLocking(ctx)) return invokeNextInterceptor(ctx, command);
+      if (ctx.isSupressLocking()) return invokeNextInterceptor(ctx, command);
       // need to make a note of ALL nodes created here!!
       List<NodeSPI> createdNodes = new LinkedList<NodeSPI>();
       // we need to mark new nodes created as deleted since they are only created to form a path to the node being removed, to
@@ -260,6 +222,7 @@
             n.getLock().releaseAll(Thread.currentThread());
          }
       }
+      ctx.clearInvocationLocksAcquired();
       // if this is a delete op and we had to create the node, return a FALSE as nothing *really* was deleted!
       return created ? false : retVal;
    }
@@ -267,43 +230,57 @@
    public Object handleRemoveKeyCommand(InvocationContext ctx, RemoveKeyCommand command) throws Throwable
    {
       lockManager.acquireLocksWithTimeout(ctx, command.getFqn(), NodeLock.LockType.WRITE, false, false, false, false, null, false);
-      return invokeNextInterceptor(ctx, command);
+      Object retVal = invokeNextInterceptor(ctx, command);
+      ctx.clearInvocationLocksAcquired();
+      return retVal;
    }
 
    public Object handleRemoveDataCommand(InvocationContext ctx, RemoveDataCommand command) throws Throwable
    {
       lockManager.acquireLocksWithTimeout(ctx, command.getFqn(), NodeLock.LockType.WRITE, false, false, false, false, null, false);
-      return invokeNextInterceptor(ctx, command);
+      Object retVal = invokeNextInterceptor(ctx, command);
+      ctx.clearInvocationLocksAcquired();
+      return retVal;
    }
 
    public Object handleEvictFqnCommand(InvocationContext ctx, EvictNodeCommand command) throws Throwable
    {
       lockManager.acquireLocksWithTimeout(ctx, command.getFqn(), NodeLock.LockType.WRITE, false, true, false, false, null, false);
-      return invokeNextInterceptor(ctx, command);
+      Object retVal = invokeNextInterceptor(ctx, command);
+      ctx.clearInvocationLocksAcquired();
+      return retVal;
    }
 
    public Object handleGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command) throws Throwable
    {
       lockManager.acquireLocksWithTimeout(ctx, command.getFqn(), NodeLock.LockType.READ, false, false, false, false, null, false);
-      return invokeNextInterceptor(ctx, command);
+      Object retVal = invokeNextInterceptor(ctx, command);
+      ctx.clearInvocationLocksAcquired();
+      return retVal;
    }
 
    public Object handleGetNodeCommand(InvocationContext ctx, GetNodeCommand command) throws Throwable
    {
       lockManager.acquireLocksWithTimeout(ctx, command.getFqn(), NodeLock.LockType.READ, false, false, false, false, null, false);
-      return invokeNextInterceptor(ctx, command);
+      Object retVal = invokeNextInterceptor(ctx, command);
+      ctx.clearInvocationLocksAcquired();
+      return retVal;
    }
 
    public Object handleGetKeysCommand(InvocationContext ctx, GetKeysCommand command) throws Throwable
    {
       lockManager.acquireLocksWithTimeout(ctx, command.getFqn(), NodeLock.LockType.READ, false, false, false, false, null, false);
-      return invokeNextInterceptor(ctx, command);
+      Object retVal = invokeNextInterceptor(ctx, command);
+      ctx.clearInvocationLocksAcquired();
+      return retVal;
    }
 
    public Object handleGetChildrenNamesCommand(InvocationContext ctx, GetChildrenNamesCommand command) throws Throwable
    {
       lockManager.acquireLocksWithTimeout(ctx, command.getFqn(), NodeLock.LockType.READ, false, false, false, false, null, false);
-      return invokeNextInterceptor(ctx, command);
+      Object retVal = invokeNextInterceptor(ctx, command);
+      ctx.clearInvocationLocksAcquired();
+      return retVal;
    }
 
    /**
@@ -325,3 +302,10 @@
       }
    }
 }
+
+/*
+* todo refactorings ideas
+*      - thre are many places in code that handles that coputes the lock owners: either GTX or Thread.local. The
+*      lockOwner can be abstractised  as a LockOwner that can be extended by CurrentThreadLock owner and
+       GlobalTransaction owner. This would make the code nicer.
+*/

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/TxInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/TxInterceptor.java	2008-04-14 15:39:38 UTC (rev 5559)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/TxInterceptor.java	2008-04-14 17:08:26 UTC (rev 5560)
@@ -419,7 +419,6 @@
             if (implicitTransaction)
             {
                log.warn("Rolling back, exception encountered", t);
-               result = t;
                try
                {
                   setTransactionalContext(tx, gtx, ctx);
@@ -429,6 +428,7 @@
                {
                   log.warn("Roll back failed encountered", th);
                }
+               throw t;
             } else
             {
                throw t;

Modified: core/trunk/src/main/java/org/jboss/cache/invocation/InterceptorChain.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/invocation/InterceptorChain.java	2008-04-14 15:39:38 UTC (rev 5559)
+++ core/trunk/src/main/java/org/jboss/cache/invocation/InterceptorChain.java	2008-04-14 17:08:26 UTC (rev 5560)
@@ -121,7 +121,21 @@
     */
    public List<ChainedInterceptor> getInterceptorsAsList()
    {
-      return Collections.unmodifiableList(getInterceptorsAsListModifiable());
+      List<ChainedInterceptor> result;
+      if (firstInChain == null)
+      {
+         result = Collections.EMPTY_LIST;
+      }
+      List<ChainedInterceptor> retval = new LinkedList<ChainedInterceptor>();
+      ChainedInterceptor tmp = firstInChain;
+      do
+      {
+         retval.add(tmp);
+         tmp = tmp.getNext();
+      }
+      while (tmp != null);
+      result = Collections.unmodifiableList(retval);
+      return result;
    }
 
 
@@ -243,20 +257,4 @@
       return firstInChain;
    }
 
-   private List<ChainedInterceptor> getInterceptorsAsListModifiable()
-   {
-      if (firstInChain == null)
-      {
-         return Collections.EMPTY_LIST;
-      }
-      List<ChainedInterceptor> retval = new LinkedList<ChainedInterceptor>();
-      ChainedInterceptor tmp = firstInChain;
-      do
-      {
-         retval.add(tmp);
-         tmp = tmp.getNext();
-      }
-      while (tmp != null);
-      return retval;
-   }
 }




More information about the jbosscache-commits mailing list