Search in sources :

Example 76 with ActionListener

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

the class TransportsTest method testOnFailureOnListenerIsCalledIfNodeIsNotInClusterState.

@Test
public void testOnFailureOnListenerIsCalledIfNodeIsNotInClusterState() throws Exception {
    Transports transports = new Transports(new NoopClusterService(), mock(TransportService.class));
    ActionListener actionListener = mock(ActionListener.class);
    transports.sendRequest("actionName", "invalid", mock(TransportRequest.class), actionListener, mock(TransportResponseHandler.class));
    verify(actionListener, times(1)).onFailure(any(Throwable.class));
}
Also used : TransportRequest(org.elasticsearch.transport.TransportRequest) ActionListener(org.elasticsearch.action.ActionListener) TransportService(org.elasticsearch.transport.TransportService) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) NoopClusterService(org.elasticsearch.test.cluster.NoopClusterService) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 77 with ActionListener

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

the class TransportGetTaskAction method runOnNodeWithTaskIfPossible.

/**
     * Executed on the coordinating node to forward execution of the remaining work to the node that matches that requested
     * {@link TaskId#getNodeId()}. If the node isn't in the cluster then this will just proceed to
     * {@link #getFinishedTaskFromIndex(Task, GetTaskRequest, ActionListener)} on this node.
     */
private void runOnNodeWithTaskIfPossible(Task thisTask, GetTaskRequest request, ActionListener<GetTaskResponse> listener) {
    TransportRequestOptions.Builder builder = TransportRequestOptions.builder();
    if (request.getTimeout() != null) {
        builder.withTimeout(request.getTimeout());
    }
    builder.withCompress(false);
    DiscoveryNode node = clusterService.state().nodes().get(request.getTaskId().getNodeId());
    if (node == null) {
        // Node is no longer part of the cluster! Try and look the task up from the results index.
        getFinishedTaskFromIndex(thisTask, request, ActionListener.wrap(listener::onResponse, e -> {
            if (e instanceof ResourceNotFoundException) {
                e = new ResourceNotFoundException("task [" + request.getTaskId() + "] belongs to the node [" + request.getTaskId().getNodeId() + "] which isn't part of the cluster and there is no record of the task", e);
            }
            listener.onFailure(e);
        }));
        return;
    }
    GetTaskRequest nodeRequest = request.nodeRequest(clusterService.localNode().getId(), thisTask.getId());
    transportService.sendRequest(node, GetTaskAction.NAME, nodeRequest, builder.build(), new TransportResponseHandler<GetTaskResponse>() {

        @Override
        public GetTaskResponse newInstance() {
            return new GetTaskResponse();
        }

        @Override
        public void handleResponse(GetTaskResponse response) {
            listener.onResponse(response);
        }

        @Override
        public void handleException(TransportException exp) {
            listener.onFailure(exp);
        }

        @Override
        public String executor() {
            return ThreadPool.Names.SAME;
        }
    });
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) ClusterService(org.elasticsearch.cluster.service.ClusterService) TaskId(org.elasticsearch.tasks.TaskId) Inject(org.elasticsearch.common.inject.Inject) XContentHelper(org.elasticsearch.common.xcontent.XContentHelper) TaskResult(org.elasticsearch.tasks.TaskResult) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) HandledTransportAction(org.elasticsearch.action.support.HandledTransportAction) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) TaskResultsService(org.elasticsearch.tasks.TaskResultsService) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportService(org.elasticsearch.transport.TransportService) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) TaskInfo(org.elasticsearch.tasks.TaskInfo) GetRequest(org.elasticsearch.action.get.GetRequest) ActionFilters(org.elasticsearch.action.support.ActionFilters) Client(org.elasticsearch.client.Client) IOException(java.io.IOException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ExceptionsHelper(org.elasticsearch.ExceptionsHelper) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) TransportListTasksAction.waitForCompletionTimeout(org.elasticsearch.action.admin.cluster.node.tasks.list.TransportListTasksAction.waitForCompletionTimeout) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Task(org.elasticsearch.tasks.Task) TransportException(org.elasticsearch.transport.TransportException) ActionListener(org.elasticsearch.action.ActionListener) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) TransportException(org.elasticsearch.transport.TransportException)

Example 78 with ActionListener

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

the class TransportBulkActionTookTests method createAction.

private TransportBulkAction createAction(boolean controlled, AtomicLong expected) {
    CapturingTransport capturingTransport = new CapturingTransport();
    TransportService transportService = new TransportService(clusterService.getSettings(), capturingTransport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundAddress -> clusterService.localNode(), null);
    transportService.start();
    transportService.acceptIncomingRequests();
    IndexNameExpressionResolver resolver = new Resolver(Settings.EMPTY);
    ActionFilters actionFilters = new ActionFilters(new HashSet<>());
    TransportCreateIndexAction createIndexAction = new TransportCreateIndexAction(Settings.EMPTY, transportService, clusterService, threadPool, null, actionFilters, resolver);
    if (controlled) {
        return new TestTransportBulkAction(Settings.EMPTY, threadPool, transportService, clusterService, null, createIndexAction, actionFilters, resolver, null, expected::get) {

            @Override
            void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses) {
                expected.set(1000000);
                super.executeBulk(task, bulkRequest, startTimeNanos, listener, responses);
            }
        };
    } else {
        return new TestTransportBulkAction(Settings.EMPTY, threadPool, transportService, clusterService, null, createIndexAction, actionFilters, resolver, null, System::nanoTime) {

            @Override
            void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses) {
                long elapsed = spinForAtLeastOneMillisecond();
                expected.set(elapsed);
                super.executeBulk(task, bulkRequest, startTimeNanos, listener, responses);
            }
        };
    }
}
Also used : Task(org.elasticsearch.tasks.Task) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) ActionFilters(org.elasticsearch.action.support.ActionFilters) TransportCreateIndexAction(org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction) ActionListener(org.elasticsearch.action.ActionListener) TransportService(org.elasticsearch.transport.TransportService) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)

Example 79 with ActionListener

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

the class TransportRethrottleActionTests method rethrottleTestCase.

/**
     * Test rethrottling.
     * @param runningSlices the number of slices still running
     * @param simulator simulate a response from the sub-request to rethrottle the child requests
     * @param verifier verify the resulting response
     */
private void rethrottleTestCase(int runningSlices, Consumer<ActionListener<ListTasksResponse>> simulator, Consumer<ActionListener<TaskInfo>> verifier) {
    Client client = mock(Client.class);
    String localNodeId = randomAsciiOfLength(5);
    float newRequestsPerSecond = randomValueOtherThanMany(f -> f <= 0, () -> randomFloat());
    @SuppressWarnings("unchecked") ActionListener<TaskInfo> listener = mock(ActionListener.class);
    TransportRethrottleAction.rethrottle(localNodeId, client, task, newRequestsPerSecond, listener);
    // Capture the sub request and the listener so we can verify they are sane
    ArgumentCaptor<RethrottleRequest> subRequest = ArgumentCaptor.forClass(RethrottleRequest.class);
    // Magical generics incantation.....
    @SuppressWarnings({ "unchecked", "rawtypes" }) ArgumentCaptor<ActionListener<ListTasksResponse>> subListener = ArgumentCaptor.forClass((Class) ActionListener.class);
    if (runningSlices > 0) {
        verify(client).execute(eq(RethrottleAction.INSTANCE), subRequest.capture(), subListener.capture());
        assertEquals(new TaskId(localNodeId, task.getId()), subRequest.getValue().getParentTaskId());
        assertEquals(newRequestsPerSecond / runningSlices, subRequest.getValue().getRequestsPerSecond(), 0.00001f);
        simulator.accept(subListener.getValue());
    }
    verifier.accept(listener);
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) TaskId(org.elasticsearch.tasks.TaskId) ActionListener(org.elasticsearch.action.ActionListener) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.elasticsearch.client.Client)

Example 80 with ActionListener

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

the class DfsQueryPhaseTests method testDfsWith1ShardFailed.

public void testDfsWith1ShardFailed() throws IOException {
    AtomicArray<DfsSearchResult> results = new AtomicArray<>(2);
    AtomicReference<AtomicArray<QuerySearchResultProvider>> responseRef = new AtomicReference<>();
    results.set(0, new DfsSearchResult(1, new SearchShardTarget("node1", new Index("test", "na"), 0)));
    results.set(1, new DfsSearchResult(2, new SearchShardTarget("node2", new Index("test", "na"), 0)));
    results.get(0).termsStatistics(new Term[0], new TermStatistics[0]);
    results.get(1).termsStatistics(new Term[0], new TermStatistics[0]);
    SearchPhaseController controller = new SearchPhaseController(Settings.EMPTY, BigArrays.NON_RECYCLING_INSTANCE, null);
    SearchTransportService searchTransportService = new SearchTransportService(Settings.builder().put("search.remote.connect", false).build(), null, null) {

        @Override
        public void sendExecuteQuery(Transport.Connection connection, QuerySearchRequest request, SearchTask task, ActionListener<QuerySearchResult> listener) {
            if (request.id() == 1) {
                QuerySearchResult queryResult = new QuerySearchResult(123, new SearchShardTarget("node1", new Index("test", "na"), 0));
                queryResult.topDocs(new TopDocs(1, new ScoreDoc[] { new ScoreDoc(42, 1.0F) }, 2.0F), new DocValueFormat[0]);
                // the size of the result set
                queryResult.size(2);
                listener.onResponse(queryResult);
            } else if (request.id() == 2) {
                listener.onFailure(new MockDirectoryWrapper.FakeIOException());
            } else {
                fail("no such request ID: " + request.id());
            }
        }
    };
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(2);
    mockSearchPhaseContext.searchTransport = searchTransportService;
    DfsQueryPhase phase = new DfsQueryPhase(results, controller, (response) -> new SearchPhase("test") {

        @Override
        public void run() throws IOException {
            responseRef.set(response.results);
        }
    }, mockSearchPhaseContext);
    assertEquals("dfs_query", phase.getName());
    phase.run();
    mockSearchPhaseContext.assertNoFailure();
    assertNotNull(responseRef.get());
    assertNotNull(responseRef.get().get(0));
    assertNull(responseRef.get().get(0).fetchResult());
    assertEquals(1, responseRef.get().get(0).queryResult().topDocs().totalHits);
    assertEquals(42, responseRef.get().get(0).queryResult().topDocs().scoreDocs[0].doc);
    assertNull(responseRef.get().get(1));
    assertEquals(1, mockSearchPhaseContext.numSuccess.get());
    assertEquals(1, mockSearchPhaseContext.failures.size());
    assertTrue(mockSearchPhaseContext.failures.get(0).getCause() instanceof MockDirectoryWrapper.FakeIOException);
    assertEquals(1, mockSearchPhaseContext.releasedSearchContexts.size());
    assertTrue(mockSearchPhaseContext.releasedSearchContexts.contains(2L));
    assertNull(responseRef.get().get(1));
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) DfsSearchResult(org.elasticsearch.search.dfs.DfsSearchResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) Index(org.elasticsearch.index.Index) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) ActionListener(org.elasticsearch.action.ActionListener) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) QuerySearchRequest(org.elasticsearch.search.query.QuerySearchRequest)

Aggregations

ActionListener (org.elasticsearch.action.ActionListener)148 IOException (java.io.IOException)75 List (java.util.List)58 ThreadPool (org.elasticsearch.threadpool.ThreadPool)53 ClusterState (org.elasticsearch.cluster.ClusterState)50 ArrayList (java.util.ArrayList)46 Settings (org.elasticsearch.common.settings.Settings)45 Map (java.util.Map)42 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)41 ShardId (org.elasticsearch.index.shard.ShardId)40 Collections (java.util.Collections)39 Set (java.util.Set)39 ClusterService (org.elasticsearch.cluster.service.ClusterService)35 Logger (org.apache.logging.log4j.Logger)34 HashMap (java.util.HashMap)32 TransportService (org.elasticsearch.transport.TransportService)32 ElasticsearchException (org.elasticsearch.ElasticsearchException)31 Collectors (java.util.stream.Collectors)30 Index (org.elasticsearch.index.Index)30 Test (org.junit.Test)30