engine: Resources package rewrite

This giant patch makes some much needed improvements to the code base.

* The engine has been rewritten and lives within engine/graph/
* All of the common interfaces and code now live in engine/
* All of the resources are in one package called engine/resources/
* The Res API can use different "traits" from engine/traits/
* The Res API has been simplified to hide many of the old internals
* The Watch & Process loops were previously inverted, but is now fixed
* The likelihood of package cycles has been reduced drastically
* And much, much more...

Unfortunately, some code had to be temporarily removed. The remote code
had to be taken out, as did the prometheus code. We hope to have these
back in new forms as soon as possible.
This commit is contained in:
James Shubin
2018-03-13 12:02:44 -04:00
parent ef49aa7e08
commit 9969286224
140 changed files with 9130 additions and 8764 deletions

View File

@@ -73,7 +73,7 @@ func (obj *Graph) GraphSync(newGraph *Graph, vertexCmpFn func(Vertex, Vertex) (b
for v := range newGraph.Adjacency() { // loop through the vertices (resources)
var vertex Vertex
// step one, direct compare with res.Compare
// step one, direct compare with res.Cmp
if vertex == nil { // redundant guard for consistency
fn := func(vv Vertex) (bool, error) {
b, err := vertexCmpFn(vv, v)

View File

@@ -20,7 +20,6 @@ package pgraph
import (
"fmt"
"log"
"sort"
"strings"
@@ -274,15 +273,11 @@ func (g *Graph) Sprint() string {
return strings.TrimSuffix(str, "\n") // trim off trailing \n if it exists
}
// Logf logs a printed representation of the graph with the printf-format string
// prefix of your choice. This is helpful to ensure each line of logged output
// has the prefix you asked for.
func (g *Graph) Logf(format string, v ...interface{}) {
w := []interface{}{}
w = append(w, v...)
// Logf logs a printed representation of the graph with the logf of your choice.
// This is helpful to ensure each line of logged output has the prefix you want.
func (g *Graph) Logf(logf func(format string, v ...interface{})) {
for _, x := range strings.Split(g.Sprint(), "\n") {
a := append(w, x) // x must be the last arg
log.Printf(format+"%s", a...)
logf("%s", x)
}
}
@@ -516,22 +511,30 @@ func (g *Graph) TopologicalSort() ([]Vertex, error) { // kahn's algorithm
// actually return a tree if we cared about correctness.
// This operates by a recursive algorithm; a more efficient version is likely.
// If you don't give this function a DAG, you might cause infinite recursion!
func (g *Graph) Reachability(a, b Vertex) []Vertex {
func (g *Graph) Reachability(a, b Vertex) ([]Vertex, error) {
if a == nil || b == nil {
return nil
return nil, fmt.Errorf("empty vertex")
}
if _, err := g.TopologicalSort(); err != nil {
return nil, err // not a dag?
}
vertices := g.OutgoingGraphVertices(a) // what points away from a ?
if len(vertices) == 0 {
return []Vertex{} // nope
return []Vertex{}, nil // nope
}
if VertexContains(b, vertices) {
return []Vertex{a, b} // found
return []Vertex{a, b}, nil // found
}
// TODO: parallelize this with go routines?
var collected = make([][]Vertex, len(vertices))
var err error
pick := -1
for i, v := range vertices {
collected[i] = g.Reachability(v, b) // find b by recursion
collected[i], err = g.Reachability(v, b) // find b by recursion
if err != nil {
return nil, err
}
if l := len(collected[i]); l > 0 {
// pick shortest path
// TODO: technically i should return a tree
@@ -541,11 +544,11 @@ func (g *Graph) Reachability(a, b Vertex) []Vertex {
}
}
if pick < 0 {
return []Vertex{} // nope
return []Vertex{}, nil // nope
}
result := []Vertex{a} // tack on a
result = append(result, collected[pick]...)
return result
return result, nil
}
// VertexMatchFn searches for a vertex in the graph and returns the vertex if

View File

@@ -384,14 +384,16 @@ func TestTopoSort2(t *testing.T) {
func TestReachability0(t *testing.T) {
{
G, _ := NewGraph("g")
result := G.Reachability(nil, nil)
if result != nil {
t.Logf("reachability failed")
str := "Got:"
for _, v := range result {
str += " " + v.String()
result, err := G.Reachability(nil, nil)
if err != nil {
t.Logf("reachability failed: %+v", err)
if result != nil {
str := "Got:"
for _, v := range result {
str += " " + v.String()
}
t.Errorf(str)
}
t.Errorf(str)
}
}
{
@@ -399,7 +401,11 @@ func TestReachability0(t *testing.T) {
v1 := NV("v1")
v6 := NV("v6")
result := G.Reachability(v1, v6)
result, err := G.Reachability(v1, v6)
if err != nil {
t.Logf("reachability failed: %+v", err)
return
}
expected := []Vertex{}
if !reflect.DeepEqual(result, expected) {
@@ -430,7 +436,11 @@ func TestReachability0(t *testing.T) {
G.AddEdge(v3, v4, e4)
G.AddEdge(v3, v5, e5)
result := G.Reachability(v1, v6)
result, err := G.Reachability(v1, v6)
if err != nil {
t.Logf("reachability failed: %+v", err)
return
}
expected := []Vertex{}
if !reflect.DeepEqual(result, expected) {
@@ -465,7 +475,11 @@ func TestReachability1(t *testing.T) {
G.AddEdge(v4, v5, e4)
G.AddEdge(v5, v6, e5)
result := G.Reachability(v1, v6)
result, err := G.Reachability(v1, v6)
if err != nil {
t.Logf("reachability failed: %+v", err)
return
}
expected := []Vertex{v1, v2, v3, v4, v5, v6}
if !reflect.DeepEqual(result, expected) {
@@ -500,7 +514,11 @@ func TestReachability2(t *testing.T) {
G.AddEdge(v4, v5, e5)
G.AddEdge(v5, v6, e6)
result := G.Reachability(v1, v6)
result, err := G.Reachability(v1, v6)
if err != nil {
t.Logf("reachability failed: %+v", err)
return
}
expected1 := []Vertex{v1, v2, v4, v5, v6}
expected2 := []Vertex{v1, v3, v4, v5, v6}
@@ -537,7 +555,11 @@ func TestReachability3(t *testing.T) {
G.AddEdge(v1, v5, e5)
G.AddEdge(v5, v6, e6)
result := G.Reachability(v1, v6)
result, err := G.Reachability(v1, v6)
if err != nil {
t.Logf("reachability failed: %+v", err)
return
}
expected := []Vertex{v1, v5, v6}
if !reflect.DeepEqual(result, expected) {
@@ -572,7 +594,11 @@ func TestReachability4(t *testing.T) {
G.AddEdge(v5, v6, e5)
G.AddEdge(v1, v6, e6)
result := G.Reachability(v1, v6)
result, err := G.Reachability(v1, v6)
if err != nil {
t.Logf("reachability failed: %+v", err)
return
}
expected := []Vertex{v1, v6}
if !reflect.DeepEqual(result, expected) {