shikhabhadoria commented on code in PR #41451:
URL: https://github.com/apache/beam/pull/41451#discussion_r4705103586
##########
sdks/python/apache_beam/io/requestresponse.py:
##########
@@ -328,12 +328,21 @@ def __init__(
self._should_backoff = should_backoff
self._repeater = repeater
self._throttler = throttler
+ self._exception_handling_args: Optional[dict[str, Any]] = None
+
+ def with_exception_handling(self, **kwargs):
Review Comment:
Updated. `with_exception_handling` on `_Call`, `RequestResponseIO`, and
`Enrichment` now takes `*args, **kwargs` and forwards them to
`DoFn.with_exception_handling`, with the docstring pointing at that method for
the accepted parameters.
##########
sdks/python/apache_beam/io/requestresponse.py:
##########
@@ -818,24 +832,38 @@ def expand(
if isinstance(self._throttler, DefaultThrottler):
# DefaultThrottler applies throttling in the DoFn of
# Call PTransform.
- responses = (
- inputs
- | _Call(
- caller=self._caller,
- timeout=self._timeout,
- should_backoff=self._should_backoff,
- repeater=self._repeater,
- throttler=self._throttler))
+ call_transform = _Call(
+ caller=self._caller,
+ timeout=self._timeout,
+ should_backoff=self._should_backoff,
+ repeater=self._repeater,
+ throttler=self._throttler)
else:
# No throttling mechanism. The requests are made to the external source
# as they come.
- responses = (
- inputs
- | _Call(
- caller=self._caller,
- timeout=self._timeout,
- should_backoff=self._should_backoff,
- repeater=self._repeater))
+ call_transform = _Call(
+ caller=self._caller,
+ timeout=self._timeout,
+ should_backoff=self._should_backoff,
+ repeater=self._repeater)
+
+ errors = None
+ if self._exception_handling_args is not None:
+ main_tag = self._exception_handling_args.get('main_tag', 'good')
+ dead_letter_tag = self._exception_handling_args.get(
+ 'dead_letter_tag', 'bad')
+ call_result = inputs | call_transform.with_exception_handling(
+ **self._exception_handling_args)
+ responses = call_result[main_tag]
+ errors = call_result[dead_letter_tag]
+ if self._batching_kwargs:
+ errors = (
+ errors
+ | "FlattenBatchErrors" >> beam.FlatMap(
+ lambda x: [(r, x[1])
+ for r in x[0]] if isinstance(x[0], list) else [x]))
Review Comment:
Needed for the batched path. When `BatchElements` is enabled, `_CallDoFn`
processes a list, so `DoFn.with_exception_handling` emits failed records as
`(batch_list, error)`. `FlattenBatchErrors` expands that to per-element
`(request, error)` so the DLQ shape matches the non-batched case. Added an
inline comment explaining this.
##########
sdks/python/apache_beam/transforms/enrichment.py:
##########
@@ -55,32 +64,145 @@ def has_valid_redis_address(host: str, port: int) -> bool:
return False
-def cross_join(left: dict[str, Any], right: dict[str, Any]) -> beam.Row:
+def cross_join(
+ left: dict[str, Any],
+ right: dict[str, Any],
+ prefix: Optional[str] = None) -> beam.Row:
"""performs a cross join on two `dict` objects.
Joins the columns of the right row onto the left row.
Args:
left (dict[str, Any]): input request dictionary.
right (dict[str, Any]): response dictionary from the API.
+ prefix (Optional[str]): optional prefix to prepend to keys from `right`
+ to avoid column name collisions.
Returns:
`beam.Row` containing the merged columns.
"""
for k, v in right.items():
- if k not in left:
+ target_key = f"{prefix}{k}" if prefix else k
+ if target_key not in left:
# Don't override the values in left.
- left[k] = v
- elif left[k] != v:
+ left[target_key] = v
+ elif left[target_key] != v:
_LOGGER.warning(
'%s exists in the input row as well the row fetched '
'from API but have different values - %s and %s. Using the input '
'value (%s) for the enriched row. You can override this behavior by '
'passing a custom `join_fn` to Enrichment transform.' %
- (k, left[k], v, left[k]))
+ (target_key, left[target_key], v, left[target_key]))
return beam.Row(**left)
+class _EnrichmentJoinFn(beam.DoFn):
+ """DoFn that joins `(request, response)` tuples with `on_miss` and
`prefix`."""
+ def __init__(
+ self,
+ join_fn: JoinFn,
+ use_custom_types: bool = False,
+ on_miss: str = 'null',
Review Comment:
Agreed. This PR is now scoped to `with_exception_handling` only. I removed
`on_miss` / `prefix` from here and preserved that work on
`feat-enrichment-on-miss-prefix-followup-41356` for a follow-up PR.
--
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]