diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/AnomalyResultTransportAction.java b/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/AnomalyResultTransportAction.java index a3a9bf4e..213ab4dc 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/AnomalyResultTransportAction.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/AnomalyResultTransportAction.java @@ -415,25 +415,26 @@ private ActionListener onFeatureResponse( private Exception coldStartIfNoModel(AtomicReference failure, AnomalyDetector detector) throws AnomalyDetectionException { AnomalyDetectionException exp = failure.get(); - if (exp != null) { - if (exp instanceof ResourceNotFoundException) { - // fetch previous cold start exception - String adID = detector.getDetectorId(); - Exception previousException = stateManager.fetchColdStartException(adID); - if (previousException != null) { - LOG.error("Previous exception of {}: {}", () -> adID, () -> previousException); - if (previousException instanceof EndRunException && ((EndRunException) previousException).isEndNow()) { - return previousException; - } - } - LOG.info("Trigger cold start for {}", detector.getDetectorId()); - coldStart(detector); - return previousException == null ? new InternalFailure(adID, NO_MODEL_ERR_MSG) : previousException; - } else { - throw exp; + if (exp == null) { + return null; + } + + if (!(exp instanceof ResourceNotFoundException)) { + throw exp; + } + + // fetch previous cold start exception + String adID = detector.getDetectorId(); + Exception previousException = stateManager.fetchColdStartException(adID); + if (previousException != null) { + LOG.error("Previous exception of {}: {}", () -> adID, () -> previousException); + if (previousException instanceof EndRunException && ((EndRunException) previousException).isEndNow()) { + return previousException; } } - return null; + LOG.info("Trigger cold start for {}", detector.getDetectorId()); + coldStart(detector); + return previousException == null ? new InternalFailure(adID, NO_MODEL_ERR_MSG) : previousException; } private void findException(Throwable cause, String adID, AtomicReference failure) { @@ -792,23 +793,19 @@ private void coldStart(AnomalyDetector detector) { stateManager .setLastColdStartException( detectorId, - new EndRunException(detector.getDetectorId(), "Invalid training data", exception, false) + new EndRunException(detectorId, "Invalid training data", exception, false) ); } else if (exception instanceof ElasticsearchTimeoutException) { stateManager .setLastColdStartException( detectorId, - new InternalFailure( - detector.getDetectorId(), - "Time out while indexing cold start checkpoint", - exception - ) + new InternalFailure(detectorId, "Time out while indexing cold start checkpoint", exception) ); } else { stateManager .setLastColdStartException( detectorId, - new EndRunException(detector.getDetectorId(), "Error while training model", exception, false) + new EndRunException(detectorId, "Error while training model", exception, false) ); } }); @@ -833,17 +830,14 @@ private void coldStart(AnomalyDetector detector) { stateManager .setLastColdStartException( detectorId, - new InternalFailure(detector.getDetectorId(), "Time out while getting training data", exception) + new InternalFailure(detectorId, "Time out while getting training data", exception) ); } else if (exception instanceof AnomalyDetectionException) { // e.g., Invalid search query stateManager.setLastColdStartException(detectorId, exception); } else { stateManager - .setLastColdStartException( - detectorId, - new EndRunException(detector.getDetectorId(), "Error while cold start", exception, false) - ); + .setLastColdStartException(detectorId, new EndRunException(detectorId, "Error while cold start", exception, false)); } });