Skip to content

Commit 81d31b3

Browse files
author
Nikita Grover
committed
Fix PubSub error
1 parent 7189a01 commit 81d31b3

2 files changed

Lines changed: 41 additions & 43 deletions

File tree

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
"""
3232

3333
# pytype: skip-file
34-
34+
import logging
3535
import re
36+
import time
3637
from typing import Any
3738
from typing import NamedTuple
3839
from typing import Optional
@@ -432,7 +433,6 @@ def expand(self, pcoll):
432433
self.pipeline_options = pcoll.pipeline.options if pcoll.pipeline else None
433434
# Warn Dataflow users to use the XLang path for ordering key support,
434435
# since _PubSubWriteDoFn._flush() is not used by Dataflow's implementation.
435-
import logging
436436
runner = self.pipeline_options.get_all_options().get(
437437
'runner', '') if self.pipeline_options else ''
438438
if 'Dataflow' in str(runner):
@@ -607,7 +607,7 @@ def __init__(self, transform):
607607
output_labels_supported = False
608608

609609
# Log debug information for troubleshooting
610-
import logging
610+
611611
runner_info = getattr(
612612
pipeline_options, 'runner',
613613
'None') if pipeline_options else 'No options'
@@ -638,7 +638,10 @@ def __init__(self, transform):
638638

639639
def setup(self):
640640
from google.cloud import pubsub
641-
self._pub_client = pubsub.PublisherClient()
641+
self._pub_client = pubsub.PublisherClient(
642+
publisher_options=pubsub.types.PublisherOptions(
643+
enable_message_ordering=True,
644+
))
642645
self._topic = self._pub_client.topic_path(
643646
self.project, self.short_topic_name)
644647

@@ -657,8 +660,6 @@ def _flush(self):
657660
if not self._buffer:
658661
return
659662

660-
import time
661-
662663
# The elements in buffer are serialized protobuf bytes from the previous
663664
# transforms. We need to deserialize them to extract data and attributes.
664665
futures = []

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

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
from apache_beam.testing import test_utils
3737
from apache_beam.testing.pipeline_verifiers import PipelineStateMatcher
3838
from apache_beam.testing.test_pipeline import TestPipeline
39+
from apache_beam.options.pipeline_options import PipelineOptions
40+
from apache_beam.options.pipeline_options import StandardOptions
41+
from apache_beam.transforms import Create
42+
from google.pubsub_v1.types import Subscription
43+
from google.cloud import pubsub
3944

4045
INPUT_TOPIC = 'psit_topic_input'
4146
OUTPUT_TOPIC = 'psit_topic_output'
@@ -139,7 +144,6 @@ def setUp(self):
139144
self.uuid = str(uuid.uuid4())
140145

141146
# Set up PubSub environment.
142-
from google.cloud import pubsub
143147
self.pub_client = pubsub.PublisherClient()
144148
self.input_topic = self.pub_client.create_topic(
145149
name=self.pub_client.topic_path(self.project, INPUT_TOPIC + self.uuid))
@@ -228,9 +232,6 @@ def _test_batch_write(self, with_attributes):
228232
with_attributes: False - Writes message data only.
229233
True - Writes message data and attributes.
230234
"""
231-
from apache_beam.options.pipeline_options import PipelineOptions
232-
from apache_beam.options.pipeline_options import StandardOptions
233-
from apache_beam.transforms import Create
234235

235236
# Create test messages for batch mode
236237
test_messages = [
@@ -309,24 +310,19 @@ def test_batch_write_with_attributes(self):
309310
def test_batch_write_with_ordering_key(self):
310311
"""Test WriteToPubSub in batch mode with ordering keys.
311312
312-
Dataflow Native PubSub Sink does not support ordering_key
313-
(https://github.com/apache/beam/issues/36201), therefore this
314-
test only applies to runners that use Beam's Python WriteToPubSub Sink.
315-
To use ordering_key, Dataflow users should use the XLang WriteToPubSub
316-
path instead.
313+
Dataflow's Native Pub/Sub Sink does not support ordering_key
314+
(see https://github.com/apache/beam/issues/36201), so this test
315+
only applies to runners using Beam's Python WriteToPubSub Sink.
316+
Dataflow users should use the XLang WriteToPubSub path instead
317+
(apache_beam.io.external.gcp.pubsub.WriteToPubSub with
318+
publish_with_ordering_key=True).
317319
"""
318320
if self.runner_name == 'TestDataflowRunner':
319321
self.skipTest(
320322
'Dataflow Native PubSub Sink does not support ordering_key '
321-
'(see https://github.com/apache/beam/issues/36201), therefore '
322-
'this test only applies to runners that use Beam\'s Python '
323-
'WriteToPubSub Sink. To use ordering_key, Dataflow users should '
324-
'use the XLang WriteToPubSub path instead.')
325-
326-
from apache_beam.options.pipeline_options import PipelineOptions
327-
from apache_beam.options.pipeline_options import StandardOptions
328-
from apache_beam.transforms import Create
329-
from google.pubsub_v1.types import Subscription
323+
'(see https://github.com/apache/beam/issues/36201). '
324+
'Use apache_beam.io.external.gcp.pubsub.WriteToPubSub '
325+
'with publish_with_ordering_key=True instead.')
330326

331327
ordering_topic = self.pub_client.create_topic(
332328
name=self.pub_client.topic_path(
@@ -336,7 +332,8 @@ def test_batch_write_with_ordering_key(self):
336332
name=self.sub_client.subscription_path(
337333
self.project, 'psit_sub_ordering' + self.uuid),
338334
topic=ordering_topic.name,
339-
enable_message_ordering=True))
335+
enable_message_ordering=True,
336+
))
340337
time.sleep(10)
341338

342339
try:
@@ -346,7 +343,7 @@ def test_batch_write_with_ordering_key(self):
346343
PubsubMessage(
347344
b'order_data002', {'attr': 'value2'}, ordering_key='key1'),
348345
PubsubMessage(
349-
b'order_data003', {'attr': 'value3'}, ordering_key='key2')
346+
b'order_data003', {'attr': 'value3'}, ordering_key='key2'),
350347
]
351348

352349
pipeline_options = PipelineOptions()
@@ -361,30 +358,30 @@ def test_batch_write_with_ordering_key(self):
361358

362359
response = self.sub_client.pull(
363360
request={
364-
"subscription": ordering_sub.name,
365-
"max_messages": 10,
361+
'subscription': ordering_sub.name,
362+
'max_messages': 10,
366363
})
367364

368365
self.assertEqual(len(response.received_messages), len(test_messages))
369366

370-
received_map = {
371-
msg.message.data: msg.message
372-
for msg in response.received_messages
373-
}
374-
self.assertEqual(received_map[b'order_data001'].ordering_key, 'key1')
375-
self.assertEqual(received_map[b'order_data002'].ordering_key, 'key1')
376-
self.assertEqual(received_map[b'order_data003'].ordering_key, 'key2')
367+
received_ordering_keys = [
368+
msg.message.ordering_key for msg in response.received_messages
369+
]
370+
expected_ordering_keys = sorted(
371+
[msg.ordering_key for msg in test_messages])
372+
self.assertEqual(sorted(received_ordering_keys), expected_ordering_keys)
377373

378374
ack_ids = [msg.ack_id for msg in response.received_messages]
379-
if ack_ids:
380-
self.sub_client.acknowledge(
381-
request={
382-
"subscription": ordering_sub.name,
383-
"ack_ids": ack_ids,
384-
})
375+
self.sub_client.acknowledge(
376+
request={
377+
'subscription': ordering_sub.name,
378+
'ack_ids': ack_ids,
379+
})
385380
finally:
386-
test_utils.cleanup_subscriptions(self.sub_client, [ordering_sub])
387-
test_utils.cleanup_topics(self.pub_client, [ordering_topic])
381+
self.sub_client.delete_subscription(
382+
request={'subscription': ordering_sub.name})
383+
self.pub_client.delete_topic(request={'topic': ordering_topic.name})
384+
388385

389386
if __name__ == '__main__':
390387
logging.getLogger().setLevel(logging.DEBUG)

0 commit comments

Comments
 (0)