|
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; |
|
27 | 26 | import org.elasticsearch.cluster.routing.GroupShardsIterator; |
28 | 27 | import org.elasticsearch.common.bytes.BytesReference; |
29 | 28 | import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 29 | +import org.elasticsearch.common.util.Maps; |
30 | 30 | import org.elasticsearch.common.util.concurrent.AtomicArray; |
31 | 31 | import org.elasticsearch.core.Releasable; |
32 | 32 | import org.elasticsearch.core.Releasables; |
|
44 | 44 | import org.elasticsearch.transport.Transport; |
45 | 45 |
|
46 | 46 | import java.util.ArrayList; |
47 | | -import java.util.Collections; |
48 | | -import java.util.HashMap; |
| 47 | +import java.util.Arrays; |
49 | 48 | import java.util.List; |
50 | 49 | import java.util.Map; |
51 | 50 | import java.util.concurrent.ConcurrentHashMap; |
@@ -99,7 +98,6 @@ abstract class AbstractSearchAsyncAction<Result extends SearchPhaseResult> exten |
99 | 98 | protected final GroupShardsIterator<SearchShardIterator> toSkipShardsIts; |
100 | 99 | protected final GroupShardsIterator<SearchShardIterator> shardsIts; |
101 | 100 | private final SearchShardIterator[] shardIterators; |
102 | | - private final Map<SearchShardIterator, Integer> shardIndexMap; |
103 | 101 | private final int expectedTotalOps; |
104 | 102 | private final AtomicInteger totalOps = new AtomicInteger(); |
105 | 103 | private final int maxConcurrentRequestsPerNode; |
@@ -143,17 +141,11 @@ abstract class AbstractSearchAsyncAction<Result extends SearchPhaseResult> exten |
143 | 141 | this.toSkipShardsIts = new GroupShardsIterator<>(toSkipIterators); |
144 | 142 | this.shardsIts = new GroupShardsIterator<>(iterators); |
145 | 143 |
|
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 |
147 | 146 | // that participate in the search request. This means that this number is |
148 | 147 | // 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); |
157 | 149 |
|
158 | 150 | // 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 |
159 | 151 | // 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() { |
237 | 229 | assert iterator.skip(); |
238 | 230 | skipShard(iterator); |
239 | 231 | } |
| 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 | + } |
240 | 236 | if (shardsIts.size() > 0) { |
241 | 237 | doCheckNoMissingShards(getName(), request, shardsIts); |
242 | 238 | Version version = request.minCompatibleShardNode(); |
|
0 commit comments