Search in sources :

Example 1 with ADTaskCancelledException

use of org.opensearch.ad.common.exception.ADTaskCancelledException in project anomaly-detection by opensearch-project.

the class ADTaskManager method handleADTaskException.

/**
 * Handle exceptions for AD task. Update task state and record error message.
 *
 * @param adTask AD task
 * @param e exception
 */
public void handleADTaskException(ADTask adTask, Exception e) {
    // TODO: handle timeout exception
    String state = ADTaskState.FAILED.name();
    Map<String, Object> updatedFields = new HashMap<>();
    if (e instanceof DuplicateTaskException) {
        // If user send multiple start detector request, we will meet race condition.
        // Cache manager will put first request in cache and throw DuplicateTaskException
        // for the second request. We will delete the second task.
        logger.warn("There is already one running task for detector, detectorId:" + adTask.getDetectorId() + ". Will delete task " + adTask.getTaskId());
        deleteADTask(adTask.getTaskId());
        return;
    }
    if (e instanceof ADTaskCancelledException) {
        logger.info("AD task cancelled, taskId: {}, detectorId: {}", adTask.getTaskId(), adTask.getDetectorId());
        state = ADTaskState.STOPPED.name();
        String stoppedBy = ((ADTaskCancelledException) e).getCancelledBy();
        if (stoppedBy != null) {
            updatedFields.put(STOPPED_BY_FIELD, stoppedBy);
        }
    } else {
        logger.error("Failed to execute AD batch task, task id: " + adTask.getTaskId() + ", detector id: " + adTask.getDetectorId(), e);
    }
    updatedFields.put(ERROR_FIELD, getErrorMessage(e));
    updatedFields.put(STATE_FIELD, state);
    updatedFields.put(EXECUTION_END_TIME_FIELD, Instant.now().toEpochMilli());
    updateADTask(adTask.getTaskId(), updatedFields);
}
Also used : HashMap(java.util.HashMap) ADTaskType.taskTypeToString(org.opensearch.ad.model.ADTaskType.taskTypeToString) DuplicateTaskException(org.opensearch.ad.common.exception.DuplicateTaskException) ADTaskCancelledException(org.opensearch.ad.common.exception.ADTaskCancelledException)

Aggregations

HashMap (java.util.HashMap)1 ADTaskCancelledException (org.opensearch.ad.common.exception.ADTaskCancelledException)1 DuplicateTaskException (org.opensearch.ad.common.exception.DuplicateTaskException)1 ADTaskType.taskTypeToString (org.opensearch.ad.model.ADTaskType.taskTypeToString)1