-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-8421: Still return data during rebalance #7312
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
Changes from all commits
37b5c63
4a34116
d964cea
f5b8f96
b07bac2
29f8942
3000d6f
8464fa7
f804b3d
3c62029
284ef77
9825063
929048a
58adc09
c79a93d
75c4e88
95ef3fb
3b844ff
781514d
8b08a3e
8fa5d74
d75fcb9
b176e4e
7a826f9
31e372e
366739b
faf787e
c71af82
ffe6146
518049c
30d5e53
fa3456d
0220d96
27d013a
94611cc
b529847
33fd785
1df9808
3057405
21f531d
effb29d
6fbd976
f4e7111
91b6606
0ad52ec
27f76b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| import org.apache.kafka.common.errors.FencedInstanceIdException; | ||
| import org.apache.kafka.common.errors.GroupAuthorizationException; | ||
| import org.apache.kafka.common.errors.InterruptException; | ||
| import org.apache.kafka.common.errors.RebalanceInProgressException; | ||
| import org.apache.kafka.common.errors.RetriableException; | ||
| import org.apache.kafka.common.errors.TimeoutException; | ||
| import org.apache.kafka.common.errors.TopicAuthorizationException; | ||
|
|
@@ -438,7 +439,7 @@ public boolean poll(Timer timer) { | |
|
|
||
| invokeCompletedOffsetCommitCallbacks(); | ||
|
|
||
| if (subscriptions.partitionsAutoAssigned()) { | ||
| if (subscriptions.hasAutoAssignedPartitions()) { | ||
| if (protocol == null) { | ||
| throw new IllegalStateException("User configured " + ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG + | ||
| " to empty while trying to subscribe for group protocol to auto assign partitions"); | ||
|
|
@@ -706,7 +707,7 @@ public void onLeavePrepare() { | |
| // we should reset assignment and trigger the callback before leaving group | ||
| Set<TopicPartition> droppedPartitions = new HashSet<>(subscriptions.assignedPartitions()); | ||
|
|
||
| if (subscriptions.partitionsAutoAssigned() && !droppedPartitions.isEmpty()) { | ||
| if (subscriptions.hasAutoAssignedPartitions() && !droppedPartitions.isEmpty()) { | ||
| final Exception e; | ||
| if (generation() != Generation.NO_GENERATION) { | ||
| e = invokePartitionsRevoked(droppedPartitions); | ||
|
|
@@ -727,7 +728,7 @@ public void onLeavePrepare() { | |
| */ | ||
| @Override | ||
| public boolean rejoinNeededOrPending() { | ||
| if (!subscriptions.partitionsAutoAssigned()) | ||
| if (!subscriptions.hasAutoAssignedPartitions()) | ||
| return false; | ||
|
|
||
| // we need to rejoin if we performed the assignment and metadata has changed; | ||
|
|
@@ -759,14 +760,24 @@ public boolean refreshCommittedOffsetsIfNeeded(Timer timer) { | |
| final TopicPartition tp = entry.getKey(); | ||
| final OffsetAndMetadata offsetAndMetadata = entry.getValue(); | ||
| if (offsetAndMetadata != null) { | ||
| final ConsumerMetadata.LeaderAndEpoch leaderAndEpoch = metadata.leaderAndEpoch(tp); | ||
| final SubscriptionState.FetchPosition position = new SubscriptionState.FetchPosition( | ||
| offsetAndMetadata.offset(), offsetAndMetadata.leaderEpoch(), | ||
| leaderAndEpoch); | ||
|
|
||
| log.info("Setting offset for partition {} to the committed offset {}", tp, position); | ||
| // first update the epoch if necessary | ||
| entry.getValue().leaderEpoch().ifPresent(epoch -> this.metadata.updateLastSeenEpochIfNewer(entry.getKey(), epoch)); | ||
| this.subscriptions.seekUnvalidated(tp, position); | ||
|
|
||
| // it's possible that the partition is no longer assigned when the response is received, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it's worth adding a debug message saying that we're ignoring the fetched offset? |
||
| // so we need to ignore seeking if that's the case | ||
| if (this.subscriptions.isAssigned(tp)) { | ||
| final ConsumerMetadata.LeaderAndEpoch leaderAndEpoch = metadata.leaderAndEpoch(tp); | ||
| final SubscriptionState.FetchPosition position = new SubscriptionState.FetchPosition( | ||
| offsetAndMetadata.offset(), offsetAndMetadata.leaderEpoch(), | ||
| leaderAndEpoch); | ||
|
|
||
| this.subscriptions.seekUnvalidated(tp, position); | ||
|
|
||
| log.info("Setting offset for partition {} to the committed offset {}", tp, position); | ||
| } else { | ||
| log.info("Ignoring the returned {} since its partition {} is no longer assigned", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd suggest "Ignoring the fetched committed offset"
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I originally did that, but then I realize |
||
| offsetAndMetadata, tp); | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
|
|
@@ -986,7 +997,7 @@ private void doAutoCommitOffsetsAsync() { | |
|
|
||
| commitOffsetsAsync(allConsumedOffsets, (offsets, exception) -> { | ||
| if (exception != null) { | ||
| if (exception instanceof RetriableException) { | ||
| if (exception instanceof RetriableCommitFailedException) { | ||
| log.debug("Asynchronous auto-commit of offsets {} failed due to retriable error: {}", offsets, | ||
| exception); | ||
| nextAutoCommitTimer.updateAndReset(rebalanceConfig.retryBackoffMs); | ||
|
|
@@ -1066,16 +1077,28 @@ private RequestFuture<Void> sendOffsetCommitRequest(final Map<TopicPartition, Of | |
| } | ||
|
|
||
| final Generation generation; | ||
| if (subscriptions.partitionsAutoAssigned()) { | ||
| if (subscriptions.hasAutoAssignedPartitions()) { | ||
| generation = generationIfStable(); | ||
| // if the generation is null, we are not part of an active group (and we expect to be). | ||
| // the only thing we can do is fail the commit and let the user rejoin the group in poll() | ||
| // the only thing we can do is fail the commit and let the user rejoin the group in poll(). | ||
| if (generation == null) { | ||
| log.info("Failing OffsetCommit request since the consumer is not part of an active group"); | ||
| return RequestFuture.failure(new CommitFailedException()); | ||
|
|
||
| if (rebalanceInProgress()) { | ||
| // if the client knows it is already rebalancing, we can use RebalanceInProgressException instead of | ||
| // CommitFailedException to indicate this is not a fatal error | ||
| return RequestFuture.failure(new RebalanceInProgressException("Offset commit cannot be completed since the " + | ||
| "consumer is undergoing a rebalance for auto partition assignment. You can try completing the rebalance " + | ||
| "by calling poll() and then retry the operation.")); | ||
| } else { | ||
| return RequestFuture.failure(new CommitFailedException("Offset commit cannot be completed since the " + | ||
| "consumer is not part of an active group for auto partition assignment; it is likely that the consumer " + | ||
| "was kicked out of the group.")); | ||
| } | ||
| } | ||
| } else | ||
| } else { | ||
| generation = Generation.NO_GENERATION; | ||
| } | ||
|
|
||
| OffsetCommitRequest.Builder builder = new OffsetCommitRequest.Builder( | ||
| new OffsetCommitRequestData() | ||
|
|
@@ -1148,15 +1171,18 @@ public void handle(OffsetCommitResponse commitResponse, RequestFuture<Void> futu | |
| future.raise(error); | ||
| return; | ||
| } else if (error == Errors.REBALANCE_IN_PROGRESS) { | ||
| /* Consumer never tries to commit offset in between join-group and sync-group, | ||
| /* Consumer should not try to commit offset in between join-group and sync-group, | ||
| * and hence on broker-side it is not expected to see a commit offset request | ||
| * during CompletingRebalance phase; if it ever happens then broker would return | ||
| * this error. In this case we should just treat as a fatal CommitFailed exception. | ||
| * However, we do not need to reset generations and just request re-join, such that | ||
| * if the caller decides to proceed and poll, it would still try to proceed and re-join normally. | ||
| * this error to indicate that we are still in the middle of a rebalance. | ||
| * In this case we would throw a RebalanceInProgressException, | ||
| * request re-join but do not reset generations. If the callers decide to retry they | ||
| * can go ahead and call poll to finish up the rebalance first, and then try commit again. | ||
| */ | ||
| requestRejoin(); | ||
| future.raise(new CommitFailedException()); | ||
| future.raise(new RebalanceInProgressException("Offset commit cannot be completed since the " + | ||
| "consumer group is executing a rebalance at the moment. You can try completing the rebalance " + | ||
| "by calling poll() and then retry commit again")); | ||
| return; | ||
| } else if (error == Errors.UNKNOWN_MEMBER_ID | ||
|
guozhangwang marked this conversation as resolved.
|
||
| || error == Errors.ILLEGAL_GENERATION) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,17 +26,16 @@ | |
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class KafkaConsumerMetrics implements AutoCloseable { | ||
| private final Metrics metrics; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a minor cleanup: do not need local variable. |
||
| private final MetricName lastPollMetricName; | ||
| private final Sensor timeBetweenPollSensor; | ||
| private final Sensor pollIdleSensor; | ||
| private final Metrics metrics; | ||
| private long lastPollMs; | ||
| private long pollStartMs; | ||
| private long timeSinceLastPollMs; | ||
|
|
||
| public KafkaConsumerMetrics(Metrics metrics, String metricGrpPrefix) { | ||
| this.metrics = metrics; | ||
|
|
||
| String metricGroupName = metricGrpPrefix + "-metrics"; | ||
| Measurable lastPoll = (mConfig, now) -> { | ||
| if (lastPollMs == 0L) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is not used anywhere so remove.