Stephen, fantastic sleuthing and work to fix.  Guys - I'm anxious to hear the results using our tests.<div><br></div><div>Stephen, great work by your team!</div><div>Dave</div><div><br clear="all"><div>David M. Oliver | <a href="mailto:david@olivercoady.com" target="_blank">david@olivercoady.com</a> | <a href="http://olivercoady.com" target="_blank">http://olivercoady.com</a> | <a href="http://dmo.tel" target="_blank">http://dmo.tel</a> | @davidmoliver | +1 970 368 2366</div>


<br><br><div class="gmail_quote">On Tue, Jan 22, 2013 at 10:51 AM, Stephen Lombardo <span dir="ltr"><<a href="mailto:sjlombardo@zetetic.net" target="_blank">sjlombardo@zetetic.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hello Everyone,<br>
<br>
We've made some solid progress with locking and stabilization of libsqlfs under multithreaded use.<br>
<br>
We started by reproducing the problems with the library under load. This involved firing up a fuse mount and running three concurrent fsx processes against the same sqlfs file system. This test quickly produced file corruption, usually within 30 seconds. From there, enhanced tracing showed that the underlying cause had to do with the database being locked at various points during execution. Even thought the library was opening an exclusive transaction, there were still numerous opportunities for the transaction to be blocked by a reader, or multiple writers to block each other. Similar behavior could also be seen with deferred transactions (i.e. standard begin).<br>


<br>
This lead us to make several change to collectively improve the stability of the library.<br>
<br>
First, we changed the transaction command in begin_transaction to use "begin immediate". This seeks an immediate reserved lock on the database, but does not exclusively lock it. This reduces unresolvable contention for write locks that would normally occur with deferred transactions, and is less restrictive than an exclusive lock, since it will continue to allow shared locks for reading.<br>


<br>
It is extremely important that we prevent write operations from failing to execute due to busy timeouts, even if another process/thread has the database locked. Even using WAL, it is still possible for a command to be blocked during attempted concurrent write operations. This causes the write operation to fail leading to corruption. While libsqlfs has some "delay()" code that provides rudimentary busy handling, it is only in use for a small number of operations leaving other critical calls unprotected. Therefore, our second change was to register SQLite's internal busy handler with a relatively high timeout (currently 10 seconds, but open for discussion) via sqlite3_busy_timeout. This provides protection for all operations in libsqlfs, reducing the likelihood that a write operation would fail outright, though it may be delayed.<br>


<br>
Finally, we enabled WAL mode to speed up write operations and further improve concurrency between readers and writers. Note that WAL mode only fsync()s on checkpoint operations, so it may be possible to enable NORMAL synchronous mode with lower overhead than the standard journal mode (we didn't change this yet).<br>


<br>
With these changes in place, three concurrent fsx processes running in parallel on a single fuse mount produced no errors in a 24hr test run. The tests also shows improved performance on read and writes. In light of these results, we'd like to get your feedback on these changes, and request that you run your own tests in the multi-threaded Android application to see if they resolve the problems that were reported.<br>


<br>
All the changes are available here:<br>
<br>
<a href="https://github.com/sjlombardo/libsqlfs/commit/90aee5079cb26d4f4e9046b0f8d94f6e92067049" target="_blank">https://github.com/sjlombardo/libsqlfs/commit/90aee5079cb26d4f4e9046b0f8d94f6e92067049</a><br>
<br>
Please let us know if you have any questions. Thanks!<br>
<br>
Cheers,<br>
Stephen<br>
<div class="HOEnZb"><div class="h5"><br>
On 2013-01-18, Hans-Christoph Steiner wrote:<br>
> On 01/18/2013 09:24 AM, David Oliver wrote:<br>
> > Thanks Stephen for this.  It seems like we're closing in on completion here<br>
> > with the final step being Stephen's team looking at the lock issue and to<br>
> > advise Guardian as to fixes/changes/etc that we (Guardian) would implement.<br>
> ><br>
> > Stephen - can you provide a timeframe for your locking review?<br>
</div></div></blockquote></div><br></div>