dajac commented on code in PR #15970:
URL: https://github.com/apache/kafka/pull/15970#discussion_r1607805094


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/AssignmentMemberSpec.java:
##########
@@ -65,7 +64,7 @@ public Optional<String> rackId() {
     /**
      * @return Collection of subscribed topic Ids.

Review Comment:
   nit: Should we change `Collection` to `Set`?



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignmentBuilderTest.java:
##########
@@ -72,7 +101,7 @@ public void testOneMemberNoTopicSubscription() {
             new AssignmentMemberSpec(
                 Optional.empty(),
                 Optional.empty(),
-                Collections.emptyList(),
+                TopicIds.EMPTY,

Review Comment:
   ditto.



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/TopicIds.java:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.assignor;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.image.TopicImage;
+import org.apache.kafka.image.TopicsImage;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * TopicIds is initialized with topic names (String) but exposes a Set of 
topic ids (Uuid) to the
+ * user and performs the conversion lazily with TopicsImage.
+ */
+public class TopicIds implements Set<Uuid> {
+
+    public static final TopicIds EMPTY = new TopicIds(Collections.emptySet(), 
TopicsImage.EMPTY);
+    private final Set<String> topicNames;
+    private final TopicsImage image;
+
+    public TopicIds(
+        Set<String> topicNames,
+        TopicsImage image
+    ) {
+        this.topicNames = topicNames;
+        this.image = image;

Review Comment:
   nit: Should we require them to be non-null?



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/RangeAssignorTest.java:
##########
@@ -69,7 +98,7 @@ public void testOneConsumerNoTopic() {
             new AssignmentMemberSpec(
                 Optional.empty(),
                 Optional.empty(),
-                Collections.emptyList(),
+                TopicIds.EMPTY,

Review Comment:
   ditto.



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/GeneralUniformAssignmentBuilderTest.java:
##########
@@ -69,13 +103,13 @@ public void testTwoMembersNoTopicSubscription() {
         members.put(memberA, new AssignmentMemberSpec(
             Optional.empty(),
             Optional.empty(),
-            Collections.emptyList(),
+            TopicIds.EMPTY,

Review Comment:
   Should we revert all the changes in this file? I don't think that there are 
necessary as the assignor requires a Set.



##########
jmh-benchmarks/src/main/java/org/apache/kafka/jmh/assignor/TargetAssignmentBuilderBenchmark.java:
##########
@@ -123,13 +130,14 @@ private Map<String, ConsumerGroupMember> 
generateMockMembers() {
 
     private Map<String, TopicMetadata> generateMockSubscriptionMetadata() {
         Map<String, TopicMetadata> subscriptionMetadata = new HashMap<>();
+        ImmutableMap<Uuid, TopicImage> topicsById = ImmutableMap.empty();
+        ImmutableMap<String, TopicImage> topicsByName = ImmutableMap.empty();

Review Comment:
   ditto.



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/TopicIds.java:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.assignor;

Review Comment:
   nit: I wonder if we should rather put it into `...group.consumer` package as 
it is more related to the target assignment builder than the assignors. What do 
you think?



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/TopicIds.java:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.assignor;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.image.TopicImage;
+import org.apache.kafka.image.TopicsImage;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * TopicIds is initialized with topic names (String) but exposes a Set of 
topic ids (Uuid) to the
+ * user and performs the conversion lazily with TopicsImage.
+ */
+public class TopicIds implements Set<Uuid> {
+
+    public static final TopicIds EMPTY = new TopicIds(Collections.emptySet(), 
TopicsImage.EMPTY);
+    private final Set<String> topicNames;
+    private final TopicsImage image;
+
+    public TopicIds(
+        Set<String> topicNames,
+        TopicsImage image
+    ) {
+        this.topicNames = topicNames;
+        this.image = image;
+    }
+
+    @Override
+    public int size() {
+        return topicNames.size();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return topicNames.isEmpty();
+    }
+
+    @Override
+    public boolean contains(Object o) {
+        if (o instanceof Uuid) {
+            Uuid topicId = (Uuid) o;
+            TopicImage topicImage = image.getTopic(topicId);
+            if (topicImage == null) return false;
+            return topicNames.contains(topicImage.name());
+        }
+        return false;
+    }
+
+    private static class TopicIdIterator implements Iterator<Uuid> {
+        final Iterator<String> iterator;
+        final TopicsImage image;
+        private Uuid next = null;
+
+        private TopicIdIterator(
+            Iterator<String> iterator,
+            TopicsImage image
+        ) {
+            this.iterator = iterator;
+            this.image = image;
+        }
+
+        @Override
+        public boolean hasNext() {
+            if (next != null) return true;
+            Uuid result = null;
+            do {
+                if (!iterator.hasNext()) {
+                    return false;
+                }
+                String next = iterator.next();
+                TopicImage topicImage = image.getTopic(next);
+                if (topicImage != null) {
+                    result = topicImage.id();
+                }
+            } while (result == null);
+            next = result;
+            return true;
+        }
+
+        @Override
+        public Uuid next() {
+            if (!hasNext()) throw new NoSuchElementException();
+            Uuid result = next;
+            next = null;
+            return result;
+        }
+    }
+
+    @Override
+    public Iterator<Uuid> iterator() {
+        return new TopicIdIterator(topicNames.iterator(), image);
+    }
+
+    @Override
+    public Object[] toArray() {
+        return new Object[0];
+    }
+
+    @Override
+    public <T> T[] toArray(T[] a) {
+        return null;

Review Comment:
   ditto.



##########
jmh-benchmarks/src/main/java/org/apache/kafka/jmh/assignor/ServerSideAssignorBenchmark.java:
##########
@@ -119,7 +123,13 @@ public enum AssignmentType {
 
     private SubscribedTopicDescriber subscribedTopicDescriber;
 
-    private final List<Uuid> allTopicIds = new ArrayList<>();
+    private final List<String> allTopicNames = new ArrayList<>();
+
+    private ImmutableMap<Uuid, TopicImage> topicsById = ImmutableMap.empty();
+
+    private ImmutableMap<String, TopicImage> topicsByName = 
ImmutableMap.empty();
+
+    private TopicsImage topicsImage = TopicsImage.EMPTY;

Review Comment:
   ditto.



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/TopicIds.java:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.assignor;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.image.TopicImage;
+import org.apache.kafka.image.TopicsImage;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * TopicIds is initialized with topic names (String) but exposes a Set of 
topic ids (Uuid) to the
+ * user and performs the conversion lazily with TopicsImage.
+ */
+public class TopicIds implements Set<Uuid> {
+
+    public static final TopicIds EMPTY = new TopicIds(Collections.emptySet(), 
TopicsImage.EMPTY);
+    private final Set<String> topicNames;
+    private final TopicsImage image;
+
+    public TopicIds(
+        Set<String> topicNames,
+        TopicsImage image
+    ) {
+        this.topicNames = topicNames;
+        this.image = image;
+    }
+
+    @Override
+    public int size() {
+        return topicNames.size();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return topicNames.isEmpty();
+    }
+
+    @Override
+    public boolean contains(Object o) {
+        if (o instanceof Uuid) {
+            Uuid topicId = (Uuid) o;
+            TopicImage topicImage = image.getTopic(topicId);
+            if (topicImage == null) return false;
+            return topicNames.contains(topicImage.name());
+        }
+        return false;
+    }
+
+    private static class TopicIdIterator implements Iterator<Uuid> {
+        final Iterator<String> iterator;
+        final TopicsImage image;
+        private Uuid next = null;
+
+        private TopicIdIterator(
+            Iterator<String> iterator,
+            TopicsImage image
+        ) {
+            this.iterator = iterator;
+            this.image = image;
+        }
+
+        @Override
+        public boolean hasNext() {
+            if (next != null) return true;
+            Uuid result = null;
+            do {
+                if (!iterator.hasNext()) {
+                    return false;
+                }
+                String next = iterator.next();
+                TopicImage topicImage = image.getTopic(next);
+                if (topicImage != null) {
+                    result = topicImage.id();
+                }
+            } while (result == null);
+            next = result;
+            return true;
+        }
+
+        @Override
+        public Uuid next() {
+            if (!hasNext()) throw new NoSuchElementException();
+            Uuid result = next;
+            next = null;
+            return result;
+        }
+    }
+
+    @Override
+    public Iterator<Uuid> iterator() {
+        return new TopicIdIterator(topicNames.iterator(), image);
+    }
+
+    @Override
+    public Object[] toArray() {
+        return new Object[0];
+    }
+
+    @Override
+    public <T> T[] toArray(T[] a) {
+        return null;
+    }
+
+    @Override
+    public boolean add(Uuid o) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public boolean remove(Object o) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public boolean addAll(Collection c) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void clear() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public boolean removeAll(Collection c) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public boolean retainAll(Collection c) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public boolean containsAll(Collection c) {
+        return false;

Review Comment:
   We should implement this one.



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/TopicIds.java:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.assignor;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.image.TopicImage;
+import org.apache.kafka.image.TopicsImage;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * TopicIds is initialized with topic names (String) but exposes a Set of 
topic ids (Uuid) to the
+ * user and performs the conversion lazily with TopicsImage.
+ */
+public class TopicIds implements Set<Uuid> {
+
+    public static final TopicIds EMPTY = new TopicIds(Collections.emptySet(), 
TopicsImage.EMPTY);
+    private final Set<String> topicNames;
+    private final TopicsImage image;
+
+    public TopicIds(
+        Set<String> topicNames,
+        TopicsImage image
+    ) {
+        this.topicNames = topicNames;
+        this.image = image;
+    }
+
+    @Override
+    public int size() {
+        return topicNames.size();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return topicNames.isEmpty();
+    }
+
+    @Override
+    public boolean contains(Object o) {
+        if (o instanceof Uuid) {
+            Uuid topicId = (Uuid) o;
+            TopicImage topicImage = image.getTopic(topicId);
+            if (topicImage == null) return false;
+            return topicNames.contains(topicImage.name());
+        }
+        return false;
+    }
+
+    private static class TopicIdIterator implements Iterator<Uuid> {
+        final Iterator<String> iterator;
+        final TopicsImage image;
+        private Uuid next = null;
+
+        private TopicIdIterator(
+            Iterator<String> iterator,
+            TopicsImage image
+        ) {
+            this.iterator = iterator;
+            this.image = image;
+        }
+
+        @Override
+        public boolean hasNext() {
+            if (next != null) return true;
+            Uuid result = null;
+            do {
+                if (!iterator.hasNext()) {
+                    return false;
+                }
+                String next = iterator.next();
+                TopicImage topicImage = image.getTopic(next);
+                if (topicImage != null) {
+                    result = topicImage.id();
+                }
+            } while (result == null);
+            next = result;
+            return true;
+        }
+
+        @Override
+        public Uuid next() {
+            if (!hasNext()) throw new NoSuchElementException();
+            Uuid result = next;
+            next = null;
+            return result;
+        }
+    }
+
+    @Override
+    public Iterator<Uuid> iterator() {
+        return new TopicIdIterator(topicNames.iterator(), image);
+    }
+
+    @Override
+    public Object[] toArray() {
+        return new Object[0];

Review Comment:
   Should we implement this one? Otherwise, we should throw 
`UnsupportedOperationException`.



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/consumer/TargetAssignmentBuilderTest.java:
##########
@@ -61,6 +71,9 @@ public static class TargetAssignmentBuilderTestContext {
         private final Map<String, Assignment> targetAssignment = new 
HashMap<>();
         private final Map<String, MemberAssignment> memberAssignments = new 
HashMap<>();
         private final Map<String, String> staticMembers = new HashMap<>();
+        private ImmutableMap<Uuid, TopicImage> topicsById = 
ImmutableMap.empty();
+        private ImmutableMap<String, TopicImage> topicsByName = 
ImmutableMap.empty();
+        private TopicsImage topicsImage = TopicsImage.EMPTY;

Review Comment:
   We have `MetadataImageBuilder` which is used by other tests. I wonder if we 
could reuse it here too. Have you considered it?



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/consumer/TargetAssignmentBuilderTest.java:
##########
@@ -242,20 +264,29 @@ public TargetAssignmentBuilder.TargetAssignmentResult 
build() {
     public void testCreateAssignmentMemberSpec() {
         Uuid fooTopicId = Uuid.randomUuid();
         Uuid barTopicId = Uuid.randomUuid();
+        TopicImage topic1Image = newTopicImage(

Review Comment:
   ditto.



##########
jmh-benchmarks/src/main/java/org/apache/kafka/jmh/assignor/ServerSideAssignorBenchmark.java:
##########
@@ -208,7 +225,7 @@ private Optional<String> rackId(int memberIndex) {
     private void addMemberSpec(
         Map<String, AssignmentMemberSpec> members,
         int memberIndex,
-        Set<Uuid> subscribedTopicIds
+        TopicIds subscribedTopicIds

Review Comment:
   nit: We could keep Set<Uuid> here.



##########
jmh-benchmarks/src/main/java/org/apache/kafka/jmh/assignor/TargetAssignmentBuilderBenchmark.java:
##########
@@ -62,7 +68,7 @@
 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 public class TargetAssignmentBuilderBenchmark {
 
-    @Param({"100", "500", "1000", "5000", "10000"})
+    @Param({"100", "500", "1000", "10000"})

Review Comment:
   Let's revert this change.



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