diff --git a/coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/HdrHistogram.java b/coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/HdrHistogram.java index 5cda3fd44c26a..1cbbe38b0f4d1 100644 --- a/coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/HdrHistogram.java +++ b/coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/HdrHistogram.java @@ -92,12 +92,12 @@ private Histogram latestHistogram(long now) { } /** - * Writes to the histogram. Caps recording to highestTrackableValue + * Writes to the histogram. Caps recording between 0 and highestTrackableValue. * - * @param value The value to be recorded. Cannot be negative. + * @param value The value to be recorded. */ public void record(long value) { - recorder.recordValue(Math.min(value, highestTrackableValue)); + recorder.recordValue(Math.min(Math.max(value, 0), highestTrackableValue)); } /** diff --git a/coordinator-common/src/test/java/org/apache/kafka/coordinator/common/runtime/HdrHistogramTest.java b/coordinator-common/src/test/java/org/apache/kafka/coordinator/common/runtime/HdrHistogramTest.java index 76e034ab70b60..b16260a7c8acd 100644 --- a/coordinator-common/src/test/java/org/apache/kafka/coordinator/common/runtime/HdrHistogramTest.java +++ b/coordinator-common/src/test/java/org/apache/kafka/coordinator/common/runtime/HdrHistogramTest.java @@ -216,10 +216,14 @@ public void testLatestHistogramRace() throws InterruptedException, ExecutionExce @Test public void testRecordLimit() { + long now = System.currentTimeMillis(); long highestTrackableValue = 10L; HdrHistogram hdrHistogram = new HdrHistogram(10L, highestTrackableValue, 3); hdrHistogram.record(highestTrackableValue + 1000L); - assertEquals(highestTrackableValue, hdrHistogram.max(System.currentTimeMillis())); + assertEquals(highestTrackableValue, hdrHistogram.max(now)); + + hdrHistogram.record(-50L); + assertEquals(0, hdrHistogram.max(now + 1000L)); } }