Use sqlite3_file_control(SQLITE_FCNTL_PERSIST_WAL) to persist WAL

Previously, Litestream would avoid closing the SQLite3 connection
in order to ensure that the WAL file was not cleaned up by the
database if it was the last connection. This commit changes the
behavior by introducing a file control call to perform the same
action. This allows us to close the database file normally in all
cases.
This commit is contained in:
Ben Johnson
2022-01-28 15:05:21 -07:00
parent 1741c82839
commit dbdde21341
6 changed files with 27 additions and 24 deletions

28
db.go
View File

@@ -405,20 +405,9 @@ func (db *DB) Open() (err error) {
return nil
}
// Close releases the read lock & closes the database. This method should only
// be called by tests as it causes the underlying database to be checkpointed.
// Close flushes outstanding WAL writes to replicas, releases the read lock,
// and closes the database.
func (db *DB) Close() (err error) {
return db.close(false)
}
// SoftClose closes everything but the underlying db connection. This method
// is available because the binary needs to avoid closing the database on exit
// to prevent autocheckpointing.
func (db *DB) SoftClose() (err error) {
return db.close(true)
}
func (db *DB) close(soft bool) (err error) {
db.cancel()
db.wg.Wait()
@@ -439,7 +428,7 @@ func (db *DB) close(soft bool) (err error) {
err = e
}
}
r.Stop(!soft)
r.Stop(true)
}
// Release the read lock to allow other applications to handle checkpointing.
@@ -449,9 +438,7 @@ func (db *DB) close(soft bool) (err error) {
}
}
// Only perform full close if this is not a soft close.
// This closes the underlying database connection which can clean up the WAL.
if !soft && db.db != nil {
if db.db != nil {
if e := db.db.Close(); e != nil && err == nil {
err = e
}
@@ -507,8 +494,9 @@ func (db *DB) init() (err error) {
dsn := db.path
dsn += fmt.Sprintf("?_busy_timeout=%d", BusyTimeout.Milliseconds())
// Connect to SQLite database.
if db.db, err = sql.Open("sqlite3", dsn); err != nil {
// Connect to SQLite database. Use the driver registered with a hook to
// prevent WAL files from being removed.
if db.db, err = sql.Open("litestream-sqlite3", dsn); err != nil {
return err
}
@@ -1536,7 +1524,7 @@ func ApplyWAL(ctx context.Context, dbPath, walPath string) error {
}
// Open SQLite database and force a truncating checkpoint.
d, err := sql.Open("sqlite3", dbPath)
d, err := sql.Open("litestream-sqlite3", dbPath)
if err != nil {
return err
}