Pull singleton config check cruft out of SG API

We were going through a bunch of trouble to make the servicegroup API
object a singleton, and only check configuration options once. This made
the unit tests for the servicegroup drivers more complex than they
needed to be. In this patch, we just change the SG API object's
constructor to check the appropriate servicegroup configuration options
as it needs to, and we adapt the unit tests for all of the SG drivers to
not involve the Service object at all.

The memcache and DB driver unit tests are converted to NoDBTestCase
derivatives, reducing the runtime for the two test cases from around 8
seconds (~1 second per test method) to less than 0.20 seconds.

NOTE: The ZooKeeper driver's unit test never runs at all. It is always
skipped.

NOTE: The next patch in this series refactors the service group API
to make the behaviour of all drivers consistent. Currently, the
Zookeeper driver has different behaviour from the other drivers in the
get_all() method: it raises exception.ServiceGroupUnavailable() if no
services are found, unlike the DB and MC drivers, which just return an
empty list. Similarly, the ZK driver's join() method returns a
FakeLoopingCall() object for some reason, while the other drivers return
None.

Related blueprint: servicegroup-api-is-up-host-topic

Change-Id: Id84ab3fdfcd6b45e1015d6dac9faec5c01fa42ad
This commit is contained in:
Jay Pipes
2015-01-25 12:05:06 -08:00
committed by EdLeafe
parent a4f9c1b9f4
commit f05eecbc3f
6 changed files with 159 additions and 324 deletions

View File

@@ -23,6 +23,12 @@ from nova.i18n import _, _LW
from nova.openstack.common import log as logging
LOG = logging.getLogger(__name__)
_driver_name_class_mapping = {
'db': 'nova.servicegroup.drivers.db.DbDriver',
'zk': 'nova.servicegroup.drivers.zk.ZooKeeperDriver',
'mc': 'nova.servicegroup.drivers.mc.MemcachedDriver'
}
_default_driver = 'db'
servicegroup_driver_opt = cfg.StrOpt('servicegroup_driver',
default=_default_driver,
@@ -39,42 +45,12 @@ INITIAL_REPORTING_DELAY = 5
class API(object):
_driver = None
_driver_name_class_mapping = {
'db': 'nova.servicegroup.drivers.db.DbDriver',
'zk': 'nova.servicegroup.drivers.zk.ZooKeeperDriver',
'mc': 'nova.servicegroup.drivers.mc.MemcachedDriver'
}
def __new__(cls, *args, **kwargs):
def __init__(self, *args, **kwargs):
'''Create an instance of the servicegroup API.
args and kwargs are passed down to the servicegroup driver when it gets
created. No args currently exist, though. Valid kwargs are:
db_allowed - Boolean. False if direct db access is not allowed and
alternative data access (conductor) should be used
instead.
created.
'''
if not cls._driver:
LOG.debug('ServiceGroup driver defined as an instance of %s',
str(CONF.servicegroup_driver))
driver_name = CONF.servicegroup_driver
try:
driver_class = cls._driver_name_class_mapping[driver_name]
except KeyError:
raise TypeError(_("unknown ServiceGroup driver name: %s")
% driver_name)
cls._driver = importutils.import_object(driver_class,
*args, **kwargs)
return super(API, cls).__new__(cls)
def __init__(self, *args, **kwargs):
self.basic_config_check()
def basic_config_check(self):
"""Perform basic config check."""
# Make sure report interval is less than service down time
report_interval = CONF.report_interval
if CONF.service_down_time <= report_interval:
@@ -88,6 +64,16 @@ class API(object):
'report_interval': report_interval,
'new_service_down_time': new_service_down_time})
CONF.set_override('service_down_time', new_service_down_time)
LOG.debug('ServiceGroup driver defined as an instance of %s',
str(CONF.servicegroup_driver))
driver_name = CONF.servicegroup_driver
try:
driver_class = _driver_name_class_mapping[driver_name]
except KeyError:
raise TypeError(_("unknown ServiceGroup driver name: %s")
% driver_name)
self._driver = importutils.import_object(driver_class,
*args, **kwargs)
def join(self, member_id, group_id, service=None):
"""Add a new member to the ServiceGroup