-
Notifications
You must be signed in to change notification settings - Fork 720
/
coordinator.go
858 lines (778 loc) · 26 KB
/
coordinator.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
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package cluster
import (
"bytes"
"context"
"fmt"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/logutil"
"github.com/tikv/pd/server/config"
"github.com/tikv/pd/server/core"
"github.com/tikv/pd/server/kv"
"github.com/tikv/pd/server/schedule"
"github.com/tikv/pd/server/schedule/hbstream"
"github.com/tikv/pd/server/schedule/operator"
"github.com/tikv/pd/server/schedulers"
"github.com/tikv/pd/server/statistics"
"go.uber.org/zap"
)
const (
runSchedulerCheckInterval = 3 * time.Second
collectFactor = 0.9
collectTimeout = 5 * time.Minute
maxScheduleRetries = 10
maxLoadConfigRetries = 10
patrolScanRegionLimit = 128 // It takes about 14 minutes to iterate 1 million regions.
// PluginLoad means action for load plugin
PluginLoad = "PluginLoad"
// PluginUnload means action for unload plugin
PluginUnload = "PluginUnload"
)
// coordinator is used to manage all schedulers and checkers to decide if the region needs to be scheduled.
type coordinator struct {
sync.RWMutex
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
cluster *RaftCluster
checkers *schedule.CheckerController
regionScatterer *schedule.RegionScatterer
regionSplitter *schedule.RegionSplitter
schedulers map[string]*scheduleController
opController *schedule.OperatorController
hbStreams *hbstream.HeartbeatStreams
pluginInterface *schedule.PluginInterface
}
// newCoordinator creates a new coordinator.
func newCoordinator(ctx context.Context, cluster *RaftCluster, hbStreams *hbstream.HeartbeatStreams) *coordinator {
ctx, cancel := context.WithCancel(ctx)
opController := schedule.NewOperatorController(ctx, cluster, hbStreams)
return &coordinator{
ctx: ctx,
cancel: cancel,
cluster: cluster,
checkers: schedule.NewCheckerController(ctx, cluster, cluster.ruleManager, opController),
regionScatterer: schedule.NewRegionScatterer(ctx, cluster),
regionSplitter: schedule.NewRegionSplitter(cluster, schedule.NewSplitRegionsHandler(cluster, opController)),
schedulers: make(map[string]*scheduleController),
opController: opController,
hbStreams: hbStreams,
pluginInterface: schedule.NewPluginInterface(),
}
}
// patrolRegions is used to scan regions.
// The checkers will check these regions to decide if they need to do some operations.
func (c *coordinator) patrolRegions() {
defer logutil.LogPanic()
defer c.wg.Done()
timer := time.NewTimer(c.cluster.GetOpts().GetPatrolRegionInterval())
defer timer.Stop()
log.Info("coordinator starts patrol regions")
start := time.Now()
var key []byte
for {
select {
case <-timer.C:
timer.Reset(c.cluster.GetOpts().GetPatrolRegionInterval())
case <-c.ctx.Done():
log.Info("patrol regions has been stopped")
return
}
// Check priority regions first.
c.checkPriorityRegions()
// Check suspect regions first.
c.checkSuspectRegions()
// Check suspect key ranges
c.checkSuspectKeyRanges()
// Check regions in the waiting list
c.checkWaitingRegions()
regions := c.cluster.ScanRegions(key, nil, patrolScanRegionLimit)
if len(regions) == 0 {
// Resets the scan key.
key = nil
continue
}
for _, region := range regions {
// Skips the region if there is already a pending operator.
if c.opController.GetOperator(region.GetID()) != nil {
continue
}
ops := c.checkers.CheckRegion(region)
key = region.GetEndKey()
if len(ops) == 0 {
continue
}
if !c.opController.ExceedStoreLimit(ops...) {
c.opController.AddWaitingOperator(ops...)
c.checkers.RemoveWaitingRegion(region.GetID())
c.cluster.RemoveSuspectRegion(region.GetID())
} else {
c.checkers.AddWaitingRegion(region)
}
}
// Updates the label level isolation statistics.
c.cluster.updateRegionsLabelLevelStats(regions)
if len(key) == 0 {
patrolCheckRegionsGauge.Set(time.Since(start).Seconds())
start = time.Now()
}
failpoint.Inject("break-patrol", func() {
failpoint.Break()
})
}
}
// checkPriorityRegions checks priority regions
func (c *coordinator) checkPriorityRegions() {
items := c.checkers.GetPriorityRegions()
removes := make([]uint64, 0)
regionListGauge.WithLabelValues("priority_list").Set(float64(len(items)))
for _, id := range items {
region := c.cluster.GetRegion(id)
if region == nil {
removes = append(removes, id)
continue
}
ops := c.checkers.CheckRegion(region)
// it should skip if region needs to merge
if len(ops) == 0 || ops[0].Kind()&operator.OpMerge != 0 {
continue
}
if !c.opController.ExceedStoreLimit(ops...) {
c.opController.AddWaitingOperator(ops...)
}
}
for _, v := range removes {
c.checkers.RemovePriorityRegions(v)
}
}
func (c *coordinator) checkSuspectRegions() {
for _, id := range c.cluster.GetSuspectRegions() {
region := c.cluster.GetRegion(id)
if region == nil {
// the region could be recent split, continue to wait.
continue
}
if c.opController.GetOperator(id) != nil {
c.cluster.RemoveSuspectRegion(id)
continue
}
ops := c.checkers.CheckRegion(region)
if len(ops) == 0 {
continue
}
if !c.opController.ExceedStoreLimit(ops...) {
c.opController.AddWaitingOperator(ops...)
c.cluster.RemoveSuspectRegion(region.GetID())
}
}
}
// checkSuspectKeyRanges would pop one suspect key range group
// The regions of new version key range and old version key range would be placed into
// the suspect regions map
func (c *coordinator) checkSuspectKeyRanges() {
keyRange, success := c.cluster.PopOneSuspectKeyRange()
if !success {
return
}
limit := 1024
regions := c.cluster.ScanRegions(keyRange[0], keyRange[1], limit)
if len(regions) == 0 {
return
}
regionIDList := make([]uint64, 0, len(regions))
for _, region := range regions {
regionIDList = append(regionIDList, region.GetID())
}
// if the last region's end key is smaller the keyRange[1] which means there existed the remaining regions between
// keyRange[0] and keyRange[1] after scan regions, so we put the end key and keyRange[1] into Suspect KeyRanges
lastRegion := regions[len(regions)-1]
if lastRegion.GetEndKey() != nil && bytes.Compare(lastRegion.GetEndKey(), keyRange[1]) < 0 {
c.cluster.AddSuspectKeyRange(lastRegion.GetEndKey(), keyRange[1])
}
c.cluster.AddSuspectRegions(regionIDList...)
}
func (c *coordinator) checkWaitingRegions() {
items := c.checkers.GetWaitingRegions()
regionListGauge.WithLabelValues("waiting_list").Set(float64(len(items)))
for _, item := range items {
id := item.Key
region := c.cluster.GetRegion(id)
if region == nil {
// the region could be recent split, continue to wait.
continue
}
if c.opController.GetOperator(id) != nil {
c.checkers.RemoveWaitingRegion(id)
continue
}
ops := c.checkers.CheckRegion(region)
if len(ops) == 0 {
continue
}
if !c.opController.ExceedStoreLimit(ops...) {
c.opController.AddWaitingOperator(ops...)
c.checkers.RemoveWaitingRegion(region.GetID())
}
}
}
// drivePushOperator is used to push the unfinished operator to the executor.
func (c *coordinator) drivePushOperator() {
defer logutil.LogPanic()
defer c.wg.Done()
log.Info("coordinator begins to actively drive push operator")
ticker := time.NewTicker(schedule.PushOperatorTickInterval)
defer ticker.Stop()
for {
select {
case <-c.ctx.Done():
log.Info("drive push operator has been stopped")
return
case <-ticker.C:
c.opController.PushOperators()
}
}
}
func (c *coordinator) run() {
ticker := time.NewTicker(runSchedulerCheckInterval)
failpoint.Inject("changeCoordinatorTicker", func() {
ticker = time.NewTicker(100 * time.Millisecond)
})
defer ticker.Stop()
log.Info("coordinator starts to collect cluster information")
for {
if c.shouldRun() {
log.Info("coordinator has finished cluster information preparation")
break
}
select {
case <-ticker.C:
case <-c.ctx.Done():
log.Info("coordinator stops running")
return
}
}
log.Info("coordinator starts to run schedulers")
var (
scheduleNames []string
configs []string
err error
)
for i := 0; i < maxLoadConfigRetries; i++ {
scheduleNames, configs, err = c.cluster.storage.LoadAllScheduleConfig()
select {
case <-c.ctx.Done():
log.Info("coordinator stops running")
return
default:
}
if err == nil {
break
}
log.Error("cannot load schedulers' config", zap.Int("retry-times", i), errs.ZapError(err))
}
if err != nil {
log.Fatal("cannot load schedulers' config", errs.ZapError(err))
}
scheduleCfg := c.cluster.opt.GetScheduleConfig().Clone()
// The new way to create scheduler with the independent configuration.
for i, name := range scheduleNames {
data := configs[i]
typ := schedule.FindSchedulerTypeByName(name)
var cfg config.SchedulerConfig
for _, c := range scheduleCfg.Schedulers {
if c.Type == typ {
cfg = c
break
}
}
if len(cfg.Type) == 0 {
log.Error("the scheduler type not found", zap.String("scheduler-name", name), errs.ZapError(errs.ErrSchedulerNotFound))
continue
}
if cfg.Disable {
log.Info("skip create scheduler with independent configuration", zap.String("scheduler-name", name), zap.String("scheduler-type", cfg.Type), zap.Strings("scheduler-args", cfg.Args))
continue
}
s, err := schedule.CreateScheduler(cfg.Type, c.opController, c.cluster.storage, schedule.ConfigJSONDecoder([]byte(data)))
if err != nil {
log.Error("can not create scheduler with independent configuration", zap.String("scheduler-name", name), zap.Strings("scheduler-args", cfg.Args), errs.ZapError(err))
continue
}
log.Info("create scheduler with independent configuration", zap.String("scheduler-name", s.GetName()))
if err = c.addScheduler(s); err != nil {
log.Error("can not add scheduler with independent configuration", zap.String("scheduler-name", s.GetName()), zap.Strings("scheduler-args", cfg.Args), errs.ZapError(err))
}
}
// The old way to create the scheduler.
k := 0
for _, schedulerCfg := range scheduleCfg.Schedulers {
if schedulerCfg.Disable {
scheduleCfg.Schedulers[k] = schedulerCfg
k++
log.Info("skip create scheduler", zap.String("scheduler-type", schedulerCfg.Type), zap.Strings("scheduler-args", schedulerCfg.Args))
continue
}
s, err := schedule.CreateScheduler(schedulerCfg.Type, c.opController, c.cluster.storage, schedule.ConfigSliceDecoder(schedulerCfg.Type, schedulerCfg.Args))
if err != nil {
log.Error("can not create scheduler", zap.String("scheduler-type", schedulerCfg.Type), zap.Strings("scheduler-args", schedulerCfg.Args), errs.ZapError(err))
continue
}
log.Info("create scheduler", zap.String("scheduler-name", s.GetName()), zap.Strings("scheduler-args", schedulerCfg.Args))
if err = c.addScheduler(s, schedulerCfg.Args...); err != nil && !errors.ErrorEqual(err, errs.ErrSchedulerExisted.FastGenByArgs()) {
log.Error("can not add scheduler", zap.String("scheduler-name", s.GetName()), zap.Strings("scheduler-args", schedulerCfg.Args), errs.ZapError(err))
} else {
// Only records the valid scheduler config.
scheduleCfg.Schedulers[k] = schedulerCfg
k++
}
}
// Removes the invalid scheduler config and persist.
scheduleCfg.Schedulers = scheduleCfg.Schedulers[:k]
c.cluster.opt.SetScheduleConfig(scheduleCfg)
if err := c.cluster.opt.Persist(c.cluster.storage); err != nil {
log.Error("cannot persist schedule config", errs.ZapError(err))
}
c.wg.Add(2)
// Starts to patrol regions.
go c.patrolRegions()
go c.drivePushOperator()
}
// LoadPlugin load user plugin
func (c *coordinator) LoadPlugin(pluginPath string, ch chan string) {
log.Info("load plugin", zap.String("plugin-path", pluginPath))
// get func: SchedulerType from plugin
SchedulerType, err := c.pluginInterface.GetFunction(pluginPath, "SchedulerType")
if err != nil {
log.Error("GetFunction SchedulerType error", errs.ZapError(err))
return
}
schedulerType := SchedulerType.(func() string)
// get func: SchedulerArgs from plugin
SchedulerArgs, err := c.pluginInterface.GetFunction(pluginPath, "SchedulerArgs")
if err != nil {
log.Error("GetFunction SchedulerArgs error", errs.ZapError(err))
return
}
schedulerArgs := SchedulerArgs.(func() []string)
// create and add user scheduler
s, err := schedule.CreateScheduler(schedulerType(), c.opController, c.cluster.storage, schedule.ConfigSliceDecoder(schedulerType(), schedulerArgs()))
if err != nil {
log.Error("can not create scheduler", zap.String("scheduler-type", schedulerType()), errs.ZapError(err))
return
}
log.Info("create scheduler", zap.String("scheduler-name", s.GetName()))
if err = c.addScheduler(s); err != nil {
log.Error("can't add scheduler", zap.String("scheduler-name", s.GetName()), errs.ZapError(err))
return
}
c.wg.Add(1)
go c.waitPluginUnload(pluginPath, s.GetName(), ch)
}
func (c *coordinator) waitPluginUnload(pluginPath, schedulerName string, ch chan string) {
defer logutil.LogPanic()
defer c.wg.Done()
// Get signal from channel which means user unload the plugin
for {
select {
case action := <-ch:
if action == PluginUnload {
err := c.removeScheduler(schedulerName)
if err != nil {
log.Error("can not remove scheduler", zap.String("scheduler-name", schedulerName), errs.ZapError(err))
} else {
log.Info("unload plugin", zap.String("plugin", pluginPath))
return
}
} else {
log.Error("unknown action", zap.String("action", action))
}
case <-c.ctx.Done():
log.Info("unload plugin has been stopped")
return
}
}
}
func (c *coordinator) stop() {
c.cancel()
}
// Hack to retrieve info from scheduler.
// TODO: remove it.
type hasHotStatus interface {
GetHotStatus(typ string) *statistics.StoreHotPeersInfos
GetPendingInfluence() map[uint64]*schedulers.Influence
}
func (c *coordinator) getHotWriteRegions() *statistics.StoreHotPeersInfos {
c.RLock()
defer c.RUnlock()
s, ok := c.schedulers[schedulers.HotRegionName]
if !ok {
return nil
}
if h, ok := s.Scheduler.(hasHotStatus); ok {
return h.GetHotStatus(schedulers.HotWriteRegionType)
}
return nil
}
func (c *coordinator) getHotReadRegions() *statistics.StoreHotPeersInfos {
c.RLock()
defer c.RUnlock()
s, ok := c.schedulers[schedulers.HotRegionName]
if !ok {
return nil
}
if h, ok := s.Scheduler.(hasHotStatus); ok {
return h.GetHotStatus(schedulers.HotReadRegionType)
}
return nil
}
func (c *coordinator) getSchedulers() []string {
c.RLock()
defer c.RUnlock()
names := make([]string, 0, len(c.schedulers))
for name := range c.schedulers {
names = append(names, name)
}
return names
}
func (c *coordinator) getSchedulerHandlers() map[string]http.Handler {
c.RLock()
defer c.RUnlock()
handlers := make(map[string]http.Handler, len(c.schedulers))
for name, scheduler := range c.schedulers {
handlers[name] = scheduler.Scheduler
}
return handlers
}
func (c *coordinator) collectSchedulerMetrics() {
c.RLock()
defer c.RUnlock()
for _, s := range c.schedulers {
var allowScheduler float64
// If the scheduler is not allowed to schedule, it will disappear in Grafana panel.
// See issue #1341.
if !s.IsPaused() {
allowScheduler = 1
}
schedulerStatusGauge.WithLabelValues(s.GetName(), "allow").Set(allowScheduler)
}
}
func (c *coordinator) resetSchedulerMetrics() {
schedulerStatusGauge.Reset()
}
func (c *coordinator) collectHotSpotMetrics() {
c.RLock()
// Collects hot write region metrics.
s, ok := c.schedulers[schedulers.HotRegionName]
if !ok {
c.RUnlock()
return
}
c.RUnlock()
stores := c.cluster.GetStores()
// Collects hot write region metrics.
collectHotMetrics(s, stores, schedulers.HotWriteRegionType)
// Collects hot read region metrics.
collectHotMetrics(s, stores, schedulers.HotReadRegionType)
// Collects pending influence.
collectPendingInfluence(s, stores)
}
func collectHotMetrics(s *scheduleController, stores []*core.StoreInfo, typ string) {
status := s.Scheduler.(hasHotStatus).GetHotStatus(typ)
var (
kind string
byteTyp, keyTyp, queryTyp statistics.RegionStatKind
)
switch typ {
case schedulers.HotReadRegionType:
kind, byteTyp, keyTyp, queryTyp = "read", statistics.RegionReadBytes, statistics.RegionReadKeys, statistics.RegionReadQuery
case schedulers.HotWriteRegionType:
kind, byteTyp, keyTyp, queryTyp = "write", statistics.RegionWriteBytes, statistics.RegionWriteKeys, statistics.RegionWriteQuery
}
for _, s := range stores {
storeAddress := s.GetAddress()
storeID := s.GetID()
storeLabel := fmt.Sprintf("%d", storeID)
stat, ok := status.AsLeader[storeID]
if ok {
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "total_"+kind+"_bytes_as_leader").Set(stat.TotalLoads[byteTyp])
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "total_"+kind+"_keys_as_leader").Set(stat.TotalLoads[keyTyp])
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "total_"+kind+"_query_as_leader").Set(stat.TotalLoads[queryTyp])
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "hot_"+kind+"_region_as_leader").Set(float64(stat.Count))
} else {
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "total_"+kind+"_bytes_as_leader").Set(0)
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "total_"+kind+"_keys_as_leader").Set(0)
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "total_"+kind+"_query_as_leader").Set(0)
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "hot_"+kind+"_region_as_leader").Set(0)
}
stat, ok = status.AsPeer[storeID]
if ok {
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "total_"+kind+"_bytes_as_peer").Set(stat.TotalLoads[byteTyp])
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "total_"+kind+"_keys_as_peer").Set(stat.TotalLoads[keyTyp])
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "total_"+kind+"_query_as_peer").Set(stat.TotalLoads[queryTyp])
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "hot_"+kind+"_region_as_peer").Set(float64(stat.Count))
} else {
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "total_"+kind+"_bytes_as_peer").Set(0)
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "total_"+kind+"_keys_as_peer").Set(0)
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "total_"+kind+"_query_as_peer").Set(0)
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "hot_"+kind+"_region_as_peer").Set(0)
}
}
}
func collectPendingInfluence(s *scheduleController, stores []*core.StoreInfo) {
pendings := s.Scheduler.(hasHotStatus).GetPendingInfluence()
for _, s := range stores {
storeAddress := s.GetAddress()
storeID := s.GetID()
storeLabel := fmt.Sprintf("%d", storeID)
if infl := pendings[storeID]; infl != nil {
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "pending_influence_byte_rate").Set(infl.Loads[statistics.ByteDim])
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "pending_influence_key_rate").Set(infl.Loads[statistics.KeyDim])
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "pending_influence_query_rate").Set(infl.Loads[statistics.QueryDim])
hotSpotStatusGauge.WithLabelValues(storeAddress, storeLabel, "pending_influence_count").Set(infl.Count)
}
}
}
func (c *coordinator) resetHotSpotMetrics() {
hotSpotStatusGauge.Reset()
}
func (c *coordinator) shouldRun() bool {
return c.cluster.prepareChecker.check(c.cluster)
}
func (c *coordinator) addScheduler(scheduler schedule.Scheduler, args ...string) error {
c.Lock()
defer c.Unlock()
if _, ok := c.schedulers[scheduler.GetName()]; ok {
return errs.ErrSchedulerExisted.FastGenByArgs()
}
s := newScheduleController(c, scheduler)
if err := s.Prepare(c.cluster); err != nil {
return err
}
c.wg.Add(1)
go c.runScheduler(s)
c.schedulers[s.GetName()] = s
c.cluster.opt.AddSchedulerCfg(s.GetType(), args)
return nil
}
func (c *coordinator) removeScheduler(name string) error {
c.Lock()
defer c.Unlock()
if c.cluster == nil {
return errs.ErrNotBootstrapped.FastGenByArgs()
}
s, ok := c.schedulers[name]
if !ok {
return errs.ErrSchedulerNotFound.FastGenByArgs()
}
opt := c.cluster.opt
if err := c.removeOptScheduler(opt, name); err != nil {
log.Error("can not remove scheduler", zap.String("scheduler-name", name), errs.ZapError(err))
return err
}
if err := opt.Persist(c.cluster.storage); err != nil {
log.Error("the option can not persist scheduler config", errs.ZapError(err))
return err
}
if err := c.cluster.storage.RemoveScheduleConfig(name); err != nil {
log.Error("can not remove the scheduler config", errs.ZapError(err))
return err
}
s.Stop()
schedulerStatusGauge.WithLabelValues(name, "allow").Set(0)
delete(c.schedulers, name)
return nil
}
func (c *coordinator) removeOptScheduler(o *config.PersistOptions, name string) error {
v := o.GetScheduleConfig().Clone()
for i, schedulerCfg := range v.Schedulers {
// To create a temporary scheduler is just used to get scheduler's name
decoder := schedule.ConfigSliceDecoder(schedulerCfg.Type, schedulerCfg.Args)
tmp, err := schedule.CreateScheduler(schedulerCfg.Type, schedule.NewOperatorController(c.ctx, nil, nil), core.NewStorage(kv.NewMemoryKV()), decoder)
if err != nil {
return err
}
if tmp.GetName() == name {
if config.IsDefaultScheduler(tmp.GetType()) {
schedulerCfg.Disable = true
v.Schedulers[i] = schedulerCfg
} else {
v.Schedulers = append(v.Schedulers[:i], v.Schedulers[i+1:]...)
}
o.SetScheduleConfig(v)
return nil
}
}
return nil
}
func (c *coordinator) pauseOrResumeScheduler(name string, t int64) error {
c.Lock()
defer c.Unlock()
if c.cluster == nil {
return errs.ErrNotBootstrapped.FastGenByArgs()
}
var s []*scheduleController
if name != "all" {
sc, ok := c.schedulers[name]
if !ok {
return errs.ErrSchedulerNotFound.FastGenByArgs()
}
s = append(s, sc)
} else {
for _, sc := range c.schedulers {
s = append(s, sc)
}
}
var err error
for _, sc := range s {
var delayUntil int64
if t > 0 {
delayUntil = time.Now().Unix() + t
}
atomic.StoreInt64(&sc.delayUntil, delayUntil)
}
return err
}
func (c *coordinator) isSchedulerPaused(name string) (bool, error) {
c.RLock()
defer c.RUnlock()
if c.cluster == nil {
return false, errs.ErrNotBootstrapped.FastGenByArgs()
}
s, ok := c.schedulers[name]
if !ok {
return false, errs.ErrSchedulerNotFound.FastGenByArgs()
}
return s.IsPaused(), nil
}
func (c *coordinator) isSchedulerDisabled(name string) (bool, error) {
c.RLock()
defer c.RUnlock()
if c.cluster == nil {
return false, errs.ErrNotBootstrapped.FastGenByArgs()
}
s, ok := c.schedulers[name]
if !ok {
return false, errs.ErrSchedulerNotFound.FastGenByArgs()
}
t := s.GetType()
scheduleConfig := c.cluster.GetOpts().GetScheduleConfig()
for _, s := range scheduleConfig.Schedulers {
if t == s.Type {
return s.Disable, nil
}
}
return false, nil
}
func (c *coordinator) isSchedulerExisted(name string) (bool, error) {
c.RLock()
defer c.RUnlock()
if c.cluster == nil {
return false, errs.ErrNotBootstrapped.FastGenByArgs()
}
_, ok := c.schedulers[name]
if !ok {
return false, errs.ErrSchedulerNotFound.FastGenByArgs()
}
return true, nil
}
func (c *coordinator) runScheduler(s *scheduleController) {
defer logutil.LogPanic()
defer c.wg.Done()
defer s.Cleanup(c.cluster)
timer := time.NewTimer(s.GetInterval())
defer timer.Stop()
for {
select {
case <-timer.C:
timer.Reset(s.GetInterval())
if !s.AllowSchedule() {
continue
}
if op := s.Schedule(); len(op) > 0 {
added := c.opController.AddWaitingOperator(op...)
log.Debug("add operator", zap.Int("added", added), zap.Int("total", len(op)), zap.String("scheduler", s.GetName()))
}
case <-s.Ctx().Done():
log.Info("scheduler has been stopped",
zap.String("scheduler-name", s.GetName()),
errs.ZapError(s.Ctx().Err()))
return
}
}
}
// scheduleController is used to manage a scheduler to schedule.
type scheduleController struct {
schedule.Scheduler
cluster *RaftCluster
opController *schedule.OperatorController
nextInterval time.Duration
ctx context.Context
cancel context.CancelFunc
delayUntil int64
}
// newScheduleController creates a new scheduleController.
func newScheduleController(c *coordinator, s schedule.Scheduler) *scheduleController {
ctx, cancel := context.WithCancel(c.ctx)
return &scheduleController{
Scheduler: s,
cluster: c.cluster,
opController: c.opController,
nextInterval: s.GetMinInterval(),
ctx: ctx,
cancel: cancel,
}
}
func (s *scheduleController) Ctx() context.Context {
return s.ctx
}
func (s *scheduleController) Stop() {
s.cancel()
}
func (s *scheduleController) Schedule() []*operator.Operator {
for i := 0; i < maxScheduleRetries; i++ {
// no need to retry if schedule should stop to speed exit
select {
case <-s.ctx.Done():
return nil
default:
}
// If we have schedule, reset interval to the minimal interval.
if op := s.Scheduler.Schedule(s.cluster); op != nil {
s.nextInterval = s.Scheduler.GetMinInterval()
return op
}
}
s.nextInterval = s.Scheduler.GetNextInterval(s.nextInterval)
return nil
}
// GetInterval returns the interval of scheduling for a scheduler.
func (s *scheduleController) GetInterval() time.Duration {
return s.nextInterval
}
// AllowSchedule returns if a scheduler is allowed to schedule.
func (s *scheduleController) AllowSchedule() bool {
return s.Scheduler.IsScheduleAllowed(s.cluster) && !s.IsPaused()
}
// isPaused returns if a scheduler is paused.
func (s *scheduleController) IsPaused() bool {
delayUntil := atomic.LoadInt64(&s.delayUntil)
return time.Now().Unix() < delayUntil
}