@@ -506,7 +506,9 @@ func (r *Replica) stagePendingTruncationRaftMuLocked(pt pendingTruncation) {
506
506
r .asLogStorage ().stagePendingTruncationRaftMuLocked (pt )
507
507
}
508
508
509
- func (r * replicaLogStorage ) stageApplySnapshot (truncState kvserverpb.RaftTruncatedState ) {
509
+ func (r * replicaLogStorage ) stageApplySnapshotRaftMuLocked (
510
+ truncState kvserverpb.RaftTruncatedState ,
511
+ ) {
510
512
r .raftMu .AssertHeld ()
511
513
512
514
// A snapshot application implies a log truncation to the snapshot's index,
@@ -522,33 +524,20 @@ func (r *replicaLogStorage) stageApplySnapshot(truncState kvserverpb.RaftTruncat
522
524
// section but before the clear will see an empty log anyway, since the
523
525
// in-memory state is already updated to reflect the truncation, even if
524
526
// entries are still present in the cache.
525
- //
526
- // NB: a reader that obtained bounds pre-critical section might be able to
527
- // load entries, though, and could repopulate the cache after it has been
528
- // cleared - the cache is not "snapshotted". Ideally, mu-only readers simply
529
- // cannot populate the cache.
530
527
defer r .cache .Drop (r .ls .RangeID )
531
528
532
529
r .mu .Lock ()
533
530
defer r .mu .Unlock ()
534
531
535
- // Raft never accepts a snapshot that does not increase the commit index, and
536
- // the commit index always refers to a log entry (unless the log is empty
537
- // already). In particular, any entries in the log are guaranteed to be at
538
- // indexes that this truncation will remove, and the result is an empty log
539
- // (and raft entry cache). This is true even if the RawNode has entries lined
540
- // up that it wants to append to the log[1] (on top of the snapshot), as these
541
- // entries are not yet stable and thus not in the log/cache yet.
532
+ // On snapshots, the entire log is cleared. This is safe:
533
+ // - log entries preceding the entry represented by the snapshot are durable
534
+ // via the snapshot itself, and
535
+ // - committed log entries ahead of the snapshot index were not acked by this
536
+ // replica, or raft would not have accepted this snapshot.
542
537
//
543
- // [1]: this is not properly supported yet and will currently fatal.
544
- // See: https://github.com/cockroachdb/cockroach/pull/125530
545
- // We also, in the same mu critical section, update the in-memory metadata
546
- // accordingly before the change is visible on the engine. This means that
547
- // even if someone used the in-memory state to grab an iterator (all within
548
- // the same mu section), they would either see pre-snapshot raft log, or the
549
- // post-snapshot (empty) log, but never any in-between state in which the
550
- // first and last index are out of sync either with each other or with what's
551
- // actually on the log engine.
538
+ // Here, we update the in-memory state to reflect this before making the
539
+ // corresponding change to on-disk state. This makes sure that concurrent
540
+ // readers don't try to access entries no longer present in the log.
552
541
r .updateStateRaftMuLockedMuLocked (logstore.RaftState {
553
542
LastIndex : truncState .Index ,
554
543
LastTerm : truncState .Term ,
0 commit comments