jeffkbkim commented on code in PR #17750:
URL: https://github.com/apache/kafka/pull/17750#discussion_r1838650823


##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupMetadataManagerTest.java:
##########
@@ -14979,6 +14980,83 @@ public void 
testReplayConsumerGroupRegularExpressionTombstone() {
         );
     }
 
+    @Test
+    public void 
testConsumerGroupMemberPicksUpExistingResolvedRegularExpression() {
+        String groupId = "fooup";
+        String memberId1 = Uuid.randomUuid().toString();
+        String memberId2 = Uuid.randomUuid().toString();
+
+        Uuid fooTopicId = Uuid.randomUuid();
+        String fooTopicName = "foo";
+
+        ConsumerGroupPartitionAssignor assignor = 
mock(ConsumerGroupPartitionAssignor.class);
+        when(assignor.name()).thenReturn("range");
+        when(assignor.assign(any(), any())).thenAnswer(answer -> {
+            GroupSpec spec = answer.getArgument(0);
+
+            List.of(memberId1, memberId2).forEach(memberId ->
+                assertEquals(
+                    Collections.singleton(fooTopicId),
+                    spec.memberSubscription(memberId).subscribedTopicIds(),
+                    String.format("Member %s has unexpected subscribed topic 
ids", memberId)
+                )

Review Comment:
   to understand, this assertion is the main check - if the coordinator did not 
pick up the existing regex, it would have created a TopicIds with empty 
subscribed topic names. is this correct?



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/modern/UnionSet.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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.coordinator.group.modern;
+
+import java.lang.reflect.Array;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.Set;
+
+public class UnionSet<T> implements Set<T> {
+    private final Set<T> largeSet;
+    private final Set<T> smallSet;
+    private int size = -1;
+
+    public UnionSet(Set<T> s1, Set<T> s2) {
+        Objects.requireNonNull(s1);
+        Objects.requireNonNull(s2);
+
+        if (s1.size() > s2.size()) {
+            largeSet = s1;
+            smallSet = s2;
+        } else {
+            largeSet = s2;
+            smallSet = s1;
+        }
+    }
+
+    @Override
+    public int size() {

Review Comment:
   if either of the two sets are modified, this would break size(), right? my 
understanding is that we don't want to convert them to immutable sets as that 
loses the purpose of this class



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/modern/TargetAssignmentBuilder.java:
##########
@@ -89,6 +92,144 @@ public Map<String, MemberAssignment> targetAssignment() {
         }
     }
 
+    public static class ConsumerTargetAssignmentBuilder extends 
TargetAssignmentBuilder<ConsumerGroupMember, ConsumerTargetAssignmentBuilder> {
+
+        /**
+         * The resolved regular expressions.
+         */
+        private Map<String, ResolvedRegularExpression> 
resolvedRegularExpressions = Collections.emptyMap();
+
+        public ConsumerTargetAssignmentBuilder(
+            String groupId,
+            int groupEpoch,
+            PartitionAssignor assignor
+        ) {
+            super(groupId, groupEpoch, assignor);
+        }
+
+        /**
+         * Adds all the existing resolved regular expressions.
+         *
+         * @param resolvedRegularExpressions The resolved regular expressions.
+         * @return This object.
+         */
+        public ConsumerTargetAssignmentBuilder withResolvedRegularExpressions(
+            Map<String, ResolvedRegularExpression> resolvedRegularExpressions
+        ) {
+            this.resolvedRegularExpressions = resolvedRegularExpressions;
+            return self();
+        }
+
+        @Override
+        protected ConsumerTargetAssignmentBuilder self() {
+            return this;
+        }
+
+        @Override
+        protected CoordinatorRecord newTargetAssignmentRecord(
+            String groupId,
+            String memberId,
+            Map<Uuid, Set<Integer>> partitions
+        ) {
+            return 
GroupCoordinatorRecordHelpers.newConsumerGroupTargetAssignmentRecord(
+                groupId,
+                memberId,
+                partitions
+            );
+        }
+
+        @Override
+        protected CoordinatorRecord newTargetAssignmentEpochRecord(String 
groupId, int assignmentEpoch) {
+            return 
GroupCoordinatorRecordHelpers.newConsumerGroupTargetAssignmentEpochRecord(
+                groupId,
+                assignmentEpoch
+            );
+        }
+
+        @Override
+        protected MemberSubscriptionAndAssignmentImpl 
newMemberSubscriptionAndAssignment(
+            ConsumerGroupMember member,
+            Assignment memberAssignment,
+            TopicIds.TopicResolver topicResolver
+        ) {
+            Set<String> subscriptions = member.subscribedTopicNames();
+
+            // Check whether the member is also subscribed to a regular 
expression. If it is,
+            // create the union of the two subscriptions.
+            String subscribedTopicRegex = member.subscribedTopicRegex();
+            if (subscribedTopicRegex != null && 
!subscribedTopicRegex.isEmpty()) {
+                ResolvedRegularExpression resolvedRegularExpression = 
resolvedRegularExpressions.get(subscribedTopicRegex);
+                if (resolvedRegularExpression != null) {
+                    if (subscriptions.isEmpty()) {
+                        subscriptions = resolvedRegularExpression.topics;
+                    } else if (!resolvedRegularExpression.topics.isEmpty()) {
+                        // We only use a UnionSet when the member uses both 
type of subscriptions. The
+                        // protocol allows it. However, the Apache Kafka 
Consumer does not support it.
+                        // Other clients such as librdkafka may support it.

Review Comment:
   Are we planning to support it in the future for the java consumer?



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