The Trivial DataBase (ccan variant here) uses fcntl locks for consistency: records are chained off a fixed-size hash table (or the free list), and a 1-byte fcntl lock at the offset of the chain head protects all records in that chain.
There’s also a tdb_lockall() function which grabs a lock across all the hash chains at once to let you do arbitrary atomic munging.Â This works because fcntl() locks have an offset and length: you can lock arbitrary byte ranges.
Unfortunately, tdb_lockall() is subject to starvation, at least under Linux.Â This is because the kernel merely checks whether a lock is available and gets it if it can, rather than queuing behind someone else who wants a superset of the lock.Â So single byte lockers come in and out while the whole-file locker waits for them all to go away.
I’m not sure this is wrong, and as it’s simpler than the alternative, I’m not prepared to change it just yet.Â Fortunately, there’s a way of avoiding this starvation in userspace, which occurred independently to both Volker Lendecke and me.Â I called this variant tdb_lockall_gradual(), in which we try to lock each chain one at a time so we compete with the single-chain lockers on fair terms.Â My first naive thought was to try to lock all the chains one at a time in order, nonblocking, then go back and retry (blocking) any we failed to get.Â This is slow, and can deadlock against another process doing the same thing.Â Volker’s suggestion was much nicer: we do a non-blocking lock, and if that fails we divide and conquer.Â If we get down to a single record, we do a blocking lock.
I wroteÂ a test program which fires off N children, each of which grabs a random chain lock for 50-150 milliseconds before sleeping for 1 second, then repeating. The parent waits for a second, then tries to do a tdb_lockall() or tdb_lockall_gradual() depending on the commandline.Â Running it five times and showing the average wait time for the lockall gives this:
Now, regarding performance.Â While there can be 10000 hash chains, this isn’t as bad as it sounds.Â The fast case is the uncontended one, and that’s as fast as before, and the contended case is already slow.Â I annotated the source to print out how many blocking/nonblocking locks it’s actually doing.Â Inevitably, if there’s contention, it will end up dividing down to a blocking lock, so log(numchains) locks before doing a blocking lock.
|Processes||Blocking locks||Nonblocking locks||Seconds|
Sure, that’s a lot of locking when we’re competing with 5000 processes, but it’s less the naive one per chain, and it’s clear that it’s not the cause of the slowdown (we’re doing fewer locks per second than the 5 processes case).
And anyway, locking the entire database cannot be a speed-critical operation.Â Indeed, after the evidence here, I followed Volker’s suggestion to simply replace tdb_lockall() with the new implementation.
Did you run into any of the Linux file-locking issues?
> Did you run into any of the Linux file-locking issues?
Neither in this case, since TDB has been dealing with it for a long time.
Do you mean the scalability issues because it’s a linked list in the kernel, or the usability issue because POSIX is braindead?
I’m not sure if they’d even be posix locks any more if they queued like that.
Honestly I wish someone could just start over from scratch and design a posix-locks replacement. They’re nothing but pain.
Comments are closed.