AndrewJSchofield commented on code in PR #23911:
URL: https://github.com/apache/kafka/pull/23911#discussion_r4348625223


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -107,6 +108,10 @@ public class DelayedShareFetch extends DelayedOperation {
     private long acquireStartTimeMs;
     private LinkedHashMap<TopicIdPartition, Long> partitionsAcquired;
     private LinkedHashMap<TopicIdPartition, LogReadResult> 
localPartitionsAlreadyFetched;
+    /**
+    * The cache of fetch offset metadata for the share fetch request. This is 
used to avoid redundant reads.

Review Comment:
   nit: Indent line 112 by one more space 😆 



##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerFairnessTest.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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.clients.consumer;
+
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterConfigProperty;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.Timeout;
+
+import java.time.Duration;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@Timeout(600)
+@ClusterTestDefaults(
+    types = {Type.KRAFT},
+    serverProperties = {
+        @ClusterConfigProperty(key = "auto.create.topics.enable", value = 
"false"),
+        @ClusterConfigProperty(key = 
"group.share.max.partition.max.record.locks", value = "10000"),
+        @ClusterConfigProperty(key = "group.share.partition.max.record.locks", 
value = "10000"),
+        @ClusterConfigProperty(key = "group.share.record.lock.duration.ms", 
value = "60000"),
+        @ClusterConfigProperty(key = "offsets.topic.replication.factor", value 
= "1"),
+        @ClusterConfigProperty(key = "share.coordinator.state.topic.min.isr", 
value = "1"),
+        @ClusterConfigProperty(key = 
"share.coordinator.state.topic.num.partitions", value = "3"),
+        @ClusterConfigProperty(key = 
"share.coordinator.state.topic.replication.factor", value = "1"),
+        @ClusterConfigProperty(key = "transaction.state.log.min.isr", value = 
"1"),
+        @ClusterConfigProperty(key = 
"transaction.state.log.replication.factor", value = "1")
+    }
+)
+/**
+ * Verifies that a share fetch spreads its record budget across all the 
partitions with data and does not
+ * keep serving the same partition while the others are starved.
+ */
+public class ShareConsumerFairnessTest extends ShareConsumerTestBase {
+
+    private static final int RECORDS_PER_PARTITION = 8000;
+    private static final int MAX_POLL_RECORDS = 100;
+    private static final int POLLS = 60;
+
+    public ShareConsumerFairnessTest(ClusterInstance cluster) {
+        super(cluster);
+    }
+
+    @ClusterTest
+    public void testPartitionsWithDeepBacklogAreServedFairly() throws 
Exception {
+        String topic = "fairness";
+        String groupId = "fairness-group";
+        int partitions = 2;
+        createTopic(topic, partitions, 1);
+        try (Producer<byte[], byte[]> producer = createProducer()) {
+            for (int i = 0; i < RECORDS_PER_PARTITION; i++) {
+                for (int p = 0; p < partitions; p++) {
+                    producer.send(new ProducerRecord<>(topic, p, null, null, 
"value".getBytes()));
+                }
+            }
+            producer.flush();
+        }
+        alterShareAutoOffsetReset(groupId, "earliest");
+
+        Map<Integer, Integer> recordsByPartition = new TreeMap<>();
+        try (ShareConsumer<byte[], byte[]> consumer = 
createShareConsumer(groupId, Map.of(
+            ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 
String.valueOf(MAX_POLL_RECORDS),
+            ConsumerConfig.SHARE_ACQUIRE_MODE_CONFIG, "record_limit"))) {
+            consumer.subscribe(Set.of(topic));
+            for (int poll = 0; poll < POLLS; poll++) {

Review Comment:
   I think I'd prefer to see this test actually consume all of the records and 
not be limited by a specific number of polls. If some records are not 
delivered, the test's timeout would catch that as a failure.



##########
core/src/test/java/kafka/server/share/DelayedShareFetchTest.java:
##########
@@ -965,7 +961,6 @@ public void 
testTryCompleteReleasesLocksWhenAsyncReadImmediatelyThrows() {
         SharePartition sp0 = mock(SharePartition.class);
         when(sp0.canAcquireRecords()).thenReturn(true);
         // Empty fetch offset metadata forces maybeReadFromLog to actually 
read from the log.

Review Comment:
   You removed the code but not the comment. Seems odd.



##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -591,19 +567,29 @@ private void 
maybeUpdateFetchOffsetMetadata(LinkedHashMap<TopicIdPartition, Long
                                                 
LinkedHashMap<TopicIdPartition, LogReadResult> readResponseData) {
         for (Map.Entry<TopicIdPartition, LogReadResult> entry : 
readResponseData.entrySet()) {
             TopicIdPartition topicIdPartition = entry.getKey();
-            SharePartition sharePartition = 
sharePartitions.get(topicIdPartition);
             LogReadResult logReadResult = entry.getValue();
             if (logReadResult.error().code() != Errors.NONE.code()) {
                 log.debug("Log read result {} errored out for topic partition 
{}",
                     logReadResult, topicIdPartition);
                 continue;
             }
-            sharePartition.updateFetchOffsetMetadata(
-                topicPartitionData.get(topicIdPartition),
+            updateFetchOffsetMetadataCache(topicIdPartition, 
topicPartitionData.get(topicIdPartition),
                 logReadResult.info().fetchOffsetMetadata);
         }
     }
 
+    private Optional<LogOffsetMetadata> fetchOffsetMetadata(TopicIdPartition 
topicIdPartition, long fetchOffset) {
+        CachedFetchOffsetMetadata cached = 
fetchOffsetMetadataCache.get(topicIdPartition);
+        if (cached == null || cached.offset() != fetchOffset || 
cached.offsetMetadata() == null)

Review Comment:
   I don't think `cached.offsetMetadata()` can be null.



##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerFairnessTest.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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.clients.consumer;
+
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterConfigProperty;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.Timeout;
+
+import java.time.Duration;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@Timeout(600)
+@ClusterTestDefaults(
+    types = {Type.KRAFT},
+    serverProperties = {
+        @ClusterConfigProperty(key = "auto.create.topics.enable", value = 
"false"),
+        @ClusterConfigProperty(key = 
"group.share.max.partition.max.record.locks", value = "10000"),
+        @ClusterConfigProperty(key = "group.share.partition.max.record.locks", 
value = "10000"),
+        @ClusterConfigProperty(key = "group.share.record.lock.duration.ms", 
value = "60000"),
+        @ClusterConfigProperty(key = "offsets.topic.replication.factor", value 
= "1"),
+        @ClusterConfigProperty(key = "share.coordinator.state.topic.min.isr", 
value = "1"),
+        @ClusterConfigProperty(key = 
"share.coordinator.state.topic.num.partitions", value = "3"),
+        @ClusterConfigProperty(key = 
"share.coordinator.state.topic.replication.factor", value = "1"),
+        @ClusterConfigProperty(key = "transaction.state.log.min.isr", value = 
"1"),
+        @ClusterConfigProperty(key = 
"transaction.state.log.replication.factor", value = "1")
+    }
+)
+/**

Review Comment:
   Normally would put the comment before the annotations.



##########
core/src/test/java/kafka/server/share/DelayedShareFetchTest.java:
##########
@@ -1497,9 +1484,6 @@ public void 
testRemoteStorageFetchTryCompleteReturnsFalse() {
         when(sp2.nextFetchOffset()).thenReturn(30L);
 
         // Fetch offset matches with the cached entry for sp0 but not for sp1 
and sp2. Hence, a replica manager fetch will happen for sp1 and sp2.

Review Comment:
   Should have removed the comment too I think.



##########
core/src/test/java/kafka/server/share/DelayedShareFetchTest.java:
##########
@@ -1648,7 +1633,6 @@ public void 
testRemoteStorageFetchPartitionLeaderChanged() {
         when(sp0.nextFetchOffset()).thenReturn(10L);
 
         // Fetch offset does not match with the cached entry for sp0, hence, a 
replica manager fetch will happen for sp0.

Review Comment:
   And here. I think it's a common pattern.



-- 
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]

Reply via email to