EHC ❯ Statistics incorrect for cloned caches
-
Bug
-
Status: Closed
-
0 Showstopper
-
Resolution: Fixed
-
ehcache-core
-
-
asingh
-
Reporter: kdombeck
-
September 21, 2009
-
0
-
Watchers: 0
-
July 27, 2012
-
September 26, 2009
Attachments
Description
I am currently using ehcache 1.5 with Hibernate and upgraded to 1.6.2 but now I am getting invalid cache statistics for memoryStoreHitCount, diskStoreHitCount, missCountNotFound, etc. I do not have any specific caches configured in ehcache.xml other than the defaultCache. What I found was that Hibernate is creating the caches via CacheManger.addCache(String cacheName). This method calls clone() on the default cache. Since the statistics counters are now AtomicLong instead of just long they are not being cloned. I am guessing that this was introduced when they were converted from longs to AtomicLongs http://sourceforge.net/tracker/ViewIssue.jspa?key=2831182 (EHC-36)
Here is a unit test that shows the issue.
CacheTest {
...
@Test
public void testCloneCompleteness() throws Exception {
Cache cache = new Cache("testGetMemoryStore", 10, false, false, 100, 200);
Cache clone = (Cache) cache.clone();
clone.setName("testGetMemoryStoreClone");
manager.addCache(cache);
manager.addCache(clone);
assertFalse(cache.getGuid().equals(clone.getGuid()));
// validate updating the statistics of one cache does NOT affect a
cloned one
cache.get("notFoundKey");
assertEquals(1, cache.getStatistics().getCacheMisses());
assertEquals(0, clone.getStatistics().getCacheMisses());
}
...
}
My guess is that at a minimum the following needs to be added
Cache {
...
public final Object clone() throws CloneNotSupportedException {
...
copy.hitCount = new AtomicLong();
copy.memoryStoreHitCount = new AtomicLong();
copy.diskStoreHitCount = new AtomicLong();
copy.missCountNotFound = new AtomicLong();
copy.missCountExpired = new AtomicLong();
copy.totalGetTime = new AtomicLong();
return copy;
}
...
}
Fixed.