From 7a77dd74964e3ae46416d9fecbcf9cd3f5cb7900 Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Wed, 9 Jan 2013 13:42:32 -0500 Subject: Create a directory for servicegroup drivers. Right now there is only one servicegroup driver, the db backed driver. Create a directory for drivers to reside in to keep things a bit more tidy as we add additional drivers. Part of blueprint rpc-based-servicegroup-driver. Change-Id: Ib563e1a8d184cef838e5730b2fc6904940d04c21 --- nova/servicegroup/api.py | 4 +- nova/servicegroup/db_driver.py | 101 ---------------------------------- nova/servicegroup/drivers/__init__.py | 0 nova/servicegroup/drivers/db.py | 101 ++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 102 deletions(-) delete mode 100644 nova/servicegroup/db_driver.py create mode 100644 nova/servicegroup/drivers/__init__.py create mode 100644 nova/servicegroup/drivers/db.py diff --git a/nova/servicegroup/api.py b/nova/servicegroup/api.py index b9653e1e2..ebd0ee6ac 100644 --- a/nova/servicegroup/api.py +++ b/nova/servicegroup/api.py @@ -39,7 +39,9 @@ CONF.register_opt(servicegroup_driver_opt) class API(object): _driver = None - _driver_name_class_mapping = {"db": "nova.servicegroup.db_driver.DbDriver"} + _driver_name_class_mapping = { + 'db': 'nova.servicegroup.drivers.db.DbDriver' + } @lockutils.synchronized('nova.servicegroup.api.new', 'nova-') def __new__(cls, *args, **kwargs): diff --git a/nova/servicegroup/db_driver.py b/nova/servicegroup/db_driver.py deleted file mode 100644 index 075db3ed8..000000000 --- a/nova/servicegroup/db_driver.py +++ /dev/null @@ -1,101 +0,0 @@ -# Copyright (c) IBM 2012 Pavel Kravchenco -# Alexey Roytman -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -# implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from nova import context -from nova import db -from nova import exception -from nova.openstack.common import cfg -from nova.openstack.common import log as logging -from nova.openstack.common import timeutils -from nova.servicegroup import api -from nova import utils - - -CONF = cfg.CONF -CONF.import_opt('service_down_time', 'nova.service') - -LOG = logging.getLogger(__name__) - - -class DbDriver(api.ServiceGroupDriver): - - def join(self, member_id, group_id, service=None): - """Join the given service with it's group.""" - - msg = _('DB_Driver: join new ServiceGroup member %(member_id)s to ' - 'the %(group_id)s group, service = %(service)s') - LOG.debug(msg, locals()) - if service is None: - raise RuntimeError(_('service is a mandatory argument for DB based' - ' ServiceGroup driver')) - report_interval = service.report_interval - if report_interval: - pulse = utils.FixedIntervalLoopingCall(self._report_state, service) - pulse.start(interval=report_interval, - initial_delay=report_interval) - return pulse - - def is_up(self, service_ref): - """Moved from nova.utils - Check whether a service is up based on last heartbeat. - """ - last_heartbeat = service_ref['updated_at'] or service_ref['created_at'] - # Timestamps in DB are UTC. - elapsed = utils.total_seconds(timeutils.utcnow() - last_heartbeat) - LOG.debug('DB_Driver.is_up last_heartbeat = %(lhb)s elapsed = %(el)s', - {'lhb': str(last_heartbeat), 'el': str(elapsed)}) - return abs(elapsed) <= CONF.service_down_time - - def get_all(self, group_id): - """ - Returns ALL members of the given group - """ - LOG.debug(_('DB_Driver: get_all members of the %s group') % group_id) - rs = [] - ctxt = context.get_admin_context() - for service in db.service_get_all_by_topic(ctxt, group_id): - if self.is_up(service): - rs.append(service['host']) - return rs - - def _report_state(self, service): - """Update the state of this service in the datastore.""" - ctxt = context.get_admin_context() - state_catalog = {} - try: - try: - service_ref = db.service_get(ctxt, service.service_id) - except exception.NotFound: - LOG.debug(_('The service database object disappeared, ' - 'Recreating it.')) - service._create_service_ref(ctxt) - service_ref = db.service_get(ctxt, service.service_id) - - state_catalog['report_count'] = service_ref['report_count'] + 1 - - db.service_update(ctxt, - service.service_id, state_catalog) - - # TODO(termie): make this pattern be more elegant. - if getattr(service, 'model_disconnected', False): - service.model_disconnected = False - LOG.error(_('Recovered model server connection!')) - - # TODO(vish): this should probably only catch connection errors - except Exception: # pylint: disable=W0702 - if not getattr(service, 'model_disconnected', False): - service.model_disconnected = True - LOG.exception(_('model server went away')) diff --git a/nova/servicegroup/drivers/__init__.py b/nova/servicegroup/drivers/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/nova/servicegroup/drivers/db.py b/nova/servicegroup/drivers/db.py new file mode 100644 index 000000000..075db3ed8 --- /dev/null +++ b/nova/servicegroup/drivers/db.py @@ -0,0 +1,101 @@ +# Copyright (c) IBM 2012 Pavel Kravchenco +# Alexey Roytman +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from nova import context +from nova import db +from nova import exception +from nova.openstack.common import cfg +from nova.openstack.common import log as logging +from nova.openstack.common import timeutils +from nova.servicegroup import api +from nova import utils + + +CONF = cfg.CONF +CONF.import_opt('service_down_time', 'nova.service') + +LOG = logging.getLogger(__name__) + + +class DbDriver(api.ServiceGroupDriver): + + def join(self, member_id, group_id, service=None): + """Join the given service with it's group.""" + + msg = _('DB_Driver: join new ServiceGroup member %(member_id)s to ' + 'the %(group_id)s group, service = %(service)s') + LOG.debug(msg, locals()) + if service is None: + raise RuntimeError(_('service is a mandatory argument for DB based' + ' ServiceGroup driver')) + report_interval = service.report_interval + if report_interval: + pulse = utils.FixedIntervalLoopingCall(self._report_state, service) + pulse.start(interval=report_interval, + initial_delay=report_interval) + return pulse + + def is_up(self, service_ref): + """Moved from nova.utils + Check whether a service is up based on last heartbeat. + """ + last_heartbeat = service_ref['updated_at'] or service_ref['created_at'] + # Timestamps in DB are UTC. + elapsed = utils.total_seconds(timeutils.utcnow() - last_heartbeat) + LOG.debug('DB_Driver.is_up last_heartbeat = %(lhb)s elapsed = %(el)s', + {'lhb': str(last_heartbeat), 'el': str(elapsed)}) + return abs(elapsed) <= CONF.service_down_time + + def get_all(self, group_id): + """ + Returns ALL members of the given group + """ + LOG.debug(_('DB_Driver: get_all members of the %s group') % group_id) + rs = [] + ctxt = context.get_admin_context() + for service in db.service_get_all_by_topic(ctxt, group_id): + if self.is_up(service): + rs.append(service['host']) + return rs + + def _report_state(self, service): + """Update the state of this service in the datastore.""" + ctxt = context.get_admin_context() + state_catalog = {} + try: + try: + service_ref = db.service_get(ctxt, service.service_id) + except exception.NotFound: + LOG.debug(_('The service database object disappeared, ' + 'Recreating it.')) + service._create_service_ref(ctxt) + service_ref = db.service_get(ctxt, service.service_id) + + state_catalog['report_count'] = service_ref['report_count'] + 1 + + db.service_update(ctxt, + service.service_id, state_catalog) + + # TODO(termie): make this pattern be more elegant. + if getattr(service, 'model_disconnected', False): + service.model_disconnected = False + LOG.error(_('Recovered model server connection!')) + + # TODO(vish): this should probably only catch connection errors + except Exception: # pylint: disable=W0702 + if not getattr(service, 'model_disconnected', False): + service.model_disconnected = True + LOG.exception(_('model server went away')) -- cgit