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

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Fri Mar 28 11:43:37 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-03-28 11:43:37 -0400 (Fri, 28 Mar 2008)
New Revision: 5475

Modified:
   core/trunk/src/main/java/org/jboss/cache/eviction/BaseEvictionAlgorithm.java
   core/trunk/src/main/java/org/jboss/cache/eviction/BaseSortedEvictionAlgorithm.java
   core/trunk/src/main/java/org/jboss/cache/eviction/ElementSizeAlgorithm.java
   core/trunk/src/main/java/org/jboss/cache/eviction/EvictedEventNode.java
   core/trunk/src/main/java/org/jboss/cache/eviction/ExpirationAlgorithm.java
   core/trunk/src/main/java/org/jboss/cache/eviction/FIFOAlgorithm.java
   core/trunk/src/main/java/org/jboss/cache/eviction/LFUAlgorithm.java
   core/trunk/src/main/java/org/jboss/cache/eviction/LRUAlgorithm.java
   core/trunk/src/main/java/org/jboss/cache/eviction/MRUAlgorithm.java
Log:
JBCACHE-1315 -  Modification timestamps on eviction events should be captured when the event occurs; not when the eviction thread analyses the events.

Modified: core/trunk/src/main/java/org/jboss/cache/eviction/BaseEvictionAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/BaseEvictionAlgorithm.java	2008-03-27 21:49:43 UTC (rev 5474)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/BaseEvictionAlgorithm.java	2008-03-28 15:43:37 UTC (rev 5475)
@@ -145,33 +145,31 @@
       int count = 0;
       while ((node = region.takeLastEventNode()) != null)
       {
-         Fqn fqn = node.getFqn();
+//         Fqn fqn = node.getFqn();
 
          count++;
          switch (node.getEventType())
          {
             case ADD_NODE_EVENT:
-               this.processAddedNodes(fqn,
-                     node.getElementDifference(),
-                     node.isResetElementCount());
+               this.processAddedNodes(node);
                break;
             case REMOVE_NODE_EVENT:
-               this.processRemovedNodes(fqn);
+               this.processRemovedNodes(node);
                break;
             case VISIT_NODE_EVENT:
-               this.processVisitedNodes(fqn);
+               this.processVisitedNodes(node);
                break;
             case ADD_ELEMENT_EVENT:
-               this.processAddedElement(fqn);
+               this.processAddedElement(node);
                break;
             case REMOVE_ELEMENT_EVENT:
-               this.processRemovedElement(fqn);
+               this.processRemovedElement(node);
                break;
             case MARK_IN_USE_EVENT:
-               this.processMarkInUseNodes(fqn, node.getInUseTimeout());
+               this.processMarkInUseNodes(node.getFqn(), node.getInUseTimeout());
                break;
             case UNMARK_USE_EVENT:
-               this.processUnmarkInUseNodes(fqn);
+               this.processUnmarkInUseNodes(node.getFqn());
                break;
             default:
                throw new RuntimeException("Illegal Eviction Event type " + node.getEventType());
@@ -271,8 +269,22 @@
       }
    }
 
-   protected void processAddedNodes(Fqn fqn, int numAddedElements, boolean resetElementCount) throws EvictionException
+   /**
+    * Convenience method, which calls {@link #processAddedNodes(EvictedEventNode, int, boolean)} using values in the
+    * evictedEventNode for number of added elements and the resetElementCount flag.
+    *
+    * @param evictedEventNode an evictedEventNode to process
+    * @throws EvictionException on problems
+    */
+   protected void processAddedNodes(EvictedEventNode evictedEventNode) throws EvictionException
    {
+      processAddedNodes(evictedEventNode, evictedEventNode.getElementDifference(), evictedEventNode.isResetElementCount());
+   }
+
+   protected void processAddedNodes(EvictedEventNode evictedEventNode, int numAddedElements, boolean resetElementCount) throws EvictionException
+   {
+      Fqn fqn = evictedEventNode.getFqn();
+
       if (log.isTraceEnabled())
       {
          log.trace("Adding node " + fqn + " with " + numAddedElements + " elements to eviction queue");
@@ -281,7 +293,7 @@
       NodeEntry ne = evictionQueue.getNodeEntry(fqn);
       if (ne != null)
       {
-         ne.setModifiedTimeStamp(System.currentTimeMillis());
+         ne.setModifiedTimeStamp(evictedEventNode.getCreationTimestamp());
          ne.setNumberOfNodeVisits(ne.getNumberOfNodeVisits() + 1);
          if (resetElementCount)
          {
@@ -295,25 +307,14 @@
          {
             log.trace("Queue already contains " + ne.getFqn() + " processing it as visited");
          }
-         this.processVisitedNodes(ne.getFqn());
+         processVisitedNodes(evictedEventNode);
          return;
       }
 
-      long stamp = System.currentTimeMillis();
       ne = new NodeEntry(fqn);
-      ne.setModifiedTimeStamp(stamp);
+      ne.setModifiedTimeStamp(evictedEventNode.getCreationTimestamp());
       ne.setNumberOfNodeVisits(1);
       ne.setNumberOfElements(numAddedElements);
-      // add it to the node map and eviction queue
-//      if (evictionQueue.containsNodeEntry(ne))
-//      {
-//         if (log.isTraceEnabled())
-//         {
-//            log.trace("Queue already contains " + ne.getFqn() + " processing it as visited");
-//         }
-//         this.processVisitedNodes(ne.getFqn());
-//         return;
-//      }
 
       evictionQueue.addNodeEntry(ne);
 
@@ -336,11 +337,12 @@
     * Because EvictionQueues are collections, when iterating them from an iterator, use iterator.remove()
     * to avoid ConcurrentModificationExceptions. Use the boolean parameter to indicate the calling context.
     *
-    * @param fqn FQN of the removed node
     * @throws EvictionException
     */
-   protected void processRemovedNodes(Fqn fqn) throws EvictionException
+   protected void processRemovedNodes(EvictedEventNode evictedEventNode) throws EvictionException
    {
+      Fqn fqn = evictedEventNode.getFqn();
+
       if (log.isTraceEnabled())
       {
          log.trace("Removing node " + fqn + " from eviction queue and attempting eviction");
@@ -387,11 +389,11 @@
     * into the queue. For some sorted collections, a remove, and a re-add is required to
     * maintain the sorted order of the elements.
     *
-    * @param fqn FQN of the visited node.
     * @throws EvictionException
     */
-   protected void processVisitedNodes(Fqn fqn) throws EvictionException
+   protected void processVisitedNodes(EvictedEventNode evictedEventNode) throws EvictionException
    {
+      Fqn fqn = evictedEventNode.getFqn();
       NodeEntry ne = evictionQueue.getNodeEntry(fqn);
       if (ne == null)
       {
@@ -399,7 +401,7 @@
          {
             log.debug("Visiting node that was not added to eviction queues. Assuming that it has 1 element.");
          }
-         this.processAddedNodes(fqn, 1, false);
+         this.processAddedNodes(evictedEventNode, 1, false);
          return;
       }
       // note this method will visit and modify the node statistics by reference!
@@ -409,8 +411,9 @@
       ne.setModifiedTimeStamp(System.currentTimeMillis());
    }
 
-   protected void processRemovedElement(Fqn fqn) throws EvictionException
+   protected void processRemovedElement(EvictedEventNode evictedEventNode) throws EvictionException
    {
+      Fqn fqn = evictedEventNode.getFqn();
       NodeEntry ne = evictionQueue.getNodeEntry(fqn);
 
       if (ne == null)
@@ -429,8 +432,9 @@
       ne.setModifiedTimeStamp(System.currentTimeMillis());
    }
 
-   protected void processAddedElement(Fqn fqn) throws EvictionException
+   protected void processAddedElement(EvictedEventNode evictedEventNode) throws EvictionException
    {
+      Fqn fqn = evictedEventNode.getFqn();
       NodeEntry ne = evictionQueue.getNodeEntry(fqn);
       if (ne == null)
       {
@@ -438,7 +442,7 @@
          {
             log.trace("Adding element " + fqn + " for a node that doesn't exist yet. Process as an add.");
          }
-         this.processAddedNodes(fqn, 1, false);
+         this.processAddedNodes(evictedEventNode, 1, false);
          return;
       }
 

Modified: core/trunk/src/main/java/org/jboss/cache/eviction/BaseSortedEvictionAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/BaseSortedEvictionAlgorithm.java	2008-03-27 21:49:43 UTC (rev 5474)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/BaseSortedEvictionAlgorithm.java	2008-03-28 15:43:37 UTC (rev 5475)
@@ -46,22 +46,22 @@
          switch (node.getEventType())
          {
             case ADD_NODE_EVENT:
-               this.processAddedNodes(fqn, node.getElementDifference(), node.isResetElementCount());
+               this.processAddedNodes(node);
                evictionNodesModified = true;
                break;
             case REMOVE_NODE_EVENT:
-               this.processRemovedNodes(fqn);
+               this.processRemovedNodes(node);
                break;
             case VISIT_NODE_EVENT:
-               this.processVisitedNodes(fqn);
+               this.processVisitedNodes(node);
                evictionNodesModified = true;
                break;
             case ADD_ELEMENT_EVENT:
-               this.processAddedElement(fqn);
+               this.processAddedElement(node);
                evictionNodesModified = true;
                break;
             case REMOVE_ELEMENT_EVENT:
-               this.processRemovedElement(fqn);
+               this.processRemovedElement(node);
                evictionNodesModified = true;
                break;
             default:

Modified: core/trunk/src/main/java/org/jboss/cache/eviction/ElementSizeAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/ElementSizeAlgorithm.java	2008-03-27 21:49:43 UTC (rev 5474)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/ElementSizeAlgorithm.java	2008-03-28 15:43:37 UTC (rev 5475)
@@ -29,15 +29,10 @@
       ElementSizeConfiguration config = (ElementSizeConfiguration) region.getEvictionPolicyConfig();
 
       int size = this.getEvictionQueue().getNumberOfNodes();
-      if (config.getMaxNodes() != 0 && size > config.getMaxNodes())
-      {
-         return true;
-      }
-
-      return ne.getNumberOfElements() > config.getMaxElementsPerNode();
+      return config.getMaxNodes() != 0 && size > config.getMaxNodes() || ne.getNumberOfElements() > config.getMaxElementsPerNode();
    }
 
-
+   @Override
    protected void prune() throws EvictionException
    {
       super.prune();

Modified: core/trunk/src/main/java/org/jboss/cache/eviction/EvictedEventNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/EvictedEventNode.java	2008-03-27 21:49:43 UTC (rev 5474)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/EvictedEventNode.java	2008-03-28 15:43:37 UTC (rev 5475)
@@ -23,6 +23,7 @@
    private boolean resetElementCount_;
 
    private long inUseTimeout;
+   private long creationTimestamp;
 
    public EvictedEventNode(Fqn fqn, NodeEventType type, int elementDifference)
    {
@@ -34,8 +35,14 @@
    {
       setFqn(fqn);
       setEventType(event);
+      creationTimestamp = System.currentTimeMillis();
    }
 
+   public long getCreationTimestamp()
+   {
+      return creationTimestamp;
+   }
+
    public long getInUseTimeout()
    {
       return inUseTimeout;

Modified: core/trunk/src/main/java/org/jboss/cache/eviction/ExpirationAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/ExpirationAlgorithm.java	2008-03-27 21:49:43 UTC (rev 5474)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/ExpirationAlgorithm.java	2008-03-28 15:43:37 UTC (rev 5475)
@@ -163,9 +163,9 @@
          Long ce = getExpiration(ee.getFqn());
          if (ce == null || ce > ee.getExpiration())
          {
-        	 // Expiration now older
-        	 i.remove();
-        	 continue;
+            // Expiration now older
+            i.remove();
+            continue;
          }
          if (ee.getExpiration() < now || (max != 0 && set.size() > max))
          {
@@ -179,8 +179,8 @@
       }
       if (max != 0 && max > set.size())
          log.warn("Unable to remove nodes to reduce region size below " +
-                 config.getMaxNodes() + ".  " +
-                 "Set expiration for nodes in this region");
+               config.getMaxNodes() + ".  " +
+               "Set expiration for nodes in this region");
    }
 
    @Override
@@ -257,6 +257,7 @@
          return fqn;
       }
 
+      @Override
       public boolean equals(Object o)
       {
          if (!(o instanceof ExpirationEntry))
@@ -265,11 +266,13 @@
          return expiration == ee.expiration && fqn.equals(ee.fqn);
       }
 
+      @Override
       public int hashCode()
       {
-         return (int)expiration ^ fqn.hashCode();
+         return (int) expiration ^ fqn.hashCode();
       }
 
+      @Override
       public String toString()
       {
          long now = System.currentTimeMillis();

Modified: core/trunk/src/main/java/org/jboss/cache/eviction/FIFOAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/FIFOAlgorithm.java	2008-03-27 21:49:43 UTC (rev 5474)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/FIFOAlgorithm.java	2008-03-28 15:43:37 UTC (rev 5475)
@@ -44,8 +44,6 @@
 
       int size = this.getEvictionQueue().getNumberOfNodes();
       return config.getMaxNodes() != 0 && size > config.getMaxNodes();
-
    }
-
 }
 

Modified: core/trunk/src/main/java/org/jboss/cache/eviction/LFUAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/LFUAlgorithm.java	2008-03-27 21:49:43 UTC (rev 5474)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/LFUAlgorithm.java	2008-03-28 15:43:37 UTC (rev 5475)
@@ -36,7 +36,6 @@
 {
    private static final Log log = LogFactory.getLog(LFUAlgorithm.class);
 
-
    protected boolean shouldEvictNode(NodeEntry ne)
    {
       if (log.isTraceEnabled())
@@ -74,6 +73,7 @@
       return new LFUQueue();
    }
 
+   @Override
    protected void prune() throws EvictionException
    {
       super.prune();

Modified: core/trunk/src/main/java/org/jboss/cache/eviction/LRUAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/LRUAlgorithm.java	2008-03-27 21:49:43 UTC (rev 5474)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/LRUAlgorithm.java	2008-03-28 15:43:37 UTC (rev 5475)
@@ -81,12 +81,11 @@
       return false;
    }
 
+   @Override
    protected void evict(NodeEntry ne)
    {
-//      NodeEntry ne = evictionQueue.getNodeEntry(fqn);
       if (ne != null)
       {
-//         evictionQueue.removeNodeEntry(ne);
          if (!this.evictCacheNode(ne.getFqn()))
          {
             try
@@ -101,6 +100,7 @@
       }
    }
 
+   @Override
    protected void prune() throws EvictionException
    {
       LRUQueue lruQueue = (LRUQueue) evictionQueue;

Modified: core/trunk/src/main/java/org/jboss/cache/eviction/MRUAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/MRUAlgorithm.java	2008-03-27 21:49:43 UTC (rev 5474)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/MRUAlgorithm.java	2008-03-28 15:43:37 UTC (rev 5475)
@@ -6,7 +6,6 @@
  */
 package org.jboss.cache.eviction;
 
-import org.jboss.cache.Fqn;
 import org.jboss.cache.Region;
 
 /**
@@ -32,14 +31,15 @@
       // check the minimum time to live and see if we should not evict the node.  This check will
       // ensure that, if configured, nodes are kept alive for at least a minimum period of time.
       if (isYoungerThanMinimumTimeToLive(ne)) return false;
-      
+
       MRUConfiguration config = (MRUConfiguration) region.getEvictionPolicyConfig();
       return evictionQueue.getNumberOfNodes() > config.getMaxNodes();
    }
 
-   protected void processVisitedNodes(Fqn fqn) throws EvictionException
+   @Override
+   protected void processVisitedNodes(EvictedEventNode evictedEventNode) throws EvictionException
    {
-      super.processVisitedNodes(fqn);
-      ((MRUQueue) evictionQueue).moveToTopOfStack(fqn);
+      super.processVisitedNodes(evictedEventNode);
+      ((MRUQueue) evictionQueue).moveToTopOfStack(evictedEventNode.getFqn());
    }
 }




More information about the jbosscache-commits mailing list