use of org.opensearch.ad.model.ADTaskState.NOT_ENDED_STATES in project anomaly-detection by opensearch-project.
the class ADTaskManager method resetEntityTasksAsStopped.
private void resetEntityTasksAsStopped(String detectorTaskId) {
UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest();
updateByQueryRequest.indices(DETECTION_STATE_INDEX);
BoolQueryBuilder query = new BoolQueryBuilder();
query.filter(new TermQueryBuilder(PARENT_TASK_ID_FIELD, detectorTaskId));
query.filter(new TermQueryBuilder(TASK_TYPE_FIELD, ADTaskType.HISTORICAL_HC_ENTITY.name()));
query.filter(new TermsQueryBuilder(STATE_FIELD, NOT_ENDED_STATES));
updateByQueryRequest.setQuery(query);
updateByQueryRequest.setRefresh(true);
String script = String.format(Locale.ROOT, "ctx._source.%s='%s';", STATE_FIELD, ADTaskState.STOPPED.name());
updateByQueryRequest.setScript(new Script(script));
client.execute(UpdateByQueryAction.INSTANCE, updateByQueryRequest, ActionListener.wrap(r -> {
List<BulkItemResponse.Failure> bulkFailures = r.getBulkFailures();
if (isNullOrEmpty(bulkFailures)) {
logger.debug("Updated {} child entity tasks state for detector task {}", r.getUpdated(), detectorTaskId);
} else {
logger.error("Failed to update child entity task's state for detector task {} ", detectorTaskId);
}
}, e -> logger.error("Exception happened when update child entity task's state for detector task " + detectorTaskId, e)));
}
use of org.opensearch.ad.model.ADTaskState.NOT_ENDED_STATES in project anomaly-detection by opensearch-project.
the class ADTaskManager method maintainRunningHistoricalTasks.
// =========================================================
// Methods below are maintenance code triggered by hourly cron
// =========================================================
/**
* Maintain running historical tasks.
* Search current running latest tasks, then maintain tasks one by one.
* Get task profile to check if task is really running on worker node.
* 1. If not running, reset task state as STOPPED.
* 2. If task is running and task for HC detector, check if there is any stale running entities and
* clean up.
*
* @param transportService transport service
* @param size return how many tasks
*/
public void maintainRunningHistoricalTasks(TransportService transportService, int size) {
// Clean expired HC batch task run state cache.
adTaskCacheManager.cleanExpiredHCBatchTaskRunStates();
// Find owning node with highest AD version to make sure we only have 1 node maintain running historical tasks
// and we use the latest logic.
Optional<DiscoveryNode> owningNode = hashRing.getOwningNodeWithHighestAdVersion(AD_TASK_MAINTAINENCE_NODE_MODEL_ID);
if (!owningNode.isPresent() || !clusterService.localNode().getId().equals(owningNode.get().getId())) {
return;
}
logger.info("Start to maintain running historical tasks");
BoolQueryBuilder query = new BoolQueryBuilder();
query.filter(new TermQueryBuilder(IS_LATEST_FIELD, true));
query.filter(new TermsQueryBuilder(TASK_TYPE_FIELD, taskTypeToString(HISTORICAL_DETECTOR_TASK_TYPES)));
query.filter(new TermsQueryBuilder(STATE_FIELD, NOT_ENDED_STATES));
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// default maintain interval is 5 seconds, so maintain 10 tasks will take at least 50 seconds.
sourceBuilder.query(query).sort(LAST_UPDATE_TIME_FIELD, SortOrder.DESC).size(size);
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(sourceBuilder);
searchRequest.indices(DETECTION_STATE_INDEX);
client.search(searchRequest, ActionListener.wrap(r -> {
if (r == null || r.getHits().getTotalHits() == null || r.getHits().getTotalHits().value == 0) {
return;
}
ConcurrentLinkedQueue<ADTask> taskQueue = new ConcurrentLinkedQueue<>();
Iterator<SearchHit> iterator = r.getHits().iterator();
while (iterator.hasNext()) {
SearchHit searchHit = iterator.next();
try (XContentParser parser = createXContentParserFromRegistry(xContentRegistry, searchHit.getSourceRef())) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
taskQueue.add(ADTask.parse(parser, searchHit.getId()));
} catch (Exception e) {
logger.error("Maintaining running historical task: failed to parse AD task " + searchHit.getId(), e);
}
}
maintainRunningHistoricalTask(taskQueue, transportService);
}, e -> {
if (e instanceof IndexNotFoundException) {
// the method will be called hourly
// don't log stack trace as most of OpenSearch domains have no AD installed
logger.debug(STATE_INDEX_NOT_EXIST_MSG);
} else {
logger.error("Failed to search historical tasks in maintaining job", e);
}
}));
}
Aggregations