Search in sources :

Example 1 with CancelTasksRequest

use of org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest in project OpenSearch by opensearch-project.

the class TransportSearchAction method cancelTask.

private void cancelTask(SearchTask task, Exception exc) {
    String errorMsg = exc.getMessage() != null ? exc.getMessage() : "";
    CancelTasksRequest req = new CancelTasksRequest().setTaskId(new TaskId(client.getLocalNodeId(), task.getId())).setReason("Fatal failure during search: " + errorMsg);
    // force the origin to execute the cancellation as a system user
    new OriginSettingClient(client, TASKS_ORIGIN).admin().cluster().cancelTasks(req, ActionListener.wrap(() -> {
    }));
}
Also used : TaskId(org.opensearch.tasks.TaskId) CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) OriginSettingClient(org.opensearch.client.OriginSettingClient)

Example 2 with CancelTasksRequest

use of org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest in project OpenSearch by opensearch-project.

the class TimeoutTaskCancellationUtility method wrapWithCancellationListener.

/**
 * Wraps a listener with a timeout listener {@link TimeoutRunnableListener} to schedule the task cancellation for provided tasks on
 * generic thread pool
 * @param client - {@link NodeClient}
 * @param taskToCancel - task to schedule cancellation for
 * @param clusterSettings - {@link ClusterSettings}
 * @param listener - original listener associated with the task
 * @return wrapped listener
 */
public static <Response> ActionListener<Response> wrapWithCancellationListener(NodeClient client, CancellableTask taskToCancel, ClusterSettings clusterSettings, ActionListener<Response> listener) {
    final TimeValue globalTimeout = clusterSettings.get(SEARCH_CANCEL_AFTER_TIME_INTERVAL_SETTING);
    final TimeValue timeoutInterval = (taskToCancel.getCancellationTimeout() == null) ? globalTimeout : taskToCancel.getCancellationTimeout();
    // Note: -1 (or no timeout) will help to turn off cancellation. The combinations will be request level set at -1 or request level
    // set to null and cluster level set to -1.
    ActionListener<Response> listenerToReturn = listener;
    if (timeoutInterval.equals(SearchService.NO_TIMEOUT)) {
        return listenerToReturn;
    }
    try {
        final TimeoutRunnableListener<Response> wrappedListener = new TimeoutRunnableListener<>(timeoutInterval, listener, () -> {
            final CancelTasksRequest cancelTasksRequest = new CancelTasksRequest();
            cancelTasksRequest.setTaskId(new TaskId(client.getLocalNodeId(), taskToCancel.getId()));
            cancelTasksRequest.setReason("Cancellation timeout of " + timeoutInterval + " is expired");
            // force the origin to execute the cancellation as a system user
            new OriginSettingClient(client, TASKS_ORIGIN).admin().cluster().cancelTasks(cancelTasksRequest, ActionListener.wrap(r -> logger.debug("Scheduled cancel task with timeout: {} for original task: {} is successfully completed", timeoutInterval, cancelTasksRequest.getTaskId()), e -> logger.error(new ParameterizedMessage("Scheduled cancel task with timeout: {} for original task: {} is failed", timeoutInterval, cancelTasksRequest.getTaskId()), e)));
        });
        wrappedListener.cancellable = client.threadPool().schedule(wrappedListener, timeoutInterval, ThreadPool.Names.GENERIC);
        listenerToReturn = wrappedListener;
    } catch (Exception ex) {
        // if there is any exception in scheduling the cancellation task then continue without it
        logger.warn("Failed to schedule the cancellation task for original task: {}, will continue without it", taskToCancel.getId());
    }
    return listenerToReturn;
}
Also used : TimeValue(org.opensearch.common.unit.TimeValue) NodeClient(org.opensearch.client.node.NodeClient) ThreadPool(org.opensearch.threadpool.ThreadPool) TaskId(org.opensearch.tasks.TaskId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) TimeUnit(java.util.concurrent.TimeUnit) Logger(org.apache.logging.log4j.Logger) CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) SEARCH_CANCEL_AFTER_TIME_INTERVAL_SETTING(org.opensearch.action.search.TransportSearchAction.SEARCH_CANCEL_AFTER_TIME_INTERVAL_SETTING) ActionListener(org.opensearch.action.ActionListener) ClusterSettings(org.opensearch.common.settings.ClusterSettings) LogManager(org.apache.logging.log4j.LogManager) OriginSettingClient(org.opensearch.client.OriginSettingClient) CancellableTask(org.opensearch.tasks.CancellableTask) Scheduler(org.opensearch.threadpool.Scheduler) SearchService(org.opensearch.search.SearchService) TASKS_ORIGIN(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskAction.TASKS_ORIGIN) TaskId(org.opensearch.tasks.TaskId) CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) OriginSettingClient(org.opensearch.client.OriginSettingClient) TimeValue(org.opensearch.common.unit.TimeValue)

Example 3 with CancelTasksRequest

use of org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest in project OpenSearch by opensearch-project.

the class PersistentTasksService method sendCancelRequest.

/**
 * Cancels a locally running task using the Task Manager API
 */
void sendCancelRequest(final long taskId, final String reason, final ActionListener<CancelTasksResponse> listener) {
    CancelTasksRequest request = new CancelTasksRequest();
    request.setTaskId(new TaskId(clusterService.localNode().getId(), taskId));
    request.setReason(reason);
    try {
        client.admin().cluster().cancelTasks(request, listener);
    } catch (Exception e) {
        listener.onFailure(e);
    }
}
Also used : TaskId(org.opensearch.tasks.TaskId) CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) NodeClosedException(org.opensearch.node.NodeClosedException)

Example 4 with CancelTasksRequest

use of org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest in project OpenSearch by opensearch-project.

the class CancellableTasksTests method testNonExistingTaskCancellation.

public void testNonExistingTaskCancellation() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    // Cancel a task that doesn't exist
    CancelTasksRequest request = new CancelTasksRequest();
    request.setReason("Testing Cancellation");
    request.setActions("do-not-match-anything");
    request.setNodes(randomSubsetOf(randomIntBetween(1, testNodes.length - 1), testNodes).stream().map(TestNode::getNodeId).toArray(String[]::new));
    // And send the cancellation request to a random node
    CancelTasksResponse response = ActionTestUtils.executeBlocking(testNodes[randomIntBetween(1, testNodes.length - 1)].transportCancelTasksAction, request);
    // Shouldn't have cancelled anything
    assertThat(response.getTasks().size(), equalTo(0));
    assertBusy(() -> {
        // Make sure that main task is no longer running
        ListTasksResponse listTasksResponse = ActionTestUtils.executeBlocking(testNodes[randomIntBetween(0, testNodes.length - 1)].transportListTasksAction, new ListTasksRequest().setActions(CancelTasksAction.NAME + "*"));
        assertEquals(0, listTasksResponse.getTasks().size());
    });
}
Also used : CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Example 5 with CancelTasksRequest

use of org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest in project OpenSearch by opensearch-project.

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<>();
    int runNodesCount = randomIntBetween(1, nodesCount);
    int blockedNodesCount = randomIntBetween(0, runNodesCount);
    Task mainTask = startCancellableTestNodesAction(true, runNodesCount, 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 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 = ActionTestUtils.executeBlocking(testNodes[randomIntBetween(1, testNodes.length - 1)].transportCancelTasksAction, request);
    // Awaiting for the main task to finish
    responseLatch.await();
    // Should have cancelled tasks at least on all nodes where it was blocked
    assertThat(response.getTasks().size(), lessThanOrEqualTo(runNodesCount));
    // but may also encounter some nodes where it was still running
    assertThat(response.getTasks().size(), greaterThanOrEqualTo(blockedNodesCount));
    assertBusy(() -> {
        // Make sure that main task is no longer running
        ListTasksResponse listTasksResponse = ActionTestUtils.executeBlocking(testNodes[randomIntBetween(0, testNodes.length - 1)].transportListTasksAction, new ListTasksRequest().setTaskId(new TaskId(testNodes[0].getNodeId(), mainTask.getId())));
        assertEquals(0, listTasksResponse.getTasks().size());
    });
}
Also used : CancellableTask(org.opensearch.tasks.CancellableTask) Task(org.opensearch.tasks.Task) TaskId(org.opensearch.tasks.TaskId) CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) IOException(java.io.IOException) TaskCancelledException(org.opensearch.tasks.TaskCancelledException) ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest)

Aggregations

CancelTasksRequest (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest)10 TaskId (org.opensearch.tasks.TaskId)8 CancelTasksResponse (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)5 ListTasksRequest (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest)5 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)5 IOException (java.io.IOException)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 CancellableTask (org.opensearch.tasks.CancellableTask)4 Task (org.opensearch.tasks.Task)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 OriginSettingClient (org.opensearch.client.OriginSettingClient)3 TaskCancelledException (org.opensearch.tasks.TaskCancelledException)3 NodeClient (org.opensearch.client.node.NodeClient)2 TaskInfo (org.opensearch.tasks.TaskInfo)2 Arrays.asList (java.util.Arrays.asList)1 Collections.unmodifiableList (java.util.Collections.unmodifiableList)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Supplier (java.util.function.Supplier)1