-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ShortestPathStable and always add graphstate vertices
These resolve engine behaving non-deterministically
- Loading branch information
1 parent
df87741
commit c1bf5c6
Showing
20 changed files
with
413 additions
and
147 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -17,5 +17,6 @@ | |
"pkg/engine": true, | ||
"pkg/knowledge_base": true, | ||
"**/node_modules": true | ||
} | ||
}, | ||
"go.testTimeout": "5m" | ||
} |
This file contains 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,23 @@ | ||
#!/bin/sh | ||
|
||
# This script takes in an input yaml, and runs the engine on it twice. | ||
# It saves the log output in out.log and out2.log. | ||
# It then filters the logs for important messages and outputs to out.queue.log and out2.queue.log. | ||
# These files can then be diffed to see if the engine is behaving the same way on the same input. | ||
|
||
file=$1 | ||
if [ ! -f "$file" ]; then | ||
file="./pkg/engine2/testdata/$1" | ||
fi | ||
|
||
rm out.log out2.log | ||
export NO_COLOR=1 | ||
export COLUMNS=80 | ||
go run ./cmd/engine Run -i "$file" -o "./out/$(basename $file .yaml)" -v 2> out.log | ||
sleep 2 | ||
go run ./cmd/engine Run -i "$file" -o "./out/$(basename $file .yaml)2" -v 2> out2.log | ||
|
||
rm out.queue.log out2.queue.log | ||
grep -E -e 'op: dequeue|eval|poll-deps' -e 'Satisfied' out.log > out.queue.log | ||
grep -E -e 'op: dequeue|eval|poll-deps' -e 'Satisfied' out2.log > out2.queue.log | ||
|
This file contains 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
This file contains 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
This file contains 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,116 @@ | ||
package construct2 | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
|
||
"github.com/klothoplatform/klotho/pkg/dot" | ||
) | ||
|
||
func dotAttributes(r *Resource) map[string]string { | ||
a := make(map[string]string) | ||
a["label"] = r.ID.String() | ||
a["shape"] = "box" | ||
return a | ||
} | ||
|
||
func dotEdgeAttributes(e ResourceEdge) map[string]string { | ||
a := make(map[string]string) | ||
_ = e.Source.WalkProperties(func(path PropertyPath, nerr error) error { | ||
v := path.Get() | ||
if v == e.Target.ID { | ||
a["label"] = path.String() | ||
return StopWalk | ||
} | ||
return nil | ||
}) | ||
return a | ||
} | ||
|
||
func GraphToDOT(g Graph, out io.Writer) error { | ||
ids, err := ToplogicalSort(g) | ||
if err != nil { | ||
return err | ||
} | ||
nodes, err := ResolveIds(g, ids) | ||
if err != nil { | ||
return err | ||
} | ||
var errs error | ||
printf := func(s string, args ...any) { | ||
_, err := fmt.Fprintf(out, s, args...) | ||
errs = errors.Join(errs, err) | ||
} | ||
printf(`digraph { | ||
rankdir = TB | ||
`) | ||
for _, n := range nodes { | ||
printf(" %q%s\n", n.ID, dot.AttributesToString(dotAttributes(n))) | ||
} | ||
|
||
topoIndex := func(id ResourceId) int { | ||
for i, id2 := range ids { | ||
if id2 == id { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
edges, err := g.Edges() | ||
if err != nil { | ||
return err | ||
} | ||
sort.Slice(edges, func(i, j int) bool { | ||
ti, tj := topoIndex(edges[i].Source), topoIndex(edges[j].Source) | ||
if ti != tj { | ||
return ti < tj | ||
} | ||
ti, tj = topoIndex(edges[i].Target), topoIndex(edges[j].Target) | ||
return ti < tj | ||
}) | ||
for _, e := range edges { | ||
edge, err := g.Edge(e.Source, e.Target) | ||
if err != nil { | ||
errs = errors.Join(errs, err) | ||
continue | ||
} | ||
printf(" %q -> %q%s\n", e.Source, e.Target, dot.AttributesToString(dotEdgeAttributes(edge))) | ||
} | ||
printf("}\n") | ||
return errs | ||
} | ||
|
||
func GraphToSVG(g Graph, prefix string) error { | ||
if debugDir := os.Getenv("KLOTHO_DEBUG_DIR"); debugDir != "" { | ||
prefix = filepath.Join(debugDir, prefix) | ||
} | ||
f, err := os.Create(prefix + ".gv") | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
dotContent := new(bytes.Buffer) | ||
err = GraphToDOT(g, io.MultiWriter(f, dotContent)) | ||
if err != nil { | ||
return fmt.Errorf("could not render graph to file %s: %v", prefix+".gv", err) | ||
} | ||
|
||
svgContent, err := dot.ExecPan(bytes.NewReader(dotContent.Bytes())) | ||
if err != nil { | ||
return fmt.Errorf("could not run 'dot' for %s: %v", prefix+".gv", err) | ||
} | ||
|
||
svgFile, err := os.Create(prefix + ".gv.svg") | ||
if err != nil { | ||
return fmt.Errorf("could not create file %s: %v", prefix+".gv.svg", err) | ||
} | ||
defer svgFile.Close() | ||
_, err = fmt.Fprint(svgFile, svgContent) | ||
return err | ||
} |
This file contains 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
This file contains 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.