KAFKA-16391: remove .lock file when FileLock#destroy (#15568)

Currently, server adds a .lock file to each log folder. The file is useless after server is down.

Reviewers: Luke Chen <showuon@gmail.com>, Chia-Ping Tsai <chia7712@gmail.com>
This commit is contained in:
PoAn Yang 2024-03-27 11:13:54 +08:00 committed by GitHub
parent ae44a08051
commit 9326476065
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View File

@ -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()
}
}

View File

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

View File

@ -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 =>
try {
Option(channel.tryLock()).foreach(_.close())
false