-
Notifications
You must be signed in to change notification settings - Fork 711
/
persist_options.go
1068 lines (929 loc) · 35.7 KB
/
persist_options.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 2017 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 config
import (
"context"
"fmt"
"reflect"
"strconv"
"strings"
"sync/atomic"
"time"
"unsafe"
"github.com/coreos/go-semver/semver"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/cache"
"github.com/tikv/pd/pkg/core/constant"
"github.com/tikv/pd/pkg/core/storelimit"
sc "github.com/tikv/pd/pkg/schedule/config"
"github.com/tikv/pd/pkg/slice"
"github.com/tikv/pd/pkg/storage/endpoint"
"github.com/tikv/pd/pkg/utils/etcdutil"
"github.com/tikv/pd/pkg/utils/typeutil"
"go.etcd.io/etcd/clientv3"
"go.uber.org/zap"
)
// PersistOptions wraps all configurations that need to persist to storage and
// allows to access them safely.
type PersistOptions struct {
// configuration -> ttl value
ttl *cache.TTLString
schedule atomic.Value
replication atomic.Value
pdServerConfig atomic.Value
replicationMode atomic.Value
labelProperty atomic.Value
keyspace atomic.Value
microService atomic.Value
storeConfig atomic.Value
clusterVersion unsafe.Pointer
}
// NewPersistOptions creates a new PersistOptions instance.
func NewPersistOptions(cfg *Config) *PersistOptions {
o := &PersistOptions{}
o.schedule.Store(&cfg.Schedule)
o.replication.Store(&cfg.Replication)
o.pdServerConfig.Store(&cfg.PDServerCfg)
o.replicationMode.Store(&cfg.ReplicationMode)
o.labelProperty.Store(cfg.LabelProperty)
o.keyspace.Store(&cfg.Keyspace)
o.microService.Store(&cfg.MicroService)
// storeConfig will be fetched from TiKV later,
// set it to an empty config here first.
o.storeConfig.Store(&sc.StoreConfig{})
o.SetClusterVersion(&cfg.ClusterVersion)
o.ttl = nil
return o
}
// GetScheduleConfig returns scheduling configurations.
func (o *PersistOptions) GetScheduleConfig() *sc.ScheduleConfig {
return o.schedule.Load().(*sc.ScheduleConfig)
}
// SetScheduleConfig sets the PD scheduling configuration.
func (o *PersistOptions) SetScheduleConfig(cfg *sc.ScheduleConfig) {
o.schedule.Store(cfg)
}
// GetReplicationConfig returns replication configurations.
func (o *PersistOptions) GetReplicationConfig() *sc.ReplicationConfig {
return o.replication.Load().(*sc.ReplicationConfig)
}
// SetReplicationConfig sets the PD replication configuration.
func (o *PersistOptions) SetReplicationConfig(cfg *sc.ReplicationConfig) {
o.replication.Store(cfg)
}
// GetPDServerConfig returns pd server configurations.
func (o *PersistOptions) GetPDServerConfig() *PDServerConfig {
return o.pdServerConfig.Load().(*PDServerConfig)
}
// SetPDServerConfig sets the PD configuration.
func (o *PersistOptions) SetPDServerConfig(cfg *PDServerConfig) {
o.pdServerConfig.Store(cfg)
}
// GetReplicationModeConfig returns the replication mode config.
func (o *PersistOptions) GetReplicationModeConfig() *ReplicationModeConfig {
return o.replicationMode.Load().(*ReplicationModeConfig)
}
// SetReplicationModeConfig sets the replication mode config.
func (o *PersistOptions) SetReplicationModeConfig(cfg *ReplicationModeConfig) {
o.replicationMode.Store(cfg)
}
// GetLabelPropertyConfig returns the label property.
func (o *PersistOptions) GetLabelPropertyConfig() LabelPropertyConfig {
return o.labelProperty.Load().(LabelPropertyConfig)
}
// SetLabelPropertyConfig sets the label property configuration.
func (o *PersistOptions) SetLabelPropertyConfig(cfg LabelPropertyConfig) {
o.labelProperty.Store(cfg)
}
// GetKeyspaceConfig returns the keyspace config.
func (o *PersistOptions) GetKeyspaceConfig() *KeyspaceConfig {
return o.keyspace.Load().(*KeyspaceConfig)
}
// SetKeyspaceConfig sets the keyspace configuration.
func (o *PersistOptions) SetKeyspaceConfig(cfg *KeyspaceConfig) {
o.keyspace.Store(cfg)
}
// GetMicroServiceConfig returns the micro service configuration.
func (o *PersistOptions) GetMicroServiceConfig() *MicroServiceConfig {
return o.microService.Load().(*MicroServiceConfig)
}
// SetMicroServiceConfig sets the micro service configuration.
func (o *PersistOptions) SetMicroServiceConfig(cfg *MicroServiceConfig) {
o.microService.Store(cfg)
}
// GetStoreConfig returns the store config.
func (o *PersistOptions) GetStoreConfig() *sc.StoreConfig {
return o.storeConfig.Load().(*sc.StoreConfig)
}
// SetStoreConfig sets the store configuration.
func (o *PersistOptions) SetStoreConfig(cfg *sc.StoreConfig) {
o.storeConfig.Store(cfg)
}
// GetClusterVersion returns the cluster version.
func (o *PersistOptions) GetClusterVersion() *semver.Version {
return (*semver.Version)(atomic.LoadPointer(&o.clusterVersion))
}
// SetClusterVersion sets the cluster version.
func (o *PersistOptions) SetClusterVersion(v *semver.Version) {
atomic.StorePointer(&o.clusterVersion, unsafe.Pointer(v))
}
// CASClusterVersion sets the cluster version.
func (o *PersistOptions) CASClusterVersion(old, new *semver.Version) bool {
return atomic.CompareAndSwapPointer(&o.clusterVersion, unsafe.Pointer(old), unsafe.Pointer(new))
}
// GetLocationLabels returns the location labels for each region.
func (o *PersistOptions) GetLocationLabels() []string {
return o.GetReplicationConfig().LocationLabels
}
// SetLocationLabels sets the location labels.
func (o *PersistOptions) SetLocationLabels(labels []string) {
v := o.GetReplicationConfig().Clone()
v.LocationLabels = labels
o.SetReplicationConfig(v)
}
// GetIsolationLevel returns the isolation label for each region.
func (o *PersistOptions) GetIsolationLevel() string {
return o.GetReplicationConfig().IsolationLevel
}
// IsPlacementRulesEnabled returns if the placement rules is enabled.
func (o *PersistOptions) IsPlacementRulesEnabled() bool {
return o.GetReplicationConfig().EnablePlacementRules
}
// SetPlacementRuleEnabled set PlacementRuleEnabled
func (o *PersistOptions) SetPlacementRuleEnabled(enabled bool) {
v := o.GetReplicationConfig().Clone()
v.EnablePlacementRules = enabled
o.SetReplicationConfig(v)
}
// IsPlacementRulesCacheEnabled returns if the placement rules cache is enabled
func (o *PersistOptions) IsPlacementRulesCacheEnabled() bool {
return o.GetReplicationConfig().EnablePlacementRulesCache
}
// SetPlacementRulesCacheEnabled set EnablePlacementRulesCache
func (o *PersistOptions) SetPlacementRulesCacheEnabled(enabled bool) {
v := o.GetReplicationConfig().Clone()
v.EnablePlacementRulesCache = enabled
o.SetReplicationConfig(v)
}
// GetStrictlyMatchLabel returns whether check label strict.
func (o *PersistOptions) GetStrictlyMatchLabel() bool {
return o.GetReplicationConfig().StrictlyMatchLabel
}
// GetMaxReplicas returns the number of replicas for each region.
func (o *PersistOptions) GetMaxReplicas() int {
return int(o.GetReplicationConfig().MaxReplicas)
}
// SetMaxReplicas sets the number of replicas for each region.
func (o *PersistOptions) SetMaxReplicas(replicas int) {
v := o.GetReplicationConfig().Clone()
v.MaxReplicas = uint64(replicas)
o.SetReplicationConfig(v)
}
var supportedTTLConfigs = []string{
sc.MaxSnapshotCountKey,
sc.MaxMergeRegionSizeKey,
sc.MaxPendingPeerCountKey,
sc.MaxMergeRegionKeysKey,
sc.LeaderScheduleLimitKey,
sc.RegionScheduleLimitKey,
sc.ReplicaRescheduleLimitKey,
sc.MergeScheduleLimitKey,
sc.HotRegionScheduleLimitKey,
sc.SchedulerMaxWaitingOperatorKey,
sc.EnableLocationReplacement,
sc.EnableTiKVSplitRegion,
sc.DefaultAddPeer,
sc.DefaultRemovePeer,
}
// IsSupportedTTLConfig checks whether a key is a supported config item with ttl
func IsSupportedTTLConfig(key string) bool {
for _, supportedConfig := range supportedTTLConfigs {
if key == supportedConfig {
return true
}
}
return strings.HasPrefix(key, "add-peer-") || strings.HasPrefix(key, "remove-peer-")
}
// GetMaxSnapshotCount returns the number of the max snapshot which is allowed to send.
func (o *PersistOptions) GetMaxSnapshotCount() uint64 {
return o.getTTLNumberOr(sc.MaxSnapshotCountKey, o.GetScheduleConfig().MaxSnapshotCount)
}
// GetMaxPendingPeerCount returns the number of the max pending peers.
func (o *PersistOptions) GetMaxPendingPeerCount() uint64 {
return o.getTTLNumberOr(sc.MaxPendingPeerCountKey, o.GetScheduleConfig().MaxPendingPeerCount)
}
// GetMaxMergeRegionSize returns the max region size.
func (o *PersistOptions) GetMaxMergeRegionSize() uint64 {
return o.getTTLNumberOr(sc.MaxMergeRegionSizeKey, o.GetScheduleConfig().MaxMergeRegionSize)
}
// GetMaxMergeRegionKeys returns the max number of keys.
// It returns size * 10000 if the key of max-merge-region-Keys doesn't exist.
func (o *PersistOptions) GetMaxMergeRegionKeys() uint64 {
keys, exist, err := o.getTTLNumber(sc.MaxMergeRegionKeysKey)
if exist && err == nil {
return keys
}
size, exist, err := o.getTTLNumber(sc.MaxMergeRegionSizeKey)
if exist && err == nil {
return size * 10000
}
return o.GetScheduleConfig().GetMaxMergeRegionKeys()
}
// GetSplitMergeInterval returns the interval between finishing split and starting to merge.
func (o *PersistOptions) GetSplitMergeInterval() time.Duration {
return o.GetScheduleConfig().SplitMergeInterval.Duration
}
// SetSplitMergeInterval to set the interval between finishing split and starting to merge. It's only used to test.
func (o *PersistOptions) SetSplitMergeInterval(splitMergeInterval time.Duration) {
v := o.GetScheduleConfig().Clone()
v.SplitMergeInterval = typeutil.Duration{Duration: splitMergeInterval}
o.SetScheduleConfig(v)
}
// GetSwitchWitnessInterval returns the interval between promote to non-witness and starting to switch to witness.
func (o *PersistOptions) GetSwitchWitnessInterval() time.Duration {
return o.GetScheduleConfig().SwitchWitnessInterval.Duration
}
// IsDiagnosticAllowed returns whether is enable to use diagnostic.
func (o *PersistOptions) IsDiagnosticAllowed() bool {
return o.GetScheduleConfig().EnableDiagnostic
}
// SetEnableDiagnostic to set the option for diagnose. It's only used to test.
func (o *PersistOptions) SetEnableDiagnostic(enable bool) {
v := o.GetScheduleConfig().Clone()
v.EnableDiagnostic = enable
o.SetScheduleConfig(v)
}
// IsWitnessAllowed returns whether is enable to use witness.
func (o *PersistOptions) IsWitnessAllowed() bool {
return o.GetScheduleConfig().EnableWitness
}
// SetEnableWitness to set the option for witness. It's only used to test.
func (o *PersistOptions) SetEnableWitness(enable bool) {
v := o.GetScheduleConfig().Clone()
v.EnableWitness = enable
o.SetScheduleConfig(v)
}
// SetMaxStoreDownTime to set the max store down time. It's only used to test.
func (o *PersistOptions) SetMaxStoreDownTime(time time.Duration) {
v := o.GetScheduleConfig().Clone()
v.MaxStoreDownTime = typeutil.NewDuration(time)
o.SetScheduleConfig(v)
}
// SetMaxMergeRegionSize sets the max merge region size.
func (o *PersistOptions) SetMaxMergeRegionSize(maxMergeRegionSize uint64) {
v := o.GetScheduleConfig().Clone()
v.MaxMergeRegionSize = maxMergeRegionSize
o.SetScheduleConfig(v)
}
// SetMaxMergeRegionKeys sets the max merge region keys.
func (o *PersistOptions) SetMaxMergeRegionKeys(maxMergeRegionKeys uint64) {
v := o.GetScheduleConfig().Clone()
v.MaxMergeRegionKeys = maxMergeRegionKeys
o.SetScheduleConfig(v)
}
// SetStoreLimit sets a store limit for a given type and rate.
func (o *PersistOptions) SetStoreLimit(storeID uint64, typ storelimit.Type, ratePerMin float64) {
v := o.GetScheduleConfig().Clone()
var slc sc.StoreLimitConfig
var rate float64
switch typ {
case storelimit.AddPeer:
if _, ok := v.StoreLimit[storeID]; !ok {
rate = sc.DefaultStoreLimit.GetDefaultStoreLimit(storelimit.RemovePeer)
} else {
rate = v.StoreLimit[storeID].RemovePeer
}
slc = sc.StoreLimitConfig{AddPeer: ratePerMin, RemovePeer: rate}
case storelimit.RemovePeer:
if _, ok := v.StoreLimit[storeID]; !ok {
rate = sc.DefaultStoreLimit.GetDefaultStoreLimit(storelimit.AddPeer)
} else {
rate = v.StoreLimit[storeID].AddPeer
}
slc = sc.StoreLimitConfig{AddPeer: rate, RemovePeer: ratePerMin}
}
v.StoreLimit[storeID] = slc
o.SetScheduleConfig(v)
}
// SetAllStoresLimit sets all store limit for a given type and rate.
func (o *PersistOptions) SetAllStoresLimit(typ storelimit.Type, ratePerMin float64) {
v := o.GetScheduleConfig().Clone()
switch typ {
case storelimit.AddPeer:
sc.DefaultStoreLimit.SetDefaultStoreLimit(storelimit.AddPeer, ratePerMin)
for storeID := range v.StoreLimit {
sc := sc.StoreLimitConfig{AddPeer: ratePerMin, RemovePeer: v.StoreLimit[storeID].RemovePeer}
v.StoreLimit[storeID] = sc
}
case storelimit.RemovePeer:
sc.DefaultStoreLimit.SetDefaultStoreLimit(storelimit.RemovePeer, ratePerMin)
for storeID := range v.StoreLimit {
sc := sc.StoreLimitConfig{AddPeer: v.StoreLimit[storeID].AddPeer, RemovePeer: ratePerMin}
v.StoreLimit[storeID] = sc
}
}
o.SetScheduleConfig(v)
}
// IsOneWayMergeEnabled returns if a region can only be merged into the next region of it.
func (o *PersistOptions) IsOneWayMergeEnabled() bool {
return o.GetScheduleConfig().EnableOneWayMerge
}
// IsCrossTableMergeEnabled returns if across table merge is enabled.
func (o *PersistOptions) IsCrossTableMergeEnabled() bool {
return o.GetScheduleConfig().EnableCrossTableMerge
}
// GetPatrolRegionInterval returns the interval of patrolling region.
func (o *PersistOptions) GetPatrolRegionInterval() time.Duration {
return o.GetScheduleConfig().PatrolRegionInterval.Duration
}
// GetMaxStoreDownTime returns the max down time of a store.
func (o *PersistOptions) GetMaxStoreDownTime() time.Duration {
return o.GetScheduleConfig().MaxStoreDownTime.Duration
}
// GetMaxStorePreparingTime returns the max preparing time of a store.
func (o *PersistOptions) GetMaxStorePreparingTime() time.Duration {
return o.GetScheduleConfig().MaxStorePreparingTime.Duration
}
// GetLeaderScheduleLimit returns the limit for leader schedule.
func (o *PersistOptions) GetLeaderScheduleLimit() uint64 {
return o.getTTLNumberOr(sc.LeaderScheduleLimitKey, o.GetScheduleConfig().LeaderScheduleLimit)
}
// GetRegionScheduleLimit returns the limit for region schedule.
func (o *PersistOptions) GetRegionScheduleLimit() uint64 {
return o.getTTLNumberOr(sc.RegionScheduleLimitKey, o.GetScheduleConfig().RegionScheduleLimit)
}
// GetWitnessScheduleLimit returns the limit for region schedule.
func (o *PersistOptions) GetWitnessScheduleLimit() uint64 {
return o.getTTLNumberOr(sc.WitnessScheduleLimitKey, o.GetScheduleConfig().WitnessScheduleLimit)
}
// GetReplicaScheduleLimit returns the limit for replica schedule.
func (o *PersistOptions) GetReplicaScheduleLimit() uint64 {
return o.getTTLNumberOr(sc.ReplicaRescheduleLimitKey, o.GetScheduleConfig().ReplicaScheduleLimit)
}
// GetMergeScheduleLimit returns the limit for merge schedule.
func (o *PersistOptions) GetMergeScheduleLimit() uint64 {
return o.getTTLNumberOr(sc.MergeScheduleLimitKey, o.GetScheduleConfig().MergeScheduleLimit)
}
// GetHotRegionScheduleLimit returns the limit for hot region schedule.
func (o *PersistOptions) GetHotRegionScheduleLimit() uint64 {
return o.getTTLNumberOr(sc.HotRegionScheduleLimitKey, o.GetScheduleConfig().HotRegionScheduleLimit)
}
// GetStoreLimit returns the limit of a store.
func (o *PersistOptions) GetStoreLimit(storeID uint64) (returnSC sc.StoreLimitConfig) {
defer func() {
returnSC.RemovePeer = o.getTTLFloatOr(fmt.Sprintf("remove-peer-%v", storeID), returnSC.RemovePeer)
returnSC.AddPeer = o.getTTLFloatOr(fmt.Sprintf("add-peer-%v", storeID), returnSC.AddPeer)
}()
if limit, ok := o.GetScheduleConfig().StoreLimit[storeID]; ok {
return limit
}
cfg := o.GetScheduleConfig().Clone()
sc := sc.StoreLimitConfig{
AddPeer: sc.DefaultStoreLimit.GetDefaultStoreLimit(storelimit.AddPeer),
RemovePeer: sc.DefaultStoreLimit.GetDefaultStoreLimit(storelimit.RemovePeer),
}
v, ok1, err := o.getTTLFloat("default-add-peer")
if err != nil {
log.Warn("failed to parse default-add-peer from PersistOptions's ttl storage", zap.Error(err))
}
canSetAddPeer := ok1 && err == nil
if canSetAddPeer {
returnSC.AddPeer = v
}
v, ok2, err := o.getTTLFloat("default-remove-peer")
if err != nil {
log.Warn("failed to parse default-remove-peer from PersistOptions's ttl storage", zap.Error(err))
}
canSetRemovePeer := ok2 && err == nil
if canSetRemovePeer {
returnSC.RemovePeer = v
}
if canSetAddPeer || canSetRemovePeer {
return returnSC
}
cfg.StoreLimit[storeID] = sc
o.SetScheduleConfig(cfg)
return o.GetScheduleConfig().StoreLimit[storeID]
}
// GetStoreLimitByType returns the limit of a store with a given type.
func (o *PersistOptions) GetStoreLimitByType(storeID uint64, typ storelimit.Type) (returned float64) {
defer func() {
if typ == storelimit.RemovePeer {
returned = o.getTTLFloatOr(fmt.Sprintf("remove-peer-%v", storeID), returned)
} else if typ == storelimit.AddPeer {
returned = o.getTTLFloatOr(fmt.Sprintf("add-peer-%v", storeID), returned)
}
}()
limit := o.GetStoreLimit(storeID)
switch typ {
case storelimit.AddPeer:
return limit.AddPeer
case storelimit.RemovePeer:
return limit.RemovePeer
// todo: impl it in store limit v2.
case storelimit.SendSnapshot:
return 0.0
default:
panic("no such limit type")
}
}
// GetAllStoresLimit returns the limit of all stores.
func (o *PersistOptions) GetAllStoresLimit() map[uint64]sc.StoreLimitConfig {
return o.GetScheduleConfig().StoreLimit
}
// GetStoreLimitVersion returns the limit version of store.
func (o *PersistOptions) GetStoreLimitVersion() string {
return o.GetScheduleConfig().StoreLimitVersion
}
// GetTolerantSizeRatio gets the tolerant size ratio.
func (o *PersistOptions) GetTolerantSizeRatio() float64 {
return o.GetScheduleConfig().TolerantSizeRatio
}
// GetLowSpaceRatio returns the low space ratio.
func (o *PersistOptions) GetLowSpaceRatio() float64 {
return o.GetScheduleConfig().LowSpaceRatio
}
// GetSlowStoreEvictingAffectedStoreRatioThreshold returns the affected ratio threshold when judging a store is slow.
func (o *PersistOptions) GetSlowStoreEvictingAffectedStoreRatioThreshold() float64 {
return o.GetScheduleConfig().SlowStoreEvictingAffectedStoreRatioThreshold
}
// GetHighSpaceRatio returns the high space ratio.
func (o *PersistOptions) GetHighSpaceRatio() float64 {
return o.GetScheduleConfig().HighSpaceRatio
}
// GetRegionScoreFormulaVersion returns the formula version config.
func (o *PersistOptions) GetRegionScoreFormulaVersion() string {
return o.GetScheduleConfig().RegionScoreFormulaVersion
}
// GetSchedulerMaxWaitingOperator returns the number of the max waiting operators.
func (o *PersistOptions) GetSchedulerMaxWaitingOperator() uint64 {
return o.getTTLNumberOr(sc.SchedulerMaxWaitingOperatorKey, o.GetScheduleConfig().SchedulerMaxWaitingOperator)
}
// GetLeaderSchedulePolicy is to get leader schedule policy.
func (o *PersistOptions) GetLeaderSchedulePolicy() constant.SchedulePolicy {
return constant.StringToSchedulePolicy(o.GetScheduleConfig().LeaderSchedulePolicy)
}
// GetKeyType is to get key type.
func (o *PersistOptions) GetKeyType() constant.KeyType {
return constant.StringToKeyType(o.GetPDServerConfig().KeyType)
}
// GetMaxResetTSGap gets the max gap to reset the tso.
func (o *PersistOptions) GetMaxResetTSGap() time.Duration {
return o.GetPDServerConfig().MaxResetTSGap.Duration
}
// GetDashboardAddress gets dashboard address.
func (o *PersistOptions) GetDashboardAddress() string {
return o.GetPDServerConfig().DashboardAddress
}
// IsUseRegionStorage returns if the independent region storage is enabled.
func (o *PersistOptions) IsUseRegionStorage() bool {
return o.GetPDServerConfig().UseRegionStorage
}
// GetServerMemoryLimit gets ServerMemoryLimit config.
func (o *PersistOptions) GetServerMemoryLimit() float64 {
return o.GetPDServerConfig().ServerMemoryLimit
}
// GetServerMemoryLimitGCTrigger gets the ServerMemoryLimitGCTrigger config.
func (o *PersistOptions) GetServerMemoryLimitGCTrigger() float64 {
return o.GetPDServerConfig().ServerMemoryLimitGCTrigger
}
// GetEnableGOGCTuner gets the EnableGOGCTuner config.
func (o *PersistOptions) GetEnableGOGCTuner() bool {
return o.GetPDServerConfig().EnableGOGCTuner
}
// GetGCTunerThreshold gets the GC tuner threshold.
func (o *PersistOptions) GetGCTunerThreshold() float64 {
return o.GetPDServerConfig().GCTunerThreshold
}
// IsRemoveDownReplicaEnabled returns if remove down replica is enabled.
func (o *PersistOptions) IsRemoveDownReplicaEnabled() bool {
return o.GetScheduleConfig().EnableRemoveDownReplica
}
// IsReplaceOfflineReplicaEnabled returns if replace offline replica is enabled.
func (o *PersistOptions) IsReplaceOfflineReplicaEnabled() bool {
return o.GetScheduleConfig().EnableReplaceOfflineReplica
}
// IsMakeUpReplicaEnabled returns if make up replica is enabled.
func (o *PersistOptions) IsMakeUpReplicaEnabled() bool {
return o.GetScheduleConfig().EnableMakeUpReplica
}
// IsRemoveExtraReplicaEnabled returns if remove extra replica is enabled.
func (o *PersistOptions) IsRemoveExtraReplicaEnabled() bool {
return o.GetScheduleConfig().EnableRemoveExtraReplica
}
// IsLocationReplacementEnabled returns if location replace is enabled.
func (o *PersistOptions) IsLocationReplacementEnabled() bool {
return o.getTTLBoolOr(sc.EnableLocationReplacement, o.GetScheduleConfig().EnableLocationReplacement)
}
// IsTikvRegionSplitEnabled returns whether tikv split region is enabled.
func (o *PersistOptions) IsTikvRegionSplitEnabled() bool {
return o.getTTLBoolOr(sc.EnableTiKVSplitRegion, o.GetScheduleConfig().EnableTiKVSplitRegion)
}
// GetMaxMovableHotPeerSize returns the max movable hot peer size.
func (o *PersistOptions) GetMaxMovableHotPeerSize() int64 {
return o.GetScheduleConfig().MaxMovableHotPeerSize
}
// IsDebugMetricsEnabled returns if debug metrics is enabled.
func (o *PersistOptions) IsDebugMetricsEnabled() bool {
return o.GetScheduleConfig().EnableDebugMetrics
}
// IsUseJointConsensus returns if using joint consensus as an operator step is enabled.
func (o *PersistOptions) IsUseJointConsensus() bool {
return o.GetScheduleConfig().EnableJointConsensus
}
// SetEnableUseJointConsensus to set the option for using joint consensus. It's only used to test.
func (o *PersistOptions) SetEnableUseJointConsensus(enable bool) {
v := o.GetScheduleConfig().Clone()
v.EnableJointConsensus = enable
o.SetScheduleConfig(v)
}
// IsTraceRegionFlow returns if the region flow is tracing.
// If the accuracy cannot reach 0.1 MB, it is considered not.
func (o *PersistOptions) IsTraceRegionFlow() bool {
return o.GetPDServerConfig().FlowRoundByDigit <= maxTraceFlowRoundByDigit
}
// GetHotRegionCacheHitsThreshold is a threshold to decide if a region is hot.
func (o *PersistOptions) GetHotRegionCacheHitsThreshold() int {
return int(o.GetScheduleConfig().HotRegionCacheHitsThreshold)
}
// GetStoresLimit gets the stores' limit.
func (o *PersistOptions) GetStoresLimit() map[uint64]sc.StoreLimitConfig {
return o.GetScheduleConfig().StoreLimit
}
// GetSchedulers gets the scheduler configurations.
func (o *PersistOptions) GetSchedulers() sc.SchedulerConfigs {
return o.GetScheduleConfig().Schedulers
}
// IsSchedulerDisabled returns if the scheduler is disabled.
func (o *PersistOptions) IsSchedulerDisabled(t string) bool {
schedulers := o.GetScheduleConfig().Schedulers
for _, s := range schedulers {
if t == s.Type {
return s.Disable
}
}
return false
}
// GetHotRegionsWriteInterval gets interval for PD to store Hot Region information.
func (o *PersistOptions) GetHotRegionsWriteInterval() time.Duration {
return o.GetScheduleConfig().HotRegionsWriteInterval.Duration
}
// GetHotRegionsReservedDays gets days hot region information is kept.
func (o *PersistOptions) GetHotRegionsReservedDays() uint64 {
return o.GetScheduleConfig().HotRegionsReservedDays
}
// AddSchedulerCfg adds the scheduler configurations.
func (o *PersistOptions) AddSchedulerCfg(tp string, args []string) {
v := o.GetScheduleConfig().Clone()
for i, schedulerCfg := range v.Schedulers {
// comparing args is to cover the case that there are schedulers in same type but not with same name
// such as two schedulers of type "evict-leader",
// one name is "evict-leader-scheduler-1" and the other is "evict-leader-scheduler-2"
if reflect.DeepEqual(schedulerCfg, sc.SchedulerConfig{Type: tp, Args: args, Disable: false}) {
return
}
if reflect.DeepEqual(schedulerCfg, sc.SchedulerConfig{Type: tp, Args: args, Disable: true}) {
schedulerCfg.Disable = false
v.Schedulers[i] = schedulerCfg
o.SetScheduleConfig(v)
return
}
}
v.Schedulers = append(v.Schedulers, sc.SchedulerConfig{Type: tp, Args: args, Disable: false})
o.SetScheduleConfig(v)
}
// RemoveSchedulerCfg removes the scheduler configurations.
func (o *PersistOptions) RemoveSchedulerCfg(tp string) {
v := o.GetScheduleConfig().Clone()
for i, schedulerCfg := range v.Schedulers {
if tp == schedulerCfg.Type {
if sc.IsDefaultScheduler(tp) {
schedulerCfg.Disable = true
v.Schedulers[i] = schedulerCfg
} else {
v.Schedulers = append(v.Schedulers[:i], v.Schedulers[i+1:]...)
}
o.SetScheduleConfig(v)
return
}
}
}
// SetLabelProperty sets the label property.
func (o *PersistOptions) SetLabelProperty(typ, labelKey, labelValue string) {
cfg := o.GetLabelPropertyConfig().Clone()
for _, l := range cfg[typ] {
if l.Key == labelKey && l.Value == labelValue {
return
}
}
cfg[typ] = append(cfg[typ], StoreLabel{Key: labelKey, Value: labelValue})
o.labelProperty.Store(cfg)
}
// DeleteLabelProperty deletes the label property.
func (o *PersistOptions) DeleteLabelProperty(typ, labelKey, labelValue string) {
cfg := o.GetLabelPropertyConfig().Clone()
oldLabels := cfg[typ]
cfg[typ] = []StoreLabel{}
for _, l := range oldLabels {
if l.Key == labelKey && l.Value == labelValue {
continue
}
cfg[typ] = append(cfg[typ], l)
}
if len(cfg[typ]) == 0 {
delete(cfg, typ)
}
o.labelProperty.Store(cfg)
}
// persistedConfig is used to merge all configs into one before saving to storage.
type persistedConfig struct {
*Config
// StoreConfig is injected into Config to avoid breaking the original API.
StoreConfig sc.StoreConfig `json:"store"`
}
// SwitchRaftV2 update some config if tikv raft engine switch into partition raft v2
func (o *PersistOptions) SwitchRaftV2(storage endpoint.ConfigStorage) error {
o.GetScheduleConfig().StoreLimitVersion = "v2"
return o.Persist(storage)
}
// Persist saves the configuration to the storage.
func (o *PersistOptions) Persist(storage endpoint.ConfigStorage) error {
cfg := &persistedConfig{
Config: &Config{
Schedule: *o.GetScheduleConfig(),
Replication: *o.GetReplicationConfig(),
PDServerCfg: *o.GetPDServerConfig(),
ReplicationMode: *o.GetReplicationModeConfig(),
LabelProperty: o.GetLabelPropertyConfig(),
Keyspace: *o.GetKeyspaceConfig(),
MicroService: *o.GetMicroServiceConfig(),
ClusterVersion: *o.GetClusterVersion(),
},
StoreConfig: *o.GetStoreConfig(),
}
failpoint.Inject("persistFail", func() {
failpoint.Return(errors.New("fail to persist"))
})
return storage.SaveConfig(cfg)
}
// Reload reloads the configuration from the storage.
func (o *PersistOptions) Reload(storage endpoint.ConfigStorage) error {
cfg := &persistedConfig{Config: &Config{}}
// Pass nil to initialize cfg to default values (all items undefined)
cfg.Adjust(nil, true)
isExist, err := storage.LoadConfig(cfg)
if err != nil {
return err
}
adjustScheduleCfg(&cfg.Schedule)
// Some fields may not be stored in the storage, we need to calculate them manually.
cfg.StoreConfig.Adjust()
cfg.PDServerCfg.MigrateDeprecatedFlags()
if isExist {
o.schedule.Store(&cfg.Schedule)
o.replication.Store(&cfg.Replication)
o.pdServerConfig.Store(&cfg.PDServerCfg)
o.replicationMode.Store(&cfg.ReplicationMode)
o.labelProperty.Store(cfg.LabelProperty)
o.keyspace.Store(&cfg.Keyspace)
o.microService.Store(&cfg.MicroService)
o.storeConfig.Store(&cfg.StoreConfig)
o.SetClusterVersion(&cfg.ClusterVersion)
}
return nil
}
func adjustScheduleCfg(scheduleCfg *sc.ScheduleConfig) {
// In case we add new default schedulers.
for _, ps := range sc.DefaultSchedulers {
if slice.NoneOf(scheduleCfg.Schedulers, func(i int) bool {
return scheduleCfg.Schedulers[i].Type == ps.Type
}) {
scheduleCfg.Schedulers = append(scheduleCfg.Schedulers, ps)
}
}
scheduleCfg.MigrateDeprecatedFlags()
}
// CheckLabelProperty checks the label property.
func (o *PersistOptions) CheckLabelProperty(typ string, labels []*metapb.StoreLabel) bool {
pc := o.labelProperty.Load().(LabelPropertyConfig)
for _, cfg := range pc[typ] {
for _, l := range labels {
if l.Key == cfg.Key && l.Value == cfg.Value {
return true
}
}
}
return false
}
// GetMinResolvedTSPersistenceInterval gets the interval for PD to save min resolved ts.
func (o *PersistOptions) GetMinResolvedTSPersistenceInterval() time.Duration {
return o.GetPDServerConfig().MinResolvedTSPersistenceInterval.Duration
}
// SetTTLData set temporary configuration
func (o *PersistOptions) SetTTLData(parCtx context.Context, client *clientv3.Client, key string, value string, ttl time.Duration) error {
if o.ttl == nil {
o.ttl = cache.NewStringTTL(parCtx, sc.DefaultGCInterval, sc.DefaultTTL)
}
if ttl != 0 {
// the minimum ttl is 5 seconds, if the given ttl is less than 5 seconds, we will use 5 seconds instead.
_, err := etcdutil.EtcdKVPutWithTTL(parCtx, client, sc.TTLConfigPrefix+"/"+key, value, int64(ttl.Seconds()))
if err != nil {
return err
}
} else {
_, err := client.Delete(parCtx, sc.TTLConfigPrefix+"/"+key)
if err != nil {
return err
}
}
o.ttl.PutWithTTL(key, value, ttl)
return nil
}
// getTTLNumber try to parse uint64 from ttl storage first, if failed, try to parse float64
func (o *PersistOptions) getTTLNumber(key string) (uint64, bool, error) {
stringForm, ok := o.GetTTLData(key)
if !ok {
return 0, false, nil
}
r, err := strconv.ParseUint(stringForm, 10, 64)
if err == nil {
return r, true, nil
}
// try to parse float64
// json unmarshal will convert number(such as `uint64(math.MaxInt32)`) to float64
f, err := strconv.ParseFloat(stringForm, 64)
if err != nil {
return 0, false, err
}
return uint64(f), true, nil
}
// getTTLNumberOr try to parse uint64 from ttl storage first, if failed, try to parse float64.
// If both failed, return defaultValue.
func (o *PersistOptions) getTTLNumberOr(key string, defaultValue uint64) uint64 {
if v, ok, err := o.getTTLNumber(key); ok {
if err == nil {
return v
}
log.Warn("failed to parse "+key+" from PersistOptions's ttl storage", zap.Error(err))
}
return defaultValue
}
func (o *PersistOptions) getTTLBool(key string) (result bool, contains bool, err error) {
stringForm, ok := o.GetTTLData(key)
if !ok {
return
}
result, err = strconv.ParseBool(stringForm)
contains = true
return
}
func (o *PersistOptions) getTTLBoolOr(key string, defaultValue bool) bool {
if v, ok, err := o.getTTLBool(key); ok {
if err == nil {
return v
}
log.Warn("failed to parse "+key+" from PersistOptions's ttl storage", zap.Error(err))
}
return defaultValue
}
func (o *PersistOptions) getTTLFloat(key string) (float64, bool, error) {
stringForm, ok := o.GetTTLData(key)
if !ok {
return 0, false, nil
}
r, err := strconv.ParseFloat(stringForm, 64)
return r, true, err
}
func (o *PersistOptions) getTTLFloatOr(key string, defaultValue float64) float64 {
if v, ok, err := o.getTTLFloat(key); ok {
if err == nil {
return v
}
log.Warn("failed to parse "+key+" from PersistOptions's ttl storage", zap.Error(err))
}
return defaultValue
}
// GetTTLData returns if there is a TTL data for a given key.
func (o *PersistOptions) GetTTLData(key string) (string, bool) {
if o.ttl == nil {
return "", false
}
if result, ok := o.ttl.Get(key); ok {
return result.(string), ok
}
return "", false
}
// LoadTTLFromEtcd loads temporary configuration which was persisted into etcd
func (o *PersistOptions) LoadTTLFromEtcd(ctx context.Context, client *clientv3.Client) error {
resps, err := etcdutil.EtcdKVGet(client, sc.TTLConfigPrefix, clientv3.WithPrefix())
if err != nil {
return err
}
if o.ttl == nil {
o.ttl = cache.NewStringTTL(ctx, sc.DefaultGCInterval, sc.DefaultTTL)
}
for _, resp := range resps.Kvs {
key := string(resp.Key)[len(sc.TTLConfigPrefix)+1:]
value := string(resp.Value)
leaseID := resp.Lease
resp, err := client.TimeToLive(ctx, clientv3.LeaseID(leaseID))
if err != nil {
return err
}
o.ttl.PutWithTTL(key, value, time.Duration(resp.TTL)*time.Second)
}
return nil
}
// SetAllStoresLimitTTL sets all store limit for a given type and rate with ttl.
func (o *PersistOptions) SetAllStoresLimitTTL(ctx context.Context, client *clientv3.Client, typ storelimit.Type, ratePerMin float64, ttl time.Duration) error {
var err error
switch typ {
case storelimit.AddPeer:
err = o.SetTTLData(ctx, client, "default-add-peer", fmt.Sprint(ratePerMin), ttl)
case storelimit.RemovePeer:
err = o.SetTTLData(ctx, client, "default-remove-peer", fmt.Sprint(ratePerMin), ttl)
}
return err
}
var haltSchedulingStatus = schedulingAllowanceStatusGauge.WithLabelValues("halt-scheduling")
// SetSchedulingAllowanceStatus sets the scheduling allowance status to help distinguish the source of the halt.
func (*PersistOptions) SetSchedulingAllowanceStatus(halt bool, source string) {
if halt {
haltSchedulingStatus.Set(1)
schedulingAllowanceStatusGauge.WithLabelValues(source).Set(1)
} else {
haltSchedulingStatus.Set(0)
schedulingAllowanceStatusGauge.WithLabelValues(source).Set(0)
}
}