Search in sources :

Example 16 with PlainActionFuture

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

the class TransportInstanceSingleOperationActionTests method testGlobalBlock.

public void testGlobalBlock() {
    Request request = new Request();
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    ClusterBlocks.Builder block = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(1, "", false, true, RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL));
    setState(clusterService, ClusterState.builder(clusterService.state()).blocks(block));
    try {
        action.new AsyncSingleAction(request, listener).start();
        listener.get();
        fail("expected ClusterBlockException");
    } catch (Exception e) {
        if (ExceptionsHelper.unwrap(e, ClusterBlockException.class) == null) {
            logger.info("expected ClusterBlockException  but got ", e);
            fail("expected ClusterBlockException");
        }
    }
}
Also used : ActionResponse(org.elasticsearch.action.ActionResponse) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) IndicesRequest(org.elasticsearch.action.IndicesRequest) 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)

Example 17 with PlainActionFuture

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

the class TransportInstanceSingleOperationActionTests method testSuccessAfterRetryWithClusterStateUpdate.

public void testSuccessAfterRetryWithClusterStateUpdate() throws Exception {
    Request request = new Request().index("test");
    request.shardId = new ShardId("test", "_na_", 0);
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    boolean local = randomBoolean();
    setState(clusterService, ClusterStateCreationUtils.state("test", local, ShardRoutingState.INITIALIZING));
    action.new AsyncSingleAction(request, listener).start();
    // this should fail because primary not initialized
    assertThat(transport.capturedRequests().length, equalTo(0));
    setState(clusterService, ClusterStateCreationUtils.state("test", local, ShardRoutingState.STARTED));
    // this time it should work
    assertThat(transport.capturedRequests().length, equalTo(1));
    transport.handleResponse(transport.capturedRequests()[0].requestId, new Response());
    listener.get();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ActionResponse(org.elasticsearch.action.ActionResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) IndicesRequest(org.elasticsearch.action.IndicesRequest)

Example 18 with PlainActionFuture

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

the class TransportInstanceSingleOperationActionTests method testSuccessAfterRetryWithExceptionFromTransport.

public void testSuccessAfterRetryWithExceptionFromTransport() throws Exception {
    Request request = new Request().index("test");
    request.shardId = new ShardId("test", "_na_", 0);
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    boolean local = randomBoolean();
    setState(clusterService, ClusterStateCreationUtils.state("test", local, 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"));
    // trigger cluster state observer
    setState(clusterService, ClusterStateCreationUtils.state("test", local, ShardRoutingState.STARTED));
    assertThat(transport.capturedRequests().length, equalTo(1));
    transport.handleResponse(transport.capturedRequests()[0].requestId, new Response());
    listener.get();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ActionResponse(org.elasticsearch.action.ActionResponse) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) IndicesRequest(org.elasticsearch.action.IndicesRequest)

Example 19 with PlainActionFuture

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

the class TransportInstanceSingleOperationActionTests method testBasicRequestWorks.

public void testBasicRequestWorks() throws InterruptedException, ExecutionException, TimeoutException {
    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(1));
    transport.handleResponse(transport.capturedRequests()[0].requestId, new Response());
    listener.get();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ActionResponse(org.elasticsearch.action.ActionResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) IndicesRequest(org.elasticsearch.action.IndicesRequest)

Example 20 with PlainActionFuture

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

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