From 0a725df5dfc6adbfc822315d0b764de47cba0535 Mon Sep 17 00:00:00 2001 From: Kaituo Li Date: Mon, 23 Mar 2020 01:59:38 -0700 Subject: [PATCH] Fix that preview exits with null pointer exception MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Recently, we changed the anomaly result index’s schema and toXContent method by introducing a few fields. During the preview, some of the fields in anomaly result index can be null. Thus, toXContent can fail with a null pointer exception. This PR fixes the issue by protecting a field’s serialization with a null check. Testing done: - Manually verified the issue is gone - gradle build --- .../ad/ml/ModelManager.java | 2 -- .../ad/model/AnomalyResult.java | 17 +++++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/ad/ml/ModelManager.java b/src/main/java/com/amazon/opendistroforelasticsearch/ad/ml/ModelManager.java index b65fe8b4..eda767ba 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/ad/ml/ModelManager.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/ad/ml/ModelManager.java @@ -26,7 +26,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; -import java.util.Random; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; @@ -692,7 +691,6 @@ public List getPreviewResults(double[][] dataPoints) { throw new IllegalArgumentException("Insufficient data for preview results. Minimum required: " + minPreviewSize); } // Train RCF models and collect non-zero scores - Random random = new Random(); int rcfNumFeatures = dataPoints[0].length; RandomCutForest forest = RandomCutForest .builder() diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/ad/model/AnomalyResult.java b/src/main/java/com/amazon/opendistroforelasticsearch/ad/model/AnomalyResult.java index 9bce9535..077ba33d 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/ad/model/AnomalyResult.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/ad/model/AnomalyResult.java @@ -88,11 +88,20 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws XContentBuilder xContentBuilder = builder .startObject() .field(DETECTOR_ID_FIELD, detectorId) - .field(FEATURE_DATA_FIELD, featureData.toArray()) .field(DATA_START_TIME_FIELD, dataStartTime.toEpochMilli()) - .field(DATA_END_TIME_FIELD, dataEndTime.toEpochMilli()) - .field(EXECUTION_START_TIME_FIELD, executionStartTime.toEpochMilli()) - .field(EXECUTION_END_TIME_FIELD, executionEndTime.toEpochMilli()); + .field(DATA_END_TIME_FIELD, dataEndTime.toEpochMilli()); + if (featureData != null) { + // can be null during preview + xContentBuilder.field(FEATURE_DATA_FIELD, featureData.toArray()); + } + if (executionStartTime != null) { + // can be null during preview + xContentBuilder.field(EXECUTION_START_TIME_FIELD, executionStartTime.toEpochMilli()); + } + if (executionEndTime != null) { + // can be null during preview + xContentBuilder.field(EXECUTION_END_TIME_FIELD, executionEndTime.toEpochMilli()); + } if (anomalyScore != null && !anomalyScore.isNaN()) { xContentBuilder.field(ANOMALY_SCORE_FIELD, anomalyScore); }