Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tiered caching] Supporting removal function on EhcacheDiskCache iterator #12653

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading