Search in sources :

Example 11 with NodeClient

use of org.opensearch.client.node.NodeClient 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 12 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class RestRethrottleAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) {
    RethrottleRequest internalRequest = new RethrottleRequest();
    internalRequest.setTaskId(new TaskId(request.param("taskId")));
    Float requestsPerSecond = AbstractBaseReindexRestHandler.parseRequestsPerSecond(request);
    if (requestsPerSecond == null) {
        throw new IllegalArgumentException("requests_per_second is a required parameter");
    }
    internalRequest.setRequestsPerSecond(requestsPerSecond);
    final String groupBy = request.param("group_by", "nodes");
    return channel -> client.execute(RethrottleAction.INSTANCE, internalRequest, listTasksResponseListener(nodesInCluster, groupBy, channel));
}
Also used : POST(org.opensearch.rest.RestRequest.Method.POST) RestListTasksAction.listTasksResponseListener(org.opensearch.rest.action.admin.cluster.RestListTasksAction.listTasksResponseListener) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) RestRequest(org.opensearch.rest.RestRequest) Arrays.asList(java.util.Arrays.asList) TaskId(org.opensearch.tasks.TaskId) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Supplier(java.util.function.Supplier) TaskId(org.opensearch.tasks.TaskId)

Example 13 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class AbstractBaseReindexRestHandler method doPrepareRequest.

protected RestChannelConsumer doPrepareRequest(RestRequest request, NodeClient client, boolean includeCreated, boolean includeUpdated) throws IOException {
    // Build the internal request
    Request internal = setCommonOptions(request, buildRequest(request, client.getNamedWriteableRegistry()));
    // Executes the request and waits for completion
    if (request.paramAsBoolean("wait_for_completion", true)) {
        Map<String, String> params = new HashMap<>();
        params.put(BulkByScrollTask.Status.INCLUDE_CREATED, Boolean.toString(includeCreated));
        params.put(BulkByScrollTask.Status.INCLUDE_UPDATED, Boolean.toString(includeUpdated));
        return channel -> client.executeLocally(action, internal, new BulkIndexByScrollResponseContentListener(channel, params));
    } else {
        internal.setShouldStoreResult(true);
    }
    /*
         * Let's try and validate before forking so the user gets some error. The
         * task can't totally validate until it starts but this is better than
         * nothing.
         */
    ActionRequestValidationException validationException = internal.validate();
    if (validationException != null) {
        throw validationException;
    }
    return sendTask(client.getLocalNodeId(), client.executeLocally(action, internal, LoggingTaskListener.instance()));
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) Task(org.opensearch.tasks.Task) RestStatus(org.opensearch.rest.RestStatus) BytesRestResponse(org.opensearch.rest.BytesRestResponse) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) LoggingTaskListener(org.opensearch.tasks.LoggingTaskListener) Map(java.util.Map) ActionType(org.opensearch.action.ActionType) BaseRestHandler(org.opensearch.rest.BaseRestHandler) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) HashMap(java.util.HashMap) RestRequest(org.opensearch.rest.RestRequest)

Example 14 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class BaseRestHandlerTests method testDefaultResponseParameters.

public void testDefaultResponseParameters() throws Exception {
    final AtomicBoolean executed = new AtomicBoolean();
    BaseRestHandler handler = new BaseRestHandler() {

        @Override
        protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
            return channel -> executed.set(true);
        }

        @Override
        public String getName() {
            return "test_default_response_action";
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("format", randomAlphaOfLength(8));
    params.put("filter_path", randomAlphaOfLength(8));
    params.put("pretty", randomFrom("true", "false", "", null));
    params.put("human", null);
    RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
    RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
    handler.handleRequest(request, channel, mockClient);
    assertTrue(executed.get());
}
Also used : Arrays(java.util.Arrays) ThreadPool(org.opensearch.threadpool.ThreadPool) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Table(org.opensearch.common.Table) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Method(org.opensearch.rest.RestRequest.Method) NodeClient(org.opensearch.client.node.NodeClient) AbstractCatAction(org.opensearch.rest.action.cat.AbstractCatAction) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) List(java.util.List) ReplacedRoute(org.opensearch.rest.RestHandler.ReplacedRoute) BytesArray(org.opensearch.common.bytes.BytesArray) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentType(org.opensearch.common.xcontent.XContentType) Route(org.opensearch.rest.RestHandler.Route) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.opensearch.client.node.NodeClient) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) StringContains.containsString(org.hamcrest.core.StringContains.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel)

Example 15 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class BaseRestHandlerTests method testUnconsumedParametersDidYouMean.

public void testUnconsumedParametersDidYouMean() throws Exception {
    final AtomicBoolean executed = new AtomicBoolean();
    BaseRestHandler handler = new BaseRestHandler() {

        @Override
        protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
            request.param("consumed");
            request.param("field");
            request.param("tokenizer");
            request.param("very_close_to_parameter_1");
            request.param("very_close_to_parameter_2");
            return channel -> executed.set(true);
        }

        @Override
        protected Set<String> responseParams() {
            return Collections.singleton("response_param");
        }

        @Override
        public String getName() {
            return "test_unconsumed_did_you_mean_response_action";
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("consumed", randomAlphaOfLength(8));
    params.put("flied", randomAlphaOfLength(8));
    params.put("respones_param", randomAlphaOfLength(8));
    params.put("tokenzier", randomAlphaOfLength(8));
    params.put("very_close_to_parametre", randomAlphaOfLength(8));
    params.put("very_far_from_every_consumed_parameter", randomAlphaOfLength(8));
    RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
    RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
    final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> handler.handleRequest(request, channel, mockClient));
    assertThat(e, hasToString(containsString("request [/] contains unrecognized parameters: " + "[flied] -> did you mean [field]?, " + "[respones_param] -> did you mean [response_param]?, " + "[tokenzier] -> did you mean [tokenizer]?, " + "[very_close_to_parametre] -> did you mean any of [very_close_to_parameter_1, very_close_to_parameter_2]?, " + "[very_far_from_every_consumed_parameter]")));
    assertFalse(executed.get());
}
Also used : Arrays(java.util.Arrays) ThreadPool(org.opensearch.threadpool.ThreadPool) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Table(org.opensearch.common.Table) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Method(org.opensearch.rest.RestRequest.Method) NodeClient(org.opensearch.client.node.NodeClient) AbstractCatAction(org.opensearch.rest.action.cat.AbstractCatAction) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) List(java.util.List) ReplacedRoute(org.opensearch.rest.RestHandler.ReplacedRoute) BytesArray(org.opensearch.common.bytes.BytesArray) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentType(org.opensearch.common.xcontent.XContentType) Route(org.opensearch.rest.RestHandler.Route) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.opensearch.client.node.NodeClient) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) StringContains.containsString(org.hamcrest.core.StringContains.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel)

Aggregations

NodeClient (org.opensearch.client.node.NodeClient)182 RestRequest (org.opensearch.rest.RestRequest)142 List (java.util.List)140 IOException (java.io.IOException)137 BaseRestHandler (org.opensearch.rest.BaseRestHandler)111 Arrays.asList (java.util.Arrays.asList)76 Collections.unmodifiableList (java.util.Collections.unmodifiableList)76 RestToXContentListener (org.opensearch.rest.action.RestToXContentListener)70 Strings (org.opensearch.common.Strings)66 GET (org.opensearch.rest.RestRequest.Method.GET)64 POST (org.opensearch.rest.RestRequest.Method.POST)47 RestResponse (org.opensearch.rest.RestResponse)39 IndicesOptions (org.opensearch.action.support.IndicesOptions)35 Settings (org.opensearch.common.settings.Settings)34 Set (java.util.Set)31 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)31 BytesRestResponse (org.opensearch.rest.BytesRestResponse)29 Collections (java.util.Collections)28 Collections.singletonList (java.util.Collections.singletonList)28 RestStatus (org.opensearch.rest.RestStatus)28