You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "aliehsaeedii (via GitHub)" <gi...@apache.org> on 2024/10/08 15:36:22 UTC

[PR] KAFKA-17600: Add nextOffsets to the ConsumerRecords [kafka]

aliehsaeedii opened a new pull request, #17414:
URL: https://github.com/apache/kafka/pull/17414

   This PR implements KIP-1094.
   


-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "aliehsaeedii (via GitHub)" <gi...@apache.org>.
aliehsaeedii commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1795584142


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/ShareConsumerImpl.java:
##########
@@ -583,7 +583,7 @@ public synchronized ConsumerRecords<K, V> poll(final Duration timeout) {
                 final ShareFetch<K, V> fetch = pollForFetches(timer);
                 if (!fetch.isEmpty()) {
                     currentFetch = fetch;
-                    return new ConsumerRecords<>(fetch.records());
+                    return new ConsumerRecords<>(fetch.records(), Collections.emptyMap());

Review Comment:
   > Trivial, but please test this in `ShareConsumerImplTest`.
   
   Mmm. `ShareConsumerImplTest` does not test the returned records as well.



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on PR #17414:
URL: https://github.com/apache/kafka/pull/17414#issuecomment-2407529164

   > I assume when control records are returned, partRecords is empty while the position is advanced.
   
   You are right. Anyway, I'd assume we add a TP to `nextOffsets` if the position advanced.
   
   I can have a look again tomorrow or next week to see if all comments have been addressed, and potentially merge it.


-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "kirktrue (via GitHub)" <gi...@apache.org>.
kirktrue commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1794387265


##########
clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecords.java:
##########
@@ -21,23 +21,33 @@
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+
 /**
  * A container that holds the list {@link ConsumerRecord} per partition for a
  * particular topic. There is one {@link ConsumerRecord} list for every topic
  * partition returned by a {@link Consumer#poll(java.time.Duration)} operation.
  */
 public class ConsumerRecords<K, V> implements Iterable<ConsumerRecord<K, V>> {
-    public static final ConsumerRecords<Object, Object> EMPTY = new ConsumerRecords<>(Collections.emptyMap());
+    public static final ConsumerRecords<Object, Object> EMPTY = new ConsumerRecords<>(Collections.emptyMap(), Collections.emptyMap());
 
     private final Map<TopicPartition, List<ConsumerRecord<K, V>>> records;
+    private final Map<TopicPartition, OffsetAndMetadata> nextOffsets;
 
+    @Deprecated
     public ConsumerRecords(Map<TopicPartition, List<ConsumerRecord<K, V>>> records) {
         this.records = records;
+        this.nextOffsets = new HashMap<>();

Review Comment:
   Since the `nextOffsets` map isn't modified after use, could this be:
   
   ```suggestion
           this.nextOffsets = Collections.emptyMap();
   ```
   
   Or, the entire constructor could be:
   
   ```java
           this(nextOffsets, Collections.emptyMap());
   ```



##########
clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java:
##########
@@ -229,6 +229,8 @@ public synchronized ConsumerRecords<K, V> poll(final Duration timeout) {
 
         // update the consumed offset
         final Map<TopicPartition, List<ConsumerRecord<K, V>>> results = new HashMap<>();
+        final Map<TopicPartition, OffsetAndMetadata> nextOffsetAndMetadata = new HashMap<>();
+

Review Comment:
   Super nit: remove the extra newline:
   
   ```suggestion
           final Map<TopicPartition, OffsetAndMetadata> nextOffsetAndMetadata = new HashMap<>();
   ```



##########
clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecords.java:
##########
@@ -53,6 +63,13 @@ public List<ConsumerRecord<K, V>> records(TopicPartition partition) {
             return Collections.unmodifiableList(recs);
     }
 
+    /**
+     * Get just the next offsets that the consumer will consume

Review Comment:
   In her defense, the comments for the existing `records()` method are similarly terse 🤷‍♂️



##########
clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecords.java:
##########
@@ -21,23 +21,33 @@
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+
 /**
  * A container that holds the list {@link ConsumerRecord} per partition for a
  * particular topic. There is one {@link ConsumerRecord} list for every topic
  * partition returned by a {@link Consumer#poll(java.time.Duration)} operation.
  */
 public class ConsumerRecords<K, V> implements Iterable<ConsumerRecord<K, V>> {
-    public static final ConsumerRecords<Object, Object> EMPTY = new ConsumerRecords<>(Collections.emptyMap());
+    public static final ConsumerRecords<Object, Object> EMPTY = new ConsumerRecords<>(Collections.emptyMap(), Collections.emptyMap());
 
     private final Map<TopicPartition, List<ConsumerRecord<K, V>>> records;
+    private final Map<TopicPartition, OffsetAndMetadata> nextOffsets;
 
+    @Deprecated
     public ConsumerRecords(Map<TopicPartition, List<ConsumerRecord<K, V>>> records) {
         this.records = records;
+        this.nextOffsets = new HashMap<>();
+    }
+
+    public ConsumerRecords(Map<TopicPartition, List<ConsumerRecord<K, V>>> records, final Map<TopicPartition, OffsetAndMetadata> nextOffsets) {
+        this.records = records;
+        this.nextOffsets = nextOffsets;

Review Comment:
   Should this be immutable?
   
   ```suggestion
           this.nextOffsets = Collections.unmodifiableMap(nextOffsets);
   ```



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/ShareConsumerImpl.java:
##########
@@ -583,7 +583,7 @@ public synchronized ConsumerRecords<K, V> poll(final Duration timeout) {
                 final ShareFetch<K, V> fetch = pollForFetches(timer);
                 if (!fetch.isEmpty()) {
                     currentFetch = fetch;
-                    return new ConsumerRecords<>(fetch.records());
+                    return new ConsumerRecords<>(fetch.records(), Collections.emptyMap());

Review Comment:
   Any reason to not leave this line as-is so that it the single-arg `ConsumerRecords` is used?



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "aliehsaeedii (via GitHub)" <gi...@apache.org>.
aliehsaeedii commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1795562350


##########
clients/src/test/java/org/apache/kafka/clients/consumer/MockConsumerTest.java:
##########
@@ -64,41 +64,11 @@ public void testSimpleMock() {
         assertFalse(iter.hasNext());
         final TopicPartition tp = new TopicPartition("test", 0);
         assertEquals(2L, consumer.position(tp));
+        assertEquals(new OffsetAndMetadata(2, Optional.empty(), ""), recs.nextOffsets().get(tp));
         consumer.commitSync();
         assertEquals(2L, consumer.committed(Collections.singleton(tp)).get(tp).offset());
     }
 
-    @SuppressWarnings("deprecation")
-    @Test
-    public void testSimpleMockDeprecated() {

Review Comment:
   to resolve the conflict with `trunk`. Maybe rebasing was a better approach.



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1809092349


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetchCollectorTest.java:
##########
@@ -620,10 +645,49 @@ public void testCollectFetchInitializationOffsetOutOfRangeErrorWithOffsetReset()
         final Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
 
         assertTrue(fetch.isEmpty());
+        assertEquals(0, fetch.nextOffsets().size());
         verify(subscriptions).requestOffsetResetIfPartitionAssigned(topicPartition0);
         verify(fetchBuffer).setNextInLineFetch(null);
     }
 
+    @Test
+    public void testReadCommittedWithAbortedTransaction() {
+        buildDependencies();

Review Comment:
   Where are you configuring the fetch collector to use read_committed mode? I think you somehow have to extend `buildDependencies` to actually do what you state in the test name. I debugged your unit test, and it indeed runs in read_uncommmitted mode. So I don't think it's testing what Andrew was asking for. Check out how `fetchConfig` is constructed.



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1810544183


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetchCollectorTest.java:
##########
@@ -620,10 +646,49 @@ public void testCollectFetchInitializationOffsetOutOfRangeErrorWithOffsetReset()
         final Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
 
         assertTrue(fetch.isEmpty());
+        assertEquals(0, fetch.nextOffsets().size());
         verify(subscriptions).requestOffsetResetIfPartitionAssigned(topicPartition0);
         verify(fetchBuffer).setNextInLineFetch(null);
     }
 
+    @Test
+    public void testReadCommittedWithAbortedTransaction() {
+        buildDependencies(IsolationLevel.READ_COMMITTED);
+        int recordCount = 20;
+        assignAndSeek(topicAPartition0);
+
+
+        // Initial CompletedFetch
+        CompletedFetch initialCompletedFetch = completedFetchBuilder
+            .recordCount(recordCount)
+            .build();
+        fetchBuffer.add(initialCompletedFetch);
+        Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
+
+        assertFalse(fetch.isEmpty());
+        assertEquals(recordCount, fetch.numRecords());
+        assertEquals(1, fetch.nextOffsets().size());
+        assertEquals(new OffsetAndMetadata(fetch.numRecords(), Optional.empty(), ""), fetch.nextOffsets().get(topicAPartition0));
+
+
+        // Subsequent CompletedFetch with abort marker
+        Records rawRecords = createTranscactionalRecords(ControlRecordType.ABORT, recordCount);
+        FetchResponseData.PartitionData partitionData = new FetchResponseData.PartitionData()
+            .setRecords(rawRecords);

Review Comment:
   If I understand correctly, the Andrew was asking for the ABORT control record to come first, before the other records.



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1810540186


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetchCollectorTest.java:
##########
@@ -620,10 +646,49 @@ public void testCollectFetchInitializationOffsetOutOfRangeErrorWithOffsetReset()
         final Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
 
         assertTrue(fetch.isEmpty());
+        assertEquals(0, fetch.nextOffsets().size());
         verify(subscriptions).requestOffsetResetIfPartitionAssigned(topicPartition0);
         verify(fetchBuffer).setNextInLineFetch(null);
     }
 
+    @Test
+    public void testReadCommittedWithAbortedTransaction() {
+        buildDependencies(IsolationLevel.READ_COMMITTED);
+        int recordCount = 20;
+        assignAndSeek(topicAPartition0);
+
+
+        // Initial CompletedFetch
+        CompletedFetch initialCompletedFetch = completedFetchBuilder

Review Comment:
   I think you may want to also make the first completedFetch a transactional completed fetch. See `CompletedFetchTest` for an example. It probably does not matter whether we include a commit marker or not.



##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetchCollectorTest.java:
##########
@@ -620,10 +646,49 @@ public void testCollectFetchInitializationOffsetOutOfRangeErrorWithOffsetReset()
         final Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
 
         assertTrue(fetch.isEmpty());
+        assertEquals(0, fetch.nextOffsets().size());
         verify(subscriptions).requestOffsetResetIfPartitionAssigned(topicPartition0);
         verify(fetchBuffer).setNextInLineFetch(null);
     }
 
+    @Test
+    public void testReadCommittedWithAbortedTransaction() {
+        buildDependencies(IsolationLevel.READ_COMMITTED);
+        int recordCount = 20;
+        assignAndSeek(topicAPartition0);
+
+
+        // Initial CompletedFetch
+        CompletedFetch initialCompletedFetch = completedFetchBuilder
+            .recordCount(recordCount)
+            .build();
+        fetchBuffer.add(initialCompletedFetch);
+        Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
+
+        assertFalse(fetch.isEmpty());
+        assertEquals(recordCount, fetch.numRecords());
+        assertEquals(1, fetch.nextOffsets().size());
+        assertEquals(new OffsetAndMetadata(fetch.numRecords(), Optional.empty(), ""), fetch.nextOffsets().get(topicAPartition0));

Review Comment:
   nit: `fetch` is the result we are checking, don't use it on the left side of the assert, that's a bit confusing. Use `recordCount`



##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetchCollectorTest.java:
##########
@@ -620,10 +646,49 @@ public void testCollectFetchInitializationOffsetOutOfRangeErrorWithOffsetReset()
         final Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
 
         assertTrue(fetch.isEmpty());
+        assertEquals(0, fetch.nextOffsets().size());
         verify(subscriptions).requestOffsetResetIfPartitionAssigned(topicPartition0);
         verify(fetchBuffer).setNextInLineFetch(null);
     }
 
+    @Test
+    public void testReadCommittedWithAbortedTransaction() {
+        buildDependencies(IsolationLevel.READ_COMMITTED);
+        int recordCount = 20;
+        assignAndSeek(topicAPartition0);
+
+
+        // Initial CompletedFetch
+        CompletedFetch initialCompletedFetch = completedFetchBuilder
+            .recordCount(recordCount)
+            .build();
+        fetchBuffer.add(initialCompletedFetch);
+        Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
+
+        assertFalse(fetch.isEmpty());
+        assertEquals(recordCount, fetch.numRecords());
+        assertEquals(1, fetch.nextOffsets().size());
+        assertEquals(new OffsetAndMetadata(fetch.numRecords(), Optional.empty(), ""), fetch.nextOffsets().get(topicAPartition0));
+
+

Review Comment:
   nit: double new line (all other tests use single new line)



##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetchCollectorTest.java:
##########
@@ -620,10 +646,49 @@ public void testCollectFetchInitializationOffsetOutOfRangeErrorWithOffsetReset()
         final Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
 
         assertTrue(fetch.isEmpty());
+        assertEquals(0, fetch.nextOffsets().size());
         verify(subscriptions).requestOffsetResetIfPartitionAssigned(topicPartition0);
         verify(fetchBuffer).setNextInLineFetch(null);
     }
 
+    @Test
+    public void testReadCommittedWithAbortedTransaction() {
+        buildDependencies(IsolationLevel.READ_COMMITTED);
+        int recordCount = 20;
+        assignAndSeek(topicAPartition0);
+
+
+        // Initial CompletedFetch
+        CompletedFetch initialCompletedFetch = completedFetchBuilder
+            .recordCount(recordCount)
+            .build();
+        fetchBuffer.add(initialCompletedFetch);
+        Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
+
+        assertFalse(fetch.isEmpty());
+        assertEquals(recordCount, fetch.numRecords());
+        assertEquals(1, fetch.nextOffsets().size());
+        assertEquals(new OffsetAndMetadata(fetch.numRecords(), Optional.empty(), ""), fetch.nextOffsets().get(topicAPartition0));
+
+
+        // Subsequent CompletedFetch with abort marker
+        Records rawRecords = createTranscactionalRecords(ControlRecordType.ABORT, recordCount);
+        FetchResponseData.PartitionData partitionData = new FetchResponseData.PartitionData()
+            .setRecords(rawRecords);

Review Comment:
   I think for the `FetchResponseData` to be correctly formed, we alos have to `setAbortedTransactions`. See, for example, `CompletedFetchTest`.



##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetchCollectorTest.java:
##########
@@ -840,7 +931,22 @@ private Records createRecords(final int recordCount) {
             for (int i = 0; i < recordCount; i++)
                 builder.append(0L, "key".getBytes(), ("value-" + i).getBytes());
 
-            return builder.build();
+            return builder;
+        }
+    }
+
+    private Records createTranscactionalRecords(ControlRecordType controlRecordType, int recordCount) {

Review Comment:
   nit: `Transactional` not `Trasncactional`



##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetchCollectorTest.java:
##########
@@ -620,10 +646,49 @@ public void testCollectFetchInitializationOffsetOutOfRangeErrorWithOffsetReset()
         final Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
 
         assertTrue(fetch.isEmpty());
+        assertEquals(0, fetch.nextOffsets().size());
         verify(subscriptions).requestOffsetResetIfPartitionAssigned(topicPartition0);
         verify(fetchBuffer).setNextInLineFetch(null);
     }
 
+    @Test
+    public void testReadCommittedWithAbortedTransaction() {
+        buildDependencies(IsolationLevel.READ_COMMITTED);
+        int recordCount = 20;
+        assignAndSeek(topicAPartition0);
+
+

Review Comment:
   nit: double new line (all other tests use single new line)



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "aliehsaeedii (via GitHub)" <gi...@apache.org>.
aliehsaeedii commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1795560527


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/ShareConsumerImpl.java:
##########
@@ -583,7 +583,7 @@ public synchronized ConsumerRecords<K, V> poll(final Duration timeout) {
                 final ShareFetch<K, V> fetch = pollForFetches(timer);
                 if (!fetch.isEmpty()) {
                     currentFetch = fetch;
-                    return new ConsumerRecords<>(fetch.records());
+                    return new ConsumerRecords<>(fetch.records(), Collections.emptyMap());

Review Comment:
   >Any reason to not leave this line as-is so that it the single-arg ConsumerRecords is used?
   
   since the current constructor with single-arg is marked as `@Deprecated`.



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1793530163


##########
clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecords.java:
##########
@@ -53,6 +63,13 @@ public List<ConsumerRecord<K, V>> records(TopicPartition partition) {
             return Collections.unmodifiableList(recs);
     }
 
+    /**
+     * Get just the next offsets that the consumer will consume
+     */
+    public Map<TopicPartition, OffsetAndMetadata> nextOffsets() {
+        return nextOffsets;

Review Comment:
   Other methods in this class return an unmodifiable collection, should we do the same?



##########
clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecords.java:
##########
@@ -53,6 +63,13 @@ public List<ConsumerRecord<K, V>> records(TopicPartition partition) {
             return Collections.unmodifiableList(recs);
     }
 
+    /**
+     * Get just the next offsets that the consumer will consume

Review Comment:
   I think this comment needs to be more precise. Which topic partitions are included, all the partitions that are assigned to the consumer, all partitions that I fetched from in the last batch? I suppose it's the latter



##########
clients/src/test/java/org/apache/kafka/clients/consumer/MockConsumerTest.java:
##########
@@ -64,41 +64,11 @@ public void testSimpleMock() {
         assertFalse(iter.hasNext());
         final TopicPartition tp = new TopicPartition("test", 0);
         assertEquals(2L, consumer.position(tp));
+        assertEquals(new OffsetAndMetadata(2, Optional.empty(), ""), recs.nextOffsets().get(tp));
         consumer.commitSync();
         assertEquals(2L, consumer.committed(Collections.singleton(tp)).get(tp).offset());
     }
 
-    @SuppressWarnings("deprecation")
-    @Test
-    public void testSimpleMockDeprecated() {

Review Comment:
   why did you remove this test?



##########
clients/src/test/java/org/apache/kafka/clients/consumer/MockConsumerTest.java:
##########
@@ -125,6 +95,7 @@ public void shouldNotClearRecordsForPausedPartitions() {
         consumer.resume(testPartitionList);
         ConsumerRecords<String, String> recordsSecondPoll = consumer.poll(Duration.ofMillis(1));
         assertEquals(1, recordsSecondPoll.count());
+        assertEquals(new OffsetAndMetadata(1, Optional.empty(), ""), recordsSecondPoll.nextOffsets().get(new TopicPartition("test", 0)));

Review Comment:
   We may want to compare all elements of `recordsSecondPoll.nextOffsets`, or at least check the size of the collection.



##########
clients/src/test/java/org/apache/kafka/clients/consumer/ConsumerRecordsTest.java:
##########
@@ -178,6 +179,7 @@ private ConsumerRecords<Integer, String> buildTopicTestRecords(int recordSize,
                                                                    int emptyPartitionIndex,
                                                                    Collection<String> topics) {
         Map<TopicPartition, List<ConsumerRecord<Integer, String>>> partitionToRecords = new LinkedHashMap<>();
+        Map<TopicPartition, OffsetAndMetadata> nextOffsets = new HashMap<>();

Review Comment:
   In `ConsumerRecordsTest`, you insert data but never check anything on it. Don't you want to verify something?



##########
clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecords.java:
##########
@@ -21,23 +21,33 @@
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+

Review Comment:
   extra new line



##########
clients/src/test/java/org/apache/kafka/clients/consumer/MockConsumerTest.java:
##########
@@ -64,41 +64,11 @@ public void testSimpleMock() {
         assertFalse(iter.hasNext());
         final TopicPartition tp = new TopicPartition("test", 0);
         assertEquals(2L, consumer.position(tp));
+        assertEquals(new OffsetAndMetadata(2, Optional.empty(), ""), recs.nextOffsets().get(tp));

Review Comment:
   We may want to compare all elements of `recs.nextOffsets`, or at least check the size of the collection.



##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerInterceptorsTest.java:
##########
@@ -126,9 +131,15 @@ public void testOnConsumeChain() {
         list3.add(new ConsumerRecord<>(filterTopicPart2.topic(), filterTopicPart2.partition(), 0, 0L, TimestampType.CREATE_TIME,
             0, 0, 1, 1, new RecordHeaders(), Optional.empty()));
         records.put(tp, list1);
+        nextOffsets.put(tp, new OffsetAndMetadata(1, Optional.empty(), ""));
+
         records.put(filterTopicPart1, list2);
+        nextOffsets.put(filterTopicPart1, new OffsetAndMetadata(1, Optional.empty(), ""));
+
         records.put(filterTopicPart2, list3);
-        ConsumerRecords<Integer, Integer> consumerRecords = new ConsumerRecords<>(records);
+        nextOffsets.put(filterTopicPart2, new OffsetAndMetadata(1, Optional.empty(), ""));
+
+        ConsumerRecords<Integer, Integer> consumerRecords = new ConsumerRecords<>(records, nextOffsets);
         ConsumerRecords<Integer, Integer> interceptedRecords = interceptors.onConsume(consumerRecords);
         assertEquals(1, interceptedRecords.count());
         assertTrue(interceptedRecords.partitions().contains(tp));

Review Comment:
   I think we need to assert here something about the result of nextOffsets right? That those topic partitions are filtered out?



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/FetchCollector.java:
##########
@@ -195,7 +196,7 @@ private Fetch<K, V> fetchRecords(final CompletedFetch nextInLineFetch, int maxRe
                     metricsManager.recordPartitionLead(tp, lead);
                 }
 
-                return Fetch.forPartition(tp, partRecords, positionAdvanced);
+                return Fetch.forPartition(tp, partRecords, positionAdvanced, new OffsetAndMetadata(nextInLineFetch.nextFetchOffset(), nextInLineFetch.lastEpoch(), ""));

Review Comment:
   We _definitely_ need to update / add tests for this change in `FetchCollectorTest`



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/ShareConsumerImpl.java:
##########
@@ -583,7 +583,7 @@ public synchronized ConsumerRecords<K, V> poll(final Duration timeout) {
                 final ShareFetch<K, V> fetch = pollForFetches(timer);
                 if (!fetch.isEmpty()) {
                     currentFetch = fetch;
-                    return new ConsumerRecords<>(fetch.records());
+                    return new ConsumerRecords<>(fetch.records(), Collections.emptyMap());

Review Comment:
   Trivial, but please test this in `ShareConsumerImplTest`.



##########
connect/runtime/src/test/java/org/apache/kafka/connect/util/clusters/EmbeddedKafkaCluster.java:
##########
@@ -480,7 +481,7 @@ public ConsumerRecords<byte[], byte[]> consume(int n, long maxDuration, Map<Stri
                     consumedRecords += r.size();
                 }
                 if (consumedRecords >= n) {
-                    return new ConsumerRecords<>(records);
+                    return new ConsumerRecords<>(records, rec.nextOffsets());

Review Comment:
   Not sure if this is correct. Aren't you dropping all the `nextOffsets` for the previous polls? Like you are aggregating `rec.records` in `records`, you should aggregate `rec.nextOffsets` somewhere.



##########
clients/src/main/java/org/apache/kafka/clients/consumer/MockShareConsumer.java:
##########
@@ -91,7 +91,7 @@ public synchronized ConsumerRecords<K, V> poll(Duration timeout) {
         }
 
         records.clear();
-        return new ConsumerRecords<>(results);
+        return new ConsumerRecords<>(results, Collections.emptyMap());

Review Comment:
   Could you check the result of `nextOffsets` in `MockShareConsumerTest` ? Generally, try to accompany each change with a change in the unit tests, if possible.



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/Fetch.java:
##########
@@ -95,6 +102,13 @@ public int numRecords() {
         return numRecords;
     }
 
+    /**
+     * @return the next offsets and metadata (last epochs is included)
+     */
+    public Map<TopicPartition, OffsetAndMetadata> nextOffsetAndMetadata() {
+        return nextOffsetAndMetadata;

Review Comment:
   `records` returns an unmodifiableMap. Should we do the same here?



##########
clients/src/test/java/org/apache/kafka/test/MockConsumerInterceptor.java:
##########
@@ -73,19 +73,24 @@ public ConsumerRecords<String, String> onConsume(ConsumerRecords<String, String>
         CLUSTER_ID_BEFORE_ON_CONSUME.compareAndSet(NO_CLUSTER_ID, CLUSTER_META.get());
 
         Map<TopicPartition, List<ConsumerRecord<String, String>>> recordMap = new HashMap<>();
+        Map<TopicPartition, OffsetAndMetadata> nextOffsets = new HashMap<>();
+
         for (TopicPartition tp : records.partitions()) {
             List<ConsumerRecord<String, String>> lst = new ArrayList<>();
+            long nextOffset = 0;

Review Comment:
   Have you considered the case here where the records for the topic partition is empty? Won't you return "next offset 0" in this case?



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "aliehsaeedii (via GitHub)" <gi...@apache.org>.
aliehsaeedii commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1799936825


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/Fetch.java:
##########
@@ -95,6 +102,13 @@ public int numRecords() {
         return numRecords;
     }
 
+    /**
+     * @return the next offsets and metadata (last epochs is included)
+     */
+    public Map<TopicPartition, OffsetAndMetadata> nextOffsetAndMetadata() {
+        return Collections.unmodifiableMap(nextOffsetAndMetadata);

Review Comment:
   `Map.of` itself returns an immutable map. Maybe no need to create a copy?



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "aliehsaeedii (via GitHub)" <gi...@apache.org>.
aliehsaeedii commented on PR #17414:
URL: https://github.com/apache/kafka/pull/17414#issuecomment-2411802079

   Thanks @AndrewJSchofield for the pointer. Yes, I followed the code, and as I stated before, when a control record is returned, the size of `partRecords` is `0` while the offset is updated.  


-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1810358829


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetchCollectorTest.java:
##########
@@ -652,7 +653,7 @@ public void testCollectFetchInitializationOffsetOutOfRangeErrorWithOffsetReset()
 
     @Test
     public void testReadCommittedWithAbortedTransaction() {
-        buildDependencies();
+        buildDependencies(IsolationLevel.READ_UNCOMMITTED);

Review Comment:
   Don't you mean READ_COMMITTED here?



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "aliehsaeedii (via GitHub)" <gi...@apache.org>.
aliehsaeedii commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1796969771


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/FetchCollector.java:
##########
@@ -195,7 +196,7 @@ private Fetch<K, V> fetchRecords(final CompletedFetch nextInLineFetch, int maxRe
                     metricsManager.recordPartitionLead(tp, lead);
                 }
 
-                return Fetch.forPartition(tp, partRecords, positionAdvanced);
+                return Fetch.forPartition(tp, partRecords, positionAdvanced, new OffsetAndMetadata(nextInLineFetch.nextFetchOffset(), nextInLineFetch.lastEpoch(), ""));

Review Comment:
   >we promise to return only those topic partitions that have fetched something?
   
   What is `something`? Can we distinguish a `real nothing` and a `control record`?



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "AndrewJSchofield (via GitHub)" <gi...@apache.org>.
AndrewJSchofield commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1812442065


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetchCollectorTest.java:
##########
@@ -620,10 +650,61 @@ public void testCollectFetchInitializationOffsetOutOfRangeErrorWithOffsetReset()
         final Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
 
         assertTrue(fetch.isEmpty());
+        assertEquals(0, fetch.nextOffsets().size());
         verify(subscriptions).requestOffsetResetIfPartitionAssigned(topicPartition0);
         verify(fetchBuffer).setNextInLineFetch(null);
     }
 
+    @Test
+    public void testReadCommittedWithAbortedTransaction() {
+        buildDependencies(IsolationLevel.READ_COMMITTED);
+        int recordCount = 20;
+        assignAndSeek(topicAPartition0);
+
+        /* The first CompletedFetch object */
+        Records rawRecords = createTransactionalRecords(ControlRecordType.ABORT, true, 0, recordCount);
+        FetchResponseData.PartitionData partitionData = new FetchResponseData.PartitionData()
+            .setRecords(rawRecords)
+            .setAbortedTransactions(createAbortedTransactions());
+        CompletedFetch completedFetch1 = completedFetchBuilder
+            .partitionData(partitionData)
+            .build();
+        fetchBuffer.add(completedFetch1);
+        Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
+
+        // The Fetch object contains no data record but the offset is moved forward
+        assertFalse(fetch.isEmpty());
+        assertEquals(0, fetch.numRecords());
+        assertEquals(1, fetch.nextOffsets().size());
+        assertEquals(new OffsetAndMetadata(recordCount + 1, Optional.of(0), ""), fetch.nextOffsets().get(topicAPartition0));
+
+        /* The second CompletedFetch object */
+        int startOffset = recordCount + 1;
+        rawRecords = createTransactionalRecords(ControlRecordType.ABORT, false, startOffset, recordCount);
+        partitionData = new FetchResponseData.PartitionData()
+            .setRecords(rawRecords)
+            .setAbortedTransactions(createAbortedTransactions());
+        CompletedFetch completedFetch2 = completedFetchBuilder
+            .partitionData(partitionData)
+            .fetchOffset(startOffset)
+            .build();
+        fetchBuffer.add(completedFetch2);
+        fetch = fetchCollector.collectFetch(fetchBuffer);
+
+        // The Fetch object contains both data records and the advanced offset
+        assertFalse(fetch.isEmpty());
+        assertEquals(recordCount, fetch.numRecords());
+        assertEquals(1, fetch.nextOffsets().size());
+        assertEquals(new OffsetAndMetadata(startOffset + recordCount + 1, Optional.of(0), ""), fetch.nextOffsets().get(topicAPartition0));
+    }
+
+    private List<FetchResponseData.AbortedTransaction> createAbortedTransactions() {

Review Comment:
   I think `createAbortedTransaction` (singular) would be better. Also, I think you need to supply the `firstOffset` as a parameter which should be the offset of the first record of the transaction which is being aborted.



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1796868931


##########
clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecords.java:
##########
@@ -53,6 +60,14 @@ public List<ConsumerRecord<K, V>> records(TopicPartition partition) {
             return Collections.unmodifiableList(recs);
     }
 
+    /**
+     * Get the next offsets and metadata corresponding to all partitions fetched from the last batch.

Review Comment:
   Why `fetched from the last batch`? When I follow the code, it seems the result can include the offsets from multiple batches?
   
   @kirktrue made a fair point that other comments are also not good in this class. But for this method specifically, I think it should be clear for which topic-partitions we include offsets and for which topic-partitions we don't, as this will define what offsets we commit, and committing tends to be important in Kafka. So it should be very clear what is the set of offsets returned by this method.
   
   I think we can say that we include offsets for all topic-partitions for which the position advanced in this poll call. But then, we need to change the code a bit (below). 
   
   For me, this is not a minor point.  



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/Fetch.java:
##########
@@ -95,6 +102,13 @@ public int numRecords() {
         return numRecords;
     }
 
+    /**
+     * @return the next offsets and metadata (last epochs is included)

Review Comment:
   nit: `last epoch` not `last epochs`



##########
clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecords.java:
##########
@@ -32,12 +32,19 @@
  * partition returned by a {@link Consumer#poll(java.time.Duration)} operation.
  */
 public class ConsumerRecords<K, V> implements Iterable<ConsumerRecord<K, V>> {
-    public static final ConsumerRecords<Object, Object> EMPTY = new ConsumerRecords<>(Collections.emptyMap());
+    public static final ConsumerRecords<Object, Object> EMPTY = new ConsumerRecords<>(Collections.emptyMap(), Collections.emptyMap());
 
     private final Map<TopicPartition, List<ConsumerRecord<K, V>>> records;
+    private final Map<TopicPartition, OffsetAndMetadata> nextOffsets;
 
+    @Deprecated
     public ConsumerRecords(Map<TopicPartition, List<ConsumerRecord<K, V>>> records) {
+        this(records, Collections.emptyMap());
+    }
+
+    public ConsumerRecords(Map<TopicPartition, List<ConsumerRecord<K, V>>> records, final Map<TopicPartition, OffsetAndMetadata> nextOffsets) {
         this.records = records;
+        this.nextOffsets = Collections.unmodifiableMap(nextOffsets);

Review Comment:
   Now, you are doing `Collections.unmodifiableMap` twice. Once in the getter, once in the contructor. I would remove it here.



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/FetchCollector.java:
##########
@@ -195,7 +196,7 @@ private Fetch<K, V> fetchRecords(final CompletedFetch nextInLineFetch, int maxRe
                     metricsManager.recordPartitionLead(tp, lead);
                 }
 
-                return Fetch.forPartition(tp, partRecords, positionAdvanced);
+                return Fetch.forPartition(tp, partRecords, positionAdvanced, new OffsetAndMetadata(nextInLineFetch.nextFetchOffset(), nextInLineFetch.lastEpoch(), ""));

Review Comment:
   Thanks for the update. 
   
   One more thing that I noticed - not 100% sure if it matters. In this code, even if the `position` does not advance (i.e., we haven't really fetched anything), we will still return the corresponding "nextOffset". I suppose (without checking further) that this would mean that `partRecords` is empty here.
   
   I'm not sure if we should include a TP that did not advance its position at all in `nextOffsets`. After all, we promise to return only those topic partitions that have fetched something?
   
   This is also why I pressed on a more precise comment above -- for which topic partitions do we precisely include a next offset, and for which do we not include one?
   
   Thoughts? @kirktrue @aliehsaeedii 



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "aliehsaeedii (via GitHub)" <gi...@apache.org>.
aliehsaeedii commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1795608179


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerInterceptorsTest.java:
##########
@@ -126,9 +131,15 @@ public void testOnConsumeChain() {
         list3.add(new ConsumerRecord<>(filterTopicPart2.topic(), filterTopicPart2.partition(), 0, 0L, TimestampType.CREATE_TIME,
             0, 0, 1, 1, new RecordHeaders(), Optional.empty()));
         records.put(tp, list1);
+        nextOffsets.put(tp, new OffsetAndMetadata(1, Optional.empty(), ""));
+
         records.put(filterTopicPart1, list2);
+        nextOffsets.put(filterTopicPart1, new OffsetAndMetadata(1, Optional.empty(), ""));
+
         records.put(filterTopicPart2, list3);
-        ConsumerRecords<Integer, Integer> consumerRecords = new ConsumerRecords<>(records);
+        nextOffsets.put(filterTopicPart2, new OffsetAndMetadata(1, Optional.empty(), ""));
+
+        ConsumerRecords<Integer, Integer> consumerRecords = new ConsumerRecords<>(records, nextOffsets);
         ConsumerRecords<Integer, Integer> interceptedRecords = interceptors.onConsume(consumerRecords);
         assertEquals(1, interceptedRecords.count());
         assertTrue(interceptedRecords.partitions().contains(tp));

Review Comment:
   >those topic partitions are filtered out?
   but we still keep the filtered out tps in `nextOffsets` map.



##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerInterceptorsTest.java:
##########
@@ -126,9 +131,15 @@ public void testOnConsumeChain() {
         list3.add(new ConsumerRecord<>(filterTopicPart2.topic(), filterTopicPart2.partition(), 0, 0L, TimestampType.CREATE_TIME,
             0, 0, 1, 1, new RecordHeaders(), Optional.empty()));
         records.put(tp, list1);
+        nextOffsets.put(tp, new OffsetAndMetadata(1, Optional.empty(), ""));
+
         records.put(filterTopicPart1, list2);
+        nextOffsets.put(filterTopicPart1, new OffsetAndMetadata(1, Optional.empty(), ""));
+
         records.put(filterTopicPart2, list3);
-        ConsumerRecords<Integer, Integer> consumerRecords = new ConsumerRecords<>(records);
+        nextOffsets.put(filterTopicPart2, new OffsetAndMetadata(1, Optional.empty(), ""));
+
+        ConsumerRecords<Integer, Integer> consumerRecords = new ConsumerRecords<>(records, nextOffsets);
         ConsumerRecords<Integer, Integer> interceptedRecords = interceptors.onConsume(consumerRecords);
         assertEquals(1, interceptedRecords.count());
         assertTrue(interceptedRecords.partitions().contains(tp));

Review Comment:
   >those topic partitions are filtered out?
   
   but we still keep the filtered out tps in `nextOffsets` 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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "aliehsaeedii (via GitHub)" <gi...@apache.org>.
aliehsaeedii commented on PR #17414:
URL: https://github.com/apache/kafka/pull/17414#issuecomment-2407470969

   @lucasbru 
   >I suppose (without checking further) that this would mean that partRecords is empty here.
   
   I assume when control records are returned, `partRecords` is empty while the position is advances.


-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1810540186


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetchCollectorTest.java:
##########
@@ -620,10 +646,49 @@ public void testCollectFetchInitializationOffsetOutOfRangeErrorWithOffsetReset()
         final Fetch<String, String> fetch = fetchCollector.collectFetch(fetchBuffer);
 
         assertTrue(fetch.isEmpty());
+        assertEquals(0, fetch.nextOffsets().size());
         verify(subscriptions).requestOffsetResetIfPartitionAssigned(topicPartition0);
         verify(fetchBuffer).setNextInLineFetch(null);
     }
 
+    @Test
+    public void testReadCommittedWithAbortedTransaction() {
+        buildDependencies(IsolationLevel.READ_COMMITTED);
+        int recordCount = 20;
+        assignAndSeek(topicAPartition0);
+
+
+        // Initial CompletedFetch
+        CompletedFetch initialCompletedFetch = completedFetchBuilder

Review Comment:
   I think you may want to also make the first completedFetch a transactional completed fetch. See `CompletedFetchTest` for an example. We should define the aborted transaction here.



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "aliehsaeedii (via GitHub)" <gi...@apache.org>.
aliehsaeedii commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1799936825


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/Fetch.java:
##########
@@ -95,6 +102,13 @@ public int numRecords() {
         return numRecords;
     }
 
+    /**
+     * @return the next offsets and metadata (last epochs is included)
+     */
+    public Map<TopicPartition, OffsetAndMetadata> nextOffsetAndMetadata() {
+        return Collections.unmodifiableMap(nextOffsetAndMetadata);

Review Comment:
   `Map.of` itself returns an immutable map. Maybe no need to create a copy?



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "AndrewJSchofield (via GitHub)" <gi...@apache.org>.
AndrewJSchofield commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1797182983


##########
clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecords.java:
##########
@@ -32,12 +32,19 @@
  * partition returned by a {@link Consumer#poll(java.time.Duration)} operation.
  */
 public class ConsumerRecords<K, V> implements Iterable<ConsumerRecord<K, V>> {
-    public static final ConsumerRecords<Object, Object> EMPTY = new ConsumerRecords<>(Collections.emptyMap());
+    public static final ConsumerRecords<Object, Object> EMPTY = new ConsumerRecords<>(Collections.emptyMap(), Collections.emptyMap());
 
     private final Map<TopicPartition, List<ConsumerRecord<K, V>>> records;
+    private final Map<TopicPartition, OffsetAndMetadata> nextOffsets;
 
+    @Deprecated
     public ConsumerRecords(Map<TopicPartition, List<ConsumerRecord<K, V>>> records) {
+        this(records, Collections.emptyMap());
+    }
+
+    public ConsumerRecords(Map<TopicPartition, List<ConsumerRecord<K, V>>> records, final Map<TopicPartition, OffsetAndMetadata> nextOffsets) {
         this.records = records;
+        this.nextOffsets = Collections.unmodifiableMap(nextOffsets);

Review Comment:
   Now that we have Java 11, this can be `Map.copyOf` instead of `Collections.unmodifiableMap`.



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/Fetch.java:
##########
@@ -95,6 +102,13 @@ public int numRecords() {
         return numRecords;
     }
 
+    /**
+     * @return the next offsets and metadata (last epochs is included)
+     */
+    public Map<TopicPartition, OffsetAndMetadata> nextOffsetAndMetadata() {
+        return Collections.unmodifiableMap(nextOffsetAndMetadata);

Review Comment:
   `Map.copyOf`



##########
connect/runtime/src/test/java/org/apache/kafka/connect/runtime/WorkerSinkTaskTest.java:
##########
@@ -1821,15 +1826,19 @@ private Answer<ConsumerRecords<byte[], byte[]>> expectConsumerPoll(final int num
     private Answer<ConsumerRecords<byte[], byte[]>> expectConsumerPoll(final int numMessages, final long timestamp, final TimestampType timestampType, Headers headers) {
         return invocation -> {
             List<ConsumerRecord<byte[], byte[]>> records = new ArrayList<>();
-            for (int i = 0; i < numMessages; i++)
-                records.add(new ConsumerRecord<>(TOPIC, PARTITION, FIRST_OFFSET + recordsReturnedTp1 + i, timestamp, timestampType,
-                        0, 0, RAW_KEY, RAW_VALUE, headers, Optional.empty()));
+            long offset = 0;
+            for (int i = 0; i < numMessages; i++) {
+                offset = FIRST_OFFSET + recordsReturnedTp1 + i;
+                records.add(new ConsumerRecord<>(TOPIC, PARTITION, offset, timestamp, timestampType,
+                    0, 0, RAW_KEY, RAW_VALUE, headers, Optional.empty()));
+            }
             recordsReturnedTp1 += numMessages;
-            return new ConsumerRecords<>(
-                    numMessages > 0 ?
-                            Collections.singletonMap(new TopicPartition(TOPIC, PARTITION), records)
-                            : Collections.emptyMap()
-            );
+            final TopicPartition tp = new TopicPartition(TOPIC, PARTITION);
+            if (numMessages > 0) {
+                return new ConsumerRecords<>(Collections.singletonMap(tp, records),

Review Comment:
   This can be `Map.of(tp, records)`.



##########
connect/runtime/src/test/java/org/apache/kafka/connect/runtime/ErrorHandlingTaskTest.java:
##########
@@ -482,8 +483,8 @@ private void createSourceTask(TargetState initialState, RetryWithToleranceOperat
     }
 
     private ConsumerRecords<byte[], byte[]> records(ConsumerRecord<byte[], byte[]> record) {
-        return new ConsumerRecords<>(Collections.singletonMap(
-                new TopicPartition(record.topic(), record.partition()), singletonList(record)));
+        final TopicPartition tp = new TopicPartition(record.topic(), record.partition());
+        return new ConsumerRecords<>(Collections.singletonMap(tp, singletonList(record)), Collections.singletonMap(tp, new OffsetAndMetadata(record.offset() + 1, record.leaderEpoch(), "")));

Review Comment:
   `new ConsumerRecords<L>(Map.of(tp, List.of(record)), Map.of(tp, new OffsetAndMetadata.....` is more concise in Java 11.



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/FetchCollector.java:
##########
@@ -195,7 +196,7 @@ private Fetch<K, V> fetchRecords(final CompletedFetch nextInLineFetch, int maxRe
                     metricsManager.recordPartitionLead(tp, lead);
                 }
 
-                return Fetch.forPartition(tp, partRecords, positionAdvanced);
+                return Fetch.forPartition(tp, partRecords, positionAdvanced, new OffsetAndMetadata(nextInLineFetch.nextFetchOffset(), nextInLineFetch.lastEpoch(), ""));

Review Comment:
   Yes, but at the next level down. `CompletedFetch.nextFetchedRecord(FetchConfig)` is where we distinguish between normal records and control records. I think you'll probably need to be working inside `CompletedFetch`. This area of code is a bit nasty unfortunately. `nextFetchedRecord` is the key method to understand I think.



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/Fetch.java:
##########
@@ -95,6 +102,13 @@ public int numRecords() {
         return numRecords;
     }
 
+    /**
+     * @return the next offsets and metadata (last epochs is included)
+     */
+    public Map<TopicPartition, OffsetAndMetadata> nextOffsetAndMetadata() {

Review Comment:
   I'd probably just call this method `nextOffsets` to match what you have in `ConsumerRecords.nextOffsets`. You can also copy the description from that method with identical words. Seems like less effort.



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "aliehsaeedii (via GitHub)" <gi...@apache.org>.
aliehsaeedii commented on PR #17414:
URL: https://github.com/apache/kafka/pull/17414#issuecomment-2412042780

   @lucasbru I think the PR is ready for the last round of review. Thanks in advance.


-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "aliehsaeedii (via GitHub)" <gi...@apache.org>.
aliehsaeedii commented on code in PR #17414:
URL: https://github.com/apache/kafka/pull/17414#discussion_r1795584142


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/ShareConsumerImpl.java:
##########
@@ -583,7 +583,7 @@ public synchronized ConsumerRecords<K, V> poll(final Duration timeout) {
                 final ShareFetch<K, V> fetch = pollForFetches(timer);
                 if (!fetch.isEmpty()) {
                     currentFetch = fetch;
-                    return new ConsumerRecords<>(fetch.records());
+                    return new ConsumerRecords<>(fetch.records(), Collections.emptyMap());

Review Comment:
   > Trivial, but please test this in `ShareConsumerImplTest`.
   
   Mmm. `ShareConsumerImplTest` does not test the returned records even.



-- 
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-17600: Add nextOffsets to the ConsumerRecords [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru merged PR #17414:
URL: https://github.com/apache/kafka/pull/17414


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