use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.
the class TaskTests method testCancellableOptionWhenCancelledFalse.
public void testCancellableOptionWhenCancelledFalse() {
String nodeId = randomAlphaOfLength(10);
long taskId = randomIntBetween(0, 100000);
long startTime = randomNonNegativeLong();
long runningTime = randomNonNegativeLong();
boolean cancellable = true;
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(map.get("cancellable"), cancellable);
assertEquals(map.get("cancelled"), cancelled);
}
use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.
the class TaskTests method testNonCancellableOption.
public void testNonCancellableOption() {
String nodeId = randomAlphaOfLength(10);
long taskId = randomIntBetween(0, 100000);
long startTime = randomNonNegativeLong();
long runningTime = randomNonNegativeLong();
boolean cancellable = false;
boolean cancelled = true;
Exception e = expectThrows(IllegalArgumentException.class, () -> 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")));
assertEquals(e.getMessage(), "task cannot be cancelled");
}
use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.
the class CancelTasksResponseTests method assertInstances.
@Override
protected void assertInstances(ByNodeCancelTasksResponse serverTestInstance, org.opensearch.client.tasks.CancelTasksResponse clientInstance) {
// checking tasks
List<TaskInfo> sTasks = serverTestInstance.getTasks();
List<org.opensearch.client.tasks.TaskInfo> cTasks = clientInstance.getTasks();
Map<org.opensearch.client.tasks.TaskId, org.opensearch.client.tasks.TaskInfo> cTasksMap = cTasks.stream().collect(Collectors.toMap(org.opensearch.client.tasks.TaskInfo::getTaskId, Function.identity()));
for (TaskInfo ti : sTasks) {
org.opensearch.client.tasks.TaskInfo taskInfo = cTasksMap.get(new org.opensearch.client.tasks.TaskId(ti.getTaskId().getNodeId(), ti.getTaskId().getId()));
assertEquals(ti.getAction(), taskInfo.getAction());
assertEquals(ti.getDescription(), taskInfo.getDescription());
assertEquals(new HashMap<>(ti.getHeaders()), new HashMap<>(taskInfo.getHeaders()));
assertEquals(ti.getType(), taskInfo.getType());
assertEquals(ti.getStartTime(), taskInfo.getStartTime());
assertEquals(ti.getRunningTimeNanos(), taskInfo.getRunningTimeNanos());
assertEquals(ti.isCancellable(), taskInfo.isCancellable());
assertEquals(ti.isCancelled(), taskInfo.isCancelled());
assertEquals(ti.getParentTaskId().getNodeId(), taskInfo.getParentTaskId().getNodeId());
assertEquals(ti.getParentTaskId().getId(), taskInfo.getParentTaskId().getId());
FakeTaskStatus status = (FakeTaskStatus) ti.getStatus();
assertEquals(status.code, taskInfo.getStatus().get("code"));
assertEquals(status.status, taskInfo.getStatus().get("status"));
}
// checking failures
List<OpenSearchException> serverNodeFailures = serverTestInstance.getNodeFailures();
List<org.opensearch.client.tasks.OpenSearchException> cNodeFailures = clientInstance.getNodeFailures();
List<String> sExceptionsMessages = serverNodeFailures.stream().map(x -> org.opensearch.client.tasks.OpenSearchException.buildMessage("exception", x.getMessage(), null)).collect(Collectors.toList());
List<String> cExceptionsMessages = cNodeFailures.stream().map(org.opensearch.client.tasks.OpenSearchException::getMsg).collect(Collectors.toList());
assertEquals(new HashSet<>(sExceptionsMessages), new HashSet<>(cExceptionsMessages));
List<TaskOperationFailure> sTaskFailures = serverTestInstance.getTaskFailures();
List<org.opensearch.client.tasks.TaskOperationFailure> cTaskFailures = clientInstance.getTaskFailures();
Map<Long, org.opensearch.client.tasks.TaskOperationFailure> cTasksFailuresMap = cTaskFailures.stream().collect(Collectors.toMap(org.opensearch.client.tasks.TaskOperationFailure::getTaskId, Function.identity()));
for (TaskOperationFailure tof : sTaskFailures) {
org.opensearch.client.tasks.TaskOperationFailure failure = cTasksFailuresMap.get(tof.getTaskId());
assertEquals(tof.getNodeId(), failure.getNodeId());
assertTrue(failure.getReason().getMsg().contains("runtime_exception"));
assertTrue(failure.getStatus().contains("" + tof.getStatus().name()));
}
}
use of org.opensearch.tasks.TaskInfo in project asynchronous-search by opensearch-project.
the class AsynchronousSearchCancellationIT method cancelSearch.
private void cancelSearch(String action) {
ListTasksResponse listTasksResponse = client().admin().cluster().prepareListTasks().setActions(action).get();
assertThat(listTasksResponse.getTasks(), hasSize(1));
TaskInfo searchTask = listTasksResponse.getTasks().get(0);
logger.info("Cancelling search");
CancelTasksResponse cancelTasksResponse = client().admin().cluster().prepareCancelTasks().setTaskId(searchTask.getTaskId()).get();
assertThat(cancelTasksResponse.getTasks(), hasSize(1));
assertThat(cancelTasksResponse.getTasks().get(0).getTaskId(), equalTo(searchTask.getTaskId()));
}
use of org.opensearch.tasks.TaskInfo in project anomaly-detection by opensearch-project.
the class ClientUtil method onListTaskResponse.
/**
* Helper function to handle ListTasksResponse
* @param listTasksResponse ListTasksResponse
* @param detectorId Anomaly Detector Id
* @param LOG Logger
*/
private void onListTaskResponse(ListTasksResponse listTasksResponse, String detectorId, Logger LOG) {
List<TaskInfo> tasks = listTasksResponse.getTasks();
TaskId matchedParentTaskId = null;
TaskId matchedSingleTaskId = null;
for (TaskInfo task : tasks) {
if (!task.getHeaders().isEmpty() && task.getHeaders().get(Task.X_OPAQUE_ID).equals(CommonName.ANOMALY_DETECTOR + ":" + detectorId)) {
if (!task.getParentTaskId().equals(TaskId.EMPTY_TASK_ID)) {
// we found the parent task, don't need to check more
matchedParentTaskId = task.getParentTaskId();
break;
} else {
// we found one task, keep checking other tasks
matchedSingleTaskId = task.getTaskId();
}
}
}
// case 1: given detectorId is not in current task list
if (matchedParentTaskId == null && matchedSingleTaskId == null) {
// log and then clear negative cache
LOG.info("Couldn't find task for detectorId: {}. Clean this entry from Throttler", detectorId);
throttler.clearFilteredQuery(detectorId);
return;
}
// case 2: we can find the task for given detectorId
CancelTasksRequest cancelTaskRequest = new CancelTasksRequest();
if (matchedParentTaskId != null) {
cancelTaskRequest.setParentTaskId(matchedParentTaskId);
LOG.info("Start to cancel task for parentTaskId: {}", matchedParentTaskId.toString());
} else {
cancelTaskRequest.setTaskId(matchedSingleTaskId);
LOG.info("Start to cancel task for taskId: {}", matchedSingleTaskId.toString());
}
client.execute(CancelTasksAction.INSTANCE, cancelTaskRequest, ActionListener.wrap(response -> {
onCancelTaskResponse(response, detectorId, LOG);
}, exception -> {
LOG.error("Failed to cancel task for detectorId: " + detectorId, exception);
throw new InternalFailure(detectorId, "Failed to cancel current tasks", exception);
}));
}
Aggregations