|
10 | 10 | package org.elasticsearch.action.search;
|
11 | 11 |
|
12 | 12 | import org.apache.logging.log4j.Logger;
|
13 |
| -import org.apache.lucene.util.CollectionUtil; |
14 | 13 | import org.apache.lucene.util.SetOnce;
|
15 | 14 | import org.elasticsearch.ElasticsearchException;
|
16 | 15 | import org.elasticsearch.ExceptionsHelper;
|
|
26 | 25 | import org.elasticsearch.cluster.routing.GroupShardsIterator;
|
27 | 26 | import org.elasticsearch.common.bytes.BytesReference;
|
28 | 27 | import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
| 28 | +import org.elasticsearch.common.util.Maps; |
29 | 29 | import org.elasticsearch.common.util.concurrent.AtomicArray;
|
30 | 30 | import org.elasticsearch.core.Releasable;
|
31 | 31 | import org.elasticsearch.core.Releasables;
|
|
43 | 43 | import org.elasticsearch.transport.Transport;
|
44 | 44 |
|
45 | 45 | import java.util.ArrayList;
|
46 |
| -import java.util.Collections; |
47 |
| -import java.util.HashMap; |
| 46 | +import java.util.Arrays; |
48 | 47 | import java.util.List;
|
49 | 48 | import java.util.Map;
|
50 | 49 | import java.util.concurrent.ConcurrentHashMap;
|
@@ -98,7 +97,6 @@ abstract class AbstractSearchAsyncAction<Result extends SearchPhaseResult> exten
|
98 | 97 | protected final GroupShardsIterator<SearchShardIterator> toSkipShardsIts;
|
99 | 98 | protected final GroupShardsIterator<SearchShardIterator> shardsIts;
|
100 | 99 | private final SearchShardIterator[] shardIterators;
|
101 |
| - private final Map<SearchShardIterator, Integer> shardIndexMap; |
102 | 100 | private final int expectedTotalOps;
|
103 | 101 | private final AtomicInteger totalOps = new AtomicInteger();
|
104 | 102 | private final int maxConcurrentRequestsPerNode;
|
@@ -142,17 +140,11 @@ abstract class AbstractSearchAsyncAction<Result extends SearchPhaseResult> exten
|
142 | 140 | this.toSkipShardsIts = new GroupShardsIterator<>(toSkipIterators);
|
143 | 141 | this.shardsIts = new GroupShardsIterator<>(iterators);
|
144 | 142 |
|
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 |
146 | 145 | // that participate in the search request. This means that this number is
|
147 | 146 | // 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); |
156 | 148 |
|
157 | 149 | // 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
|
158 | 150 | // 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() {
|
236 | 228 | assert iterator.skip();
|
237 | 229 | skipShard(iterator);
|
238 | 230 | }
|
| 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 | + } |
239 | 235 | if (shardsIts.size() > 0) {
|
240 | 236 | doCheckNoMissingShards(getName(), request, shardsIts);
|
241 | 237 | for (int i = 0; i < shardsIts.size(); i++) {
|
|
0 commit comments