Skip to content

Commit 1e68789

Browse files
committed
Reduce snapshot check frequency
Previously, a bug was introduced that added a `LIST` operation on every replica sync which significantly increased the cost of running Litestream against S3. This changes the behavior to only issue the `LIST` operation when the generation has changed.
1 parent 55c17b9 commit 1e68789

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

replica.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,22 +159,21 @@ func (r *Replica) Sync(ctx context.Context) (err error) {
159159

160160
Tracef("%s(%s): replica sync: db.pos=%s", r.db.Path(), r.Name(), dpos)
161161

162-
// Create snapshot if no snapshots exist for generation.
163-
snapshotN, err := r.snapshotN(generation)
164-
if err != nil {
165-
return err
166-
} else if snapshotN == 0 {
167-
if info, err := r.Snapshot(ctx); err != nil {
162+
// Create a new snapshot and update the current replica position if
163+
// the generation on the database has changed.
164+
if r.Pos().Generation != generation {
165+
// Create snapshot if no snapshots exist for generation.
166+
snapshotN, err := r.snapshotN(generation)
167+
if err != nil {
168168
return err
169-
} else if info.Generation != generation {
170-
return fmt.Errorf("generation changed during snapshot, exiting sync")
169+
} else if snapshotN == 0 {
170+
if info, err := r.Snapshot(ctx); err != nil {
171+
return err
172+
} else if info.Generation != generation {
173+
return fmt.Errorf("generation changed during snapshot, exiting sync")
174+
}
171175
}
172-
snapshotN = 1
173-
}
174-
replicaSnapshotTotalGaugeVec.WithLabelValues(r.db.Path(), r.Name()).Set(float64(snapshotN))
175176

176-
// Determine position, if necessary.
177-
if r.Pos().Generation != generation {
178177
pos, err := r.calcPos(ctx, generation)
179178
if err != nil {
180179
return fmt.Errorf("cannot determine replica position: %s", err)
@@ -1307,13 +1306,6 @@ func (r *Replica) downloadWAL(ctx context.Context, generation string, index int,
13071306

13081307
// Replica metrics.
13091308
var (
1310-
replicaSnapshotTotalGaugeVec = promauto.NewGaugeVec(prometheus.GaugeOpts{
1311-
Namespace: "litestream",
1312-
Subsystem: "replica",
1313-
Name: "snapshot_total",
1314-
Help: "The current number of snapshots",
1315-
}, []string{"db", "name"})
1316-
13171309
replicaWALBytesCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{
13181310
Namespace: "litestream",
13191311
Subsystem: "replica",

0 commit comments

Comments
 (0)