diff --git a/x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/shared/SharedBlobCacheService.java b/x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/shared/SharedBlobCacheService.java index 0f27d9c16a1f5..80026ca40119f 100644 --- a/x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/shared/SharedBlobCacheService.java +++ b/x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/shared/SharedBlobCacheService.java @@ -745,13 +745,23 @@ private static void throwAlreadyEvicted() { throwAlreadyClosed("File chunk is evicted"); } + /** + * Optimistically try to read from the region + * @return true if successful, i.e., not evicted and data available, false if evicted + */ boolean tryRead(ByteBuffer buf, long offset) throws IOException { - int readBytes = io.read(buf, getRegionRelativePosition(offset)); - if (isEvicted()) { - buf.position(buf.position() - readBytes); + SharedBytes.IO io = this.io; + if (io != null) { + int readBytes = io.read(buf, getRegionRelativePosition(offset)); + if (isEvicted()) { + buf.position(buf.position() - readBytes); + return false; + } + return true; + } else { + // taken by someone else return false; } - return true; } /**