You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@subversion.apache.org by Malcolm Rowe <ma...@farside.org.uk> on 2007/03/05 08:07:19 UTC

Merge tracking: will SQLite make NFS sad?

Hi Dan,

I notice that SQLite currently recommends [1] that you don't use it over a
network filesystem.  People are currently happily using FSFS repositories
stored on NAS boxes (over CIFS and NFS), because we've been careful to
make sure it works.

How do we reconcile these two statements?

I'm also wondering how SQLite would work out with multiple clients:
conceivably you could deploy a farm of mod_dav_svn servers for
load-balancing purposes, and they could share a single backend FS on a
NAS.

In my (albeit limited) experience of SQLite, it doesn't seem to attempt a
great deal of concurrency (until recently, you could deadlock by deleting
rows that you were currently iterating through with a SELECT).

Regards,
Malcolm

[1] http://www.sqlite.org/lockingv3.html
    (under "How To Corrupt Your Database Files")

Re: Merge tracking: will SQLite make NFS sad?

Posted by Daniel Rall <dl...@collab.net>.
On Mon, 05 Mar 2007, Malcolm Rowe wrote:

> On Mon, Mar 05, 2007 at 12:34:06PM -0500, Daniel Berlin wrote:
> > >In my (albeit limited) experience of SQLite, it doesn't seem to attempt a
> > >great deal of concurrency (until recently, you could deadlock by deleting
> > >rows that you were currently iterating through with a SELECT).
> > 
> > They don't recommend it mainly for the same reasons nobody recommends
> > doing anything that requires locking over NFS:
> > Too many buggy locking implementations.
> > 
> > If you actually take a look at what sqlite does code wise, it requires
> > that exactly the same lock mechanisms work that we require work for
> > FSFS, so we're fine if FSFS would work.
> 
> Well, it's not quite the same.  FSFS only requires that whole-file
> advisory locking works, while SQLite seems to require 'proper' byte
> range locking.  I don't know if there's a difference in the quality of
> client implementations in practice.

DannyB indicated that last time he looked at SQLite's code, that it
was only doing whole-file locking.

While I haven't looked at its code, after reading "File Locking And
Concurrency In SQLite Version 3" <http://www.sqlite.org/lockingv3.html>
(the document Malcolm referenced in his original post), I'd guess that
SQLite is taking out POSIX advisory locks on its journal (probably in
addition to its DB files), and using the journal to coordinate
byte-range locking on the actual database file.  Again, I haven't
confirmed this...

> I'm also interested to know what SQLite's concurrency is like: do we
> have to do anything special to ensure that multiple readers (on different
> threads, processes, machines) can co-exist happily with writers?

Section 2.0 and 3.0 of lockingv3.html indicate that SQLite v3 locking
is per-thread (or process), and effectively works much like FSFS for
writes.

Writer starvation, defined as no thread ever being able to make
changes to the database because of inability to acquire a write lock,
was a concurrency problem in SQLite v2.  This was caused by there
never being a time when all readers were inactive, and is handled in
v3 by a new PENDING lock state, which temporarily blocks new read
locks from being acquired, allowing the writer to (eventually) persist
its changes to the DB (see section 5.1 of lockingv3.html).
 
> (By the way, don't take this as a dig at SQLite at all - I think it's
> great, and I use it myself for single-user applications, but I don't
> know what the characteristics are for multi-user or shared filesystem
> access).

For sure -- let's air these potential issues now.

Re: Merge tracking: will SQLite make NFS sad?

Posted by Bill Bumgarner <bb...@mac.com>.
On Mar 5, 2007, at 2:07 PM, Daniel Rall wrote:
> At least on *nix, SQLite uses runtime detection of file system type,
> and (possibly) even actual fcntl() behavior, to determine the type of
> locking to use (e.g. POSIX fcntl() locking vs. dot locking).  See the
> sqlite3DetectLockingStyle() and sqlite3TestLockingStyle() routines of
> <http://www.sqlite.org/cvstrac/fileview?f=sqlite/src/ 
> os_unix.c&v=1.118>.

Hey! I wrote that code!   Specifically, the code was written for Apple  
in the context of the Tiger release.   It is relatively conservative,  
choosing .lock style all-or-none locking on NFS even when fcntl() may  
actually work.   A number of NFS servers completely lie about their  
locking capabilities, thus the defensive posture of that code.

It has sense been updated as the locking code in SQLite has evolved.

In general, you want to avoid using SQLite in the many-writers-one- 
database situation.  It works relatively well on a local filesystem,  
but can be somewhat inefficient.   For network filesystems, the  
performance drops significantly if you have to back off to completely  
exclusive locks -- only one reader or writer at a time -- or it may  
fail entirely if the network filesystem lies about capabilities.

Dealing with the transactions associated with "unsticking" locks is  
not difficult once you understand how it works.

As stated, SQLite uses fcntl() based byte range locks to escalate  
locks through a range from read only to exclusive to support both  
multiple readers and a queue of potential writers.

The problem happens when:

- client A begins a transaction and does a select (thus grabbing a  
read lock)
- client B begins a transaction and does a select (thus grabbing a  
read lock)
- client A does an insert or update, thus escalating the lock to  
PENDING (i.e. indicates that it wants exclusive, but doesn't have it  
yet)
- client B tries to do an insert / update and fails to grab the  
PENDING because A is already PENDING
- client A can't commit the transaction between a write requires the  
PENDING to be escalated to EXCLUSIVE
- neither A or B end their transaction, each looping waiting for their  
locks to be escalated
- user is unhappy

So, manage your transactions carefully and back the hell off when you  
can't escalate a lock!

b.bum

Re: Merge tracking: will SQLite make NFS sad?

Posted by Daniel Rall <dl...@collab.net>.
On Mon, 05 Mar 2007, Florian Weimer wrote:

> * Malcolm Rowe:
> 
> > Well, it's not quite the same.  FSFS only requires that whole-file
> > advisory locking works, while SQLite seems to require 'proper' byte
> > range locking.
> 
> They are not essential, byte range locks are only used to increase
> fairness AFAICT.

I just spent some time looking through the latest CVS source code of
SQLite <http://www.sqlite.org/cvstrac/dir?d=sqlite>.  To my unfamiliar
eye, things look pretty good even for network file systems.

It appears that POSIX fcntl() byte range locking isn't currently used
to protect actual ranges of data.  Instead, when available it's used
to manage lock types to improve concurrency characteristics.

At least on *nix, SQLite uses runtime detection of file system type,
and (possibly) even actual fcntl() behavior, to determine the type of
locking to use (e.g. POSIX fcntl() locking vs. dot locking).  See the
sqlite3DetectLockingStyle() and sqlite3TestLockingStyle() routines of
<http://www.sqlite.org/cvstrac/fileview?f=sqlite/src/os_unix.c&v=1.118>.

AFAICT, this protects us against file systems which don't fully
support POSIX locking, so long as their APIs they don't lie about
their capabilities.


While this isn't particularly friendly for our general use case,
SQLite even lets you set a fixed locking style at compile-time by
defining SQLITE_FIXED_LOCKING_STYLE to a value from its
sqlite3LockingStyle enumeration:

/*
** The locking styles are associated with the different file locking
** capabilities supported by different file systems.  
**
** POSIX locking style fully supports shared and exclusive byte-range locks 
** ADP locking only supports exclusive byte-range locks
** FLOCK only supports a single file-global exclusive lock
** DOTLOCK isn't a true locking style, it refers to the use of a special
**   file named the same as the database file with a '.lock' extension, this
**   can be used on file systems that do not offer any reliable file locking
** NO locking means that no locking will be attempted, this is only used for
**   read-only file systems currently
** UNSUPPORTED means that no locking will be attempted, this is only used for
**   file systems that are known to be unsupported
*/
typedef enum {
	posixLockingStyle = 0,       /* standard posix-advisory locks */
	afpLockingStyle,             /* use afp locks */
	flockLockingStyle,           /* use flock() */
	dotlockLockingStyle,         /* use <file>.lock files */
	noLockingStyle,              /* useful for read-only file system */
	unsupportedLockingStyle      /* indicates unsupported file system */
} sqlite3LockingStyle;


Re: Merge tracking: will SQLite make NFS sad?

Posted by Florian Weimer <fw...@deneb.enyo.de>.
* Malcolm Rowe:

> Well, it's not quite the same.  FSFS only requires that whole-file
> advisory locking works, while SQLite seems to require 'proper' byte
> range locking.

They are not essential, byte range locks are only used to increase
fairness AFAICT.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: Merge tracking: will SQLite make NFS sad?

Posted by Malcolm Rowe <ma...@farside.org.uk>.
On Mon, Mar 05, 2007 at 12:34:06PM -0500, Daniel Berlin wrote:
> >In my (albeit limited) experience of SQLite, it doesn't seem to attempt a
> >great deal of concurrency (until recently, you could deadlock by deleting
> >rows that you were currently iterating through with a SELECT).
> 
> They don't recommend it mainly for the same reasons nobody recommends
> doing anything that requires locking over NFS:
> Too many buggy locking implementations.
> 
> If you actually take a look at what sqlite does code wise, it requires
> that exactly the same lock mechanisms work that we require work for
> FSFS, so we're fine if FSFS would work.
> 

Well, it's not quite the same.  FSFS only requires that whole-file
advisory locking works, while SQLite seems to require 'proper' byte
range locking.  I don't know if there's a difference in the quality of
client implementations in practice.

I'm also interested to know what SQLite's concurrency is like: do we
have to do anything special to ensure that multiple readers (on different
threads, processes, machines) can co-exist happily with writers?

(By the way, don't take this as a dig at SQLite at all - I think it's
great, and I use it myself for single-user applications, but I don't
know what the characteristics are for multi-user or shared filesystem
access).

Regards,
Malcolm

Re: Merge tracking: will SQLite make NFS sad?

Posted by Daniel Berlin <db...@dberlin.org>.
> In my (albeit limited) experience of SQLite, it doesn't seem to attempt a
> great deal of concurrency (until recently, you could deadlock by deleting
> rows that you were currently iterating through with a SELECT).

They don't recommend it mainly for the same reasons nobody recommends
doing anything that requires locking over NFS:
Too many buggy locking implementations.

If you actually take a look at what sqlite does code wise, it requires
that exactly the same lock mechanisms work that we require work for
FSFS, so we're fine if FSFS would work.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: Merge tracking: will SQLite make NFS sad?

Posted by Daniel Rall <dl...@collab.net>.
On Mon, 05 Mar 2007, Malcolm Rowe wrote:

> On Mon, Mar 05, 2007 at 09:15:30AM -0500, Mark Phippard wrote:
> > On 3/5/07, David Glasser <gl...@mit.edu> wrote:
> > >My impression (which may be out of date) is that sqlite2 used only
> > >file-level locking (like fsfs), but sqlite3 uses byte-level locking.
> > >I don't know much about NFS, but this is certainly bad for AFS at
> > >least.
>
> I don't know about sqlite2, but yes, that's my understanding of sqlite3
> too.  FSFS only uses whole-file byte-range locks (which are a special
> case of POSIX fcntl() locks).

My other mail on this thread explores the file locking with SQLite.

> > If the database writes are all being serialized at a higher level by fsfs
> > (meaning it ensures that only one transaction at a time is being written)
> 
> FSFS ensures that only one transaction is being committed at one time -
> are you doing the SQLite writes under the fs-wide write-lock?

The SQLite writes happen by way of the call to
svn_fs_merge_info__update_index() in libsvn_fs_fs/fs_fs.c:commit_body()
(documented as "called with the FS write lock"), and occur immediately
before we update the 'current' file.

My fuzz-fuzz fu is weak, but I believe this means the answer is "yes".

http://svn.collab.net/viewvc/svn/branches/merge-tracking/subversion/libsvn_fs_fs/fs_fs.c?view=markup

> > does that insulate us from problems at all?  Or does the fact that other
> > client might be reading the database mean that corruption could happen?  I
> > would assume it at least means that incorrect answers could be returned.
> 
> Hence my question.

Re: Merge tracking: will SQLite make NFS sad?

Posted by Malcolm Rowe <ma...@farside.org.uk>.
On Mon, Mar 05, 2007 at 09:15:30AM -0500, Mark Phippard wrote:
> On 3/5/07, David Glasser <gl...@mit.edu> wrote:
> >My impression (which may be out of date) is that sqlite2 used only
> >file-level locking (like fsfs), but sqlite3 uses byte-level locking.
> >I don't know much about NFS, but this is certainly bad for AFS at
> >least.

I don't know about sqlite2, but yes, that's my understanding of sqlite3
too.  FSFS only uses whole-file byte-range locks (which are a special
case of POSIX fcntl() locks).

> If the database writes are all being serialized at a higher level by fsfs
> (meaning it ensures that only one transaction at a time is being written)

FSFS ensures that only one transaction is being committed at one time -
are you doing the SQLite writes under the fs-wide write-lock?

> does that insulate us from problems at all?  Or does the fact that other
> client might be reading the database mean that corruption could happen?  I
> would assume it at least means that incorrect answers could be returned.

Hence my question.

Regards,
Malcolm

Re: Merge tracking: will SQLite make NFS sad?

Posted by Mark Phippard <ma...@gmail.com>.
On 3/5/07, David Glasser <gl...@mit.edu> wrote:
>
> On 3/5/07, Mark Phippard <ma...@gmail.com> wrote:
> > Doesn't fsfs itself use similar locking mechanisms to what is described
> in
> > those docs?  I thought this was discussed once and that was the answer
> as to
> > why using SQLite was not making things any worse.  Of course I suppose
> it is
> > possible that SQLite could be more dependent on them than the fsfs
> > implementation.
>
> My impression (which may be out of date) is that sqlite2 used only
> file-level locking (like fsfs), but sqlite3 uses byte-level locking.
> I don't know much about NFS, but this is certainly bad for AFS at
> least.


If the database writes are all being serialized at a higher level by fsfs
(meaning it ensures that only one transaction at a time is being written)
does that insulate us from problems at all?  Or does the fact that other
client might be reading the database mean that corruption could happen?  I
would assume it at least means that incorrect answers could be returned.

-- 
Thanks

Mark Phippard
http://markphip.blogspot.com/

Re: Merge tracking: will SQLite make NFS sad?

Posted by Mark Phippard <ma...@gmail.com>.
On 3/5/07, Malcolm Rowe <ma...@farside.org.uk> wrote:
>
>
> I notice that SQLite currently recommends [1] that you don't use it over a
> network filesystem.  People are currently happily using FSFS repositories
> stored on NAS boxes (over CIFS and NFS), because we've been careful to
> make sure it works.
>
> How do we reconcile these two statements?
>
> I'm also wondering how SQLite would work out with multiple clients:
> conceivably you could deploy a farm of mod_dav_svn servers for
> load-balancing purposes, and they could share a single backend FS on a
> NAS.
>
> In my (albeit limited) experience of SQLite, it doesn't seem to attempt a
> great deal of concurrency (until recently, you could deadlock by deleting
> rows that you were currently iterating through with a SELECT).


Doesn't fsfs itself use similar locking mechanisms to what is described in
those docs?  I thought this was discussed once and that was the answer as to
why using SQLite was not making things any worse.  Of course I suppose it is
possible that SQLite could be more dependent on them than the fsfs
implementation.

At some point, doesn't the question come down to "what is the alternative"?
We appear to need something like what SQLite offers to implement the
feature, and we all agree the merge tracking feature is high priority, so
what is the alternative?

-- 
Thanks

Mark Phippard
http://markphip.blogspot.com/