summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabio Verboso <fverboso@unime.it>2018-09-14 15:53:26 +0200
committerFabio Verboso <fverboso@unime.it>2018-09-14 15:53:26 +0200
commit7e0dd37681bfca96ee995fa690fcaa6585168077 (patch)
treef60b3f0d9535093bc3f36178e030d7d30ca9ee6b
parentd829c66233ddf53b0d8e4b6aad60b3da72a38f68 (diff)
Update RPC service
Notes
Notes (review): Code-Review+2: Fabio Verboso <fverboso@unime.it> Workflow+1: Fabio Verboso <fverboso@unime.it> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Mon, 17 Sep 2018 10:14:21 +0000 Reviewed-on: https://review.openstack.org/603066 Project: openstack/iotronic Branch: refs/heads/master
-rw-r--r--iotronic/common/rpc.py113
-rw-r--r--iotronic/common/service.py10
2 files changed, 67 insertions, 56 deletions
diff --git a/iotronic/common/rpc.py b/iotronic/common/rpc.py
index 8e834cb..71f3a5e 100644
--- a/iotronic/common/rpc.py
+++ b/iotronic/common/rpc.py
@@ -16,87 +16,70 @@
16from oslo_config import cfg 16from oslo_config import cfg
17import oslo_messaging as messaging 17import oslo_messaging as messaging
18from oslo_messaging.rpc import dispatcher 18from oslo_messaging.rpc import dispatcher
19from oslo_serialization import jsonutils 19from osprofiler import profiler
20 20
21from iotronic.common import context as iotronic_context 21from iotronic.common import context as iotronic_context
22from iotronic.common import exception 22from iotronic.common import exception
23 23
24__all__ = [
25 'init',
26 'cleanup',
27 'set_defaults',
28 'add_extra_exmods',
29 'clear_extra_exmods',
30 'get_allowed_exmods',
31 'RequestContextSerializer',
32 'get_client',
33 'get_server',
34 'get_notifier',
35 'TRANSPORT_ALIASES',
36]
37 24
38CONF = cfg.CONF 25CONF = cfg.CONF
26
39TRANSPORT = None 27TRANSPORT = None
40NOTIFIER = None 28NOTIFICATION_TRANSPORT = None
29SENSORS_NOTIFIER = None
30VERSIONED_NOTIFIER = None
41 31
42ALLOWED_EXMODS = [ 32ALLOWED_EXMODS = [
43 exception.__name__, 33 exception.__name__,
44] 34]
45EXTRA_EXMODS = [] 35EXTRA_EXMODS = []
46 36
47# NOTE(lucasagomes): The iotronic.openstack.common.rpc entries are for
48# backwards compat with IceHouse rpc_backend configuration values.
49TRANSPORT_ALIASES = {
50 'iotronic.openstack.common.rpc.impl_kombu': 'rabbit',
51 'iotronic.openstack.common.rpc.impl_qpid': 'qpid',
52 'iotronic.openstack.common.rpc.impl_zmq': 'zmq',
53 'iotronic.rpc.impl_kombu': 'rabbit',
54 'iotronic.rpc.impl_qpid': 'qpid',
55 'iotronic.rpc.impl_zmq': 'zmq',
56}
57
58 37
59def init(conf): 38def init(conf):
60 global TRANSPORT, NOTIFIER 39 global TRANSPORT, NOTIFICATION_TRANSPORT
40 global SENSORS_NOTIFIER, VERSIONED_NOTIFIER
61 exmods = get_allowed_exmods() 41 exmods = get_allowed_exmods()
62 TRANSPORT = messaging.get_transport(conf, 42 TRANSPORT = messaging.get_rpc_transport(conf,
63 allowed_remote_exmods=exmods, 43 allowed_remote_exmods=exmods)
64 aliases=TRANSPORT_ALIASES) 44 NOTIFICATION_TRANSPORT = messaging.get_notification_transport(
65 serializer = RequestContextSerializer(JsonPayloadSerializer()) 45 conf,
66 NOTIFIER = messaging.Notifier(TRANSPORT, serializer=serializer) 46 allowed_remote_exmods=exmods)
47
48 serializer = RequestContextSerializer(messaging.JsonPayloadSerializer())
49 SENSORS_NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
50 serializer=serializer)
51 if conf.notification_level is None:
52 VERSIONED_NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
53 serializer=serializer,
54 driver='noop')
55 else:
56 VERSIONED_NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
57 serializer=serializer,
58 topics=['iotronic_versioned_'
59 'notifications'])
67 60
68 61
69def cleanup(): 62def cleanup():
70 global TRANSPORT, NOTIFIER 63 global TRANSPORT, NOTIFICATION_TRANSPORT
64 global SENSORS_NOTIFIER, VERSIONED_NOTIFIER
71 assert TRANSPORT is not None 65 assert TRANSPORT is not None
72 assert NOTIFIER is not None 66 assert NOTIFICATION_TRANSPORT is not None
67 assert SENSORS_NOTIFIER is not None
68 assert VERSIONED_NOTIFIER is not None
73 TRANSPORT.cleanup() 69 TRANSPORT.cleanup()
74 TRANSPORT = NOTIFIER = None 70 NOTIFICATION_TRANSPORT.cleanup()
71 TRANSPORT = NOTIFICATION_TRANSPORT = None
72 SENSORS_NOTIFIER = VERSIONED_NOTIFIER = None
75 73
76 74
77def set_defaults(control_exchange): 75def set_defaults(control_exchange):
78 messaging.set_transport_defaults(control_exchange) 76 messaging.set_transport_defaults(control_exchange)
79 77
80 78
81def add_extra_exmods(*args):
82 EXTRA_EXMODS.extend(args)
83
84
85def clear_extra_exmods():
86 del EXTRA_EXMODS[:]
87
88
89def get_allowed_exmods(): 79def get_allowed_exmods():
90 return ALLOWED_EXMODS + EXTRA_EXMODS 80 return ALLOWED_EXMODS + EXTRA_EXMODS
91 81
92 82
93class JsonPayloadSerializer(messaging.NoOpSerializer):
94
95 @staticmethod
96 def serialize_entity(context, entity):
97 return jsonutils.to_primitive(entity, convert_instances=True)
98
99
100class RequestContextSerializer(messaging.Serializer): 83class RequestContextSerializer(messaging.Serializer):
101 84
102 def __init__(self, base): 85 def __init__(self, base):
@@ -113,14 +96,26 @@ class RequestContextSerializer(messaging.Serializer):
113 return self._base.deserialize_entity(context, entity) 96 return self._base.deserialize_entity(context, entity)
114 97
115 def serialize_context(self, context): 98 def serialize_context(self, context):
116 return context.to_dict() 99 _context = context.to_dict()
100 prof = profiler.get()
101 if prof:
102 trace_info = {
103 "hmac_key": prof.hmac_key,
104 "base_id": prof.get_base_id(),
105 "parent_id": prof.get_id()
106 }
107 _context.update({"trace_info": trace_info})
108 return _context
117 109
118 def deserialize_context(self, context): 110 def deserialize_context(self, context):
111 trace_info = context.pop("trace_info", None)
112 if trace_info:
113 profiler.init(**trace_info)
119 return iotronic_context.RequestContext.from_dict(context) 114 return iotronic_context.RequestContext.from_dict(context)
120 115
121 116
122def get_transport_url(url_str=None): 117def get_transport_url(url_str=None):
123 return messaging.TransportURL.parse(CONF, url_str, TRANSPORT_ALIASES) 118 return messaging.TransportURL.parse(CONF, url_str)
124 119
125 120
126def get_client(target, version_cap=None, serializer=None): 121def get_client(target, version_cap=None, serializer=None):
@@ -134,8 +129,8 @@ def get_client(target, version_cap=None, serializer=None):
134 129
135def get_server(target, endpoints, serializer=None): 130def get_server(target, endpoints, serializer=None):
136 assert TRANSPORT is not None 131 assert TRANSPORT is not None
137 access_policy = dispatcher.DefaultRPCAccessPolicy
138 serializer = RequestContextSerializer(serializer) 132 serializer = RequestContextSerializer(serializer)
133 access_policy = dispatcher.DefaultRPCAccessPolicy
139 return messaging.get_rpc_server(TRANSPORT, 134 return messaging.get_rpc_server(TRANSPORT,
140 target, 135 target,
141 endpoints, 136 endpoints,
@@ -144,8 +139,14 @@ def get_server(target, endpoints, serializer=None):
144 access_policy=access_policy) 139 access_policy=access_policy)
145 140
146 141
147def get_notifier(service=None, host=None, publisher_id=None): 142def get_sensors_notifier(service=None, host=None, publisher_id=None):
148 assert NOTIFIER is not None 143 assert SENSORS_NOTIFIER is not None
149 if not publisher_id: 144 if not publisher_id:
150 publisher_id = "%s.%s" % (service, host or CONF.host) 145 publisher_id = "%s.%s" % (service, host or CONF.host)
151 return NOTIFIER.prepare(publisher_id=publisher_id) 146 return SENSORS_NOTIFIER.prepare(publisher_id=publisher_id)
147
148
149def get_versioned_notifier(publisher_id=None):
150 assert VERSIONED_NOTIFIER is not None
151 assert publisher_id is not None
152 return VERSIONED_NOTIFIER.prepare(publisher_id=publisher_id)
diff --git a/iotronic/common/service.py b/iotronic/common/service.py
index fbe2cc7..a22a254 100644
--- a/iotronic/common/service.py
+++ b/iotronic/common/service.py
@@ -24,6 +24,7 @@ import oslo_messaging as messaging
24from oslo_utils import importutils 24from oslo_utils import importutils
25 25
26from iotronic.common import config 26from iotronic.common import config
27from iotronic.common.i18n import _
27from iotronic.common.i18n import _LE 28from iotronic.common.i18n import _LE
28from iotronic.common.i18n import _LI 29from iotronic.common.i18n import _LI
29from iotronic.common import rpc 30from iotronic.common import rpc
@@ -42,6 +43,15 @@ service_opts = [
42 'However, the board name must be valid within ' 43 'However, the board name must be valid within '
43 'an AMQP key, and if using ZeroMQ, a valid ' 44 'an AMQP key, and if using ZeroMQ, a valid '
44 'hostname, FQDN, or IP address.'), 45 'hostname, FQDN, or IP address.'),
46 cfg.StrOpt('notification_level',
47 choices=[('debug', _('"debug" level')),
48 ('info', _('"info" level')),
49 ('warning', _('"warning" level')),
50 ('error', _('"error" level')),
51 ('critical', _('"critical" level'))],
52 help=_('Specifies the minimum level for which to send '
53 'notifications. If not set, no notifications will '
54 'be sent. The default is for this option to be unset.')),
45] 55]
46 56
47cfg.CONF.register_opts(service_opts) 57cfg.CONF.register_opts(service_opts)