Skip to content

Commit ea608a5

Browse files
sgup432shiv0408
authored andcommitted
Fixing ehcache flaky test (opensearch-project#12764)
* Fixing ehcache flaky test Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Adding a ehcache issue reference for thread leak issue Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Updating comment Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> --------- Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> Signed-off-by: Shivansh Arora <hishiv@amazon.com>
1 parent 90d7440 commit ea608a5

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

plugins/cache-ehcache/src/test/java/org/opensearch/cache/store/disk/EhCacheDiskCacheTests.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
package org.opensearch.cache.store.disk;
1010

11+
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters;
12+
1113
import org.opensearch.cache.EhcacheDiskCacheSettings;
1214
import org.opensearch.common.Randomness;
1315
import org.opensearch.common.cache.CacheType;
@@ -47,6 +49,7 @@
4749
import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_STORAGE_PATH_KEY;
4850
import static org.hamcrest.CoreMatchers.instanceOf;
4951

52+
@ThreadLeakFilters(filters = { EhcacheThreadLeakFilter.class })
5053
public class EhCacheDiskCacheTests extends OpenSearchSingleNodeTestCase {
5154

5255
private static final int CACHE_SIZE_IN_BYTES = 1024 * 101;
@@ -633,6 +636,47 @@ public void testBasicGetAndPutBytesReference() throws Exception {
633636
}
634637
}
635638

639+
public void testInvalidate() throws Exception {
640+
Settings settings = Settings.builder().build();
641+
MockRemovalListener<String, String> removalListener = new MockRemovalListener<>();
642+
try (NodeEnvironment env = newNodeEnvironment(settings)) {
643+
ICache<String, String> ehcacheTest = new EhcacheDiskCache.Builder<String, String>().setThreadPoolAlias("ehcacheTest")
644+
.setStoragePath(env.nodePaths()[0].indicesPath.toString() + "/request_cache")
645+
.setIsEventListenerModeSync(true)
646+
.setKeyType(String.class)
647+
.setKeySerializer(new StringSerializer())
648+
.setValueSerializer(new StringSerializer())
649+
.setValueType(String.class)
650+
.setCacheType(CacheType.INDICES_REQUEST_CACHE)
651+
.setSettings(settings)
652+
.setExpireAfterAccess(TimeValue.MAX_VALUE)
653+
.setMaximumWeightInBytes(CACHE_SIZE_IN_BYTES)
654+
.setRemovalListener(removalListener)
655+
.build();
656+
int randomKeys = randomIntBetween(10, 100);
657+
Map<String, String> keyValueMap = new HashMap<>();
658+
for (int i = 0; i < randomKeys; i++) {
659+
keyValueMap.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
660+
}
661+
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
662+
ehcacheTest.put(entry.getKey(), entry.getValue());
663+
}
664+
assertEquals(keyValueMap.size(), ehcacheTest.count());
665+
List<String> removedKeyList = new ArrayList<>();
666+
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
667+
if (randomBoolean()) {
668+
removedKeyList.add(entry.getKey());
669+
ehcacheTest.invalidate(entry.getKey());
670+
}
671+
}
672+
for (String removedKey : removedKeyList) {
673+
assertNull(ehcacheTest.get(removedKey));
674+
}
675+
assertEquals(keyValueMap.size() - removedKeyList.size(), ehcacheTest.count());
676+
ehcacheTest.close();
677+
}
678+
}
679+
636680
private static String generateRandomString(int length) {
637681
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
638682
StringBuilder randomString = new StringBuilder(length);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.cache.store.disk;
10+
11+
import com.carrotsearch.randomizedtesting.ThreadFilter;
12+
13+
/**
14+
* In Ehcache(as of 3.10.8), while calling remove/invalidate() on entries causes to start a daemon thread in the
15+
* background to clean up the stale offheap memory associated with the disk cache. And this thread is not closed even
16+
* after we try to close the cache or cache manager. Considering that it requires a node restart to switch between
17+
* different cache plugins, this shouldn't be a problem for now.
18+
*
19+
* See: https://github.com/ehcache/ehcache3/issues/3204
20+
*/
21+
public class EhcacheThreadLeakFilter implements ThreadFilter {
22+
23+
private static final String OFFENDING_THREAD_NAME = "MappedByteBufferSource";
24+
25+
@Override
26+
public boolean reject(Thread t) {
27+
return t.getName().startsWith(OFFENDING_THREAD_NAME);
28+
}
29+
}

0 commit comments

Comments
 (0)