mimaison commented on code in PR #21689: URL: https://github.com/apache/kafka/pull/21689#discussion_r2910231456
########## server/src/test/java/org/apache/kafka/server/TestUtils.java: ########## @@ -0,0 +1,195 @@ +/* + * 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.server; + +import kafka.server.KafkaBroker; + +import org.apache.kafka.clients.consumer.Consumer; +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.apache.kafka.clients.consumer.ConsumerRecords; +import org.apache.kafka.clients.producer.Producer; +import org.apache.kafka.clients.producer.ProducerConfig; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.requests.FetchRequest; +import org.apache.kafka.common.serialization.StringSerializer; +import org.apache.kafka.common.test.ClusterInstance; +import org.apache.kafka.metadata.LeaderAndIsr; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; +import java.util.function.Supplier; + +import static org.apache.kafka.test.TestUtils.DEFAULT_MAX_WAIT_MS; +import static org.apache.kafka.test.TestUtils.waitForCondition; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestUtils { + + private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class); + + /** + * Wait until a valid leader is propagated to the metadata cache in each broker. + * It assumes that the leader propagated to each broker is the same. + * + * @param brokers The list of brokers that the metadata should reach + * @param topic The topic name + * @param partition The partitionId + * @return The metadata of the partition. + */ + public static LeaderAndIsr waitForPartitionMetadata(Collection<KafkaBroker> brokers, String topic, int partition) throws Exception { + if (brokers.isEmpty()) { + throw new IllegalArgumentException("Empty broker list"); + } + waitForCondition( + () -> brokers.stream().allMatch(broker -> { + Optional<LeaderAndIsr> leaderAndIsr = broker.metadataCache().getLeaderAndIsr(topic, partition); + return leaderAndIsr.filter(andIsr -> FetchRequest.isValidBrokerId(andIsr.leader())).isPresent(); + }), + DEFAULT_MAX_WAIT_MS, + "Partition [" + topic + "," + partition + "] metadata not propagated after " + DEFAULT_MAX_WAIT_MS + " ms"); + + return brokers.iterator().next().metadataCache().getLeaderAndIsr(topic, partition).orElseThrow(() -> + new IllegalStateException("Cannot get topic: " + topic + ", partition: " + partition + " in server metadata cache")); + } + + private static <K, V> List<ConsumerRecord<K, V>> pollUntilAtLeastNumRecords(Consumer<K, V> consumer, int numRecords) throws Exception { Review Comment: The original methods in `kafka.utils.TestUtils` are called from multiple places hence why I kept them separate there too. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
