Search in sources :

Example 51 with PlainActionFuture

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

the class TransportBroadcastByNodeActionTests method testOneRequestIsSentToEachNodeHoldingAShard.

public void testOneRequestIsSentToEachNodeHoldingAShard() {
    Request request = new Request(new String[] { TEST_INDEX });
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    action.new AsyncAction(null, request, listener).start();
    Map<String, List<CapturingTransport.CapturedRequest>> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear();
    ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { TEST_INDEX });
    Set<String> set = new HashSet<>();
    for (ShardRouting shard : shardIt.asUnordered()) {
        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 node
        assertEquals(1, entry.getValue().size());
    }
}
Also used : 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) HashSet(java.util.HashSet)

Example 52 with PlainActionFuture

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

the class TransportBroadcastByNodeActionTests method testResultAggregation.

public void testResultAggregation() throws ExecutionException, InterruptedException {
    Request request = new Request(new String[] { TEST_INDEX });
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    // simulate removing the master
    final boolean simulateFailedMasterNode = rarely();
    DiscoveryNode failedMasterNode = null;
    if (simulateFailedMasterNode) {
        failedMasterNode = clusterService.state().nodes().getMasterNode();
        DiscoveryNodes.Builder builder = DiscoveryNodes.builder(clusterService.state().getNodes());
        builder.remove(failedMasterNode.getId());
        builder.masterNodeId(null);
        setState(clusterService, ClusterState.builder(clusterService.state()).nodes(builder));
    }
    action.new AsyncAction(null, request, listener).start();
    Map<String, List<CapturingTransport.CapturedRequest>> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear();
    ShardsIterator shardIt = clusterService.state().getRoutingTable().allShards(new String[] { TEST_INDEX });
    Map<String, List<ShardRouting>> map = new HashMap<>();
    for (ShardRouting shard : shardIt.asUnordered()) {
        if (!map.containsKey(shard.currentNodeId())) {
            map.put(shard.currentNodeId(), new ArrayList<>());
        }
        map.get(shard.currentNodeId()).add(shard);
    }
    int totalShards = 0;
    int totalSuccessfulShards = 0;
    int totalFailedShards = 0;
    for (Map.Entry<String, List<CapturingTransport.CapturedRequest>> entry : capturedRequests.entrySet()) {
        List<BroadcastShardOperationFailedException> exceptions = new ArrayList<>();
        long requestId = entry.getValue().get(0).requestId;
        if (rarely()) {
            // simulate node failure
            totalShards += map.get(entry.getKey()).size();
            totalFailedShards += map.get(entry.getKey()).size();
            transport.handleRemoteError(requestId, new Exception());
        } else {
            List<ShardRouting> shards = map.get(entry.getKey());
            List<TransportBroadcastByNodeAction.EmptyResult> shardResults = new ArrayList<>();
            for (ShardRouting shard : shards) {
                totalShards++;
                if (rarely()) {
                    // simulate operation failure
                    totalFailedShards++;
                    exceptions.add(new BroadcastShardOperationFailedException(shard.shardId(), "operation indices:admin/test failed"));
                } else {
                    shardResults.add(TransportBroadcastByNodeAction.EmptyResult.INSTANCE);
                }
            }
            totalSuccessfulShards += shardResults.size();
            TransportBroadcastByNodeAction.NodeResponse nodeResponse = action.new NodeResponse(entry.getKey(), shards.size(), shardResults, exceptions);
            transport.handleResponse(requestId, nodeResponse);
        }
    }
    if (simulateFailedMasterNode) {
        totalShards += map.get(failedMasterNode.getId()).size();
    }
    Response response = listener.get();
    assertEquals("total shards", totalShards, response.getTotalShards());
    assertEquals("successful shards", totalSuccessfulShards, response.getSuccessfulShards());
    assertEquals("failed shards", totalFailedShards, response.getFailedShards());
    assertEquals("accumulated exceptions", totalFailedShards, response.getShardFailures().length);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) ShardsIterator(org.elasticsearch.cluster.routing.ShardsIterator) List(java.util.List) ArrayList(java.util.ArrayList) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) IndicesRequest(org.elasticsearch.action.IndicesRequest) BroadcastRequest(org.elasticsearch.action.support.broadcast.BroadcastRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) ExecutionException(java.util.concurrent.ExecutionException) BroadcastResponse(org.elasticsearch.action.support.broadcast.BroadcastResponse) TransportResponse(org.elasticsearch.transport.TransportResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) 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)

Example 53 with PlainActionFuture

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

the class TransportMasterNodeActionTests method testDelegateToFailingMaster.

public void testDelegateToFailingMaster() throws ExecutionException, InterruptedException {
    boolean failsWithConnectTransportException = randomBoolean();
    Request request = new Request().masterNodeTimeout(TimeValue.timeValueSeconds(failsWithConnectTransportException ? 60 : 0));
    setState(clusterService, ClusterStateCreationUtils.state(localNode, remoteNode, allNodes));
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    new Action(Settings.EMPTY, "testAction", transportService, clusterService, threadPool).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"));
    if (failsWithConnectTransportException) {
        transport.handleRemoteError(capturedRequest.requestId, new ConnectTransportException(remoteNode, "Fake error"));
        assertFalse(listener.isDone());
        setState(clusterService, ClusterStateCreationUtils.state(localNode, localNode, allNodes));
        assertTrue(listener.isDone());
        listener.get();
    } else {
        ElasticsearchException t = new ElasticsearchException("test");
        t.addHeader("header", "is here");
        transport.handleRemoteError(capturedRequest.requestId, t);
        assertTrue(listener.isDone());
        try {
            listener.get();
            fail("Expected exception but returned proper result");
        } catch (ExecutionException ex) {
            final Throwable cause = ex.getCause().getCause();
            assertThat(cause, instanceOf(ElasticsearchException.class));
            final ElasticsearchException es = (ElasticsearchException) cause;
            assertThat(es.getMessage(), equalTo(t.getMessage()));
            assertThat(es.getHeader("header"), equalTo(t.getHeader("header")));
        }
    }
}
Also used : ActionResponse(org.elasticsearch.action.ActionResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) ElasticsearchException(org.elasticsearch.ElasticsearchException) ExecutionException(java.util.concurrent.ExecutionException)

Example 54 with PlainActionFuture

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

the class TransportMasterNodeActionTests method testDelegateToMaster.

public void testDelegateToMaster() throws ExecutionException, InterruptedException {
    Request request = new Request();
    setState(clusterService, ClusterStateCreationUtils.state(localNode, remoteNode, allNodes));
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    new Action(Settings.EMPTY, "testAction", transportService, clusterService, threadPool).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"));
    Response response = new Response();
    transport.handleResponse(capturedRequest.requestId, response);
    assertTrue(listener.isDone());
    assertThat(listener.get(), equalTo(response));
}
Also used : ActionResponse(org.elasticsearch.action.ActionResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport)

Example 55 with PlainActionFuture

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

the class IndexShardOperationsLockTests method testAllOperationsInvoked.

public void testAllOperationsInvoked() throws InterruptedException, TimeoutException, ExecutionException {
    int numThreads = 10;
    List<PlainActionFuture<Releasable>> futures = new ArrayList<>();
    List<Thread> operationThreads = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(numThreads / 2);
    for (int i = 0; i < numThreads; i++) {
        PlainActionFuture<Releasable> future = new PlainActionFuture<Releasable>() {

            @Override
            public void onResponse(Releasable releasable) {
                releasable.close();
                super.onResponse(releasable);
            }
        };
        Thread thread = new Thread() {

            public void run() {
                latch.countDown();
                block.acquire(future, ThreadPool.Names.GENERIC, true);
            }
        };
        futures.add(future);
        operationThreads.add(thread);
    }
    CountDownLatch blockFinished = new CountDownLatch(1);
    threadPool.generic().execute(() -> {
        try {
            latch.await();
            blockAndWait().close();
            blockFinished.countDown();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    });
    for (Thread thread : operationThreads) {
        thread.start();
    }
    for (PlainActionFuture<Releasable> future : futures) {
        assertNotNull(future.get(1, TimeUnit.MINUTES));
    }
    for (Thread thread : operationThreads) {
        thread.join();
    }
    blockFinished.await();
}
Also used : PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ArrayList(java.util.ArrayList) Releasable(org.elasticsearch.common.lease.Releasable) CountDownLatch(java.util.concurrent.CountDownLatch)

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