-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[Go SDK] Rewrite dot runner to generate DOT from portable pipeline proto #37673
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
base: master
Are you sure you want to change the base?
Changes from all commits
7e6be92
30926f4
8c826f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,9 @@ | |
| // limitations under the License. | ||
|
|
||
| // Package dot produces DOT graphs from Beam graph representations. | ||
| // | ||
| // Deprecated:This package is no longer used by the Beam Go SDK. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
| // It is slated for removal in a future Beam release. | ||
|
Comment on lines
+18
to
+19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition to mentioning this here, perhaps worth adding it in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, I'd avoid bringing it up until after the more comprehensive refactoring is complete. It's a non-critical package that's not likely to be broadly used, so it doesn't require that attention at this stage. |
||
| package dot | ||
|
|
||
| import ( | ||
|
|
||
|
YousufFFFF marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,13 +21,18 @@ import ( | |
| "bytes" | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "os" | ||
| "sort" | ||
|
|
||
| "github.com/apache/beam/sdks/v2/go/pkg/beam" | ||
| dotlib "github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/dot" | ||
|
YousufFFFF marked this conversation as resolved.
|
||
| "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/graphx" | ||
| "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/pipelinex" | ||
| "github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors" | ||
| ) | ||
|
|
||
| var errNoComponents = errors.New("pipeline has no components") | ||
|
|
||
| func init() { | ||
| beam.RegisterRunner("dot", Execute) | ||
| } | ||
|
|
@@ -42,14 +47,74 @@ func Execute(ctx context.Context, p *beam.Pipeline) (beam.PipelineResult, error) | |
| return nil, errors.New("must supply dot_file argument") | ||
| } | ||
|
|
||
| edges, nodes, err := p.Build() | ||
| edges, _, err := p.Build() | ||
| if err != nil { | ||
| return nil, errors.New("can't get data to render") | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| if err := dotlib.Render(edges, nodes, &buf); err != nil { | ||
| pipeline, err := graphx.Marshal(edges, &graphx.Options{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| buf.WriteString("digraph G {\n") | ||
|
|
||
| components := pipeline.GetComponents() | ||
| if components == nil { | ||
| return nil, errNoComponents | ||
| } | ||
|
|
||
| transforms := components.GetTransforms() | ||
|
|
||
|
Comment on lines
+68
to
+69
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If transforms := components.GetTransforms()
if len(transforms) == 0 {
buf.WriteString("}\n")
return nil, os.WriteFile(*dotFile, buf.Bytes(), 0644)
}
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be ignored, as that's not a problem the runner should be dealing with and indicates an issue with the submitting SDK. |
||
| // Build reverse input index: PCollectionID -> []TransformID | ||
| consumers := make(map[string][]string) | ||
| for tid, t := range transforms { | ||
|
lostluck marked this conversation as resolved.
|
||
| // Skip composite transforms | ||
| if len(t.GetSubtransforms()) != 0 { | ||
| continue | ||
| } | ||
|
|
||
| for _, pcollID := range t.GetInputs() { | ||
| consumers[pcollID] = append(consumers[pcollID], tid) | ||
| } | ||
| } | ||
|
|
||
| // Ensure deterministic ordering of consumer lists | ||
| for pcollID := range consumers { | ||
| sort.Strings(consumers[pcollID]) | ||
| } | ||
|
|
||
| // Topologically sort transforms for deterministic emission. | ||
| // We use the same ordering utility as Prism to ensure stable and execution-consistent graph traversal. | ||
| roots := pipeline.GetRootTransformIds() | ||
| ordered := pipelinex.TopologicalSort(transforms, roots) | ||
|
|
||
| // Generate edges | ||
| for _, tid := range ordered { | ||
| t := transforms[tid] | ||
| // Skip composite transforms | ||
| if len(t.GetSubtransforms()) != 0 { | ||
| continue | ||
| } | ||
|
|
||
| from := t.GetUniqueName() | ||
|
|
||
| for _, pcollID := range t.GetOutputs() { | ||
|
YousufFFFF marked this conversation as resolved.
|
||
| for _, consumerID := range consumers[pcollID] { | ||
|
|
||
| consumer, ok := transforms[consumerID] | ||
| if !ok { | ||
| continue // Defensively skip if the consumer transform is missing | ||
| } | ||
|
|
||
| to := consumer.GetUniqueName() | ||
| fmt.Fprintf(&buf, "\"%s\" -> \"%s\";\n", from, to) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| buf.WriteString("}\n") | ||
|
|
||
| return nil, os.WriteFile(*dotFile, buf.Bytes(), 0644) | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,67 @@ | ||||||||||||||||||||||||||||||||||||||||
| // Licensed to the Apache Software Foundation (ASF) under one or more | ||||||||||||||||||||||||||||||||||||||||
| // contributor license agreements. See the NOTICE file distributed with | ||||||||||||||||||||||||||||||||||||||||
| // this work for additional information regarding copyright ownership. | ||||||||||||||||||||||||||||||||||||||||
| // The ASF licenses this file to You under the Apache License, Version 2.0 | ||||||||||||||||||||||||||||||||||||||||
| // (the "License"); you may not use this file except in compliance with | ||||||||||||||||||||||||||||||||||||||||
| // the License. You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||||||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||||||||||
| // See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||||||||||
| // limitations under the License. | ||||||||||||||||||||||||||||||||||||||||
| package dot | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||
| "testing" | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| "github.com/apache/beam/sdks/v2/go/pkg/beam" | ||||||||||||||||||||||||||||||||||||||||
| "github.com/apache/beam/sdks/v2/go/pkg/beam/testing/passert" | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func TestDotRunner_GeneratesDeterministicOutput(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||
| ctx := context.Background() | ||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is an option of the built-in Line 23 in 3197d88
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Create temporary DOT file | ||||||||||||||||||||||||||||||||||||||||
| tmpFile, err := os.CreateTemp("", "dot_test_*.dot") | ||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be the built-in |
||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||
| t.Fatalf("failed to create temp file: %v", err) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| defer os.Remove(tmpFile.Name()) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Set flag manually | ||||||||||||||||||||||||||||||||||||||||
| *dotFile = tmpFile.Name() | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Build simple pipeline | ||||||||||||||||||||||||||||||||||||||||
| p, s := beam.NewPipelineWithRoot() | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| col := beam.Create(s, "a", "b", "c") | ||||||||||||||||||||||||||||||||||||||||
| passert.Count(s, col, "", 3) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Run with dot runner | ||||||||||||||||||||||||||||||||||||||||
| _, err = Execute(ctx, p) | ||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||
| t.Fatalf("Execute failed: %v", err) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Read generated file | ||||||||||||||||||||||||||||||||||||||||
| data, err := os.ReadFile(tmpFile.Name()) | ||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||
| t.Fatalf("failed to read dot file: %v", err) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| content := string(data) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if !strings.HasPrefix(content, "digraph G {") { | ||||||||||||||||||||||||||||||||||||||||
| t.Fatalf("dot output missing header") | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if !strings.Contains(content, "->") { | ||||||||||||||||||||||||||||||||||||||||
| t.Fatalf("dot output contains no edges") | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can it be something along those lines for more comprehensive checks?
Suggested change
Comment on lines
+46
to
+66
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test name is // Run with dot runner
_, err = Execute(ctx, p)
if err != nil {
t.Fatalf("Execute failed: %v", err)
}
// Read generated file
data1, err := os.ReadFile(tmpFile.Name())
if err != nil {
t.Fatalf("failed to read dot file: %v", err)
}
content1 := string(data1)
if !strings.HasPrefix(content1, "digraph G {") {
t.Fatalf("dot output missing header")
}
if !strings.Contains(content1, "->") {
t.Fatalf("dot output contains no edges")
}
// Run again on a new identical pipeline to check for determinism.
p2, s2 := beam.NewPipelineWithRoot()
col2 := beam.Create(s2, "a", "b", "c")
passert.Count(s2, col2, "", 3)
_, err = Execute(ctx, p2)
if err != nil {
t.Fatalf("Execute on second pipeline failed: %v", err)
}
data2, err := os.ReadFile(tmpFile.Name())
if err != nil {
t.Fatalf("failed to read dot file on second run: %v", err)
}
if content1 != string(data2) {
t.Errorf("output is not deterministic. Run 1:\n%s\nRun 2:\n%s", content1, string(data2))
} |
||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
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.