-
Notifications
You must be signed in to change notification settings - Fork 3
/
engine.go
385 lines (322 loc) · 8.7 KB
/
engine.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
package running
import (
"context"
"encoding/json"
"fmt"
"runtime/debug"
"strconv"
"sync"
"time"
)
type Engine struct {
StateBuilder func() State
builders map[string]BuildNodeFunc
buildersInfo map[string]NodeBuilderInfo
plans map[string]*Plan
pools map[string]*_WorkerPool
buildersLocker, plansLocker, poolsLocker sync.RWMutex
}
// RegisterNodeBuilder register node builder to engine
func (engine *Engine) RegisterNodeBuilder(name string, builder BuildNodeFunc) {
engine.buildersLocker.Lock()
engine.builders[name] = builder
engine.buildersLocker.Unlock()
}
// RegisterPlan register plan to engine
func (engine *Engine) RegisterPlan(name string, plan *Plan) error {
err := plan.Init()
if err != nil {
return err
}
engine.plansLocker.Lock()
engine.plans[name] = plan
engine.plansLocker.Unlock()
return nil
}
// LoadPlanFromJson load plan from json data
// name: name of plan to load
// jsonData: json data of plan
// prebuilt: prebuilt nodes, can be nil
func (engine *Engine) LoadPlanFromJson(name string, jsonData []byte, prebuilt []Node) error {
plan := &Plan{}
err := json.Unmarshal(jsonData, plan)
if err != nil {
return err
}
plan.prebuilt = make(map[string]Node)
for _, node := range prebuilt {
plan.prebuilt[node.Name()] = node
}
plan.version = strconv.FormatInt(time.Now().Unix(), 10)
engine.plansLocker.Lock()
engine.plans[name] = plan
engine.plansLocker.Unlock()
return nil
}
// ExecPlan exec plan register in engine
func (engine *Engine) ExecPlan(name string, ctx context.Context) <-chan Output {
output := Output{}
outputCh := make(chan Output, 1)
if ctx == nil {
ctx = context.Background()
}
go func() {
engine.plansLocker.RLock()
plan := engine.plans[name]
engine.plansLocker.RUnlock()
if plan == nil {
output.Err = ErrPlanNotFound
outputCh <- output
return
}
engine.poolsLocker.RLock()
pool := engine.pools[name]
engine.poolsLocker.RUnlock()
// set worker pool for new plan
if pool == nil {
engine.poolsLocker.Lock()
if engine.pools[name] == nil {
engine.pools[name] = &_WorkerPool{
Pool: sync.Pool{
New: func() interface{} {
worker, err := engine.buildWorker(name)
if err != nil {
return fmt.Errorf("%w, err: %s", ErrBuildWorkerFailed, err)
} else {
return worker
}
},
},
}
}
pool = engine.pools[name]
engine.poolsLocker.Unlock()
}
// get worker from pool and work
worker, err := pool.GetWorker()
if err != nil {
output.Err = err
outputCh <- output
return
}
output = <-worker.Work(ctx)
outputCh <- output
// if the plan has not been updated, reuse the worker
plan.locker.RLock()
version := plan.version
plan.locker.RUnlock()
if worker.Version == version {
pool.PutWorker(worker)
}
}()
return outputCh
}
// UpdatePlan update plan register in engine
func (engine *Engine) UpdatePlan(name string, update func(plan *Plan)) error {
engine.plansLocker.RLock()
plan := engine.plans[name]
engine.plansLocker.RUnlock()
plan.locker.Lock()
update(plan)
plan.locker.Unlock()
err := plan.Init()
if err != nil {
return err
}
return nil
}
func (engine *Engine) ExportPlan(name string) ([]byte, error) {
engine.plansLocker.RLock()
plan := engine.plans[name]
engine.plansLocker.RUnlock()
if plan != nil {
return json.Marshal(plan)
} else {
return nil, fmt.Errorf("plan: %s not found", name)
}
}
// WarmupPool warm up pool to avoid cold start
// name: plan name
// size: set size of worker buf queue
func (engine *Engine) WarmupPool(name string, size int) {
engine.poolsLocker.Lock()
if engine.pools[name] != nil {
engine.pools[name].Warmup(size)
}
engine.poolsLocker.Unlock()
}
// ClearPool clear worker pool of plan, invoke it to make plan effect immediately after update
// name: name of plan
func (engine *Engine) ClearPool(name string) {
engine.poolsLocker.Lock()
engine.pools[name] = nil
engine.poolsLocker.Unlock()
}
type NodeBuilderInfo struct {
Type string
From string
Note string
}
func (engine *Engine) SetNodeBuilderInfo(name string, info NodeBuilderInfo) {
engine.buildersLocker.Lock()
if engine.buildersInfo == nil {
engine.buildersInfo = make(map[string]NodeBuilderInfo)
}
engine.buildersInfo[name] = info
engine.buildersLocker.Unlock()
}
func (engine *Engine) buildWorker(name string) (worker *_Worker, err error) {
engine.plansLocker.RLock()
plan := engine.plans[name]
engine.plansLocker.RUnlock()
plan.locker.RLock()
defer plan.locker.RUnlock()
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("engine panic when build worker, panic info: %v", r)
debug.PrintStack()
return
}
}()
nodeMap := map[string]Node{}
reuse := map[string]Node{} // collect nodes which can be reused in the build nodes process
for _, v := range plan.graph.Vertexes {
if v.RefRoot.Virtual {
continue
}
nodeName := v.RefRoot.NodeName
nodeMap[nodeName], err = engine.buildNode(plan, nodeName, "", reuse)
if err != nil {
return
}
}
if len(reuse) > 0 {
plan.locker.RUnlock()
plan.locker.Lock()
for nodeName, node := range reuse {
plan.prebuilt[nodeName] = node
}
plan.locker.Unlock()
plan.locker.RLock()
}
worker = &_Worker{
Works: newWorkList(plan.graph),
Nodes: nodeMap,
StateBuilder: engine.StateBuilder,
Version: plan.version,
}
return
}
// buildNode build node by ref, props and prebuilt nodes.
// prefix will be added to node name,
// example: prefix = ClusterA, node name = SubNodeB => ClusterA.SubNodeB
func (engine *Engine) buildNode(plan *Plan, nodeName string, prefix string, reuse map[string]Node) (Node, error) {
engine.buildersLocker.RLock()
defer engine.buildersLocker.RUnlock()
root := plan.graph.NodeRefs[nodeName]
props := plan.props
prebuilt := plan.prebuilt
if props == nil {
props = EmptyProps{}
}
var rootNode Node
var err error
if prefix != "" {
nodeName = prefix + "." + root.NodeName
} else {
nodeName = root.NodeName
}
// prefer to use pre-built nodes
if node := getPrebuiltNode(prebuilt, nodeName); node != nil {
rootNode = node
} else if builder := engine.builders[root.NodeType]; builder != nil {
rootNode, err = builder(nodeName, props)
if err != nil {
return nil, fmt.Errorf("failed to build %s, err=%s", nodeName, err.Error())
}
} else {
return nil, fmt.Errorf("no builder found for type %s", root.NodeType)
}
if root.ReUse {
if _, ok := prebuilt[nodeName]; !ok {
if cloneableNode, ok := rootNode.(Cloneable); ok {
reuse[nodeName] = cloneableNode.Clone()
} else {
reuse[nodeName] = nil
}
}
}
// inject sub-nodes for cluster
if cluster, ok := rootNode.(Cluster); ok {
var subNodes []Node
// build sub-nodes just like root node(cluster)
for _, ref := range root.SubRefs {
if ref.Virtual {
continue
}
var subNode Node
if len(ref.SubRefs) == 0 {
if node := getPrebuiltNode(prebuilt, rootNode.Name()+"."+ref.NodeName); node != nil {
subNode = node
} else if builder := engine.builders[ref.NodeType]; builder != nil {
subNode, err = builder(rootNode.Name()+"."+ref.NodeName, props)
if err != nil {
return nil, fmt.Errorf("failed to build %s, err=%s", nodeName, err.Error())
}
} else {
return nil, fmt.Errorf("no builder found for type %s", ref.NodeType)
}
if ref.ReUse && prebuilt[rootNode.Name()+"."+ref.NodeName] == nil {
reuse[rootNode.Name()+"."+ref.NodeName] = subNode
}
subNode, err = engine.wrapNode(subNode, ref.Wrappers, props)
if err != nil {
return nil, err
}
subNodes = append(subNodes, subNode)
} else {
if prefix == "" {
prefix = root.NodeName
} else {
prefix = prefix + "." + root.NodeName
}
if subNode, err = engine.buildNode(plan, ref.NodeName, prefix, reuse); err != nil {
return nil, err
} else {
subNodes = append(subNodes, subNode)
}
}
}
cluster.Inject(subNodes)
}
rootNode, err = engine.wrapNode(rootNode, root.Wrappers, props)
if err != nil {
return nil, err
}
return rootNode, nil
}
func (engine *Engine) wrapNode(target Node, wrappers []string, props Props) (Node, error) {
for _, wrapper := range wrappers {
if builder := engine.builders[wrapper]; builder != nil {
node, err := builder(target.Name(), props)
if err != nil {
return nil, fmt.Errorf("failed to build wrapper, wrapper name: %s, target name: %s, err: %s",
wrapper, target.Name(), err)
}
if wrapperNode, ok := node.(Wrapper); ok {
wrapperNode.Wrap(target)
target = wrapperNode
}
}
}
return target, nil
}
func getPrebuiltNode(prebuilt map[string]Node, nodeName string) Node {
var node Node
if prebuilt[nodeName] != nil {
if cloneableNode, ok := prebuilt[nodeName].(Cloneable); ok {
node = cloneableNode.Clone()
}
}
return node
}