cool9850311 commented on code in PR #24199:
URL: https://github.com/apache/kafka/pull/24199#discussion_r4572793146


##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerCallbackTest.java:
##########
@@ -537,6 +537,63 @@ public void onPartitionsRevoked(Collection<TopicPartition> 
partitions, Rebalance
         assertEquals(2, revokedCount.get(), "close() should invoke 
onPartitionsRevoked");
     }
 
+    @ClusterTest
+    public void testClassicConsumerRebalanceListenerRemovedByNull() throws 
InterruptedException {
+        testRebalanceListenerRemovedByNull(CLASSIC);
+    }
+
+    @ClusterTest
+    public void testAsyncConsumerRebalanceListenerRemovedByNull() throws 
InterruptedException {
+        testRebalanceListenerRemovedByNull(CONSUMER);
+    }
+
+    private void testRebalanceListenerRemovedByNull(GroupProtocol 
groupProtocol) throws InterruptedException {
+        var assignedCount = new AtomicInteger(0);
+        var revokedCount = new AtomicInteger(0);
+        try (var consumer = createConsumer(groupProtocol)) {
+            consumer.setRebalanceListener(new RebalanceListener() {
+                @Override
+                public void onPartitionsAssigned(Collection<TopicPartition> 
partitions, RebalanceConsumer rc) {
+                    if (partitions.contains(tp)) {
+                        assignedCount.incrementAndGet();
+                    }
+                }
+
+                @Override
+                public void onPartitionsRevoked(Collection<TopicPartition> 
partitions, RebalanceConsumer rc) {
+                    if (partitions.contains(tp)) {
+                        revokedCount.incrementAndGet();
+                    }
+                }
+            });
+            consumer.subscribe(List.of(topic));
+            ClientsTestUtils.pollUntilTrue(
+                    consumer,
+                    () -> assignedCount.get() == 1,
+                    "Timed out before expected rebalance completed"
+            );
+
+            consumer.setRebalanceListener(null);
+
+            // with a listener set, unsubscribe() invokes onPartitionsRevoked 
without a poll, see
+            // testRebalanceListenerRetainedAfterUnsubscribe
+            consumer.unsubscribe();
+            assertEquals(0, revokedCount.get(), "unsubscribe() should not 
invoke the removed listener");
+
+            // the partition coming back proves the rebalance really happened, 
yet no callback ran
+            consumer.subscribe(List.of(topic));
+            ClientsTestUtils.pollUntilTrue(
+                    consumer,
+                    () -> consumer.assignment().contains(tp),
+                    "Timed out before the partition was assigned again"
+            );
+            assertEquals(1, assignedCount.get(), "re-subscribing should not 
invoke the removed listener");

Review Comment:
   Done, and this one caught a real gap, so thank you. The test now produces 10 
records and consumes all of them after re-subscribing with no listener.
   
   To check it buys something, I broke the no-listener fast path in 
`AsyncKafkaConsumer#process(PartitionsAssignedEvent)` so the event's future is 
never completed. The previous version of the test passed; this one fails.
   



##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerCallbackTest.java:
##########
@@ -537,6 +537,63 @@ public void onPartitionsRevoked(Collection<TopicPartition> 
partitions, Rebalance
         assertEquals(2, revokedCount.get(), "close() should invoke 
onPartitionsRevoked");
     }
 
+    @ClusterTest
+    public void testClassicConsumerRebalanceListenerRemovedByNull() throws 
InterruptedException {
+        testRebalanceListenerRemovedByNull(CLASSIC);
+    }
+
+    @ClusterTest
+    public void testAsyncConsumerRebalanceListenerRemovedByNull() throws 
InterruptedException {
+        testRebalanceListenerRemovedByNull(CONSUMER);
+    }
+
+    private void testRebalanceListenerRemovedByNull(GroupProtocol 
groupProtocol) throws InterruptedException {
+        var assignedCount = new AtomicInteger(0);
+        var revokedCount = new AtomicInteger(0);
+        try (var consumer = createConsumer(groupProtocol)) {
+            consumer.setRebalanceListener(new RebalanceListener() {
+                @Override
+                public void onPartitionsAssigned(Collection<TopicPartition> 
partitions, RebalanceConsumer rc) {
+                    if (partitions.contains(tp)) {
+                        assignedCount.incrementAndGet();
+                    }
+                }
+
+                @Override
+                public void onPartitionsRevoked(Collection<TopicPartition> 
partitions, RebalanceConsumer rc) {
+                    if (partitions.contains(tp)) {
+                        revokedCount.incrementAndGet();
+                    }
+                }
+            });
+            consumer.subscribe(List.of(topic));
+            ClientsTestUtils.pollUntilTrue(
+                    consumer,
+                    () -> assignedCount.get() == 1,
+                    "Timed out before expected rebalance completed"
+            );
+
+            consumer.setRebalanceListener(null);
+
+            // with a listener set, unsubscribe() invokes onPartitionsRevoked 
without a poll, see
+            // testRebalanceListenerRetainedAfterUnsubscribe
+            consumer.unsubscribe();
+            assertEquals(0, revokedCount.get(), "unsubscribe() should not 
invoke the removed listener");
+
+            // the partition coming back proves the rebalance really happened, 
yet no callback ran
+            consumer.subscribe(List.of(topic));
+            ClientsTestUtils.pollUntilTrue(
+                    consumer,
+                    () -> consumer.assignment().contains(tp),
+                    "Timed out before the partition was assigned again"
+            );
+            assertEquals(1, assignedCount.get(), "re-subscribing should not 
invoke the removed listener");
+        }
+        // close() gives up the assignment, again without invoking the removed 
listener

Review Comment:
   Done. The consumer is closed in a `finally` block and the test asserts 
`assignment()` throws `IllegalStateException` before asserting that no further 
callbacks ran. Same idea applied to 
`KafkaConsumerTest#testCloseAfterNullRebalanceListenerSkipsCallbacks`.
   



##########
clients/src/test/java/org/apache/kafka/clients/consumer/KafkaConsumerTest.java:
##########
@@ -3305,6 +3305,18 @@ public void 
onPartitionsAssigned(Collection<TopicPartition> partitions, Rebalanc
         };
     }
 
+    private MockRebalanceListener getCountingRebalanceListener() {
+        return new MockRebalanceListener() {

Review Comment:
   Done, see the reply above -- it is now `private static class 
CountingSeekToZeroRebalanceListener extends MockRebalanceListener`.
   
   I left the two pre-existing factories (`getRebalanceListener`, 
`getExceptionRebalanceListener`) alone to keep this PR to its subject. Happy to 
convert them here if you would rather have the file consistent in one go.
   



##########
clients/src/test/java/org/apache/kafka/clients/consumer/KafkaConsumerTest.java:
##########
@@ -3909,6 +3921,97 @@ public void 
testEnforceRebalanceTriggersRebalanceOnNextPoll(GroupProtocol groupP
         assertEquals(1, countingRebalanceListener.revokedCount);
     }
 
+    // NOTE: this test uses the enforceRebalance API which is not implemented 
in the CONSUMER group protocol.
+    @ParameterizedTest
+    @EnumSource(value = GroupProtocol.class, names = "CLASSIC")
+    public void 
testSetRebalanceListenerNullSkipsCallbacksOnRebalance(GroupProtocol 
groupProtocol) {
+        Time time = new MockTime(1L);
+        ConsumerMetadata metadata = createMetadata(subscription);
+        MockClient client = new MockClient(time, metadata);
+        consumer = newConsumer(groupProtocol, time, client, subscription, 
metadata, assignor, false, groupInstanceId);
+        MockRebalanceListener countingRebalanceListener = 
getCountingRebalanceListener();
+        initMetadata(client, Map.of(topic, 1, topic2, 1));
+
+        consumer.setRebalanceListener(countingRebalanceListener);
+        consumer.subscribe(Arrays.asList(topic, topic2));
+        Node node = metadata.fetch().nodes().get(0);
+        Node coordinator = prepareRebalance(client, node, assignor, 
Arrays.asList(tp0, t2p0), null);
+
+        // two poll calls, since the first rebalance needs two round trips to 
finish join / sync-group
+        consumer.poll(Duration.ZERO);
+        consumer.poll(Duration.ZERO);
+
+        assertEquals(Set.of(tp0, t2p0), consumer.assignment());
+        assertEquals(1, countingRebalanceListener.assignedCount);
+        assertEquals(0, countingRebalanceListener.revokedCount);
+
+        // removing the listener stops the callbacks, not the rebalance itself
+        consumer.setRebalanceListener(null);
+        prepareRebalance(client, node, assignor, List.of(tp0), coordinator);
+        consumer.enforceRebalance();
+        consumer.poll(Duration.ZERO);
+        consumer.poll(Duration.ZERO);
+
+        // the narrowed assignment proves the second rebalance really completed
+        assertEquals(Set.of(tp0), consumer.assignment());
+        assertEquals(1, countingRebalanceListener.assignedCount);
+        assertEquals(0, countingRebalanceListener.revokedCount);
+    }
+
+    // NOTE: the rebalance flow in prepareRebalance is specific to the CLASSIC 
consumer.
+    @ParameterizedTest
+    @EnumSource(value = GroupProtocol.class, names = "CLASSIC")

Review Comment:
   Same as above.
   



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to