Merge "dispatcher/database: simplify connection retrieving"

This commit is contained in:
Jenkins 2016-08-18 15:22:29 +00:00 committed by Gerrit Code Review
commit 4ce3339670
2 changed files with 8 additions and 28 deletions

View File

@ -24,7 +24,7 @@ from ceilometer import storage
LOG = log.getLogger(__name__)
class DatabaseDispatcher(object):
class DatabaseDispatcher(dispatcher.Base):
"""Dispatcher class for recording metering data into database.
The dispatcher class which records each meter into a database configured
@ -37,24 +37,12 @@ class DatabaseDispatcher(object):
meter_dispatchers = database
event_dispatchers = database
"""
def __init__(self, conf):
self.conf = conf
self._conn = self._get_db_conn(conf, self.CONNECTION_TYPE, True)
def _get_db_conn(self, conf, purpose, ignore_exception=False):
try:
return storage.get_connection_from_config(conf, purpose)
except Exception as err:
params = {"purpose": purpose, "err": err}
LOG.exception(_LE("Failed to connect to db, purpose %(purpose)s "
"re-try later: %(err)s") % params)
if not ignore_exception:
raise
@property
def conn(self):
if not self._conn:
self._conn = self._get_db_conn(self.conf, self.CONNECTION_TYPE)
if not hasattr(self, "_conn"):
self._conn = storage.get_connection_from_config(
self.conf, self.CONNECTION_TYPE)
return self._conn
@ -62,10 +50,6 @@ class MeterDatabaseDispatcher(dispatcher.MeterDispatcherBase,
DatabaseDispatcher):
CONNECTION_TYPE = 'metering'
def __init__(self, conf):
DatabaseDispatcher.__init__(self, conf)
dispatcher.MeterDispatcherBase.__init__(self, conf)
def record_metering_data(self, data):
# We may have receive only one counter on the wire
if not data:
@ -99,10 +83,6 @@ class EventDatabaseDispatcher(dispatcher.EventDispatcherBase,
DatabaseDispatcher):
CONNECTION_TYPE = 'event'
def __init__(self, conf):
DatabaseDispatcher.__init__(self, conf)
dispatcher.EventDispatcherBase.__init__(self, conf)
def record_events(self, events):
if not isinstance(events, list):
events = [events]

View File

@ -40,7 +40,7 @@ class TestDispatcherDB(base.BaseTestCase):
[], {})
event = utils.message_from_event(event,
self.CONF.publisher.telemetry_secret)
with mock.patch.object(self.event_dispatcher._conn,
with mock.patch.object(self.event_dispatcher.conn,
'record_events') as record_events:
self.event_dispatcher.record_events(event)
self.assertEqual(1, len(record_events.call_args_list[0][0][0]))
@ -54,7 +54,7 @@ class TestDispatcherDB(base.BaseTestCase):
msg, self.CONF.publisher.telemetry_secret,
)
with mock.patch.object(self.meter_dispatcher._conn,
with mock.patch.object(self.meter_dispatcher.conn,
'record_metering_data') as record_metering_data:
self.meter_dispatcher.record_metering_data(msg)
@ -73,7 +73,7 @@ class TestDispatcherDB(base.BaseTestCase):
expected = msg.copy()
expected['timestamp'] = datetime.datetime(2012, 7, 2, 13, 53, 40)
with mock.patch.object(self.meter_dispatcher._conn,
with mock.patch.object(self.meter_dispatcher.conn,
'record_metering_data') as record_metering_data:
self.meter_dispatcher.record_metering_data(msg)
@ -93,7 +93,7 @@ class TestDispatcherDB(base.BaseTestCase):
expected['timestamp'] = datetime.datetime(2012, 9, 30, 23,
31, 50, 262000)
with mock.patch.object(self.meter_dispatcher._conn,
with mock.patch.object(self.meter_dispatcher.conn,
'record_metering_data') as record_metering_data:
self.meter_dispatcher.record_metering_data(msg)