-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.go
212 lines (177 loc) · 5.27 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
package go_scheduler
import (
"context"
"errors"
"runtime"
dag "github.com/yashbhutwala/go-directed-acyclic-graph"
)
// Scheduler related errors.
var (
ErrCircularDependency = errors.New("A task has a circular dependency")
ErrOrderFailure = errors.New("A problem occurred sorting the tasks in the correct order")
)
// Scheduler is a concurrent task scheduler.
type Scheduler struct {
options *Options
graph *dag.DirectedGraph
dirty bool
tasks [][]*Task
runner *taskRunner
}
// New returns a concurrent task scheduler.
// Tasks may be dependent on each, being sorted into a layered topological order
// using the Coffman-Graham algorithm.
func New(options ...Option) *Scheduler {
scheduler := &Scheduler{
graph: dag.NewDirectedGraph(),
tasks: make([][]*Task, 0),
runner: newTaskRunner(),
options: &Options{
ConcurrentTasks: runtime.NumCPU(),
},
}
for _, option := range options {
option(scheduler.options)
}
return scheduler
}
// Tasks returns a list of the tasks registered with the scheduler.
func (s *Scheduler) Tasks() []*Task {
nodes := s.graph.Nodes()
tasks := make([]*Task, 0, len(nodes))
for _, node := range nodes {
if task, ok := node.(*Task); ok {
tasks = append(tasks, task)
}
}
return tasks
}
// TaskCount returns the number of tasks registered with the scheduler.
func (s *Scheduler) TaskCount() int {
return s.graph.NodeCount()
}
// AddTask registers the function with the scheduler and returns a Task.
func (s *Scheduler) AddTask(function TaskFunc) *Task {
task := newTask(s, function)
s.graph.AddNode(task)
s.invalidate()
return task
}
// RemoveTask removes the specified task from the scheduler.
func (s *Scheduler) RemoveTask(task *Task) {
s.graph.RemoveNode(task)
s.invalidate()
}
// RemoveTasks removes the specified tasks from the scheduler.
func (s *Scheduler) RemoveTasks(tasks ...*Task) {
nodes := make([]interface{}, len(tasks))
for i, task := range tasks {
nodes[i] = task
}
s.graph.RemoveNodes(nodes...)
s.invalidate()
}
// Dependencies lists the tasks the specified task depends on.
func (s *Scheduler) Dependencies(task *Task) []*Task {
edges := s.graph.IncomingEdges(task)
dependencies := make([]*Task, len(edges))
for i, edge := range edges {
dependencies[i] = edge.(*Task)
}
return dependencies
}
// DependencyCount returns the number of tasks the specified task depends on.
func (s *Scheduler) DependencyCount(task *Task) int {
return s.graph.IncomingEdgeCount(task)
}
// AddDependency creates a dependency between the specified task itself
// and the dependency task.
// When ran, the scheduler ensures the dependency task is executed first.
func (s *Scheduler) AddDependency(task *Task, dependency *Task) {
s.graph.AddEdge(dependency, task)
s.invalidate()
}
// RemoveDependency removes the dependency between the specified task itself
// and the dependency task.
func (s *Scheduler) RemoveDependency(task *Task, dependency *Task) {
s.graph.RemoveEdge(dependency, task)
s.invalidate()
}
func (s *Scheduler) invalidate() {
s.dirty = true
}
func (s *Scheduler) resizeLevels(count int) {
currentCount := len(s.tasks)
// extend or shrink the task levels slice depending on the difference
// between the new and old level counts
if count < currentCount {
for i := count; i < currentCount; i++ {
s.tasks[i] = nil
}
s.tasks = s.tasks[:count]
} else if count > currentCount {
extendBy := count - currentCount
s.tasks = append(s.tasks, make([][]*Task, extendBy)...)
}
}
func (s *Scheduler) resizeTasks(level, count int) {
currentCount := len(s.tasks[level])
// extend or shrink the tasks slice depending on the difference
// between the new and old concurrency counts
if count < currentCount {
for j := count; j < currentCount; j++ {
s.tasks[level][j] = nil
}
s.tasks[level] = s.tasks[level][:count]
} else if count > currentCount {
extendBy := count - currentCount
s.tasks[level] = append(s.tasks[level], make([]*Task, extendBy)...)
}
}
func (s *Scheduler) sort() error {
// sort the tasks into a layered topological order using the Coffman-Graham algorithm
levels, err := s.graph.CoffmanGrahamSort(s.options.ConcurrentTasks)
if err != nil {
if err == dag.ErrCyclicGraph {
return ErrCircularDependency
}
if err == dag.ErrDependencyOrder {
return ErrOrderFailure
}
return err
}
// resize the top-level tasks slice to extend or shrink to the new number of levels
s.resizeLevels(len(levels))
for i, tasks := range levels {
if s.tasks[i] == nil {
s.tasks[i] = make([]*Task, len(tasks))
} else {
// resize the second-level tasks slice to extend or shrink to the new
// number of tasks within the set
s.resizeTasks(i, len(tasks))
}
for j, task := range tasks {
s.tasks[i][j] = task.(*Task)
}
}
return nil
}
// Run executes the scheduler's tasks.
func (s *Scheduler) Run(ctx context.Context) error {
// check whether the scheduler's state has changed since the last time
// it was ran, in which we case we need to sort the task graph
if s.dirty {
if err := s.sort(); err != nil {
return err
}
s.dirty = false
}
// run each set of tasks, the count of the tasks within each set is guaranteed
// to be equal or less than options.ConcurrentTasks
for _, set := range s.tasks {
if err := s.runner.Run(ctx, set); err != nil {
return err
}
}
return nil
}