summaryrefslogtreecommitdiff
path: root/oslo/messaging/_drivers/matchmaker_ring.py
diff options
context:
space:
mode:
authorMark McLoughlin <markmc@redhat.com>2013-08-28 09:44:59 +0100
committerMark McLoughlin <markmc@redhat.com>2013-08-28 09:44:59 +0100
commitff3a4155bffc7a535e0098a5e5523940b40d7b9e (patch)
treec7858e8b8d9ecc7dab0df2a9bb5d093bd5e9d59a /oslo/messaging/_drivers/matchmaker_ring.py
parent868c085c834b5d20ebe007ce43675b54df8deb7f (diff)
Import zmq driver code with minimal modifications
Modifications are: - use stdlib logging; no huge need for oslo logging here - stub out the _() function; we don't have any l10n infrastructure in the project and may never have - change imports to oslo.messaging.openstack.common and oslo.messaging._drivers as appropriate Change-Id: I87b85b79a33dec65e51ed95fff90cc56042240c5
Notes
Notes (review): Verified+2: Jenkins Approved+1: Davanum Srinivas (dims) <davanum@gmail.com> Code-Review+2: Davanum Srinivas (dims) <davanum@gmail.com> Submitted-by: Jenkins Submitted-at: Thu, 05 Sep 2013 09:23:13 +0000 Reviewed-on: https://review.openstack.org/44044 Project: openstack/oslo.messaging Branch: refs/heads/master
Diffstat (limited to 'oslo/messaging/_drivers/matchmaker_ring.py')
-rw-r--r--oslo/messaging/_drivers/matchmaker_ring.py109
1 files changed, 109 insertions, 0 deletions
diff --git a/oslo/messaging/_drivers/matchmaker_ring.py b/oslo/messaging/_drivers/matchmaker_ring.py
new file mode 100644
index 0000000..d0a7ecf
--- /dev/null
+++ b/oslo/messaging/_drivers/matchmaker_ring.py
@@ -0,0 +1,109 @@
1# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2011-2013 Cloudscaling Group, Inc
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16"""
17The MatchMaker classes should except a Topic or Fanout exchange key and
18return keys for direct exchanges, per (approximate) AMQP parlance.
19"""
20
21import itertools
22import json
23import logging
24
25from oslo.config import cfg
26
27from oslo.messaging._drivers import matchmaker as mm
28
29# FIXME(markmc): remove this
30_ = lambda s: s
31
32matchmaker_opts = [
33 # Matchmaker ring file
34 cfg.StrOpt('ringfile',
35 deprecated_name='matchmaker_ringfile',
36 deprecated_group='DEFAULT',
37 default='/etc/oslo/matchmaker_ring.json',
38 help='Matchmaker ring file (JSON)'),
39]
40
41CONF = cfg.CONF
42CONF.register_opts(matchmaker_opts, 'matchmaker_ring')
43LOG = logging.getLogger(__name__)
44
45
46class RingExchange(mm.Exchange):
47 """Match Maker where hosts are loaded from a static JSON formatted file.
48
49 __init__ takes optional ring dictionary argument, otherwise
50 loads the ringfile from CONF.mathcmaker_ringfile.
51 """
52 def __init__(self, ring=None):
53 super(RingExchange, self).__init__()
54
55 if ring:
56 self.ring = ring
57 else:
58 fh = open(CONF.matchmaker_ring.ringfile, 'r')
59 self.ring = json.load(fh)
60 fh.close()
61
62 self.ring0 = {}
63 for k in self.ring.keys():
64 self.ring0[k] = itertools.cycle(self.ring[k])
65
66 def _ring_has(self, key):
67 return key in self.ring0
68
69
70class RoundRobinRingExchange(RingExchange):
71 """A Topic Exchange based on a hashmap."""
72 def __init__(self, ring=None):
73 super(RoundRobinRingExchange, self).__init__(ring)
74
75 def run(self, key):
76 if not self._ring_has(key):
77 LOG.warn(
78 _("No key defining hosts for topic '%s', "
79 "see ringfile") % (key, )
80 )
81 return []
82 host = next(self.ring0[key])
83 return [(key + '.' + host, host)]
84
85
86class FanoutRingExchange(RingExchange):
87 """Fanout Exchange based on a hashmap."""
88 def __init__(self, ring=None):
89 super(FanoutRingExchange, self).__init__(ring)
90
91 def run(self, key):
92 # Assume starts with "fanout~", strip it for lookup.
93 nkey = key.split('fanout~')[1:][0]
94 if not self._ring_has(nkey):
95 LOG.warn(
96 _("No key defining hosts for topic '%s', "
97 "see ringfile") % (nkey, )
98 )
99 return []
100 return map(lambda x: (key + '.' + x, x), self.ring[nkey])
101
102
103class MatchMakerRing(mm.MatchMakerBase):
104 """Match Maker where hosts are loaded from a static hashmap."""
105 def __init__(self, ring=None):
106 super(MatchMakerRing, self).__init__()
107 self.add_binding(mm.FanoutBinding(), FanoutRingExchange(ring))
108 self.add_binding(mm.DirectBinding(), mm.DirectExchange())
109 self.add_binding(mm.TopicBinding(), RoundRobinRingExchange(ring))