Search in sources :

Example 1 with ShardsIterator

use of org.elasticsearch.cluster.routing.ShardsIterator in project elasticsearch by elastic.

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.asUnordered()) {
        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.elasticsearch.cluster.node.DiscoveryNode) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) IndicesRequest(org.elasticsearch.action.IndicesRequest) BroadcastRequest(org.elasticsearch.action.support.broadcast.BroadcastRequest) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) ShardsIterator(org.elasticsearch.cluster.routing.ShardsIterator) BroadcastResponse(org.elasticsearch.action.support.broadcast.BroadcastResponse) TransportResponse(org.elasticsearch.transport.TransportResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) List(java.util.List) ArrayList(java.util.ArrayList) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet)

Example 2 with ShardsIterator

use of org.elasticsearch.cluster.routing.ShardsIterator in project elasticsearch by elastic.

the class RoutingIteratorTests method testNodeSelectorRouting.

public void testNodeSelectorRouting() {
    AllocationService strategy = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always").build());
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build();
    ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build();
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("fred", "node1", singletonMap("disk", "ebs"))).add(newNode("barney", "node2", singletonMap("disk", "ephemeral"))).localNodeId("node1")).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    ShardsIterator shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("disk:ebs", clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(1));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node1"));
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("dis*:eph*", clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(1));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node2"));
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("fred", clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(1));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node1"));
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("bar*", clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(1));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node2"));
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt(new String[] { "disk:eph*", "disk:ebs" }, clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(2));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node2"));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node1"));
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt(new String[] { "disk:*", "invalid_name" }, clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(2));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node2"));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node1"));
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt(new String[] { "disk:*", "disk:*" }, clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(2));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node2"));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node1"));
    try {
        shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("welma", clusterState.nodes());
        fail("should have raised illegalArgumentException");
    } catch (IllegalArgumentException illegal) {
    //expected exception
    }
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("fred", clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(1));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node1"));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) ShardsIterator(org.elasticsearch.cluster.routing.ShardsIterator) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Example 3 with ShardsIterator

use of org.elasticsearch.cluster.routing.ShardsIterator in project elasticsearch by elastic.

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.elasticsearch.action.PrimaryMissingActionException) ShardsIterator(org.elasticsearch.cluster.routing.ShardsIterator)

Example 4 with ShardsIterator

use of org.elasticsearch.cluster.routing.ShardsIterator in project elasticsearch by elastic.

the class TransportBroadcastByNodeActionTests method testOperationExecution.

public void testOperationExecution() throws Exception {
    ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { TEST_INDEX });
    Set<ShardRouting> shards = new HashSet<>();
    String nodeId = shardIt.asUnordered().iterator().next().currentNodeId();
    for (ShardRouting shard : shardIt.asUnordered()) {
        if (nodeId.equals(shard.currentNodeId())) {
            shards.add(shard);
        }
    }
    final TransportBroadcastByNodeAction.BroadcastByNodeTransportRequestHandler handler = action.new BroadcastByNodeTransportRequestHandler();
    TestTransportChannel channel = new TestTransportChannel();
    handler.messageReceived(action.new NodeRequest(nodeId, new Request(), new ArrayList<>(shards)), channel);
    // check the operation was executed only on the expected shards
    assertEquals(shards, action.getResults().keySet());
    TransportResponse response = channel.getCapturedResponse();
    assertTrue(response instanceof TransportBroadcastByNodeAction.NodeResponse);
    TransportBroadcastByNodeAction.NodeResponse nodeResponse = (TransportBroadcastByNodeAction.NodeResponse) response;
    // check the operation was executed on the correct node
    assertEquals("node id", nodeId, nodeResponse.getNodeId());
    int successfulShards = 0;
    int failedShards = 0;
    for (Object result : action.getResults().values()) {
        if (!(result instanceof ElasticsearchException)) {
            successfulShards++;
        } else {
            failedShards++;
        }
    }
    // check the operation results
    assertEquals("successful shards", successfulShards, nodeResponse.getSuccessfulShards());
    assertEquals("total shards", action.getResults().size(), nodeResponse.getTotalShards());
    assertEquals("failed shards", failedShards, nodeResponse.getExceptions().size());
    List<BroadcastShardOperationFailedException> exceptions = nodeResponse.getExceptions();
    for (BroadcastShardOperationFailedException exception : exceptions) {
        assertThat(exception.getMessage(), is("operation indices:admin/test failed"));
        assertThat(exception, hasToString(containsString("operation failed")));
    }
}
Also used : IndicesRequest(org.elasticsearch.action.IndicesRequest) BroadcastRequest(org.elasticsearch.action.support.broadcast.BroadcastRequest) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardsIterator(org.elasticsearch.cluster.routing.ShardsIterator) TransportResponse(org.elasticsearch.transport.TransportResponse) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) HashSet(java.util.HashSet)

Example 5 with ShardsIterator

use of org.elasticsearch.cluster.routing.ShardsIterator in project elasticsearch by elastic.

the class TransportBroadcastByNodeActionTests method testOneRequestIsSentToEachNodeHoldingAShard.

public void testOneRequestIsSentToEachNodeHoldingAShard() {
    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().routingTable().allShards(new String[] { TEST_INDEX });
    Set<String> set = new HashSet<>();
    for (ShardRouting shard : shardIt.asUnordered()) {
        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 node
        assertEquals(1, entry.getValue().size());
    }
}
Also used : CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) IndicesRequest(org.elasticsearch.action.IndicesRequest) BroadcastRequest(org.elasticsearch.action.support.broadcast.BroadcastRequest) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) ShardsIterator(org.elasticsearch.cluster.routing.ShardsIterator) BroadcastResponse(org.elasticsearch.action.support.broadcast.BroadcastResponse) TransportResponse(org.elasticsearch.transport.TransportResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) List(java.util.List) ArrayList(java.util.ArrayList) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap) HashSet(java.util.HashSet)

Aggregations

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