kevin-wu24 commented on code in PR #23737:
URL: https://github.com/apache/kafka/pull/23737#discussion_r4332252769
##########
raft/src/test/java/org/apache/kafka/raft/internals/RecordsIteratorTest.java:
##########
@@ -131,6 +132,88 @@ public void testFileRecords() {
});
}
+ @Test
+ public void testFileRecordsSliceLargerThanBatchSize() throws IOException {
+ // Models KafkaRaftClient reading committed records: the slice ends at
the high-watermark
+ // and the file has uncommitted batches after the end of the slice.
+ List<TestBatch<String>> sliceBatches = List.of(
+ new TestBatch<>(0, 1, 0, List.of("a", "b", "c", "d", "e")),
+ new TestBatch<>(5, 1, 0, List.of("f", "g", "h", "i", "j"))
+ );
+ List<TestBatch<String>> batchesAfterSlice = List.of(
+ new TestBatch<>(10, 1, 0, List.of("k"))
+ );
+
+ try (FileRecords fileRecords = FileRecords.open(TestUtils.tempFile()))
{
+ fileRecords.append(buildRecords(CompressionType.NONE,
sliceBatches));
+ int sliceSize = fileRecords.sizeInBytes();
+ fileRecords.append(buildRecords(CompressionType.NONE,
batchesAfterSlice));
+ FileRecords slice = fileRecords.slice(0, sliceSize);
+
+ // A batch size one byte smaller than the slice forces a second
read, which has only
+ // one byte of the slice left to read, but the buffer can hold
`batchesAfterSlice`
+ List<TestBatch<String>> actualBatches = new ArrayList<>();
+ try (RecordsIterator<String> iterator = createIterator(
+ slice,
+ BufferSupplier.NO_CACHING,
+ sliceSize - 1,
+ true
+ )) {
+ iterator.forEachRemaining(batch ->
actualBatches.add(TestBatch.from(batch)));
+ }
+
+ assertEquals(sliceBatches, actualBatches);
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(ints = {1, Records.HEADER_SIZE_UP_TO_MAGIC - 1,
Records.HEADER_SIZE_UP_TO_MAGIC})
+ public void testRecordsEndingInPartialBatch(int partialBatchBytes) throws
IOException {
+ TestBatch<String> firstBatch = new TestBatch<>(0, 1, 0, List.of("a",
"b", "c", "d", "e"));
+ TestBatch<String> secondBatch = new TestBatch<>(5, 1, 0,
Collections.nCopies(10, "f"));
+ MemoryRecords records = buildRecords(
+ CompressionType.NONE,
+ List.of(firstBatch, secondBatch)
+ );
+ int firstBatchSize = records.firstBatchSize();
+ // The records end partialBatchBytes into the second batch
+ int partialRecordsSize = firstBatchSize + partialBatchBytes;
+
+ try (FileRecords fileRecords = FileRecords.open(TestUtils.tempFile()))
{
+ fileRecords.append(records);
+
+ testIterator(List.of(firstBatch), records.slice(0,
partialRecordsSize), true);
+ testIterator(List.of(firstBatch), fileRecords.slice(0,
partialRecordsSize), true);
+ }
+
Review Comment:
Removed, thanks.
--
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]