Repository navigation
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR introduces namespace-qualified pipeline option resolution across the Java, Python, and Go SDKs, enabling cross-language and multi-interface pipelines to disambiguate colliding option names (including incompatible types in Java). Overall, the approach is well-structured and backed by comprehensive unit tests across all three SDKs, though there are several bugs around default value caching, setter consistency, display data lookup, and Runner API URN case-conversion that should be addressed.
🔍 General Feedback
- Java (
ProxyInvocationHandler&PipelineOptionsFactory): Lazy default binding currently writes to the unqualifiedpropertyNamekey without checking if a conflicting value or colliding getter already exists, which causes getters with incompatible return types or different@Defaultannotations to overwrite or break each other. Additionally,PipelineOptionsTranslationconverts option keys using Guava'sCaseFormat, which corrupts uppercase class names and package dots during Runner API translation. - Python (
pipeline_options.py): Setting an attribute on aPipelineOptionsview unconditionally overwrites the unqualified key in_all_optionswhile reading prioritizes the class-qualified key, creating inconsistent state across views that share option names. - Go (
pipeline_options.go): Struct tag names and qualified snake_case/camelCase keys should be handled consistently duringLoadOptionsFromFlags.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
💬 Inline Findings
sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java(line 274): 🔴 Critical: When lazily binding a default value,options.put(propertyName, BoundValue.fromDefault(value))unconditionally writes to the unqualifiedpropertyNamekey. This causes two bugs when two interfaces define the same option name:
- If
optionsalready holds an explicitly set value forpropertyNameof an incompatible type (e.g.,StringfromFirstOptions, and nowSecondOptions.getFoo()is called expectingIntegerwith no qualified override set), this overwrites the explicitStringvalue inoptionswithSecondOptions's defaultInteger! - If two interfaces have the same option name with the same return type but different
@Defaultannotations (or neither is set yet), the first getter called binds its default to the sharedpropertyName, and the second getter will return the first interface's default instead of its own.
Lazy defaults should either be bound to a qualified candidate key (e.g., candidates.get(0)) when not declared on PipelineOptions, or at minimum not overwrite an existing entry in options.
String keyToBind = candidates.get(0);
-
sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java(line 292): 🟠 High: In the setter branch,targetKeyonly falls back tocandidates.get(0)when an existing unqualified value has an incompatible runtime type. If two interfaces share a property name with the same type (e.g.,DbOptions.setTimeout(100)followed byNetOptions.setTimeout(200)), both writes go to the unqualified"timeout"key and overwrite each other. Furthermore, ifDbOptions.setTimeout(null)is called whenpropertyNameholds aString,args[0] != nullis false so it overwrites the unqualified key withnull. Consider checking whether the property is defined on multiple known interfaces or if a qualified key is appropriate when disambiguating. -
sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java(line 317): 🟠 High:getCandidateKeysiterates overproxy.getClass().getInterfaces()BEFORE checkingmethod.getDeclaringClass(). When multiple interfaces are registered/proxied together (or when a sub-interface extends multiple option interfaces),proxy.getClass().getInterfaces()may list another interface beforemethod.getDeclaringClass(). If both interfaces have a qualified entry inoptions, the getter/setter forInterfaceBwill matchInterfaceA.foofirst!method.getDeclaringClass()(or the sub-interface inproxy.getClass().getInterfaces()to whichdeclaringClassis assignable, i.e.,declaringClass.isAssignableFrom(iface)) should be prioritized over unrelated interfaces on the proxy.
if (PipelineOptions.class.isAssignableFrom(iface) && iface != PipelineOptions.class && declaringClass.isAssignableFrom(iface)) {
-
sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java(line 500): 🟡 Medium: InPipelineOptionsDisplayData.populateDisplayData,optionsMapis keyed only by the unqualifiedprop.getName()(seebuildOptionNameToSpecMap). When an option is stored inoptionsorjsonOptionsunder a qualified key (e.g.,"FirstOptions.foo"or"firstOptions.foo"),optionsMap.get(option.getKey())returns an empty collection. As a result, qualified options incopiedOptionsare silently dropped fromDisplayData, and qualified options injsonOptionsare added underUnknownPipelineOptionsinstead of their defining interface. -
sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java(line 1762): 🟠 High:PipelineOptionsTranslation.toProtoandfromProtoconvert option keys betweenLOWER_CAMELandLOWER_UNDERSCOREusing Guava'sCaseFormat. For qualified keys: -
In
toProto:"MyOptions.fooBar"becomes"beam:option:my_options.foo_bar:v1". -
In
fromProto:CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "my_options.foo_bar")produces"myOptions.fooBar", and for a snake_case class prefix from Python like"beam:option:test_db_options.timeout:v1"it produces"testDbOptions.timeout"(which works viadecap), except when the class name has consecutive uppercase letters (e.g.MyDBOptions-> Python_camel_to_snakeproducesmy_db_options-> JavaLOWER_UNDERSCORE.to(LOWER_CAMEL)producesmyDbOptions, whereas JavaIntrospector.decapitalize("MyDBOptions")is"MyDBOptions"because the first two characters are uppercase!). -
Worse, if a fully-qualified class name is used (
org.example.MyOptions.fooBar),LOWER_UNDERSCORE.to(LOWER_CAMEL)turnsorg.example.my_options.foo_barintoorg.example.myOptions.fooBar, which will not matchfull(org.example.MyOptions.fooBar),simple, ordecap. -
sdks/python/apache_beam/options/pipeline_options.py(line 225): 🟠 High: When_add_argparse_argsis called with conflicting unqualified flags across subclasses (inget_all_options),has_conflictskipssuper().add_argument(*args, **kwargs). However, for the qualified flags added below (--Cls.flag,--cls_snake.flag),q_kwargs['default'] = Noneis used. If the second conflicting class defines a differentdefaultfor--flag, the unqualified dest inget_all_options()will only have the first class's default, and if a user instantiatesview = options.view_as(SecondClass),view._visible_optionshasSecondClass.flag = None, while_all_options['flag']was already populated by the first class's default, soview.flagwill return the first class's default instead ofSecondClass's default. -
sdks/python/apache_beam/options/pipeline_options.py(line 569): 🟠 High: In__setattr__, setting an option on a subclass view writes to bothself._all_options[f"{cls_name}.{name}"] = valueANDself._all_options[name] = value, whereas__getattr__checksself._all_options[f"{cls_name}.{name}"]beforeself._all_options[name]. This causes two issues:
- If
--TestDbOptions.timeout=100is passed on the CLI and the user later setsoptions.view_as(TestDbOptions).timeout = None,__getattr__will ignoreNone(because ofand self._all_options[qualified_key] is not Noneon line 555) or fall back to_all_options['timeout']. - If two views share a valid option (e.g. a base class and subclass, or two views sharing a standard option), setting
view1.opt = 'a'setsView1.opt = 'a'andopt = 'a'. Ifview2.opt = 'b'is then set on another view,optbecomes'b', butview1.optis stuck returning'a'forever becauseView1.optin_all_optionsis never updated when modified viaPipelineOptionsor another view! As documented inview_as(), modifications in any view object are supposed to be reflected across views unless explicitly disambiguated.
sdks/go/pkg/beam/options/gcpopts/options_test.go(line 1): 🟢 Low: Insdks/go/pkg/beam/core/runtime/pipelinex/replace.go/sdks/go/container/tools/pipeline_options.go(oroptions.go),LoadOptionsFromFlagsmatches struct fields byjsontag name (tag := strings.Split(f.Tag.Get("json"), ",")[0]), which is often snake_case or differs fromf.Name. However, the qualified name candidates<structName>.<fieldName>only usef.Name(andstrings.ToLower(f.Name[:1]) + f.Name[1:]), nottag(iftag != ""andtag != f.Name). Supporting<prefix>.<tag>as well would ensure struct fields with custom JSON tags work with qualified flags.
|
Assigning reviewers: R: @chamikaramj for label java. This pull request likely touches a core component ("core" label). Please review with scrutiny. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Description
Closes #19331
This PR introduces namespace support for PipelineOptions across the Java and Python SDKs, allowing options with identical names to be distinguished through qualified names.
Motivation
PipelineOptions with identical property names across different option interfaces or groups can create naming conflicts, especially when their types differ.
This change introduces qualified option names while preserving existing unqualified option behaviour.
Java SDK
--<Group>.<option>, decapitalized--<group>.<option>, and FQCN--<pkg>.<Group>.<option>).MultimapinPipelineOptionsFactoryto allow disjoint interfaces to define identical property names without false type collisions.ProxyInvocationHandler.Python SDK
--<Group>.<option>and snake_case--<group_name>.<option>) in_BeamArgumentParser.PipelineOptions.__getattr__.PipelineOptions.__setattr__.to_runner_api/from_runner_api).Backward Compatibility
testDisjointSiblingsShareValuespasses).Tests
:sdks:java:core:test):./gradlew :sdks:java:core:test --tests "org.apache.beam.sdk.options.*" --tests "org.apache.beam.sdk.util.construction.PipelineOptionsTranslationTest*"Result: 257 passed, 0 failed, 0 skipped.
:runners:direct-java:test):./gradlew :runners:direct-java:test --tests "org.apache.beam.runners.direct.DirectRunnerTest*"Result: 22 passed, 0 failed, 0 skipped.
pytest):pytest sdks/python/apache_beam/options/pipeline_options_test.pyResult: 162 passed, 2 skipped, 0 failed.
./gradlew :sdks:java:core:spotlessCheck(BUILD SUCCESSFUL).Discussion
Initial design discussion was started on the Apache Beam developer mailing list. Feedback on compatibility, option resolution and runner integration is welcome.
Issue: #19331