Skip to content

Commit

Permalink
feature: better topological sorting
Browse files Browse the repository at this point in the history
Signed-off-by: Valery Piashchynski <[email protected]>
  • Loading branch information
rustatian committed Oct 9, 2023
1 parent f0b1f9b commit 99b4d40
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
25 changes: 20 additions & 5 deletions edges.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (e *Endure) resolveCollectorEdges(plugin any) error {
}

// resolveEdges adds edges between the vertices
// At this point, we know all plugins and all provides values
// At this point, we know all plugins, and all provides values
func (e *Endure) resolveEdges() error {
vertices := e.graph.Vertices()

Expand Down Expand Up @@ -90,7 +90,7 @@ func (e *Endure) resolveEdges() error {

// we should have here exact the same number of the deps implementing every particular arg
if count != len(args[1:]) {
// if there are no plugins which implement Init deps, remove this vertex from the tree
// if there are no plugins that implement Init deps, remove this vertex from the tree
del := e.graph.Remove(vertices[i].Plugin())
for k := 0; k < len(del); k++ {
e.registar.Remove(del[k].Plugin())
Expand All @@ -115,9 +115,24 @@ func (e *Endure) resolveEdges() error {
}
}

ok := e.graph.TopologicalSort()
if !ok {
return errors.E("cyclic dependencies found, see the DEBUG log")
e.graph.TopologicalSort()

// notify user about the disabled plugins
// after topological sorting we remove all plugins with indegree > 0, because there are no edges to them
if len(e.graph.TopologicalOrder()) != len(e.graph.Vertices()) {
tpl := e.graph.TopologicalOrder()
vrt := e.graph.Vertices()

tmpM := make(map[string]struct{}, 2)
for _, v := range tpl {
tmpM[v.ID().String()] = struct{}{}
}

for _, v := range vrt {
if _, ok := tmpM[v.ID().String()]; !ok {
e.log.Warn("topological sort, plugin disabled", slog.String("plugin", v.ID().String()))
}
}
}

return nil
Expand Down
4 changes: 1 addition & 3 deletions graph/toposort.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package graph

func (g *Graph) TopologicalSort() bool {
func (g *Graph) TopologicalSort() {
heap := &VertexHeap{}

for _, v := range g.vertices {
Expand Down Expand Up @@ -33,6 +33,4 @@ func (g *Graph) TopologicalSort() bool {
}
}
}

return len(g.topologicalOrder) >= len(g.vertices)
}

0 comments on commit 99b4d40

Please sign in to comment.