[jbosscache-commits] JBoss Cache SVN: r4972 - core/trunk/src/main/java/org/jboss/cache/interceptors.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Thu Jan 3 20:59:29 EST 2008


Author: manik.surtani at jboss.com
Date: 2008-01-03 20:59:29 -0500 (Thu, 03 Jan 2008)
New Revision: 4972

Modified:
   core/trunk/src/main/java/org/jboss/cache/interceptors/ActivationInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/CacheLoaderInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/CacheStoreInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/DataGravitatorInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/EvictionInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/Interceptor.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/InvocationContextInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticCreateIfNotExistsInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/PassivationInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/PessimisticLockInterceptor.java
Log:
JBCACHE-881 - cache node references in invocation context to save on multiple expensive cache.peek() calls.

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/ActivationInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/ActivationInterceptor.java	2008-01-04 01:58:33 UTC (rev 4971)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/ActivationInterceptor.java	2008-01-04 01:59:29 UTC (rev 4972)
@@ -49,6 +49,7 @@
    {
       initLogger();
       isActivation = true;
+      useCacheStore = false;
    }
 
    @Override
@@ -176,7 +177,7 @@
    private void removeNodeFromCacheLoader(InvocationContext ctx, Fqn fqn) throws Throwable
    {
       NodeSPI n;
-      if (((n = getNode(fqn)) != null) && n.isDataLoaded() && loader.exists(fqn))
+      if (((n = peekNode(ctx, fqn, false, true)) != null) && n.isDataLoaded() && loader.exists(fqn))
       {
          // node not null and attributes have been loaded?
          if (!n.getChildrenDirect().isEmpty())
@@ -338,9 +339,9 @@
                // AND it was found in the cache loader (nodeLoaded = true).
                // Then notify the listeners that the node has been activated.
                Fqn fqn = (Fqn) args[1];
-               if (fqn != null && cache.peek(fqn, false) != null && loader.exists(fqn))
+               if (fqn != null && peekNode(ctx, fqn, false, false) != null && loader.exists(fqn))
                {
-                  NodeSPI n = getNode(fqn);// don't load
+                  NodeSPI n = peekNode(ctx, fqn, false, true);// don't load
                   // node not null and attributes have been loaded?
                   if (n != null && n.isDataLoaded())
                   {

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/CacheLoaderInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/CacheLoaderInterceptor.java	2008-01-04 01:58:33 UTC (rev 4971)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/CacheLoaderInterceptor.java	2008-01-04 01:59:29 UTC (rev 4972)
@@ -468,16 +468,6 @@
 //      nextInterceptor(m);
    }
 
-   /**
-    * Retrieves a node from memory; doesn't access the cache loader
-    *
-    * @param fqn
-    */
-   protected NodeSPI getNode(Fqn fqn)
-   {
-      return cache.peek(fqn, true);
-   }
-
    private TransactionEntry getTransactionEntry(InvocationContext ctx)
    {
       GlobalTransaction gtx = ctx.getGlobalTransaction();

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/CacheStoreInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/CacheStoreInterceptor.java	2008-01-04 01:58:33 UTC (rev 4971)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/CacheStoreInterceptor.java	2008-01-04 01:59:29 UTC (rev 4972)
@@ -212,7 +212,7 @@
       {
          loader.removeData(fqn);
          // we need to mark this node as data loaded
-         NodeSPI n = cache.peek(fqn, false);
+         NodeSPI n = peekNode(ctx, fqn, false, false);//cache.peek(fqn, false);
          if (n != null)
          {
             n.setDataLoaded(true);
@@ -245,7 +245,7 @@
       }
       loader.removeData(fqn);
       // if we are erasing all the data then consider this node loaded
-      NodeSPI n = cache.peek(fqn, false);
+      NodeSPI n = peekNode(ctx, fqn, false, false);//cache.peek(fqn, false);
       n.setDataLoaded(true);
       return returnValue;
    }

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/DataGravitatorInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/DataGravitatorInterceptor.java	2008-01-04 01:58:33 UTC (rev 4971)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/DataGravitatorInterceptor.java	2008-01-04 01:59:29 UTC (rev 4972)
@@ -172,7 +172,7 @@
          }
          else
          {
-            if (cache.peek(fqn, false) == null)
+            if (peekNode(ctx, fqn, false, false) == null)
             {
                if (trace) log.trace("Gravitating from local backup tree");
                BackupData data = localBackupGet(fqn, ctx);
@@ -192,7 +192,7 @@
                   // Make sure I replicate to my buddies.
                   if (trace)
                      log.trace("Passing the put call locally to make sure state is persisted and ownership is correctly established.");
-                  createNode(data.backupData, false);
+                  createNode(ctx, data.backupData, false);
 
                   // very strange, the invocation contexts get twisted up here, and will need preservation.
                   // a bit crappy and hacky, all will be solved when we move to JBoss AOP in 2.1.0
@@ -379,13 +379,14 @@
    }
 
    @SuppressWarnings("unchecked")
-   private void createNode(List<NodeData> nodeData, boolean localOnly) throws CacheException
+   private void createNode(InvocationContext ctx, List<NodeData> nodeData, boolean localOnly) throws CacheException
    {
       for (NodeData data : nodeData)
       {
          if (localOnly)
          {
-            if (cache.peek(data.getFqn(), false) == null)
+//            if (cache.peek(data.getFqn(), false) == null)
+            if (peekNode(ctx, data.getFqn(), false, false) == null)
             {
                createNodesLocally(data.getFqn(), data.getAttributes());
             }

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/EvictionInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/EvictionInterceptor.java	2008-01-04 01:58:33 UTC (rev 4971)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/EvictionInterceptor.java	2008-01-04 01:59:29 UTC (rev 4972)
@@ -68,7 +68,7 @@
       {
          if (fqn != null && !canIgnoreEvent(fqn, NodeEventType.ADD_NODE_EVENT))
          {
-            registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT, 0));
+            registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT, 0), ctx);
          }
       }
       return retVal;
@@ -86,7 +86,7 @@
       Object retVal = nextInterceptor(ctx);
       if (fqn != null && key != null && !canIgnoreEvent(fqn, NodeEventType.ADD_ELEMENT_EVENT))
       {
-         registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.ADD_ELEMENT_EVENT, 1));
+         registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.ADD_ELEMENT_EVENT, 1), ctx);
       }
       return retVal;
    }
@@ -113,7 +113,7 @@
             }
             EvictedEventNode event = new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT, size);
             event.setResetElementCount(eraseContents);
-            registerEvictionEventToRegionManager(event);
+            registerEvictionEventToRegionManager(event, ctx);
          }
       }
       return retVal;
@@ -140,7 +140,7 @@
             {
                size = data.size();
             }
-            registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT, size));
+            registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT, size), ctx);
          }
       }
       return retVal;
@@ -162,7 +162,7 @@
       {
          if (fqn != null && key != null && !canIgnoreEvent(fqn, NodeEventType.REMOVE_ELEMENT_EVENT))
          {
-            registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.REMOVE_ELEMENT_EVENT, 1));
+            registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.REMOVE_ELEMENT_EVENT, 1), ctx);
          }
       }
       return retVal;
@@ -183,7 +183,7 @@
       {
          if (fqn != null && !canIgnoreEvent(fqn, NodeEventType.VISIT_NODE_EVENT))
          {
-            registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.VISIT_NODE_EVENT));
+            registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.VISIT_NODE_EVENT), ctx);
          }
       }
       return retVal;
@@ -208,7 +208,7 @@
       }
       else if (fqn != null && key != null && !canIgnoreEvent(fqn, NodeEventType.VISIT_NODE_EVENT))
       {
-         registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.VISIT_NODE_EVENT));
+         registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.VISIT_NODE_EVENT), ctx);
       }
       return retVal;
    }
@@ -219,7 +219,7 @@
       Object retVal = nextInterceptor(ctx);
       if (fqn != null && !canIgnoreEvent(fqn, NodeEventType.REMOVE_NODE_EVENT))
       {
-         registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.REMOVE_NODE_EVENT));
+         registerEvictionEventToRegionManager(new EvictedEventNode(fqn, NodeEventType.REMOVE_NODE_EVENT), ctx);
       }
       return retVal;
    }
@@ -230,7 +230,7 @@
       return handleRemoveNodeMethod(ctx, tx, fqn, createUndoOps);
    }
 
-   private void registerEvictionEventToRegionManager(EvictedEventNode event)
+   private void registerEvictionEventToRegionManager(EvictedEventNode event, InvocationContext ctx)
    {
       if (event == null)
       {
@@ -238,7 +238,7 @@
          return;
       }
 
-      NodeSPI<?, ?> nodeSPI = cache.peek(event.getFqn(), false);
+      NodeSPI<?, ?> nodeSPI = peekNode(ctx, event.getFqn(), false, false);//cache.peek(event.getFqn(), false);
       //we do not trigger eviction events for resident nodes
       if (nodeSPI != null && nodeSPI.isResident())
       {

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/Interceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/Interceptor.java	2008-01-04 01:58:33 UTC (rev 4971)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/Interceptor.java	2008-01-04 01:59:29 UTC (rev 4972)
@@ -25,7 +25,9 @@
 import org.apache.commons.logging.LogFactory;
 import org.jboss.cache.CacheException;
 import org.jboss.cache.CacheSPI;
+import org.jboss.cache.Fqn;
 import org.jboss.cache.InvocationContext;
+import org.jboss.cache.NodeSPI;
 import org.jboss.cache.config.Configuration;
 import org.jboss.cache.factories.annotations.Inject;
 import org.jboss.cache.factories.annotations.Start;
@@ -220,4 +222,29 @@
             + (getLast() == null ? null : getLast().getClass())
             + "}";
    }
+
+   /**
+    * First checks the invocation context for previously obtained reference to a node, if this doesn't exist, performs
+    * a cache.peek() and holds on to the node reference.
+    *
+    * @param ctx                 invocation context
+    * @param f                   fqn to find
+    * @param forceRefresh        forces calling cache.peek() even if we hold a reference to the relevant node.
+    * @param includeDeletedNodes includes nodes marked for deletion if this is true
+    * @return a node, or null if one cannot be found.
+    * @since 2.1.0
+    */
+   public NodeSPI peekNode(InvocationContext ctx, Fqn f, boolean forceRefresh, boolean includeDeletedNodes)
+   {
+      NodeSPI n;
+      if (forceRefresh || (n = ctx.getPeekedNode(f)) == null)
+      {
+         n = cache.peek(f, true);
+         // put this in the invocation cache
+         ctx.savePeekedNode(n, f);
+      }
+
+      if (n != null && !includeDeletedNodes && n.isDeleted()) return null;
+      return n;
+   }
 }

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/InvocationContextInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/InvocationContextInterceptor.java	2008-01-04 01:58:33 UTC (rev 4971)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/InvocationContextInterceptor.java	2008-01-04 01:59:29 UTC (rev 4972)
@@ -107,6 +107,8 @@
                copyInvocationScopeOptionsToTxScope(ctx);
             }
          }
+
+         ctx.wipePeekedNodes();
       }
    }
 

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticCreateIfNotExistsInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticCreateIfNotExistsInterceptor.java	2008-01-04 01:58:33 UTC (rev 4971)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticCreateIfNotExistsInterceptor.java	2008-01-04 01:59:29 UTC (rev 4972)
@@ -136,7 +136,8 @@
     */
    private void createNode(InvocationContext ctx, Fqn targetFqn, boolean suppressNotification) throws CacheException
    {
-      if (cache.peek(targetFqn, false) != null) return;
+//      if (cache.peek(targetFqn, false) != null) return;
+      if (peekNode(ctx, targetFqn, false, false) != null) return;
       // we do nothing if targetFqn is null
       if (targetFqn == null) return;
 

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/PassivationInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/PassivationInterceptor.java	2008-01-04 01:58:33 UTC (rev 4971)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/PassivationInterceptor.java	2008-01-04 01:59:29 UTC (rev 4972)
@@ -47,7 +47,7 @@
       {
          // evict method local doesn't hold attributes therefore we have
          // to get them manually
-         Map attributes = getNodeAttributes(fqn);
+         Map attributes = getNodeAttributes(ctx, fqn);
          // notify listeners that this node is about to be passivated
          cache.getNotifier().notifyNodePassivated(fqn, true, attributes, ctx);
          loader.put(fqn, attributes);
@@ -88,13 +88,13 @@
    /**
     * Returns attributes for a node.
     */
-   private Map getNodeAttributes(Fqn fqn) throws NodeNotLoadedException
+   private Map getNodeAttributes(InvocationContext ctx, Fqn fqn) throws NodeNotLoadedException
    {
       if (fqn == null)
       {
          throw new NodeNotLoadedException();
       }
-      NodeSPI n = cache.peek(fqn, true);
+      NodeSPI n = peekNode(ctx, fqn, false, true);
 
       if (n != 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-01-04 01:58:33 UTC (rev 4971)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/PessimisticLockInterceptor.java	2008-01-04 01:59:29 UTC (rev 4972)
@@ -133,7 +133,8 @@
       acquireLocksWithTimeout(ctx, fqn, lockType, false, false, false, false);
       if (recursive)
       {
-         acquireLocksOnChildren(cache.peek(fqn, false), lockType, ctx);
+         //acquireLocksOnChildren(cache.peek(fqn, false), lockType, ctx);
+         acquireLocksOnChildren(peekNode(ctx, fqn, false, false), lockType, ctx);
       }
       return null;
    }
@@ -208,18 +209,18 @@
          {
             cache.getTransactionTable().get(ctx.getGlobalTransaction()).addRemovedNode(from);
          }
-         acquireLocksOnChildren(cache.peek(from, true), NodeLock.LockType.WRITE, ctx);
+         acquireLocksOnChildren(peekNode(ctx, from, false, true), NodeLock.LockType.WRITE, ctx);
       }
       if (to != null && !(configuration.getIsolationLevel() == IsolationLevel.NONE))
       {
          //now for an RL for the new parent.
          if (trace) log.trace("Attempting to get RL on new parent [" + to + "]");
          lock(ctx, to, NodeLock.LockType.READ, false, timeout, false, false);
-         acquireLocksOnChildren(cache.peek(to, true), NodeLock.LockType.READ, ctx);
+         acquireLocksOnChildren(peekNode(ctx, to, false, true), NodeLock.LockType.READ, ctx);
       }
       Object retValue = nextInterceptor(ctx);
       // do a REAL remove here.
-      NodeSPI n = cache.peek(from, true);
+      NodeSPI n = peekNode(ctx, from, false, true);
       if (n != null)
       {
          n.getLock().releaseAll(Thread.currentThread());
@@ -239,7 +240,7 @@
       if (ctx.getGlobalTransaction() == null)
       {
          cacheImpl.realRemove(fqn, true);
-         NodeSPI n = cache.peek(fqn, true);
+         NodeSPI n = peekNode(ctx, fqn, false, true);
          if (n != null)
          {
             n.getLock().releaseAll(Thread.currentThread());
@@ -336,7 +337,7 @@
          created = lock(ctx, fqn, lockType, createIfNotExists, timeout, acquireLockOnParent, reverseRemoveCheck);
          firstTry = false;
       }
-      while (createIfNotExists && cache.peek(fqn, true) == null);// keep trying until we have the lock (fixes concurrent remove())
+      while (createIfNotExists && peekNode(ctx, fqn, false, true) == null);// keep trying until we have the lock (fixes concurrent remove())
       return created;
    }
 
@@ -489,7 +490,8 @@
          {
             return true;// we're doing a remove and we've reached the PARENT node of the target to be removed.
          }
-         if (!isTargetNode && cache.peek(targetFqn.getAncestor(currentNodeIndex + 2), false) == null)
+         if (!isTargetNode && peekNode(ctx, targetFqn.getAncestor(currentNodeIndex + 2), false, false) == null)
+         //if (!isTargetNode && cache.peek(targetFqn.getAncestor(currentNodeIndex + 2), false) == null)
          //if (!isTargetNode && cache.peek(new Fqn(currentNode.getFqn(), targetFqn.get(currentNodeIndex + 1)), false) == null)
          {
             return createIfNotExists;// we're at a node in the tree, not yet at the target node, and we need to create the next node.  So we need a WL here.




More information about the jbosscache-commits mailing list