Skip to content

Commit 19e1155

Browse files
committed
kvserver: avoid leaking sideloaded entries with snapshot
1 parent b637406 commit 19e1155

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pkg/kv/kvserver/replica_application_result.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,27 @@ func (r *replicaLogStorage) stageApplySnapshot(truncState kvserverpb.RaftTruncat
559559
r.shMu.sizeTrusted = true
560560
}
561561

562+
func (r *replicaLogStorage) finalizeApplySnapshotRaftMuLocked(ctx context.Context) {
563+
r.raftMu.AssertHeld()
564+
// This mirrors finalizeTruncationRaftMuLocked, but a snapshot may regress the last
565+
// index (to discard a divergent log). For example:
566+
//
567+
// Raft log (before snapshot):
568+
// - entry 100-150: term 1 [committed]
569+
// - entry 151-200: term 2
570+
// Committed raft log (on leader):
571+
// - entry 100-150: term 1
572+
// - entry 151: term 3
573+
//
574+
// The replica may receive a snapshot at index 151. If we don't clear the
575+
// sideloaded storage all the way up to the *old* last index, we may leak
576+
// sideloaded entries. Rather than remember the old last index, we instead
577+
// clear the sideloaded storage entirely. This is equivalent.
578+
if err := r.ls.Sideload.Clear(ctx); err != nil {
579+
log.Errorf(ctx, "while clearing sideloaded storage after snapshot: %+v", err)
580+
}
581+
}
582+
562583
func (r *replicaLogStorage) stagePendingTruncationRaftMuLocked(pt pendingTruncation) {
563584
r.raftMu.AssertHeld()
564585
// NB: The expected first index can be zero if this proposal is from before

pkg/kv/kvserver/replica_raftstorage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ func (r *Replica) applySnapshotRaftMuLocked(
651651
writeBytes = uint64(inSnap.SSTSize)
652652
}
653653
// The snapshot is visible, so finalize the truncation.
654-
r.finalizeTruncationRaftMuLocked(ctx)
654+
ls.finalizeApplySnapshotRaftMuLocked(ctx)
655655

656656
// The "ignored" here is to ignore the writes to create the AC linear models
657657
// for LSM writes. Since these writes typically correspond to actual writes

0 commit comments

Comments
 (0)