Skip to content

Commit

Permalink
Fix relocation targets in FieldCapabilitiesIT (elastic#121606)
Browse files Browse the repository at this point in the history
The test failed because we tried to move a shard to a node that already 
has a copy. This change prevents that from happening.

Closes elastic#119280
Closes elastic#120772
  • Loading branch information
dnhatn authored and fzowl committed Feb 4, 2025
1 parent 8273d37 commit 97ceae3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 0 additions & 2 deletions muted-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,6 @@ tests:
- class: org.elasticsearch.xpack.esql.action.CrossClusterAsyncEnrichStopIT
method: testEnrichAfterStop
issue: https://github.com/elastic/elasticsearch/issues/120757
- class: org.elasticsearch.search.fieldcaps.FieldCapabilitiesIT
issue: https://github.com/elastic/elasticsearch/issues/120772
- class: org.elasticsearch.xpack.test.rest.XPackRestIT
method: test {p0=ml/3rd_party_deployment/Test start deployment fails while model download in progress}
issue: https://github.com/elastic/elasticsearch/issues/120810
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.client.Cancellable;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand;
import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider;
Expand Down Expand Up @@ -73,6 +74,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CancellationException;
Expand Down Expand Up @@ -591,21 +593,31 @@ public void testNoActiveCopy() throws Exception {

private void moveOrCloseShardsOnNodes(String nodeName) throws Exception {
final IndicesService indicesService = internalCluster().getInstance(IndicesService.class, nodeName);
final ClusterState clusterState = clusterService().state();
for (IndexService indexService : indicesService) {
for (IndexShard indexShard : indexService) {
if (randomBoolean()) {
closeShardNoCheck(indexShard, randomBoolean());
} else if (randomBoolean()) {
final ShardId shardId = indexShard.shardId();

final var assignedNodes = new HashSet<>();
clusterState.routingTable().shardRoutingTable(shardId).allShards().forEach(shr -> {
if (shr.currentNodeId() != null) {
assignedNodes.add(shr.currentNodeId());
}
if (shr.relocatingNodeId() != null) {
assignedNodes.add(shr.relocatingNodeId());
}
});
final var targetNodes = new ArrayList<String>();
for (final var targetIndicesService : internalCluster().getInstances(IndicesService.class)) {
final var targetNode = targetIndicesService.clusterService().localNode();
if (targetNode.canContainData() && targetIndicesService.getShardOrNull(shardId) == null) {
if (targetNode.canContainData()
&& targetIndicesService.getShardOrNull(shardId) == null
&& assignedNodes.contains(targetNode.getId()) == false) {
targetNodes.add(targetNode.getId());
}
}

if (targetNodes.isEmpty()) {
continue;
}
Expand Down

0 comments on commit 97ceae3

Please sign in to comment.