Search in sources :

Example 26 with TaskId

use of org.elasticsearch.tasks.TaskId 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 27 with TaskId

use of org.elasticsearch.tasks.TaskId in project elasticsearch by elastic.

the class CancellableTasksTests method testChildTasksCancellation.

public void testChildTasksCancellation() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    CountDownLatch responseLatch = new CountDownLatch(1);
    final AtomicReference<NodesResponse> responseReference = new AtomicReference<>();
    final AtomicReference<Throwable> throwableReference = new AtomicReference<>();
    Task mainTask = startCancellableTestNodesAction(true, nodesCount, new ActionListener<NodesResponse>() {

        @Override
        public void onResponse(NodesResponse listTasksResponse) {
            responseReference.set(listTasksResponse);
            responseLatch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            throwableReference.set(e);
            responseLatch.countDown();
        }
    });
    // Cancel all child tasks without cancelling the main task, which should quit on its own
    CancelTasksRequest request = new CancelTasksRequest();
    request.setReason("Testing Cancellation");
    request.setParentTaskId(new TaskId(testNodes[0].getNodeId(), mainTask.getId()));
    // And send the cancellation request to a random node
    CancelTasksResponse response = testNodes[randomIntBetween(1, testNodes.length - 1)].transportCancelTasksAction.execute(request).get();
    // Awaiting for the main task to finish
    responseLatch.await();
    // Should have cancelled tasks on all nodes
    assertThat(response.getTasks().size(), equalTo(testNodes.length));
    assertBusy(() -> {
        try {
            // Make sure that main task is no longer running
            ListTasksResponse listTasksResponse = testNodes[randomIntBetween(0, testNodes.length - 1)].transportListTasksAction.execute(new ListTasksRequest().setTaskId(new TaskId(testNodes[0].getNodeId(), mainTask.getId()))).get();
            assertEquals(0, listTasksResponse.getTasks().size());
        } catch (ExecutionException | InterruptedException ex) {
            throw new RuntimeException(ex);
        }
    });
}
Also used : CancellableTask(org.elasticsearch.tasks.CancellableTask) Task(org.elasticsearch.tasks.Task) TaskId(org.elasticsearch.tasks.TaskId) CancelTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) CancelTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TaskCancelledException(org.elasticsearch.tasks.TaskCancelledException) ListTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest) ExecutionException(java.util.concurrent.ExecutionException)

Example 28 with TaskId

use of org.elasticsearch.tasks.TaskId in project elasticsearch by elastic.

the class CancellableTasksTests method testBasicTaskCancellation.

public void testBasicTaskCancellation() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    CountDownLatch responseLatch = new CountDownLatch(1);
    boolean waitForActionToStart = randomBoolean();
    logger.info("waitForActionToStart is set to {}", waitForActionToStart);
    final AtomicReference<NodesResponse> responseReference = new AtomicReference<>();
    final AtomicReference<Throwable> throwableReference = new AtomicReference<>();
    int blockedNodesCount = randomIntBetween(0, nodesCount);
    Task mainTask = startCancellableTestNodesAction(waitForActionToStart, blockedNodesCount, new ActionListener<NodesResponse>() {

        @Override
        public void onResponse(NodesResponse listTasksResponse) {
            responseReference.set(listTasksResponse);
            responseLatch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            throwableReference.set(e);
            responseLatch.countDown();
        }
    });
    // Cancel main task
    CancelTasksRequest request = new CancelTasksRequest();
    request.setReason("Testing Cancellation");
    request.setTaskId(new TaskId(testNodes[0].getNodeId(), mainTask.getId()));
    // And send the cancellation request to a random node
    CancelTasksResponse response = testNodes[randomIntBetween(0, testNodes.length - 1)].transportCancelTasksAction.execute(request).get();
    // Awaiting for the main task to finish
    responseLatch.await();
    if (response.getTasks().size() == 0) {
        // We didn't cancel the request and it finished successfully
        // That should be rare and can be only in case we didn't block on a single node
        assertEquals(0, blockedNodesCount);
        // Make sure that the request was successful
        assertNull(throwableReference.get());
        assertNotNull(responseReference.get());
        assertEquals(nodesCount, responseReference.get().getNodes().size());
        assertEquals(0, responseReference.get().failureCount());
    } else {
        // We canceled the request, in this case it should have fail, but we should get partial response
        assertNull(throwableReference.get());
        assertEquals(nodesCount, responseReference.get().failureCount() + responseReference.get().getNodes().size());
        // and we should have at least as many failures as the number of blocked operations
        // (we might have cancelled some non-blocked operations before they even started and that's ok)
        assertThat(responseReference.get().failureCount(), greaterThanOrEqualTo(blockedNodesCount));
        // We should have the information about the cancelled task in the cancel operation response
        assertEquals(1, response.getTasks().size());
        assertEquals(mainTask.getId(), response.getTasks().get(0).getId());
        // Verify that all cancelled tasks reported that they support cancellation
        for (TaskInfo taskInfo : response.getTasks()) {
            assertTrue(taskInfo.isCancellable());
        }
    }
    // Make sure that tasks are no longer running
    ListTasksResponse listTasksResponse = testNodes[randomIntBetween(0, testNodes.length - 1)].transportListTasksAction.execute(new ListTasksRequest().setTaskId(new TaskId(testNodes[0].getNodeId(), mainTask.getId()))).get();
    assertEquals(0, listTasksResponse.getTasks().size());
    // Make sure that there are no leftover bans, the ban removal is async, so we might return from the cancellation
    // while the ban is still there, but it should disappear shortly
    assertBusy(() -> {
        for (int i = 0; i < testNodes.length; i++) {
            assertEquals("No bans on the node " + i, 0, testNodes[i].transportService.getTaskManager().getBanCount());
        }
    });
}
Also used : CancellableTask(org.elasticsearch.tasks.CancellableTask) Task(org.elasticsearch.tasks.Task) TaskId(org.elasticsearch.tasks.TaskId) CancelTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) CancelTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TaskCancelledException(org.elasticsearch.tasks.TaskCancelledException) TaskInfo(org.elasticsearch.tasks.TaskInfo) ListTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest)

Aggregations

TaskId (org.elasticsearch.tasks.TaskId)28 TaskInfo (org.elasticsearch.tasks.TaskInfo)13 IOException (java.io.IOException)10 ListTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse)9 CountDownLatch (java.util.concurrent.CountDownLatch)6 ListTasksRequest (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest)6 Task (org.elasticsearch.tasks.Task)6 CancelTasksRequest (org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest)5 Settings (org.elasticsearch.common.settings.Settings)5 ArrayList (java.util.ArrayList)4 ExecutionException (java.util.concurrent.ExecutionException)4 ActionListener (org.elasticsearch.action.ActionListener)4 CancelTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)4 BulkByScrollTask (org.elasticsearch.action.bulk.byscroll.BulkByScrollTask)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 HashMap (java.util.HashMap)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 FailedNodeException (org.elasticsearch.action.FailedNodeException)3 GetTaskResponse (org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse)3