tursodatabase / libsql

libSQL is a fork of SQLite that is both Open Source, and Open Contributions.
https://turso.tech/libsql
MIT License
9.54k stars 252 forks source link

fix replica snapshot isolation bug #1734

Closed MarinPostma closed 2 weeks ago

MarinPostma commented 2 weeks ago

This PR fixes a bug in libsql-wal transaction isolation that was thankfully caught by some assertions left behind.

Basically, we used to check that the frames we read from the main segment would not violate the max_frame_no property, we would use the log offset combined with the start offset in the index. This all worked fine with the regular mode of operation where frames are inserted in order, with increment of 1 in the wal. Things started to break with the replica, because frames can be inserted out-of-order, and with random increments.

To fix that, we remove the start_frame_no from the index, and instead we record the max_offset in the transaction: that offset correspond the max frame_no in the current segment, iow, any frame before that offset is guaranteed to be less that max_frame_no for that transaction.