Search in sources :

Example 26 with TaskInfo

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);
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) BytesArray(org.opensearch.common.bytes.BytesArray) TaskId(org.opensearch.tasks.TaskId)

Example 27 with TaskInfo

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");
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) TaskId(org.opensearch.tasks.TaskId)

Example 28 with TaskInfo

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()));
    }
}
Also used : DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) AbstractResponseTestCase(org.opensearch.client.AbstractResponseTestCase) Version(org.opensearch.Version) StreamOutput(org.opensearch.common.io.stream.StreamOutput) HashMap(java.util.HashMap) OpenSearchException(org.opensearch.OpenSearchException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) XContentParser(org.opensearch.common.xcontent.XContentParser) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Map(java.util.Map) CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) StreamInput(org.opensearch.common.io.stream.StreamInput) Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptySet(java.util.Collections.emptySet) TaskId(org.opensearch.tasks.TaskId) IOException(java.io.IOException) Task(org.opensearch.tasks.Task) Collectors(java.util.stream.Collectors) TransportAddress(org.opensearch.common.transport.TransportAddress) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) TaskInfo(org.opensearch.tasks.TaskInfo) List(java.util.List) TaskOperationFailure(org.opensearch.action.TaskOperationFailure) XContentType(org.opensearch.common.xcontent.XContentType) Collections(java.util.Collections) TaskId(org.opensearch.tasks.TaskId) TaskInfo(org.opensearch.tasks.TaskInfo) TaskOperationFailure(org.opensearch.action.TaskOperationFailure) OpenSearchException(org.opensearch.OpenSearchException)

Example 29 with TaskInfo

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()));
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Example 30 with TaskInfo

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);
    }));
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) REQUEST_TIMEOUT(org.opensearch.ad.settings.AnomalyDetectorSettings.REQUEST_TIMEOUT) ThreadPool(org.opensearch.threadpool.ThreadPool) ActionRequest(org.opensearch.action.ActionRequest) OpenSearchException(org.opensearch.OpenSearchException) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) BiConsumer(java.util.function.BiConsumer) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) ActionResponse(org.opensearch.action.ActionResponse) ActionType(org.opensearch.action.ActionType) CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) Client(org.opensearch.client.Client) InternalFailure(org.opensearch.ad.common.exception.InternalFailure) CommonName(org.opensearch.ad.constant.CommonName) TimeValue(org.opensearch.common.unit.TimeValue) ListTasksAction(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksAction) TaskId(org.opensearch.tasks.TaskId) CancelTasksAction(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksAction) Settings(org.opensearch.common.settings.Settings) Task(org.opensearch.tasks.Task) ActionFuture(org.opensearch.action.ActionFuture) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) TaskInfo(org.opensearch.tasks.TaskInfo) Logger(org.apache.logging.log4j.Logger) CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) TaskOperationFailure(org.opensearch.action.TaskOperationFailure) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) Optional(java.util.Optional) TaskId(org.opensearch.tasks.TaskId) InternalFailure(org.opensearch.ad.common.exception.InternalFailure) CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest)

Aggregations

TaskInfo (org.opensearch.tasks.TaskInfo)47 TaskId (org.opensearch.tasks.TaskId)24 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)19 Task (org.opensearch.tasks.Task)11 CancelTasksResponse (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 IOException (java.io.IOException)7 OpenSearchException (org.opensearch.OpenSearchException)7 GetTaskResponse (org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse)7 PersistentTask (org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)7 TestParams (org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)7 TransportService (org.opensearch.transport.TransportService)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ActionListener (org.opensearch.action.ActionListener)6 TaskOperationFailure (org.opensearch.action.TaskOperationFailure)6