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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
* Fixed unbounded checkpoint state growth for splittable DoFns that self-checkpoint on the portable Flink runner (Java) ([#27648](https://github.com/apache/beam/issues/27648)).
* Improved Java pipeline performance by avoiding repeated `DoFn` type descriptor resolution when creating cached invokers ([#39309](https://github.com/apache/beam/issues/39309)).
* (Python) Fixed a memory leak in Python SDK caused by storing exceptions with potentially large stack frames in a cache ([#39406](https://github.com/apache/beam/issues/39406)).
* (Python) Fixed incorrect profiler options handling on portable runners ([#39613](https://github.com/apache/beam/issues/39613)).

## Security Fixes

Expand Down
8 changes: 7 additions & 1 deletion sdks/go/container/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,14 @@ func main() {
logger.Fatalf(ctx, "Failed to convert pipeline options: %v", err)
}

// Go SDK wraps pipeline options inside the URN namespace: "beam:option:go_options:v1".
po, err := tools.ParseOptionsFromProto(info.GetPipelineOptions(), "go_options")
if err != nil {
logger.Fatalf(ctx, "Failed to parse pipeline options: %v", err)
}

// Inject artifact validation enabled state into context
ctx = artifact.WithArtifactValidation(ctx, !artifact.HasExperiment(info.GetPipelineOptions(), "disable_staged_file_integrity_checks"))
ctx = artifact.WithArtifactValidation(ctx, !po.HasExperiment("disable_staged_file_integrity_checks"))

// (2) Retrieve the staged files.
//
Expand Down
182 changes: 182 additions & 0 deletions sdks/go/container/tools/pipeline_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"

structpb "google.golang.org/protobuf/types/known/structpb"
)

// MakePipelineOptionsFileAndEnvVar writes the pipeline options to a file.
Expand All @@ -42,3 +46,181 @@ func MakePipelineOptionsFileAndEnvVar(options string) error {
os.Setenv("PIPELINE_OPTIONS_FILE", f.Name())
return nil
}

// PipelineOptions represents parsed pipeline options as a normalized map.
type PipelineOptions struct {
options map[string]any
experiments map[string]string
}

// ParseOptionsFromProto creates normalized PipelineOptions directly from a protobuf Struct.
func ParseOptionsFromProto(opt *structpb.Struct, sdkNamespace string) (*PipelineOptions, error) {
if opt == nil {
return &PipelineOptions{options: make(map[string]any), experiments: make(map[string]string)}, nil
}
raw := opt.AsMap()
flat := make(map[string]any)

// 1. Extract nested options if present (Dataflow runner uses this structure)
if optsVal, ok := raw["options"]; ok {
if optsMap, ok := optsVal.(map[string]any); ok {
for k, v := range optsMap {
flat[k] = v
}
}
}

// 2. Extract standard URN keys (Portable runners use this structure)
for k, v := range raw {
if k == "options" || k == "display_data" {
continue
}
if strings.HasPrefix(k, "beam:option:") && strings.HasSuffix(k, ":v1") {
name := strings.TrimPrefix(k, "beam:option:")
name = strings.TrimSuffix(name, ":v1")
flat[name] = v
}
}

// 3. Promote specified SDK namespace options (Highest precedence, may overwrite earlier entries).
// Beam Go SDK uses this structure.
if sdkNamespace != "" {
sdkURN := fmt.Sprintf("beam:option:%s:v1", sdkNamespace)
if sdkVal, ok := raw[sdkURN]; ok {
if urnMap, ok := sdkVal.(map[string]any); ok {
if nestedOpts, ok := urnMap["options"].(map[string]any); ok {
for nk, nv := range nestedOpts {
flat[nk] = nv
}
}
}
}
}

po := &PipelineOptions{
options: flat,
experiments: make(map[string]string),
}
if exps, err := po.GetStringSlice("experiments"); err == nil {
if expsMap, err := parseExperiments(exps); err == nil {
po.experiments = expsMap
}
}
return po, nil
Comment on lines +104 to +109

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 is the only place in the function where the code potentially receiving and handling an error, but it does wind up swallowing it. If the error here is relevant it should be surfaced, otherwise the function doesn't need to return an error at all.

}

func parseExperiments(slice []string) (map[string]string, error) {

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.

Same thing here, there's never an error case to return. Is there an interface you're programming towards here?

res := make(map[string]string)
for _, item := range slice {
if strings.Contains(item, "=") {
parts := strings.SplitN(item, "=", 2)
res[parts[0]] = parts[1]
} else {
res[item] = ""
}
}
return res, nil
}

// HasOption returns true if the option is defined and not nil.
func (po *PipelineOptions) HasOption(name string) bool {
val, ok := po.options[name]
return ok && val != nil
}

// GetString returns the value of an option as a string.
func (po *PipelineOptions) GetString(name string) (string, error) {
val, ok := po.options[name]
if !ok || val == nil {
return "", fmt.Errorf("option %q not defined", name)
}
if str, ok := val.(string); ok {
return str, nil
}
return "", fmt.Errorf("option %q: expected string, got type %T", name, val)
}

// GetStringSlice returns the value of an option as a string slice.
func (po *PipelineOptions) GetStringSlice(name string) ([]string, error) {
val, ok := po.options[name]
if !ok || val == nil {
return nil, fmt.Errorf("option %q not defined", name)
}
if slice, ok := val.([]any); ok {
var res []string
for _, item := range slice {
if str, ok := item.(string); ok {
res = append(res, str)
} else {
return nil, fmt.Errorf("option %q: expected string slice element, got type %T", name, item)
}
}
return res, nil
}
if str, ok := val.(string); ok {
// Go SDK models multi-value list flags (like experiments or dataflow_service_options)
// as comma-separated string flags rather than JSON arrays.
if str == "" {
return nil, nil
}
return strings.Split(str, ","), nil
}
return nil, fmt.Errorf("option %q: expected string slice, got type %T", name, val)
}

// GetInt returns the value of an option as an integer.
func (po *PipelineOptions) GetInt(name string) (int, error) {
val, ok := po.options[name]
if !ok || val == nil {
return 0, fmt.Errorf("option %q not defined", name)
}
switch v := val.(type) {
case float64:
return int(v), nil
case string:
res, err := strconv.Atoi(v)
if err == nil {
return res, nil
}
return 0, fmt.Errorf("option %q: failed to parse %q as int: %w", name, v, err)
default:
return 0, fmt.Errorf("option %q: expected int (represented as number or string), got type %T", name, val)
}
}

// GetBool returns the value of an option as a boolean.
func (po *PipelineOptions) GetBool(name string) (bool, error) {
val, ok := po.options[name]
if !ok || val == nil {
return false, fmt.Errorf("option %q not defined", name)
}
switch v := val.(type) {
case bool:
return v, nil
case string:
res, err := strconv.ParseBool(v)
if err != nil {
return false, fmt.Errorf("option %q: failed to parse %q as bool: %w", name, v, err)
}
return res, nil
case float64:
return v != 0, nil
default:
return false, fmt.Errorf("option %q: expected bool, got type %T", name, val)
}
}

// LookupExperiment returns the value of an experiment option if present.
// - If the experiment is present but has no value (e.g., --experiments=foo), it returns "", true.
// - If the experiment is present as a key-value pair (e.g., --experiments=foo=bar), it returns "bar", true.
// - If the experiment is not present, it returns "", false.
func (po *PipelineOptions) LookupExperiment(key string) (string, bool) {
val, ok := po.experiments[key]
return val, ok
}

// HasExperiment returns true if the specified experiment is present in the options (either as a flag or key-value pair).
func (po *PipelineOptions) HasExperiment(name string) bool {
_, ok := po.LookupExperiment(name)
return ok
}
Loading
Loading