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

current segment recovery #1613

Closed MarinPostma closed 1 month ago

MarinPostma commented 2 months ago

The goal of this PR is to make the current segment recoverable in case of a crash. Taking inspiration from SQLite's wal, we introduce a grinning checksum to the current log, where each frame is hashed with the hash of the previous frame. If we find an unsealed segment on startup, we enter recovery mode for that segment, meaning that we iterate over its frames, recomputing the checksums. We stop whenever we hit a checksum that breaks the list invariant and patch the segment. This means that in NORMAL synchronous mode, we can lose committed data on the crash, but this is expected. We'll introduce a more robust sync mode later that flushes on every commit and has stronger guarantees.

This scheme introduces a bit more complexity:

I have added a test that simulates a crash during flush, where not all pages are flushed to disk, to demonstrate recovery.