forked from docker/compose
/
dependencies.go
401 lines (329 loc) · 9.71 KB
/
dependencies.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package compose
import (
"context"
"fmt"
"strings"
"sync"
"github.com/compose-spec/compose-go/types"
"golang.org/x/sync/errgroup"
"github.com/docker/compose/v2/pkg/utils"
)
// ServiceStatus indicates the status of a service
type ServiceStatus int
// Services status flags
const (
ServiceStopped ServiceStatus = iota
ServiceStarted
)
type graphTraversal struct {
mu sync.Mutex
seen map[string]struct{}
extremityNodesFn func(*Graph) []*Vertex // leaves or roots
adjacentNodesFn func(*Vertex) []*Vertex // getParents or getChildren
filterAdjacentByStatusFn func(*Graph, string, ServiceStatus) []*Vertex // filterChildren or filterParents
targetServiceStatus ServiceStatus
adjacentServiceStatusToSkip ServiceStatus
visitorFn func(context.Context, string) error
maxConcurrency int
}
func upDirectionTraversal(visitorFn func(context.Context, string) error) *graphTraversal {
return &graphTraversal{
extremityNodesFn: leaves,
adjacentNodesFn: getParents,
filterAdjacentByStatusFn: filterChildren,
adjacentServiceStatusToSkip: ServiceStopped,
targetServiceStatus: ServiceStarted,
visitorFn: visitorFn,
}
}
func downDirectionTraversal(visitorFn func(context.Context, string) error) *graphTraversal {
return &graphTraversal{
extremityNodesFn: roots,
adjacentNodesFn: getChildren,
filterAdjacentByStatusFn: filterParents,
adjacentServiceStatusToSkip: ServiceStarted,
targetServiceStatus: ServiceStopped,
visitorFn: visitorFn,
}
}
// InDependencyOrder applies the function to the services of the project taking in account the dependency order
func InDependencyOrder(ctx context.Context, project *types.Project, fn func(context.Context, string) error, options ...func(*graphTraversal)) error {
graph, err := NewGraph(project.Services, ServiceStopped)
if err != nil {
return err
}
t := upDirectionTraversal(fn)
for _, option := range options {
option(t)
}
return t.visit(ctx, graph)
}
// InReverseDependencyOrder applies the function to the services of the project in reverse order of dependencies
func InReverseDependencyOrder(ctx context.Context, project *types.Project, fn func(context.Context, string) error) error {
graph, err := NewGraph(project.Services, ServiceStarted)
if err != nil {
return err
}
t := downDirectionTraversal(fn)
return t.visit(ctx, graph)
}
func (t *graphTraversal) visit(ctx context.Context, g *Graph) error {
nodes := t.extremityNodesFn(g)
eg, ctx := errgroup.WithContext(ctx)
if t.maxConcurrency > 0 {
eg.SetLimit(t.maxConcurrency)
}
t.run(ctx, g, eg, nodes)
return eg.Wait()
}
// Note: this could be `graph.walk` or whatever
func (t *graphTraversal) run(ctx context.Context, graph *Graph, eg *errgroup.Group, nodes []*Vertex) {
for _, node := range nodes {
// Don't start this service yet if all of its children have
// not been started yet.
if len(t.filterAdjacentByStatusFn(graph, node.Key, t.adjacentServiceStatusToSkip)) != 0 {
continue
}
node := node
if !t.consume(node.Key) {
// another worker already visited this node
continue
}
eg.Go(func() error {
err := t.visitorFn(ctx, node.Service)
if err != nil {
return err
}
graph.UpdateStatus(node.Key, t.targetServiceStatus)
t.run(ctx, graph, eg, t.adjacentNodesFn(node))
return nil
})
}
}
func (t *graphTraversal) consume(nodeKey string) bool {
t.mu.Lock()
defer t.mu.Unlock()
if t.seen == nil {
t.seen = make(map[string]struct{})
}
if _, ok := t.seen[nodeKey]; ok {
return false
}
t.seen[nodeKey] = struct{}{}
return true
}
// Graph represents project as service dependencies
type Graph struct {
Vertices map[string]*Vertex
lock sync.RWMutex
}
// Vertex represents a service in the dependencies structure
type Vertex struct {
Key string
Service string
Status ServiceStatus
Children map[string]*Vertex
Parents map[string]*Vertex
}
func getParents(v *Vertex) []*Vertex {
return v.GetParents()
}
// GetParents returns a slice with the parent vertices of the a Vertex
func (v *Vertex) GetParents() []*Vertex {
var res []*Vertex
for _, p := range v.Parents {
res = append(res, p)
}
return res
}
func getChildren(v *Vertex) []*Vertex {
return v.GetChildren()
}
// GetChildren returns a slice with the child vertices of the a Vertex
func (v *Vertex) GetChildren() []*Vertex {
var res []*Vertex
for _, p := range v.Children {
res = append(res, p)
}
return res
}
// NewGraph returns the dependency graph of the services
func NewGraph(services types.Services, initialStatus ServiceStatus) (*Graph, error) {
graph := &Graph{
lock: sync.RWMutex{},
Vertices: map[string]*Vertex{},
}
for _, s := range services {
graph.AddVertex(s.Name, s.Name, initialStatus)
}
for _, s := range services {
for _, name := range s.GetDependencies() {
_ = graph.AddEdge(s.Name, name)
}
}
if b, err := graph.HasCycles(); b {
return nil, err
}
return graph, nil
}
// NewVertex is the constructor function for the Vertex
func NewVertex(key string, service string, initialStatus ServiceStatus) *Vertex {
return &Vertex{
Key: key,
Service: service,
Status: initialStatus,
Parents: map[string]*Vertex{},
Children: map[string]*Vertex{},
}
}
// AddVertex adds a vertex to the Graph
func (g *Graph) AddVertex(key string, service string, initialStatus ServiceStatus) {
g.lock.Lock()
defer g.lock.Unlock()
v := NewVertex(key, service, initialStatus)
g.Vertices[key] = v
}
// AddEdge adds a relationship of dependency between vertices `source` and `destination`
func (g *Graph) AddEdge(source string, destination string) error {
g.lock.Lock()
defer g.lock.Unlock()
sourceVertex := g.Vertices[source]
destinationVertex := g.Vertices[destination]
if sourceVertex == nil {
return fmt.Errorf("could not find %s", source)
}
if destinationVertex == nil {
return fmt.Errorf("could not find %s", destination)
}
// If they are already connected
if _, ok := sourceVertex.Children[destination]; ok {
return nil
}
sourceVertex.Children[destination] = destinationVertex
destinationVertex.Parents[source] = sourceVertex
return nil
}
func leaves(g *Graph) []*Vertex {
return g.Leaves()
}
// Leaves returns the slice of leaves of the graph
func (g *Graph) Leaves() []*Vertex {
g.lock.Lock()
defer g.lock.Unlock()
var res []*Vertex
for _, v := range g.Vertices {
if len(v.Children) == 0 {
res = append(res, v)
}
}
return res
}
func roots(g *Graph) []*Vertex {
return g.Roots()
}
// Roots returns the slice of "Roots" of the graph
func (g *Graph) Roots() []*Vertex {
g.lock.Lock()
defer g.lock.Unlock()
var res []*Vertex
for _, v := range g.Vertices {
if len(v.Parents) == 0 {
res = append(res, v)
}
}
return res
}
// UpdateStatus updates the status of a certain vertex
func (g *Graph) UpdateStatus(key string, status ServiceStatus) {
g.lock.Lock()
defer g.lock.Unlock()
g.Vertices[key].Status = status
}
func filterChildren(g *Graph, k string, s ServiceStatus) []*Vertex {
return g.FilterChildren(k, s)
}
// FilterChildren returns children of a certain vertex that are in a certain status
func (g *Graph) FilterChildren(key string, status ServiceStatus) []*Vertex {
g.lock.Lock()
defer g.lock.Unlock()
var res []*Vertex
vertex := g.Vertices[key]
for _, child := range vertex.Children {
if child.Status == status {
res = append(res, child)
}
}
return res
}
func filterParents(g *Graph, k string, s ServiceStatus) []*Vertex {
return g.FilterParents(k, s)
}
// FilterParents returns the parents of a certain vertex that are in a certain status
func (g *Graph) FilterParents(key string, status ServiceStatus) []*Vertex {
g.lock.Lock()
defer g.lock.Unlock()
var res []*Vertex
vertex := g.Vertices[key]
for _, parent := range vertex.Parents {
if parent.Status == status {
res = append(res, parent)
}
}
return res
}
// HasCycles detects cycles in the graph
func (g *Graph) HasCycles() (bool, error) {
discovered := []string{}
finished := []string{}
for _, vertex := range g.Vertices {
path := []string{
vertex.Key,
}
if !utils.StringContains(discovered, vertex.Key) && !utils.StringContains(finished, vertex.Key) {
var err error
discovered, finished, err = g.visit(vertex.Key, path, discovered, finished)
if err != nil {
return true, err
}
}
}
return false, nil
}
func (g *Graph) visit(key string, path []string, discovered []string, finished []string) ([]string, []string, error) {
discovered = append(discovered, key)
for _, v := range g.Vertices[key].Children {
path := append(path, v.Key)
if utils.StringContains(discovered, v.Key) {
return nil, nil, fmt.Errorf("cycle found: %s", strings.Join(path, " -> "))
}
if !utils.StringContains(finished, v.Key) {
if _, _, err := g.visit(v.Key, path, discovered, finished); err != nil {
return nil, nil, err
}
}
}
discovered = remove(discovered, key)
finished = append(finished, key)
return discovered, finished, nil
}
func remove(slice []string, item string) []string {
var s []string
for _, i := range slice {
if i != item {
s = append(s, i)
}
}
return s
}