This is an automated email from the ASF dual-hosted git repository.

dajac pushed a commit to branch 4.1
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/4.1 by this push:
     new 03a8aa6d86b KAFKA-19888: Clamp negative values in coordinator 
histograms (#20986)
03a8aa6d86b is described below

commit 03a8aa6d86b7126ee1f19592779f1116d489bce1
Author: Sean Quah <[email protected]>
AuthorDate: Wed Nov 26 14:25:40 2025 +0000

    KAFKA-19888: Clamp negative values in coordinator histograms (#20986)
    
    The coordinator runtime and group coordinator currently use wall clock
    time to measure durations for metrics. When the system clock goes
    backwards due to time adjustments, we attempt to record negative
    durations for metrics, which throws an ArrayIndexOutOfBoundsException
    exception. This causes request processing and partition loading to fail
    while the clock is being adjusted. If partition loading fails, the group
    the coordinator for that partition becomes unavailable until the broker
    is restarted or leadership changes again.
    
    To address this, we clamp negative durations to zero in histograms
    instead of throwing ArrayIndexOutOfBoundsExceptions. We will move
    towards using a monotonic clock for metrics in future work.
    
    Reviewers: David Jacot <[email protected]>
---
 .../org/apache/kafka/coordinator/common/runtime/HdrHistogram.java   | 6 +++---
 .../apache/kafka/coordinator/common/runtime/HdrHistogramTest.java   | 6 +++++-
 2 files changed, 8 insertions(+), 4 deletions(-)

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 5cda3fd44c2..1cbbe38b0f4 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 @@ public final class HdrHistogram {
     }
 
     /**
-     * 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 76e034ab70b..b16260a7c8a 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 class HdrHistogramTest {
 
     @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));
     }
 }

Reply via email to