Search in sources :

Example 1 with PageIterator

use of org.opensearch.ad.feature.CompositeRetriever.PageIterator in project anomaly-detection by opensearch-project.

the class AnomalyResultTransportAction method executeAnomalyDetection.

private void executeAnomalyDetection(ActionListener<AnomalyResultResponse> listener, String adID, AnomalyResultRequest request, AnomalyDetector anomalyDetector, long dataStartTime, long dataEndTime) {
    // HC logic starts here
    if (anomalyDetector.isMultientityDetector()) {
        Optional<Exception> previousException = stateManager.fetchExceptionAndClear(adID);
        if (previousException.isPresent()) {
            Exception exception = previousException.get();
            LOG.error(new ParameterizedMessage("Previous exception of [{}]", adID), exception);
            if (exception instanceof EndRunException) {
                EndRunException endRunException = (EndRunException) exception;
                if (endRunException.isEndNow()) {
                    listener.onFailure(exception);
                    return;
                }
            }
        }
        // assume request are in epoch milliseconds
        long nextDetectionStartTime = request.getEnd() + (long) (anomalyDetector.getDetectorIntervalInMilliseconds() * intervalRatioForRequest);
        CompositeRetriever compositeRetriever = new CompositeRetriever(dataStartTime, dataEndTime, anomalyDetector, xContentRegistry, client, nextDetectionStartTime, settings, maxEntitiesPerInterval, pageSize);
        PageIterator pageIterator = null;
        try {
            pageIterator = compositeRetriever.iterator();
        } catch (Exception e) {
            listener.onFailure(new EndRunException(anomalyDetector.getDetectorId(), CommonErrorMessages.INVALID_SEARCH_QUERY_MSG, e, false));
            return;
        }
        PageListener getEntityFeatureslistener = new PageListener(pageIterator, adID, dataStartTime, dataEndTime);
        if (pageIterator.hasNext()) {
            pageIterator.next(getEntityFeatureslistener);
        }
        // Pagination will stop itself when the time is up.
        if (previousException.isPresent()) {
            listener.onFailure(previousException.get());
        } else {
            listener.onResponse(new AnomalyResultResponse(new ArrayList<FeatureData>(), null, null, anomalyDetector.getDetectorIntervalInMinutes(), true));
        }
        return;
    }
    // HC logic ends and single entity logic starts here
    // We are going to use only 1 model partition for a single stream detector.
    // That's why we use 0 here.
    String rcfModelID = SingleStreamModelIdMapper.getRcfModelId(adID, 0);
    Optional<DiscoveryNode> asRCFNode = hashRing.getOwningNodeWithSameLocalAdVersionForRealtimeAD(rcfModelID);
    if (!asRCFNode.isPresent()) {
        listener.onFailure(new InternalFailure(adID, "RCF model node is not available."));
        return;
    }
    DiscoveryNode rcfNode = asRCFNode.get();
    if (!shouldStart(listener, adID, anomalyDetector, rcfNode.getId(), rcfModelID)) {
        return;
    }
    featureManager.getCurrentFeatures(anomalyDetector, dataStartTime, dataEndTime, onFeatureResponseForSingleEntityDetector(adID, anomalyDetector, listener, rcfModelID, rcfNode, dataStartTime, dataEndTime));
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) EndRunException(org.opensearch.ad.common.exception.EndRunException) ArrayList(java.util.ArrayList) CompositeRetriever(org.opensearch.ad.feature.CompositeRetriever) ResourceNotFoundException(org.opensearch.ad.common.exception.ResourceNotFoundException) LimitExceededException(org.opensearch.ad.common.exception.LimitExceededException) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) NodeClosedException(org.opensearch.node.NodeClosedException) NodeNotConnectedException(org.opensearch.transport.NodeNotConnectedException) ActionNotFoundTransportException(org.opensearch.transport.ActionNotFoundTransportException) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) ConnectTransportException(org.opensearch.transport.ConnectTransportException) ReceiveTimeoutTransportException(org.opensearch.transport.ReceiveTimeoutTransportException) ClientException(org.opensearch.ad.common.exception.ClientException) ConnectException(java.net.ConnectException) EndRunException(org.opensearch.ad.common.exception.EndRunException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) PageIterator(org.opensearch.ad.feature.CompositeRetriever.PageIterator) InternalFailure(org.opensearch.ad.common.exception.InternalFailure) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Aggregations

ConnectException (java.net.ConnectException)1 ArrayList (java.util.ArrayList)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 OpenSearchTimeoutException (org.opensearch.OpenSearchTimeoutException)1 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)1 AnomalyDetectionException (org.opensearch.ad.common.exception.AnomalyDetectionException)1 ClientException (org.opensearch.ad.common.exception.ClientException)1 EndRunException (org.opensearch.ad.common.exception.EndRunException)1 InternalFailure (org.opensearch.ad.common.exception.InternalFailure)1 LimitExceededException (org.opensearch.ad.common.exception.LimitExceededException)1 ResourceNotFoundException (org.opensearch.ad.common.exception.ResourceNotFoundException)1 CompositeRetriever (org.opensearch.ad.feature.CompositeRetriever)1 PageIterator (org.opensearch.ad.feature.CompositeRetriever.PageIterator)1 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)1 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)1 NodeClosedException (org.opensearch.node.NodeClosedException)1 ActionNotFoundTransportException (org.opensearch.transport.ActionNotFoundTransportException)1 ConnectTransportException (org.opensearch.transport.ConnectTransportException)1 NodeNotConnectedException (org.opensearch.transport.NodeNotConnectedException)1 ReceiveTimeoutTransportException (org.opensearch.transport.ReceiveTimeoutTransportException)1