Skip to content

Commit

Permalink
Sync replica snapshots to previous
Browse files Browse the repository at this point in the history
If Litestream is restarted often with high snapshot interval it's
possible for it to skip multiple snapshots in a row causing long
restore times and inconsistent storage patterns.
  • Loading branch information
hifi committed Oct 14, 2023
1 parent fb0311b commit 3dfd623
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,31 @@ func (r *Replica) snapshotter(ctx context.Context) {
return
}

logger := r.Logger()
if pos, err := r.db.Pos(); err != nil {
logger.Error("snapshotter cannot determine generation", "error", err)
} else if !pos.IsZero() {
if snapshot, err := r.maxSnapshot(ctx, pos.Generation); err != nil {
logger.Error("snapshotter cannot determine latest snapshot", "error", err)
} else if snapshot != nil {
nextSnapshot := r.SnapshotInterval - time.Since(snapshot.CreatedAt)
if nextSnapshot < 0 {
nextSnapshot = 0
}

logger.Info("snapshot interval adjusted", "previous", snapshot.CreatedAt.Format(time.RFC3339), "next", nextSnapshot.String())

select {
case <-ctx.Done():
return
case <-time.After(nextSnapshot):
if _, err := r.Snapshot(ctx); err != nil && err != ErrNoGeneration {
logger.Error("snapshotter error", "error", err)
}
}
}
}

ticker := time.NewTicker(r.SnapshotInterval)
defer ticker.Stop()

Expand Down

0 comments on commit 3dfd623

Please sign in to comment.