use of org.opensearch.ad.rest.handler.AnomalyDetectorFunction in project anomaly-detection by opensearch-project.
the class ADDataMigrator method backfillRealtimeTask.
/**
* Backfill realtiem task for realtime job.
* @param detectorJobs realtime AD jobs
* @param backfillAllJob backfill task for all realtime job or not
*/
public void backfillRealtimeTask(ConcurrentLinkedQueue<AnomalyDetectorJob> detectorJobs, boolean backfillAllJob) {
AnomalyDetectorJob job = detectorJobs.poll();
if (job == null) {
logger.info("AD data migration done.");
if (backfillAllJob) {
this.dataMigrated.set(true);
}
return;
}
String jobId = job.getName();
AnomalyDetectorFunction createRealtimeTaskFunction = () -> {
GetRequest getRequest = new GetRequest(DETECTION_STATE_INDEX, jobId);
client.get(getRequest, ActionListener.wrap(r -> {
if (r != null && r.isExists()) {
try (XContentParser parser = createXContentParserFromRegistry(xContentRegistry, r.getSourceAsBytesRef())) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
DetectorInternalState detectorState = DetectorInternalState.parse(parser);
createRealtimeADTask(job, detectorState.getError(), detectorJobs, backfillAllJob);
} catch (IOException e) {
logger.error("Failed to parse detector internal state " + jobId, e);
createRealtimeADTask(job, null, detectorJobs, backfillAllJob);
}
} else {
createRealtimeADTask(job, null, detectorJobs, backfillAllJob);
}
}, e -> {
logger.error("Failed to query detector internal state " + jobId, e);
createRealtimeADTask(job, null, detectorJobs, backfillAllJob);
}));
};
checkIfRealtimeTaskExistsAndBackfill(job, createRealtimeTaskFunction, detectorJobs, backfillAllJob);
}
use of org.opensearch.ad.rest.handler.AnomalyDetectorFunction 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.rest.handler.AnomalyDetectorFunction in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method testDeleteADTasksWithBulkFailures.
@SuppressWarnings("unchecked")
public void testDeleteADTasksWithBulkFailures() {
doAnswer(invocation -> {
ActionListener<BulkByScrollResponse> actionListener = invocation.getArgument(2);
BulkByScrollResponse response = mock(BulkByScrollResponse.class);
List<BulkItemResponse.Failure> failures = ImmutableList.of(new BulkItemResponse.Failure(DETECTION_STATE_INDEX, CommonName.MAPPING_TYPE, randomAlphaOfLength(5), new VersionConflictEngineException(new ShardId(DETECTION_STATE_INDEX, "", 1), "id", "test")));
when(response.getBulkFailures()).thenReturn(failures);
actionListener.onResponse(response);
return null;
}).when(client).execute(any(), any(), any());
String detectorId = randomAlphaOfLength(5);
AnomalyDetectorFunction function = mock(AnomalyDetectorFunction.class);
ActionListener<DeleteResponse> listener = mock(ActionListener.class);
adTaskManager.deleteADTasks(detectorId, function, listener);
verify(listener, times(1)).onFailure(any());
}
use of org.opensearch.ad.rest.handler.AnomalyDetectorFunction in project anomaly-detection by opensearch-project.
the class IndexAnomalyDetectorTransportAction method checkIndicesAndExecute.
private void checkIndicesAndExecute(List<String> indices, AnomalyDetectorFunction function, ActionListener<IndexAnomalyDetectorResponse> listener) {
SearchRequest searchRequest = new SearchRequest().indices(indices.toArray(new String[0])).source(new SearchSourceBuilder().size(1).query(QueryBuilders.matchAllQuery()));
client.search(searchRequest, ActionListener.wrap(r -> {
function.execute();
}, e -> {
// Due to below issue with security plugin, we get security_exception when invalid index name is mentioned.
// https://github.com/opendistro-for-elasticsearch/security/issues/718
LOG.error(e);
listener.onFailure(e);
}));
}
use of org.opensearch.ad.rest.handler.AnomalyDetectorFunction in project anomaly-detection by opensearch-project.
the class ADTaskManager method cleanDetectorCache.
protected void cleanDetectorCache(ADTask adTask, TransportService transportService, AnomalyDetectorFunction function) {
String detectorId = adTask.getDetectorId();
String taskId = adTask.getTaskId();
cleanDetectorCache(adTask, transportService, function, ActionListener.wrap(r -> {
logger.debug("Successfully cleaned cache for detector {}, task {}", detectorId, taskId);
}, e -> {
logger.error("Failed to clean cache for detector " + detectorId + ", task " + taskId, e);
}));
}
Aggregations