I'm managing a rather large project that uses flat files and a series of hashes written to disk (ala Data::Dumper) to store data.
Some of our clients are trying to push the system to extremes it was never intended nor designed to handle.
Most of those are doing that unintentionally (by placing the system on slow, old servers, etc).
Unfortunately for me, it's my job to figure out what to do about it. :D
Here's the situation in short.
We're using flock (use Fcntl ':flock'; flock(FILE, LOCK_EX), etc) to valiantly try to prevent the scripts from clobbering each other's data.
It isn't working. Allow me to demonstrate with one of the many situations we've encountered:
Process 1 needs to rewrite and ADD TO some of the data in FILE.
Process 2 needs to rewrite some of the data in FILE.
Process 1 opens, flocks (shared), and reads FILE.
Process 2 opens, flocks (shared), and reads FILE.
Process 1 appends the data internally.
Process 2 alters the data internally.
Process 1 opens, truncates, and flocks FILE (exclusive) Process 2 is also able to open and truncate FILE, right as Process 1 is doing the same.
Process 2 gets blocked after the open by Process 1's flock.
Process 1 happily dumps its data into FILE.
Process 1 releases the lock & closes FILE.
Process 2 notes the lock has been released and begins rewriting FILE.
Process 2 releases the lock & closes FILE.
FILE now has the data set from process 1 overwritten by the lesser data in process 2, resulting in the file being quite corrupt.
This is bad.
I need to figure out if there is any way to establish a file lock BEFORE I open a file.
Using a lock file is not out of the question. The currently prevailing idea is to use a handful of lock files (for different functions, etc), then use flock to establish and release locks while work is being done.
At best, this is a messy, awful workaround, and is not suitable for production.
Does anyone have any wisdom to share? Is there any way to lock a file before opening it? Is there a way to use a central lock file without it making the gods cringe?
I thank you in advance for any knowledge you may wish to share, and swear that I will do my best to help others if I have the knowledge.
:)
In reply to File locking, lock files, and how it all sucks by tocie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |