From a11e52fea2dc5dad37707d376a4153935ecb6e23 Mon Sep 17 00:00:00 2001 From: David Kyle Date: Thu, 20 Apr 2017 18:10:32 +0100 Subject: [PATCH] [ML] Add check to prevent NoSuchElementException (elastic/x-pack-elasticsearch#1147) Original commit: elastic/x-pack-elasticsearch@dca4020734976666522c2c92628a8fc8f7751ef5 --- .../xpack/ml/job/process/DataStreamDiagnostics.java | 4 +++- .../xpack/ml/job/process/DataStreamDiagnosticsTests.java | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/DataStreamDiagnostics.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/DataStreamDiagnostics.java index 2ff9be9af77..524b111ef23 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/DataStreamDiagnostics.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/DataStreamDiagnostics.java @@ -81,7 +81,9 @@ public class DataStreamDiagnostics { */ public void flush() { // flush all we know - flush(movingBucketHistogram.lastKey() + 1); + if (movingBucketHistogram.isEmpty() == false) { + flush(movingBucketHistogram.lastKey() + 1); + } } /** diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/DataStreamDiagnosticsTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/DataStreamDiagnosticsTests.java index eccd712c1c0..7b80febdf20 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/DataStreamDiagnosticsTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/DataStreamDiagnosticsTests.java @@ -259,6 +259,12 @@ public class DataStreamDiagnosticsTests extends ESTestCase { assertEquals(null, d.getLatestEmptyBucketTime()); } + public void testFlushAfterZeroRecords() { + DataStreamDiagnostics d = new DataStreamDiagnostics(job); + d.flush(); + assertEquals(0, d.getBucketCount()); + } + private void sendManyDataPoints(DataStreamDiagnostics d, long recordTimestampInMsMin, long recordTimestampInMsMax, long howMuch) { long range = recordTimestampInMsMax - recordTimestampInMsMin;