diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/ClientRebootstrapTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/ClientRebootstrapTest.java index 1693cd33c64fd..bfb88458111f6 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/ClientRebootstrapTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/ClientRebootstrapTest.java @@ -119,9 +119,7 @@ public void testAdminRebootstrapDisabled(ClusterInstance clusterInstance) throws } ) public void testProducerRebootstrap(ClusterInstance clusterInstance) throws ExecutionException, InterruptedException { - try (var admin = clusterInstance.admin()) { - admin.createTopics(List.of(new NewTopic(TOPIC, PARTITIONS, (short) REPLICAS))); - } + clusterInstance.createTopic(TOPIC, PARTITIONS, (short) REPLICAS); var broker0 = 0; var broker1 = 1; @@ -154,9 +152,7 @@ public void testProducerRebootstrap(ClusterInstance clusterInstance) throws Exec } ) public void testProducerRebootstrapDisabled(ClusterInstance clusterInstance) throws ExecutionException, InterruptedException { - try (var admin = clusterInstance.admin()) { - admin.createTopics(List.of(new NewTopic(TOPIC, PARTITIONS, (short) REPLICAS))); - } + clusterInstance.createTopic(TOPIC, PARTITIONS, (short) REPLICAS); var broker0 = 0; var broker1 = 1; @@ -307,9 +303,7 @@ public void testConsumerRebootstrapDisabled(ClusterInstance clusterInstance) thr } ) public void testRebootstrapOnMetadataClusterCheckFail(ClusterInstance clusterInstance) throws ExecutionException, InterruptedException { - try (var admin = clusterInstance.admin()) { - admin.createTopics(List.of(new NewTopic(TOPIC, 2, (short) 1))); - } + clusterInstance.createTopic(TOPIC, 2, (short) 1); try (var producer = clusterInstance.producer()) { var recordMetadata0 = producer.send(new ProducerRecord<>(TOPIC, 0, null, "value 0".getBytes())).get(); diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/AddPartitionsTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/AddPartitionsTest.java index 48414055c4c7f..97c2b9becea07 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/AddPartitionsTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/AddPartitionsTest.java @@ -130,14 +130,10 @@ public void testIncrementPartitions(ClusterInstance cluster) throws Exception { @ClusterTest(brokers = 3, controllers = 3, metadataVersion = MetadataVersion.IBP_3_7_IV2) }) public void testCreatePartitionsAcrossMetadataVersions(ClusterInstance cluster) throws Exception { - try (Admin admin = cluster.admin()) { - Map> createResults = admin.createTopics(List.of( - new NewTopic("foo", 1, (short) 3), - new NewTopic("bar", 2, (short) 3) - )).values(); - createResults.get("foo").get(); - createResults.get("bar").get(); + cluster.createTopic("foo", 1, (short) 3); + cluster.createTopic("bar", 2, (short) 3); + try (Admin admin = cluster.admin()) { Map> increaseResults = admin.createPartitions(Map.of( "foo", NewPartitions.increaseTo(3), "bar", NewPartitions.increaseTo(2) @@ -246,11 +242,8 @@ public void testCreatePartitions(ClusterInstance cluster) throws Exception { try (Admin admin = cluster.admin()) { String topic1 = "create-partitions-topic-1"; String topic2 = "create-partitions-topic-2"; - admin.createTopics(List.of( - new NewTopic(topic1, 1, (short) 1), - new NewTopic(topic2, 1, (short) 2))).all().get(); - cluster.waitTopicCreation(topic1, 1); - cluster.waitTopicCreation(topic2, 1); + cluster.createTopic(topic1, 1, (short) 1); + cluster.createTopic(topic2, 1, (short) 2); assertEquals(1, numPartitions(admin, topic1)); assertEquals(1, numPartitions(admin, topic2)); diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/AdminMetadataTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/AdminMetadataTest.java index 9492ac50767c2..df5df5d3da5bb 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/AdminMetadataTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/AdminMetadataTest.java @@ -277,11 +277,9 @@ public void testDescribeReplicaLogDirs() throws Exception { @ClusterTest public void testDescribeTopicsWithOptionPartitionSizeLimitPerResponse() throws Exception { + String testTopic = "test-topic"; + clusterInstance.createTopic(testTopic, 3, (short) 1); try (Admin admin = clusterInstance.admin()) { - String testTopic = "test-topic"; - admin.createTopics(List.of(new NewTopic(testTopic, 3, (short) 1))).all().get(); - clusterInstance.waitTopicCreation(testTopic, 3); - Map topics = admin.describeTopics(List.of(testTopic), new DescribeTopicsOptions().partitionSizeLimitPerResponse(1)).allTopicNames().get(); assertEquals(1, topics.size()); @@ -307,11 +305,9 @@ public void testDescribeTopicsWithOptionTimeoutMs() { */ @ClusterTest public void testDescribeNonExistingTopic() throws Exception { + String existingTopic = "existing-topic"; + clusterInstance.createTopic(existingTopic, 1, (short) 1); try (Admin admin = clusterInstance.admin()) { - String existingTopic = "existing-topic"; - admin.createTopics(List.of(new NewTopic(existingTopic, 1, (short) 1))).all().get(); - clusterInstance.waitTopicCreation(existingTopic, 1); - String nonExistingTopic = "non-existing"; Map> results = admin.describeTopics(List.of(nonExistingTopic, existingTopic)).topicNameValues(); diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/ClientTelemetryTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/ClientTelemetryTest.java index 85d9ff9623c0b..beb474409d596 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/ClientTelemetryTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/ClientTelemetryTest.java @@ -70,11 +70,9 @@ public void testClientInstanceId(ClusterInstance clusterInstance) throws Interru Map configs = new HashMap<>(); configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, clusterInstance.bootstrapServers()); configs.put(AdminClientConfig.ENABLE_METRICS_PUSH_CONFIG, true); + String testTopicName = "test_topic"; + clusterInstance.createTopic(testTopicName, 1, (short) 1); try (Admin admin = Admin.create(configs)) { - String testTopicName = "test_topic"; - admin.createTopics(Collections.singletonList(new NewTopic(testTopicName, 1, (short) 1))); - clusterInstance.waitTopicCreation(testTopicName, 1); - Map producerConfigs = new HashMap<>(); producerConfigs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, clusterInstance.bootstrapServers()); producerConfigs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DeleteTopicTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DeleteTopicTest.java index 1ba69d862ee1a..5ed4f1f59ff56 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DeleteTopicTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DeleteTopicTest.java @@ -71,10 +71,7 @@ public void testCreateAndDeleteTopic(ClusterInstance cluster) throws Exception { String testTopic = "test-topic"; try (Admin admin = cluster.admin()) { // Create a test topic - List newTopics = List.of(new NewTopic(testTopic, 1, (short) 3)); - CreateTopicsResult createTopicResult = admin.createTopics(newTopics); - createTopicResult.all().get(); - cluster.waitTopicCreation(testTopic, 1); + cluster.createTopic(testTopic, 1, (short) 3); // Delete topic DeleteTopicsResult deleteResult = admin.deleteTopics(List.of(testTopic)); @@ -88,7 +85,7 @@ public void testCreateAndDeleteTopic(ClusterInstance cluster) throws Exception { @ClusterTest public void testDeleteTopicWithAllAliveReplicas(ClusterInstance cluster) throws Exception { try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(DEFAULT_TOPIC, expectedReplicaAssignment))).all().get(); + cluster.createTopicWithAssignment(DEFAULT_TOPIC, expectedReplicaAssignment); admin.deleteTopics(List.of(DEFAULT_TOPIC)).all().get(); cluster.waitTopicDeletion(DEFAULT_TOPIC); } @@ -97,7 +94,7 @@ public void testDeleteTopicWithAllAliveReplicas(ClusterInstance cluster) throws @ClusterTest public void testResumeDeleteTopicWithRecoveredFollower(ClusterInstance cluster) throws Exception { try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(DEFAULT_TOPIC, expectedReplicaAssignment))).all().get(); + cluster.createTopicWithAssignment(DEFAULT_TOPIC, expectedReplicaAssignment); TopicPartition topicPartition = new TopicPartition(DEFAULT_TOPIC, 0); int leaderId = waitUtilLeaderIsKnown(cluster.brokers(), topicPartition); KafkaBroker follower = findFollower(cluster.brokers().values(), leaderId); @@ -120,7 +117,7 @@ public void testResumeDeleteTopicWithRecoveredFollower(ClusterInstance cluster) @ClusterTest(brokers = 4) public void testPartitionReassignmentDuringDeleteTopic(ClusterInstance cluster) throws Exception { try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(DEFAULT_TOPIC, expectedReplicaAssignment))).all().get(); + cluster.createTopicWithAssignment(DEFAULT_TOPIC, expectedReplicaAssignment); TopicPartition topicPartition = new TopicPartition(DEFAULT_TOPIC, 0); Map servers = findPartitionHostingBrokers(cluster.brokers()); int leaderId = waitUtilLeaderIsKnown(cluster.brokers(), topicPartition); @@ -146,7 +143,7 @@ public void testPartitionReassignmentDuringDeleteTopic(ClusterInstance cluster) @ClusterTest(brokers = 4) public void testIncreasePartitionCountDuringDeleteTopic(ClusterInstance cluster) throws Exception { try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(DEFAULT_TOPIC, expectedReplicaAssignment))).all().get(); + cluster.createTopicWithAssignment(DEFAULT_TOPIC, expectedReplicaAssignment); TopicPartition topicPartition = new TopicPartition(DEFAULT_TOPIC, 0); Map partitionHostingBrokers = findPartitionHostingBrokers(cluster.brokers()); waitForReplicaCreated(partitionHostingBrokers, topicPartition, "Replicas for topic test not created."); @@ -174,7 +171,7 @@ public void testIncreasePartitionCountDuringDeleteTopic(ClusterInstance cluster) @ClusterTest public void testDeleteTopicDuringAddPartition(ClusterInstance cluster) throws Exception { try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(DEFAULT_TOPIC, expectedReplicaAssignment))).all().get(); + cluster.createTopicWithAssignment(DEFAULT_TOPIC, expectedReplicaAssignment); int leaderId = waitUtilLeaderIsKnown(cluster.brokers(), new TopicPartition(DEFAULT_TOPIC, 0)); TopicPartition newTopicPartition = new TopicPartition(DEFAULT_TOPIC, 1); KafkaBroker follower = findFollower(cluster.brokers().values(), leaderId); @@ -199,7 +196,7 @@ public void testDeleteTopicDuringAddPartition(ClusterInstance cluster) throws Ex @ClusterTest public void testAddPartitionDuringDeleteTopic(ClusterInstance cluster) throws Exception { try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(DEFAULT_TOPIC, expectedReplicaAssignment))).all().get(); + cluster.createTopicWithAssignment(DEFAULT_TOPIC, expectedReplicaAssignment); // partitions to be added to the topic later TopicPartition newTopicPartition = new TopicPartition(DEFAULT_TOPIC, 1); admin.deleteTopics(List.of(DEFAULT_TOPIC)).all().get(); @@ -213,12 +210,12 @@ public void testAddPartitionDuringDeleteTopic(ClusterInstance cluster) throws Ex @ClusterTest public void testRecreateTopicAfterDeletion(ClusterInstance cluster) throws Exception { try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(DEFAULT_TOPIC, expectedReplicaAssignment))).all().get(); + cluster.createTopicWithAssignment(DEFAULT_TOPIC, expectedReplicaAssignment); TopicPartition topicPartition = new TopicPartition(DEFAULT_TOPIC, 0); admin.deleteTopics(List.of(DEFAULT_TOPIC)).all().get(); cluster.waitTopicDeletion(DEFAULT_TOPIC); // re-create topic on same replicas - admin.createTopics(List.of(new NewTopic(DEFAULT_TOPIC, expectedReplicaAssignment))).all().get(); + cluster.createTopicWithAssignment(DEFAULT_TOPIC, expectedReplicaAssignment); waitForReplicaCreated(cluster.brokers(), topicPartition, "Replicas for topic " + DEFAULT_TOPIC + " not created."); } } @@ -226,7 +223,7 @@ public void testRecreateTopicAfterDeletion(ClusterInstance cluster) throws Excep @ClusterTest public void testDeleteNonExistingTopic(ClusterInstance cluster) throws Exception { try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(DEFAULT_TOPIC, expectedReplicaAssignment))).all().get(); + cluster.createTopicWithAssignment(DEFAULT_TOPIC, expectedReplicaAssignment); TopicPartition topicPartition = new TopicPartition(DEFAULT_TOPIC, 0); String topic = "test2"; TestUtils.waitForCondition(() -> { @@ -252,7 +249,7 @@ public void testDeleteNonExistingTopic(ClusterInstance cluster) throws Exception }) public void testDeleteTopicWithCleaner(ClusterInstance cluster) throws Exception { try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(DEFAULT_TOPIC, expectedReplicaAssignment))).all().get(); + cluster.createTopicWithAssignment(DEFAULT_TOPIC, expectedReplicaAssignment); TopicPartition topicPartition = new TopicPartition(DEFAULT_TOPIC, 0); // for simplicity, we are validating cleaner offsets on a single broker KafkaBroker server = cluster.brokers().values().stream().findFirst().orElseThrow(); @@ -273,7 +270,7 @@ public void testDeleteTopicWithCleaner(ClusterInstance cluster) throws Exception @ClusterTest public void testDeleteTopicAlreadyMarkedAsDeleted(ClusterInstance cluster) throws Exception { try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(DEFAULT_TOPIC, expectedReplicaAssignment))).all().get(); + cluster.createTopicWithAssignment(DEFAULT_TOPIC, expectedReplicaAssignment); admin.deleteTopics(List.of(DEFAULT_TOPIC)).all().get(); TestUtils.waitForCondition(() -> { @@ -293,7 +290,7 @@ public void testDeleteTopicAlreadyMarkedAsDeleted(ClusterInstance cluster) throw serverProperties = {@ClusterConfigProperty(key = ServerConfigs.DELETE_TOPIC_ENABLE_CONFIG, value = "false")}) public void testDisableDeleteTopic(ClusterInstance cluster) throws Exception { try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(DEFAULT_TOPIC, expectedReplicaAssignment))).all().get(); + cluster.createTopicWithAssignment(DEFAULT_TOPIC, expectedReplicaAssignment); TopicPartition topicPartition = new TopicPartition(DEFAULT_TOPIC, 0); TestUtils.waitForCondition(() -> { try { diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DescribeAuthorizedOperationsTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DescribeAuthorizedOperationsTest.java index 1464fc06eb10f..251ee3b081080 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DescribeAuthorizedOperationsTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DescribeAuthorizedOperationsTest.java @@ -117,8 +117,7 @@ public void testConsumerGroupAuthorizedOperations(ClusterInstance clusterInstanc try (Admin admin = clusterInstance.admin(createAdminConfig(JaasUtils.KAFKA_PLAIN_ADMIN, JaasUtils.KAFKA_PLAIN_ADMIN_PASSWORD)); Admin user1 = clusterInstance.admin(createAdminConfig(JaasUtils.KAFKA_PLAIN_USER1, JaasUtils.KAFKA_PLAIN_USER1_PASSWORD)) ) { - admin.createTopics(List.of(new NewTopic("topic1", 1, (short) 1))); - clusterInstance.waitTopicCreation("topic1", 1); + clusterInstance.createTopic("topic1", 1, (short) 1); // create consumers to avoid group not found error TopicPartition tp = new TopicPartition("topic1", 0); @@ -188,14 +187,8 @@ public void testTopicAuthorizedOperations(ClusterInstance clusterInstance) throw String topic1 = "topic1"; String topic2 = "topic2"; setupSecurity(clusterInstance); - try (Admin admin = clusterInstance.admin(createAdminConfig(JaasUtils.KAFKA_PLAIN_ADMIN, JaasUtils.KAFKA_PLAIN_ADMIN_PASSWORD))) { - admin.createTopics(List.of( - new NewTopic(topic1, 1, (short) 1), - new NewTopic(topic2, 1, (short) 1) - )); - clusterInstance.waitTopicCreation(topic1, 1); - clusterInstance.waitTopicCreation(topic2, 1); - } + clusterInstance.createTopic(topic1, 1, (short) 1); + clusterInstance.createTopic(topic2, 1, (short) 1); try (Admin admin = clusterInstance.admin(createAdminConfig(JaasUtils.KAFKA_PLAIN_USER1, JaasUtils.KAFKA_PLAIN_USER1_PASSWORD))) { // test without includeAuthorizedOperations flag diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DynamicBrokerConfigTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DynamicBrokerConfigTest.java index c049545a5aa3e..bba5733cd4d45 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DynamicBrokerConfigTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DynamicBrokerConfigTest.java @@ -21,7 +21,6 @@ import org.apache.kafka.common.test.api.ClusterTest; import org.apache.kafka.common.test.api.Type; import org.apache.kafka.server.config.ServerConfigs; -import org.apache.kafka.test.TestUtils; import java.util.List; import java.util.Map; @@ -40,11 +39,7 @@ public void testIncreaseNumIoThreads(ClusterInstance cluster) throws Exception { // KAFKA-16649 introduced this coverage as a safeguard against future deadlocks. // Verify that the broker continues serving requests after resizing the I/O thread pool. - admin.createTopics(List.of(new NewTopic("test-topic", 1, (short) 1))).all().get(); - TestUtils.waitForCondition( - () -> admin.listTopics().names().get().contains("test-topic"), - "Failed to find test-topic" - ); + cluster.createTopic("test-topic", 1, (short) 1); } } } diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/StaticBrokerConfigTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/StaticBrokerConfigTest.java index 6f35f7ec30757..56cbc68f6f6be 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/StaticBrokerConfigTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/StaticBrokerConfigTest.java @@ -131,9 +131,7 @@ public void testInternalConfigsDoNotReturnForDescribeConfigs(ClusterInstance clu ConfigResource groupResource = new ConfigResource(ConfigResource.Type.GROUP, "testGroup"); ConfigResource clientMetricsResource = new ConfigResource(ConfigResource.Type.CLIENT_METRICS, "testClient"); - admin.createTopics(List.of(new NewTopic(TOPIC, 1, (short) 1))).config(TOPIC).get(); - // make sure the topic metadata exist - cluster.waitTopicCreation(TOPIC, 1); + cluster.createTopic(TOPIC, 1, (short) 1); Map configResourceMap = admin.describeConfigs( List.of(brokerResource, topicResource, groupResource, clientMetricsResource)).all().get(); diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerIntegrationTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerIntegrationTest.java index fcfb493388dec..366821fed9dfc 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerIntegrationTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerIntegrationTest.java @@ -19,7 +19,6 @@ import org.apache.kafka.clients.admin.Admin; import org.apache.kafka.clients.admin.NewPartitionReassignment; import org.apache.kafka.clients.admin.NewPartitions; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.consumer.internals.AbstractHeartbeatRequestManager; import org.apache.kafka.clients.producer.Producer; import org.apache.kafka.clients.producer.ProducerConfig; @@ -261,7 +260,9 @@ public void testLeaderEpoch(ClusterInstance clusterInstance) throws Exception { ) }) public void testRackAwareAssignment(ClusterInstance clusterInstance) throws ExecutionException, InterruptedException { + // Create a new topic with 1 partition on broker 0. String topic = "test-topic"; + clusterInstance.createTopicWithAssignment(topic, Map.of(0, List.of(0))); try (Admin admin = clusterInstance.admin(); Producer producer = clusterInstance.producer(); Consumer consumer0 = clusterInstance.consumer(Map.of( @@ -283,10 +284,6 @@ public void testRackAwareAssignment(ClusterInstance clusterInstance) throws Exec ConsumerConfig.GROUP_PROTOCOL_CONFIG, GroupProtocol.CONSUMER.name() )) ) { - // Create a new topic with 1 partition on broker 0. - admin.createTopics(List.of(new NewTopic(topic, Map.of(0, List.of(0))))); - clusterInstance.waitTopicCreation(topic, 1); - producer.send(new ProducerRecord<>(topic, "key".getBytes(), "value".getBytes())); producer.flush(); @@ -376,9 +373,7 @@ public void testSingleCoordinatorOwnershipAfterPartitionReassignment(ClusterInst producer.send(new ProducerRecord<>("topic", "value".getBytes(StandardCharsets.UTF_8))); } - try (var admin = clusterInstance.admin()) { - admin.createTopics(List.of(new NewTopic(Topic.GROUP_METADATA_TOPIC_NAME, Map.of(0, List.of(0))))).all().get(); - } + clusterInstance.createTopicWithAssignment(Topic.GROUP_METADATA_TOPIC_NAME, Map.of(0, List.of(0))); try (var consumer = clusterInstance.consumer(Map.of(ConsumerConfig.GROUP_ID_CONFIG, "test-group")); var admin = clusterInstance.admin()) { diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerWithLegacyMessageFormatIntegrationTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerWithLegacyMessageFormatIntegrationTest.java index c5feb31cf5ae1..6c413e9788322 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerWithLegacyMessageFormatIntegrationTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerWithLegacyMessageFormatIntegrationTest.java @@ -17,8 +17,6 @@ package org.apache.kafka.clients.consumer; import org.apache.kafka.clients.ClientsTestUtils; -import org.apache.kafka.clients.admin.Admin; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.common.TopicPartition; import org.apache.kafka.common.compress.Compression; import org.apache.kafka.common.record.TimestampType; @@ -108,19 +106,11 @@ private void appendLegacyRecords(int numRecords, TopicPartition tp, int brokerId }); } - private void createTopicWithAssignment(String topic, Map> assignment) throws InterruptedException { - try (Admin admin = cluster.admin()) { - NewTopic newTopic = new NewTopic(topic, assignment); - admin.createTopics(List.of(newTopic)); - cluster.waitTopicCreation(topic, assignment.size()); - } - } - @BeforeEach public void setupTopics() throws InterruptedException { cluster.createTopic(topic1, 2, (short) 1); - createTopicWithAssignment(topic2, Map.of(0, List.of(0), 1, List.of(1))); - createTopicWithAssignment(topic3, Map.of(0, List.of(0), 1, List.of(1))); + cluster.createTopicWithAssignment(topic2, Map.of(0, List.of(0), 1, List.of(1))); + cluster.createTopicWithAssignment(topic3, Map.of(0, List.of(0), 1, List.of(1))); // v2 message format for topic1 ClientsTestUtils.sendRecords(cluster, t1p0, 100, 0); diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerDLQTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerDLQTest.java index 6776a3d8373ba..91bd21734ff44 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerDLQTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerDLQTest.java @@ -21,7 +21,6 @@ import org.apache.kafka.clients.admin.AlterConfigsOptions; import org.apache.kafka.clients.admin.Config; import org.apache.kafka.clients.admin.ConfigEntry; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.admin.OffsetSpec; import org.apache.kafka.clients.producer.Producer; import org.apache.kafka.clients.producer.ProducerConfig; @@ -501,16 +500,11 @@ public void testDlqRespectsDlqTopicMaxMessageBytesNotEqToSourceTopic() throws Ex // so a single record's DLQ copy cleanly fits under the limit but two together clearly don't. int payloadSize = dlqMaxMessageBytes - 2_000; - try (Admin admin = createAdminClient()) { - admin.createTopics(Set.of( - new NewTopic(sourceTopic, 1, (short) 1) - .configs(Map.of(TopicConfig.MAX_MESSAGE_BYTES_CONFIG, Integer.toString(sourceMaxMessageBytes))), - new NewTopic(dlqTopic, 1, (short) 1) - .configs(Map.of( - TopicConfig.ERRORS_DEADLETTERQUEUE_GROUP_ENABLE_CONFIG, "true", - TopicConfig.MAX_MESSAGE_BYTES_CONFIG, Integer.toString(dlqMaxMessageBytes))) - )).all().get(); - } + cluster.createTopic(sourceTopic, 1, (short) 1, + Map.of(TopicConfig.MAX_MESSAGE_BYTES_CONFIG, Integer.toString(sourceMaxMessageBytes))); + cluster.createTopic(dlqTopic, 1, (short) 1, Map.of( + TopicConfig.ERRORS_DEADLETTERQUEUE_GROUP_ENABLE_CONFIG, "true", + TopicConfig.MAX_MESSAGE_BYTES_CONFIG, Integer.toString(dlqMaxMessageBytes))); alterShareAutoOffsetReset(groupId, "earliest"); alterShareGroupConfig(groupId, GroupConfig.ERRORS_DEADLETTERQUEUE_TOPIC_NAME_CONFIG, dlqTopic); @@ -634,15 +628,10 @@ public void testDlqCopyRecordSkippedWhenDecompressedSizeExceedsDlqMaxMessageByte // plus batch/record framing overhead. int highDlqMaxMessageBytes = 500_000; - try (Admin admin = createAdminClient()) { - admin.createTopics(Set.of( - new NewTopic(sourceTopic, 1, (short) 1), - new NewTopic(dlqTopic, 1, (short) 1) - .configs(Map.of( - TopicConfig.ERRORS_DEADLETTERQUEUE_GROUP_ENABLE_CONFIG, "true", - TopicConfig.MAX_MESSAGE_BYTES_CONFIG, Integer.toString(lowDlqMaxMessageBytes))) - )).all().get(); - } + cluster.createTopic(sourceTopic, 1, (short) 1); + cluster.createTopic(dlqTopic, 1, (short) 1, Map.of( + TopicConfig.ERRORS_DEADLETTERQUEUE_GROUP_ENABLE_CONFIG, "true", + TopicConfig.MAX_MESSAGE_BYTES_CONFIG, Integer.toString(lowDlqMaxMessageBytes))); alterShareAutoOffsetReset(groupId, "earliest"); alterShareGroupConfig(groupId, GroupConfig.ERRORS_DEADLETTERQUEUE_TOPIC_NAME_CONFIG, dlqTopic); @@ -678,14 +667,9 @@ public void testDlqCopyRecordSkippedWhenDecompressedSizeExceedsDlqMaxMessageByte // comfortably above the decompressed payload size up front - simpler than altering the first // topic's config and waiting for it to propagate. String dlqTopic2 = "dlq.decompress-cap-2"; - try (Admin admin = createAdminClient()) { - admin.createTopics(Set.of( - new NewTopic(dlqTopic2, 1, (short) 1) - .configs(Map.of( - TopicConfig.ERRORS_DEADLETTERQUEUE_GROUP_ENABLE_CONFIG, "true", - TopicConfig.MAX_MESSAGE_BYTES_CONFIG, Integer.toString(highDlqMaxMessageBytes))) - )).all().get(); - } + cluster.createTopic(dlqTopic2, 1, (short) 1, Map.of( + TopicConfig.ERRORS_DEADLETTERQUEUE_GROUP_ENABLE_CONFIG, "true", + TopicConfig.MAX_MESSAGE_BYTES_CONFIG, Integer.toString(highDlqMaxMessageBytes))); alterShareGroupConfig(groupId, GroupConfig.ERRORS_DEADLETTERQUEUE_TOPIC_NAME_CONFIG, dlqTopic2); try (Producer producer = createProducer(Map.of(ProducerConfig.COMPRESSION_TYPE_CONFIG, "gzip"))) { @@ -972,13 +956,8 @@ private void createDlqTopic(String topicName) { } private void createDlqTopic(String topicName, int numPartitions) { - assertDoesNotThrow(() -> { - try (Admin admin = createAdminClient()) { - NewTopic newTopic = new NewTopic(topicName, numPartitions, (short) 1) - .configs(Map.of(TopicConfig.ERRORS_DEADLETTERQUEUE_GROUP_ENABLE_CONFIG, "true")); - admin.createTopics(Set.of(newTopic)).all().get(); - } - }, "Failed to create DLQ topic"); + assertDoesNotThrow(() -> cluster.createTopic(topicName, numPartitions, (short) 1, + Map.of(TopicConfig.ERRORS_DEADLETTERQUEUE_GROUP_ENABLE_CONFIG, "true")), "Failed to create DLQ topic"); } // Creates a single-partition source topic with tiered storage enabled and one log segment per record (via @@ -987,18 +966,15 @@ private void createDlqTopic(String topicName, int numPartitions) { // storage; the total retention (`retentionMs`) is kept generous so the remote segments are not deleted while // the test is still running. private void createRemoteStorageSourceTopic(String topic, long retentionMs, long localRetentionMs) { - assertDoesNotThrow(() -> { - try (Admin admin = createAdminClient()) { - Map configs = Map.of( - TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, "true", - TopicConfig.RETENTION_MS_CONFIG, Long.toString(retentionMs), - TopicConfig.LOCAL_LOG_RETENTION_MS_CONFIG, Long.toString(localRetentionMs), - // Roll a segment for every record so each inactive segment can be offloaded then deleted locally. - TopicConfig.INDEX_INTERVAL_BYTES_CONFIG, "1", - TopicConfig.SEGMENT_INDEX_BYTES_CONFIG, "12"); - admin.createTopics(Set.of(new NewTopic(topic, 1, (short) 1).configs(configs))).all().get(); - } - }, "Failed to create remote-storage source topic"); + Map configs = Map.of( + TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, "true", + TopicConfig.RETENTION_MS_CONFIG, Long.toString(retentionMs), + TopicConfig.LOCAL_LOG_RETENTION_MS_CONFIG, Long.toString(localRetentionMs), + // Roll a segment for every record so each inactive segment can be offloaded then deleted locally. + TopicConfig.INDEX_INTERVAL_BYTES_CONFIG, "1", + TopicConfig.SEGMENT_INDEX_BYTES_CONFIG, "12"); + assertDoesNotThrow(() -> cluster.createTopic(topic, 1, (short) 1, configs), + "Failed to create remote-storage source topic"); } // The earliest offset still held in local storage. Offsets below this have been removed locally (e.g. after diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerRackAwareTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerRackAwareTest.java index 3c9b07be26adb..9c1a8a1be6918 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerRackAwareTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerRackAwareTest.java @@ -20,7 +20,6 @@ import org.apache.kafka.clients.admin.Admin; import org.apache.kafka.clients.admin.NewPartitionReassignment; import org.apache.kafka.clients.admin.NewPartitions; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.admin.ShareGroupDescription; import org.apache.kafka.clients.producer.Producer; import org.apache.kafka.clients.producer.ProducerRecord; @@ -66,6 +65,8 @@ public class ShareConsumerRackAwareTest { public void testShareConsumerWithRackAwareAssignor(ClusterInstance clusterInstance) throws ExecutionException, InterruptedException { String groupId = "group0"; String topic = "test-topic"; + // Create a new topic with 1 partition on broker 0. + clusterInstance.createTopicWithAssignment(topic, Map.of(0, List.of(0))); try (Admin admin = clusterInstance.admin(); Producer producer = clusterInstance.producer(); ShareConsumer consumer0 = clusterInstance.shareConsumer(Map.of( @@ -84,10 +85,6 @@ public void testShareConsumerWithRackAwareAssignor(ClusterInstance clusterInstan CommonClientConfigs.CLIENT_RACK_CONFIG, "rack2" )) ) { - // Create a new topic with 1 partition on broker 0. - admin.createTopics(List.of(new NewTopic(topic, Map.of(0, List.of(0))))); - clusterInstance.waitTopicCreation(topic, 1); - producer.send(new ProducerRecord<>(topic, "key".getBytes(), "value".getBytes())); producer.flush(); diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerFailureHandlingTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerFailureHandlingTest.java index d06a29d72ad33..33034e50e886e 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerFailureHandlingTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerFailureHandlingTest.java @@ -20,7 +20,6 @@ import org.apache.kafka.clients.admin.Admin; import org.apache.kafka.clients.admin.ListTopicsOptions; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.common.config.TopicConfig; import org.apache.kafka.common.errors.InvalidTopicException; import org.apache.kafka.common.errors.NotEnoughReplicasAfterAppendException; @@ -36,7 +35,6 @@ import org.apache.kafka.common.test.api.Type; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; @@ -278,9 +276,7 @@ public void testNotEnoughReplicasAfterBrokerShutdown(ClusterInstance clusterInst String topicName = "minisrtest2"; int brokerNum = clusterInstance.brokers().size(); Map topicConfig = Map.of(MIN_IN_SYNC_REPLICAS_CONFIG, String.valueOf(brokerNum)); - try (Admin admin = clusterInstance.admin()) { - admin.createTopics(List.of(new NewTopic(topicName, 1, (short) brokerNum).configs(topicConfig))); - } + clusterInstance.createTopic(topicName, 1, (short) brokerNum, topicConfig); ProducerRecord record = new ProducerRecord<>(topicName, null, "key".getBytes(), "value".getBytes()); diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerIntegrationTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerIntegrationTest.java index d9bdd3347a378..273169dd04cff 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerIntegrationTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerIntegrationTest.java @@ -16,7 +16,6 @@ */ package org.apache.kafka.clients.producer; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.admin.TransactionState; import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerConfig; @@ -121,25 +120,22 @@ public void testTransactionWithAndWithoutSend(ClusterInstance cluster) { @ClusterTest(features = { @ClusterFeature(feature = Feature.TRANSACTION_VERSION, version = 2)}), }) - public void testTransactionWithInvalidSendAndEndTxnRequestSent(ClusterInstance cluster) { - var topic = new NewTopic("foobar", 1, (short) 1) - .configs(Map.of(TopicConfig.MAX_MESSAGE_BYTES_CONFIG, "100")); + public void testTransactionWithInvalidSendAndEndTxnRequestSent(ClusterInstance cluster) throws InterruptedException { + var topic = "foobar"; + cluster.createTopic(topic, 1, (short) 1, Map.of(TopicConfig.MAX_MESSAGE_BYTES_CONFIG, "100")); String txnId = "test-txn"; Map properties = Map.of( ProducerConfig.TRANSACTIONAL_ID_CONFIG, txnId, ProducerConfig.CLIENT_ID_CONFIG, "test", ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true"); - try (var admin = cluster.admin(); - var producer = cluster.producer(properties)) { - admin.createTopics(List.of(topic)); - + try (var producer = cluster.producer(properties)) { producer.initTransactions(); producer.beginTransaction(); assertInstanceOf(RecordTooLargeException.class, assertThrows(ExecutionException.class, () -> producer.send(new ProducerRecord<>( - topic.name(), new byte[100], new byte[100])).get()).getCause()); + topic, new byte[100], new byte[100])).get()).getCause()); producer.abortTransaction(); } diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerSendWhileDeletionTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerSendWhileDeletionTest.java index 7d5f5e5584dbe..409f99338a346 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerSendWhileDeletionTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerSendWhileDeletionTest.java @@ -100,7 +100,7 @@ public void testSendWithTopicDeletionMidWay() throws Exception { 0, List.of(0, 1), 1, List.of(0, 1) ); - admin.createTopics(List.of(new NewTopic(topic, topicAssignment))); + cluster.createTopicWithAssignment(topic, topicAssignment); // Change leader to 1 for both the partitions to increase leader epoch from 0 -> 1 var reassignment = Map.of( @@ -204,7 +204,7 @@ public void testSendWithTopicReassignmentIsMidWay() throws Exception { var producer = createProducer() ) { // Create topic with leader as 0 for the 1 partition. - admin.createTopics(List.of(new NewTopic(topic, Map.of(0, List.of(0))))); + cluster.createTopicWithAssignment(topic, Map.of(0, List.of(0))); assertLeader(partition0, 0); var topicDetails = topicMetadata(); diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/security/GroupAuthorizerIntegrationTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/security/GroupAuthorizerIntegrationTest.java index 32069732db824..3e97122d65bb9 100644 --- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/security/GroupAuthorizerIntegrationTest.java +++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/security/GroupAuthorizerIntegrationTest.java @@ -16,9 +16,6 @@ */ package org.apache.kafka.clients.security; -import org.apache.kafka.clients.admin.Admin; -import org.apache.kafka.clients.admin.AdminClientConfig; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.clients.consumer.ConsumerRecords; @@ -106,13 +103,7 @@ private void setup(ClusterInstance clusterInstance) throws InterruptedException clusterInstance ); - NewTopic offsetTopic = new NewTopic(Topic.GROUP_METADATA_TOPIC_NAME, 1, (short) 1); - try (Admin admin = clusterInstance.admin(Map.of( - AdminClientConfig.ENABLE_METRICS_PUSH_CONFIG, true)) - ) { - admin.createTopics(Collections.singleton(offsetTopic)); - clusterInstance.waitTopicCreation(Topic.GROUP_METADATA_TOPIC_NAME, 1); - } + clusterInstance.createTopic(Topic.GROUP_METADATA_TOPIC_NAME, 1, (short) 1); } public static class GroupPrincipalBuilder extends DefaultKafkaPrincipalBuilder { diff --git a/server/src/test/java/org/apache/kafka/server/BootstrapControllersIntegrationTest.java b/server/src/test/java/org/apache/kafka/server/BootstrapControllersIntegrationTest.java index 1ca1f6924b50d..d6535bbcdd63f 100644 --- a/server/src/test/java/org/apache/kafka/server/BootstrapControllersIntegrationTest.java +++ b/server/src/test/java/org/apache/kafka/server/BootstrapControllersIntegrationTest.java @@ -21,7 +21,6 @@ import org.apache.kafka.clients.admin.AlterConfigOp; import org.apache.kafka.clients.admin.Config; import org.apache.kafka.clients.admin.ConfigEntry; -import org.apache.kafka.clients.admin.CreateTopicsResult; import org.apache.kafka.clients.admin.DescribeClusterResult; import org.apache.kafka.clients.admin.DescribeFeaturesResult; import org.apache.kafka.clients.admin.DescribeMetadataQuorumResult; @@ -29,7 +28,6 @@ import org.apache.kafka.clients.admin.FinalizedVersionRange; import org.apache.kafka.clients.admin.ListOffsetsResult; import org.apache.kafka.clients.admin.NewPartitionReassignment; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.admin.OffsetSpec; import org.apache.kafka.clients.admin.TopicDescription; import org.apache.kafka.clients.admin.UpdateFeaturesResult; @@ -324,9 +322,7 @@ public void testAlterReassignmentsWithBootstrapControllers(ClusterInstance clust 1, List.of(1, 2, 0), 2, List.of(2, 1, 0) ); - CreateTopicsResult createTopicResult = admin.createTopics(List.of(new NewTopic(topicName, assignments))); - createTopicResult.all().get(); - waitForTopics(admin, Set.of(topicName)); + clusterInstance.createTopicWithAssignment(topicName, assignments); List part0Reassignment = List.of(2, 1, 0); List part1Reassignment = List.of(0, 1, 2); @@ -357,11 +353,6 @@ public void testAlterReassignmentsWithBootstrapControllers(ClusterInstance clust } } - private static void waitForTopics(Admin admin, Set expectedTopics) throws InterruptedException { - TestUtils.waitForCondition(() -> admin.listTopics().names().get().containsAll(expectedTopics), - "timed out waiting for topics"); - } - private static List> translatePartitionInfoToNodeIdList(List partitions) { return partitions.stream() .map(partition -> partition.replicas().stream().map(Node::id).toList()) diff --git a/server/src/test/java/org/apache/kafka/server/CordonedLogDirsIntegrationTest.java b/server/src/test/java/org/apache/kafka/server/CordonedLogDirsIntegrationTest.java index c7a670514960e..c4b7e5931df1f 100644 --- a/server/src/test/java/org/apache/kafka/server/CordonedLogDirsIntegrationTest.java +++ b/server/src/test/java/org/apache/kafka/server/CordonedLogDirsIntegrationTest.java @@ -105,7 +105,7 @@ private void testFeatureNotEnabled(List initialCordonedLogDirs) throws E try (Admin admin = clusterInstance.admin()) { // When the metadata version does not support cordoning log dirs: // 1. we can create a topic, even if cordon.log.dirs is statically set - admin.createTopics(newTopic(TOPIC1)).all().get(); + createTopic(TOPIC1); // 2. no log dirs are marked as cordoned assertCordonedLogDirs(admin, List.of()); // 3. we can't dynamically configure cordoned.log.dirs @@ -147,7 +147,7 @@ private void testFeatureNotEnabled(List initialCordonedLogDirs) throws E // After uncordoning log dirs, we can create topics and partitions again setCordonedLogDirs(admin, List.of(), BROKER_0); - admin.createTopics(newTopics).all().get(); + createTopic(TOPIC2); admin.createPartitions(newPartitions).all().get(); } } @@ -159,7 +159,7 @@ public void testCordonUncordonLogDirs() throws Exception { assertCordonedLogDirs(admin, List.of()); // We can create topics - admin.createTopics(newTopic(TOPIC1)).all().get(); + createTopic(TOPIC1); // Cordon all log dirs setCordonedLogDirs(admin, logDirsBroker0, BROKER_0); @@ -196,7 +196,7 @@ public void testCordonUncordonLogDirs() throws Exception { assertCordonedLogDirs(admin, List.of(logDirsBroker0.get(0))); // We can create topics and partitions again - admin.createTopics(newTopics).all().get(); + createTopic(TOPIC2); admin.createPartitions(newPartitions).all().get(); } } @@ -218,8 +218,8 @@ public void testStaticCordonUncordonLogDirs() throws Exception { // Uncordon log dirs setCordonedLogDirs(admin, List.of(), BROKER_0); - // We can't create topics again - admin.createTopics(newTopics).all().get(); + // We can create topics again + createTopic(TOPIC1); } } @@ -227,7 +227,7 @@ public void testStaticCordonUncordonLogDirs() throws Exception { public void testAlterReplicaWithCordonedLogDirs() throws Exception { TopicPartitionReplica replica = new TopicPartitionReplica(TOPIC1, 0, 0); try (Admin admin = clusterInstance.admin()) { - admin.createTopics(newTopic(TOPIC1)).all().get(); + createTopic(TOPIC1); // Find the log dir that does not host the replica and cordon it AtomicReference logDir = new AtomicReference<>(); @@ -258,7 +258,7 @@ public void testAlterReplicaWithCordonedLogDirs() throws Exception { public void testAlterPartitionWithCordonedLogDirs() throws Exception { Set allBrokers = new HashSet<>(clusterInstance.brokerIds()); try (Admin admin = clusterInstance.admin()) { - admin.createTopics(newTopic(TOPIC1)).all().get(); + createTopic(TOPIC1); // Find the broker that hosts the partition and cordon the other broker AtomicReference partitionBroker = new AtomicReference<>(); @@ -348,7 +348,7 @@ public void testDecommissionBroker() throws ExecutionException, InterruptedExcep try (Admin admin = clusterInstance.admin()) { // Create 10 topics for (int i = 0; i < 10; i++) { - admin.createTopics(newTopic("topic" + i, (short) 1)).all().get(); + createTopic("topic" + i, (short) 1); } // Check the 10 topics have been created and find the partitions on brokerId @@ -377,9 +377,8 @@ public void testDecommissionBroker() throws ExecutionException, InterruptedExcep // Create another 10 topics for (int i = 10; i < 20; i++) { - admin.createTopics(newTopic("topic" + i, (short) 1)).all().get(); + createTopic("topic" + i, (short) 1); } - TestUtils.waitForCondition(() -> admin.listTopics().names().get().size() == 20, 10_000, "Topics 10-19 were not created"); // Check only the other broker has replicas Map> logDescriptionsPerBroker = admin.describeLogDirs(clusterInstance.brokerIds()).allDescriptions().get(); @@ -417,9 +416,8 @@ public void testDecommissionLogDir() throws ExecutionException, InterruptedExcep // Create 10 topics, replicated to every broker so brokerId is guaranteed to host some // replicas on the log dir we're about to decommission for (int i = 0; i < 10; i++) { - admin.createTopics(newTopic("topic" + i)).all().get(); + createTopic("topic" + i); } - TestUtils.waitForCondition(() -> admin.listTopics().names().get().size() == 10, 10_000, "Topics were not created"); ConfigResource brokerResource = new ConfigResource(ConfigResource.Type.BROKER, String.valueOf(brokerId)); @@ -454,10 +452,7 @@ public void testDecommissionLogDir() throws ExecutionException, InterruptedExcep 10_000, "Broker " + brokerId + " is still reporting the removed log dir " + logDirToRemove); // The broker is still fully functional after losing a log dir - admin.createTopics(newTopic("topic-after-decommission")).all().get(); - TestUtils.waitForCondition(() -> - admin.listTopics().names().get().size() == 11, - 10_000, "Topic was not created after decommissioning a log dir"); + createTopic("topic-after-decommission"); } } @@ -499,6 +494,14 @@ private void setCordonedLogDirs(Admin admin, List logDirs, ConfigResourc }, 10_000, "Unable to set the " + CORDONED_LOG_DIRS_CONFIG + " configuration on " + cr + "."); } + private void createTopic(String name) throws InterruptedException { + createTopic(name, (short) clusterInstance.brokers().size()); + } + + private void createTopic(String name, short replicationFactor) throws InterruptedException { + clusterInstance.createTopic(name, 1, replicationFactor); + } + private Set newTopic(String name) { return newTopic(name, (short) clusterInstance.brokers().size()); } diff --git a/server/src/test/java/org/apache/kafka/server/EligibleLeaderReplicasIntegrationTest.java b/server/src/test/java/org/apache/kafka/server/EligibleLeaderReplicasIntegrationTest.java index 8a335fc626db2..d586809c83410 100644 --- a/server/src/test/java/org/apache/kafka/server/EligibleLeaderReplicasIntegrationTest.java +++ b/server/src/test/java/org/apache/kafka/server/EligibleLeaderReplicasIntegrationTest.java @@ -20,7 +20,6 @@ import org.apache.kafka.clients.admin.AlterConfigOp; import org.apache.kafka.clients.admin.ConfigEntry; import org.apache.kafka.clients.admin.FeatureUpdate; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.admin.OffsetSpec; import org.apache.kafka.clients.admin.TopicDescription; import org.apache.kafka.clients.consumer.Consumer; @@ -98,8 +97,7 @@ public void testHighWatermarkShouldNotAdvanceIfUnderMinIsr() throws ExecutionExc Map.of(EligibleLeaderReplicasVersion.FEATURE_NAME, new FeatureUpdate(EligibleLeaderReplicasVersion.ELRV_1.featureLevel(), FeatureUpdate.UpgradeType.UPGRADE))).all().get(); - admin.createTopics(List.of(new NewTopic(testTopicName, 1, (short) 4))).all().get(); - clusterInstance.waitTopicCreation(testTopicName, 1); + clusterInstance.createTopic(testTopicName, 1, (short) 4); ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, testTopicName); Collection ops = new ArrayList<>(); @@ -163,8 +161,7 @@ public void testElrMemberCanBeElected() throws ExecutionException, InterruptedEx admin.updateFeatures( Map.of(EligibleLeaderReplicasVersion.FEATURE_NAME, new FeatureUpdate(EligibleLeaderReplicasVersion.ELRV_1.featureLevel(), FeatureUpdate.UpgradeType.UPGRADE))).all().get(); - admin.createTopics(List.of(new NewTopic(testTopicName, 1, (short) 4))).all().get(); - clusterInstance.waitTopicCreation(testTopicName, 1); + clusterInstance.createTopic(testTopicName, 1, (short) 4); ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, testTopicName); Collection ops = new ArrayList<>(); @@ -232,8 +229,7 @@ public void testElrMemberShouldBeKickOutWhenUncleanShutdown() throws ExecutionEx admin.updateFeatures( Map.of(EligibleLeaderReplicasVersion.FEATURE_NAME, new FeatureUpdate(EligibleLeaderReplicasVersion.ELRV_1.featureLevel(), FeatureUpdate.UpgradeType.UPGRADE))).all().get(); - admin.createTopics(List.of(new NewTopic(testTopicName, 1, (short) 4))).all().get(); - clusterInstance.waitTopicCreation(testTopicName, 1); + clusterInstance.createTopic(testTopicName, 1, (short) 4); ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, testTopicName); Collection ops = new ArrayList<>(); @@ -289,8 +285,7 @@ public void testLastKnownLeaderShouldBeElectedIfEmptyElr() throws ExecutionExcep admin.updateFeatures( Map.of(EligibleLeaderReplicasVersion.FEATURE_NAME, new FeatureUpdate(EligibleLeaderReplicasVersion.ELRV_1.featureLevel(), FeatureUpdate.UpgradeType.UPGRADE))).all().get(); - admin.createTopics(List.of(new NewTopic(testTopicName, 1, (short) 4))).all().get(); - clusterInstance.waitTopicCreation(testTopicName, 1); + clusterInstance.createTopic(testTopicName, 1, (short) 4); ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, testTopicName); Collection ops = new ArrayList<>(); diff --git a/server/src/test/java/org/apache/kafka/server/LogManagerIntegrationTest.java b/server/src/test/java/org/apache/kafka/server/LogManagerIntegrationTest.java index 36f035c80f7db..4dda00077911b 100644 --- a/server/src/test/java/org/apache/kafka/server/LogManagerIntegrationTest.java +++ b/server/src/test/java/org/apache/kafka/server/LogManagerIntegrationTest.java @@ -17,7 +17,6 @@ package org.apache.kafka.server; import org.apache.kafka.clients.admin.Admin; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.clients.consumer.ConsumerRecord; @@ -61,10 +60,7 @@ public LogManagerIntegrationTest(ClusterInstance cluster) { @ClusterTest(types = {Type.KRAFT}) public void testIOExceptionOnLogSegmentCloseResultsInRecovery() throws IOException, InterruptedException, ExecutionException { - try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic("foo", 1, (short) 1))).all().get(); - } - cluster.waitTopicCreation("foo", 1); + cluster.createTopic("foo", 1, (short) 1); // Produce some data into the topic Map producerConfigs = Map.of( @@ -126,10 +122,7 @@ public void testIOExceptionOnLogSegmentCloseResultsInRecovery() throws IOExcepti @ClusterTest(types = {Type.KRAFT, Type.CO_KRAFT}, brokers = 3) public void testRestartBrokerNoErrorIfMissingPartitionMetadata() throws IOException, ExecutionException, InterruptedException { - try (Admin admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic("foo", 1, (short) 3))).all().get(); - } - cluster.waitTopicCreation("foo", 1); + cluster.createTopic("foo", 1, (short) 3); Optional partitionMetadataFile = cluster.brokers().get(0).logManager() .getLog(new TopicPartition("foo", 0), false).get() diff --git a/server/src/test/java/org/apache/kafka/server/UncleanLeaderElectionTest.java b/server/src/test/java/org/apache/kafka/server/UncleanLeaderElectionTest.java index aa7ee17577fb6..171cdf4c99a70 100644 --- a/server/src/test/java/org/apache/kafka/server/UncleanLeaderElectionTest.java +++ b/server/src/test/java/org/apache/kafka/server/UncleanLeaderElectionTest.java @@ -143,8 +143,7 @@ private void testUncleanLeaderElectionEnabled(GroupProtocol groupProtocol) throw disableEligibleLeaderReplicas(); // create topic with 1 partition, 2 replicas, one on each broker - NewTopic newTopic = new NewTopic(TOPIC, Map.of(PARTITION_ID, List.of(BROKER_ID_0, BROKER_ID_1))); - admin.createTopics(List.of(newTopic)).all().get(); + cluster.createTopicWithAssignment(TOPIC, Map.of(PARTITION_ID, List.of(BROKER_ID_0, BROKER_ID_1))); verifyUncleanLeaderElectionEnabled(groupProtocol); } @@ -164,8 +163,7 @@ private void testUncleanLeaderElectionDisabled(GroupProtocol groupProtocol) thro disableEligibleLeaderReplicas(); // create topic with 1 partition, 2 replicas, one on each broker - NewTopic newTopic = new NewTopic(TOPIC, Map.of(PARTITION_ID, List.of(BROKER_ID_0, BROKER_ID_1))); - admin.createTopics(List.of(newTopic)).all().get(); + cluster.createTopicWithAssignment(TOPIC, Map.of(PARTITION_ID, List.of(BROKER_ID_0, BROKER_ID_1))); verifyUncleanLeaderElectionDisabled(groupProtocol); } @@ -184,9 +182,8 @@ private void testUncleanLeaderElectionEnabledByTopicOverride(GroupProtocol group disableEligibleLeaderReplicas(); // create topic with 1 partition, 2 replicas, one on each broker, and unclean leader election enabled - NewTopic newTopic = new NewTopic(TOPIC, Map.of(PARTITION_ID, List.of(BROKER_ID_0, BROKER_ID_1))) - .configs(Map.of(TopicConfig.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG, "true")); - admin.createTopics(List.of(newTopic)).all().get(); + cluster.createTopicWithAssignment(TOPIC, Map.of(PARTITION_ID, List.of(BROKER_ID_0, BROKER_ID_1)), + Map.of(TopicConfig.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG, "true")); verifyUncleanLeaderElectionEnabled(groupProtocol); } @@ -213,9 +210,8 @@ private void testUncleanLeaderElectionDisabledByTopicOverride(GroupProtocol grou disableEligibleLeaderReplicas(); // create topic with 1 partition, 2 replicas, one on each broker, and unclean leader election disabled - NewTopic newTopic = new NewTopic(TOPIC, Map.of(PARTITION_ID, List.of(BROKER_ID_0, BROKER_ID_1))) - .configs(Map.of(TopicConfig.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG, "false")); - admin.createTopics(List.of(newTopic)).all().get(); + cluster.createTopicWithAssignment(TOPIC, Map.of(PARTITION_ID, List.of(BROKER_ID_0, BROKER_ID_1)), + Map.of(TopicConfig.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG, "false")); verifyUncleanLeaderElectionDisabled(groupProtocol); } @@ -377,8 +373,7 @@ private void testTopicUncleanLeaderElectionEnableWithAlterTopicConfigs(GroupProt disableEligibleLeaderReplicas(); // create topic with 1 partition, 2 replicas, one on each broker - NewTopic newTopic = new NewTopic(TOPIC, Map.of(PARTITION_ID, List.of(BROKER_ID_0, BROKER_ID_1))); - admin.createTopics(List.of(newTopic)).all().get(); + cluster.createTopicWithAssignment(TOPIC, Map.of(PARTITION_ID, List.of(BROKER_ID_0, BROKER_ID_1))); // wait until leader is elected int leaderId = awaitLeaderChange(cluster, TOPIC_PARTITION, Optional.empty()); diff --git a/storage/src/test/java/org/apache/kafka/admin/RemoteTopicCrudTest.java b/storage/src/test/java/org/apache/kafka/admin/RemoteTopicCrudTest.java index e5b06e05d09e0..0dc2e6e0ebe35 100644 --- a/storage/src/test/java/org/apache/kafka/admin/RemoteTopicCrudTest.java +++ b/storage/src/test/java/org/apache/kafka/admin/RemoteTopicCrudTest.java @@ -56,11 +56,9 @@ import java.util.Optional; import java.util.Set; import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import static org.apache.kafka.test.TestUtils.assertFutureThrows; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -97,15 +95,13 @@ void setUp(TestInfo info) { } @ClusterTest - void testCreateRemoteTopicWithValidRetentionTime() { - try (var admin = cluster.admin()) { - var topicConfig = Map.of( - TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, "true", - TopicConfig.RETENTION_MS_CONFIG, "60000", - TopicConfig.LOCAL_LOG_RETENTION_MS_CONFIG, "30000" - ); - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor).configs(topicConfig))); - } + void testCreateRemoteTopicWithValidRetentionTime() throws InterruptedException { + var topicConfig = Map.of( + TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, "true", + TopicConfig.RETENTION_MS_CONFIG, "60000", + TopicConfig.LOCAL_LOG_RETENTION_MS_CONFIG, "30000" + ); + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); } @ClusterTest @@ -115,9 +111,7 @@ void testCreateRemoteTopicWithValidRetentionSize() throws Exception { TopicConfig.RETENTION_BYTES_CONFIG, "512", TopicConfig.LOCAL_LOG_RETENTION_BYTES_CONFIG, "256" ); - try (var admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor).configs(topicConfig))); - } + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); verifyRemoteLogTopicConfigs(topicConfig); } @@ -129,9 +123,7 @@ void testCreateRemoteTopicWithInheritedLocalRetentionTime() throws Exception { TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, "true", TopicConfig.RETENTION_MS_CONFIG, "1001" ); - try (var admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor).configs(topicConfig))); - } + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); verifyRemoteLogTopicConfigs(topicConfig); } @@ -142,9 +134,7 @@ void testCreateRemoteTopicWithInheritedLocalRetentionSize() throws Exception { TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, "true", TopicConfig.RETENTION_BYTES_CONFIG, "1025" ); - try (var admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor).configs(topicConfig))); - } + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); verifyRemoteLogTopicConfigs(topicConfig); } @@ -210,11 +200,7 @@ void testCreateRemoteTopicWithCopyDisabledAndDeleteOnDisable() throws Exception TopicConfig.REMOTE_LOG_DELETE_ON_DISABLE_CONFIG, cluster.config().serverProperties().get(TopicConfig.REMOTE_LOG_DELETE_ON_DISABLE_CONFIG) ); - try (var admin = cluster.admin()) { - var result = admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor).configs(topicConfig))); - assertDoesNotThrow(() -> result.all().get(30, TimeUnit.SECONDS)); - } - + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); verifyRemoteLogTopicConfigs(topicConfig); } @@ -241,11 +227,11 @@ void testCreateTopicRetentionMsValidationWithRemoteCopyDisabled() throws Excepti // 2. change the local.retention.ms value to the same value as retention.ms should successfully create the topic topicConfig.put(TopicConfig.LOCAL_LOG_RETENTION_MS_CONFIG, "1000"); - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor).configs(topicConfig))).all().get(); + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); // 3. change the local.retention.ms value to "-2" should also successfully create the topic topicConfig.put(TopicConfig.LOCAL_LOG_RETENTION_MS_CONFIG, "-2"); - admin.createTopics(List.of(new NewTopic(testTopicName2, numPartitions, numReplicationFactor).configs(topicConfig))).values().get(testTopicName2).get(); + cluster.createTopic(testTopicName2, numPartitions, numReplicationFactor, topicConfig); // 4. create a topic with `remote.log.copy.disable=false` and have different local.retention.ms and retention.ms value, // it should successfully creates the topic. @@ -254,7 +240,7 @@ void testCreateTopicRetentionMsValidationWithRemoteCopyDisabled() throws Excepti topicConfig.put(TopicConfig.LOCAL_LOG_RETENTION_MS_CONFIG, "100"); topicConfig.put(TopicConfig.RETENTION_MS_CONFIG, "1000"); topicConfig.put(TopicConfig.LOCAL_LOG_RETENTION_BYTES_CONFIG, "-2"); - admin.createTopics(List.of(new NewTopic(testTopicName3, numPartitions, numReplicationFactor).configs(topicConfig))).values().get(testTopicName3).get(); + cluster.createTopic(testTopicName3, numPartitions, numReplicationFactor, topicConfig); // 5. alter the config to `remote.log.copy.disable=true`, it should fail the config change var configs = new HashMap>(); @@ -303,11 +289,11 @@ void testCreateTopicRetentionBytesValidationWithRemoteCopyDisabled() throws Exce // 2. change the local.retention.bytes value to the same value as retention.bytes should successfully create the topic topicConfig.put(TopicConfig.LOCAL_LOG_RETENTION_BYTES_CONFIG, "1000"); - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor).configs(topicConfig))).all().get(); + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); // 3. change the local.retention.bytes value to "-2" should also successfully create the topic topicConfig.put(TopicConfig.LOCAL_LOG_RETENTION_BYTES_CONFIG, "-2"); - admin.createTopics(List.of(new NewTopic(testTopicName2, numPartitions, numReplicationFactor).configs(topicConfig))).values().get(testTopicName2).get(); + cluster.createTopic(testTopicName2, numPartitions, numReplicationFactor, topicConfig); // 4. create a topic with `remote.log.copy.disable=false` and have different local.retention.bytes and retention.bytes value, // it should successfully creates the topic. @@ -316,7 +302,7 @@ void testCreateTopicRetentionBytesValidationWithRemoteCopyDisabled() throws Exce topicConfig.put(TopicConfig.LOCAL_LOG_RETENTION_BYTES_CONFIG, "100"); topicConfig.put(TopicConfig.RETENTION_BYTES_CONFIG, "1000"); topicConfig.put(TopicConfig.LOCAL_LOG_RETENTION_MS_CONFIG, "-2"); - admin.createTopics(List.of(new NewTopic(testTopicName3, numPartitions, numReplicationFactor).configs(topicConfig))).values().get(testTopicName3).get(); + cluster.createTopic(testTopicName3, numPartitions, numReplicationFactor, topicConfig); // 5. alter the config to `remote.log.copy.disable=true`, it should fail the config change var configs = new HashMap>(); @@ -343,7 +329,7 @@ void testCreateTopicRetentionBytesValidationWithRemoteCopyDisabled() throws Exce @ClusterTest void testEnableRemoteLogOnExistingTopic() throws Exception { try (var admin = cluster.admin()) { - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor).configs(Map.of()))).all().get(); + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, Map.of()); var configs = new HashMap>(); configs.put(new ConfigResource(ConfigResource.Type.TOPIC, testTopicName), @@ -365,7 +351,7 @@ void testEnableRemoteLogWhenSystemRemoteStorageIsDisabled() throws ExecutionExce var error = assertFutureThrows(InvalidConfigurationException.class, admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor).configs(topicConfig))).all()); assertTrue(Objects.requireNonNull(error).getMessage().contains("Tiered Storage functionality is disabled in the broker")); - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor))).all().get(); + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor); var configs = new HashMap>(); configs.put(new ConfigResource(ConfigResource.Type.TOPIC, testTopicName), @@ -384,8 +370,7 @@ void testUpdateTopicConfigWithValidRetentionTime() throws Exception { var topicConfig = Map.of( TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, "true" ); - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor) - .configs(topicConfig))).all().get(); + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); var configs = new HashMap>(); configs.put(new ConfigResource(ConfigResource.Type.TOPIC, testTopicName), @@ -407,8 +392,7 @@ void testUpdateTopicConfigWithValidRetentionSize() throws Exception { TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, "true" ); - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor) - .configs(topicConfig))).all().get(); + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); var configs = new HashMap>(); configs.put(new ConfigResource(ConfigResource.Type.TOPIC, testTopicName), @@ -430,8 +414,7 @@ void testUpdateTopicConfigWithInheritedLocalRetentionTime() throws Exception { TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, "true" ); - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor) - .configs(topicConfig))).all().get(); + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); // inherited local retention ms is 1000 var configs = new HashMap>(); @@ -452,8 +435,7 @@ void testUpdateTopicConfigWithInheritedLocalRetentionSize() throws Exception { TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, "true" ); - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor) - .configs(topicConfig))).all().get(); + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); // inherited local retention bytes is 1024 var configs = new HashMap>(); @@ -474,8 +456,7 @@ void testUpdateTopicConfigWithDisablingRemoteStorage() throws Exception { TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, "true" ); - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor) - .configs(topicConfig))).all().get(); + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); var configs = new HashMap>(); configs.put(new ConfigResource(ConfigResource.Type.TOPIC, testTopicName), @@ -500,8 +481,7 @@ void testUpdateTopicConfigWithDisablingRemoteStorageWithDeleteOnDisable() throws TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, "true" ); - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor) - .configs(topicConfig))).all().get(); + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); var configs = new HashMap>(); configs.put(new ConfigResource(ConfigResource.Type.TOPIC, testTopicName), @@ -536,8 +516,7 @@ void testTopicDeletion() throws Exception { TopicConfig.RETENTION_MS_CONFIG, "200", TopicConfig.LOCAL_LOG_RETENTION_MS_CONFIG, "100" ); - admin.createTopics(List.of(new NewTopic(testTopicName, numPartitions, numReplicationFactor) - .configs(topicConfig))).all().get(); + cluster.createTopic(testTopicName, numPartitions, numReplicationFactor, topicConfig); admin.deleteTopics(List.of(testTopicName)).all().get(); diff --git a/storage/src/test/java/org/apache/kafka/server/log/LogAppendTimeTest.java b/storage/src/test/java/org/apache/kafka/server/log/LogAppendTimeTest.java index bd26f99848be8..ae31f03012600 100644 --- a/storage/src/test/java/org/apache/kafka/server/log/LogAppendTimeTest.java +++ b/storage/src/test/java/org/apache/kafka/server/log/LogAppendTimeTest.java @@ -16,8 +16,6 @@ */ package org.apache.kafka.server.log; -import org.apache.kafka.clients.admin.Admin; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.clients.consumer.ConsumerRecord; @@ -73,12 +71,8 @@ public void testProduceConsumeWithConfigOnBroker(ClusterInstance clusterInstance } ) public void testProduceConsumeWithConfigOnTopic(ClusterInstance clusterInstance) throws InterruptedException { - try (Admin admin = clusterInstance.admin()) { - admin.createTopics(List.of( - new NewTopic(TOPIC, NUM_PARTITION, NUM_REPLICAS). - configs(Map.of(TopicConfig.MESSAGE_TIMESTAMP_TYPE_CONFIG, "LogAppendTime")))); - clusterInstance.waitTopicCreation(TOPIC, NUM_PARTITION); - } + clusterInstance.createTopic(TOPIC, NUM_PARTITION, NUM_REPLICAS, + Map.of(TopicConfig.MESSAGE_TIMESTAMP_TYPE_CONFIG, "LogAppendTime")); testProduceConsume(clusterInstance); } diff --git a/storage/src/test/java/org/apache/kafka/server/log/remote/metadata/storage/TopicBasedRemoteLogMetadataManagerMultipleSubscriptionsTest.java b/storage/src/test/java/org/apache/kafka/server/log/remote/metadata/storage/TopicBasedRemoteLogMetadataManagerMultipleSubscriptionsTest.java index 62befa0f6e3d5..c4288bdfa7092 100644 --- a/storage/src/test/java/org/apache/kafka/server/log/remote/metadata/storage/TopicBasedRemoteLogMetadataManagerMultipleSubscriptionsTest.java +++ b/storage/src/test/java/org/apache/kafka/server/log/remote/metadata/storage/TopicBasedRemoteLogMetadataManagerMultipleSubscriptionsTest.java @@ -17,9 +17,6 @@ package org.apache.kafka.server.log.remote.metadata.storage; -import org.apache.kafka.clients.CommonClientConfigs; -import org.apache.kafka.clients.admin.Admin; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.common.TopicIdPartition; import org.apache.kafka.common.TopicPartition; import org.apache.kafka.common.Uuid; @@ -171,9 +168,6 @@ public int metadataPartition(TopicIdPartition topicIdPartition) { } private void createTopic(String topic, Map> replicasAssignments) { - try (Admin admin = Admin.create(Map.of(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, clusterInstance.bootstrapServers()))) { - admin.createTopics(List.of(new NewTopic(topic, replicasAssignments))); - assertDoesNotThrow(() -> clusterInstance.waitTopicCreation(topic, replicasAssignments.size())); - } + assertDoesNotThrow(() -> clusterInstance.createTopicWithAssignment(topic, replicasAssignments)); } } diff --git a/storage/src/test/java/org/apache/kafka/server/log/remote/metadata/storage/TopicBasedRemoteLogMetadataManagerRestartTest.java b/storage/src/test/java/org/apache/kafka/server/log/remote/metadata/storage/TopicBasedRemoteLogMetadataManagerRestartTest.java index a407882e42a4d..2304fe0407c89 100644 --- a/storage/src/test/java/org/apache/kafka/server/log/remote/metadata/storage/TopicBasedRemoteLogMetadataManagerRestartTest.java +++ b/storage/src/test/java/org/apache/kafka/server/log/remote/metadata/storage/TopicBasedRemoteLogMetadataManagerRestartTest.java @@ -16,8 +16,6 @@ */ package org.apache.kafka.server.log.remote.metadata.storage; -import org.apache.kafka.clients.admin.Admin; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.common.TopicIdPartition; import org.apache.kafka.common.TopicPartition; import org.apache.kafka.common.Uuid; @@ -58,15 +56,10 @@ public void testRLMMAPIsAfterRestart() throws Exception { // Create topics. String leaderTopic = "new-leader"; String followerTopic = "new-follower"; - try (Admin admin = clusterInstance.admin()) { - // Set broker id 0 as the first entry which is taken as the leader. - NewTopic newLeaderTopic = new NewTopic(leaderTopic, Map.of(0, List.of(0, 1, 2))); - // Set broker id 1 as the first entry which is taken as the leader. - NewTopic newFollowerTopic = new NewTopic(followerTopic, Map.of(0, List.of(1, 2, 0))); - admin.createTopics(List.of(newLeaderTopic, newFollowerTopic)).all().get(); - } - clusterInstance.waitTopicCreation(leaderTopic, 1); - clusterInstance.waitTopicCreation(followerTopic, 1); + // Set broker id 0 as the first entry which is taken as the leader. + clusterInstance.createTopicWithAssignment(leaderTopic, Map.of(0, List.of(0, 1, 2))); + // Set broker id 1 as the first entry which is taken as the leader. + clusterInstance.createTopicWithAssignment(followerTopic, Map.of(0, List.of(1, 2, 0))); TopicIdPartition leaderTopicIdPartition = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition(leaderTopic, 0)); TopicIdPartition followerTopicIdPartition = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition(followerTopic, 0)); diff --git a/storage/src/test/java/org/apache/kafka/server/log/remote/metadata/storage/TopicBasedRemoteLogMetadataManagerTest.java b/storage/src/test/java/org/apache/kafka/server/log/remote/metadata/storage/TopicBasedRemoteLogMetadataManagerTest.java index 34c7b34473a48..f1aeb5d6f9173 100644 --- a/storage/src/test/java/org/apache/kafka/server/log/remote/metadata/storage/TopicBasedRemoteLogMetadataManagerTest.java +++ b/storage/src/test/java/org/apache/kafka/server/log/remote/metadata/storage/TopicBasedRemoteLogMetadataManagerTest.java @@ -21,7 +21,6 @@ import org.apache.kafka.clients.admin.ConfigEntry; import org.apache.kafka.clients.admin.DescribeConfigsResult; import org.apache.kafka.clients.admin.DescribeTopicsResult; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.admin.TopicDescription; import org.apache.kafka.common.KafkaFuture; import org.apache.kafka.common.TopicIdPartition; @@ -94,10 +93,9 @@ public void teardown() throws IOException { @ClusterTest public void testDoesTopicExist() throws ExecutionException, InterruptedException { + String topic = "test-topic-exist"; + clusterInstance.createTopic(topic, 1, (short) 1); try (Admin admin = clusterInstance.admin()) { - String topic = "test-topic-exist"; - admin.createTopics(List.of(new NewTopic(topic, 1, (short) 1))).all().get(); - clusterInstance.waitTopicCreation(topic, 1); boolean doesTopicExist = topicBasedRlmm().doesTopicExist(admin, topic); assertTrue(doesTopicExist); } @@ -144,13 +142,9 @@ public void testNewPartitionUpdates() throws Exception { // Create topics. String leaderTopic = "new-leader"; String followerTopic = "new-follower"; - try (Admin admin = clusterInstance.admin()) { - // Set broker id 0 as the first entry which is taken as the leader. - admin.createTopics(List.of(new NewTopic(leaderTopic, Map.of(0, List.of(0, 1, 2))))).all().get(); - clusterInstance.waitTopicCreation(leaderTopic, 1); - admin.createTopics(List.of(new NewTopic(followerTopic, Map.of(0, List.of(1, 2, 0))))).all().get(); - clusterInstance.waitTopicCreation(followerTopic, 1); - } + // Set broker id 0 as the first entry which is taken as the leader. + clusterInstance.createTopicWithAssignment(leaderTopic, Map.of(0, List.of(0, 1, 2))); + clusterInstance.createTopicWithAssignment(followerTopic, Map.of(0, List.of(1, 2, 0))); final TopicIdPartition newLeaderTopicIdPartition = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition(leaderTopic, 0)); final TopicIdPartition newFollowerTopicIdPartition = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition(followerTopic, 0)); diff --git a/storage/src/test/java/org/apache/kafka/tiered/storage/TieredStorageTestContext.java b/storage/src/test/java/org/apache/kafka/tiered/storage/TieredStorageTestContext.java index 0c12b564183a2..d80309c7f7f13 100644 --- a/storage/src/test/java/org/apache/kafka/tiered/storage/TieredStorageTestContext.java +++ b/storage/src/test/java/org/apache/kafka/tiered/storage/TieredStorageTestContext.java @@ -23,7 +23,6 @@ import org.apache.kafka.clients.admin.AlterConfigsOptions; import org.apache.kafka.clients.admin.ConfigEntry; import org.apache.kafka.clients.admin.NewPartitions; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.admin.TopicDescription; import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerConfig; @@ -143,17 +142,12 @@ private static List localStorages(Collection br .toList(); } - public void createTopic(TopicSpec spec) throws ExecutionException, InterruptedException { - NewTopic newTopic; + public void createTopic(TopicSpec spec) throws InterruptedException { if (spec.assignment() == null || spec.assignment().isEmpty()) { - newTopic = new NewTopic(spec.topicName(), spec.partitionCount(), (short) spec.replicationFactor()); + cluster.createTopic(spec.topicName(), spec.partitionCount(), (short) spec.replicationFactor(), spec.properties()); } else { - Map> replicasAssignments = spec.assignment(); - newTopic = new NewTopic(spec.topicName(), replicasAssignments); + cluster.createTopicWithAssignment(spec.topicName(), spec.assignment(), spec.properties()); } - newTopic.configs(spec.properties()); - admin.createTopics(List.of(newTopic)).all().get(); - cluster.waitTopicCreation(spec.topicName(), spec.partitionCount()); synchronized (this) { topicSpecs.put(spec.topicName(), spec); } diff --git a/test-common/test-common-runtime/src/main/java/org/apache/kafka/common/test/ClusterInstance.java b/test-common/test-common-runtime/src/main/java/org/apache/kafka/common/test/ClusterInstance.java index 3d28aa3272758..2fd63d771ef9d 100644 --- a/test-common/test-common-runtime/src/main/java/org/apache/kafka/common/test/ClusterInstance.java +++ b/test-common/test-common-runtime/src/main/java/org/apache/kafka/common/test/ClusterInstance.java @@ -341,8 +341,12 @@ default void createTopic(String topicName, int partitions, short replicas, Map> replicaAssignment) throws InterruptedException { + createTopicWithAssignment(topicName, replicaAssignment, Map.of()); + } + + default void createTopicWithAssignment(String topicName, Map> replicaAssignment, Map props) throws InterruptedException { try (Admin admin = admin()) { - admin.createTopics(List.of(new NewTopic(topicName, replicaAssignment))); + admin.createTopics(List.of(new NewTopic(topicName, replicaAssignment).configs(props))); int partitions = replicaAssignment.size(); waitTopicCreation(topicName, partitions); } diff --git a/test-common/test-common-runtime/src/test/java/org/apache/kafka/common/test/junit/ClusterTestExtensionsTest.java b/test-common/test-common-runtime/src/test/java/org/apache/kafka/common/test/junit/ClusterTestExtensionsTest.java index 8299bfeb55528..c23884491e431 100644 --- a/test-common/test-common-runtime/src/test/java/org/apache/kafka/common/test/junit/ClusterTestExtensionsTest.java +++ b/test-common/test-common-runtime/src/test/java/org/apache/kafka/common/test/junit/ClusterTestExtensionsTest.java @@ -25,7 +25,6 @@ import org.apache.kafka.clients.admin.AdminClientConfig; import org.apache.kafka.clients.admin.Config; import org.apache.kafka.clients.admin.DescribeLogDirsResult; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; @@ -301,10 +300,9 @@ public void testClusterAliveBrokers(ClusterInstance clusterInstance) throws Exce } ) public void testVerifyTopicDeletion(ClusterInstance clusterInstance) throws Exception { + String testTopic = "testTopic"; + clusterInstance.createTopic(testTopic, 1, (short) 1); try (Admin admin = clusterInstance.admin()) { - String testTopic = "testTopic"; - admin.createTopics(List.of(new NewTopic(testTopic, 1, (short) 1))); - clusterInstance.waitTopicCreation(testTopic, 1); admin.deleteTopics(List.of(testTopic)); clusterInstance.waitTopicDeletion(testTopic); Assertions.assertTrue(admin.listTopics().listings().get().stream().noneMatch( @@ -318,8 +316,8 @@ public void testCreateProducerAndConsumer(ClusterInstance cluster) throws Interr String topic = "topic"; String key = "key"; String value = "value"; - try (Admin adminClient = cluster.admin(); - Producer producer = cluster.producer(Map.of( + cluster.createTopic(topic, 1, (short) 1); + try (Producer producer = cluster.producer(Map.of( ACKS_CONFIG, "all", KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName(), VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName())); @@ -327,7 +325,6 @@ public void testCreateProducerAndConsumer(ClusterInstance cluster) throws Interr KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName(), VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName())) ) { - adminClient.createTopics(Set.of(new NewTopic(topic, 1, (short) 1))); assertNotNull(producer); assertNotNull(consumer); producer.send(new ProducerRecord<>(topic, key, value)); @@ -351,11 +348,10 @@ public void testCreateDefaultProducerAndConsumer(ClusterInstance cluster) throws String topic = "topic"; byte[] key = "key".getBytes(StandardCharsets.UTF_8); byte[] value = "value".getBytes(StandardCharsets.UTF_8); - try (Admin adminClient = cluster.admin(); - Producer producer = cluster.producer(); + cluster.createTopic(topic, 1, (short) 1); + try (Producer producer = cluster.producer(); Consumer consumer = cluster.consumer() ) { - adminClient.createTopics(Set.of(new NewTopic(topic, 1, (short) 1))); assertNotNull(producer); assertNotNull(consumer); producer.send(new ProducerRecord<>(topic, key, value)); @@ -382,14 +378,10 @@ public void testControllerListenerName(ClusterInstance cluster) throws Execution @ClusterTest(types = {Type.CO_KRAFT, Type.KRAFT}, brokers = 1) public void testBrokerRestart(ClusterInstance cluster) throws ExecutionException, InterruptedException { final String topicName = "topic"; - try (Admin admin = cluster.admin(); - Producer producer = cluster.producer(Map.of( + cluster.createTopic(topicName, 1, (short) 1); + try (Producer producer = cluster.producer(Map.of( ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName(), ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()))) { - admin.createTopics(List.of(new NewTopic(topicName, 1, (short) 1))).all().get(); - - cluster.waitTopicCreation(topicName, 1); - cluster.brokers().values().forEach(broker -> { broker.shutdown(); broker.awaitShutdown(); diff --git a/tools/src/test/java/org/apache/kafka/tools/ConfigCommandIntegrationTest.java b/tools/src/test/java/org/apache/kafka/tools/ConfigCommandIntegrationTest.java index 7dd92a38b102d..9cb11db06872e 100644 --- a/tools/src/test/java/org/apache/kafka/tools/ConfigCommandIntegrationTest.java +++ b/tools/src/test/java/org/apache/kafka/tools/ConfigCommandIntegrationTest.java @@ -23,7 +23,6 @@ import org.apache.kafka.clients.admin.AlterConfigsOptions; import org.apache.kafka.clients.admin.AlterConfigsResult; import org.apache.kafka.clients.admin.ConfigEntry; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.producer.Producer; import org.apache.kafka.common.config.ConfigResource; import org.apache.kafka.common.errors.InvalidConfigurationException; @@ -217,23 +216,19 @@ public void testNullStatusOnKraftCommandAlterClientMetrics() { @ClusterTest public void testAddConfigKeyValuesUsingCommand() throws Exception { - try (Admin client = cluster.admin()) { - NewTopic newTopic = new NewTopic("topic", 1, (short) 1); - client.createTopics(Set.of(newTopic)).all().get(); - cluster.waitTopicCreation("topic", 1); - Stream command = Stream.concat(quorumArgs(), Stream.of( - "--entity-type", "topics", - "--entity-name", "topic", - "--alter", "--add-config", "cleanup.policy=[delete,compact]")); - String message = captureStandardOut(run(command)); - assertEquals("Completed updating config for topic topic.", message); - command = Stream.concat(quorumArgs(), Stream.of( - "--entity-type", "topics", - "--entity-name", "topic", - "--describe")); - message = captureStandardOut(run(command)); - assertTrue(message.contains("cleanup.policy=delete,compact"), "Config entry was not added correctly"); - } + cluster.createTopic("topic", 1, (short) 1); + Stream command = Stream.concat(quorumArgs(), Stream.of( + "--entity-type", "topics", + "--entity-name", "topic", + "--alter", "--add-config", "cleanup.policy=[delete,compact]")); + String message = captureStandardOut(run(command)); + assertEquals("Completed updating config for topic topic.", message); + command = Stream.concat(quorumArgs(), Stream.of( + "--entity-type", "topics", + "--entity-name", "topic", + "--describe")); + message = captureStandardOut(run(command)); + assertTrue(message.contains("cleanup.policy=delete,compact"), "Config entry was not added correctly"); } @ClusterTest @@ -645,7 +640,7 @@ private void updateAndCheckInvalidBrokerConfig(Optional brokerIdOrDefaul public void testUpdateInvalidTopicConfigs() throws ExecutionException, InterruptedException { List alterOpts = List.of("--bootstrap-server", cluster.bootstrapServers(), "--entity-type", "topics", "--alter"); try (Admin client = cluster.admin()) { - client.createTopics(List.of(new NewTopic("test-config-topic", 1, (short) 1))).all().get(); + cluster.createTopic("test-config-topic", 1, (short) 1); assertInstanceOf( InvalidConfigurationException.class, assertThrows( @@ -664,7 +659,7 @@ public void testUpdateInvalidTopicConfigs() throws ExecutionException, Interrupt public void testDeleteNonExistentConfigIsIdempotent() throws Exception { String topicName = "test-delete-nonexistent-topic"; try (Admin client = cluster.admin()) { - client.createTopics(List.of(new NewTopic(topicName, 1, (short) 1))).all().get(); + cluster.createTopic(topicName, 1, (short) 1); ConfigCommand.alterConfig(client, new ConfigCommand.ConfigCommandOptions(toArray( List.of("--bootstrap-server", cluster.bootstrapServers(), @@ -686,9 +681,8 @@ public void testDeleteNonExistentConfigIsIdempotent() throws Exception { @ClusterTest public void testDeleteNonExistentConfigIsIdempotentWithBootstrapController() throws Exception { String topicName = "test-delete-nonexistent-topic"; - try (Admin bootstrapControllerClient = cluster.admin(Map.of(), true); - Admin bootstrapServerClient = cluster.admin(Map.of())) { - bootstrapServerClient.createTopics(List.of(new NewTopic(topicName, 1, (short) 1))).all().get(); + try (Admin bootstrapControllerClient = cluster.admin(Map.of(), true)) { + cluster.createTopic(topicName, 1, (short) 1); ConfigCommand.alterConfig(bootstrapControllerClient, new ConfigCommand.ConfigCommandOptions(toArray( List.of("--bootstrap-controller", cluster.bootstrapControllers(), "--entity-type", "topics", "--entity-name", topicName, diff --git a/tools/src/test/java/org/apache/kafka/tools/ConnectInternalTopicsTest.java b/tools/src/test/java/org/apache/kafka/tools/ConnectInternalTopicsTest.java index 89df8fb19a5c4..ce42f2816d389 100644 --- a/tools/src/test/java/org/apache/kafka/tools/ConnectInternalTopicsTest.java +++ b/tools/src/test/java/org/apache/kafka/tools/ConnectInternalTopicsTest.java @@ -17,7 +17,6 @@ package org.apache.kafka.tools; import org.apache.kafka.clients.admin.Admin; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.common.config.ConfigResource; import org.apache.kafka.common.test.ClusterInstance; import org.apache.kafka.common.test.api.ClusterTest; @@ -137,13 +136,7 @@ void testTopicConfigOverrides(ClusterInstance cluster) throws Exception { @ClusterTest(brokers = 3) void testCreateMissingTopics(ClusterInstance cluster) throws Exception { - try (var adminClient = cluster.admin()) { - adminClient.createTopics(Set.of( - new NewTopic(CONFIG_TOPIC_NAME, 1, (short) 1) - .configs(Map.of("retention.ms", "1000")) - )); - waitForTopics(adminClient, Set.of(CONFIG_TOPIC_NAME)); - } + cluster.createTopic(CONFIG_TOPIC_NAME, 1, (short) 1, Map.of("retention.ms", "1000")); var properties = new Properties(); properties.setProperty("bootstrap.servers", cluster.bootstrapServers()); properties.setProperty("config.storage.topic", CONFIG_TOPIC_NAME); diff --git a/tools/src/test/java/org/apache/kafka/tools/DeleteRecordsCommandTest.java b/tools/src/test/java/org/apache/kafka/tools/DeleteRecordsCommandTest.java index 73e9a8fb1e243..129d48a71b139 100644 --- a/tools/src/test/java/org/apache/kafka/tools/DeleteRecordsCommandTest.java +++ b/tools/src/test/java/org/apache/kafka/tools/DeleteRecordsCommandTest.java @@ -20,7 +20,6 @@ import org.apache.kafka.clients.admin.AdminClientConfig; import org.apache.kafka.clients.admin.DeleteRecordsResult; import org.apache.kafka.clients.admin.DeletedRecords; -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; @@ -43,7 +42,6 @@ import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -70,7 +68,7 @@ public void testCommand(ClusterInstance cluster) throws Exception { "Offset json file contains duplicate topic partitions: t-0" ); - admin.createTopics(Set.of(new NewTopic("t", 1, (short) 1))).all().get(); + cluster.createTopic("t", 1, (short) 1); Properties props = new Properties(); diff --git a/tools/src/test/java/org/apache/kafka/tools/LeaderElectionCommandTest.java b/tools/src/test/java/org/apache/kafka/tools/LeaderElectionCommandTest.java index 8e3c5fb729a84..c39c39d00127e 100644 --- a/tools/src/test/java/org/apache/kafka/tools/LeaderElectionCommandTest.java +++ b/tools/src/test/java/org/apache/kafka/tools/LeaderElectionCommandTest.java @@ -18,8 +18,6 @@ import org.apache.kafka.clients.admin.Admin; import org.apache.kafka.clients.admin.AdminClientConfig; -import org.apache.kafka.clients.admin.CreateTopicsResult; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.admin.TopicDescription; import org.apache.kafka.common.Node; import org.apache.kafka.common.TopicPartition; @@ -50,7 +48,6 @@ import java.util.Map; import java.util.Properties; import java.util.Set; -import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -89,9 +86,9 @@ public void testUncleanLeaderElectionInputModes(ClusterInstance cluster) throws cluster.waitForReadyBrokers(); try (Admin client = cluster.admin()) { - createTopic(client, topicPartitionTopic, Map.of(partition, assignment)); - createTopic(client, jsonFileTopic, Map.of(partition, assignment)); - createTopic(client, allTopicPartitionsTopic, Map.of(partition, assignment)); + cluster.createTopicWithAssignment(topicPartitionTopic, Map.of(partition, assignment)); + cluster.createTopicWithAssignment(jsonFileTopic, Map.of(partition, assignment)); + cluster.createTopicWithAssignment(allTopicPartitionsTopic, Map.of(partition, assignment)); for (TopicPartition partitionToCheck : topicPartitions) { assertLeader(client, partitionToCheck, broker2); @@ -215,7 +212,7 @@ public void testPreferredReplicaElection(ClusterInstance cluster) throws Excepti Map> partitionAssignment = new HashMap<>(); partitionAssignment.put(partition, assignment); - createTopic(client, topic, partitionAssignment); + cluster.createTopicWithAssignment(topic, partitionAssignment); TopicPartition topicPartition = new TopicPartition(topic, partition); @@ -265,7 +262,7 @@ public void testElectionResultOutput(ClusterInstance cluster) throws Exception { partitionAssignment.put(partition0, assignment0); partitionAssignment.put(partition1, assignment1); - createTopic(client, topic, partitionAssignment); + cluster.createTopicWithAssignment(topic, partitionAssignment); topicPartition0 = new TopicPartition(topic, partition0); topicPartition1 = new TopicPartition(topic, partition1); @@ -302,13 +299,6 @@ public void testElectionResultOutput(ClusterInstance cluster) throws Exception { String.format("Unexpected output: %s", secondLine)); } - private void createTopic(Admin admin, String topic, Map> replicaAssignment) throws ExecutionException, InterruptedException { - NewTopic newTopic = new NewTopic(topic, replicaAssignment); - List newTopics = List.of(newTopic); - CreateTopicsResult createTopicResult = admin.createTopics(newTopics); - createTopicResult.all().get(); - } - private Path tempTopicPartitionFile(List partitions) throws Exception { java.io.File file = TestUtils.tempFile("leader-election-command", ".json"); diff --git a/tools/src/test/java/org/apache/kafka/tools/LogDirsCommandTest.java b/tools/src/test/java/org/apache/kafka/tools/LogDirsCommandTest.java index 80bab74131f8e..37b8ca8355412 100644 --- a/tools/src/test/java/org/apache/kafka/tools/LogDirsCommandTest.java +++ b/tools/src/test/java/org/apache/kafka/tools/LogDirsCommandTest.java @@ -20,7 +20,6 @@ import org.apache.kafka.clients.admin.Admin; import org.apache.kafka.clients.admin.LogDirDescription; import org.apache.kafka.clients.admin.MockAdminClient; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.common.Node; import org.apache.kafka.common.TopicPartition; import org.apache.kafka.common.test.ClusterInstance; @@ -218,9 +217,6 @@ private String execute(LogDirsCommand.LogDirsCommandOptions options, Admin admin } private void createTopic(ClusterInstance clusterInstance, String topic) { - try (Admin admin = Admin.create(Map.of(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, clusterInstance.bootstrapServers()))) { - assertDoesNotThrow(() -> admin.createTopics(List.of(new NewTopic(topic, Map.of(0, List.of(0))))).topicId(topic).get()); - assertDoesNotThrow(() -> clusterInstance.waitTopicCreation(topic, 1)); - } + assertDoesNotThrow(() -> clusterInstance.createTopicWithAssignment(topic, Map.of(0, List.of(0)))); } } diff --git a/tools/src/test/java/org/apache/kafka/tools/TopicCommandTest.java b/tools/src/test/java/org/apache/kafka/tools/TopicCommandTest.java index 26645a75f7d75..9f0b148eaa431 100644 --- a/tools/src/test/java/org/apache/kafka/tools/TopicCommandTest.java +++ b/tools/src/test/java/org/apache/kafka/tools/TopicCommandTest.java @@ -383,11 +383,9 @@ static List generate() { ) public void testCreate(ClusterInstance clusterInstance) throws InterruptedException, ExecutionException { String testTopicName = TestUtils.randomString(10); + clusterInstance.createTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor); try (Admin adminClient = clusterInstance.admin()) { - adminClient.createTopics(List.of(new NewTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor))); - - clusterInstance.waitTopicCreation(testTopicName, defaultNumPartitions); Assertions.assertTrue(adminClient.listTopics().names().get().contains(testTopicName), "Admin client didn't see the created topic. It saw: " + adminClient.listTopics().names().get()); @@ -407,11 +405,9 @@ public void testCreate(ClusterInstance clusterInstance) throws InterruptedExcept ) public void testCreateWithDefaults(ClusterInstance clusterInstance) throws InterruptedException, ExecutionException { String testTopicName = TestUtils.randomString(10); + clusterInstance.createTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor); try (Admin adminClient = clusterInstance.admin()) { - adminClient.createTopics(List.of(new NewTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor))); - - clusterInstance.waitTopicCreation(testTopicName, defaultNumPartitions); Assertions.assertTrue(adminClient.listTopics().names().get().contains(testTopicName), "Admin client didn't see the created topic. It saw: " + adminClient.listTopics().names().get()); @@ -440,10 +436,9 @@ public void testCreateWithDefaults(ClusterInstance clusterInstance) throws Inter ) public void testCreateWithDefaultReplication(ClusterInstance clusterInstance) throws InterruptedException, ExecutionException { String testTopicName = TestUtils.randomString(10); + clusterInstance.createTopic(testTopicName, 2, defaultReplicationFactor); try (Admin adminClient = clusterInstance.admin()) { - adminClient.createTopics(List.of(new NewTopic(testTopicName, 2, defaultReplicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, 2); List partitions = adminClient .describeTopics(List.of(testTopicName)) .allTopicNames() @@ -458,10 +453,9 @@ public void testCreateWithDefaultReplication(ClusterInstance clusterInstance) th @ClusterTest(brokers = 3) public void testCreateWithDefaultPartitions(ClusterInstance clusterInstance) throws InterruptedException, ExecutionException { String testTopicName = TestUtils.randomString(10); + clusterInstance.createTopic(testTopicName, defaultNumPartitions, (short) 2); try (Admin adminClient = clusterInstance.admin()) { - adminClient.createTopics(List.of(new NewTopic(testTopicName, defaultNumPartitions, (short) 2))); - clusterInstance.waitTopicCreation(testTopicName, defaultNumPartitions); List partitions = adminClient .describeTopics(List.of(testTopicName)) .allTopicNames() @@ -477,16 +471,12 @@ public void testCreateWithDefaultPartitions(ClusterInstance clusterInstance) thr @ClusterTest(brokers = 3) public void testCreateWithConfigs(ClusterInstance clusterInstance) throws Exception { String testTopicName = TestUtils.randomString(10); + Map topicConfig = new HashMap<>(); + topicConfig.put(TopicConfig.DELETE_RETENTION_MS_CONFIG, "1000"); + clusterInstance.createTopic(testTopicName, 2, (short) 2, topicConfig); try (Admin adminClient = clusterInstance.admin()) { ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, testTopicName); - Map topicConfig = new HashMap<>(); - topicConfig.put(TopicConfig.DELETE_RETENTION_MS_CONFIG, "1000"); - - adminClient.createTopics(List.of(new NewTopic(testTopicName, 2, (short) 2).configs(topicConfig))); - clusterInstance.waitTopicCreation(testTopicName, 2); - - Config configs = adminClient.describeConfigs(Set.of(configResource)).all().get().get(configResource); assertEquals(1000, Integer.valueOf(configs.get("delete.retention.ms").value()), "Config not set correctly: " + configs.get("delete.retention.ms").value()); @@ -496,15 +486,13 @@ public void testCreateWithConfigs(ClusterInstance clusterInstance) throws Except @ClusterTest(brokers = 3) public void testCreateWhenAlreadyExists(ClusterInstance clusterInstance) throws Exception { String testTopicName = TestUtils.randomString(10); + clusterInstance.createTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor); try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { TopicCommand.TopicCommandOptions createOpts = buildTopicCommandOptionsWithBootstrap( clusterInstance, "--create", "--partitions", Integer.toString(defaultNumPartitions), "--replication-factor", "1", "--topic", testTopicName); - adminClient.createTopics(List.of(new NewTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, defaultNumPartitions); - // try to re-create the topic assertThrows(TopicExistsException.class, () -> topicService.createTopic(createOpts), "Expected TopicExistsException to throw"); @@ -514,11 +502,9 @@ public void testCreateWhenAlreadyExists(ClusterInstance clusterInstance) throws @ClusterTest(brokers = 3) public void testCreateWhenAlreadyExistsWithIfNotExists(ClusterInstance clusterInstance) throws Exception { String testTopicName = TestUtils.randomString(10); + clusterInstance.createTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor); try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { - adminClient.createTopics(List.of(new NewTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, defaultNumPartitions); - TopicCommand.TopicCommandOptions createOpts = buildTopicCommandOptionsWithBootstrap(clusterInstance, "--create", "--topic", testTopicName, "--if-not-exists"); topicService.createTopic(createOpts); @@ -532,16 +518,15 @@ private List getPartitionReplicas(List partitions, @ClusterTemplate("generate") public void testCreateWithReplicaAssignment(ClusterInstance clusterInstance) throws Exception { Map> replicaAssignmentMap = new HashMap<>(); - try (Admin adminClient = clusterInstance.admin()) { - String testTopicName = TestUtils.randomString(10); + String testTopicName = TestUtils.randomString(10); - replicaAssignmentMap.put(0, List.of(5, 4)); - replicaAssignmentMap.put(1, List.of(3, 2)); - replicaAssignmentMap.put(2, List.of(1, 0)); + replicaAssignmentMap.put(0, List.of(5, 4)); + replicaAssignmentMap.put(1, List.of(3, 2)); + replicaAssignmentMap.put(2, List.of(1, 0)); - adminClient.createTopics(List.of(new NewTopic(testTopicName, replicaAssignmentMap))); - clusterInstance.waitTopicCreation(testTopicName, 3); + clusterInstance.createTopicWithAssignment(testTopicName, replicaAssignmentMap); + try (Admin adminClient = clusterInstance.admin()) { List partitions = adminClient .describeTopics(List.of(testTopicName)) .allTopicNames() @@ -609,62 +594,50 @@ public void testInvalidTopicLevelConfig(ClusterInstance clusterInstance) { @ClusterTest public void testListTopics(ClusterInstance clusterInstance) throws InterruptedException { String testTopicName = TestUtils.randomString(10); - try (Admin adminClient = clusterInstance.admin()) { - adminClient.createTopics(List.of(new NewTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, defaultNumPartitions); + clusterInstance.createTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor); - String output = captureListTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--list")); - assertTrue(output.contains(testTopicName), "Expected topic name to be present in output: " + output); - } + String output = captureListTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--list")); + assertTrue(output.contains(testTopicName), "Expected topic name to be present in output: " + output); } @ClusterTest(brokers = 3) public void testListTopicsWithIncludeList(ClusterInstance clusterInstance) throws InterruptedException { - try (Admin adminClient = clusterInstance.admin()) { - String topic1 = "kafka.testTopic1"; - String topic2 = "kafka.testTopic2"; - String topic3 = "oooof.testTopic1"; - int partition = 2; - short replicationFactor = 2; - adminClient.createTopics(List.of(new NewTopic(topic1, partition, replicationFactor))); - adminClient.createTopics(List.of(new NewTopic(topic2, partition, replicationFactor))); - adminClient.createTopics(List.of(new NewTopic(topic3, partition, replicationFactor))); - clusterInstance.waitTopicCreation(topic1, partition); - clusterInstance.waitTopicCreation(topic2, partition); - clusterInstance.waitTopicCreation(topic3, partition); - - String output = captureListTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--list", "--topic", "kafka.*")); - assertTrue(output.contains(topic1), "Expected topic name " + topic1 + " to be present in output: " + output); - assertTrue(output.contains(topic2), "Expected topic name " + topic2 + " to be present in output: " + output); - assertFalse(output.contains(topic3), "Do not expect topic name " + topic3 + " to be present in output: " + output); - } + String topic1 = "kafka.testTopic1"; + String topic2 = "kafka.testTopic2"; + String topic3 = "oooof.testTopic1"; + int partition = 2; + short replicationFactor = 2; + clusterInstance.createTopic(topic1, partition, replicationFactor); + clusterInstance.createTopic(topic2, partition, replicationFactor); + clusterInstance.createTopic(topic3, partition, replicationFactor); + + String output = captureListTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--list", "--topic", "kafka.*")); + assertTrue(output.contains(topic1), "Expected topic name " + topic1 + " to be present in output: " + output); + assertTrue(output.contains(topic2), "Expected topic name " + topic2 + " to be present in output: " + output); + assertFalse(output.contains(topic3), "Do not expect topic name " + topic3 + " to be present in output: " + output); } @ClusterTest(brokers = 3) public void testListTopicsWithExcludeInternal(ClusterInstance clusterInstance) throws InterruptedException { - try (Admin adminClient = clusterInstance.admin()) { - String topic1 = "kafka.testTopic1"; - String hiddenConsumerTopic = Topic.GROUP_METADATA_TOPIC_NAME; - int partition = 2; - short replicationFactor = 2; - adminClient.createTopics(List.of(new NewTopic(topic1, partition, replicationFactor))); - clusterInstance.waitTopicCreation(topic1, partition); - - String output = captureListTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--list", "--exclude-internal")); - assertTrue(output.contains(topic1), "Expected topic name " + topic1 + " to be present in output: " + output); - assertFalse(output.contains(hiddenConsumerTopic), "Do not expect topic name " + hiddenConsumerTopic + " to be present in output: " + output); - } + String topic1 = "kafka.testTopic1"; + String hiddenConsumerTopic = Topic.GROUP_METADATA_TOPIC_NAME; + int partition = 2; + short replicationFactor = 2; + clusterInstance.createTopic(topic1, partition, replicationFactor); + + String output = captureListTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--list", "--exclude-internal")); + assertTrue(output.contains(topic1), "Expected topic name " + topic1 + " to be present in output: " + output); + assertFalse(output.contains(hiddenConsumerTopic), "Do not expect topic name " + hiddenConsumerTopic + " to be present in output: " + output); } @ClusterTest(brokers = 3) public void testAlterPartitionCount(ClusterInstance clusterInstance) throws Exception { String testTopicName = TestUtils.randomString(10); + int partition = 2; + short replicationFactor = 2; + clusterInstance.createTopic(testTopicName, partition, replicationFactor); try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { - int partition = 2; - short replicationFactor = 2; - adminClient.createTopics(List.of(new NewTopic(testTopicName, partition, replicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, partition); topicService.alterTopic(buildTopicCommandOptionsWithBootstrap(clusterInstance, "--alter", "--topic", testTopicName, "--partitions", "3")); TestUtils.waitForCondition( @@ -684,14 +657,11 @@ public void testAlterPartitionCount(ClusterInstance clusterInstance) throws Exce @ClusterTemplate("generate") public void testAlterAssignment(ClusterInstance clusterInstance) throws Exception { String testTopicName = TestUtils.randomString(10); + int partition = 2; + short replicationFactor = 2; + clusterInstance.createTopic(testTopicName, partition, replicationFactor); try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { - int partition = 2; - short replicationFactor = 2; - - adminClient.createTopics(List.of(new NewTopic(testTopicName, partition, replicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, partition); - topicService.alterTopic(buildTopicCommandOptionsWithBootstrap(clusterInstance, "--alter", "--topic", testTopicName, "--replica-assignment", "5:3,3:1,4:2", "--partitions", "3")); @@ -716,14 +686,11 @@ public void testAlterAssignment(ClusterInstance clusterInstance) throws Exceptio @ClusterTest(brokers = 3) public void testAlterAssignmentWithMoreAssignmentThanPartitions(ClusterInstance clusterInstance) throws Exception { String testTopicName = TestUtils.randomString(10); + int partition = 2; + short replicationFactor = 2; + clusterInstance.createTopic(testTopicName, partition, replicationFactor); try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { - - int partition = 2; - short replicationFactor = 2; - adminClient.createTopics(List.of(new NewTopic(testTopicName, partition, replicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, partition); - assertThrows(ExecutionException.class, () -> topicService.alterTopic(buildTopicCommandOptionsWithBootstrap(clusterInstance, "--alter", "--topic", testTopicName, "--replica-assignment", "5:3,3:1,4:2,3:2", "--partitions", "3")), @@ -735,13 +702,11 @@ public void testAlterAssignmentWithMoreAssignmentThanPartitions(ClusterInstance @ClusterTemplate("generate") public void testAlterAssignmentWithMorePartitionsThanAssignment(ClusterInstance clusterInstance) throws Exception { String testTopicName = TestUtils.randomString(10); + int partition = 2; + short replicationFactor = 2; + clusterInstance.createTopic(testTopicName, partition, replicationFactor); try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { - int partition = 2; - short replicationFactor = 2; - adminClient.createTopics(List.of(new NewTopic(testTopicName, partition, replicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, partition); - assertThrows(ExecutionException.class, () -> topicService.alterTopic(buildTopicCommandOptionsWithBootstrap(clusterInstance, "--alter", "--topic", testTopicName, "--replica-assignment", "5:3,3:1,4:2", "--partitions", "6")), @@ -753,12 +718,10 @@ public void testAlterAssignmentWithMorePartitionsThanAssignment(ClusterInstance @ClusterTest public void testAlterWithInvalidPartitionCount(ClusterInstance clusterInstance) throws Exception { String testTopicName = TestUtils.randomString(10); + clusterInstance.createTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor); try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { - adminClient.createTopics(List.of(new NewTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, defaultNumPartitions); - assertThrows(ExecutionException.class, () -> topicService.alterTopic(buildTopicCommandOptionsWithBootstrap(clusterInstance, "--alter", "--partitions", "-1", "--topic", testTopicName)), "Expected to fail with ExecutionException"); @@ -792,22 +755,20 @@ public void testAlterWhenTopicDoesntExistWithIfExists(ClusterInstance clusterIns @ClusterTemplate("generate") public void testCreateAlterTopicWithRackAware(ClusterInstance clusterInstance) throws Exception { String testTopicName = TestUtils.randomString(10); + Map rackInfo = new HashMap<>(); + rackInfo.put(0, "rack1"); + rackInfo.put(1, "rack2"); + rackInfo.put(2, "rack2"); + rackInfo.put(3, "rack1"); + rackInfo.put(4, "rack3"); + rackInfo.put(5, "rack3"); + + int numPartitions = 18; + int replicationFactor = 3; + clusterInstance.createTopic(testTopicName, numPartitions, (short) replicationFactor); + try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { - - Map rackInfo = new HashMap<>(); - rackInfo.put(0, "rack1"); - rackInfo.put(1, "rack2"); - rackInfo.put(2, "rack2"); - rackInfo.put(3, "rack1"); - rackInfo.put(4, "rack3"); - rackInfo.put(5, "rack3"); - - int numPartitions = 18; - int replicationFactor = 3; - adminClient.createTopics(List.of(new NewTopic(testTopicName, numPartitions, (short) replicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, numPartitions); - Map> assignment = adminClient.describeTopics(List.of(testTopicName)) .allTopicNames().get().get(testTopicName).partitions() .stream() @@ -844,15 +805,13 @@ public void testCreateAlterTopicWithRackAware(ClusterInstance clusterInstance) t @ClusterTest(brokers = 3) public void testConfigPreservationAcrossPartitionAlteration(ClusterInstance clusterInstance) throws Exception { String testTopicName = TestUtils.randomString(10); + String cleanUpPolicy = "compact"; + HashMap topicConfig = new HashMap<>(); + topicConfig.put(TopicConfig.CLEANUP_POLICY_CONFIG, cleanUpPolicy); + clusterInstance.createTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor, topicConfig); + try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { - - String cleanUpPolicy = "compact"; - HashMap topicConfig = new HashMap<>(); - topicConfig.put(TopicConfig.CLEANUP_POLICY_CONFIG, cleanUpPolicy); - adminClient.createTopics(List.of(new NewTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor).configs(topicConfig))); - clusterInstance.waitTopicCreation(testTopicName, defaultNumPartitions); - ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, testTopicName); Config props = adminClient.describeConfigs(Set.of(configResource)).all().get().get(configResource); assertNotNull(props.get(TopicConfig.CLEANUP_POLICY_CONFIG), "Properties after creation don't contain " + cleanUpPolicy); @@ -883,13 +842,10 @@ public void testConfigPreservationAcrossPartitionAlteration(ClusterInstance clus } ) public void testTopicDeletion(ClusterInstance clusterInstance) throws Exception { + String testTopicName = "testing"; + clusterInstance.createTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor); try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { - String testTopicName = "testing"; - - adminClient.createTopics(List.of(new NewTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, defaultNumPartitions); - // delete the NormalTopic TopicCommand.TopicCommandOptions deleteOpts = buildTopicCommandOptionsWithBootstrap(clusterInstance, "--delete", "--topic", testTopicName); topicService.deleteTopic(deleteOpts); @@ -909,13 +865,11 @@ public void testTopicDeletion(ClusterInstance clusterInstance) throws Exception } ) public void testTopicWithCollidingCharDeletionAndCreateAgain(ClusterInstance clusterInstance) throws Exception { + // create the topic with colliding chars + String topicWithCollidingChar = "test.a"; + clusterInstance.createTopic(topicWithCollidingChar, defaultNumPartitions, defaultReplicationFactor); try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { - // create the topic with colliding chars - String topicWithCollidingChar = "test.a"; - adminClient.createTopics(List.of(new NewTopic(topicWithCollidingChar, defaultNumPartitions, defaultReplicationFactor))); - clusterInstance.waitTopicCreation(topicWithCollidingChar, defaultNumPartitions); - // delete the topic TopicCommand.TopicCommandOptions deleteOpts = buildTopicCommandOptionsWithBootstrap(clusterInstance, "--delete", "--topic", topicWithCollidingChar); topicService.deleteTopic(deleteOpts); @@ -925,11 +879,10 @@ public void testTopicWithCollidingCharDeletionAndCreateAgain(ClusterInstance clu ); clusterInstance.waitTopicDeletion(topicWithCollidingChar); - - // recreate same topic - adminClient.createTopics(List.of(new NewTopic(topicWithCollidingChar, defaultNumPartitions, defaultReplicationFactor))); - clusterInstance.waitTopicCreation(topicWithCollidingChar, defaultNumPartitions); } + + // recreate same topic + clusterInstance.createTopic(topicWithCollidingChar, defaultNumPartitions, defaultReplicationFactor); } @ClusterTest( @@ -940,13 +893,10 @@ public void testTopicWithCollidingCharDeletionAndCreateAgain(ClusterInstance clu } ) public void testDeleteInternalTopic(ClusterInstance clusterInstance) throws Exception { + // create the offset topic + clusterInstance.createTopic(Topic.GROUP_METADATA_TOPIC_NAME, defaultNumPartitions, defaultReplicationFactor); try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { - - // create the offset topic - adminClient.createTopics(List.of(new NewTopic(Topic.GROUP_METADATA_TOPIC_NAME, defaultNumPartitions, defaultReplicationFactor))); - clusterInstance.waitTopicCreation(Topic.GROUP_METADATA_TOPIC_NAME, defaultNumPartitions); - // Try to delete the Topic.GROUP_METADATA_TOPIC_NAME which is allowed by default. // This is a difference between the new and the old command as the old one didn't allow internal topic deletion. // If deleting internal topics is not desired, ACLS should be used to control it. @@ -998,48 +948,33 @@ public void testDeleteWhenTopicDoesntExistWithIfExists(ClusterInstance clusterIn @ClusterTemplate("generate") public void testDescribe(ClusterInstance clusterInstance) throws InterruptedException { String testTopicName = TestUtils.randomString(10); - try (Admin adminClient = clusterInstance.admin()) { - int partition = 2; - short replicationFactor = 2; - adminClient.createTopics(List.of(new NewTopic(testTopicName, partition, replicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, partition); + int partition = 2; + short replicationFactor = 2; + clusterInstance.createTopic(testTopicName, partition, replicationFactor); - String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--topic", testTopicName)); - String[] rows = output.split(System.lineSeparator()); - assertEquals(3, rows.length, "Expected 3 rows in output, got " + rows.length); - assertTrue(rows[0].startsWith(String.format("Topic: %s", testTopicName)), "Row does not start with " + testTopicName + ". Row is: " + rows[0]); - } + String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--topic", testTopicName)); + String[] rows = output.split(System.lineSeparator()); + assertEquals(3, rows.length, "Expected 3 rows in output, got " + rows.length); + assertTrue(rows[0].startsWith(String.format("Topic: %s", testTopicName)), "Row does not start with " + testTopicName + ". Row is: " + rows[0]); } @ClusterTemplate("generate") public void testDescribeWithDescribeTopicPartitionsApi(ClusterInstance clusterInstance) throws InterruptedException { String testTopicName = TestUtils.randomString(10); - try (Admin adminClient = clusterInstance.admin()) { - - List topics = new ArrayList<>(); - topics.add(new NewTopic(testTopicName, 20, (short) 2)); - topics.add(new NewTopic("test-2", 41, (short) 2)); - topics.add(new NewTopic("test-3", 5, (short) 2)); - topics.add(new NewTopic("test-4", 5, (short) 2)); - topics.add(new NewTopic("test-5", 100, (short) 2)); - - adminClient.createTopics(topics); - clusterInstance.waitTopicCreation(testTopicName, 20); - clusterInstance.waitTopicCreation("test-2", 41); - clusterInstance.waitTopicCreation("test-3", 5); - clusterInstance.waitTopicCreation("test-4", 5); - clusterInstance.waitTopicCreation("test-5", 100); - - String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, - "--describe", "--partition-size-limit-per-response=20", "--exclude-internal")); - String[] rows = output.split("\n"); + clusterInstance.createTopic(testTopicName, 20, (short) 2); + clusterInstance.createTopic("test-2", 41, (short) 2); + clusterInstance.createTopic("test-3", 5, (short) 2); + clusterInstance.createTopic("test-4", 5, (short) 2); + clusterInstance.createTopic("test-5", 100, (short) 2); - assertEquals(176, rows.length, String.join("\n", rows)); - assertTrue(rows[2].contains("\tElr"), rows[2]); - assertTrue(rows[2].contains("LastKnownElr"), rows[2]); + String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, + "--describe", "--partition-size-limit-per-response=20", "--exclude-internal")); + String[] rows = output.split("\n"); - } + assertEquals(176, rows.length, String.join("\n", rows)); + assertTrue(rows[2].contains("\tElr"), rows[2]); + assertTrue(rows[2].contains("LastKnownElr"), rows[2]); } @ClusterTest @@ -1070,96 +1005,82 @@ public void testDescribeWhenTopicDoesntExistWithIfExists(ClusterInstance cluster @ClusterTest(brokers = 3) public void testDescribeUnavailablePartitions(ClusterInstance clusterInstance) throws InterruptedException { String testTopicName = TestUtils.randomString(10); + int partitions = 3; + short replicationFactor = 1; - try (Admin adminClient = clusterInstance.admin()) { - int partitions = 3; - short replicationFactor = 1; - - adminClient.createTopics(List.of(new NewTopic(testTopicName, partitions, replicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, partitions); + clusterInstance.createTopic(testTopicName, partitions, replicationFactor); - // check which partition is on broker 0 which we'll kill - clusterInstance.shutdownBroker(0); - assertEquals(2, clusterInstance.aliveBrokers().size()); - - // wait until the topic metadata for the test topic is propagated to each alive broker - clusterInstance.waitTopicCreation(testTopicName, 3); + // check which partition is on broker 0 which we'll kill + clusterInstance.shutdownBroker(0); + assertEquals(2, clusterInstance.aliveBrokers().size()); - // grab the console output and assert - String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--topic", testTopicName, "--unavailable-partitions")); - String[] rows = output.split(System.lineSeparator()); - assertTrue(rows[0].startsWith(String.format("Topic: %s", testTopicName)), - "Unexpected Topic " + rows[0] + " received. Expect " + String.format("Topic: %s", testTopicName)); - assertTrue(rows[0].contains("Leader: none\tReplicas: 0\tIsr:"), - "Rows did not contain 'Leader: none\tReplicas: 0\tIsr:'"); + // wait until the topic metadata for the test topic is propagated to each alive broker + clusterInstance.waitTopicCreation(testTopicName, 3); - } + // grab the console output and assert + String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--topic", testTopicName, "--unavailable-partitions")); + String[] rows = output.split(System.lineSeparator()); + assertTrue(rows[0].startsWith(String.format("Topic: %s", testTopicName)), + "Unexpected Topic " + rows[0] + " received. Expect " + String.format("Topic: %s", testTopicName)); + assertTrue(rows[0].contains("Leader: none\tReplicas: 0\tIsr:"), + "Rows did not contain 'Leader: none\tReplicas: 0\tIsr:'"); } @ClusterTest(brokers = 3) public void testDescribeUnderReplicatedPartitions(ClusterInstance clusterInstance) throws InterruptedException { String testTopicName = TestUtils.randomString(10); - try (Admin adminClient = clusterInstance.admin()) { - int partitions = 1; - short replicationFactor = 3; - adminClient.createTopics(List.of(new NewTopic(testTopicName, partitions, replicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, partitions); - - clusterInstance.shutdownBroker(0); - Assertions.assertEquals(2, clusterInstance.aliveBrokers().size()); - - TestUtils.waitForCondition( - () -> clusterInstance.aliveBrokers().values().stream().allMatch( - broker -> { - Optional partitionState = Optional.ofNullable( - broker.metadataCache().getLeaderAndIsr(testTopicName, 0).orElseGet(null)); - return partitionState.map(s -> FetchRequest.isValidBrokerId(s.leader())).orElse(false); - } - ), CLUSTER_WAIT_MS, String.format("Metadata propagation fail in %s ms", CLUSTER_WAIT_MS)); - - String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--under-replicated-partitions")); - String[] rows = output.split(System.lineSeparator()); - assertTrue(rows[0].startsWith(String.format("Topic: %s", testTopicName)), String.format("Unexpected output: %s", rows[0])); - } + int partitions = 1; + short replicationFactor = 3; + clusterInstance.createTopic(testTopicName, partitions, replicationFactor); + + clusterInstance.shutdownBroker(0); + Assertions.assertEquals(2, clusterInstance.aliveBrokers().size()); + + TestUtils.waitForCondition( + () -> clusterInstance.aliveBrokers().values().stream().allMatch( + broker -> { + Optional partitionState = Optional.ofNullable( + broker.metadataCache().getLeaderAndIsr(testTopicName, 0).orElseGet(null)); + return partitionState.map(s -> FetchRequest.isValidBrokerId(s.leader())).orElse(false); + } + ), CLUSTER_WAIT_MS, String.format("Metadata propagation fail in %s ms", CLUSTER_WAIT_MS)); + + String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--under-replicated-partitions")); + String[] rows = output.split(System.lineSeparator()); + assertTrue(rows[0].startsWith(String.format("Topic: %s", testTopicName)), String.format("Unexpected output: %s", rows[0])); } @ClusterTest(brokers = 3) public void testDescribeUnderMinIsrPartitions(ClusterInstance clusterInstance) throws InterruptedException { String testTopicName = TestUtils.randomString(10); + Map topicConfig = new HashMap<>(); + topicConfig.put(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, "3"); + int partitions = 1; + short replicationFactor = 3; + clusterInstance.createTopic(testTopicName, partitions, replicationFactor, topicConfig); + + clusterInstance.shutdownBroker(0); + assertEquals(2, clusterInstance.aliveBrokers().size()); + + TestUtils.waitForCondition( + () -> clusterInstance.aliveBrokers().values().stream().allMatch(broker -> broker.metadataCache().getLeaderAndIsr(testTopicName, 0).get().isr().size() == 2), + CLUSTER_WAIT_MS, String.format("Timeout waiting for partition metadata propagating to brokers for %s topic", testTopicName) + ); - try (Admin adminClient = clusterInstance.admin()) { - Map topicConfig = new HashMap<>(); - topicConfig.put(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, "3"); - int partitions = 1; - short replicationFactor = 3; - adminClient.createTopics(List.of(new NewTopic(testTopicName, partitions, replicationFactor).configs(topicConfig))); - clusterInstance.waitTopicCreation(testTopicName, partitions); - - clusterInstance.shutdownBroker(0); - assertEquals(2, clusterInstance.aliveBrokers().size()); - - TestUtils.waitForCondition( - () -> clusterInstance.aliveBrokers().values().stream().allMatch(broker -> broker.metadataCache().getLeaderAndIsr(testTopicName, 0).get().isr().size() == 2), - CLUSTER_WAIT_MS, String.format("Timeout waiting for partition metadata propagating to brokers for %s topic", testTopicName) - ); - - String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--under-min-isr-partitions", "--exclude-internal")); - String[] rows = output.split(System.lineSeparator()); - assertTrue(rows[0].startsWith(String.format("Topic: %s", testTopicName)), - "Unexpected topic: " + rows[0]); - } + String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--under-min-isr-partitions", "--exclude-internal")); + String[] rows = output.split(System.lineSeparator()); + assertTrue(rows[0].startsWith(String.format("Topic: %s", testTopicName)), + "Unexpected topic: " + rows[0]); } @ClusterTemplate("generate") public void testDescribeUnderReplicatedPartitionsWhenReassignmentIsInProgress(ClusterInstance clusterInstance) throws ExecutionException, InterruptedException { String testTopicName = TestUtils.randomString(10); + clusterInstance.createTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor); try (Admin adminClient = clusterInstance.admin(); KafkaProducer producer = createProducer(clusterInstance)) { - adminClient.createTopics(List.of(new NewTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, defaultNumPartitions); - TopicPartition tp = new TopicPartition(testTopicName, 0); // Produce multiple batches. @@ -1228,33 +1149,29 @@ public void testDescribeUnderReplicatedPartitionsWhenReassignmentIsInProgress(Cl @ClusterTemplate("generate") public void testDescribeAtMinIsrPartitions(ClusterInstance clusterInstance) throws InterruptedException { String testTopicName = TestUtils.randomString(10); + Map topicConfig = new HashMap<>(); + topicConfig.put(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, "4"); - try (Admin adminClient = clusterInstance.admin()) { - Map topicConfig = new HashMap<>(); - topicConfig.put(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, "4"); - - int partitions = 1; - short replicationFactor = 6; + int partitions = 1; + short replicationFactor = 6; - adminClient.createTopics(List.of(new NewTopic(testTopicName, partitions, replicationFactor).configs(topicConfig))); - clusterInstance.waitTopicCreation(testTopicName, partitions); + clusterInstance.createTopic(testTopicName, partitions, replicationFactor, topicConfig); - clusterInstance.shutdownBroker(0); - clusterInstance.shutdownBroker(1); - assertEquals(4, clusterInstance.aliveBrokers().size()); + clusterInstance.shutdownBroker(0); + clusterInstance.shutdownBroker(1); + assertEquals(4, clusterInstance.aliveBrokers().size()); - TestUtils.waitForCondition( - () -> clusterInstance.aliveBrokers().values().stream().allMatch(broker -> broker.metadataCache().getLeaderAndIsr(testTopicName, 0).get().isr().size() == 4), - CLUSTER_WAIT_MS, String.format("Timeout waiting for partition metadata propagating to brokers for %s topic", testTopicName) - ); + TestUtils.waitForCondition( + () -> clusterInstance.aliveBrokers().values().stream().allMatch(broker -> broker.metadataCache().getLeaderAndIsr(testTopicName, 0).get().isr().size() == 4), + CLUSTER_WAIT_MS, String.format("Timeout waiting for partition metadata propagating to brokers for %s topic", testTopicName) + ); - String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--at-min-isr-partitions", "--exclude-internal")); - String[] rows = output.split(System.lineSeparator()); - assertTrue(rows[0].startsWith(String.format("Topic: %s", testTopicName)), - "Unexpected output: " + rows[0]); - assertEquals(1, rows.length); - } + String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--at-min-isr-partitions", "--exclude-internal")); + String[] rows = output.split(System.lineSeparator()); + assertTrue(rows[0].startsWith(String.format("Topic: %s", testTopicName)), + "Unexpected output: " + rows[0]); + assertEquals(1, rows.length); } /** @@ -1276,8 +1193,6 @@ public void testDescribeUnderMinIsrPartitionsMixed(ClusterInstance clusterInstan int partitions = 1; short replicationFactor = 6; - List newTopics = new ArrayList<>(); - Map> fullyReplicatedReplicaAssignmentMap = new HashMap<>(); fullyReplicatedReplicaAssignmentMap.put(0, List.of(1, 2, 3)); @@ -1287,15 +1202,10 @@ public void testDescribeUnderMinIsrPartitionsMixed(ClusterInstance clusterInstan Map topicConfig = new HashMap<>(); topicConfig.put(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, "6"); - newTopics.add(new NewTopic(underMinIsrTopic, partitions, replicationFactor).configs(topicConfig)); - newTopics.add(new NewTopic(notUnderMinIsrTopic, partitions, replicationFactor)); - newTopics.add(new NewTopic(offlineTopic, offlineReplicaAssignmentMap)); - newTopics.add(new NewTopic(fullyReplicatedTopic, fullyReplicatedReplicaAssignmentMap)); - - adminClient.createTopics(newTopics); - for (NewTopic topioc: newTopics) { - clusterInstance.waitTopicCreation(topioc.name(), partitions); - } + clusterInstance.createTopic(underMinIsrTopic, partitions, replicationFactor, topicConfig); + clusterInstance.createTopic(notUnderMinIsrTopic, partitions, replicationFactor); + clusterInstance.createTopicWithAssignment(offlineTopic, offlineReplicaAssignmentMap); + clusterInstance.createTopicWithAssignment(fullyReplicatedTopic, fullyReplicatedReplicaAssignmentMap); clusterInstance.shutdownBroker(0); Assertions.assertEquals(5, clusterInstance.aliveBrokers().size()); @@ -1326,47 +1236,43 @@ public void testDescribeUnderMinIsrPartitionsMixed(ClusterInstance clusterInstan @ClusterTest(brokers = 3) public void testDescribeReportOverriddenConfigs(ClusterInstance clusterInstance) throws InterruptedException { String testTopicName = TestUtils.randomString(10); - try (Admin adminClient = clusterInstance.admin()) { - String config = "file.delete.delay.ms=1000"; - Map topicConfig = new HashMap<>(); - topicConfig.put(TopicConfig.FILE_DELETE_DELAY_MS_CONFIG, "1000"); + String config = "file.delete.delay.ms=1000"; + Map topicConfig = new HashMap<>(); + topicConfig.put(TopicConfig.FILE_DELETE_DELAY_MS_CONFIG, "1000"); - int partitions = 2; - short replicationFactor = 2; + int partitions = 2; + short replicationFactor = 2; - adminClient.createTopics(List.of(new NewTopic(testTopicName, partitions, replicationFactor).configs(topicConfig))); - clusterInstance.waitTopicCreation(testTopicName, partitions); + clusterInstance.createTopic(testTopicName, partitions, replicationFactor, topicConfig); - String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe")); - assertTrue(output.contains(config), String.format("Describe output should have contained %s", config)); - } + String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe")); + assertTrue(output.contains(config), String.format("Describe output should have contained %s", config)); } @ClusterTest public void testDescribeAndListTopicsWithoutInternalTopics(ClusterInstance clusterInstance) throws InterruptedException { String testTopicName = TestUtils.randomString(10); - try (Admin adminClient = clusterInstance.admin()) { - adminClient.createTopics(List.of(new NewTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, defaultNumPartitions); - - // test describe - String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--describe", "--exclude-internal")); - assertTrue(output.contains(testTopicName), - String.format("Output should have contained %s", testTopicName)); - assertFalse(output.contains(Topic.GROUP_METADATA_TOPIC_NAME), - "Output should not have contained " + Topic.GROUP_METADATA_TOPIC_NAME); - - // test list - output = captureListTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--list", "--exclude-internal")); - assertTrue(output.contains(testTopicName), String.format("Output should have contained %s", testTopicName)); - assertFalse(output.contains(Topic.GROUP_METADATA_TOPIC_NAME), - "Output should not have contained " + Topic.GROUP_METADATA_TOPIC_NAME); - } + clusterInstance.createTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor); + + // test describe + String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--describe", "--exclude-internal")); + assertTrue(output.contains(testTopicName), + String.format("Output should have contained %s", testTopicName)); + assertFalse(output.contains(Topic.GROUP_METADATA_TOPIC_NAME), + "Output should not have contained " + Topic.GROUP_METADATA_TOPIC_NAME); + + // test list + output = captureListTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--list", "--exclude-internal")); + assertTrue(output.contains(testTopicName), String.format("Output should have contained %s", testTopicName)); + assertFalse(output.contains(Topic.GROUP_METADATA_TOPIC_NAME), + "Output should not have contained " + Topic.GROUP_METADATA_TOPIC_NAME); } @ClusterTest public void testDescribeDoesNotFailWhenListingReassignmentIsUnauthorized(ClusterInstance clusterInstance) throws Exception { String testTopicName = TestUtils.randomString(10); + clusterInstance.createTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor); + Admin adminClient = clusterInstance.admin(); adminClient = spy(adminClient); @@ -1377,8 +1283,6 @@ public void testDescribeDoesNotFailWhenListingReassignmentIsUnauthorized(Cluster doReturn(result).when(adminClient).listPartitionReassignments( Set.of(new TopicPartition(testTopicName, 0)) ); - adminClient.createTopics(List.of(new NewTopic(testTopicName, defaultNumPartitions, defaultReplicationFactor))); - clusterInstance.waitTopicCreation(testTopicName, defaultNumPartitions); String output = captureDescribeTopicStandardOut(clusterInstance, buildTopicCommandOptionsWithBootstrap(clusterInstance, "--describe", "--topic", testTopicName)); String[] rows = output.split(System.lineSeparator()); @@ -1390,15 +1294,13 @@ public void testDescribeDoesNotFailWhenListingReassignmentIsUnauthorized(Cluster @ClusterTest(brokers = 3) public void testCreateWithTopicNameCollision(ClusterInstance clusterInstance) throws Exception { + String topic = "foo_bar"; + int partitions = 1; + short replicationFactor = 3; + clusterInstance.createTopic(topic, partitions, replicationFactor); + try (Admin adminClient = clusterInstance.admin(); TopicCommand.TopicService topicService = new TopicCommand.TopicService(adminClient)) { - - String topic = "foo_bar"; - int partitions = 1; - short replicationFactor = 3; - adminClient.createTopics(List.of(new NewTopic(topic, partitions, replicationFactor))); - clusterInstance.waitTopicCreation(topic, defaultNumPartitions); - assertThrows(TopicExistsException.class, () -> topicService.createTopic(buildTopicCommandOptionsWithBootstrap(clusterInstance, "--create", "--topic", topic))); diff --git a/tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleConsumerTest.java b/tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleConsumerTest.java index db4280b0dbd98..7f0779599778b 100644 --- a/tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleConsumerTest.java +++ b/tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleConsumerTest.java @@ -16,8 +16,6 @@ */ package org.apache.kafka.tools.consumer; -import org.apache.kafka.clients.admin.Admin; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; @@ -65,7 +63,6 @@ import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.Set; import java.util.regex.Pattern; import static org.apache.kafka.clients.CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG; @@ -294,80 +291,72 @@ public void shouldWorkWithoutTopicOption() throws IOException { @ClusterTest(brokers = 3) public void testTransactionLogMessageFormatter(ClusterInstance cluster) throws Exception { - try (Admin admin = cluster.admin()) { - - NewTopic newTopic = new NewTopic(topic, 1, (short) 1); - admin.createTopics(Set.of(newTopic)); - produceMessagesWithTxn(cluster); + cluster.createTopic(topic, 1, (short) 1); + produceMessagesWithTxn(cluster); - String[] transactionLogMessageFormatter = createConsoleConsumerArgs(cluster, - Topic.TRANSACTION_STATE_TOPIC_NAME, - "org.apache.kafka.tools.consumer.TransactionLogMessageFormatter"); + String[] transactionLogMessageFormatter = createConsoleConsumerArgs(cluster, + Topic.TRANSACTION_STATE_TOPIC_NAME, + "org.apache.kafka.tools.consumer.TransactionLogMessageFormatter"); - ConsoleConsumerOptions options = new ConsoleConsumerOptions(transactionLogMessageFormatter); - ConsoleConsumer.ConsumerWrapper consumerWrapper = new ConsoleConsumer.ConsumerWrapper(options, createTxnConsumer(cluster)); + ConsoleConsumerOptions options = new ConsoleConsumerOptions(transactionLogMessageFormatter); + ConsoleConsumer.ConsumerWrapper consumerWrapper = new ConsoleConsumer.ConsumerWrapper(options, createTxnConsumer(cluster)); + + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + PrintStream output = new PrintStream(out)) { + ConsoleConsumer.process(1, options.formatter(), consumerWrapper, output, true); - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); - PrintStream output = new PrintStream(out)) { - ConsoleConsumer.process(1, options.formatter(), consumerWrapper, output, true); - - JsonNode jsonNode = objectMapper.reader().readTree(out.toByteArray()); - JsonNode keyNode = jsonNode.get("key"); - - TransactionLogKey logKey = - TransactionLogKeyJsonConverter.read(keyNode.get("data"), TransactionLogKey.HIGHEST_SUPPORTED_VERSION); - assertNotNull(logKey); - assertEquals(transactionId, logKey.transactionalId()); - - JsonNode valueData = jsonNode.get("value").get("data"); - assertNotNull(valueData); - assertEquals(0, valueData.get("producerId").asInt()); - assertEquals(TransactionState.EMPTY.stateName(), valueData.get("transactionStatus").asText()); - } finally { - consumerWrapper.cleanup(); - } + JsonNode jsonNode = objectMapper.reader().readTree(out.toByteArray()); + JsonNode keyNode = jsonNode.get("key"); + + TransactionLogKey logKey = + TransactionLogKeyJsonConverter.read(keyNode.get("data"), TransactionLogKey.HIGHEST_SUPPORTED_VERSION); + assertNotNull(logKey); + assertEquals(transactionId, logKey.transactionalId()); + + JsonNode valueData = jsonNode.get("value").get("data"); + assertNotNull(valueData); + assertEquals(0, valueData.get("producerId").asInt()); + assertEquals(TransactionState.EMPTY.stateName(), valueData.get("transactionStatus").asText()); + } finally { + consumerWrapper.cleanup(); } } @ClusterTest(brokers = 3) public void testOffsetsMessageFormatter(ClusterInstance cluster) throws Exception { - try (Admin admin = cluster.admin()) { - - NewTopic newTopic = new NewTopic(topic, 1, (short) 1); - admin.createTopics(Set.of(newTopic)); - produceMessages(cluster); - - String[] offsetsMessageFormatter = createConsoleConsumerArgs(cluster, - Topic.GROUP_METADATA_TOPIC_NAME, - "org.apache.kafka.tools.consumer.OffsetsMessageFormatter"); - - ConsoleConsumerOptions options = new ConsoleConsumerOptions(offsetsMessageFormatter); - ConsoleConsumer.ConsumerWrapper consumerWrapper = new ConsoleConsumer.ConsumerWrapper(options, createOffsetConsumer(cluster)); - - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); - PrintStream output = new PrintStream(out)) { - ConsoleConsumer.process(1, options.formatter(), consumerWrapper, output, true); - - JsonNode jsonNode = objectMapper.reader().readTree(out.toByteArray()); - JsonNode keyNode = jsonNode.get("key"); - - OffsetCommitKey offsetCommitKey = - OffsetCommitKeyJsonConverter.read(keyNode.get("data"), OffsetCommitKey.HIGHEST_SUPPORTED_VERSION); - assertNotNull(offsetCommitKey); - assertEquals(Topic.GROUP_METADATA_TOPIC_NAME, offsetCommitKey.topic()); - assertEquals(groupId, offsetCommitKey.group()); - - JsonNode valueNode = jsonNode.get("value"); - OffsetCommitValue offsetCommitValue = - OffsetCommitValueJsonConverter.read(valueNode.get("data"), OffsetCommitValue.HIGHEST_SUPPORTED_VERSION); - assertNotNull(offsetCommitValue); - assertEquals(0, offsetCommitValue.offset()); - assertEquals(-1, offsetCommitValue.leaderEpoch()); - assertNotNull(offsetCommitValue.metadata()); - assertEquals(-1, offsetCommitValue.expireTimestamp()); - } finally { - consumerWrapper.cleanup(); - } + cluster.createTopic(topic, 1, (short) 1); + produceMessages(cluster); + + String[] offsetsMessageFormatter = createConsoleConsumerArgs(cluster, + Topic.GROUP_METADATA_TOPIC_NAME, + "org.apache.kafka.tools.consumer.OffsetsMessageFormatter"); + + ConsoleConsumerOptions options = new ConsoleConsumerOptions(offsetsMessageFormatter); + ConsoleConsumer.ConsumerWrapper consumerWrapper = new ConsoleConsumer.ConsumerWrapper(options, createOffsetConsumer(cluster)); + + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + PrintStream output = new PrintStream(out)) { + ConsoleConsumer.process(1, options.formatter(), consumerWrapper, output, true); + + JsonNode jsonNode = objectMapper.reader().readTree(out.toByteArray()); + JsonNode keyNode = jsonNode.get("key"); + + OffsetCommitKey offsetCommitKey = + OffsetCommitKeyJsonConverter.read(keyNode.get("data"), OffsetCommitKey.HIGHEST_SUPPORTED_VERSION); + assertNotNull(offsetCommitKey); + assertEquals(Topic.GROUP_METADATA_TOPIC_NAME, offsetCommitKey.topic()); + assertEquals(groupId, offsetCommitKey.group()); + + JsonNode valueNode = jsonNode.get("value"); + OffsetCommitValue offsetCommitValue = + OffsetCommitValueJsonConverter.read(valueNode.get("data"), OffsetCommitValue.HIGHEST_SUPPORTED_VERSION); + assertNotNull(offsetCommitValue); + assertEquals(0, offsetCommitValue.offset()); + assertEquals(-1, offsetCommitValue.leaderEpoch()); + assertNotNull(offsetCommitValue.metadata()); + assertEquals(-1, offsetCommitValue.expireTimestamp()); + } finally { + consumerWrapper.cleanup(); } } @@ -376,45 +365,41 @@ public void testOffsetsMessageFormatter(ClusterInstance cluster) throws Exceptio @ClusterConfigProperty(key = OFFSETS_TOPIC_REPLICATION_FACTOR_CONFIG, value = "1") }) public void testGroupMetadataMessageFormatterWithClassicGroupProtocol(ClusterInstance cluster) throws Exception { - try (Admin admin = cluster.admin()) { - - NewTopic newTopic = new NewTopic(topic, 1, (short) 1); - admin.createTopics(Set.of(newTopic)); - produceMessages(cluster); - - String[] groupMetadataMessageFormatter = createConsoleConsumerArgs(cluster, - Topic.GROUP_METADATA_TOPIC_NAME, - "org.apache.kafka.tools.consumer.GroupMetadataMessageFormatter"); - - ConsoleConsumerOptions options = new ConsoleConsumerOptions(groupMetadataMessageFormatter); - ConsoleConsumer.ConsumerWrapper consumerWrapper = - new ConsoleConsumer.ConsumerWrapper(options, createGroupMetadataConsumer(cluster, GroupProtocol.CLASSIC)); - - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); - PrintStream output = new PrintStream(out)) { - ConsoleConsumer.process(1, options.formatter(), consumerWrapper, output, true); - - JsonNode jsonNode = objectMapper.reader().readTree(out.toByteArray()); - - // The group coordinator writes an empty group metadata record when the group is created for the first time - JsonNode keyNode = jsonNode.get("key"); - GroupMetadataKey groupMetadataKey = - GroupMetadataKeyJsonConverter.read(keyNode.get("data"), GroupMetadataKey.HIGHEST_SUPPORTED_VERSION); - assertNotNull(groupMetadataKey); - assertEquals(groupId, groupMetadataKey.group()); - - JsonNode valueNode = jsonNode.get("value"); - GroupMetadataValue groupMetadataValue = - GroupMetadataValueJsonConverter.read(valueNode.get("data"), GroupMetadataValue.HIGHEST_SUPPORTED_VERSION); - assertNotNull(groupMetadataValue); - assertEquals("", groupMetadataValue.protocolType()); - assertEquals(0, groupMetadataValue.generation()); - assertNull(groupMetadataValue.protocol()); - assertNull(groupMetadataValue.leader()); - assertEquals(0, groupMetadataValue.members().size()); - } finally { - consumerWrapper.cleanup(); - } + cluster.createTopic(topic, 1, (short) 1); + produceMessages(cluster); + + String[] groupMetadataMessageFormatter = createConsoleConsumerArgs(cluster, + Topic.GROUP_METADATA_TOPIC_NAME, + "org.apache.kafka.tools.consumer.GroupMetadataMessageFormatter"); + + ConsoleConsumerOptions options = new ConsoleConsumerOptions(groupMetadataMessageFormatter); + ConsoleConsumer.ConsumerWrapper consumerWrapper = + new ConsoleConsumer.ConsumerWrapper(options, createGroupMetadataConsumer(cluster, GroupProtocol.CLASSIC)); + + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + PrintStream output = new PrintStream(out)) { + ConsoleConsumer.process(1, options.formatter(), consumerWrapper, output, true); + + JsonNode jsonNode = objectMapper.reader().readTree(out.toByteArray()); + + // The group coordinator writes an empty group metadata record when the group is created for the first time + JsonNode keyNode = jsonNode.get("key"); + GroupMetadataKey groupMetadataKey = + GroupMetadataKeyJsonConverter.read(keyNode.get("data"), GroupMetadataKey.HIGHEST_SUPPORTED_VERSION); + assertNotNull(groupMetadataKey); + assertEquals(groupId, groupMetadataKey.group()); + + JsonNode valueNode = jsonNode.get("value"); + GroupMetadataValue groupMetadataValue = + GroupMetadataValueJsonConverter.read(valueNode.get("data"), GroupMetadataValue.HIGHEST_SUPPORTED_VERSION); + assertNotNull(groupMetadataValue); + assertEquals("", groupMetadataValue.protocolType()); + assertEquals(0, groupMetadataValue.generation()); + assertNull(groupMetadataValue.protocol()); + assertNull(groupMetadataValue.leader()); + assertEquals(0, groupMetadataValue.members().size()); + } finally { + consumerWrapper.cleanup(); } } diff --git a/tools/src/test/java/org/apache/kafka/tools/consumer/group/ConsumerGroupCommandAuthorizationTest.java b/tools/src/test/java/org/apache/kafka/tools/consumer/group/ConsumerGroupCommandAuthorizationTest.java index eb20fefb7a19a..636d6c9f548ec 100644 --- a/tools/src/test/java/org/apache/kafka/tools/consumer/group/ConsumerGroupCommandAuthorizationTest.java +++ b/tools/src/test/java/org/apache/kafka/tools/consumer/group/ConsumerGroupCommandAuthorizationTest.java @@ -17,7 +17,6 @@ package org.apache.kafka.tools.consumer.group; import org.apache.kafka.clients.admin.Admin; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.common.acl.AccessControlEntry; import org.apache.kafka.common.acl.AccessControlEntryFilter; import org.apache.kafka.common.acl.AclBinding; @@ -86,11 +85,7 @@ public class ConsumerGroupCommandAuthorizationTest { public void testDescribeGroupCliWithGroupDescribe(ClusterInstance clusterInstance) throws Exception { addAndVerifyAcls(Set.of(createAcl(CREATE, CLIENT_PRINCIPAL)), OFFSETS_TOPIC_RESOURCE, clusterInstance); - NewTopic offsetTopic = new NewTopic(Topic.GROUP_METADATA_TOPIC_NAME, 1, (short) 1); - try (Admin admin = clusterInstance.admin()) { - admin.createTopics(List.of(offsetTopic)).all().get(); - clusterInstance.waitTopicCreation(Topic.GROUP_METADATA_TOPIC_NAME, 1); - } + clusterInstance.createTopic(Topic.GROUP_METADATA_TOPIC_NAME, 1, (short) 1); addAndVerifyAcls(Set.of(createAcl(DESCRIBE, CLIENT_PRINCIPAL)), GROUP_RESOURCE, clusterInstance); diff --git a/tools/src/test/java/org/apache/kafka/tools/consumer/group/DeleteOffsetsConsumerGroupCommandIntegrationTest.java b/tools/src/test/java/org/apache/kafka/tools/consumer/group/DeleteOffsetsConsumerGroupCommandIntegrationTest.java index 265b880e4ea3c..a1c8e6755df45 100644 --- a/tools/src/test/java/org/apache/kafka/tools/consumer/group/DeleteOffsetsConsumerGroupCommandIntegrationTest.java +++ b/tools/src/test/java/org/apache/kafka/tools/consumer/group/DeleteOffsetsConsumerGroupCommandIntegrationTest.java @@ -19,7 +19,6 @@ import org.apache.kafka.clients.CommonClientConfigs; import org.apache.kafka.clients.admin.Admin; import org.apache.kafka.clients.admin.AdminClientConfig; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.clients.consumer.ConsumerRecords; @@ -260,9 +259,7 @@ private Consumer createConsumer(String group, GroupProtocol grou } private void createTopic(String topic) { - try (Admin admin = Admin.create(Map.of(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, clusterInstance.bootstrapServers()))) { - Assertions.assertDoesNotThrow(() -> admin.createTopics(List.of(new NewTopic(topic, 1, (short) 1))).topicId(topic).get()); - } + Assertions.assertDoesNotThrow(() -> clusterInstance.createTopic(topic, 1, (short) 1)); } private void removeTopic(String topic) { diff --git a/tools/src/test/java/org/apache/kafka/tools/consumer/group/DescribeConsumerGroupTest.java b/tools/src/test/java/org/apache/kafka/tools/consumer/group/DescribeConsumerGroupTest.java index 695652df3d1f1..dc275fed0d2d0 100644 --- a/tools/src/test/java/org/apache/kafka/tools/consumer/group/DescribeConsumerGroupTest.java +++ b/tools/src/test/java/org/apache/kafka/tools/consumer/group/DescribeConsumerGroupTest.java @@ -21,7 +21,6 @@ import org.apache.kafka.clients.admin.AdminClientConfig; import org.apache.kafka.clients.admin.ConsumerGroupDescription; import org.apache.kafka.clients.admin.MemberDescription; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.clients.consumer.GroupProtocol; import org.apache.kafka.clients.consumer.KafkaConsumer; @@ -1174,9 +1173,7 @@ private void createTopic(String topic) { } private void createTopic(String topic, int numPartitions) { - try (Admin admin = Admin.create(Map.of(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, clusterInstance.bootstrapServers()))) { - Assertions.assertDoesNotThrow(() -> admin.createTopics(List.of(new NewTopic(topic, numPartitions, (short) 1))).topicId(topic).get()); - } + Assertions.assertDoesNotThrow(() -> clusterInstance.createTopic(topic, numPartitions, (short) 1)); } private void deleteConsumerGroups(Collection groupIds) { diff --git a/tools/src/test/java/org/apache/kafka/tools/consumer/group/ResetConsumerGroupOffsetTest.java b/tools/src/test/java/org/apache/kafka/tools/consumer/group/ResetConsumerGroupOffsetTest.java index 503a5bea217e9..0c888e7dcecb2 100644 --- a/tools/src/test/java/org/apache/kafka/tools/consumer/group/ResetConsumerGroupOffsetTest.java +++ b/tools/src/test/java/org/apache/kafka/tools/consumer/group/ResetConsumerGroupOffsetTest.java @@ -17,7 +17,6 @@ package org.apache.kafka.tools.consumer.group; import org.apache.kafka.clients.admin.Admin; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.consumer.GroupProtocol; import org.apache.kafka.clients.consumer.KafkaConsumer; import org.apache.kafka.clients.consumer.OffsetAndMetadata; @@ -160,10 +159,9 @@ public void testResetOffsetsWithOfflinePartitionNotInResetTarget(ClusterInstance String group = "new.group"; String[] args = buildArgsForGroup(cluster, group, "--to-earliest", "--execute", "--topic", topic + ":0"); - try (Admin admin = cluster.admin(); ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(args)) { - admin.createTopics(List.of(new NewTopic(topic, Map.of(0, List.of(0), 1, List.of(1))))); - cluster.waitTopicCreation(topic, 2); + cluster.createTopicWithAssignment(topic, Map.of(0, List.of(0), 1, List.of(1))); + try (ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(args)) { cluster.shutdownBroker(1); Map resetOffsets = service.resetOffsets().get(group); @@ -341,7 +339,7 @@ public void testResetOffsetsByDurationFallbackToLatestWhenNoRecords(ClusterInsta String[] args = buildArgsForGroup(cluster, group, "--topic", topic, "--by-duration", "PT1M", "--execute"); try (Admin admin = cluster.admin()) { - admin.createTopics(Set.of(new NewTopic(topic, 1, (short) 1))).all().get(); + cluster.createTopic(topic, 1, (short) 1); resetAndAssertOffsets(cluster, args, 0, false, List.of(topic)); admin.deleteTopics(Set.of(topic)).all().get(); } @@ -471,7 +469,7 @@ public void testResetOffsetsToEarliestOnOneTopicAndPartition(ClusterInstance clu try (Admin admin = cluster.admin(); ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(args)) { - admin.createTopics(Set.of(new NewTopic(topic, 2, (short) 1))).all().get(); + cluster.createTopic(topic, 2, (short) 1); produceConsumeAndShutdown(cluster, topic, group, 2, groupProtocol); Map priorCommittedOffsets = committedOffsets(cluster, topic, group); @@ -500,8 +498,8 @@ public void testResetOffsetsToEarliestOnTopics(ClusterInstance cluster) throws E try (Admin admin = cluster.admin(); ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(args)) { - admin.createTopics(List.of(new NewTopic(topic1, 1, (short) 1), - new NewTopic(topic2, 1, (short) 1))).all().get(); + cluster.createTopic(topic1, 1, (short) 1); + cluster.createTopic(topic2, 1, (short) 1); produceConsumeAndShutdown(cluster, topic1, group, 1, groupProtocol); produceConsumeAndShutdown(cluster, topic2, group, 1, groupProtocol); @@ -535,8 +533,8 @@ public void testResetOffsetsToEarliestOnTopicsAndPartitions(ClusterInstance clus try (Admin admin = cluster.admin(); ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(args)) { - admin.createTopics(List.of(new NewTopic(topic1, 2, (short) 1), - new NewTopic(topic2, 2, (short) 1))).all().get(); + cluster.createTopic(topic1, 2, (short) 1); + cluster.createTopic(topic2, 2, (short) 1); produceConsumeAndShutdown(cluster, topic1, group, 2, groupProtocol); produceConsumeAndShutdown(cluster, topic2, group, 2, groupProtocol); @@ -579,7 +577,7 @@ public void testResetOffsetsExportImportPlanSingleGroupArg(ClusterInstance clust try (Admin admin = cluster.admin(); ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(cgcArgs)) { - admin.createTopics(Set.of(new NewTopic(topic, 2, (short) 1))).all().get(); + cluster.createTopic(topic, 2, (short) 1); produceConsumeAndShutdown(cluster, topic, group, 2, groupProtocol); Map> exportedOffsets = service.resetOffsets(); @@ -624,8 +622,8 @@ public void testResetOffsetsExportImportPlan(ClusterInstance cluster) throws Exc try (Admin admin = cluster.admin(); ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(cgcArgs)) { - admin.createTopics(List.of(new NewTopic(topic1, 2, (short) 1), - new NewTopic(topic2, 2, (short) 1))).all().get(); + cluster.createTopic(topic1, 2, (short) 1); + cluster.createTopic(topic2, 2, (short) 1); produceConsumeAndShutdown(cluster, topic1, group1, 1, groupProtocol); produceConsumeAndShutdown(cluster, topic2, group2, 1, groupProtocol); @@ -687,10 +685,9 @@ public void testResetOffsetsWithPartitionNoneLeader(ClusterInstance cluster) thr String[] args = buildArgsForGroup(cluster, group, "--topic", topic + ":0,1,2", "--to-earliest", "--execute"); - try (Admin admin = cluster.admin(); - ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(args)) { + try (ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(args)) { - admin.createTopics(Set.of(new NewTopic(topic, 3, (short) 1))).all().get(); + cluster.createTopic(topic, 3, (short) 1); produceConsumeAndShutdown(cluster, topic, group, 2, GroupProtocol.CLASSIC); assertDoesNotThrow(() -> resetOffsets(service)); // shutdown a broker to make some partitions missing leader @@ -706,10 +703,9 @@ public void testResetOffsetsWithPartitionNotExist(ClusterInstance cluster) throw String[] args = buildArgsForGroup(cluster, group, "--topic", topic + ":2,3", "--to-earliest", "--execute"); - try (Admin admin = cluster.admin(); - ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(args)) { + try (ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(args)) { - admin.createTopics(Set.of(new NewTopic(topic, 1, (short) 1))).all().get(); + cluster.createTopic(topic, 1, (short) 1); produceConsumeAndShutdown(cluster, topic, group, 2, GroupProtocol.CLASSIC); assertThrows(UnknownTopicOrPartitionException.class, () -> resetOffsets(service)); } diff --git a/tools/src/test/java/org/apache/kafka/tools/reassign/ReassignPartitionsCommandTest.java b/tools/src/test/java/org/apache/kafka/tools/reassign/ReassignPartitionsCommandTest.java index a71e4779a3e4e..b14c4ed712585 100644 --- a/tools/src/test/java/org/apache/kafka/tools/reassign/ReassignPartitionsCommandTest.java +++ b/tools/src/test/java/org/apache/kafka/tools/reassign/ReassignPartitionsCommandTest.java @@ -24,7 +24,6 @@ import org.apache.kafka.clients.admin.ConfigEntry; import org.apache.kafka.clients.admin.DescribeLogDirsResult; import org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo; -import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.admin.OffsetSpec; import org.apache.kafka.clients.admin.TopicDescription; import org.apache.kafka.clients.consumer.Consumer; @@ -546,25 +545,20 @@ public void testExecuteAssignmentWithOneBootstrapServerShutdownWontTimeout() thr } private void createTopics() { - try (Admin admin = Admin.create(Map.of(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, clusterInstance.bootstrapServers()))) { - Map> fooReplicasAssignments = new HashMap<>(); - fooReplicasAssignments.put(0, List.of(0, 1, 2)); - fooReplicasAssignments.put(1, List.of(1, 2, 3)); - Assertions.assertDoesNotThrow(() -> admin.createTopics(List.of(new NewTopic("foo", fooReplicasAssignments))).topicId("foo").get()); - Assertions.assertDoesNotThrow(() -> clusterInstance.waitTopicCreation("foo", fooReplicasAssignments.size())); - - Map> barReplicasAssignments = new HashMap<>(); - barReplicasAssignments.put(0, List.of(3, 2, 1)); - Assertions.assertDoesNotThrow(() -> admin.createTopics(List.of(new NewTopic("bar", barReplicasAssignments))).topicId("bar").get()); - Assertions.assertDoesNotThrow(() -> clusterInstance.waitTopicCreation("bar", barReplicasAssignments.size())); - - Map> bazReplicasAssignments = new HashMap<>(); - bazReplicasAssignments.put(0, List.of(1, 0, 2)); - bazReplicasAssignments.put(1, List.of(2, 0, 1)); - bazReplicasAssignments.put(2, List.of(0, 2, 1)); - Assertions.assertDoesNotThrow(() -> admin.createTopics(List.of(new NewTopic("baz", bazReplicasAssignments))).topicId("baz").get()); - Assertions.assertDoesNotThrow(() -> clusterInstance.waitTopicCreation("baz", bazReplicasAssignments.size())); - } + Map> fooReplicasAssignments = new HashMap<>(); + fooReplicasAssignments.put(0, List.of(0, 1, 2)); + fooReplicasAssignments.put(1, List.of(1, 2, 3)); + Assertions.assertDoesNotThrow(() -> clusterInstance.createTopicWithAssignment("foo", fooReplicasAssignments)); + + Map> barReplicasAssignments = new HashMap<>(); + barReplicasAssignments.put(0, List.of(3, 2, 1)); + Assertions.assertDoesNotThrow(() -> clusterInstance.createTopicWithAssignment("bar", barReplicasAssignments)); + + Map> bazReplicasAssignments = new HashMap<>(); + bazReplicasAssignments.put(0, List.of(1, 0, 2)); + bazReplicasAssignments.put(1, List.of(2, 0, 1)); + bazReplicasAssignments.put(2, List.of(0, 2, 1)); + Assertions.assertDoesNotThrow(() -> clusterInstance.createTopicWithAssignment("baz", bazReplicasAssignments)); } private void produceMessages(String topic, int partition, int numMessages) {