Repository navigation
[Python] Preserve per-element event-time timestamps in batched RequestResponseIO / Enrichment (#41348) - #41433
shikhabhadoria wants to merge 7 commits into
Conversation
…tResponseIO / Enrichment (apache#41348)
There was a problem hiding this comment.
📋 Review Summary
This PR fixes #41348 by preserving per-element event-time timestamps, windows, and pane metadata across BatchElements in RequestResponseIO and Enrichment. The approach cleanly wraps elements in _WindowedRequest prior to batching, unwraps them before invoking the Caller, re-associates responses (supporting both out-of-order (request, response) tuples and positional responses), and restores each element's WindowedValue in FlattenBatch.
🔍 General Feedback
- Adapting
element_size_fn,length_fn, andkey_fnin_adapt_batching_kwargsensures custom batch sizing predicates continue to receive the unwrapped request object. - The two-pass request matching in
_reattach_window_metadata(identity fast-path followed by equality fallback and positional fallback) properly handles handlers that reorder responses or reconstructbeam.Rowinstances. - Unit tests thoroughly cover out-of-order batch responses, downstream
FixedWindowsaggregation, pre-windowed inputs, and metadata reattachment fallbacks.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @tvalentyn for label python. 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). |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #41433 +/- ##
============================================
- Coverage 58.78% 58.68% -0.10%
Complexity 2296 2296
============================================
Files 1135 1135
Lines 170282 170341 +59
Branches 1490 1490
============================================
- Hits 100096 99964 -132
- Misses 67642 67833 +191
Partials 2544 2544
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
thanks, are you a user of RR IO? |
|
Hi @tvalentyn, we encountered this through |
|
/gemini review |
|
i see. thanks, i can take a closer look in 2 weeks or so. if you would like a faster response, you can comment " assign to next reviewer " |
|
Assigning reviewers: R: @shunping for label python. 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). |
|
Thanks @tvalentyn! Since the bot has already reassigned the review to @shunping, @shunping could you please take a look when you get a chance? |
shunping
left a comment
There was a problem hiding this comment.
Thanks for the contribution!
While I understand the logic of this PR, I have a design question.
Since BatchElements emits every batch at the end of its window by design, losing per-element event-time timestamps affects any caller of BatchElements (such as RequestResponseIO, RunInference, etc.).
I'm wondering whether we should handle this downstream in RequestResponseIO or push it upstream as an option/feature in BatchElements (e.g., returning WindowedValues or tracking element metadata) so other transforms can benefit as well.
Thoughts? @damccorm @tvalentyn
I think it would be nice, but in practice I'm having a hard time imagining an experience that makes sense for everyone. For example, take RunInference - it takes the outputs of the BatchElements call and feeds them directly into a model as a sequence - wrapping the elements in windowed objects wouldn't work here. Most downstream consumers probably don't actually care as well. The place where it does make sense is where you're doing some batching -> an operation -> unbatching, which is not actually that generic IMO (and there is a workaround available as shown in this PR). Disclaimer, I haven't reviewed the PR so don't consider this an approval, but the approach generally is reasonable at least. ;tldr - I'm probably -1 on trying to make this generic. |
Makes sense—thanks for the input, @damccorm! I'm onboard with keeping this scoped to |
| if idx < len(windowed_requests) else windowed_requests[-1]) | ||
| wrapped.append( | ||
| _WindowedRequest( | ||
| resp_item, |
There was a problem hiding this comment.
Nit: It feels a bit confusing for _reattach_window_metadata to return a list[_WindowedRequest] (and for FlattenBatch to yield element.request) when the wrapped item at that point is actually the response.
Could we either rename _WindowedRequest / .request to something stage-neutral like _WindowedElement / .value, use a plain (value, timestamp, window, pane_info) tuple, or use apache_beam.utils.windowed_value.WindowedValue (keeping in mind that yielding a top-level WindowedValue from a DoFn is automatically unwrapped by Beam, so it can't be used directly as the pre-batch element wrapper)?
There was a problem hiding this comment.
Good catch. Renamed to _WindowedElement with a stage-neutral .value payload (request before the call, response afterward). FlattenBatch now yields element.value.
| by_id[id(w.request)].append(w) | ||
| remaining: list[_WindowedRequest] = list(windowed_requests) | ||
|
|
||
| wrapped: list[_WindowedRequest] = [] |
There was a problem hiding this comment.
It's worth adding a brief docstring or inline comments here to document the matching heuristics.
There was a problem hiding this comment.
Added a docstring on _reattach_window_metadata documenting the matching order: identity on (request, response) tuples, then equality for reconstructed values, then positional fallback, plus the scalar/beam.Row wrapping case.
Address review feedback: use stage-neutral _WindowedElement.value instead of _WindowedRequest.request, and document identity/equality/ positional matching in _reattach_window_metadata.
|
@shunping thanks for the review. Addressed both comments in 84aa794:
Also merged latest Agree with keeping this scoped to |
Summary
Fixes #41348.
When
RequestResponseIO/Enrichmentruns with batching enabled (self._batching_kwargsnon-empty, e.g.BigQueryEnrichmentHandlerorCloudSQLEnrichmentHandler):inputs | BatchElements(**self._batching_kwargs)emits each batch atwindow.max_timestamp()(END_OF_GLOBAL_WINDOW=Timestamp(9223371950454.774999)inGlobalWindows).FlattenBatchpreviously yielded un-timestamped elements from the batch, causing every enriched record to inheritEND_OF_GLOBAL_WINDOWand breaking downstreamWindowInto(FixedWindows(...))and timestamp-sensitive sinks.Changes
_AttachWindowMetadataDoFnand_WindowedRequestprior toBatchElementsinRequestResponseIO.expandto capture each element's(timestamp, windows, pane_info)(while adaptingelement_size_fn,length_fn, andkey_fninbatch_elements_kwargsto unwrap_WindowedRequest)._CallDoFn.processto unwrap raw requests before invokingself._repeater.repeat(self._caller, ...), re-associate each returned response with its corresponding request's windowing metadata (supporting both out-of-order(request, response)tuples and positional responses), and emit the batch atmin(timestamps).FlattenBatch.processto emitwindowed_value.WindowedValue(element.request, element.timestamp, element.windows, element.pane_info), preserving exact per-element event-time timestamps and windows.sdks/python/apache_beam/io/requestresponse_test.pyand updatedCHANGES.md.Checklist
addresses #123orfixes #456).CHANGES.mdwith noteworthy changes.