Skip to content

Commit 9326476

Browse files
KAFKA-16391: remove .lock file when FileLock#destroy (apache#15568)
Currently, server adds a .lock file to each log folder. The file is useless after server is down. Reviewers: Luke Chen <[email protected]>, Chia-Ping Tsai <[email protected]>
1 parent ae44a08 commit 9326476

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

core/src/main/scala/kafka/utils/FileLock.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class FileLock(val file: File) extends Logging {
7676
def destroy(): Unit = {
7777
this synchronized {
7878
unlock()
79+
if (file.exists() && file.delete()) {
80+
trace(s"Delete ${file.getAbsolutePath}")
81+
}
7982
channel.close()
8083
}
8184
}

core/src/test/scala/unit/kafka/log/LogManagerTest.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,26 @@ class LogManagerTest {
13031303
createLeaderAndIsrRequestForStrayDetection(present),
13041304
onDisk.map(mockLog(_))).toSet)
13051305
}
1306+
1307+
/**
1308+
* Test LogManager takes file lock by default and the lock is released after shutdown.
1309+
*/
1310+
@Test
1311+
def testLock(): Unit = {
1312+
val tmpLogDir = TestUtils.tempDir()
1313+
val tmpLogManager = createLogManager(Seq(tmpLogDir))
1314+
1315+
try {
1316+
// ${tmpLogDir}.lock is acquired by tmpLogManager
1317+
val fileLock = new FileLock(new File(tmpLogDir, LogManager.LockFileName))
1318+
assertFalse(fileLock.tryLock())
1319+
} finally {
1320+
// ${tmpLogDir}.lock is removed after shutdown
1321+
tmpLogManager.shutdown()
1322+
val f = new File(tmpLogDir, LogManager.LockFileName)
1323+
assertFalse(f.exists())
1324+
}
1325+
}
13061326
}
13071327

13081328
object LogManagerTest {

core/src/test/scala/unit/kafka/raft/RaftManagerTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class RaftManagerTest {
178178
}
179179

180180
private def fileLocked(path: Path): Boolean = {
181-
TestUtils.resource(FileChannel.open(path, StandardOpenOption.WRITE)) { channel =>
181+
TestUtils.resource(FileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) { channel =>
182182
try {
183183
Option(channel.tryLock()).foreach(_.close())
184184
false

0 commit comments

Comments
 (0)