cxm940188 commented on code in PR #23525:
URL: https://github.com/apache/kafka/pull/23525#discussion_r4090399889
##########
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:
Thanks for the comment @kevin-wu24 . Three points about this method:
1. The old method name was misleading, so I renamed it to
`testNonAtomicWriteDoesNotTriggerControllerFailover`.
2. It verifies that a real non-atomic write through the controller (with
`controller.max.records.per.batch`=1, so any non-empty write is an exact
multiple) no longer makes the controller renounce leadership.
3. I've added inline comments in the method explaining each step. Could you
please take another look and confirm whether you think it's worth keeping?
--
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]