-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler_replica.go
288 lines (239 loc) · 7.72 KB
/
scheduler_replica.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
package prophet
import (
"math"
)
type replicaChecker struct {
cfg *Cfg
rt *Runtime
filters []Filter
}
func newReplicaChecker(cfg *Cfg, rt *Runtime, filters ...Filter) *replicaChecker {
return &replicaChecker{
cfg: cfg,
rt: rt,
filters: filters,
}
}
// Check return the Operator to add or remove replica
func (r *replicaChecker) Check(target *ResourceRuntime) Operator {
if op := r.checkDownPeer(target); op != nil {
return op
}
if op := r.checkOfflinePeer(target); op != nil {
return op
}
currReplicasCount := len(target.meta.Peers())
log.Debugf("dispatch resource %d hit replica checker, expect %d, current %d",
target.meta.ID(),
currReplicasCount,
r.cfg.CountResourceReplicas)
if currReplicasCount < r.cfg.CountResourceReplicas {
newPeer, _ := r.selectBestPeer(target, true, r.filters...)
if newPeer == nil {
return nil
}
return newAddPeerAggregationOp(r.cfg, target, newPeer)
}
if currReplicasCount > r.cfg.CountResourceReplicas {
log.Debugf("dispatch resource %d has more replica, expect %d, current %d",
target.meta.ID(),
currReplicasCount,
r.cfg.CountResourceReplicas)
oldPeer, _ := r.selectWorstPeer(target)
if oldPeer == nil {
return nil
}
return newRemovePeerOp(target.meta.ID(), oldPeer)
}
return r.checkBestReplacement(target)
}
func (r *replicaChecker) checkDownPeer(target *ResourceRuntime) Operator {
for _, stats := range target.downPeers {
peer := stats.Peer
container := r.rt.Container(peer.ContainerID)
if nil != container && container.Downtime() < r.cfg.MaxAllowContainerDownDuration {
continue
}
if nil != container && stats.DownSeconds < uint64(r.cfg.MaxAllowContainerDownDuration.Seconds()) {
continue
}
return newRemovePeerOp(target.meta.ID(), peer)
}
return nil
}
func (r *replicaChecker) checkOfflinePeer(target *ResourceRuntime) Operator {
for _, peer := range target.meta.Peers() {
container := r.rt.Container(peer.ContainerID)
if container != nil && container.IsUp() {
continue
}
newPeer, _ := r.selectBestPeer(target, true)
if newPeer == nil {
return nil
}
return newTransferPeerAggregationOp(r.cfg, target, peer, newPeer)
}
return nil
}
// selectWorstPeer returns the worst peer in the resource.
func (r *replicaChecker) selectWorstPeer(target *ResourceRuntime, filters ...Filter) (*Peer, float64) {
var (
worstContainer *ContainerRuntime
worstScore float64
)
// Select the container with lowest distinct score.
// If the scores are the same, select the container with maximal resource score.
containers := r.rt.ResourceContainers(target)
for _, container := range containers {
if filterSource(container, filters) {
continue
}
score := r.cfg.getDistinctScore(containers, container)
if worstContainer == nil || compareContainerScore(container, score, worstContainer, worstScore) < 0 {
worstContainer = container
worstScore = score
}
}
if worstContainer == nil || filterSource(worstContainer, r.filters) {
return nil, 0
}
return target.GetContainerPeer(worstContainer.meta.ID()), worstScore
}
// selectBestPeer returns the best peer in other containers.
func (r *replicaChecker) selectBestPeer(target *ResourceRuntime, allocPeerID bool, filters ...Filter) (*Peer, float64) {
// Add some must have filters.
filters = append(filters, NewStateFilter(r.cfg))
filters = append(filters, NewStorageThresholdFilter(r.cfg))
filters = append(filters, NewExcludedFilter(nil, target.GetContainerIDs()))
filters = append(filters, NewBlockFilter())
filters = append(filters, NewLabelFilter(target.meta.Labels())) // make sure the container has the tags needed for the resource
var (
bestContainer *ContainerRuntime
bestScore float64
)
// Select the container with best distinct score.
// If the scores are the same, select the container with minimal replica score.
containers := r.rt.ResourceContainers(target)
log.Debugf("resource %d select best peer using %d containers",
target.meta.ID(),
len(containers))
for _, container := range r.rt.Containers() {
if filterTarget(container, filters) {
log.Debugf("resource %d select best peer skip container %d",
target.meta.ID(),
container.meta.ID())
continue
}
score := getDistinctScore(r.cfg, containers, container)
if bestContainer == nil || compareContainerScore(container, score, bestContainer, bestScore) > 0 {
bestContainer = container
bestScore = score
}
}
if bestContainer == nil || filterTarget(bestContainer, r.filters) {
if bestContainer != nil {
log.Debugf("resource %d select best peer skip best container %d",
target.meta.ID(),
bestContainer.meta.ID())
}
return nil, 0
}
log.Debugf("resource %d select best peer use container %d",
target.meta.ID(),
bestContainer.meta.ID())
newPeer, err := allocPeer(bestContainer.meta.ID(), r.rt.p.store, allocPeerID)
if err != nil {
log.Errorf("scheduler: allocate peer failure, errors:\n %+v", err)
return nil, 0
}
return newPeer, bestScore
}
func (r *replicaChecker) checkBestReplacement(target *ResourceRuntime) Operator {
oldPeer, oldScore := r.selectWorstPeer(target)
if oldPeer == nil {
return nil
}
newPeer, newScore := r.selectBestReplacement(target, oldPeer)
if newPeer == nil {
return nil
}
// Make sure the new peer is better than the old peer.
if newScore <= oldScore {
return nil
}
id, err := r.rt.p.store.AllocID()
if err != nil {
log.Errorf("allocate peer failure, %+v", err)
return nil
}
newPeer.ID = id
return newTransferPeerAggregationOp(r.cfg, target, oldPeer, newPeer)
}
// selectBestReplacement returns the best peer to replace the resource peer.
func (r *replicaChecker) selectBestReplacement(target *ResourceRuntime, peer *Peer) (*Peer, float64) {
// selectBestReplacement returns the best peer to replace the resource peer.
// Get a new resource without the peer we are going to replace.
newResource := target.Clone()
newResource.RemoveContainerPeer(peer.ContainerID)
return r.selectBestPeer(newResource, false, NewExcludedFilter(nil, target.GetContainerIDs()))
}
// getDistinctScore returns the score that the other is distinct from the containers.
// A higher score means the other container is more different from the existed containers.
func getDistinctScore(cfg *Cfg, containers []*ContainerRuntime, other *ContainerRuntime) float64 {
score := float64(0)
locationLabels := cfg.getLocationLabels()
for i := range locationLabels {
keys := locationLabels[0 : i+1]
level := len(locationLabels) - i - 1
levelScore := math.Pow(replicaBaseScore, float64(level))
for _, s := range containers {
if s.meta.ID() == other.meta.ID() {
continue
}
id1 := s.GetLocationID(keys)
if len(id1) == 0 {
return 0
}
id2 := other.GetLocationID(keys)
if len(id2) == 0 {
return 0
}
if id1 != id2 {
score += levelScore
}
}
}
return score
}
// compareContainerScore compares which container is better for replica.
// Returns 0 if container A is as good as container B.
// Returns 1 if container A is better than container B.
// Returns -1 if container B is better than container A.
func compareContainerScore(containerA *ContainerRuntime, scoreA float64, containerB *ContainerRuntime, scoreB float64) int {
// The container with higher score is better.
if scoreA > scoreB {
return 1
}
if scoreA < scoreB {
return -1
}
// The container with lower resource score is better.
if containerA.ReplicaScore() < containerB.ReplicaScore() {
return 1
}
if containerA.ReplicaScore() > containerB.ReplicaScore() {
return -1
}
return 0
}
func allocPeer(containerID uint64, store Store, allocPeerID bool) (*Peer, error) {
value := &Peer{ContainerID: containerID}
if allocPeerID {
peerID, err := store.AllocID()
if err != nil {
return nil, err
}
value.ID = peerID
}
return value, nil
}