From 2f98f56cb5fc0e5400bdb4173c752e0e92c702f5 Mon Sep 17 00:00:00 2001
From: Nguyen Phuong An <AnNP@vn.fujitsu.com>
Date: Wed, 21 Mar 2018 09:47:23 +0700
Subject: [PATCH] Allow neutron-api load config from WSGI process

neutron-api needs to load config files when deployed under
WSGI process. This patch will do this. neutron-rpc-server
also need to be aware of plugin extension.

uWSGI seems not happy with eventlet.GreenPool. So this patch
will switch to futurist.ThreadPoolExcutor for ML2 OVO push
notification.

Partially-implements: blueprint run-in-wsgi-server

Change-Id: Ia2659c9c8f1deeceefc1c75f1149a1e47c91e7a9
Closes-Bug: #1708389
---
 neutron/common/config.py       | 11 +++++++----
 neutron/plugins/ml2/ovo_rpc.py | 17 +++++++++++++----
 neutron/server/__init__.py     | 28 +++++++++++++++++++++++++++-
 neutron/server/rpc_eventlet.py |  6 +++++-
 neutron/service.py             |  5 +++--
 5 files changed, 55 insertions(+), 12 deletions(-)

diff --git a/neutron/common/config.py b/neutron/common/config.py
index 75eebfc85b6..ba4cb3fdaf3 100644
--- a/neutron/common/config.py
+++ b/neutron/common/config.py
@@ -28,6 +28,7 @@ from oslo_middleware import cors
 from oslo_service import wsgi
 
 from neutron._i18n import _
+from neutron.common import rpc as n_rpc
 from neutron.conf import common as common_config
 from neutron import policy
 from neutron import version
@@ -72,14 +73,12 @@ common_config.register_placement_opts()
 logging.register_options(cfg.CONF)
 
 
-def init(args, **kwargs):
+def init(args, default_config_files=None, **kwargs):
     cfg.CONF(args=args, project='neutron',
              version='%%(prog)s %s' % version.version_info.release_string(),
+             default_config_files=default_config_files,
              **kwargs)
 
-    # FIXME(ihrachys): if import is put in global, circular import
-    # failure occurs
-    from neutron.common import rpc as n_rpc
     n_rpc.init(cfg.CONF)
 
     # Validate that the base_mac is of the correct format
@@ -119,6 +118,10 @@ def load_paste_app(app_name):
     :param app_name: Name of the application to load
     """
     loader = wsgi.Loader(cfg.CONF)
+
+    # Log the values of registered opts
+    if cfg.CONF.debug:
+        cfg.CONF.log_opt_values(LOG, logging.DEBUG)
     app = loader.load_app(app_name)
     return app
 
diff --git a/neutron/plugins/ml2/ovo_rpc.py b/neutron/plugins/ml2/ovo_rpc.py
index 5de72ef3c8c..6d2c47f637c 100644
--- a/neutron/plugins/ml2/ovo_rpc.py
+++ b/neutron/plugins/ml2/ovo_rpc.py
@@ -13,7 +13,9 @@
 
 import traceback
 
-import eventlet
+import futurist
+from futurist import waiters
+
 from neutron_lib.callbacks import events
 from neutron_lib.callbacks import registry
 from neutron_lib.callbacks import resources
@@ -38,7 +40,12 @@ class _ObjectChangeHandler(object):
         self._obj_class = object_class
         self._resource_push_api = resource_push_api
         self._resources_to_push = {}
-        self._worker_pool = eventlet.GreenPool()
+
+        # NOTE(annp): uWSGI seems not happy with eventlet.GreenPool.
+        # So switching to ThreadPool
+        self._worker_pool = futurist.ThreadPoolExecutor()
+        self.fts = []
+
         self._semantic_warned = False
         for event in (events.AFTER_CREATE, events.AFTER_UPDATE,
                       events.AFTER_DELETE):
@@ -46,7 +53,9 @@ class _ObjectChangeHandler(object):
 
     def wait(self):
         """Waits for all outstanding events to be dispatched."""
-        self._worker_pool.waitall()
+        done, not_done = waiters.wait_for_all(self.fts)
+        if not not_done:
+            del self.fts[:]
 
     def _is_session_semantic_violated(self, context, resource, event):
         """Return True and print an ugly error on transaction violation.
@@ -82,7 +91,7 @@ class _ObjectChangeHandler(object):
         # to the server-side event that triggered it
         self._resources_to_push[resource_id] = context.to_dict()
         # spawn worker so we don't block main AFTER_UPDATE thread
-        self._worker_pool.spawn(self.dispatch_events)
+        self.fts.append(self._worker_pool.submit(self.dispatch_events))
 
     @lockutils.synchronized('event-dispatch')
     def dispatch_events(self):
diff --git a/neutron/server/__init__.py b/neutron/server/__init__.py
index ac4bc91d767..f3cbe17c2c5 100644
--- a/neutron/server/__init__.py
+++ b/neutron/server/__init__.py
@@ -16,6 +16,7 @@
 # If ../neutron/__init__.py exists, add ../ to Python search path, so that
 # it will override what happens to be installed in /usr/(local/)lib/python...
 
+import os
 import sys
 
 from oslo_config import cfg
@@ -24,10 +25,35 @@ from neutron._i18n import _
 from neutron.common import config
 from neutron.common import profiler
 
+# NOTE(annp): These environment variables are required for deploying
+# neutron-api under mod_wsgi. Currently, these variables are set as DevStack's
+# default. If you intend to use other tools for deploying neutron-api under
+# mod_wsgi, you should export these variables with your values.
+
+NEUTRON_CONF = 'neutron.conf'
+NEUTRON_CONF_DIR = '/etc/neutron/'
+NEUTRON_PLUGIN_CONF = 'plugins/ml2/ml2_conf.ini'
+
+
+def _get_config_files(env=None):
+    if env is None:
+        env = os.environ
+    dirname = env.get('OS_NEUTRON_CONFIG_DIR', NEUTRON_CONF_DIR).strip()
+
+    files = [s.strip() for s in
+             env.get('OS_NEUTRON_CONFIG_FILES', '').split(';') if s.strip()]
+
+    if not files:
+        files = [NEUTRON_CONF, NEUTRON_PLUGIN_CONF]
+
+    return [os.path.join(dirname, fname) for fname in files]
+
 
 def _init_configuration():
     # the configuration will be read into the cfg.CONF global data structure
-    config.init(sys.argv[1:])
+    conf_files = _get_config_files()
+
+    config.init(sys.argv[1:], default_config_files=conf_files)
     config.setup_logging()
     config.set_config_defaults()
     if not cfg.CONF.config_file:
diff --git a/neutron/server/rpc_eventlet.py b/neutron/server/rpc_eventlet.py
index 93af4d48f13..b445279750e 100644
--- a/neutron/server/rpc_eventlet.py
+++ b/neutron/server/rpc_eventlet.py
@@ -18,8 +18,10 @@
 # If ../neutron/__init__.py exists, add ../ to Python search path, so that
 # it will override what happens to be installed in /usr/(local/)lib/python...
 
+from neutron_lib.api import attributes
 from oslo_log import log
 
+from neutron.api import extensions
 from neutron import manager
 from neutron import service
 
@@ -31,7 +33,9 @@ def eventlet_rpc_server():
 
     try:
         manager.init()
-        rpc_workers_launcher = service.start_all_workers()
+        ext_mgr = extensions.PluginAwareExtensionManager.get_instance()
+        ext_mgr.extend_resources("2.0", attributes.RESOURCES)
+        rpc_workers_launcher = service.start_rpc_workers()
     except NotImplementedError:
         LOG.info("RPC was already started in parent process by "
                  "plugin.")
diff --git a/neutron/service.py b/neutron/service.py
index 158ada87dd7..8e5d89d63c8 100644
--- a/neutron/service.py
+++ b/neutron/service.py
@@ -269,9 +269,10 @@ def start_all_workers():
 
 def start_rpc_workers():
     rpc_workers = _get_rpc_workers()
-
     LOG.debug('using launcher for rpc, workers=%s', cfg.CONF.rpc_workers)
-    return _start_workers(rpc_workers)
+    launcher = _start_workers(rpc_workers)
+    registry.publish(resources.PROCESS, events.AFTER_SPAWN, None)
+    return launcher
 
 
 def start_plugins_workers():