Skip to content

Commit

Permalink
Adding logic to clear up the disk cache files during close()
Browse files Browse the repository at this point in the history
Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com>
  • Loading branch information
sgup432 committed Mar 18, 2024
1 parent ec61cbc commit 0c6201f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.opensearch.OpenSearchException;
import org.opensearch.cache.EhcacheDiskCacheSettings;
import org.opensearch.common.SuppressForbidden;
Expand All @@ -29,6 +30,10 @@
import org.opensearch.common.unit.TimeValue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -57,6 +62,7 @@
import org.ehcache.impl.config.store.disk.OffHeapDiskStoreConfiguration;
import org.ehcache.spi.loaderwriter.CacheLoadingException;
import org.ehcache.spi.loaderwriter.CacheWritingException;
import org.opensearch.common.util.io.IOUtils;

import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_CACHE_ALIAS_KEY;
import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_CACHE_EXPIRE_AFTER_ACCESS_KEY;
Expand Down Expand Up @@ -347,7 +353,9 @@ public void invalidate(K key) {
}

@Override
public void invalidateAll() {}
public void invalidateAll() {
cache.clear();
}

/**
* Provides a way to iterate over disk cache keys.
Expand Down Expand Up @@ -378,8 +386,16 @@ public void close() {
cacheManager.close();
try {
cacheManager.destroyCache(this.diskCacheAlias);
// Delete all the disk cache related files/data
Path ehcacheDirectory = Paths.get(this.storagePath);
if (Files.exists(ehcacheDirectory)) {
IOUtils.rm(ehcacheDirectory);
}
} catch (CachePersistenceException e) {
throw new OpenSearchException("Exception occurred while destroying ehcache and associated data", e);
} catch (IOException e) {
logger.error(() -> new ParameterizedMessage("Failed to delete ehcache disk cache data under path: {}",
this.storagePath));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

package org.opensearch.cache.store.disk;

import com.carrotsearch.randomizedtesting.annotations.ThreadLeakAction;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakLingering;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakZombies;
import org.opensearch.cache.EhcacheDiskCacheSettings;
import org.opensearch.common.cache.CacheType;
import org.opensearch.common.cache.ICache;
Expand Down Expand Up @@ -38,6 +42,7 @@
import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_STORAGE_PATH_KEY;
import static org.hamcrest.CoreMatchers.instanceOf;

@ThreadLeakScope(ThreadLeakScope.Scope.TEST)
public class EhCacheDiskCacheTests extends OpenSearchSingleNodeTestCase {

private static final int CACHE_SIZE_IN_BYTES = 1024 * 101;
Expand Down Expand Up @@ -514,8 +519,10 @@ public void testEhcacheKeyIteratorWithRemove() throws IOException {
iterator.remove();
}
}
int count = 0;
// Verify the removed key doesn't exist anymore.
for (String ehcacheKey : removedKeyList) {
System.out.println("SAGARX count = " + count++);
assertNull(ehcacheTest.get(ehcacheKey));
}
// Verify ehcache entry size again.
Expand All @@ -525,6 +532,74 @@ public void testEhcacheKeyIteratorWithRemove() throws IOException {

}

public void testInvalidateAll() throws Exception {
Settings settings = Settings.builder().build();
MockRemovalListener<String, String> removalListener = new MockRemovalListener<>();
try (NodeEnvironment env = newNodeEnvironment(settings)) {
ICache<String, String> ehcacheTest = new EhcacheDiskCache.Builder<String, String>().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(removalListener)
.build();
int randomKeys = randomIntBetween(10, 100);
Map<String, String> keyValueMap = new HashMap<>();
for (int i = 0; i < randomKeys; i++) {
keyValueMap.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
}
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
ehcacheTest.put(entry.getKey(), entry.getValue());
}
ehcacheTest.invalidateAll(); // clear all the entries.
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
assertNull(ehcacheTest.get(entry.getKey()));
}
ehcacheTest.close();
}
}

public void testInvalidate() throws Exception {
Settings settings = Settings.builder().build();
MockRemovalListener<String, String> removalListener = new MockRemovalListener<>();
try (NodeEnvironment env = newNodeEnvironment(settings)) {
ICache<String, String> ehcacheTest = new EhcacheDiskCache.Builder<String, String>().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(removalListener)
.build();
int randomKeys = randomIntBetween(10, 100);
Map<String, String> keyValueMap = new HashMap<>();
for (int i = 0; i < randomKeys; i++) {
keyValueMap.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
}
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
ehcacheTest.put(entry.getKey(), entry.getValue());
}
List<String> removedKeyList = new ArrayList<>();
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
if (randomBoolean()) {
removedKeyList.add(entry.getKey());
ehcacheTest.invalidate(entry.getKey());
}
}
for (String removedKey: removedKeyList) {
assertNull(ehcacheTest.get(removedKey));
}
ehcacheTest.close();
}
}

private static String generateRandomString(int length) {
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder randomString = new StringBuilder(length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ public final class IndicesRequestCache implements RemovalListener<IndicesRequest
}

@Override
public void close() {
public void close() throws IOException {
cache.invalidateAll();
cache.close();
}

void clear(CacheEntity entity) {
Expand Down

0 comments on commit 0c6201f

Please sign in to comment.