kevin-wu24 commented on code in PR #23525:
URL: https://github.com/apache/kafka/pull/23525#discussion_r4084088842


##########
metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTest.java:
##########
@@ -1637,6 +1652,72 @@ public void testAppendRecordsAtomically() {
                         appender)).getMessage());
     }
 
+    @ParameterizedTest(name = "numRecords={0}, max={1}")
+    @CsvSource({
+        "1, 1", "1, 2", "2, 2", "3, 2", "4, 2", "5, 2", "6, 2",
+        "2, 1", "3, 1", "5, 1", "100, 1",
+        "1, 10", "2, 10", "10, 3", "100, 7"
+    })
+    public void testAppendRecordsNonAtomicBatching(int numRecords, int max) {

Review Comment:
   Can we rename the parameter `max` to something more descriptive? What about 
`maxRecordsPerBatch`?



##########
metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTest.java:
##########
@@ -1637,6 +1652,72 @@ public void testAppendRecordsAtomically() {
                         appender)).getMessage());
     }
 
+    @ParameterizedTest(name = "numRecords={0}, max={1}")
+    @CsvSource({
+        "1, 1", "1, 2", "2, 2", "3, 2", "4, 2", "5, 2", "6, 2",
+        "2, 1", "3, 1", "5, 1", "100, 1",
+        "1, 10", "2, 10", "10, 3", "100, 7"

Review Comment:
   I think it is sufficient to test the the following cases:
   
   - numRecords is a multiple of maxRecordsPerBatch
   - numRecords == maxRecordsPerBatch (you can argue this is not even needed)
   - numRecords > maxRecordsPerBatch but not a multiple
   - numRecords < maxRecordsPerBatch
   
   And maybe choose two values for `maxRecordsPerBatch`, something simple like 
1 and something like 10.



##########
metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTest.java:
##########
@@ -1637,6 +1652,72 @@ public void testAppendRecordsAtomically() {
                         appender)).getMessage());
     }
 
+    @ParameterizedTest(name = "numRecords={0}, max={1}")
+    @CsvSource({
+        "1, 1", "1, 2", "2, 2", "3, 2", "4, 2", "5, 2", "6, 2",
+        "2, 1", "3, 1", "5, 1", "100, 1",
+        "1, 10", "2, 10", "10, 3", "100, 7"
+    })
+    public void testAppendRecordsNonAtomicBatching(int numRecords, int max) {
+        List<ApiMessageAndVersion> records = new ArrayList<>();
+        for (int i = 0; i < numRecords; i++) {
+            records.add(rec(i));
+        }
+        RecordingAppender appender = new RecordingAppender();
+        long returned = QuorumController.appendRecords(log,
+            ControllerResult.of(records, null), max, appender);
+
+        List<Integer> expectedSizes = new ArrayList<>();
+        for (int i = 0; i < numRecords; i += max) {
+            expectedSizes.add(Math.min(max, numRecords - i));
+        }
+        assertEquals(expectedSizes, appender.batchSizes,
+            "batches should be exactly max-sized except the final one");
+        assertTrue(appender.batchSizes.stream().allMatch(s -> s > 0 && s <= 
max),
+            "every batch should be non-empty and no larger than max");
+        assertEquals(numRecords, 
appender.batchSizes.stream().mapToLong(Integer::longValue).sum(),
+            "no record should be dropped or duplicated");
+        assertEquals(numRecords == 0 ? -1L : (long) numRecords, returned);
+    }
+
+    @Test
+    public void testAppendRecordsNonAtomicDoesNotAppendEmptyBatch() {
+        assertEquals(2L, appendWithThrowingAppender(2, 2));
+        assertEquals(4L, appendWithThrowingAppender(4, 2));
+    }
+
+    private static long appendWithThrowingAppender(int numRecords, int max) {
+        AtomicLong offset = new AtomicLong(0);
+        Function<List<ApiMessageAndVersion>, Long> appender = records -> {
+            if (records.isEmpty()) {
+                throw new IllegalArgumentException("Append failed because 
there are no records");
+            }
+            return offset.addAndGet(records.size());
+        };
+        List<ApiMessageAndVersion> records = new ArrayList<>();
+        for (int i = 0; i < numRecords; i++) {
+            records.add(rec(i));
+        }
+        return QuorumController.appendRecords(log, 
ControllerResult.of(records, null), max, appender);
+    }
+
+    @Test
+    public void testNonAtomicWriteDoesNotAppendEmptyBatch() throws Throwable {

Review Comment:
   What is this test doing?



##########
metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTest.java:
##########
@@ -1637,6 +1652,72 @@ public void testAppendRecordsAtomically() {
                         appender)).getMessage());
     }
 
+    @ParameterizedTest(name = "numRecords={0}, max={1}")
+    @CsvSource({
+        "1, 1", "1, 2", "2, 2", "3, 2", "4, 2", "5, 2", "6, 2",
+        "2, 1", "3, 1", "5, 1", "100, 1",
+        "1, 10", "2, 10", "10, 3", "100, 7"
+    })
+    public void testAppendRecordsNonAtomicBatching(int numRecords, int max) {
+        List<ApiMessageAndVersion> records = new ArrayList<>();
+        for (int i = 0; i < numRecords; i++) {
+            records.add(rec(i));
+        }
+        RecordingAppender appender = new RecordingAppender();
+        long returned = QuorumController.appendRecords(log,
+            ControllerResult.of(records, null), max, appender);
+
+        List<Integer> expectedSizes = new ArrayList<>();
+        for (int i = 0; i < numRecords; i += max) {
+            expectedSizes.add(Math.min(max, numRecords - i));
+        }
+        assertEquals(expectedSizes, appender.batchSizes,
+            "batches should be exactly max-sized except the final one");
+        assertTrue(appender.batchSizes.stream().allMatch(s -> s > 0 && s <= 
max),
+            "every batch should be non-empty and no larger than max");
+        assertEquals(numRecords, 
appender.batchSizes.stream().mapToLong(Integer::longValue).sum(),
+            "no record should be dropped or duplicated");
+        assertEquals(numRecords == 0 ? -1L : (long) numRecords, returned);
+    }
+
+    @Test
+    public void testAppendRecordsNonAtomicDoesNotAppendEmptyBatch() {
+        assertEquals(2L, appendWithThrowingAppender(2, 2));
+        assertEquals(4L, appendWithThrowingAppender(4, 2));

Review Comment:
   This test seems like a duplicate test no as the one above no?



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