jolshan commented on a change in pull request #11331: URL: https://github.com/apache/kafka/pull/11331#discussion_r745930497
########## File path: clients/src/test/java/org/apache/kafka/common/requests/FetchRequestTest.java ########## @@ -0,0 +1,214 @@ +/* + * 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.common.requests; + +import org.apache.kafka.common.TopicIdPartition; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.protocol.ApiKeys; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.Collections; +import java.util.Map; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.fail; + +public class FetchRequestTest { + + private static Stream<Arguments> fetchVersions() { + return ApiKeys.FETCH.allVersions().stream().map(version -> Arguments.of(version)); + } + + @ParameterizedTest + @MethodSource("fetchVersions") + public void testToReplaceWithDifferentVersions(short version) { + + Uuid topicId = Uuid.randomUuid(); + TopicPartition tp = new TopicPartition("topic", 0); + TopicIdPartition tidp = new TopicIdPartition(topicId, tp); + + Map<TopicPartition, FetchRequest.PartitionData> partitionData = Collections.singletonMap(tp, + new FetchRequest.PartitionData(topicId, 0, 0, 0, Optional.empty())); + List<TopicIdPartition> toReplace = Collections.singletonList(tidp); + boolean fetchRequestUsesTopicIds = version >= 13; + + FetchRequest fetchRequest = FetchRequest.Builder + .forReplica(version, 0, 1, 1, partitionData) + .removed(Collections.emptyList()) + .replaced(toReplace) + .metadata(FetchMetadata.newIncremental(123)).build(version); + + // If version < 13, we should not see any partitions in forgottenTopics. This is because we can not + // distinguish different topic IDs on versions earlier than 13. + assertEquals(fetchRequestUsesTopicIds, fetchRequest.data().forgottenTopicsData().size() > 0); + assertEquals(1, fetchRequest.data().topics().size()); + } + + @ParameterizedTest + @MethodSource("fetchVersions") + public void testFetchData(short version) { + TopicPartition topicPartition0 = new TopicPartition("topic", 0); + TopicPartition topicPartition1 = new TopicPartition("unknownIdTopic", 0); + Uuid topicId0 = Uuid.randomUuid(); + Uuid topicId1 = Uuid.randomUuid(); + + // Only include topic IDs for the first topic partition. + Map<Uuid, String> topicNames = Collections.singletonMap(topicId0, topicPartition0.topic()); + List<TopicIdPartition> topicIdPartitions = new LinkedList<>(); + topicIdPartitions.add(new TopicIdPartition(topicId0, topicPartition0)); + topicIdPartitions.add(new TopicIdPartition(topicId1, topicPartition1)); + + // Include one topic with topic IDs in the topic names map and one without. + Map<TopicPartition, FetchRequest.PartitionData> partitionData = new LinkedHashMap<>(); + partitionData.put(topicPartition0, new FetchRequest.PartitionData(topicId0, 0, 0, 0, Optional.empty())); + partitionData.put(topicPartition1, new FetchRequest.PartitionData(topicId1, 0, 0, 0, Optional.empty())); + boolean fetchRequestUsesTopicIds = version >= 13; + + FetchRequest fetchRequest = FetchRequest.parse(FetchRequest.Builder + .forReplica(version, 0, 1, 1, partitionData) + .removed(Collections.emptyList()) + .replaced(Collections.emptyList()) + .metadata(FetchMetadata.newIncremental(123)).build(version).serialize(), version); + + // For versions < 13, we will be provided a topic name and a zero UUID in FetchRequestData. + // Versions 13+ will contain a valid topic ID but an empty topic name. + List<TopicIdPartition> expectedFetchData = new LinkedList<>(); + topicIdPartitions.forEach(tidp -> { + String expectedName = fetchRequestUsesTopicIds ? "" : tidp.topic(); + Uuid expectedTopicId = fetchRequestUsesTopicIds ? tidp.topicId() : Uuid.ZERO_UUID; + expectedFetchData.add(new TopicIdPartition(expectedName, expectedTopicId, tidp.partition())); + }); + + // Build the list of TopicIdPartitions based on the FetchRequestData that was serialized and parsed. + List<TopicIdPartition> convertedFetchData = new LinkedList<>(); + fetchRequest.data().topics().forEach(topic -> + topic.partitions().forEach(partition -> + convertedFetchData.add(new TopicIdPartition(topic.topic(), topic.topicId(), partition.partition())) + ) + ); + // The TopicIdPartitions built from the request data should match what we expect. + assertListEquals(expectedFetchData, convertedFetchData); + + // Get the fetchData map from the request data. + Map<TopicIdPartition, FetchRequest.PartitionData> fetchData = fetchRequest.fetchData(topicNames); + + // For fetch request version 13+ we expect topic names to be filled in for all topics in the topicNames map. + // Otherwise, the topic name should be null. + // For earlier request versions, we expect topic names and zero Uuids. + assertEquals(expectedFetchData.size(), fetchData.size()); + expectedFetchData.forEach(tidp -> { + String expectedName = fetchRequestUsesTopicIds ? topicNames.get(tidp.topicId()) : tidp.topic(); + TopicIdPartition tpKey = new TopicIdPartition(tidp.topicId(), new TopicPartition(expectedName, tidp.partition())); Review comment: the expectedName will be null if it is not in the map. map.get returns null if the ID is not in the map. -- 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