Search in sources :

Example 56 with PlainActionFuture

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

the class SyncedFlushSingleNodeTests method testSyncFailsIfOperationIsInFlight.

public void testSyncFailsIfOperationIsInFlight() throws InterruptedException, ExecutionException {
    createIndex("test");
    client().prepareIndex("test", "test", "1").setSource("{}", XContentType.JSON).get();
    IndexService test = getInstanceFromNode(IndicesService.class).indexService(resolveIndex("test"));
    IndexShard shard = test.getShardOrNull(0);
    SyncedFlushService flushService = getInstanceFromNode(SyncedFlushService.class);
    final ShardId shardId = shard.shardId();
    PlainActionFuture<Releasable> fut = new PlainActionFuture<>();
    shard.acquirePrimaryOperationLock(fut, ThreadPool.Names.INDEX);
    try (Releasable operationLock = fut.get()) {
        SyncedFlushUtil.LatchedListener<ShardsSyncedFlushResult> listener = new SyncedFlushUtil.LatchedListener<>();
        flushService.attemptSyncedFlush(shardId, listener);
        listener.latch.await();
        assertNull(listener.error);
        ShardsSyncedFlushResult syncedFlushResult = listener.result;
        assertNotNull(syncedFlushResult);
        assertEquals(0, syncedFlushResult.successfulShards());
        assertNotEquals(0, syncedFlushResult.totalShards());
        assertEquals("[1] ongoing operations on primary", syncedFlushResult.failureReason());
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexService(org.elasticsearch.index.IndexService) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) Releasable(org.elasticsearch.common.lease.Releasable)

Example 57 with PlainActionFuture

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

the class TransportInstanceSingleOperationActionTests method testUnresolvableRequestDoesNotHang.

public void testUnresolvableRequestDoesNotHang() throws InterruptedException, ExecutionException, TimeoutException {
    action = new TestTransportInstanceSingleOperationAction(Settings.EMPTY, "indices:admin/test_unresolvable", transportService, new ActionFilters(new HashSet<>()), new MyResolver(), Request::new) {

        @Override
        protected void resolveRequest(ClusterState state, Request request) {
            throw new IllegalStateException("request cannot be resolved");
        }
    };
    Request request = new Request().index("test");
    request.shardId = new ShardId("test", "_na_", 0);
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    setState(clusterService, ClusterStateCreationUtils.state("test", randomBoolean(), ShardRoutingState.STARTED));
    action.new AsyncSingleAction(request, listener).start();
    assertThat(transport.capturedRequests().length, equalTo(0));
    try {
        listener.get();
    } catch (Exception e) {
        if (ExceptionsHelper.unwrap(e, IllegalStateException.class) == null) {
            logger.info("expected IllegalStateException  but got ", e);
            fail("expected and IllegalStateException");
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndicesRequest(org.elasticsearch.action.IndicesRequest) ActionFilters(org.elasticsearch.action.support.ActionFilters) TimeoutException(java.util.concurrent.TimeoutException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ExecutionException(java.util.concurrent.ExecutionException) TransportException(org.elasticsearch.transport.TransportException) ShardId(org.elasticsearch.index.shard.ShardId) ActionResponse(org.elasticsearch.action.ActionResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture)

Example 58 with PlainActionFuture

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

the class TransportInstanceSingleOperationActionTests method testRetryOfAnAlreadyTimedOutRequest.

public void testRetryOfAnAlreadyTimedOutRequest() throws Exception {
    Request request = new Request().index("test").timeout(new TimeValue(0, TimeUnit.MILLISECONDS));
    request.shardId = new ShardId("test", "_na_", 0);
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    setState(clusterService, ClusterStateCreationUtils.state("test", randomBoolean(), ShardRoutingState.STARTED));
    action.new AsyncSingleAction(request, listener).start();
    assertThat(transport.capturedRequests().length, equalTo(1));
    long requestId = transport.capturedRequests()[0].requestId;
    transport.clear();
    DiscoveryNode node = clusterService.state().getNodes().getLocalNode();
    transport.handleLocalError(requestId, new ConnectTransportException(node, "test exception"));
    // wait until the timeout was triggered and we actually tried to send for the second time
    assertBusy(new Runnable() {

        @Override
        public void run() {
            assertThat(transport.capturedRequests().length, equalTo(1));
        }
    });
    // let it fail the second time too
    requestId = transport.capturedRequests()[0].requestId;
    transport.handleLocalError(requestId, new ConnectTransportException(node, "test exception"));
    try {
        // result should return immediately
        assertTrue(listener.isDone());
        listener.get();
        fail("this should fail with a transport exception");
    } catch (ExecutionException t) {
        if (ExceptionsHelper.unwrap(t, ConnectTransportException.class) == null) {
            logger.info("expected ConnectTransportException  but got ", t);
            fail("expected and ConnectTransportException");
        }
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) IndicesRequest(org.elasticsearch.action.IndicesRequest) ShardId(org.elasticsearch.index.shard.ShardId) ActionResponse(org.elasticsearch.action.ActionResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) ExecutionException(java.util.concurrent.ExecutionException) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 59 with PlainActionFuture

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

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

the class TransportReplicationActionTests method testSeqNoIsSetOnPrimary.

public void testSeqNoIsSetOnPrimary() throws Exception {
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    // we use one replica to check the primary term was set on the operation and sent to the replica
    setState(clusterService, state(index, true, ShardRoutingState.STARTED, randomFrom(ShardRoutingState.INITIALIZING, ShardRoutingState.STARTED)));
    logger.debug("--> using initial state:\n{}", clusterService.state());
    final ShardRouting routingEntry = clusterService.state().getRoutingTable().index("test").shard(0).primaryShard();
    Request request = new Request(shardId);
    TransportReplicationAction.ConcreteShardRequest<Request> concreteShardRequest = new TransportReplicationAction.ConcreteShardRequest<>(request, routingEntry.allocationId().getId());
    PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
    final IndexShard shard = mock(IndexShard.class);
    long primaryTerm = clusterService.state().getMetaData().index(index).primaryTerm(0);
    when(shard.getPrimaryTerm()).thenReturn(primaryTerm);
    when(shard.routingEntry()).thenReturn(routingEntry);
    AtomicBoolean closed = new AtomicBoolean();
    Releasable releasable = () -> {
        if (closed.compareAndSet(false, true) == false) {
            fail("releasable is closed twice");
        }
    };
    TestAction action = new TestAction(Settings.EMPTY, "testSeqNoIsSetOnPrimary", transportService, clusterService, shardStateAction, threadPool);
    TransportReplicationAction<Request, Request, TestResponse>.PrimaryOperationTransportHandler<Request, Request, TestResponse> primaryPhase = action.new PrimaryOperationTransportHandler();
    primaryPhase.messageReceived(concreteShardRequest, createTransportChannel(listener), null);
    CapturingTransport.CapturedRequest[] requestsToReplicas = transport.capturedRequests();
    assertThat(requestsToReplicas, arrayWithSize(1));
    assertThat(((TransportReplicationAction.ConcreteShardRequest<Request>) requestsToReplicas[0].request).getRequest().primaryTerm(), equalTo(primaryTerm));
}
Also used : IndexShard(org.elasticsearch.index.shard.IndexShard) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) Releasable(org.elasticsearch.common.lease.Releasable) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Aggregations

PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)82 ShardId (org.elasticsearch.index.shard.ShardId)37 ClusterState (org.elasticsearch.cluster.ClusterState)28 ExecutionException (java.util.concurrent.ExecutionException)27 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)25 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)21 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)20 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)18 ElasticsearchException (org.elasticsearch.ElasticsearchException)17 TransportRequest (org.elasticsearch.transport.TransportRequest)17 Matchers.anyString (org.mockito.Matchers.anyString)17 HashSet (java.util.HashSet)16 List (java.util.List)16 CloseIndexRequest (org.elasticsearch.action.admin.indices.close.CloseIndexRequest)16 IndexShard (org.elasticsearch.index.shard.IndexShard)16 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)16 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)15 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)13