-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdag.go
131 lines (119 loc) · 2.83 KB
/
dag.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
package godag
import (
"context"
"sync"
"time"
// "fmt"
)
// default go impl, can replace by other go routine pool impl
var Go = func (f func()) {
go f()
}
type Op interface {
// global: global variable to pass through DAG
// input: the output generated by parent (the order is the same as node.prev)
Process(ctx context.Context, global interface{}, input ...interface{}) interface{} // pass input with the order of prev
}
type StateKey string
const NodeID = "__nodeID__"
type DAG struct {
startNode *Node
mu sync.Mutex
activeNum int
taskChan chan *Node
doneChan chan struct{}
stateKeeper StateKeeper
}
func (p *DAG) Init(startNode *Node, stateKeeper StateKeeper) bool {
p.startNode = startNode
if stateKeeper == nil {
p.stateKeeper = NewDefaultStateKeeper()
} else {
p.stateKeeper = stateKeeper
}
p.activeNum = 1
p.taskChan = make(chan *Node)
p.doneChan = make(chan struct{})
Go(func() {
p.taskChan <- startNode
})
return true
}
func (p *DAG) Execute(ctx context.Context) {
for {
select {
case node := <-p.taskChan:
Go(func (){
p.processNode(ctx, node)
})
case <-p.doneChan:
return
}
}
}
func (p *DAG) processNode(ctx context.Context, node *Node) {
if ctx == nil {
ctx = context.Background()
}
if node.timeout > 0 {
// fmt.Println("node timeout = ", node.id, node.timeout)
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, node.timeout)
defer cancel()
}
doneChan := make(chan struct{})
startTime := time.Now()
Go(func() {
if node.op != nil {
args := make([]interface{}, len(node.prev))
for idx := range node.prev {
// NOTE: the order of prev will result the order of args passed to op
args[idx] = p.stateKeeper.GetInput(node.prev[idx].id, node.id) // will get the parent output as input of current
}
ctx = context.WithValue(ctx, StateKey(NodeID), node.id)
global := p.stateKeeper.GetGlobal()
output := node.op.Process(ctx, global, args...)
if !node.isCanceled { // if timeout, no need to save output
p.stateKeeper.SetOutput(node.id, output)
}
}
close(doneChan) // close can make chan readable
})
if node.timeout > 0 {
select {
case <-ctx.Done():
// fmt.Println("timeout", node, ctx)
node.isCanceled = true
case <-doneChan:
node.isCanceled = false
}
} else {
<- doneChan
node.isCanceled = false
}
node.costTime = time.Now().Sub(startTime)
Go(func() {
for _, nextOne := range node.next {
p.mu.Lock()
nextOne.indegree--
indegree := nextOne.indegree
if indegree == 0 {
p.activeNum++ // should add before chan put
}
p.mu.Unlock()
if indegree == 0 {
p.taskChan <- nextOne
}
}
p.mu.Lock()
p.activeNum--
activeNum := p.activeNum
p.mu.Unlock()
if activeNum == 0 {
close(p.doneChan)
}
})
}
func (d *DAG) GetStateKeeper() StateKeeper {
return d.stateKeeper
}