Skip to content

KAFKA-19493: Incorrect rate metric with larger window size #20152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.kafka.common.message.LeaveGroupResponseData.MemberResponse;
import org.apache.kafka.common.message.SyncGroupRequestData;
import org.apache.kafka.common.metrics.Measurable;
import org.apache.kafka.common.metrics.MetricConfig;
import org.apache.kafka.common.metrics.Metrics;
import org.apache.kafka.common.metrics.Sensor;
import org.apache.kafka.common.metrics.stats.Avg;
Expand Down Expand Up @@ -1407,7 +1408,8 @@ public GroupCoordinatorMetrics(Metrics metrics, String metricGrpPrefix) {
this.metricGrpName,
"The number of successful rebalance events per hour, each event is composed of " +
"several failed re-trials until it succeeded"),
new Rate(TimeUnit.HOURS, new WindowedCount())
new Rate(TimeUnit.HOURS, new WindowedCount()),
new MetricConfig(successfulRebalanceSensor.metricConfig()).timeWindow(1, TimeUnit.HOURS)
);

this.failedRebalanceSensor = metrics.sensor("failed-rebalance");
Expand All @@ -1422,7 +1424,8 @@ public GroupCoordinatorMetrics(Metrics metrics, String metricGrpPrefix) {
"failed-rebalance-rate-per-hour",
this.metricGrpName,
"The number of failed rebalance events per hour"),
new Rate(TimeUnit.HOURS, new WindowedCount())
new Rate(TimeUnit.HOURS, new WindowedCount()),
new MetricConfig(failedRebalanceSensor.metricConfig()).timeWindow(1, TimeUnit.HOURS)
);

Measurable lastRebalance = (config, now) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.metrics.Measurable;
import org.apache.kafka.common.metrics.MetricConfig;
import org.apache.kafka.common.metrics.Metrics;
import org.apache.kafka.common.metrics.Sensor;
import org.apache.kafka.common.metrics.stats.Avg;
Expand Down Expand Up @@ -70,11 +71,19 @@ public ConsumerRebalanceMetricsManager(Metrics metrics) {
successfulRebalanceSensor.add(rebalanceLatencyMax, new Max());
successfulRebalanceSensor.add(rebalanceLatencyTotal, new CumulativeSum());
successfulRebalanceSensor.add(rebalanceTotal, new CumulativeCount());
successfulRebalanceSensor.add(rebalanceRatePerHour, new Rate(TimeUnit.HOURS, new WindowedCount()));
successfulRebalanceSensor.add(
rebalanceRatePerHour,
new Rate(TimeUnit.HOURS, new WindowedCount()),
new MetricConfig(successfulRebalanceSensor.metricConfig()).timeWindow(1, TimeUnit.HOURS)
);

failedRebalanceSensor = metrics.sensor("failed-rebalance");
failedRebalanceSensor.add(failedRebalanceTotal, new CumulativeSum());
failedRebalanceSensor.add(failedRebalanceRate, new Rate(TimeUnit.HOURS, new WindowedCount()));
failedRebalanceSensor.add(
failedRebalanceRate,
new Rate(TimeUnit.HOURS, new WindowedCount()),
new MetricConfig(failedRebalanceSensor.metricConfig()).timeWindow(1, TimeUnit.HOURS)
);

Measurable lastRebalance = (config, now) -> {
if (lastRebalanceEndMs == -1L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.kafka.clients.consumer.internals.metrics;

import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.metrics.MetricConfig;
import org.apache.kafka.common.metrics.Metrics;
import org.apache.kafka.common.metrics.Sensor;
import org.apache.kafka.common.metrics.stats.CumulativeCount;
Expand Down Expand Up @@ -45,7 +46,11 @@ public ShareRebalanceMetricsManager(Metrics metrics) {

rebalanceSensor = metrics.sensor("rebalance-latency");
rebalanceSensor.add(rebalanceTotal, new CumulativeCount());
rebalanceSensor.add(rebalanceRatePerHour, new Rate(TimeUnit.HOURS, new WindowedCount()));
rebalanceSensor.add(
rebalanceRatePerHour,
new Rate(TimeUnit.HOURS, new WindowedCount()),
new MetricConfig(rebalanceSensor.metricConfig()).timeWindow(1, TimeUnit.HOURS)
);
}

public void recordRebalanceStarted(long nowMs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public MetricConfig() {
this.recordingLevel = Sensor.RecordingLevel.INFO;
}

public MetricConfig(MetricConfig config) {
this.quota = config.quota;
this.samples = config.samples;
this.eventWindow = config.eventWindow;
this.timeWindowMs = config.timeWindowMs;
this.tags = config.tags;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being picky, this copy constructor is not copying the fields from the input parameter. Shouldn't we create a copy of the quota and tags, for example?

this.recordingLevel = config.recordingLevel;
}

public Quota quota() {
return this.quota;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ public String name() {
return this.name;
}

public MetricConfig metricConfig() {
return this.config;
}

List<Sensor> parents() {
return unmodifiableList(asList(parents));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,31 @@ public void testMetrics() {
assertEquals(3.0d, getMetric("rebalance-latency-avg").metricValue());
assertEquals(6.0d, getMetric("rebalance-latency-max").metricValue());
assertEquals(9.0d, getMetric("rebalance-latency-total").metricValue());
assertEquals(360.0d, getMetric("rebalance-rate-per-hour").metricValue());
assertEquals(3.0d, getMetric("rebalance-rate-per-hour").metricValue());
assertEquals(3.0d, getMetric("rebalance-total").metricValue());

metrics.sensor("failed-rebalance").record(1.0d);
metrics.sensor("failed-rebalance").record(6.0d);
metrics.sensor("failed-rebalance").record(2.0d);

assertEquals(360.0d, getMetric("failed-rebalance-rate-per-hour").metricValue());
assertEquals(3.0d, getMetric("failed-rebalance-rate-per-hour").metricValue());
assertEquals(3.0d, getMetric("failed-rebalance-total").metricValue());

assertEquals(-1.0d, getMetric("last-rebalance-seconds-ago").metricValue());
coordinator.setLastRebalanceTime(mockTime.milliseconds());
assertEquals(0.0d, getMetric("last-rebalance-seconds-ago").metricValue());
mockTime.sleep(10 * 1000L);
assertEquals(10.0d, getMetric("last-rebalance-seconds-ago").metricValue());

long windowLength = metrics.config().samples() * 3600_000L;
mockTime.sleep(windowLength - 10 * 1000L - 1);
assertEquals(3d / metrics.config().samples(), (double) getMetric("failed-rebalance-rate-per-hour").metricValue(), 0.1d);
assertEquals(3d / metrics.config().samples(), (double) getMetric("rebalance-rate-per-hour").metricValue(), 0.1d);

// WindowLength have passed, triggering metric reset
mockTime.sleep(1L);
assertEquals(0d, getMetric("failed-rebalance-rate-per-hour").metricValue());
assertEquals(0d, getMetric("rebalance-rate-per-hour").metricValue());
}

private KafkaMetric getMetric(final String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2385,10 +2385,20 @@ public void testRebalanceMetricsOnSuccessfulRebalance() {
assertEquals((double) reconciliationDurationMs, getMetricValue(metrics, rebalanceMetricsManager.rebalanceLatencyAvg));
assertEquals((double) reconciliationDurationMs, getMetricValue(metrics, rebalanceMetricsManager.rebalanceLatencyMax));
assertEquals(1d, getMetricValue(metrics, rebalanceMetricsManager.rebalanceTotal));
assertEquals(120d, 1d, (double) getMetricValue(metrics, rebalanceMetricsManager.rebalanceRatePerHour));
assertEquals(1d, getMetricValue(metrics, rebalanceMetricsManager.rebalanceRatePerHour));
assertEquals(0d, getMetricValue(metrics, rebalanceMetricsManager.failedRebalanceRate));
assertEquals(0d, getMetricValue(metrics, rebalanceMetricsManager.failedRebalanceTotal));
assertEquals(0d, getMetricValue(metrics, rebalanceMetricsManager.lastRebalanceSecondsAgo));

long windowLength = metrics.config().samples() * 3600_000L;
time.sleep(windowLength - reconciliationDurationMs - 1);
assertEquals(1d / metrics.config().samples(), (double) getMetricValue(metrics, rebalanceMetricsManager.rebalanceRatePerHour), 0.1d);
assertEquals(0d, getMetricValue(metrics, rebalanceMetricsManager.failedRebalanceRate));

// WindowLength have passed, triggering metric reset
time.sleep(windowLength);
assertEquals(0d, getMetricValue(metrics, rebalanceMetricsManager.rebalanceRatePerHour));
assertEquals(0d, getMetricValue(metrics, rebalanceMetricsManager.failedRebalanceRate));
}

@Test
Expand Down Expand Up @@ -2483,9 +2493,17 @@ public void testRebalanceMetricsOnFailedRebalance() {

assertEquals((double) 0, getMetricValue(metrics, rebalanceMetricsManager.rebalanceLatencyTotal));
assertEquals(0d, getMetricValue(metrics, rebalanceMetricsManager.rebalanceTotal));
assertEquals(120d, getMetricValue(metrics, rebalanceMetricsManager.failedRebalanceRate));
assertEquals(1d, getMetricValue(metrics, rebalanceMetricsManager.failedRebalanceRate));
assertEquals(1d, getMetricValue(metrics, rebalanceMetricsManager.failedRebalanceTotal));
assertEquals(-1d, getMetricValue(metrics, rebalanceMetricsManager.lastRebalanceSecondsAgo));

long windowLength = metrics.config().samples() * 3600_000L;
time.sleep(windowLength - 2300 - 1);
assertEquals(1d / metrics.config().samples(), (double) getMetricValue(metrics, rebalanceMetricsManager.failedRebalanceRate), 0.1d);

// WindowLength have passed, triggering metric reset
time.sleep(windowLength);
assertEquals(0d, getMetricValue(metrics, rebalanceMetricsManager.failedRebalanceRate));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,15 @@ public void testRebalanceMetricsOnSuccessfulRebalance() {
commitResult.complete(null);

assertEquals(1d, getMetricValue(metrics, rebalanceMetricsManager.rebalanceTotal));
assertEquals(120d, 1d, (double) getMetricValue(metrics, rebalanceMetricsManager.rebalanceRatePerHour));
assertEquals(1d, (double) getMetricValue(metrics, rebalanceMetricsManager.rebalanceRatePerHour));

long windowLength = metrics.config().samples() * 3600_000L;
time.sleep(windowLength - reconciliationDurationMs - 1);
assertEquals(1d / metrics.config().samples(), (double) getMetricValue(metrics, rebalanceMetricsManager.rebalanceRatePerHour), 0.1d);

// WindowLength have passed, triggering metric reset
time.sleep(windowLength);
assertEquals(0d, getMetricValue(metrics, rebalanceMetricsManager.rebalanceRatePerHour));
}

@Test
Expand Down Expand Up @@ -1620,7 +1628,7 @@ private ShareGroupHeartbeatResponse createShareGroupHeartbeatResponse(
.setMemberEpoch(MEMBER_EPOCH)
.setAssignment(assignment));
}

private ShareGroupHeartbeatResponse createShareGroupLeaveResponse(String memberId) {
return new ShareGroupHeartbeatResponse(new ShareGroupHeartbeatResponseData()
.setErrorCode(Errors.NONE.code())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public void testRebalanceMetrics() {
shareRebalanceMetricsManager.recordRebalanceEnded(100);

assertEquals(3.0d, metrics.metric(shareRebalanceMetricsManager.rebalanceTotal).metricValue());
assertEquals(360.d, metrics.metric(shareRebalanceMetricsManager.rebalanceRatePerHour).metricValue());
assertEquals(3.d, metrics.metric(shareRebalanceMetricsManager.rebalanceRatePerHour).metricValue());
}
}