Wrong working infinispan tree cache

青春壹個敷衍的年華 提交于 2019-12-24 04:12:58

问题


I use tree cache from infinispan 8.2.6.Final. And i have next service:

public class TestService {
    public static final String FIRST_CACHE = "firstCache";

    private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    @MCacheable(value = FIRST_CACHE, key = "/#{id}/first")
    public Object putInFirstCache(String id, String data) {
        log.info("putInFirstCache");
        return data;
    }

    @MCacheable(value = FIRST_CACHE, key = "/#{id}/second")
    public Object putInSecondCache(String id, String data) {
        log.info("putInSecondCache");
        return data;
    }

    @MCacheEvict(value = FIRST_CACHE, key = "/#{id}")
    public void evictFromCache(String id) {
        log.info("evictFromCache");
    }

}

Aspect @MCacheable puts a result of method to TreeCache with fqn "key" through treeCache.put(fqn, "data", result);

Aspect @MCacheEvict remove value from cache through treeCache.removeNode(fqn);

Value in aspect is key for TreeCache in hashMap.

So, i wrote a simple test for this aspects:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CacheTestConfig.class, loader = AnnotationConfigContextLoader.class)
public class AspectTest {

    @Autowired
    private TestService testService;

    @Autowired
    private TreeCache treeCache;

    @Test
    public void testMCacheEvicts() throws InterruptedException {
        String id = UUID.randomUUID().toString();
        String data = System.currentTimeMillis() + "";

        testService.putInFirstCache(id, data);
        Thread.sleep(2000);
        testService.putInSecondCache(id, data);
        cacheDump(treeCache);

        Thread.sleep(3500);
        //miss here
        cacheDump(treeCache);
        assertEquals(null, treeCacheContainer.get(TestService.FIRST_CACHE).get(id + "/first", "data"));
        testService.evictFromCache(id);
        cacheDump(treeCache);
        assertEquals(null, treeCacheContainer.get(TestService.FIRST_CACHE).get(id + "/second", "data"));

    }

    private void cacheDump(TreeCache<String, Object> treeCache) {
        StringBuilder sb = new StringBuilder("{\r\n");
        for (Map.Entry<?, ?> entry : treeCache.getCache().entrySet()) {
            sb.append("[ ").append(entry.getKey()).append(",").append(entry.getValue()).append(" ]\r\n");
        }
        sb.append("}\r\n");

        System.out.println("Cache dump: " + sb.toString());
    }
}

Configuration of cache is:

@Bean
public DefaultCacheManager cacheManager() {
    org.infinispan.configuration.cache.Configuration cfg = new ConfigurationBuilder()
            .jmxStatistics().enable()
            .eviction().strategy(EvictionStrategy.LRU).type(EvictionType.COUNT).size(5000)
            .expiration().wakeUpInterval(2, TimeUnit.SECONDS).lifespan(5, TimeUnit.SECONDS).maxIdle(100, TimeUnit.SECONDS)
            .invocationBatching().enable()
            .build();
    return new DefaultCacheManager(cfg, true);
}

And result of this test is very strange:

Cache dump: {
[ NodeKey{DATA, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/second},AtomicHashMap{size=1} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/second},AtomicHashMap{size=0} ]
[ NodeKey{DATA, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5},AtomicHashMap{size=0} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5},AtomicHashMap{size=2} ]
[ NodeKey{DATA, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/first},AtomicHashMap{size=1} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/first},AtomicHashMap{size=0} ]
[ NodeKey{STRUCTURE, fqn=/},AtomicHashMap{size=1} ]
[ NodeKey{DATA, fqn=/},AtomicHashMap{size=0} ]
}

Cache dump: {
[ NodeKey{DATA, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/second},AtomicHashMap{size=1} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/second},AtomicHashMap{size=0} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5},AtomicHashMap{size=2} ]
}

removed: false, exists: false
Cache dump: {
[ NodeKey{DATA, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/second},AtomicHashMap{size=1} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/second},AtomicHashMap{size=0} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5},AtomicHashMap{size=2} ]
[ NodeKey{DATA, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/first},AtomicHashMap{size=0} ]
}

com.test.AspectTest > testMCacheEvicts FAILED
    java.lang.AssertionError at AspectTest.java:55

expected:<null> but was:<1489165005306>
Expected :null
Actual   :1489165005306
 <Click to see difference>

Who know why infinispan expired a root nodes of cache, if children are exists?


回答1:


The simple answer is, because there is nothing in the TreeCache implementation to deal with expiration.

The underlying cache expires each entry individually, and TreeCache would have to touch each parent's STRUCTURE and DATA entries when a child is modified to keep them alive. Alternatively, and probably easier to implement, TreeCache could make sure that all children are removed from the cache when a parent expires.



来源:https://stackoverflow.com/questions/42724342/wrong-working-infinispan-tree-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!