Atomic file write operations (cross platform)

后端 未结 7 1747
礼貌的吻别
礼貌的吻别 2020-12-28 15:34

How do I build up an atomic file write operation? The file is to be written by a Java service and read by python scripts.
For the record, reads are far greater than writ

相关标签:
7条回答
  • 2020-12-28 15:45

    AFAIK no.

    And the reason is that for such an atomic operation to be possible, there has to be OS support in the form of a transactional file system. And none of the mainstream operating system offer a transactional file system.

    EDIT - I'm wrong for POSIX-compliant systems at least. The POSIX rename syscall performs an atomic replace if a file with the target name already exists ... as pointed out by @janneb. That should be sufficient to do the OP's operation atomically.

    However, the fact remains that the Java File.renameTo() method is explicitly not guaranteed to be atomic, so it does not provide a cross-platform solution to the OP's problem.

    EDIT 2 - With Java 7 you can use java.nio.file.Files.move(Path source, Path target, CopyOption... options) with copyOptions and ATOMIC_MOVE. If this is not supported (by the OS / file system) you should get an exception.

    0 讨论(0)
  • 2020-12-28 15:46

    It's a classic producer/consumer problem. You should be able to solve this by using file renaming, which is atomic on POSIX systems.

    0 讨论(0)
  • 2020-12-28 15:56

    Try Java FileLock API

    0 讨论(0)
  • 2020-12-28 15:58

    In Linux, Solaris, Unix this is easy. Just use rename() from your program or mv. The files need to be on the same filesystem.

    On Windows, this is possible if you can control both programs. LockFileEx. For reads, open a shared lock on the lockfile. For writes, open an exclusive lock on the lockfile. Locking is weird in Windows, so I recommend using a separate lock file for this.

    0 讨论(0)
  • 2020-12-28 15:59

    Have the python scripts request permission from the service. While the service is writing it would place a lock on the file. If the lock exists, the service would reject the python request.

    0 讨论(0)
  • 2020-12-28 16:01

    You could try and use an extra file to act as a lock, but I'm not sure if that will work out ok. (It would force you to create lock-checking and retry logic at both sides, java and python)

    Another solution might be to not create files at all, maybe you could make your java process listen on a port and serve data from there rather than from a file?

    0 讨论(0)
提交回复
热议问题