Skip to content

Commit 9adc8dc

Browse files
authored
Support Protobuf 6.x. (#35477)
* Support Protobuf 6.x. * Fix these flaky tests due to Protobuf not guaranteeing map iteration order. * Run YAPF.
1 parent 230f3ea commit 9adc8dc

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

sdks/python/apache_beam/runners/worker/data_sampler_test.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,10 @@ def test_samples_all_with_both_experiments(self):
383383
MAIN_TRANSFORM_ID, descriptor, self.primitives_coder_factory)
384384

385385
# Get the samples for the two outputs.
386-
a_sampler = self.data_sampler.sampler_for_output(MAIN_TRANSFORM_ID, 0)
387-
b_sampler = self.data_sampler.sampler_for_output(MAIN_TRANSFORM_ID, 1)
386+
# N.B. the order of the samplers is not guaranteed due to Protobuf not
387+
# guaranteeing map iteration order.
388+
first_sampler = self.data_sampler.sampler_for_output(MAIN_TRANSFORM_ID, 0)
389+
second_sampler = self.data_sampler.sampler_for_output(MAIN_TRANSFORM_ID, 1)
388390

389391
# Sample an exception for the output 'a', this will show up in the final
390392
# samples response.
@@ -393,19 +395,25 @@ def test_samples_all_with_both_experiments(self):
393395
raise Exception('test')
394396
except Exception:
395397
exc_info = sys.exc_info()
396-
a_sampler.sample_exception('a', exc_info, MAIN_TRANSFORM_ID, 'instid')
398+
first_sampler.sample_exception(
399+
'first', exc_info, MAIN_TRANSFORM_ID, 'instid')
397400

398401
# Sample a normal element for the output 'b', this will not show up in the
399402
# final samples response.
400-
b_sampler.element_sampler.el = 'b'
401-
b_sampler.element_sampler.has_element = True
403+
second_sampler.element_sampler.el = 'second'
404+
second_sampler.element_sampler.has_element = True
402405

403406
samples = self.data_sampler.wait_for_samples(['a', 'b'])
404407
self.assertEqual(len(samples.element_samples), 2)
405-
self.assertTrue(
406-
samples.element_samples['a'].elements[0].HasField('exception'))
407-
self.assertFalse(
408-
samples.element_samples['b'].elements[0].HasField('exception'))
408+
sample_elements = list(
409+
s.elements[0] for s in samples.element_samples.values())
410+
num_exceptions = sum(
411+
1 for element in sample_elements if element.HasField('exception'))
412+
self.assertEqual(
413+
num_exceptions,
414+
1,
415+
"Only one of the samples should have an exception, found: {}".format(
416+
sample_elements))
409417

410418
def test_only_sample_exceptions(self):
411419
"""Tests that the exception sampling experiment only samples exceptions."""
@@ -420,8 +428,10 @@ def test_only_sample_exceptions(self):
420428
MAIN_TRANSFORM_ID, descriptor, self.primitives_coder_factory)
421429

422430
# Get the samples for the two outputs.
423-
a_sampler = self.data_sampler.sampler_for_output(MAIN_TRANSFORM_ID, 0)
424-
b_sampler = self.data_sampler.sampler_for_output(MAIN_TRANSFORM_ID, 1)
431+
# N.B. the order of the samplers is not guaranteed due to Protobuf not
432+
# guaranteeing map iteration order.
433+
first_sampler = self.data_sampler.sampler_for_output(MAIN_TRANSFORM_ID, 0)
434+
second_sampler = self.data_sampler.sampler_for_output(MAIN_TRANSFORM_ID, 1)
425435

426436
# Sample an exception for the output 'a', this will show up in the final
427437
# samples response.
@@ -430,16 +440,18 @@ def test_only_sample_exceptions(self):
430440
raise Exception('test')
431441
except Exception:
432442
exc_info = sys.exc_info()
433-
a_sampler.sample_exception('a', exc_info, MAIN_TRANSFORM_ID, 'instid')
443+
first_sampler.sample_exception(
444+
'first', exc_info, MAIN_TRANSFORM_ID, 'instid')
434445

435446
# Sample a normal element for the output 'b', this will not show up in the
436447
# final samples response.
437-
b_sampler.element_sampler.el = 'b'
438-
b_sampler.element_sampler.has_element = True
448+
second_sampler.element_sampler.el = 'second'
449+
second_sampler.element_sampler.has_element = True
439450

440451
samples = self.data_sampler.wait_for_samples([])
441452
self.assertEqual(len(samples.element_samples), 1)
442-
self.assertIsNotNone(samples.element_samples['a'].elements[0].exception)
453+
value = list(samples.element_samples.values())[0]
454+
self.assertIsNotNone(value.elements[0].exception)
443455

444456

445457
class OutputSamplerTest(unittest.TestCase):

sdks/python/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def get_portability_package_data():
392392
#
393393
# 3. Exclude protobuf 4 versions that leak memory, see:
394394
# https://github.com/apache/beam/issues/28246
395-
'protobuf>=3.20.3,<6.0.0.dev0,!=4.0.*,!=4.21.*,!=4.22.0,!=4.23.*,!=4.24.*', # pylint: disable=line-too-long
395+
'protobuf>=3.20.3,<7.0.0.dev0,!=4.0.*,!=4.21.*,!=4.22.0,!=4.23.*,!=4.24.*', # pylint: disable=line-too-long
396396
'pydot>=1.2.0,<2',
397397
'python-dateutil>=2.8.0,<3',
398398
'pytz>=2018.3',

0 commit comments

Comments
 (0)