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));
}
Aggregations