Skip to content

Commit 4d98f70

Browse files
original-brownbearcbuescher
authored andcommitted
Fix O(N) list building in TransportSearchAction.asyncSearchExecutor (elastic#112474)
This at least avoids the O(N) list building which is needlessly heavy for large index counts. Not sure the logic makes perfect sense in all cases, but it should remain practically unchanged for now (except when there's more than 2 indices and they're all system ones).
1 parent cdb935a commit 4d98f70

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

server/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,16 +1285,27 @@ private void executeSearch(
12851285
}
12861286

12871287
Executor asyncSearchExecutor(final String[] indices) {
1288-
final List<String> executorsForIndices = Arrays.stream(indices).map(executorSelector::executorForSearch).toList();
1289-
if (executorsForIndices.size() == 1) { // all indices have same executor
1290-
return threadPool.executor(executorsForIndices.get(0));
1288+
boolean seenSystem = false;
1289+
boolean seenCritical = false;
1290+
for (String index : indices) {
1291+
final String executorName = executorSelector.executorForSearch(index);
1292+
switch (executorName) {
1293+
case SYSTEM_READ -> seenSystem = true;
1294+
case SYSTEM_CRITICAL_READ -> seenCritical = true;
1295+
default -> {
1296+
return threadPool.executor(executorName);
1297+
}
1298+
}
12911299
}
1292-
if (executorsForIndices.size() == 2
1293-
&& executorsForIndices.contains(SYSTEM_READ)
1294-
&& executorsForIndices.contains(SYSTEM_CRITICAL_READ)) { // mix of critical and non critical system indices
1295-
return threadPool.executor(SYSTEM_READ);
1300+
final String executor;
1301+
if (seenSystem == false && seenCritical) {
1302+
executor = SYSTEM_CRITICAL_READ;
1303+
} else if (seenSystem) {
1304+
executor = SYSTEM_READ;
1305+
} else {
1306+
executor = ThreadPool.Names.SEARCH;
12961307
}
1297-
return threadPool.executor(ThreadPool.Names.SEARCH);
1308+
return threadPool.executor(executor);
12981309
}
12991310

13001311
static BiFunction<String, String, Transport.Connection> buildConnectionLookup(

0 commit comments

Comments
 (0)