Skip to content

Commit

Permalink
Sync replica snapshots to previous (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
hifi authored Oct 20, 2023
1 parent b1abd6b commit d02ba97
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 d02ba97

Please sign in to comment.