Skip to content

Commit

Permalink
Addressed Sagar's comments
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
  • Loading branch information
Peter Alfonsi committed Mar 18, 2024
1 parent 847877c commit f0b40b0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public class EhcacheDiskCache<K, V> implements ICache<K, V> {
// 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<K, ByteArrayWrapper> cache;
private final long maxWeightInBytes;
private final String storagePath;
Expand Down Expand Up @@ -188,6 +188,10 @@ public Duration getExpiryForUpdate(K key, Supplier<? extends ByteArrayWrapper> o
)
.withKeySerializer(new KeySerializerWrapper<K>(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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down

0 comments on commit f0b40b0

Please sign in to comment.