From f0b40b0e6e3268ceabafeacf8cb070d420f2fc92 Mon Sep 17 00:00:00 2001 From: Peter Alfonsi Date: Mon, 18 Mar 2024 10:18:53 -0700 Subject: [PATCH] Addressed Sagar's comments Signed-off-by: Peter Alfonsi --- .../org/opensearch/cache/store/disk/EhcacheDiskCache.java | 6 +++++- .../org/opensearch/indices/IRCKeyWriteableSerializer.java | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/plugins/cache-ehcache/src/main/java/org/opensearch/cache/store/disk/EhcacheDiskCache.java b/plugins/cache-ehcache/src/main/java/org/opensearch/cache/store/disk/EhcacheDiskCache.java index 778cfc6e3e45d..d83a55e60fd2b 100644 --- a/plugins/cache-ehcache/src/main/java/org/opensearch/cache/store/disk/EhcacheDiskCache.java +++ b/plugins/cache-ehcache/src/main/java/org/opensearch/cache/store/disk/EhcacheDiskCache.java @@ -94,7 +94,7 @@ public class EhcacheDiskCache implements ICache { // A Cache manager can create many caches. private final PersistentCacheManager cacheManager; - // Disk cache. Because of a bug in ehcache, we have to store ByteArrayWrapper rather than byte[]. + // Disk cache. Using ByteArrayWrapper to compare two byte[] by values rather than the default reference checks private Cache cache; private final long maxWeightInBytes; private final String storagePath; @@ -188,6 +188,10 @@ public Duration getExpiryForUpdate(K key, Supplier o ) .withKeySerializer(new KeySerializerWrapper(keySerializer)) .withValueSerializer(new ByteArrayWrapperSerializer()) + // We pass ByteArrayWrapperSerializer as ehcache's value serializer. If V is an interface, and we pass its + // serializer directly to ehcache, ehcache requires the classes match exactly before/after serialization. + // This is not always feasible or necessary, like for BytesReference. So, we handle the value serialization + // before V hits ehcache. ); } catch (IllegalArgumentException ex) { logger.error("Ehcache disk cache initialization failed due to illegal argument: {}", ex.getMessage()); diff --git a/server/src/main/java/org/opensearch/indices/IRCKeyWriteableSerializer.java b/server/src/main/java/org/opensearch/indices/IRCKeyWriteableSerializer.java index b83957d4a2508..781f5765d8da8 100644 --- a/server/src/main/java/org/opensearch/indices/IRCKeyWriteableSerializer.java +++ b/server/src/main/java/org/opensearch/indices/IRCKeyWriteableSerializer.java @@ -26,12 +26,15 @@ public IRCKeyWriteableSerializer() {} @Override public byte[] serialize(IndicesRequestCache.Key object) { + if (object == null) { + return null; + } try { BytesStreamOutput os = new BytesStreamOutput(); object.writeTo(os); return BytesReference.toBytes(os.bytes()); } catch (IOException e) { - throw new OpenSearchException(e); + throw new OpenSearchException("Unable to serialize IndicesRequestCache.Key", e); } } @@ -44,7 +47,7 @@ public IndicesRequestCache.Key deserialize(byte[] bytes) { BytesStreamInput is = new BytesStreamInput(bytes, 0, bytes.length); return new IndicesRequestCache.Key(is); } catch (IOException e) { - throw new OpenSearchException(e); + throw new OpenSearchException("Unable to deserialize byte[] to IndicesRequestCache.Key", e); } }