Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: better topological sorting #149

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions edges.go
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@
}

// 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()

@@ -90,7 +90,7 @@

// 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())
@@ -115,9 +115,24 @@
}
}

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{}{}
}

Check warning on line 129 in edges.go

Codecov / codecov/patch

edges.go#L128-L129

Added lines #L128 - L129 were not covered by tests

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
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 {
@@ -33,6 +33,4 @@ func (g *Graph) TopologicalSort() bool {
}
}
}

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