summaryrefslogtreecommitdiff
path: root/kombu/transport/consul.py
blob: 7ace52f6b0bb6bae9e9e062bfce13145ef856e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""Consul Transport module for Kombu.

Features
========

It uses Consul.io's Key/Value store to transport messages in Queues

It uses python-consul for talking to Consul's HTTP API

Features
========
* Type: Native
* Supports Direct: Yes
* Supports Topic: *Unreviewed*
* Supports Fanout: *Unreviewed*
* Supports Priority: *Unreviewed*
* Supports TTL: *Unreviewed*

Connection String
=================

Connection string has the following format:

.. code-block::

    consul://CONSUL_ADDRESS[:PORT]

"""

from __future__ import annotations

import socket
import uuid
from collections import defaultdict
from contextlib import contextmanager
from queue import Empty
from time import monotonic

from kombu.exceptions import ChannelError
from kombu.log import get_logger
from kombu.utils.json import dumps, loads
from kombu.utils.objects import cached_property

from . import virtual

try:
    import consul
except ImportError:
    consul = None

logger = get_logger('kombu.transport.consul')

DEFAULT_PORT = 8500
DEFAULT_HOST = 'localhost'


class LockError(Exception):
    """An error occurred while trying to acquire the lock."""


class Channel(virtual.Channel):
    """Consul Channel class which talks to the Consul Key/Value store."""

    prefix = 'kombu'
    index = None
    timeout = '10s'
    session_ttl = 30

    def __init__(self, *args, **kwargs):
        if consul is None:
            raise ImportError('Missing python-consul library')

        super().__init__(*args, **kwargs)

        port = self.connection.client.port or self.connection.default_port
        host = self.connection.client.hostname or DEFAULT_HOST

        logger.debug('Host: %s Port: %s Timeout: %s', host, port, self.timeout)

        self.queues = defaultdict(dict)

        self.client = consul.Consul(host=host, port=int(port))

    def _lock_key(self, queue):
        return f'{self.prefix}/{queue}.lock'

    def _key_prefix(self, queue):
        return f'{self.prefix}/{queue}'

    def _get_or_create_session(self, queue):
        """Get or create consul session.

        Try to renew the session if it exists, otherwise create a new
        session in Consul.

        This session is used to acquire a lock inside Consul so that we achieve
        read-consistency between the nodes.

        Arguments:
            queue (str): The name of the Queue.

        Returns:
            str: The ID of the session.
        """
        try:
            session_id = self.queues[queue]['session_id']
        except KeyError:
            session_id = None
        return (self._renew_existing_session(session_id)
                if session_id is not None else self._create_new_session())

    def _renew_existing_session(self, session_id):
        logger.debug('Trying to renew existing session %s', session_id)
        session = self.client.session.renew(session_id=session_id)
        return session.get('ID')

    def _create_new_session(self):
        logger.debug('Creating session %s with TTL %s',
                     self.lock_name, self.session_ttl)
        session_id = self.client.session.create(
            name=self.lock_name, ttl=self.session_ttl)
        logger.debug('Created session %s with id %s',
                     self.lock_name, session_id)
        return session_id

    @contextmanager
    def _queue_lock(self, queue, raising=LockError):
        """Try to acquire a lock on the Queue.

        It does so by creating a object called 'lock' which is locked by the
        current session..

        This way other nodes are not able to write to the lock object which
        means that they have to wait before the lock is released.

        Arguments:
            queue (str): The name of the Queue.
            raising (Exception): Set custom lock error class.

        Raises:
            LockError: if the lock cannot be acquired.

        Returns:
            bool: success?
        """
        self._acquire_lock(queue, raising=raising)
        try:
            yield
        finally:
            self._release_lock(queue)

    def _acquire_lock(self, queue, raising=LockError):
        session_id = self._get_or_create_session(queue)
        lock_key = self._lock_key(queue)

        logger.debug('Trying to create lock object %s with session %s',
                     lock_key, session_id)

        if self.client.kv.put(key=lock_key,
                              acquire=session_id,
                              value=self.lock_name):
            self.queues[queue]['session_id'] = session_id
            return
        logger.info('Could not acquire lock on key %s', lock_key)
        raise raising()

    def _release_lock(self, queue):
        """Try to release a lock.

        It does so by simply removing the lock key in Consul.

        Arguments:
            queue (str): The name of the queue we want to release
                the lock from.
        """
        logger.debug('Removing lock key %s', self._lock_key(queue))
        self.client.kv.delete(key=self._lock_key(queue))

    def _destroy_session(self, queue):
        """Destroy a previously created Consul session.

        Will release all locks it still might hold.

        Arguments:
            queue (str): The name of the Queue.
        """
        logger.debug('Destroying session %s', self.queues[queue]['session_id'])
        self.client.session.destroy(self.queues[queue]['session_id'])

    def _new_queue(self, queue, **_):
        self.queues[queue] = {'session_id': None}
        return self.client.kv.put(key=self._key_prefix(queue), value=None)

    def _delete(self, queue, *args, **_):
        self._destroy_session(queue)
        self.queues.pop(queue, None)
        self._purge(queue)

    def _put(self, queue, payload, **_):
        """Put `message` onto `queue`.

        This simply writes a key to the K/V store of Consul
        """
        key = '{}/msg/{}_{}'.format(
            self._key_prefix(queue),
            int(round(monotonic() * 1000)),
            uuid.uuid4(),
        )
        if not self.client.kv.put(key=key, value=dumps(payload), cas=0):
            raise ChannelError(f'Cannot add key {key!r} to consul')

    def _get(self, queue, timeout=None):
        """Get the first available message from the queue.

        Before it does so it acquires a lock on the Key/Value store so
        only one node reads at the same time. This is for read consistency
        """
        with self._queue_lock(queue, raising=Empty):
            key = f'{self._key_prefix(queue)}/msg/'
            logger.debug('Fetching key %s with index %s', key, self.index)
            self.index, data = self.client.kv.get(
                key=key, recurse=True,
                index=self.index, wait=self.timeout,
            )

            try:
                if data is None:
                    raise Empty()

                logger.debug('Removing key %s with modifyindex %s',
                             data[0]['Key'], data[0]['ModifyIndex'])

                self.client.kv.delete(key=data[0]['Key'],
                                      cas=data[0]['ModifyIndex'])

                return loads(data[0]['Value'])
            except TypeError:
                pass

        raise Empty()

    def _purge(self, queue):
        self._destroy_session(queue)
        return self.client.kv.delete(
            key=f'{self._key_prefix(queue)}/msg/',
            recurse=True,
        )

    def _size(self, queue):
        size = 0
        try:
            key = f'{self._key_prefix(queue)}/msg/'
            logger.debug('Fetching key recursively %s with index %s',
                         key, self.index)
            self.index, data = self.client.kv.get(
                key=key, recurse=True,
                index=self.index, wait=self.timeout,
            )
            size = len(data)
        except TypeError:
            pass

        logger.debug('Found %s keys under %s with index %s',
                     size, key, self.index)
        return size

    @cached_property
    def lock_name(self):
        return f'{socket.gethostname()}'


class Transport(virtual.Transport):
    """Consul K/V storage Transport for Kombu."""

    Channel = Channel

    default_port = DEFAULT_PORT
    driver_type = 'consul'
    driver_name = 'consul'

    if consul:
        connection_errors = (
            virtual.Transport.connection_errors + (
                consul.ConsulException, consul.base.ConsulException
            )
        )

        channel_errors = (
            virtual.Transport.channel_errors + (
                consul.ConsulException, consul.base.ConsulException
            )
        )

    def __init__(self, *args, **kwargs):
        if consul is None:
            raise ImportError('Missing python-consul library')

        super().__init__(*args, **kwargs)

    def verify_connection(self, connection):
        port = connection.client.port or self.default_port
        host = connection.client.hostname or DEFAULT_HOST

        logger.debug('Verify Consul connection to %s:%s', host, port)

        try:
            client = consul.Consul(host=host, port=int(port))
            client.agent.self()
            return True
        except ValueError:
            pass

        return False

    def driver_version(self):
        return consul.__version__