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;
}
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));
}
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()));
}
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());
}
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());
}
Aggregations