forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.go
99 lines (89 loc) · 3.3 KB
/
label.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
// Copyright 2018 PingCAP, Inc.
//
// 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 schedulers
import (
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/schedule"
log "github.com/sirupsen/logrus"
)
func init() {
schedule.RegisterScheduler("label", func(limiter *schedule.Limiter, args []string) (schedule.Scheduler, error) {
return newLabelScheduler(limiter), nil
})
}
type labelScheduler struct {
*baseScheduler
selector schedule.Selector
}
func newLabelScheduler(limiter *schedule.Limiter) schedule.Scheduler {
filters := []schedule.Filter{
schedule.NewBlockFilter(),
schedule.NewStateFilter(),
schedule.NewHealthFilter(),
schedule.NewDisconnectFilter(),
schedule.NewRejectLeaderFilter(),
}
return &labelScheduler{
baseScheduler: newBaseScheduler(limiter),
selector: schedule.NewBalanceSelector(core.LeaderKind, filters),
}
}
func (s *labelScheduler) GetName() string {
return "label-scheduler"
}
func (s *labelScheduler) GetType() string {
return "label"
}
func (s *labelScheduler) IsScheduleAllowed(cluster schedule.Cluster) bool {
return s.limiter.OperatorCount(schedule.OpLeader) < cluster.GetLeaderScheduleLimit()
}
func (s *labelScheduler) Schedule(cluster schedule.Cluster, opInfluence schedule.OpInfluence) []*schedule.Operator {
schedulerCounter.WithLabelValues(s.GetName(), "schedule").Inc()
stores := cluster.GetStores()
rejectLeaderStores := make(map[uint64]struct{})
for _, s := range stores {
if cluster.CheckLabelProperty(schedule.RejectLeader, s.Labels) {
rejectLeaderStores[s.GetId()] = struct{}{}
}
}
if len(rejectLeaderStores) == 0 {
schedulerCounter.WithLabelValues(s.GetName(), "skip").Inc()
return nil
}
log.Debugf("label scheduler reject leader store list: %v", rejectLeaderStores)
for id := range rejectLeaderStores {
if region := cluster.RandLeaderRegion(id); region != nil {
log.Debugf("label scheduler selects region %d to transfer leader", region.GetId())
excludeStores := make(map[uint64]struct{})
for _, p := range region.DownPeers {
excludeStores[p.GetPeer().GetStoreId()] = struct{}{}
}
for _, p := range region.PendingPeers {
excludeStores[p.GetStoreId()] = struct{}{}
}
filter := schedule.NewExcludedFilter(nil, excludeStores)
target := s.selector.SelectTarget(cluster, cluster.GetFollowerStores(region), filter)
if target == nil {
log.Debugf("label scheduler no target found for region %d", region.GetId())
schedulerCounter.WithLabelValues(s.GetName(), "no_target").Inc()
continue
}
schedulerCounter.WithLabelValues(s.GetName(), "new_operator").Inc()
step := schedule.TransferLeader{FromStore: id, ToStore: target.GetId()}
op := schedule.NewOperator("label-reject-leader", region.GetId(), region.GetRegionEpoch(), schedule.OpLeader, step)
return []*schedule.Operator{op}
}
}
schedulerCounter.WithLabelValues(s.GetName(), "no_region").Inc()
return nil
}