Search in sources :

Example 1 with ShardsIterator

use of org.opensearch.cluster.routing.ShardsIterator in project OpenSearch by opensearch-project.

the class TransportBroadcastByNodeActionTests method testRequestsAreNotSentToFailedMaster.

// simulate the master being removed from the cluster but before a new master is elected
// as such, the shards assigned to the master will still show up in the cluster state as assigned to a node but
// that node will not be in the local cluster state on any node that has detected the master as failing
// in this case, such a shard should be treated as unassigned
public void testRequestsAreNotSentToFailedMaster() {
    Request request = new Request(new String[] { TEST_INDEX });
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    DiscoveryNode masterNode = clusterService.state().nodes().getMasterNode();
    DiscoveryNodes.Builder builder = DiscoveryNodes.builder(clusterService.state().getNodes());
    builder.remove(masterNode.getId());
    setState(clusterService, ClusterState.builder(clusterService.state()).nodes(builder));
    action.new AsyncAction(null, request, listener).start();
    Map<String, List<CapturingTransport.CapturedRequest>> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear();
    // the master should not be in the list of nodes that requests were sent to
    ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { TEST_INDEX });
    Set<String> set = new HashSet<>();
    for (ShardRouting shard : shardIt) {
        if (!shard.currentNodeId().equals(masterNode.getId())) {
            set.add(shard.currentNodeId());
        }
    }
    // check a request was sent to the right number of nodes
    assertEquals(set.size(), capturedRequests.size());
    // check requests were sent to the right nodes
    assertEquals(set, capturedRequests.keySet());
    for (Map.Entry<String, List<CapturingTransport.CapturedRequest>> entry : capturedRequests.entrySet()) {
        // check one request was sent to each non-master node
        assertEquals(1, entry.getValue().size());
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) CapturingTransport(org.opensearch.test.transport.CapturingTransport) BroadcastRequest(org.opensearch.action.support.broadcast.BroadcastRequest) IndicesRequest(org.opensearch.action.IndicesRequest) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) ShardsIterator(org.opensearch.cluster.routing.ShardsIterator) TransportResponse(org.opensearch.transport.TransportResponse) BroadcastResponse(org.opensearch.action.support.broadcast.BroadcastResponse) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) List(java.util.List) ArrayList(java.util.ArrayList) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet)

Example 2 with ShardsIterator

use of org.opensearch.cluster.routing.ShardsIterator in project OpenSearch by opensearch-project.

the class TransportBroadcastByNodeActionTests method testResultAggregation.

public void testResultAggregation() throws ExecutionException, InterruptedException {
    Request request = new Request(new String[] { TEST_INDEX });
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    // simulate removing the master
    final boolean simulateFailedMasterNode = rarely();
    DiscoveryNode failedMasterNode = null;
    if (simulateFailedMasterNode) {
        failedMasterNode = clusterService.state().nodes().getMasterNode();
        DiscoveryNodes.Builder builder = DiscoveryNodes.builder(clusterService.state().getNodes());
        builder.remove(failedMasterNode.getId());
        builder.masterNodeId(null);
        setState(clusterService, ClusterState.builder(clusterService.state()).nodes(builder));
    }
    action.new AsyncAction(null, request, listener).start();
    Map<String, List<CapturingTransport.CapturedRequest>> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear();
    ShardsIterator shardIt = clusterService.state().getRoutingTable().allShards(new String[] { TEST_INDEX });
    Map<String, List<ShardRouting>> map = new HashMap<>();
    for (ShardRouting shard : shardIt) {
        if (!map.containsKey(shard.currentNodeId())) {
            map.put(shard.currentNodeId(), new ArrayList<>());
        }
        map.get(shard.currentNodeId()).add(shard);
    }
    int totalShards = 0;
    int totalSuccessfulShards = 0;
    int totalFailedShards = 0;
    for (Map.Entry<String, List<CapturingTransport.CapturedRequest>> entry : capturedRequests.entrySet()) {
        List<BroadcastShardOperationFailedException> exceptions = new ArrayList<>();
        long requestId = entry.getValue().get(0).requestId;
        if (rarely()) {
            // simulate node failure
            totalShards += map.get(entry.getKey()).size();
            totalFailedShards += map.get(entry.getKey()).size();
            transport.handleRemoteError(requestId, new Exception());
        } else {
            List<ShardRouting> shards = map.get(entry.getKey());
            List<TransportBroadcastByNodeAction.EmptyResult> shardResults = new ArrayList<>();
            for (ShardRouting shard : shards) {
                totalShards++;
                if (rarely()) {
                    // simulate operation failure
                    totalFailedShards++;
                    exceptions.add(new BroadcastShardOperationFailedException(shard.shardId(), "operation indices:admin/test failed"));
                } else {
                    shardResults.add(TransportBroadcastByNodeAction.EmptyResult.INSTANCE);
                }
            }
            totalSuccessfulShards += shardResults.size();
            TransportBroadcastByNodeAction.NodeResponse nodeResponse = action.new NodeResponse(entry.getKey(), shards.size(), shardResults, exceptions);
            transport.handleResponse(requestId, nodeResponse);
        }
    }
    if (simulateFailedMasterNode) {
        totalShards += map.get(failedMasterNode.getId()).size();
    }
    Response response = listener.get();
    assertEquals("total shards", totalShards, response.getTotalShards());
    assertEquals("successful shards", totalSuccessfulShards, response.getSuccessfulShards());
    assertEquals("failed shards", totalFailedShards, response.getFailedShards());
    assertEquals("accumulated exceptions", totalFailedShards, response.getShardFailures().length);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) ShardsIterator(org.opensearch.cluster.routing.ShardsIterator) List(java.util.List) ArrayList(java.util.ArrayList) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) CapturingTransport(org.opensearch.test.transport.CapturingTransport) BroadcastRequest(org.opensearch.action.support.broadcast.BroadcastRequest) IndicesRequest(org.opensearch.action.IndicesRequest) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) ReceiveTimeoutTransportException(org.opensearch.transport.ReceiveTimeoutTransportException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TransportResponse(org.opensearch.transport.TransportResponse) BroadcastResponse(org.opensearch.action.support.broadcast.BroadcastResponse) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap)

Example 3 with ShardsIterator

use of org.opensearch.cluster.routing.ShardsIterator in project OpenSearch by opensearch-project.

the class ShardStateActionTests method getRandomShardRouting.

private ShardRouting getRandomShardRouting(String index) {
    IndexRoutingTable indexRoutingTable = clusterService.state().routingTable().index(index);
    ShardsIterator shardsIterator = indexRoutingTable.randomAllActiveShardsIt();
    ShardRouting shardRouting = shardsIterator.nextOrNull();
    assert shardRouting != null;
    return shardRouting;
}
Also used : IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardsIterator(org.opensearch.cluster.routing.ShardsIterator)

Example 4 with ShardsIterator

use of org.opensearch.cluster.routing.ShardsIterator in project OpenSearch by opensearch-project.

the class TransportUpgradeAction method shards.

/**
 * The upgrade request works against *all* shards.
 */
@Override
protected ShardsIterator shards(ClusterState clusterState, UpgradeRequest request, String[] concreteIndices) {
    ShardsIterator iterator = clusterState.routingTable().allShards(concreteIndices);
    Set<String> indicesWithMissingPrimaries = indicesWithMissingPrimaries(clusterState, concreteIndices);
    if (indicesWithMissingPrimaries.isEmpty()) {
        return iterator;
    }
    // If some primary shards are not available the request should fail.
    throw new PrimaryMissingActionException("Cannot upgrade indices because the following indices are missing primary shards " + indicesWithMissingPrimaries);
}
Also used : PrimaryMissingActionException(org.opensearch.action.PrimaryMissingActionException) ShardsIterator(org.opensearch.cluster.routing.ShardsIterator)

Example 5 with ShardsIterator

use of org.opensearch.cluster.routing.ShardsIterator in project OpenSearch by opensearch-project.

the class TransportBroadcastByNodeActionTests method testResultWithTimeouts.

public void testResultWithTimeouts() throws ExecutionException, InterruptedException {
    Request request = new Request(new String[] { TEST_INDEX });
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    action.new AsyncAction(null, request, listener).start();
    Map<String, List<CapturingTransport.CapturedRequest>> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear();
    ShardsIterator shardIt = clusterService.state().getRoutingTable().allShards(new String[] { TEST_INDEX });
    Map<String, List<ShardRouting>> map = new HashMap<>();
    for (ShardRouting shard : shardIt) {
        if (!map.containsKey(shard.currentNodeId())) {
            map.put(shard.currentNodeId(), new ArrayList<>());
        }
        map.get(shard.currentNodeId()).add(shard);
    }
    int totalShards = 0;
    int totalSuccessfulShards = 0;
    int totalFailedShards = 0;
    String failedNodeId = "node_" + randomIntBetween(0, capturedRequests.size());
    for (Map.Entry<String, List<CapturingTransport.CapturedRequest>> entry : capturedRequests.entrySet()) {
        List<BroadcastShardOperationFailedException> exceptions = new ArrayList<>();
        long requestId = entry.getValue().get(0).requestId;
        if (entry.getKey().equals(failedNodeId)) {
            // simulate node timeout
            totalShards += map.get(entry.getKey()).size();
            totalFailedShards += map.get(entry.getKey()).size();
            transport.handleError(requestId, new ReceiveTimeoutTransportException(clusterService.state().getRoutingNodes().node(entry.getKey()).node(), "indices:admin/test", "time_out_simulated"));
        } else {
            List<ShardRouting> shards = map.get(entry.getKey());
            List<TransportBroadcastByNodeAction.EmptyResult> shardResults = new ArrayList<>();
            for (ShardRouting shard : shards) {
                totalShards++;
                if (rarely()) {
                    // simulate operation failure
                    totalFailedShards++;
                    exceptions.add(new BroadcastShardOperationFailedException(shard.shardId(), "operation indices:admin/test failed"));
                } else {
                    shardResults.add(TransportBroadcastByNodeAction.EmptyResult.INSTANCE);
                }
            }
            totalSuccessfulShards += shardResults.size();
            TransportBroadcastByNodeAction.NodeResponse nodeResponse = action.new NodeResponse(entry.getKey(), shards.size(), shardResults, exceptions);
            transport.handleResponse(requestId, nodeResponse);
        }
    }
    Response response = listener.get();
    assertEquals("total shards", totalShards, response.getTotalShards());
    assertEquals("successful shards", totalSuccessfulShards, response.getSuccessfulShards());
    assertEquals("failed shards", totalFailedShards, response.getFailedShards());
    assertEquals("accumulated exceptions", totalFailedShards, response.getShardFailures().length);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) ShardsIterator(org.opensearch.cluster.routing.ShardsIterator) ReceiveTimeoutTransportException(org.opensearch.transport.ReceiveTimeoutTransportException) List(java.util.List) ArrayList(java.util.ArrayList) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) CapturingTransport(org.opensearch.test.transport.CapturingTransport) BroadcastRequest(org.opensearch.action.support.broadcast.BroadcastRequest) IndicesRequest(org.opensearch.action.IndicesRequest) TransportResponse(org.opensearch.transport.TransportResponse) BroadcastResponse(org.opensearch.action.support.broadcast.BroadcastResponse) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap)

Aggregations

ShardsIterator (org.opensearch.cluster.routing.ShardsIterator)8 ShardRouting (org.opensearch.cluster.routing.ShardRouting)6 ArrayList (java.util.ArrayList)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 HasToString.hasToString (org.hamcrest.object.HasToString.hasToString)5 IndicesRequest (org.opensearch.action.IndicesRequest)5 BroadcastRequest (org.opensearch.action.support.broadcast.BroadcastRequest)5 TestShardRouting (org.opensearch.cluster.routing.TestShardRouting)5 TransportResponse (org.opensearch.transport.TransportResponse)5 Collections.emptyMap (java.util.Collections.emptyMap)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)4 BroadcastResponse (org.opensearch.action.support.broadcast.BroadcastResponse)4 CapturingTransport (org.opensearch.test.transport.CapturingTransport)4 HashSet (java.util.HashSet)3 BroadcastShardOperationFailedException (org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException)3 OpenSearchException (org.opensearch.OpenSearchException)2 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)2