google-cloud-python icon indicating copy to clipboard operation
google-cloud-python copied to clipboard

Pub/Sub subscriptions don't bind to the specified topic as stated in documentation

Open georgegiovanni opened this issue 10 years ago • 3 comments

Page Name: pubsub-topic Release: 0.18.1

Can a helper subscription method be added in the gcloud.pubsub.client.Client class to facilitate subscribing to subscriptions similar to how there's a topic method to facilitate creating topics. Current instructions for receiving pull messages suggest invoking the gcloud.pubsub.topic.Topic.subscription method which creates a subscription bound to the specified topic as follows:

pubsub_client = pubsub.Client()
topic = pubsub_client.topic(topic_name)
subscription = topic.subscription(subscription_name)

results = subscription.pull(return_immediately=True)

However a non-existent topic name can be supplied for topic_name and messages can still be received from the results.

The following code also works without having to specify a topic:

pubsub_client = pubsub.Client()
subscription = pubsub.subscription.Subscription(subscription_name, client=pubsub_client)

The gcloud.pubsub.topic.Topic.subscription documentation stating "Creates a subscription bound to the current topic" is misleading because it seems it binds to all topics.

georgegiovanni avatar Sep 17 '16 19:09 georgegiovanni

@georgegiovanni Thanks for the report!

tseaver avatar Sep 17 '16 19:09 tseaver

It doesn't really work:

#!/usr/bin/python3

from google.cloud import pubsub

ps = pubsub.Client()
subscription = pubsub.subscription.Subscription('all', client=ps)
if not subscription.exists():
  subscription.create()

print("waiting for messages")
while True:
  with pubsub.subscription.AutoAck(subscription, max_messages=10) as ack:
    for ack_id, message in ack.items():
      print('msg-id: %s, ts: %s, %s' % (
          message.message_id, message.service_timestamp,
          message.data.decode('utf-8')))

if you do this you get:

Traceback (most recent call last):
  File "./suball.py", line 8, in <module>
    subscription.create()
  File "/usr/local/lib/python3.4/dist-packages/google/cloud/pubsub/subscription.py", line 185, in create
    self.full_name, self.topic.full_name, self.ack_deadline,
AttributeError: 'NoneType' object has no attribute 'full_name'

If you leave out the subscription.create() part your get:

Traceback (most recent call last):
  File "./suball.py", line 16, in <module>
    results = subscription.pull(return_immediately=True, max_messages=10)
  File "/usr/local/lib/python3.4/dist-packages/google/cloud/pubsub/subscription.py", line 328, in pull
    self.full_name, return_immediately, max_messages)
  File "/usr/local/lib/python3.4/dist-packages/google/cloud/pubsub/_gax.py", line 418, in subscription_pull
    raise NotFound(subscription_path)
google.cloud.exceptions.NotFound: 404 projects/<project>/subscriptions/all

Having a subscription for all topics would be nice to work around the performance issues with pulling from many topics.

ensonic avatar Feb 23 '17 09:02 ensonic

The back-end does not provide an API method for creating a subscription which binds to multiple topics.

Not passing a topic to the Subscription constructor (or passing None) indicates that the topic for the subscription no longer exists. The only useful thing one can do with such an instance is to call its delete() method.

Creating a Topic instance with a non-existent name and then using it to create a Subscription instance is a pointless exercise: its create() and pull() method will raise NotFound:

>>> from google.cloud.pubsub import Client
>>> client = Client()
>>> topic = client.topic('issue-2341-nonesuch')
>>> topic.exists()
False
>>> subscription = topic.subscription('doomed-to-failure')
>>> results = subscription.pull()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/google-cloud-python/.tox/system-tests/lib/python2.7/site-packages/google/cloud/pubsub/subscription.py", line 332, in pull
    self.full_name, return_immediately, max_messages)
  File "/path/to/google-cloud-python/.tox/system-tests/lib/python2.7/site-packages/google/cloud/pubsub/_gax.py", line 418, in subscription_pull
    raise NotFound(subscription_path)
google.cloud.exceptions.NotFound: 404 projects/<PROJECT-ID>/subscriptions/doomed-to-failure
>>> subscription.create()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/google-cloud-python/.tox/system-tests/lib/python2.7/site-packages/google/cloud/pubsub/subscription.py", line 186, in create
    self.push_endpoint)
  File "/path/to/google-cloud-python/.tox/system-tests/lib/python2.7/site-packages/google/cloud/pubsub/_gax.py", line 315, in subscription_create
    push_config=push_config, ack_deadline_seconds=ack_deadline)
  File "/path/to/google-cloud-python/.tox/system-tests/lib/python2.7/site-packages/google/cloud/gapic/pubsub/v1/subscriber_client.py", line 336, in create_subscription
    return self._create_subscription(request, options)
  File "/path/to/google-cloud-python/.tox/system-tests/lib/python2.7/site-packages/google/gax/api_callable.py", line 442, in inner
    return api_caller(api_call, this_settings, request)
  File "/path/to/google-cloud-python/.tox/system-tests/lib/python2.7/site-packages/google/gax/api_callable.py", line 430, in base_caller
    return api_call(*args)
  File "/path/to/google-cloud-python/.tox/system-tests/lib/python2.7/site-packages/google/gax/api_callable.py", line 395, in inner
    gax.errors.create_error('RPC failed', cause=exception))
  File "/path/to/google-cloud-python/.tox/system-tests/lib/python2.7/site-packages/google/gax/api_callable.py", line 391, in inner
    return a_func(*args, **kwargs)
  File "/path/to/google-cloud-python/.tox/system-tests/lib/python2.7/site-packages/google/gax/retry.py", line 67, in inner
    return a_func(*updated_args, **kwargs)
  File "/path/to/google-cloud-python/.tox/system-tests/lib/python2.7/site-packages/grpc/_channel.py", line 511, in __call__
    return _end_unary_response_blocking(state, False, deadline)
  File "/path/to/google-cloud-python/.tox/system-tests/lib/python2.7/site-packages/grpc/_channel.py", line 459, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
google.gax.errors.GaxError: GaxError(RPC failed, caused by <_Rendezvous of RPC that terminated with (StatusCode.NOT_FOUND, Resource not found (resource=issue-2341-nonesuch).)>)

tseaver avatar Feb 23 '17 17:02 tseaver