-
Notifications
You must be signed in to change notification settings - Fork 593
fix: Stop components in a deterministic order #5613
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
09399b7
Make stopping determenistic and make sure runnables are stopped in the
kalleep 4275653
fixes
kalleep 8404e52
lint
kalleep 0f2e2e2
lint
kalleep 27159b9
use graph
kalleep 6395dd6
remove context
kalleep a672766
fix
kalleep 51b4d20
add function to determine weakly connected components in a graph with
kalleep a6643bd
Asign empherial graph id and record rank that is used when starting and
kalleep b07c1c7
revert change
kalleep e9d9f2e
remove print
kalleep 631b544
Update comments
kalleep 0b99d0a
Wait for tasks to stop before starting new ones and also wait for every
kalleep 9c6048b
Update comment
kalleep 326bee8
remove comment
kalleep 933d520
fix test
kalleep bfba4a8
Add lock
kalleep 6a572f9
Update internal/dag/weak.go
kalleep 29b5e47
Add debug logs
kalleep a59763b
Add comment
kalleep 8747a57
Update internal/runtime/internal/controller/scheduler.go
kalleep efd1e6a
update comment
kalleep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -228,3 +228,7 @@ func (g *Graph) Clone() *Graph { | |
| } | ||
| return newGraph | ||
| } | ||
|
|
||
| func (g *Graph) Len() int { | ||
| return len(g.nodes) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package dag | ||
|
|
||
| type FilterFunc func(g *Graph, n Node) bool | ||
|
|
||
| // FilterAllFunc includes every node. | ||
| func FilterAllFunc(_ *Graph, _ Node) bool { | ||
| return true | ||
| } | ||
|
|
||
| // FilterLeavesFunc includes only leaves. | ||
| func FilterLeavesFunc(g *Graph, n Node) bool { | ||
| return len(g.outEdges[n]) == 0 | ||
| } | ||
|
|
||
| // FilterRootsFunc includes only roots. | ||
| func FilterRootsFunc(g *Graph, n Node) bool { | ||
|
kalleep marked this conversation as resolved.
|
||
| return len(g.inEdges[n]) == 0 | ||
| } | ||
|
|
||
| // WeaklyConnectedComponents returns the graph split into weakly connected | ||
| // components. Two nodes are in the same component if there is a path between them in either direction. | ||
| // Each node appears in exactly one component. The graph is unchanged. | ||
| func WeaklyConnectedComponents(g *Graph, f FilterFunc) [][]Node { | ||
| visited := make(nodeSet) | ||
| var components [][]Node | ||
|
|
||
| for _, n := range g.Nodes() { | ||
| if visited.Has(n) { | ||
| continue | ||
| } | ||
|
|
||
| var ( | ||
| queue = []Node{n} | ||
| component = make([]Node, 0) | ||
| ) | ||
|
|
||
| visited.Add(n) | ||
| for len(queue) > 0 { | ||
| curr := queue[0] | ||
| queue = queue[1:] | ||
|
|
||
| if f(g, curr) { | ||
| component = append(component, curr) | ||
| } | ||
|
|
||
| for _, neighbor := range neighbors(g, curr) { | ||
| if visited.Has(neighbor) { | ||
| continue | ||
| } | ||
| visited.Add(neighbor) | ||
| queue = append(queue, neighbor) | ||
| } | ||
| } | ||
|
|
||
| if len(component) > 0 { | ||
| components = append(components, component) | ||
| } | ||
| } | ||
|
|
||
| return components | ||
| } | ||
|
|
||
| // neighbors returns all nodes adjacent to n ignoring edge direction. | ||
| func neighbors(g *Graph, n Node) []Node { | ||
| out := make([]Node, 0, len(g.outEdges[n])+len(g.inEdges[n])) | ||
| for neighbor := range g.outEdges[n] { | ||
| out = append(out, neighbor) | ||
| } | ||
| for neighbor := range g.inEdges[n] { | ||
| out = append(out, neighbor) | ||
| } | ||
| return out | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package dag | ||
|
|
||
| import ( | ||
| "sort" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestWeaklyConnectedComponents(t *testing.T) { | ||
| t.Run("two disjoint graphs", func(t *testing.T) { | ||
| var g Graph | ||
| a, b, c, d := stringNode("a"), stringNode("b"), stringNode("c"), stringNode("d") | ||
| g.Add(a) | ||
| g.Add(b) | ||
| g.Add(c) | ||
| g.Add(d) | ||
| g.AddEdge(Edge{From: a, To: b}) | ||
| g.AddEdge(Edge{From: c, To: d}) | ||
|
|
||
| got := WeaklyConnectedComponents(&g, FilterAllFunc) | ||
| got = sortComponents(got) | ||
|
|
||
| require.Equal(t, [][]Node{{a, b}, {c, d}}, got) | ||
| }) | ||
|
|
||
| t.Run("single graph", func(t *testing.T) { | ||
| var g Graph | ||
| a, b, c := stringNode("a"), stringNode("b"), stringNode("c") | ||
| g.Add(a) | ||
| g.Add(b) | ||
| g.Add(c) | ||
| g.AddEdge(Edge{From: a, To: b}) | ||
| g.AddEdge(Edge{From: b, To: c}) | ||
|
|
||
| got := WeaklyConnectedComponents(&g, FilterAllFunc) | ||
| got = sortComponents(got) | ||
| require.Equal(t, [][]Node{{a, b, c}}, got) | ||
| }) | ||
|
|
||
| t.Run("isolated graphs", func(t *testing.T) { | ||
| var g Graph | ||
| a, b, c := stringNode("a"), stringNode("b"), stringNode("c") | ||
| g.Add(a) | ||
| g.Add(b) | ||
| g.Add(c) | ||
|
|
||
| got := WeaklyConnectedComponents(&g, FilterAllFunc) | ||
| got = sortComponents(got) | ||
| require.Equal(t, [][]Node{{a}, {b}, {c}}, got) | ||
| }) | ||
|
|
||
| t.Run("only leaves", func(t *testing.T) { | ||
| var g Graph | ||
| a, b, c, d := stringNode("a"), stringNode("b"), stringNode("c"), stringNode("d") | ||
| g.Add(a) | ||
| g.Add(b) | ||
| g.Add(c) | ||
| g.Add(d) | ||
| g.AddEdge(Edge{From: a, To: b}) | ||
| g.AddEdge(Edge{From: c, To: d}) | ||
|
|
||
| got := WeaklyConnectedComponents(&g, FilterLeavesFunc) | ||
| got = sortComponents(got) | ||
| require.Equal(t, [][]Node{{b}, {d}}, got) | ||
| }) | ||
|
|
||
| t.Run("only roots", func(t *testing.T) { | ||
| var g Graph | ||
| a, b, c, d := stringNode("a"), stringNode("b"), stringNode("c"), stringNode("d") | ||
| g.Add(a) | ||
| g.Add(b) | ||
| g.Add(c) | ||
| g.Add(d) | ||
| g.AddEdge(Edge{From: a, To: b}) | ||
| g.AddEdge(Edge{From: c, To: d}) | ||
|
|
||
| got := WeaklyConnectedComponents(&g, FilterRootsFunc) | ||
| got = sortComponents(got) | ||
| require.Equal(t, [][]Node{{a}, {c}}, got) | ||
| }) | ||
| } | ||
|
|
||
| func sortComponents(components [][]Node) [][]Node { | ||
| for _, c := range components { | ||
| sort.Slice(c, func(i, j int) bool { | ||
| return c[i].NodeID() < c[j].NodeID() | ||
| }) | ||
| } | ||
| sort.Slice(components, func(i, j int) bool { | ||
| return components[i][0].NodeID() < components[j][0].NodeID() | ||
| }) | ||
| return components | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only defined func we use, is it useful to keep the others or wait to add them when we need them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah so first I just implemented
WeaklyConnectedComponentswithout filtering but for scheduling to work correctly I only need leaves, but felt bad about only adding a specialized function for it.I am not sure we need the rest of them I just added it for consistency but I can remove unused ones, though I think FilterAllFunc is useful just to test that the algorithm is correct in general