2015-01-23 19:52:51 -06:00
|
|
|
package dag
|
|
|
|
|
2015-02-04 09:10:32 -06:00
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-23 13:24:26 -05:00
|
|
|
"sort"
|
2015-02-04 18:38:38 -06:00
|
|
|
"strings"
|
2015-02-07 11:52:34 -06:00
|
|
|
|
2018-04-06 18:25:35 -05:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
|
2015-02-07 11:52:34 -06:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2015-02-04 09:10:32 -06:00
|
|
|
)
|
|
|
|
|
2015-01-23 19:52:51 -06:00
|
|
|
// AcyclicGraph is a specialization of Graph that cannot have cycles. With
|
|
|
|
// this property, we get the property of sane graph traversal.
|
|
|
|
type AcyclicGraph struct {
|
2015-02-04 09:10:32 -06:00
|
|
|
Graph
|
2015-01-23 19:52:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// WalkFunc is the callback used for walking the graph.
|
2018-04-06 18:25:35 -05:00
|
|
|
type WalkFunc func(Vertex) tfdiags.Diagnostics
|
2015-01-23 19:52:51 -06:00
|
|
|
|
2015-04-23 13:24:26 -05:00
|
|
|
// DepthWalkFunc is a walk function that also receives the current depth of the
|
|
|
|
// walk as an argument
|
|
|
|
type DepthWalkFunc func(Vertex, int) error
|
|
|
|
|
2016-11-09 15:52:22 -06:00
|
|
|
func (g *AcyclicGraph) DirectedGraph() Grapher {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2015-03-24 11:18:15 -05:00
|
|
|
// Returns a Set that includes every Vertex yielded by walking down from the
|
|
|
|
// provided starting Vertex v.
|
|
|
|
func (g *AcyclicGraph) Ancestors(v Vertex) (*Set, error) {
|
|
|
|
s := new(Set)
|
2015-04-23 13:24:26 -05:00
|
|
|
start := AsVertexList(g.DownEdges(v))
|
|
|
|
memoFunc := func(v Vertex, d int) error {
|
2015-03-24 11:18:15 -05:00
|
|
|
s.Add(v)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:24:26 -05:00
|
|
|
if err := g.DepthFirstWalk(start, memoFunc); err != nil {
|
2015-03-24 11:18:15 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a Set that includes every Vertex yielded by walking up from the
|
|
|
|
// provided starting Vertex v.
|
|
|
|
func (g *AcyclicGraph) Descendents(v Vertex) (*Set, error) {
|
|
|
|
s := new(Set)
|
2015-04-23 13:24:26 -05:00
|
|
|
start := AsVertexList(g.UpEdges(v))
|
|
|
|
memoFunc := func(v Vertex, d int) error {
|
2015-03-24 11:18:15 -05:00
|
|
|
s.Add(v)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:24:26 -05:00
|
|
|
if err := g.ReverseDepthFirstWalk(start, memoFunc); err != nil {
|
2015-03-24 11:18:15 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2015-02-04 09:10:32 -06:00
|
|
|
// Root returns the root of the DAG, or an error.
|
|
|
|
//
|
|
|
|
// Complexity: O(V)
|
|
|
|
func (g *AcyclicGraph) Root() (Vertex, error) {
|
|
|
|
roots := make([]Vertex, 0, 1)
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
if g.UpEdges(v).Len() == 0 {
|
|
|
|
roots = append(roots, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(roots) > 1 {
|
|
|
|
// TODO(mitchellh): make this error message a lot better
|
|
|
|
return nil, fmt.Errorf("multiple roots: %#v", roots)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(roots) == 0 {
|
|
|
|
return nil, fmt.Errorf("no roots found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return roots[0], nil
|
|
|
|
}
|
|
|
|
|
2015-02-27 21:12:19 -06:00
|
|
|
// TransitiveReduction performs the transitive reduction of graph g in place.
|
|
|
|
// The transitive reduction of a graph is a graph with as few edges as
|
|
|
|
// possible with the same reachability as the original graph. This means
|
|
|
|
// that if there are three nodes A => B => C, and A connects to both
|
|
|
|
// B and C, and B connects to C, then the transitive reduction is the
|
|
|
|
// same graph with only a single edge between A and B, and a single edge
|
|
|
|
// between B and C.
|
|
|
|
//
|
|
|
|
// The graph must be valid for this operation to behave properly. If
|
|
|
|
// Validate() returns an error, the behavior is undefined and the results
|
|
|
|
// will likely be unexpected.
|
|
|
|
//
|
|
|
|
// Complexity: O(V(V+E)), or asymptotically O(VE)
|
|
|
|
func (g *AcyclicGraph) TransitiveReduction() {
|
2015-02-27 21:37:59 -06:00
|
|
|
// For each vertex u in graph g, do a DFS starting from each vertex
|
|
|
|
// v such that the edge (u,v) exists (v is a direct descendant of u).
|
|
|
|
//
|
|
|
|
// For each v-prime reachable from v, remove the edge (u, v-prime).
|
2016-11-14 10:42:52 -06:00
|
|
|
defer g.debug.BeginOperation("TransitiveReduction", "").End("")
|
2016-11-10 12:40:27 -06:00
|
|
|
|
2015-02-27 21:37:59 -06:00
|
|
|
for _, u := range g.Vertices() {
|
|
|
|
uTargets := g.DownEdges(u)
|
2015-04-23 13:24:26 -05:00
|
|
|
vs := AsVertexList(g.DownEdges(u))
|
2015-02-27 21:12:19 -06:00
|
|
|
|
2017-10-03 10:08:32 -05:00
|
|
|
g.depthFirstWalk(vs, false, func(v Vertex, d int) error {
|
2015-02-27 21:37:59 -06:00
|
|
|
shared := uTargets.Intersection(g.DownEdges(v))
|
2015-04-23 13:24:26 -05:00
|
|
|
for _, vPrime := range AsVertexList(shared) {
|
2015-02-27 21:37:59 -06:00
|
|
|
g.RemoveEdge(BasicEdge(u, vPrime))
|
2015-02-27 21:12:19 -06:00
|
|
|
}
|
|
|
|
|
2015-02-27 21:37:59 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2015-02-27 21:12:19 -06:00
|
|
|
}
|
|
|
|
|
2015-02-04 09:36:33 -06:00
|
|
|
// Validate validates the DAG. A DAG is valid if it has a single root
|
|
|
|
// with no cycles.
|
|
|
|
func (g *AcyclicGraph) Validate() error {
|
|
|
|
if _, err := g.Root(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-07 11:52:34 -06:00
|
|
|
// Look for cycles of more than 1 component
|
|
|
|
var err error
|
2015-04-23 13:24:26 -05:00
|
|
|
cycles := g.Cycles()
|
2015-02-04 09:36:33 -06:00
|
|
|
if len(cycles) > 0 {
|
2015-02-07 11:52:34 -06:00
|
|
|
for _, cycle := range cycles {
|
2015-02-04 18:38:38 -06:00
|
|
|
cycleStr := make([]string, len(cycle))
|
|
|
|
for j, vertex := range cycle {
|
|
|
|
cycleStr[j] = VertexName(vertex)
|
|
|
|
}
|
|
|
|
|
2015-02-07 11:52:34 -06:00
|
|
|
err = multierror.Append(err, fmt.Errorf(
|
|
|
|
"Cycle: %s", strings.Join(cycleStr, ", ")))
|
2015-02-04 18:38:38 -06:00
|
|
|
}
|
2015-02-07 11:52:34 -06:00
|
|
|
}
|
2015-02-04 18:38:38 -06:00
|
|
|
|
2015-02-07 11:52:34 -06:00
|
|
|
// Look for cycles to self
|
|
|
|
for _, e := range g.Edges() {
|
|
|
|
if e.Source() == e.Target() {
|
|
|
|
err = multierror.Append(err, fmt.Errorf(
|
|
|
|
"Self reference: %s", VertexName(e.Source())))
|
|
|
|
}
|
2015-02-04 09:36:33 -06:00
|
|
|
}
|
|
|
|
|
2015-02-07 11:52:34 -06:00
|
|
|
return err
|
2015-02-04 09:36:33 -06:00
|
|
|
}
|
|
|
|
|
2015-04-23 13:24:26 -05:00
|
|
|
func (g *AcyclicGraph) Cycles() [][]Vertex {
|
|
|
|
var cycles [][]Vertex
|
|
|
|
for _, cycle := range StronglyConnected(&g.Graph) {
|
|
|
|
if len(cycle) > 1 {
|
|
|
|
cycles = append(cycles, cycle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cycles
|
|
|
|
}
|
|
|
|
|
2015-01-23 19:52:51 -06:00
|
|
|
// Walk walks the graph, calling your callback as each node is visited.
|
2018-04-06 18:25:35 -05:00
|
|
|
// This will walk nodes in parallel if it can. The resulting diagnostics
|
|
|
|
// contains problems from all graphs visited, in no particular order.
|
|
|
|
func (g *AcyclicGraph) Walk(cb WalkFunc) tfdiags.Diagnostics {
|
2016-11-15 09:15:52 -06:00
|
|
|
defer g.debug.BeginOperation(typeWalk, "").End("")
|
2016-11-10 12:40:27 -06:00
|
|
|
|
2017-02-03 04:22:26 -06:00
|
|
|
w := &Walker{Callback: cb, Reverse: true}
|
2017-02-03 04:48:09 -06:00
|
|
|
w.Update(g)
|
2017-02-03 04:04:39 -06:00
|
|
|
return w.Wait()
|
2015-01-23 19:52:51 -06:00
|
|
|
}
|
2015-02-27 21:12:19 -06:00
|
|
|
|
2015-03-24 11:18:15 -05:00
|
|
|
// simple convenience helper for converting a dag.Set to a []Vertex
|
2015-04-23 13:24:26 -05:00
|
|
|
func AsVertexList(s *Set) []Vertex {
|
2015-03-24 11:18:15 -05:00
|
|
|
rawList := s.List()
|
|
|
|
vertexList := make([]Vertex, len(rawList))
|
|
|
|
for i, raw := range rawList {
|
|
|
|
vertexList[i] = raw.(Vertex)
|
|
|
|
}
|
|
|
|
return vertexList
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:24:26 -05:00
|
|
|
type vertexAtDepth struct {
|
|
|
|
Vertex Vertex
|
|
|
|
Depth int
|
|
|
|
}
|
|
|
|
|
2015-02-27 21:12:19 -06:00
|
|
|
// depthFirstWalk does a depth-first walk of the graph starting from
|
2017-10-03 10:08:32 -05:00
|
|
|
// the vertices in start.
|
2015-04-23 13:24:26 -05:00
|
|
|
func (g *AcyclicGraph) DepthFirstWalk(start []Vertex, f DepthWalkFunc) error {
|
2017-10-03 10:08:32 -05:00
|
|
|
return g.depthFirstWalk(start, true, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This internal method provides the option of not sorting the vertices during
|
|
|
|
// the walk, which we use for the Transitive reduction.
|
|
|
|
// Some configurations can lead to fully-connected subgraphs, which makes our
|
|
|
|
// transitive reduction algorithm O(n^3). This is still passable for the size
|
|
|
|
// of our graphs, but the additional n^2 sort operations would make this
|
|
|
|
// uncomputable in a reasonable amount of time.
|
|
|
|
func (g *AcyclicGraph) depthFirstWalk(start []Vertex, sorted bool, f DepthWalkFunc) error {
|
2016-11-15 09:15:52 -06:00
|
|
|
defer g.debug.BeginOperation(typeDepthFirstWalk, "").End("")
|
2016-11-10 12:40:27 -06:00
|
|
|
|
2015-02-27 21:12:19 -06:00
|
|
|
seen := make(map[Vertex]struct{})
|
2015-04-23 13:24:26 -05:00
|
|
|
frontier := make([]*vertexAtDepth, len(start))
|
|
|
|
for i, v := range start {
|
|
|
|
frontier[i] = &vertexAtDepth{
|
|
|
|
Vertex: v,
|
|
|
|
Depth: 0,
|
|
|
|
}
|
|
|
|
}
|
2015-02-27 21:12:19 -06:00
|
|
|
for len(frontier) > 0 {
|
|
|
|
// Pop the current vertex
|
|
|
|
n := len(frontier)
|
|
|
|
current := frontier[n-1]
|
|
|
|
frontier = frontier[:n-1]
|
|
|
|
|
|
|
|
// Check if we've seen this already and return...
|
2015-04-23 13:24:26 -05:00
|
|
|
if _, ok := seen[current.Vertex]; ok {
|
2015-02-27 21:12:19 -06:00
|
|
|
continue
|
|
|
|
}
|
2015-04-23 13:24:26 -05:00
|
|
|
seen[current.Vertex] = struct{}{}
|
2015-02-27 21:12:19 -06:00
|
|
|
|
|
|
|
// Visit the current node
|
2015-04-23 13:24:26 -05:00
|
|
|
if err := f(current.Vertex, current.Depth); err != nil {
|
2015-02-27 21:12:19 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:24:26 -05:00
|
|
|
// Visit targets of this in a consistent order.
|
|
|
|
targets := AsVertexList(g.DownEdges(current.Vertex))
|
2017-10-03 10:08:32 -05:00
|
|
|
|
|
|
|
if sorted {
|
|
|
|
sort.Sort(byVertexName(targets))
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:24:26 -05:00
|
|
|
for _, t := range targets {
|
|
|
|
frontier = append(frontier, &vertexAtDepth{
|
|
|
|
Vertex: t,
|
|
|
|
Depth: current.Depth + 1,
|
|
|
|
})
|
2015-02-27 21:12:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-24 11:18:15 -05:00
|
|
|
|
|
|
|
// reverseDepthFirstWalk does a depth-first walk _up_ the graph starting from
|
|
|
|
// the vertices in start.
|
2015-04-23 13:24:26 -05:00
|
|
|
func (g *AcyclicGraph) ReverseDepthFirstWalk(start []Vertex, f DepthWalkFunc) error {
|
2016-11-15 09:15:52 -06:00
|
|
|
defer g.debug.BeginOperation(typeReverseDepthFirstWalk, "").End("")
|
2016-11-10 12:40:27 -06:00
|
|
|
|
2015-03-24 11:18:15 -05:00
|
|
|
seen := make(map[Vertex]struct{})
|
2015-04-23 13:24:26 -05:00
|
|
|
frontier := make([]*vertexAtDepth, len(start))
|
|
|
|
for i, v := range start {
|
|
|
|
frontier[i] = &vertexAtDepth{
|
|
|
|
Vertex: v,
|
|
|
|
Depth: 0,
|
|
|
|
}
|
|
|
|
}
|
2015-03-24 11:18:15 -05:00
|
|
|
for len(frontier) > 0 {
|
|
|
|
// Pop the current vertex
|
|
|
|
n := len(frontier)
|
|
|
|
current := frontier[n-1]
|
|
|
|
frontier = frontier[:n-1]
|
|
|
|
|
|
|
|
// Check if we've seen this already and return...
|
2015-04-23 13:24:26 -05:00
|
|
|
if _, ok := seen[current.Vertex]; ok {
|
2015-03-24 11:18:15 -05:00
|
|
|
continue
|
|
|
|
}
|
2015-04-23 13:24:26 -05:00
|
|
|
seen[current.Vertex] = struct{}{}
|
2015-03-24 11:18:15 -05:00
|
|
|
|
2016-07-14 10:33:37 -05:00
|
|
|
// Add next set of targets in a consistent order.
|
2015-04-23 13:24:26 -05:00
|
|
|
targets := AsVertexList(g.UpEdges(current.Vertex))
|
|
|
|
sort.Sort(byVertexName(targets))
|
|
|
|
for _, t := range targets {
|
|
|
|
frontier = append(frontier, &vertexAtDepth{
|
|
|
|
Vertex: t,
|
|
|
|
Depth: current.Depth + 1,
|
|
|
|
})
|
2015-03-24 11:18:15 -05:00
|
|
|
}
|
2016-07-14 10:33:37 -05:00
|
|
|
|
|
|
|
// Visit the current node
|
|
|
|
if err := f(current.Vertex, current.Depth); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-24 11:18:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-23 13:24:26 -05:00
|
|
|
|
|
|
|
// byVertexName implements sort.Interface so a list of Vertices can be sorted
|
|
|
|
// consistently by their VertexName
|
|
|
|
type byVertexName []Vertex
|
|
|
|
|
|
|
|
func (b byVertexName) Len() int { return len(b) }
|
|
|
|
func (b byVertexName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
|
|
|
func (b byVertexName) Less(i, j int) bool {
|
|
|
|
return VertexName(b[i]) < VertexName(b[j])
|
|
|
|
}
|