shikhabhadoria commented on code in PR #41433:
URL: https://github.com/apache/beam/pull/41433#discussion_r4705068275
##########
sdks/python/apache_beam/io/requestresponse.py:
##########
@@ -336,6 +338,93 @@ def expand(
_CallDoFn(self._caller, self._timeout, self._repeater,
self._throttler))
+class _WindowedRequest:
+ """Internal wrapper preserving per-element windowing metadata across
batching."""
+ def __init__(
+ self,
+ request: Any,
+ timestamp: Any,
+ windows: tuple[Any, ...],
+ pane_info: Any = windowed_value.PANE_INFO_UNKNOWN):
+ self.request = request
+ self.timestamp = timestamp
+ self.windows = windows
+ self.pane_info = pane_info
+
+
+class _AttachWindowMetadataDoFn(beam.DoFn):
+ """Captures per-element timestamp, window, and pane_info before batching."""
+ def process(
+ self,
+ element,
+ timestamp=beam.DoFn.TimestampParam,
+ window=beam.DoFn.WindowParam,
+ pane_info=beam.DoFn.PaneInfoParam):
+ yield _WindowedRequest(element, timestamp, (window, ), pane_info)
+
+
+def _adapt_batching_kwargs(
+ batching_kwargs: Mapping[str, Any]) -> dict[str, Any]:
+ adapted = dict(batching_kwargs)
+ for fn_key in ('element_size_fn', 'length_fn', 'key_fn'):
+ if fn_key in adapted and adapted[fn_key] is not None:
+ orig_fn = adapted[fn_key]
+ adapted[fn_key] = (
+ lambda el, _fn=orig_fn: _fn(
+ el.request if isinstance(el, _WindowedRequest) else el))
+ return adapted
+
+
+def _reattach_window_metadata(
+ windowed_requests: list[_WindowedRequest],
+ raw_responses: Any) -> list[_WindowedRequest]:
+ if not isinstance(raw_responses, collections.abc.Iterable) or isinstance(
+ raw_responses, (bytes, str, beam.Row)):
+ w0 = windowed_requests[0]
+ return [
+ _WindowedRequest(raw_responses, w0.timestamp, w0.windows, w0.pane_info)
+ ]
+
+ resp_list = list(raw_responses)
+ by_id: dict[int, collections.deque[_WindowedRequest]] = (
+ collections.defaultdict(collections.deque))
+ for w in windowed_requests:
+ by_id[id(w.request)].append(w)
+ remaining: list[_WindowedRequest] = list(windowed_requests)
+
+ wrapped: list[_WindowedRequest] = []
+ for idx, resp_item in enumerate(resp_list):
+ matched_w: Optional[_WindowedRequest] = None
+ if isinstance(resp_item, tuple) and len(resp_item) == 2:
+ req_candidate = resp_item[0]
+ cand_id = id(req_candidate)
+ if cand_id in by_id and by_id[cand_id]:
+ matched_w = by_id[cand_id].popleft()
+ for r_idx, rem_w in enumerate(remaining):
+ if rem_w is matched_w:
+ remaining.pop(r_idx)
+ break
+ else:
+ for r_idx, rem_w in enumerate(remaining):
+ if rem_w.request == req_candidate:
+ matched_w = remaining.pop(r_idx)
+ rem_id = id(matched_w.request)
+ if rem_id in by_id and matched_w in by_id[rem_id]:
+ by_id[rem_id].remove(matched_w)
+ break
+ if matched_w is None:
+ matched_w = (
+ windowed_requests[idx]
+ if idx < len(windowed_requests) else windowed_requests[-1])
+ wrapped.append(
+ _WindowedRequest(
+ resp_item,
Review Comment:
Good catch. Renamed to `_WindowedElement` with a stage-neutral `.value`
payload (request before the call, response afterward). `FlattenBatch` now
yields `element.value`.
##########
sdks/python/apache_beam/io/requestresponse.py:
##########
@@ -336,6 +338,93 @@ def expand(
_CallDoFn(self._caller, self._timeout, self._repeater,
self._throttler))
+class _WindowedRequest:
+ """Internal wrapper preserving per-element windowing metadata across
batching."""
+ def __init__(
+ self,
+ request: Any,
+ timestamp: Any,
+ windows: tuple[Any, ...],
+ pane_info: Any = windowed_value.PANE_INFO_UNKNOWN):
+ self.request = request
+ self.timestamp = timestamp
+ self.windows = windows
+ self.pane_info = pane_info
+
+
+class _AttachWindowMetadataDoFn(beam.DoFn):
+ """Captures per-element timestamp, window, and pane_info before batching."""
+ def process(
+ self,
+ element,
+ timestamp=beam.DoFn.TimestampParam,
+ window=beam.DoFn.WindowParam,
+ pane_info=beam.DoFn.PaneInfoParam):
+ yield _WindowedRequest(element, timestamp, (window, ), pane_info)
+
+
+def _adapt_batching_kwargs(
+ batching_kwargs: Mapping[str, Any]) -> dict[str, Any]:
+ adapted = dict(batching_kwargs)
+ for fn_key in ('element_size_fn', 'length_fn', 'key_fn'):
+ if fn_key in adapted and adapted[fn_key] is not None:
+ orig_fn = adapted[fn_key]
+ adapted[fn_key] = (
+ lambda el, _fn=orig_fn: _fn(
+ el.request if isinstance(el, _WindowedRequest) else el))
+ return adapted
+
+
+def _reattach_window_metadata(
+ windowed_requests: list[_WindowedRequest],
+ raw_responses: Any) -> list[_WindowedRequest]:
+ if not isinstance(raw_responses, collections.abc.Iterable) or isinstance(
+ raw_responses, (bytes, str, beam.Row)):
+ w0 = windowed_requests[0]
+ return [
+ _WindowedRequest(raw_responses, w0.timestamp, w0.windows, w0.pane_info)
+ ]
+
+ resp_list = list(raw_responses)
+ by_id: dict[int, collections.deque[_WindowedRequest]] = (
+ collections.defaultdict(collections.deque))
+ for w in windowed_requests:
+ by_id[id(w.request)].append(w)
+ remaining: list[_WindowedRequest] = list(windowed_requests)
+
+ wrapped: list[_WindowedRequest] = []
Review 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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]