Skip to content

Commit

Permalink
Use safe checkpointing before snapshots
Browse files Browse the repository at this point in the history
The intention of the code is to do a checkpoint before starting to
write out the snapshot. Executing a manual checkpoint will at worst
make Litestream lose the track of the WAL as it will in specific
circumtances flush out the single page in the WAL file out.

Using the public checkpoint function of the database to issue a
managed passive checkpoint will ensure the sequence table is
written after and the position of the WAL isn't lost.

Fixes benbjohnson#521
  • Loading branch information
hifi committed Nov 15, 2023
1 parent 85ddf32 commit 11e0f05
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,16 +477,16 @@ func (r *Replica) Snapshot(ctx context.Context) (info SnapshotInfo, err error) {
r.muf.Lock()
defer r.muf.Unlock()

// Prevent checkpoints during snapshot.
r.db.BeginSnapshot()
defer r.db.EndSnapshot()

// Issue a passive checkpoint to flush any pages to disk before snapshotting.
if _, err := r.db.db.ExecContext(ctx, `PRAGMA wal_checkpoint(PASSIVE);`); err != nil {
if err := r.db.Checkpoint(ctx, CheckpointModePassive); err != nil {
return info, fmt.Errorf("pre-snapshot checkpoint: %w", err)
}

// Acquire a read lock on the database during snapshot to prevent checkpoints.
// Prevent internal checkpoints during snapshot.
r.db.BeginSnapshot()
defer r.db.EndSnapshot()

// Acquire a read lock on the database during snapshot to prevent external checkpoints.
tx, err := r.db.db.Begin()
if err != nil {
return info, err
Expand Down

0 comments on commit 11e0f05

Please sign in to comment.