dajac commented on code in PR #15970: URL: https://github.com/apache/kafka/pull/15970#discussion_r1608815939
########## group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignmentBuilderTest.java: ########## @@ -53,6 +55,13 @@ public class OptimizedUniformAssignmentBuilderTest { private final String memberB = "B"; private final String memberC = "C"; + private final TopicsImage topicsImage = new MetadataImageBuilder() Review Comment: Do we still need this one? ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/TopicIds.java: ########## @@ -0,0 +1,185 @@ +/* + * 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.consumer; + +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.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> { + private final Set<String> topicNames; + private final TopicsImage image; + + public TopicIds( + Set<String> topicNames, + TopicsImage image + ) { + this.topicNames = Objects.requireNonNull(topicNames); + this.image = Objects.requireNonNull(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 = Objects.requireNonNull(iterator); + this.image = Objects.requireNonNull(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() { + throw new UnsupportedOperationException(); + } + + @Override + public <T> T[] toArray(T[] a) { + throw new UnsupportedOperationException(); + } + + @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) { + for (Object o : c) { + if (o instanceof Uuid) { + Uuid topicId = (Uuid) o; + TopicImage topicImage = image.getTopic(topicId); + if (topicImage == null) return false; + if (!topicNames.contains(topicImage.name())) { + return false; + } + } else { + return false; + } Review Comment: Could we call `contains` instead of duplicating the code? ########## group-coordinator/src/test/java/org/apache/kafka/coordinator/group/consumer/TopicIdsTest.java: ########## @@ -0,0 +1,144 @@ +/* + * 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.consumer; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.coordinator.group.MetadataImageBuilder; +import org.apache.kafka.image.TopicsImage; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import static org.apache.kafka.common.utils.Utils.mkSet; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TopicIdsTest { + + @Test + public void testTopicNamesCannotBeNull() { + assertThrows(NullPointerException.class, () -> new TopicIds(null, TopicsImage.EMPTY)); + } + + @Test + public void testTopicsImageCannotBeNull() { + assertThrows(NullPointerException.class, () -> new TopicIds(Collections.emptySet(), null)); + } + + @Test + public void testSize() { + Set<String> topicNames = mkSet("foo", "bar", "baz"); + Set<Uuid> topicIds = new TopicIds(topicNames, TopicsImage.EMPTY); + assertEquals(topicNames.size(), topicIds.size()); + } + + @Test + public void testIsEmpty() { + Set<String> topicNames = Collections.emptySet(); + Set<Uuid> topicIds = new TopicIds(topicNames, TopicsImage.EMPTY); + assertEquals(topicNames.size(), topicIds.size()); + } + + @Test + public void testContains() { + Uuid fooUuid = Uuid.randomUuid(); + Uuid barUuid = Uuid.randomUuid(); + Uuid bazUuid = Uuid.randomUuid(); + Uuid quxUuid = Uuid.randomUuid(); + TopicsImage topicsImage = new MetadataImageBuilder() + .addTopic(fooUuid, "foo", 3) + .addTopic(barUuid, "bar", 3) + .addTopic(bazUuid, "qux", 3) + .build() + .topics(); + + Set<Uuid> topicIds = new TopicIds(mkSet("foo", "bar", "baz"), topicsImage); + + assertTrue(topicIds.contains(fooUuid)); + assertTrue(topicIds.contains(barUuid)); + assertFalse(topicIds.contains(bazUuid)); + assertFalse(topicIds.contains(quxUuid)); + } + + @Test + public void testContainsAll() { + Uuid fooUuid = Uuid.randomUuid(); + Uuid barUuid = Uuid.randomUuid(); + Uuid bazUuid = Uuid.randomUuid(); + Uuid quxUuid = Uuid.randomUuid(); + TopicsImage topicsImage = new MetadataImageBuilder() + .addTopic(fooUuid, "foo", 3) + .addTopic(barUuid, "bar", 3) + .addTopic(bazUuid, "baz", 3) + .addTopic(quxUuid, "qux", 3) + .build() + .topics(); + + Set<Uuid> topicIds = new TopicIds(mkSet("foo", "bar", "baz", "qux"), topicsImage); + + assertTrue(topicIds.contains(fooUuid)); + assertTrue(topicIds.contains(barUuid)); + assertTrue(topicIds.contains(bazUuid)); + assertTrue(topicIds.contains(quxUuid)); + assertTrue(topicIds.containsAll(mkSet(fooUuid, barUuid, bazUuid, quxUuid))); + } + + @Test + public void testIterator() { Review Comment: Should we also add a case where one of the topic is not resolvable? ########## group-coordinator/src/test/java/org/apache/kafka/coordinator/group/consumer/TopicIdsTest.java: ########## @@ -0,0 +1,144 @@ +/* + * 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.consumer; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.coordinator.group.MetadataImageBuilder; +import org.apache.kafka.image.TopicsImage; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import static org.apache.kafka.common.utils.Utils.mkSet; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TopicIdsTest { + + @Test + public void testTopicNamesCannotBeNull() { + assertThrows(NullPointerException.class, () -> new TopicIds(null, TopicsImage.EMPTY)); + } + + @Test + public void testTopicsImageCannotBeNull() { + assertThrows(NullPointerException.class, () -> new TopicIds(Collections.emptySet(), null)); + } + + @Test + public void testSize() { + Set<String> topicNames = mkSet("foo", "bar", "baz"); + Set<Uuid> topicIds = new TopicIds(topicNames, TopicsImage.EMPTY); + assertEquals(topicNames.size(), topicIds.size()); + } + + @Test + public void testIsEmpty() { + Set<String> topicNames = Collections.emptySet(); + Set<Uuid> topicIds = new TopicIds(topicNames, TopicsImage.EMPTY); + assertEquals(topicNames.size(), topicIds.size()); + } + + @Test + public void testContains() { + Uuid fooUuid = Uuid.randomUuid(); + Uuid barUuid = Uuid.randomUuid(); + Uuid bazUuid = Uuid.randomUuid(); + Uuid quxUuid = Uuid.randomUuid(); + TopicsImage topicsImage = new MetadataImageBuilder() + .addTopic(fooUuid, "foo", 3) + .addTopic(barUuid, "bar", 3) + .addTopic(bazUuid, "qux", 3) + .build() + .topics(); + + Set<Uuid> topicIds = new TopicIds(mkSet("foo", "bar", "baz"), topicsImage); + + assertTrue(topicIds.contains(fooUuid)); + assertTrue(topicIds.contains(barUuid)); + assertFalse(topicIds.contains(bazUuid)); + assertFalse(topicIds.contains(quxUuid)); + } + + @Test + public void testContainsAll() { Review Comment: ditto. -- 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