|
12 | 12 | import org.apache.lucene.util.CollectionUtil; |
13 | 13 | import org.elasticsearch.action.ActionListener; |
14 | 14 | import org.elasticsearch.action.FailedNodeException; |
| 15 | +import org.elasticsearch.action.admin.indices.shards.IndicesShardStoresResponse.Failure; |
| 16 | +import org.elasticsearch.action.admin.indices.shards.IndicesShardStoresResponse.StoreStatus; |
| 17 | +import org.elasticsearch.action.admin.indices.shards.IndicesShardStoresResponse.StoreStatus.AllocationStatus; |
15 | 18 | import org.elasticsearch.action.support.ActionFilters; |
16 | 19 | import org.elasticsearch.action.support.master.TransportMasterNodeReadAction; |
17 | 20 | import org.elasticsearch.action.support.nodes.BaseNodesResponse; |
|
31 | 34 | import org.elasticsearch.cluster.routing.RoutingTable; |
32 | 35 | import org.elasticsearch.cluster.routing.ShardRouting; |
33 | 36 | import org.elasticsearch.cluster.service.ClusterService; |
34 | | -import org.elasticsearch.common.collect.ImmutableOpenIntMap; |
35 | | -import org.elasticsearch.common.collect.ImmutableOpenMap; |
36 | 37 | import org.elasticsearch.common.inject.Inject; |
37 | 38 | import org.elasticsearch.common.util.concurrent.CountDown; |
38 | 39 | import org.elasticsearch.core.Tuple; |
|
47 | 48 |
|
48 | 49 | import java.util.ArrayList; |
49 | 50 | import java.util.Collections; |
| 51 | +import java.util.HashMap; |
50 | 52 | import java.util.HashSet; |
51 | 53 | import java.util.List; |
| 54 | +import java.util.Map; |
52 | 55 | import java.util.Queue; |
53 | 56 | import java.util.Set; |
54 | 57 | import java.util.concurrent.ConcurrentLinkedQueue; |
@@ -206,81 +209,50 @@ protected synchronized void processAsyncFetch( |
206 | 209 | } |
207 | 210 |
|
208 | 211 | void finish() { |
209 | | - ImmutableOpenMap.Builder< |
210 | | - String, |
211 | | - ImmutableOpenIntMap<java.util.List<IndicesShardStoresResponse.StoreStatus>>> indicesStoreStatusesBuilder = |
212 | | - ImmutableOpenMap.builder(); |
213 | | - |
214 | | - java.util.List<IndicesShardStoresResponse.Failure> failureBuilder = new ArrayList<>(); |
| 212 | + Map<String, Map<Integer, List<StoreStatus>>> indicesStatuses = new HashMap<>(); |
| 213 | + List<Failure> failures = new ArrayList<>(); |
215 | 214 | for (Response fetchResponse : fetchResponses) { |
216 | | - ImmutableOpenIntMap<java.util.List<IndicesShardStoresResponse.StoreStatus>> indexStoreStatuses = |
217 | | - indicesStoreStatusesBuilder.get(fetchResponse.shardId.getIndexName()); |
218 | | - final ImmutableOpenIntMap.Builder<java.util.List<IndicesShardStoresResponse.StoreStatus>> indexShardsBuilder; |
219 | | - if (indexStoreStatuses == null) { |
220 | | - indexShardsBuilder = ImmutableOpenIntMap.builder(); |
221 | | - } else { |
222 | | - indexShardsBuilder = ImmutableOpenIntMap.builder(indexStoreStatuses); |
223 | | - } |
224 | | - java.util.List<IndicesShardStoresResponse.StoreStatus> storeStatuses = indexShardsBuilder.get( |
225 | | - fetchResponse.shardId.id() |
226 | | - ); |
227 | | - if (storeStatuses == null) { |
228 | | - storeStatuses = new ArrayList<>(); |
229 | | - } |
230 | | - for (NodeGatewayStartedShards response : fetchResponse.responses) { |
231 | | - if (shardExistsInNode(response)) { |
232 | | - IndicesShardStoresResponse.StoreStatus.AllocationStatus allocationStatus = getAllocationStatus( |
233 | | - fetchResponse.shardId.getIndexName(), |
234 | | - fetchResponse.shardId.id(), |
235 | | - response.getNode() |
236 | | - ); |
237 | | - storeStatuses.add( |
238 | | - new IndicesShardStoresResponse.StoreStatus( |
239 | | - response.getNode(), |
240 | | - response.allocationId(), |
241 | | - allocationStatus, |
242 | | - response.storeException() |
243 | | - ) |
244 | | - ); |
| 215 | + var indexName = fetchResponse.shardId.getIndexName(); |
| 216 | + var shardId = fetchResponse.shardId.id(); |
| 217 | + var indexStatuses = indicesStatuses.computeIfAbsent(indexName, k -> new HashMap<>()); |
| 218 | + var storeStatuses = indexStatuses.computeIfAbsent(shardId, k -> new ArrayList<>()); |
| 219 | + |
| 220 | + for (NodeGatewayStartedShards r : fetchResponse.responses) { |
| 221 | + if (shardExistsInNode(r)) { |
| 222 | + var allocationStatus = getAllocationStatus(indexName, shardId, r.getNode()); |
| 223 | + storeStatuses.add(new StoreStatus(r.getNode(), r.allocationId(), allocationStatus, r.storeException())); |
245 | 224 | } |
246 | 225 | } |
247 | | - CollectionUtil.timSort(storeStatuses); |
248 | | - indexShardsBuilder.put(fetchResponse.shardId.id(), storeStatuses); |
249 | | - indicesStoreStatusesBuilder.put(fetchResponse.shardId.getIndexName(), indexShardsBuilder.build()); |
| 226 | + |
250 | 227 | for (FailedNodeException failure : fetchResponse.failures) { |
251 | | - failureBuilder.add( |
252 | | - new IndicesShardStoresResponse.Failure( |
253 | | - failure.nodeId(), |
254 | | - fetchResponse.shardId.getIndexName(), |
255 | | - fetchResponse.shardId.id(), |
256 | | - failure.getCause() |
257 | | - ) |
258 | | - ); |
| 228 | + failures.add(new Failure(failure.nodeId(), indexName, shardId, failure.getCause())); |
259 | 229 | } |
260 | 230 | } |
261 | | - listener.onResponse( |
262 | | - new IndicesShardStoresResponse(indicesStoreStatusesBuilder.build(), Collections.unmodifiableList(failureBuilder)) |
263 | | - ); |
| 231 | + // make the status structure immutable |
| 232 | + indicesStatuses.replaceAll((k, v) -> { |
| 233 | + v.replaceAll((s, l) -> { |
| 234 | + CollectionUtil.timSort(l); |
| 235 | + return List.copyOf(l); |
| 236 | + }); |
| 237 | + return Map.copyOf(v); |
| 238 | + }); |
| 239 | + listener.onResponse(new IndicesShardStoresResponse(Map.copyOf(indicesStatuses), List.copyOf(failures))); |
264 | 240 | } |
265 | 241 |
|
266 | | - private IndicesShardStoresResponse.StoreStatus.AllocationStatus getAllocationStatus( |
267 | | - String index, |
268 | | - int shardID, |
269 | | - DiscoveryNode node |
270 | | - ) { |
| 242 | + private AllocationStatus getAllocationStatus(String index, int shardID, DiscoveryNode node) { |
271 | 243 | for (ShardRouting shardRouting : routingNodes.node(node.getId())) { |
272 | 244 | ShardId shardId = shardRouting.shardId(); |
273 | 245 | if (shardId.id() == shardID && shardId.getIndexName().equals(index)) { |
274 | 246 | if (shardRouting.primary()) { |
275 | | - return IndicesShardStoresResponse.StoreStatus.AllocationStatus.PRIMARY; |
| 247 | + return AllocationStatus.PRIMARY; |
276 | 248 | } else if (shardRouting.assignedToNode()) { |
277 | | - return IndicesShardStoresResponse.StoreStatus.AllocationStatus.REPLICA; |
| 249 | + return AllocationStatus.REPLICA; |
278 | 250 | } else { |
279 | | - return IndicesShardStoresResponse.StoreStatus.AllocationStatus.UNUSED; |
| 251 | + return AllocationStatus.UNUSED; |
280 | 252 | } |
281 | 253 | } |
282 | 254 | } |
283 | | - return IndicesShardStoresResponse.StoreStatus.AllocationStatus.UNUSED; |
| 255 | + return AllocationStatus.UNUSED; |
284 | 256 | } |
285 | 257 |
|
286 | 258 | /** |
|
0 commit comments