This is an automated email from the ASF dual-hosted git repository.
dajac pushed a commit to branch 4.2
in repository https://gitbox.apache.org/repos/asf/kafka.git
The following commit(s) were added to refs/heads/4.2 by this push:
new 10d2de64b70 KAFKA-19888: Clamp negative values in coordinator
histograms (#20986)
10d2de64b70 is described below
commit 10d2de64b703709205f635babeb8904edd5b6a53
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));
}
}