Forum Moderators: coopster & phranque

Message Too Old, No Replies

More flocking fun

         

sugarkane

2:25 pm on Jan 23, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We've debated before whether it's necessary to flock files on read access, write access or both. The conclusion seemed to be that it was only necessary on write.

Well, here's a scenario I came across today:

- a cron job appends to a file, using exclusive lock
- a cgi uses this file to build a page, read only, so no flock
- when two instances of the cgi are running at the same time as the cron job, all three processes hang, system load rockets, and often restarting apache is the only way out.

I switched to using exclusive locks on every access, and the problem seems to be solved. I can't see a logical explanation for this - any ideas?

(Using Perl 5.004 on RH6.2)

Brett_Tabke

2:35 pm on Jan 23, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Let's start with this one:

>appends to a file, using exclusive lock

You shouldn't have to flock an append - I don't. Take the active list here. It gets abused with reads and writes all day long. The only time I flock it is when a read-modify-write sequence occurrs (replies), but on new posts (append), I never do. Nor do I use it when replies are written to threads. (I'm about to add this reply - no flock on append - no problem).

I can't see how the cron fits into it any different than a normal server request fulfillment.

I don't know if any of that helps in your situation.

sugarkane

2:47 pm on Jan 23, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> You shouldn't have to flock an append

Hmm... the file in question is used by several cgis, including some that read-modify-write. Suppose the file is appended to during the modify stage of another script? It's your classic 'race condition' - Bam! the appended data is lost... or am I missing something?

Brett_Tabke

2:48 pm on Jan 23, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The read-modify-write should do the flocking, which will block the append from happening. Follow?

sugarkane

2:55 pm on Jan 23, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My undestanding is that you have to specify a flock (whether exclusive or not) if you want Perl to respect any existing locks on a file. From perldoc:

"Such discretionary locks are more flexible, but offer
fewer guarantees. This means that files locked with C<flock> may be
modified by programs that do not also use C<flock>."

That reads to me that the append is allowed to happen if no flock is set for it?

bird

3:21 pm on Jan 23, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The read-modify-write should do the flocking, which will block the append from happening. Follow?

If one process accessing a file uses flock, then all other processes accessing the same file in whatever way have to use it as well, or interesting things will happen. The flock calls don't block anything, they only create "advisory locks". Which is quite similar a concept as "cooperative multitasking"... ;)

sugarkane

3:24 pm on Jan 23, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> advisory locks

Yep, just tested this with a couple of scripts. The appending process ignored the read-modify-write lock.

Brett_Tabke

4:58 pm on Jan 23, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



This doesn't compute. So, 13 million hits last year, 175k public/private message, and we were just lucky? hmm

sugarkane

5:15 pm on Jan 23, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> we were just lucky

Depending on the speed of the modification script, the window of opportunity for corruption is miniscule. But a race condition will bite eventually, you can bet on it.

ggrot

5:20 pm on Jan 23, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is why I like transactional databases.

bird

10:25 pm on Jan 23, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



13 million hits last year, 175k public/private message, and we were just lucky?

Lucky indeed!

On the other hand, if you're talking about the forums here, the editing operations are probably very few and far between, compared to the appends. If I'm calculating correctly, then your figure sums up to an average of 20 messages added per hour, which in reality probably oscillates between 1 and 500. This means that, in an estimated worst case, you get less than ten appends per minute, distributed among more than 30 forums and probably hundreds of threads. slipping a few edits in between there only takes a lot of luck, not a hell of a lot... ;)

Brett_Tabke

7:36 am on Jan 24, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



> The flock calls don't block anything,
> they only create "advisory locks".

I just didn't realize that. BOTH the read and write processes have to use flock. I thought it was at the file system/os level.