| 
28 | 28 | import org.elasticsearch.client.Cancellable;  | 
29 | 29 | import org.elasticsearch.client.Request;  | 
30 | 30 | import org.elasticsearch.client.Response;  | 
 | 31 | +import org.elasticsearch.cluster.ClusterState;  | 
31 | 32 | import org.elasticsearch.cluster.metadata.IndexMetadata;  | 
32 | 33 | import org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand;  | 
33 | 34 | import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider;  | 
 | 
73 | 74 | import java.util.Collection;  | 
74 | 75 | import java.util.Collections;  | 
75 | 76 | import java.util.HashMap;  | 
 | 77 | +import java.util.HashSet;  | 
76 | 78 | import java.util.List;  | 
77 | 79 | import java.util.Map;  | 
78 | 80 | import java.util.concurrent.CancellationException;  | 
@@ -591,21 +593,31 @@ public void testNoActiveCopy() throws Exception {  | 
591 | 593 | 
 
  | 
592 | 594 |     private void moveOrCloseShardsOnNodes(String nodeName) throws Exception {  | 
593 | 595 |         final IndicesService indicesService = internalCluster().getInstance(IndicesService.class, nodeName);  | 
 | 596 | +        final ClusterState clusterState = clusterService().state();  | 
594 | 597 |         for (IndexService indexService : indicesService) {  | 
595 | 598 |             for (IndexShard indexShard : indexService) {  | 
596 | 599 |                 if (randomBoolean()) {  | 
597 | 600 |                     closeShardNoCheck(indexShard, randomBoolean());  | 
598 | 601 |                 } else if (randomBoolean()) {  | 
599 | 602 |                     final ShardId shardId = indexShard.shardId();  | 
600 |  | - | 
 | 603 | +                    final var assignedNodes = new HashSet<>();  | 
 | 604 | +                    clusterState.routingTable().shardRoutingTable(shardId).allShards().forEach(shr -> {  | 
 | 605 | +                        if (shr.currentNodeId() != null) {  | 
 | 606 | +                            assignedNodes.add(shr.currentNodeId());  | 
 | 607 | +                        }  | 
 | 608 | +                        if (shr.relocatingNodeId() != null) {  | 
 | 609 | +                            assignedNodes.add(shr.relocatingNodeId());  | 
 | 610 | +                        }  | 
 | 611 | +                    });  | 
601 | 612 |                     final var targetNodes = new ArrayList<String>();  | 
602 | 613 |                     for (final var targetIndicesService : internalCluster().getInstances(IndicesService.class)) {  | 
603 | 614 |                         final var targetNode = targetIndicesService.clusterService().localNode();  | 
604 |  | -                        if (targetNode.canContainData() && targetIndicesService.getShardOrNull(shardId) == null) {  | 
 | 615 | +                        if (targetNode.canContainData()  | 
 | 616 | +                            && targetIndicesService.getShardOrNull(shardId) == null  | 
 | 617 | +                            && assignedNodes.contains(targetNode.getId()) == false) {  | 
605 | 618 |                             targetNodes.add(targetNode.getId());  | 
606 | 619 |                         }  | 
607 | 620 |                     }  | 
608 |  | - | 
609 | 621 |                     if (targetNodes.isEmpty()) {  | 
610 | 622 |                         continue;  | 
611 | 623 |                     }  | 
 | 
0 commit comments