/
scheduler.go
366 lines (299 loc) · 8.42 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
package main
import (
"errors"
"fmt"
log "minilog"
"sort"
"strings"
)
type Scheduler struct {
queue []*QueuedVMs
hosts []*HostStats
hostSortBy // embed
// output from scheduler map of queued vms, indexed by host that they
// should be scheduled on
res map[string][]*QueuedVMs
// colocated are VMs that need to be scheduled with another VM, indexed by
// the name of the VM to be colocated with
colocated map[string][]*QueuedVMs
}
// hostSortBy defines the ordering of hosts based on some notion of load
type hostSortBy func(h1, h2 *HostStats) bool
// hostSorter acts a minheap so that the least-loaded host is in position 0.
type hostSorter struct {
hosts []*HostStats
by hostSortBy
}
var hostSortByFns = map[string]hostSortBy{
"netcommit": networkCommit,
"cpucommit": cpuCommit,
"memcommit": memoryCommit,
}
func (s *Scheduler) Schedule() (map[string][]*QueuedVMs, error) {
if len(s.hosts) == 0 {
return nil, errors.New("no hosts to schedule VMs on")
}
if len(s.hosts) == 1 {
log.Warn("only one host in namespace, scheduling all VMs on it")
res := map[string][]*QueuedVMs{
s.hosts[0].Name: s.queue,
}
return res, nil
}
s.res = map[string][]*QueuedVMs{}
s.colocated = map[string][]*QueuedVMs{}
for _, q := range s.queue {
// resolve `localhost` to actual hostname
if q.Schedule == Localhost {
q.Schedule = hostname
}
// ensure we can get host stats to simplify scheduling loop
if host := q.Schedule; host != "" {
// host should exist
if s.findHostStats(host) == nil {
return nil, fmt.Errorf("VM scheduled on unknown host: `%v`", host)
}
}
// found "floating" VM
if q.Colocate != "" && q.Schedule == "" {
s.colocated[q.Colocate] = append(s.colocated[q.Colocate], q)
}
}
// update colocatedCounts so that we can include it in the sorting
for _, q := range s.queue {
q.colocatedCounts = map[string]int{}
q.colocatedCount = 0
for _, name := range q.Names {
if _, ok := s.colocated[name]; !ok {
continue
}
v := len(s.colocated[name])
q.colocatedCounts[name] = v
q.colocatedCount += v
}
}
// perform initial sort of queued VMs and hosts
sort.Slice(s.queue, func(i, j int) bool {
return s.queue[i].Less(s.queue[j])
})
s.hostSortBy.Sort(s.hosts)
for _, q := range s.queue {
if q.Colocate != "" && q.Schedule == "" {
// floating, ignore
continue
}
for _, name := range q.Names {
// least loaded host is at position zero
host := s.hosts[0]
if v := q.Schedule; v != "" {
// find the specified host
host = s.findHostStats(v)
}
if err := s.add(host, name, q); err != nil {
s.dumpSchedule()
return nil, err
}
s.hostSortBy.Update(s.hosts, host.Name)
}
}
// floating VMs never found a home... should probably be an error, right?
if len(s.colocated) != 0 {
names := []string{}
for k := range s.colocated {
names = append(names, k)
}
sort.Strings(names)
return nil, fmt.Errorf("nonexistent colocate VMs: [%v]", strings.Join(names, ", "))
}
return s.res, nil
}
// helper to write schedule to log
func (s *Scheduler) dumpSchedule() {
log.Info("partial schedule:")
for host := range s.res {
names := []string{}
for _, q := range s.res[host] {
names = append(names, q.Names...)
}
log.Info("VMs scheduled on %v: %v", host, names)
}
}
// helper to find host stats by name
func (s *Scheduler) findHostStats(host string) *HostStats {
for _, v := range s.hosts {
if v.Name == host {
return v
}
}
return nil
}
// add a VM to the given host, checking and adjusting limits if necessary
func (s *Scheduler) add(host *HostStats, name string, q *QueuedVMs) error {
limit := int(q.Coschedule)
if limit != -1 {
// number of peers is one less than the number of VMs that we
// should launch on the node
limit := limit + 1
if host.Limit == -1 {
// set initial limit
log.Debug("set initial limit on %v to %v", host.Name, limit)
host.Limit = limit
} else if limit < host.Limit {
// lower the limit for the host
log.Debug("lower limit on %v from %v to %v", host.Name, host.Limit, limit)
host.Limit = limit
}
}
// update commit based on this VM's specs
host.increment(q.VMConfig)
// schedule all floating VMs on this host as well
for _, q := range s.colocated[name] {
for _, name2 := range q.Names {
log.Debug("colocating %v with %v", name2, name)
if err := s.add(host, name2, q); err != nil {
return err
}
}
}
// we no longer need to track these floating VMs
delete(s.colocated, name)
if host.Limit != -1 && host.VMs > host.Limit {
return fmt.Errorf("too many VMs scheduled on %v for coschedule requirement of %v", host.Name, host.Limit)
}
// create copy of q
q2 := *q
q2.Names = []string{name}
//log.Debug("scheduling VM on %v: %v", host.Name, name)
s.res[host.Name] = append(s.res[host.Name], &q2)
return nil
}
func (by hostSortBy) Sort(hosts []*HostStats) {
h := &hostSorter{
hosts: hosts,
by: by,
}
// Floyd method -- start at the lowest levels and siftDown in each subtree.
for i := (h.Len() - 1) / 2; i >= 0; i-- {
h.siftDown(i)
}
}
// Update sort order after specified host has been modified. Should only be
// used when a single host is updated.
func (by hostSortBy) Update(hosts []*HostStats, name string) {
h := &hostSorter{
hosts: hosts,
by: by,
}
for i, host := range hosts {
if host.Name == name {
h.siftDown(i)
return
}
}
}
// siftDown is a slightly tweaked version of the siftDown function from the
// sort package.
func (h *hostSorter) siftDown(root int) {
for {
child := 2*root + 1
if child >= h.Len() {
return
}
// figure out if left or right child is bigger, proceed with the
// smaller of the two
if child+1 < h.Len() && !h.Less(child, child+1) {
child++
}
// already meet heap requirement -- done
if h.Less(root, child) {
return
}
h.Swap(root, child)
root = child
}
}
func (h *hostSorter) Len() int {
return len(h.hosts)
}
func (h *hostSorter) Swap(i, j int) {
h.hosts[i], h.hosts[j] = h.hosts[j], h.hosts[i]
}
func (h *hostSorter) Less(i, j int) bool {
return h.by(h.hosts[i], h.hosts[j])
}
// Less function for sorting QueuedVMs such that:
// * host and a coschedule limit come first
// * then those that specify a host
// * then those that specify a coschedule limit
// * then those that have more colocated VMs
// * then those that specify neither
func (q *QueuedVMs) Less(q2 *QueuedVMs) bool {
host, host2 := q.Schedule, q2.Schedule
// VMs with specified hosts should be less than those that are unspecified.
if host != host2 {
// check if only one specified
if host != "" || host2 != "" {
return host != ""
}
return host < host2
}
// VMs with specified peers should be less than those that are unspecified.
// Within VMs that have coschedule limits, we want to process the lower
// coschedule limits first.
if q.Coschedule != q2.Coschedule {
// check if only one specified
if q.Coschedule == -1 || q2.Coschedule == -1 {
return q.Coschedule != -1
}
return q.Coschedule < q2.Coschedule
}
// VMs with more colocated VMs are next
if q.colocatedCount != q2.colocatedCount {
return q.colocatedCount > q2.colocatedCount
}
// We don't really care about the ordering of the rest but we should
// probably schedule larger groups first.
return len(q.Names) > len(q2.Names)
}
func (s *HostStats) increment(config VMConfig) {
s.VMs += 1
s.CPUCommit += config.VCPUs
s.MemCommit += config.Memory
s.NetworkCommit += len(config.Networks)
}
// cpuCommit tests whether h1 < h2.
func cpuCommit(h1, h2 *HostStats) bool {
// fully loaded host is always greater
if full := h1.IsFull(); full != h2.IsFull() {
return !full
}
r1 := float64(h1.CPUCommit) / float64(h1.CPUs)
r2 := float64(h2.CPUCommit) / float64(h2.CPUs)
return r1 < r2
}
// memoryCommit tests whether h1 < h2.
func memoryCommit(h1, h2 *HostStats) bool {
// fully loaded host is always greater
if full := h1.IsFull(); full != h2.IsFull() {
return !full
}
r1 := float64(h1.MemCommit) / float64(h1.MemTotal)
r2 := float64(h2.MemCommit) / float64(h2.MemTotal)
return r1 < r2
}
// networkCommit tests whether h1 < h2.
func networkCommit(h1, h2 *HostStats) bool {
// fully loaded host is always greater
if full := h1.IsFull(); full != h2.IsFull() {
return !full
}
return h1.NetworkCommit < h2.NetworkCommit
}
func schedule(q []*QueuedVMs, hosts []*HostStats, by hostSortBy) (map[string][]*QueuedVMs, error) {
s := &Scheduler{
queue: q,
hosts: hosts,
hostSortBy: by,
}
return s.Schedule()
}