From 34380df15b3e28d7bfa4ca3a5a11fcbbcb65e376 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Wed, 18 Mar 2015 04:13:11 -0700 Subject: [PATCH] Only call get_engine().pool.dispose if _FACADE Avoid calling neutron.db.api.get_engine().pool.dispose() if an engine facade has not yet been created since there won't be any connections to get rid of. Calling it on services that do not use the DB (e.g. agents) unnecessarily creates a database connection engine that will never be used. Change-Id: I3dbad1bef5da7b3765898e7d539b4d119b89e73a Closes-Bug: #1433536 --- neutron/agent/metadata_agent.py | 2 -- neutron/db/api.py | 6 ++++++ neutron/service.py | 2 +- neutron/tests/unit/agent/metadata/test_agent.py | 2 -- neutron/tests/unit/test_wsgi.py | 10 +++++----- neutron/wsgi.py | 3 +-- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/neutron/agent/metadata_agent.py b/neutron/agent/metadata_agent.py index b6e27914bd1..b392ed1f3ce 100644 --- a/neutron/agent/metadata_agent.py +++ b/neutron/agent/metadata_agent.py @@ -37,7 +37,5 @@ def main(): config.init(sys.argv[1:]) config.setup_logging() utils.log_opt_values(LOG) - # metadata agent need not connect DB - cfg.CONF.set_override("connection", "", "database") proxy = agent.UnixDomainMetadataProxy(cfg.CONF) proxy.run() diff --git a/neutron/db/api.py b/neutron/db/api.py index 3957de91a41..4418bbc2825 100644 --- a/neutron/db/api.py +++ b/neutron/db/api.py @@ -40,6 +40,12 @@ def get_engine(): return facade.get_engine() +def dispose(): + # Don't need to do anything if an enginefacade hasn't been created + if _FACADE is not None: + get_engine().pool.dispose() + + def get_session(autocommit=True, expire_on_commit=False): """Helper method to grab session.""" facade = _create_facade_lazily() diff --git a/neutron/service.py b/neutron/service.py index 448251c383a..02221282309 100644 --- a/neutron/service.py +++ b/neutron/service.py @@ -119,7 +119,7 @@ class RpcWorker(object): # We may have just forked from parent process. A quick disposal of the # existing sql connections avoids producing errors later when they are # discovered to be broken. - session.get_engine().pool.dispose() + session.dispose() self._servers = self._plugin.start_rpc_listeners() def wait(self): diff --git a/neutron/tests/unit/agent/metadata/test_agent.py b/neutron/tests/unit/agent/metadata/test_agent.py index e5bbe9b3791..3f328a24123 100644 --- a/neutron/tests/unit/agent/metadata/test_agent.py +++ b/neutron/tests/unit/agent/metadata/test_agent.py @@ -587,8 +587,6 @@ class TestUnixDomainMetadataProxy(base.BaseTestCase): mock.call(cfg.CONF), mock.call().run()] ) - cfg.CONF.set_override.assert_called_once_with( - "connection", "", "database") def test_init_state_reporting(self): with mock.patch('os.makedirs'): diff --git a/neutron/tests/unit/test_wsgi.py b/neutron/tests/unit/test_wsgi.py index da116ece392..b0e57d34814 100644 --- a/neutron/tests/unit/test_wsgi.py +++ b/neutron/tests/unit/test_wsgi.py @@ -25,6 +25,7 @@ import webob import webob.exc from neutron.common import exceptions as exception +from neutron.db import api from neutron.tests import base from neutron import wsgi @@ -52,18 +53,17 @@ def open_no_proxy(*args, **kwargs): class TestWorkerService(base.BaseTestCase): """WorkerService tests.""" - @mock.patch('neutron.db.api') + @mock.patch('neutron.db.api.get_engine') def test_start_withoutdb_call(self, apimock): + # clear engine from other tests + api._FACADE = None _service = mock.Mock() - _service.pool = mock.Mock() - _service.pool.spawn = mock.Mock() _service.pool.spawn.return_value = None _app = mock.Mock() - cfg.CONF.set_override("connection", "", "database") workerservice = wsgi.WorkerService(_service, _app) workerservice.start() - self.assertFalse(apimock.get_engine.called) + self.assertFalse(apimock.called) class TestWSGIServer(base.BaseTestCase): diff --git a/neutron/wsgi.py b/neutron/wsgi.py index cbb3292536d..a526fb824b6 100644 --- a/neutron/wsgi.py +++ b/neutron/wsgi.py @@ -101,8 +101,7 @@ class WorkerService(object): # We may have just forked from parent process. A quick disposal of the # existing sql connections avoids producing 500 errors later when they # are discovered to be broken. - if CONF.database.connection: - api.get_engine().pool.dispose() + api.dispose() self._server = self._service.pool.spawn(self._service._run, self._application, self._service._socket)