kevin-wu24 commented on code in PR #23525:
URL: https://github.com/apache/kafka/pull/23525#discussion_r4100864051
##########
metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTest.java:
##########
@@ -1637,6 +1652,61 @@ public void testAppendRecordsAtomically() {
appender)).getMessage());
}
+ @ParameterizedTest(name = "numRecords={0}, maxRecordsPerBatch={1}")
+ @CsvSource({
+ "1, 1",
+ "5, 1",
+ "5, 10",
+ "10, 10",
+ "15, 10",
+ "20, 10"
+ })
+ public void testAppendRecordsNonAtomicBatching(int numRecords, int
maxRecordsPerBatch) {
+ 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), maxRecordsPerBatch, appender);
+
+ List<Integer> expectedSizes = new ArrayList<>();
+ for (int i = 0; i < numRecords; i += maxRecordsPerBatch) {
+ expectedSizes.add(Math.min(maxRecordsPerBatch, numRecords - i));
+ }
+ assertEquals(expectedSizes, appender.batchSizes,
+ "batches should be exactly maxRecordsPerBatch-sized except the
final one");
+ assertTrue(appender.batchSizes.stream().allMatch(s -> s > 0 && s <=
maxRecordsPerBatch),
+ "every batch should be non-empty and no larger than
maxRecordsPerBatch");
+ assertEquals(numRecords,
appender.batchSizes.stream().mapToLong(Integer::longValue).sum(),
+ "no record should be dropped or duplicated");
+ assertEquals((long) numRecords, returned);
+ }
+
+ @Test
+ public void testNonAtomicWriteDoesNotTriggerControllerFailover() throws
Throwable {
+ try (
+ MockRaftClientTestEnv clientEnv = new
MockRaftClientTestEnv.Builder(1).build();
+ QuorumControllerTestEnv controlEnv = new
QuorumControllerTestEnv.Builder(clientEnv)
+ // Set the batch size to 1 so that any non-empty write is an
exact multiple
+ // of maxRecordsPerBatch, exercising the off-by-one path.
+ .setControllerBuilderInitializer(b ->
b.setControllerMaxRecordsPerBatch(1))
+ .build()
+ ) {
+ // Wait for the controller to become active and finish activation.
+ QuorumController controller = controlEnv.activeController(true);
+
+ // Remember the leadership epoch so we can detect a failover.
+ int epochBefore = controller.curClaimEpoch();
+
+ // A non-atomic write (broker registration) goes through the real
production appender
+ assertEquals(1, registerBrokersAndUnfence(controller, 1).size());
Review Comment:
I don't think we need to assert the record size. I think it is sufficient to
just generate the records. If the `maxRecordsPerBatch == 1`, any amount of
records generated is a multiple of `maxRecordsPerBatch`. What do you think?
##########
metadata/src/test/java/org/apache/kafka/controller/QuorumControllerTest.java:
##########
@@ -1637,6 +1652,61 @@ public void testAppendRecordsAtomically() {
appender)).getMessage());
}
+ @ParameterizedTest(name = "numRecords={0}, maxRecordsPerBatch={1}")
+ @CsvSource({
+ "1, 1",
+ "5, 1",
+ "5, 10",
+ "10, 10",
+ "15, 10",
+ "20, 10"
+ })
+ public void testAppendRecordsNonAtomicBatching(int numRecords, int
maxRecordsPerBatch) {
+ 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), maxRecordsPerBatch, appender);
+
+ List<Integer> expectedSizes = new ArrayList<>();
+ for (int i = 0; i < numRecords; i += maxRecordsPerBatch) {
+ expectedSizes.add(Math.min(maxRecordsPerBatch, numRecords - i));
+ }
+ assertEquals(expectedSizes, appender.batchSizes,
+ "batches should be exactly maxRecordsPerBatch-sized except the
final one");
+ assertTrue(appender.batchSizes.stream().allMatch(s -> s > 0 && s <=
maxRecordsPerBatch),
+ "every batch should be non-empty and no larger than
maxRecordsPerBatch");
+ assertEquals(numRecords,
appender.batchSizes.stream().mapToLong(Integer::longValue).sum(),
+ "no record should be dropped or duplicated");
+ assertEquals((long) numRecords, returned);
+ }
+
+ @Test
+ public void testNonAtomicWriteDoesNotTriggerControllerFailover() throws
Throwable {
+ try (
+ MockRaftClientTestEnv clientEnv = new
MockRaftClientTestEnv.Builder(1).build();
+ QuorumControllerTestEnv controlEnv = new
QuorumControllerTestEnv.Builder(clientEnv)
+ // Set the batch size to 1 so that any non-empty write is an
exact multiple
+ // of maxRecordsPerBatch, exercising the off-by-one path.
Review Comment:
I think it is fine to remove these in-line comments. I was just confused by
the test name.
--
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]