Skip to content

Commit

Permalink
[#149]: feature: better topological sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
rustatian authored Oct 9, 2023
2 parents f0b1f9b + 7a7485c commit d2ff679
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 45 deletions.
76 changes: 39 additions & 37 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ name: "CodeQL"

on:
push:
branches: [master]
branches: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [master]
branches: [ master ]
schedule:
- cron: '0 16 * * 6'
- cron: '0 15 * * 6'

jobs:
analyze:
Expand All @@ -24,48 +24,50 @@ jobs:
matrix:
# Override automatic language detection by changing the below list
# Supported options are ['csharp', 'cpp', 'go', 'java', 'javascript', 'python']
language: ['go']
language: [ 'go' ]
# Learn more...
# https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
# We must fetch at least the immediate parents so that if this is
# a pull request then we can checkout the head.
fetch-depth: 2
- name: Checkout repository
uses: actions/checkout@v4
with:
# We must fetch at least the immediate parents so that if this is
# a pull request then we can checkout the head.
fetch-depth: 2

# If this run was triggered by a pull request event, then checkout
# the head of the pull request instead of the merge commit.
- run: git checkout HEAD^2
if: ${{ github.event_name == 'pull_request' }}
# Initializes the Golang environment for the CodeQL tools.
# https://github.com/github/codeql-action/issues/1842#issuecomment-1704398087
- name: Install Go
uses: actions/setup-go@v4
with:
go-version-file: go.mod

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release
#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
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 d2ff679

Please sign in to comment.