Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic move and many processes running it at the same time

Tags:

java

nio

In my application I have scheduled task that monitor a directory for files with certain extension. Those files are further processed. There can be a multiple processes executing this app and to prevent multiple instances from processing the same file I change its extension so its not listed another time using Files.move with ATOMIC_MOVE option. There is a chance that more than one instance of app try to execute this method at the same time though - my question is: what happens then? Should I expect some exceptions being thrown?

like image 588
Piotr Podraza Avatar asked Oct 17 '22 10:10

Piotr Podraza


1 Answers

You should be able to count on a correct behavior: first one "wins", second one gets java.nio.file.NoSuchFileException, so you catch that particular exception and ignore the file.

try {
    Files.move(previousPath, renamedPath, StandardCopyOption.ATOMIC_MOVE);
}
catch (NoSuchFileException e) {
    /* Another process has already processed that file. Ignore and move on. */
}
catch (IOException e) {
    /* Must handle that one: something else has failed */
}

I've verified on Linux and macOS with a program that lists a directory with some files, and renames them all with a random UUID, and running several instances of that program on the same directory for a while. I got many NoSuchFileException but nothing else.

I would be quite confident with this on a local filesystem. However, if you're using that on a networked filesystem (NFS...), all bets are off.

like image 70
Hugues M. Avatar answered Oct 21 '22 04:10

Hugues M.