Author: mircea.markus
Date: 2008-11-06 09:05:12 -0500 (Thu, 06 Nov 2008)
New Revision: 7090
Added:
core/branches/flat/src/test/java/org/jboss/starobrno/eviction/
core/branches/flat/src/test/java/org/jboss/starobrno/eviction/EvictionWatcher.java
core/branches/flat/src/test/java/org/jboss/starobrno/eviction/FIFOPolicyTest.java
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/commands/write/EvictCommand.java
Log:
ongoing eviction work
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/commands/write/EvictCommand.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/commands/write/EvictCommand.java 2008-11-06
00:31:36 UTC (rev 7089)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/commands/write/EvictCommand.java 2008-11-06
14:05:12 UTC (rev 7090)
@@ -59,12 +59,12 @@
MVCCEntry e = ctx.lookupEntry(key);
if (e != null && !e.isNullEntry())
{
+ //todo - add a actual eviction from thr container
notifier.notifyCacheEntryEvicted(key, true, ctx);
e.setDeleted(true);
e.setValid(false);
notifier.notifyCacheEntryEvicted(key, false, ctx);
}
-
return null;
}
Added: core/branches/flat/src/test/java/org/jboss/starobrno/eviction/EvictionWatcher.java
===================================================================
--- core/branches/flat/src/test/java/org/jboss/starobrno/eviction/EvictionWatcher.java
(rev 0)
+++
core/branches/flat/src/test/java/org/jboss/starobrno/eviction/EvictionWatcher.java 2008-11-06
14:05:12 UTC (rev 7090)
@@ -0,0 +1,56 @@
+package org.jboss.starobrno.eviction;
+
+import org.jboss.cache.Fqn;
+import org.jboss.starobrno.Cache;
+import org.jboss.starobrno.notifications.annotation.CacheListener;
+import org.jboss.starobrno.notifications.annotation.CacheEntryEvicted;
+import org.jboss.starobrno.notifications.event.CacheEntryEvictedEvent;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Watches and waits for eviction events
+ *
+ * @author Manik Surtani (<a href="mailto:manik AT jboss DOT org">manik
AT jboss DOT org</a>)
+ * @since 3.0
+ */
+@CacheListener
+public class EvictionWatcher
+{
+ Cache<?, ?> cache;
+ List<Object> keysToWaitFor;
+
+ public EvictionWatcher(Cache<?, ?> cache, Fqn... keysToWaitFor)
+ {
+// this(cache, Arrays.asList(keysToWaitFor));
+ }
+
+ public EvictionWatcher(Cache<?, ?> cache, List<Object> keysToWaitFor)
+ {
+ this.cache = cache;
+ this.keysToWaitFor = new ArrayList<Object>(keysToWaitFor);
+ cache.addCacheListener(this);
+ }
+
+ @CacheEntryEvicted
+ public void receive(CacheEntryEvictedEvent ee)
+ {
+ boolean xpect = false;
+ if (ee.isPre() && keysToWaitFor.contains(ee.getKey()))
+ {
+ xpect = true;
+ keysToWaitFor.remove(ee.getKey());
+ }
+
+ if (ee.isPre()) System.out.println("Saw " + ee.getKey() + " was
expecting? " + xpect);
+ }
+
+ public boolean allNodesEvicted()
+ {
+ return keysToWaitFor.isEmpty();
+ }
+}
\ No newline at end of file
Added: core/branches/flat/src/test/java/org/jboss/starobrno/eviction/FIFOPolicyTest.java
===================================================================
--- core/branches/flat/src/test/java/org/jboss/starobrno/eviction/FIFOPolicyTest.java
(rev 0)
+++
core/branches/flat/src/test/java/org/jboss/starobrno/eviction/FIFOPolicyTest.java 2008-11-06
14:05:12 UTC (rev 7090)
@@ -0,0 +1,152 @@
+package org.jboss.starobrno.eviction;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.Test;
+import static org.testng.AssertJUnit.fail;
+import static org.testng.AssertJUnit.assertNull;
+import static org.testng.AssertJUnit.assertNotNull;
+import static org.testng.AssertJUnit.assertEquals;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.lock.IsolationLevel;
+import org.jboss.cache.transaction.DummyTransactionManagerLookup;
+import org.jboss.starobrno.CacheSPI;
+import org.jboss.starobrno.UnitTestCacheFactory;
+import org.jboss.starobrno.util.TestingUtil;
+import org.jboss.starobrno.eviction.algorithms.fifo.FIFOAlgorithmConfig;
+import org.jboss.starobrno.config.EvictionConfig;
+import org.jboss.starobrno.config.Configuration;
+import org.jboss.starobrno.config.EvictionCacheConfig;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author Mircea.Markus(a)jboss.com
+ */
+@Test(groups = "functional")
+public class FIFOPolicyTest
+{
+ CacheSPI<Object, Object> cache;
+ final String CACHE_NAME = "test";
+ Throwable t1_ex, t2_ex;
+ volatile boolean isTrue;
+ int maxNodes = 5;
+
+ @BeforeMethod(alwaysRun = true)
+ public void setUp() throws Exception
+ {
+ initCaches();
+ t1_ex = t2_ex = null;
+ isTrue = true;
+ }
+
+ private void log(String s)
+ {
+ System.out.println("---log :" + s);
+ }
+
+ void initCaches() throws Exception
+ {
+
+ Configuration config = new Configuration();
+ EvictionConfig evConfig = new EvictionConfig();
+ evConfig.setWakeupInterval(-1);//external thread
+ config.setEvictionConfig(evConfig);
+ config.setCacheMode(Configuration.CacheMode.LOCAL);
+ FIFOAlgorithmConfig algorithmConfig = new FIFOAlgorithmConfig(maxNodes);
+ EvictionCacheConfig ecc = new EvictionCacheConfig(2000000, algorithmConfig,
EvictionConfig.EVICTION_ACTION_CLASS_DEFAULT);
+ config.addEvictionCacheConfig(this.CACHE_NAME, ecc);
+
config.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
+ config.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
+ cache = (CacheSPI<Object, Object>) new UnitTestCacheFactory<Object,
Object>().createCache(config, true);
+ }
+
+ @AfterMethod(alwaysRun = true)
+ public void tearDown() throws Exception
+ {
+ TestingUtil.killCaches(cache);
+ cache = null;
+ }
+
+ public void testEviction()
+ {
+ String rootStr = "root";
+ for (int i = 0; i < 10; i++)
+ {
+ String str = rootStr + i;
+ try
+ {
+ cache.put(str, str);
+ }
+ catch (Exception e)
+ {
+ fail("Failed to insert data" + e);
+ e.printStackTrace();
+ }
+ }
+ cache.getEvictionManager().runEviction();
+
+ try
+ {
+ String val = (String) cache.get(rootStr + "3");
+ assertNull("DataNode should be empty ", val);
+ assertNull(cache.get(rootStr + "1"));
+ assertNull(cache.get(rootStr + "2"));
+ assertNull(cache.get(rootStr + "0"));
+ assertNull(cache.get(rootStr + "4"));
+
+ assertNotNull(cache.get(rootStr + "5"));
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ fail("Failed to get" + e);
+ }
+ }
+
+ public void testNodeVisited() throws InterruptedException
+ {
+ String rootStr = "root";
+ List<Object> keysToBeEvicted = new ArrayList<Object>();
+ for (int i = 0; i < 5; i++) keysToBeEvicted.add(rootStr + i);
+ EvictionWatcher watcher = new EvictionWatcher(cache, keysToBeEvicted);
+
+ for (int i = 0; i < 10; i++)
+ {
+ String str = rootStr + i;
+ try
+ {
+ cache.put(str, str);
+ }
+ catch (Exception e)
+ {
+ fail("Failed to insert data" + e);
+ e.printStackTrace();
+ }
+ }
+
+ cache.getEvictionManager().runEviction();
+
+ assert watcher.allNodesEvicted();
+
+ try
+ {
+ for (int i = 0; i < 5; i++)
+ {
+ assertNull(cache.get(rootStr + i));
+ }
+ for (int i = 5; i < 10; i++)
+ {
+ assertNotNull(cache.get(rootStr + i));
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ fail("Failed to evict" + e);
+ }
+ }
+
+}
Show replies by date