damccorm commented on code in PR #41451:
URL: https://github.com/apache/beam/pull/41451#discussion_r4744098381
##########
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:
Nit: `with_exception_handling` lives on `ParDo`, not `DoFn` — please update
these Sphinx refs to
`:meth:`apache_beam.transforms.core.ParDo.with_exception_handling`` here as
well as at lines 813 and 875, `enrichment.py:157`, and `CHANGES.md:97` (sorry
for the misleading shorthand in my earlier comment!).
##########
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:
Since the docstrings point users to `ParDo.with_exception_handling`, where
the canonical pattern is `good, bad = pcoll |
Transform().with_exception_handling()`, returning a plain `dict` is a sharp
footgun: unpacking a `dict` silently binds `good = 'good'` and `bad = 'bad'`
(the string keys!), `res.good` raises `AttributeError`, and
`PTransform.type_check_outputs` doesn't skip `dict` like it skips
`DoOutputsTuple` (so `.with_output_types(MyType)` will fail against
`DeadLetter`). Also, `RequestResponseIO.with_exception_handling`'s docstring
doesn't currently mention returning a `dict`.
Could we return a `DoOutputsTuple` / tagged-output container that supports
`good, bad = ...`, `res[tag]`, and `res.good` — or if you intentionally want
`dict` (like `ToRows`), explicitly document that on `RequestResponseIO` too
with a usage snippet?
##########
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:
Passing `error_handler=...` will currently crash here with `TypeError:
'PCollection' object is not subscriptable`, because `_ExceptionHandlingWrapper`
returns the main `PCollection` (not a `DoOutputsTuple`) when `error_handler` is
set (`core.py:2446-2450`). On the batched path, forwarding `error_handler`
directly to `_Call` would also register the unflattened `(batch_list, error)`
collection before `FlattenBatchErrors` runs.
Could we pop `error_handler` from a shallow copy of `eh_kwargs` before
forwarding to the inner `with_exception_handling`, then call
`error_handler.add_error_pcollection(errors)` on the final/flattened errors and
return `responses` directly? (Same for `Enrichment.expand`.)
##########
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:
Minor: `_join_element` closes over `self`, which now holds
`self._exception_handling_args` (including things like `ErrorHandler` once
supported, which references `PCollection`/`Pipeline` and isn't picklable).
Binding `join_fn = self._join_fn` and `use_custom_types =
self._use_custom_types` outside `_join_element` avoids pickling `self` 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:
Note that forwarding `threshold` independently to both `rrio` and
`enrichment_join` means e.g. `threshold=0.5` won't trip if 49/100 fail in RRIO
(`49%`) and 25/51 fail in join (`49%`), even though `74%` of total inputs
failed. Similarly, in `RequestResponseIO`, `threshold` is evaluated per-batch
when batching is enabled and over cache misses only when caching is enabled.
Worth either computing `threshold` once at the outer level or documenting those
caveats clearly.
##########
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:
`rrio_errors` emits `(request, error_info)`, while
`join_result[dead_letter_tag]` emits `((request, response), error_info)`
because `fetched_data` holds `(request, response)` tuples. Flattening both into
`all_errors` mixes two shapes in one `PCollection` (which is why
`enrichment_test.py` had to branch on `isinstance(x[0], tuple)`, and that
breaks if `use_custom_types=True` with tuple requests or with typed downstream
consumers like Beam YAML).
Let's normalize `join_result[dead_letter_tag]` via `beam.Map(lambda x:
(x[0][0], x[1]))` before flattening so the DLQ consistently matches
`(original_input, error_info)` (matching what `FlattenBatchErrors` does for
batched RPC errors).
##########
sdks/python/apache_beam/transforms/enrichment_test.py:
##########
@@ -19,14 +19,36 @@
import unittest
import apache_beam as beam
+from apache_beam.io.requestresponse import NoOpsRepeater
+from apache_beam.io.requestresponse import UserCodeTimeoutException
Review Comment:
Please move `NoOpsRepeater` and `UserCodeTimeoutException` inside the `try
... except ImportError` block below — `requestresponse.py` imports
`google.api_core` at top level (optional `[gcp]` extra), so importing it above
the guard breaks the `SkipTest` in environments without `google-api-core`.
Also, could we add unit tests covering
`RequestResponseIO.with_exception_handling` directly, the batched
`FlattenBatchErrors` path, default tags (`()`), `cache` +
`with_exception_handling`, `exc_class`, and `error_handler`?
--
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]