Repository navigation
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR adds ElementByteSizeObservableIterable support to FnApiStateAccessor.CachingStateIterable, enabling lazy byte-size observation for iterables returned from BagUserState, MultimapUserState, IterableSideInput, and MultimapSideInput when emitted to output PCollections. However, the current implementation in CachingStateIterable bypasses PrefetchableIterable semantics for non-StateBackedIterable delegates (such as LazyCachingIteratorToIterable and concatenated iterables) and fails to notify byte-size observers when elements are served from StateBackedIterable's prefix or suffix lists.
🔍 General Feedback
CachingStateIterablewraps a variety ofPrefetchableIterableimplementations (not justStateBackedIterable, e.g.,PrefetchableIterables.concat(...)afterBagUserState.appendorLazyCachingIteratorToIterableinMultimapUserState.entries()). Relying solely oninstanceof ElementByteSizeObservableIterabledrops lazy byte-size observation andPrefetchableIteratordelegation wheneverStateBackedIterableis wrapped or not used.- Even when the delegate is a
StateBackedIterable,StateBackedIterable.createIterator()only returns anElementByteSizeObservableIteratorfor the state-fetched middle portion; elements inStateBackedIterable.prefixandStateBackedIterable.suffixare iterated via standard collection iterators that do not notify registered byte-size observers.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
💬 Inline Findings
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/FnApiStateAccessor.java(line 656): 🟠 High:CachingStateIterablewraps arbitraryPrefetchableIterable<T>instances returned byBagUserState,MultimapUserState,IterableSideInput, andMultimapSideInput. In many common cases,stateIterableis not a bareStateBackedIterable:
- After
BagUserState.append(...)orMultimapUserState.put(...)on an existing key,get()returnsPrefetchableIterables.concat(oldValues, newValues), which extendsPrefetchableIterables.Default(notElementByteSizeObservableIterable). - After
BagUserState.clear()+append(...)orMultimapUserState.put(...)on a new/removed key,get()returnsPrefetchableIterables.limit(...). MultimapUserState.entries()andMultimapUserState.keys()returnLazyCachingIteratorToIterableorPrefetchableIterables.concat(...).- Even when
stateIterableis aStateBackedIterable,StateBackedIterable.iterator()concatenatesprefix.iterator(),StateFetchingIterators.readAllAndDecodeStartingFrom(...), andsuffix.iterator().StateBackedIterable.createIterator()only creates the middle iterator (ignoringprefixandsuffix), and whenCachingStateIterable.createIterator()falls back tostateIterable.iterator()(or whenStateBackedIterablehas non-emptyprefix/suffix), elements fromprefix,suffix, or non-StateBackedIterabledelegates are never reported vianotifyValueReturned(...).
Additionally, the fallback iterator here does not forward isReady() or prefetch() to delegate (which is a PrefetchableIterator<T>), breaking prefetching when createIterator() is called.
Consider either:
- Having
CachingStateIterablewrapstateIterable.iterator()and compute/notify byte sizes using the elementCoder<T>(or delegating byte-size observation when the underlying iterator is anElementByteSizeObservableIteratorand falling back toelemCoder.registerByteSizeObserver(element, ...)for in-memory/appended elements), while also implementingPrefetchableIterator<T>on the returned iterator soisReady()andprefetch()are preserved; or - Ensuring
StateBackedIterable(includingprefix/suffix) andPrefetchableIterablescombinators (concat,limit,LazyCachingIteratorToIterable) propagateElementByteSizeObservableIterable/ElementByteSizeObserver.
FnApiStateAccessor.bindBag().read() previously returned BagUserState.get() directly, which does not implement ElementByteSizeObservableIterable. When a DoFn such as GroupIntoBatches emits a BagState iterable, PCollectionConsumerRegistry.SampleByteSizeDistribution calls IterableLikeCoder.registerByteSizeObserver(), triggering a full eager fetch and decode of the entire bag state before the downstream DoFn iterates it a second time. Expose StateBackedIterable.WrappedObservingIterator as package-private and reuse it via FnApiStateAccessor.observeIterable() in bindBag().read() so byte-size observation occurs lazily during the single downstream pass, and short-circuit WrappedObservingIterator.next() when no observers are attached.
6b918f0 to
1a6e3f4
Compare
|
The review comments is odd
There is no CachingStateIterable under FnApiStateAccessor. The PR only touches
etc |

FnApiStateAccessor.bindBag().read() previously returned BagUserState.get() directly, which does not implement ElementByteSizeObservableIterable. When a DoFn such as GroupIntoBatches emits a BagState iterable, PCollectionConsumerRegistry.SampleByteSizeDistribution calls IterableLikeCoder.registerByteSizeObserver(), triggering a full eager fetch and decode of the entire bag state before the downstream DoFn iterates it a second time.
Reuse StateBackedIterable.WrappedObservingIterator via a package-private StateBackedIterable.observeIterable() helper in FnApiStateAccessor.bindBag().read() so byte-size observation occurs lazily during the single downstream pass, and short-circuit WrappedObservingIterator.next() when no observers are attached.