Skip to content

Commit 03a8aa6

Browse files
squah-confluentdajac
authored andcommitted
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]>
1 parent 207e4d6 commit 03a8aa6

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/HdrHistogram.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ private Histogram latestHistogram(long now) {
9292
}
9393

9494
/**
95-
* Writes to the histogram. Caps recording to highestTrackableValue
95+
* Writes to the histogram. Caps recording between 0 and highestTrackableValue.
9696
*
97-
* @param value The value to be recorded. Cannot be negative.
97+
* @param value The value to be recorded.
9898
*/
9999
public void record(long value) {
100-
recorder.recordValue(Math.min(value, highestTrackableValue));
100+
recorder.recordValue(Math.min(Math.max(value, 0), highestTrackableValue));
101101
}
102102

103103
/**

coordinator-common/src/test/java/org/apache/kafka/coordinator/common/runtime/HdrHistogramTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,14 @@ public void testLatestHistogramRace() throws InterruptedException, ExecutionExce
216216

217217
@Test
218218
public void testRecordLimit() {
219+
long now = System.currentTimeMillis();
219220
long highestTrackableValue = 10L;
220221
HdrHistogram hdrHistogram = new HdrHistogram(10L, highestTrackableValue, 3);
221222

222223
hdrHistogram.record(highestTrackableValue + 1000L);
223-
assertEquals(highestTrackableValue, hdrHistogram.max(System.currentTimeMillis()));
224+
assertEquals(highestTrackableValue, hdrHistogram.max(now));
225+
226+
hdrHistogram.record(-50L);
227+
assertEquals(0, hdrHistogram.max(now + 1000L));
224228
}
225229
}

0 commit comments

Comments
 (0)