Bug in Ehcache 3 updateResourcePools

23 views
Skip to first unread message

Teresa Tang

unread,
Jul 22, 2024, 5:01:58 PM7/22/24
to ehcache-users
Hello! 

When using CacheRuntimeConfiguration::updateResourcePools, I can change the heap size from what it was created with, but then cannot revert back to the original size. The capacity variable in OnHeapStore is not updated to the original size.

I believe OnHeapStore::cacheConfigurationChange should compare the updated configuration with the new configuration, instead of the original configuration and the new configuration, to determine if capacity needs to be updated.

Please see this test below as a demonstration:
@Test
public void testCannotUndoChangedCacheSize()
{
CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();

final CacheConfiguration<Object, Object> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
ResourcePoolsBuilder.heap(5)).build();

Cache<Object, Object> cache = cacheManager.createCache("test", Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration));

//noinspection unchecked
CacheRuntimeConfiguration<Object, Object> runtimeConfiguration =
(CacheRuntimeConfiguration<Object, Object>) cache.getConfiguration(Eh107Configuration.class).unwrap(CacheRuntimeConfiguration.class);
runtimeConfiguration.updateResourcePools(ResourcePoolsBuilder.heap(10).build()); // Change cache size once

final MutableBoolean hadEvictions = new MutableBoolean(false);

runtimeConfiguration.registerCacheEventListener((e) -> hadEvictions.setTrue(),
EventOrdering.ORDERED, EventFiring.SYNCHRONOUS, Set.of(EventType.EVICTED, EventType.EXPIRED, EventType.REMOVED));

for(int i = 1; i <= 8; i++)
cache.put(i, "item"+i) ;
assertThat(getCacheSize(cache), equalTo(8));
assertThat("Unexpected evictions, removals, or expirations", hadEvictions.isFalse());

// try to reset back to original
//noinspection unchecked
((CacheRuntimeConfiguration<Object, Object>) cache.getConfiguration(Eh107Configuration.class).unwrap(CacheRuntimeConfiguration.class))
.updateResourcePools(ResourcePoolsBuilder.heap(5).build());

// we should already see evictions but we don't!
assertThat("Expect no shrinking: ehcache bug", getCacheSize(cache), equalTo(8));

cache.put(9, "item9");
assertThat("Expect no effect of reduced size: ehcache bug", getCacheSize(cache), equalTo(9));
assertThat("Expect no evictions, removals, or expirations: ehcache bug", hadEvictions.isFalse());
}

public static <K, V> int getCacheSize(Cache<K, V> cache)
{
Iterator<Cache.Entry<K, V>> cacheIterator = cache.iterator();
int size = 0;
while (cacheIterator.hasNext())
{
cacheIterator.next();
size++;
}
return size;
}


Thank you,
Teresa
Reply all
Reply to author
Forward
0 new messages