use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.
the class TransportRethrottleActionTests method rethrottleTestCase.
/**
* Test rethrottling.
* @param runningSlices the number of slices still running
* @param simulator simulate a response from the sub-request to rethrottle the child requests
* @param verifier verify the resulting response
*/
private void rethrottleTestCase(int runningSlices, Consumer<ActionListener<ListTasksResponse>> simulator, Consumer<ActionListener<TaskInfo>> verifier) {
Client client = mock(Client.class);
String localNodeId = randomAlphaOfLength(5);
float newRequestsPerSecond = randomValueOtherThanMany(f -> f <= 0, () -> randomFloat());
@SuppressWarnings("unchecked") ActionListener<TaskInfo> listener = mock(ActionListener.class);
TransportRethrottleAction.rethrottle(logger, localNodeId, client, task, newRequestsPerSecond, listener);
// Capture the sub request and the listener so we can verify they are sane
ArgumentCaptor<RethrottleRequest> subRequest = ArgumentCaptor.forClass(RethrottleRequest.class);
// Magical generics incantation.....
@SuppressWarnings({ "unchecked", "rawtypes" }) ArgumentCaptor<ActionListener<ListTasksResponse>> subListener = ArgumentCaptor.forClass((Class) ActionListener.class);
if (runningSlices > 0) {
verify(client).execute(eq(RethrottleAction.INSTANCE), subRequest.capture(), subListener.capture());
assertEquals(new TaskId(localNodeId, task.getId()), subRequest.getValue().getParentTaskId());
assertEquals(newRequestsPerSecond / runningSlices, subRequest.getValue().getRequestsPerSecond(), 0.00001f);
simulator.accept(subListener.getValue());
}
verifier.accept(listener);
}
use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.
the class ListTasksResponse method toXContentGroupedByNone.
/**
* Presents a flat list of tasks
*/
public XContentBuilder toXContentGroupedByNone(XContentBuilder builder, Params params) throws IOException {
toXContentCommon(builder, params);
builder.startArray(TASKS);
for (TaskInfo taskInfo : getTasks()) {
builder.startObject();
taskInfo.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
return builder;
}
use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.
the class CancellableTasksTests method testBasicTaskCancellation.
public void testBasicTaskCancellation() throws Exception {
setupTestNodes(Settings.EMPTY);
connectNodes(testNodes);
CountDownLatch responseLatch = new CountDownLatch(1);
boolean waitForActionToStart = randomBoolean();
logger.info("waitForActionToStart is set to {}", waitForActionToStart);
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(waitForActionToStart, 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 main task
CancelTasksRequest request = new CancelTasksRequest();
request.setReason("Testing Cancellation");
request.setTaskId(new TaskId(testNodes[0].getNodeId(), mainTask.getId()));
// And send the cancellation request to a random node
CancelTasksResponse response = ActionTestUtils.executeBlocking(testNodes[randomIntBetween(0, testNodes.length - 1)].transportCancelTasksAction, request);
// Awaiting for the main task to finish
responseLatch.await();
if (response.getTasks().size() == 0) {
// We didn't cancel the request and it finished successfully
// That should be rare and can be only in case we didn't block on a single node
assertEquals(0, blockedNodesCount);
// Make sure that the request was successful
assertNull(throwableReference.get());
assertNotNull(responseReference.get());
assertEquals(runNodesCount, responseReference.get().getNodes().size());
assertEquals(0, responseReference.get().failureCount());
} else {
// We canceled the request, in this case it should have fail, but we should get partial response
assertNull(throwableReference.get());
assertEquals(runNodesCount, responseReference.get().failureCount() + responseReference.get().getNodes().size());
// and we should have at least as many failures as the number of blocked operations
// (we might have cancelled some non-blocked operations before they even started and that's ok)
assertThat(responseReference.get().failureCount(), greaterThanOrEqualTo(blockedNodesCount));
// We should have the information about the cancelled task in the cancel operation response
assertEquals(1, response.getTasks().size());
assertEquals(mainTask.getId(), response.getTasks().get(0).getId());
// Verify that all cancelled tasks reported that they support cancellation
for (TaskInfo taskInfo : response.getTasks()) {
assertTrue(taskInfo.isCancellable());
}
}
// Make sure that tasks are 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());
// Make sure that there are no leftover bans, the ban removal is async, so we might return from the cancellation
// while the ban is still there, but it should disappear shortly
assertBusy(() -> {
for (int i = 0; i < testNodes.length; i++) {
assertEquals("No bans on the node " + i, 0, testNodes[i].transportService.getTaskManager().getBanCount());
}
});
}
use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.
the class TaskTests method testTaskInfoToString.
public void testTaskInfoToString() {
String nodeId = randomAlphaOfLength(10);
long taskId = randomIntBetween(0, 100000);
long startTime = randomNonNegativeLong();
long runningTime = randomNonNegativeLong();
boolean cancellable = false;
boolean cancelled = false;
TaskInfo taskInfo = new TaskInfo(new TaskId(nodeId, taskId), "test_type", "test_action", "test_description", null, startTime, runningTime, cancellable, cancelled, TaskId.EMPTY_TASK_ID, Collections.singletonMap("foo", "bar"));
String taskInfoString = taskInfo.toString();
Map<String, Object> map = XContentHelper.convertToMap(new BytesArray(taskInfoString.getBytes(StandardCharsets.UTF_8)), true).v2();
assertEquals(((Number) map.get("id")).longValue(), taskId);
assertEquals(map.get("type"), "test_type");
assertEquals(map.get("action"), "test_action");
assertEquals(map.get("description"), "test_description");
assertEquals(((Number) map.get("start_time_in_millis")).longValue(), startTime);
assertEquals(((Number) map.get("running_time_in_nanos")).longValue(), runningTime);
assertEquals(map.get("cancellable"), cancellable);
assertEquals(map.get("cancelled"), cancelled);
assertEquals(map.get("headers"), Collections.singletonMap("foo", "bar"));
}
use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.
the class TaskTests method testCancellableOptionWhenCancelledTrue.
public void testCancellableOptionWhenCancelledTrue() {
String nodeId = randomAlphaOfLength(10);
long taskId = randomIntBetween(0, 100000);
long startTime = randomNonNegativeLong();
long runningTime = randomNonNegativeLong();
boolean cancellable = true;
boolean cancelled = true;
TaskInfo taskInfo = new TaskInfo(new TaskId(nodeId, taskId), "test_type", "test_action", "test_description", null, startTime, runningTime, cancellable, cancelled, TaskId.EMPTY_TASK_ID, Collections.singletonMap("foo", "bar"));
String taskInfoString = taskInfo.toString();
Map<String, Object> map = XContentHelper.convertToMap(new BytesArray(taskInfoString.getBytes(StandardCharsets.UTF_8)), true).v2();
assertEquals(map.get("cancellable"), cancellable);
assertEquals(map.get("cancelled"), cancelled);
}
Aggregations