adikou commented on code in PR #24199:
URL: https://github.com/apache/kafka/pull/24199#discussion_r4552204457
##########
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");
Review Comment:
We're asserting a negative action which only makes sense from a positive
control action: verify that revoked was actually invoked on a previous
unsubscribe. Then asset size 1, then unsubscribe, then assert 0
##########
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:
Same here: we need to assert on another external state that the side effect
of rebalance actually took place, so : send a few records to the partition and
ensure you can consume them all while not triggering any rebalance callback.
##########
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.
Review Comment:
Then, why parameterize? Just hardcode CLASSIC?
##########
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:
Similar, just hardcode CLASSIC?
##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerRebalanceListenerInvokerTest.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer.internals;
+
+import
org.apache.kafka.clients.consumer.internals.metrics.RebalanceCallbackMetricsManager;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.utils.MockTime;
+import org.apache.kafka.common.utils.internals.LogContext;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
+
+import java.util.HashSet;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests that the invoker skips the dispatch entirely when no rebalance
listener is registered,
+ * which is the case after {@link
org.apache.kafka.clients.consumer.Consumer#setRebalanceListener(
+ * org.apache.kafka.clients.consumer.RebalanceListener)} is passed null. The
subscription state is
+ * mocked rather than real on purpose: a real one silently drops the callbacks
itself, so it would
+ * hide whether the invoker checked at all.
+ */
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.STRICT_STUBS)
+public class ConsumerRebalanceListenerInvokerTest {
+
+ private final SortedSet<TopicPartition> partitions = new TreeSet<>(new
Utils.TopicPartitionComparator());
+
+ @Mock
+ private ConsumerSubscriptionState subscriptions;
+
+ @Mock
+ private RebalanceCallbackMetricsManager metricsManager;
+
+ private ConsumerRebalanceListenerInvoker invoker;
+
+ @BeforeEach
+ public void setup() {
+ partitions.add(new TopicPartition("topic", 0));
+ invoker = new ConsumerRebalanceListenerInvoker(
+ new LogContext(), subscriptions, new MockTime(), metricsManager);
+ }
+
+ @Test
+ public void
testInvokePartitionsAssignedWithoutRebalanceListenerSkipsDispatch() {
+ when(subscriptions.hasRebalanceListener()).thenReturn(false);
+
+ assertNull(invoker.invokePartitionsAssigned(partitions));
+
+ verify(subscriptions, never()).onPartitionsAssigned(any());
+ verifyNoInteractions(metricsManager);
+ }
+
+ @Test
+ public void
testInvokePartitionsRevokedWithoutRebalanceListenerSkipsDispatch() {
+ when(subscriptions.hasRebalanceListener()).thenReturn(false);
+ when(subscriptions.pausedPartitions()).thenReturn(new HashSet<>());
+
+ assertNull(invoker.invokePartitionsRevoked(partitions));
+
+ verify(subscriptions, never()).onPartitionsRevoked(any());
+ verifyNoInteractions(metricsManager);
+ }
+
+ @Test
+ public void
testInvokePartitionsLostWithoutRebalanceListenerSkipsDispatch() {
+ when(subscriptions.hasRebalanceListener()).thenReturn(false);
+ when(subscriptions.pausedPartitions()).thenReturn(new HashSet<>());
+
+ assertNull(invoker.invokePartitionsLost(partitions));
+
+ verify(subscriptions, never()).onPartitionsLost(any());
+ verifyNoInteractions(metricsManager);
+ }
+
+ @Test
+ public void testInvokeCallbacksWithRebalanceListenerDispatches() {
Review Comment:
RebalanceInvoker also catches exceptions thrown from the rebalance listener
via SubscriptionState. We need assertions on these as well
##########
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:
Why not keep a static class in this test?
##########
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() {
Review Comment:
Method name seems to mismatch with what we're actually doing.
##########
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);
+
Review Comment:
Ah, so the super is a counting rebalance listener.
Since you're using the new API to seek offsets, assert that
consumer#offsetsForPartitions point to 0? And assign different offsets after
setRebalanceListener(null) and verify that offsets are untouched
##########
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:
+here. Assert that the consumer is actually closed. And then assert on no
more callbacks
--
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]