majialoong commented on code in PR #20739:
URL: https://github.com/apache/kafka/pull/20739#discussion_r2458871231


##########
coordinator-common/src/test/java/org/apache/kafka/coordinator/common/runtime/CoordinatorRuntimeTest.java:
##########
@@ -4866,6 +4867,83 @@ public void testCoordinatorExecutor() {
         assertTrue(write1.isDone());
     }
 
+    @Test
+    public void testLingerTimeComparisonInMaybeFlushCurrentBatch() throws 
Exception {
+        // Provides the runtime clock; we will advance it.
+        MockTimer clockTimer = new MockTimer();
+        // Used for scheduling timer tasks; we won't advance it to avoid a 
timer-triggered batch flush.
+        MockTimer schedulerTimer = new MockTimer();
+
+        MockPartitionWriter writer = new MockPartitionWriter();
+
+        CoordinatorRuntime<MockCoordinatorShard, String> runtime =
+                new CoordinatorRuntime.Builder<MockCoordinatorShard, String>()
+                        .withTime(clockTimer.time())
+                        .withTimer(schedulerTimer)
+                        .withDefaultWriteTimeOut(Duration.ofMillis(20))
+                        .withLoader(new MockCoordinatorLoader())
+                        .withEventProcessor(new DirectEventProcessor())
+                        .withPartitionWriter(writer)
+                        .withCoordinatorShardBuilderSupplier(new 
MockCoordinatorShardBuilderSupplier())
+                        
.withCoordinatorRuntimeMetrics(mock(CoordinatorRuntimeMetrics.class))
+                        .withCoordinatorMetrics(mock(CoordinatorMetrics.class))
+                        .withSerializer(new StringSerializer())
+                        .withAppendLingerMs(10)
+                        .withExecutorService(mock(ExecutorService.class))
+                        .build();
+
+        // Schedule the loading.
+        runtime.scheduleLoadOperation(TP, 10);
+
+        // Verify the initial state.
+        CoordinatorRuntime<MockCoordinatorShard, String>.CoordinatorContext 
ctx = runtime.contextOrThrow(TP);
+        assertEquals(ACTIVE, ctx.state);
+        assertNull(ctx.currentBatch);
+
+        // Write #1.
+        CompletableFuture<String> write1 = runtime.scheduleWriteOperation(
+                "write#1", TP, Duration.ofMillis(20),
+                state -> new CoordinatorResult<>(List.of("record1"), 
"response1")
+        );
+        assertFalse(write1.isDone());
+        assertNotNull(ctx.currentBatch);
+        assertEquals(0, writer.entries(TP).size());
+
+        // Verify that the linger timeout task is created; there will also be 
a default write timeout task.
+        assertEquals(2, schedulerTimer.size());
+
+        // Advance past the linger time.
+        clockTimer.advanceClock(11);

Review Comment:
   Yes, the purpose of advancing the `clockTimer` is to ensure that 
`flushCurrentBatch` is executed when writing #2.
   
   However, I think that after advancing the `clockTimer`, we should check the 
number of tasks in the `schedulerTimer` to ensure that the linger task still 
exists (has not been executed or canceled) before writing #2.
   
   This ensures that `flushCurrentBatch` is executed when writing #2, rather 
than being triggered by the linger timer task.



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