[Java SDK] Warn when ValueState contains collection types#37530
[Java SDK] Warn when ValueState contains collection types#37530PDGGK wants to merge 3 commits intoapache:masterfrom
Conversation
When users declare ValueState<Map>, ValueState<List>, or ValueState<Set>, log a warning suggesting they use MapState, BagState, or SetState instead. Storing collections in ValueState requires reading and writing the entire collection on each access, which can cause performance issues for large collections. The specialized state types provide better performance. Fixes apache#36746
Summary of ChangesHello @PDGGK, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a potential performance bottleneck in Apache Beam by introducing a warning mechanism. When users declare Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Assigning reviewers: R: @kennknowles for label java. 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). |
Hi Jason,this looks like a great move for scalability! Promoting However, I'd like to bring up a small concern about potential false positives. In scenarios where the collection is small or the workflow requires atomic full overwrites (read-modify-write the entire list), If we log a warning for all collection types, it might unintentionally lead users to refactor efficient code (for their specific small-data use case) into less efficient patterns. Do you think it might be worth mentioning this trade-off in the log message, or perhaps restricting the warning to cases where we can detect partial updates (though I know that's hard to catch at graph construction time)? Just want to make sure we don't alarm users who are intentionally using |
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/DoFnSignatures.java
Outdated
Show resolved
Hide resolved
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/DoFnSignatures.java
Outdated
Show resolved
Hide resolved
|
Thanks @Eliaaazzz, this is a very good point! You're absolutely right that
I'll make the following changes:
This way we guide new users without alarming experienced users who intentionally use this pattern for valid use cases. Thanks for the thorough review! |
…e-offs - Change LOG.warn to LOG.info (performance hint, not correctness issue) - Clarify that ValueState is appropriate for small collections or atomic replacement - Add runner compatibility caveat for specialized state types - Address community feedback from @Eliaaazzz
- Use TypeDescriptor.resolveType() instead of raw Type manipulation - Use hasUnresolvedParameters() instead of instanceof checks - Use isSubtypeOf() for collection type detection - Remove catch-all Exception block entirely Addresses @kennknowles code review comments
|
Thanks for the review! I've addressed both comments:
This keeps all logic within the TypeDescriptor API as suggested. |
Summary
This PR adds a warning when users declare
ValueStatewith collection types (Map,List,Set) that could benefit from using specialized state types for better performance.Problem:
When users store collections in
ValueState, the entire collection must be read and written on each access. This can cause significant performance issues for large collections.Solution:
Log a warning during pipeline construction suggesting:
ValueState<Map>→ UseMapStateinsteadValueState<List>→ UseBagStateorOrderedListStateinsteadValueState<Set>→ UseSetStateinsteadChanges:
DoFnSignatures.java: AddedwarnIfValueStateContainsCollection()method that inspects state declarations and logs warnings for collection typesDoFnSignaturesTest.java: Added test cases to verify the warning logic works correctlyFixes #36746
Test plan
ValueState<Map>,ValueState<List>,ValueState<Set>ValueState<String>(no warning expected)🤖 Generated with Claude Code