Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/src/main/scala/kafka/utils/FileLock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class FileLock(val file: File) extends Logging {
def destroy(): Unit = {
this synchronized {
unlock()
if (file.exists() && file.delete()) {
trace(s"Delete ${file.getAbsolutePath}")
}
channel.close()
}
}
Expand Down
20 changes: 20 additions & 0 deletions core/src/test/scala/unit/kafka/log/LogManagerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,26 @@ class LogManagerTest {
createLeaderAndIsrRequestForStrayDetection(present),
onDisk.map(mockLog(_))).toSet)
}

/**
* Test LogManager takes file lock by default and the lock is released after shutdown.
*/
@Test
def testLock(): Unit = {
val tmpLogDir = TestUtils.tempDir()
val tmpLogManager = createLogManager(Seq(tmpLogDir))

try {
// ${tmpLogDir}.lock is acquired by tmpLogManager
val fileLock = new FileLock(new File(tmpLogDir, LogManager.LockFileName))
assertFalse(fileLock.tryLock())
} finally {
// ${tmpLogDir}.lock is removed after shutdown
tmpLogManager.shutdown()
val f = new File(tmpLogDir, LogManager.LockFileName)
assertFalse(f.exists())
}
}
}

object LogManagerTest {
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/scala/unit/kafka/raft/RaftManagerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class RaftManagerTest {
}

private def fileLocked(path: Path): Boolean = {
TestUtils.resource(FileChannel.open(path, StandardOpenOption.WRITE)) { channel =>
TestUtils.resource(FileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) { channel =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FrankYang0529 , could you explain more why we need this change? Thanks.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We remove the .lock file when RaftManager#shutdown. In this case, FileChannel can't open a nonexistent file and will throw NoSuchFileException, so I add StandardOpenOption.CREATE to avoid the error.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation!

try {
Option(channel.tryLock()).foreach(_.close())
false
Expand Down