diff --git a/server/src/main/java/org/elasticsearch/index/engine/Engine.java b/server/src/main/java/org/elasticsearch/index/engine/Engine.java index 0589741a70281..3298d8757ca92 100644 --- a/server/src/main/java/org/elasticsearch/index/engine/Engine.java +++ b/server/src/main/java/org/elasticsearch/index/engine/Engine.java @@ -1214,25 +1214,29 @@ private void fillSegmentInfo( public abstract List segments(boolean includeVectorFormatsInfo); public boolean refreshNeeded() { - if (store.tryIncRef()) { - /* - we need to inc the store here since we acquire a searcher and that might keep a file open on the - store. this violates the assumption that all files are closed when - the store is closed so we need to make sure we increment it here - */ + if (store.tryIncRef() == false) { + return false; + } + /* + we need to inc the store here since we acquire a directory reader and that might open a file on the store. + This violates the assumption that all files are closed when the store is closed so we need to make + sure we increment it here. + */ + try { + var refManager = getReferenceManager(SearcherScope.EXTERNAL); + var reader = refManager.acquire(); try { - try (Searcher searcher = acquireSearcher("refresh_needed", SearcherScope.EXTERNAL)) { - return searcher.getDirectoryReader().isCurrent() == false; - } - } catch (IOException e) { - logger.error("failed to access searcher manager", e); - failEngine("failed to access searcher manager", e); - throw new EngineException(shardId, "failed to access searcher manager", e); + return reader.isCurrent() == false; } finally { - store.decRef(); + refManager.release(reader); } + } catch (IOException e) { + logger.error("failed to access directory reader", e); + failEngine("failed to access directory reader", e); + throw new EngineException(shardId, "failed to access directory reader", e); + } finally { + store.decRef(); } - return false; } /**