-
Notifications
You must be signed in to change notification settings - Fork 0
/
balance_region.go
141 lines (117 loc) · 4.46 KB
/
balance_region.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
// Copyright 2017 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 (
"time"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/server/cache"
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/schedule"
)
func init() {
schedule.RegisterScheduler("balance-region", func(opt schedule.Options, args []string) (schedule.Scheduler, error) {
return newBalanceRegionScheduler(opt), nil
})
}
const storeCacheInterval = 30 * time.Second
type balanceRegionScheduler struct {
opt schedule.Options
cache *cache.TTLUint64
limit uint64
selector schedule.Selector
}
// newBalanceRegionScheduler creates a scheduler that tends to keep regions on
// each store balanced.
func newBalanceRegionScheduler(opt schedule.Options) schedule.Scheduler {
ttlCache := cache.NewIDTTL(storeCacheInterval, 4*storeCacheInterval)
filters := []schedule.Filter{
schedule.NewCacheFilter(ttlCache),
schedule.NewStateFilter(opt),
schedule.NewHealthFilter(opt),
schedule.NewSnapshotCountFilter(opt),
schedule.NewStorageThresholdFilter(opt),
}
return &balanceRegionScheduler{
opt: opt,
cache: ttlCache,
limit: 1,
selector: schedule.NewBalanceSelector(core.RegionKind, filters),
}
}
func (s *balanceRegionScheduler) GetName() string {
return "balance-region-scheduler"
}
func (s *balanceRegionScheduler) GetType() string {
return "balance-region"
}
func (s *balanceRegionScheduler) GetInterval() time.Duration {
return schedule.MinScheduleInterval
}
func (s *balanceRegionScheduler) GetResourceKind() core.ResourceKind {
return core.RegionKind
}
func (s *balanceRegionScheduler) GetResourceLimit() uint64 {
return minUint64(s.limit, s.opt.GetRegionScheduleLimit())
}
func (s *balanceRegionScheduler) Prepare(cluster schedule.Cluster) error { return nil }
func (s *balanceRegionScheduler) Cleanup(cluster schedule.Cluster) {}
func (s *balanceRegionScheduler) Schedule(cluster schedule.Cluster) *schedule.Operator {
schedulerCounter.WithLabelValues(s.GetName(), "schedule").Inc()
// Select a peer from the store with most regions.
region, oldPeer := scheduleRemovePeer(cluster, s.GetName(), s.selector)
if region == nil {
return nil
}
// We don't schedule region with abnormal number of replicas.
if len(region.GetPeers()) != s.opt.GetMaxReplicas() {
schedulerCounter.WithLabelValues(s.GetName(), "abnormal_replica").Inc()
return nil
}
// Skip hot regions.
if cluster.IsRegionHot(region.GetId()) {
schedulerCounter.WithLabelValues(s.GetName(), "region_hot").Inc()
return nil
}
op := s.transferPeer(cluster, region, oldPeer)
if op == nil {
// We can't transfer peer from this store now, so we add it to the cache
// and skip it for a while.
s.cache.Put(oldPeer.GetStoreId())
}
schedulerCounter.WithLabelValues(s.GetName(), "new_operator").Inc()
return op
}
func (s *balanceRegionScheduler) transferPeer(cluster schedule.Cluster, region *core.RegionInfo, oldPeer *metapb.Peer) *schedule.Operator {
// scoreGuard guarantees that the distinct score will not decrease.
stores := cluster.GetRegionStores(region)
source := cluster.GetStore(oldPeer.GetStoreId())
scoreGuard := schedule.NewDistinctScoreFilter(s.opt.GetLocationLabels(), stores, source)
checker := schedule.NewReplicaChecker(s.opt, cluster, nil)
newPeer := checker.SelectBestPeerToAddReplica(region, scoreGuard)
if newPeer == nil {
schedulerCounter.WithLabelValues(s.GetName(), "no_peer").Inc()
return nil
}
target := cluster.GetStore(newPeer.GetStoreId())
if !shouldBalance(source, target, s.GetResourceKind()) {
schedulerCounter.WithLabelValues(s.GetName(), "skip").Inc()
return nil
}
s.limit = adjustBalanceLimit(cluster, s.GetResourceKind())
return schedule.CreateMovePeerOperator("balance-region", region, core.RegionKind, oldPeer.GetStoreId(), newPeer.GetStoreId(), newPeer.GetId())
}
// GetCache returns interval id cache in the scheduler. This is for test only.
// TODO: remove it after moving tests into this directory.
func (s *balanceRegionScheduler) GetCache() *cache.TTLUint64 {
return s.cache
}