use of org.opensearch.tasks.TaskId in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testRethrottle.
public void testRethrottle() {
TaskId taskId = new TaskId(randomAlphaOfLength(10), randomIntBetween(1, 100));
RethrottleRequest rethrottleRequest;
Float requestsPerSecond;
Map<String, String> expectedParams = new HashMap<>();
if (frequently()) {
requestsPerSecond = (float) randomDoubleBetween(0.0, 100.0, true);
rethrottleRequest = new RethrottleRequest(taskId, requestsPerSecond);
expectedParams.put(RethrottleRequest.REQUEST_PER_SECOND_PARAMETER, Float.toString(requestsPerSecond));
} else {
rethrottleRequest = new RethrottleRequest(taskId);
expectedParams.put(RethrottleRequest.REQUEST_PER_SECOND_PARAMETER, "-1");
}
expectedParams.put("group_by", "none");
List<Tuple<String, Supplier<Request>>> variants = new ArrayList<>();
variants.add(new Tuple<String, Supplier<Request>>("_reindex", () -> RequestConverters.rethrottleReindex(rethrottleRequest)));
variants.add(new Tuple<String, Supplier<Request>>("_update_by_query", () -> RequestConverters.rethrottleUpdateByQuery(rethrottleRequest)));
variants.add(new Tuple<String, Supplier<Request>>("_delete_by_query", () -> RequestConverters.rethrottleDeleteByQuery(rethrottleRequest)));
for (Tuple<String, Supplier<Request>> variant : variants) {
Request request = variant.v2().get();
assertEquals("/" + variant.v1() + "/" + taskId + "/_rethrottle", request.getEndpoint());
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
assertEquals(expectedParams, request.getParameters());
assertNull(request.getEntity());
}
// test illegal RethrottleRequest values
Exception e = expectThrows(NullPointerException.class, () -> new RethrottleRequest(null, 1.0f));
assertEquals("taskId cannot be null", e.getMessage());
e = expectThrows(IllegalArgumentException.class, () -> new RethrottleRequest(new TaskId("taskId", 1), -5.0f));
assertEquals("requestsPerSecond needs to be positive value but was [-5.0]", e.getMessage());
}
use of org.opensearch.tasks.TaskId in project OpenSearch by opensearch-project.
the class OpenSearchRestHighLevelClientTestCase method findTaskToRethrottle.
protected static TaskId findTaskToRethrottle(String actionName, String description) throws IOException {
long start = System.nanoTime();
ListTasksRequest request = new ListTasksRequest();
request.setActions(actionName);
request.setDetailed(true);
do {
ListTasksResponse list = highLevelClient().tasks().list(request, RequestOptions.DEFAULT);
list.rethrowFailures("Finding tasks to rethrottle");
List<TaskGroup> taskGroups = list.getTaskGroups().stream().filter(taskGroup -> taskGroup.getTaskInfo().getDescription().equals(description)).collect(Collectors.toList());
assertThat("tasks are left over from the last execution of this test", taskGroups, hasSize(lessThan(2)));
if (0 == taskGroups.size()) {
// The parent task hasn't started yet
continue;
}
TaskGroup taskGroup = taskGroups.get(0);
assertThat(taskGroup.getChildTasks(), empty());
return taskGroup.getTaskInfo().getTaskId();
} while (System.nanoTime() - start < TimeUnit.SECONDS.toNanos(10));
throw new AssertionError("Couldn't find tasks to rethrottle. Here are the running tasks " + highLevelClient().tasks().list(request, RequestOptions.DEFAULT));
}
use of org.opensearch.tasks.TaskId in project OpenSearch by opensearch-project.
the class CRUDDocumentationIT method testReindexRethrottle.
@SuppressWarnings("unused")
public void testReindexRethrottle() throws Exception {
RestHighLevelClient client = highLevelClient();
TaskId taskId = new TaskId("oTUltX4IQMOUUVeiohTt8A:124");
{
// tag::rethrottle-disable-request
// <1>
RethrottleRequest request = new RethrottleRequest(taskId);
// end::rethrottle-disable-request
}
{
// tag::rethrottle-request
// <1>
RethrottleRequest request = new RethrottleRequest(taskId, 100.0f);
// end::rethrottle-request
}
{
RethrottleRequest request = new RethrottleRequest(taskId);
// tag::rethrottle-request-execution
// <1>
client.reindexRethrottle(request, RequestOptions.DEFAULT);
// <2>
client.updateByQueryRethrottle(request, RequestOptions.DEFAULT);
// <3>
client.deleteByQueryRethrottle(request, RequestOptions.DEFAULT);
// end::rethrottle-request-execution
}
ActionListener<ListTasksResponse> listener;
// tag::rethrottle-request-async-listener
listener = new ActionListener<ListTasksResponse>() {
@Override
public void onResponse(ListTasksResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::rethrottle-request-async-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(3);
listener = new LatchedActionListener<>(listener, latch);
RethrottleRequest request = new RethrottleRequest(taskId);
// tag::rethrottle-execute-async
client.reindexRethrottleAsync(request, RequestOptions.DEFAULT, // <1>
listener);
client.updateByQueryRethrottleAsync(request, RequestOptions.DEFAULT, // <2>
listener);
client.deleteByQueryRethrottleAsync(request, RequestOptions.DEFAULT, // <3>
listener);
// end::rethrottle-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.tasks.TaskId in project OpenSearch by opensearch-project.
the class TasksRequestConvertersTests method testCancelTasks.
public void testCancelTasks() {
Map<String, String> expectedParams = new HashMap<>();
org.opensearch.client.tasks.TaskId taskId = new org.opensearch.client.tasks.TaskId(randomAlphaOfLength(5), randomNonNegativeLong());
org.opensearch.client.tasks.TaskId parentTaskId = new org.opensearch.client.tasks.TaskId(randomAlphaOfLength(5), randomNonNegativeLong());
CancelTasksRequest.Builder builder = new CancelTasksRequest.Builder().withTaskId(taskId).withParentTaskId(parentTaskId);
expectedParams.put("task_id", taskId.toString());
expectedParams.put("parent_task_id", parentTaskId.toString());
if (randomBoolean()) {
boolean waitForCompletion = randomBoolean();
builder.withWaitForCompletion(waitForCompletion);
expectedParams.put("wait_for_completion", Boolean.toString(waitForCompletion));
}
Request httpRequest = TasksRequestConverters.cancelTasks(builder.build());
assertThat(httpRequest, notNullValue());
assertThat(httpRequest.getMethod(), equalTo(HttpPost.METHOD_NAME));
assertThat(httpRequest.getEntity(), nullValue());
assertThat(httpRequest.getEndpoint(), equalTo("/_tasks/_cancel"));
assertThat(httpRequest.getParameters(), equalTo(expectedParams));
}
use of org.opensearch.tasks.TaskId in project OpenSearch by opensearch-project.
the class CancelTasksResponseTests method createServerTestInstance.
@Override
protected CancelTasksResponseTests.ByNodeCancelTasksResponse createServerTestInstance(XContentType xContentType) {
List<org.opensearch.tasks.TaskInfo> tasks = new ArrayList<>();
List<TaskOperationFailure> taskFailures = new ArrayList<>();
List<OpenSearchException> nodeFailures = new ArrayList<>();
for (int i = 0; i < randomIntBetween(1, 4); i++) {
taskFailures.add(new TaskOperationFailure(randomAlphaOfLength(4), (long) i, new RuntimeException(randomAlphaOfLength(4))));
}
for (int i = 0; i < randomIntBetween(1, 4); i++) {
nodeFailures.add(new OpenSearchException(new RuntimeException(randomAlphaOfLength(10))));
}
for (int i = 0; i < 4; i++) {
boolean cancellable = randomBoolean();
boolean cancelled = cancellable == true ? randomBoolean() : false;
tasks.add(new org.opensearch.tasks.TaskInfo(new TaskId(NODE_ID, (long) i), randomAlphaOfLength(4), randomAlphaOfLength(4), randomAlphaOfLength(10), new FakeTaskStatus(randomAlphaOfLength(4), randomInt()), randomLongBetween(1, 3), randomIntBetween(5, 10), cancellable, cancelled, new TaskId("node1", randomLong()), Collections.singletonMap("x-header-of", "some-value")));
}
return new ByNodeCancelTasksResponse(tasks, taskFailures, nodeFailures);
}
Aggregations