forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
181 lines (156 loc) · 5.22 KB
/
utils.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
// 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 (
"math"
"time"
log "github.com/Sirupsen/logrus"
"github.com/montanaflynn/stats"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/schedule"
)
// scheduleTransferLeader schedules a region to transfer leader to the peer.
func scheduleTransferLeader(cluster schedule.Cluster, schedulerName string, s schedule.Selector, filters ...schedule.Filter) (*core.RegionInfo, *metapb.Peer) {
stores := cluster.GetStores()
if len(stores) == 0 {
schedulerCounter.WithLabelValues(schedulerName, "no_store").Inc()
return nil, nil
}
var averageLeader float64
for _, s := range stores {
averageLeader += float64(s.LeaderScore()) / float64(len(stores))
}
mostLeaderStore := s.SelectSource(stores, filters...)
leastLeaderStore := s.SelectTarget(stores, filters...)
var mostLeaderDistance, leastLeaderDistance float64
if mostLeaderStore != nil {
mostLeaderDistance = math.Abs(mostLeaderStore.LeaderScore() - averageLeader)
}
if leastLeaderStore != nil {
leastLeaderDistance = math.Abs(leastLeaderStore.LeaderScore() - averageLeader)
}
if mostLeaderDistance == 0 && leastLeaderDistance == 0 {
schedulerCounter.WithLabelValues(schedulerName, "already_balanced").Inc()
return nil, nil
}
if mostLeaderDistance > leastLeaderDistance {
// Transfer a leader out of mostLeaderStore.
region := cluster.RandLeaderRegion(mostLeaderStore.GetId())
if region == nil {
schedulerCounter.WithLabelValues(schedulerName, "no_leader_region").Inc()
return nil, nil
}
targetStores := cluster.GetFollowerStores(region)
target := s.SelectTarget(targetStores)
if target == nil {
schedulerCounter.WithLabelValues(schedulerName, "no_target_store").Inc()
return nil, nil
}
return region, region.GetStorePeer(target.GetId())
}
// Transfer a leader into leastLeaderStore.
region := cluster.RandFollowerRegion(leastLeaderStore.GetId())
if region == nil {
schedulerCounter.WithLabelValues(schedulerName, "no_target_peer").Inc()
return nil, nil
}
return region, region.GetStorePeer(leastLeaderStore.GetId())
}
// scheduleRemovePeer schedules a region to remove the peer.
func scheduleRemovePeer(cluster schedule.Cluster, schedulerName string, s schedule.Selector, filters ...schedule.Filter) (*core.RegionInfo, *metapb.Peer) {
stores := cluster.GetStores()
source := s.SelectSource(stores, filters...)
if source == nil {
schedulerCounter.WithLabelValues(schedulerName, "no_store").Inc()
return nil, nil
}
region := cluster.RandFollowerRegion(source.GetId())
if region == nil {
region = cluster.RandLeaderRegion(source.GetId())
}
if region == nil {
schedulerCounter.WithLabelValues(schedulerName, "no_region").Inc()
return nil, nil
}
return region, region.GetStorePeer(source.GetId())
}
// scheduleAddPeer schedules a new peer.
func scheduleAddPeer(cluster schedule.Cluster, s schedule.Selector, filters ...schedule.Filter) *metapb.Peer {
stores := cluster.GetStores()
target := s.SelectTarget(stores, filters...)
if target == nil {
return nil
}
newPeer, err := cluster.AllocPeer(target.GetId())
if err != nil {
log.Errorf("failed to allocate peer: %v", err)
return nil
}
return newPeer
}
func minUint64(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
func maxUint64(a, b uint64) uint64 {
if a > b {
return a
}
return b
}
func minDuration(a, b time.Duration) time.Duration {
if a < b {
return a
}
return b
}
const (
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 *core.StoreInfo, kind core.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 schedule.Cluster, kind core.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))
}