use of org.opensearch.ad.model.ADTask.PARENT_TASK_ID_FIELD 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.ADTask.PARENT_TASK_ID_FIELD in project anomaly-detection by opensearch-project.
the class ADTaskManager method getAndExecuteOnLatestADTasks.
/**
* Get latest AD tasks and execute consumer function.
* If resetTaskState is true, will collect latest task's profile data from all data nodes. If no data
* node running the latest task, will reset the task state as STOPPED; otherwise, check if there is
* any stale running entities(entity exists in coordinating node cache but no task running on worker
* node) and clean up.
* [Important!] Make sure listener returns in function
*
* @param detectorId detector id
* @param parentTaskId parent task id
* @param entity entity value
* @param adTaskTypes AD task types
* @param function consumer function
* @param transportService transport service
* @param resetTaskState reset task state or not
* @param size return how many AD tasks
* @param listener action listener
* @param <T> response type of action listener
*/
public <T> void getAndExecuteOnLatestADTasks(String detectorId, String parentTaskId, Entity entity, List<ADTaskType> adTaskTypes, Consumer<List<ADTask>> function, TransportService transportService, boolean resetTaskState, int size, ActionListener<T> listener) {
BoolQueryBuilder query = new BoolQueryBuilder();
query.filter(new TermQueryBuilder(DETECTOR_ID_FIELD, detectorId));
query.filter(new TermQueryBuilder(IS_LATEST_FIELD, true));
if (parentTaskId != null) {
query.filter(new TermQueryBuilder(PARENT_TASK_ID_FIELD, parentTaskId));
}
if (adTaskTypes != null && adTaskTypes.size() > 0) {
query.filter(new TermsQueryBuilder(TASK_TYPE_FIELD, taskTypeToString(adTaskTypes)));
}
if (entity != null && !isNullOrEmpty(entity.getAttributes())) {
String path = "entity";
String entityKeyFieldName = path + ".name";
String entityValueFieldName = path + ".value";
for (Map.Entry<String, String> attribute : entity.getAttributes().entrySet()) {
BoolQueryBuilder entityBoolQuery = new BoolQueryBuilder();
TermQueryBuilder entityKeyFilterQuery = QueryBuilders.termQuery(entityKeyFieldName, attribute.getKey());
TermQueryBuilder entityValueFilterQuery = QueryBuilders.termQuery(entityValueFieldName, attribute.getValue());
entityBoolQuery.filter(entityKeyFilterQuery).filter(entityValueFilterQuery);
NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder(path, entityBoolQuery, ScoreMode.None);
query.filter(nestedQueryBuilder);
}
}
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(query).sort(EXECUTION_START_TIME_FIELD, SortOrder.DESC).size(size);
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(sourceBuilder);
searchRequest.indices(DETECTION_STATE_INDEX);
client.search(searchRequest, ActionListener.wrap(r -> {
// https://github.com/opendistro-for-elasticsearch/anomaly-detection/pull/359#discussion_r558653132
// getTotalHits will be null when we track_total_hits is false in the query request.
// Add more checking here to cover some unknown cases.
List<ADTask> adTasks = new ArrayList<>();
if (r == null || r.getHits().getTotalHits() == null || r.getHits().getTotalHits().value == 0) {
// don't throw exception here as consumer functions need to handle missing task
// in different way.
function.accept(adTasks);
return;
}
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);
ADTask adTask = ADTask.parse(parser, searchHit.getId());
adTasks.add(adTask);
} catch (Exception e) {
String message = "Failed to parse AD task for detector " + detectorId + ", task id " + searchHit.getId();
logger.error(message, e);
listener.onFailure(new OpenSearchStatusException(message, RestStatus.INTERNAL_SERVER_ERROR));
}
}
if (resetTaskState) {
resetLatestDetectorTaskState(adTasks, function, transportService, listener);
} else {
function.accept(adTasks);
}
}, e -> {
if (e instanceof IndexNotFoundException) {
function.accept(new ArrayList<>());
} else {
logger.error("Failed to search AD task for detector " + detectorId, e);
listener.onFailure(e);
}
}));
}
use of org.opensearch.ad.model.ADTask.PARENT_TASK_ID_FIELD in project anomaly-detection by opensearch-project.
the class ADTaskManager method countEntityTasksByState.
/**
* Count entity tasks by state with detector level task id(parent task id).
*
* @param detectorTaskId detector level task id
* @param taskStates task states
* @param listener action listener
*/
public void countEntityTasksByState(String detectorTaskId, List<ADTaskState> taskStates, ActionListener<Long> listener) {
BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
queryBuilder.filter(new TermQueryBuilder(PARENT_TASK_ID_FIELD, detectorTaskId));
if (taskStates != null && taskStates.size() > 0) {
queryBuilder.filter(new TermsQueryBuilder(STATE_FIELD, taskStates.stream().map(s -> s.name()).collect(Collectors.toList())));
}
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(queryBuilder);
sourceBuilder.size(0);
sourceBuilder.trackTotalHits(true);
SearchRequest request = new SearchRequest();
request.source(sourceBuilder);
request.indices(DETECTION_STATE_INDEX);
client.search(request, ActionListener.wrap(r -> {
TotalHits totalHits = r.getHits().getTotalHits();
listener.onResponse(totalHits.value);
}, e -> listener.onFailure(e)));
}
use of org.opensearch.ad.model.ADTask.PARENT_TASK_ID_FIELD in project anomaly-detection by opensearch-project.
the class ADTaskManager method cleanChildTasksAndADResultsOfDeletedTask.
/**
* Poll deleted detector task from cache and delete its child tasks and AD results.
*/
public void cleanChildTasksAndADResultsOfDeletedTask() {
if (!adTaskCacheManager.hasDeletedDetectorTask()) {
return;
}
threadPool.schedule(() -> {
String taskId = adTaskCacheManager.pollDeletedDetectorTask();
if (taskId == null) {
return;
}
DeleteByQueryRequest deleteADResultsRequest = new DeleteByQueryRequest(ALL_AD_RESULTS_INDEX_PATTERN);
deleteADResultsRequest.setQuery(new TermsQueryBuilder(TASK_ID_FIELD, taskId));
client.execute(DeleteByQueryAction.INSTANCE, deleteADResultsRequest, ActionListener.wrap(res -> {
logger.debug("Successfully deleted AD results of task " + taskId);
DeleteByQueryRequest deleteChildTasksRequest = new DeleteByQueryRequest(DETECTION_STATE_INDEX);
deleteChildTasksRequest.setQuery(new TermsQueryBuilder(PARENT_TASK_ID_FIELD, taskId));
client.execute(DeleteByQueryAction.INSTANCE, deleteChildTasksRequest, ActionListener.wrap(r -> {
logger.debug("Successfully deleted child tasks of task " + taskId);
cleanChildTasksAndADResultsOfDeletedTask();
}, e -> {
logger.error("Failed to delete child tasks of task " + taskId, e);
}));
}, ex -> {
logger.error("Failed to delete AD results for task " + taskId, ex);
}));
}, TimeValue.timeValueSeconds(DEFAULT_MAINTAIN_INTERVAL_IN_SECONDS), AD_BATCH_TASK_THREAD_POOL_NAME);
}
Aggregations