Search in sources :

Example 46 with IndexShardRoutingTable

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

the class AckClusterUpdateSettingsIT method testClusterUpdateSettingsAcknowledgement.

public void testClusterUpdateSettingsAcknowledgement() {
    createIndex("test");
    ensureGreen();
    // now that the cluster is stable, remove timeout
    removePublishTimeout();
    NodesInfoResponse nodesInfo = client().admin().cluster().prepareNodesInfo().get();
    String excludedNodeId = null;
    for (NodeInfo nodeInfo : nodesInfo.getNodes()) {
        if (nodeInfo.getNode().isDataNode()) {
            excludedNodeId = nodeInfo.getNode().getId();
            break;
        }
    }
    assertNotNull(excludedNodeId);
    ClusterUpdateSettingsResponse clusterUpdateSettingsResponse = client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put("cluster.routing.allocation.exclude._id", excludedNodeId)).get();
    assertAcked(clusterUpdateSettingsResponse);
    assertThat(clusterUpdateSettingsResponse.getTransientSettings().get("cluster.routing.allocation.exclude._id"), equalTo(excludedNodeId));
    for (Client client : clients()) {
        ClusterState clusterState = getLocalClusterState(client);
        assertThat(clusterState.metaData().transientSettings().get("cluster.routing.allocation.exclude._id"), equalTo(excludedNodeId));
        for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
            for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
                for (ShardRouting shardRouting : indexShardRoutingTable) {
                    assert clusterState.nodes() != null;
                    if (shardRouting.unassigned() == false && clusterState.nodes().get(shardRouting.currentNodeId()).getId().equals(excludedNodeId)) {
                        //if the shard is still there it must be relocating and all nodes need to know, since the request was acknowledged
                        //reroute happens as part of the update settings and we made sure no throttling comes into the picture via settings
                        assertThat(shardRouting.relocating(), equalTo(true));
                    }
                }
            }
        }
    }
}
Also used : NodesInfoResponse(org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse) ClusterState(org.elasticsearch.cluster.ClusterState) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) NodeInfo(org.elasticsearch.action.admin.cluster.node.info.NodeInfo) ClusterUpdateSettingsResponse(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse) Client(org.elasticsearch.client.Client) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 47 with IndexShardRoutingTable

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

the class ReplicationOperationTests method testReplicationWithShadowIndex.

public void testReplicationWithShadowIndex() throws Exception {
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    final ClusterState state = stateWithActivePrimary(index, true, randomInt(5));
    final long primaryTerm = state.getMetaData().index(index).primaryTerm(0);
    final IndexShardRoutingTable indexShardRoutingTable = state.getRoutingTable().shardRoutingTable(shardId);
    final ShardRouting primaryShard = indexShardRoutingTable.primaryShard();
    Request request = new Request(shardId);
    PlainActionFuture<TestPrimary.Result> listener = new PlainActionFuture<>();
    final TestReplicationOperation op = new TestReplicationOperation(request, new TestPrimary(primaryShard, primaryTerm), listener, false, new TestReplicaProxy(), () -> state, logger, "test");
    op.execute();
    assertThat("request was not processed on primary", request.processedOnPrimary.get(), equalTo(true));
    assertThat(request.processedOnReplicas, equalTo(Collections.emptySet()));
    assertTrue("listener is not marked as done", listener.isDone());
    ShardInfo shardInfo = listener.actionGet().getShardInfo();
    assertThat(shardInfo.getFailed(), equalTo(0));
    assertThat(shardInfo.getFailures(), arrayWithSize(0));
    assertThat(shardInfo.getSuccessful(), equalTo(1));
    assertThat(shardInfo.getTotal(), equalTo(indexShardRoutingTable.getSize()));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ShardId(org.elasticsearch.index.shard.ShardId) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardInfo(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo)

Example 48 with IndexShardRoutingTable

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

the class TransportReplicationActionTests method testNoRerouteOnStaleClusterState.

/**
     * When relocating a primary shard, there is a cluster state update at the end of relocation where the active primary is switched from
     * the relocation source to the relocation target. If relocation source receives and processes this cluster state
     * before the relocation target, there is a time span where relocation source believes active primary to be on
     * relocation target and relocation target believes active primary to be on relocation source. This results in replication
     * requests being sent back and forth.
     * <p>
     * This test checks that replication request is not routed back from relocation target to relocation source in case of
     * stale index routing table on relocation target.
     */
public void testNoRerouteOnStaleClusterState() throws InterruptedException, ExecutionException {
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    ClusterState state = state(index, true, ShardRoutingState.RELOCATING);
    String relocationTargetNode = state.getRoutingTable().shardRoutingTable(shardId).primaryShard().relocatingNodeId();
    state = ClusterState.builder(state).nodes(DiscoveryNodes.builder(state.nodes()).localNodeId(relocationTargetNode)).build();
    setState(clusterService, state);
    logger.debug("--> relocation ongoing state:\n{}", clusterService.state());
    Request request = new Request(shardId).timeout("1ms").routedBasedOnClusterVersion(clusterService.state().version() + 1);
    PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
    TestAction.ReroutePhase reroutePhase = action.new ReroutePhase(null, request, listener);
    reroutePhase.run();
    assertListenerThrows("cluster state too old didn't cause a timeout", listener, UnavailableShardsException.class);
    assertTrue(request.isRetrySet.compareAndSet(true, false));
    request = new Request(shardId).routedBasedOnClusterVersion(clusterService.state().version() + 1);
    listener = new PlainActionFuture<>();
    reroutePhase = action.new ReroutePhase(null, request, listener);
    reroutePhase.run();
    assertFalse("cluster state too old didn't cause a retry", listener.isDone());
    assertTrue(request.isRetrySet.get());
    // finish relocation
    ShardRouting relocationTarget = clusterService.state().getRoutingTable().shardRoutingTable(shardId).shardsWithState(ShardRoutingState.INITIALIZING).get(0);
    AllocationService allocationService = ESAllocationTestCase.createAllocationService();
    ClusterState updatedState = allocationService.applyStartedShards(state, Collections.singletonList(relocationTarget));
    setState(clusterService, updatedState);
    logger.debug("--> relocation complete state:\n{}", clusterService.state());
    IndexShardRoutingTable shardRoutingTable = clusterService.state().routingTable().index(index).shard(shardId.id());
    final String primaryNodeId = shardRoutingTable.primaryShard().currentNodeId();
    final List<CapturingTransport.CapturedRequest> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear().get(primaryNodeId);
    assertThat(capturedRequests, notNullValue());
    assertThat(capturedRequests.size(), equalTo(1));
    assertThat(capturedRequests.get(0).action, equalTo("testAction[p]"));
    assertIndexShardCounter(0);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) TransportRequest(org.elasticsearch.transport.TransportRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) ShardId(org.elasticsearch.index.shard.ShardId) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Example 49 with IndexShardRoutingTable

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

the class TransportReplicationActionTests method testNotStartedPrimary.

public void testNotStartedPrimary() throws InterruptedException, ExecutionException {
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    // no replicas in oder to skip the replication part
    setState(clusterService, state(index, true, randomBoolean() ? ShardRoutingState.INITIALIZING : ShardRoutingState.UNASSIGNED));
    ReplicationTask task = maybeTask();
    logger.debug("--> using initial state:\n{}", clusterService.state());
    Request request = new Request(shardId).timeout("1ms");
    PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
    TestAction.ReroutePhase reroutePhase = action.new ReroutePhase(task, request, listener);
    reroutePhase.run();
    assertListenerThrows("unassigned primary didn't cause a timeout", listener, UnavailableShardsException.class);
    assertPhase(task, "failed");
    assertTrue(request.isRetrySet.get());
    request = new Request(shardId);
    listener = new PlainActionFuture<>();
    reroutePhase = action.new ReroutePhase(task, request, listener);
    reroutePhase.run();
    assertFalse("unassigned primary didn't cause a retry", listener.isDone());
    assertPhase(task, "waiting_for_retry");
    assertTrue(request.isRetrySet.get());
    setState(clusterService, state(index, true, ShardRoutingState.STARTED));
    logger.debug("--> primary assigned state:\n{}", clusterService.state());
    final IndexShardRoutingTable shardRoutingTable = clusterService.state().routingTable().index(index).shard(shardId.id());
    final String primaryNodeId = shardRoutingTable.primaryShard().currentNodeId();
    final List<CapturingTransport.CapturedRequest> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear().get(primaryNodeId);
    assertThat(capturedRequests, notNullValue());
    assertThat(capturedRequests.size(), equalTo(1));
    assertThat(capturedRequests.get(0).action, equalTo("testAction[p]"));
    assertIndexShardCounter(0);
}
Also used : IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) TransportRequest(org.elasticsearch.transport.TransportRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) ShardId(org.elasticsearch.index.shard.ShardId) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture)

Example 50 with IndexShardRoutingTable

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

the class ESIntegTestCase method assertAllShardsOnNodes.

/**
     * Asserts that all shards are allocated on nodes matching the given node pattern.
     */
public Set<String> assertAllShardsOnNodes(String index, String... pattern) {
    Set<String> nodes = new HashSet<>();
    ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
    for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
        for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
            for (ShardRouting shardRouting : indexShardRoutingTable) {
                if (shardRouting.currentNodeId() != null && index.equals(shardRouting.getIndexName())) {
                    String name = clusterState.nodes().get(shardRouting.currentNodeId()).getName();
                    nodes.add(name);
                    assertThat("Allocated on new node: " + name, Regex.simpleMatch(pattern, name), is(true));
                }
            }
        }
    }
    return nodes;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) HashSet(java.util.HashSet)

Aggregations

IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)54 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)39 ClusterState (org.elasticsearch.cluster.ClusterState)33 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)22 ShardId (org.elasticsearch.index.shard.ShardId)21 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)15 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)14 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)8 Settings (org.elasticsearch.common.settings.Settings)8 HashSet (java.util.HashSet)7 HashMap (java.util.HashMap)6 IndexService (org.elasticsearch.index.IndexService)6 IndicesService (org.elasticsearch.indices.IndicesService)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 Set (java.util.Set)5 MetaData (org.elasticsearch.cluster.metadata.MetaData)5 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)5 TransportRequest (org.elasticsearch.transport.TransportRequest)5 Matchers.anyString (org.mockito.Matchers.anyString)5