You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "apoorvmittal10 (via GitHub)" <gi...@apache.org> on 2025/01/20 22:15:11 UTC
[PR] KAFKA-17951: Share parition rotate strategy [kafka]
apoorvmittal10 opened a new pull request, #18651:
URL: https://github.com/apache/kafka/pull/18651
Defined PartitionRotateStrategy class and round robin implementation for partitions. This is required to let not starve any single partition during share-fetch.
### Committer Checklist (excluded from commit message)
- [ ] Verify design and implementation
- [ ] Verify test coverage and CI build status
- [ ] Verify documentation (including upgrade notes)
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17951: Share parition rotate strategy [kafka]
Posted by "AndrewJSchofield (via GitHub)" <gi...@apache.org>.
AndrewJSchofield commented on PR #18651:
URL: https://github.com/apache/kafka/pull/18651#issuecomment-2616317649
@apoorvmittal10 Please merge latest changes.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17951: Share parition rotate strategy [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on PR #18651:
URL: https://github.com/apache/kafka/pull/18651#issuecomment-2616372410
> @apoorvmittal10 Please merge latest changes.
Missed rebasing, done.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17951: Share parition rotate strategy [kafka]
Posted by "AndrewJSchofield (via GitHub)" <gi...@apache.org>.
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17951: Share parition rotate strategy [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #18651:
URL: https://github.com/apache/kafka/pull/18651#discussion_r1931142635
##########
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:
Done.
##########
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:
Added comments.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17951: Share parition rotate strategy [kafka]
Posted by "adixitconfluent (via GitHub)" <gi...@apache.org>.
adixitconfluent commented on code in PR #18651:
URL: https://github.com/apache/kafka/pull/18651#discussion_r1928192830
##########
server/src/main/java/org/apache/kafka/server/share/fetch/PartitionRotateStrategy.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.Locale;
+import java.util.Map;
+
+/**
+ * The PartitionRotateStrategy is used to rotate the partitions based on the respective strategy.
+ * The share-partitions are rotated to ensure no share-partitions are starved from records being fetched.
+ */
+public interface PartitionRotateStrategy {
+
+ /**
+ * The strategy type to rotate the partitions.
+ */
+ enum StrategyType {
+ ROUND_ROBIN;
+
+ @Override
+ public String toString() {
+ return super.toString().toLowerCase(Locale.ROOT);
+ }
+ }
+
+ /**
+ * Rotate the partitions based on the strategy.
+ *
+ * @param topicIdPartitions the topicIdPartitions to rotate
+ * @param metadata the metadata to rotate
+ *
+ * @return the rotated topicIdPartitions
+ */
+ LinkedHashMap<TopicIdPartition, Integer> rotate(LinkedHashMap<TopicIdPartition, Integer> topicIdPartitions, PartitionRotateMetadata metadata);
+
+ static PartitionRotateStrategy type(StrategyType type) {
+ return switch (type) {
+ case ROUND_ROBIN -> PartitionRotateStrategy::rotateRoundRobin;
+ };
+ }
+
+ /**
+ * Rotate the partitions based on the round-robin strategy.
+ *
+ * @param topicIdPartitions the topicIdPartitions to rotate
+ * @param metadata the metadata to rotate
+ *
+ * @return the rotated topicIdPartitions
+ */
+ static LinkedHashMap<TopicIdPartition, Integer> rotateRoundRobin(
+ LinkedHashMap<TopicIdPartition, Integer> topicIdPartitions,
+ PartitionRotateMetadata metadata
+ ) {
+ if (topicIdPartitions.isEmpty() || topicIdPartitions.size() == 1 || metadata.sessionEpoch < 1) {
+ // No need to rotate the partitions if there are no partitions, only one partition or the
+ // session epoch is initial or final.
+ return topicIdPartitions;
+ }
+
+ int rotateAt = metadata.sessionEpoch % topicIdPartitions.size();
+ if (rotateAt == 0) {
+ // No need to rotate the partitions if the rotateAt is 0.
+ return topicIdPartitions;
+ }
+
+ // Once the partition max bytes is removed then the partition will be a linked list and rotation
Review Comment:
Can we add a TODO here before this comment
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17951: Share parition rotate strategy [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #18651:
URL: https://github.com/apache/kafka/pull/18651#discussion_r1930867148
##########
server/src/main/java/org/apache/kafka/server/share/fetch/PartitionRotateStrategy.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.Locale;
+import java.util.Map;
+
+/**
+ * The PartitionRotateStrategy is used to rotate the partitions based on the respective strategy.
+ * The share-partitions are rotated to ensure no share-partitions are starved from records being fetched.
+ */
+public interface PartitionRotateStrategy {
+
+ /**
+ * The strategy type to rotate the partitions.
+ */
+ enum StrategyType {
+ ROUND_ROBIN;
+
+ @Override
+ public String toString() {
+ return super.toString().toLowerCase(Locale.ROOT);
+ }
+ }
+
+ /**
+ * Rotate the partitions based on the strategy.
+ *
+ * @param topicIdPartitions the topicIdPartitions to rotate
+ * @param metadata the metadata to rotate
+ *
+ * @return the rotated topicIdPartitions
+ */
+ LinkedHashMap<TopicIdPartition, Integer> rotate(LinkedHashMap<TopicIdPartition, Integer> topicIdPartitions, PartitionRotateMetadata metadata);
+
+ static PartitionRotateStrategy type(StrategyType type) {
+ return switch (type) {
+ case ROUND_ROBIN -> PartitionRotateStrategy::rotateRoundRobin;
+ };
+ }
+
+ /**
+ * Rotate the partitions based on the round-robin strategy.
+ *
+ * @param topicIdPartitions the topicIdPartitions to rotate
+ * @param metadata the metadata to rotate
+ *
+ * @return the rotated topicIdPartitions
+ */
+ static LinkedHashMap<TopicIdPartition, Integer> rotateRoundRobin(
+ LinkedHashMap<TopicIdPartition, Integer> topicIdPartitions,
+ PartitionRotateMetadata metadata
+ ) {
+ if (topicIdPartitions.isEmpty() || topicIdPartitions.size() == 1 || metadata.sessionEpoch < 1) {
+ // No need to rotate the partitions if there are no partitions, only one partition or the
+ // session epoch is initial or final.
+ return topicIdPartitions;
+ }
+
+ int rotateAt = metadata.sessionEpoch % topicIdPartitions.size();
+ if (rotateAt == 0) {
+ // No need to rotate the partitions if the rotateAt is 0.
+ return topicIdPartitions;
+ }
+
+ // Once the partition max bytes is removed then the partition will be a linked list and rotation
Review Comment:
Done.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17951: Share parition rotate strategy [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #18651:
URL: https://github.com/apache/kafka/pull/18651#discussion_r1931143901
##########
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:
I wanted to explicitly mention that rotation should happen at 1, hence wrote it. Though I have missed a comment on couple of lines above about the test, added that.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17951: Share parition rotate strategy [kafka]
Posted by "AndrewJSchofield (via GitHub)" <gi...@apache.org>.
AndrewJSchofield merged PR #18651:
URL: https://github.com/apache/kafka/pull/18651
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org