Skip to content

Commit dda3e3d

Browse files
authored
[Tiered Caching] Clear up disk cache(ehcache) files during node shutdown (opensearch-project#12734)
* Adding logic to clear up the disk cache files during close() * Adding logic to update entries count after invalidateAll() * Removing unneeded system log statement * Added comment in test for readability * Fixing issue where we were sending compacted byte[] array to ehcache but calculating size with padded byte[] --------- Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> Signed-off-by: Sagar <99425694+sgup432@users.noreply.github.com>
1 parent 902a10b commit dda3e3d

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

libs/core/src/main/java/org/opensearch/core/common/bytes/BytesReference.java

+5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ static byte[] toBytes(BytesReference reference) {
8282
return ArrayUtil.copyOfSubArray(bytesRef.bytes, bytesRef.offset, bytesRef.offset + bytesRef.length);
8383
}
8484

85+
static byte[] toBytesWithoutCompact(BytesReference reference) {
86+
final BytesRef bytesRef = reference.toBytesRef();
87+
return bytesRef.bytes;
88+
}
89+
8590
/**
8691
* Returns an array of byte buffers from the given BytesReference.
8792
*/

plugins/cache-ehcache/src/main/java/org/opensearch/cache/store/disk/EhcacheDiskCache.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
13+
import org.apache.logging.log4j.message.ParameterizedMessage;
1314
import org.opensearch.OpenSearchException;
1415
import org.opensearch.cache.EhcacheDiskCacheSettings;
1516
import org.opensearch.common.SuppressForbidden;
@@ -28,9 +29,14 @@
2829
import org.opensearch.common.settings.Setting;
2930
import org.opensearch.common.settings.Settings;
3031
import org.opensearch.common.unit.TimeValue;
32+
import org.opensearch.common.util.io.IOUtils;
3133

3234
import java.io.File;
35+
import java.io.IOException;
3336
import java.nio.ByteBuffer;
37+
import java.nio.file.Files;
38+
import java.nio.file.Path;
39+
import java.nio.file.Paths;
3440
import java.time.Duration;
3541
import java.util.Arrays;
3642
import java.util.Iterator;
@@ -363,7 +369,10 @@ public void invalidate(K key) {
363369
}
364370

365371
@Override
366-
public void invalidateAll() {}
372+
public void invalidateAll() {
373+
cache.clear();
374+
this.entries.dec(this.entries.count()); // reset to zero.
375+
}
367376

368377
/**
369378
* Provides a way to iterate over disk cache keys.
@@ -389,13 +398,21 @@ public void refresh() {
389398
}
390399

391400
@Override
401+
@SuppressForbidden(reason = "Ehcache uses File.io")
392402
public void close() {
393403
cacheManager.removeCache(this.diskCacheAlias);
394404
cacheManager.close();
395405
try {
396406
cacheManager.destroyCache(this.diskCacheAlias);
407+
// Delete all the disk cache related files/data
408+
Path ehcacheDirectory = Paths.get(this.storagePath);
409+
if (Files.exists(ehcacheDirectory)) {
410+
IOUtils.rm(ehcacheDirectory);
411+
}
397412
} catch (CachePersistenceException e) {
398413
throw new OpenSearchException("Exception occurred while destroying ehcache and associated data", e);
414+
} catch (IOException e) {
415+
logger.error(() -> new ParameterizedMessage("Failed to delete ehcache disk cache data under path: {}", this.storagePath));
399416
}
400417
}
401418

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

+35
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,41 @@ public void testEhcacheKeyIteratorWithRemove() throws IOException {
554554

555555
}
556556

557+
public void testInvalidateAll() throws Exception {
558+
Settings settings = Settings.builder().build();
559+
MockRemovalListener<String, String> removalListener = new MockRemovalListener<>();
560+
try (NodeEnvironment env = newNodeEnvironment(settings)) {
561+
ICache<String, String> ehcacheTest = new EhcacheDiskCache.Builder<String, String>().setThreadPoolAlias("ehcacheTest")
562+
.setStoragePath(env.nodePaths()[0].indicesPath.toString() + "/request_cache")
563+
.setIsEventListenerModeSync(true)
564+
.setKeyType(String.class)
565+
.setValueType(String.class)
566+
.setKeySerializer(new StringSerializer())
567+
.setValueSerializer(new StringSerializer())
568+
.setCacheType(CacheType.INDICES_REQUEST_CACHE)
569+
.setSettings(settings)
570+
.setExpireAfterAccess(TimeValue.MAX_VALUE)
571+
.setMaximumWeightInBytes(CACHE_SIZE_IN_BYTES)
572+
.setRemovalListener(removalListener)
573+
.build();
574+
int randomKeys = randomIntBetween(10, 100);
575+
Map<String, String> keyValueMap = new HashMap<>();
576+
for (int i = 0; i < randomKeys; i++) {
577+
keyValueMap.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
578+
}
579+
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
580+
ehcacheTest.put(entry.getKey(), entry.getValue());
581+
}
582+
ehcacheTest.invalidateAll(); // clear all the entries.
583+
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
584+
// Verify that value is null for a removed entry.
585+
assertNull(ehcacheTest.get(entry.getKey()));
586+
}
587+
assertEquals(0, ehcacheTest.count());
588+
ehcacheTest.close();
589+
}
590+
}
591+
557592
public void testBasicGetAndPutBytesReference() throws Exception {
558593
Settings settings = Settings.builder().build();
559594
try (NodeEnvironment env = newNodeEnvironment(settings)) {

server/src/main/java/org/opensearch/common/cache/serializer/BytesReferenceSerializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public BytesReferenceSerializer() {}
2424

2525
@Override
2626
public byte[] serialize(BytesReference object) {
27-
return BytesReference.toBytes(object);
27+
return BytesReference.toBytesWithoutCompact(object);
2828
}
2929

3030
@Override

server/src/main/java/org/opensearch/indices/IndicesRequestCache.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ public final class IndicesRequestCache implements RemovalListener<IndicesRequest
185185
}
186186

187187
@Override
188-
public void close() {
188+
public void close() throws IOException {
189189
cache.invalidateAll();
190+
cache.close();
190191
cacheCleanupManager.close();
191192
}
192193

0 commit comments

Comments
 (0)