Author: manik.surtani(a)jboss.com
Date: 2007-09-19 09:32:27 -0400 (Wed, 19 Sep 2007)
New Revision: 4486
Added:
core/trunk/src/test/java/org/jboss/cache/eviction/minttl/
core/trunk/src/test/java/org/jboss/cache/eviction/minttl/FIFOMinTTLTest.java
core/trunk/src/test/java/org/jboss/cache/eviction/minttl/LFUMinTTLTest.java
core/trunk/src/test/java/org/jboss/cache/eviction/minttl/LRUMinTTLTest.java
core/trunk/src/test/java/org/jboss/cache/eviction/minttl/MRUMinTTLTest.java
core/trunk/src/test/java/org/jboss/cache/eviction/minttl/MinTTLTestBase.java
Modified:
core/trunk/src/main/docbook/userguide/en/modules/eviction_policies.xml
core/trunk/src/main/docbook/userguide/en/modules/transactions.xml
core/trunk/src/main/java/org/jboss/cache/RegionManager.java
core/trunk/src/main/java/org/jboss/cache/eviction/BaseEvictionAlgorithm.java
core/trunk/src/main/java/org/jboss/cache/eviction/ElementSizeAlgorithm.java
core/trunk/src/main/java/org/jboss/cache/eviction/EvictionPolicyConfigBase.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
core/trunk/src/main/java/org/jboss/cache/eviction/NodeEntry.java
core/trunk/src/main/java/org/jboss/cache/interceptors/InvalidationInterceptor.java
core/trunk/src/test/java/org/jboss/cache/LifeCycleTest.java
core/trunk/src/test/resources/log4j.xml
Log:
JBCACHE-1155
Modified: core/trunk/src/main/docbook/userguide/en/modules/eviction_policies.xml
===================================================================
--- core/trunk/src/main/docbook/userguide/en/modules/eviction_policies.xml 2007-09-18
16:58:19 UTC (rev 4485)
+++ core/trunk/src/main/docbook/userguide/en/modules/eviction_policies.xml 2007-09-19
13:32:27 UTC (rev 4486)
@@ -36,8 +36,9 @@
</region>
<!-- override policy used for this region -->
- <region name="/org/jboss/data"
policyClass="org.jboss.cache.eviction.MRUPolicy">
+ <region name="/org/jboss/data"
policyClass="org.jboss.cache.eviction.LRUPolicy">
<attribute name="maxNodes">250</attribute>
+ <attribute name="minTimeToLiveSeconds">10</attribute>
</region>
<!-- We expect a lot of events for this region,
@@ -105,7 +106,8 @@
<literal>maxNodes</literal>
parameter which defines
how many nodes can exist in the region before it chooses to start evicting
nodes. See the javadocs for each
- policy for a list of allowed parameters.
+ policy for a list of allowed parameters. It also defines a
<literal>minTimeToLiveSeconds</literal> parameter,
+ which defines a minimum time a node must exist in memory before being
considered for eviction.
</para>
<section>
@@ -206,6 +208,12 @@
- Lifespan of a node (in seconds) regardless of idle time before the node
is swept away. 0 denotes no
limit.
</listitem>
+
+ <listitem>
+ <literal>minTimeToLiveSeconds</literal>
+ - the minimum amount of time a node must be allowed to live after being
accessed before it is allowed to
+ be considered for eviction. 0 denotes that this feature is disabled,
which is the default value.
+ </listitem>
</itemizedlist>
</section>
@@ -226,6 +234,11 @@
<literal>maxNodes</literal>
- This is the maximum number of nodes allowed in this region. 0 denotes no
limit.
</listitem>
+ <listitem>
+ <literal>minTimeToLiveSeconds</literal>
+ - the minimum amount of time a node must be allowed to live after being
accessed before it is allowed to
+ be considered for eviction. 0 denotes that this feature is disabled,
which is the default value.
+ </listitem>
</itemizedlist>
</section>
@@ -249,6 +262,11 @@
<literal>maxNodes</literal>
- This is the maximum number of nodes allowed in this region. 0 denotes no
limit.
</listitem>
+ <listitem>
+ <literal>minTimeToLiveSeconds</literal>
+ - the minimum amount of time a node must be allowed to live after being
accessed before it is allowed to
+ be considered for eviction. 0 denotes that this feature is disabled,
which is the default value.
+ </listitem>
</itemizedlist>
</section>
@@ -293,7 +311,11 @@
eviction timer makes a pass through the eviction
algorithm.
</listitem>
-
+ <listitem>
+ <literal>minTimeToLiveSeconds</literal>
+ - the minimum amount of time a node must be allowed to live after being
accessed before it is allowed to
+ be considered for eviction. 0 denotes that this feature is disabled,
which is the default value.
+ </listitem>
</itemizedlist>
</section>
@@ -342,7 +364,6 @@
<literal>maxNodes</literal>
- This is the maximum number of nodes allowed in this region. 0 denotes no
limit.
</listitem>
-
</itemizedlist>
<para>
@@ -391,6 +412,11 @@
- This is the trigger number of attributes per node for the node to be
selected for eviction. 0 denotes
no limit.
</listitem>
+ <listitem>
+ <literal>minTimeToLiveSeconds</literal>
+ - the minimum amount of time a node must be allowed to live after being
accessed before it is allowed to
+ be considered for eviction. 0 denotes that this feature is disabled,
which is the default value.
+ </listitem>
</itemizedlist>
</section>
</section>
Modified: core/trunk/src/main/docbook/userguide/en/modules/transactions.xml
===================================================================
--- core/trunk/src/main/docbook/userguide/en/modules/transactions.xml 2007-09-18 16:58:19
UTC (rev 4485)
+++ core/trunk/src/main/docbook/userguide/en/modules/transactions.xml 2007-09-19 13:32:27
UTC (rev 4486)
@@ -247,6 +247,12 @@
...
]]>
</programlisting>
+ It is generally advisable that if you have an eviction policy defined along
with optimistic locking, you define
+ the eviction policy's <literal>minTimeToLiveSeconds</literal>
parameter to be slightly greater than the transaction
+ timeout value set in your transaction manager. This ensures that data
versions in the cache are not evicted
+ while transactions are in progress<footnote>
+ <para>See <ulink
url="http://jira.jboss.com/jira/browse/JBCACHE-1155">JBCACHE...
+ </footnote>.
</section>
</section>
</section>
Modified: core/trunk/src/main/java/org/jboss/cache/RegionManager.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/RegionManager.java 2007-09-18 16:58:19 UTC
(rev 4485)
+++ core/trunk/src/main/java/org/jboss/cache/RegionManager.java 2007-09-19 13:32:27 UTC
(rev 4486)
@@ -166,15 +166,6 @@
}
return r;
}
- else
- {
- // first test if the default region has been defined. If not, and if eviction
regions
- // are in use, throw an exception since it is required.
- if (isUsingEvictions() && !regionsRegistry.containsKey(Fqn.ROOT))
- {
- throw new RuntimeException("No default eviction region defined!");
- }
- }
// else try and find a parent which has a defined region, may return null if
nothing is defined.
Region nextBestThing = null;
@@ -199,6 +190,14 @@
if (nextFqn.isRoot()) break;
}
+ // test if the default region has been defined. If not, and if eviction regions
+ // are in use, throw an exception since it is required.
+ if ((nextBestThing == null || nextBestThing.getFqn().isRoot() &&
!regionsRegistry.containsKey(Fqn.ROOT))
+ && isUsingEvictions())
+ {
+ throw new RuntimeException("No default eviction region defined!");
+ }
+
return nextBestThing;
}
Modified: core/trunk/src/main/java/org/jboss/cache/eviction/BaseEvictionAlgorithm.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/eviction/BaseEvictionAlgorithm.java 2007-09-18
16:58:19 UTC (rev 4485)
+++
core/trunk/src/main/java/org/jboss/cache/eviction/BaseEvictionAlgorithm.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -428,6 +428,7 @@
// also treat it as a node visit.
ne.setNumberOfNodeVisits(ne.getNumberOfNodeVisits() + 1);
ne.setModifiedTimeStamp(System.currentTimeMillis());
+// log.error ("*** Processing nodeAdded for fqn " + fqn + "
NodeEntry's hashcode is " + ne.hashCode());
}
@@ -529,4 +530,24 @@
" evict=" + evictionQueue.getNumberOfNodes();
}
+ /**
+ * Tests whether a node entry is younger than the minimum time to live - if one is
configured.
+ * @param entry the node entry being examined
+ * @return true if the node is younger than - or exactly equal to - the minimum time
to live, if one is configured for the given region. False otherwise.
+ */
+ protected boolean isYoungerThanMinimumTimeToLive(NodeEntry entry)
+ {
+ if (region.getEvictionPolicyConfig() instanceof EvictionPolicyConfigBase)
+ {
+ EvictionPolicyConfigBase cfg = (EvictionPolicyConfigBase)
region.getEvictionPolicyConfig();
+ int minTTL = cfg.getMinTimeToLiveSeconds();
+ return minTTL >= 1 && (entry.getModifiedTimeStamp() + (1000 * minTTL)
> System.currentTimeMillis());
+ }
+ else
+ {
+ log.trace("Eviction policy implementation does not support minimum
TTL!");
+ return false;
+ }
+ }
+
}
Modified: core/trunk/src/main/java/org/jboss/cache/eviction/ElementSizeAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/ElementSizeAlgorithm.java 2007-09-18
16:58:19 UTC (rev 4485)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/ElementSizeAlgorithm.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -22,6 +22,10 @@
protected boolean shouldEvictNode(NodeEntry ne)
{
+ // 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;
+
ElementSizeConfiguration config = (ElementSizeConfiguration)
region.getEvictionPolicyConfig();
int size = this.getEvictionQueue().getNumberOfNodes();
Modified: core/trunk/src/main/java/org/jboss/cache/eviction/EvictionPolicyConfigBase.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/eviction/EvictionPolicyConfigBase.java 2007-09-18
16:58:19 UTC (rev 4485)
+++
core/trunk/src/main/java/org/jboss/cache/eviction/EvictionPolicyConfigBase.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -22,6 +22,9 @@
@Dynamic
private int maxNodes = 0;
+ @Dynamic
+ private int minTimeToLiveSeconds = 0;
+
/**
* Can only be instantiated by a subclass.
*
@@ -53,6 +56,16 @@
testImmutability("maxNodes");
this.maxNodes = maxNodes;
}
+
+ public int getMinTimeToLiveSeconds()
+ {
+ return this.minTimeToLiveSeconds;
+ }
+
+ public void setMinTimeToLiveSeconds(int minTimeToLiveSeconds)
+ {
+ this.minTimeToLiveSeconds = minTimeToLiveSeconds;
+ }
public void validate() throws ConfigurationException
{
@@ -69,7 +82,7 @@
{
EvictionPolicyConfigBase other = (EvictionPolicyConfigBase) obj;
- return this.maxNodes == other.maxNodes
+ return this.maxNodes == other.maxNodes && this.minTimeToLiveSeconds ==
other.minTimeToLiveSeconds
&& safeEquals(this.evictionPolicyClass,
other.evictionPolicyClass);
}
@@ -81,6 +94,7 @@
{
int result = 17;
result = 31 * result + maxNodes;
+ result = 31 * result + minTimeToLiveSeconds;
result = 31 * result + (evictionPolicyClass == null ? 0 :
evictionPolicyClass.hashCode());
return result;
}
@@ -89,6 +103,7 @@
{
setEvictionPolicyClass(null);
setMaxNodes(0);
+ setMinTimeToLiveSeconds(0);
setEvictionPolicyClassName();
}
Modified: core/trunk/src/main/java/org/jboss/cache/eviction/FIFOAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/FIFOAlgorithm.java 2007-09-18
16:58:19 UTC (rev 4485)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/FIFOAlgorithm.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -37,6 +37,10 @@
*/
protected boolean shouldEvictNode(NodeEntry ne)
{
+ // 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;
+
FIFOConfiguration config = (FIFOConfiguration) region.getEvictionPolicyConfig();
if (log.isTraceEnabled())
{
Modified: core/trunk/src/main/java/org/jboss/cache/eviction/LFUAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/LFUAlgorithm.java 2007-09-18
16:58:19 UTC (rev 4485)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/LFUAlgorithm.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -49,6 +49,10 @@
log.trace("Deciding whether node in queue " + ne.getFqn() + "
requires eviction.");
}
+ // 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;
+
LFUConfiguration config = (LFUConfiguration) region.getEvictionPolicyConfig();
int size = this.getEvictionQueue().getNumberOfNodes();
if (config.getMaxNodes() != 0 && size > config.getMaxNodes())
Modified: core/trunk/src/main/java/org/jboss/cache/eviction/LRUAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/LRUAlgorithm.java 2007-09-18
16:58:19 UTC (rev 4485)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/LRUAlgorithm.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -36,6 +36,10 @@
protected boolean shouldEvictNode(NodeEntry entry)
{
+ // 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(entry)) return false;
+
LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
// no idle or max time limit
if (config.getTimeToLiveSeconds() == 0 && config.getMaxAgeSeconds() == 0)
return false;
Modified: core/trunk/src/main/java/org/jboss/cache/eviction/MRUAlgorithm.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/MRUAlgorithm.java 2007-09-18
16:58:19 UTC (rev 4485)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/MRUAlgorithm.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -29,6 +29,10 @@
protected boolean shouldEvictNode(NodeEntry ne)
{
+ // 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();
}
Modified: core/trunk/src/main/java/org/jboss/cache/eviction/NodeEntry.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/NodeEntry.java 2007-09-18 16:58:19
UTC (rev 4485)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/NodeEntry.java 2007-09-19 13:32:27
UTC (rev 4486)
@@ -1,6 +1,8 @@
package org.jboss.cache.eviction;
import org.jboss.cache.Fqn;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
/**
* Value object used in queue
@@ -20,6 +22,7 @@
private boolean currentlyInUse = false;
EvictionQueue queue;
+ private static Log log = LogFactory.getLog(NodeEntry.class);
/**
* Private constructor that automatically sets the creation time stamp of the node
entry.
@@ -78,6 +81,7 @@
public void setModifiedTimeStamp(long modifiedTimeStamp)
{
+// log.error("Being modified to " + modifiedTimeStamp, new Throwable());
this.modifiedTimeStamp = modifiedTimeStamp;
}
Modified:
core/trunk/src/main/java/org/jboss/cache/interceptors/InvalidationInterceptor.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/interceptors/InvalidationInterceptor.java 2007-09-18
16:58:19 UTC (rev 4485)
+++
core/trunk/src/main/java/org/jboss/cache/interceptors/InvalidationInterceptor.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -205,7 +205,7 @@
/**
* Same as <code>invalidateModifications(modifications, workspace,
defaultSynchronous)</code>
- * @deprecated use {@link #invalidateModifications(List<MethodCall>,
TransactionWorkspace, boolean)
+ * @deprecated use {@link #invalidateAcrossCluster(org.jboss.cache.Fqn,
org.jboss.cache.optimistic.TransactionWorkspace, boolean)}
*/
protected void invalidateModifications(List<MethodCall> modifications,
TransactionWorkspace workspace) throws Throwable
{
Modified: core/trunk/src/test/java/org/jboss/cache/LifeCycleTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/LifeCycleTest.java 2007-09-18 16:58:19 UTC
(rev 4485)
+++ core/trunk/src/test/java/org/jboss/cache/LifeCycleTest.java 2007-09-19 13:32:27 UTC
(rev 4486)
@@ -312,6 +312,27 @@
}
@Test(groups = {"functional"})
+ @SuppressWarnings("unchecked")
+ public void testStopInstanceWhileOtherInstanceSends() throws Exception
+ {
+ CacheImpl <Object, Object> c[] = new CacheImpl[2];
+ try
+ {
+ c[0] = createCache(Configuration.CacheMode.REPL_SYNC);
+ c[1] = createCache(Configuration.CacheMode.REPL_SYNC);
+
+ c[0].put()
+ }
+ finally
+ {
+ for (CacheImpl ci : c)
+ {
+ if (ci != null) ci.stop();
+ }
+ }
+ }
+
+ @Test(groups = {"functional"})
public void testInvalidStateTxRollback() throws Exception
{
CacheImpl<Object, Object> c = createCache(Configuration.CacheMode.LOCAL);
Added: core/trunk/src/test/java/org/jboss/cache/eviction/minttl/FIFOMinTTLTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/eviction/minttl/FIFOMinTTLTest.java
(rev 0)
+++
core/trunk/src/test/java/org/jboss/cache/eviction/minttl/FIFOMinTTLTest.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -0,0 +1,76 @@
+package org.jboss.cache.eviction.minttl;
+
+import org.testng.annotations.Test;
+import org.testng.annotations.AfterMethod;
+import org.jboss.cache.eviction.EvictionPolicyConfigBase;
+import org.jboss.cache.eviction.LRUConfiguration;
+import org.jboss.cache.eviction.FIFOConfiguration;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.CacheStatus;
+import org.jboss.cache.util.CachePrinter;
+import org.jboss.cache.misc.TestingUtil;
+
+/**
+ *
+ * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
+ * @since 2.1.0
+ */
+@Test(groups = {"functional"})
+public class FIFOMinTTLTest extends MinTTLTestBase
+{
+ private Fqn fqn2 = new Fqn(region, "b");
+ private Thread busyThread;
+ private boolean busyThreadRunning = true;
+
+ @Override
+ protected EvictionPolicyConfigBase getEvictionPolicyConfig()
+ {
+ startBusyThread();
+ FIFOConfiguration cfg = new FIFOConfiguration();
+ cfg.setMaxNodes(1);
+ return cfg;
+ }
+
+ @AfterMethod
+ public void stopBusyThread()
+ {
+ busyThreadRunning = false;
+ try
+ {
+ busyThread.join();
+ }
+ catch (InterruptedException e)
+ {
+ }
+ }
+
+ private void startBusyThread()
+ {
+ // start a thread to constantly put another node in the cache to make sure the
maxNodes is exceeded.
+ // this should only happen AFTER the main node is entered to guarantee FIFO.
+ busyThreadRunning = true;
+ busyThread = new Thread("BusyThread")
+ {
+ public void run()
+ {
+ try
+ {
+ cacheInitialisedLatch.await();
+ }
+ catch (InterruptedException e)
+ {
+ // do nothing
+ }
+
+ while (busyThreadRunning)
+ {
+ cache.put(fqn2, "k", "v");
+ TestingUtil.sleepRandom(150);
+ }
+ }
+ };
+
+ busyThread.setDaemon(true);
+ busyThread.start();
+ }
+}
Added: core/trunk/src/test/java/org/jboss/cache/eviction/minttl/LFUMinTTLTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/eviction/minttl/LFUMinTTLTest.java
(rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/eviction/minttl/LFUMinTTLTest.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -0,0 +1,22 @@
+package org.jboss.cache.eviction.minttl;
+
+import org.testng.annotations.Test;
+import org.jboss.cache.eviction.EvictionPolicyConfigBase;
+import org.jboss.cache.eviction.LRUConfiguration;
+import org.jboss.cache.eviction.LFUConfiguration;
+
+/**
+ *
+ * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
+ * @since 2.1.0
+ */
+@Test(groups = {"functional"})
+public class LFUMinTTLTest extends MinTTLTestBase
+{
+ @Override
+ protected EvictionPolicyConfigBase getEvictionPolicyConfig()
+ {
+ LFUConfiguration cfg = new LFUConfiguration();
+ return cfg;
+ }
+}
Added: core/trunk/src/test/java/org/jboss/cache/eviction/minttl/LRUMinTTLTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/eviction/minttl/LRUMinTTLTest.java
(rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/eviction/minttl/LRUMinTTLTest.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -0,0 +1,22 @@
+package org.jboss.cache.eviction.minttl;
+
+import org.testng.annotations.Test;
+import org.jboss.cache.eviction.EvictionPolicyConfigBase;
+import org.jboss.cache.eviction.LRUConfiguration;
+
+/**
+ *
+ * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
+ * @since 2.1.0
+ */
+@Test(groups = {"functional"})
+public class LRUMinTTLTest extends MinTTLTestBase
+{
+ @Override
+ protected EvictionPolicyConfigBase getEvictionPolicyConfig()
+ {
+ LRUConfiguration cfg = new LRUConfiguration();
+ cfg.setTimeToLiveSeconds(1);
+ return cfg;
+ }
+}
Added: core/trunk/src/test/java/org/jboss/cache/eviction/minttl/MRUMinTTLTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/eviction/minttl/MRUMinTTLTest.java
(rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/eviction/minttl/MRUMinTTLTest.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -0,0 +1,59 @@
+package org.jboss.cache.eviction.minttl;
+
+import org.testng.annotations.Test;
+import org.jboss.cache.eviction.EvictionPolicyConfigBase;
+import org.jboss.cache.eviction.LRUConfiguration;
+import org.jboss.cache.eviction.MRUConfiguration;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.CacheStatus;
+import org.jboss.cache.misc.TestingUtil;
+
+/**
+ *
+ * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
+ * @since 2.1.0
+ */
+@Test(groups = {"functional"})
+public class MRUMinTTLTest extends MinTTLTestBase
+{
+ private Fqn fqn2 = new Fqn(region, "b");
+ @Override
+ protected EvictionPolicyConfigBase getEvictionPolicyConfig()
+ {
+ MRUConfiguration cfg = new MRUConfiguration();
+ cfg.setMaxNodes(1);
+ startBusyThread();
+ return cfg;
+ }
+
+ private void startBusyThread()
+ {
+ // start a thread to constantly put another node in the cache to make sure the
maxNodes is exceeded.
+ // this should only happen AFTER the main node is entered to guarantee FIFO.
+
+ Thread busyThread = new Thread()
+ {
+ public void run()
+ {
+ while (true)
+ {
+ if (cache != null)
+ {
+ if (cache.getCacheStatus() == CacheStatus.STARTED)
+ {
+ if (cache.getRoot().hasChild(fqn))
+ {
+ cache.put(fqn2, "k", "v");
+ break;
+ }
+ }
+ }
+ TestingUtil.sleepRandom(50);
+ }
+ }
+ };
+
+ busyThread.setDaemon(true);
+ busyThread.start();
+ }
+}
Added: core/trunk/src/test/java/org/jboss/cache/eviction/minttl/MinTTLTestBase.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/eviction/minttl/MinTTLTestBase.java
(rev 0)
+++
core/trunk/src/test/java/org/jboss/cache/eviction/minttl/MinTTLTestBase.java 2007-09-19
13:32:27 UTC (rev 4486)
@@ -0,0 +1,114 @@
+package org.jboss.cache.eviction.minttl;
+
+import org.testng.annotations.Test;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.jboss.cache.Cache;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.util.CachePrinter;
+import org.jboss.cache.eviction.EvictionPolicyConfigBase;
+import org.jboss.cache.eviction.LRUConfiguration;
+import org.jboss.cache.misc.TestingUtil;
+import org.jboss.cache.config.EvictionConfig;
+import org.jboss.cache.config.EvictionRegionConfig;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.util.List;
+import java.util.LinkedList;
+import java.util.Collections;
+import java.util.concurrent.CountDownLatch;
+
+/**
+ * This test exercises the minimum time to live for any element in the cache
+ *
+ * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
+ * @since 2.1.0
+ */
+public abstract class MinTTLTestBase
+{
+ // this should ideally be in an eviction test base class so all eviction policies can
be tested
+
+ protected Cache cache;
+ protected Fqn region = Fqn.fromString("/test-region");
+ protected Fqn fqn = new Fqn(region, "a");
+ // allows the test methods to notify any support threads in subclasses that data is in
the cache and the test is about to begin
+ protected CountDownLatch cacheInitialisedLatch;
+
+ protected abstract EvictionPolicyConfigBase getEvictionPolicyConfig();
+
+ @BeforeMethod
+ public void setUp()
+ {
+ cacheInitialisedLatch = new CountDownLatch(1);
+
+ // the LRU policy cfg
+ EvictionPolicyConfigBase cfg = getEvictionPolicyConfig();
+
+ // the region configuration
+ EvictionRegionConfig regionCfg = new EvictionRegionConfig();
+ regionCfg.setRegionFqn(region);
+ regionCfg.setRegionName(region.toString());
+ regionCfg.setEvictionPolicyConfig(cfg);
+
+ // set regions in a list
+ List<EvictionRegionConfig> evictionRegionConfigs =
Collections.singletonList(regionCfg);
+
+ // cache-wide
+ EvictionConfig ec = new EvictionConfig();
+ ec.setWakeupIntervalSeconds(1);
+ ec.setEvictionRegionConfigs(evictionRegionConfigs);
+
+ cache = DefaultCacheFactory.getInstance().createCache(false);
+ cache.getConfiguration().setEvictionConfig(ec);
+ }
+
+ @AfterMethod
+ public void tearDown()
+ {
+ cache.stop();
+ }
+
+ @Test(groups = {"functional"})
+ public void testNoMinimumTTL()
+ {
+ cache.start();
+ cache.put(fqn, "k", "v");
+ // in case any waiting threads in subclasses are waiting for the cache to be
initialised
+ cacheInitialisedLatch.countDown();
+
+ assert cache.get(fqn, "k") != null : "Node should be in the
cache";
+
+ TestingUtil.sleepThread(3000);
+
+ assert cache.get(fqn, "k") == null : "Node should have been
evicted";
+ }
+
+ @Test(groups = {"functional"})
+ public void testWithMinimumTTL()
+ {
+ ((EvictionPolicyConfigBase)
cache.getConfiguration().getEvictionConfig().getEvictionRegionConfigs().get(0).getEvictionPolicyConfig()).setMinTimeToLiveSeconds(3);
+
+ cache.start();
+ cache.put(fqn, "k", "v");
+ // in case any waiting threads in subclasses are waiting for the cache to be
initialised
+ cacheInitialisedLatch.countDown();
+
+ assert cache.get(fqn, "k") != null : "Node should be in the
cache";
+
+ TestingUtil.sleepThread(3000);
+
+ assert cache.get(fqn, "k") != null : "Node should still be in cache
due to a minTTL of 3 secs";
+
+ // the last cache.get() would have updated the last modified tstamp so we need to
wait at least 3 secs (+1 sec maybe for the eviction thread)
+ // to make sure this is evicted.
+
+ TestingUtil.sleepThread(4500);
+
+ assert cache.get(fqn, "k") == null : "Node should have been
evicted";
+ }
+
+}
Modified: core/trunk/src/test/resources/log4j.xml
===================================================================
--- core/trunk/src/test/resources/log4j.xml 2007-09-18 16:58:19 UTC (rev 4485)
+++ core/trunk/src/test/resources/log4j.xml 2007-09-19 13:32:27 UTC (rev 4486)
@@ -69,7 +69,7 @@
-->
<category name="org.jboss.cache">
- <priority value="ERROR"/>
+ <priority value="INFO"/>
</category>
<category name="org.jboss.tm">
@@ -86,7 +86,7 @@
<root>
<priority value="ERROR"/>
- <!-- appender-ref ref="CONSOLE"/> -->
+ <appender-ref ref="CONSOLE"/>
<!--<appender-ref ref="FILE"/>-->
</root>