FrankYang0529 commented on code in PR #19582:
URL: https://github.com/apache/kafka/pull/19582#discussion_r2063882027


##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerAssignmentPoller.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.WakeupException;
+import org.apache.kafka.server.util.ShutdownableThread;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+public class ConsumerAssignmentPoller extends ShutdownableThread {
+    private final Consumer<byte[], byte[]> consumer;
+    private final Set<TopicPartition> partitionsToAssign;
+    private final ConsumerRebalanceListener userRebalanceListener;
+
+    private volatile Throwable thrownException = null;
+    private volatile int receivedMessages = 0;
+
+    private final Set<TopicPartition> partitionAssignment = new HashSet<>();
+    private volatile boolean subscriptionChanged = false;
+    private List<String> topicsSubscription;
+    private final ConsumerRebalanceListener rebalanceListener;
+
+    public ConsumerAssignmentPoller(
+        Consumer<byte[], byte[]> consumer,
+        List<String> topicsToSubscribe
+    ) {
+        this(consumer, topicsToSubscribe, new HashSet<>(), null);
+    }
+
+    public ConsumerAssignmentPoller(
+        Consumer<byte[], byte[]> consumer,
+        Set<TopicPartition> partitionsToAssign
+    ) {
+        this(consumer, new ArrayList<>(), partitionsToAssign, null);

Review Comment:
   ```suggestion
           this(consumer, List.of(), partitionsToAssign, null);
   ```



##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerAssignmentPoller.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.WakeupException;
+import org.apache.kafka.server.util.ShutdownableThread;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+public class ConsumerAssignmentPoller extends ShutdownableThread {
+    private final Consumer<byte[], byte[]> consumer;
+    private final Set<TopicPartition> partitionsToAssign;
+    private final ConsumerRebalanceListener userRebalanceListener;
+
+    private volatile Throwable thrownException = null;
+    private volatile int receivedMessages = 0;
+
+    private final Set<TopicPartition> partitionAssignment = new HashSet<>();
+    private volatile boolean subscriptionChanged = false;
+    private List<String> topicsSubscription;
+    private final ConsumerRebalanceListener rebalanceListener;
+
+    public ConsumerAssignmentPoller(
+        Consumer<byte[], byte[]> consumer,
+        List<String> topicsToSubscribe
+    ) {
+        this(consumer, topicsToSubscribe, new HashSet<>(), null);
+    }
+
+    public ConsumerAssignmentPoller(
+        Consumer<byte[], byte[]> consumer,
+        Set<TopicPartition> partitionsToAssign
+    ) {
+        this(consumer, new ArrayList<>(), partitionsToAssign, null);
+    }
+
+    public ConsumerAssignmentPoller(
+        Consumer<byte[], byte[]> consumer,
+        List<String> topicsToSubscribe,
+        Set<TopicPartition> partitionsToAssign,
+        ConsumerRebalanceListener userRebalanceListener
+    ) {
+        super("daemon-consumer-assignment", false);
+        this.consumer = consumer;
+        this.topicsSubscription = topicsToSubscribe;
+        this.partitionsToAssign = partitionsToAssign;
+        this.userRebalanceListener = userRebalanceListener;
+
+        this.rebalanceListener = new ConsumerRebalanceListener() {
+            @Override
+            public void onPartitionsAssigned(Collection<TopicPartition> 
partitions) {
+                partitionAssignment.addAll(partitions);
+                if (userRebalanceListener != null)
+                    userRebalanceListener.onPartitionsAssigned(partitions);
+            }
+
+            @Override
+            public void onPartitionsRevoked(Collection<TopicPartition> 
partitions) {
+                partitionAssignment.removeAll(partitions);
+                if (userRebalanceListener != null)
+                    userRebalanceListener.onPartitionsRevoked(partitions);
+            }
+        };
+
+        if (partitionsToAssign.isEmpty()) {
+            consumer.subscribe(topicsToSubscribe, rebalanceListener);
+        } else {
+            consumer.assign(new ArrayList<>(partitionsToAssign));
+        }
+    }
+
+    public Set<TopicPartition> consumerAssignment() {
+        return new HashSet<>(partitionAssignment);

Review Comment:
   ```suggestion
           return Set.copyOf(partitionAssignment);
   ```



##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerAssignmentPoller.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.WakeupException;
+import org.apache.kafka.server.util.ShutdownableThread;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+public class ConsumerAssignmentPoller extends ShutdownableThread {
+    private final Consumer<byte[], byte[]> consumer;
+    private final Set<TopicPartition> partitionsToAssign;
+    private final ConsumerRebalanceListener userRebalanceListener;
+
+    private volatile Throwable thrownException = null;
+    private volatile int receivedMessages = 0;
+
+    private final Set<TopicPartition> partitionAssignment = new HashSet<>();
+    private volatile boolean subscriptionChanged = false;
+    private List<String> topicsSubscription;
+    private final ConsumerRebalanceListener rebalanceListener;
+
+    public ConsumerAssignmentPoller(
+        Consumer<byte[], byte[]> consumer,
+        List<String> topicsToSubscribe
+    ) {
+        this(consumer, topicsToSubscribe, new HashSet<>(), null);

Review Comment:
   ```suggestion
           this(consumer, topicsToSubscribe, Set.of(), null);
   ```



-- 
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-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to