Search in sources :

Example 46 with PlainActionFuture

use of org.elasticsearch.action.support.PlainActionFuture 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 47 with PlainActionFuture

use of org.elasticsearch.action.support.PlainActionFuture in project elasticsearch by elastic.

the class TransportReplicationActionTests method testPrimaryActionRejectsWrongAid.

/** test that a primary request is rejected if it arrives at a shard with a wrong allocation id */
public void testPrimaryActionRejectsWrongAid() throws Exception {
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    setState(clusterService, state(index, true, ShardRoutingState.STARTED));
    PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
    Request request = new Request(shardId).timeout("1ms");
    action.new PrimaryOperationTransportHandler().messageReceived(new TransportReplicationAction.ConcreteShardRequest<>(request, "_not_a_valid_aid_"), createTransportChannel(listener), maybeTask());
    try {
        listener.get();
        fail("using a wrong aid didn't fail the operation");
    } catch (ExecutionException execException) {
        Throwable throwable = execException.getCause();
        logger.debug("got exception:", throwable);
        assertTrue(throwable.getClass() + " is not a retry exception", action.retryPrimaryException(throwable));
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) 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) ExecutionException(java.util.concurrent.ExecutionException)

Example 48 with PlainActionFuture

use of org.elasticsearch.action.support.PlainActionFuture 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 49 with PlainActionFuture

use of org.elasticsearch.action.support.PlainActionFuture in project elasticsearch by elastic.

the class TransportReplicationActionTests method testReplicasCounter.

public void testReplicasCounter() throws Exception {
    final ShardId shardId = new ShardId("test", "_na_", 0);
    final ClusterState state = state(shardId.getIndexName(), true, ShardRoutingState.STARTED, ShardRoutingState.STARTED);
    setState(clusterService, state);
    final ShardRouting replicaRouting = state.getRoutingTable().shardRoutingTable(shardId).replicaShards().get(0);
    boolean throwException = randomBoolean();
    final ReplicationTask task = maybeTask();
    TestAction action = new TestAction(Settings.EMPTY, "testActionWithExceptions", transportService, clusterService, shardStateAction, threadPool) {

        @Override
        protected ReplicaResult shardOperationOnReplica(Request request, IndexShard replica) {
            assertIndexShardCounter(1);
            assertPhase(task, "replica");
            if (throwException) {
                throw new ElasticsearchException("simulated");
            }
            return new ReplicaResult();
        }
    };
    final TestAction.ReplicaOperationTransportHandler replicaOperationTransportHandler = action.new ReplicaOperationTransportHandler();
    try {
        replicaOperationTransportHandler.messageReceived(new TransportReplicationAction.ConcreteShardRequest<>(new Request().setShardId(shardId), replicaRouting.allocationId().getId()), createTransportChannel(new PlainActionFuture<>()), task);
    } catch (ElasticsearchException e) {
        assertThat(e.getMessage(), containsString("simulated"));
        assertTrue(throwException);
    }
    assertPhase(task, "finished");
    // operation should have finished and counter decreased because no outstanding replica requests
    assertIndexShardCounter(0);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShard(org.elasticsearch.index.shard.IndexShard) TransportRequest(org.elasticsearch.transport.TransportRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardId(org.elasticsearch.index.shard.ShardId) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 50 with PlainActionFuture

use of org.elasticsearch.action.support.PlainActionFuture in project elasticsearch by elastic.

the class TransportReplicationActionTests method testClosedIndexOnReroute.

public void testClosedIndexOnReroute() throws InterruptedException {
    final String index = "test";
    // no replicas in oder to skip the replication part
    setState(clusterService, new ClusterStateChanges(xContentRegistry(), threadPool).closeIndices(state(index, true, ShardRoutingState.UNASSIGNED), new CloseIndexRequest(index)));
    logger.debug("--> using initial state:\n{}", clusterService.state());
    Request request = new Request(new ShardId("test", "_na_", 0)).timeout("1ms");
    PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
    ReplicationTask task = maybeTask();
    ClusterBlockLevel indexBlockLevel = randomBoolean() ? ClusterBlockLevel.WRITE : null;
    TestAction action = new TestAction(Settings.EMPTY, "testActionWithBlocks", transportService, clusterService, shardStateAction, threadPool) {

        @Override
        protected ClusterBlockLevel indexBlockLevel() {
            return indexBlockLevel;
        }
    };
    TestAction.ReroutePhase reroutePhase = action.new ReroutePhase(task, request, listener);
    reroutePhase.run();
    if (indexBlockLevel == ClusterBlockLevel.WRITE) {
        assertListenerThrows("must throw block exception", listener, ClusterBlockException.class);
    } else {
        assertListenerThrows("must throw index closed exception", listener, IndexClosedException.class);
    }
    assertPhase(task, "failed");
    assertFalse(request.isRetrySet.get());
}
Also used : 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) ClusterStateChanges(org.elasticsearch.indices.cluster.ClusterStateChanges) ShardId(org.elasticsearch.index.shard.ShardId) ClusterBlockLevel(org.elasticsearch.cluster.block.ClusterBlockLevel) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture)

Aggregations

PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)50 ShardId (org.elasticsearch.index.shard.ShardId)27 ExecutionException (java.util.concurrent.ExecutionException)20 ClusterState (org.elasticsearch.cluster.ClusterState)20 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)18 CloseIndexRequest (org.elasticsearch.action.admin.indices.close.CloseIndexRequest)16 TransportRequest (org.elasticsearch.transport.TransportRequest)16 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)16 ElasticsearchException (org.elasticsearch.ElasticsearchException)14 Matchers.anyString (org.mockito.Matchers.anyString)14 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)13 ActionResponse (org.elasticsearch.action.ActionResponse)12 IndicesRequest (org.elasticsearch.action.IndicesRequest)12 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)12 IOException (java.io.IOException)9 ConnectTransportException (org.elasticsearch.transport.ConnectTransportException)9 TransportException (org.elasticsearch.transport.TransportException)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)8 CapturingTransport (org.elasticsearch.test.transport.CapturingTransport)8