-
Notifications
You must be signed in to change notification settings - Fork 728
/
Copy pathadjacent_region.go
299 lines (268 loc) · 9.42 KB
/
adjacent_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
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
// 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 (
"bytes"
"strconv"
"time"
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/schedule"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const (
scanLimit = 1000
defaultAdjacentPeerLimit = 1
defaultAdjacentLeaderLimit = 64
minAdjacentSchedulerInterval = time.Second
maxAdjacentSchedulerInterval = 30 * time.Second
)
func init() {
schedule.RegisterScheduler("adjacent-region", func(limiter *schedule.Limiter, args []string) (schedule.Scheduler, error) {
if len(args) == 2 {
leaderLimit, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return nil, errors.WithStack(err)
}
peerLimit, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return nil, errors.WithStack(err)
}
return newBalanceAdjacentRegionScheduler(limiter, leaderLimit, peerLimit), nil
}
return newBalanceAdjacentRegionScheduler(limiter), nil
})
}
// balanceAdjacentRegionScheduler will disperse adjacent regions.
// we will scan a part regions order by key, then select the longest
// adjacent regions and disperse them. finally, we will guarantee
// 1. any two adjacent regions' leader will not in the same store
// 2. the two regions' leader will not in the public store of this two regions
type balanceAdjacentRegionScheduler struct {
*baseScheduler
selector *schedule.RandomSelector
leaderLimit uint64
peerLimit uint64
lastKey []byte
cacheRegions *adjacentState
adjacentRegionsCount int
}
type adjacentState struct {
assignedStoreIds []uint64
regions []*core.RegionInfo
head int
}
func (a *adjacentState) clear() {
a.assignedStoreIds = a.assignedStoreIds[:0]
a.regions = a.regions[:0]
a.head = 0
}
func (a *adjacentState) len() int {
return len(a.regions) - a.head
}
// newBalanceAdjacentRegionScheduler creates a scheduler that tends to disperse adjacent region
// on each store.
func newBalanceAdjacentRegionScheduler(limiter *schedule.Limiter, args ...uint64) schedule.Scheduler {
filters := []schedule.Filter{schedule.StoreStateFilter{TransferLeader: true, MoveRegion: true}}
base := newBaseScheduler(limiter)
s := &balanceAdjacentRegionScheduler{
baseScheduler: base,
selector: schedule.NewRandomSelector(filters),
leaderLimit: defaultAdjacentLeaderLimit,
peerLimit: defaultAdjacentPeerLimit,
lastKey: []byte(""),
}
if len(args) == 2 {
s.leaderLimit = args[0]
s.peerLimit = args[1]
}
return s
}
func (l *balanceAdjacentRegionScheduler) GetName() string {
return "balance-adjacent-region-scheduler"
}
func (l *balanceAdjacentRegionScheduler) GetType() string {
return "adjacent-region"
}
func (l *balanceAdjacentRegionScheduler) GetMinInterval() time.Duration {
return minAdjacentSchedulerInterval
}
func (l *balanceAdjacentRegionScheduler) GetNextInterval(interval time.Duration) time.Duration {
return intervalGrow(interval, maxAdjacentSchedulerInterval, linearGrowth)
}
func (l *balanceAdjacentRegionScheduler) IsScheduleAllowed(cluster schedule.Cluster) bool {
return l.allowBalanceLeader() || l.allowBalancePeer()
}
func (l *balanceAdjacentRegionScheduler) allowBalanceLeader() bool {
return l.limiter.OperatorCount(schedule.OpAdjacent|schedule.OpLeader) < l.leaderLimit
}
func (l *balanceAdjacentRegionScheduler) allowBalancePeer() bool {
return l.limiter.OperatorCount(schedule.OpAdjacent|schedule.OpRegion) < l.peerLimit
}
func (l *balanceAdjacentRegionScheduler) Schedule(cluster schedule.Cluster, opInfluence schedule.OpInfluence) []*schedule.Operator {
if l.cacheRegions == nil {
l.cacheRegions = &adjacentState{
assignedStoreIds: make([]uint64, 0, len(cluster.GetStores())),
regions: make([]*core.RegionInfo, 0, scanLimit),
head: 0,
}
}
// we will process cache firstly
if l.cacheRegions.len() >= 2 {
return l.process(cluster)
}
l.cacheRegions.clear()
regions := cluster.ScanRegions(l.lastKey, scanLimit)
// scan to the end
if len(regions) <= 1 {
l.adjacentRegionsCount = 0
schedulerStatus.WithLabelValues(l.GetName(), "adjacent_count").Set(float64(l.adjacentRegionsCount))
l.lastKey = []byte("")
return nil
}
// calculate max adjacentRegions and record to the cache
adjacentRegions := make([]*core.RegionInfo, 0, scanLimit)
adjacentRegions = append(adjacentRegions, regions[0])
maxLen := 0
for i, r := range regions[1:] {
l.lastKey = r.GetStartKey()
// append if the region are adjacent
lastRegion := adjacentRegions[len(adjacentRegions)-1]
if lastRegion.GetLeader().GetStoreId() == r.GetLeader().GetStoreId() && bytes.Equal(lastRegion.GetEndKey(), r.GetStartKey()) {
adjacentRegions = append(adjacentRegions, r)
if i != len(regions)-2 { // not the last element
continue
}
}
if len(adjacentRegions) == 1 {
adjacentRegions[0] = r
} else {
// got an max length adjacent regions in this range
if maxLen < len(adjacentRegions) {
l.cacheRegions.clear()
maxLen = len(adjacentRegions)
l.cacheRegions.regions = append(l.cacheRegions.regions, adjacentRegions...)
adjacentRegions = adjacentRegions[:0]
adjacentRegions = append(adjacentRegions, r)
}
}
}
l.adjacentRegionsCount += maxLen
return l.process(cluster)
}
func (l *balanceAdjacentRegionScheduler) process(cluster schedule.Cluster) []*schedule.Operator {
if l.cacheRegions.len() < 2 {
return nil
}
head := l.cacheRegions.head
r1 := l.cacheRegions.regions[head]
r2 := l.cacheRegions.regions[head+1]
defer func() {
if l.cacheRegions.len() < 0 {
log.Fatalf("[%s]the cache overflow should never happen", l.GetName())
}
l.cacheRegions.head = head + 1
l.lastKey = r2.GetStartKey()
}()
if l.unsafeToBalance(cluster, r1) {
schedulerCounter.WithLabelValues(l.GetName(), "skip").Inc()
return nil
}
op := l.disperseLeader(cluster, r1, r2)
if op == nil {
schedulerCounter.WithLabelValues(l.GetName(), "no_leader").Inc()
op = l.dispersePeer(cluster, r1)
}
if op == nil {
schedulerCounter.WithLabelValues(l.GetName(), "no_peer").Inc()
l.cacheRegions.assignedStoreIds = l.cacheRegions.assignedStoreIds[:0]
return nil
}
return []*schedule.Operator{op}
}
func (l *balanceAdjacentRegionScheduler) unsafeToBalance(cluster schedule.Cluster, region *core.RegionInfo) bool {
if len(region.GetPeers()) != cluster.GetMaxReplicas() {
return true
}
store := cluster.GetStore(region.GetLeader().GetStoreId())
s := l.selector.SelectSource(cluster, []*core.StoreInfo{store})
if s == nil {
return true
}
// Skip hot regions.
if cluster.IsRegionHot(region.GetID()) {
schedulerCounter.WithLabelValues(l.GetName(), "region_hot").Inc()
return true
}
return false
}
func (l *balanceAdjacentRegionScheduler) disperseLeader(cluster schedule.Cluster, before *core.RegionInfo, after *core.RegionInfo) *schedule.Operator {
if !l.allowBalanceLeader() {
return nil
}
diffPeers := before.GetDiffFollowers(after)
if len(diffPeers) == 0 {
return nil
}
storesInfo := make([]*core.StoreInfo, 0, len(diffPeers))
for _, p := range diffPeers {
storesInfo = append(storesInfo, cluster.GetStore(p.GetStoreId()))
}
target := l.selector.SelectTarget(cluster, storesInfo)
if target == nil {
return nil
}
step := schedule.TransferLeader{FromStore: before.GetLeader().GetStoreId(), ToStore: target.GetId()}
op := schedule.NewOperator("balance-adjacent-leader", before.GetID(), before.GetRegionEpoch(), schedule.OpAdjacent|schedule.OpLeader, step)
op.SetPriorityLevel(core.LowPriority)
schedulerCounter.WithLabelValues(l.GetName(), "adjacent_leader").Inc()
return op
}
func (l *balanceAdjacentRegionScheduler) dispersePeer(cluster schedule.Cluster, region *core.RegionInfo) *schedule.Operator {
if !l.allowBalancePeer() {
return nil
}
// scoreGuard guarantees that the distinct score will not decrease.
leaderStoreID := region.GetLeader().GetStoreId()
stores := cluster.GetRegionStores(region)
source := cluster.GetStore(leaderStoreID)
scoreGuard := schedule.NewDistinctScoreFilter(cluster.GetLocationLabels(), stores, source)
excludeStores := region.GetStoreIds()
for _, storeID := range l.cacheRegions.assignedStoreIds {
if _, ok := excludeStores[storeID]; !ok {
excludeStores[storeID] = struct{}{}
}
}
filters := []schedule.Filter{
schedule.NewExcludedFilter(nil, excludeStores),
scoreGuard,
}
target := l.selector.SelectTarget(cluster, cluster.GetStores(), filters...)
if target == nil {
return nil
}
newPeer, err := cluster.AllocPeer(target.GetId())
if err != nil {
return nil
}
if newPeer == nil {
schedulerCounter.WithLabelValues(l.GetName(), "no_peer").Inc()
return nil
}
// record the store id and exclude it in next time
l.cacheRegions.assignedStoreIds = append(l.cacheRegions.assignedStoreIds, newPeer.GetStoreId())
op := schedule.CreateMovePeerOperator("balance-adjacent-peer", cluster, region, schedule.OpAdjacent, leaderStoreID, newPeer.GetStoreId(), newPeer.GetId())
op.SetPriorityLevel(core.LowPriority)
schedulerCounter.WithLabelValues(l.GetName(), "adjacent_peer").Inc()
return op
}