-
Notifications
You must be signed in to change notification settings - Fork 712
/
store.go
761 lines (671 loc) · 23.2 KB
/
store.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
// Copyright 2016 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package core
import (
"math"
"strings"
"time"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/server/core/storelimit"
"go.uber.org/zap"
)
const (
// Interval to save store meta (including heartbeat ts) to etcd.
storePersistInterval = 5 * time.Minute
mb = 1 << 20 // megabyte
gb = 1 << 30 // 1GB size
initialMaxRegionCounts = 30 // exclude storage Threshold Filter when region less than 30
initialMinSpace = 1 << 33 // 2^33=8GB
slowStoreThreshold = 80
// EngineKey is the label key used to indicate engine.
EngineKey = "engine"
// EngineTiFlash is the tiflash value of the engine label.
EngineTiFlash = "tiflash"
// EngineTiKV indicates the tikv engine in metrics
EngineTiKV = "tikv"
)
// StoreInfo contains information about a store.
// NOTE: This type is exported by HTTP API. Please pay more attention when modifying it.
type StoreInfo struct {
meta *metapb.Store
*storeStats
pauseLeaderTransfer bool // not allow to be used as source or target of transfer leader
slowStoreEvicted bool // this store has been evicted as a slow store, should not transfer leader to it
leaderCount int
regionCount int
leaderSize int64
regionSize int64
pendingPeerCount int
lastPersistTime time.Time
leaderWeight float64
regionWeight float64
limiter map[storelimit.Type]*storelimit.StoreLimit
minResolvedTS uint64
}
// NewStoreInfo creates StoreInfo with meta data.
func NewStoreInfo(store *metapb.Store, opts ...StoreCreateOption) *StoreInfo {
storeInfo := &StoreInfo{
meta: store,
storeStats: newStoreStats(),
leaderWeight: 1.0,
regionWeight: 1.0,
limiter: make(map[storelimit.Type]*storelimit.StoreLimit),
minResolvedTS: 0,
}
for _, opt := range opts {
opt(storeInfo)
}
return storeInfo
}
func (s *StoreInfo) cloneMetaStore() *metapb.Store {
b, _ := s.meta.Marshal()
store := &metapb.Store{}
store.Unmarshal(b)
return store
}
// Clone creates a copy of current StoreInfo.
func (s *StoreInfo) Clone(opts ...StoreCreateOption) *StoreInfo {
store := &StoreInfo{
meta: s.cloneMetaStore(),
storeStats: s.storeStats,
pauseLeaderTransfer: s.pauseLeaderTransfer,
slowStoreEvicted: s.slowStoreEvicted,
leaderCount: s.leaderCount,
regionCount: s.regionCount,
leaderSize: s.leaderSize,
regionSize: s.regionSize,
pendingPeerCount: s.pendingPeerCount,
lastPersistTime: s.lastPersistTime,
leaderWeight: s.leaderWeight,
regionWeight: s.regionWeight,
limiter: s.limiter,
minResolvedTS: s.minResolvedTS,
}
for _, opt := range opts {
opt(store)
}
return store
}
// ShallowClone creates a copy of current StoreInfo, but not clone 'meta'.
func (s *StoreInfo) ShallowClone(opts ...StoreCreateOption) *StoreInfo {
store := &StoreInfo{
meta: s.meta,
storeStats: s.storeStats,
pauseLeaderTransfer: s.pauseLeaderTransfer,
slowStoreEvicted: s.slowStoreEvicted,
leaderCount: s.leaderCount,
regionCount: s.regionCount,
leaderSize: s.leaderSize,
regionSize: s.regionSize,
pendingPeerCount: s.pendingPeerCount,
lastPersistTime: s.lastPersistTime,
leaderWeight: s.leaderWeight,
regionWeight: s.regionWeight,
limiter: s.limiter,
minResolvedTS: s.minResolvedTS,
}
for _, opt := range opts {
opt(store)
}
return store
}
// AllowLeaderTransfer returns if the store is allowed to be selected
// as source or target of transfer leader.
func (s *StoreInfo) AllowLeaderTransfer() bool {
return !s.pauseLeaderTransfer
}
// EvictedAsSlowStore returns if the store should be evicted as a slow store.
func (s *StoreInfo) EvictedAsSlowStore() bool {
return s.slowStoreEvicted
}
// IsAvailable returns if the store bucket of limitation is available
func (s *StoreInfo) IsAvailable(limitType storelimit.Type) bool {
s.mu.RLock()
defer s.mu.RUnlock()
if s.limiter != nil && s.limiter[limitType] != nil {
return s.limiter[limitType].Available(storelimit.RegionInfluence[limitType])
}
return true
}
// IsTiFlash returns true if the store is tiflash.
func (s *StoreInfo) IsTiFlash() bool {
return IsStoreContainLabel(s.GetMeta(), EngineKey, EngineTiFlash)
}
// IsUp returns true if store is serving or preparing.
func (s *StoreInfo) IsUp() bool {
return s.IsServing() || s.IsPreparing()
}
// IsPreparing checks if the store's state is preparing.
func (s *StoreInfo) IsPreparing() bool {
return s.GetNodeState() == metapb.NodeState_Preparing
}
// IsServing checks if the store's state is serving.
func (s *StoreInfo) IsServing() bool {
return s.GetNodeState() == metapb.NodeState_Serving
}
// IsRemoving checks if the store's state is removing.
func (s *StoreInfo) IsRemoving() bool {
return s.GetNodeState() == metapb.NodeState_Removing
}
// IsRemoved checks if the store's state is removed.
func (s *StoreInfo) IsRemoved() bool {
return s.GetNodeState() == metapb.NodeState_Removed
}
// GetSlowScore returns the slow score of the store.
func (s *StoreInfo) GetSlowScore() uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.rawStats.GetSlowScore()
}
// IsSlow checks if the slow score reaches the threshold.
func (s *StoreInfo) IsSlow() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.rawStats.GetSlowScore() >= slowStoreThreshold
}
// IsPhysicallyDestroyed checks if the store's physically destroyed.
func (s *StoreInfo) IsPhysicallyDestroyed() bool {
return s.GetMeta().GetPhysicallyDestroyed()
}
// DownTime returns the time elapsed since last heartbeat.
func (s *StoreInfo) DownTime() time.Duration {
return time.Since(s.GetLastHeartbeatTS())
}
// GetMeta returns the meta information of the store.
func (s *StoreInfo) GetMeta() *metapb.Store {
return s.meta
}
// GetState returns the state of the store.
func (s *StoreInfo) GetState() metapb.StoreState {
return s.meta.GetState()
}
// GetNodeState returns the state of the node.
func (s *StoreInfo) GetNodeState() metapb.NodeState {
return s.meta.GetNodeState()
}
// GetStatusAddress returns the http address of the store.
func (s *StoreInfo) GetStatusAddress() string {
return s.meta.GetStatusAddress()
}
// GetAddress returns the address of the store.
func (s *StoreInfo) GetAddress() string {
return s.meta.GetAddress()
}
// GetVersion returns the version of the store.
func (s *StoreInfo) GetVersion() string {
return s.meta.GetVersion()
}
// GetLabels returns the labels of the store.
func (s *StoreInfo) GetLabels() []*metapb.StoreLabel {
return s.meta.GetLabels()
}
// GetID returns the ID of the store.
func (s *StoreInfo) GetID() uint64 {
return s.meta.GetId()
}
// GetLeaderCount returns the leader count of the store.
func (s *StoreInfo) GetLeaderCount() int {
return s.leaderCount
}
// GetRegionCount returns the Region count of the store.
func (s *StoreInfo) GetRegionCount() int {
return s.regionCount
}
// GetLeaderSize returns the leader size of the store.
func (s *StoreInfo) GetLeaderSize() int64 {
return s.leaderSize
}
// GetRegionSize returns the Region size of the store.
func (s *StoreInfo) GetRegionSize() int64 {
return s.regionSize
}
// GetPendingPeerCount returns the pending peer count of the store.
func (s *StoreInfo) GetPendingPeerCount() int {
return s.pendingPeerCount
}
// GetLeaderWeight returns the leader weight of the store.
func (s *StoreInfo) GetLeaderWeight() float64 {
return s.leaderWeight
}
// GetRegionWeight returns the Region weight of the store.
func (s *StoreInfo) GetRegionWeight() float64 {
return s.regionWeight
}
// GetLastHeartbeatTS returns the last heartbeat timestamp of the store.
func (s *StoreInfo) GetLastHeartbeatTS() time.Time {
return time.Unix(0, s.meta.GetLastHeartbeat())
}
// NeedPersist returns if it needs to save to etcd.
func (s *StoreInfo) NeedPersist() bool {
return s.GetLastHeartbeatTS().Sub(s.lastPersistTime) > storePersistInterval
}
// GetStoreLimit return the limit of a specific store.
func (s *StoreInfo) GetStoreLimit(limitType storelimit.Type) *storelimit.StoreLimit {
s.mu.RLock()
defer s.mu.RUnlock()
return s.limiter[limitType]
}
const minWeight = 1e-6
const maxScore = 1024 * 1024 * 1024
// LeaderScore returns the store's leader score.
func (s *StoreInfo) LeaderScore(policy SchedulePolicy, delta int64) float64 {
switch policy {
case BySize:
return float64(s.GetLeaderSize()+delta) / math.Max(s.GetLeaderWeight(), minWeight)
case ByCount:
return float64(int64(s.GetLeaderCount())+delta) / math.Max(s.GetLeaderWeight(), minWeight)
default:
return 0
}
}
// RegionScore returns the store's region score.
// Deviation It is used to control the direction of the deviation considered
// when calculating the region score. It is set to -1 when it is the source
// store of balance, 1 when it is the target, and 0 in the rest of cases.
func (s *StoreInfo) RegionScore(version string, highSpaceRatio, lowSpaceRatio float64, delta int64) float64 {
switch version {
case "v2":
return s.regionScoreV2(delta, lowSpaceRatio)
case "v1":
fallthrough
default:
return s.regionScoreV1(highSpaceRatio, lowSpaceRatio, delta)
}
}
func (s *StoreInfo) regionScoreV1(highSpaceRatio, lowSpaceRatio float64, delta int64) float64 {
var score float64
var amplification float64
available := float64(s.GetAvailable()) / mb
used := float64(s.GetUsedSize()) / mb
capacity := float64(s.GetCapacity()) / mb
if s.GetRegionSize() == 0 || used == 0 {
amplification = 1
} else {
// because of rocksdb compression, region size is larger than actual used size
amplification = float64(s.GetRegionSize()) / used
}
// highSpaceBound is the lower bound of the high space stage.
highSpaceBound := (1 - highSpaceRatio) * capacity
// lowSpaceBound is the upper bound of the low space stage.
lowSpaceBound := (1 - lowSpaceRatio) * capacity
if available-float64(delta)/amplification >= highSpaceBound {
score = float64(s.GetRegionSize() + delta)
} else if available-float64(delta)/amplification <= lowSpaceBound {
score = maxScore - (available - float64(delta)/amplification)
} else {
// to make the score function continuous, we use linear function y = k * x + b as transition period
// from above we know that there are two points must on the function image
// note that it is possible that other irrelative files occupy a lot of storage, so capacity == available + used + irrelative
// and we regarded irrelative as a fixed value.
// Then amp = size / used = size / (capacity - irrelative - available)
//
// When available == highSpaceBound,
// we can conclude that size = (capacity - irrelative - highSpaceBound) * amp = (used + available - highSpaceBound) * amp
// Similarly, when available == lowSpaceBound,
// we can conclude that size = (capacity - irrelative - lowSpaceBound) * amp = (used + available - lowSpaceBound) * amp
// These are the two fixed points' x-coordinates, and y-coordinates which can be easily obtained from the above two functions.
x1, y1 := (used+available-highSpaceBound)*amplification, (used+available-highSpaceBound)*amplification
x2, y2 := (used+available-lowSpaceBound)*amplification, maxScore-lowSpaceBound
k := (y2 - y1) / (x2 - x1)
b := y1 - k*x1
score = k*float64(s.GetRegionSize()+delta) + b
}
return score / math.Max(s.GetRegionWeight(), minWeight)
}
func (s *StoreInfo) regionScoreV2(delta int64, lowSpaceRatio float64) float64 {
A := float64(s.GetAvgAvailable()) / gb
C := float64(s.GetCapacity()) / gb
R := float64(s.GetRegionSize() + delta)
if R < 0 {
R = float64(s.GetRegionSize())
}
U := C - A
if s.GetRegionSize() != 0 {
U += U * (float64(delta)) / float64(s.GetRegionSize())
if U < C && U > 0 {
A = C - U
}
}
var (
K, M float64 = 1, 256 // Experience value to control the weight of the available influence on score
F float64 = 50 // Experience value to prevent some nodes from running out of disk space prematurely.
B = 1e10
)
F = math.Max(F, C*(1-lowSpaceRatio))
var score float64
if A >= C || C < 1 {
score = R
} else if A > F {
// As the amount of data increases (available becomes smaller), the weight of region size on total score
// increases. Ideally, all nodes converge at the position where remaining space is F (default 20GiB).
score = (K + M*(math.Log(C)-math.Log(A-F+1))/(C-A+F-1)) * R
} else {
// When remaining space is less then F, the score is mainly determined by available space.
// store's score will increase rapidly after it has few space. and it will reach similar score when they has no space
score = (K+M*math.Log(C)/C)*R + B*(F-A)/F
}
return score / math.Max(s.GetRegionWeight(), minWeight)
}
// StorageSize returns store's used storage size reported from tikv.
func (s *StoreInfo) StorageSize() uint64 {
return s.GetUsedSize()
}
// AvailableRatio is store's freeSpace/capacity.
func (s *StoreInfo) AvailableRatio() float64 {
if s.GetCapacity() == 0 {
return 0
}
return float64(s.GetAvailable()) / float64(s.GetCapacity())
}
// IsLowSpace checks if the store is lack of space. Not check if region count less
// than initialMaxRegionCounts and available space more than initialMinSpace
func (s *StoreInfo) IsLowSpace(lowSpaceRatio float64) bool {
if s.GetStoreStats() == nil {
return false
}
// issue #3444
if s.regionCount < initialMaxRegionCounts && s.GetAvailable() > initialMinSpace {
return false
}
return s.AvailableRatio() < 1-lowSpaceRatio
}
// ResourceCount returns count of leader/region in the store.
func (s *StoreInfo) ResourceCount(kind ResourceKind) uint64 {
switch kind {
case LeaderKind:
return uint64(s.GetLeaderCount())
case RegionKind:
return uint64(s.GetRegionCount())
default:
return 0
}
}
// ResourceSize returns size of leader/region in the store
func (s *StoreInfo) ResourceSize(kind ResourceKind) int64 {
switch kind {
case LeaderKind:
return s.GetLeaderSize()
case RegionKind:
return s.GetRegionSize()
default:
return 0
}
}
// ResourceWeight returns weight of leader/region in the score
func (s *StoreInfo) ResourceWeight(kind ResourceKind) float64 {
switch kind {
case LeaderKind:
leaderWeight := s.GetLeaderWeight()
if leaderWeight <= 0 {
return minWeight
}
return leaderWeight
case RegionKind:
regionWeight := s.GetRegionWeight()
if regionWeight <= 0 {
return minWeight
}
return regionWeight
default:
return 0
}
}
// GetStartTime returns the start timestamp.
func (s *StoreInfo) GetStartTime() time.Time {
return time.Unix(s.meta.GetStartTimestamp(), 0)
}
// GetUptime returns the uptime.
func (s *StoreInfo) GetUptime() time.Duration {
uptime := s.GetLastHeartbeatTS().Sub(s.GetStartTime())
if uptime > 0 {
return uptime
}
return 0
}
// GetMinResolvedTS returns min resolved ts.
func (s *StoreInfo) GetMinResolvedTS() uint64 {
return s.minResolvedTS
}
var (
// If a store's last heartbeat is storeDisconnectDuration ago, the store will
// be marked as disconnected state. The value should be greater than tikv's
// store heartbeat interval (default 10s).
storeDisconnectDuration = 20 * time.Second
storeUnhealthyDuration = 10 * time.Minute
)
// IsDisconnected checks if a store is disconnected, which means PD misses
// tikv's store heartbeat for a short time, maybe caused by process restart or
// temporary network failure.
func (s *StoreInfo) IsDisconnected() bool {
return s.DownTime() > storeDisconnectDuration
}
// IsUnhealthy checks if a store is unhealthy.
func (s *StoreInfo) IsUnhealthy() bool {
return s.DownTime() > storeUnhealthyDuration
}
// GetLabelValue returns a label's value (if exists).
func (s *StoreInfo) GetLabelValue(key string) string {
for _, label := range s.GetLabels() {
if strings.EqualFold(label.GetKey(), key) {
return label.GetValue()
}
}
return ""
}
// CompareLocation compares 2 stores' labels and returns at which level their
// locations are different. It returns -1 if they are at the same location.
func (s *StoreInfo) CompareLocation(other *StoreInfo, labels []string) int {
for i, key := range labels {
v1, v2 := s.GetLabelValue(key), other.GetLabelValue(key)
// If label is not set, the store is considered at the same location
// with any other store.
if v1 != "" && v2 != "" && !strings.EqualFold(v1, v2) {
return i
}
}
return -1
}
const replicaBaseScore = 100
// DistinctScore returns the score that the other is distinct from the stores.
// A higher score means the other store is more different from the existed stores.
func DistinctScore(labels []string, stores []*StoreInfo, other *StoreInfo) float64 {
var score float64
for _, s := range stores {
if s.GetID() == other.GetID() {
continue
}
if index := s.CompareLocation(other, labels); index != -1 {
score += math.Pow(replicaBaseScore, float64(len(labels)-index-1))
}
}
return score
}
// MergeLabels merges the passed in labels with origins, overriding duplicated
// ones.
func (s *StoreInfo) MergeLabels(labels []*metapb.StoreLabel) []*metapb.StoreLabel {
storeLabels := s.GetLabels()
L:
for _, newLabel := range labels {
for _, label := range storeLabels {
if strings.EqualFold(label.Key, newLabel.Key) {
label.Value = newLabel.Value
continue L
}
}
storeLabels = append(storeLabels, newLabel)
}
res := storeLabels[:0]
for _, l := range storeLabels {
if l.Value != "" {
res = append(res, l)
}
}
return res
}
// StoresInfo contains information about all stores.
type StoresInfo struct {
stores map[uint64]*StoreInfo
}
// NewStoresInfo create a StoresInfo with map of storeID to StoreInfo
func NewStoresInfo() *StoresInfo {
return &StoresInfo{
stores: make(map[uint64]*StoreInfo),
}
}
// GetStore returns a copy of the StoreInfo with the specified storeID.
func (s *StoresInfo) GetStore(storeID uint64) *StoreInfo {
store, ok := s.stores[storeID]
if !ok {
return nil
}
return store
}
// SetStore sets a StoreInfo with storeID.
func (s *StoresInfo) SetStore(store *StoreInfo) {
s.stores[store.GetID()] = store
}
// PauseLeaderTransfer pauses a StoreInfo with storeID.
func (s *StoresInfo) PauseLeaderTransfer(storeID uint64) error {
store, ok := s.stores[storeID]
if !ok {
return errs.ErrStoreNotFound.FastGenByArgs(storeID)
}
if !store.AllowLeaderTransfer() {
return errs.ErrPauseLeaderTransfer.FastGenByArgs(storeID)
}
s.stores[storeID] = store.Clone(PauseLeaderTransfer())
return nil
}
// ResumeLeaderTransfer cleans a store's pause state. The store can be selected
// as source or target of TransferLeader again.
func (s *StoresInfo) ResumeLeaderTransfer(storeID uint64) {
store, ok := s.stores[storeID]
if !ok {
log.Warn("try to clean a store's pause state, but it is not found. It may be cleanup",
zap.Uint64("store-id", storeID))
return
}
s.stores[storeID] = store.Clone(ResumeLeaderTransfer())
}
// SlowStoreEvicted marks a store as a slow store and prevents transferring
// leader to the store
func (s *StoresInfo) SlowStoreEvicted(storeID uint64) error {
store, ok := s.stores[storeID]
if !ok {
return errs.ErrStoreNotFound.FastGenByArgs(storeID)
}
if store.EvictedAsSlowStore() {
return errs.ErrSlowStoreEvicted.FastGenByArgs(storeID)
}
s.stores[storeID] = store.Clone(SlowStoreEvicted())
return nil
}
// SlowStoreRecovered cleans the evicted state of a store.
func (s *StoresInfo) SlowStoreRecovered(storeID uint64) {
store, ok := s.stores[storeID]
if !ok {
log.Warn("try to clean a store's evicted as a slow store state, but it is not found. It may be cleanup",
zap.Uint64("store-id", storeID))
return
}
s.stores[storeID] = store.Clone(SlowStoreRecovered())
}
// ResetStoreLimit resets the limit for a specific store.
func (s *StoresInfo) ResetStoreLimit(storeID uint64, limitType storelimit.Type, ratePerSec ...float64) {
if store, ok := s.stores[storeID]; ok {
s.stores[storeID] = store.Clone(ResetStoreLimit(limitType, ratePerSec...))
}
}
// GetStores gets a complete set of StoreInfo.
func (s *StoresInfo) GetStores() []*StoreInfo {
stores := make([]*StoreInfo, 0, len(s.stores))
for _, store := range s.stores {
stores = append(stores, store)
}
return stores
}
// GetMetaStores gets a complete set of metapb.Store.
func (s *StoresInfo) GetMetaStores() []*metapb.Store {
stores := make([]*metapb.Store, 0, len(s.stores))
for _, store := range s.stores {
stores = append(stores, store.GetMeta())
}
return stores
}
// DeleteStore deletes tombstone record form store
func (s *StoresInfo) DeleteStore(store *StoreInfo) {
delete(s.stores, store.GetID())
}
// GetStoreCount returns the total count of storeInfo.
func (s *StoresInfo) GetStoreCount() int {
return len(s.stores)
}
// SetLeaderCount sets the leader count to a storeInfo.
func (s *StoresInfo) SetLeaderCount(storeID uint64, leaderCount int) {
if store, ok := s.stores[storeID]; ok {
s.stores[storeID] = store.Clone(SetLeaderCount(leaderCount))
}
}
// SetRegionCount sets the region count to a storeInfo.
func (s *StoresInfo) SetRegionCount(storeID uint64, regionCount int) {
if store, ok := s.stores[storeID]; ok {
s.stores[storeID] = store.Clone(SetRegionCount(regionCount))
}
}
// SetPendingPeerCount sets the pending count to a storeInfo.
func (s *StoresInfo) SetPendingPeerCount(storeID uint64, pendingPeerCount int) {
if store, ok := s.stores[storeID]; ok {
s.stores[storeID] = store.Clone(SetPendingPeerCount(pendingPeerCount))
}
}
// SetLeaderSize sets the leader size to a storeInfo.
func (s *StoresInfo) SetLeaderSize(storeID uint64, leaderSize int64) {
if store, ok := s.stores[storeID]; ok {
s.stores[storeID] = store.Clone(SetLeaderSize(leaderSize))
}
}
// SetRegionSize sets the region size to a storeInfo.
func (s *StoresInfo) SetRegionSize(storeID uint64, regionSize int64) {
if store, ok := s.stores[storeID]; ok {
s.stores[storeID] = store.Clone(SetRegionSize(regionSize))
}
}
// UpdateStoreStatus updates the information of the store.
func (s *StoresInfo) UpdateStoreStatus(storeID uint64, leaderCount int, regionCount int, pendingPeerCount int, leaderSize int64, regionSize int64) {
if store, ok := s.stores[storeID]; ok {
newStore := store.ShallowClone(SetLeaderCount(leaderCount),
SetRegionCount(regionCount),
SetPendingPeerCount(pendingPeerCount),
SetLeaderSize(leaderSize),
SetRegionSize(regionSize))
s.SetStore(newStore)
}
}
// IsStoreContainLabel returns if the store contains the given label.
func IsStoreContainLabel(store *metapb.Store, key, value string) bool {
for _, l := range store.GetLabels() {
if l.GetKey() == key && l.GetValue() == value {
return true
}
}
return false
}
// IsAvailableForMinResolvedTS returns if the store is available for min resolved ts.
func IsAvailableForMinResolvedTS(s *StoreInfo) bool {
// If a store is tombstone or no leader, it is not meaningful for min resolved ts.
// And we will skip tiflash, because it does not report min resolved ts.
return !s.IsRemoved() && !s.IsTiFlash() && s.GetLeaderCount() != 0
}