@@ -13,7 +13,6 @@ import (
13
13
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
14
14
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
15
15
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvstorage/snaprecv"
16
- "github.com/cockroachdb/cockroach/pkg/kv/kvserver/logstore"
17
16
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/rditer"
18
17
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/readsummary"
19
18
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/spanset"
@@ -607,15 +606,11 @@ func (r *Replica) applySnapshotRaftMuLocked(
607
606
clearedSpans = append (clearedSpans , clearedUnreplicatedSpan )
608
607
clearedSpans = append (clearedSpans , clearedSubsumedSpans ... )
609
608
610
- // Drop the entry cache before ingestion, like a real truncation would.
611
- //
612
- // TODO(sep-raft-log): like a real truncation, we should also bump the
613
- // in-memory truncated state to the snapshot index. We should also assert
614
- // that this leads to a (logically) empty log (otherwise we wouldn't have
615
- // accepted the snapshot).
616
- //
617
- // See: https://github.com/cockroachdb/cockroach/pull/145328#discussion_r2068209588
618
- r .store .raftEntryCache .Drop (r .RangeID )
609
+ ls := r .asLogStorage ()
610
+
611
+ // Stage the truncation, so that in-memory state reflects an
612
+ // empty log.
613
+ ls .stageApplySnapshotRaftMuLocked (truncState )
619
614
620
615
stats .subsumedReplicas = timeutil .Now ()
621
616
@@ -655,6 +650,9 @@ func (r *Replica) applySnapshotRaftMuLocked(
655
650
// snapshot.
656
651
writeBytes = uint64 (inSnap .SSTSize )
657
652
}
653
+ // The snapshot is visible, so finalize the truncation.
654
+ ls .finalizeApplySnapshotRaftMuLocked (ctx )
655
+
658
656
// The "ignored" here is to ignore the writes to create the AC linear models
659
657
// for LSM writes. Since these writes typically correspond to actual writes
660
658
// onto the disk, we account for them separately in
@@ -683,6 +681,9 @@ func (r *Replica) applySnapshotRaftMuLocked(
683
681
log .Fatalf (ctx , "snapshot RaftAppliedIndexTerm %d doesn't match its metadata term %d" ,
684
682
state .RaftAppliedIndexTerm , nonemptySnap .Metadata .Term )
685
683
}
684
+ if ls .shMu .size != 0 {
685
+ log .Fatalf (ctx , "expected empty raft log after snapshot, got %d" , ls .shMu .size )
686
+ }
686
687
687
688
// Read the prior read summary for this range, which was included in the
688
689
// snapshot. We may need to use it to bump our timestamp cache if we
@@ -737,23 +738,6 @@ func (r *Replica) applySnapshotRaftMuLocked(
737
738
// without risking a lock-ordering deadlock.
738
739
r .store .mu .Unlock ()
739
740
740
- // The log has been cleared and reset to start at the snapshot's applied
741
- // index/term. Update the in-memory metadata accordingly.
742
- r .asLogStorage ().updateStateRaftMuLockedMuLocked (logstore.RaftState {
743
- LastIndex : truncState .Index ,
744
- LastTerm : truncState .Term ,
745
- ByteSize : 0 , // the log is empty now
746
- })
747
- ls := r .asLogStorage ()
748
- ls .shMu .trunc = truncState
749
- // Snapshots typically have fewer log entries than the leaseholder. The next
750
- // time we hold the lease, recompute the log size before making decisions.
751
- //
752
- // TODO(pav-kv): does this assume that snapshots can contain log entries,
753
- // which is no longer true? The comment needs an update, and the decision to
754
- // set this flag to false revisited.
755
- ls .shMu .sizeTrusted = false
756
-
757
741
// Update the store stats for the data in the snapshot.
758
742
r .store .metrics .subtractMVCCStats (ctx , r .tenantMetricsRef , * r .shMu .state .Stats )
759
743
r .store .metrics .addMVCCStats (ctx , r .tenantMetricsRef , * state .Stats )
@@ -791,14 +775,6 @@ func (r *Replica) applySnapshotRaftMuLocked(
791
775
792
776
r .mu .Unlock ()
793
777
794
- // Assert that the in-memory and on-disk states of the Replica are congruent
795
- // after the application of the snapshot. Do so under a read lock, as this
796
- // operation can be expensive. This is safe, as we hold the Replica.raftMu
797
- // across both Replica.mu critical sections.
798
- r .mu .RLock ()
799
- r .assertStateRaftMuLockedReplicaMuRLocked (ctx , r .store .TODOEngine ())
800
- r .mu .RUnlock ()
801
-
802
778
// The rangefeed processor is listening for the logical ops attached to
803
779
// each raft command. These will be lost during a snapshot, so disconnect
804
780
// the rangefeed, if one exists.
0 commit comments