kamalcph commented on code in PR #14116: URL: https://github.com/apache/kafka/pull/14116#discussion_r1286072822
########## storage/src/test/java/org/apache/kafka/tiered/storage/TieredStorageTestBuilder.java: ########## @@ -0,0 +1,427 @@ +/* + * 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.actions.BounceBrokerAction; +import org.apache.kafka.tiered.storage.actions.ConsumeAction; +import org.apache.kafka.tiered.storage.actions.CreatePartitionsAction; +import org.apache.kafka.tiered.storage.actions.CreateTopicAction; +import org.apache.kafka.tiered.storage.actions.DeleteRecordsAction; +import org.apache.kafka.tiered.storage.actions.DeleteTopicAction; +import org.apache.kafka.tiered.storage.actions.EraseBrokerStorageAction; +import org.apache.kafka.tiered.storage.actions.ExpectBrokerInISRAction; +import org.apache.kafka.tiered.storage.actions.ExpectEmptyRemoteStorageAction; +import org.apache.kafka.tiered.storage.actions.ExpectLeaderAction; +import org.apache.kafka.tiered.storage.actions.ExpectLeaderEpochCheckpointAction; +import org.apache.kafka.tiered.storage.actions.ExpectListOffsetsAction; +import org.apache.kafka.tiered.storage.actions.ExpectTopicIdToMatchInRemoteStorageAction; +import org.apache.kafka.tiered.storage.actions.ExpectUserTopicMappedToMetadataPartitionsAction; +import org.apache.kafka.tiered.storage.actions.ProduceAction; +import org.apache.kafka.tiered.storage.actions.ReassignReplicaAction; +import org.apache.kafka.tiered.storage.actions.ShrinkReplicaAction; +import org.apache.kafka.tiered.storage.actions.StartBrokerAction; +import org.apache.kafka.tiered.storage.actions.StopBrokerAction; +import org.apache.kafka.tiered.storage.actions.UpdateBrokerConfigAction; +import org.apache.kafka.tiered.storage.actions.UpdateTopicConfigAction; +import org.apache.kafka.tiered.storage.specs.ConsumableSpec; +import org.apache.kafka.tiered.storage.specs.DeletableSpec; +import org.apache.kafka.tiered.storage.specs.ExpandPartitionCountSpec; +import org.apache.kafka.tiered.storage.specs.FetchableSpec; +import org.apache.kafka.tiered.storage.specs.KeyValueSpec; +import org.apache.kafka.tiered.storage.specs.OffloadableSpec; +import org.apache.kafka.tiered.storage.specs.OffloadedSegmentSpec; +import org.apache.kafka.tiered.storage.specs.ProducableSpec; +import org.apache.kafka.tiered.storage.specs.RemoteDeleteSegmentSpec; +import org.apache.kafka.tiered.storage.specs.RemoteFetchSpec; +import org.apache.kafka.tiered.storage.specs.TopicSpec; +import org.apache.kafka.clients.admin.OffsetSpec; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.config.TopicConfig; +import org.apache.kafka.server.log.remote.storage.LocalTieredStorageEvent; +import org.apache.kafka.storage.internals.log.EpochEntry; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public final class TieredStorageTestBuilder { + + private final int defaultProducedBatchSize = 1; + private final long defaultEarliestLocalOffsetExpectedInLogDirectory = 0; + + private Map<TopicPartition, ProducableSpec> producables = new HashMap<>(); + private Map<TopicPartition, List<OffloadableSpec>> offloadables = new HashMap<>(); + private Map<TopicPartition, ConsumableSpec> consumables = new HashMap<>(); + private Map<TopicPartition, FetchableSpec> fetchables = new HashMap<>(); + private Map<TopicPartition, List<DeletableSpec>> deletables = new HashMap<>(); + private List<TieredStorageTestAction> actions = new ArrayList<>(); + + public TieredStorageTestBuilder() { + } + + public TieredStorageTestBuilder createTopic(String topic, + Integer partitionCount, + Integer replicationFactor, + Integer maxBatchCountPerSegment, + Map<Integer, List<Integer>> replicaAssignment, + Boolean enableRemoteLogStorage) { + assert maxBatchCountPerSegment >= 1 : "Segments size for topic " + topic + " needs to be >= 1"; + assert partitionCount >= 1 : "Partition count for topic " + topic + " needs to be >= 1"; + assert replicationFactor >= 1 : "Replication factor for topic " + topic + " needs to be >= 1"; + maybeCreateProduceAction(); + maybeCreateConsumeActions(); + Map<String, String> properties = new HashMap<>(); + properties.put(TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, enableRemoteLogStorage.toString()); + TopicSpec topicSpec = new TopicSpec(topic, partitionCount, replicationFactor, maxBatchCountPerSegment, + replicaAssignment, properties); + actions.add(new CreateTopicAction(topicSpec)); + return this; + } + + public TieredStorageTestBuilder createPartitions(String topic, + Integer partitionCount, + Map<Integer, List<Integer>> replicaAssignment) { + assert partitionCount >= 1 : "Partition count for topic " + topic + " needs to be >= 1"; + maybeCreateProduceAction(); + maybeCreateConsumeActions(); + ExpandPartitionCountSpec spec = new ExpandPartitionCountSpec(topic, partitionCount, replicaAssignment); + actions.add(new CreatePartitionsAction(spec)); + return this; + } + + public TieredStorageTestBuilder updateTopicConfig(String topic, + Map<String, String> configsToBeAdded, + List<String> configsToBeDeleted) { + assert !configsToBeAdded.isEmpty() || !configsToBeDeleted.isEmpty() + : "Topic " + topic + " configs shouldn't be empty"; + maybeCreateProduceAction(); + maybeCreateConsumeActions(); + actions.add(new UpdateTopicConfigAction(topic, configsToBeAdded, configsToBeDeleted)); + return this; + } + + public TieredStorageTestBuilder updateBrokerConfig(Integer brokerId, + Map<String, String> configsToBeAdded, + List<String> configsToBeDeleted) { + assert !configsToBeAdded.isEmpty() || !configsToBeDeleted.isEmpty() + : "Broker " + brokerId + " configs shouldn't be empty"; + maybeCreateProduceAction(); + maybeCreateConsumeActions(); + actions.add(new UpdateBrokerConfigAction(brokerId, configsToBeAdded, configsToBeDeleted)); + return this; + } + + public TieredStorageTestBuilder deleteTopic(List<String> topics) { + maybeCreateProduceAction(); + maybeCreateConsumeActions(); + topics.forEach(topic -> actions.add(buildDeleteTopicAction(topic, true))); + return this; + } + + public TieredStorageTestBuilder produce(String topic, Review Comment: Updated the `produce` and `consume` methods to be deterministic. Previously, the DSL can be written as: ``` .produce(topicA, p0, new KeyValueSpec("k1", "v1"), new KeyValueSpec("k2", "v2")) .withBatchSize(topicA, p0, batchSize) .expectSegmentToBeOffloaded(broker, topicA, p0, 0, new KeyValueSpec("k1", "v1")) .expectEarliestLocalOffsetInLogDirectory(topicBA, p0, 1L) ``` Now, with this change, the expectation needs to be called before the `produce` action: ``` .withBatchSize(topicA, p0, batchSize) .expectSegmentToBeOffloaded(broker, topicA, p0, 0, new KeyValueSpec("k1", "v1")) .expectEarliestLocalOffsetInLogDirectory(topicBA, p0, 1L) .produce(topicA, p0, new KeyValueSpec("k1", "v1"), new KeyValueSpec("k2", "v2")) ``` Updated the code as the changes are intuitive to the user. Similarly for the consume method. ########## storage/src/test/java/org/apache/kafka/tiered/storage/TieredStorageTestContext.java: ########## @@ -0,0 +1,348 @@ +/* + * 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.network.ListenerName; +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.CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG; +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 ListenerName listenerName; + + 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, + ListenerName listenerName) { + this.brokers = brokers; + this.producerConfig = producerConfig; + this.consumerConfig = consumerConfig; + this.listenerName = listenerName; + this.ser = Serdes.String().serializer(); + this.de = Serdes.String().deserializer(); + this.topicSpecs = new HashMap<>(); + this.testReport = new TieredStorageTestReport(this); + initContext(); + } + + private void initContext() { + String bootstrapServers = TestUtils.bootstrapServers(brokers, listenerName); + producerConfig.put(BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); + // 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); + + consumerConfig.put(BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); + consumer = new KafkaConsumer<>(consumerConfig, de, de); + + Properties adminConfig = new Properties(); + adminConfig.put(BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); + 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(); + } + + public void updateTopicConfig(String topic, + Map<String, String> configsToBeAdded, + List<String> configsToBeDeleted) + throws ExecutionException, InterruptedException, TimeoutException { + ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, topic); + updateResource(configResource, configsToBeAdded, configsToBeDeleted); + } + + public void updateBrokerConfig(Integer brokerId, + Map<String, String> configsToBeAdded, + List<String> configsToBeDeleted) + throws ExecutionException, InterruptedException, TimeoutException { + ConfigResource configResource = new ConfigResource(ConfigResource.Type.BROKER, brokerId.toString()); + updateResource(configResource, configsToBeAdded, configsToBeDeleted); + } + + private void updateResource(ConfigResource configResource, + Map<String, String> configsToBeAdded, + List<String> configsToBeDeleted) + throws ExecutionException, InterruptedException, TimeoutException { + List<AlterConfigOp> alterEntries = new ArrayList<>(); + configsToBeDeleted.forEach(k -> + alterEntries.add(new AlterConfigOp(new ConfigEntry(k, ""), AlterConfigOp.OpType.DELETE))); + configsToBeAdded.forEach((k, v) -> + alterEntries.add(new AlterConfigOp(new ConfigEntry(k, v), AlterConfigOp.OpType.SET))); + AlterConfigsOptions alterOptions = new AlterConfigsOptions().timeoutMs(30000); + Map<ConfigResource, Collection<AlterConfigOp>> configsMap = + Collections.singletonMap(configResource, alterEntries); + admin.incrementalAlterConfigs(configsMap, alterOptions).all().get(30, TimeUnit.SECONDS); + } + + public void deleteTopic(String topic) throws ExecutionException, InterruptedException, TimeoutException { Review Comment: ditto -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org