Search in sources :

Example 11 with ActionListener

use of org.elasticsearch.action.ActionListener in project elasticsearch by elastic.

the class TransportReplicationActionTests method mockIndexShard.

private IndexShard mockIndexShard(ShardId shardId, ClusterService clusterService) {
    final IndexShard indexShard = mock(IndexShard.class);
    doAnswer(invocation -> {
        ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[0];
        count.incrementAndGet();
        callback.onResponse(count::decrementAndGet);
        return null;
    }).when(indexShard).acquirePrimaryOperationLock(any(ActionListener.class), anyString());
    doAnswer(invocation -> {
        long term = (Long) invocation.getArguments()[0];
        ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[1];
        final long primaryTerm = indexShard.getPrimaryTerm();
        if (term < primaryTerm) {
            throw new IllegalArgumentException(String.format(Locale.ROOT, "%s operation term [%d] is too old (current [%d])", shardId, term, primaryTerm));
        }
        count.incrementAndGet();
        callback.onResponse(count::decrementAndGet);
        return null;
    }).when(indexShard).acquireReplicaOperationLock(anyLong(), any(ActionListener.class), anyString());
    when(indexShard.routingEntry()).thenAnswer(invocationOnMock -> {
        final ClusterState state = clusterService.state();
        final RoutingNode node = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
        final ShardRouting routing = node.getByShardId(shardId);
        if (routing == null) {
            throw new ShardNotFoundException(shardId, "shard is no longer assigned to current node");
        }
        return routing;
    });
    when(indexShard.state()).thenAnswer(invocationOnMock -> isRelocated.get() ? IndexShardState.RELOCATED : IndexShardState.STARTED);
    doThrow(new AssertionError("failed shard is not supported")).when(indexShard).failShard(anyString(), any(Exception.class));
    when(indexShard.getPrimaryTerm()).thenAnswer(i -> clusterService.state().metaData().getIndexSafe(shardId.getIndex()).primaryTerm(shardId.id()));
    return indexShard;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShard(org.elasticsearch.index.shard.IndexShard) ElasticsearchException(org.elasticsearch.ElasticsearchException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IndexClosedException(org.elasticsearch.indices.IndexClosedException) NodeClosedException(org.elasticsearch.node.NodeClosedException) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) TransportException(org.elasticsearch.transport.TransportException) IndexShardClosedException(org.elasticsearch.index.shard.IndexShardClosedException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) ExecutionException(java.util.concurrent.ExecutionException) ActionListener(org.elasticsearch.action.ActionListener) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) Matchers.anyLong(org.mockito.Matchers.anyLong) Releasable(org.elasticsearch.common.lease.Releasable) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 12 with ActionListener

use of org.elasticsearch.action.ActionListener in project elasticsearch by elastic.

the class BroadcastReplicationTests method testResultCombine.

public void testResultCombine() throws InterruptedException, ExecutionException, IOException {
    final String index = "test";
    int numShards = 1 + randomInt(3);
    setState(clusterService, stateWithAssignedPrimariesAndOneReplica(index, numShards));
    logger.debug("--> using initial state:\n{}", clusterService.state());
    Future<BroadcastResponse> response = (broadcastReplicationAction.execute(new DummyBroadcastRequest().indices(index)));
    int succeeded = 0;
    int failed = 0;
    for (Tuple<ShardId, ActionListener<ReplicationResponse>> shardRequests : broadcastReplicationAction.capturedShardRequests) {
        if (randomBoolean()) {
            ReplicationResponse.ShardInfo.Failure[] failures = new ReplicationResponse.ShardInfo.Failure[0];
            int shardsSucceeded = randomInt(1) + 1;
            succeeded += shardsSucceeded;
            ReplicationResponse replicationResponse = new ReplicationResponse();
            if (shardsSucceeded == 1 && randomBoolean()) {
                //sometimes add failure (no failure means shard unavailable)
                failures = new ReplicationResponse.ShardInfo.Failure[1];
                failures[0] = new ReplicationResponse.ShardInfo.Failure(shardRequests.v1(), null, new Exception("pretend shard failed"), RestStatus.GATEWAY_TIMEOUT, false);
                failed++;
            }
            replicationResponse.setShardInfo(new ReplicationResponse.ShardInfo(2, shardsSucceeded, failures));
            shardRequests.v2().onResponse(replicationResponse);
        } else {
            // sometimes fail
            failed += 2;
            // just add a general exception and see if failed shards will be incremented by 2
            shardRequests.v2().onFailure(new Exception("pretend shard failed"));
        }
    }
    assertBroadcastResponse(2 * numShards, succeeded, failed, response.get(), Exception.class);
}
Also used : BroadcastResponse(org.elasticsearch.action.support.broadcast.BroadcastResponse) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) IOException(java.io.IOException) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) ExecutionException(java.util.concurrent.ExecutionException) ShardId(org.elasticsearch.index.shard.ShardId) ActionListener(org.elasticsearch.action.ActionListener)

Example 13 with ActionListener

use of org.elasticsearch.action.ActionListener in project elasticsearch by elastic.

the class BroadcastReplicationTests method testNotStartedPrimary.

public void testNotStartedPrimary() throws InterruptedException, ExecutionException, IOException {
    final String index = "test";
    setState(clusterService, state(index, randomBoolean(), randomBoolean() ? ShardRoutingState.INITIALIZING : ShardRoutingState.UNASSIGNED, ShardRoutingState.UNASSIGNED));
    logger.debug("--> using initial state:\n{}", clusterService.state());
    Future<BroadcastResponse> response = (broadcastReplicationAction.execute(new DummyBroadcastRequest().indices(index)));
    for (Tuple<ShardId, ActionListener<ReplicationResponse>> shardRequests : broadcastReplicationAction.capturedShardRequests) {
        if (randomBoolean()) {
            shardRequests.v2().onFailure(new NoShardAvailableActionException(shardRequests.v1()));
        } else {
            shardRequests.v2().onFailure(new UnavailableShardsException(shardRequests.v1(), "test exception"));
        }
    }
    response.get();
    logger.info("total shards: {}, ", response.get().getTotalShards());
    // we expect no failures here because UnavailableShardsException does not count as failed
    assertBroadcastResponse(2, 0, 0, response.get(), null);
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ActionListener(org.elasticsearch.action.ActionListener) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) BroadcastResponse(org.elasticsearch.action.support.broadcast.BroadcastResponse)

Example 14 with ActionListener

use of org.elasticsearch.action.ActionListener in project elasticsearch by elastic.

the class TransportWriteActionTests method mockIndexShard.

private IndexShard mockIndexShard(ShardId shardId, ClusterService clusterService) {
    final IndexShard indexShard = mock(IndexShard.class);
    doAnswer(invocation -> {
        ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[0];
        count.incrementAndGet();
        callback.onResponse(count::decrementAndGet);
        return null;
    }).when(indexShard).acquirePrimaryOperationLock(any(ActionListener.class), anyString());
    doAnswer(invocation -> {
        long term = (Long) invocation.getArguments()[0];
        ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[1];
        final long primaryTerm = indexShard.getPrimaryTerm();
        if (term < primaryTerm) {
            throw new IllegalArgumentException(String.format(Locale.ROOT, "%s operation term [%d] is too old (current [%d])", shardId, term, primaryTerm));
        }
        count.incrementAndGet();
        callback.onResponse(count::decrementAndGet);
        return null;
    }).when(indexShard).acquireReplicaOperationLock(anyLong(), any(ActionListener.class), anyString());
    when(indexShard.routingEntry()).thenAnswer(invocationOnMock -> {
        final ClusterState state = clusterService.state();
        final RoutingNode node = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
        final ShardRouting routing = node.getByShardId(shardId);
        if (routing == null) {
            throw new ShardNotFoundException(shardId, "shard is no longer assigned to current node");
        }
        return routing;
    });
    when(indexShard.state()).thenAnswer(invocationOnMock -> isRelocated.get() ? IndexShardState.RELOCATED : IndexShardState.STARTED);
    doThrow(new AssertionError("failed shard is not supported")).when(indexShard).failShard(anyString(), any(Exception.class));
    when(indexShard.getPrimaryTerm()).thenAnswer(i -> clusterService.state().metaData().getIndexSafe(shardId.getIndex()).primaryTerm(shardId.id()));
    return indexShard;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShard(org.elasticsearch.index.shard.IndexShard) ElasticsearchException(org.elasticsearch.ElasticsearchException) NodeClosedException(org.elasticsearch.node.NodeClosedException) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) TransportException(org.elasticsearch.transport.TransportException) ExecutionException(java.util.concurrent.ExecutionException) ActionListener(org.elasticsearch.action.ActionListener) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) Matchers.anyLong(org.mockito.Matchers.anyLong) Releasable(org.elasticsearch.common.lease.Releasable) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 15 with ActionListener

use of org.elasticsearch.action.ActionListener in project elasticsearch by elastic.

the class TransportActionFilterChainTests method testTooManyContinueProcessingRequest.

public void testTooManyContinueProcessingRequest() throws ExecutionException, InterruptedException {
    final int additionalContinueCount = randomInt(10);
    RequestTestFilter testFilter = new RequestTestFilter(randomInt(), new RequestCallback() {

        @Override
        public <Request extends ActionRequest, Response extends ActionResponse> void execute(Task task, String action, Request request, ActionListener<Response> listener, ActionFilterChain<Request, Response> actionFilterChain) {
            for (int i = 0; i <= additionalContinueCount; i++) {
                actionFilterChain.proceed(task, action, request, listener);
            }
        }
    });
    Set<ActionFilter> filters = new HashSet<>();
    filters.add(testFilter);
    String actionName = randomAsciiOfLength(randomInt(30));
    ActionFilters actionFilters = new ActionFilters(filters);
    TransportAction<TestRequest, TestResponse> transportAction = new TransportAction<TestRequest, TestResponse>(Settings.EMPTY, actionName, null, actionFilters, null, new TaskManager(Settings.EMPTY)) {

        @Override
        protected void doExecute(TestRequest request, ActionListener<TestResponse> listener) {
            listener.onResponse(new TestResponse());
        }
    };
    final CountDownLatch latch = new CountDownLatch(additionalContinueCount + 1);
    final AtomicInteger responses = new AtomicInteger();
    final List<Throwable> failures = new CopyOnWriteArrayList<>();
    transportAction.execute(new TestRequest(), new ActionListener<TestResponse>() {

        @Override
        public void onResponse(TestResponse testResponse) {
            responses.incrementAndGet();
            latch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            failures.add(e);
            latch.countDown();
        }
    });
    if (!latch.await(10, TimeUnit.SECONDS)) {
        fail("timeout waiting for the filter to notify the listener as many times as expected");
    }
    assertThat(testFilter.runs.get(), equalTo(1));
    assertThat(testFilter.lastActionName, equalTo(actionName));
    assertThat(responses.get(), equalTo(1));
    assertThat(failures.size(), equalTo(additionalContinueCount));
    for (Throwable failure : failures) {
        assertThat(failure, instanceOf(IllegalStateException.class));
    }
}
Also used : Task(org.elasticsearch.tasks.Task) HashSet(java.util.HashSet) ActionRequest(org.elasticsearch.action.ActionRequest) CountDownLatch(java.util.concurrent.CountDownLatch) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) ActionResponse(org.elasticsearch.action.ActionResponse) TaskManager(org.elasticsearch.tasks.TaskManager) ActionListener(org.elasticsearch.action.ActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

ActionListener (org.elasticsearch.action.ActionListener)57 IOException (java.io.IOException)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 ThreadPool (org.elasticsearch.threadpool.ThreadPool)18 ClusterState (org.elasticsearch.cluster.ClusterState)17 Settings (org.elasticsearch.common.settings.Settings)17 Index (org.elasticsearch.index.Index)13 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)12 ShardId (org.elasticsearch.index.shard.ShardId)12 TransportService (org.elasticsearch.transport.TransportService)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 ArrayList (java.util.ArrayList)10 TimeValue (org.elasticsearch.common.unit.TimeValue)10 CrateUnitTest (io.crate.test.integration.CrateUnitTest)9 List (java.util.List)9 ActionFilters (org.elasticsearch.action.support.ActionFilters)9 TransportException (org.elasticsearch.transport.TransportException)9 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)8 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)8 ShardResponse (io.crate.executor.transport.ShardResponse)7