Skip to content

Commit

Permalink
fix cycle detection logic
Browse files Browse the repository at this point in the history
visited map holds name of the nodes been visited as keys and "true"
as value. This map is being updated with currentName.HashKey on every
iteration which results in keys such as a.d, b.d where a, b, and d
are nodes of a dag. But such keys are not utilized anywhere in the
visit function. Visit function checks existence of the node just
by the name without any string concatenation.

This extra addition in the map is causing severe delay for a graph
with more than >60 nodes.
  • Loading branch information
pritidesai committed Nov 18, 2020
1 parent 4348839 commit 30a4f39
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions pkg/reconciler/pipeline/dag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,21 @@ func linkPipelineTasks(prev *Node, next *Node) error {
// Check if we are adding cycles.
visited := map[string]bool{prev.Task.HashKey(): true, next.Task.HashKey(): true}
path := []string{next.Task.HashKey(), prev.Task.HashKey()}
if err := visit(next.Task.HashKey(), prev.Prev, path, visited); err != nil {
if err := visit(prev.Prev, path, visited); err != nil {
return fmt.Errorf("cycle detected: %w", err)
}
next.Prev = append(next.Prev, prev)
prev.Next = append(prev.Next, next)
return nil
}

func visit(currentName string, nodes []*Node, path []string, visited map[string]bool) error {
var sb strings.Builder
func visit(nodes []*Node, path []string, visited map[string]bool) error {
for _, n := range nodes {
path = append(path, n.Task.HashKey())
if _, ok := visited[n.Task.HashKey()]; ok {
return errors.New(getVisitedPath(path))
}
sb.WriteString(currentName)
sb.WriteByte('.')
sb.WriteString(n.Task.HashKey())
visited[sb.String()] = true
if err := visit(n.Task.HashKey(), n.Prev, path, visited); err != nil {
if err := visit(n.Prev, path, visited); err != nil {
return err
}
}
Expand Down

0 comments on commit 30a4f39

Please sign in to comment.