Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sdks/go/pkg/beam/core/util/dot/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Deprecated:This package is no longer used by the Beam Go SDK.
// Deprecated: This package is no longer used by the Beam Go SDK.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to Go's style guide for comments, there should be a space after the colon in a Deprecated: notice. This improves readability.

Suggested change
// Deprecated:This package is no longer used by the Beam Go SDK.
// Deprecated: This package is no longer used by the Beam Go SDK.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to mentioning this here, perhaps worth adding it in Deprecations section in the release notes of the current unreleased version e.g
https://github.com/apache/beam/blob/master/CHANGES.md#deprecations

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 (
Expand Down
73 changes: 69 additions & 4 deletions sdks/go/pkg/beam/runners/dot/dot.go
Comment thread
YousufFFFF marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Comment thread
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)
}
Expand All @@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If pipeline.GetComponents() returns a non-nil Components but with a nil or empty Transforms map, pipelinex.TopologicalSort could panic if pipeline.GetRootTransformIds() returns a non-empty slice. It's safer to handle the case of no transforms explicitly to prevent a potential panic.

	transforms := components.GetTransforms()
	if len(transforms) == 0 {
		buf.WriteString("}\n")
		return nil, os.WriteFile(*dotFile, buf.Bytes(), 0644)
	}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 {
Comment thread
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() {
Comment thread
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)
}
67 changes: 67 additions & 0 deletions sdks/go/pkg/beam/runners/dot/dot_test.go
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()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an option of the built-in t.Context(). It is introduced in Go v1.24, and Beam Go SDK supports that version

go 1.25.0


// Create temporary DOT file
tmpFile, err := os.CreateTemp("", "dot_test_*.dot")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be the built-in t.TempDir() and it is removed automatically when outside the scope of test

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")
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
}
// Validate the output is parseable as a basic DOT digraph.
// Checks: header, footer, and that every non-structural line is a valid edge.
lines := strings.Split(strings.TrimSpace(content), "\n")
if lines[0] != "digraph G {" {
t.Fatalf("missing digraph header, got: %s", lines[0])
}
if lines[len(lines)-1] != "}" {
t.Fatalf("missing closing brace, got: %s", lines[len(lines)-1])
}
for _, line := range lines[1 : len(lines)-1] {
line = strings.TrimSpace(line)
if line == "" {
continue
}
if !strings.Contains(line, "->") || !strings.HasSuffix(line, ";") {
t.Fatalf("invalid DOT edge line: %s", line)
}
}

Comment on lines +46 to +66
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test name is TestDotRunner_GeneratesDeterministicOutput, but it doesn't actually verify determinism. It only performs a basic smoke test. To properly test for determinism, you could run the generation twice with identical pipelines and assert that the outputs are identical. This would make the test more aligned with its name and more robust.

	// 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))
	}

}
Loading