66package state
77
88import (
9+ "context"
910 "fmt"
1011 "time"
1112
1213 "github.com/cockroachdb/cockroach/pkg/kv/kvpb"
1314 "github.com/cockroachdb/cockroach/pkg/kv/kvserver"
1415 "github.com/cockroachdb/cockroach/pkg/roachpb"
16+ "github.com/cockroachdb/cockroach/pkg/util/log"
1517 "github.com/google/btree"
1618)
1719
1820// Change is a state change for a range, to a target store that has some delay.
1921type Change interface {
2022 // Apply applies a change to the state.
21- Apply (s State )
23+ Apply (ctx context. Context , s State )
2224 // Target returns the recipient store of the change.
2325 Target () StoreID
2426 // Range returns the range id the change is for.
@@ -40,7 +42,7 @@ type Changer interface {
4042 Push (tick time.Time , sc Change ) (time.Time , bool )
4143 // Tick updates state changer to apply any changes that have occurred
4244 // between the last tick and this one.
43- Tick (tick time.Time , state State )
45+ Tick (ctx context. Context , tick time.Time , state State )
4446}
4547
4648// ReplicaChange contains information necessary to add, remove or move (both) a
@@ -70,9 +72,10 @@ type LeaseTransferChange struct {
7072}
7173
7274// Apply applies a change to the state.
73- func (lt * LeaseTransferChange ) Apply (s State ) {
75+ func (lt * LeaseTransferChange ) Apply (ctx context. Context , s State ) {
7476 if s .TransferLease (lt .RangeID , lt .TransferTarget ) {
7577 s .ClusterUsageInfo ().storeRef (lt .Author ).LeaseTransfers ++
78+ log .KvDistribution .VEventf (ctx , 3 , "r%d: s%d transferred lease to s%d (took %s)" , lt .RangeID , lt .Author , lt .TransferTarget , lt .Wait )
7679 }
7780}
7881
@@ -99,9 +102,10 @@ func (lt *LeaseTransferChange) Blocking() bool {
99102}
100103
101104// Apply applies a change to the state.
102- func (rsc * RangeSplitChange ) Apply (s State ) {
105+ func (rsc * RangeSplitChange ) Apply (ctx context. Context , s State ) {
103106 if _ , _ , ok := s .SplitRange (rsc .SplitKey ); ok {
104107 s .ClusterUsageInfo ().storeRef (rsc .Author ).RangeSplits ++
108+ log .KvDistribution .VEventf (ctx , 3 , "r%d: s%d split range at key=%d (took %s)" , rsc .RangeID , rsc .Author , rsc .SplitKey , rsc .Wait )
105109 }
106110}
107111
@@ -183,7 +187,7 @@ func replChangeHasStoreID(storeID StoreID, changes []roachpb.ReplicationTarget)
183187// Apply applies a replica change for a range. This is an implementation of the
184188// Change interface. It requires that a replica being removed, must not hold
185189// the lease unless a replica is also being added in the same change.
186- func (rc * ReplicaChange ) Apply (s State ) {
190+ func (rc * ReplicaChange ) Apply (ctx context. Context , s State ) {
187191 if len (rc .Changes ) == 0 {
188192 // Nothing to do.
189193 return
@@ -197,6 +201,11 @@ func (rc *ReplicaChange) Apply(s State) {
197201 rangeID := rc .RangeID
198202
199203 defer func () {
204+ if rollback != nil {
205+ log .KvDistribution .VEventf (ctx , 5 , "r%d: s%d failed to apply replica change (after %s)" , rangeID , rc .Author , rc .Wait )
206+ } else {
207+ log .KvDistribution .VEventf (ctx , 5 , "r%d: s%d successfully applied replica change (after %s)" , rangeID , rc .Author , rc .Wait )
208+ }
200209 n := len (rollback )
201210 for i := n - 1 ; i > - 1 ; i -- {
202211 if rollback [i ] != nil {
@@ -275,6 +284,7 @@ func (rc *ReplicaChange) Apply(s State) {
275284 for _ , nonVoterPromotion := range targets .NonVoterPromotions {
276285 ok , revert := promoDemo (
277286 s , rangeID , StoreID (nonVoterPromotion .StoreID ), roachpb .NON_VOTER , roachpb .VOTER_FULL )
287+ log .KvDistribution .VEventf (ctx , 5 , "r%d: author s%d promoted non-voter s%s to voter" , rangeID , rc .Author , nonVoterPromotion .StoreID )
278288 rollback = append (rollback , revert ... )
279289 if ! ok {
280290 return
@@ -283,6 +293,7 @@ func (rc *ReplicaChange) Apply(s State) {
283293 for _ , voterAddition := range targets .VoterAdditions {
284294 ok , revert := addReplica (
285295 s , rangeID , StoreID (voterAddition .StoreID ), roachpb .VOTER_FULL )
296+ log .KvDistribution .VEventf (ctx , 5 , "r%d: author s%d added voter s%s" , rangeID , rc .Author , voterAddition .StoreID )
286297 rollback = append (rollback , revert )
287298 if ! ok {
288299 return
@@ -296,6 +307,7 @@ func (rc *ReplicaChange) Apply(s State) {
296307 if ! s .TransferLease (rangeID , nextLH ) {
297308 return
298309 }
310+ log .KvDistribution .VEventf (ctx , 5 , "r%d: author s%d transferred lease to s%d" , rangeID , rc .Author , nextLH )
299311 rollback = append (rollback , func () {
300312 if ! s .TransferLease (rangeID , lhStore .StoreID ()) {
301313 panic ("unable to rollback lease transfer" )
@@ -306,6 +318,7 @@ func (rc *ReplicaChange) Apply(s State) {
306318 for _ , voterDemotion := range targets .VoterDemotions {
307319 ok , revert := promoDemo (
308320 s , rangeID , StoreID (voterDemotion .StoreID ), roachpb .VOTER_FULL , roachpb .NON_VOTER )
321+ log .KvDistribution .VEventf (ctx , 5 , "r%d: author s%d demoted voter s%s to non-voter" , rangeID , rc .Author , voterDemotion .StoreID )
309322 rollback = append (rollback , revert ... )
310323 if ! ok {
311324 return
@@ -314,6 +327,7 @@ func (rc *ReplicaChange) Apply(s State) {
314327 for _ , voterRemoval := range targets .VoterRemovals {
315328 ok , revert := removeReplica (
316329 s , rangeID , StoreID (voterRemoval .StoreID ), roachpb .VOTER_FULL )
330+ log .KvDistribution .VEventf (ctx , 5 , "r%d: author s%d removed voter s%s" , rangeID , rc .Author , voterRemoval .StoreID )
317331 rollback = append (rollback , revert )
318332 if ! ok {
319333 return
@@ -322,6 +336,7 @@ func (rc *ReplicaChange) Apply(s State) {
322336 for _ , nonVoterAddition := range targets .NonVoterAdditions {
323337 ok , revert := addReplica (
324338 s , rangeID , StoreID (nonVoterAddition .StoreID ), roachpb .NON_VOTER )
339+ log .KvDistribution .VEventf (ctx , 5 , "r%d: author s%d added non-voter s%s" , rangeID , rc .Author , nonVoterAddition .StoreID )
325340 rollback = append (rollback , revert )
326341 if ! ok {
327342 return
@@ -330,6 +345,7 @@ func (rc *ReplicaChange) Apply(s State) {
330345 for _ , nonVoterRemoval := range targets .NonVoterRemovals {
331346 ok , revert := removeReplica (
332347 s , rangeID , StoreID (nonVoterRemoval .StoreID ), roachpb .NON_VOTER )
348+ log .KvDistribution .VEventf (ctx , 5 , "r%d: author s%d removed non-voter s%s" , rangeID , rc .Author , nonVoterRemoval .StoreID )
333349 rollback = append (rollback , revert )
334350 if ! ok {
335351 return
@@ -345,6 +361,7 @@ func (rc *ReplicaChange) Apply(s State) {
345361
346362 authorUsageInfo := s .ClusterUsageInfo ().storeRef (rc .Author )
347363 authorUsageInfo .Rebalances ++
364+ log .KvDistribution .VEventf (ctx , 5 , "r%d: author s%d rebalancing" , rangeID , rc .Author )
348365 if requiresUpReplication {
349366 authorUsageInfo .RebalanceSentBytes += r .Size ()
350367 s .ClusterUsageInfo ().storeRef (storeNeedingSnapshot ).RebalanceRcvdBytes += r .Size ()
@@ -470,7 +487,7 @@ func (rc *replicaChanger) Push(tick time.Time, change Change) (time.Time, bool)
470487
471488// Tick updates state changer to apply any changes that have occurred
472489// between the last tick and this one.
473- func (rc * replicaChanger ) Tick (tick time.Time , state State ) {
490+ func (rc * replicaChanger ) Tick (ctx context. Context , tick time.Time , state State ) {
474491 var changeList []* pendingChange
475492
476493 // NB: Add the smallest unit of time, in order to find all items in
@@ -483,7 +500,7 @@ func (rc *replicaChanger) Tick(tick time.Time, state State) {
483500
484501 for _ , nextChange := range changeList {
485502 change := rc .pendingTickets [nextChange .ticket ]
486- change .Apply (state )
503+ change .Apply (ctx , state )
487504
488505 // Cleanup the pending trackers for this ticket. This allows another
489506 // change to be pushed for Range().
0 commit comments