diff --git a/CHANGES.md b/CHANGES.md index 84021b015141..92d404bcafd0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -131,6 +131,7 @@ ## Breaking Changes [comment]: # "generic breaking changes if not covered in following sections." +* (Python) `AfterAny` and `AfterAll` now finish after their first firing. A `GroupByKey` using one of them with a non-finishing subtrigger, outside `Repeatedly`, is now rejected as a trigger that may lose data; wrap it in `Repeatedly` or set `--allow_unsafe_triggers` ([#21262](https://github.com/apache/beam/issues/21262)). * (Java) Flink runner: batch pipelines now assign source splits to readers statically by default instead of lazily. Set `--sourceStaticSplitThresholdMb=0` to restore lazy assignment ([#40607](https://github.com/apache/beam/issues/40607)). * (Python) Datatable support in the XGBoost model handler has been removed. ([#41586](https://github.com/apache/beam/issues/41586)). * X behavior was changed ([#X](https://github.com/apache/beam/issues/X)). @@ -162,6 +163,7 @@ * (Python) Fixed local filesystem copies deleting source data when copying onto the same file or directory, and deleting an existing destination when the source is missing ([#41561](https://github.com/apache/beam/issues/41561)). * (Python) Fixed reading concatenated bzip2, zstd and lzma files when a compressed member ends exactly at an internal read boundary ([#41669](https://github.com/apache/beam/issues/41669)). * (Python) Fixed compressed files returning incomplete data from `read()` and `fileio.ReadableFile.read()`; omitted, `None`, and negative read sizes now read all remaining data ([#41621](https://github.com/apache/beam/issues/41621)). +* (Python) Fixed `AfterAny` and `AfterAll` failing to finish after their first firing when composed with non-finishing triggers ([#21262](https://github.com/apache/beam/issues/21262)). * (Java) Fixed Dataflow's streaming `WriteFiles` sharding override dropping options configured on the transform (such as `maxNumWritersPerBundle`, `evictWritersWhenFull`, `skipIfEmpty`, `shardingFunction`, and `badRecordErrorHandler`) when a fixed shard count is not set ([#40533](https://github.com/apache/beam/issues/40533)). * (Java) Fixed Dataflow Streaming Engine `MapState` and `MultimapState` being weighed as 1 byte in the worker state cache, which let the cache grow far past `--workerCacheMb` and run workers out of memory ([#41597](https://github.com/apache/beam/issues/41597)). * (Python) Fixed Python and Beam YAML pipelines submitted to Spark with `--spark_submit_uber_jar` failing on the driver unless `--storage_level` was set. The Spark runner now defaults to `MEMORY_ONLY` when no Java runner is available to choose the storage level from ([#41080](https://github.com/apache/beam/issues/41080)). diff --git a/sdks/python/apache_beam/transforms/trigger.py b/sdks/python/apache_beam/transforms/trigger.py index c1ac0275aee2..26cb9c367305 100644 --- a/sdks/python/apache_beam/transforms/trigger.py +++ b/sdks/python/apache_beam/transforms/trigger.py @@ -784,6 +784,8 @@ def get_continuation_trigger(self): class _ParallelTriggerFn(TriggerFn, metaclass=ABCMeta): + _finishes_on_fire = True + def __init__(self, *triggers): self.triggers = triggers @@ -826,9 +828,11 @@ def on_fire(self, watermark, window, context): window, nested_context): finished.append(trigger.on_fire(watermark, window, nested_context)) - return self.combine_op(finished) + return self._finishes_on_fire or self.combine_op(finished) def may_lose_data(self, windowing): + if self._finishes_on_fire: + return DataLossReason.MAY_FINISH may_finish = self.combine_op( _IncludesMayFinish(t.may_lose_data(windowing)) for t in self.triggers) return ( @@ -882,7 +886,7 @@ def get_continuation_trigger(self): class AfterAny(_ParallelTriggerFn): """Fires when any subtrigger fires. - Also finishes when any subtrigger finishes. + Finishes after its first firing. """ combine_op = any @@ -890,7 +894,7 @@ class AfterAny(_ParallelTriggerFn): class AfterAll(_ParallelTriggerFn): """Fires when all subtriggers have fired. - Also finishes when all subtriggers have finished. + Finishes after its first firing. """ combine_op = all @@ -989,6 +993,9 @@ def get_continuation_trigger(self): class OrFinally(AfterAny): + # OrFinally can fire repeatedly while its main trigger remains active. + _finishes_on_fire = False + @staticmethod def from_runner_api(proto, context): return OrFinally( @@ -1562,7 +1569,8 @@ def _output(self, window, finished, state, output_watermark, maybe_ontime): if timestamp is None: # If no watermark hold was set, output at end of window. timestamp = window.max_timestamp() - elif output_watermark < window.end and self.trigger_fn.has_ontime_pane(): + elif (not finished and output_watermark < window.end and + self.trigger_fn.has_ontime_pane()): # Hold the watermark in case there is an empty pane that needs to be fired # at the end of the window. pass diff --git a/sdks/python/apache_beam/transforms/trigger_test.py b/sdks/python/apache_beam/transforms/trigger_test.py index a5ed77be7c6d..70f7b0e096e9 100644 --- a/sdks/python/apache_beam/transforms/trigger_test.py +++ b/sdks/python/apache_beam/transforms/trigger_test.py @@ -417,6 +417,23 @@ def test_sessions_repeatedly_after_count(self): 1, 3) + def test_after_each_advances_after_parallel_trigger_fires(self): + for parallel in (AfterAny, AfterAll): + with self.subTest(parallel=parallel.__name__): + self.run_trigger_simple( + FixedWindows(10), + AfterEach( + parallel(trigger.Always(), trigger.Always()), AfterCount(2)), + AccumulationMode.DISCARDING, [(1, 'a'), (2, 'b'), (3, 'c')], + {IntervalWindow(0, 10): [set('a'), set('bc')]}) + + def test_or_finally_can_fire_before_finishing(self): + self.run_trigger_simple( + FixedWindows(10), + trigger.OrFinally(trigger.Always(), AfterCount(2)), + AccumulationMode.DISCARDING, [(1, 'a'), (2, 'b'), (3, 'c')], + {IntervalWindow(0, 10): [set('a'), set('b')]}) + def test_sessions_after_each(self): self.run_trigger_simple( Sessions(10), # pyformat break @@ -507,17 +524,15 @@ def test_after_any_one_may_finish(self): 0, DataLossReason.MAY_FINISH) - def test_after_any_all_safe(self): + def test_after_any_finishes_with_non_finishing_children(self): self._test( AfterAny(Repeatedly(AfterCount(42)), DefaultTrigger()), 0, - DataLossReason.NO_POTENTIAL_LOSS) + DataLossReason.MAY_FINISH) - def test_after_all_some_may_finish(self): + def test_after_all_finishes_with_non_finishing_child(self): self._test( - AfterAll(AfterCount(1), DefaultTrigger()), - 0, - DataLossReason.NO_POTENTIAL_LOSS) + AfterAll(AfterCount(1), DefaultTrigger()), 0, DataLossReason.MAY_FINISH) def test_afer_all_all_may_finish(self): self._test(