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

Elias Ross genman at noderunner.net
Fri Dec 8 14:19:05 EST 2006


  User: genman  
  Date: 06/12/08 14:19:05

  Modified:    src/org/jboss/cache/eviction    ExpirationPolicy.java
                        ExpirationConfiguration.java
                        ExpirationAlgorithm.java
  Log:
  JBCACHE-880 - clean up expiration policy (still a work in progress)
  
  Revision  Changes    Path
  1.2       +35 -26    JBossCache/src/org/jboss/cache/eviction/ExpirationPolicy.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ExpirationPolicy.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/eviction/ExpirationPolicy.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- ExpirationPolicy.java	27 Nov 2006 04:51:24 -0000	1.1
  +++ ExpirationPolicy.java	8 Dec 2006 19:19:05 -0000	1.2
  @@ -1,5 +1,7 @@
   package org.jboss.cache.eviction;
   
  +import org.jboss.cache.Fqn;
  +
   /**
    * Returns the {@link ExpirationAlgorithm} as the policy's algorithm.
    * @author rosse
  @@ -23,4 +25,11 @@
         return ExpirationConfiguration.class;
      }
   
  +   /**
  +    * Returns true if it's a visit node event. 
  +    */
  +   @Override
  +   public boolean canIgnoreEvent(Fqn fqn, NodeEventType eventType) {
  +      return (eventType == NodeEventType.VISIT_NODE_EVENT);
  +   }
   }
  
  
  
  1.2       +82 -69    JBossCache/src/org/jboss/cache/eviction/ExpirationConfiguration.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ExpirationConfiguration.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/eviction/ExpirationConfiguration.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- ExpirationConfiguration.java	27 Nov 2006 04:51:24 -0000	1.1
  +++ ExpirationConfiguration.java	8 Dec 2006 19:19:05 -0000	1.2
  @@ -22,6 +22,9 @@
      @Dynamic
      private boolean warnNoExpirationKey = true;
   
  +   @Dynamic
  +   private int timeToLiveSeconds = 0;
  +
      public ExpirationConfiguration()
      {
         setEvictionPolicyClassName();
  @@ -66,4 +69,14 @@
         this.warnNoExpirationKey = warnNoExpirationKey;
      }
   
  +   public int getTimeToLiveSeconds()
  +   {
  +      return timeToLiveSeconds;
  +   }
  +
  +   public void setTimeToLiveSeconds(int timeToLiveSeconds)
  +   {
  +      this.timeToLiveSeconds = timeToLiveSeconds;
  +   }   
  +
   }
  
  
  
  1.3       +68 -8     JBossCache/src/org/jboss/cache/eviction/ExpirationAlgorithm.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ExpirationAlgorithm.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/eviction/ExpirationAlgorithm.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- ExpirationAlgorithm.java	27 Nov 2006 19:59:11 -0000	1.2
  +++ ExpirationAlgorithm.java	8 Dec 2006 19:19:05 -0000	1.3
  @@ -11,12 +11,53 @@
   import org.jboss.cache.optimistic.FqnComparator;
   
   /**
  - * Eviction algorithm that uses a key in the Node data that indicates the time the node should
  - * be evicted.
  - * This algorithm also considers {@link ExpirationConfiguration#getMaxNodes()}, and 
  - * will remove the most soon to expire entires first.
  + * Eviction algorithm that uses a key in the Node data that indicates the time
  + * the node should be evicted.  The key must be a java.lang.Long object, with
  + * the time to expire as milliseconds past midnight January 1st, 1970 UTC (the
  + * same relative time as provided by {@link
  + * java.lang.System#currentTimeMillis()}).
    * <p/>
  - * Note that if the expiration key is absent, the node will not be evicted.
  + * Alternatively to setting the Node expiration key value, calling {@link
  + * Region#markNodeCurrentlyInUse} will cause the node to expire once the
  + * timeout is reached. Note that, because the expiration value is not stored on
  + * the node, the expiration time cannot be persisted or replicated.  Calling
  + * {@link Region#unmarkNodeCurrentlyInUse} unsets the expiration value.
  + * <p/>
  + * This algorithm also obeys the configuration key {@link
  + * ExpirationConfiguration#getMaxNodes()}, and will evict the soonest to
  + * expire entires first to reduce the region size.  If there are not enough
  + * nodes with expiration keys set, a warning is logged.
  + * <p/>
  + * If a node in the eviction region does not have an expiration value, then
  + * {@link ExpirationConfiguration#getTimeToLiveSeconds} (if set) will be used.
  + * The expiration is updated when a node is added or updated.
  + * <p/>
  + * If there is no time-to-live set, and a node in the eviction region does not
  + * have an expiration value, then that node will never be evicted.  As
  + * forgetting to indicate an expiration value is likely a mistake, a warning
  + * message is logged by this class. This warning, however, can be disabled
  + * through {@link ExpirationConfiguration#setWarnNoExpirationKey(boolean)}/
  + * <p/>
  + * A node's expiration time can be changed by setting a new value in the node.
  + * <p/>
  + * Example usage:
  + <pre>
  + Cache cache;
  + Fqn fqn1 = Fqn.fromString("/node/1");
  + Long future = Long.valueOf(System.currentTimeMillis() + 2000);
  + cache.put(fqn1, ExpirationConfiguration.DEFAULT_EXPIRATION_KEY, future);
  + cache.put(fqn1, "foo");
  + assertTrue(cache.get(fqn1) != null);
  + 
  + Thread.sleep(5000); // 5 seconds
  + assertTrue(cache.get(fqn1) == null);
  +
  + // Alternatively, this expires a node in one second
  + cache.put(fqn1, "foo");
  + cache.getRegion(fqn1, false).markNodeCurrentlyInUse(fqn1, 1000);
  + Thread.sleep(2000); // 5 seconds
  + assertTrue(cache.get(fqn1) == null);
  + </pre>
    */
   public class ExpirationAlgorithm extends BaseEvictionAlgorithm
   {
  @@ -53,8 +94,15 @@
               log.warn("No expiration key '" + config.getExpirationKeyName() + "' for Node: " + fqn);
            else if (log.isDebugEnabled())
               log.debug("No expiration key for Node: " + fqn);
  -         return;
         }
  +      else
  +      {
  +         setExpiration(fqn, l);
  +      }
  +   }
  +   
  +   private void setExpiration(Fqn fqn, Long l)
  +   {
         boolean found = set.remove(new ExpirationEntry(fqn));
         if (found && log.isTraceEnabled())
            log.trace("removed old expiration for " + fqn);
  @@ -88,12 +136,14 @@
                  break;
               case REMOVE_ELEMENT_EVENT :
               case REMOVE_NODE_EVENT :
  +            case UNMARK_USE_EVENT :
                  removeEvictionEntry(node);
                  break;
               case VISIT_NODE_EVENT :
  +               // unused
  +               break;
               case MARK_IN_USE_EVENT :
  -            case UNMARK_USE_EVENT :
  -               // TODO
  +               markInUse(node);
                  break;
               default :
                  throw new RuntimeException("Illegal Eviction Event type " + node.getEventType());
  @@ -106,6 +156,12 @@
         }
      }
   
  +   private void markInUse(EvictedEventNode node)
  +   {
  +      long expiration = node.getInUseTimeout() + System.currentTimeMillis();
  +      setExpiration(node.getFqn(), new Long(expiration));
  +   }
  +
      @Override
      protected void prune() throws EvictionException
      {
  @@ -126,6 +182,10 @@
               break;
            }
         }
  +	  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");
      }
   
      private void removeEvictionEntry(EvictedEventNode node)
  
  
  



More information about the jboss-cvs-commits mailing list