Search in sources :

Example 1 with DetectorInternalState

use of org.opensearch.ad.model.DetectorInternalState 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);
}
Also used : GetRequest(org.opensearch.action.get.GetRequest) DetectorInternalState(org.opensearch.ad.model.DetectorInternalState) ADTaskType.taskTypeToString(org.opensearch.ad.model.ADTaskType.taskTypeToString) IOException(java.io.IOException) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) AnomalyDetectorFunction(org.opensearch.ad.rest.handler.AnomalyDetectorFunction) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

IOException (java.io.IOException)1 GetRequest (org.opensearch.action.get.GetRequest)1 ADTaskType.taskTypeToString (org.opensearch.ad.model.ADTaskType.taskTypeToString)1 AnomalyDetectorJob (org.opensearch.ad.model.AnomalyDetectorJob)1 DetectorInternalState (org.opensearch.ad.model.DetectorInternalState)1 AnomalyDetectorFunction (org.opensearch.ad.rest.handler.AnomalyDetectorFunction)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1