AmoghAtreya commented on code in PR #40765:
URL: https://github.com/apache/beam/pull/40765#discussion_r4674948094
##########
sdks/python/apache_beam/ml/inference/utils_test.py:
##########
@@ -26,82 +26,130 @@
from apache_beam.ml.inference.base import ModelHandler
from apache_beam.ml.inference.base import PredictionResult
from apache_beam.ml.inference.base import RunInference
+from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.testing.test_pipeline import TestPipeline
+from apache_beam.testing.test_stream import TestStream
from apache_beam.testing.util import assert_that
from apache_beam.testing.util import equal_to
+def _file(path, seconds_from_start):
+ return FileMetadata(
+ path,
+ 10,
+ last_updated_in_seconds=utils._START_TIME_STAMP + seconds_from_start)
+
+
+def _model_ids(pcoll, label='ModelIds'):
+ return pcoll | label >> beam.Map(lambda model: model.model_id)
+
+
class WatchFilePatternTest(unittest.TestCase):
- def test_latest_file_by_timestamp_default_value(self):
- # match continuously returns the files in sorted timestamp order.
- main_input_pcoll = [
- FileMetadata(
- 'path1.py',
- 10,
- last_updated_in_seconds=utils._START_TIME_STAMP - 20),
- FileMetadata(
- 'path2.py',
- 10,
- last_updated_in_seconds=utils._START_TIME_STAMP - 10)
- ]
+ def test_latest_file_combine_fn_picks_newest_regardless_of_order(self):
+ older = _file('older.py', 10)
+ newer = _file('newer.py', 50)
+ initial = _file('initial.py', -5)
+ combine = utils._LatestFileCombineFn()
+
+ def accumulate(files):
+ accumulator = combine.create_accumulator()
+ for metadata in files:
+ accumulator = combine.add_input(accumulator, metadata)
+ return accumulator
+
+ merged = combine.merge_accumulators(
+ [accumulate([older]), accumulate([newer, initial])])
+ self.assertEqual('newer.py', combine.extract_output(merged).path)
+ swapped = combine.merge_accumulators(
+ [accumulate([newer, initial]), accumulate([older])])
+ self.assertEqual('newer.py', combine.extract_output(swapped).path)
+
+ def test_latest_file_combine_fn_breaks_timestamp_ties_by_path(self):
+ combine = utils._LatestFileCombineFn()
+ first = combine.add_input(combine.create_accumulator(), _file('b.py', 10))
+ second = combine.add_input(combine.create_accumulator(), _file('a.py', 10))
+ merged = combine.merge_accumulators([first, second])
+ self.assertEqual('b.py', combine.extract_output(merged).path)
+
+ def test_files_at_or_before_pipeline_start_emit_configured_model_once(self):
+ # Several pre-start files are one poll. They publish the configured model
+ # once (an empty model id), not one value per file.
+ files = [_file('path1.py', -20), _file('path2.py', -10), _file('now.py',
0)]
+ with TestPipeline() as p:
+ emitted = _model_ids(p | beam.Create(files) | utils._SelectLatestModel())
+ assert_that(emitted, equal_to(['']))
+
+ def test_configured_model_then_a_newer_file(self):
+ files = [_file('initial.py', -5), _file('newer.py', 50)]
with TestPipeline() as p:
- files_pc = (
+ emitted = _model_ids(
p
- | beam.Create(main_input_pcoll)
- | beam.Map(lambda x: (x.path, x))
- | beam.ParDo(utils._GetLatestFileByTimeStamp())
- | beam.Map(lambda x: x[0]))
- assert_that(files_pc, equal_to(['', '']))
-
- def test_latest_file_with_timestamp_after_pipeline_construction_time(self):
- main_input_pcoll = [
- FileMetadata(
- 'path1.py',
- 10,
- last_updated_in_seconds=utils._START_TIME_STAMP + 10)
+ | beam.Create(files)
+ | beam.Map(lambda metadata: (utils._GLOBAL_MODEL_KEY, metadata))
+ | beam.ParDo(utils._EmitLatestModel()),
+ label='ConfiguredThenUpdated')
+ assert_that(emitted, equal_to(['', 'newer.py']))
+
+ def test_file_newer_than_pipeline_start_is_emitted(self):
+ with TestPipeline() as p:
+ emitted = _model_ids(
+ p | beam.Create([_file('path1.py', 10)]) |
utils._SelectLatestModel())
+ assert_that(emitted, equal_to(['path1.py']))
+
+ def test_one_poll_emits_only_the_newest_model(self):
+ # Newest file is not last, and another post-start file is present, so
+ # neither input order nor "every new path" can select the right model.
+ files = [
+ _file('path4.py', 20),
+ _file('path1.py', -20),
+ _file('path3.py', 10),
+ _file('path2.py', -10),
]
with TestPipeline() as p:
- files_pc = (
+ updates = p | 'Files' >> beam.Create(files) | utils._SelectLatestModel()
+ assert_that(_model_ids(updates), equal_to(['path4.py']), label='emitted')
+ singleton = (
p
- | beam.Create(main_input_pcoll)
- | beam.Map(lambda x: (x.path, x))
- | beam.ParDo(utils._GetLatestFileByTimeStamp())
- | beam.Map(lambda x: x[0]))
- assert_that(files_pc, equal_to(['path1.py']))
-
- def test_emitting_singleton_output(self):
- # match continuously returns the files in sorted timestamp order.
- main_input_pcoll = [
- FileMetadata(
- 'path1.py',
- 10,
- last_updated_in_seconds=utils._START_TIME_STAMP - 20),
- # returns default
- FileMetadata(
- 'path2.py',
- 10,
- last_updated_in_seconds=utils._START_TIME_STAMP - 10),
- # returns default
- FileMetadata(
- 'path3.py',
- 10,
- last_updated_in_seconds=utils._START_TIME_STAMP + 10),
- FileMetadata(
- 'path4.py',
- 10,
- last_updated_in_seconds=utils._START_TIME_STAMP + 20)
- ]
- # returns path3.py
+ | 'Probe' >> beam.Create([None])
+ | beam.Map(
+ lambda _, model: model.model_id,
+ beam.pvalue.AsSingleton(updates)))
+ assert_that(singleton, equal_to(['path4.py']), label='singleton')
+
+ def test_older_update_does_not_replace_a_newer_model(self):
+ # Shared key, processed in input order. The second file is still newer
+ # than pipeline start, but older than the model already accepted.
+ files = [_file('newer.py', 50), _file('older.py', 10)]
+ with TestPipeline() as p:
+ emitted = _model_ids(
+ p
+ | beam.Create(files)
+ | beam.Map(lambda metadata: (utils._GLOBAL_MODEL_KEY, metadata))
+ | beam.ParDo(utils._EmitLatestModel()))
+ assert_that(emitted, equal_to(['newer.py']))
+ def test_newer_update_replaces_an_older_model(self):
+ files = [_file('older.py', 10), _file('newer.py', 50)]
with TestPipeline() as p:
- files_pc = (
+ emitted = _model_ids(
p
- | beam.Create(main_input_pcoll)
- | beam.Map(lambda x: (x.path, x))
- | beam.ParDo(utils._GetLatestFileByTimeStamp())
- | beam.ParDo(utils._ConvertIterToSingleton())
- | beam.Map(lambda x: x[0]))
- assert_that(files_pc, equal_to(['', 'path3.py', 'path4.py']))
+ | beam.Create(files)
+ | beam.Map(lambda metadata: (utils._GLOBAL_MODEL_KEY, metadata))
+ | beam.ParDo(utils._EmitLatestModel()),
+ label='UpdatedModelIds')
+ assert_that(emitted, equal_to(['older.py', 'newer.py']))
+
+ def test_later_poll_does_not_emit_an_older_model(self):
+ options = PipelineOptions(flags=['--streaming'])
+ with TestPipeline(options=options) as p:
+ stream = (
+ TestStream().advance_watermark_to(0).add_elements(
+ [_file('newer.py',
50)]).advance_processing_time(5).add_elements([
+ _file('older.py', 10)
+ ]).advance_processing_time(5).advance_watermark_to_infinity())
+ emitted = _model_ids(
+ p | stream | utils._SelectLatestModel(), label='PolledModelIds')
+ assert_that(emitted, equal_to(['newer.py']))
Review Comment:
Expanded that streaming test into one TestStream run through
_SelectLatestModel. Processing-time advances separate the polls: pre-start
files emit '', the next poll emits v1.py, a later file older than v1.py is
dropped, and a poll that sees both also_new.py and v2.py emits only v2.py. The
assertion is equal_to(['', 'v1.py', 'v2.py']). One pane at the final watermark
would emit only v2.py, and a lost timestamp would also emit stale.py.
--
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]