Skip to content

Commit 2a9de3f

Browse files
Avoid creating IndexSearcher in Engine.refreshNeeded (#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 #122374
1 parent c06bde4 commit 2a9de3f

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
@@ -1214,25 +1214,29 @@ private void fillSegmentInfo(
12141214
public abstract List<Segment> segments(boolean includeVectorFormatsInfo);
12151215

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

12381242
/**

0 commit comments

Comments
 (0)