showuon commented on code in PR #20289:
URL: https://github.com/apache/kafka/pull/20289#discussion_r2685714689
##########
core/src/test/scala/unit/kafka/log/LogManagerTest.scala:
##########
@@ -1112,6 +1112,45 @@ class LogManagerTest {
assertEquals(2, logManager.directoryIdsSet.size)
}
+ /**
+ * Test that replaceCurrentWithFutureLog does not close the source log,
preventing race conditions
+ * where a concurrent read/flush could fail with ClosedChannelException.
+ */
+ @Test
+ def testReplaceCurrentWithFutureLogDoesNotCloseSourceLog(): Unit = {
+ val logDir1 = TestUtils.tempDir()
+ val logDir2 = TestUtils.tempDir()
+ logManager = createLogManager(Seq(logDir1, logDir2))
+ logManager.startup(Set.empty)
+
+ val topicName = "replace-log"
+ val tp = new TopicPartition(topicName, 0)
+ val currentLog = logManager.getOrCreateLog(tp, topicId = Optional.empty)
+ // Create a future log in a different directory
+ logManager.maybeUpdatePreferredLogDir(tp, logDir2.getAbsolutePath)
+ logManager.getOrCreateLog(tp, isFuture = true, topicId = Optional.empty)
+
+ // Spy on the source log to verify close() is not called
+ val spyCurrentLog = spy(currentLog)
+ // Inject the spy into the map
+ val field = classOf[LogManager].getDeclaredField("currentLogs")
+ field.setAccessible(true)
+ val currentLogs =
field.get(logManager).asInstanceOf[ConcurrentHashMap[TopicPartition,
UnifiedLog]]
+ currentLogs.put(tp, spyCurrentLog)
+
+ logManager.replaceCurrentWithFutureLog(tp)
+
+ // Verify close() was NOT called on the source log
+ verify(spyCurrentLog, never()).close()
+
+ // Verify the source log was renamed to .delete
+
assertTrue(spyCurrentLog.dir.getName.endsWith(LogFileUtils.DELETE_DIR_SUFFIX))
+
+ // Verify that flush() can be called without error (no
ClosedChannelException)
+ val flushLog: Executable = () => spyCurrentLog.flush(false)
+ assertDoesNotThrow(flushLog)
Review Comment:
To trigger the flush, we have to set `flushOffset >
localLog.recoveryPoint()`
([here](https://github.com/apache/kafka/blob/trunk/storage/src/main/java/org/apache/kafka/storage/internals/log/UnifiedLog.java#L2232-L2235)).
Because there's no any data, the flushOffset is 0 and recoveryPoint is 0, too.
I just tested it, and this flush will not throw exception even if we close the
srcLog as before. We have to make the `flushOffset > 0` to trigger the
exception, something like this:
```
// Verify that flush() can be called without error (no
ClosedChannelException)
when(spyCurrentLog.logEndOffset()).thenReturn(100L)
val flushLog: Executable = () => spyCurrentLog.flush(false)
assertDoesNotThrow(flushLog)
```
--
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]