2015-06-06 06:05:29 +00:00
|
|
|
# Copyright (C) 2015 Cisco Systems, Inc.
|
|
|
|
#
|
|
|
|
# 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.
|
2016-06-21 11:57:17 +00:00
|
|
|
|
|
|
|
# Following code fixes 2 issues with kafka-python and
|
|
|
|
# The current release of eventlet (0.19.0) does not actually remove
|
|
|
|
# select.poll [1]. Because of kafka-python.selectors34 selects
|
|
|
|
# PollSelector instead of SelectSelector [2]. PollSelector relies on
|
|
|
|
# select.poll, which does not work when eventlet/greenlet is used. This
|
|
|
|
# bug in evenlet is fixed in the master branch [3], but there's no
|
|
|
|
# release of eventlet that includes this fix at this point.
|
|
|
|
|
|
|
|
import json
|
2015-06-06 06:05:29 +00:00
|
|
|
import threading
|
|
|
|
|
2016-06-21 11:57:17 +00:00
|
|
|
import kafka
|
|
|
|
from kafka.client_async import selectors
|
|
|
|
import kafka.errors
|
|
|
|
from oslo_log import log as logging
|
|
|
|
from oslo_utils import eventletutils
|
|
|
|
import tenacity
|
|
|
|
|
2015-06-06 06:05:29 +00:00
|
|
|
from oslo_messaging._drivers import base
|
|
|
|
from oslo_messaging._drivers import common as driver_common
|
2016-06-21 11:57:17 +00:00
|
|
|
from oslo_messaging._drivers import kafka_options
|
2015-06-06 06:05:29 +00:00
|
|
|
from oslo_messaging._drivers import pool as driver_pool
|
|
|
|
from oslo_messaging._i18n import _LE
|
|
|
|
from oslo_messaging._i18n import _LW
|
|
|
|
from oslo_serialization import jsonutils
|
|
|
|
|
2016-12-08 08:58:20 +00:00
|
|
|
import logging as l
|
|
|
|
l.basicConfig(level=l.INFO)
|
|
|
|
l.getLogger("kafka").setLevel(l.WARN)
|
|
|
|
l.getLogger("stevedore").setLevel(l.WARN)
|
|
|
|
|
2016-06-21 11:57:17 +00:00
|
|
|
if eventletutils.is_monkey_patched('select'):
|
|
|
|
# monkeypatch the vendored SelectSelector._select like eventlet does
|
|
|
|
# https://github.com/eventlet/eventlet/blob/master/eventlet/green/selectors.py#L32
|
|
|
|
from eventlet.green import select
|
|
|
|
selectors.SelectSelector._select = staticmethod(select.select)
|
2015-06-06 06:05:29 +00:00
|
|
|
|
2016-06-21 11:57:17 +00:00
|
|
|
# Force to use the select selectors
|
|
|
|
KAFKA_SELECTOR = selectors.SelectSelector
|
|
|
|
else:
|
|
|
|
KAFKA_SELECTOR = selectors.DefaultSelector
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-06-21 11:57:17 +00:00
|
|
|
def unpack_message(msg):
|
|
|
|
context = {}
|
|
|
|
message = None
|
2016-12-08 08:58:20 +00:00
|
|
|
msg = json.loads(msg)
|
|
|
|
message = driver_common.deserialize_msg(msg)
|
|
|
|
context = message['_context']
|
|
|
|
del message['_context']
|
|
|
|
return context, message
|
2016-06-21 11:57:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pack_message(ctxt, msg):
|
2015-06-06 06:05:29 +00:00
|
|
|
"""Pack context into msg."""
|
2016-06-21 11:57:17 +00:00
|
|
|
|
2015-06-06 06:05:29 +00:00
|
|
|
if isinstance(ctxt, dict):
|
|
|
|
context_d = ctxt
|
|
|
|
else:
|
|
|
|
context_d = ctxt.to_dict()
|
2016-12-08 08:58:20 +00:00
|
|
|
msg['_context'] = context_d
|
2016-06-21 11:57:17 +00:00
|
|
|
|
|
|
|
msg = driver_common.serialize_msg(msg)
|
2015-06-06 06:05:29 +00:00
|
|
|
|
2016-06-21 11:57:17 +00:00
|
|
|
return msg
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
|
2016-02-26 14:00:58 +00:00
|
|
|
def target_to_topic(target, priority=None):
|
2015-06-06 06:05:29 +00:00
|
|
|
"""Convert target into topic string
|
|
|
|
|
|
|
|
:param target: Message destination target
|
|
|
|
:type target: oslo_messaging.Target
|
2016-02-26 14:00:58 +00:00
|
|
|
:param priority: Notification priority
|
|
|
|
:type priority: string
|
2015-06-06 06:05:29 +00:00
|
|
|
"""
|
2016-02-26 14:00:58 +00:00
|
|
|
if not priority:
|
2015-06-06 06:05:29 +00:00
|
|
|
return target.topic
|
2016-02-26 14:00:58 +00:00
|
|
|
return target.topic + '.' + priority
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
|
2016-06-21 11:57:17 +00:00
|
|
|
def retry_on_retriable_kafka_error(exc):
|
|
|
|
return (isinstance(exc, kafka.errors.KafkaError) and exc.retriable)
|
|
|
|
|
|
|
|
|
|
|
|
def with_reconnect(retries=None):
|
|
|
|
def decorator(func):
|
|
|
|
@tenacity.retry(
|
|
|
|
retry=tenacity.retry_if_exception(retry_on_retriable_kafka_error),
|
|
|
|
wait=tenacity.wait_fixed(1),
|
|
|
|
stop=tenacity.stop_after_attempt(retries),
|
|
|
|
reraise=True
|
|
|
|
)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2015-06-06 06:05:29 +00:00
|
|
|
class Connection(object):
|
|
|
|
|
|
|
|
def __init__(self, conf, url, purpose):
|
|
|
|
|
2016-06-21 11:57:17 +00:00
|
|
|
self.client = None
|
2015-06-06 06:05:29 +00:00
|
|
|
driver_conf = conf.oslo_messaging_kafka
|
2016-06-21 11:57:17 +00:00
|
|
|
self.batch_size = driver_conf.producer_batch_size
|
|
|
|
self.linger_ms = driver_conf.producer_batch_timeout * 1000
|
2015-06-06 06:05:29 +00:00
|
|
|
self.conf = conf
|
|
|
|
self.producer = None
|
2016-12-07 17:28:55 +00:00
|
|
|
self.producer_lock = threading.Lock()
|
2015-06-06 06:05:29 +00:00
|
|
|
self.consumer = None
|
|
|
|
self.consumer_timeout = float(driver_conf.kafka_consumer_timeout)
|
2016-06-21 11:57:17 +00:00
|
|
|
self.max_fetch_bytes = driver_conf.kafka_max_fetch_bytes
|
|
|
|
self.group_id = driver_conf.consumer_group
|
2015-06-06 06:05:29 +00:00
|
|
|
self.url = url
|
|
|
|
self._parse_url()
|
|
|
|
# TODO(Support for manual/auto_commit functionality)
|
|
|
|
# When auto_commit is False, consumer can manually notify
|
|
|
|
# the completion of the subscription.
|
|
|
|
# Currently we don't support for non auto commit option
|
|
|
|
self.auto_commit = True
|
|
|
|
self._consume_loop_stopped = False
|
|
|
|
|
|
|
|
def _parse_url(self):
|
|
|
|
driver_conf = self.conf.oslo_messaging_kafka
|
2016-04-14 16:45:32 +00:00
|
|
|
self.hostaddrs = []
|
2015-06-06 06:05:29 +00:00
|
|
|
|
2016-04-14 16:45:32 +00:00
|
|
|
for host in self.url.hosts:
|
|
|
|
if host.hostname:
|
|
|
|
self.hostaddrs.append("%s:%s" % (
|
|
|
|
host.hostname,
|
|
|
|
host.port or driver_conf.kafka_default_port))
|
2015-06-06 06:05:29 +00:00
|
|
|
|
2016-04-14 16:45:32 +00:00
|
|
|
if not self.hostaddrs:
|
|
|
|
self.hostaddrs.append("%s:%s" % (driver_conf.kafka_default_host,
|
|
|
|
driver_conf.kafka_default_port))
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
def notify_send(self, topic, ctxt, msg, retry):
|
|
|
|
"""Send messages to Kafka broker.
|
|
|
|
|
|
|
|
:param topic: String of the topic
|
|
|
|
:param ctxt: context for the messages
|
|
|
|
:param msg: messages for publishing
|
|
|
|
:param retry: the number of retry
|
|
|
|
"""
|
2016-06-21 11:57:17 +00:00
|
|
|
retry = retry if retry >= 0 else None
|
2016-12-07 17:28:55 +00:00
|
|
|
message = pack_message(ctxt, msg)
|
|
|
|
message = jsonutils.dumps(message)
|
2016-06-21 11:57:17 +00:00
|
|
|
|
|
|
|
@with_reconnect(retries=retry)
|
2016-12-07 17:28:55 +00:00
|
|
|
def wrapped_with_reconnect():
|
|
|
|
self._ensure_producer()
|
|
|
|
# NOTE(sileht): This returns a future, we can use get()
|
|
|
|
# if we want to block like other driver
|
2016-06-21 11:57:17 +00:00
|
|
|
self.producer.send(topic, message)
|
|
|
|
|
|
|
|
try:
|
2016-12-07 17:28:55 +00:00
|
|
|
wrapped_with_reconnect()
|
2016-06-21 11:57:17 +00:00
|
|
|
except Exception:
|
2016-12-07 17:28:55 +00:00
|
|
|
# NOTE(sileht): if something goes wrong close the producer
|
|
|
|
# connection
|
|
|
|
self._close_producer()
|
|
|
|
raise
|
2016-06-21 11:57:17 +00:00
|
|
|
|
|
|
|
@with_reconnect()
|
|
|
|
def _poll_messages(self, timeout):
|
2016-12-08 08:58:20 +00:00
|
|
|
messages = self.consumer.poll(timeout * 1000.0)
|
|
|
|
messages = [record.value
|
|
|
|
for records in messages.values() if records
|
|
|
|
for record in records]
|
|
|
|
if not messages:
|
|
|
|
# NOTE(sileht): really ? you return payload but no messages...
|
|
|
|
# simulate timeout to consume message again
|
|
|
|
raise kafka.errors.ConsumerTimeout()
|
|
|
|
return messages
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
def consume(self, timeout=None):
|
2016-02-23 06:15:18 +00:00
|
|
|
"""Receive up to 'max_fetch_messages' messages.
|
2015-06-06 06:05:29 +00:00
|
|
|
|
2016-02-23 06:15:18 +00:00
|
|
|
:param timeout: poll timeout in seconds
|
2015-06-06 06:05:29 +00:00
|
|
|
"""
|
2016-06-21 11:57:17 +00:00
|
|
|
if self._consume_loop_stopped:
|
|
|
|
return None
|
2015-06-06 06:05:29 +00:00
|
|
|
|
2016-06-21 11:57:17 +00:00
|
|
|
timeout = timeout if timeout >= 0 else self.consumer_timeout
|
|
|
|
try:
|
|
|
|
messages = self._poll_messages(timeout)
|
|
|
|
except kafka.errors.ConsumerTimeout as e:
|
|
|
|
raise driver_common.Timeout(e.message)
|
|
|
|
except Exception:
|
|
|
|
LOG.exception(_LE("Failed to consume messages"))
|
|
|
|
messages = None
|
|
|
|
return messages
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
def stop_consuming(self):
|
|
|
|
self._consume_loop_stopped = True
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
"""Reset a connection so it can be used again."""
|
2016-06-21 11:57:17 +00:00
|
|
|
pass
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
def close(self):
|
2016-12-07 17:28:55 +00:00
|
|
|
self._close_producer()
|
2016-06-21 11:57:17 +00:00
|
|
|
if self.consumer:
|
|
|
|
self.consumer.close()
|
2016-12-07 17:28:55 +00:00
|
|
|
self.consumer = None
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
"""Commit is used by subscribers belonging to the same group.
|
|
|
|
After subscribing messages, commit is called to prevent
|
|
|
|
the other subscribers which belong to the same group
|
|
|
|
from re-subscribing the same messages.
|
|
|
|
|
|
|
|
Currently self.auto_commit option is always True,
|
|
|
|
so we don't need to call this function.
|
|
|
|
"""
|
|
|
|
self.consumer.commit()
|
|
|
|
|
2016-12-07 17:28:55 +00:00
|
|
|
def _close_producer(self):
|
|
|
|
with self.producer_lock:
|
|
|
|
if self.producer:
|
|
|
|
self.producer.close()
|
|
|
|
self.producer = None
|
|
|
|
|
|
|
|
def _ensure_producer(self):
|
|
|
|
if self.producer:
|
|
|
|
return
|
|
|
|
with self.producer_lock:
|
|
|
|
if self.producer:
|
|
|
|
return
|
|
|
|
self.producer = kafka.KafkaProducer(
|
|
|
|
bootstrap_servers=self.hostaddrs,
|
|
|
|
linger_ms=self.linger_ms,
|
|
|
|
batch_size=self.batch_size,
|
|
|
|
selector=KAFKA_SELECTOR)
|
2016-06-21 11:57:17 +00:00
|
|
|
|
|
|
|
@with_reconnect()
|
2015-06-06 06:05:29 +00:00
|
|
|
def declare_topic_consumer(self, topics, group=None):
|
|
|
|
self.consumer = kafka.KafkaConsumer(
|
2016-06-21 11:57:17 +00:00
|
|
|
*topics, group_id=(group or self.group_id),
|
2016-04-14 16:45:32 +00:00
|
|
|
bootstrap_servers=self.hostaddrs,
|
2016-06-21 11:57:17 +00:00
|
|
|
max_partition_fetch_bytes=self.max_fetch_bytes,
|
|
|
|
selector=KAFKA_SELECTOR
|
|
|
|
)
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
|
2016-02-01 15:15:46 +00:00
|
|
|
class OsloKafkaMessage(base.RpcIncomingMessage):
|
2015-06-06 06:05:29 +00:00
|
|
|
|
2016-02-01 15:15:46 +00:00
|
|
|
def __init__(self, ctxt, message):
|
|
|
|
super(OsloKafkaMessage, self).__init__(ctxt, message)
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
def requeue(self):
|
2015-12-26 08:18:56 +00:00
|
|
|
LOG.warning(_LW("requeue is not supported"))
|
2015-06-06 06:05:29 +00:00
|
|
|
|
2016-05-13 12:41:22 +00:00
|
|
|
def reply(self, reply=None, failure=None):
|
2015-12-26 08:18:56 +00:00
|
|
|
LOG.warning(_LW("reply is not supported"))
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
|
2016-04-02 11:58:29 +00:00
|
|
|
class KafkaListener(base.PollStyleListener):
|
2015-06-06 06:05:29 +00:00
|
|
|
|
2016-02-01 15:15:46 +00:00
|
|
|
def __init__(self, conn):
|
|
|
|
super(KafkaListener, self).__init__()
|
2015-06-06 06:05:29 +00:00
|
|
|
self._stopped = threading.Event()
|
|
|
|
self.conn = conn
|
|
|
|
self.incoming_queue = []
|
|
|
|
|
2015-08-02 08:26:02 +00:00
|
|
|
@base.batch_poll_helper
|
2015-06-06 06:05:29 +00:00
|
|
|
def poll(self, timeout=None):
|
|
|
|
while not self._stopped.is_set():
|
|
|
|
if self.incoming_queue:
|
|
|
|
return self.incoming_queue.pop(0)
|
|
|
|
try:
|
2016-12-08 08:58:20 +00:00
|
|
|
messages = self.conn.consume(timeout=timeout) or []
|
|
|
|
for message in messages:
|
|
|
|
msg = OsloKafkaMessage(*unpack_message(message))
|
|
|
|
self.incoming_queue.append(msg)
|
2015-06-06 06:05:29 +00:00
|
|
|
except driver_common.Timeout:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self._stopped.set()
|
|
|
|
self.conn.stop_consuming()
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
self.conn.close()
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
# TODO(Support for manually/auto commit functionality)
|
|
|
|
# It's better to allow users to commit manually and support for
|
|
|
|
# self.auto_commit = False option. For now, this commit function
|
|
|
|
# is meaningless since user couldn't call this function and
|
|
|
|
# auto_commit option is always True.
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
|
|
|
|
class KafkaDriver(base.BaseDriver):
|
|
|
|
"""Note: Current implementation of this driver is experimental.
|
|
|
|
We will have functional and/or integrated testing enabled for this driver.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, conf, url, default_exchange=None,
|
|
|
|
allowed_remote_exmods=None):
|
|
|
|
super(KafkaDriver, self).__init__(
|
|
|
|
conf, url, default_exchange, allowed_remote_exmods)
|
|
|
|
|
2016-12-07 18:30:40 +00:00
|
|
|
kafka_options.register_opts(conf)
|
2016-07-11 12:25:23 +00:00
|
|
|
# the pool configuration properties
|
|
|
|
max_size = self.conf.oslo_messaging_kafka.pool_size
|
|
|
|
min_size = self.conf.oslo_messaging_kafka.conn_pool_min_size
|
|
|
|
ttl = self.conf.oslo_messaging_kafka.conn_pool_ttl
|
|
|
|
|
2015-06-06 06:05:29 +00:00
|
|
|
self.connection_pool = driver_pool.ConnectionPool(
|
2016-07-11 12:25:23 +00:00
|
|
|
self.conf, max_size, min_size, ttl,
|
2015-06-06 06:05:29 +00:00
|
|
|
self._url, Connection)
|
|
|
|
self.listeners = []
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
for c in self.listeners:
|
|
|
|
c.close()
|
|
|
|
self.listeners = []
|
|
|
|
|
|
|
|
def send(self, target, ctxt, message, wait_for_reply=None, timeout=None,
|
|
|
|
retry=None):
|
|
|
|
raise NotImplementedError(
|
|
|
|
'The RPC implementation for Kafka is not implemented')
|
|
|
|
|
|
|
|
def send_notification(self, target, ctxt, message, version, retry=None):
|
|
|
|
"""Send notification to Kafka brokers
|
|
|
|
|
|
|
|
:param target: Message destination target
|
|
|
|
:type target: oslo_messaging.Target
|
|
|
|
:param ctxt: Message context
|
|
|
|
:type ctxt: dict
|
|
|
|
:param message: Message payload to pass
|
|
|
|
:type message: dict
|
|
|
|
:param version: Messaging API version (currently not used)
|
|
|
|
:type version: str
|
|
|
|
:param retry: an optional default kafka consumer retries configuration
|
|
|
|
None means to retry forever
|
|
|
|
0 means no retry
|
|
|
|
N means N retries
|
|
|
|
:type retry: int
|
|
|
|
"""
|
2016-06-21 11:57:17 +00:00
|
|
|
with self._get_connection(purpose=driver_common.PURPOSE_SEND) as conn:
|
2015-06-06 06:05:29 +00:00
|
|
|
conn.notify_send(target_to_topic(target), ctxt, message, retry)
|
|
|
|
|
2016-08-29 02:24:51 +00:00
|
|
|
def listen(self, target, batch_size, batch_timeout):
|
2015-06-06 06:05:29 +00:00
|
|
|
raise NotImplementedError(
|
|
|
|
'The RPC implementation for Kafka is not implemented')
|
|
|
|
|
2016-04-02 11:58:29 +00:00
|
|
|
def listen_for_notifications(self, targets_and_priorities, pool,
|
2016-04-15 16:57:16 +00:00
|
|
|
batch_size, batch_timeout):
|
2015-06-06 06:05:29 +00:00
|
|
|
"""Listen to a specified list of targets on Kafka brokers
|
|
|
|
|
|
|
|
:param targets_and_priorities: List of pairs (target, priority)
|
|
|
|
priority is not used for kafka driver
|
|
|
|
target.exchange_target.topic is used as
|
|
|
|
a kafka topic
|
|
|
|
:type targets_and_priorities: list
|
|
|
|
:param pool: consumer group of Kafka consumers
|
|
|
|
:type pool: string
|
|
|
|
"""
|
2016-06-21 11:57:17 +00:00
|
|
|
conn = self._get_connection(purpose=driver_common.PURPOSE_LISTEN)
|
2016-03-10 13:28:40 +00:00
|
|
|
topics = set()
|
2015-06-06 06:05:29 +00:00
|
|
|
for target, priority in targets_and_priorities:
|
2016-03-10 13:28:40 +00:00
|
|
|
topics.add(target_to_topic(target, priority))
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
conn.declare_topic_consumer(topics, pool)
|
|
|
|
|
2016-02-01 15:15:46 +00:00
|
|
|
listener = KafkaListener(conn)
|
2016-04-15 16:57:16 +00:00
|
|
|
return base.PollStyleListenerAdapter(listener, batch_size,
|
|
|
|
batch_timeout)
|
2015-06-06 06:05:29 +00:00
|
|
|
|
|
|
|
def _get_connection(self, purpose):
|
|
|
|
return driver_common.ConnectionContext(self.connection_pool, purpose)
|