Search in sources :

Example 21 with PlainActionFuture

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

the class TransportMasterNodeActionTests method testMasterFailoverAfterStepDown.

public void testMasterFailoverAfterStepDown() throws ExecutionException, InterruptedException {
    Request request = new Request().masterNodeTimeout(TimeValue.timeValueHours(1));
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    final Response response = new Response();
    setState(clusterService, ClusterStateCreationUtils.state(localNode, localNode, allNodes));
    new Action(Settings.EMPTY, "testAction", transportService, clusterService, threadPool) {

        @Override
        protected void masterOperation(Request request, ClusterState state, ActionListener<Response> listener) throws Exception {
            // The other node has become master, simulate failures of this node while publishing cluster state through ZenDiscovery
            setState(clusterService, ClusterStateCreationUtils.state(localNode, remoteNode, allNodes));
            Exception failure = randomBoolean() ? new Discovery.FailedToCommitClusterStateException("Fake error") : new NotMasterException("Fake error");
            listener.onFailure(failure);
        }
    }.execute(request, listener);
    assertThat(transport.capturedRequests().length, equalTo(1));
    CapturingTransport.CapturedRequest capturedRequest = transport.capturedRequests()[0];
    assertTrue(capturedRequest.node.isMasterNode());
    assertThat(capturedRequest.request, equalTo(request));
    assertThat(capturedRequest.action, equalTo("testAction"));
    transport.handleResponse(capturedRequest.requestId, response);
    assertTrue(listener.isDone());
    assertThat(listener.get(), equalTo(response));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) ElasticsearchException(org.elasticsearch.ElasticsearchException) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) NotMasterException(org.elasticsearch.cluster.NotMasterException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ExecutionException(java.util.concurrent.ExecutionException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) ActionResponse(org.elasticsearch.action.ActionResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) NotMasterException(org.elasticsearch.cluster.NotMasterException)

Example 22 with PlainActionFuture

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

the class TransportMasterNodeActionTests method testLocalOperationWithBlocks.

public void testLocalOperationWithBlocks() throws ExecutionException, InterruptedException {
    final boolean retryableBlock = randomBoolean();
    final boolean unblockBeforeTimeout = randomBoolean();
    Request request = new Request().masterNodeTimeout(TimeValue.timeValueSeconds(unblockBeforeTimeout ? 60 : 0));
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    ClusterBlock block = new ClusterBlock(1, "", retryableBlock, true, randomFrom(RestStatus.values()), ClusterBlockLevel.ALL);
    ClusterState stateWithBlock = ClusterState.builder(ClusterStateCreationUtils.state(localNode, localNode, allNodes)).blocks(ClusterBlocks.builder().addGlobalBlock(block)).build();
    setState(clusterService, stateWithBlock);
    new Action(Settings.EMPTY, "testAction", transportService, clusterService, threadPool) {

        @Override
        protected ClusterBlockException checkBlock(Request request, ClusterState state) {
            Set<ClusterBlock> blocks = state.blocks().global();
            return blocks.isEmpty() ? null : new ClusterBlockException(blocks);
        }
    }.execute(request, listener);
    if (retryableBlock && unblockBeforeTimeout) {
        assertFalse(listener.isDone());
        setState(clusterService, ClusterState.builder(ClusterStateCreationUtils.state(localNode, localNode, allNodes)).blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).build());
        assertTrue(listener.isDone());
        listener.get();
        return;
    }
    assertTrue(listener.isDone());
    if (retryableBlock) {
        try {
            listener.get();
            fail("Expected exception but returned proper result");
        } catch (ExecutionException ex) {
            assertThat(ex.getCause(), instanceOf(MasterNotDiscoveredException.class));
            assertThat(ex.getCause().getCause(), instanceOf(ClusterBlockException.class));
        }
    } else {
        assertListenerThrows("ClusterBlockException should be thrown", listener, ClusterBlockException.class);
    }
}
Also used : ActionResponse(org.elasticsearch.action.ActionResponse) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) ClusterState(org.elasticsearch.cluster.ClusterState) HashSet(java.util.HashSet) Set(java.util.Set) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ExecutionException(java.util.concurrent.ExecutionException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException)

Example 23 with PlainActionFuture

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

the class TransportMasterNodeActionTests method testLocalOperationWithoutBlocks.

public void testLocalOperationWithoutBlocks() throws ExecutionException, InterruptedException {
    final boolean masterOperationFailure = randomBoolean();
    Request request = new Request();
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    final Exception exception = new Exception();
    final Response response = new Response();
    setState(clusterService, ClusterStateCreationUtils.state(localNode, localNode, allNodes));
    new Action(Settings.EMPTY, "testAction", transportService, clusterService, threadPool) {

        @Override
        protected void masterOperation(Task task, Request request, ClusterState state, ActionListener<Response> listener) throws Exception {
            if (masterOperationFailure) {
                listener.onFailure(exception);
            } else {
                listener.onResponse(response);
            }
        }
    }.execute(request, listener);
    assertTrue(listener.isDone());
    if (masterOperationFailure) {
        try {
            listener.get();
            fail("Expected exception but returned proper result");
        } catch (ExecutionException ex) {
            assertThat(ex.getCause(), equalTo(exception));
        }
    } else {
        assertThat(listener.get(), equalTo(response));
    }
}
Also used : ActionResponse(org.elasticsearch.action.ActionResponse) ClusterState(org.elasticsearch.cluster.ClusterState) Task(org.elasticsearch.tasks.Task) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ExecutionException(java.util.concurrent.ExecutionException) ElasticsearchException(org.elasticsearch.ElasticsearchException) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) NotMasterException(org.elasticsearch.cluster.NotMasterException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ExecutionException(java.util.concurrent.ExecutionException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException)

Example 24 with PlainActionFuture

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

the class WaitForRefreshAndCloseTests method start.

private ActionFuture<String> start(String method, String path, HttpEntity body) {
    PlainActionFuture<String> future = new PlainActionFuture<>();
    Map<String, String> params = new HashMap<>();
    params.put("refresh", "wait_for");
    params.put("error_trace", "");
    client().performRequestAsync(method, docPath() + path, params, body, new ResponseListener() {

        @Override
        public void onSuccess(Response response) {
            try {
                future.onResponse(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8));
            } catch (IOException e) {
                future.onFailure(e);
            }
        }

        @Override
        public void onFailure(Exception exception) {
            future.onFailure(exception);
        }
    });
    return future;
}
Also used : Response(org.elasticsearch.client.Response) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) HashMap(java.util.HashMap) ResponseListener(org.elasticsearch.client.ResponseListener) IOException(java.io.IOException) ResponseException(org.elasticsearch.client.ResponseException) IOException(java.io.IOException)

Example 25 with PlainActionFuture

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

the class IndexShardOperationsLockTests method testOperationsDelayedIfBlock.

public void testOperationsDelayedIfBlock() throws ExecutionException, InterruptedException, TimeoutException {
    PlainActionFuture<Releasable> future = new PlainActionFuture<>();
    try (Releasable releasable = blockAndWait()) {
        block.acquire(future, ThreadPool.Names.GENERIC, true);
        assertFalse(future.isDone());
    }
    future.get(1, TimeUnit.HOURS).close();
}
Also used : PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) Releasable(org.elasticsearch.common.lease.Releasable)

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