@@ -35,7 +35,6 @@ import (
3535 "github.com/cockroachdb/cockroach/pkg/keys"
3636 "github.com/cockroachdb/cockroach/pkg/roachpb"
3737 "github.com/cockroachdb/cockroach/pkg/rpc"
38- "github.com/cockroachdb/cockroach/pkg/settings/cluster"
3938 "github.com/cockroachdb/cockroach/pkg/storage/batcheval"
4039 "github.com/cockroachdb/cockroach/pkg/storage/engine"
4140 "github.com/cockroachdb/cockroach/pkg/storage/engine/enginepb"
@@ -9742,173 +9741,6 @@ func TestReplicaPushed1PC(t *testing.T) {
97429741 }
97439742}
97449743
9745- // assertUsingRangeAppliedState asserts that the value of
9746- // ReplicaState.UsingAppliedStateKey is equal to the expected value.
9747- func assertUsingRangeAppliedState (t * testing.T , repl * Replica , expSet bool ) {
9748- t .Helper ()
9749- repl .raftMu .Lock ()
9750- defer repl .raftMu .Unlock ()
9751- repl .mu .Lock ()
9752- defer repl .mu .Unlock ()
9753-
9754- usingAppliedStateKey := repl .mu .state .UsingAppliedStateKey
9755- if usingAppliedStateKey != expSet {
9756- t .Errorf ("expected ReplicaState.UsingAppliedStateKey=%t, found %t" ,
9757- expSet , usingAppliedStateKey )
9758- }
9759- }
9760-
9761- // assertRangeAppliedStateRelatedKeysExist performs a series of assertions
9762- // that each key related to the RangeAppliedState key migration is either
9763- // present or missing, depending on the expRASK flag.
9764- func assertRangeAppliedStateRelatedKeysExist (
9765- ctx context.Context , t * testing.T , repl * Replica , expRASK bool ,
9766- ) {
9767- t .Helper ()
9768- repl .raftMu .Lock ()
9769- defer repl .raftMu .Unlock ()
9770- repl .mu .Lock ()
9771- defer repl .mu .Unlock ()
9772-
9773- assertHasKey := func (key roachpb.Key , expect bool ) {
9774- t .Helper ()
9775- val , _ , err := engine .MVCCGet (ctx , repl .store .Engine (), key , hlc.Timestamp {},
9776- engine.MVCCGetOptions {})
9777- if err != nil {
9778- t .Fatal (err )
9779- }
9780-
9781- exists := val != nil
9782- if exists != expect {
9783- t .Errorf ("expected key %s to exist=%t, found %t" , key , expect , exists )
9784- }
9785- }
9786-
9787- rsl := repl .mu .stateLoader
9788- assertHasKey (rsl .RangeAppliedStateKey (), expRASK )
9789- assertHasKey (rsl .RaftAppliedIndexLegacyKey (), ! expRASK )
9790- assertHasKey (rsl .LeaseAppliedIndexLegacyKey (), ! expRASK )
9791- assertHasKey (rsl .RangeStatsLegacyKey (), ! expRASK )
9792- }
9793-
9794- // TestReplicaBootstrapRangeAppliedStateKey verifies that a bootstrapped range
9795- // is only created with a RangeAppliedStateKey if the cluster version is high
9796- // enough to permit it.
9797- func TestReplicaBootstrapRangeAppliedStateKey (t * testing.T ) {
9798- defer leaktest .AfterTest (t )()
9799-
9800- testCases := []struct {
9801- version roachpb.Version
9802- expRangeAppliedStateKey bool
9803- }{
9804- {
9805- version : cluster .VersionByKey (cluster .Version2_0 ),
9806- expRangeAppliedStateKey : false ,
9807- },
9808- {
9809- version : cluster .VersionByKey (cluster .VersionRangeAppliedStateKey ),
9810- expRangeAppliedStateKey : true ,
9811- },
9812- {
9813- version : cluster .BinaryServerVersion ,
9814- expRangeAppliedStateKey : true ,
9815- },
9816- }
9817- for _ , c := range testCases {
9818- t .Run (fmt .Sprintf ("version=%s" , c .version ), func (t * testing.T ) {
9819- ctx := context .Background ()
9820- stopper := stop .NewStopper ()
9821- defer stopper .Stop (ctx )
9822-
9823- cfg := TestStoreConfig (nil )
9824- cfg .Settings = cluster .MakeTestingClusterSettingsWithVersion (
9825- c .version /* minVersion */ , c .version /* serverVersion */ )
9826- tc := testContext {}
9827- tc .StartWithStoreConfig (t , stopper , cfg )
9828- repl := tc .repl
9829-
9830- // Check that that UsingAppliedStateKey flag in ReplicaState is set
9831- // as expected.
9832- assertInMemState := func (t * testing.T ) {
9833- t .Helper ()
9834- assertUsingRangeAppliedState (t , repl , c .expRangeAppliedStateKey )
9835- }
9836-
9837- // Check that persisted keys agree with the UsingAppliedStateKey flag.
9838- assertPersistentState := func (t * testing.T ) {
9839- t .Helper ()
9840- assertRangeAppliedStateRelatedKeysExist (ctx , t , repl , c .expRangeAppliedStateKey )
9841- }
9842-
9843- // Check that in-mem and persistent state agree.
9844- assertInMemAndPersistentStateAgree := func (t * testing.T ) {
9845- t .Helper ()
9846- repl .AssertState (ctx , tc .engine )
9847- }
9848-
9849- // Check that the MVCCStats are correct.
9850- computeStatsDelta := func (db * client.DB ) (enginepb.MVCCStats , error ) {
9851- var b client.Batch
9852- b .AddRawRequest (& roachpb.RecomputeStatsRequest {
9853- RequestHeader : roachpb.RequestHeader {Key : roachpb .KeyMin },
9854- DryRun : true ,
9855- })
9856- if err := db .Run (ctx , & b ); err != nil {
9857- return enginepb.MVCCStats {}, err
9858- }
9859- resp := b .RawResponse ().Responses [0 ].GetInner ().(* roachpb.RecomputeStatsResponse )
9860- delta := enginepb .MVCCStats (resp .AddedDelta )
9861- delta .AgeTo (0 )
9862- return delta , nil
9863- }
9864- assertEmptyStatsDelta := func (t * testing.T ) {
9865- t .Helper ()
9866- delta , err := computeStatsDelta (repl .DB ())
9867- if err != nil {
9868- t .Fatal (err )
9869- }
9870- if delta != (enginepb.MVCCStats {}) {
9871- t .Errorf ("unexpected stats adjustment of %+v" , delta )
9872- }
9873- }
9874-
9875- // Perform initial series of assertions.
9876- assertInMemState (t )
9877- assertPersistentState (t )
9878- assertInMemAndPersistentStateAgree (t )
9879- assertEmptyStatsDelta (t )
9880-
9881- // Save the ReplicaState and perform persistent assertions again.
9882- repl .raftMu .Lock ()
9883- repl .mu .Lock ()
9884- if _ , err := repl .mu .stateLoader .Save (
9885- ctx , tc .engine , repl .mu .state ,
9886- stateloader .TruncatedStateUnreplicated ,
9887- ); err != nil {
9888- t .Fatalf ("could not save ReplicaState: %v" , err )
9889- }
9890- repl .mu .Unlock ()
9891- repl .raftMu .Unlock ()
9892- assertPersistentState (t )
9893- assertInMemAndPersistentStateAgree (t )
9894- assertEmptyStatsDelta (t )
9895-
9896- // Load the ReplicaState and perform in-memory assertions again.
9897- repl .raftMu .Lock ()
9898- repl .mu .Lock ()
9899- state , err := repl .mu .stateLoader .Load (ctx , tc .engine , repl .DescLocked ())
9900- if err != nil {
9901- t .Fatalf ("could not load ReplicaState: %v" , err )
9902- }
9903- repl .mu .state = state
9904- repl .mu .Unlock ()
9905- repl .raftMu .Unlock ()
9906- assertInMemState (t )
9907- assertInMemAndPersistentStateAgree (t )
9908- })
9909- }
9910- }
9911-
99129744func TestReplicaShouldCampaignOnWake (t * testing.T ) {
99139745 defer leaktest .AfterTest (t )()
99149746
0 commit comments