Skip to content

Commit f4bbe75

Browse files
Make shardIndexMap in AbstractSearchAsyncAction a local variable (#117168)
We only need this rather large map in `run`, lets create it on the fly there to save the rather large redundant field and save on state and complexity in general.
1 parent 4640165 commit f4bbe75

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package org.elasticsearch.action.search;
1111

1212
import org.apache.logging.log4j.Logger;
13-
import org.apache.lucene.util.CollectionUtil;
1413
import org.apache.lucene.util.SetOnce;
1514
import org.elasticsearch.ElasticsearchException;
1615
import org.elasticsearch.ExceptionsHelper;
@@ -26,6 +25,7 @@
2625
import org.elasticsearch.cluster.routing.GroupShardsIterator;
2726
import org.elasticsearch.common.bytes.BytesReference;
2827
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
28+
import org.elasticsearch.common.util.Maps;
2929
import org.elasticsearch.common.util.concurrent.AtomicArray;
3030
import org.elasticsearch.core.Releasable;
3131
import org.elasticsearch.core.Releasables;
@@ -43,8 +43,7 @@
4343
import org.elasticsearch.transport.Transport;
4444

4545
import java.util.ArrayList;
46-
import java.util.Collections;
47-
import java.util.HashMap;
46+
import java.util.Arrays;
4847
import java.util.List;
4948
import java.util.Map;
5049
import java.util.concurrent.ConcurrentHashMap;
@@ -98,7 +97,6 @@ abstract class AbstractSearchAsyncAction<Result extends SearchPhaseResult> exten
9897
protected final GroupShardsIterator<SearchShardIterator> toSkipShardsIts;
9998
protected final GroupShardsIterator<SearchShardIterator> shardsIts;
10099
private final SearchShardIterator[] shardIterators;
101-
private final Map<SearchShardIterator, Integer> shardIndexMap;
102100
private final int expectedTotalOps;
103101
private final AtomicInteger totalOps = new AtomicInteger();
104102
private final int maxConcurrentRequestsPerNode;
@@ -142,17 +140,11 @@ abstract class AbstractSearchAsyncAction<Result extends SearchPhaseResult> exten
142140
this.toSkipShardsIts = new GroupShardsIterator<>(toSkipIterators);
143141
this.shardsIts = new GroupShardsIterator<>(iterators);
144142

145-
// we compute the shard index based on the natural order of the shards
143+
this.shardIterators = iterators.toArray(new SearchShardIterator[0]);
144+
// we later compute the shard index based on the natural order of the shards
146145
// that participate in the search request. This means that this number is
147146
// consistent between two requests that target the same shards.
148-
Map<SearchShardIterator, Integer> shardMap = new HashMap<>();
149-
List<SearchShardIterator> searchIterators = new ArrayList<>(iterators);
150-
CollectionUtil.timSort(searchIterators);
151-
for (int i = 0; i < searchIterators.size(); i++) {
152-
shardMap.put(searchIterators.get(i), i);
153-
}
154-
this.shardIndexMap = Collections.unmodifiableMap(shardMap);
155-
this.shardIterators = searchIterators.toArray(SearchShardIterator[]::new);
147+
Arrays.sort(shardIterators);
156148

157149
// we need to add 1 for non active partition, since we count it in the total. This means for each shard in the iterator we sum up
158150
// it's number of active shards but use 1 as the default if no replica of a shard is active at this point.
@@ -236,6 +228,10 @@ protected final void run() {
236228
assert iterator.skip();
237229
skipShard(iterator);
238230
}
231+
final Map<SearchShardIterator, Integer> shardIndexMap = Maps.newHashMapWithExpectedSize(shardIterators.length);
232+
for (int i = 0; i < shardIterators.length; i++) {
233+
shardIndexMap.put(shardIterators[i], i);
234+
}
239235
if (shardsIts.size() > 0) {
240236
doCheckNoMissingShards(getName(), request, shardsIts);
241237
for (int i = 0; i < shardsIts.size(); i++) {

0 commit comments

Comments
 (0)