Skip to content

Instantly share code, notes, and snippets.

@ajayhn
Last active August 29, 2015 14:15
Show Gist options
  • Save ajayhn/59d46695b6360e521f15 to your computer and use it in GitHub Desktop.
Save ajayhn/59d46695b6360e521f15 to your computer and use it in GitHub Desktop.
api-workers-diff
commit 9dad980977d7e16f108235d832ee10ff27e86475
Author: Hampapur Ajay <[email protected]>
Date: Tue Feb 10 11:49:22 2015 -0800
config: Add support for contrail-api workers
Change-Id: If20f6a86d556e420eba72c8d3f593867cc0abef9
diff --git a/src/config/api-server/vnc_auth_keystone.py b/src/config/api-server/vnc_auth_keystone.py
index b4e15f9..b599634 100644
--- a/src/config/api-server/vnc_auth_keystone.py
+++ b/src/config/api-server/vnc_auth_keystone.py
@@ -215,10 +215,10 @@ class AuthServiceKeystone(object):
self._server_mgr.config_log("Auth token fetched from keystone.",
level=SandeshLevel.SYS_NOTICE)
- # open access for troubleshooting
- admin_port = self._conf_info['admin_port']
- self._local_auth_app = LocalAuth(bottle.app(), self._conf_info)
- gevent.spawn(self._local_auth_app.start_http_server)
+ # open access for troubleshooting, only on worker 0
+ if int(self._server_mgr.get_worker_id()) == 0:
+ self._local_auth_app = LocalAuth(bottle.app(), self._conf_info)
+ gevent.spawn(self._local_auth_app.start_http_server)
app = auth_middleware
diff --git a/src/config/api-server/vnc_cfg_api_server.py b/src/config/api-server/vnc_cfg_api_server.py
index 3e8581a..43041a5 100644
--- a/src/config/api-server/vnc_cfg_api_server.py
+++ b/src/config/api-server/vnc_cfg_api_server.py
@@ -442,7 +442,10 @@ class VncApiServer(VncApiServerGen):
self._db_connect(True)
else:
self._db_connect(self._args.reset_config)
- self._db_init_entries()
+ if int(self._args.worker_id) == 0:
+ # only worker 0 gets to poll from keystone,
+ # initialize db entries and sync to ifmap
+ self._db_init_entries()
# Cpuinfo interface
sysinfo_req = True
@@ -570,6 +573,10 @@ class VncApiServer(VncApiServerGen):
return self._args.listen_port
# end get_server_port
+ def get_worker_id(self):
+ return self._args.worker_id
+ # end get_worker_id
+
def get_pipe_start_app(self):
return self._pipe_start_app
# end get_pipe_start_app
diff --git a/src/config/api-server/vnc_cfg_ifmap.py b/src/config/api-server/vnc_cfg_ifmap.py
index 742e7b1..1d228da 100644
--- a/src/config/api-server/vnc_cfg_ifmap.py
+++ b/src/config/api-server/vnc_cfg_ifmap.py
@@ -104,7 +104,8 @@ class VncIfmapClient(VncIfmapClientGen):
file.close()
def __init__(self, db_client_mgr, ifmap_srv_ip, ifmap_srv_port,
- uname, passwd, ssl_options, ifmap_srv_loc=None):
+ uname, passwd, ssl_options, ifmap_srv_loc=None,
+ is_publisher=False):
super(VncIfmapClient, self).__init__()
self._ifmap_srv_ip = ifmap_srv_ip
self._ifmap_srv_port = ifmap_srv_port
@@ -148,8 +149,10 @@ class VncIfmapClient(VncIfmapClientGen):
self._imid_handler = Imid()
imid = self._imid_handler
- self._init_conn()
- self._publish_config_root()
+ self._is_publisher = is_publisher
+ if self._is_publisher:
+ self._init_conn()
+ self._publish_config_root()
# end __init__
@@ -201,7 +204,7 @@ class VncIfmapClient(VncIfmapClientGen):
ns_prefix='contrail', elements=id_perms_xml)
self._update_id_self_meta(update, meta)
self._publish_update("contrail:config-root:root", update)
- # end __init__
+ # end _publish_config_root
def config_log(self, msg, level):
self._db_client_mgr.config_log(msg, level)
@@ -302,8 +305,9 @@ class VncIfmapClient(VncIfmapClientGen):
def _publish_to_ifmap(self, oper, oper_body, async, do_trace=True):
# safety check, if we proceed ifmap-server reports error
- # asking for update|delete in publish
- if not oper_body:
+ # asking for update|delete in publish. Also only publishers(worker 0)
+ # should publish.
+ if not oper_body or not self._is_publisher:
return
if do_trace:
@@ -888,11 +892,13 @@ class VncServerCassandraClient(VncCassandraClient):
class VncServerKombuClient(VncKombuClient):
- def __init__(self, db_client_mgr, rabbit_ip, rabbit_port, ifmap_db,
- rabbit_user, rabbit_password, rabbit_vhost, rabbit_ha_mode):
+ def __init__(self, db_client_mgr, worker_id, rabbit_ip, rabbit_port,
+ ifmap_db, rabbit_user, rabbit_password, rabbit_vhost,
+ rabbit_ha_mode):
self._db_client_mgr = db_client_mgr
self._sandesh = db_client_mgr._sandesh
self._ifmap_db = ifmap_db
+ self._worker_id = int(worker_id)
listen_port = db_client_mgr.get_server_port()
q_name = 'vnc_config.%s-%s' %(socket.gethostname(), listen_port)
super(VncServerKombuClient, self).__init__(
@@ -973,6 +979,10 @@ class VncServerKombuClient(VncKombuClient):
self.config_log(err_msg, level=SandeshLevel.SYS_ERR)
raise
finally:
+ if self._worker_id != 0:
+ # only worker 0 writes to ifmap
+ return
+
method_name = obj_info['type'].replace('-', '_')
method = getattr(self._ifmap_db, "_ifmap_%s_create" % (method_name))
(ok, result) = method(obj_info, obj_dict)
@@ -1003,6 +1013,10 @@ class VncServerKombuClient(VncKombuClient):
self.config_log(msg, level=SandeshLevel.SYS_ERR)
raise
finally:
+ if self._worker_id != 0:
+ # only worker 0 writes to ifmap
+ return
+
ifmap_id = self._db_client_mgr.uuid_to_ifmap_id(obj_info['type'],
obj_info['uuid'])
method_name = obj_info['type'].replace('-', '_')
@@ -1033,6 +1047,10 @@ class VncServerKombuClient(VncKombuClient):
self.config_log(msg, level=SandeshLevel.SYS_ERR)
raise
finally:
+ if self._worker_id != 0:
+ # only worker 0 writes to ifmap
+ return
+
method_name = obj_info['type'].replace('-', '_')
method = getattr(self._ifmap_db, "_ifmap_%s_delete" % (method_name))
(ok, ifmap_result) = method(obj_info)
@@ -1169,14 +1187,23 @@ class VncDbClient(object):
}
self._db_resync_done = gevent.event.Event()
+ worker_id = api_svr_mgr.get_worker_id()
+
+ if int(worker_id) == 0:
+ msg = "Connecting to ifmap on %s:%s as %s" \
+ % (ifmap_srv_ip, ifmap_srv_port, uname)
+ self.config_log(msg, level=SandeshLevel.SYS_NOTICE)
+ self._ifmap_db = VncIfmapClient(
+ self, ifmap_srv_ip, ifmap_srv_port,
+ uname, passwd, ssl_options, ifmap_srv_loc,
+ is_publisher=True)
+ else:
+ # Use the class just for helper routines
+ self._ifmap_db = VncIfmapClient(
+ self, None, None,
+ None, None, None, None,
+ is_publisher=False)
- msg = "Connecting to ifmap on %s:%s as %s" \
- % (ifmap_srv_ip, ifmap_srv_port, uname)
- self.config_log(msg, level=SandeshLevel.SYS_NOTICE)
-
- self._ifmap_db = VncIfmapClient(
- self, ifmap_srv_ip, ifmap_srv_port,
- uname, passwd, ssl_options, ifmap_srv_loc)
msg = "Connecting to cassandra on %s" % (cass_srv_list,)
self.config_log(msg, level=SandeshLevel.SYS_NOTICE)
@@ -1186,10 +1213,10 @@ class VncDbClient(object):
msg = "Connecting to zookeeper on %s" % (zk_server_ip)
self.config_log(msg, level=SandeshLevel.SYS_NOTICE)
- self._zk_db = VncZkClient(api_svr_mgr._args.worker_id, zk_server_ip,
+ self._zk_db = VncZkClient(worker_id, zk_server_ip,
reset_config, db_prefix, self.config_log)
- self._msgbus = VncServerKombuClient(self, rabbit_servers,
+ self._msgbus = VncServerKombuClient(self, worker_id, rabbit_servers,
rabbit_port, self._ifmap_db,
rabbit_user, rabbit_password,
rabbit_vhost, rabbit_ha_mode)
@@ -1231,7 +1258,9 @@ class VncDbClient(object):
# end db_resync
def wait_for_resync_done(self):
- self._db_resync_done.wait()
+ if int(self._api_svr_mgr.get_worker_id()) == 0:
+ # only worker 0 is reponsible for syncing db to ifmap
+ self._db_resync_done.wait()
# end wait_for_resync_done
def db_check(self):
diff --git a/src/config/vnc_openstack/vnc_openstack/neutron_plugin_interface.py b/src/config/vnc_openstack/vnc_openstack/neutron_plugin_interface.py
index 1c89807..983ffd7 100644
--- a/src/config/vnc_openstack/vnc_openstack/neutron_plugin_interface.py
+++ b/src/config/vnc_openstack/vnc_openstack/neutron_plugin_interface.py
@@ -42,7 +42,6 @@ class NeutronPluginInterface(object):
self._vnc_api_ip = '127.0.0.1'
else:
self._vnc_api_ip = api_server_ip
- self._vnc_api_port = api_server_port
self._config_sections = conf_sections
self._auth_user = conf_sections.get('KEYSTONE', 'admin_user')
self._auth_passwd = conf_sections.get('KEYSTONE', 'admin_password')
@@ -55,6 +54,15 @@ class NeutronPluginInterface(object):
exts_enabled = True
self._contrail_extensions_enabled = exts_enabled
+ # connect to canonical(8082) port if specified, else connect
+ # to local api server private port (9100)
+ try:
+ api_port = conf_sections.get('NEUTRON',
+ 'api_server_port')
+ except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
+ api_port = api_server_port
+ self._vnc_api_port = api_port
+
try:
self._multi_tenancy = conf_sections.get('DEFAULTS', 'multi_tenancy')
except ConfigParser.NoOptionError:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment