From 7b4b3749a113d4d7984ec12fb5c1b6d2b4786ee3 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Fri, 24 Jul 2015 11:22:08 +0200 Subject: [PATCH] storage: pass conf rather at __init__ than using a global one Change-Id: Ic71f8cd08296bc2329ab235198020d5fa6f1670e --- aodh/storage/__init__.py | 2 +- aodh/storage/base.py | 2 +- aodh/storage/hbase/base.py | 2 +- aodh/storage/impl_db2.py | 2 +- aodh/storage/impl_hbase.py | 3 --- aodh/storage/impl_mongodb.py | 8 ++++---- aodh/storage/impl_sqlalchemy.py | 5 ++--- aodh/tests/storage/test_impl_hbase.py | 2 +- aodh/tests/storage/test_impl_log.py | 5 ++--- aodh/tests/storage/test_impl_mongodb.py | 4 ++-- 10 files changed, 15 insertions(+), 20 deletions(-) diff --git a/aodh/storage/__init__.py b/aodh/storage/__init__.py index 4776d3d42..4beb2e832 100644 --- a/aodh/storage/__init__.py +++ b/aodh/storage/__init__.py @@ -107,7 +107,7 @@ def get_connection(url): LOG.debug('looking for %(name)r driver in %(namespace)r', {'name': engine_name, 'namespace': _NAMESPACE}) mgr = driver.DriverManager(_NAMESPACE, engine_name) - return mgr.driver(url) + return mgr.driver(cfg.CONF, url) class SampleFilter(object): diff --git a/aodh/storage/base.py b/aodh/storage/base.py index 0583d32a4..3d652a50f 100644 --- a/aodh/storage/base.py +++ b/aodh/storage/base.py @@ -65,7 +65,7 @@ class Connection(object): 'storage': {'production_ready': False}, } - def __init__(self, url): + def __init__(self, conf, url): pass @staticmethod diff --git a/aodh/storage/hbase/base.py b/aodh/storage/hbase/base.py index 0186b8bd5..0acc51d14 100644 --- a/aodh/storage/hbase/base.py +++ b/aodh/storage/hbase/base.py @@ -29,7 +29,7 @@ class Connection(object): _memory_instance = None - def __init__(self, url): + def __init__(self, conf, url): """Hbase Connection Initialization.""" opts = self._parse_connection_url(url) diff --git a/aodh/storage/impl_db2.py b/aodh/storage/impl_db2.py index 714220978..d22e32bb4 100644 --- a/aodh/storage/impl_db2.py +++ b/aodh/storage/impl_db2.py @@ -33,7 +33,7 @@ class Connection(pymongo_base.Connection): CONNECTION_POOL = pymongo_utils.ConnectionPool() - def __init__(self, url): + def __init__(self, conf, url): # Since we are using pymongo, even though we are connecting to DB2 # we still have to make sure that the scheme which used to distinguish diff --git a/aodh/storage/impl_hbase.py b/aodh/storage/impl_hbase.py index d80b061c0..4ef1544f4 100644 --- a/aodh/storage/impl_hbase.py +++ b/aodh/storage/impl_hbase.py @@ -73,9 +73,6 @@ class Connection(hbase_base.Connection, base.Connection): ALARM_TABLE = "alarm" ALARM_HISTORY_TABLE = "alarm_h" - def __init__(self, url): - super(Connection, self).__init__(url) - def upgrade(self): tables = [self.ALARM_HISTORY_TABLE, self.ALARM_TABLE] column_families = {'f': dict()} diff --git a/aodh/storage/impl_mongodb.py b/aodh/storage/impl_mongodb.py index 195cd8732..1b586eda5 100644 --- a/aodh/storage/impl_mongodb.py +++ b/aodh/storage/impl_mongodb.py @@ -39,9 +39,9 @@ class Connection(pymongo_base.Connection): CONNECTION_POOL = pymongo_utils.ConnectionPool() - def __init__(self, url): - - # NOTE(jd) Use our own connection pooling on top of the Pymongo one. + def __init__(self, conf, url): + self.conf = conf + # NOTE(jd) Use our own connection pooling on top of the Pymongo one./ # We need that otherwise we overflow the MongoDB instance with new # connection since we instantiate a Pymongo client each time someone # requires a new storage connection. @@ -90,7 +90,7 @@ class Connection(pymongo_base.Connection): def upgrade(self): super(Connection, self).upgrade() # Establish indexes - ttl = cfg.CONF.database.alarm_history_time_to_live + ttl = self.conf.database.alarm_history_time_to_live self.update_ttl( ttl, 'alarm_history_ttl', 'timestamp', self.db.alarm_history) diff --git a/aodh/storage/impl_sqlalchemy.py b/aodh/storage/impl_sqlalchemy.py index 32a10aa98..d045e5a81 100644 --- a/aodh/storage/impl_sqlalchemy.py +++ b/aodh/storage/impl_sqlalchemy.py @@ -15,7 +15,6 @@ from __future__ import absolute_import import datetime -from oslo_config import cfg from oslo_db.sqlalchemy import session as db_session from oslo_log import log from oslo_utils import timeutils @@ -52,12 +51,12 @@ class Connection(base.Connection): AVAILABLE_STORAGE_CAPABILITIES, ) - def __init__(self, url): + def __init__(self, conf, url): # Set max_retries to 0, since oslo.db in certain cases may attempt # to retry making the db connection retried max_retries ^ 2 times # in failure case and db reconnection has already been implemented # in storage.__init__.get_connection_from_config function - options = dict(cfg.CONF.database.items()) + options = dict(conf.database.items()) options['max_retries'] = 0 self._engine_facade = db_session.EngineFacade(url, **options) diff --git a/aodh/tests/storage/test_impl_hbase.py b/aodh/tests/storage/test_impl_hbase.py index 423c4eef9..0cfa71af0 100644 --- a/aodh/tests/storage/test_impl_hbase.py +++ b/aodh/tests/storage/test_impl_hbase.py @@ -51,7 +51,7 @@ class ConnectionTest(tests_db.TestBase, with mock.patch.object(impl_hbase.Connection, '_get_connection_pool', side_effect=get_connection_pool): - conn = impl_hbase.Connection('hbase://test_hbase:9090') + conn = impl_hbase.Connection(self.CONF, 'hbase://test_hbase:9090') self.assertIsInstance(conn.conn_pool, TestConn) diff --git a/aodh/tests/storage/test_impl_log.py b/aodh/tests/storage/test_impl_log.py index 1a1467196..9a6c71a7b 100644 --- a/aodh/tests/storage/test_impl_log.py +++ b/aodh/tests/storage/test_impl_log.py @@ -12,8 +12,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -"""Tests for aodh/storage/impl_log.py -""" +from oslo_config import cfg from oslotest import base from aodh.storage import impl_log @@ -22,4 +21,4 @@ from aodh.storage import impl_log class ConnectionTest(base.BaseTestCase): @staticmethod def test_get_connection(): - impl_log.Connection(None) + impl_log.Connection(cfg.CONF, None) diff --git a/aodh/tests/storage/test_impl_mongodb.py b/aodh/tests/storage/test_impl_mongodb.py index 872d17358..3726acb14 100644 --- a/aodh/tests/storage/test_impl_mongodb.py +++ b/aodh/tests/storage/test_impl_mongodb.py @@ -30,12 +30,12 @@ from aodh.tests import db as tests_db class MongoDBConnection(tests_db.TestBase, tests_db.MixinTestsWithBackendScenarios): def test_connection_pooling(self): - test_conn = impl_mongodb.Connection(self.db_manager.url) + test_conn = impl_mongodb.Connection(self.CONF, self.db_manager.url) self.assertEqual(self.alarm_conn.conn, test_conn.conn) def test_replica_set(self): url = self.db_manager._url + '?replicaSet=foobar' - conn = impl_mongodb.Connection(url) + conn = impl_mongodb.Connection(self.CONF, url) self.assertTrue(conn.conn)