shashankhs11 commented on code in PR #20292:
URL: https://github.com/apache/kafka/pull/20292#discussion_r2629452761


##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/PartitionGroupTest.java:
##########
@@ -912,6 +925,81 @@ public void shouldIdleAsSpecifiedWhenLagIsZero() {
         }
     }
 
+    @Test
+    public void 
shouldUpdateTotalBytesBufferedOnRecordsAdditionAndConsumption() {
+        final PartitionGroup group = getBasicGroup();
+
+        assertEquals(0, group.numBuffered());
+        assertEquals(0L, group.totalBytesBuffered());
+
+        // add three 3 records with timestamp 1, 5, 3 to partition-1
+        final List<ConsumerRecord<byte[], byte[]>> list1 = Arrays.asList(
+            new ConsumerRecord<>("topic", 1, 1L, new 
MockTime().milliseconds(), TimestampType.CREATE_TIME, recordKey.length, 
recordValue.length, recordKey, recordValue, new RecordHeaders(), 
Optional.empty()),
+            new ConsumerRecord<>("topic", 1, 5L, new 
MockTime().milliseconds(), TimestampType.CREATE_TIME, recordKey.length, 
recordValue.length, recordKey, recordValue, new RecordHeaders(), 
Optional.empty()),
+            new ConsumerRecord<>("topic", 1, 3L, new 
MockTime().milliseconds(), TimestampType.CREATE_TIME, recordKey.length, 
recordValue.length, recordKey, recordValue, new RecordHeaders(), 
Optional.empty()));
+
+        long partition1TotalBytes = getBytesBufferedForRawRecords(list1);
+        group.addRawRecords(partition1, list1);
+
+        verifyBuffered(3, 3, 0, group);
+        assertEquals(group.totalBytesBuffered(), partition1TotalBytes);
+        assertEquals(-1L, group.streamTime());
+        assertEquals(0.0, metrics.metric(lastLatenessValue).metricValue());
+        assertThat(metrics.metric(totalBytesValue).metricValue(), is((double) 
partition1TotalBytes));
+
+        StampedRecord record;
+        final PartitionGroup.RecordInfo info = new PartitionGroup.RecordInfo();
+
+        // get first two records from partition 1
+        record = group.nextRecord(info, time.milliseconds());
+        assertEquals(record.timestamp, 1L);
+        record = group.nextRecord(info, time.milliseconds());
+        assertEquals(record.timestamp, 5L);
+
+        partition1TotalBytes -= 
getBytesBufferedForRawRecords(Arrays.asList(list1.get(0), list1.get(1)));
+        assertEquals(group.totalBytesBuffered(), partition1TotalBytes);
+        assertThat(metrics.metric(totalBytesValue).metricValue(), is((double) 
partition1TotalBytes));
+
+        // add three 3 records with timestamp 2, 4, 6 to partition-2
+        final List<ConsumerRecord<byte[], byte[]>> list2 = Arrays.asList(
+            new ConsumerRecord<>("topic", 2, 2L, record.timestamp, 
TimestampType.CREATE_TIME, recordKey.length, recordValue.length, recordKey, 
recordValue, new RecordHeaders(), Optional.empty()),
+            new ConsumerRecord<>("topic", 2, 4L, record.timestamp, 
TimestampType.CREATE_TIME, recordKey.length, recordValue.length, recordKey, 
recordValue, new RecordHeaders(), Optional.empty()),
+            new ConsumerRecord<>("topic", 2, 6L, record.timestamp, 
TimestampType.CREATE_TIME, recordKey.length, recordValue.length, recordKey, 
recordValue, new RecordHeaders(), Optional.empty()));
+
+        long partition2TotalBytes = getBytesBufferedForRawRecords(list2);
+        group.addRawRecords(partition2, list2);
+        // 1:[3]
+        // 2:[2, 4, 6]
+        assertEquals(group.totalBytesBuffered(), partition2TotalBytes + 
partition1TotalBytes);
+        assertThat(metrics.metric(totalBytesValue).metricValue(), is((double) 
partition2TotalBytes + partition1TotalBytes));
+
+        // get one record, next record should be ts=2 from partition 2
+        record = group.nextRecord(info, time.milliseconds());
+        // 1:[3]
+        // 2:[4, 6]
+        partition2TotalBytes -= 
getBytesBufferedForRawRecords(Collections.singletonList(list2.get(0)));

Review Comment:
   addressed in 52ad5fc



##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/PartitionGroupTest.java:
##########
@@ -912,6 +925,81 @@ public void shouldIdleAsSpecifiedWhenLagIsZero() {
         }
     }
 
+    @Test
+    public void 
shouldUpdateTotalBytesBufferedOnRecordsAdditionAndConsumption() {
+        final PartitionGroup group = getBasicGroup();
+
+        assertEquals(0, group.numBuffered());
+        assertEquals(0L, group.totalBytesBuffered());
+
+        // add three 3 records with timestamp 1, 5, 3 to partition-1
+        final List<ConsumerRecord<byte[], byte[]>> list1 = Arrays.asList(
+            new ConsumerRecord<>("topic", 1, 1L, new 
MockTime().milliseconds(), TimestampType.CREATE_TIME, recordKey.length, 
recordValue.length, recordKey, recordValue, new RecordHeaders(), 
Optional.empty()),
+            new ConsumerRecord<>("topic", 1, 5L, new 
MockTime().milliseconds(), TimestampType.CREATE_TIME, recordKey.length, 
recordValue.length, recordKey, recordValue, new RecordHeaders(), 
Optional.empty()),
+            new ConsumerRecord<>("topic", 1, 3L, new 
MockTime().milliseconds(), TimestampType.CREATE_TIME, recordKey.length, 
recordValue.length, recordKey, recordValue, new RecordHeaders(), 
Optional.empty()));
+
+        long partition1TotalBytes = getBytesBufferedForRawRecords(list1);
+        group.addRawRecords(partition1, list1);
+
+        verifyBuffered(3, 3, 0, group);
+        assertEquals(group.totalBytesBuffered(), partition1TotalBytes);
+        assertEquals(-1L, group.streamTime());
+        assertEquals(0.0, metrics.metric(lastLatenessValue).metricValue());
+        assertThat(metrics.metric(totalBytesValue).metricValue(), is((double) 
partition1TotalBytes));
+
+        StampedRecord record;
+        final PartitionGroup.RecordInfo info = new PartitionGroup.RecordInfo();
+
+        // get first two records from partition 1
+        record = group.nextRecord(info, time.milliseconds());
+        assertEquals(record.timestamp, 1L);
+        record = group.nextRecord(info, time.milliseconds());
+        assertEquals(record.timestamp, 5L);
+
+        partition1TotalBytes -= 
getBytesBufferedForRawRecords(Arrays.asList(list1.get(0), list1.get(1)));
+        assertEquals(group.totalBytesBuffered(), partition1TotalBytes);
+        assertThat(metrics.metric(totalBytesValue).metricValue(), is((double) 
partition1TotalBytes));
+
+        // add three 3 records with timestamp 2, 4, 6 to partition-2
+        final List<ConsumerRecord<byte[], byte[]>> list2 = Arrays.asList(
+            new ConsumerRecord<>("topic", 2, 2L, record.timestamp, 
TimestampType.CREATE_TIME, recordKey.length, recordValue.length, recordKey, 
recordValue, new RecordHeaders(), Optional.empty()),
+            new ConsumerRecord<>("topic", 2, 4L, record.timestamp, 
TimestampType.CREATE_TIME, recordKey.length, recordValue.length, recordKey, 
recordValue, new RecordHeaders(), Optional.empty()),
+            new ConsumerRecord<>("topic", 2, 6L, record.timestamp, 
TimestampType.CREATE_TIME, recordKey.length, recordValue.length, recordKey, 
recordValue, new RecordHeaders(), Optional.empty()));
+
+        long partition2TotalBytes = getBytesBufferedForRawRecords(list2);
+        group.addRawRecords(partition2, list2);
+        // 1:[3]
+        // 2:[2, 4, 6]
+        assertEquals(group.totalBytesBuffered(), partition2TotalBytes + 
partition1TotalBytes);
+        assertThat(metrics.metric(totalBytesValue).metricValue(), is((double) 
partition2TotalBytes + partition1TotalBytes));
+
+        // get one record, next record should be ts=2 from partition 2
+        record = group.nextRecord(info, time.milliseconds());
+        // 1:[3]
+        // 2:[4, 6]
+        partition2TotalBytes -= 
getBytesBufferedForRawRecords(Collections.singletonList(list2.get(0)));
+        assertEquals(group.totalBytesBuffered(), partition2TotalBytes + 
partition1TotalBytes);
+        assertThat(metrics.metric(totalBytesValue).metricValue(), is((double) 
partition2TotalBytes + partition1TotalBytes));
+        assertEquals(record.timestamp, 2L);
+
+        // get one record, next up should have ts=3 from partition 1 (even 
though it has seen a larger max timestamp =5)
+        record = group.nextRecord(info, time.milliseconds());
+        // 1:[]
+        // 2:[4, 6]
+        partition1TotalBytes -= 
getBytesBufferedForRawRecords(Collections.singletonList(list1.get(2)));

Review Comment:
   addressed in 52ad5fc



##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/StreamThreadTest.java:
##########
@@ -4237,6 +4258,281 @@ t2p1, new PartitionInfo(t2p1.topic(), t2p1.partition(), 
null, new Node[0], new N
         );
     }
 
+    @Test
+    public void 
shouldPauseNonEmptyPartitionsWhenTotalBufferSizeExceedsMaxBufferSize() {
+        // Set up consumer mock
+        @SuppressWarnings("unchecked")
+        final Consumer<byte[], byte[]> consumer = mock(Consumer.class);
+        final ConsumerGroupMetadata consumerGroupMetadata = 
mock(ConsumerGroupMetadata.class);
+        when(consumer.groupMetadata()).thenReturn(consumerGroupMetadata);
+        
when(consumerGroupMetadata.groupInstanceId()).thenReturn(Optional.empty());
+
+        // Create records for polling
+        final Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> 
polledRecords = new HashMap<>();
+        final List<ConsumerRecord<byte[], byte[]>> t1p1Records = new 
ArrayList<>();
+
+        t1p1Records.add(new ConsumerRecord<>(
+            t1p1.topic(),
+            t1p1.partition(),
+            0,
+            mockTime.milliseconds(),
+            TimestampType.CREATE_TIME,
+            2,
+            6,
+            new byte[2],
+            new byte[6],
+            new RecordHeaders(),
+            Optional.empty()));
+
+        t1p1Records.add(new ConsumerRecord<>(
+            t1p1.topic(),
+            t1p1.partition(),
+            1,
+            mockTime.milliseconds(),
+            TimestampType.CREATE_TIME,
+            2,
+            6,
+            new byte[2],
+            new byte[6],
+            new RecordHeaders(),
+            Optional.empty()));
+
+        final List<ConsumerRecord<byte[], byte[]>> t2p1Records = 
Collections.singletonList(

Review Comment:
   addressed in 52ad5fc



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