Skip to content

Commit

Permalink
[Tiered caching] Supporting removal function on EhcacheDiskCache iter…
Browse files Browse the repository at this point in the history
…ator (opensearch-project#12653)

* [Tiered caching] Supporting removal function on EhcacheDiskCache iterator

Signed-off-by: Sagar Upadhyaya <[email protected]>

* Minor refactoring in unit test

Signed-off-by: Sagar Upadhyaya <[email protected]>

---------

Signed-off-by: Sagar Upadhyaya <[email protected]>
  • Loading branch information
sgup432 authored Mar 15, 2024
1 parent ccdf3ff commit ec61cbc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ public K next() {
}
return iterator.next().getKey();
}

@Override
public void remove() {
iterator.remove(); // Calls underlying ehcache iterator.remove()
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.opensearch.test.OpenSearchSingleNodeTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -481,6 +482,49 @@ public String load(String key) throws Exception {
}
}

public void testEhcacheKeyIteratorWithRemove() throws IOException {
Settings settings = Settings.builder().build();
try (NodeEnvironment env = newNodeEnvironment(settings)) {
ICache<String, String> ehcacheTest = new EhcacheDiskCache.Builder<String, String>().setDiskCacheAlias("test1")
.setThreadPoolAlias("ehcacheTest")
.setStoragePath(env.nodePaths()[0].indicesPath.toString() + "/request_cache")
.setIsEventListenerModeSync(true)
.setKeyType(String.class)
.setValueType(String.class)
.setCacheType(CacheType.INDICES_REQUEST_CACHE)
.setSettings(settings)
.setExpireAfterAccess(TimeValue.MAX_VALUE)
.setMaximumWeightInBytes(CACHE_SIZE_IN_BYTES)
.setRemovalListener(new MockRemovalListener<>())
.build();

int randomKeys = randomIntBetween(2, 100);
for (int i = 0; i < randomKeys; i++) {
ehcacheTest.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
}
long originalSize = ehcacheTest.count();
assertEquals(randomKeys, originalSize);

// Now try removing subset of keys and verify
List<String> removedKeyList = new ArrayList<>();
for (Iterator<String> iterator = ehcacheTest.keys().iterator(); iterator.hasNext();) {
String key = iterator.next();
if (randomBoolean()) {
removedKeyList.add(key);
iterator.remove();
}
}
// Verify the removed key doesn't exist anymore.
for (String ehcacheKey : removedKeyList) {
assertNull(ehcacheTest.get(ehcacheKey));
}
// Verify ehcache entry size again.
assertEquals(originalSize - removedKeyList.size(), ehcacheTest.count());
ehcacheTest.close();
}

}

private static String generateRandomString(int length) {
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder randomString = new StringBuilder(length);
Expand Down

0 comments on commit ec61cbc

Please sign in to comment.