Skip to content

Commit 45ac58b

Browse files
committed
kvserver: comment and style updates
1 parent 19e1155 commit 45ac58b

File tree

3 files changed

+13
-25
lines changed

3 files changed

+13
-25
lines changed

pkg/kv/kvserver/replica_application_result.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,9 @@ func (r *Replica) stagePendingTruncationRaftMuLocked(pt pendingTruncation) {
506506
r.asLogStorage().stagePendingTruncationRaftMuLocked(pt)
507507
}
508508

509-
func (r *replicaLogStorage) stageApplySnapshot(truncState kvserverpb.RaftTruncatedState) {
509+
func (r *replicaLogStorage) stageApplySnapshotRaftMuLocked(
510+
truncState kvserverpb.RaftTruncatedState,
511+
) {
510512
r.raftMu.AssertHeld()
511513

512514
// A snapshot application implies a log truncation to the snapshot's index,
@@ -522,33 +524,20 @@ func (r *replicaLogStorage) stageApplySnapshot(truncState kvserverpb.RaftTruncat
522524
// section but before the clear will see an empty log anyway, since the
523525
// in-memory state is already updated to reflect the truncation, even if
524526
// 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.
530527
defer r.cache.Drop(r.ls.RangeID)
531528

532529
r.mu.Lock()
533530
defer r.mu.Unlock()
534531

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.
542537
//
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.
552541
r.updateStateRaftMuLockedMuLocked(logstore.RaftState{
553542
LastIndex: truncState.Index,
554543
LastTerm: truncState.Term,

pkg/kv/kvserver/replica_raft.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,6 @@ func (r *Replica) handleRaftReadyRaftMuLocked(
12821282
// pass both engines in.
12831283
sm.r.assertStateRaftMuLockedReplicaMuRLocked(ctx, sm.r.store.TODOEngine())
12841284
sm.r.mu.RUnlock()
1285-
12861285
}
12871286

12881287
if refreshReason != noReason {

pkg/kv/kvserver/replica_raftstorage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ func (r *Replica) applySnapshotRaftMuLocked(
610610

611611
// Stage the truncation, so that in-memory state reflects an
612612
// empty log.
613-
ls.stageApplySnapshot(truncState)
613+
ls.stageApplySnapshotRaftMuLocked(truncState)
614614

615615
stats.subsumedReplicas = timeutil.Now()
616616

@@ -682,7 +682,7 @@ func (r *Replica) applySnapshotRaftMuLocked(
682682
state.RaftAppliedIndexTerm, nonemptySnap.Metadata.Term)
683683
}
684684
if ls.shMu.size != 0 {
685-
log.Fatalf(ctx, "expected empty raftLogSize after snapshot, got %d", ls.shMu.size)
685+
log.Fatalf(ctx, "expected empty raft log after snapshot, got %d", ls.shMu.size)
686686
}
687687

688688
// Read the prior read summary for this range, which was included in the

0 commit comments

Comments
 (0)