-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add helpers to interact with pipeline options in boot entrypoints #39595
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
Open
tvalentyn
wants to merge
4
commits into
apache:master
Choose a base branch
from
tvalentyn:profiler_options_2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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 | ||
| } | ||
|
|
||
| func parseExperiments(slice []string) (map[string]string, error) { | ||
|
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. 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 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.