Search in sources :

Example 21 with DetectionDateRange

use of org.opensearch.ad.model.DetectionDateRange in project anomaly-detection by opensearch-project.

the class ADBatchTaskRunner method forwardOrExecuteADTask.

/**
 * Forward AD task to work node.
 * 1. For HC detector, return directly if no more pending entity. Otherwise check if
 *    there is AD task created for this entity. If yes, just forward the entity task
 *    to worker node; otherwise, create entity task first, then forward.
 * 2. For single entity detector, set task as INIT state and forward task to worker
 *    node.
 *
 * @param adTask AD task
 * @param transportService transport service
 * @param listener action listener
 */
public void forwardOrExecuteADTask(ADTask adTask, TransportService transportService, ActionListener<ADBatchAnomalyResultResponse> listener) {
    try {
        checkIfADTaskCancelledAndCleanupCache(adTask);
        String detectorId = adTask.getDetectorId();
        AnomalyDetector detector = adTask.getDetector();
        boolean isHCDetector = detector.isMultientityDetector();
        if (isHCDetector) {
            String entityString = adTaskCacheManager.pollEntity(detectorId);
            logger.debug("Start to run entity: {} of detector {}", entityString, detectorId);
            if (entityString == null) {
                listener.onResponse(new ADBatchAnomalyResultResponse(clusterService.localNode().getId(), false));
                return;
            }
            ActionListener<Object> wrappedListener = ActionListener.wrap(r -> logger.debug("Entity task created successfully"), e -> {
                logger.error("Failed to start entity task for detector: {}, entity: {}", detectorId, entityString);
                // If fail, move the entity into pending task queue
                adTaskCacheManager.addPendingEntity(detectorId, entityString);
            });
            // This is to handle retry case. To retry entity, we need to get the old entity task created before.
            Entity entity = adTaskManager.parseEntityFromString(entityString, adTask);
            String parentTaskId = adTask.getTaskType().equals(ADTaskType.HISTORICAL_HC_ENTITY.name()) ? // For HISTORICAL_HC_ENTITY task, return its parent task id
            adTask.getParentTaskId() : // For HISTORICAL_HC_DETECTOR task, its task id is parent task id
            adTask.getTaskId();
            adTaskManager.getAndExecuteOnLatestADTask(detectorId, parentTaskId, entity, ImmutableList.of(ADTaskType.HISTORICAL_HC_ENTITY), existingEntityTask -> {
                if (existingEntityTask.isPresent()) {
                    // retry failed entity caused by limit exceed exception
                    // TODO: if task failed due to limit exceed exception in half way, resume from the break point or just clear
                    // the
                    // old AD tasks and rerun it? Currently we just support rerunning task failed due to limit exceed exception
                    // before starting.
                    ADTask adEntityTask = existingEntityTask.get();
                    logger.debug("Rerun entity task for task id: {}, error of last run: {}", adEntityTask.getTaskId(), adEntityTask.getError());
                    ActionListener<ADBatchAnomalyResultResponse> workerNodeResponseListener = workerNodeResponseListener(adEntityTask, transportService, listener);
                    forwardOrExecuteEntityTask(adEntityTask, transportService, workerNodeResponseListener);
                } else {
                    logger.info("Create entity task for entity:{}", entityString);
                    Instant now = Instant.now();
                    ADTask adEntityTask = new ADTask.Builder().detectorId(adTask.getDetectorId()).detector(detector).isLatest(true).taskType(ADTaskType.HISTORICAL_HC_ENTITY.name()).executionStartTime(now).taskProgress(0.0f).initProgress(0.0f).state(ADTaskState.INIT.name()).initProgress(0.0f).lastUpdateTime(now).startedBy(adTask.getStartedBy()).coordinatingNode(clusterService.localNode().getId()).detectionDateRange(adTask.getDetectionDateRange()).user(adTask.getUser()).entity(entity).parentTaskId(parentTaskId).build();
                    adTaskManager.createADTaskDirectly(adEntityTask, r -> {
                        adEntityTask.setTaskId(r.getId());
                        ActionListener<ADBatchAnomalyResultResponse> workerNodeResponseListener = workerNodeResponseListener(adEntityTask, transportService, listener);
                        forwardOrExecuteEntityTask(adEntityTask, transportService, workerNodeResponseListener);
                    }, wrappedListener);
                }
            }, transportService, false, wrappedListener);
        } else {
            Map<String, Object> updatedFields = new HashMap<>();
            updatedFields.put(STATE_FIELD, ADTaskState.INIT.name());
            updatedFields.put(INIT_PROGRESS_FIELD, 0.0f);
            ActionListener<ADBatchAnomalyResultResponse> workerNodeResponseListener = workerNodeResponseListener(adTask, transportService, listener);
            adTaskManager.updateADTask(adTask.getTaskId(), updatedFields, ActionListener.wrap(r -> forwardOrExecuteEntityTask(adTask, transportService, workerNodeResponseListener), e -> {
                workerNodeResponseListener.onFailure(e);
            }));
        }
    } catch (Exception e) {
        logger.error("Failed to forward or execute AD task " + adTask.getTaskId(), e);
        listener.onFailure(e);
    }
}
Also used : AnomalyResultBulkIndexHandler(org.opensearch.ad.transport.handler.AnomalyResultBulkIndexHandler) ModelManager(org.opensearch.ad.ml.ModelManager) HashRing(org.opensearch.ad.cluster.HashRing) LimitExceededException(org.opensearch.ad.common.exception.LimitExceededException) ADTaskState(org.opensearch.ad.model.ADTaskState) AnomalyDetectorFunction(org.opensearch.ad.rest.handler.AnomalyDetectorFunction) ADIndex(org.opensearch.ad.indices.ADIndex) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) BATCH_TASK_PIECE_SIZE(org.opensearch.ad.settings.AnomalyDetectorSettings.BATCH_TASK_PIECE_SIZE) ADStatsNodeResponse(org.opensearch.ad.transport.ADStatsNodeResponse) MAX_RUNNING_ENTITIES_PER_DETECTOR_FOR_HISTORICAL_ANALYSIS(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_RUNNING_ENTITIES_PER_DETECTOR_FOR_HISTORICAL_ANALYSIS) Client(org.opensearch.client.Client) AD_BATCH_TASK_THREAD_POOL_NAME(org.opensearch.ad.AnomalyDetectorPlugin.AD_BATCH_TASK_THREAD_POOL_NAME) TimeValue(org.opensearch.common.unit.TimeValue) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) Settings(org.opensearch.common.settings.Settings) NO_ELIGIBLE_NODE_TO_RUN_DETECTOR(org.opensearch.ad.constant.CommonErrorMessages.NO_ELIGIBLE_NODE_TO_RUN_DETECTOR) TransportService(org.opensearch.transport.TransportService) RandomCutForest(com.amazon.randomcutforest.RandomCutForest) Logger(org.apache.logging.log4j.Logger) PriorityTracker(org.opensearch.ad.caching.PriorityTracker) ExceptionUtil(org.opensearch.ad.util.ExceptionUtil) ActionListenerResponseHandler(org.opensearch.action.ActionListenerResponseHandler) SearchFeatureDao(org.opensearch.ad.feature.SearchFeatureDao) CheckedRunnable(org.opensearch.common.CheckedRunnable) EXECUTION_END_TIME_FIELD(org.opensearch.ad.model.ADTask.EXECUTION_END_TIME_FIELD) InjectSecurity(org.opensearch.commons.InjectSecurity) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) BATCH_TASK_PIECE_INTERVAL_SECONDS(org.opensearch.ad.settings.AnomalyDetectorSettings.BATCH_TASK_PIECE_INTERVAL_SECONDS) ADCircuitBreakerService(org.opensearch.ad.breaker.ADCircuitBreakerService) MAX_BATCH_TASK_PER_NODE(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_BATCH_TASK_PER_NODE) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) ThreadPool(org.opensearch.threadpool.ThreadPool) EnabledSetting(org.opensearch.ad.settings.EnabledSetting) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) ArrayList(java.util.ArrayList) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) BiConsumer(java.util.function.BiConsumer) SearchRequest(org.opensearch.action.search.SearchRequest) INIT_PROGRESS_FIELD(org.opensearch.ad.model.ADTask.INIT_PROGRESS_FIELD) SinglePointFeatures(org.opensearch.ad.feature.SinglePointFeatures) FeatureManager(org.opensearch.ad.feature.FeatureManager) MAX_TOP_ENTITIES_FOR_HISTORICAL_ANALYSIS(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_TOP_ENTITIES_FOR_HISTORICAL_ANALYSIS) ADStatsNodesAction(org.opensearch.ad.transport.ADStatsNodesAction) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) ClusterService(org.opensearch.cluster.service.ClusterService) StatNames(org.opensearch.ad.stats.StatNames) ParseUtils(org.opensearch.ad.util.ParseUtils) InternalMin(org.opensearch.search.aggregations.metrics.InternalMin) AD_EXECUTING_BATCH_TASK_COUNT(org.opensearch.ad.stats.StatNames.AD_EXECUTING_BATCH_TASK_COUNT) ResourceNotFoundException(org.opensearch.ad.common.exception.ResourceNotFoundException) ADStats(org.opensearch.ad.stats.ADStats) ParseUtils.isNullOrEmpty(org.opensearch.ad.util.ParseUtils.isNullOrEmpty) ADTaskCancelledException(org.opensearch.ad.common.exception.ADTaskCancelledException) ADStatsRequest(org.opensearch.ad.transport.ADStatsRequest) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) TASK_PROGRESS_FIELD(org.opensearch.ad.model.ADTask.TASK_PROGRESS_FIELD) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) ADBatchTaskRemoteExecutionAction(org.opensearch.ad.transport.ADBatchTaskRemoteExecutionAction) ADTaskType(org.opensearch.ad.model.ADTaskType) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) AnomalyResult(org.opensearch.ad.model.AnomalyResult) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) DEFAULT_JVM_HEAP_USAGE_THRESHOLD(org.opensearch.ad.breaker.MemoryCircuitBreaker.DEFAULT_JVM_HEAP_USAGE_THRESHOLD) DetectionDateRange(org.opensearch.ad.model.DetectionDateRange) NUM_MIN_SAMPLES(org.opensearch.ad.settings.AnomalyDetectorSettings.NUM_MIN_SAMPLES) Optional(java.util.Optional) AnomalyDescriptor(com.amazon.randomcutforest.parkservices.AnomalyDescriptor) ADTask(org.opensearch.ad.model.ADTask) FeatureData(org.opensearch.ad.model.FeatureData) HashMap(java.util.HashMap) Deque(java.util.Deque) ThreadedActionListener(org.opensearch.action.support.ThreadedActionListener) CURRENT_PIECE_FIELD(org.opensearch.ad.model.ADTask.CURRENT_PIECE_FIELD) ImmutableList(com.google.common.collect.ImmutableList) ADBatchAnomalyResultResponse(org.opensearch.ad.transport.ADBatchAnomalyResultResponse) JVM_HEAP_USAGE(org.opensearch.ad.stats.InternalStatNames.JVM_HEAP_USAGE) EndRunException(org.opensearch.ad.common.exception.EndRunException) ADBatchAnomalyResultRequest(org.opensearch.ad.transport.ADBatchAnomalyResultRequest) AGG_NAME_MAX_TIME(org.opensearch.ad.constant.CommonName.AGG_NAME_MAX_TIME) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) MAX_TOP_ENTITIES_LIMIT_FOR_HISTORICAL_ANALYSIS(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_TOP_ENTITIES_LIMIT_FOR_HISTORICAL_ANALYSIS) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) STATE_FIELD(org.opensearch.ad.model.ADTask.STATE_FIELD) WORKER_NODE_FIELD(org.opensearch.ad.model.ADTask.WORKER_NODE_FIELD) Entity(org.opensearch.ad.model.Entity) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) ThresholdedRandomCutForest(com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest) Clock(java.time.Clock) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) LogManager(org.apache.logging.log4j.LogManager) AGG_NAME_MIN_TIME(org.opensearch.ad.constant.CommonName.AGG_NAME_MIN_TIME) Entity(org.opensearch.ad.model.Entity) HashMap(java.util.HashMap) Instant(java.time.Instant) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) LimitExceededException(org.opensearch.ad.common.exception.LimitExceededException) ResourceNotFoundException(org.opensearch.ad.common.exception.ResourceNotFoundException) ADTaskCancelledException(org.opensearch.ad.common.exception.ADTaskCancelledException) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) EndRunException(org.opensearch.ad.common.exception.EndRunException) ADBatchAnomalyResultResponse(org.opensearch.ad.transport.ADBatchAnomalyResultResponse) ADTask(org.opensearch.ad.model.ADTask)

Example 22 with DetectionDateRange

use of org.opensearch.ad.model.DetectionDateRange in project anomaly-detection by opensearch-project.

the class ADTaskManager method updateLatestFlagOfOldTasksAndCreateNewTask.

private void updateLatestFlagOfOldTasksAndCreateNewTask(AnomalyDetector detector, DetectionDateRange detectionDateRange, User user, ActionListener<AnomalyDetectorJobResponse> listener) {
    UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest();
    updateByQueryRequest.indices(DETECTION_STATE_INDEX);
    BoolQueryBuilder query = new BoolQueryBuilder();
    query.filter(new TermQueryBuilder(DETECTOR_ID_FIELD, detector.getDetectorId()));
    query.filter(new TermQueryBuilder(IS_LATEST_FIELD, true));
    // make sure we reset all latest task as false when user switch from single entity to HC, vice versa.
    query.filter(new TermsQueryBuilder(TASK_TYPE_FIELD, taskTypeToString(getADTaskTypes(detectionDateRange, true))));
    updateByQueryRequest.setQuery(query);
    updateByQueryRequest.setRefresh(true);
    String script = String.format(Locale.ROOT, "ctx._source.%s=%s;", IS_LATEST_FIELD, false);
    updateByQueryRequest.setScript(new Script(script));
    client.execute(UpdateByQueryAction.INSTANCE, updateByQueryRequest, ActionListener.wrap(r -> {
        List<BulkItemResponse.Failure> bulkFailures = r.getBulkFailures();
        if (bulkFailures.isEmpty()) {
            // Realtime AD coordinating node is chosen by job scheduler, we won't know it until realtime AD job
            // runs. Just set realtime AD coordinating node as null here, and AD job runner will reset correct
            // coordinating node once realtime job starts.
            // For historical analysis, this method will be called on coordinating node, so we can set coordinating
            // node as local node.
            String coordinatingNode = detectionDateRange == null ? null : clusterService.localNode().getId();
            createNewADTask(detector, detectionDateRange, user, coordinatingNode, listener);
        } else {
            logger.error("Failed to update old task's state for detector: {}, response: {} ", detector.getDetectorId(), r.toString());
            listener.onFailure(bulkFailures.get(0).getCause());
        }
    }, e -> {
        logger.error("Failed to reset old tasks as not latest for detector " + detector.getDetectorId(), e);
        listener.onFailure(e);
    }));
}
Also used : PARENT_TASK_ID_FIELD(org.opensearch.ad.model.ADTask.PARENT_TASK_ID_FIELD) IndexResponse(org.opensearch.action.index.IndexResponse) HashRing(org.opensearch.ad.cluster.HashRing) Version(org.opensearch.Version) FAIL_TO_FIND_DETECTOR_MSG(org.opensearch.ad.constant.CommonErrorMessages.FAIL_TO_FIND_DETECTOR_MSG) BulkAction(org.opensearch.action.bulk.BulkAction) LimitExceededException(org.opensearch.ad.common.exception.LimitExceededException) ExceptionUtil.getShardsFailure(org.opensearch.ad.util.ExceptionUtil.getShardsFailure) ADTaskState(org.opensearch.ad.model.ADTaskState) AnomalyDetectorFunction(org.opensearch.ad.rest.handler.AnomalyDetectorFunction) STOPPED_BY_FIELD(org.opensearch.ad.model.ADTask.STOPPED_BY_FIELD) WriteRequest(org.opensearch.action.support.WriteRequest) ANOMALY_DETECTOR_JOB_INDEX(org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX) MAX_OLD_AD_TASK_DOCS_PER_DETECTOR(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_OLD_AD_TASK_DOCS_PER_DETECTOR) Map(java.util.Map) ForwardADTaskAction(org.opensearch.ad.transport.ForwardADTaskAction) ActionListener(org.opensearch.action.ActionListener) DETECTOR_IS_RUNNING(org.opensearch.ad.constant.CommonErrorMessages.DETECTOR_IS_RUNNING) ADStatsNodeResponse(org.opensearch.ad.transport.ADStatsNodeResponse) DeleteRequest(org.opensearch.action.delete.DeleteRequest) MAX_RUNNING_ENTITIES_PER_DETECTOR_FOR_HISTORICAL_ANALYSIS(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_RUNNING_ENTITIES_PER_DETECTOR_FOR_HISTORICAL_ANALYSIS) Client(org.opensearch.client.Client) UpdateByQueryAction(org.opensearch.index.reindex.UpdateByQueryAction) AD_BATCH_TASK_THREAD_POOL_NAME(org.opensearch.ad.AnomalyDetectorPlugin.AD_BATCH_TASK_THREAD_POOL_NAME) ADTaskProfileRequest(org.opensearch.ad.transport.ADTaskProfileRequest) TimeValue(org.opensearch.common.unit.TimeValue) RestHandlerUtils.createXContentParserFromRegistry(org.opensearch.ad.util.RestHandlerUtils.createXContentParserFromRegistry) SearchHit(org.opensearch.search.SearchHit) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) Set(java.util.Set) ExceptionsHelper(org.opensearch.ExceptionsHelper) Settings(org.opensearch.common.settings.Settings) NO_ELIGIBLE_NODE_TO_RUN_DETECTOR(org.opensearch.ad.constant.CommonErrorMessages.NO_ELIGIBLE_NODE_TO_RUN_DETECTOR) IS_LATEST_FIELD(org.opensearch.ad.model.ADTask.IS_LATEST_FIELD) DETECTION_STATE_INDEX(org.opensearch.ad.constant.CommonName.DETECTION_STATE_INDEX) ScoreMode(org.apache.lucene.search.join.ScoreMode) TransportService(org.opensearch.transport.TransportService) Logger(org.apache.logging.log4j.Logger) CAN_NOT_FIND_LATEST_TASK(org.opensearch.ad.constant.CommonErrorMessages.CAN_NOT_FIND_LATEST_TASK) ADEntityTaskProfile(org.opensearch.ad.model.ADEntityTaskProfile) ActionListenerResponseHandler(org.opensearch.action.ActionListenerResponseHandler) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) EXECUTION_END_TIME_FIELD(org.opensearch.ad.model.ADTask.EXECUTION_END_TIME_FIELD) UpdateRequest(org.opensearch.action.update.UpdateRequest) XContentType(org.opensearch.common.xcontent.XContentType) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) BATCH_TASK_PIECE_INTERVAL_SECONDS(org.opensearch.ad.settings.AnomalyDetectorSettings.BATCH_TASK_PIECE_INTERVAL_SECONDS) MAX_BATCH_TASK_PER_NODE(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_BATCH_TASK_PER_NODE) UpdateResponse(org.opensearch.action.update.UpdateResponse) ThreadPool(org.opensearch.threadpool.ThreadPool) ADTaskType.taskTypeToString(org.opensearch.ad.model.ADTaskType.taskTypeToString) ADTaskProfile(org.opensearch.ad.model.ADTaskProfile) ArrayList(java.util.ArrayList) XCONTENT_WITH_TYPE(org.opensearch.ad.util.RestHandlerUtils.XCONTENT_WITH_TYPE) HC_DETECTOR_TASK_IS_UPDATING(org.opensearch.ad.constant.CommonErrorMessages.HC_DETECTOR_TASK_IS_UPDATING) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) ADCancelTaskAction(org.opensearch.ad.transport.ADCancelTaskAction) BiConsumer(java.util.function.BiConsumer) DeleteResponse(org.opensearch.action.delete.DeleteResponse) SearchRequest(org.opensearch.action.search.SearchRequest) INIT_PROGRESS_FIELD(org.opensearch.ad.model.ADTask.INIT_PROGRESS_FIELD) QueryBuilders(org.opensearch.index.query.QueryBuilders) ADStatsNodesAction(org.opensearch.ad.transport.ADStatsNodesAction) IOException(java.io.IOException) TotalHits(org.apache.lucene.search.TotalHits) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) ChronoUnit(java.time.temporal.ChronoUnit) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) ExceptionUtil.getErrorMessage(org.opensearch.ad.util.ExceptionUtil.getErrorMessage) ADTaskAction(org.opensearch.ad.model.ADTaskAction) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) AD_DETECTOR_ASSIGNED_BATCH_TASK_SLOT_COUNT(org.opensearch.ad.stats.InternalStatNames.AD_DETECTOR_ASSIGNED_BATCH_TASK_SLOT_COUNT) DeleteByQueryAction(org.opensearch.index.reindex.DeleteByQueryAction) COORDINATING_NODE_FIELD(org.opensearch.ad.model.ADTask.COORDINATING_NODE_FIELD) DELETE_AD_RESULT_WHEN_DELETE_DETECTOR(org.opensearch.ad.settings.AnomalyDetectorSettings.DELETE_AD_RESULT_WHEN_DELETE_DETECTOR) ResourceNotFoundException(org.opensearch.ad.common.exception.ResourceNotFoundException) MAX_OLD_AD_TASK_DOCS(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_OLD_AD_TASK_DOCS) ADBatchAnomalyResultAction(org.opensearch.ad.transport.ADBatchAnomalyResultAction) ToXContent(org.opensearch.common.xcontent.ToXContent) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) BulkRequest(org.opensearch.action.bulk.BulkRequest) ERROR_FIELD(org.opensearch.ad.model.ADTask.ERROR_FIELD) EXECUTION_START_TIME_FIELD(org.opensearch.ad.model.ADTask.EXECUTION_START_TIME_FIELD) ParseUtils.isNullOrEmpty(org.opensearch.ad.util.ParseUtils.isNullOrEmpty) ADTaskCancelledException(org.opensearch.ad.common.exception.ADTaskCancelledException) ADStatsRequest(org.opensearch.ad.transport.ADStatsRequest) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) XContentParser(org.opensearch.common.xcontent.XContentParser) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) IndexAnomalyDetectorJobActionHandler(org.opensearch.ad.rest.handler.IndexAnomalyDetectorJobActionHandler) DETECTOR_ID_FIELD(org.opensearch.ad.model.ADTask.DETECTOR_ID_FIELD) Locale(java.util.Locale) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) XContentFactory(org.opensearch.common.xcontent.XContentFactory) REALTIME_TASK_TYPES(org.opensearch.ad.model.ADTaskType.REALTIME_TASK_TYPES) NestedQueryBuilder(org.opensearch.index.query.NestedQueryBuilder) TASK_PROGRESS_FIELD(org.opensearch.ad.model.ADTask.TASK_PROGRESS_FIELD) DetectorProfile(org.opensearch.ad.model.DetectorProfile) ImmutableSet(com.google.common.collect.ImmutableSet) ESTIMATED_MINUTES_LEFT_FIELD(org.opensearch.ad.model.ADTask.ESTIMATED_MINUTES_LEFT_FIELD) NOT_ENDED_STATES(org.opensearch.ad.model.ADTaskState.NOT_ENDED_STATES) ImmutableMap(com.google.common.collect.ImmutableMap) Script(org.opensearch.script.Script) LoggingDeprecationHandler(org.opensearch.common.xcontent.LoggingDeprecationHandler) ADTaskType(org.opensearch.ad.model.ADTaskType) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) ADTaskProfileNodeResponse(org.opensearch.ad.transport.ADTaskProfileNodeResponse) LAST_UPDATE_TIME_FIELD(org.opensearch.ad.model.ADTask.LAST_UPDATE_TIME_FIELD) Instant(java.time.Instant) RestStatus(org.opensearch.rest.RestStatus) Collectors(java.util.stream.Collectors) TASK_TYPE_FIELD(org.opensearch.ad.model.ADTask.TASK_TYPE_FIELD) Objects(java.util.Objects) AnomalyDetectorJobResponse(org.opensearch.ad.transport.AnomalyDetectorJobResponse) List(java.util.List) ForwardADTaskRequest(org.opensearch.ad.transport.ForwardADTaskRequest) DuplicateTaskException(org.opensearch.ad.common.exception.DuplicateTaskException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) CREATED(org.opensearch.action.DocWriteResponse.Result.CREATED) DetectionDateRange(org.opensearch.ad.model.DetectionDateRange) NUM_MIN_SAMPLES(org.opensearch.ad.settings.AnomalyDetectorSettings.NUM_MIN_SAMPLES) Optional(java.util.Optional) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) ALL_HISTORICAL_TASK_TYPES(org.opensearch.ad.model.ADTaskType.ALL_HISTORICAL_TASK_TYPES) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RestHandlerUtils(org.opensearch.ad.util.RestHandlerUtils) TASK_ID_FIELD(org.opensearch.ad.model.AnomalyResult.TASK_ID_FIELD) REQUEST_TIMEOUT(org.opensearch.ad.settings.AnomalyDetectorSettings.REQUEST_TIMEOUT) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) BytesReference(org.opensearch.common.bytes.BytesReference) ADTask(org.opensearch.ad.model.ADTask) HashMap(java.util.HashMap) SortOrder(org.opensearch.search.sort.SortOrder) ImmutableList(com.google.common.collect.ImmutableList) EXCEED_HISTORICAL_ANALYSIS_LIMIT(org.opensearch.ad.constant.CommonErrorMessages.EXCEED_HISTORICAL_ANALYSIS_LIMIT) ADCancelTaskRequest(org.opensearch.ad.transport.ADCancelTaskRequest) SearchResponse(org.opensearch.action.search.SearchResponse) EndRunException(org.opensearch.ad.common.exception.EndRunException) ADBatchAnomalyResultRequest(org.opensearch.ad.transport.ADBatchAnomalyResultRequest) HISTORICAL_DETECTOR_TASK_TYPES(org.opensearch.ad.model.ADTaskType.HISTORICAL_DETECTOR_TASK_TYPES) Iterator(java.util.Iterator) ALL_AD_RESULTS_INDEX_PATTERN(org.opensearch.ad.indices.AnomalyDetectionIndices.ALL_AD_RESULTS_INDEX_PATTERN) Semaphore(java.util.concurrent.Semaphore) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) STATE_FIELD(org.opensearch.ad.model.ADTask.STATE_FIELD) CREATE_INDEX_NOT_ACKNOWLEDGED(org.opensearch.ad.constant.CommonErrorMessages.CREATE_INDEX_NOT_ACKNOWLEDGED) Consumer(java.util.function.Consumer) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) AD_USED_BATCH_TASK_SLOT_COUNT(org.opensearch.ad.stats.InternalStatNames.AD_USED_BATCH_TASK_SLOT_COUNT) Entity(org.opensearch.ad.model.Entity) User(org.opensearch.commons.authuser.User) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) IndexRequest(org.opensearch.action.index.IndexRequest) LogManager(org.apache.logging.log4j.LogManager) ADTaskProfileAction(org.opensearch.ad.transport.ADTaskProfileAction) Script(org.opensearch.script.Script) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) ADTaskType.taskTypeToString(org.opensearch.ad.model.ADTaskType.taskTypeToString) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest)

Example 23 with DetectionDateRange

use of org.opensearch.ad.model.DetectionDateRange in project anomaly-detection by opensearch-project.

the class ADRestTestUtils method createAnomalyDetector.

public static Response createAnomalyDetector(RestClient client, String indexName, String timeField, int detectionIntervalInMinutes, int windowDelayIntervalInMinutes, String valueField, String aggregationMethod, String filterQuery, List<String> categoryFields, boolean historical) throws Exception {
    Instant now = Instant.now();
    AnomalyDetector detector = new AnomalyDetector(randomAlphaOfLength(10), randomLong(), // TODO: check why throw duplicate detector name error with randomAlphaOfLength(20) in twoThirdsUpgradedClusterTask
    randomAlphaOfLength(20) + now.toEpochMilli(), randomAlphaOfLength(30), timeField, ImmutableList.of(indexName), ImmutableList.of(TestHelpers.randomFeature(randomAlphaOfLength(5), valueField, aggregationMethod, true)), filterQuery == null ? TestHelpers.randomQuery("{\"match_all\":{\"boost\":1}}") : TestHelpers.randomQuery(filterQuery), new IntervalTimeConfiguration(detectionIntervalInMinutes, ChronoUnit.MINUTES), new IntervalTimeConfiguration(windowDelayIntervalInMinutes, ChronoUnit.MINUTES), randomIntBetween(1, 20), null, randomInt(), now, categoryFields, TestHelpers.randomUser(), null);
    if (historical) {
        detector.setDetectionDateRange(new DetectionDateRange(now.minus(30, ChronoUnit.DAYS), now));
    }
    return TestHelpers.makeRequest(client, "POST", TestHelpers.LEGACY_OPENDISTRO_AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null);
}
Also used : Instant(java.time.Instant) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) DetectionDateRange(org.opensearch.ad.model.DetectionDateRange)

Example 24 with DetectionDateRange

use of org.opensearch.ad.model.DetectionDateRange in project anomaly-detection by opensearch-project.

the class MockAnomalyDetectorJobTransportActionWithUser method doExecute.

@Override
protected void doExecute(Task task, AnomalyDetectorJobRequest request, ActionListener<AnomalyDetectorJobResponse> listener) {
    String detectorId = request.getDetectorID();
    DetectionDateRange detectionDateRange = request.getDetectionDateRange();
    boolean historical = request.isHistorical();
    long seqNo = request.getSeqNo();
    long primaryTerm = request.getPrimaryTerm();
    String rawPath = request.getRawPath();
    TimeValue requestTimeout = REQUEST_TIMEOUT.get(settings);
    String userStr = "user_name|backendrole1,backendrole2|roles1,role2";
    // By the time request reaches here, the user permissions are validated by Security plugin.
    User user = User.parse(userStr);
    try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {
        resolveUserAndExecute(user, detectorId, filterByEnabled, listener, (anomalyDetector) -> executeDetector(listener, detectorId, seqNo, primaryTerm, rawPath, requestTimeout, user, detectionDateRange, historical), client, clusterService, xContentRegistry);
    } catch (Exception e) {
        logger.error(e);
        listener.onFailure(e);
    }
}
Also used : User(org.opensearch.commons.authuser.User) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) DetectionDateRange(org.opensearch.ad.model.DetectionDateRange) TimeValue(org.opensearch.common.unit.TimeValue)

Example 25 with DetectionDateRange

use of org.opensearch.ad.model.DetectionDateRange in project anomaly-detection by opensearch-project.

the class ADTaskManagerTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    Instant now = Instant.now();
    Instant startTime = now.minus(10, ChronoUnit.DAYS);
    Instant endTime = now.minus(1, ChronoUnit.DAYS);
    detectionDateRange = new DetectionDateRange(startTime, endTime);
    settings = Settings.builder().put(MAX_OLD_AD_TASK_DOCS_PER_DETECTOR.getKey(), 2).put(BATCH_TASK_PIECE_INTERVAL_SECONDS.getKey(), 1).put(REQUEST_TIMEOUT.getKey(), TimeValue.timeValueSeconds(10)).build();
    clusterSettings = clusterSetting(settings, MAX_OLD_AD_TASK_DOCS_PER_DETECTOR, BATCH_TASK_PIECE_INTERVAL_SECONDS, REQUEST_TIMEOUT, DELETE_AD_RESULT_WHEN_DELETE_DETECTOR, MAX_BATCH_TASK_PER_NODE, MAX_RUNNING_ENTITIES_PER_DETECTOR_FOR_HISTORICAL_ANALYSIS);
    maxBatchTaskPerNode = MAX_BATCH_TASK_PER_NODE.get(settings);
    clusterService = spy(new ClusterService(settings, clusterSettings, null));
    client = mock(Client.class);
    nodeFilter = mock(DiscoveryNodeFilterer.class);
    detectionIndices = mock(AnomalyDetectionIndices.class);
    adTaskCacheManager = mock(ADTaskCacheManager.class);
    hashRing = mock(HashRing.class);
    transportService = mock(TransportService.class);
    threadPool = mock(ThreadPool.class);
    threadContext = new ThreadContext(settings);
    when(threadPool.getThreadContext()).thenReturn(threadContext);
    when(client.threadPool()).thenReturn(threadPool);
    indexAnomalyDetectorJobActionHandler = mock(IndexAnomalyDetectorJobActionHandler.class);
    adTaskManager = spy(new ADTaskManager(settings, clusterService, client, TestHelpers.xContentRegistry(), detectionIndices, nodeFilter, hashRing, adTaskCacheManager, threadPool));
    listener = spy(new ActionListener<AnomalyDetectorJobResponse>() {

        @Override
        public void onResponse(AnomalyDetectorJobResponse bulkItemResponses) {
        }

        @Override
        public void onFailure(Exception e) {
        }
    });
    node1 = new DiscoveryNode("nodeName1", "node1", new TransportAddress(TransportAddress.META_ADDRESS, 9300), emptyMap(), emptySet(), Version.CURRENT);
    node2 = new DiscoveryNode("nodeName2", "node2", new TransportAddress(TransportAddress.META_ADDRESS, 9300), emptyMap(), emptySet(), Version.CURRENT);
    maxRunningEntities = MAX_RUNNING_ENTITIES_PER_DETECTOR_FOR_HISTORICAL_ANALYSIS.get(settings).intValue();
    ThreadContext threadContext = new ThreadContext(settings);
    context = threadContext.stashContext();
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) TransportAddress(org.opensearch.common.transport.TransportAddress) Instant(java.time.Instant) ThreadPool(org.opensearch.threadpool.ThreadPool) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) AnomalyDetectorJobResponse(org.opensearch.ad.transport.AnomalyDetectorJobResponse) TestHelpers.randomDetectionDateRange(org.opensearch.ad.TestHelpers.randomDetectionDateRange) DetectionDateRange(org.opensearch.ad.model.DetectionDateRange) IOException(java.io.IOException) DuplicateTaskException(org.opensearch.ad.common.exception.DuplicateTaskException) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) HashRing(org.opensearch.ad.cluster.HashRing) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) Client(org.opensearch.client.Client) IndexAnomalyDetectorJobActionHandler(org.opensearch.ad.rest.handler.IndexAnomalyDetectorJobActionHandler)

Aggregations

DetectionDateRange (org.opensearch.ad.model.DetectionDateRange)32 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)14 Instant (java.time.Instant)13 IOException (java.io.IOException)8 AnomalyDetectorJobResponse (org.opensearch.ad.transport.AnomalyDetectorJobResponse)8 TimeValue (org.opensearch.common.unit.TimeValue)8 List (java.util.List)7 ActionListener (org.opensearch.action.ActionListener)7 EndRunException (org.opensearch.ad.common.exception.EndRunException)7 ClusterService (org.opensearch.cluster.service.ClusterService)7 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 Optional (java.util.Optional)6 LogManager (org.apache.logging.log4j.LogManager)6 Logger (org.apache.logging.log4j.Logger)6 HashRing (org.opensearch.ad.cluster.HashRing)6 AnomalyDetectionIndices (org.opensearch.ad.indices.AnomalyDetectionIndices)6 ADTask (org.opensearch.ad.model.ADTask)6 User (org.opensearch.commons.authuser.User)6 ImmutableSet (com.google.common.collect.ImmutableSet)5