-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag.go
195 lines (172 loc) · 4.9 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
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
package core
import (
"encoding/json"
"fmt"
"time"
"github.com/AnubhavUjjawal/MoMo/logger"
)
// Dict type, can be used to hold arbitrary data.
type Dict map[string]interface{}
// DAG is a basic collection of tasks that we want to run, organised in a way
// that specifies their dependencies and relationships.
type DAG struct {
Name string
Schedule time.Duration
DefaultArgs Dict
Description string
StartDate time.Time
tasks map[string]TaskInterface
}
type DagRunType struct {
SchTime int64
Completed bool
}
func (dr *DagRunType) MarshalBinary() ([]byte, error) {
return json.Marshal(dr)
}
func (dr *DagRunType) UnmarshalBinary(val []byte) error {
return json.Unmarshal(val, dr)
}
func (dag *DAG) GetTasks() map[string]TaskInterface {
return dag.tasks
}
func (dag *DAG) SetTasks(tasks map[string]TaskInterface) {
dag.tasks = tasks
}
func (dag *DAG) DetectCycles() error {
unvisitedSet := make(map[string]struct{})
visitingSet := make(map[string]struct{})
visitedSet := make(map[string]struct{})
for taskName := range dag.tasks {
unvisitedSet[taskName] = struct{}{}
}
var nextEl string
for len(unvisitedSet) > 0 {
for el := range unvisitedSet {
nextEl = el
break
}
if dag.detectCycleDFS(nextEl, &unvisitedSet, &visitingSet, &visitedSet) {
return fmt.Errorf("cycle in DAG %s", dag.Name)
}
}
return nil
}
func (dag *DAG) MarshalJSON() ([]byte, error) {
tasks := make([]string, 0)
for task := range dag.tasks {
tasks = append(tasks, task)
}
return json.Marshal(map[string]interface{}{
"Name": dag.Name,
"Schedule": dag.Schedule,
"DefaultArgs": dag.DefaultArgs,
"Description": dag.Description,
"StartDate": dag.StartDate,
"tasks": tasks,
})
}
// UnmarshalJSON contains the logic of Unmarshaling the dag marshaled by
// MarshalDag. Check BaseTask.MarshalTask as well.
func (dag *DAG) UnmarshalJSON(b []byte) error {
// sugar := logger.GetSugaredLogger()
var data map[string]interface{}
err := json.Unmarshal(b, &data)
if err != nil {
return err
}
dag.Name = data["Name"].(string)
dag.Schedule = time.Duration(data["Schedule"].(float64))
dag.DefaultArgs = Dict(data["DefaultArgs"].(map[string]interface{}))
dag.Description = data["Description"].(string)
dag.StartDate, err = time.Parse(time.RFC3339, data["StartDate"].(string))
dag.tasks = make(map[string]TaskInterface)
if err != nil {
panic(err)
}
tasks := data["tasks"].([]interface{})
for _, task := range tasks {
task := task.(string)
dag.tasks[task] = nil
}
return nil
}
func (dag *DAG) detectCycleDFS(nextEl string, unvisitedSet, visitingSet, visitedSet *map[string]struct{}) bool {
// move nextEl from unvisited to visiting
delete(*unvisitedSet, nextEl)
(*visitingSet)[nextEl] = struct{}{}
// iterate over neighbors and call detectCycleDFS on them if they are not
// already visited.
for neighbor := range dag.tasks[nextEl].GetUpstream() {
_, ok := (*visitedSet)[neighbor]
// neighbor already visited
if ok {
continue
}
_, ok = (*visitingSet)[neighbor]
// cycle detected
if ok {
sugar := logger.GetSugaredLogger()
sugar.Infow("Cycle detected b/w nodes", "nodes", visitingSet)
return true
}
if dag.detectCycleDFS(neighbor, unvisitedSet, visitingSet, visitedSet) {
return true
}
}
// move nextEl from visiting to visited
delete(*visitingSet, nextEl)
(*visitedSet)[nextEl] = struct{}{}
return false
}
// TopologicalSortedTasks : Modify this so that it sends out new tasks in
// a channel on updates of finished tasks from a channel received in function param.
func (dag *DAG) TopologicalSortedTasks() chan TaskInterface {
// fmt.Println(dag.tasks)
if err := dag.DetectCycles(); err != nil {
panic("Cannot do TopologicalSort, dag has a cycle.")
}
taskQ := make([]TaskInterface, 0)
taskChan := make(chan TaskInterface)
inDegree := make(map[string]int)
for taskName, task := range dag.tasks {
inDegree[taskName] = len(task.GetDownstream())
}
// Find the tasks which have no downstreams. Those are the tasks which can
// run first.
for task := range inDegree {
if inDegree[task] == 0 {
taskQ = append(taskQ, dag.tasks[task])
}
}
go func() {
// This recover func is useful if we want to see the starting
// n tasks only. Then we can close the taskChan and recover from panic.
defer func() {
if r := recover(); r != nil {
}
}()
for len(taskQ) > 0 {
task := taskQ[0]
taskQ = taskQ[1:]
for upStreamTaskName := range dag.tasks[task.GetName()].GetUpstream() {
inDegree[upStreamTaskName]--
if inDegree[upStreamTaskName] == 0 {
taskQ = append(taskQ, dag.tasks[upStreamTaskName])
}
}
taskChan <- task
}
close(taskChan)
}()
return taskChan
}
// LogInfo logs DAG info.
func (dag *DAG) LogInfo() {
sugar := logger.GetSugaredLogger()
tasks := make([]string, 0)
for task := range dag.tasks {
tasks = append(tasks, task)
}
sugar.Infow("DAG info:", "DAG", dag.Name, "schedule", dag.Schedule, "tasks", tasks)
}