Search in sources :

Example 1 with MAX_DETECTOR_UPPER_LIMIT

use of org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_DETECTOR_UPPER_LIMIT 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);
        }
    }));
}
Also used : ResourceNotFoundException(org.opensearch.ad.common.exception.ResourceNotFoundException) ADTask(org.opensearch.ad.model.ADTask) MAX_DETECTOR_UPPER_LIMIT(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_DETECTOR_UPPER_LIMIT) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ADTaskType.taskTypeToString(org.opensearch.ad.model.ADTaskType.taskTypeToString) ADTaskState(org.opensearch.ad.model.ADTaskState) XCONTENT_WITH_TYPE(org.opensearch.ad.util.RestHandlerUtils.XCONTENT_WITH_TYPE) AnomalyDetectorFunction(org.opensearch.ad.rest.handler.AnomalyDetectorFunction) XContentParser(org.opensearch.common.xcontent.XContentParser) WriteRequest(org.opensearch.action.support.WriteRequest) ANOMALY_DETECTOR_JOB_INDEX(org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX) DETECTOR_ID_FIELD(org.opensearch.ad.model.ADTask.DETECTOR_ID_FIELD) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) XContentFactory(org.opensearch.common.xcontent.XContentFactory) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) Client(org.opensearch.client.Client) CommonName(org.opensearch.ad.constant.CommonName) RestHandlerUtils.createXContentParserFromRegistry(org.opensearch.ad.util.RestHandlerUtils.createXContentParserFromRegistry) Iterator(java.util.Iterator) SearchHit(org.opensearch.search.SearchHit) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) ExceptionsHelper(org.opensearch.ExceptionsHelper) ADTaskType(org.opensearch.ad.model.ADTaskType) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) IOException(java.io.IOException) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) IS_LATEST_FIELD(org.opensearch.ad.model.ADTask.IS_LATEST_FIELD) DETECTION_STATE_INDEX(org.opensearch.ad.constant.CommonName.DETECTION_STATE_INDEX) Instant(java.time.Instant) TASK_TYPE_FIELD(org.opensearch.ad.model.ADTask.TASK_TYPE_FIELD) Logger(org.apache.logging.log4j.Logger) ExceptionUtil(org.opensearch.ad.util.ExceptionUtil) DetectorInternalState(org.opensearch.ad.model.DetectorInternalState) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) IndexRequest(org.opensearch.action.index.IndexRequest) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) LogManager(org.apache.logging.log4j.LogManager) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) SearchRequest(org.opensearch.action.search.SearchRequest) SearchHit(org.opensearch.search.SearchHit) Iterator(java.util.Iterator) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) IOException(java.io.IOException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) XContentParser(org.opensearch.common.xcontent.XContentParser) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 2 with MAX_DETECTOR_UPPER_LIMIT

use of org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_DETECTOR_UPPER_LIMIT in project anomaly-detection by opensearch-project.

the class SearchAnomalyResultTransportAction method doExecute.

@Override
protected void doExecute(Task task, SearchRequest request, ActionListener<SearchResponse> listener) {
    String[] indices = request.indices();
    if (indices == null || indices.length == 0) {
        listener.onFailure(new IllegalArgumentException("No indices set in search request"));
        return;
    }
    // Set query indices as default result indices, will check custom result indices permission and add
    // custom indices which user has search permission later.
    boolean onlyQueryCustomResultIndex = true;
    for (String indexName : indices) {
        // If only query custom result index, don't need to set ALL_AD_RESULTS_INDEX_PATTERN in search request
        if (ALL_AD_RESULTS_INDEX_PATTERN.equals(indexName)) {
            onlyQueryCustomResultIndex = false;
        }
    }
    String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(clusterService.state(), IndicesOptions.lenientExpandOpen(), indices);
    // If concreteIndices is null or empty, don't throw exception. Detector list page will search both
    // default and custom result indices to get anomaly of last 24 hours. If throw exception, detector
    // list page will throw error and won't show any detector.
    // If a cluster has no custom result indices, and some new non-custom-result-detector that hasn't
    // finished one interval (where no default result index exists), then no result indices found. We
    // will still search ".opendistro-anomaly-results*" (even these default indices don't exist) to
    // return an empty SearchResponse. This search looks unnecessary, but this can make sure the
    // detector list page show all detectors correctly. The other solution is to catch errors from
    // frontend when search anomaly results to make sure frontend won't crash. Check this Github issue:
    // https://github.com/opensearch-project/anomaly-detection-dashboards-plugin/issues/154
    Set<String> customResultIndices = new HashSet<>();
    if (concreteIndices != null) {
        for (String index : concreteIndices) {
            if (index.startsWith(CUSTOM_RESULT_INDEX_PREFIX)) {
                customResultIndices.add(index);
            }
        }
    }
    // we should not search anymore. Just throw exception here.
    if (onlyQueryCustomResultIndex && customResultIndices.size() == 0) {
        listener.onFailure(new IllegalArgumentException("No custom result indices found"));
        return;
    }
    if (customResultIndices.size() > 0) {
        // Search both custom AD result index and default result index
        String resultIndexAggName = "result_index";
        SearchSourceBuilder searchResultIndexBuilder = new SearchSourceBuilder();
        AggregationBuilder aggregation = new TermsAggregationBuilder(resultIndexAggName).field(AnomalyDetector.RESULT_INDEX_FIELD).size(MAX_DETECTOR_UPPER_LIMIT);
        searchResultIndexBuilder.aggregation(aggregation).size(0);
        SearchRequest searchResultIndex = new SearchRequest(AnomalyDetector.ANOMALY_DETECTORS_INDEX).source(searchResultIndexBuilder);
        try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {
            // Search result indices of all detectors. User may create index with same prefix of custom result index
            // which not used for AD, so we should avoid searching extra indices which not used by anomaly detectors.
            // Variable used in lambda expression should be final or effectively final, so copy to a final boolean and
            // use the final boolean in lambda below.
            boolean finalOnlyQueryCustomResultIndex = onlyQueryCustomResultIndex;
            client.search(searchResultIndex, ActionListener.wrap(allResultIndicesResponse -> {
                Aggregations aggregations = allResultIndicesResponse.getAggregations();
                StringTerms resultIndicesAgg = aggregations.get(resultIndexAggName);
                List<StringTerms.Bucket> buckets = resultIndicesAgg.getBuckets();
                Set<String> resultIndicesOfDetector = new HashSet<>();
                if (buckets == null) {
                    searchHandler.search(request, listener);
                    return;
                }
                buckets.stream().forEach(b -> resultIndicesOfDetector.add(b.getKeyAsString()));
                List<String> targetIndices = new ArrayList<>();
                for (String index : customResultIndices) {
                    if (resultIndicesOfDetector.contains(index)) {
                        targetIndices.add(index);
                    }
                }
                if (targetIndices.size() == 0) {
                    // No custom result indices used by detectors, just search default result index
                    searchHandler.search(request, listener);
                    return;
                }
                MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
                for (String index : targetIndices) {
                    multiSearchRequest.add(new SearchRequest(index).source(new SearchSourceBuilder().query(new MatchAllQueryBuilder()).size(0)));
                }
                List<String> readableIndices = new ArrayList<>();
                if (!finalOnlyQueryCustomResultIndex) {
                    readableIndices.add(ALL_AD_RESULTS_INDEX_PATTERN);
                }
                context.restore();
                // Send multiple search to check which index a user has permission to read. If search all indices directly,
                // search request will throw exception if user has no permission to search any index.
                client.multiSearch(multiSearchRequest, ActionListener.wrap(multiSearchResponse -> {
                    MultiSearchResponse.Item[] responses = multiSearchResponse.getResponses();
                    for (int i = 0; i < responses.length; i++) {
                        MultiSearchResponse.Item item = responses[i];
                        String indexName = targetIndices.get(i);
                        if (item.getFailure() == null) {
                            readableIndices.add(indexName);
                        }
                    }
                    if (readableIndices.size() == 0) {
                        listener.onFailure(new IllegalArgumentException("No readable custom result indices found"));
                        return;
                    }
                    request.indices(readableIndices.toArray(new String[0]));
                    searchHandler.search(request, listener);
                }, multiSearchException -> {
                    logger.error("Failed to search custom AD result indices", multiSearchException);
                    listener.onFailure(multiSearchException);
                }));
            }, e -> {
                logger.error("Failed to search result indices for all detectors", e);
                listener.onFailure(e);
            }));
        } catch (Exception e) {
            logger.error(e);
            listener.onFailure(e);
        }
    } else {
        // onlyQueryCustomResultIndex is false in this branch
        // Search only default result index
        request.indices(ALL_AD_RESULTS_INDEX_PATTERN);
        searchHandler.search(request, listener);
    }
}
Also used : CUSTOM_RESULT_INDEX_PREFIX(org.opensearch.ad.constant.CommonName.CUSTOM_RESULT_INDEX_PREFIX) HandledTransportAction(org.opensearch.action.support.HandledTransportAction) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) MAX_DETECTOR_UPPER_LIMIT(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_DETECTOR_UPPER_LIMIT) Aggregations(org.opensearch.search.aggregations.Aggregations) IndicesOptions(org.opensearch.action.support.IndicesOptions) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ADSearchHandler(org.opensearch.ad.transport.handler.ADSearchHandler) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SearchRequest(org.opensearch.action.search.SearchRequest) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) Client(org.opensearch.client.Client) ALL_AD_RESULTS_INDEX_PATTERN(org.opensearch.ad.indices.AnomalyDetectionIndices.ALL_AD_RESULTS_INDEX_PATTERN) Set(java.util.Set) Task(org.opensearch.tasks.Task) TransportService(org.opensearch.transport.TransportService) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) ActionFilters(org.opensearch.action.support.ActionFilters) List(java.util.List) Logger(org.apache.logging.log4j.Logger) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) ClusterService(org.opensearch.cluster.service.ClusterService) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) LogManager(org.apache.logging.log4j.LogManager) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) HashSet(java.util.HashSet) Set(java.util.Set) Aggregations(org.opensearch.search.aggregations.Aggregations) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Aggregations

LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 ActionListener (org.opensearch.action.ActionListener)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)2 MAX_DETECTOR_UPPER_LIMIT (org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_DETECTOR_UPPER_LIMIT)2 Client (org.opensearch.client.Client)2 ClusterService (org.opensearch.cluster.service.ClusterService)2 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)2 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)2 IOException (java.io.IOException)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Set (java.util.Set)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ExceptionsHelper (org.opensearch.ExceptionsHelper)1