forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
387 lines (365 loc) · 12.3 KB
/
scheduler.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright 2013 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package docker
import (
"fmt"
"math"
"strconv"
"sync"
docker "github.com/fsouza/go-dockerclient"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/pkg/errors"
"github.com/tsuru/docker-cluster/cluster"
"github.com/tsuru/tsuru/app"
"github.com/tsuru/tsuru/autoscale"
"github.com/tsuru/tsuru/log"
"github.com/tsuru/tsuru/net"
"github.com/tsuru/tsuru/provision/docker/container"
"github.com/tsuru/tsuru/provision/node"
)
type segregatedScheduler struct {
hostMutex sync.Mutex
maxMemoryRatio float32
TotalMemoryMetadata string
provisioner *dockerProvisioner
// ignored containers is only set in provisioner returned by
// cloneProvisioner which will set this field to exclude some container
// ids from balancing (containers being removed by rebalance usually).
ignoredContainers []string
}
func (s *segregatedScheduler) Schedule(c *cluster.Cluster, opts *docker.CreateContainerOptions, schedulerOpts cluster.SchedulerOptions) (cluster.Node, error) {
schedOpts, ok := schedulerOpts.(*container.SchedulerOpts)
if !ok {
return cluster.Node{}, &container.SchedulerError{
Base: errors.Errorf("invalid scheduler opts: %#v", schedulerOpts),
}
}
var appName string
filterNodesMap := map[string]struct{}{}
if schedOpts != nil {
appName = schedOpts.AppName
for _, n := range schedOpts.FilterNodes {
filterNodesMap[n] = struct{}{}
}
}
if appName == "" {
return s.scheduleAnyNode(c, filterNodesMap)
}
a, _ := app.GetByName(schedOpts.AppName)
nodes, err := s.provisioner.Nodes(a)
if err != nil {
return cluster.Node{}, &container.SchedulerError{Base: err}
}
nodes = filterNodes(nodes, filterNodesMap)
nodes, err = s.filterByMemoryUsage(a, nodes, s.maxMemoryRatio, s.TotalMemoryMetadata)
if err != nil {
return cluster.Node{}, &container.SchedulerError{Base: err}
}
node, err := s.chooseNodeToAdd(nodes, opts.Name, schedOpts.AppName, schedOpts.ProcessName)
if err != nil {
return cluster.Node{}, &container.SchedulerError{Base: err}
}
if schedOpts.ActionLimiter != nil {
schedOpts.LimiterDone = schedOpts.ActionLimiter.Start(net.URLToHost(node))
}
if schedOpts.UpdateName {
err = s.updateContainerName(opts, schedOpts.AppName)
if err != nil {
return cluster.Node{}, &container.SchedulerError{Base: err}
}
}
return cluster.Node{Address: node}, nil
}
func (s *segregatedScheduler) scheduleAnyNode(c *cluster.Cluster, filter map[string]struct{}) (cluster.Node, error) {
nodes, err := c.Nodes()
if err != nil {
return cluster.Node{}, err
}
nodes = filterNodes(nodes, filter)
if len(nodes) < 1 {
return cluster.Node{}, errors.New("There is no Docker node. Add one with `tsuru node-add`")
}
log.Debugf("[scheduler] Schedule any node with filter %#v possible nodes: %#v", filter, nodes)
nodeAddr, _, err := s.minMaxNodes(nodes, "", "")
if err != nil {
return cluster.Node{}, err
}
log.Debugf("[scheduler] Schedule any node with filter %#v chosen node: %q", filter, nodeAddr)
return cluster.Node{Address: nodeAddr}, nil
}
func (s *segregatedScheduler) updateContainerName(opts *docker.CreateContainerOptions, appName string) error {
if opts.Name == "" {
return nil
}
newName := generateContainerName(appName)
coll := s.provisioner.Collection()
defer coll.Close()
err := coll.Update(bson.M{"name": opts.Name}, bson.M{"$set": bson.M{"name": newName}})
if err != nil {
return err
}
opts.Name = newName
return nil
}
func (s *segregatedScheduler) filterByMemoryUsage(a *app.App, nodes []cluster.Node, maxMemoryRatio float32, TotalMemoryMetadata string) ([]cluster.Node, error) {
if maxMemoryRatio == 0 || TotalMemoryMetadata == "" {
return nodes, nil
}
hosts := make([]string, len(nodes))
for i := range nodes {
hosts[i] = net.URLToHost(nodes[i].Address)
}
containers, err := s.provisioner.ListContainers(bson.M{"hostaddr": bson.M{"$in": hosts}, "id": bson.M{"$nin": s.ignoredContainers}})
if err != nil {
return nil, err
}
hostReserved := make(map[string]int64)
for _, cont := range containers {
contApp, err := app.GetByName(cont.AppName)
if err != nil {
return nil, err
}
hostReserved[cont.HostAddr] += contApp.Plan.Memory
}
megabyte := float64(1024 * 1024)
nodeList := make([]cluster.Node, 0, len(nodes))
for _, node := range nodes {
totalMemory, _ := strconv.ParseFloat(node.Metadata[TotalMemoryMetadata], 64)
shouldAdd := true
if totalMemory != 0 {
maxMemory := totalMemory * float64(maxMemoryRatio)
host := net.URLToHost(node.Address)
nodeReserved := hostReserved[host] + a.Plan.Memory
if nodeReserved > int64(maxMemory) {
shouldAdd = false
tryingToReserveMB := float64(a.Plan.Memory) / megabyte
reservedMB := float64(hostReserved[host]) / megabyte
limitMB := maxMemory / megabyte
log.Errorf("Node %q has reached its memory limit. "+
"Limit %0.4fMB. Reserved: %0.4fMB. Needed additional %0.4fMB",
host, limitMB, reservedMB, tryingToReserveMB)
}
}
if shouldAdd {
nodeList = append(nodeList, node)
}
}
if len(nodeList) == 0 {
var autoScaleEnabled bool
rule, _ := autoscale.AutoScaleRuleForMetadata(a.Pool)
if rule != nil {
autoScaleEnabled = rule.Enabled
}
errMsg := fmt.Sprintf("no nodes found with enough memory for container of %q: %0.4fMB",
a.Name, float64(a.Plan.Memory)/megabyte)
if autoScaleEnabled {
// Allow going over quota temporarily because auto-scale will be
// able to detect this and automatically add a new nodes.
log.Errorf("WARNING: %s. Will ignore memory restrictions.", errMsg)
return nodes, nil
}
return nil, errors.New(errMsg)
}
return nodeList, nil
}
type nodeAggregate struct {
HostAddr string `bson:"_id"`
Count int
}
// aggregateContainersBy aggregates and counts how many containers
// exist each node that matches received filters
func (s *segregatedScheduler) aggregateContainersBy(matcher bson.M) (map[string]int, error) {
coll := s.provisioner.Collection()
defer coll.Close()
pipe := coll.Pipe([]bson.M{
matcher,
{"$group": bson.M{"_id": "$hostaddr", "count": bson.M{"$sum": 1}}},
})
var results []nodeAggregate
err := pipe.All(&results)
if err != nil {
return nil, err
}
countMap := make(map[string]int)
for _, result := range results {
countMap[result.HostAddr] = result.Count
}
return countMap, nil
}
func (s *segregatedScheduler) aggregateContainersByHost(hosts []string) (map[string]int, error) {
return s.aggregateContainersBy(bson.M{"$match": bson.M{"hostaddr": bson.M{"$in": hosts}, "id": bson.M{"$nin": s.ignoredContainers}}})
}
func (s *segregatedScheduler) aggregateContainersByHostAppProcess(hosts []string, appName, process string) (map[string]int, error) {
matcher := bson.M{
"hostaddr": bson.M{"$in": hosts},
"id": bson.M{"$nin": s.ignoredContainers},
}
if appName != "" {
matcher["appname"] = appName
}
if process == "" {
matcher["$or"] = []bson.M{{"processname": bson.M{"$exists": false}}, {"processname": ""}}
} else {
matcher["processname"] = process
}
return s.aggregateContainersBy(bson.M{"$match": matcher})
}
func (s *segregatedScheduler) GetRemovableContainer(appName string, process string) (string, error) {
a, err := app.GetByName(appName)
if err != nil {
return "", err
}
nodes, err := s.provisioner.Nodes(a)
if err != nil {
return "", err
}
return s.chooseContainerToRemove(nodes, appName, process)
}
type errContainerNotFound struct {
AppName string
ProcessName string
}
func (m *errContainerNotFound) Error() string {
return fmt.Sprintf("Container of app %q with process %q was not found in any servers", m.AppName, m.ProcessName)
}
func (s *segregatedScheduler) getContainerPreferablyFromHost(host string, appName, process string) (string, error) {
coll := s.provisioner.Collection()
defer coll.Close()
var c container.Container
query := bson.M{
"id": bson.M{"$nin": s.ignoredContainers},
"appname": appName,
"hostaddr": net.URLToHost(host),
}
if process == "" {
query["$or"] = []bson.M{{"processname": bson.M{"$exists": false}}, {"processname": ""}}
} else {
query["processname"] = process
}
err := coll.Find(query).Select(bson.M{"id": 1}).One(&c)
if err == mgo.ErrNotFound {
delete(query, "hostaddr")
err = coll.Find(query).Select(bson.M{"id": 1}).One(&c)
}
if err == mgo.ErrNotFound {
return "", &errContainerNotFound{AppName: appName, ProcessName: process}
}
return c.ID, err
}
func (s *segregatedScheduler) nodesToHosts(nodes []cluster.Node) ([]string, map[string]string) {
hosts := make([]string, len(nodes))
hostsMap := make(map[string]string)
// Only hostname is saved in the docker containers collection
// so we need to extract and map then to the original node.
for i, node := range nodes {
host := net.URLToHost(node.Address)
hosts[i] = host
hostsMap[host] = node.Address
}
return hosts, hostsMap
}
// chooseNodeToAdd finds which is the node with the minimum number of containers
// and returns it
func (s *segregatedScheduler) chooseNodeToAdd(nodes []cluster.Node, contName string, appName, process string) (string, error) {
log.Debugf("[scheduler] Possible nodes for container %s: %#v", contName, nodes)
s.hostMutex.Lock()
defer s.hostMutex.Unlock()
chosenNode, _, err := s.minMaxNodes(nodes, appName, process)
if err != nil {
return "", err
}
log.Debugf("[scheduler] Chosen node for container %s: %#v", contName, chosenNode)
if contName != "" {
coll := s.provisioner.Collection()
defer coll.Close()
err = coll.Update(bson.M{"name": contName}, bson.M{"$set": bson.M{"hostaddr": net.URLToHost(chosenNode)}})
}
return chosenNode, err
}
// chooseContainerToRemove finds a container from the the node with maximum
// number of containers and returns it
func (s *segregatedScheduler) chooseContainerToRemove(nodes []cluster.Node, appName, process string) (string, error) {
_, chosenNode, err := s.minMaxNodes(nodes, appName, process)
if err != nil {
return "", err
}
log.Debugf("[scheduler] Chosen node for remove a container: %#v", chosenNode)
containerID, err := s.getContainerPreferablyFromHost(chosenNode, appName, process)
if err != nil {
return "", err
}
return containerID, err
}
func appGroupCount(hostGroups map[string]int, appCountHost map[string]int) map[string]int {
groupCounters := map[int]int{}
for host, count := range appCountHost {
groupCounters[hostGroups[host]] += count
}
result := map[string]int{}
for host := range hostGroups {
result[host] = groupCounters[hostGroups[host]]
}
return result
}
// Find the host with the minimum (good to add a new container) and maximum
// (good to remove a container) value for the pair [(number of containers for
// app-process), (number of containers in host)]
func (s *segregatedScheduler) minMaxNodes(nodes []cluster.Node, appName, process string) (string, string, error) {
nodesList := make(node.NodeList, len(nodes))
for i := range nodes {
nodesList[i] = &clusterNodeWrapper{Node: &nodes[i], prov: s.provisioner}
}
metaFreqList, _, err := nodesList.SplitMetadata()
if err != nil {
log.Debugf("[scheduler] ignoring metadata diff when selecting node: %s", err)
}
hostGroupMap := map[string]int{}
for i, m := range metaFreqList {
for _, n := range m.Nodes {
hostGroupMap[net.URLToHost(n.Address())] = i
}
}
hosts, hostsMap := s.nodesToHosts(nodes)
hostCountMap, err := s.aggregateContainersByHost(hosts)
if err != nil {
return "", "", err
}
appCountMap, err := s.aggregateContainersByHostAppProcess(hosts, appName, process)
if err != nil {
return "", "", err
}
priorityEntries := []map[string]int{appGroupCount(hostGroupMap, appCountMap), appCountMap, hostCountMap}
var minHost, maxHost string
var minScore uint64 = math.MaxUint64
var maxScore uint64 = 0
for _, host := range hosts {
var score uint64
for i, e := range priorityEntries {
score += uint64(e[host]) << uint((len(priorityEntries)-i-1)*(64/len(priorityEntries)))
}
if score < minScore {
minScore = score
minHost = host
}
if score >= maxScore {
maxScore = score
maxHost = host
}
}
return hostsMap[minHost], hostsMap[maxHost], nil
}
func filterNodes(nodes []cluster.Node, filter map[string]struct{}) []cluster.Node {
if len(filter) == 0 {
return nodes
}
for i := 0; i < len(nodes); i++ {
if _, ok := filter[nodes[i].Address]; !ok {
nodes[i] = nodes[len(nodes)-1]
nodes = nodes[:len(nodes)-1]
i--
}
}
return nodes
}