Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Refactoring code like not calling getDetectorId repeatedly
Browse files Browse the repository at this point in the history
  • Loading branch information
kaituo committed Aug 12, 2020
1 parent d59b96b commit 7e3065f
Showing 1 changed file with 23 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,25 +415,26 @@ private ActionListener<SinglePointFeatures> onFeatureResponse(
private Exception coldStartIfNoModel(AtomicReference<AnomalyDetectionException> 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<AnomalyDetectionException> failure) {
Expand Down Expand Up @@ -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)
);
}
});
Expand All @@ -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));
}
});

Expand Down

0 comments on commit 7e3065f

Please sign in to comment.