Search in sources :

Example 1 with OriginSettingClient

use of org.opensearch.client.OriginSettingClient 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 OriginSettingClient

use of org.opensearch.client.OriginSettingClient 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 OriginSettingClient

use of org.opensearch.client.OriginSettingClient in project OpenSearch by opensearch-project.

the class RestCancellableNodeClient method cancelTask.

private void cancelTask(TaskId taskId) {
    CancelTasksRequest req = new CancelTasksRequest().setTaskId(taskId).setReason("channel closed");
    // force the origin to execute the cancellation as a system user
    new OriginSettingClient(client, TASKS_ORIGIN).admin().cluster().cancelTasks(req, ActionListener.wrap(() -> {
    }));
}
Also used : CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) OriginSettingClient(org.opensearch.client.OriginSettingClient)

Aggregations

CancelTasksRequest (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest)3 OriginSettingClient (org.opensearch.client.OriginSettingClient)3 TaskId (org.opensearch.tasks.TaskId)2 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 ActionListener (org.opensearch.action.ActionListener)1 TASKS_ORIGIN (org.opensearch.action.admin.cluster.node.tasks.get.GetTaskAction.TASKS_ORIGIN)1 SEARCH_CANCEL_AFTER_TIME_INTERVAL_SETTING (org.opensearch.action.search.TransportSearchAction.SEARCH_CANCEL_AFTER_TIME_INTERVAL_SETTING)1 NodeClient (org.opensearch.client.node.NodeClient)1 ClusterSettings (org.opensearch.common.settings.ClusterSettings)1 TimeValue (org.opensearch.common.unit.TimeValue)1 SearchService (org.opensearch.search.SearchService)1 CancellableTask (org.opensearch.tasks.CancellableTask)1 Scheduler (org.opensearch.threadpool.Scheduler)1 ThreadPool (org.opensearch.threadpool.ThreadPool)1