forked from apache/kafka
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KAFKA-16116: Rebalance Metrics for AsyncKafkaConsumer (apache#15339)
Adding the following rebalance metrics to the consumer: rebalance-latency-avg rebalance-latency-max rebalance-latency-total rebalance-rate-per-hour rebalance-total failed-rebalance-rate-per-hour failed-rebalance-total Due to the difference in protocol, we need to redefine when rebalance starts and ends. Start of Rebalance: Current: Right before sending out JoinGroup ConsumerGroup: When the client receives assignments from the HB End of Rebalance - Successful Case: Current: Receiving SyncGroup request after transitioning to "COMPLETING_REBALANCE" ConsumerGroup: After completing reconciliation and right before sending out "Ack" heartbeat End of Rebalance - Failed Case: Current: Any failure in the JoinGroup/SyncGroup response ConsumerGroup: Failure in the heartbeat Note: Afterall, we try to be consistent with the current protocol. Rebalances start and end with sending and receiving network requests. Failures in network requests signify the user failures in rebalance. And it is entirely possible to have multiple failures before having a successful one. Reviewers: Lucas Brutschy <[email protected]>
- Loading branch information
Showing
8 changed files
with
408 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...ain/java/org/apache/kafka/clients/consumer/internals/metrics/RebalanceMetricsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kafka.clients.consumer.internals.metrics; | ||
|
||
import org.apache.kafka.common.MetricName; | ||
import org.apache.kafka.common.metrics.Measurable; | ||
import org.apache.kafka.common.metrics.Metrics; | ||
import org.apache.kafka.common.metrics.Sensor; | ||
import org.apache.kafka.common.metrics.stats.Avg; | ||
import org.apache.kafka.common.metrics.stats.CumulativeCount; | ||
import org.apache.kafka.common.metrics.stats.CumulativeSum; | ||
import org.apache.kafka.common.metrics.stats.Max; | ||
import org.apache.kafka.common.metrics.stats.Rate; | ||
import org.apache.kafka.common.metrics.stats.WindowedCount; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.apache.kafka.clients.consumer.internals.ConsumerUtils.CONSUMER_METRIC_GROUP_PREFIX; | ||
import static org.apache.kafka.clients.consumer.internals.ConsumerUtils.COORDINATOR_METRICS_SUFFIX; | ||
|
||
public class RebalanceMetricsManager { | ||
private final Sensor successfulRebalanceSensor; | ||
private final Sensor failedRebalanceSensor; | ||
private final String metricGroupName; | ||
|
||
public final MetricName rebalanceLatencyAvg; | ||
public final MetricName rebalanceLatencyMax; | ||
public final MetricName rebalanceLatencyTotal; | ||
public final MetricName rebalanceTotal; | ||
public final MetricName rebalanceRatePerHour; | ||
public final MetricName lastRebalanceSecondsAgo; | ||
public final MetricName failedRebalanceTotal; | ||
public final MetricName failedRebalanceRate; | ||
private long lastRebalanceEndMs = -1L; | ||
private long lastRebalanceStartMs = -1L; | ||
|
||
public RebalanceMetricsManager(Metrics metrics) { | ||
metricGroupName = CONSUMER_METRIC_GROUP_PREFIX + COORDINATOR_METRICS_SUFFIX; | ||
|
||
rebalanceLatencyAvg = createMetric(metrics, "rebalance-latency-avg", | ||
"The average time taken for a group to complete a rebalance"); | ||
rebalanceLatencyMax = createMetric(metrics, "rebalance-latency-max", | ||
"The max time taken for a group to complete a rebalance"); | ||
rebalanceLatencyTotal = createMetric(metrics, "rebalance-latency-total", | ||
"The total number of milliseconds spent in rebalances"); | ||
rebalanceTotal = createMetric(metrics, "rebalance-total", | ||
"The total number of rebalance events"); | ||
rebalanceRatePerHour = createMetric(metrics, "rebalance-rate-per-hour", | ||
"The number of rebalance events per hour"); | ||
failedRebalanceTotal = createMetric(metrics, "failed-rebalance-total", | ||
"The total number of failed rebalance events"); | ||
failedRebalanceRate = createMetric(metrics, "failed-rebalance-rate-per-hour", | ||
"The number of failed rebalance events per hour"); | ||
|
||
successfulRebalanceSensor = metrics.sensor("rebalance-latency"); | ||
successfulRebalanceSensor.add(rebalanceLatencyAvg, new Avg()); | ||
successfulRebalanceSensor.add(rebalanceLatencyMax, new Max()); | ||
successfulRebalanceSensor.add(rebalanceLatencyTotal, new CumulativeSum()); | ||
successfulRebalanceSensor.add(rebalanceTotal, new CumulativeCount()); | ||
successfulRebalanceSensor.add(rebalanceRatePerHour, new Rate(TimeUnit.HOURS, new WindowedCount())); | ||
|
||
failedRebalanceSensor = metrics.sensor("failed-rebalance"); | ||
failedRebalanceSensor.add(failedRebalanceTotal, new CumulativeSum()); | ||
failedRebalanceSensor.add(failedRebalanceRate, new Rate(TimeUnit.HOURS, new WindowedCount())); | ||
|
||
Measurable lastRebalance = (config, now) -> { | ||
if (lastRebalanceEndMs == -1L) | ||
return -1d; | ||
else | ||
return TimeUnit.SECONDS.convert(now - lastRebalanceEndMs, TimeUnit.MILLISECONDS); | ||
}; | ||
lastRebalanceSecondsAgo = createMetric(metrics, | ||
"last-rebalance-seconds-ago", | ||
"The number of seconds since the last rebalance event"); | ||
metrics.addMetric(lastRebalanceSecondsAgo, lastRebalance); | ||
} | ||
|
||
private MetricName createMetric(Metrics metrics, String name, String description) { | ||
return metrics.metricName(name, metricGroupName, description); | ||
} | ||
|
||
public void recordRebalanceStarted(long nowMs) { | ||
lastRebalanceStartMs = nowMs; | ||
} | ||
|
||
public void recordRebalanceEnded(long nowMs) { | ||
lastRebalanceEndMs = nowMs; | ||
successfulRebalanceSensor.record(nowMs - lastRebalanceStartMs); | ||
} | ||
|
||
public void maybeRecordRebalanceFailed() { | ||
if (lastRebalanceStartMs <= lastRebalanceEndMs) | ||
return; | ||
failedRebalanceSensor.record(); | ||
} | ||
|
||
public boolean rebalanceStarted() { | ||
return lastRebalanceStartMs > lastRebalanceEndMs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.