forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
445 lines (385 loc) · 11.4 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package scheduler
import (
"container/heap"
"container/list"
"time"
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/log"
"github.com/docker/swarmkit/manager/state"
"github.com/docker/swarmkit/manager/state/store"
"github.com/docker/swarmkit/protobuf/ptypes"
"golang.org/x/net/context"
)
type schedulingDecision struct {
old *api.Task
new *api.Task
}
// Scheduler assigns tasks to nodes.
type Scheduler struct {
store *store.MemoryStore
unassignedTasks *list.List
// preassignedTasks already have NodeID, need resource validation
preassignedTasks map[string]*api.Task
nodeHeap nodeHeap
allTasks map[string]*api.Task
pipeline *Pipeline
// stopChan signals to the state machine to stop running
stopChan chan struct{}
// doneChan is closed when the state machine terminates
doneChan chan struct{}
// This currently exists only for benchmarking. It tells the scheduler
// scan the whole heap instead of taking the minimum-valued node
// blindly.
scanAllNodes bool
}
// New creates a new scheduler.
func New(store *store.MemoryStore) *Scheduler {
return &Scheduler{
store: store,
unassignedTasks: list.New(),
preassignedTasks: make(map[string]*api.Task),
allTasks: make(map[string]*api.Task),
stopChan: make(chan struct{}),
doneChan: make(chan struct{}),
pipeline: NewPipeline(),
}
}
func (s *Scheduler) setupTasksList(tx store.ReadTx) error {
tasks, err := store.FindTasks(tx, store.All)
if err != nil {
return err
}
tasksByNode := make(map[string]map[string]*api.Task)
for _, t := range tasks {
// Ignore all tasks that have not reached ALLOCATED
// state.
if t.Status.State < api.TaskStateAllocated {
continue
}
s.allTasks[t.ID] = t
if t.NodeID == "" {
s.enqueue(t)
continue
}
// preassigned tasks need to validate resource requirement on corresponding node
if t.Status.State == api.TaskStateAllocated {
s.preassignedTasks[t.ID] = t
continue
}
if tasksByNode[t.NodeID] == nil {
tasksByNode[t.NodeID] = make(map[string]*api.Task)
}
tasksByNode[t.NodeID][t.ID] = t
}
if err := s.buildNodeHeap(tx, tasksByNode); err != nil {
return err
}
return nil
}
// Run is the scheduler event loop.
func (s *Scheduler) Run(ctx context.Context) error {
defer close(s.doneChan)
updates, cancel, err := store.ViewAndWatch(s.store, s.setupTasksList)
if err != nil {
log.G(ctx).WithError(err).Errorf("snapshot store update failed")
return err
}
defer cancel()
// Validate resource for tasks from preassigned tasks
// do this before other tasks because preassigned tasks like
// global service should start before other tasks
s.processPreassignedTasks(ctx)
// Queue all unassigned tasks before processing changes.
s.tick(ctx)
pendingChanges := 0
// Watch for changes.
for {
select {
case event := <-updates:
switch v := event.(type) {
case state.EventCreateTask:
pendingChanges += s.createTask(ctx, v.Task)
case state.EventUpdateTask:
pendingChanges += s.updateTask(ctx, v.Task)
case state.EventDeleteTask:
s.deleteTask(ctx, v.Task)
case state.EventCreateNode:
s.createOrUpdateNode(v.Node)
pendingChanges++
case state.EventUpdateNode:
s.createOrUpdateNode(v.Node)
pendingChanges++
case state.EventDeleteNode:
s.nodeHeap.remove(v.Node.ID)
case state.EventCommit:
if len(s.preassignedTasks) > 0 {
s.processPreassignedTasks(ctx)
}
if pendingChanges > 0 {
s.tick(ctx)
pendingChanges = 0
}
}
case <-s.stopChan:
return nil
}
}
}
// Stop causes the scheduler event loop to stop running.
func (s *Scheduler) Stop() {
close(s.stopChan)
<-s.doneChan
}
// enqueue queues a task for scheduling.
func (s *Scheduler) enqueue(t *api.Task) {
s.unassignedTasks.PushBack(t)
}
func (s *Scheduler) createTask(ctx context.Context, t *api.Task) int {
// Ignore all tasks that have not reached ALLOCATED
// state, and tasks that no longer consume resources.
if t.Status.State < api.TaskStateAllocated || t.Status.State > api.TaskStateRunning {
return 0
}
s.allTasks[t.ID] = t
if t.NodeID == "" {
// unassigned task
s.enqueue(t)
return 1
}
if t.Status.State == api.TaskStateAllocated {
s.preassignedTasks[t.ID] = t
// preassigned tasks do not contribute to running tasks count
return 0
}
nodeInfo, err := s.nodeHeap.nodeInfo(t.NodeID)
if err == nil && nodeInfo.addTask(t) {
s.nodeHeap.updateNode(nodeInfo)
}
return 0
}
func (s *Scheduler) updateTask(ctx context.Context, t *api.Task) int {
// Ignore all tasks that have not reached ALLOCATED
// state.
if t.Status.State < api.TaskStateAllocated {
return 0
}
oldTask := s.allTasks[t.ID]
// Ignore all tasks that have not reached ALLOCATED
// state, and tasks that no longer consume resources.
if t.Status.State > api.TaskStateRunning {
if oldTask != nil {
s.deleteTask(ctx, oldTask)
}
return 1
}
if t.NodeID == "" {
// unassigned task
if oldTask != nil {
s.deleteTask(ctx, oldTask)
}
s.allTasks[t.ID] = t
s.enqueue(t)
return 1
}
if t.Status.State == api.TaskStateAllocated {
if oldTask != nil {
s.deleteTask(ctx, oldTask)
}
s.allTasks[t.ID] = t
s.preassignedTasks[t.ID] = t
// preassigned tasks do not contribute to running tasks count
return 0
}
s.allTasks[t.ID] = t
nodeInfo, err := s.nodeHeap.nodeInfo(t.NodeID)
if err == nil && nodeInfo.addTask(t) {
s.nodeHeap.updateNode(nodeInfo)
}
return 0
}
func (s *Scheduler) deleteTask(ctx context.Context, t *api.Task) {
delete(s.allTasks, t.ID)
delete(s.preassignedTasks, t.ID)
nodeInfo, err := s.nodeHeap.nodeInfo(t.NodeID)
if err == nil && nodeInfo.removeTask(t) {
s.nodeHeap.updateNode(nodeInfo)
}
}
func (s *Scheduler) createOrUpdateNode(n *api.Node) {
nodeInfo, _ := s.nodeHeap.nodeInfo(n.ID)
var resources api.Resources
if n.Description != nil && n.Description.Resources != nil {
resources = *n.Description.Resources
// reconcile resources by looping over all tasks in this node
for _, task := range nodeInfo.Tasks {
reservations := taskReservations(task.Spec)
resources.MemoryBytes -= reservations.MemoryBytes
resources.NanoCPUs -= reservations.NanoCPUs
}
}
nodeInfo.Node = n
nodeInfo.AvailableResources = resources
s.nodeHeap.addOrUpdateNode(nodeInfo)
}
func (s *Scheduler) processPreassignedTasks(ctx context.Context) {
schedulingDecisions := make(map[string]schedulingDecision, len(s.preassignedTasks))
for _, t := range s.preassignedTasks {
newT := s.taskFitNode(ctx, t, t.NodeID)
if newT == nil {
continue
}
schedulingDecisions[t.ID] = schedulingDecision{old: t, new: newT}
}
successful, failed := s.applySchedulingDecisions(ctx, schedulingDecisions)
for _, decision := range successful {
delete(s.preassignedTasks, decision.old.ID)
}
for _, decision := range failed {
s.allTasks[decision.old.ID] = decision.old
nodeInfo, err := s.nodeHeap.nodeInfo(decision.new.NodeID)
if err == nil && nodeInfo.removeTask(decision.new) {
s.nodeHeap.updateNode(nodeInfo)
}
}
}
// tick attempts to schedule the queue.
func (s *Scheduler) tick(ctx context.Context) {
schedulingDecisions := make(map[string]schedulingDecision, s.unassignedTasks.Len())
var next *list.Element
for e := s.unassignedTasks.Front(); e != nil; e = next {
next = e.Next()
id := e.Value.(*api.Task).ID
if _, ok := schedulingDecisions[id]; ok {
s.unassignedTasks.Remove(e)
continue
}
t := s.allTasks[e.Value.(*api.Task).ID]
if t == nil || t.NodeID != "" {
// task deleted or already assigned
s.unassignedTasks.Remove(e)
continue
}
if newT := s.scheduleTask(ctx, t); newT != nil {
schedulingDecisions[id] = schedulingDecision{old: t, new: newT}
s.unassignedTasks.Remove(e)
}
}
_, failed := s.applySchedulingDecisions(ctx, schedulingDecisions)
for _, decision := range failed {
s.allTasks[decision.old.ID] = decision.old
nodeInfo, err := s.nodeHeap.nodeInfo(decision.new.NodeID)
if err == nil && nodeInfo.removeTask(decision.new) {
s.nodeHeap.updateNode(nodeInfo)
}
// enqueue task for next scheduling attempt
s.enqueue(decision.old)
}
}
func (s *Scheduler) applySchedulingDecisions(ctx context.Context, schedulingDecisions map[string]schedulingDecision) (successful, failed []schedulingDecision) {
if len(schedulingDecisions) == 0 {
return
}
successful = make([]schedulingDecision, 0, len(schedulingDecisions))
// Apply changes to master store
applied, err := s.store.Batch(func(batch *store.Batch) error {
for len(schedulingDecisions) > 0 {
err := batch.Update(func(tx store.Tx) error {
// Update exactly one task inside this Update
// callback.
for taskID, decision := range schedulingDecisions {
delete(schedulingDecisions, taskID)
t := store.GetTask(tx, taskID)
if t == nil {
// Task no longer exists. Do nothing.
failed = append(failed, decision)
continue
}
if err := store.UpdateTask(tx, decision.new); err != nil {
log.G(ctx).Debugf("scheduler failed to update task %s; will retry", taskID)
failed = append(failed, decision)
continue
}
successful = append(successful, decision)
return nil
}
return nil
})
if err != nil {
return err
}
}
return nil
})
if err != nil {
log.G(ctx).WithError(err).Error("scheduler tick transaction failed")
failed = append(failed, successful[applied:]...)
successful = successful[:applied]
}
return
}
// taskFitNode checks if a node has enough resource to accommodate a task
func (s *Scheduler) taskFitNode(ctx context.Context, t *api.Task, nodeID string) *api.Task {
nodeInfo, err := s.nodeHeap.nodeInfo(nodeID)
if err != nil {
// node does not exist in heap (it may have been deleted)
return nil
}
s.pipeline.SetTask(t)
if !s.pipeline.Process(&nodeInfo) {
// this node cannot accommodate this task
return nil
}
newT := *t
newT.Status = api.TaskStatus{
State: api.TaskStateAssigned,
Timestamp: ptypes.MustTimestampProto(time.Now()),
Message: "scheduler confirmed task can run on preassigned node",
}
s.allTasks[t.ID] = &newT
if nodeInfo.addTask(&newT) {
s.nodeHeap.updateNode(nodeInfo)
}
return &newT
}
// scheduleTask schedules a single task.
func (s *Scheduler) scheduleTask(ctx context.Context, t *api.Task) *api.Task {
s.pipeline.SetTask(t)
n, _ := s.nodeHeap.findMin(s.pipeline.Process, s.scanAllNodes)
if n == nil {
log.G(ctx).WithField("task.id", t.ID).Debug("No suitable node available for task")
return nil
}
log.G(ctx).WithField("task.id", t.ID).Debugf("Assigning to node %s", n.ID)
newT := *t
newT.NodeID = n.ID
newT.Status = api.TaskStatus{
State: api.TaskStateAssigned,
Timestamp: ptypes.MustTimestampProto(time.Now()),
Message: "scheduler assigned task to node",
}
s.allTasks[t.ID] = &newT
nodeInfo, err := s.nodeHeap.nodeInfo(n.ID)
if err == nil && nodeInfo.addTask(&newT) {
s.nodeHeap.updateNode(nodeInfo)
}
return &newT
}
func (s *Scheduler) buildNodeHeap(tx store.ReadTx, tasksByNode map[string]map[string]*api.Task) error {
nodes, err := store.FindNodes(tx, store.All)
if err != nil {
return err
}
s.nodeHeap.alloc(len(nodes))
i := 0
for _, n := range nodes {
var resources api.Resources
if n.Description != nil && n.Description.Resources != nil {
resources = *n.Description.Resources
}
s.nodeHeap.heap = append(s.nodeHeap.heap, newNodeInfo(n, tasksByNode[n.ID], resources))
s.nodeHeap.index[n.ID] = i
i++
}
heap.Init(&s.nodeHeap)
return nil
}