Prevent simultaneous processes accessing same file

Viewed 29

We have an NFS share which receives files. We also have multiple processes listening for new files in this share.

What would be a safe way (in Java) of accessing this file and ensure that only one process can process this file?

We were planning to (as first step) let the process move the file - but that might not be atomic on NFS? What about renaming the file first and then move it? Or will multiple processes be able to rename the same file on NFS? I guess a safe way would be to add a file locked flag in a database with row locking but that seems to be overkill.

Any advice?

1 Answers

In NFS, moving a file is implemented using RENAME.

Within the same mount, RENAME is atomic in NFS:

3.3.14 Procedure 14: RENAME - Rename a File or Directory

...

Procedure RENAME renames the file identified by from.name in the directory, from.dir, to to.name in the di- rectory, to.dir. The operation is required to be atomic to the client.

Sources of inconsistency would include:

  • Non-atomic client translation of move to NFS RENAME (e.g. very old client OS) - See Files.move(ATOMIC_MOVE).
  • Weak Cache Consistency (WCC) of paths (e.g. if the racing processes are on different machines) - See NFSv4.

I'd say yes - move the files. Moving files within a mount is atomic on Unix-like systems, Windows systems, NFS, and SMB. You should be safe.

Related