Author: bstansberry(a)jboss.com
Date: 2008-01-11 16:27:24 -0500 (Fri, 11 Jan 2008)
New Revision: 5119
Added:
core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionAlgorithm.java
core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionPolicy.java
core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionPolicyConfig.java
core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionQueue.java
core/trunk/src/main/resources/META-INF/local-null-eviction-service.xml
core/trunk/src/test/java/org/jboss/cache/eviction/NullEvictionConfigTest.java
core/trunk/src/test/java/org/jboss/cache/eviction/NullEvictionPolicyTest.java
Log:
[JBCACHE-1215] NullEvictionPolicy
Added: core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionAlgorithm.java
(rev 0)
+++
core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionAlgorithm.java 2008-01-11
21:27:24 UTC (rev 5119)
@@ -0,0 +1,45 @@
+/**
+ *
+ */
+package org.jboss.cache.eviction;
+
+import org.jboss.cache.Region;
+
+/**
+ * Algorithm for {@link NullEvictionPolicy}.
+ *
+ * @author Brian Stansberry
+ */
+public class NullEvictionAlgorithm implements EvictionAlgorithm
+{
+ /** Singleton instance of this class. */
+ public static final NullEvictionAlgorithm INSTANCE = new NullEvictionAlgorithm();
+
+ /**
+ * Constructs a new NullEvictionAlgorithm.
+ */
+ private NullEvictionAlgorithm()
+ {
+ }
+
+ /**
+ * Returns {@link NullEvictionQueue#INSTANCE}.
+ */
+ public EvictionQueue getEvictionQueue()
+ {
+ return NullEvictionQueue.INSTANCE;
+ }
+
+ /** No-op */
+ public void process(Region region) throws EvictionException
+ {
+ // no-op
+ }
+
+ /** No-op */
+ public void resetEvictionQueue(Region region)
+ {
+ // no-op
+ }
+
+}
Property changes on:
core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionAlgorithm.java
___________________________________________________________________
Name: svn:executable
+ *
Added: core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionPolicy.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionPolicy.java
(rev 0)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionPolicy.java 2008-01-11
21:27:24 UTC (rev 5119)
@@ -0,0 +1,69 @@
+/**
+ *
+ */
+package org.jboss.cache.eviction;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.config.EvictionPolicyConfig;
+
+/**
+ * Eviction policy that does nothing and always tells the eviction
+ * interceptor an event can be ignored, saving the overhead of
+ * constructing and processing event objects. Basically useful
+ * as a default policy for a cache or subtree that is
+ * shared between multiple usages, some of which don't
+ * want eviction.
+ *
+ * @author Brian Stansberry
+ */
+public class NullEvictionPolicy implements EvictionPolicy
+{
+ private static final Log log = LogFactory.getLog(NullEvictionPolicy.class);
+
+ private CacheSPI cache;
+
+ /**
+ * Constructs a new NullEvictionPolicy.
+ */
+ public NullEvictionPolicy()
+ {
+ }
+
+ /** Returns <code>true</code> */
+ public boolean canIgnoreEvent(Fqn fqn, NodeEventType eventType)
+ {
+ return true;
+ }
+
+ /** No-op */
+ public void evict(Fqn fqn) throws Exception
+ {
+ log.debug("evict should not be called on NullEvictionPolicy");
+ }
+
+ /** Returns {@link NullEvictionAlgorithm#INSTANCE}. */
+ public EvictionAlgorithm getEvictionAlgorithm()
+ {
+ return NullEvictionAlgorithm.INSTANCE;
+ }
+
+ /** Returns {@link NullEvictionPolicyConfig}. */
+ public Class<? extends EvictionPolicyConfig> getEvictionConfigurationClass()
+ {
+ return NullEvictionPolicyConfig.class;
+ }
+
+ public CacheSPI getCache()
+ {
+ return cache;
+ }
+
+ public void setCache(CacheSPI cache)
+ {
+ this.cache = cache;
+ }
+
+}
Property changes on:
core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionPolicy.java
___________________________________________________________________
Name: svn:executable
+ *
Added: core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionPolicyConfig.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionPolicyConfig.java
(rev 0)
+++
core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionPolicyConfig.java 2008-01-11
21:27:24 UTC (rev 5119)
@@ -0,0 +1,49 @@
+/**
+ *
+ */
+package org.jboss.cache.eviction;
+
+import org.jboss.cache.config.ConfigurationComponent;
+import org.jboss.cache.config.ConfigurationException;
+import org.jboss.cache.config.EvictionPolicyConfig;
+
+/**
+ * Configuration class for {@link NullEvictionPolicy}.
+ *
+ * @author Brian Stansberry
+ */
+public class NullEvictionPolicyConfig
+ extends ConfigurationComponent
+ implements EvictionPolicyConfig
+{
+
+ private static final long serialVersionUID = -6591180473728241737L;
+
+ /**
+ * Constructs a new NullEvictionPolicyConfig.
+ */
+ public NullEvictionPolicyConfig()
+ {
+ }
+
+ /**
+ * Returns {@link NullEvictionPolicy}.
+ */
+ public String getEvictionPolicyClass()
+ {
+ return NullEvictionPolicy.class.getName();
+ }
+
+ /** No-op */
+ public void reset()
+ {
+ // no-op
+ }
+
+ /** No-op */
+ public void validate() throws ConfigurationException
+ {
+ // no-op
+ }
+
+}
Property changes on:
core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionPolicyConfig.java
___________________________________________________________________
Name: svn:executable
+ *
Added: core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionQueue.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionQueue.java
(rev 0)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionQueue.java 2008-01-11
21:27:24 UTC (rev 5119)
@@ -0,0 +1,119 @@
+/**
+ *
+ */
+package org.jboss.cache.eviction;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import org.jboss.cache.Fqn;
+
+/**
+ * A queue that does nothing.
+ *
+ * @author Brian Stansberry
+ */
+public class NullEvictionQueue implements EvictionQueue
+{
+ /** Singleton instance of this class. */
+ public static final NullEvictionQueue INSTANCE = new NullEvictionQueue();
+
+ /**
+ * Constructs a new NullEvictionQueue.
+ */
+ private NullEvictionQueue()
+ {
+ }
+
+ /** No-op */
+ public void addNodeEntry(NodeEntry entry)
+ {
+ // no-op
+ }
+
+ /** No-op */
+ public void clear()
+ {
+ // no-op
+ }
+
+ /** Returns <code>false</code> */
+ public boolean containsNodeEntry(NodeEntry entry)
+ {
+ return false;
+ }
+
+ /** Returns <code>null</code> */
+ public NodeEntry getFirstNodeEntry()
+ {
+ return null;
+ }
+
+ /** Returns <code>null</code> */
+ public NodeEntry getNodeEntry(Fqn fqn)
+ {
+ return null;
+ }
+
+ /** Returns <code>null</code> */
+ public NodeEntry getNodeEntry(String fqn)
+ {
+ return null;
+ }
+
+ /** Returns <code>0</code> */
+ public int getNumberOfElements()
+ {
+ return 0;
+ }
+
+ /** Returns <code>0</code> */
+ public int getNumberOfNodes()
+ {
+ return 0;
+ }
+
+ /**
+ * Returns an <code>Iterator</code> whose
+ * <code>hasNext()</code> returns <code>false</code>.
+ */
+ public Iterator iterate()
+ {
+ return NullQueueIterator.INSTANCE;
+ }
+
+ /** No-op */
+ public void modifyElementCount(int difference)
+ {
+ // no-op
+ }
+
+ /** No-op */
+ public void removeNodeEntry(NodeEntry entry)
+ {
+ // no-op
+ }
+
+ static class NullQueueIterator implements Iterator
+ {
+ private static final NullQueueIterator INSTANCE = new NullQueueIterator();
+
+ private NullQueueIterator() {}
+
+ public boolean hasNext()
+ {
+ return false;
+ }
+
+ public Object next()
+ {
+ throw new NoSuchElementException("No more elements");
+ }
+
+ public void remove()
+ {
+ throw new IllegalStateException("Must call next() before remove()");
+ }
+ }
+
+}
Property changes on:
core/trunk/src/main/java/org/jboss/cache/eviction/NullEvictionQueue.java
___________________________________________________________________
Name: svn:executable
+ *
Added: core/trunk/src/main/resources/META-INF/local-null-eviction-service.xml
===================================================================
--- core/trunk/src/main/resources/META-INF/local-null-eviction-service.xml
(rev 0)
+++ core/trunk/src/main/resources/META-INF/local-null-eviction-service.xml 2008-01-11
21:27:24 UTC (rev 5119)
@@ -0,0 +1,105 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!-- ===================================================================== -->
+<!-- -->
+<!-- Sample JBoss Cache Service Configuration -->
+<!-- -->
+<!-- ===================================================================== -->
+
+<server>
+
+ <!-- ==================================================================== -->
+ <!-- Defines JBoss Cache configuration -->
+ <!-- ==================================================================== -->
+
+ <mbean code="org.jboss.cache.jmx.CacheJmxWrapper"
+ name="jboss.cache:service=Cache">
+
+ <depends>jboss:service=Naming</depends>
+ <depends>jboss:service=TransactionManager</depends>
+
+ <!--
+ Configure the TransactionManager
+ -->
+ <attribute
name="TransactionManagerLookupClass">org.jboss.cache.transaction.GenericTransactionManagerLookup
+ </attribute>
+
+
+ <!--
+ Node locking level : SERIALIZABLE
+ REPEATABLE_READ (default)
+ READ_COMMITTED
+ READ_UNCOMMITTED
+ NONE
+ -->
+ <attribute name="IsolationLevel">REPEATABLE_READ</attribute>
+
+ <!--
+ Valid modes are LOCAL
+ REPL_ASYNC
+ REPL_SYNC
+ INVALIDATION_ASYNC
+ INVALIDATION_SYNC
+ -->
+ <attribute name="CacheMode">LOCAL</attribute>
+
+ <!-- Name of cluster. Needs to be the same for all TreeCache nodes in a
+ cluster in order to find each other.
+ -->
+ <attribute name="ClusterName">JBossCache-Cluster</attribute>
+
+ <!--Uncomment next three statements to enable JGroups multiplexer.
+ This configuration is dependent on the JGroups multiplexer being
+ registered in an MBean server such as JBossAS. -->
+ <!--
+ <depends>jgroups.mux:name=Multiplexer</depends>
+ <attribute
name="MultiplexerService">jgroups.mux:name=Multiplexer</attribute>
+ <attribute
name="MultiplexerStack">fc-fast-minimalthreads</attribute>
+ -->
+
+ <!-- JGroups protocol stack properties NOT NEEDED since CacheMode is LOCAL
-->
+
+
+ <!--
+ The max amount of time (in milliseconds) we wait until the
+ state (ie. the contents of the cache) are retrieved from
+ existing members in a clustered environment
+ -->
+ <attribute name="StateRetrievalTimeout">20000</attribute>
+
+ <!--
+ Number of milliseconds to wait until all responses for a
+ synchronous call have been received.
+ -->
+ <attribute name="SyncReplTimeout">20000</attribute>
+
+ <!-- Max number of milliseconds to wait for a lock acquisition -->
+ <attribute name="LockAcquisitionTimeout">15000</attribute>
+
+ <!-- Specific eviction policy configurations. This is LRU -->
+ <attribute name="EvictionPolicyConfig">
+ <config>
+ <attribute name="wakeUpIntervalSeconds">1</attribute>
+ <!-- This defaults to 200000 if not specified -->
+ <attribute name="eventQueueSize">200000</attribute>
+ <attribute
name="policyClass">org.jboss.cache.eviction.NullEvictionPolicy</attribute>
+
+ <!-- Cache wide default -->
+ <region name="/_default_">
+ <attribute name="maxNodes">5000</attribute>
+ <attribute name="timeToLiveSeconds">1</attribute>
+ </region>
+ <region name="/test"
policyClass="org.jboss.cache.eviction.NullEvictionPolicy">
+ <attribute name="maxNodes">10000</attribute>
+ <attribute name="timeToLiveSeconds">1</attribute>
+ </region>
+ <region name="/lru"
policyClass="org.jboss.cache.eviction.LRUPolicy">
+ <attribute name="maxNodes">10000</attribute>
+ <attribute name="timeToLiveSeconds">1</attribute>
+ </region>
+ </config>
+ </attribute>
+ </mbean>
+
+
+</server>
Added: core/trunk/src/test/java/org/jboss/cache/eviction/NullEvictionConfigTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/eviction/NullEvictionConfigTest.java
(rev 0)
+++
core/trunk/src/test/java/org/jboss/cache/eviction/NullEvictionConfigTest.java 2008-01-11
21:27:24 UTC (rev 5119)
@@ -0,0 +1,87 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at
gnu.org.
+ */
+package org.jboss.cache.eviction;
+
+import static org.testng.AssertJUnit.fail;
+
+import org.jboss.cache.factories.XmlConfigurationParser;
+import org.jboss.cache.xml.XmlHelper;
+import org.testng.annotations.Test;
+import org.w3c.dom.Element;
+
+/**
+ * Unit tests for NullEvictionPolicyConfig.
+ *
+ * @author Daniel Huang (dhuang(a)jboss.org)
+ * @version $Revision: 4444 $
+ */
+@Test(groups = {"functional"})
+public class NullEvictionConfigTest
+{
+ /**
+ * Creates a bunch of region elements with LRU configs and confirms
+ * that NullEvictionPolicyConfig doesn't barf.
+ *
+ * @throws Exception
+ */
+ public void testXMLParsing() throws Exception
+ {
+ String xml =
+ "<region name=\"/org/jboss/data\">\n" +
+ "<attribute
name=\"maxNodes\">5000</attribute>\n" +
+ "<attribute
name=\"timeToLiveSeconds\">1000</attribute>\n" +
+ "</region>";
+
+ testConfigBlock(xml);
+
+ xml = "<region name=\"/maxAgeTest/\">\n" +
+ "<attribute
name=\"maxNodes\">10000</attribute>\n" +
+ "<attribute
name=\"timeToLiveSeconds\">8</attribute>\n" +
+ "<attribute
name=\"maxAgeSeconds\">10</attribute>\n" +
+ "</region>";
+
+ testConfigBlock(xml);
+
+ xml = "<region name=\"/maxAgeTest/\">\n" +
+ "<attribute
name=\"maxNodes\">10000</attribute>\n" +
+ "<attribute
name=\"maxAgeSeconds\">10</attribute>\n" +
+ "</region>";
+
+ testConfigBlock(xml);
+
+ xml = "<region name=\"/maxAgeTest/\">\n" +
+ "<attribute
name=\"timeToLiveSeconds\">8</attribute>\n" +
+ "<attribute
name=\"maxAgeSeconds\">10</attribute>\n" +
+ "</region>";
+
+ testConfigBlock(xml);
+
+ xml = "<region name=\"/maxAgeTest/\"/>\n";
+
+ testConfigBlock(xml);
+ }
+
+ /**
+ * FIXME Comment this
+ *
+ * @param xml
+ * @throws Exception
+ */
+ private void testConfigBlock(String xml) throws Exception
+ {
+ Element element = XmlHelper.stringToElement(xml);
+ NullEvictionPolicyConfig config = new NullEvictionPolicyConfig();
+ try
+ {
+ XmlConfigurationParser.parseEvictionPolicyConfig(element, config);
+ }
+ catch (Exception e)
+ {
+ fail(e.getMessage());
+ }
+ }
+}
Added: core/trunk/src/test/java/org/jboss/cache/eviction/NullEvictionPolicyTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/eviction/NullEvictionPolicyTest.java
(rev 0)
+++
core/trunk/src/test/java/org/jboss/cache/eviction/NullEvictionPolicyTest.java 2008-01-11
21:27:24 UTC (rev 5119)
@@ -0,0 +1,113 @@
+package org.jboss.cache.eviction;
+
+
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.lock.IsolationLevel;
+import org.jboss.cache.misc.TestingUtil;
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * Unit tests for LRU Policy.
+ *
+ * @author Ben Wang, Feb 11, 2004
+ * @author Daniel Huang - dhuang(a)jboss.org
+ * @version $Revision: 4880 $
+ */
+@Test(groups = {"functional"})
+public class NullEvictionPolicyTest
+{
+ CacheSPI<Object, Object> cache_;
+
+ @BeforeMethod(alwaysRun = true)
+ public void setUp() throws Exception
+ {
+ cache_ = null;
+ }
+
+ @AfterMethod(alwaysRun = true)
+ public void tearDown() throws Exception
+ {
+ if (cache_ != null)
+ {
+ cache_.stop();
+ cache_.destroy();
+ }
+ }
+
+ /**
+ * Builds a cache was null eviction by default and in "/test" region,
+ * LRU in "/lru" region. Does a mix of puts/reads/removes, wakes for
+ * eviction thread to kick in, checks that nothing was evicted from the
+ * null policy regions but was from lru region.
+ */
+ public void testEviction()
+ {
+ cache_ = (CacheSPI<Object, Object>) new
DefaultCacheFactory().createCache("META-INF/local-null-eviction-service.xml",
false);// read in generic local xml
+
cache_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
+ cache_.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
+ cache_.start();
+
+ String dfltRootStr = "/a/";
+ String testRootStr = "/test/";
+ String lruRootStr = "/lru/";
+
+ for (int i = 0; i < 20; i++)
+ {
+ if (i % 1 == 0 || i % 3 == 0)
+ {
+ Fqn dflt = Fqn.fromString(dfltRootStr + i);
+ Fqn test = Fqn.fromString(testRootStr + i);
+ Fqn lru = Fqn.fromString(lruRootStr + i);
+ cache_.put(dflt, "key", "value");
+ cache_.put(test, "key", "value");
+ cache_.put(lru, "key", "value");
+ }
+ else
+ {
+ Fqn dflt = Fqn.fromString(dfltRootStr + (i - 1));
+ Fqn test = Fqn.fromString(testRootStr + (i - 1));
+ Fqn lru = Fqn.fromString(lruRootStr + (i - 1));
+ if (i % 2 == 0)
+ {
+ assertEquals("value", cache_.get(dflt, "key"));
+ assertEquals("value", cache_.get(test, "key"));
+ assertEquals("value", cache_.get(lru, "key"));
+ }
+ else
+ {
+ cache_.removeNode(dflt);
+ cache_.removeNode(test);
+ cache_.removeNode(lru);
+ }
+ }
+ }
+
+ TestingUtil.sleepThread(2500);
+
+ for (int i = 0; i < 20; i++)
+ {
+ Fqn dflt = Fqn.fromString(dfltRootStr + i);
+ Fqn test = Fqn.fromString(testRootStr + i);
+ Fqn lru = Fqn.fromString(lruRootStr + i);
+
+ if (i % 1 == 0)
+ {
+ assertEquals("value", cache_.get(dflt, "key"));
+ assertEquals("value", cache_.get(test, "key"));
+ assertNull(cache_.get(lru, "key"));
+ }
+ else if (i % 3 == 0)
+ {
+ assertNull(cache_.get(dflt, "key"));
+ assertNull(cache_.get(test, "key"));
+ assertNull(cache_.get(lru, "key"));
+ }
+ }
+ }
+
+}