-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancer.go
372 lines (318 loc) · 10.3 KB
/
balancer.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
// Copyright 2016 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 server
import (
"math"
"time"
"github.com/montanaflynn/stats"
"github.com/ngaut/log"
"github.com/pingcap/kvproto/pkg/metapb"
)
const (
storeCacheInterval = 30 * time.Second
bootstrapBalanceCount = 10
bootstrapBalanceDiff = 2
)
// minBalanceDiff returns the minimal diff to do balance. The formula is based
// on experience to let the diff increase alone with the count slowly.
func minBalanceDiff(count uint64) float64 {
if count < bootstrapBalanceCount {
return bootstrapBalanceDiff
}
return math.Sqrt(float64(count))
}
// shouldBalance returns true if we should balance the source and target store.
// The min balance diff provides a buffer to make the cluster stable, so that we
// don't need to schedule very frequently.
func shouldBalance(source, target *storeInfo, kind ResourceKind) bool {
sourceCount := source.resourceCount(kind)
sourceScore := source.resourceScore(kind)
targetScore := target.resourceScore(kind)
if targetScore >= sourceScore {
return false
}
diffRatio := 1 - targetScore/sourceScore
diffCount := diffRatio * float64(sourceCount)
return diffCount >= minBalanceDiff(sourceCount)
}
func adjustBalanceLimit(cluster *clusterInfo, kind ResourceKind) uint64 {
stores := cluster.getStores()
counts := make([]float64, 0, len(stores))
for _, s := range stores {
if s.isUp() {
counts = append(counts, float64(s.resourceCount(kind)))
}
}
limit, _ := stats.StandardDeviation(stats.Float64Data(counts))
return maxUint64(1, uint64(limit))
}
type balanceLeaderScheduler struct {
opt *scheduleOption
limit uint64
selector Selector
}
func newBalanceLeaderScheduler(opt *scheduleOption) *balanceLeaderScheduler {
var filters []Filter
filters = append(filters, newBlockFilter())
filters = append(filters, newStateFilter(opt))
filters = append(filters, newHealthFilter(opt))
return &balanceLeaderScheduler{
opt: opt,
limit: 1,
selector: newBalanceSelector(leaderKind, filters),
}
}
func (l *balanceLeaderScheduler) GetName() string {
return "balance-leader-scheduler"
}
func (l *balanceLeaderScheduler) GetResourceKind() ResourceKind {
return leaderKind
}
func (l *balanceLeaderScheduler) GetResourceLimit() uint64 {
return minUint64(l.limit, l.opt.GetLeaderScheduleLimit())
}
func (l *balanceLeaderScheduler) Prepare(cluster *clusterInfo) error { return nil }
func (l *balanceLeaderScheduler) Cleanup(cluster *clusterInfo) {}
func (l *balanceLeaderScheduler) Schedule(cluster *clusterInfo) Operator {
region, newLeader := scheduleTransferLeader(cluster, l.selector)
if region == nil {
return nil
}
source := cluster.getStore(region.Leader.GetStoreId())
target := cluster.getStore(newLeader.GetStoreId())
if !shouldBalance(source, target, l.GetResourceKind()) {
return nil
}
l.limit = adjustBalanceLimit(cluster, l.GetResourceKind())
return newTransferLeader(region, newLeader)
}
type balanceRegionScheduler struct {
opt *scheduleOption
rep *Replication
cache *idCache
limit uint64
selector Selector
}
func newBalanceRegionScheduler(opt *scheduleOption) *balanceRegionScheduler {
cache := newIDCache(storeCacheInterval, 4*storeCacheInterval)
var filters []Filter
filters = append(filters, newCacheFilter(cache))
filters = append(filters, newStateFilter(opt))
filters = append(filters, newHealthFilter(opt))
filters = append(filters, newSnapshotCountFilter(opt))
filters = append(filters, newStorageThresholdFilter(opt))
return &balanceRegionScheduler{
opt: opt,
rep: opt.GetReplication(),
cache: cache,
limit: 1,
selector: newBalanceSelector(regionKind, filters),
}
}
func (s *balanceRegionScheduler) GetName() string {
return "balance-region-scheduler"
}
func (s *balanceRegionScheduler) GetResourceKind() ResourceKind {
return regionKind
}
func (s *balanceRegionScheduler) GetResourceLimit() uint64 {
return minUint64(s.limit, s.opt.GetRegionScheduleLimit())
}
func (s *balanceRegionScheduler) Prepare(cluster *clusterInfo) error { return nil }
func (s *balanceRegionScheduler) Cleanup(cluster *clusterInfo) {}
func (s *balanceRegionScheduler) Schedule(cluster *clusterInfo) Operator {
// Select a peer from the store with most regions.
region, oldPeer := scheduleRemovePeer(cluster, s.selector)
if region == nil {
return nil
}
// We don't schedule region with abnormal number of replicas.
if len(region.GetPeers()) != s.rep.GetMaxReplicas() {
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.set(oldPeer.GetStoreId())
}
return op
}
func (s *balanceRegionScheduler) transferPeer(cluster *clusterInfo, region *RegionInfo, oldPeer *metapb.Peer) Operator {
// scoreGuard guarantees that the distinct score will not decrease.
stores := cluster.getRegionStores(region)
source := cluster.getStore(oldPeer.GetStoreId())
scoreGuard := newDistinctScoreFilter(s.rep, stores, source)
checker := newReplicaChecker(s.opt, cluster)
newPeer, _ := checker.selectBestPeer(region, scoreGuard)
if newPeer == nil {
return nil
}
target := cluster.getStore(newPeer.GetStoreId())
if !shouldBalance(source, target, s.GetResourceKind()) {
return nil
}
s.limit = adjustBalanceLimit(cluster, s.GetResourceKind())
return newTransferPeer(region, oldPeer, newPeer)
}
// replicaChecker ensures region has the best replicas.
type replicaChecker struct {
opt *scheduleOption
rep *Replication
cluster *clusterInfo
filters []Filter
}
func newReplicaChecker(opt *scheduleOption, cluster *clusterInfo) *replicaChecker {
var filters []Filter
filters = append(filters, newHealthFilter(opt))
filters = append(filters, newSnapshotCountFilter(opt))
return &replicaChecker{
opt: opt,
rep: opt.GetReplication(),
cluster: cluster,
filters: filters,
}
}
func (r *replicaChecker) Check(region *RegionInfo) Operator {
if op := r.checkDownPeer(region); op != nil {
return op
}
if op := r.checkOfflinePeer(region); op != nil {
return op
}
if len(region.GetPeers()) < r.rep.GetMaxReplicas() {
newPeer, _ := r.selectBestPeer(region, r.filters...)
if newPeer == nil {
return nil
}
return newAddPeer(region, newPeer)
}
if len(region.GetPeers()) > r.rep.GetMaxReplicas() {
oldPeer, _ := r.selectWorstPeer(region)
if oldPeer == nil {
return nil
}
return newRemovePeer(region, oldPeer)
}
return r.checkBestReplacement(region)
}
// selectBestPeer returns the best peer in other stores.
func (r *replicaChecker) selectBestPeer(region *RegionInfo, filters ...Filter) (*metapb.Peer, float64) {
// Add some must have filters.
filters = append(filters, newStateFilter(r.opt))
filters = append(filters, newStorageThresholdFilter(r.opt))
filters = append(filters, newExcludedFilter(nil, region.GetStoreIds()))
var (
bestStore *storeInfo
bestScore float64
)
// Select the store with best distinct score.
// If the scores are the same, select the store with minimal region score.
stores := r.cluster.getRegionStores(region)
for _, store := range r.cluster.getStores() {
if filterTarget(store, filters) {
continue
}
score := r.rep.GetDistinctScore(stores, store)
if bestStore == nil || compareStoreScore(store, score, bestStore, bestScore) > 0 {
bestStore = store
bestScore = score
}
}
if bestStore == nil || filterTarget(bestStore, r.filters) {
return nil, 0
}
newPeer, err := r.cluster.allocPeer(bestStore.GetId())
if err != nil {
log.Errorf("failed to allocate peer: %v", err)
return nil, 0
}
return newPeer, bestScore
}
// selectWorstPeer returns the worst peer in the region.
func (r *replicaChecker) selectWorstPeer(region *RegionInfo, filters ...Filter) (*metapb.Peer, float64) {
var (
worstStore *storeInfo
worstScore float64
)
// Select the store with lowest distinct score.
// If the scores are the same, select the store with maximal region score.
stores := r.cluster.getRegionStores(region)
for _, store := range stores {
if filterSource(store, filters) {
continue
}
score := r.rep.GetDistinctScore(stores, store)
if worstStore == nil || compareStoreScore(store, score, worstStore, worstScore) < 0 {
worstStore = store
worstScore = score
}
}
if worstStore == nil || filterSource(worstStore, r.filters) {
return nil, 0
}
return region.GetStorePeer(worstStore.GetId()), worstScore
}
// selectBestReplacement returns the best peer to replace the region peer.
func (r *replicaChecker) selectBestReplacement(region *RegionInfo, peer *metapb.Peer) (*metapb.Peer, float64) {
// Get a new region without the peer we are going to replace.
newRegion := region.clone()
newRegion.RemoveStorePeer(peer.GetStoreId())
return r.selectBestPeer(newRegion, newExcludedFilter(nil, region.GetStoreIds()))
}
func (r *replicaChecker) checkDownPeer(region *RegionInfo) Operator {
for _, stats := range region.DownPeers {
peer := stats.GetPeer()
if peer == nil {
continue
}
store := r.cluster.getStore(peer.GetStoreId())
if store.downTime() < r.opt.GetMaxStoreDownTime() {
continue
}
if stats.GetDownSeconds() < uint64(r.opt.GetMaxStoreDownTime().Seconds()) {
continue
}
return newRemovePeer(region, peer)
}
return nil
}
func (r *replicaChecker) checkOfflinePeer(region *RegionInfo) Operator {
for _, peer := range region.GetPeers() {
store := r.cluster.getStore(peer.GetStoreId())
if store.isUp() {
continue
}
newPeer, _ := r.selectBestPeer(region)
if newPeer == nil {
return nil
}
return newTransferPeer(region, peer, newPeer)
}
return nil
}
func (r *replicaChecker) checkBestReplacement(region *RegionInfo) Operator {
oldPeer, oldScore := r.selectWorstPeer(region)
if oldPeer == nil {
return nil
}
newPeer, newScore := r.selectBestReplacement(region, oldPeer)
if newPeer == nil {
return nil
}
// Make sure the new peer is better than the old peer.
if newScore <= oldScore {
return nil
}
return newTransferPeer(region, oldPeer, newPeer)
}