use of org.opensearch.ad.constant.CommonName.DETECTION_STATE_INDEX in project anomaly-detection by opensearch-project.
the class ADDataMigrator method checkIfRealtimeTaskExistsAndBackfill.
private void checkIfRealtimeTaskExistsAndBackfill(AnomalyDetectorJob job, AnomalyDetectorFunction createRealtimeTaskFunction, ConcurrentLinkedQueue<AnomalyDetectorJob> detectorJobs, boolean migrateAll) {
String jobId = job.getName();
BoolQueryBuilder query = new BoolQueryBuilder();
query.filter(new TermQueryBuilder(DETECTOR_ID_FIELD, jobId));
if (job.isEnabled()) {
query.filter(new TermQueryBuilder(IS_LATEST_FIELD, true));
}
query.filter(new TermsQueryBuilder(TASK_TYPE_FIELD, taskTypeToString(ADTaskType.REALTIME_TASK_TYPES)));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(query).size(1);
SearchRequest searchRequest = new SearchRequest(DETECTION_STATE_INDEX).source(searchSourceBuilder);
client.search(searchRequest, ActionListener.wrap(r -> {
if (r != null && r.getHits().getTotalHits().value > 0) {
// Backfill next realtime job
backfillRealtimeTask(detectorJobs, migrateAll);
return;
}
createRealtimeTaskFunction.execute();
}, e -> {
if (e instanceof ResourceNotFoundException) {
createRealtimeTaskFunction.execute();
}
logger.error("Failed to search tasks of detector " + jobId);
}));
}
use of org.opensearch.ad.constant.CommonName.DETECTION_STATE_INDEX in project anomaly-detection by opensearch-project.
the class ADTaskManager method deleteADTasks.
/**
* Delete AD tasks docs.
* [Important!] Make sure listener returns in function
*
* @param detectorId detector id
* @param function AD function
* @param listener action listener
*/
public void deleteADTasks(String detectorId, AnomalyDetectorFunction function, ActionListener<DeleteResponse> listener) {
DeleteByQueryRequest request = new DeleteByQueryRequest(DETECTION_STATE_INDEX);
BoolQueryBuilder query = new BoolQueryBuilder();
query.filter(new TermQueryBuilder(DETECTOR_ID_FIELD, detectorId));
request.setQuery(query);
client.execute(DeleteByQueryAction.INSTANCE, request, ActionListener.wrap(r -> {
if (r.getBulkFailures() == null || r.getBulkFailures().size() == 0) {
logger.info("AD tasks deleted for detector {}", detectorId);
deleteADResultOfDetector(detectorId);
function.execute();
} else {
listener.onFailure(new OpenSearchStatusException("Failed to delete all AD tasks", RestStatus.INTERNAL_SERVER_ERROR));
}
}, e -> {
logger.info("Failed to delete AD tasks for " + detectorId, e);
if (e instanceof IndexNotFoundException) {
deleteADResultOfDetector(detectorId);
function.execute();
} else {
listener.onFailure(e);
}
}));
}
use of org.opensearch.ad.constant.CommonName.DETECTION_STATE_INDEX in project anomaly-detection by opensearch-project.
the class ADTaskManager method createADTaskDirectly.
/**
* Create AD task directly without checking index exists of not.
* [Important!] Make sure listener returns in function
*
* @param adTask AD task
* @param function consumer function
* @param listener action listener
* @param <T> action listener response type
*/
public <T> void createADTaskDirectly(ADTask adTask, Consumer<IndexResponse> function, ActionListener<T> listener) {
IndexRequest request = new IndexRequest(DETECTION_STATE_INDEX);
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
request.source(adTask.toXContent(builder, RestHandlerUtils.XCONTENT_WITH_TYPE)).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
client.index(request, ActionListener.wrap(r -> function.accept(r), e -> {
logger.error("Failed to create AD task for detector " + adTask.getDetectorId(), e);
listener.onFailure(e);
}));
} catch (Exception e) {
logger.error("Failed to create AD task for detector " + adTask.getDetectorId(), e);
listener.onFailure(e);
}
}
use of org.opensearch.ad.constant.CommonName.DETECTION_STATE_INDEX 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.constant.CommonName.DETECTION_STATE_INDEX in project anomaly-detection by opensearch-project.
the class ADTaskManager method getADTask.
/**
* Get AD task with task id and execute listener.
* @param taskId task id
* @param listener action listener
*/
public void getADTask(String taskId, ActionListener<Optional<ADTask>> listener) {
GetRequest request = new GetRequest(DETECTION_STATE_INDEX, taskId);
client.get(request, ActionListener.wrap(r -> {
if (r != null && r.isExists()) {
try (XContentParser parser = createXContentParserFromRegistry(xContentRegistry, r.getSourceAsBytesRef())) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
ADTask adTask = ADTask.parse(parser, r.getId());
listener.onResponse(Optional.ofNullable(adTask));
} catch (Exception e) {
logger.error("Failed to parse AD task " + r.getId(), e);
listener.onFailure(e);
}
} else {
listener.onResponse(Optional.empty());
}
}, e -> {
if (e instanceof IndexNotFoundException) {
listener.onResponse(Optional.empty());
} else {
logger.error("Failed to get AD task " + taskId, e);
listener.onFailure(e);
}
}));
}
Aggregations