[Solved] Kafka in Windows error:java. nio. file. Filesystemexception: this file is in use by another program and cannot be accessed by the process

Problem description

Kafka deployed in Windows environment hangs up after running for a period of time
view logs logs/server Log the following errors are found:

 ERROR Failed to clean up log for __consumer_offsets-44 in dir D:\kafka\kafka_2.13-2.8.0\kafkakafka_2.13-2.8.0kafka-logs due to IOException (kafka.server.LogDirFailureChannel)
java.nio.file.FileSystemException: D:\kafka\kafka_2.13-2.8.0\kafkakafka_2.13-2.8.0kafka-logs\__consumer_offsets-44\00000000000000000000.timeindex.cleaned -> D:\kafka\kafka_2.13-2.8.0\kafkakafka_2.13-2.8.0kafka-logs\__consumer_offsets-44\00000000000000000000.timeindex.swap: 另一个程序正在使用此文件,进程无法访问。

	at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
	at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:387)
	at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287)
	at java.nio.file.Files.move(Files.java:1395)
	at org.apache.kafka.common.utils.Utils.atomicMoveWithFallback(Utils.java:904)
	at kafka.log.AbstractIndex.renameTo(AbstractIndex.scala:210)
	at kafka.log.LazyIndex$IndexValue.renameTo(LazyIndex.scala:155)
	at kafka.log.LazyIndex.$anonfun$renameTo$1(LazyIndex.scala:79)
	at kafka.log.LazyIndex.renameTo(LazyIndex.scala:79)
	at kafka.log.LogSegment.changeFileSuffixes(LogSegment.scala:496)
	at kafka.log.Log.$anonfun$replaceSegments$4(Log.scala:2402)
	at kafka.log.Log.$anonfun$replaceSegments$4$adapted(Log.scala:2402)
	at scala.collection.immutable.List.foreach(List.scala:333)
	at kafka.log.Log.replaceSegments(Log.scala:2402)
	at kafka.log.Cleaner.cleanSegments(LogCleaner.scala:613)
	at kafka.log.Cleaner.$anonfun$doClean$6(LogCleaner.scala:538)
	at kafka.log.Cleaner.$anonfun$doClean$6$adapted(LogCleaner.scala:537)
	at scala.collection.immutable.List.foreach(List.scala:333)
	at kafka.log.Cleaner.doClean(LogCleaner.scala:537)
	at kafka.log.Cleaner.clean(LogCleaner.scala:511)
	at kafka.log.LogCleaner$CleanerThread.cleanLog(LogCleaner.scala:380)
	at kafka.log.LogCleaner$CleanerThread.cleanFilthiestLog(LogCleaner.scala:352)
	at kafka.log.LogCleaner$CleanerThread.tryCleanFilthiestLog(LogCleaner.scala:332)
	at kafka.log.LogCleaner$CleanerThread.doWork(LogCleaner.scala:321)
	at kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:96)
	Suppressed: java.nio.file.FileSystemException: D:\kafka\kafka_2.13-2.8.0\kafkakafka_2.13-2.8.0kafka-logs\__consumer_offsets-44\00000000000000000000.timeindex.cleaned -> D:\kafka\kafka_2.13-2.8.0\kafkakafka_2.13-2.8.0kafka-logs\__consumer_offsets-44\00000000000000000000.timeindex.swap: 另一个程序正在使用此文件,进程无法访问。

		at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
		at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
		at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:301)
		at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287)
		at java.nio.file.Files.move(Files.java:1395)
		at org.apache.kafka.common.utils.Utils.atomicMoveWithFallback(Utils.java:901)
		... 20 more

reason

When the consumer offsets the log cleanup or the log file scrolls, Kafka broker will shut down because the file renaming fails. This is because cannot modify the file in use in windows.

Solution

In server In the properties configuration file:
1.Modify log retention. Hours = - 1
2.Add log at the end of the file cleaner. Enable = false
3.Restart

Suggestion

There are similar issues and PR in GitHub, but they are not merged. The author uses kafka_2.13-2.8.0 version has this problem. Kafka is not recommended for win environments.

Read More: