-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
store_pool.go
1340 lines (1209 loc) · 50.4 KB
/
store_pool.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 2022 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 storepool
import (
"bytes"
"context"
"fmt"
"sort"
"time"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/allocator"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/allocator/load"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness"
"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/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/shuffle"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/errors"
)
const (
// TestTimeUntilStoreDead is the test value for TimeUntilStoreDead to
// quickly mark stores as dead.
TestTimeUntilStoreDead = 5 * time.Millisecond
// TestTimeUntilStoreDeadOff is the test value for TimeUntilStoreDead that
// prevents the store pool from marking stores as dead.
TestTimeUntilStoreDeadOff = 24 * time.Hour
)
// FailedReservationsTimeout specifies a duration during which the local
// replicate queue will not consider stores which have failed a reservation a
// viable target.
var FailedReservationsTimeout = settings.RegisterDurationSetting(
settings.TenantWritable,
"server.failed_reservation_timeout",
"the amount of time to consider the store throttled for up-replication after a failed reservation call",
5*time.Second,
settings.NonNegativeDuration,
)
const timeAfterStoreSuspectSettingName = "server.time_after_store_suspect"
// TimeAfterStoreSuspect measures how long we consider a store suspect since
// it's last failure.
var TimeAfterStoreSuspect = settings.RegisterDurationSetting(
settings.TenantWritable,
timeAfterStoreSuspectSettingName,
"the amount of time we consider a store suspect for after it fails a node liveness heartbeat."+
" A suspect node would not receive any new replicas or lease transfers, but will keep the replicas it has.",
30*time.Second,
settings.NonNegativeDuration,
func(v time.Duration) error {
// We enforce a maximum value of 5 minutes for this settings, as setting this
// to high may result in a prolonged period of unavailability as a recovered
// store will not be able to acquire leases or replicas for a long time.
const maxTimeAfterStoreSuspect = 5 * time.Minute
if v > maxTimeAfterStoreSuspect {
return errors.Errorf("cannot set %s to more than %v: %v",
timeAfterStoreSuspectSettingName, maxTimeAfterStoreSuspect, v)
}
return nil
},
)
const timeUntilStoreDeadSettingName = "server.time_until_store_dead"
// TimeUntilStoreDead wraps "server.time_until_store_dead".
var TimeUntilStoreDead = func() *settings.DurationSetting {
s := settings.RegisterDurationSetting(
settings.TenantWritable,
timeUntilStoreDeadSettingName,
"the time after which if there is no new gossiped information about a store, it is considered dead",
5*time.Minute,
func(v time.Duration) error {
// Setting this to less than the interval for gossiping stores is a big
// no-no, since this value is compared to the age of the most recent gossip
// from each store to determine whether that store is live. Put a buffer of
// 15 seconds on top to allow time for gossip to propagate.
const minTimeUntilStoreDead = gossip.StoresInterval + 15*time.Second
if v < minTimeUntilStoreDead {
return errors.Errorf("cannot set %s to less than %v: %v",
timeUntilStoreDeadSettingName, minTimeUntilStoreDead, v)
}
return nil
},
)
s.SetVisibility(settings.Public)
return s
}()
// The NodeCountFunc returns a count of the total number of nodes the user
// intends for their to be in the cluster. The count includes dead nodes, but
// not decommissioned nodes.
type NodeCountFunc func() int
// A NodeLivenessFunc accepts a node ID and current time and returns whether or
// not the node is live. A node is considered dead if its liveness record has
// expired by more than TimeUntilStoreDead.
type NodeLivenessFunc func(
nid roachpb.NodeID, now time.Time, timeUntilStoreDead time.Duration,
) livenesspb.NodeLivenessStatus
// MakeStorePoolNodeLivenessFunc returns a function which determines
// the status of a node based on information provided by the specified
// NodeLiveness.
func MakeStorePoolNodeLivenessFunc(nodeLiveness *liveness.NodeLiveness) NodeLivenessFunc {
return func(
nodeID roachpb.NodeID, now time.Time, timeUntilStoreDead time.Duration,
) livenesspb.NodeLivenessStatus {
liveness, ok := nodeLiveness.GetLiveness(nodeID)
if !ok {
return livenesspb.NodeLivenessStatus_UNKNOWN
}
return LivenessStatus(liveness.Liveness, now, timeUntilStoreDead)
}
}
// LivenessStatus returns a NodeLivenessStatus enumeration value for the
// provided Liveness based on the provided timestamp and threshold.
//
// See the note on IsLive() for considerations on what should be passed in as
// `now`.
//
// The timeline of the states that a liveness goes through as time passes after
// the respective liveness record is written is the following:
//
// -----|-------LIVE---|------UNAVAILABLE---|------DEAD------------> time
// tWrite tExp tExp+threshold
//
// Explanation:
//
// - Let's say a node write its liveness record at tWrite. It sets the
// Expiration field of the record as tExp=tWrite+livenessThreshold.
// The node is considered LIVE (or DECOMMISSIONING or DRAINING).
// - At tExp, the IsLive() method starts returning false. The state becomes
// UNAVAILABLE (or stays DECOMMISSIONING or DRAINING).
// - Once threshold passes, the node is considered DEAD (or DECOMMISSIONED).
//
// NB: There's a bit of discrepancy between what "Decommissioned" represents, as
// seen by NodeStatusLiveness, and what "Decommissioned" represents as
// understood by MembershipStatus. Currently it's possible for a live node, that
// was marked as fully decommissioned, to have a NodeLivenessStatus of
// "Decommissioning". This was kept this way for backwards compatibility, and
// ideally we should remove usage of NodeLivenessStatus altogether. See #50707
// for more details.
func LivenessStatus(
l livenesspb.Liveness, now time.Time, deadThreshold time.Duration,
) livenesspb.NodeLivenessStatus {
if l.IsDead(now, deadThreshold) {
if !l.Membership.Active() {
return livenesspb.NodeLivenessStatus_DECOMMISSIONED
}
return livenesspb.NodeLivenessStatus_DEAD
}
if l.IsLive(now) {
if !l.Membership.Active() {
return livenesspb.NodeLivenessStatus_DECOMMISSIONING
}
if l.Draining {
return livenesspb.NodeLivenessStatus_DRAINING
}
return livenesspb.NodeLivenessStatus_LIVE
}
return livenesspb.NodeLivenessStatus_UNAVAILABLE
}
// StoreDetail groups together store-relevant details.
type StoreDetail struct {
Desc *roachpb.StoreDescriptor
// ThrottledUntil is when a throttled store can be considered available again
// due to a failed or declined snapshot.
ThrottledUntil time.Time
// throttledBecause is set to the most recent reason for which a store was
// marked as throttled.
throttledBecause string
// LastUpdatedTime is set when a store is first consulted and every time
// gossip arrives for a store.
LastUpdatedTime time.Time
// LastUnavailable is set when it's detected that a store was unavailable,
// i.e. failed liveness.
LastUnavailable time.Time
// LastAvailable is set when it's detected that a store was available,
// i.e. we got a liveness heartbeat.
LastAvailable time.Time
}
// isThrottled returns whether the store is currently throttled.
func (sd StoreDetail) isThrottled(now time.Time) bool {
return sd.ThrottledUntil.After(now)
}
// isSuspect returns whether the store is currently suspect. We measure that by
// looking at the time it was last unavailable making sure we have not seen any
// failures for a period of time defined by StoreSuspectDuration.
func (sd StoreDetail) isSuspect(now time.Time, suspectDuration time.Duration) bool {
return sd.LastUnavailable.Add(suspectDuration).After(now)
}
// storeStatus is the current status of a store.
type storeStatus int
// These are the possible values for a storeStatus.
const (
_ storeStatus = iota
// The store's node is not live or no gossip has been received from
// the store for more than the timeUntilStoreDead threshold.
storeStatusDead
// The store isn't available because it hasn't gossiped yet. This
// status lasts until either gossip is received from the store or
// the timeUntilStoreDead threshold has passed, at which point its
// status will change to dead.
storeStatusUnknown
// The store is alive but it is throttled.
storeStatusThrottled
// The store is alive and available.
storeStatusAvailable
// The store is decommissioning.
storeStatusDecommissioning
// The store failed it's liveness heartbeat recently and is considered
// suspect. Consequently, stores always move from `storeStatusUnknown`
// (indicating a node that has a non-live node liveness record) to
// `storeStatusSuspect`.
storeStatusSuspect
// The store is alive but is currently marked as draining, so it is not a
// candidate for lease transfers or replica rebalancing.
storeStatusDraining
)
func (sd *StoreDetail) status(
now time.Time, threshold time.Duration, nl NodeLivenessFunc, suspectDuration time.Duration,
) storeStatus {
// During normal operation, we expect the state transitions for stores to look like the following:
//
// Successful heartbeats
// throughout the suspect
// +-----------------------+ duration
// | storeStatusAvailable |<-+------------------------------------+
// +-----------------------+ | |
// | |
// | +--------------------+
// | | storeStatusSuspect |
// +---------------------------+ +--------------------+
// | Failed liveness ^
// | heartbeat |
// | |
// | |
// | +----------------------+ |
// +->| storeStatusUnknown |--------------------------------------+
// +----------------------+ Successful liveness
// heartbeat
//
// The store is considered dead if it hasn't been updated via gossip
// within the liveness threshold. Note that LastUpdatedTime is set
// when the store detail is created and will have a non-zero value
// even before the first gossip arrives for a store.
deadAsOf := sd.LastUpdatedTime.Add(threshold)
if now.After(deadAsOf) {
// Wipe out the lastAvailable timestamp, so that once a node comes back
// from the dead we dont consider it suspect.
sd.LastAvailable = time.Time{}
return storeStatusDead
}
// If there's no descriptor (meaning no gossip ever arrived for this
// store), return unavailable.
if sd.Desc == nil {
return storeStatusUnknown
}
// Even if the store has been updated via gossip, we still rely on
// the node liveness to determine whether it is considered live.
//
// Store statuses checked in the following order:
// dead -> decommissioning -> unknown -> draining -> suspect -> available.
switch nl(sd.Desc.Node.NodeID, now, threshold) {
case livenesspb.NodeLivenessStatus_DEAD, livenesspb.NodeLivenessStatus_DECOMMISSIONED:
return storeStatusDead
case livenesspb.NodeLivenessStatus_DECOMMISSIONING:
return storeStatusDecommissioning
case livenesspb.NodeLivenessStatus_UNAVAILABLE:
// We don't want to suspect a node on startup or when it's first added to a
// cluster, because we dont know its liveness yet.
if !sd.LastAvailable.IsZero() {
sd.LastUnavailable = now
}
return storeStatusUnknown
case livenesspb.NodeLivenessStatus_UNKNOWN:
return storeStatusUnknown
case livenesspb.NodeLivenessStatus_DRAINING:
// Wipe out the lastAvailable timestamp, so if this node comes back after a
// graceful restart it will not be considered as suspect. This is best effort
// and we may not see a store in this state. To help with that we perform
// a similar clear of lastAvailable on a DEAD store.
sd.LastAvailable = time.Time{}
return storeStatusDraining
}
if sd.isThrottled(now) {
return storeStatusThrottled
}
if sd.isSuspect(now, suspectDuration) {
return storeStatusSuspect
}
sd.LastAvailable = now
return storeStatusAvailable
}
// localityWithString maintains a string representation of each locality along
// with its protocol buffer implementation. This is for the sake of optimizing
// memory usage by allocating a single copy of each that can be returned to
// callers of getNodeLocalityString rather than each caller (which is currently
// each replica in the local store) making its own copy.
type localityWithString struct {
locality roachpb.Locality
str string
}
// AllocatorStorePool provides an interface for use by the allocator to a list
// of all known stores in the cluster and information on their health.
type AllocatorStorePool interface {
fmt.Stringer
// ClusterNodeCount returns the number of nodes that are possible allocation
// targets.
// See comment on StorePool.ClusterNodeCount().
ClusterNodeCount() int
// IsDeterministic returns true iff the pool is configured to be deterministic.
IsDeterministic() bool
// IsStoreReadyForRoutineReplicaTransfer returns true iff the store's node is
// live (as indicated by its `NodeLivenessStatus`) and thus a legal candidate
// to receive a replica.
// See comment on StorePool.IsStoreReadyForRoutineReplicaTransfer(..).
IsStoreReadyForRoutineReplicaTransfer(ctx context.Context, targetStoreID roachpb.StoreID) bool
// Clock returns the store pool's clock.
// TODO(sarkesian): If possible, this should be removed.
Clock() *hlc.Clock
// DecommissioningReplicas selects the replicas on decommissioning
// node/stores from the provided list.
DecommissioningReplicas(repls []roachpb.ReplicaDescriptor) []roachpb.ReplicaDescriptor
// GetLocalitiesByNode returns the localities for the provided replicas by NodeID.
// See comment on StorePool.GetLocalitiesByNode(..).
GetLocalitiesByNode(replicas []roachpb.ReplicaDescriptor) map[roachpb.NodeID]roachpb.Locality
// GetLocalitiesByStore returns the localities for the provided replicas by StoreID.
// See comment on StorePool.GetLocalitiesByStore(..).
GetLocalitiesByStore(replicas []roachpb.ReplicaDescriptor) map[roachpb.StoreID]roachpb.Locality
// GetStores returns information on all the stores with descriptor in the pool.
// See comment on StorePool.GetStores().
GetStores() map[roachpb.StoreID]roachpb.StoreDescriptor
// GetStoreDescriptor returns the latest store descriptor for the given
// storeID.
GetStoreDescriptor(storeID roachpb.StoreID) (roachpb.StoreDescriptor, bool)
// GetStoreList returns a storeList of active stores based on a filter.
// See comment on StorePool.GetStoreList(..).
GetStoreList(filter StoreFilter) (StoreList, int, ThrottledStoreReasons)
// GetStoreListFromIDs is the same function as GetStoreList but only returns stores
// from the subset of passed in store IDs.
GetStoreListFromIDs(
storeIDs roachpb.StoreIDSlice,
filter StoreFilter,
) (StoreList, int, ThrottledStoreReasons)
// GetStoreListForTargets is the same as GetStoreList, but only returns stores
// from the subset of stores present in the passed in replication targets,
// converting to a StoreList.
GetStoreListForTargets(
candidates []roachpb.ReplicationTarget,
filter StoreFilter,
) (StoreList, int, ThrottledStoreReasons)
// LiveAndDeadReplicas divides the provided repls slice into two slices: the
// first for live replicas, and the second for dead replicas.
// See comment on StorePool.LiveAndDeadReplicas(..).
LiveAndDeadReplicas(
repls []roachpb.ReplicaDescriptor,
includeSuspectAndDrainingStores bool,
) (liveReplicas, deadReplicas []roachpb.ReplicaDescriptor)
// UpdateLocalStoreAfterRebalance is used to update the local copy of the
// target store immediately after a replica addition or removal.
UpdateLocalStoreAfterRebalance(
storeID roachpb.StoreID,
rangeUsageInfo allocator.RangeUsageInfo,
changeType roachpb.ReplicaChangeType,
)
// UpdateLocalStoresAfterLeaseTransfer is used to update the local copies of the
// involved store descriptors immediately after a lease transfer.
UpdateLocalStoresAfterLeaseTransfer(
from roachpb.StoreID,
to roachpb.StoreID,
rangeUsageInfo allocator.RangeUsageInfo,
)
// UpdateLocalStoreAfterRelocate is used to update the local copy of the
// previous and new replica stores immediately after a successful relocate
// range.
UpdateLocalStoreAfterRelocate(
voterTargets, nonVoterTargets []roachpb.ReplicationTarget,
oldVoters, oldNonVoters []roachpb.ReplicaDescriptor,
localStore roachpb.StoreID,
rangeUsageInfo allocator.RangeUsageInfo,
)
}
// StorePool maintains a list of all known stores in the cluster and
// information on their health.
type StorePool struct {
log.AmbientContext
st *cluster.Settings
clock *hlc.Clock
gossip *gossip.Gossip
nodeCountFn NodeCountFunc
NodeLivenessFn NodeLivenessFunc
startTime time.Time
deterministic bool
// We use separate mutexes for storeDetails and nodeLocalities because the
// nodeLocalities map is used in the critical code path of Replica.Send()
// and we'd rather not block that on something less important accessing
// storeDetails.
// NB: Exported for use in tests and allocator simulator.
DetailsMu struct {
syncutil.RWMutex
StoreDetails map[roachpb.StoreID]*StoreDetail
}
localitiesMu struct {
syncutil.RWMutex
nodeLocalities map[roachpb.NodeID]localityWithString
}
// OverrideIsStoreReadyForRoutineReplicaTransferFn, if set, is used in
// IsStoreReadyForRoutineReplicaTransfer. This is defined as a closure reference here instead
// of a regular method so it can be overridden in tests.
// TODO(sarkesian): Consider moving to a TestingKnobs struct.
OverrideIsStoreReadyForRoutineReplicaTransferFn func(context.Context, roachpb.StoreID) bool
}
var _ AllocatorStorePool = &StorePool{}
// NewStorePool creates a StorePool and registers the store updating callback
// with gossip.
func NewStorePool(
ambient log.AmbientContext,
st *cluster.Settings,
g *gossip.Gossip,
clock *hlc.Clock,
nodeCountFn NodeCountFunc,
nodeLivenessFn NodeLivenessFunc,
deterministic bool,
) *StorePool {
sp := &StorePool{
AmbientContext: ambient,
st: st,
clock: clock,
gossip: g,
nodeCountFn: nodeCountFn,
NodeLivenessFn: nodeLivenessFn,
startTime: clock.PhysicalTime(),
deterministic: deterministic,
}
sp.DetailsMu.StoreDetails = make(map[roachpb.StoreID]*StoreDetail)
sp.localitiesMu.nodeLocalities = make(map[roachpb.NodeID]localityWithString)
// Enable redundant callbacks for the store keys because we use these
// callbacks as a clock to determine when a store was last updated even if it
// hasn't otherwise changed.
storeRegex := gossip.MakePrefixPattern(gossip.KeyStoreDescPrefix)
g.RegisterCallback(storeRegex, sp.storeGossipUpdate, gossip.Redundant)
return sp
}
func (sp *StorePool) String() string {
return sp.statusString(sp.NodeLivenessFn)
}
func (sp *StorePool) statusString(nl NodeLivenessFunc) string {
sp.DetailsMu.RLock()
defer sp.DetailsMu.RUnlock()
ids := make(roachpb.StoreIDSlice, 0, len(sp.DetailsMu.StoreDetails))
for id := range sp.DetailsMu.StoreDetails {
ids = append(ids, id)
}
sort.Sort(ids)
var buf bytes.Buffer
now := sp.clock.Now().GoTime()
timeUntilStoreDead := TimeUntilStoreDead.Get(&sp.st.SV)
timeAfterStoreSuspect := TimeAfterStoreSuspect.Get(&sp.st.SV)
for _, id := range ids {
detail := sp.DetailsMu.StoreDetails[id]
fmt.Fprintf(&buf, "%d", id)
status := detail.status(now, timeUntilStoreDead, nl, timeAfterStoreSuspect)
if status != storeStatusAvailable {
fmt.Fprintf(&buf, " (status=%d)", status)
}
if detail.Desc != nil {
fmt.Fprintf(&buf, ": range-count=%d fraction-used=%.2f",
detail.Desc.Capacity.RangeCount, detail.Desc.Capacity.FractionUsed())
}
throttled := detail.ThrottledUntil.Sub(now)
if throttled > 0 {
fmt.Fprintf(&buf, " [throttled=%.1fs]", throttled.Seconds())
}
_, _ = buf.WriteString("\n")
}
return buf.String()
}
// storeGossipUpdate is the Gossip callback used to keep the StorePool up to date.
func (sp *StorePool) storeGossipUpdate(_ string, content roachpb.Value) {
var storeDesc roachpb.StoreDescriptor
if err := content.GetProto(&storeDesc); err != nil {
ctx := sp.AnnotateCtx(context.TODO())
log.Errorf(ctx, "%v", err)
return
}
sp.DetailsMu.Lock()
detail := sp.GetStoreDetailLocked(storeDesc.StoreID)
detail.Desc = &storeDesc
detail.LastUpdatedTime = sp.clock.PhysicalTime()
sp.DetailsMu.Unlock()
sp.localitiesMu.Lock()
sp.localitiesMu.nodeLocalities[storeDesc.Node.NodeID] =
localityWithString{storeDesc.Node.Locality, storeDesc.Node.Locality.String()}
sp.localitiesMu.Unlock()
}
// UpdateLocalStoreAfterRebalance is used to update the local copy of the
// target store immediately after a replica addition or removal.
func (sp *StorePool) UpdateLocalStoreAfterRebalance(
storeID roachpb.StoreID,
rangeUsageInfo allocator.RangeUsageInfo,
changeType roachpb.ReplicaChangeType,
) {
sp.DetailsMu.Lock()
defer sp.DetailsMu.Unlock()
detail := *sp.GetStoreDetailLocked(storeID)
if detail.Desc == nil {
// We don't have this store yet (this is normal when we're
// starting up and don't have full information from the gossip
// network). We can't update the local store at this time.
return
}
// Only apply the raft cpu delta on rebalance. This estimate assumes that
// the raft cpu usage is approximately equal across replicas for a range.
switch changeType {
case roachpb.ADD_VOTER, roachpb.ADD_NON_VOTER:
detail.Desc.Capacity.RangeCount++
detail.Desc.Capacity.LogicalBytes += rangeUsageInfo.LogicalBytes
detail.Desc.Capacity.WritesPerSecond += rangeUsageInfo.WritesPerSecond
detail.Desc.Capacity.CPUPerSecond += rangeUsageInfo.RaftCPUNanosPerSecond
case roachpb.REMOVE_VOTER, roachpb.REMOVE_NON_VOTER:
detail.Desc.Capacity.RangeCount--
if detail.Desc.Capacity.LogicalBytes <= rangeUsageInfo.LogicalBytes {
detail.Desc.Capacity.LogicalBytes = 0
} else {
detail.Desc.Capacity.LogicalBytes -= rangeUsageInfo.LogicalBytes
}
if detail.Desc.Capacity.WritesPerSecond <= rangeUsageInfo.WritesPerSecond {
detail.Desc.Capacity.WritesPerSecond = 0
} else {
detail.Desc.Capacity.WritesPerSecond -= rangeUsageInfo.WritesPerSecond
}
if detail.Desc.Capacity.CPUPerSecond <= rangeUsageInfo.RaftCPUNanosPerSecond {
detail.Desc.Capacity.CPUPerSecond = 0
} else {
detail.Desc.Capacity.CPUPerSecond -= rangeUsageInfo.RaftCPUNanosPerSecond
}
default:
return
}
sp.DetailsMu.StoreDetails[storeID] = &detail
}
// UpdateLocalStoreAfterRelocate is used to update the local copy of the
// previous and new replica stores immediately after a successful relocate
// range.
//
// TODO(kvoli): We do not update the logical bytes or writes per second here.
// Once #91593 is in, update these methods to instead take a general purpose
// representation. This is less relevant at the moment.
func (sp *StorePool) UpdateLocalStoreAfterRelocate(
voterTargets, nonVoterTargets []roachpb.ReplicationTarget,
oldVoters, oldNonVoters []roachpb.ReplicaDescriptor,
localStore roachpb.StoreID,
rangeUsageInfo allocator.RangeUsageInfo,
) {
if len(voterTargets) < 1 {
return
}
leaseTarget := voterTargets[0]
sp.UpdateLocalStoresAfterLeaseTransfer(localStore, leaseTarget.StoreID, rangeUsageInfo)
sp.DetailsMu.Lock()
defer sp.DetailsMu.Unlock()
// Only apply the raft cpu delta on rebalance. This estimate assumes that
// the raft cpu usage is approximately equal across replicas for a range.
// TODO(kvoli): Separate into LH vs Replica, similar to the comment on
// range_usage_info.
updateTargets := func(targets []roachpb.ReplicationTarget) {
for _, target := range targets {
if toDetail := sp.GetStoreDetailLocked(target.StoreID); toDetail != nil {
toDetail.Desc.Capacity.RangeCount++
toDetail.Desc.Capacity.CPUPerSecond += rangeUsageInfo.RaftCPUNanosPerSecond
}
}
}
updatePrevious := func(previous []roachpb.ReplicaDescriptor) {
for _, old := range previous {
if toDetail := sp.GetStoreDetailLocked(old.StoreID); toDetail != nil {
toDetail.Desc.Capacity.RangeCount--
toDetail.Desc.Capacity.CPUPerSecond -= rangeUsageInfo.RaftCPUNanosPerSecond
}
}
}
updateTargets(voterTargets)
updateTargets(nonVoterTargets)
updatePrevious(oldVoters)
updatePrevious(oldNonVoters)
}
// UpdateLocalStoresAfterLeaseTransfer is used to update the local copies of the
// involved store descriptors immediately after a lease transfer.
func (sp *StorePool) UpdateLocalStoresAfterLeaseTransfer(
from roachpb.StoreID, to roachpb.StoreID, rangeUsageInfo allocator.RangeUsageInfo,
) {
sp.DetailsMu.Lock()
defer sp.DetailsMu.Unlock()
fromDetail := *sp.GetStoreDetailLocked(from)
if fromDetail.Desc != nil {
fromDetail.Desc.Capacity.LeaseCount--
if fromDetail.Desc.Capacity.QueriesPerSecond < rangeUsageInfo.QueriesPerSecond {
fromDetail.Desc.Capacity.QueriesPerSecond = 0
} else {
fromDetail.Desc.Capacity.QueriesPerSecond -= rangeUsageInfo.QueriesPerSecond
}
// Only apply the request cpu (leaseholder + follower-reads) delta on
// transfers. Note this does not correctly account for follower reads
// remaining on the prior leaseholder after lease transfer. Instead,
// only a cpu delta specific to the lease should be applied.
if fromDetail.Desc.Capacity.CPUPerSecond <= rangeUsageInfo.RequestCPUNanosPerSecond {
fromDetail.Desc.Capacity.CPUPerSecond = 0
} else {
fromDetail.Desc.Capacity.CPUPerSecond -= rangeUsageInfo.RequestCPUNanosPerSecond
}
sp.DetailsMu.StoreDetails[from] = &fromDetail
}
toDetail := *sp.GetStoreDetailLocked(to)
if toDetail.Desc != nil {
toDetail.Desc.Capacity.LeaseCount++
toDetail.Desc.Capacity.QueriesPerSecond += rangeUsageInfo.QueriesPerSecond
toDetail.Desc.Capacity.CPUPerSecond += rangeUsageInfo.RequestCPUNanosPerSecond
sp.DetailsMu.StoreDetails[to] = &toDetail
}
}
// newStoreDetail makes a new StoreDetail struct. It sets index to be -1 to
// ensure that it will be processed by a queue immediately.
func newStoreDetail() *StoreDetail {
return &StoreDetail{}
}
// GetStores returns information on all the stores with descriptor in the pool.
// Stores without descriptor (a node that didn't come up yet after a cluster
// restart) will not be part of the returned set.
func (sp *StorePool) GetStores() map[roachpb.StoreID]roachpb.StoreDescriptor {
sp.DetailsMu.RLock()
defer sp.DetailsMu.RUnlock()
stores := make(map[roachpb.StoreID]roachpb.StoreDescriptor, len(sp.DetailsMu.StoreDetails))
for _, s := range sp.DetailsMu.StoreDetails {
if s.Desc != nil {
stores[s.Desc.StoreID] = *s.Desc
}
}
return stores
}
// GetStoreDetailLocked returns the store detail for the given storeID. The
// lock must be held *in write mode* even though this looks like a read-only
// method. The store detail returned is a mutable reference.
func (sp *StorePool) GetStoreDetailLocked(storeID roachpb.StoreID) *StoreDetail {
detail, ok := sp.DetailsMu.StoreDetails[storeID]
if !ok {
// We don't have this store yet (this is normal when we're
// starting up and don't have full information from the gossip
// network). The first time this occurs, presume the store is
// alive, but start the clock so it will become dead if enough
// time passes without updates from gossip.
detail = newStoreDetail()
detail.LastUpdatedTime = sp.startTime
sp.DetailsMu.StoreDetails[storeID] = detail
}
return detail
}
// GetStoreDescriptor returns the latest store descriptor for the given
// storeID.
func (sp *StorePool) GetStoreDescriptor(storeID roachpb.StoreID) (roachpb.StoreDescriptor, bool) {
sp.DetailsMu.RLock()
defer sp.DetailsMu.RUnlock()
if detail, ok := sp.DetailsMu.StoreDetails[storeID]; ok && detail.Desc != nil {
return *detail.Desc, true
}
return roachpb.StoreDescriptor{}, false
}
// DecommissioningReplicas filters out replicas on decommissioning node/store
// from the provided repls and returns them in a slice.
func (sp *StorePool) DecommissioningReplicas(
repls []roachpb.ReplicaDescriptor,
) (decommissioningReplicas []roachpb.ReplicaDescriptor) {
return sp.decommissioningReplicasWithLiveness(repls, sp.NodeLivenessFn)
}
// decommissioningReplicasWithLiveness filters out replicas on decommissioning node/store
// from the provided repls and returns them in a slice, using the provided NodeLivenessFunc.
func (sp *StorePool) decommissioningReplicasWithLiveness(
repls []roachpb.ReplicaDescriptor, nl NodeLivenessFunc,
) (decommissioningReplicas []roachpb.ReplicaDescriptor) {
sp.DetailsMu.Lock()
defer sp.DetailsMu.Unlock()
// NB: We use clock.Now().GoTime() instead of clock.PhysicalTime() is order to
// take clock signals from remote nodes into consideration.
now := sp.clock.Now().GoTime()
timeUntilStoreDead := TimeUntilStoreDead.Get(&sp.st.SV)
timeAfterStoreSuspect := TimeAfterStoreSuspect.Get(&sp.st.SV)
for _, repl := range repls {
detail := sp.GetStoreDetailLocked(repl.StoreID)
switch detail.status(now, timeUntilStoreDead, nl, timeAfterStoreSuspect) {
case storeStatusDecommissioning:
decommissioningReplicas = append(decommissioningReplicas, repl)
}
}
return
}
// ClusterNodeCount returns the number of nodes that are possible allocation
// targets. This includes dead nodes, but not decommissioning or decommissioned
// nodes.
func (sp *StorePool) ClusterNodeCount() int {
return sp.nodeCountFn()
}
// Clock returns the store pool's clock.
func (sp *StorePool) Clock() *hlc.Clock {
return sp.clock
}
// IsDeterministic returns true iff the pool is configured to be deterministic.
func (sp *StorePool) IsDeterministic() bool {
return sp.deterministic
}
// IsDead determines if a store is dead. It will return an error if the store is
// not found in the store pool or the status is unknown. If the store is not dead,
// it returns the time to death.
func (sp *StorePool) IsDead(storeID roachpb.StoreID) (bool, time.Duration, error) {
sp.DetailsMu.Lock()
defer sp.DetailsMu.Unlock()
sd, ok := sp.DetailsMu.StoreDetails[storeID]
if !ok {
return false, 0, errors.Errorf("store %d was not found", storeID)
}
// NB: We use clock.Now().GoTime() instead of clock.PhysicalTime() is order to
// take clock signals from remote nodes into consideration.
now := sp.clock.Now().GoTime()
timeUntilStoreDead := TimeUntilStoreDead.Get(&sp.st.SV)
deadAsOf := sd.LastUpdatedTime.Add(timeUntilStoreDead)
if now.After(deadAsOf) {
return true, 0, nil
}
// If there's no descriptor (meaning no gossip ever arrived for this
// store), return unavailable.
if sd.Desc == nil {
return false, 0, errors.Errorf("store %d status unknown, cant tell if it's dead or alive", storeID)
}
return false, deadAsOf.Sub(now), nil
}
// IsUnknown returns true if the given store's status is `storeStatusUnknown`
// (i.e. it just failed a liveness heartbeat and we cannot ascertain its
// liveness or deadness at the moment) or an error if the store is not found in
// the pool.
func (sp *StorePool) IsUnknown(storeID roachpb.StoreID) (bool, error) {
status, err := sp.storeStatus(storeID, sp.NodeLivenessFn)
if err != nil {
return false, err
}
return status == storeStatusUnknown, nil
}
// IsDraining returns true if the given store's status is `storeStatusDraining`
// or an error if the store is not found in the pool.
func (sp *StorePool) IsDraining(storeID roachpb.StoreID) (bool, error) {
status, err := sp.storeStatus(storeID, sp.NodeLivenessFn)
if err != nil {
return false, err
}
return status == storeStatusDraining, nil
}
// IsLive returns true if the node is considered alive by the store pool or an error
// if the store is not found in the pool.
func (sp *StorePool) IsLive(storeID roachpb.StoreID) (bool, error) {
status, err := sp.storeStatus(storeID, sp.NodeLivenessFn)
if err != nil {
return false, err
}
return status == storeStatusAvailable, nil
}
func (sp *StorePool) storeStatus(
storeID roachpb.StoreID, nl NodeLivenessFunc,
) (storeStatus, error) {
sp.DetailsMu.Lock()
defer sp.DetailsMu.Unlock()
sd, ok := sp.DetailsMu.StoreDetails[storeID]
if !ok {
return storeStatusUnknown, errors.Errorf("store %d was not found", storeID)
}
// NB: We use clock.Now().GoTime() instead of clock.PhysicalTime() is order to
// take clock signals from remote nodes into consideration.
now := sp.clock.Now().GoTime()
timeUntilStoreDead := TimeUntilStoreDead.Get(&sp.st.SV)
timeAfterStoreSuspect := TimeAfterStoreSuspect.Get(&sp.st.SV)
return sd.status(now, timeUntilStoreDead, nl, timeAfterStoreSuspect), nil
}
// LiveAndDeadReplicas divides the provided repls slice into two slices: the
// first for live replicas, and the second for dead replicas.
//
// - Replicas for which liveness or deadness cannot be ascertained
// (storeStatusUnknown) are excluded from the returned slices.
//
// - Replicas on decommissioning node/store are considered live.
//
// - If `includeSuspectAndDrainingStores` is true, stores that are marked
// suspect (i.e. stores that have failed a liveness heartbeat in the recent
// past), and stores that are marked as draining are considered live. Otherwise,
// they are excluded from the returned slices.
func (sp *StorePool) LiveAndDeadReplicas(
repls []roachpb.ReplicaDescriptor, includeSuspectAndDrainingStores bool,
) (liveReplicas, deadReplicas []roachpb.ReplicaDescriptor) {
return sp.liveAndDeadReplicasWithLiveness(repls, sp.NodeLivenessFn, includeSuspectAndDrainingStores)
}
// liveAndDeadReplicasWithLiveness divides the provided repls slice into two slices: the
// first for live replicas, and the second for dead replicas, using the
// provided NodeLivenessFunc.
// See comment on StorePool.LiveAndDeadReplicas(..).
func (sp *StorePool) liveAndDeadReplicasWithLiveness(
repls []roachpb.ReplicaDescriptor, nl NodeLivenessFunc, includeSuspectAndDrainingStores bool,
) (liveReplicas, deadReplicas []roachpb.ReplicaDescriptor) {
sp.DetailsMu.Lock()
defer sp.DetailsMu.Unlock()
now := sp.clock.Now().GoTime()
timeUntilStoreDead := TimeUntilStoreDead.Get(&sp.st.SV)
timeAfterStoreSuspect := TimeAfterStoreSuspect.Get(&sp.st.SV)
for _, repl := range repls {
detail := sp.GetStoreDetailLocked(repl.StoreID)
// Mark replica as dead if store is dead.
status := detail.status(now, timeUntilStoreDead, nl, timeAfterStoreSuspect)
switch status {
case storeStatusDead:
deadReplicas = append(deadReplicas, repl)
case storeStatusAvailable, storeStatusThrottled, storeStatusDecommissioning:
// We count both available and throttled stores to be live for the
// purpose of computing quorum.
// We count decommissioning replicas to be alive because they are readable
// and should be used for up-replication if necessary.
liveReplicas = append(liveReplicas, repl)
case storeStatusUnknown:
// No-op.
case storeStatusSuspect, storeStatusDraining:
if includeSuspectAndDrainingStores {
liveReplicas = append(liveReplicas, repl)
}
default:
log.Fatalf(context.TODO(), "unknown store status %d", status)
}
}
return
}
// Stat provides a running sample size and running stats.
type Stat struct {
n, Mean float64
}
// Update adds the specified value to the Stat, augmenting the running stats.
func (s *Stat) update(x float64) {
s.n++
s.Mean += (x - s.Mean) / s.n
}
// StoreList holds a list of store descriptors and associated count and used
// stats for those stores.
type StoreList struct {
Stores []roachpb.StoreDescriptor
// CandidateRanges tracks range count stats for Stores that are eligible to
// be rebalance targets (their used capacity percentage must be lower than
// maxFractionUsedThreshold).
CandidateRanges Stat
// CandidateLeases tracks range lease stats for Stores that are eligible to
// be rebalance targets.
CandidateLeases Stat
// candidateLogicalBytes tracks disk usage stats for Stores that are eligible
// to be rebalance targets.
candidateLogicalBytes Stat
// CandidateCPU tracks store-cpu-per-second stats for Stores that are
// eligible to be rebalance targets.
CandidateCPU Stat
// CandidateQueriesPerSecond tracks queries-per-second stats for Stores that
// are eligible to be rebalance targets.
CandidateQueriesPerSecond Stat
// candidateWritesPerSecond tracks writes-per-second stats for Stores that are
// eligible to be rebalance targets.
candidateWritesPerSecond Stat
// candidateWritesPerSecond tracks L0 sub-level stats for Stores that are
// eligible to be rebalance targets.
CandidateL0Sublevels Stat
}
// MakeStoreList constructs a new store list based on the passed in descriptors.
// It will maintain the order of those descriptors.
func MakeStoreList(descriptors []roachpb.StoreDescriptor) StoreList {
sl := StoreList{Stores: descriptors}
for _, desc := range descriptors {
if allocator.MaxCapacityCheck(desc) {
sl.CandidateRanges.update(float64(desc.Capacity.RangeCount))
}
sl.CandidateLeases.update(float64(desc.Capacity.LeaseCount))
sl.candidateLogicalBytes.update(float64(desc.Capacity.LogicalBytes))
sl.CandidateQueriesPerSecond.update(desc.Capacity.QueriesPerSecond)
sl.candidateWritesPerSecond.update(desc.Capacity.WritesPerSecond)
sl.CandidateL0Sublevels.update(float64(desc.Capacity.L0Sublevels))
sl.CandidateCPU.update(desc.Capacity.CPUPerSecond)
}
return sl
}
func (sl StoreList) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf,
" candidate: avg-ranges=%v avg-leases=%v avg-disk-usage=%v avg-queries-per-second=%v avg-store-cpu-per-second=%v",
sl.CandidateRanges.Mean,
sl.CandidateLeases.Mean,
humanizeutil.IBytes(int64(sl.candidateLogicalBytes.Mean)),