use of org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX in project anomaly-detection by opensearch-project.
the class ADDataMigrator method migrateDetectorInternalStateToRealtimeTask.
/**
* Migrate detector internal state to realtime task.
*/
public void migrateDetectorInternalStateToRealtimeTask() {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(new MatchAllQueryBuilder()).size(MAX_DETECTOR_UPPER_LIMIT);
SearchRequest searchRequest = new SearchRequest(ANOMALY_DETECTOR_JOB_INDEX).source(searchSourceBuilder);
client.search(searchRequest, ActionListener.wrap(r -> {
if (r == null || r.getHits().getTotalHits() == null || r.getHits().getTotalHits().value == 0) {
logger.info("No anomaly detector job found, no need to migrate");
return;
}
ConcurrentLinkedQueue<AnomalyDetectorJob> detectorJobs = 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);
AnomalyDetectorJob job = AnomalyDetectorJob.parse(parser);
detectorJobs.add(job);
} catch (IOException e) {
logger.error("Fail to parse AD job " + searchHit.getId(), e);
}
}
logger.info("Total AD jobs to backfill realtime task: {}", detectorJobs.size());
backfillRealtimeTask(detectorJobs, true);
}, e -> {
if (ExceptionUtil.getErrorMessage(e).contains("all shards failed")) {
// This error may happen when AD job index not ready for query as some nodes not in cluster yet.
// Will recreate realtime task when AD job starts.
logger.warn("No available shards of AD job index, reset dataMigrated as false");
this.dataMigrated.set(false);
} else if (!(e instanceof IndexNotFoundException)) {
logger.error("Failed to migrate AD data", e);
}
}));
}
use of org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX in project anomaly-detection by opensearch-project.
the class EntityProfileRunnerTests method testJobIndexNotFound.
@SuppressWarnings("unchecked")
public void testJobIndexNotFound() throws InterruptedException {
setUpExecuteEntityProfileAction(InittedEverResultStatus.INITTED);
final CountDownLatch inProgressLatch = new CountDownLatch(1);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
GetRequest request = (GetRequest) args[0];
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
String indexName = request.index();
if (indexName.equals(ANOMALY_DETECTORS_INDEX)) {
listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
} else if (indexName.equals(ANOMALY_DETECTOR_JOB_INDEX)) {
listener.onFailure(new IndexNotFoundException(ANOMALY_DETECTOR_JOB_INDEX));
}
return null;
}).when(client).get(any(), any());
EntityProfile expectedProfile = new EntityProfile.Builder().build();
runner.profile(detectorId, entity, initNInfo, ActionListener.wrap(response -> {
assertEquals(expectedProfile, response);
inProgressLatch.countDown();
}, exception -> {
LOG.error("Unexpected error", exception);
assertTrue("Should not reach here", false);
inProgressLatch.countDown();
}));
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
use of org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX in project anomaly-detection by opensearch-project.
the class ADTaskManager method resetRealtimeDetectorTaskState.
private <T> void resetRealtimeDetectorTaskState(List<ADTask> runningRealtimeTasks, AnomalyDetectorFunction function, TransportService transportService, ActionListener<T> listener) {
if (isNullOrEmpty(runningRealtimeTasks)) {
function.execute();
return;
}
ADTask adTask = runningRealtimeTasks.get(0);
String detectorId = adTask.getDetectorId();
GetRequest getJobRequest = new GetRequest(ANOMALY_DETECTOR_JOB_INDEX).id(detectorId);
client.get(getJobRequest, ActionListener.wrap(r -> {
if (r.isExists()) {
try (XContentParser parser = createXContentParserFromRegistry(xContentRegistry, r.getSourceAsBytesRef())) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
AnomalyDetectorJob job = AnomalyDetectorJob.parse(parser);
if (!job.isEnabled()) {
logger.debug("AD job is disabled, reset realtime task as stopped for detector {}", detectorId);
resetTaskStateAsStopped(adTask, function, transportService, listener);
} else {
function.execute();
}
} catch (IOException e) {
logger.error(" Failed to parse AD job " + detectorId, e);
listener.onFailure(e);
}
} else {
logger.debug("AD job is not found, reset realtime task as stopped for detector {}", detectorId);
resetTaskStateAsStopped(adTask, function, transportService, listener);
}
}, e -> {
logger.error("Fail to get AD realtime job for detector " + detectorId, e);
listener.onFailure(e);
}));
}
use of org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX in project anomaly-detection by opensearch-project.
the class DeleteAnomalyDetectorTransportAction method getDetectorJob.
private void getDetectorJob(String detectorId, ActionListener<DeleteResponse> listener, AnomalyDetectorFunction function) {
if (clusterService.state().metadata().indices().containsKey(ANOMALY_DETECTOR_JOB_INDEX)) {
GetRequest request = new GetRequest(ANOMALY_DETECTOR_JOB_INDEX).id(detectorId);
client.get(request, ActionListener.wrap(response -> onGetAdJobResponseForWrite(response, listener, function), exception -> {
LOG.error("Fail to get anomaly detector job: " + detectorId, exception);
listener.onFailure(exception);
}));
} else {
function.execute();
}
}
use of org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX in project anomaly-detection by opensearch-project.
the class AnomalyDetectorActionHandler method getDetectorJob.
/**
* Get detector job for update/delete AD job.
* If AD job exist, will return error message; otherwise, execute function.
*
* @param clusterService ES cluster service
* @param client ES node client
* @param detectorId detector identifier
* @param listener Listener to send response
* @param function AD function
* @param xContentRegistry Registry which is used for XContentParser
*/
public void getDetectorJob(ClusterService clusterService, Client client, String detectorId, ActionListener listener, AnomalyDetectorFunction function, NamedXContentRegistry xContentRegistry) {
if (clusterService.state().metadata().indices().containsKey(ANOMALY_DETECTOR_JOB_INDEX)) {
GetRequest request = new GetRequest(ANOMALY_DETECTOR_JOB_INDEX).id(detectorId);
client.get(request, ActionListener.wrap(response -> onGetAdJobResponseForWrite(response, listener, function, xContentRegistry), exception -> {
logger.error("Fail to get anomaly detector job: " + detectorId, exception);
listener.onFailure(exception);
}));
} else {
function.execute();
}
}
Aggregations