diff --git a/server/src/main/java/org/opensearch/index/store/remote/utils/TransferManager.java b/server/src/main/java/org/opensearch/index/store/remote/utils/TransferManager.java index 9aaab7e0aa0d4..b6eb8401f2d2f 100644 --- a/server/src/main/java/org/opensearch/index/store/remote/utils/TransferManager.java +++ b/server/src/main/java/org/opensearch/index/store/remote/utils/TransferManager.java @@ -15,7 +15,6 @@ import org.opensearch.index.store.remote.filecache.CachedIndexInput; import org.opensearch.index.store.remote.filecache.FileCache; import org.opensearch.index.store.remote.filecache.FileCachedIndexInput; -import org.opensearch.index.store.remote.utils.cache.CacheUsage; import java.io.BufferedOutputStream; import java.io.IOException; @@ -29,6 +28,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Supplier; /** * This acts as entry point to fetch {@link BlobFetchRequest} and return actual {@link IndexInput}. Utilizes the BlobContainer interface to @@ -99,7 +99,10 @@ private static FileCachedIndexInput createIndexInput(FileCache fileCache, Stream // This local file cache is ref counted and may not strictly enforce capacity. // In our use case capacity directly relates to disk usage. // If we find available capacity is exceeded deny further BlobFetchRequests. - if (fileCache.usage().usage() > fileCache.capacity()) { + Supplier cacheUsagePerc = () -> + fileCache.capacity() <= 0 ? 0 : (short) (Math.round((100d * fileCache.usage().usage()) / fileCache.capacity())); + + if (cacheUsagePerc.get() >= 99) { System.gc(); // File reference cleanup is not immediate and is processed @@ -111,7 +114,7 @@ private static FileCachedIndexInput createIndexInput(FileCache fileCache, Stream } fileCache.prune(); - if (fileCache.usage().usage() > fileCache.capacity()) { + if (cacheUsagePerc.get() >= 99) { throw new IOException("Local file cache capacity exceeded - BlobFetchRequest failed: " + request.getFilePath()); } }