You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "brandboat (via GitHub)" <gi...@apache.org> on 2024/12/07 03:32:54 UTC

[PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped, potentially leading to corrupted added partitions [kafka]

brandboat opened a new pull request, #18089:
URL: https://github.com/apache/kafka/pull/18089

   related to https://issues.apache.org/jira/browse/KAFKA-18160
   
   This pr fix two issues as described below:
   
   - ConsumerRebalanceListenerCallbackCompletedEvent is skipped
   `invokeRebalanceCallbacks`could throw WakeupException/InterruptException [0] and they are NOT handled. Hence, the event `ConsumerRebalanceListenerCallbackCompletedEvent` is NOT sent to background thread.
   
   Solution: We should use try-catch blocks to propagate both InterruptedException and WakeupException to the background thread.
   
   - Corrupted added partitions
   In the next iteration of invokeRebalanceCallbacks, non-fetchable assigned partitions are treated as owned partitions [1]. This results in "empty" partitions being passed to the listener, meaning that the listener never receives the correctly added partitions after the first execution fails. Consequently, this causes the test_pause_and_resume_sink ([KAFKA-17962](https://issues.apache.org/jira/browse/KAFKA-17962)) to become unstable when using AsyncConsumer.
   
   Solution: We should add only partitions where pendingOnAssignedCallback is false to the owned partitions.
   
   [0] https://github.com/apache/kafka/blob/2d39d5be64d4f5b6446f4b9ec3f32b039707d9d1/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AsyncKafkaConsumer.java#L2046
   
   [1] https://github.com/apache/kafka/blob/2d39d5be64d4f5b6446f4b9ec3f32b039707d9d1/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java#L828
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880272565


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   Hi all, to me, this is more about how closely the behavior of the classic and async consumers should align. Personally, I think we should strive to match the behavior of the classic consumer as closely as possible. We can start by documenting any issues we encounter, as Chia-Ping suggested, by opening a JIRA ticket (or a KIP if necessary). Doing this would also allow us to leverage the existing tests to cover all possible scenarios. Just my 2 cents, thanks!



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1878451069


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   I'd like to better understand this change. Could you please provide a small summary for it?



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880874503


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   great, we're on the same page @chia7712. If you agree @brandboat, could you try the sample code from above,  we'll validate it with the test already included here that fails without this PR. Thanks!



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 merged PR #18089:
URL: https://github.com/apache/kafka/pull/18089


-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1879280407


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   I see, and agree we you there @chia7712, the 2nd round of callbacks will include empty partitions (on both consumers), but then isn't the bug still on the recovery (meaning, re-enabling the partitions that were awaiting callbacks)? 
   
   On the new consumer we "enable" only the added partitions (empty on the 2nd round of callbacks), but we still have "assignedPartitions" that will remain awaitingCallbacks so never fetchable. This line:
   https://github.com/apache/kafka/blob/3cf8745243026b83d97fbc80f2b1fe03f1455701/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java#L1181
   
   Shouldn't we make sure we `enablePartitionsAwaitingCallback` for all assignedPartitions instead of only the added ones? and that's what will allow to resume fetching



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880705091


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   If I remember correctly, if the callback always fails, the async consumer poll will also fail. So, even if we mark the added partitions as fetchable, the user still can't consume any data. Is that correct?



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880856270


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   > we could end up in a weird situation sending a fetch request in the background? It's not conceptually right.
   
   Yes, you're right. If the background thread is too slow to produce the `ConsumerRebalanceListenerCallbackNeededEvent`, the application thread could successfully append a `CreateFetchRequestsEvent` to initiate the fetch request even while it is in the `RECONCILING` state.



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1878512850


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   Hey Chia, interesting finding, I do believe we have a bug here but not sure if it's the one we're describing:
   1. Regarding 
   > the classic consumer continues to send fetch requests for those partitions even if the listener throws an exception
   
   I may be missing something but I expect that's not the case, because if callbacks fail, the classic detects that when polling the coordinator here and propagates the exception, so it never makes it to the fetching part of the consumer.poll
   https://github.com/apache/kafka/blob/f57fd2d9fd1c8a240d6c15ad35e83fd9283a958f/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L641
   We intentionally introduced the awaitingCallback status for partitions to achieve the same with the new consumer, and do not allow fetch if the onAssigned callback hasn't completed successfully (users may be seeking on those, for instance, so we really shouldn't fetch until its done)
   
   2. The test you shared does reveal an inconsistent, but I would describe the bug as : the new consumer does not recover to fetch after a failed onAssigned callback. And for that bug, I wonder if the gap is that once the callback fails the first time, the new consumer considers the partition assigned (but awaitingCallback). So on the next reconciliation attempt, where we expect that the callback should be triggered again, I think it may not be triggered because we consider only "added" partitions (and this one is not added in this second round)
   https://github.com/apache/kafka/blob/f57fd2d9fd1c8a240d6c15ad35e83fd9283a958f/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java#L1177  
   
   Is that maybe the bug? Thoughts?



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1878537031


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   > I think it may not be triggered because we consider only "added" partitions (and this one is not added in this second round)
   
   Yes, I was on the same page. However, adding those partitions to the second round would introduce another inconsistency ([see this comment](https://github.com/apache/kafka/pull/18089#issuecomment-2524852986)).



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880843295


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   that's true in the app thread, but the risk is that with the new consumer the fetching happens in the background thread, so I wonder if by enabling the partition when the callback failed (and hasn't recovered) we could end up sending a fetch request in the background? It's not conceptually right. 
   
   I don't understand why do we need to enable the partition for fetching if the callback failed and hasn't recovered? Isn't it enough to keep the logic as it was (enable for fetching only when callback succeeds), but with the fix of enabling all assigned partitions? 



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1878512850


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   Hey Chia, interesting finding, I do believe we have a bug here but not sure if it's the one we're describing:
   1. Regarding 
   > the classic consumer continues to send fetch requests for those partitions even if the listener throws an exception
   I may be missing something but I expect that's not the case, because if callbacks fail, the classic detects that when polling the coordinator here and propagates the exception, so it never makes it to the fetching part of the consumer.poll
   https://github.com/apache/kafka/blob/f57fd2d9fd1c8a240d6c15ad35e83fd9283a958f/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L641
   We intentionally introduced the awaitingCallback status for partitions to achieve the same with the new consumer, and do not allow fetch if the onAssigned callback hasn't completed successfully (users may be seeking on those, for instance, so we really shouldn't fetch until its done)
   2. The test you shared does reveal an inconsistent, but I would describe the bug as : the new consumer does not recover to fetch after a failed onAssigned callback. And for that bug, I wonder if the gap is that once the callback fails the first time, the new consumer considers the partition assigned (but awaitingCallback). So on the next reconciliation attempt, where we expect that the callback should be triggered again, I think it may not be triggered because we consider only "added" partitions (and this one is not added in this second round)
   https://github.com/apache/kafka/blob/f57fd2d9fd1c8a240d6c15ad35e83fd9283a958f/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java#L1177  
   
   Is that maybe the bug? Thoughts?



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   Hey Chia, interesting finding, I do believe we have a bug here but not sure if it's the one we're describing:
   1. Regarding 
   > the classic consumer continues to send fetch requests for those partitions even if the listener throws an exception
   
   I may be missing something but I expect that's not the case, because if callbacks fail, the classic detects that when polling the coordinator here and propagates the exception, so it never makes it to the fetching part of the consumer.poll
   https://github.com/apache/kafka/blob/f57fd2d9fd1c8a240d6c15ad35e83fd9283a958f/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L641
   We intentionally introduced the awaitingCallback status for partitions to achieve the same with the new consumer, and do not allow fetch if the onAssigned callback hasn't completed successfully (users may be seeking on those, for instance, so we really shouldn't fetch until its done)
   2. The test you shared does reveal an inconsistent, but I would describe the bug as : the new consumer does not recover to fetch after a failed onAssigned callback. And for that bug, I wonder if the gap is that once the callback fails the first time, the new consumer considers the partition assigned (but awaitingCallback). So on the next reconciliation attempt, where we expect that the callback should be triggered again, I think it may not be triggered because we consider only "added" partitions (and this one is not added in this second round)
   https://github.com/apache/kafka/blob/f57fd2d9fd1c8a240d6c15ad35e83fd9283a958f/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java#L1177  
   
   Is that maybe the bug? Thoughts?



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880221790


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   > then isn't the bug still on the recovery (meaning, re-enabling the partitions that were awaiting callbacks)?
   
   Introducing the new concept of "recovery" presents another issue: identifying which partitions are currently undergoing recovery. For example, some callbacks might process some partitions successfully, so it is hard to pass "recovery" partitions to the correct callbacks. Additionally, according to the [documentation](https://github.com/apache/kafka/blob/7591868aead54fff7d5e8a44c5e06746ed34866b/clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRebalanceListener.java#L87), we may already define the behavior of the callback.
   
   However, I agree that the current behavior appears to be a bug, but addressing it may require a KIP (or further discussion) to define the new behavior of the callback in handling exceptions. For now, this PR can focus on fixing the inconsistent behavior.
   



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880554051


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   my concern with the change that we currently have in this PR is that it seems to me it's allowing to fetch records even if callbacks always fail, doesn't it? (vs. allow fetch if the callback recovers)
   
   I expect the classic does not allow fetch if the callback always fail, because it will always break the poll loop when polling the coordinator, before making it to the fetch part. 
   
   The classic consumer allows fetch after a failed callback only if the callback recovers (and this is what the new consumer doesn't achieve because in that case t only re-enables added instead of assigned).
   
   Sorry if I'm missing something, thoughts? 



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1878512850


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   Hey Chia, interesting finding, I do believe we have a bug here but not sure if it's the one we're describing:
   1. Regarding 
   > the classic consumer continues to send fetch requests for those partitions even if the listener throws an exception
   
   I may be missing something but I expect that's not the case, because if callbacks fail, the classic detects that when polling the coordinator here and propagates the exception, so it never makes it to the fetching part of the consumer.poll
   https://github.com/apache/kafka/blob/f57fd2d9fd1c8a240d6c15ad35e83fd9283a958f/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L641
   We intentionally introduced the awaitingCallback status for partitions to achieve the same with the new consumer, and do not allow fetch if the onAssigned callback hasn't completed successfully (users may be seeking on those, for instance, so we really shouldn't fetch until its done)
   
   2. The test you shared does reveal an inconsistency, but I would describe the bug as : the new consumer does not recover to fetch after a failed onAssigned callback. And for that bug, I wonder if the gap is that once the callback fails the first time, the new consumer considers the partition assigned (but awaitingCallback). So on the next reconciliation attempt, where we expect that the callback should be triggered again, I think it may not be triggered because we consider only "added" partitions (and this one is not added in this second round)
   https://github.com/apache/kafka/blob/f57fd2d9fd1c8a240d6c15ad35e83fd9283a958f/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java#L1177  
   
   Is that maybe the bug? Thoughts?



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "kirktrue (via GitHub)" <gi...@apache.org>.
kirktrue commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1884684115


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   AIUI, the idea proposed is to change the set of partitions that are passed to `enablePartitionsAwaitingCallback()` from the newly added partitions (`addedPartitions`) to both the currently assigned partitions _and_ the added partitions (`assignedPartitions`). Is that correct?
   
   If so, it seems like we need to also change the comments for `SubscriptionState.enablePartitionsAwaitingCallback()` to reflect that.



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on PR #18089:
URL: https://github.com/apache/kafka/pull/18089#issuecomment-2525265697

   @brandboat there is another inconsistent behavior between classic consumer and async consumer. see following test:
   ```java
       @ClusterTest(brokers = 3)
       public void testFetchPartitionsAfterFailedListener(ClusterInstance clusterInstance) throws InterruptedException {
           var topic = "topic";
           try (var producer = clusterInstance.producer(Map.of(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class,
                   VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class))) {
               producer.send(new ProducerRecord<>(topic, "key".getBytes(), "value".getBytes()));
           }
           try (var consumer = clusterInstance.consumer(Map.of(ConsumerConfig.GROUP_PROTOCOL_CONFIG, "consumer"))) {
               consumer.subscribe(List.of(topic), new ConsumerRebalanceListener() {
                   private int count = 0;
                   @Override
                   public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
                   }
   
                   @Override
                   public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
                       count++;
                       if (count == 1) throw new IllegalArgumentException("temporary error");
                   }
               });
   
               // classic consumer passes
               // async consumer fails
               TestUtils.waitForCondition(() -> consumer.poll(Duration.ofSeconds(1)).count() == 1,
                       5000,
                       "failed to poll data");
           }
       }
   ```
   
   In the classic consumer, assigned partitions remain fetchable even if the onPartitionsAssigned method throws an exception. In contrast, the async consumer marks those assigned partitions as non-fetchable, preventing us from polling records from them.
   
   As a simple solution, maybe we should set the `pendingOnAssignedCallback` to `false` even though the callback is failed.
   
   ```java
       private CompletableFuture<Void> assignPartitions(
               SortedSet<TopicIdPartition> assignedPartitions,
               SortedSet<TopicPartition> addedPartitions) {
         ...
   
           // Invoke user call back.
           CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
           result.whenComplete((__, exception) -> {
               subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
           });
      }
   ```
   
   @kirktrue @lianetm FYI


-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880531515


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   > Sorry I didn't mean to introduce anything new for recovery
   
   oh, sorry for my bad reading :(
   
   > , just calling enablePartitionsAwaitingCallback passing assigned partitions instead of adding partitions. That call will be harmless if the assigned partitions are already enabled, but it will fix the bug that assigned partitions for which the callback previously failed remain unfetchable, right? Would that work to fix the inconsistency? (agree that here we're trying to make the 2 consumer behave in the same way)
   
   Yes, we can use `assignedPartitions` instead of `addedPartitions` as a guard.



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880847078


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   > Is that correct?
   > Sorry if I'm missing something, thoughts?
   
   There are so many issues we need to dig in :(
   
   > my concern with the change that we currently have in this PR is that it seems to me it's allowing to fetch records even if callbacks always fail, doesn't it? (vs. allow fetch if the callback recovers)
   
   It should get fixed if we adopt your suggestion (https://github.com/apache/kafka/pull/18089#discussion_r1880508552). see following sample code:
   ```java
           result.whenComplete((__, exception) -> {
               if (exception == null) {
                   // Enable newly added partitions to start fetching and updating positions for them.
                   // we use assignedPartitions instead of addedPartitions !!!!!!!!!!!!!!!!!!!!!
                   subscriptions.enablePartitionsAwaitingCallback(assignedPartitions);
               } else {
                   // Keeping newly added partitions as non-fetchable after the callback failure.
                   // They will be retried on the next reconciliation loop, until it succeeds or the
                   // broker removes them from the assignment.
                   if (!addedPartitions.isEmpty()) {
                       log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
                               "requiring initializing positions after onPartitionsAssigned callback failed.",
                           addedPartitions, exception);
                   }
               }
           });
   ```
   
   In short, a partition can become fetchable only if a successful callback occurs subsequently.



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880949459


##########
core/src/test/java/kafka/clients/consumer/ConsumerIntegrationTest.java:
##########
@@ -70,4 +77,52 @@ public void testAsyncConsumerWithOldGroupCoordinator(ClusterInstance clusterInst
             }, "Should get UnsupportedVersionException and how to revert to classic protocol");
         }
     }
+
+    @ClusterTest(serverProperties = {
+        @ClusterConfigProperty(key = "offsets.topic.num.partitions", value = "1"),
+        @ClusterConfigProperty(key = "offsets.topic.replication.factor", value = "1"),
+    })
+    public void testFetchPartitionsAfterFailedListenerWithGroupProtocolClassic(ClusterInstance clusterInstance)
+            throws InterruptedException {
+        testFetchPartitionsAfterFailedListener(clusterInstance, GroupProtocol.CLASSIC);
+    }
+
+    @ClusterTest(serverProperties = {
+        @ClusterConfigProperty(key = "offsets.topic.num.partitions", value = "1"),
+        @ClusterConfigProperty(key = "offsets.topic.replication.factor", value = "1"),
+    })
+    public void testFetchPartitionsAfterFailedListenerWithGroupProtocolConsumer(ClusterInstance clusterInstance)
+            throws InterruptedException {
+        testFetchPartitionsAfterFailedListener(clusterInstance, GroupProtocol.CONSUMER);
+    }
+
+    private static void testFetchPartitionsAfterFailedListener(ClusterInstance clusterInstance, GroupProtocol groupProtocol)
+            throws InterruptedException {
+        var topic = "topic";
+        try (var producer = clusterInstance.producer(Map.of(
+                ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class,
+                ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class))) {
+            producer.send(new ProducerRecord<>(topic, "key".getBytes(), "value".getBytes()));
+        }
+
+        try (var consumer = clusterInstance.consumer(Map.of(
+                ConsumerConfig.GROUP_PROTOCOL_CONFIG, groupProtocol.name()))) {
+            consumer.subscribe(List.of(topic), new ConsumerRebalanceListener() {
+                private int count = 0;
+                @Override
+                public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
+                }
+
+                @Override
+                public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
+                    count++;
+                    if (count == 1) throw new IllegalArgumentException("temporary error");

Review Comment:
   Please add a test case where the callback never succeeds, and ensure that no records can be polled.



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880843295


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   that's true in the app thread, but the risk is that with the new consumer the fetching happens in the background thread, so I wonder if by enabling the partition when the callback failed (and hasn't recovered) we could end up in a weird situation sending a fetch request in the background? It's not conceptually right. 
   
   I don't understand why do we need to enable the partition for fetching if the callback failed and hasn't recovered? Isn't it enough to keep the logic as it was (enable for fetching only when callback succeeds), but with the fix of enabling all assigned partitions? 



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1881981040


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   > that's true in the app thread, but the risk is that with the new consumer the fetching happens in the background thread, so I wonder if by enabling the partition when the callback failed (and hasn't recovered) we could end up in a weird situation sending a fetch request in the background? It's not conceptually right.
   
   Sorry I didn't notice this part, yes it's weird that fetch request still triggered even if callback failed, that is also something never happened in the classic consumer. I should be more careful :(
   
   I'll address comments above and add more tests later, thank you all !



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "kirktrue (via GitHub)" <gi...@apache.org>.
kirktrue commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1884678941


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,10 +1175,17 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
+        // Enable newly added partitions to start fetching and updating positions for them.
         result.whenComplete((__, exception) -> {
             if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
+                // Enable assigned partitions to start fetching and updating positions for them.
+                // We use assignedPartitions here instead of addedPartitions because there's a chance that the callback
+                // might throw an exception, leaving addedPartitions empty. This would result in the poll operation
+                // returning no records, as no topic partitions are marked as fetchable. In contrast, with the classic consumer,
+                // if the first callback fails but the next one succeeds, polling can still retrieve data. To align with
+                // this behavior, we rely on assignedPartitions to avoid such scenarios.
+                subscriptions.enablePartitionsAwaitingCallback(
+                        assignedPartitions.stream().map(TopicIdPartition::topicPartition).collect(Collectors.toSet()));

Review Comment:
   Can this be simplified to:
   
   ```suggestion
                   subscriptions.enablePartitionsAwaitingCallback(toTopicPartitionSet(assignedPartitions));
   ```



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1878537031


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   > I think it may not be triggered because we consider only "added" partitions (and this one is not added in this second round)



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880508552


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   > I agree that the current behavior appears to be a bug, but addressing it may require a KIP (or further discussion) to define the new behavior of the callback in handling exceptions. For now, this PR can focus on fixing the inconsistent behavior.
   
   Sorry I didn't mean to introduce anything new for recovery, just calling `enablePartitionsAwaitingCallback` passing assigned partitions instead of adding partitions. That call will be harmless if the assigned partitions are already enabled, but it will fix the bug that assigned partitions for which the callback previously failed remain unfetchable, right? Would that work to fix the inconsistency? (agree that here we're trying to make the 2 consumer behave in the same way, nothing more)



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880508552


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   > I agree that the current behavior appears to be a bug, but addressing it may require a KIP (or further discussion) to define the new behavior of the callback in handling exceptions. For now, this PR can focus on fixing the inconsistent behavior.
   
   Sorry I didn't mean to introduce anything new for recovery, just calling `enablePartitionsAwaitingCallback` passing assigned partitions instead of adding partitions. That call will be harmless if the assigned partitions are already enabled, but it will fix the bug that assigned partitions for which the callback previously failed remain unfetchable, right? Would that work to fix the inconsistency? (agree that here we're trying to make the 2 consumer behave in the same way)



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1880850350


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   > we could end up in a weird situation sending a fetch request in the background? It's not conceptually right.



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   > we could end up in a weird situation sending a fetch request in the background? It's not conceptually right.



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1878512850


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   Hey Chia, interesting finding, I do believe we have a bug here but not sure if it's the one we're describing:
   1. Regarding 
   > the classic consumer continues to send fetch requests for those partitions even if the listener throws an exception
   
   I may be missing something but I expect that's not the case, because if callbacks fail, the classic detects that when polling the coordinator here and propagates the exception, so it never makes it to the fetching part of the consumer.poll
   https://github.com/apache/kafka/blob/f57fd2d9fd1c8a240d6c15ad35e83fd9283a958f/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L641
   We intentionally introduced the awaitingCallback status for partitions to achieve the same with the new consumer, and do not allow fetch if the onAssigned callback hasn't completed successfully (users may be seeking on those, for instance, so we really shouldn't fetch until its done)
   
   2. The test you shared does reveal an inconsistency, but I would describe the bug as : the new consumer does not recover to fetch after a failed onAssigned callback. (This would explain why on your test the classic returns records and the async does not, recovery). And for that bug, I wonder if the gap is that once the callback fails the first time, the new consumer considers the partition assigned (but awaitingCallback). So on the next reconciliation attempt, where we expect that the callback should be triggered again, I think it may not be triggered because we consider only "added" partitions (and this one is not added in this second round)
   https://github.com/apache/kafka/blob/f57fd2d9fd1c8a240d6c15ad35e83fd9283a958f/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java#L1177  
   
   Is that maybe the bug? Thoughts?



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped, potentially leading to corrupted added partitions [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on PR #18089:
URL: https://github.com/apache/kafka/pull/18089#issuecomment-2524852986

   I discussed this with @brandboat offline. The second issue is expected behavior, as the classic consumer also passes empty partitions. Therefore, we should update the listener's documentation instead of changing its behavior.


-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1883733856


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerMembershipManagerTest.java:
##########
@@ -1920,6 +1935,8 @@ public void testOnPartitionsLostNoError() {
     @Test
     public void testOnPartitionsLostError() {
         testOnPartitionsLost(Optional.of(new KafkaException("Intentional error for test")));
+        testOnPartitionsLost(Optional.of(new WakeupException()));
+        testOnPartitionsLost(Optional.of(new InterruptException("Intentional error for test")));

Review Comment:
   Updated, thank you!



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1883117990


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,18 +1175,20 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
+        // Enable newly added partitions to start fetching and updating positions for them.
         result.whenComplete((__, exception) -> {
             if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
+                // Enable assigned partitions to start fetching and updating positions for them.
+                subscriptions.enablePartitionsAwaitingCallback(
+                        assignedPartitions.stream().map(TopicIdPartition::topicPartition).collect(Collectors.toSet()));
             } else {
                 // Keeping newly added partitions as non-fetchable after the callback failure.
                 // They will be retried on the next reconciliation loop, until it succeeds or the
                 // broker removes them from the assignment.
                 if (!addedPartitions.isEmpty()) {
                     log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
+                                    "requiring initializing positions after onPartitionsAssigned callback failed.",

Review Comment:
   please revert this change



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,18 +1175,20 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
+        // Enable newly added partitions to start fetching and updating positions for them.
         result.whenComplete((__, exception) -> {
             if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
+                // Enable assigned partitions to start fetching and updating positions for them.
+                subscriptions.enablePartitionsAwaitingCallback(
+                        assignedPartitions.stream().map(TopicIdPartition::topicPartition).collect(Collectors.toSet()));

Review Comment:
   please add comments for this change



##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerMembershipManagerTest.java:
##########
@@ -1920,6 +1935,8 @@ public void testOnPartitionsLostNoError() {
     @Test
     public void testOnPartitionsLostError() {
         testOnPartitionsLost(Optional.of(new KafkaException("Intentional error for test")));
+        testOnPartitionsLost(Optional.of(new WakeupException()));
+        testOnPartitionsLost(Optional.of(new InterruptException("Intentional error for test")));

Review Comment:
   ditto



##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerMembershipManagerTest.java:
##########
@@ -1737,14 +1740,20 @@ public void testListenerCallbacksBasic() {
 
     @Test
     public void testListenerCallbacksThrowsErrorOnPartitionsRevoked() {
+        testErrorsOnPartitionsRevoked(Optional.of(new WakeupException()));
+        testErrorsOnPartitionsRevoked(Optional.of(new InterruptException("Intentional onPartitionsRevoked() error")));
+        testErrorsOnPartitionsRevoked(Optional.of(new IllegalArgumentException("Intentional onPartitionsRevoked() error")));
+    }
+
+    private void testErrorsOnPartitionsRevoked(Optional<RuntimeException> error) {

Review Comment:
   we don't need `Optional`, right?



##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerMembershipManagerTest.java:
##########
@@ -1791,14 +1800,20 @@ public void testListenerCallbacksThrowsErrorOnPartitionsRevoked() {
 
     @Test
     public void testListenerCallbacksThrowsErrorOnPartitionsAssigned() {
+        testErrorsOnPartitionsAssigned(Optional.of(new WakeupException()));
+        testErrorsOnPartitionsAssigned(Optional.of(new InterruptException("Intentional error")));
+        testErrorsOnPartitionsAssigned(Optional.of(new IllegalArgumentException("Intentional error")));
+    }
+
+    private void testErrorsOnPartitionsAssigned(Optional<RuntimeException> error) {

Review Comment:
   ditto



-- 
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


Re: [PR] KAFKA-18160: Interrupting or waking up onPartitionsAssigned in AsyncConsumer can cause the ConsumerRebalanceListenerCallbackCompletedEvent to be skipped [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on code in PR #18089:
URL: https://github.com/apache/kafka/pull/18089#discussion_r1884786515


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractMembershipManager.java:
##########
@@ -1175,21 +1175,8 @@ private CompletableFuture<Void> assignPartitions(
 
         // Invoke user call back.
         CompletableFuture<Void> result = signalPartitionsAssigned(addedPartitions);
-        result.whenComplete((__, exception) -> {
-            if (exception == null) {
-                // Enable newly added partitions to start fetching and updating positions for them.
-                subscriptions.enablePartitionsAwaitingCallback(addedPartitions);
-            } else {
-                // Keeping newly added partitions as non-fetchable after the callback failure.
-                // They will be retried on the next reconciliation loop, until it succeeds or the
-                // broker removes them from the assignment.
-                if (!addedPartitions.isEmpty()) {
-                    log.warn("Leaving newly assigned partitions {} marked as non-fetchable and not " +
-                            "requiring initializing positions after onPartitionsAssigned callback failed.",
-                        addedPartitions, exception);
-                }
-            }
-        });
+        // Enable newly added partitions to start fetching and updating positions for them.
+        result.whenComplete((__, exception) -> subscriptions.enablePartitionsAwaitingCallback(addedPartitions));

Review Comment:
   Appreciate for the comment @kirktrue!
   
   > AIUI, the idea proposed is to change the set of partitions that are passed to enablePartitionsAwaitingCallback() from the newly added partitions (addedPartitions) to both the currently assigned partitions and the added partitions (assignedPartitions). Is that correct?
   
   Yes, you are right!
   
   > If so, it seems like we need to also change the comments for SubscriptionState.enablePartitionsAwaitingCallback() to reflect that.
   
   Thanks for the suggestion, will roger that!



-- 
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