Skip to content

Commit 80f2fe8

Browse files
Make shardIndexMap in AbstractSearchAsyncAction a local variable (#117168) (#121408)
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 1af7eab commit 80f2fe8

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;
@@ -27,6 +26,7 @@
2726
import org.elasticsearch.cluster.routing.GroupShardsIterator;
2827
import org.elasticsearch.common.bytes.BytesReference;
2928
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
29+
import org.elasticsearch.common.util.Maps;
3030
import org.elasticsearch.common.util.concurrent.AtomicArray;
3131
import org.elasticsearch.core.Releasable;
3232
import org.elasticsearch.core.Releasables;
@@ -44,8 +44,7 @@
4444
import org.elasticsearch.transport.Transport;
4545

4646
import java.util.ArrayList;
47-
import java.util.Collections;
48-
import java.util.HashMap;
47+
import java.util.Arrays;
4948
import java.util.List;
5049
import java.util.Map;
5150
import java.util.concurrent.ConcurrentHashMap;
@@ -99,7 +98,6 @@ abstract class AbstractSearchAsyncAction<Result extends SearchPhaseResult> exten
9998
protected final GroupShardsIterator<SearchShardIterator> toSkipShardsIts;
10099
protected final GroupShardsIterator<SearchShardIterator> shardsIts;
101100
private final SearchShardIterator[] shardIterators;
102-
private final Map<SearchShardIterator, Integer> shardIndexMap;
103101
private final int expectedTotalOps;
104102
private final AtomicInteger totalOps = new AtomicInteger();
105103
private final int maxConcurrentRequestsPerNode;
@@ -143,17 +141,11 @@ abstract class AbstractSearchAsyncAction<Result extends SearchPhaseResult> exten
143141
this.toSkipShardsIts = new GroupShardsIterator<>(toSkipIterators);
144142
this.shardsIts = new GroupShardsIterator<>(iterators);
145143

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

158150
// 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
159151
// it's number of active shards but use 1 as the default if no replica of a shard is active at this point.
@@ -237,6 +229,10 @@ public final void run() {
237229
assert iterator.skip();
238230
skipShard(iterator);
239231
}
232+
final Map<SearchShardIterator, Integer> shardIndexMap = Maps.newHashMapWithExpectedSize(shardIterators.length);
233+
for (int i = 0; i < shardIterators.length; i++) {
234+
shardIndexMap.put(shardIterators[i], i);
235+
}
240236
if (shardsIts.size() > 0) {
241237
doCheckNoMissingShards(getName(), request, shardsIts);
242238
Version version = request.minCompatibleShardNode();

0 commit comments

Comments
 (0)