This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
df_publisher_service.py
158 lines (139 loc) · 5.34 KB
/
df_publisher_service.py
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
# 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.
import eventlet
from Queue import Queue
import sys
import time
import traceback
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import importutils
from neutron.agent.common import config
from neutron.common import config as common_config
from dragonflow._i18n import _LW
from dragonflow.common import common_params
from dragonflow.common import exceptions
from dragonflow.common import utils as df_utils
from dragonflow.db import db_common
from dragonflow.db import pub_sub_api
eventlet.monkey_patch()
LOG = logging.getLogger(__name__)
class PublisherService(object):
def __init__(self):
self._queue = Queue()
self.publisher = self._get_publisher()
self.multiproc_subscriber = self._get_multiproc_subscriber()
nb_driver_class = importutils.import_class(cfg.CONF.df.nb_db_class)
self.db = nb_driver_class()
self.uuid = pub_sub_api.generate_publisher_uuid()
self._rate_limit = df_utils.RateLimiter(2, 30)
def _get_publisher(self):
pub_sub_driver = df_utils.load_driver(
cfg.CONF.df.pub_sub_driver,
df_utils.DF_PUBSUB_DRIVER_NAMESPACE)
return pub_sub_driver.get_publisher()
def _get_multiproc_subscriber(self):
"""
Return the subscriber for inter-process communication. If multi-proc
communication is not use (i.e. disabled from config), return None.
"""
if not cfg.CONF.df.pub_sub_use_multiproc:
return None
pub_sub_driver = df_utils.load_driver(
cfg.CONF.df.pub_sub_multiproc_driver,
df_utils.DF_PUBSUB_DRIVER_NAMESPACE)
return pub_sub_driver.get_subscriber()
def initialize(self):
if self.multiproc_subscriber:
self.multiproc_subscriber.initialize(self._append_event_to_queue)
self.publisher.initialize()
# TODO(oanson) TableMonitor initialisation goes here
def _append_event_to_queue(self, table, key, action, value, topic):
event = db_common.DbUpdate(table, key, action, value, topic=topic)
self._queue.put(event)
eventlet.sleep(0)
def run(self):
if self.multiproc_subscriber:
self.multiproc_subscriber.daemonize()
self.db.initialize(
db_ip=cfg.CONF.df.remote_db_ip,
db_port=cfg.CONF.df.remote_db_port,
config=cfg.CONF.df
)
self._register_as_publisher()
self._publishers_table_monitor = pub_sub_api.StalePublisherMonitor(
self.db,
self.publisher,
cfg.CONF.df.publisher_timeout
)
self._publishers_table_monitor.daemonize()
# TODO(oanson) TableMonitor daemonize will go here
while True:
try:
event = self._queue.get()
self.publisher.send_event(event)
if event.table != pub_sub_api.PUBLISHER_TABLE:
self._update_timestamp_in_db()
eventlet.sleep(0)
except Exception as e:
LOG.warning(_LW("Exception in main loop: {}, {}").format(
e, traceback.format_exc()
))
# Ignore
def _update_timestamp_in_db(self):
if self._rate_limit():
return
try:
publisher_json = self.db.get_key(
pub_sub_api.PUBLISHER_TABLE,
self.uuid,
)
publisher = jsonutils.loads(publisher_json)
publisher['last_activity_timestamp'] = time.time()
publisher_json = jsonutils.dumps(publisher)
self.db.set_key(
pub_sub_api.PUBLISHER_TABLE,
self.uuid,
publisher_json
)
except exceptions.DBKeyNotFound:
self._register_as_publisher()
def _register_as_publisher(self):
publisher = {
'id': self.uuid,
'uri': self._get_uri(),
'last_activity_timestamp': time.time(),
}
publisher_json = jsonutils.dumps(publisher)
self.db.create_key(
pub_sub_api.PUBLISHER_TABLE,
self.uuid, publisher_json
)
def _get_uri(self):
ip = cfg.CONF.df.publisher_bind_address
if ip == '*' or ip == '127.0.0.1':
ip = cfg.CONF.df.local_ip
return "{}://{}:{}".format(
cfg.CONF.df.publisher_transport,
ip,
cfg.CONF.df.publisher_port,
)
def main():
cfg.CONF.register_opts(common_params.df_opts, 'df')
common_config.init(sys.argv[1:])
config.setup_logging()
service = PublisherService()
service.initialize()
service.run()
if __name__ == "__main__":
main()