You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "kirktrue (via GitHub)" <gi...@apache.org> on 2026/04/02 00:22:10 UTC
Re: [PR] KAFKA-20332: Fix to ensure app thread not collecting records for partitions being revoked [kafka]
kirktrue commented on code in PR #21897:
URL: https://github.com/apache/kafka/pull/21897#discussion_r3025221660
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/events/AsyncPollEvent.java:
##########
@@ -85,6 +86,25 @@ public void markValidatePositionsComplete() {
this.isValidatePositionsComplete = true;
}
+ /**
+ *
Review Comment:
Super nitpicky: extra line.
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AsyncKafkaConsumer.java:
##########
@@ -2004,6 +2004,15 @@ private Fetch<K, V> pollForFetches(Timer timer) {
* for returning.
*/
private Fetch<K, V> collectFetch() {
+ // Do not return buffered records if the background hasn't checked for pending reconciliations
+ // for the inflight poll event.
+ // This is key because partitions may need revocation, so we need to wait for the reconciliation check
+ // that triggers commits and marks partitions as pending revocation, before we can
+ // safely collect records from the buffer.
+ if (inflightPoll != null && !inflightPoll.isReconciliationCheckComplete()) {
+ return Fetch.empty();
+ }
Review Comment:
My main concern is if this affect the CPU performance 🤔
When reconciliation hasn't occurred and this method returns an empty `Fetch`, the calling code will "fall through" to block on the `FetchBuffer`. Having the application thread block for the background thread to produce data really hurts the CPU load metrics.
But in the end, correctness wins. It would be ideal if we could see if this change increases CPU load, and if so, by how much.
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/ShareMembershipManager.java:
##########
@@ -199,4 +200,16 @@ public int joinGroupEpoch() {
public int leaveGroupEpoch() {
return ShareGroupHeartbeatRequest.LEAVE_GROUP_MEMBER_EPOCH;
}
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * For the ShareConsumer, full reconciliations can always be triggered from the background thread
+ * (fully updates assignment).
+ */
+ @Override
+ public PollResult poll(final long currentTimeMs) {
+ maybeReconcile(true);
+ return PollResult.EMPTY;
+ }
Review Comment:
Is this directly related to this change? I assume that the changes made to `maybeReconcile()` trigger the need to add this.
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/ShareMembershipManager.java:
##########
@@ -199,4 +200,16 @@ public int joinGroupEpoch() {
public int leaveGroupEpoch() {
return ShareGroupHeartbeatRequest.LEAVE_GROUP_MEMBER_EPOCH;
}
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * For the ShareConsumer, full reconciliations can always be triggered from the background thread
+ * (fully updates assignment).
+ */
+ @Override
+ public PollResult poll(final long currentTimeMs) {
+ maybeReconcile(true);
+ return PollResult.EMPTY;
+ }
Review Comment:
Also, do we need a new unit test to validate this behavior? Or is it covered by an existing test?
##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/AsyncKafkaConsumerTest.java:
##########
@@ -529,6 +529,68 @@ public void testClearWakeupTriggerAfterPoll() {
assertDoesNotThrow(() -> consumer.poll(Duration.ZERO));
}
+ /**
+ * Test that poll() does not return records until the reconciliation check is complete.
+ * This prevents a race condition where records could be returned for partitions that
+ * are being revoked (see KAFKA-20332).
+ */
+ @Test
+ public void testPollWaitsForReconciliationCheckComplete() {
+ final String topicName = "foo";
+ final int partition = 3;
+ final TopicPartition tp = new TopicPartition(topicName, partition);
+ final List<ConsumerRecord<String, String>> records = asList(
+ new ConsumerRecord<>(topicName, partition, 2, "key1", "value1"),
+ new ConsumerRecord<>(topicName, partition, 3, "key2", "value2")
+ );
+
+ SubscriptionState subscriptions = new SubscriptionState(new LogContext(), AutoOffsetResetStrategy.NONE);
+ consumer = newConsumer(
+ mock(FetchBuffer.class),
+ new ConsumerInterceptors<>(Collections.emptyList(), metrics),
+ mock(ConsumerRebalanceListenerInvoker.class),
+ subscriptions);
+
+ doReturn(LeaderAndEpoch.noLeaderOrEpoch()).when(metadata).currentLeader(any());
+ // PositionsValidator starts with metadataUpdateVersion=-1. Stub metadata.updateVersion() to match,
+ // so canSkipUpdateFetchPositions() passes and we test the reconciliation check path.
+ doReturn(-1).when(metadata).updateVersion();
+
+ completeTopicSubscriptionChangeEventSuccessfully();
+ consumer.subscribe(singleton(topicName), mock(ConsumerRebalanceListener.class));
+ // Simulate partition assignment from group coordinator
+ subscriptions.assignFromSubscribed(singleton(tp));
+
+ // Set up position so canSkipUpdateFetchPositions() returns true (partition in FETCHING state)
+ completeSeekUnvalidatedEventSuccessfully();
+ subscriptions.seek(tp, 0);
+
+ // Set up fetch collector to return records when called
+ doReturn(Fetch.forPartition(tp, records, true, new OffsetAndMetadata(4, Optional.of(0), "")))
+ .when(fetchCollector).collectFetch(any(FetchBuffer.class));
+
+ // Capture the AsyncPollEvent to manually control when reconciliation check is marked complete
+ AtomicReference<AsyncPollEvent> capturedEvent = new AtomicReference<>();
+ doAnswer(invocation -> {
+ AsyncPollEvent event = invocation.getArgument(0);
+ capturedEvent.set(event);
Review Comment:
In some cases `AsyncKafkaConsumer.poll()` can _technically_ submit more than `AsyncPollEvent`. Perhaps I'm being too paranoid, but defensively, this line could ensure that doesn't happen by a CAS:
```suggestion
assertTrue(capturedEvent.compareAndSet(null, event));
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org