-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
replicate_queue.go
1437 lines (1340 loc) · 51 KB
/
replicate_queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package kvserver
import (
"bytes"
"context"
"fmt"
"sync/atomic"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness/livenesspb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/metric"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"go.etcd.io/etcd/raft/v3"
)
const (
// replicateQueueTimerDuration is the duration between replication of queued
// replicas.
replicateQueueTimerDuration = 0 // zero duration to process replication greedily
// newReplicaGracePeriod is the amount of time that we allow for a new
// replica's raft state to catch up to the leader's before we start
// considering it to be behind for the sake of rebalancing. We choose a
// large value here because snapshots of large replicas can take a while
// in high latency clusters, and not allowing enough of a cushion can
// make rebalance thrashing more likely (#17879).
newReplicaGracePeriod = 5 * time.Minute
)
// MinLeaseTransferInterval controls how frequently leases can be transferred
// for rebalancing. It does not prevent transferring leases in order to allow
// a replica to be removed from a range.
var MinLeaseTransferInterval = settings.RegisterDurationSetting(
"kv.allocator.min_lease_transfer_interval",
"controls how frequently leases can be transferred for rebalancing. "+
"It does not prevent transferring leases in order to allow a "+
"replica to be removed from a range.",
1*time.Second,
settings.NonNegativeDuration,
)
// TODO(aayush): Expand this metric set to include metrics about non-voting replicas.
var (
metaReplicateQueueAddReplicaCount = metric.Metadata{
Name: "queue.replicate.addreplica",
Help: "Number of replica additions attempted by the replicate queue",
Measurement: "Replica Additions",
Unit: metric.Unit_COUNT,
}
metaReplicateQueueRemoveReplicaCount = metric.Metadata{
Name: "queue.replicate.removereplica",
Help: "Number of replica removals attempted by the replicate queue (typically in response to a rebalancer-initiated addition)",
Measurement: "Replica Removals",
Unit: metric.Unit_COUNT,
}
metaReplicateQueueRemoveDeadReplicaCount = metric.Metadata{
Name: "queue.replicate.removedeadreplica",
Help: "Number of dead replica removals attempted by the replicate queue (typically in response to a node outage)",
Measurement: "Replica Removals",
Unit: metric.Unit_COUNT,
}
metaReplicateQueueRemoveLearnerReplicaCount = metric.Metadata{
Name: "queue.replicate.removelearnerreplica",
Help: "Number of learner replica removals attempted by the replicate queue (typically due to internal race conditions)",
Measurement: "Replica Removals",
Unit: metric.Unit_COUNT,
}
metaReplicateQueueRebalanceReplicaCount = metric.Metadata{
Name: "queue.replicate.rebalancereplica",
Help: "Number of replica rebalancer-initiated additions attempted by the replicate queue",
Measurement: "Replica Additions",
Unit: metric.Unit_COUNT,
}
metaReplicateQueueTransferLeaseCount = metric.Metadata{
Name: "queue.replicate.transferlease",
Help: "Number of range lease transfers attempted by the replicate queue",
Measurement: "Lease Transfers",
Unit: metric.Unit_COUNT,
}
metaReplicateQueueNonVoterPromotionsCount = metric.Metadata{
Name: "queue.replicate.nonvoterpromotions",
Help: "Number of non-voters promoted to voters by the replicate queue",
Measurement: "Promotions of Non Voters to Voters",
Unit: metric.Unit_COUNT,
}
metaReplicateQueueVoterDemotionsCount = metric.Metadata{
Name: "queue.replicate.voterdemotions",
Help: "Number of voters demoted to non-voters by the replicate queue",
Measurement: "Demotions of Voters to Non Voters",
Unit: metric.Unit_COUNT,
}
)
// quorumError indicates a retryable error condition which sends replicas being
// processed through the replicate queue into purgatory so that they can be
// retried quickly as soon as nodes come online.
type quorumError struct {
msg string
}
func newQuorumError(f string, args ...interface{}) *quorumError {
return &quorumError{
msg: fmt.Sprintf(f, args...),
}
}
func (e *quorumError) Error() string {
return e.msg
}
func (*quorumError) purgatoryErrorMarker() {}
// ReplicateQueueMetrics is the set of metrics for the replicate queue.
// TODO(aayush): Track metrics for non-voting replicas separately here.
type ReplicateQueueMetrics struct {
AddReplicaCount *metric.Counter
RemoveReplicaCount *metric.Counter
RemoveDeadReplicaCount *metric.Counter
RemoveLearnerReplicaCount *metric.Counter
RebalanceReplicaCount *metric.Counter
TransferLeaseCount *metric.Counter
NonVoterPromotionsCount *metric.Counter
VoterDemotionsCount *metric.Counter
}
func makeReplicateQueueMetrics() ReplicateQueueMetrics {
return ReplicateQueueMetrics{
AddReplicaCount: metric.NewCounter(metaReplicateQueueAddReplicaCount),
RemoveReplicaCount: metric.NewCounter(metaReplicateQueueRemoveReplicaCount),
RemoveDeadReplicaCount: metric.NewCounter(metaReplicateQueueRemoveDeadReplicaCount),
RemoveLearnerReplicaCount: metric.NewCounter(metaReplicateQueueRemoveLearnerReplicaCount),
RebalanceReplicaCount: metric.NewCounter(metaReplicateQueueRebalanceReplicaCount),
TransferLeaseCount: metric.NewCounter(metaReplicateQueueTransferLeaseCount),
NonVoterPromotionsCount: metric.NewCounter(metaReplicateQueueNonVoterPromotionsCount),
VoterDemotionsCount: metric.NewCounter(metaReplicateQueueVoterDemotionsCount),
}
}
// replicateQueue manages a queue of replicas which may need to add an
// additional replica to their range.
type replicateQueue struct {
*baseQueue
metrics ReplicateQueueMetrics
allocator Allocator
updateChan chan time.Time
lastLeaseTransfer atomic.Value // read and written by scanner & queue goroutines
}
// newReplicateQueue returns a new instance of replicateQueue.
func newReplicateQueue(store *Store, g *gossip.Gossip, allocator Allocator) *replicateQueue {
rq := &replicateQueue{
metrics: makeReplicateQueueMetrics(),
allocator: allocator,
updateChan: make(chan time.Time, 1),
}
store.metrics.registry.AddMetricStruct(&rq.metrics)
rq.baseQueue = newBaseQueue(
"replicate", rq, store, g,
queueConfig{
maxSize: defaultQueueMaxSize,
needsLease: true,
needsSystemConfig: true,
acceptsUnsplitRanges: store.TestingKnobs().ReplicateQueueAcceptsUnsplit,
// The processing of the replicate queue often needs to send snapshots
// so we use the raftSnapshotQueueTimeoutFunc. This function sets a
// timeout based on the range size and the sending rate in addition
// to consulting the setting which controls the minimum timeout.
processTimeoutFunc: makeRateLimitedTimeoutFunc(rebalanceSnapshotRate),
successes: store.metrics.ReplicateQueueSuccesses,
failures: store.metrics.ReplicateQueueFailures,
pending: store.metrics.ReplicateQueuePending,
processingNanos: store.metrics.ReplicateQueueProcessingNanos,
purgatory: store.metrics.ReplicateQueuePurgatory,
},
)
updateFn := func() {
select {
case rq.updateChan <- timeutil.Now():
default:
}
}
// Register gossip and node liveness callbacks to signal that
// replicas in purgatory might be retried.
if g != nil { // gossip is nil for some unittests
g.RegisterCallback(gossip.MakePrefixPattern(gossip.KeyStorePrefix), func(key string, _ roachpb.Value) {
if !rq.store.IsStarted() {
return
}
// Because updates to our store's own descriptor won't affect
// replicas in purgatory, skip updating the purgatory channel
// in this case.
if storeID, err := gossip.StoreIDFromKey(key); err == nil && storeID == rq.store.StoreID() {
return
}
updateFn()
})
}
if nl := store.cfg.NodeLiveness; nl != nil { // node liveness is nil for some unittests
nl.RegisterCallback(func(_ livenesspb.Liveness) {
updateFn()
})
}
return rq
}
func (rq *replicateQueue) shouldQueue(
ctx context.Context, now hlc.ClockTimestamp, repl *Replica, sysCfg *config.SystemConfig,
) (shouldQ bool, priority float64) {
desc, zone := repl.DescAndZone()
action, priority := rq.allocator.ComputeAction(ctx, zone, desc)
// For simplicity, the first thing the allocator does is remove learners, so
// it can do all of its reasoning about only voters. We do the same here so
// the executions of the allocator's decisions can be in terms of voters.
if action == AllocatorRemoveLearner {
return true, priority
}
voterReplicas := desc.Replicas().VoterDescriptors()
nonVoterReplicas := desc.Replicas().NonVoterDescriptors()
if action == AllocatorNoop {
log.VEventf(ctx, 2, "no action to take")
return false, 0
} else if action != AllocatorConsiderRebalance {
log.VEventf(ctx, 2, "repair needed (%s), enqueuing", action)
return true, priority
}
if !rq.store.TestingKnobs().DisableReplicaRebalancing {
rangeUsageInfo := rangeUsageInfoForRepl(repl)
_, _, _, ok := rq.allocator.RebalanceVoter(
ctx,
zone,
repl.RaftStatus(),
voterReplicas,
nonVoterReplicas,
rangeUsageInfo,
storeFilterThrottled,
)
if ok {
log.VEventf(ctx, 2, "rebalance target found for voter, enqueuing")
return true, 0
}
_, _, _, ok = rq.allocator.RebalanceNonVoter(
ctx,
zone,
repl.RaftStatus(),
voterReplicas,
nonVoterReplicas,
rangeUsageInfo,
storeFilterThrottled,
)
if ok {
log.VEventf(ctx, 2, "rebalance target found for non-voter, enqueuing")
return true, 0
}
log.VEventf(ctx, 2, "no rebalance target found, not enqueuing")
}
// If the lease is valid, check to see if we should transfer it.
status := repl.LeaseStatusAt(ctx, now)
if status.IsValid() &&
rq.canTransferLeaseFrom(ctx, repl) &&
rq.allocator.ShouldTransferLease(ctx, zone, voterReplicas, status.Lease.Replica.StoreID, repl.leaseholderStats) {
log.VEventf(ctx, 2, "lease transfer needed, enqueuing")
return true, 0
}
return false, 0
}
func (rq *replicateQueue) process(
ctx context.Context, repl *Replica, sysCfg *config.SystemConfig,
) (processed bool, err error) {
retryOpts := retry.Options{
InitialBackoff: 50 * time.Millisecond,
MaxBackoff: 1 * time.Second,
Multiplier: 2,
MaxRetries: 5,
}
// Use a retry loop in order to backoff in the case of snapshot errors,
// usually signaling that a rebalancing reservation could not be made with the
// selected target.
for r := retry.StartWithCtx(ctx, retryOpts); r.Next(); {
for {
requeue, err := rq.processOneChange(ctx, repl, rq.canTransferLeaseFrom, false /* dryRun */)
if isSnapshotError(err) {
// If ChangeReplicas failed because the snapshot failed, we log the
// error but then return success indicating we should retry the
// operation. The most likely causes of the snapshot failing are a
// declined reservation or the remote node being unavailable. In either
// case we don't want to wait another scanner cycle before reconsidering
// the range.
log.Infof(ctx, "%v", err)
break
}
if err != nil {
return false, err
}
if testingAggressiveConsistencyChecks {
if _, err := rq.store.consistencyQueue.process(ctx, repl, sysCfg); err != nil {
log.Warningf(ctx, "%v", err)
}
}
if !requeue {
return true, nil
}
log.VEventf(ctx, 1, "re-processing")
}
}
return false, errors.Errorf("failed to replicate after %d retries", retryOpts.MaxRetries)
}
func (rq *replicateQueue) processOneChange(
ctx context.Context,
repl *Replica,
canTransferLeaseFrom func(ctx context.Context, repl *Replica) bool,
dryRun bool,
) (requeue bool, _ error) {
// Check lease and destroy status here. The queue does this higher up already, but
// adminScatter (and potential other future callers) also call this method and don't
// perform this check, which could lead to infinite loops.
if _, err := repl.IsDestroyed(); err != nil {
return false, err
}
if _, pErr := repl.redirectOnOrAcquireLease(ctx); pErr != nil {
return false, pErr.GoError()
}
// TODO(aayush): The fact that we're calling `repl.DescAndZone()` here once to
// pass to `ComputeAction()` to use for deciding which action to take to
// repair a range, and then calling it again inside methods like
// `addOrReplace{Non}Voters()` or `remove{Dead,Decommissioning}` to execute
// upon that decision is a bit unfortunate. It means that we could
// successfully execute a decision that was based on the state of a stale
// range descriptor.
desc, zone := repl.DescAndZone()
// Avoid taking action if the range has too many dead replicas to make
// quorum.
voterReplicas := desc.Replicas().VoterDescriptors()
nonVoterReplicas := desc.Replicas().NonVoterDescriptors()
liveVoterReplicas, deadVoterReplicas := rq.allocator.storePool.liveAndDeadReplicas(voterReplicas)
liveNonVoterReplicas, deadNonVoterReplicas := rq.allocator.storePool.liveAndDeadReplicas(nonVoterReplicas)
// NB: the replication layer ensures that the below operations don't cause
// unavailability; see:
_ = execChangeReplicasTxn
action, _ := rq.allocator.ComputeAction(ctx, zone, desc)
log.VEventf(ctx, 1, "next replica action: %s", action)
// For simplicity, the first thing the allocator does is remove learners, so
// it can do all of its reasoning about only voters. We do the same here so
// the executions of the allocator's decisions can be in terms of voters.
if action == AllocatorRemoveLearner {
return rq.removeLearner(ctx, repl, dryRun)
}
switch action {
case AllocatorNoop, AllocatorRangeUnavailable:
// We're either missing liveness information or the range is known to have
// lost quorum. Either way, it's not a good idea to make changes right now.
// Let the scanner requeue it again later.
return false, nil
// Add replicas.
case AllocatorAddVoter:
return rq.addOrReplaceVoters(ctx, repl, liveVoterReplicas, liveNonVoterReplicas, -1 /* removeIdx */, dryRun)
case AllocatorAddNonVoter:
return rq.addOrReplaceNonVoters(ctx, repl, liveVoterReplicas, liveNonVoterReplicas, -1 /* removeIdx */, dryRun)
// Remove replicas.
case AllocatorRemoveVoter:
return rq.removeVoter(ctx, repl, voterReplicas, nonVoterReplicas, dryRun)
case AllocatorRemoveNonVoter:
return rq.removeNonVoter(ctx, repl, voterReplicas, nonVoterReplicas, dryRun)
// Replace dead replicas.
case AllocatorReplaceDeadVoter:
if len(deadVoterReplicas) == 0 {
// Nothing to do.
return false, nil
}
removeIdx := getRemoveIdx(voterReplicas, deadVoterReplicas[0])
if removeIdx < 0 {
return false, errors.AssertionFailedf(
"dead voter %v unexpectedly not found in %v",
deadVoterReplicas[0], voterReplicas)
}
return rq.addOrReplaceVoters(ctx, repl, liveVoterReplicas, liveNonVoterReplicas, removeIdx, dryRun)
case AllocatorReplaceDeadNonVoter:
if len(deadNonVoterReplicas) == 0 {
// Nothing to do.
return false, nil
}
removeIdx := getRemoveIdx(nonVoterReplicas, deadNonVoterReplicas[0])
if removeIdx < 0 {
return false, errors.AssertionFailedf(
"dead non-voter %v unexpectedly not found in %v",
deadNonVoterReplicas[0], nonVoterReplicas)
}
return rq.addOrReplaceNonVoters(ctx, repl, liveVoterReplicas, liveNonVoterReplicas, removeIdx, dryRun)
// Replace decommissioning replicas.
case AllocatorReplaceDecommissioningVoter:
decommissioningVoterReplicas := rq.allocator.storePool.decommissioningReplicas(voterReplicas)
if len(decommissioningVoterReplicas) == 0 {
// Nothing to do.
return false, nil
}
removeIdx := getRemoveIdx(voterReplicas, decommissioningVoterReplicas[0])
if removeIdx < 0 {
return false, errors.AssertionFailedf(
"decommissioning voter %v unexpectedly not found in %v",
decommissioningVoterReplicas[0], voterReplicas)
}
return rq.addOrReplaceVoters(ctx, repl, liveVoterReplicas, liveNonVoterReplicas, removeIdx, dryRun)
case AllocatorReplaceDecommissioningNonVoter:
decommissioningNonVoterReplicas := rq.allocator.storePool.decommissioningReplicas(nonVoterReplicas)
if len(decommissioningNonVoterReplicas) == 0 {
return false, nil
}
removeIdx := getRemoveIdx(nonVoterReplicas, decommissioningNonVoterReplicas[0])
if removeIdx < 0 {
return false, errors.AssertionFailedf(
"decommissioning non-voter %v unexpectedly not found in %v",
decommissioningNonVoterReplicas[0], nonVoterReplicas)
}
return rq.addOrReplaceNonVoters(ctx, repl, liveVoterReplicas, liveNonVoterReplicas, removeIdx, dryRun)
// Remove decommissioning replicas.
//
// NB: these two paths will only be hit when the range is over-replicated and
// has decommissioning replicas; in the common case we'll hit
// AllocatorReplaceDecommissioning{Non}Voter above.
case AllocatorRemoveDecommissioningVoter:
return rq.removeDecommissioning(ctx, repl, voterTarget, dryRun)
case AllocatorRemoveDecommissioningNonVoter:
return rq.removeDecommissioning(ctx, repl, nonVoterTarget, dryRun)
// Remove dead replicas.
//
// NB: these two paths below will only be hit when the range is
// over-replicated and has dead replicas; in the common case we'll hit
// AllocatorReplaceDead{Non}Voter above.
case AllocatorRemoveDeadVoter:
return rq.removeDead(ctx, repl, deadVoterReplicas, voterTarget, dryRun)
case AllocatorRemoveDeadNonVoter:
return rq.removeDead(ctx, repl, deadNonVoterReplicas, nonVoterTarget, dryRun)
case AllocatorRemoveLearner:
return rq.removeLearner(ctx, repl, dryRun)
case AllocatorConsiderRebalance:
return rq.considerRebalance(ctx, repl, voterReplicas, nonVoterReplicas, canTransferLeaseFrom, dryRun)
case AllocatorFinalizeAtomicReplicationChange:
_, err := maybeLeaveAtomicChangeReplicasAndRemoveLearners(ctx, repl.store, repl.Desc())
// Requeue because either we failed to transition out of a joint state
// (bad) or we did and there might be more to do for that range.
return true, err
default:
return false, errors.Errorf("unknown allocator action %v", action)
}
}
func getRemoveIdx(
repls []roachpb.ReplicaDescriptor, deadOrDecommissioningRepl roachpb.ReplicaDescriptor,
) (removeIdx int) {
for i, rDesc := range repls {
if rDesc.StoreID == deadOrDecommissioningRepl.StoreID {
removeIdx = i
break
}
}
return removeIdx
}
// addOrReplaceVoters adds or replaces a voting replica. If removeIdx is -1, an
// addition is carried out. Otherwise, removeIdx must be a valid index into
// existingVoters and specifies which voter to replace with a new one.
//
// The method preferably issues an atomic replica swap, but may not be able to
// do this in all cases, such as when the range consists of a single replica. As
// a fall back, only the addition is carried out; the removal is then a
// follow-up step for the next scanner cycle.
func (rq *replicateQueue) addOrReplaceVoters(
ctx context.Context,
repl *Replica,
liveVoterReplicas, liveNonVoterReplicas []roachpb.ReplicaDescriptor,
removeIdx int,
dryRun bool,
) (requeue bool, _ error) {
desc, zone := repl.DescAndZone()
existingVoters := desc.Replicas().VoterDescriptors()
if len(existingVoters) == 1 {
// If only one replica remains, that replica is the leaseholder and
// we won't be able to swap it out. Ignore the removal and simply add
// a replica.
removeIdx = -1
}
remainingLiveVoters := liveVoterReplicas
remainingLiveNonVoters := liveNonVoterReplicas
if removeIdx >= 0 {
replToRemove := existingVoters[removeIdx]
for i, r := range liveVoterReplicas {
if r.ReplicaID == replToRemove.ReplicaID {
remainingLiveVoters = append(liveVoterReplicas[:i:i], liveVoterReplicas[i+1:]...)
break
}
}
// See about transferring the lease away if we're about to remove the
// leaseholder.
done, err := rq.maybeTransferLeaseAway(
ctx, repl, existingVoters[removeIdx].StoreID, dryRun, nil /* canTransferLease */)
if err != nil {
return false, err
}
if done {
// Lease was transferred away. Next leaseholder is going to take over.
return false, nil
}
}
// The allocator should not try to re-add this replica since there is a reason
// we're removing it (i.e. dead or decommissioning). If we left the replica in
// the slice, the allocator would not be guaranteed to pick a replica that
// fills the gap removeRepl leaves once it's gone.
newStore, details, err := rq.allocator.AllocateVoter(ctx, zone, remainingLiveVoters, remainingLiveNonVoters)
if err != nil {
return false, err
}
if removeIdx >= 0 && newStore.StoreID == existingVoters[removeIdx].StoreID {
return false, errors.AssertionFailedf("allocator suggested to replace replica on s%d with itself", newStore.StoreID)
}
newVoter := roachpb.ReplicationTarget{
NodeID: newStore.Node.NodeID,
StoreID: newStore.StoreID,
}
clusterNodes := rq.allocator.storePool.ClusterNodeCount()
neededVoters := GetNeededVoters(zone.GetNumVoters(), clusterNodes)
// Only up-replicate if there are suitable allocation targets such that,
// either the replication goal is met, or it is possible to get to the next
// odd number of replicas. A consensus group of size 2n has worse failure
// tolerance properties than a group of size 2n - 1 because it has a larger
// quorum. For example, up-replicating from 1 to 2 replicas only makes sense
// if it is possible to be able to go to 3 replicas.
//
// NB: If willHave > neededVoters, then always allow up-replicating as that
// will be the case when up-replicating a range with a decommissioning
// replica.
//
// We skip this check if we're swapping a replica, since that does not
// change the quorum size.
if willHave := len(existingVoters) + 1; removeIdx < 0 && willHave < neededVoters && willHave%2 == 0 {
// This means we are going to up-replicate to an even replica state.
// Check if it is possible to go to an odd replica state beyond it.
oldPlusNewReplicas := append([]roachpb.ReplicaDescriptor(nil), existingVoters...)
oldPlusNewReplicas = append(oldPlusNewReplicas, roachpb.ReplicaDescriptor{
NodeID: newStore.Node.NodeID,
StoreID: newStore.StoreID,
})
_, _, err := rq.allocator.AllocateVoter(ctx, zone, oldPlusNewReplicas, remainingLiveNonVoters)
if err != nil {
// It does not seem possible to go to the next odd replica state. Note
// that AllocateVoter returns an allocatorError (a purgatoryError)
// when purgatory is requested.
return false, errors.Wrap(err, "avoid up-replicating to fragile quorum")
}
}
rq.metrics.AddReplicaCount.Inc(1)
// Figure out whether we should be promoting an existing non-voting replica to
// a voting replica or if we ought to be adding a voter afresh.
var ops []roachpb.ReplicationChange
replDesc, found := desc.GetReplicaDescriptor(newVoter.StoreID)
if found {
if replDesc.GetType() != roachpb.NON_VOTER {
return false, errors.AssertionFailedf("allocation target %s for a voter"+
" already has an unexpected replica: %s", newVoter, replDesc)
}
// If the allocation target has a non-voter already, we will promote it to a
// voter.
rq.metrics.NonVoterPromotionsCount.Inc(1)
ops = roachpb.ReplicationChangesForPromotion(newVoter)
} else {
ops = roachpb.MakeReplicationChanges(roachpb.ADD_VOTER, newVoter)
}
if removeIdx < 0 {
log.VEventf(ctx, 1, "adding voter %+v: %s",
newVoter, rangeRaftProgress(repl.RaftStatus(), existingVoters))
} else {
rq.metrics.RemoveReplicaCount.Inc(1)
removeVoter := existingVoters[removeIdx]
log.VEventf(ctx, 1, "replacing voter %s with %+v: %s",
removeVoter, newVoter, rangeRaftProgress(repl.RaftStatus(), existingVoters))
// NB: We may have performed a promotion of a non-voter above, but we will
// not perform a demotion here and instead just remove the existing replica
// entirely. This is because we know that the `removeVoter` is either dead
// or decommissioning (see `Allocator.computeAction`). This means that after
// this allocation is executed, we could be one non-voter short. This will
// be handled by the replicateQueue's next attempt at this range.
ops = append(ops,
roachpb.MakeReplicationChanges(roachpb.REMOVE_VOTER, roachpb.ReplicationTarget{
StoreID: removeVoter.StoreID,
NodeID: removeVoter.NodeID,
})...)
}
if err := rq.changeReplicas(
ctx,
repl,
ops,
desc,
SnapshotRequest_RECOVERY,
kvserverpb.ReasonRangeUnderReplicated,
details,
dryRun,
); err != nil {
return false, err
}
// Always requeue to see if more work needs to be done.
return true, nil
}
// addOrReplaceNonVoters adds a non-voting replica to `repl`s range.
func (rq *replicateQueue) addOrReplaceNonVoters(
ctx context.Context,
repl *Replica,
liveVoterReplicas, liveNonVoterReplicas []roachpb.ReplicaDescriptor,
removeIdx int,
dryRun bool,
) (requeue bool, _ error) {
// Non-voter creation is disabled before 21.1.
if v, st := clusterversion.NonVotingReplicas, repl.ClusterSettings(); !st.Version.IsActive(ctx, v) {
return false, errors.AssertionFailedf("non-voting replicas cannot be created pre-21.1")
}
desc, zone := repl.DescAndZone()
existingNonVoters := desc.Replicas().NonVoterDescriptors()
newStore, details, err := rq.allocator.AllocateNonVoter(ctx, zone, liveVoterReplicas, liveNonVoterReplicas)
if err != nil {
return false, err
}
rq.metrics.AddReplicaCount.Inc(1)
newNonVoter := roachpb.ReplicationTarget{
NodeID: newStore.Node.NodeID,
StoreID: newStore.StoreID,
}
ops := roachpb.MakeReplicationChanges(roachpb.ADD_NON_VOTER, newNonVoter)
if removeIdx < 0 {
log.VEventf(ctx, 1, "adding non-voter %+v: %s",
newNonVoter, rangeRaftProgress(repl.RaftStatus(), existingNonVoters))
} else {
rq.metrics.RemoveReplicaCount.Inc(1)
removeNonVoter := existingNonVoters[removeIdx]
log.VEventf(ctx, 1, "replacing non-voter %s with %+v: %s",
removeNonVoter, newNonVoter, rangeRaftProgress(repl.RaftStatus(), existingNonVoters))
ops = append(ops,
roachpb.MakeReplicationChanges(roachpb.REMOVE_NON_VOTER, roachpb.ReplicationTarget{
StoreID: removeNonVoter.StoreID,
NodeID: removeNonVoter.NodeID,
})...)
}
if err := rq.changeReplicas(
ctx,
repl,
ops,
desc,
SnapshotRequest_RECOVERY,
kvserverpb.ReasonRangeUnderReplicated,
details,
dryRun,
); err != nil {
return false, err
}
// Always requeue to see if more work needs to be done.
return true, nil
}
// findRemoveVoter takes a list of voting replicas and picks one to remove,
// making sure to not remove a newly added voter or to violate the zone configs
// in the process.
//
// TODO(aayush): The structure around replica removal is not great. The entire
// logic of this method should probably live inside Allocator.RemoveVoter. Doing
// so also makes the flow of adding new replicas and removing replicas more
// symmetric.
func (rq *replicateQueue) findRemoveVoter(
ctx context.Context,
repl interface {
DescAndZone() (*roachpb.RangeDescriptor, *zonepb.ZoneConfig)
LastReplicaAdded() (roachpb.ReplicaID, time.Time)
RaftStatus() *raft.Status
},
existingVoters, existingNonVoters []roachpb.ReplicaDescriptor,
) (roachpb.ReplicaDescriptor, string, error) {
_, zone := repl.DescAndZone()
// This retry loop involves quick operations on local state, so a
// small MaxBackoff is good (but those local variables change on
// network time scales as raft receives responses).
//
// TODO(bdarnell): There's another retry loop at process(). It
// would be nice to combine these, but I'm keeping them separate
// for now so we can tune the options separately.
retryOpts := retry.Options{
InitialBackoff: time.Millisecond,
MaxBackoff: 200 * time.Millisecond,
Multiplier: 2,
}
var candidates []roachpb.ReplicaDescriptor
deadline := timeutil.Now().Add(2 * base.NetworkTimeout)
for r := retry.StartWithCtx(ctx, retryOpts); r.Next() && timeutil.Now().Before(deadline); {
lastReplAdded, lastAddedTime := repl.LastReplicaAdded()
if timeutil.Since(lastAddedTime) > newReplicaGracePeriod {
lastReplAdded = 0
}
raftStatus := repl.RaftStatus()
if raftStatus == nil || raftStatus.RaftState != raft.StateLeader {
// If we've lost raft leadership, we're unlikely to regain it so give up immediately.
return roachpb.ReplicaDescriptor{}, "", &benignError{errors.Errorf("not raft leader while range needs removal")}
}
candidates = filterUnremovableReplicas(ctx, raftStatus, existingVoters, lastReplAdded)
log.VEventf(ctx, 3, "filtered unremovable replicas from %v to get %v as candidates for removal: %s",
existingVoters, candidates, rangeRaftProgress(raftStatus, existingVoters))
if len(candidates) > 0 {
break
}
if len(raftStatus.Progress) <= 2 {
// HACK(bdarnell): Downreplicating to a single node from
// multiple nodes is not really supported. There are edge
// cases in which the two peers stop communicating with each
// other too soon and we don't reach a satisfactory
// resolution. However, some tests (notably
// TestRepartitioning) get into this state, and if the
// replication queue spends its entire timeout waiting for the
// downreplication to finish the test will time out. As a
// hack, just fail-fast when we're trying to go down to a
// single replica.
break
}
// After upreplication, the candidates for removal could still
// be catching up. The allocator determined that the range was
// over-replicated, and it's important to clear that state as
// quickly as we can (because over-replicated ranges may be
// under-diversified). If we return an error here, this range
// probably won't be processed again until the next scanner
// cycle, which is too long, so we retry here.
}
if len(candidates) == 0 {
// If we timed out and still don't have any valid candidates, give up.
return roachpb.ReplicaDescriptor{}, "", &benignError{errors.Errorf("no removable replicas from range that needs a removal: %s",
rangeRaftProgress(repl.RaftStatus(), existingVoters))}
}
return rq.allocator.RemoveVoter(ctx, zone, candidates, existingVoters, existingNonVoters)
}
// maybeTransferLeaseAway is called whenever a replica on a given store is
// slated for removal. If the store corresponds to the store of the caller
// (which is very likely to be the leaseholder), then this removal would fail.
// Instead, this method will attempt to transfer the lease away, and returns
// true to indicate to the caller that it should not pursue the current
// replication change further because it is no longer the leaseholder. When the
// returned bool is false, it should continue. On error, the caller should also
// stop. If canTransferLease is non-nil, it is consulted and an error is
// returned if it returns false.
func (rq *replicateQueue) maybeTransferLeaseAway(
ctx context.Context,
repl *Replica,
removeStoreID roachpb.StoreID,
dryRun bool,
canTransferLeaseFrom func(ctx context.Context, repl *Replica) bool,
) (done bool, _ error) {
if removeStoreID != repl.store.StoreID() {
return false, nil
}
if canTransferLeaseFrom != nil && !canTransferLeaseFrom(ctx, repl) {
return false, errors.Errorf("cannot transfer lease")
}
desc, zone := repl.DescAndZone()
// The local replica was selected as the removal target, but that replica
// is the leaseholder, so transfer the lease instead. We don't check that
// the current store has too many leases in this case under the
// assumption that replica balance is a greater concern. Also note that
// AllocatorRemoveVoter action takes preference over AllocatorConsiderRebalance
// (rebalancing) which is where lease transfer would otherwise occur. We
// need to be able to transfer leases in AllocatorRemoveVoter in order to get
// out of situations where this store is overfull and yet holds all the
// leases. The fullness checks need to be ignored for cases where
// a replica needs to be removed for constraint violations.
transferred, err := rq.shedLease(
ctx,
repl,
desc,
zone,
transferLeaseOptions{
dryRun: dryRun,
},
)
return transferred == transferOK, err
}
func (rq *replicateQueue) removeVoter(
ctx context.Context,
repl *Replica,
existingVoters, existingNonVoters []roachpb.ReplicaDescriptor,
dryRun bool,
) (requeue bool, _ error) {
removeVoter, details, err := rq.findRemoveVoter(ctx, repl, existingVoters, existingNonVoters)
if err != nil {
return false, err
}
done, err := rq.maybeTransferLeaseAway(
ctx, repl, removeVoter.StoreID, dryRun, nil /* canTransferLease */)
if err != nil {
return false, err
}
if done {
// Lease is now elsewhere, so we're not in charge any more.
return false, nil
}
// Remove a replica.
rq.metrics.RemoveReplicaCount.Inc(1)
log.VEventf(ctx, 1, "removing voting replica %+v due to over-replication: %s",
removeVoter, rangeRaftProgress(repl.RaftStatus(), existingVoters))
target := roachpb.ReplicationTarget{
NodeID: removeVoter.NodeID,
StoreID: removeVoter.StoreID,
}
desc, _ := repl.DescAndZone()
// TODO(aayush): Directly removing the voter here is a bit of a missed
// opportunity since we could potentially be 1 non-voter short and the
// `target` could be a valid store for a non-voter. In such a scenario, we
// could save a bunch of work by just performing an atomic demotion of a
// voter.
if err := rq.changeReplicas(
ctx,
repl,
roachpb.MakeReplicationChanges(roachpb.REMOVE_VOTER, target),
desc,
SnapshotRequest_UNKNOWN, // unused
kvserverpb.ReasonRangeOverReplicated,
details,
dryRun,
); err != nil {
return false, err
}
return true, nil
}
func (rq *replicateQueue) removeNonVoter(
ctx context.Context,
repl *Replica,
existingVoters, existingNonVoters []roachpb.ReplicaDescriptor,
dryRun bool,
) (requeue bool, _ error) {
rq.metrics.RemoveReplicaCount.Inc(1)
desc, zone := repl.DescAndZone()
removeNonVoter, details, err := rq.allocator.RemoveNonVoter(
ctx,
zone,
existingNonVoters,
existingVoters, existingNonVoters,
)
if err != nil {
return false, err
}
log.VEventf(ctx, 1, "removing non-voting replica %+v due to over-replication: %s",
removeNonVoter, rangeRaftProgress(repl.RaftStatus(), existingVoters))
target := roachpb.ReplicationTarget{
NodeID: removeNonVoter.NodeID,
StoreID: removeNonVoter.StoreID,
}
if err := rq.changeReplicas(
ctx,
repl,
roachpb.MakeReplicationChanges(roachpb.REMOVE_NON_VOTER, target),
desc,
SnapshotRequest_UNKNOWN,
kvserverpb.ReasonRangeOverReplicated,
details,
dryRun,
); err != nil {
return false, err
}
return true, nil
}
func (rq *replicateQueue) removeDecommissioning(
ctx context.Context, repl *Replica, targetType targetReplicaType, dryRun bool,
) (requeue bool, _ error) {
desc, _ := repl.DescAndZone()
var decommissioningReplicas []roachpb.ReplicaDescriptor
switch targetType {
case voterTarget:
decommissioningReplicas = rq.allocator.storePool.decommissioningReplicas(
desc.Replicas().VoterDescriptors(),
)
case nonVoterTarget:
decommissioningReplicas = rq.allocator.storePool.decommissioningReplicas(
desc.Replicas().NonVoterDescriptors(),
)
default:
panic(fmt.Sprintf("unknown targetReplicaType: %s", targetType))
}
if len(decommissioningReplicas) == 0 {
log.VEventf(ctx, 1, "range of %[1]ss %[2]s was identified as having decommissioning %[1]ss, "+
"but no decommissioning %[1]ss were found", targetType, repl)
return true, nil
}
decommissioningReplica := decommissioningReplicas[0]
done, err := rq.maybeTransferLeaseAway(
ctx, repl, decommissioningReplica.StoreID, dryRun, nil /* canTransferLease */)
if err != nil {
return false, err
}
if done {
// Not leaseholder any more.
return false, nil
}
// Remove the decommissioning replica.
rq.metrics.RemoveReplicaCount.Inc(1)
log.VEventf(ctx, 1, "removing decommissioning %s %+v from store", targetType, decommissioningReplica)
target := roachpb.ReplicationTarget{
NodeID: decommissioningReplica.NodeID,
StoreID: decommissioningReplica.StoreID,
}
if err := rq.changeReplicas(
ctx,
repl,
roachpb.MakeReplicationChanges(targetType.RemoveChangeType(), target),
desc,
SnapshotRequest_UNKNOWN, // unused
kvserverpb.ReasonStoreDecommissioning, "", dryRun,
); err != nil {
return false, err
}
// We removed a replica, so check if there's more to do.
return true, nil
}
func (rq *replicateQueue) removeDead(
ctx context.Context,
repl *Replica,
deadReplicas []roachpb.ReplicaDescriptor,
targetType targetReplicaType,
dryRun bool,
) (requeue bool, _ error) {
desc := repl.Desc()
if len(deadReplicas) == 0 {
log.VEventf(
ctx,
1,
"range of %[1]s %[2]s was identified as having dead %[1]ss, but no dead %[1]ss were found",