You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "ruslankrivoshein (via GitHub)" <gi...@apache.org> on 2023/06/04 18:35:23 UTC

[GitHub] [kafka] ruslankrivoshein commented on a diff in pull request #13562: KAFKA-14581: Moving GetOffsetShell to tools

ruslankrivoshein commented on code in PR #13562:
URL: https://github.com/apache/kafka/pull/13562#discussion_r1216997701


##########
tools/src/test/java/org/apache/kafka/tools/GetOffsetShellTest.java:
##########
@@ -0,0 +1,376 @@
+/*
+ * 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.tools;
+
+import kafka.test.ClusterInstance;
+import kafka.test.annotation.ClusterTest;
+import kafka.test.annotation.ClusterTestDefaults;
+import kafka.test.annotation.Type;
+import kafka.test.junit.ClusterTestExtensions;
+import org.apache.kafka.clients.CommonClientConfigs;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.NewTopic;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerConfig;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.serialization.StringSerializer;
+import org.apache.kafka.common.utils.Exit;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.Properties;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@ExtendWith(value = ClusterTestExtensions.class)
+@ClusterTestDefaults(clusterType = Type.ZK)
+@Tag("integration")
+public class GetOffsetShellTest {
+    private final int topicCount = 4;
+    private final int offsetTopicPartitionCount = 4;
+    private final ClusterInstance cluster;
+
+    public GetOffsetShellTest(ClusterInstance cluster) {
+        this.cluster = cluster;
+    }
+
+    private String getTopicName(int i) {
+        return "topic" + i;
+    }
+
+    public void setUp() {
+        cluster.config().serverProperties().put("auto.create.topics.enable", false);
+        cluster.config().serverProperties().put("offsets.topic.replication.factor", "1");
+        cluster.config().serverProperties().put("offsets.topic.num.partitions", String.valueOf(offsetTopicPartitionCount));
+
+        try (Admin admin = Admin.create(cluster.config().adminClientProperties())) {
+            List<NewTopic> topics = new ArrayList<>();
+
+            IntStream.range(0, topicCount + 1).forEach(i -> topics.add(new NewTopic(getTopicName(i), i, (short) 1)));
+
+            admin.createTopics(topics);
+        }
+
+        Properties props = new Properties();
+        props.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, cluster.config().producerProperties().get("bootstrap.servers"));
+        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
+        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
+
+        try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) {
+            IntStream.range(0, topicCount + 1)
+                .forEach(i -> IntStream.range(0, i * i)
+                        .forEach(msgCount -> producer.send(
+                                new ProducerRecord<>(getTopicName(i), msgCount % i, null, "val" + msgCount)))
+                );
+        }
+    }
+
+    static class Row {
+        private String name;
+        private int partition;
+        private Long timestamp;
+
+        public Row(String name, int partition, Long timestamp) {
+            this.name = name;
+            this.partition = partition;
+            this.timestamp = timestamp;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (o == this) return true;
+
+            if (!(o instanceof Row)) return false;
+
+            Row r = (Row) o;
+
+            return name.equals(r.name) && partition == r.partition && Objects.equals(timestamp, r.timestamp);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(name, partition, timestamp);
+        }
+    }
+
+    @ClusterTest
+    public void testNoFilterOptions() {
+        setUp();
+
+        List<Row> output = executeAndParse();
+
+        assertEquals(expectedOffsetsWithInternal(), output);
+    }
+
+    @ClusterTest
+    public void testInternalExcluded() {
+        setUp();
+
+        List<Row> output = executeAndParse("--exclude-internal-topics");
+
+        assertEquals(expectedTestTopicOffsets(), output);
+    }
+
+    @ClusterTest
+    public void testTopicNameArg() {
+        setUp();
+
+        IntStream.range(1, topicCount + 1).forEach(i -> {
+            List<Row> offsets = executeAndParse("--topic", getTopicName(i));
+
+            assertEquals(expectedOffsetsForTopic(i), offsets, () -> "Offset output did not match for " + getTopicName(i));
+        });
+    }
+
+    @ClusterTest
+    public void testTopicPatternArg() {
+        setUp();
+
+        List<Row> offsets = executeAndParse("--topic", "topic.*");
+
+        assertEquals(expectedTestTopicOffsets(), offsets);
+    }
+
+    @ClusterTest
+    public void testPartitionsArg() {
+        setUp();
+
+        List<Row> offsets = executeAndParse("--partitions", "0,1");
+
+        assertEquals(expectedOffsetsWithInternal().stream().filter(r -> r.partition <= 1).collect(Collectors.toList()), offsets);
+    }
+
+    @ClusterTest
+    public void testTopicPatternArgWithPartitionsArg() {
+        setUp();
+
+        List<Row> offsets = executeAndParse("--topic", "topic.*", "--partitions", "0,1");
+
+        assertEquals(expectedTestTopicOffsets().stream().filter(r -> r.partition <= 1).collect(Collectors.toList()), offsets);
+    }
+
+    @ClusterTest
+    public void testTopicPartitionsArg() {
+        setUp();
+
+        List<Row> offsets = executeAndParse("--topic-partitions", "topic1:0,topic2:1,topic(3|4):2,__.*:3");
+        List<Row> expected = Arrays.asList(
+                new Row("__consumer_offsets", 3, 0L),
+                new Row("topic1", 0, 1L),
+                new Row("topic2", 1, 2L),
+                new Row("topic3", 2, 3L),
+                new Row("topic4", 2, 4L)
+        );
+
+        assertEquals(expected, offsets);
+    }
+
+    @ClusterTest
+    public void testGetLatestOffsets() {
+        setUp();
+
+        for (String time : new String[] {"-1", "latest"}) {

Review Comment:
   I also didn't find a way how to use `ParametrizedTest` and `ValueSource` in the same place with `ClusterTest`. It throws error about `ParameterResolver`, so I did this approach.



##########
tools/src/test/java/org/apache/kafka/tools/GetOffsetShellTest.java:
##########
@@ -0,0 +1,376 @@
+/*
+ * 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.tools;
+
+import kafka.test.ClusterInstance;
+import kafka.test.annotation.ClusterTest;
+import kafka.test.annotation.ClusterTestDefaults;
+import kafka.test.annotation.Type;
+import kafka.test.junit.ClusterTestExtensions;
+import org.apache.kafka.clients.CommonClientConfigs;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.NewTopic;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerConfig;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.serialization.StringSerializer;
+import org.apache.kafka.common.utils.Exit;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.Properties;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@ExtendWith(value = ClusterTestExtensions.class)
+@ClusterTestDefaults(clusterType = Type.ZK)
+@Tag("integration")
+public class GetOffsetShellTest {
+    private final int topicCount = 4;
+    private final int offsetTopicPartitionCount = 4;
+    private final ClusterInstance cluster;
+
+    public GetOffsetShellTest(ClusterInstance cluster) {
+        this.cluster = cluster;
+    }
+
+    private String getTopicName(int i) {
+        return "topic" + i;
+    }
+
+    public void setUp() {
+        cluster.config().serverProperties().put("auto.create.topics.enable", false);
+        cluster.config().serverProperties().put("offsets.topic.replication.factor", "1");
+        cluster.config().serverProperties().put("offsets.topic.num.partitions", String.valueOf(offsetTopicPartitionCount));
+
+        try (Admin admin = Admin.create(cluster.config().adminClientProperties())) {
+            List<NewTopic> topics = new ArrayList<>();
+
+            IntStream.range(0, topicCount + 1).forEach(i -> topics.add(new NewTopic(getTopicName(i), i, (short) 1)));
+
+            admin.createTopics(topics);
+        }
+
+        Properties props = new Properties();
+        props.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, cluster.config().producerProperties().get("bootstrap.servers"));
+        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
+        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
+
+        try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) {
+            IntStream.range(0, topicCount + 1)
+                .forEach(i -> IntStream.range(0, i * i)
+                        .forEach(msgCount -> producer.send(
+                                new ProducerRecord<>(getTopicName(i), msgCount % i, null, "val" + msgCount)))
+                );
+        }
+    }
+
+    static class Row {
+        private String name;
+        private int partition;
+        private Long timestamp;
+
+        public Row(String name, int partition, Long timestamp) {
+            this.name = name;
+            this.partition = partition;
+            this.timestamp = timestamp;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (o == this) return true;
+
+            if (!(o instanceof Row)) return false;
+
+            Row r = (Row) o;
+
+            return name.equals(r.name) && partition == r.partition && Objects.equals(timestamp, r.timestamp);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(name, partition, timestamp);
+        }
+    }
+
+    @ClusterTest
+    public void testNoFilterOptions() {
+        setUp();
+
+        List<Row> output = executeAndParse();
+
+        assertEquals(expectedOffsetsWithInternal(), output);
+    }
+
+    @ClusterTest
+    public void testInternalExcluded() {
+        setUp();

Review Comment:
   This thing confuses me, but I couldn't find any workaround



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

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