kamalcph commented on code in PR #14116: URL: https://github.com/apache/kafka/pull/14116#discussion_r1294503554
########## storage/src/test/java/org/apache/kafka/tiered/storage/TieredStorageTestContext.java: ########## @@ -0,0 +1,339 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.tiered.storage; + +import org.apache.kafka.tiered.storage.specs.ExpandPartitionCountSpec; +import org.apache.kafka.tiered.storage.specs.TopicSpec; +import org.apache.kafka.tiered.storage.utils.BrokerLocalStorage; +import kafka.log.UnifiedLog; +import kafka.server.KafkaBroker; +import kafka.utils.TestUtils; +import org.apache.kafka.clients.admin.Admin; +import org.apache.kafka.clients.admin.AlterConfigOp; +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.ConsumerRecord; +import org.apache.kafka.clients.consumer.ConsumerRecords; +import org.apache.kafka.clients.consumer.KafkaConsumer; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.TopicPartitionInfo; +import org.apache.kafka.common.config.ConfigResource; +import org.apache.kafka.common.serialization.Deserializer; +import org.apache.kafka.common.serialization.Serdes; +import org.apache.kafka.common.serialization.Serializer; +import org.apache.kafka.common.utils.Utils; +import org.apache.kafka.metadata.BrokerState; +import org.apache.kafka.server.log.remote.storage.LocalTieredStorage; +import org.apache.kafka.server.log.remote.storage.LocalTieredStorageHistory; +import org.apache.kafka.server.log.remote.storage.LocalTieredStorageSnapshot; +import scala.Function0; +import scala.Function1; + +import java.io.IOException; +import java.io.PrintStream; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Properties; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.stream.Collectors; + +import scala.Option; +import scala.collection.Seq; + +import static org.apache.kafka.clients.producer.ProducerConfig.LINGER_MS_CONFIG; + +public final class TieredStorageTestContext { + + private final Seq<KafkaBroker> brokers; + private final Properties producerConfig; + private final Properties consumerConfig; + private final Properties adminConfig; + + private final Serializer<String> ser; + private final Deserializer<String> de; + + private final Map<String, TopicSpec> topicSpecs; + private final TieredStorageTestReport testReport; + + private volatile KafkaProducer<String, String> producer; + private volatile KafkaConsumer<String, String> consumer; + private volatile Admin admin; + private volatile List<LocalTieredStorage> tieredStorages; + private volatile List<BrokerLocalStorage> localStorages; + + public TieredStorageTestContext(Seq<KafkaBroker> brokers, + Properties producerConfig, + Properties consumerConfig, + Properties adminConfig) { + this.brokers = brokers; + this.producerConfig = producerConfig; + this.consumerConfig = consumerConfig; + this.adminConfig = adminConfig; + this.ser = Serdes.String().serializer(); + this.de = Serdes.String().deserializer(); + this.topicSpecs = new HashMap<>(); + this.testReport = new TieredStorageTestReport(this); + initContext(); + } + + private void initContext() { + // Set a producer linger of 60 seconds, in order to optimistically generate batches of + // records with a pre-determined size. + producerConfig.put(LINGER_MS_CONFIG, String.valueOf(TimeUnit.SECONDS.toMillis(60))); + producer = new KafkaProducer<>(producerConfig, ser, ser); + consumer = new KafkaConsumer<>(consumerConfig, de, de); + admin = Admin.create(adminConfig); + + tieredStorages = TieredStorageTestHarness.getTieredStorages(brokers); + localStorages = TieredStorageTestHarness.getLocalStorages(brokers); + } + + public void createTopic(TopicSpec spec) throws ExecutionException, InterruptedException { + NewTopic newTopic; + if (spec.getAssignment() == null || spec.getAssignment().isEmpty()) { + newTopic = new NewTopic(spec.getTopicName(), spec.getPartitionCount(), (short) spec.getReplicationFactor()); + } else { + Map<Integer, List<Integer>> replicasAssignments = spec.getAssignment(); + newTopic = new NewTopic(spec.getTopicName(), replicasAssignments); + } + newTopic.configs(spec.getProperties()); + admin.createTopics(Collections.singletonList(newTopic)).all().get(); + synchronized (this) { + topicSpecs.put(spec.getTopicName(), spec); + } + } + + public void createPartitions(ExpandPartitionCountSpec spec) throws ExecutionException, InterruptedException { + NewPartitions newPartitions; + if (spec.getAssignment() == null || spec.getAssignment().isEmpty()) { + newPartitions = NewPartitions.increaseTo(spec.getPartitionCount()); + } else { + Map<Integer, List<Integer>> assignment = spec.getAssignment(); + List<List<Integer>> newAssignments = assignment.entrySet().stream() + .sorted(Map.Entry.comparingByKey()) + .map(Map.Entry::getValue) + .collect(Collectors.toList()); + newPartitions = NewPartitions.increaseTo(spec.getPartitionCount(), newAssignments); + } + Map<String, NewPartitions> partitionsMap = Collections.singletonMap(spec.getTopicName(), newPartitions); + admin.createPartitions(partitionsMap).all().get(); Review Comment: We can reuse the below method for partition expansion: ``` TestUtils.waitForAllPartitionsMetadata(harness.brokers(), spec.getTopicName(), spec.getPartitionCount()); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org