Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock a file on different application levels?

Here's the scenario: I have a multi threaded java web application which is running inside a servlet container. The application is deployed multiple times inside the servlet container. There are multiple servlet containers running on different servers.

Perhaps this graph makes it clear:

server1
+- servlet container
   +- application1
   |  +- thread1
   |  +- thread2
   +- application2
      +- thread1
      +- thread2
server2
+- servlet container
   +- application1
   |  +- thread1
   |  +- thread2
   +- application2
      +- thread1
      +- thread2

There is a file inside a network shared directory which all those threads can access. And they do access the file frequently. Most of the time the file is only read by those threads. But sometimes it is written.

I need a fail safe solution to synchronize all those threads so data consistency is guaranteed.


Solutions which do not work (properly):

  1. Using java.nio.channels.FileLock
    I am able to synchronize threads from different servers using the FileLock class. But this does not work for threads inside the same process (servlet container) since file locks are available process wide.

  2. Using a separate file for synchronization
    I could create a separate file which indicates that a process is reading from or wrinting to the file. This solution works for all threads but has several drawbacks:

    • Performance. Creating, deleting and checking files are rather slow operations. The low weight implementations with one synchronization file will prevent parallel reading of the file.
    • The synchronization file will remain after a JVM crash making a manual clean up necessary.
    • We have had already strange problems deleting files on network file systems.
  3. Using messaging
    We could implement a messaging system which the threads would use to coordinate the file access. But this seems too complex for this problem. And again: performance will be poor.

Any thoughts?

like image 462
Eduard Wirch Avatar asked Jan 27 '09 16:01

Eduard Wirch


2 Answers

You've enumerated the possible solutions except the obvious one: remove the dependency on that file

Is there another way for the threads to obtain that data instead of reading it from a file? How about setting up some kind of process who is responsible for coordinating access to that information instead of having all the threads read the file.

like image 52
Kevin Avatar answered Sep 22 '22 10:09

Kevin


A. Sounds like it's time for a database :-). Rather than having a shared file what about storing the data in a database.

B. Alternatively - layering:

  1. Lock threads within a process with a standard synchronized lock.
  2. Lock inter-process/machine with a file-based lock type thing - e.g. create a directory to hold the lock.

Nest 2 inside 1.

Still has the clean-up problem.

C. Alternatively some kind of write to new file/rename strategy so that reader don't need to lock maybe?

like image 28
Douglas Leeder Avatar answered Sep 26 '22 10:09

Douglas Leeder