-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathexecutor.go
More file actions
265 lines (232 loc) · 6.75 KB
/
executor.go
File metadata and controls
265 lines (232 loc) · 6.75 KB
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
package gotaskflow
import (
"cmp"
"fmt"
"io"
"log"
"runtime/debug"
"slices"
"sync"
"time"
"github.com/noneback/go-taskflow/utils"
)
// Executor schedule and execute taskflow
type Executor interface {
Wait() // Wait block until all tasks finished
Profile(w io.Writer) error // Profile write flame graph raw text into w
Run(tf *TaskFlow) Executor // Run start to schedule and execute taskflow
}
type innerExecutorImpl struct {
concurrency uint
pool *utils.Copool
wq *utils.Queue[*innerNode]
wg *sync.WaitGroup
profiler *profiler
mu *sync.Mutex
}
// NewExecutor return a Executor with a specified max goroutine concurrency(recommend a value bigger than Runtime.NumCPU, **MUST** bigger than num(subflows). )
func NewExecutor(concurrency uint) Executor {
if concurrency == 0 {
panic("executor concurrency cannot be zero")
}
t := newProfiler()
return &innerExecutorImpl{
concurrency: concurrency,
pool: utils.NewCopool(concurrency),
wq: utils.NewQueue[*innerNode](false),
wg: &sync.WaitGroup{},
profiler: t,
mu: &sync.Mutex{},
}
}
// Run start to schedule and execute taskflow
func (e *innerExecutorImpl) Run(tf *TaskFlow) Executor {
tf.frozen = true
e.scheduleGraph(nil, tf.graph, nil)
return e
}
func (e *innerExecutorImpl) invokeGraph(g *eGraph, parentSpan *span) bool {
for {
g.scheCond.L.Lock()
e.mu.Lock()
for !g.recyclable() && e.wq.Len() == 0 && !g.canceled.Load() {
e.mu.Unlock()
g.scheCond.Wait()
e.mu.Lock()
}
g.scheCond.L.Unlock()
// tasks can only be executed after sched, and joinCounter incr when sched, so here no need to lock up.
if g.recyclable() || g.canceled.Load() {
e.mu.Unlock()
break
}
node := e.wq.Pop()
e.mu.Unlock()
e.invokeNode(node, parentSpan)
}
return !g.canceled.Load()
}
func (e *innerExecutorImpl) sche_successors(node *innerNode) {
candidate := make([]*innerNode, 0, len(node.successors))
for _, n := range node.successors {
n.mu.Lock()
if n.recyclable() && n.state.Load() == kNodeStateIdle {
// deps all done or condition node or task has been sched.
n.state.Store(kNodeStateWaiting)
candidate = append(candidate, n)
}
n.mu.Unlock()
}
slices.SortFunc(candidate, func(i, j *innerNode) int {
return cmp.Compare(i.priority, j.priority)
})
node.setup() // make node repeatable
e.schedule(candidate...)
}
func (e *innerExecutorImpl) invokeStatic(node *innerNode, parentSpan *span, p *Static) func() {
return func() {
span := span{extra: attr{
typ: nodeStatic,
name: node.name,
}, begin: time.Now(), parent: parentSpan}
defer func() {
span.cost = time.Since(span.begin)
if r := recover(); r != nil {
node.g.canceled.Store(true)
log.Printf("graph %v is canceled, since static node %v panics", node.g.name, node.name)
log.Printf("[recovered] static node %s, panic: %v, stack: %s", node.name, r, debug.Stack())
} else {
e.profiler.AddSpan(&span) // remove canceled node span
}
node.drop()
e.sche_successors(node)
node.g.deref()
e.wg.Done()
}()
if !node.g.canceled.Load() {
node.state.Store(kNodeStateRunning)
p.handle()
node.state.Store(kNodeStateFinished)
}
}
}
func (e *innerExecutorImpl) invokeSubflow(node *innerNode, parentSpan *span, p *Subflow) func() {
return func() {
span := span{extra: attr{
typ: nodeSubflow,
name: node.name,
}, begin: time.Now(), parent: parentSpan}
defer func() {
span.cost = time.Since(span.begin)
if r := recover(); r != nil {
log.Printf("graph %v is canceled, since subflow %v panics", node.g.name, node.name)
log.Printf("[recovered] subflow %s, panic: %v, stack: %s", node.name, r, debug.Stack())
node.g.canceled.Store(true)
p.g.canceled.Store(true)
} else {
e.profiler.AddSpan(&span) // remove canceled node span
}
e.scheduleGraph(node.g, p.g, &span)
node.drop()
e.sche_successors(node)
node.g.deref()
e.wg.Done()
}()
if !node.g.canceled.Load() {
node.state.Store(kNodeStateRunning)
if !p.g.instantiated {
p.handle(p)
}
p.g.instantiated = true
node.state.Store(kNodeStateFinished)
}
}
}
func (e *innerExecutorImpl) invokeCondition(node *innerNode, parentSpan *span, p *Condition) func() {
return func() {
span := span{extra: attr{
typ: nodeCondition,
name: node.name,
}, begin: time.Now(), parent: parentSpan}
defer func() {
span.cost = time.Since(span.begin)
if r := recover(); r != nil {
node.g.canceled.Store(true)
log.Printf("graph %v is canceled, since condition node %v panics", node.g.name, node.name)
log.Printf("[recovered] condition node %s, panic: %v, stack: %s", node.name, r, debug.Stack())
} else {
e.profiler.AddSpan(&span) // remove canceled node span
}
node.drop()
// e.sche_successors(node)
node.g.deref()
node.setup()
e.wg.Done()
}()
if !node.g.canceled.Load() {
node.state.Store(kNodeStateRunning)
choice := p.handle()
if choice > uint(len(p.mapper)) {
panic(fmt.Sprintln("condition task failed, successors of condition should be more than precondition choice", p.handle()))
}
// do choice and cancel others
node.state.Store(kNodeStateFinished)
e.schedule(p.mapper[choice])
}
}
}
func (e *innerExecutorImpl) invokeNode(node *innerNode, parentSpan *span) {
switch p := node.ptr.(type) {
case *Static:
e.pool.Go(e.invokeStatic(node, parentSpan, p))
case *Subflow:
e.pool.Go(e.invokeSubflow(node, parentSpan, p))
case *Condition:
e.pool.Go(e.invokeCondition(node, parentSpan, p))
default:
panic("unsupported node")
}
}
func (e *innerExecutorImpl) pushIntoQueue(node *innerNode) {
e.mu.Lock()
defer e.mu.Unlock()
e.wq.Put(node)
}
func (e *innerExecutorImpl) schedule(nodes ...*innerNode) {
for _, node := range nodes {
if node.g.canceled.Load() {
// no need
node.g.scheCond.L.Lock()
node.g.scheCond.Signal()
node.g.scheCond.L.Unlock()
log.Printf("node %v is not scheduled, since graph %v is canceled\n", node.name, node.g.name)
return
}
e.wg.Add(1)
node.g.scheCond.L.Lock()
node.g.ref()
e.pushIntoQueue(node)
node.g.scheCond.Signal()
node.g.scheCond.L.Unlock()
}
}
func (e *innerExecutorImpl) scheduleGraph(parentg, g *eGraph, parentSpan *span) {
g.setup()
slices.SortFunc(g.entries, func(i, j *innerNode) int {
return cmp.Compare(i.priority, j.priority)
})
e.schedule(g.entries...)
if !e.invokeGraph(g, parentSpan) && parentg != nil {
parentg.canceled.Store(true)
log.Printf("graph %s canceled, since subgraph %s is canceled\n", parentg.name, g.name)
}
g.scheCond.Signal()
}
// Wait: block until all tasks finished
func (e *innerExecutorImpl) Wait() {
e.wg.Wait()
}
// Profile write flame graph raw text into w
func (e *innerExecutorImpl) Profile(w io.Writer) error {
return e.profiler.draw(w)
}