2013-07-23 16:28:15 +01:00
|
|
|
# Copyright 2011 OpenStack Foundation
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2015-04-30 23:33:39 +02:00
|
|
|
import collections
|
2015-01-21 09:13:10 +01:00
|
|
|
import contextlib
|
2013-07-23 17:34:18 +01:00
|
|
|
import logging
|
2014-11-27 14:46:52 +01:00
|
|
|
import os
|
2013-07-23 16:28:15 +01:00
|
|
|
import socket
|
|
|
|
import ssl
|
2015-01-21 09:13:10 +01:00
|
|
|
import threading
|
2013-07-23 16:28:15 +01:00
|
|
|
import time
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
import kombu
|
|
|
|
import kombu.connection
|
|
|
|
import kombu.entity
|
|
|
|
import kombu.messaging
|
2015-01-21 18:46:55 -05:00
|
|
|
from oslo_config import cfg
|
|
|
|
from oslo_utils import netutils
|
2013-12-03 15:41:59 +00:00
|
|
|
import six
|
2014-11-17 16:46:46 +01:00
|
|
|
from six.moves.urllib import parse
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-01-02 14:24:57 -05:00
|
|
|
from oslo_messaging._drivers import amqp as rpc_amqp
|
|
|
|
from oslo_messaging._drivers import amqpdriver
|
2015-04-23 17:08:24 +01:00
|
|
|
from oslo_messaging._drivers import base
|
2015-01-02 14:24:57 -05:00
|
|
|
from oslo_messaging._drivers import common as rpc_common
|
|
|
|
from oslo_messaging._i18n import _
|
2015-01-21 09:13:10 +01:00
|
|
|
from oslo_messaging._i18n import _LE
|
2015-01-02 14:24:57 -05:00
|
|
|
from oslo_messaging._i18n import _LI
|
2014-12-08 16:48:36 +01:00
|
|
|
from oslo_messaging._i18n import _LW
|
2015-01-02 14:24:57 -05:00
|
|
|
from oslo_messaging import exceptions
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2014-11-17 16:46:46 +01:00
|
|
|
|
2013-07-23 18:14:17 +01:00
|
|
|
rabbit_opts = [
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.StrOpt('kombu_ssl_version',
|
|
|
|
default='',
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2013-07-23 16:28:15 +01:00
|
|
|
help='SSL version to use (valid only if SSL enabled). '
|
2014-11-25 10:59:05 -06:00
|
|
|
'Valid values are TLSv1 and SSLv23. SSLv2, SSLv3, '
|
|
|
|
'TLSv1_1, and TLSv1_2 may be available on some '
|
|
|
|
'distributions.'
|
2013-07-23 16:28:15 +01:00
|
|
|
),
|
|
|
|
cfg.StrOpt('kombu_ssl_keyfile',
|
|
|
|
default='',
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-02-07 22:13:30 +01:00
|
|
|
help='SSL key file (valid only if SSL enabled).'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.StrOpt('kombu_ssl_certfile',
|
|
|
|
default='',
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-02-07 22:13:30 +01:00
|
|
|
help='SSL cert file (valid only if SSL enabled).'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.StrOpt('kombu_ssl_ca_certs',
|
|
|
|
default='',
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-04-03 17:27:48 +08:00
|
|
|
help='SSL certification authority file '
|
|
|
|
'(valid only if SSL enabled).'),
|
2014-02-26 15:21:01 -08:00
|
|
|
cfg.FloatOpt('kombu_reconnect_delay',
|
|
|
|
default=1.0,
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-02-26 15:21:01 -08:00
|
|
|
help='How long to wait before reconnecting in response to an '
|
|
|
|
'AMQP consumer cancel notification.'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.StrOpt('rabbit_host',
|
|
|
|
default='localhost',
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-02-07 22:13:30 +01:00
|
|
|
help='The RabbitMQ broker address where a single node is '
|
|
|
|
'used.'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.IntOpt('rabbit_port',
|
|
|
|
default=5672,
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-02-07 22:13:30 +01:00
|
|
|
help='The RabbitMQ broker port where a single node is used.'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.ListOpt('rabbit_hosts',
|
|
|
|
default=['$rabbit_host:$rabbit_port'],
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-02-07 22:13:30 +01:00
|
|
|
help='RabbitMQ HA cluster host:port pairs.'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.BoolOpt('rabbit_use_ssl',
|
|
|
|
default=False,
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-02-07 22:13:30 +01:00
|
|
|
help='Connect over SSL for RabbitMQ.'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.StrOpt('rabbit_userid',
|
|
|
|
default='guest',
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-02-07 22:13:30 +01:00
|
|
|
help='The RabbitMQ userid.'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.StrOpt('rabbit_password',
|
|
|
|
default='guest',
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-02-07 22:13:30 +01:00
|
|
|
help='The RabbitMQ password.',
|
2013-07-23 16:28:15 +01:00
|
|
|
secret=True),
|
2014-01-16 19:06:31 +01:00
|
|
|
cfg.StrOpt('rabbit_login_method',
|
|
|
|
default='AMQPLAIN',
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-08-28 06:59:33 +02:00
|
|
|
help='The RabbitMQ login method.'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.StrOpt('rabbit_virtual_host',
|
|
|
|
default='/',
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-02-07 22:13:30 +01:00
|
|
|
help='The RabbitMQ virtual host.'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.IntOpt('rabbit_retry_interval',
|
|
|
|
default=1,
|
2014-02-07 22:13:30 +01:00
|
|
|
help='How frequently to retry connecting with RabbitMQ.'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.IntOpt('rabbit_retry_backoff',
|
|
|
|
default=2,
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-01-18 14:36:11 +01:00
|
|
|
help='How long to backoff for between retries when connecting '
|
2014-02-07 22:13:30 +01:00
|
|
|
'to RabbitMQ.'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.IntOpt('rabbit_max_retries',
|
|
|
|
default=0,
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-01-18 14:36:11 +01:00
|
|
|
help='Maximum number of RabbitMQ connection retries. '
|
2014-02-07 22:13:30 +01:00
|
|
|
'Default is 0 (infinite retry count).'),
|
2013-07-23 16:28:15 +01:00
|
|
|
cfg.BoolOpt('rabbit_ha_queues',
|
|
|
|
default=False,
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-01-18 14:36:11 +01:00
|
|
|
help='Use HA queues in RabbitMQ (x-ha-policy: all). '
|
|
|
|
'If you change this option, you must wipe the '
|
|
|
|
'RabbitMQ database.'),
|
2015-01-21 09:13:10 +01:00
|
|
|
cfg.IntOpt('heartbeat_timeout_threshold',
|
2015-04-17 17:37:34 +02:00
|
|
|
default=0,
|
2015-01-21 09:13:10 +01:00
|
|
|
help="Number of seconds after which the Rabbit broker is "
|
|
|
|
"considered down if heartbeat's keep-alive fails "
|
2015-04-17 17:37:34 +02:00
|
|
|
"(0 disable the heartbeat). EXPERIMENTAL"),
|
2015-01-21 09:13:10 +01:00
|
|
|
cfg.IntOpt('heartbeat_rate',
|
|
|
|
default=2,
|
|
|
|
help='How often times during the heartbeat_timeout_threshold '
|
|
|
|
'we check the heartbeat.'),
|
2014-12-04 08:10:13 +01:00
|
|
|
|
2015-01-02 14:24:57 -05:00
|
|
|
# NOTE(sileht): deprecated option since oslo_messaging 1.5.0,
|
2014-12-04 08:10:13 +01:00
|
|
|
cfg.BoolOpt('fake_rabbit',
|
|
|
|
default=False,
|
2015-01-28 08:57:21 +01:00
|
|
|
deprecated_group='DEFAULT',
|
2014-12-04 08:10:13 +01:00
|
|
|
help='Deprecated, use rpc_backend=kombu+memory or '
|
|
|
|
'rpc_backend=fake'),
|
2013-07-23 16:28:15 +01:00
|
|
|
]
|
|
|
|
|
2013-07-23 17:34:18 +01:00
|
|
|
LOG = logging.getLogger(__name__)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _get_queue_arguments(conf):
|
|
|
|
"""Construct the arguments for declaring a queue.
|
|
|
|
|
|
|
|
If the rabbit_ha_queues option is set, we declare a mirrored queue
|
|
|
|
as described here:
|
|
|
|
|
|
|
|
http://www.rabbitmq.com/ha.html
|
|
|
|
|
|
|
|
Setting x-ha-policy to all means that the queue will be mirrored
|
|
|
|
to all nodes in the cluster.
|
|
|
|
"""
|
|
|
|
return {'x-ha-policy': 'all'} if conf.rabbit_ha_queues else {}
|
|
|
|
|
|
|
|
|
2013-11-29 16:03:37 +01:00
|
|
|
class RabbitMessage(dict):
|
|
|
|
def __init__(self, raw_message):
|
|
|
|
super(RabbitMessage, self).__init__(
|
|
|
|
rpc_common.deserialize_msg(raw_message.payload))
|
|
|
|
self._raw_message = raw_message
|
|
|
|
|
|
|
|
def acknowledge(self):
|
|
|
|
self._raw_message.ack()
|
|
|
|
|
2013-12-11 16:50:09 +01:00
|
|
|
def requeue(self):
|
|
|
|
self._raw_message.requeue()
|
|
|
|
|
2013-11-29 16:03:37 +01:00
|
|
|
|
2015-04-30 17:46:42 +02:00
|
|
|
class Consumer(object):
|
|
|
|
"""Consumer class."""
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 17:46:42 +02:00
|
|
|
def __init__(self, conf, exchange_name, queue_name, routing_key, type,
|
|
|
|
durable, auto_delete, callback, nowait=True):
|
|
|
|
"""Init the Publisher class with the exchange_name, routing_key,
|
|
|
|
type, durable auto_delete
|
2013-07-23 16:28:15 +01:00
|
|
|
"""
|
2015-04-30 17:46:42 +02:00
|
|
|
self.queue_name = queue_name
|
|
|
|
self.exchange_name = exchange_name
|
|
|
|
self.routing_key = routing_key
|
|
|
|
self.auto_delete = auto_delete
|
|
|
|
self.durable = durable
|
2013-07-23 16:28:15 +01:00
|
|
|
self.callback = callback
|
2015-04-30 17:46:42 +02:00
|
|
|
self.type = type
|
|
|
|
self.nowait = nowait
|
|
|
|
self.queue_arguments = _get_queue_arguments(conf)
|
|
|
|
|
2013-07-23 16:28:15 +01:00
|
|
|
self.queue = None
|
2015-04-30 17:46:42 +02:00
|
|
|
self.exchange = kombu.entity.Exchange(
|
|
|
|
name=exchange_name,
|
|
|
|
type=type,
|
|
|
|
durable=self.durable,
|
|
|
|
auto_delete=self.auto_delete)
|
|
|
|
|
2015-05-01 00:24:46 +02:00
|
|
|
def declare(self, conn):
|
2015-04-30 17:46:42 +02:00
|
|
|
"""Re-declare the queue after a rabbit (re)connect."""
|
|
|
|
self.queue = kombu.entity.Queue(
|
|
|
|
name=self.queue_name,
|
2015-05-01 00:24:46 +02:00
|
|
|
channel=conn.channel,
|
2015-04-30 17:46:42 +02:00
|
|
|
exchange=self.exchange,
|
|
|
|
durable=self.durable,
|
|
|
|
auto_delete=self.auto_delete,
|
|
|
|
routing_key=self.routing_key,
|
|
|
|
queue_arguments=self.queue_arguments)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
Fix reconnect race condition with RabbitMQ cluster
Retry Queue creation to workaround race condition
that may happen when both the client and broker race over
exchange creation and deletion respectively which happen only
when the Queue/Exchange were created with auto-delete flag.
Queues/Exchange declared with auto-delete instruct the Broker to
delete the Queue when the last Consumer disconnect from it, and
the Exchange when the last Queue is deleted from this Exchange.
Now in a RabbitMQ cluster setup, if the cluster node that we are
connected to go down, 2 things will happen:
1. From RabbitMQ side, the Queues w/ auto-delete will be deleted
from the other cluster nodes and then the Exchanges that the
Queues are bind to if they were also created w/ auto-delete.
2. From client side, client will reconnect to another cluster
node and call queue.declare() which create Exchanges then
Queues then Binding in that order.
Now in a happy path the queues/exchanges will be deleted from the
broker before client start re-creating them again, but it also
possible that the client first start by creating queues/exchange
as part of the queue.declare() call, which are no-op operations
b/c they alreay existed, but before it could bind Queue to
Exchange, RabbitMQ nodes just received the 'signal' that the
queue doesn't have any consumer so it should be delete, and the
same with exchanges, which will lead to binding fail with
NotFound error.
Illustration of the time line from Client and RabbitMQ cluster
respectively when the race condition happen:
e-declare(E) q-declare(Q) q-bind(Q, E)
-----+------------------+----------------+----------->
e-delete(E)
------------------------------+---------------------->
Change-Id: Ideb73af6f246a8282780cdb204d675d5d4555bf0
Closes-Bug: #1318721
2014-06-27 16:46:47 +02:00
|
|
|
try:
|
|
|
|
self.queue.declare()
|
2015-05-01 00:24:46 +02:00
|
|
|
except conn.connection.channel_errors as exc:
|
|
|
|
# NOTE(jrosenboom): This exception may be triggered by a race
|
|
|
|
# condition. Simply retrying will solve the error most of the time
|
|
|
|
# and should work well enough as a workaround until the race
|
|
|
|
# condition itself can be fixed.
|
Fix reconnect race condition with RabbitMQ cluster
Retry Queue creation to workaround race condition
that may happen when both the client and broker race over
exchange creation and deletion respectively which happen only
when the Queue/Exchange were created with auto-delete flag.
Queues/Exchange declared with auto-delete instruct the Broker to
delete the Queue when the last Consumer disconnect from it, and
the Exchange when the last Queue is deleted from this Exchange.
Now in a RabbitMQ cluster setup, if the cluster node that we are
connected to go down, 2 things will happen:
1. From RabbitMQ side, the Queues w/ auto-delete will be deleted
from the other cluster nodes and then the Exchanges that the
Queues are bind to if they were also created w/ auto-delete.
2. From client side, client will reconnect to another cluster
node and call queue.declare() which create Exchanges then
Queues then Binding in that order.
Now in a happy path the queues/exchanges will be deleted from the
broker before client start re-creating them again, but it also
possible that the client first start by creating queues/exchange
as part of the queue.declare() call, which are no-op operations
b/c they alreay existed, but before it could bind Queue to
Exchange, RabbitMQ nodes just received the 'signal' that the
queue doesn't have any consumer so it should be delete, and the
same with exchanges, which will lead to binding fail with
NotFound error.
Illustration of the time line from Client and RabbitMQ cluster
respectively when the race condition happen:
e-declare(E) q-declare(Q) q-bind(Q, E)
-----+------------------+----------------+----------->
e-delete(E)
------------------------------+---------------------->
Change-Id: Ideb73af6f246a8282780cdb204d675d5d4555bf0
Closes-Bug: #1318721
2014-06-27 16:46:47 +02:00
|
|
|
# See https://bugs.launchpad.net/neutron/+bug/1318721 for details.
|
2015-05-01 00:24:46 +02:00
|
|
|
if exc.code == 404:
|
|
|
|
self.queue.declare()
|
|
|
|
else:
|
|
|
|
raise
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 17:46:42 +02:00
|
|
|
def consume(self, tag):
|
2013-07-23 16:28:15 +01:00
|
|
|
"""Actually declare the consumer on the amqp channel. This will
|
|
|
|
start the flow of messages from the queue. Using the
|
2015-04-30 08:56:20 +02:00
|
|
|
Connection.consume() will process the messages,
|
2013-07-23 16:28:15 +01:00
|
|
|
calling the appropriate callback.
|
|
|
|
"""
|
|
|
|
|
2015-04-30 17:46:42 +02:00
|
|
|
self.queue.consume(callback=self._callback,
|
|
|
|
consumer_tag=six.text_type(tag),
|
|
|
|
nowait=self.nowait)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 17:46:42 +02:00
|
|
|
def _callback(self, message):
|
|
|
|
"""Call callback with deserialized message.
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 17:46:42 +02:00
|
|
|
Messages that are processed and ack'ed.
|
2013-07-23 16:28:15 +01:00
|
|
|
"""
|
|
|
|
|
2015-04-30 17:46:42 +02:00
|
|
|
m2p = getattr(self.queue.channel, 'message_to_python', None)
|
|
|
|
if m2p:
|
|
|
|
message = m2p(message)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.callback(RabbitMessage(message))
|
|
|
|
except Exception:
|
|
|
|
LOG.exception(_("Failed to process message"
|
|
|
|
" ... skipping it."))
|
|
|
|
message.ack()
|
2013-07-23 16:28:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Publisher(object):
|
2015-04-30 16:13:14 +02:00
|
|
|
"""Publisher that silently creates exchange but no queues."""
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
passive = False
|
|
|
|
|
|
|
|
def __init__(self, conf, exchange_name, routing_key, type, durable,
|
|
|
|
auto_delete):
|
2013-07-23 16:28:15 +01:00
|
|
|
"""Init the Publisher class with the exchange_name, routing_key,
|
2015-04-30 16:13:14 +02:00
|
|
|
type, durable auto_delete
|
2013-07-23 16:28:15 +01:00
|
|
|
"""
|
2015-04-30 16:13:14 +02:00
|
|
|
self.queue_arguments = _get_queue_arguments(conf)
|
2013-07-23 16:28:15 +01:00
|
|
|
self.exchange_name = exchange_name
|
|
|
|
self.routing_key = routing_key
|
2015-04-30 16:13:14 +02:00
|
|
|
self.auto_delete = auto_delete
|
|
|
|
self.durable = durable
|
2013-07-23 16:28:15 +01:00
|
|
|
self.exchange = kombu.entity.Exchange(name=self.exchange_name,
|
2015-04-30 16:13:14 +02:00
|
|
|
type=type,
|
|
|
|
durable=durable,
|
|
|
|
auto_delete=auto_delete,
|
|
|
|
passive=self.passive)
|
|
|
|
|
|
|
|
def send(self, conn, msg, timeout=None):
|
|
|
|
"""Send a message on an channel."""
|
|
|
|
producer = kombu.messaging.Producer(exchange=self.exchange,
|
|
|
|
channel=conn.channel,
|
|
|
|
routing_key=self.routing_key)
|
|
|
|
|
|
|
|
headers = {}
|
2013-07-23 16:28:15 +01:00
|
|
|
if timeout:
|
2015-04-30 16:13:14 +02:00
|
|
|
# AMQP TTL is in milliseconds when set in the property.
|
|
|
|
# Details: http://www.rabbitmq.com/ttl.html#per-message-ttl
|
|
|
|
# NOTE(sileht): this amqp header doesn't exists ... LP#1444854
|
|
|
|
headers['ttl'] = timeout * 1000
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
producer.publish(msg, headers=headers)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2014-07-22 09:42:52 -07:00
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
class DeclareQueuePublisher(Publisher):
|
|
|
|
"""Publisher that declares a default queue
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
When the exchange is missing instead of silently creating an exchange
|
|
|
|
not binded to a queue, this publisher creates a default queue
|
|
|
|
named with the routing_key.
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
This is mainly used to not miss notifications in case of nobody consumes
|
|
|
|
them yet. If the future consumer binds the default queue it can retrieve
|
|
|
|
missing messages.
|
|
|
|
"""
|
2015-04-30 23:33:39 +02:00
|
|
|
|
|
|
|
DECLARED_QUEUES = collections.defaultdict(set)
|
2015-04-30 16:13:14 +02:00
|
|
|
|
|
|
|
def send(self, conn, msg, timeout=None):
|
2015-04-30 23:33:39 +02:00
|
|
|
queue_indentifier = (self.exchange_name,
|
|
|
|
self.routing_key)
|
|
|
|
# NOTE(sileht): We only do it once per reconnection
|
|
|
|
# the Connection._set_current_channel() is responsible to clear
|
|
|
|
# this cache
|
|
|
|
if queue_indentifier not in self.DECLARED_QUEUES[conn.channel]:
|
|
|
|
queue = kombu.entity.Queue(
|
|
|
|
channel=conn.channel,
|
|
|
|
exchange=self.exchange,
|
|
|
|
durable=self.durable,
|
|
|
|
auto_delete=self.auto_delete,
|
|
|
|
name=self.routing_key,
|
|
|
|
routing_key=self.routing_key,
|
|
|
|
queue_arguments=self.queue_arguments)
|
|
|
|
queue.declare()
|
|
|
|
self.DECLARED_QUEUES[conn.channel].add(queue_indentifier)
|
2015-04-30 16:13:14 +02:00
|
|
|
super(DeclareQueuePublisher, self).send(
|
|
|
|
conn, msg, timeout)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 23:33:39 +02:00
|
|
|
@classmethod
|
|
|
|
def reset_cache(cls, channel):
|
|
|
|
cls.DECLARED_QUEUES.pop(channel, None)
|
|
|
|
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
class RetryOnMissingExchangePublisher(Publisher):
|
|
|
|
"""Publisher that retry during 60 seconds if the exchange is missing."""
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
passive = True
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
def send(self, conn, msg, timeout=None):
|
|
|
|
# TODO(sileht):
|
|
|
|
# * use timeout parameter when available
|
|
|
|
# * use rpc_timeout if not instead of hardcoded 60
|
|
|
|
# * use @retrying
|
|
|
|
timer = rpc_common.DecayingTimer(duration=60)
|
|
|
|
timer.start()
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
super(RetryOnMissingExchangePublisher, self).send(conn, msg,
|
|
|
|
timeout)
|
|
|
|
return
|
|
|
|
except conn.connection.channel_errors as exc:
|
|
|
|
# NOTE(noelbk/sileht):
|
|
|
|
# If rabbit dies, the consumer can be disconnected before the
|
|
|
|
# publisher sends, and if the consumer hasn't declared the
|
|
|
|
# queue, the publisher's will send a message to an exchange
|
|
|
|
# that's not bound to a queue, and the message wll be lost.
|
|
|
|
# So we set passive=True to the publisher exchange and catch
|
|
|
|
# the 404 kombu ChannelError and retry until the exchange
|
|
|
|
# appears
|
|
|
|
if exc.code == 404 and timer.check_return() > 0:
|
|
|
|
LOG.info(_LI("The exchange %(exchange)s to send to "
|
|
|
|
"%(routing_key)s doesn't exist yet, "
|
|
|
|
"retrying...") % {
|
|
|
|
'exchange': self.exchange,
|
|
|
|
'routing_key': self.routing_key})
|
|
|
|
time.sleep(1)
|
|
|
|
continue
|
|
|
|
raise
|
2013-07-23 16:28:15 +01:00
|
|
|
|
|
|
|
|
2015-01-21 09:13:10 +01:00
|
|
|
class DummyConnectionLock(object):
|
|
|
|
def acquire(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def release(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def heartbeat_acquire(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.acquire()
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
self.release()
|
|
|
|
|
|
|
|
|
|
|
|
class ConnectionLock(DummyConnectionLock):
|
|
|
|
"""Lock object to protect access the the kombu connection
|
|
|
|
|
|
|
|
This is a lock object to protect access the the kombu connection
|
|
|
|
object between the heartbeat thread and the driver thread.
|
|
|
|
|
|
|
|
They are two way to acquire this lock:
|
|
|
|
* lock.acquire()
|
|
|
|
* lock.heartbeat_acquire()
|
|
|
|
|
|
|
|
In both case lock.release(), release the lock.
|
|
|
|
|
|
|
|
The goal is that the heartbeat thread always have the priority
|
|
|
|
for acquiring the lock. This ensures we have no heartbeat
|
|
|
|
starvation when the driver sends a lot of messages.
|
|
|
|
|
|
|
|
So when lock.heartbeat_acquire() is called next time the lock
|
|
|
|
is released(), the caller unconditionnaly acquires
|
|
|
|
the lock, even someone else have asked for the lock before it.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._workers_waiting = 0
|
|
|
|
self._heartbeat_waiting = False
|
|
|
|
self._lock_acquired = None
|
|
|
|
self._monitor = threading.Lock()
|
|
|
|
self._workers_locks = threading.Condition(self._monitor)
|
|
|
|
self._heartbeat_lock = threading.Condition(self._monitor)
|
|
|
|
self._get_thread_id = self._fetch_current_thread_functor()
|
|
|
|
|
|
|
|
def acquire(self):
|
|
|
|
with self._monitor:
|
|
|
|
while self._lock_acquired:
|
|
|
|
self._workers_waiting += 1
|
|
|
|
self._workers_locks.wait()
|
|
|
|
self._workers_waiting -= 1
|
|
|
|
self._lock_acquired = self._get_thread_id()
|
|
|
|
|
|
|
|
def heartbeat_acquire(self):
|
|
|
|
# NOTE(sileht): must be called only one time
|
|
|
|
with self._monitor:
|
|
|
|
while self._lock_acquired is not None:
|
|
|
|
self._heartbeat_waiting = True
|
|
|
|
self._heartbeat_lock.wait()
|
|
|
|
self._heartbeat_waiting = False
|
|
|
|
self._lock_acquired = self._get_thread_id()
|
|
|
|
|
|
|
|
def release(self):
|
|
|
|
with self._monitor:
|
|
|
|
if self._lock_acquired is None:
|
|
|
|
raise RuntimeError("We can't release a not acquired lock")
|
|
|
|
thread_id = self._get_thread_id()
|
|
|
|
if self._lock_acquired != thread_id:
|
|
|
|
raise RuntimeError("We can't release lock acquired by another "
|
|
|
|
"thread/greenthread; %s vs %s" %
|
|
|
|
(self._lock_acquired, thread_id))
|
|
|
|
self._lock_acquired = None
|
|
|
|
if self._heartbeat_waiting:
|
|
|
|
self._heartbeat_lock.notify()
|
|
|
|
elif self._workers_waiting > 0:
|
|
|
|
self._workers_locks.notify()
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def for_heartbeat(self):
|
|
|
|
self.heartbeat_acquire()
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
self.release()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _fetch_current_thread_functor():
|
|
|
|
# Until https://github.com/eventlet/eventlet/issues/172 is resolved
|
|
|
|
# or addressed we have to use complicated workaround to get a object
|
|
|
|
# that will not be recycled; the usage of threading.current_thread()
|
|
|
|
# doesn't appear to currently be monkey patched and therefore isn't
|
|
|
|
# reliable to use (and breaks badly when used as all threads share
|
|
|
|
# the same current_thread() object)...
|
|
|
|
try:
|
|
|
|
import eventlet
|
|
|
|
from eventlet import patcher
|
|
|
|
green_threaded = patcher.is_monkey_patched('thread')
|
|
|
|
except ImportError:
|
|
|
|
green_threaded = False
|
|
|
|
if green_threaded:
|
|
|
|
return lambda: eventlet.getcurrent()
|
|
|
|
else:
|
|
|
|
return lambda: threading.current_thread()
|
|
|
|
|
|
|
|
|
2013-07-23 16:28:15 +01:00
|
|
|
class Connection(object):
|
|
|
|
"""Connection object."""
|
|
|
|
|
2014-03-07 10:46:17 +01:00
|
|
|
pools = {}
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-01-21 09:13:10 +01:00
|
|
|
def __init__(self, conf, url, purpose):
|
2013-07-23 16:28:15 +01:00
|
|
|
self.conf = conf
|
2015-01-28 08:57:21 +01:00
|
|
|
self.driver_conf = self.conf.oslo_messaging_rabbit
|
|
|
|
self.max_retries = self.driver_conf.rabbit_max_retries
|
2013-07-23 16:28:15 +01:00
|
|
|
# Try forever?
|
|
|
|
if self.max_retries <= 0:
|
|
|
|
self.max_retries = None
|
2015-01-28 08:57:21 +01:00
|
|
|
self.interval_start = self.driver_conf.rabbit_retry_interval
|
|
|
|
self.interval_stepping = self.driver_conf.rabbit_retry_backoff
|
2013-07-23 16:28:15 +01:00
|
|
|
# max retry-interval = 30 seconds
|
|
|
|
self.interval_max = 30
|
|
|
|
|
2015-01-28 08:57:21 +01:00
|
|
|
self._login_method = self.driver_conf.rabbit_login_method
|
2014-03-07 10:46:17 +01:00
|
|
|
|
|
|
|
if url.virtual_host is not None:
|
|
|
|
virtual_host = url.virtual_host
|
|
|
|
else:
|
2015-01-28 08:57:21 +01:00
|
|
|
virtual_host = self.driver_conf.rabbit_virtual_host
|
2014-03-07 10:46:17 +01:00
|
|
|
|
2014-11-17 16:46:46 +01:00
|
|
|
self._url = ''
|
2015-01-28 08:57:21 +01:00
|
|
|
if self.driver_conf.fake_rabbit:
|
2014-12-04 08:10:13 +01:00
|
|
|
LOG.warn("Deprecated: fake_rabbit option is deprecated, set "
|
|
|
|
"rpc_backend to kombu+memory or use the fake "
|
|
|
|
"driver instead.")
|
|
|
|
self._url = 'memory://%s/' % virtual_host
|
|
|
|
elif url.hosts:
|
2014-12-08 16:48:36 +01:00
|
|
|
if url.transport.startswith('kombu+'):
|
|
|
|
LOG.warn(_LW('Selecting the kombu transport through the '
|
|
|
|
'transport url (%s) is a experimental feature '
|
|
|
|
'and this is not yet supported.') % url.transport)
|
2014-03-07 10:46:17 +01:00
|
|
|
for host in url.hosts:
|
2014-11-17 16:46:46 +01:00
|
|
|
transport = url.transport.replace('kombu+', '')
|
2014-12-08 16:48:36 +01:00
|
|
|
transport = transport.replace('rabbit', 'amqp')
|
2014-11-17 16:46:46 +01:00
|
|
|
self._url += '%s%s://%s:%s@%s:%s/%s' % (
|
|
|
|
";" if self._url else '',
|
|
|
|
transport,
|
|
|
|
parse.quote(host.username or ''),
|
|
|
|
parse.quote(host.password or ''),
|
2015-03-19 15:09:52 +01:00
|
|
|
self._parse_url_hostname(host.hostname) or '',
|
|
|
|
str(host.port or 5672),
|
2014-11-17 16:46:46 +01:00
|
|
|
virtual_host)
|
2014-12-01 11:28:13 +01:00
|
|
|
elif url.transport.startswith('kombu+'):
|
|
|
|
# NOTE(sileht): url have a + but no hosts
|
|
|
|
# (like kombu+memory:///), pass it to kombu as-is
|
|
|
|
transport = url.transport.replace('kombu+', '')
|
|
|
|
self._url = "%s://%s" % (transport, virtual_host)
|
2014-03-07 10:46:17 +01:00
|
|
|
else:
|
2015-01-28 08:57:21 +01:00
|
|
|
for adr in self.driver_conf.rabbit_hosts:
|
2014-08-21 13:01:40 +02:00
|
|
|
hostname, port = netutils.parse_host_port(
|
2015-01-28 08:57:21 +01:00
|
|
|
adr, default_port=self.driver_conf.rabbit_port)
|
2014-11-17 16:46:46 +01:00
|
|
|
self._url += '%samqp://%s:%s@%s:%s/%s' % (
|
|
|
|
";" if self._url else '',
|
2015-01-28 08:57:21 +01:00
|
|
|
parse.quote(self.driver_conf.rabbit_userid),
|
|
|
|
parse.quote(self.driver_conf.rabbit_password),
|
2015-03-19 15:09:52 +01:00
|
|
|
self._parse_url_hostname(hostname), port,
|
2014-11-17 16:46:46 +01:00
|
|
|
virtual_host)
|
2014-03-07 10:46:17 +01:00
|
|
|
|
2014-11-27 14:46:52 +01:00
|
|
|
self._initial_pid = os.getpid()
|
|
|
|
|
2015-05-01 00:09:18 +02:00
|
|
|
self._consumers = []
|
|
|
|
self._new_consumers = []
|
2014-12-08 10:52:45 +01:00
|
|
|
self._consume_loop_stopped = False
|
2014-11-17 16:46:46 +01:00
|
|
|
self.channel = None
|
2015-01-21 09:13:10 +01:00
|
|
|
|
|
|
|
# NOTE(sileht): if purpose is PURPOSE_LISTEN
|
|
|
|
# we don't need the lock because we don't
|
|
|
|
# have a heartbeat thread
|
|
|
|
if purpose == rpc_amqp.PURPOSE_SEND:
|
|
|
|
self._connection_lock = ConnectionLock()
|
|
|
|
else:
|
|
|
|
self._connection_lock = DummyConnectionLock()
|
|
|
|
|
2014-11-17 16:46:46 +01:00
|
|
|
self.connection = kombu.connection.Connection(
|
2015-02-10 13:32:22 +01:00
|
|
|
self._url, ssl=self._fetch_ssl_params(),
|
|
|
|
login_method=self._login_method,
|
2015-01-21 09:13:10 +01:00
|
|
|
failover_strategy="shuffle",
|
|
|
|
heartbeat=self.driver_conf.heartbeat_timeout_threshold)
|
2014-11-17 16:46:46 +01:00
|
|
|
|
2015-05-06 11:11:03 +02:00
|
|
|
LOG.info(_LI('Connecting to AMQP server on %(hostname)s:%(port)s'),
|
2014-12-08 16:48:36 +01:00
|
|
|
self.connection.info())
|
2015-01-21 09:13:10 +01:00
|
|
|
|
|
|
|
# NOTE(sileht): kombu recommend to run heartbeat_check every
|
|
|
|
# seconds, but we use a lock around the kombu connection
|
|
|
|
# so, to not lock to much this lock to most of the time do nothing
|
|
|
|
# expected waiting the events drain, we start heartbeat_check and
|
|
|
|
# retreive the server heartbeat packet only two times more than
|
|
|
|
# the minimum required for the heartbeat works
|
|
|
|
# (heatbeat_timeout/heartbeat_rate/2.0, default kombu
|
|
|
|
# heartbeat_rate is 2)
|
|
|
|
self._heartbeat_wait_timeout = (
|
|
|
|
float(self.driver_conf.heartbeat_timeout_threshold) /
|
|
|
|
float(self.driver_conf.heartbeat_rate) / 2.0)
|
|
|
|
self._heartbeat_support_log_emitted = False
|
|
|
|
|
2014-11-17 16:46:46 +01:00
|
|
|
# NOTE(sileht): just ensure the connection is setuped at startup
|
2015-01-21 09:13:10 +01:00
|
|
|
self.ensure_connection()
|
|
|
|
|
|
|
|
# NOTE(sileht): if purpose is PURPOSE_LISTEN
|
|
|
|
# the consume code does the heartbeat stuff
|
|
|
|
# we don't need a thread
|
|
|
|
if purpose == rpc_amqp.PURPOSE_SEND:
|
|
|
|
self._heartbeat_start()
|
|
|
|
|
2015-05-06 11:11:03 +02:00
|
|
|
LOG.info(_LI('Connected to AMQP server on %(hostname)s:%(port)s'),
|
2014-12-08 16:48:36 +01:00
|
|
|
self.connection.info())
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-01-21 10:45:50 +01:00
|
|
|
# NOTE(sileht):
|
|
|
|
# value choosen according the best practice from kombu:
|
|
|
|
# http://kombu.readthedocs.org/en/latest/reference/kombu.common.html#kombu.common.eventloop
|
|
|
|
self._poll_timeout = 1
|
|
|
|
|
2014-12-01 11:28:13 +01:00
|
|
|
if self._url.startswith('memory://'):
|
2014-11-17 16:46:46 +01:00
|
|
|
# Kludge to speed up tests.
|
|
|
|
self.connection.transport.polling_interval = 0.0
|
2015-04-30 23:33:39 +02:00
|
|
|
# Fixup logging
|
|
|
|
self.connection.hostname = "memory_driver"
|
|
|
|
self.connection.port = 1234
|
2015-01-21 10:45:50 +01:00
|
|
|
self._poll_timeout = 0.05
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2014-03-04 06:23:35 -08:00
|
|
|
# FIXME(markmc): use oslo sslutils when it is available as a library
|
|
|
|
_SSL_PROTOCOLS = {
|
|
|
|
"tlsv1": ssl.PROTOCOL_TLSv1,
|
2014-11-21 17:40:46 +08:00
|
|
|
"sslv23": ssl.PROTOCOL_SSLv23
|
2014-03-04 06:23:35 -08:00
|
|
|
}
|
|
|
|
|
2014-11-25 10:59:05 -06:00
|
|
|
_OPTIONAL_PROTOCOLS = {
|
|
|
|
'sslv2': 'PROTOCOL_SSLv2',
|
|
|
|
'sslv3': 'PROTOCOL_SSLv3',
|
|
|
|
'tlsv1_1': 'PROTOCOL_TLSv1_1',
|
|
|
|
'tlsv1_2': 'PROTOCOL_TLSv1_2',
|
|
|
|
}
|
|
|
|
for protocol in _OPTIONAL_PROTOCOLS:
|
|
|
|
try:
|
|
|
|
_SSL_PROTOCOLS[protocol] = getattr(ssl,
|
|
|
|
_OPTIONAL_PROTOCOLS[protocol])
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2014-03-04 06:23:35 -08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def validate_ssl_version(cls, version):
|
|
|
|
key = version.lower()
|
|
|
|
try:
|
|
|
|
return cls._SSL_PROTOCOLS[key]
|
|
|
|
except KeyError:
|
|
|
|
raise RuntimeError(_("Invalid SSL version : %s") % version)
|
|
|
|
|
2015-03-19 15:09:52 +01:00
|
|
|
def _parse_url_hostname(self, hostname):
|
|
|
|
"""Handles hostname returned from urlparse and checks whether it's
|
|
|
|
ipaddress. If it's ipaddress it ensures that it has brackets for IPv6.
|
|
|
|
"""
|
|
|
|
return '[%s]' % hostname if ':' in hostname else hostname
|
|
|
|
|
2013-07-23 16:28:15 +01:00
|
|
|
def _fetch_ssl_params(self):
|
|
|
|
"""Handles fetching what ssl params should be used for the connection
|
|
|
|
(if any).
|
|
|
|
"""
|
2015-02-10 13:32:22 +01:00
|
|
|
if self.driver_conf.rabbit_use_ssl:
|
|
|
|
ssl_params = dict()
|
|
|
|
|
|
|
|
# http://docs.python.org/library/ssl.html - ssl.wrap_socket
|
|
|
|
if self.driver_conf.kombu_ssl_version:
|
|
|
|
ssl_params['ssl_version'] = self.validate_ssl_version(
|
|
|
|
self.driver_conf.kombu_ssl_version)
|
|
|
|
if self.driver_conf.kombu_ssl_keyfile:
|
|
|
|
ssl_params['keyfile'] = self.driver_conf.kombu_ssl_keyfile
|
|
|
|
if self.driver_conf.kombu_ssl_certfile:
|
|
|
|
ssl_params['certfile'] = self.driver_conf.kombu_ssl_certfile
|
|
|
|
if self.driver_conf.kombu_ssl_ca_certs:
|
|
|
|
ssl_params['ca_certs'] = self.driver_conf.kombu_ssl_ca_certs
|
|
|
|
# We might want to allow variations in the
|
|
|
|
# future with this?
|
|
|
|
ssl_params['cert_reqs'] = ssl.CERT_REQUIRED
|
|
|
|
return ssl_params or True
|
|
|
|
return False
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-01-21 09:13:10 +01:00
|
|
|
def ensure_connection(self):
|
2015-03-18 07:59:19 +01:00
|
|
|
self.ensure(method=lambda: True)
|
2015-01-21 09:13:10 +01:00
|
|
|
|
2015-03-18 07:59:19 +01:00
|
|
|
def ensure(self, method, retry=None,
|
|
|
|
recoverable_error_callback=None, error_callback=None,
|
2014-11-17 16:46:46 +01:00
|
|
|
timeout_is_error=True):
|
|
|
|
"""Will retry up to retry number of times.
|
2014-02-21 11:50:45 +01:00
|
|
|
retry = None means use the value of rabbit_max_retries
|
|
|
|
retry = -1 means to retry forever
|
|
|
|
retry = 0 means no retry
|
|
|
|
retry = N means N retries
|
2015-01-21 09:13:10 +01:00
|
|
|
|
|
|
|
NOTE(sileht): Must be called within the connection lock
|
2013-07-23 16:28:15 +01:00
|
|
|
"""
|
|
|
|
|
2014-11-27 14:46:52 +01:00
|
|
|
current_pid = os.getpid()
|
|
|
|
if self._initial_pid != current_pid:
|
|
|
|
LOG.warn("Process forked after connection established! "
|
2014-12-04 16:04:37 -05:00
|
|
|
"This can result in unpredictable behavior. "
|
2014-11-27 14:46:52 +01:00
|
|
|
"See: http://docs.openstack.org/developer/"
|
2015-01-02 14:24:57 -05:00
|
|
|
"oslo_messaging/transport.html")
|
2014-11-27 14:46:52 +01:00
|
|
|
self._initial_pid = current_pid
|
|
|
|
|
2014-02-21 11:50:45 +01:00
|
|
|
if retry is None:
|
|
|
|
retry = self.max_retries
|
|
|
|
if retry is None or retry < 0:
|
2014-11-17 16:46:46 +01:00
|
|
|
retry = None
|
2014-02-21 11:50:45 +01:00
|
|
|
|
2014-11-17 16:46:46 +01:00
|
|
|
def on_error(exc, interval):
|
2015-03-18 07:59:19 +01:00
|
|
|
LOG.debug(_("Received recoverable error from kombu:"),
|
|
|
|
exc_info=True)
|
|
|
|
|
|
|
|
recoverable_error_callback and recoverable_error_callback(exc)
|
2014-02-21 11:50:45 +01:00
|
|
|
|
2015-01-28 08:57:21 +01:00
|
|
|
interval = (self.driver_conf.kombu_reconnect_delay + interval
|
|
|
|
if self.driver_conf.kombu_reconnect_delay > 0
|
|
|
|
else interval)
|
2014-12-01 23:27:46 +01:00
|
|
|
|
2014-12-08 16:48:36 +01:00
|
|
|
info = {'err_str': exc, 'sleep_time': interval}
|
|
|
|
info.update(self.connection.info())
|
2014-11-17 16:46:46 +01:00
|
|
|
|
|
|
|
if 'Socket closed' in six.text_type(exc):
|
2015-05-06 11:11:03 +02:00
|
|
|
LOG.error(_LE('AMQP server %(hostname)s:%(port)s closed'
|
2015-03-18 07:59:19 +01:00
|
|
|
' the connection. Check login credentials:'
|
|
|
|
' %(err_str)s'), info)
|
2014-02-21 11:50:45 +01:00
|
|
|
else:
|
2015-05-06 11:11:03 +02:00
|
|
|
LOG.error(_LE('AMQP server on %(hostname)s:%(port)s is '
|
2015-03-18 07:59:19 +01:00
|
|
|
'unreachable: %(err_str)s. Trying again in '
|
|
|
|
'%(sleep_time)d seconds.'), info)
|
2014-11-17 16:46:46 +01:00
|
|
|
|
|
|
|
# XXX(nic): when reconnecting to a RabbitMQ cluster
|
|
|
|
# with mirrored queues in use, the attempt to release the
|
|
|
|
# connection can hang "indefinitely" somewhere deep down
|
|
|
|
# in Kombu. Blocking the thread for a bit prior to
|
|
|
|
# release seems to kludge around the problem where it is
|
|
|
|
# otherwise reproduceable.
|
|
|
|
# TODO(sileht): Check if this is useful since we
|
|
|
|
# use kombu for HA connection, the interval_step
|
|
|
|
# should sufficient, because the underlying kombu transport
|
|
|
|
# connection object freed.
|
2015-01-28 08:57:21 +01:00
|
|
|
if self.driver_conf.kombu_reconnect_delay > 0:
|
|
|
|
time.sleep(self.driver_conf.kombu_reconnect_delay)
|
2014-11-17 16:46:46 +01:00
|
|
|
|
2014-12-01 23:27:46 +01:00
|
|
|
def on_reconnection(new_channel):
|
|
|
|
"""Callback invoked when the kombu reconnects and creates
|
|
|
|
a new channel, we use it the reconfigure our consumers.
|
|
|
|
"""
|
2015-01-06 15:11:55 +01:00
|
|
|
self._set_current_channel(new_channel)
|
2015-05-01 00:09:18 +02:00
|
|
|
for consumer in self._consumers:
|
2015-05-01 00:24:46 +02:00
|
|
|
consumer.declare(self)
|
2014-12-01 23:27:46 +01:00
|
|
|
|
2014-12-02 15:48:34 +01:00
|
|
|
LOG.info(_LI('Reconnected to AMQP server on '
|
2015-05-06 11:11:03 +02:00
|
|
|
'%(hostname)s:%(port)s'),
|
|
|
|
self.connection.info())
|
2014-12-02 15:48:34 +01:00
|
|
|
|
2015-01-06 15:11:55 +01:00
|
|
|
def execute_method(channel):
|
|
|
|
self._set_current_channel(channel)
|
|
|
|
method()
|
|
|
|
|
2015-04-30 23:33:39 +02:00
|
|
|
# NOTE(sileht): Some dummy driver like the in-memory one doesn't
|
|
|
|
# have notion of recoverable connection, so we must raise the original
|
|
|
|
# exception like kombu does in this case.
|
|
|
|
has_modern_errors = hasattr(
|
|
|
|
self.connection.transport, 'recoverable_connection_errors',
|
|
|
|
)
|
|
|
|
if has_modern_errors:
|
|
|
|
recoverable_errors = (
|
|
|
|
self.connection.recoverable_channel_errors +
|
|
|
|
self.connection.recoverable_connection_errors)
|
|
|
|
else:
|
|
|
|
recoverable_errors = ()
|
2015-01-21 09:13:10 +01:00
|
|
|
|
2014-11-17 16:46:46 +01:00
|
|
|
try:
|
|
|
|
autoretry_method = self.connection.autoretry(
|
2015-01-06 15:11:55 +01:00
|
|
|
execute_method, channel=self.channel,
|
2014-11-17 16:46:46 +01:00
|
|
|
max_retries=retry,
|
|
|
|
errback=on_error,
|
|
|
|
interval_start=self.interval_start or 1,
|
|
|
|
interval_step=self.interval_stepping,
|
2014-12-01 23:27:46 +01:00
|
|
|
on_revive=on_reconnection,
|
2014-11-17 16:46:46 +01:00
|
|
|
)
|
|
|
|
ret, channel = autoretry_method()
|
2015-01-06 15:11:55 +01:00
|
|
|
self._set_current_channel(channel)
|
2014-11-17 16:46:46 +01:00
|
|
|
return ret
|
|
|
|
except recoverable_errors as exc:
|
2015-03-18 07:59:19 +01:00
|
|
|
LOG.debug(_("Received recoverable error from kombu:"),
|
|
|
|
exc_info=True)
|
|
|
|
error_callback and error_callback(exc)
|
2015-01-06 15:11:55 +01:00
|
|
|
self._set_current_channel(None)
|
2014-11-17 16:46:46 +01:00
|
|
|
# NOTE(sileht): number of retry exceeded and the connection
|
|
|
|
# is still broken
|
2015-05-06 11:11:03 +02:00
|
|
|
info = {'err_str': exc, 'retry': retry}
|
|
|
|
info.update(self.connection.info())
|
2014-11-17 16:46:46 +01:00
|
|
|
msg = _('Unable to connect to AMQP server on '
|
2015-05-06 11:11:03 +02:00
|
|
|
'%(hostname)s:%(port)s after %(retry)s '
|
|
|
|
'tries: %(err_str)s') % info
|
2014-11-17 16:46:46 +01:00
|
|
|
LOG.error(msg)
|
|
|
|
raise exceptions.MessageDeliveryFailure(msg)
|
2015-03-18 07:59:19 +01:00
|
|
|
except Exception as exc:
|
|
|
|
error_callback and error_callback(exc)
|
|
|
|
raise
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-01-06 15:11:55 +01:00
|
|
|
def _set_current_channel(self, new_channel):
|
2015-01-21 09:13:10 +01:00
|
|
|
"""Change the channel to use.
|
|
|
|
|
|
|
|
NOTE(sileht): Must be called within the connection lock
|
|
|
|
"""
|
2015-01-06 15:11:55 +01:00
|
|
|
if self.channel is not None and new_channel != self.channel:
|
2015-04-30 23:33:39 +02:00
|
|
|
DeclareQueuePublisher.reset_cache(self.channel)
|
2015-01-06 15:11:55 +01:00
|
|
|
self.connection.maybe_close_channel(self.channel)
|
|
|
|
self.channel = new_channel
|
|
|
|
|
2013-07-23 16:28:15 +01:00
|
|
|
def close(self):
|
|
|
|
"""Close/release this connection."""
|
2015-01-21 09:13:10 +01:00
|
|
|
self._heartbeat_stop()
|
2014-02-21 11:50:45 +01:00
|
|
|
if self.connection:
|
2015-01-06 15:11:55 +01:00
|
|
|
self._set_current_channel(None)
|
2014-02-21 11:50:45 +01:00
|
|
|
self.connection.release()
|
|
|
|
self.connection = None
|
2013-07-23 16:28:15 +01:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
"""Reset a connection so it can be used again."""
|
2015-03-18 08:31:32 +01:00
|
|
|
recoverable_errors = (self.connection.recoverable_channel_errors +
|
|
|
|
self.connection.recoverable_connection_errors)
|
|
|
|
|
2015-01-21 09:13:10 +01:00
|
|
|
with self._connection_lock:
|
2015-03-18 08:31:32 +01:00
|
|
|
try:
|
|
|
|
self._set_current_channel(self.connection.channel())
|
|
|
|
except recoverable_errors:
|
|
|
|
self._set_current_channel(None)
|
|
|
|
self.ensure_connection()
|
2015-05-01 00:09:18 +02:00
|
|
|
self._consumers = []
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-01-21 09:13:10 +01:00
|
|
|
def _heartbeat_supported_and_enabled(self):
|
|
|
|
if self.driver_conf.heartbeat_timeout_threshold <= 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.connection.supports_heartbeats:
|
|
|
|
return True
|
|
|
|
elif not self._heartbeat_support_log_emitted:
|
|
|
|
LOG.warn(_LW("Heartbeat support requested but it is not supported "
|
|
|
|
"by the kombu driver or the broker"))
|
|
|
|
self._heartbeat_support_log_emitted = True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _heartbeat_start(self):
|
|
|
|
if self._heartbeat_supported_and_enabled():
|
|
|
|
self._heartbeat_exit_event = threading.Event()
|
|
|
|
self._heartbeat_thread = threading.Thread(
|
|
|
|
target=self._heartbeat_thread_job)
|
|
|
|
self._heartbeat_thread.daemon = True
|
|
|
|
self._heartbeat_thread.start()
|
|
|
|
else:
|
|
|
|
self._heartbeat_thread = None
|
|
|
|
|
|
|
|
def _heartbeat_stop(self):
|
|
|
|
if self._heartbeat_thread is not None:
|
|
|
|
self._heartbeat_exit_event.set()
|
|
|
|
self._heartbeat_thread.join()
|
|
|
|
self._heartbeat_thread = None
|
|
|
|
|
|
|
|
def _heartbeat_thread_job(self):
|
|
|
|
"""Thread that maintains inactive connections
|
|
|
|
"""
|
|
|
|
while not self._heartbeat_exit_event.is_set():
|
|
|
|
with self._connection_lock.for_heartbeat():
|
|
|
|
|
|
|
|
recoverable_errors = (
|
|
|
|
self.connection.recoverable_channel_errors +
|
|
|
|
self.connection.recoverable_connection_errors)
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
self.connection.heartbeat_check(
|
|
|
|
rate=self.driver_conf.heartbeat_rate)
|
|
|
|
# NOTE(sileht): We need to drain event to receive
|
|
|
|
# heartbeat from the broker but don't hold the
|
|
|
|
# connection too much times. In amqpdriver a connection
|
|
|
|
# is used exclusivly for read or for write, so we have
|
|
|
|
# to do this for connection used for write drain_events
|
|
|
|
# already do that for other connection
|
|
|
|
try:
|
|
|
|
self.connection.drain_events(timeout=0.001)
|
|
|
|
except socket.timeout:
|
|
|
|
pass
|
|
|
|
except recoverable_errors as exc:
|
|
|
|
LOG.info(_LI("A recoverable connection/channel error "
|
2015-03-18 15:31:41 +00:00
|
|
|
"occurred, trying to reconnect: %s"), exc)
|
2015-03-19 09:33:00 +01:00
|
|
|
self.ensure_connection()
|
2015-01-21 09:13:10 +01:00
|
|
|
except Exception:
|
2015-03-18 14:19:47 -04:00
|
|
|
LOG.warning(_LW("Unexpected error during heartbeart "
|
|
|
|
"thread processing, retrying..."))
|
|
|
|
LOG.debug('Exception', exc_info=True)
|
2015-01-21 09:13:10 +01:00
|
|
|
|
|
|
|
self._heartbeat_exit_event.wait(
|
|
|
|
timeout=self._heartbeat_wait_timeout)
|
|
|
|
self._heartbeat_exit_event.clear()
|
|
|
|
|
2015-04-30 17:46:42 +02:00
|
|
|
def declare_consumer(self, consumer):
|
2013-07-23 16:28:15 +01:00
|
|
|
"""Create a Consumer using the class that was passed in and
|
|
|
|
add it to our list of consumers
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _connect_error(exc):
|
2015-04-30 17:46:42 +02:00
|
|
|
log_info = {'topic': consumer.routing_key, 'err_str': exc}
|
2013-07-23 16:28:15 +01:00
|
|
|
LOG.error(_("Failed to declare consumer for topic '%(topic)s': "
|
2014-05-20 14:50:23 +02:00
|
|
|
"%(err_str)s"), log_info)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-01-06 15:11:55 +01:00
|
|
|
def _declare_consumer():
|
2015-05-01 00:24:46 +02:00
|
|
|
consumer.declare(self)
|
2015-05-01 00:09:18 +02:00
|
|
|
self._consumers.append(consumer)
|
|
|
|
self._new_consumers.append(consumer)
|
2013-07-23 16:28:15 +01:00
|
|
|
return consumer
|
|
|
|
|
2015-01-21 09:13:10 +01:00
|
|
|
with self._connection_lock:
|
2015-03-18 07:59:19 +01:00
|
|
|
return self.ensure(_declare_consumer,
|
|
|
|
error_callback=_connect_error)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 08:56:20 +02:00
|
|
|
def consume(self, timeout=None):
|
|
|
|
"""Consume from all queues/consumers."""
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2014-12-26 20:22:21 +08:00
|
|
|
timer = rpc_common.DecayingTimer(duration=timeout)
|
|
|
|
timer.start()
|
2014-12-01 23:27:46 +01:00
|
|
|
|
2014-12-08 10:56:52 +01:00
|
|
|
def _raise_timeout(exc):
|
|
|
|
LOG.debug('Timed out waiting for RPC response: %s', exc)
|
|
|
|
raise rpc_common.Timeout()
|
2014-12-01 23:27:46 +01:00
|
|
|
|
2015-03-18 07:59:19 +01:00
|
|
|
def _recoverable_error_callback(exc):
|
2015-05-01 00:09:18 +02:00
|
|
|
self._new_consumers = self._consumers
|
2014-12-08 10:56:52 +01:00
|
|
|
timer.check_return(_raise_timeout, exc)
|
2015-03-18 07:59:19 +01:00
|
|
|
|
|
|
|
def _error_callback(exc):
|
|
|
|
_recoverable_error_callback(exc)
|
2015-03-18 14:19:47 -04:00
|
|
|
LOG.error(_('Failed to consume message from queue: %s'),
|
|
|
|
exc)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-01-06 15:11:55 +01:00
|
|
|
def _consume():
|
2015-05-05 10:29:22 +02:00
|
|
|
# NOTE(sileht): in case the acknowledgement or requeue of a
|
|
|
|
# message fail, the kombu transport can be disconnected
|
|
|
|
# In this case, we must redeclare our consumers, so raise
|
|
|
|
# a recoverable error to trigger the reconnection code.
|
|
|
|
if not self.connection.connected:
|
|
|
|
raise self.connection.recoverable_connection_errors[0]
|
|
|
|
|
2015-05-01 00:09:18 +02:00
|
|
|
if self._new_consumers:
|
|
|
|
for tag, consumer in enumerate(self._consumers):
|
|
|
|
if consumer in self._new_consumers:
|
|
|
|
consumer.consume(tag=tag)
|
|
|
|
self._new_consumers = []
|
2014-12-08 10:56:52 +01:00
|
|
|
|
2015-01-21 10:45:50 +01:00
|
|
|
poll_timeout = (self._poll_timeout if timeout is None
|
|
|
|
else min(timeout, self._poll_timeout))
|
2014-12-01 23:27:46 +01:00
|
|
|
while True:
|
2014-12-08 10:52:45 +01:00
|
|
|
if self._consume_loop_stopped:
|
2015-04-30 08:56:20 +02:00
|
|
|
return
|
2014-12-08 10:52:45 +01:00
|
|
|
|
2015-01-21 09:13:10 +01:00
|
|
|
if self._heartbeat_supported_and_enabled():
|
|
|
|
self.connection.heartbeat_check(
|
|
|
|
rate=self.driver_conf.heartbeat_rate)
|
2014-12-01 23:27:46 +01:00
|
|
|
try:
|
2015-04-30 08:56:20 +02:00
|
|
|
self.connection.drain_events(timeout=poll_timeout)
|
|
|
|
return
|
2014-12-01 23:27:46 +01:00
|
|
|
except socket.timeout as exc:
|
2015-01-21 10:45:50 +01:00
|
|
|
poll_timeout = timer.check_return(
|
|
|
|
_raise_timeout, exc, maximum=self._poll_timeout)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-04-30 08:56:20 +02:00
|
|
|
with self._connection_lock:
|
|
|
|
self.ensure(_consume,
|
|
|
|
recoverable_error_callback=_recoverable_error_callback,
|
|
|
|
error_callback=_error_callback)
|
2015-03-18 07:59:19 +01:00
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
def publisher_send(self, publisher, msg, timeout=None, retry=None):
|
2013-07-23 16:28:15 +01:00
|
|
|
"""Send to a publisher based on the publisher class."""
|
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
def _error_callback(exc):
|
|
|
|
log_info = {'topic': publisher.exchange_name, 'err_str': exc}
|
|
|
|
LOG.error(_("Failed to publish message to topic "
|
|
|
|
"'%(topic)s': %(err_str)s"), log_info)
|
|
|
|
LOG.debug('Exception', exc_info=exc)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-01-06 15:11:55 +01:00
|
|
|
def _publish():
|
2015-04-30 16:13:14 +02:00
|
|
|
publisher.send(self, msg, timeout)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2015-01-21 09:13:10 +01:00
|
|
|
with self._connection_lock:
|
2015-04-30 16:13:14 +02:00
|
|
|
self.ensure(_publish, retry=retry, error_callback=_error_callback)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
|
|
|
def declare_direct_consumer(self, topic, callback):
|
|
|
|
"""Create a 'direct' queue.
|
|
|
|
In nova's use, this is generally a msg_id queue used for
|
|
|
|
responses for call/multicall
|
|
|
|
"""
|
2015-04-30 17:46:42 +02:00
|
|
|
|
|
|
|
consumer = Consumer(self.driver_conf,
|
|
|
|
exchange_name=topic,
|
|
|
|
queue_name=topic,
|
|
|
|
routing_key=topic,
|
|
|
|
type='direct',
|
|
|
|
durable=False,
|
|
|
|
auto_delete=True,
|
|
|
|
callback=callback)
|
|
|
|
|
|
|
|
self.declare_consumer(consumer)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2014-04-24 12:04:20 +02:00
|
|
|
def declare_topic_consumer(self, exchange_name, topic, callback=None,
|
|
|
|
queue_name=None):
|
2013-07-23 16:28:15 +01:00
|
|
|
"""Create a 'topic' consumer."""
|
2015-04-30 17:46:42 +02:00
|
|
|
consumer = Consumer(self.driver_conf,
|
|
|
|
exchange_name=exchange_name,
|
|
|
|
queue_name=queue_name or topic,
|
|
|
|
routing_key=topic,
|
|
|
|
type='topic',
|
|
|
|
durable=self.driver_conf.amqp_durable_queues,
|
|
|
|
auto_delete=self.driver_conf.amqp_auto_delete,
|
|
|
|
callback=callback)
|
|
|
|
|
|
|
|
self.declare_consumer(consumer)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
|
|
|
def declare_fanout_consumer(self, topic, callback):
|
|
|
|
"""Create a 'fanout' consumer."""
|
2015-04-30 17:46:42 +02:00
|
|
|
|
|
|
|
unique = uuid.uuid4().hex
|
|
|
|
exchange_name = '%s_fanout' % topic
|
|
|
|
queue_name = '%s_fanout_%s' % (topic, unique)
|
|
|
|
|
|
|
|
consumer = Consumer(self.driver_conf,
|
|
|
|
exchange_name=exchange_name,
|
|
|
|
queue_name=queue_name,
|
|
|
|
routing_key=topic,
|
|
|
|
type='fanout',
|
|
|
|
durable=False,
|
|
|
|
auto_delete=True,
|
|
|
|
callback=callback,
|
|
|
|
nowait=False)
|
|
|
|
|
|
|
|
self.declare_consumer(consumer)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
|
|
|
def direct_send(self, msg_id, msg):
|
|
|
|
"""Send a 'direct' message."""
|
2014-07-22 09:42:52 -07:00
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
p = RetryOnMissingExchangePublisher(self.driver_conf,
|
|
|
|
exchange_name=msg_id,
|
|
|
|
routing_key=msg_id,
|
|
|
|
type='direct',
|
|
|
|
durable=False,
|
|
|
|
auto_delete=True)
|
2014-07-22 09:42:52 -07:00
|
|
|
|
2015-04-30 16:13:14 +02:00
|
|
|
self.publisher_send(p, msg)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2014-02-21 11:50:45 +01:00
|
|
|
def topic_send(self, exchange_name, topic, msg, timeout=None, retry=None):
|
2013-07-23 16:28:15 +01:00
|
|
|
"""Send a 'topic' message."""
|
2015-04-30 16:13:14 +02:00
|
|
|
p = Publisher(self.driver_conf,
|
|
|
|
exchange_name=exchange_name,
|
|
|
|
routing_key=topic,
|
|
|
|
type='topic',
|
|
|
|
durable=self.driver_conf.amqp_durable_queues,
|
|
|
|
auto_delete=self.driver_conf.amqp_auto_delete)
|
|
|
|
self.publisher_send(p, msg, timeout, retry=retry)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2014-02-21 11:50:45 +01:00
|
|
|
def fanout_send(self, topic, msg, retry=None):
|
2013-07-23 16:28:15 +01:00
|
|
|
"""Send a 'fanout' message."""
|
2015-04-30 16:13:14 +02:00
|
|
|
|
|
|
|
p = Publisher(self.driver_conf,
|
|
|
|
exchange_name='%s_fanout' % topic,
|
|
|
|
routing_key=None,
|
|
|
|
type='fanout',
|
|
|
|
durable=False,
|
|
|
|
auto_delete=True)
|
|
|
|
|
|
|
|
self.publisher_send(p, msg, retry=retry)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2014-05-06 13:47:12 +02:00
|
|
|
def notify_send(self, exchange_name, topic, msg, retry=None, **kwargs):
|
2013-07-23 16:28:15 +01:00
|
|
|
"""Send a notify message on a topic."""
|
2015-04-30 16:13:14 +02:00
|
|
|
p = DeclareQueuePublisher(
|
|
|
|
self.driver_conf,
|
|
|
|
exchange_name=exchange_name,
|
|
|
|
routing_key=topic,
|
|
|
|
type='topic',
|
|
|
|
durable=self.driver_conf.amqp_durable_queues,
|
|
|
|
auto_delete=self.driver_conf.amqp_auto_delete)
|
|
|
|
|
|
|
|
self.publisher_send(p, msg, timeout=None, retry=retry)
|
2013-07-23 16:28:15 +01:00
|
|
|
|
2014-12-08 10:52:45 +01:00
|
|
|
def stop_consuming(self):
|
|
|
|
self._consume_loop_stopped = True
|
|
|
|
|
2013-07-23 18:14:17 +01:00
|
|
|
|
2013-07-29 07:20:01 +01:00
|
|
|
class RabbitDriver(amqpdriver.AMQPDriverBase):
|
2013-07-23 18:14:17 +01:00
|
|
|
|
2014-02-21 11:50:45 +01:00
|
|
|
def __init__(self, conf, url,
|
|
|
|
default_exchange=None,
|
2014-06-07 12:06:08 +08:00
|
|
|
allowed_remote_exmods=None):
|
2015-01-28 08:57:21 +01:00
|
|
|
opt_group = cfg.OptGroup(name='oslo_messaging_rabbit',
|
|
|
|
title='RabbitMQ driver options')
|
|
|
|
conf.register_group(opt_group)
|
|
|
|
conf.register_opts(rabbit_opts, group=opt_group)
|
|
|
|
conf.register_opts(rpc_amqp.amqp_opts, group=opt_group)
|
2015-04-23 17:08:24 +01:00
|
|
|
conf.register_opts(base.base_opts, group=opt_group)
|
2015-01-28 08:57:21 +01:00
|
|
|
|
|
|
|
connection_pool = rpc_amqp.ConnectionPool(
|
|
|
|
conf, conf.oslo_messaging_rabbit.rpc_conn_pool_size,
|
|
|
|
url, Connection)
|
2013-07-23 18:14:17 +01:00
|
|
|
|
Add a TransportURL class to the public API
Nova's cells/rpc_driver.py has some code which allows user of the REST
API to update elements of a cell's transport URL (say, the host name of
the message broker) stored in the database. To achieve this, it has
a parse_transport_url() method which breaks the URL into its constituent
parts and an unparse_transport_url() which re-forms it again after
updating some of its parts.
This is all fine and, since it's fairly specialized, it wouldn't be a
big deal to leave this code in Nova for now ... except the unparse
method looks at CONF.rpc_backend to know what scheme to use in the
returned URL if now backend was specified.
oslo.messaging registers the rpc_backend option, but the ability to
reference any option registered by the library should not be relied upon
by users of the library. Imagine, for instance, if we renamed the option
in future (with backwards compat for old configurations), then this
would mean API breakage.
So, long story short - an API along these lines makes some sense, but
especially since not having it would mean we'd need to add some way to
query the name of the transport driver.
In this commit, we add a simple new TransportURL class:
>>> url = messaging.TransportURL.parse(cfg.CONF, 'foo:///')
>>> str(url), url
('foo:///', <TransportURL transport='foo'>)
>>> url.hosts.append(messaging.TransportHost(hostname='localhost'))
>>> str(url), url
('foo://localhost/', <TransportURL transport='foo', hosts=[<TransportHost hostname='localhost'>]>)
>>> url.transport = None
>>> str(url), url
('kombu://localhost/', <TransportURL transport='kombu', hosts=[<TransportHost hostname='localhost'>]>)
>>> cfg.CONF.set_override('rpc_backend', 'bar')
>>> str(url), url
('bar://localhost/', <TransportURL transport='bar', hosts=[<TransportHost hostname='localhost'>]>)
The TransportURL.parse() method equates to parse_transport_url() and
TransportURL.__str__() equates to unparse_transport().
The transport drivers are also updated to take a TransportURL as a
required argument, which simplifies the handling of transport URLs in
the drivers.
Change-Id: Ic04173476329858e4a2c2d2707e9d4aeb212d127
2013-08-12 17:16:44 +01:00
|
|
|
super(RabbitDriver, self).__init__(conf, url,
|
|
|
|
connection_pool,
|
|
|
|
default_exchange,
|
2013-08-07 13:07:05 +01:00
|
|
|
allowed_remote_exmods)
|
2014-03-03 07:35:29 -08:00
|
|
|
|
|
|
|
def require_features(self, requeue=True):
|
|
|
|
pass
|