-
Notifications
You must be signed in to change notification settings - Fork 11
/
executor.go
234 lines (203 loc) · 5.86 KB
/
executor.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
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
}
// 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 concrurency cannot be zero")
}
t := newProfiler()
return &innerExecutorImpl{
concurrency: concurrency,
pool: utils.NewCopool(concurrency),
wq: utils.NewQueue[*innerNode](),
wg: &sync.WaitGroup{},
profiler: t,
}
}
// Run start to schedule and execute taskflow
func (e *innerExecutorImpl) Run(tf *TaskFlow) Executor {
tf.forzen = true
e.scheduleGraph(tf.graph, nil)
return e
}
func (e *innerExecutorImpl) invokeGraph(g *eGraph, parentSpan *span) {
for {
g.scheCond.L.Lock()
for g.JoinCounter() != 0 && e.wq.Len() == 0 && !g.canceled.Load() {
g.scheCond.Wait()
}
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.JoinCounter() == 0 || g.canceled.Load() {
break
}
node := e.wq.Pop() // hang
e.invokeNode(node, parentSpan)
}
}
func (e *innerExecutorImpl) sche_successors(node *innerNode) {
candidate := make([]*innerNode, 0, len(node.successors))
for _, n := range node.successors {
if n.JoinCounter() == 0 || n.Typ == nodeCondition {
// deps all done or condition node
candidate = append(candidate, n)
}
}
slices.SortFunc(candidate, func(i, j *innerNode) int {
return cmp.Compare(i.priority, j.priority)
})
node.setup()
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("[recovered] node %s, panic: %s, stack: %s", node.name, r, debug.Stack())
} else {
e.profiler.AddSpan(&span) // remove canceled node span
}
node.drop()
e.sche_successors(node)
node.g.joinCounter.Decrease()
e.wg.Done()
node.g.scheCond.Signal()
}()
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("[recovered] subflow %s, panic: %s, 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(p.g, &span)
node.drop()
e.sche_successors(node)
node.g.joinCounter.Decrease()
e.wg.Done()
node.g.scheCond.Signal()
}()
node.state.Store(kNodeStateRunning)
if !p.g.instancelized {
p.handle(p)
}
p.g.instancelized = 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("[recovered] node %s, panic: %s, stack: %s", node.name, r, debug.Stack())
} else {
e.profiler.AddSpan(&span) // remove canceled node span
}
node.drop()
// e.sche_successors(node)
node.g.joinCounter.Decrease()
node.setup()
e.wg.Done()
node.g.scheCond.Signal()
}()
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) schedule(nodes ...*innerNode) {
for _, node := range nodes {
if node.g.canceled.Load() {
node.g.scheCond.Signal()
log.Printf("node %v is not scheduled, as graph %v is canceled\n", node.name, node.g.name)
return
}
node.g.joinCounter.Increase()
e.wg.Add(1)
e.wq.Put(node)
node.state.Store(kNodeStateWaiting)
node.g.scheCond.Signal()
}
}
func (e *innerExecutorImpl) scheduleGraph(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...)
e.invokeGraph(g, parentSpan)
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)
}