Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After much messing around, it turns out the mysterious behavior is determined by whether or not the L1 cache has any knowledge of what's in the L2 cache. The attached unit tests are meant to be executed in pairs, each element of the pair run in a separate JVM to avoid EHCache singleton-ness. </p> <p>The way I think a well-behaved L1->L2 relationship should work is that the if you do a .put() without error, you should be able to do a get() of the same key immediately after without a problem (assuming no other concurrently running threads are messing with stuff). However, with Terracotta EHCache, if that .put() requires something to be evicted, the eviction does NOT happen, and the put() is silently ignored, unless the L1 client 'knows' about the keys that can be evicted. In the *JVM2 tests, it finds out about those other keys by using .getAllWithLoader(), and then the *JVM2 tests work as expected.</p> <p>So what we have done to address our original is to make it so that periodically clients do a .getAllWithLoader(). Then we can be sure that all the eviction rules are followed.</p> <pre><code>package com.mine; import java.io.Serializable; import junit.framework.TestCase; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import net.sf.ehcache.config.CacheConfiguration; import net.sf.ehcache.config.Configuration; import net.sf.ehcache.config.TerracottaClientConfiguration; import net.sf.ehcache.config.TerracottaConfiguration; public class CacheEvictionTest extends TestCase { private static final String SERVER = "localhost:9510"; private CacheManager cacheManager; private Cache cache; private final Serializable keyA = "a"; private final Serializable keyB = "b"; private final Serializable keyC = "c"; @Override protected void setUp() throws Exception { Configuration configuration = new Configuration(); TerracottaClientConfiguration terracottaConfig = new TerracottaClientConfiguration(); terracottaConfig.setUrl(SERVER); configuration.addTerracottaConfig(terracottaConfig); int maxElementsInMemory = 1; int maxElementsOnDisk = 2; long timeToIdleSeconds = 15; long timeToLiveSeconds = 15; String cacheName = "TEST_CACHE"; CacheConfiguration amoebaCache = new CacheConfiguration(cacheName, maxElementsInMemory).statistics(true) .terracotta(new TerracottaConfiguration()) .logging(true) .maxElementsOnDisk(maxElementsOnDisk) .timeToIdleSeconds(timeToIdleSeconds) .timeToLiveSeconds(timeToLiveSeconds); configuration.addCache(amoebaCache); configuration.addDefaultCache(new CacheConfiguration("default", 0)); cacheManager = new CacheManager(configuration); cache = cacheManager.getCache(cacheName); } @Override protected void tearDown() throws Exception { if (cache != null) { cache.removeAll(); cache.clearStatistics(); } } public void testMaxElementOnDiskEvictionJVM1() throws Exception { cache.clearStatistics(); cache.put(new Element(keyA, keyA)); cache.put(new Element(keyB, keyB)); cache = null; } public void testMaxElementOnDiskEvictionJVM2() throws Exception { assertEquals(2, cache.getSize()); for (Object key : cache.getKeys()) { cache.get(key; } cache.put(new Element(keyC, keyC)); assertEquals(2, cache.getSize()); assertNotNull(cache.get(keyC)); } public void testEvictsExpiredElementsFromDiskWhenNotInMemoryAndWeNeverKnewAboutItJVM1() throws Exception { cache.clearStatistics(); cache.put(new Element(keyA, keyA)); cache = null; cacheManager = null; } public void testEvictsExpiredElementsFromDiskWhenNotInMemoryAndWeNeverKnewAboutItJVM2() throws Exception { cache.clearStatistics(); for (Object key : cache.getKeys()) { cache.get(key; } assertEquals(0, cache.getStatistics().getEvictionCount()); Thread.sleep(20000); assertEquals(1, cache.getStatistics().getEvictionCount()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload