AndrewJSchofield commented on code in PR #18651: URL: https://github.com/apache/kafka/pull/18651#discussion_r1930848818
########## server/src/test/java/org/apache/kafka/server/share/fetch/PartitionRotateStrategyTest.java: ########## @@ -0,0 +1,94 @@ +/* + * 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.share.fetch; + +import org.apache.kafka.common.TopicIdPartition; +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.requests.ShareRequestMetadata; +import org.apache.kafka.server.share.fetch.PartitionRotateStrategy.PartitionRotateMetadata; +import org.apache.kafka.server.share.fetch.PartitionRotateStrategy.StrategyType; + +import org.junit.jupiter.api.Test; + +import java.util.LinkedHashMap; + +import static org.apache.kafka.server.share.fetch.ShareFetchTestUtils.validateRotatedMapEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class PartitionRotateStrategyTest { + + @Test + public void testRoundRobinStrategy() { + PartitionRotateStrategy strategy = PartitionRotateStrategy.type(StrategyType.ROUND_ROBIN); + LinkedHashMap<TopicIdPartition, Integer> partitions = createPartitions(3); + + LinkedHashMap<TopicIdPartition, Integer> result = strategy.rotate(partitions, new PartitionRotateMetadata(1)); + assertEquals(3, result.size()); + validateRotatedMapEquals(partitions, result, 1); + + // Session epoch is greater than the number of partitions. + result = strategy.rotate(partitions, new PartitionRotateMetadata(5)); + assertEquals(3, result.size()); + validateRotatedMapEquals(partitions, result, 2); + + // Session epoch is at Integer.MAX_VALUE. + result = strategy.rotate(partitions, new PartitionRotateMetadata(Integer.MAX_VALUE)); + assertEquals(3, result.size()); + validateRotatedMapEquals(partitions, result, 1); + + // No rotation at same size as epoch. + result = strategy.rotate(partitions, new PartitionRotateMetadata(3)); + assertEquals(3, result.size()); + validateRotatedMapEquals(partitions, result, 0); + } + + @Test + public void testRoundRobinStrategyWithSpecialSessionEpochs() { + PartitionRotateStrategy strategy = PartitionRotateStrategy.type(StrategyType.ROUND_ROBIN); + + LinkedHashMap<TopicIdPartition, Integer> partitions = createPartitions(3); + LinkedHashMap<TopicIdPartition, Integer> result = strategy.rotate( + partitions, + new PartitionRotateMetadata(ShareRequestMetadata.INITIAL_EPOCH)); + assertEquals(3, result.size()); + validateRotatedMapEquals(partitions, result, 0); + + result = strategy.rotate( + partitions, + new PartitionRotateMetadata(ShareRequestMetadata.FINAL_EPOCH)); + assertEquals(3, result.size()); + validateRotatedMapEquals(partitions, result, 0); + } + + @Test + public void testRoundRobinStrategyWithEmptyPartitions() { + PartitionRotateStrategy strategy = PartitionRotateStrategy.type(StrategyType.ROUND_ROBIN); + // Empty partitions. + LinkedHashMap<TopicIdPartition, Integer> result = strategy.rotate(new LinkedHashMap<>(), new PartitionRotateMetadata(5)); + // The result should be empty. + assertTrue(result.isEmpty()); + } + + private LinkedHashMap<TopicIdPartition, Integer> createPartitions(int size) { Review Comment: So this creates a `LinkedHashMap` that contains `size` entries, each with 2 partitions. Comment? ########## server/src/test/java/org/apache/kafka/server/share/fetch/ShareFetchTestUtils.java: ########## @@ -0,0 +1,79 @@ +/* + * 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.share.fetch; + +import org.apache.kafka.common.TopicIdPartition; + +import java.util.LinkedHashMap; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Helper functions for writing share fetch unit tests. + */ +public class ShareFetchTestUtils { + + /** + * Create an ordered map of TopicIdPartition to partition max bytes. + * + * @param partitionMaxBytes The maximum number of bytes that can be fetched for each partition. + * @param topicIdPartitions The topic partitions to create the map for. + * + * @return The ordered map of TopicIdPartition to partition max bytes. + */ + public static LinkedHashMap<TopicIdPartition, Integer> orderedMap(int partitionMaxBytes, TopicIdPartition... topicIdPartitions) { + LinkedHashMap<TopicIdPartition, Integer> map = new LinkedHashMap<>(); + for (TopicIdPartition tp : topicIdPartitions) { + map.put(tp, partitionMaxBytes); + } + return map; + } + + /** + * Validate that the rotated map is equal to the original map with the keys rotated by the given position. + * + * @param original The original map. + * @param result The rotated map. + * Review Comment: nit: Extra blank line ########## core/src/test/java/kafka/server/share/SharePartitionManagerTest.java: ########## @@ -2595,6 +2570,63 @@ public void testSharePartitionListenerOnBecomingFollower() { testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onBecomingFollower); } + @Test + public void testFetchMessagesRotatePartitions() { + String groupId = "grp"; + Uuid memberId1 = Uuid.randomUuid(); + TopicIdPartition tp0 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)); + TopicIdPartition tp1 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 1)); + TopicIdPartition tp2 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("bar", 0)); + TopicIdPartition tp3 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("bar", 1)); + TopicIdPartition tp4 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 2)); + TopicIdPartition tp5 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("bar", 2)); + TopicIdPartition tp6 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 3)); + LinkedHashMap<TopicIdPartition, Integer> partitionMaxBytes = orderedMap(PARTITION_MAX_BYTES, + tp0, tp1, tp2, tp3, tp4, tp5, tp6); + + SharePartitionManager sharePartitionManager = Mockito.spy(SharePartitionManagerBuilder.builder().build()); + // Capture the arguments passed to processShareFetch. + ArgumentCaptor<ShareFetch> captor = ArgumentCaptor.forClass(ShareFetch.class); + + sharePartitionManager.fetchMessages(groupId, memberId1.toString(), FETCH_PARAMS, 0, BATCH_SIZE, + partitionMaxBytes); + verify(sharePartitionManager, times(1)).processShareFetch(captor.capture()); + // Verify the partitions rotation, no rotation. + ShareFetch resultShareFetch = captor.getValue(); + validateRotatedMapEquals(resultShareFetch.partitionMaxBytes(), partitionMaxBytes, 0); + + // Single rotation. + sharePartitionManager.fetchMessages(groupId, memberId1.toString(), FETCH_PARAMS, 1, BATCH_SIZE, + partitionMaxBytes); + verify(sharePartitionManager, times(2)).processShareFetch(captor.capture()); + // Verify the partitions rotation, rotate by 1. + resultShareFetch = captor.getValue(); + validateRotatedMapEquals(partitionMaxBytes, resultShareFetch.partitionMaxBytes(), 1); + + // Rotation by 3, less that the number of partitions. + sharePartitionManager.fetchMessages(groupId, memberId1.toString(), FETCH_PARAMS, 3, BATCH_SIZE, + partitionMaxBytes); + verify(sharePartitionManager, times(3)).processShareFetch(captor.capture()); + // Verify the partitions rotation, rotate by 3. + resultShareFetch = captor.getValue(); + validateRotatedMapEquals(partitionMaxBytes, resultShareFetch.partitionMaxBytes(), 3); + + // Rotation by 12, more than the number of partitions. + sharePartitionManager.fetchMessages(groupId, memberId1.toString(), FETCH_PARAMS, 12, BATCH_SIZE, + partitionMaxBytes); + verify(sharePartitionManager, times(4)).processShareFetch(captor.capture()); + // Verify the partitions rotation, rotate by 5 (12 % 7). + resultShareFetch = captor.getValue(); + validateRotatedMapEquals(partitionMaxBytes, resultShareFetch.partitionMaxBytes(), 5); + + sharePartitionManager.fetchMessages(groupId, memberId1.toString(), FETCH_PARAMS, Integer.MAX_VALUE, BATCH_SIZE, + partitionMaxBytes); + verify(sharePartitionManager, times(5)).processShareFetch(captor.capture()); + // Verify the partitions rotation, rotate by 1 (2147483647 % 7). + resultShareFetch = captor.getValue(); + validateRotatedMapEquals(partitionMaxBytes, resultShareFetch.partitionMaxBytes(), 1); Review Comment: You probably could use the calculation `Integer.MAX_VALUE % 7` as the parameter. -- 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