/
graph.go
262 lines (237 loc) · 5.87 KB
/
graph.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
package project
import (
"fmt"
"sync"
"github.com/apex/log"
"github.com/shalb/cluster.dev/pkg/config"
)
type ExecutionStatus uint16
const (
Backlog ExecutionStatus = iota + 1
ReadyForExec
InProgress
Finished
)
type ExecSet struct {
execUnits []*UnitPlanningStatus
}
func (e *ExecSet) Find(u Unit) *UnitPlanningStatus {
for _, eu := range e.execUnits {
if eu.UnitPtr == u {
return eu
}
}
return nil
}
func (e *ExecSet) Index(u Unit) int {
for i, eu := range e.execUnits {
if eu.UnitPtr == u {
return i
}
}
return -1
}
func (e *ExecSet) AddUnit(u *UnitPlanningStatus) {
if u == nil {
log.Debug("Internal problem: create exec set, added unit is nil, ignore")
return
}
if e.Find(u.UnitPtr) != nil {
return
}
e.execUnits = append(e.execUnits, u)
}
func (e *ExecSet) Delete(u Unit) {
index := e.Index(u)
if index < 0 {
return
}
e.execUnits = append(e.execUnits[:index], e.execUnits[index+1:]...)
}
func (e *ExecSet) Slice() []*UnitPlanningStatus {
return e.execUnits
}
func (e *ExecSet) Len() int {
return len(e.execUnits)
}
func (e *ExecSet) IsEmpty() bool {
return len(e.execUnits) == 0
}
func (e *ExecSet) Front() *UnitPlanningStatus {
if len(e.execUnits) > 0 {
return e.execUnits[0]
}
return nil
}
func (e *ExecSet) StatusFilter(statusList ...ExecutionStatus) *ExecSet {
res := ExecSet{
execUnits: make([]*UnitPlanningStatus, 0),
}
for _, ue := range e.execUnits {
for _, status := range statusList {
if ue.UnitPtr.GetExecStatus() == status {
res.AddUnit(ue)
break
}
}
}
return &res
}
func NewExecSet(planningStatus *ProjectPlanningStatus) *ExecSet {
res := ExecSet{
execUnits: make([]*UnitPlanningStatus, 0),
}
for _, unit := range planningStatus.Slice() {
res.AddUnit(unit)
unit.UnitPtr.SetExecStatus(Backlog)
}
return &res
}
type graph struct {
units *ExecSet
mux sync.Mutex
waitUnitDone chan *UnitPlanningStatus
maxParallel int
indexedSlice []*UnitPlanningStatus
planningUnits *ProjectPlanningStatus
// sigTrap chan os.Signal
// stopChan chan struct{}
}
func (g *graph) BuildNew(planningStatus *ProjectPlanningStatus, maxParallel int) error {
g.units = NewExecSet(planningStatus)
g.maxParallel = maxParallel
g.waitUnitDone = make(chan *UnitPlanningStatus)
g.planningUnits = planningStatus
return g.checkAndBuildIndexes()
// g.listenHupSig()
}
func (g *graph) GetNextAsync() (*UnitPlanningStatus, func(error), error) {
g.mux.Lock()
defer g.mux.Unlock()
for {
g.updateQueueNew()
if config.Interrupted {
return nil, nil, fmt.Errorf("interrupted")
}
readyFroExecList := g.units.StatusFilter(ReadyForExec)
if readyFroExecList.Len() > 0 && g.units.StatusFilter(InProgress).Len() < g.maxParallel {
unitForExec := readyFroExecList.Front()
finFunc := func(err error) {
g.waitUnitDone <- unitForExec
}
unitForExec.UnitPtr.SetExecStatus(InProgress)
g.updateQueueNew()
return unitForExec, finFunc, nil
}
if g.units.StatusFilter(Backlog, InProgress, ReadyForExec).IsEmpty() {
return nil, nil, nil
}
unitFinished := <-g.waitUnitDone
unitFinished.UnitPtr.SetExecStatus(Finished)
g.updateQueueNew()
if unitFinished.UnitPtr.ExecError() != nil {
return unitFinished, nil, fmt.Errorf("error while unit running")
}
}
}
func (g *graph) checkAndBuildIndexes() error {
i := 0
g.indexedSlice = []*UnitPlanningStatus{}
apply := []*UnitPlanningStatus{}
notChanged := []*UnitPlanningStatus{}
for {
readyCount := g.updateQueueNew()
if readyCount == 0 {
if g.units.StatusFilter(Backlog).Len() > 0 {
return fmt.Errorf("the graph is broken, can't resolve sequence")
}
break
}
for _, u := range g.units.StatusFilter(ReadyForExec).Slice() {
switch u.Operation {
case Destroy:
// Place 'destroy' units first in queue
g.indexedSlice = append(g.indexedSlice, u)
u.Index = i
case Apply, Update:
// Then place 'apply/update' units
apply = append(apply, u)
u.Index = i
default:
// Place notChanged units to the end of queue and mark them as 'Finished' in graph
notChanged = append(notChanged, u)
u.Index = -1
}
// Mark unit as finished for next updateQueue
u.UnitPtr.SetExecStatus(Finished)
}
i++
}
g.indexedSlice = append(g.indexedSlice, apply...)
g.indexedSlice = append(g.indexedSlice, notChanged...)
g.resetUnitsStatus()
return nil
}
// IndexedSlice return the slice of units in sorted in order ready for exec.
func (g *graph) IndexedSlice() []*UnitPlanningStatus {
return g.indexedSlice
}
func (g *graph) resetUnitsStatus() {
for _, u := range g.units.Slice() {
if u.Operation != NotChanged {
u.UnitPtr.SetExecStatus(Backlog)
}
}
}
func (g *graph) Errors() []Unit {
res := []Unit{}
for _, u := range g.units.Slice() {
if u.UnitPtr.ExecError() != nil {
res = append(res, u.UnitPtr)
}
}
return res
}
func (g *graph) Len() int {
return g.units.StatusFilter(Backlog, InProgress, ReadyForExec).Len()
}
func (g *graph) updateQueueNew() int {
count := 0
for _, unit := range g.units.StatusFilter(Backlog).Slice() {
blockedByDep := false
switch unit.Operation {
case Apply, Update:
for _, dep := range unit.UnitPtr.Dependencies().Slice() {
if g.units.StatusFilter(Backlog, InProgress, ReadyForExec).Find(dep.Unit) != nil {
blockedByDep = true
break
}
}
case Destroy:
for _, unitForCheck := range g.units.StatusFilter(Backlog, InProgress, ReadyForExec).Slice() {
for _, link := range unitForCheck.UnitPtr.Dependencies().Slice() {
if link.UnitKey() == unit.UnitPtr.Key() {
blockedByDep = true
}
}
}
case NotChanged:
unit.UnitPtr.SetExecStatus(Finished)
}
if !blockedByDep {
unit.UnitPtr.SetExecStatus(ReadyForExec)
count++
continue
}
}
return count
}
func (g *graph) Wait() {
for {
if g.units.StatusFilter(InProgress).Len() == 0 {
return
}
doneUnit := <-g.waitUnitDone
doneUnit.UnitPtr.SetExecStatus(Finished)
}
}