shikhabhadoria commented on code in PR #41451:
URL: https://github.com/apache/beam/pull/41451#discussion_r4757729170
##########
sdks/python/apache_beam/io/requestresponse.py:
##########
@@ -328,12 +328,26 @@ def __init__(
self._should_backoff = should_backoff
self._repeater = repeater
self._throttler = throttler
+ self._exception_handling_args: Optional[tuple] = None
+
+ def with_exception_handling(self, *args, **kwargs):
+ """Enable dead-letter outputs for failed calls.
+
+ Arguments are forwarded to
+ :meth:`apache_beam.transforms.core.DoFn.with_exception_handling`.
Review Comment:
Updated all references in `requestresponse.py`, `enrichment.py`, and
`CHANGES.md` to
`:meth:`apache_beam.transforms.core.ParDo.with_exception_handling``.
##########
sdks/python/apache_beam/io/requestresponse.py:
##########
@@ -818,24 +844,43 @@ 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:
+ eh_args, eh_kwargs = self._exception_handling_args
+ main_tag = eh_kwargs.get(
+ 'main_tag', eh_args[0] if len(eh_args) > 0 else 'good')
+ dead_letter_tag = eh_kwargs.get(
+ 'dead_letter_tag', eh_args[1] if len(eh_args) > 1 else 'bad')
+ call_result = inputs | call_transform.with_exception_handling(
+ *eh_args, **eh_kwargs)
+ responses = call_result[main_tag]
Review Comment:
Done — both `RequestResponseIO.expand` and `Enrichment.expand` now pop
`error_handler` from a shallow copy of `eh_kwargs`, call
`error_handler.add_error_pcollection(...)` on the final/flattened error
PCollection, and return the main output PCollection directly. Covered by unit
tests on unbatched RRIO, batched RRIO, and Enrichment.
##########
sdks/python/apache_beam/io/requestresponse.py:
##########
@@ -844,6 +889,9 @@ def expand(
if self._cache:
# write to cache.
_ = responses | self._cache.get_write()
- return (cached_responses, responses) | beam.Flatten()
+ responses = (cached_responses, responses) | beam.Flatten()
+
+ if errors is not None:
+ return {main_tag: responses, dead_letter_tag: errors} # type:
ignore[return-value]
Review Comment:
Updated both `RequestResponseIO.expand` and `Enrichment.expand` to return a
`pvalue.DoOutputsTuple` via `_build_do_outputs_tuple`, supporting `good, bad =
...`, `res[tag]`, `res.good`, and `.with_output_types(...)`. Updated docstrings
on both transforms accordingly.
##########
sdks/python/apache_beam/transforms/enrichment.py:
##########
@@ -158,21 +174,49 @@ def expand(self,
if self._cache:
self._cache.request_coder = request_coder
- fetched_data = (
- input_row
- | "Enrichment-RRIO" >> RequestResponseIO(
- caller=self._source_handler,
- timeout=self._timeout,
- repeater=self._repeater,
- cache=self._cache,
- throttler=self._throttler))
+ rrio = RequestResponseIO(
+ caller=self._source_handler,
+ timeout=self._timeout,
+ repeater=self._repeater,
+ cache=self._cache,
+ throttler=self._throttler)
+
+ def _join_element(element):
+ request, response = element
+ if self._use_custom_types:
Review Comment:
Bound `join_fn = self._join_fn` and `use_custom_types =
self._use_custom_types` outside `_join_element` so `self` (and
`self._exception_handling_args`) is not captured or pickled into the join DoFn.
##########
sdks/python/apache_beam/transforms/enrichment.py:
##########
@@ -158,21 +174,49 @@ def expand(self,
if self._cache:
self._cache.request_coder = request_coder
- fetched_data = (
- input_row
- | "Enrichment-RRIO" >> RequestResponseIO(
- caller=self._source_handler,
- timeout=self._timeout,
- repeater=self._repeater,
- cache=self._cache,
- throttler=self._throttler))
+ rrio = RequestResponseIO(
+ caller=self._source_handler,
+ timeout=self._timeout,
+ repeater=self._repeater,
+ cache=self._cache,
+ throttler=self._throttler)
+
+ def _join_element(element):
+ request, response = element
+ if self._use_custom_types:
+ return self._join_fn(request, response)
+ return self._join_fn(request._asdict(), response._asdict())
+
+ if self._exception_handling_args is not None:
+ eh_args, eh_kwargs = self._exception_handling_args
+ main_tag = eh_kwargs.get(
+ 'main_tag', eh_args[0] if len(eh_args) > 0 else 'good')
+ dead_letter_tag = eh_kwargs.get(
+ 'dead_letter_tag', eh_args[1] if len(eh_args) > 1 else 'bad')
+ rrio_result = (
+ input_row
+ | "Enrichment-RRIO" >> rrio.with_exception_handling(
Review Comment:
Popped `threshold` and `threshold_windowing` in both
`RequestResponseIO.expand` and `Enrichment.expand` and evaluated threshold once
at the outer level via `_check_exception_threshold` over all inputs and final
flattened/normalized errors. Added unit tests covering combined RRIO + join
failure ratios and cached inputs.
##########
sdks/python/apache_beam/transforms/enrichment.py:
##########
@@ -158,21 +174,49 @@ def expand(self,
if self._cache:
self._cache.request_coder = request_coder
- fetched_data = (
- input_row
- | "Enrichment-RRIO" >> RequestResponseIO(
- caller=self._source_handler,
- timeout=self._timeout,
- repeater=self._repeater,
- cache=self._cache,
- throttler=self._throttler))
+ rrio = RequestResponseIO(
+ caller=self._source_handler,
+ timeout=self._timeout,
+ repeater=self._repeater,
+ cache=self._cache,
+ throttler=self._throttler)
+
+ def _join_element(element):
+ request, response = element
+ if self._use_custom_types:
+ return self._join_fn(request, response)
+ return self._join_fn(request._asdict(), response._asdict())
+
+ if self._exception_handling_args is not None:
+ eh_args, eh_kwargs = self._exception_handling_args
+ main_tag = eh_kwargs.get(
+ 'main_tag', eh_args[0] if len(eh_args) > 0 else 'good')
+ dead_letter_tag = eh_kwargs.get(
+ 'dead_letter_tag', eh_args[1] if len(eh_args) > 1 else 'bad')
+ rrio_result = (
+ input_row
+ | "Enrichment-RRIO" >> rrio.with_exception_handling(
+ *eh_args, **eh_kwargs))
+ fetched_data = rrio_result[main_tag]
+ rrio_errors = rrio_result[dead_letter_tag]
+
+ # EnrichmentSourceHandler returns a tuple of (request, response).
+ join_result = (
+ fetched_data
+ |
+ "enrichment_join" >> beam.Map(_join_element).with_exception_handling(
+ *eh_args, **eh_kwargs))
+ all_errors = ((rrio_errors, join_result[dead_letter_tag])
Review Comment:
Normalized `join_result[dead_letter_tag]` via `beam.Map(lambda x: (x[0][0],
x[1]))` before flattening so all DLQ entries consistently have
`(original_input, error_info)` shape. Removed the `isinstance` branching in
tests and added `test_with_exception_handling_custom_types_tuple_requests`.
--
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]