Skip to content

Commit 63d9d9f

Browse files
author
Nikita Grover
committed
Rename to publish_with_ordering_key, gate Dataflow warning, fix test assertions
1 parent b44f8e0 commit 63d9d9f

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

sdks/python/apache_beam/io/gcp/pubsub.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def __init__(
390390
with_attributes: bool = False,
391391
id_label: Optional[str] = None,
392392
timestamp_attribute: Optional[str] = None,
393-
enable_message_ordering: bool = False) -> None:
393+
publish_with_ordering_key: bool = False) -> None:
394394
"""Initializes ``WriteToPubSub``.
395395
396396
Args:
@@ -406,13 +406,13 @@ def __init__(
406406
in a ReadFromPubSub PTransform to deduplicate messages.
407407
timestamp_attribute: If set, will set an attribute for each Cloud Pub/Sub
408408
message with the given name and the message's publish time as the value.
409-
enable_message_ordering: If True, enables message ordering on the
409+
publish_with_ordering_key: If True, enables message ordering on the
410410
PublisherClient. Messages with an ordering_key will be delivered
411411
in order. Requires messages to have ordering_key set.
412412
"""
413413
super().__init__()
414414
self.with_attributes = with_attributes
415-
self.enable_message_ordering = enable_message_ordering
415+
self.publish_with_ordering_key = publish_with_ordering_key
416416
self.id_label = id_label
417417
self.timestamp_attribute = timestamp_attribute
418418
self.project, self.topic_name = parse_topic(topic)
@@ -440,7 +440,7 @@ def expand(self, pcoll):
440440
# since _PubSubWriteDoFn._flush() is not used by Dataflow's implementation.
441441
runner = self.pipeline_options.get_all_options().get(
442442
'runner', '') if self.pipeline_options else ''
443-
if 'Dataflow' in str(runner):
443+
if 'Dataflow' in str(runner) and self.publish_with_ordering_key:
444444
logging.warning(
445445
'WriteToPubSub ordering_key support is not available on Dataflow '
446446
'via this transform. Use the XLang WriteToPubSub path instead: '
@@ -472,9 +472,9 @@ def display_data(self):
472472
True, label='With Attributes').drop_if_none(),
473473
'timestamp_attribute': DisplayDataItem(
474474
self.timestamp_attribute, label='Timestamp Attribute'),
475-
'enable_message_ordering': DisplayDataItem(
476-
self.enable_message_ordering,
477-
label='Enable Message Ordering').drop_if_none(),
475+
'publish_with_ordering_key': DisplayDataItem(
476+
self.publish_with_ordering_key,
477+
label='Publish With Ordering Key').drop_if_none(),
478478
}
479479

480480

@@ -581,7 +581,7 @@ def __init__(self, transform):
581581
self.id_label = transform.id_label
582582
self.timestamp_attribute = transform.timestamp_attribute
583583
self.with_attributes = transform.with_attributes
584-
self.with_ordering = transform.enable_message_ordering
584+
self.with_ordering = transform.publish_with_ordering_key
585585

586586
# TODO(https://github.com/apache/beam/issues/18939): Add support for
587587
# id_label and timestamp_attribute.

sdks/python/apache_beam/io/gcp/pubsub_integration_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,7 @@ def test_batch_write_with_ordering_key(self):
319319
if self.runner_name == 'TestDataflowRunner':
320320
self.skipTest(
321321
'Dataflow Native PubSub Sink does not support ordering_key '
322-
'(see https://github.com/apache/beam/issues/36201). '
323-
'Use apache_beam.io.external.gcp.pubsub.WriteToPubSub '
324-
'with publish_with_ordering_key=True instead.')
322+
'(see https://github.com/apache/beam/issues/36201).')
325323
from google.pubsub_v1.types import Subscription
326324

327325
from apache_beam.options.pipeline_options import PipelineOptions
@@ -356,7 +354,9 @@ def test_batch_write_with_ordering_key(self):
356354
with TestPipeline(options=pipeline_options) as p:
357355
messages = p | 'CreateMessages' >> Create(test_messages)
358356
_ = messages | 'WriteToPubSub' >> WriteToPubSub(
359-
ordering_topic.name, with_attributes=True)
357+
ordering_topic.name,
358+
with_attributes=True,
359+
publish_with_ordering_key=True)
360360

361361
time.sleep(10)
362362

sdks/python/apache_beam/io/gcp/pubsub_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ def test_display_data(self):
467467
DisplayDataItemMatcher('id_label', 'id'),
468468
DisplayDataItemMatcher('with_attributes', True),
469469
DisplayDataItemMatcher('timestamp_attribute', 'time'),
470+
DisplayDataItemMatcher('publish_with_ordering_key', False),
470471
]
471472

472473
hc.assert_that(dd.items, hc.contains_inanyorder(*expected_items))
@@ -1114,7 +1115,7 @@ def test_write_messages_with_ordering_key(self, mock_pubsub):
11141115
| WriteToPubSub(
11151116
'projects/fakeprj/topics/a_topic',
11161117
with_attributes=True,
1117-
enable_message_ordering=True))
1118+
publish_with_ordering_key=True))
11181119

11191120
# Verify that publish was called with ordering_key
11201121
mock_pubsub.return_value.publish.assert_called()
@@ -1139,7 +1140,7 @@ def test_write_messages_with_ordering_key_no_attributes(self, mock_pubsub):
11391140
| WriteToPubSub(
11401141
'projects/fakeprj/topics/a_topic',
11411142
with_attributes=True,
1142-
enable_message_ordering=True))
1143+
publish_with_ordering_key=True))
11431144

11441145
# Verify that publish was called with ordering_key
11451146
mock_pubsub.return_value.publish.assert_called()
@@ -1166,6 +1167,8 @@ def test_write_messages_without_ordering_key(self, mock_pubsub):
11661167

11671168
# Verify that publish was called
11681169
mock_pubsub.return_value.publish.assert_called()
1170+
call_args = mock_pubsub.return_value.publish.call_args
1171+
self.assertNotIn('ordering_key', call_args.kwargs)
11691172

11701173

11711174
if __name__ == '__main__':

0 commit comments

Comments
 (0)