Skip to content

Commit 932a08c

Browse files
Avoid creating IndexSearcher in Engine.refreshNeeded (elastic#123218)
Checking whether we need to refresh does not require a searcher so we can simplify this to just work based on the reader and avoid lots of contention etc. for setting up the searcher. relates elastic#122374
1 parent 2aa011d commit 932a08c

File tree

1 file changed

+19
-15
lines changed
  • server/src/main/java/org/elasticsearch/index/engine

1 file changed

+19
-15
lines changed

server/src/main/java/org/elasticsearch/index/engine/Engine.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,25 +1216,29 @@ private void fillSegmentInfo(
12161216
public abstract List<Segment> segments(boolean includeVectorFormatsInfo);
12171217

12181218
public boolean refreshNeeded() {
1219-
if (store.tryIncRef()) {
1220-
/*
1221-
we need to inc the store here since we acquire a searcher and that might keep a file open on the
1222-
store. this violates the assumption that all files are closed when
1223-
the store is closed so we need to make sure we increment it here
1224-
*/
1219+
if (store.tryIncRef() == false) {
1220+
return false;
1221+
}
1222+
/*
1223+
we need to inc the store here since we acquire a directory reader and that might open a file on the store.
1224+
This violates the assumption that all files are closed when the store is closed so we need to make
1225+
sure we increment it here.
1226+
*/
1227+
try {
1228+
var refManager = getReferenceManager(SearcherScope.EXTERNAL);
1229+
var reader = refManager.acquire();
12251230
try {
1226-
try (Searcher searcher = acquireSearcher("refresh_needed", SearcherScope.EXTERNAL)) {
1227-
return searcher.getDirectoryReader().isCurrent() == false;
1228-
}
1229-
} catch (IOException e) {
1230-
logger.error("failed to access searcher manager", e);
1231-
failEngine("failed to access searcher manager", e);
1232-
throw new EngineException(shardId, "failed to access searcher manager", e);
1231+
return reader.isCurrent() == false;
12331232
} finally {
1234-
store.decRef();
1233+
refManager.release(reader);
12351234
}
1235+
} catch (IOException e) {
1236+
logger.error("failed to access directory reader", e);
1237+
failEngine("failed to access directory reader", e);
1238+
throw new EngineException(shardId, "failed to access directory reader", e);
1239+
} finally {
1240+
store.decRef();
12361241
}
1237-
return false;
12381242
}
12391243

12401244
/**

0 commit comments

Comments
 (0)