Minor refactor of _setup_instance_group()

This patch is a minor refactoring of the _setup_instance_group()
method in the filter scheduler.  The majority of the method was inside
of a conditional.  This primarily reverses that and reduces the
nesting depth used.  There should be no functional changes at all in
this patch.

Change-Id: Iab4d63200653a4320c91dc1356091ff9d961cafd
Related-bug: #1379451
This commit is contained in:
Russell Bryant 2014-10-09 18:08:52 +00:00
parent f0624f4446
commit 0eb7f36cfc
1 changed files with 30 additions and 22 deletions

View File

@ -208,30 +208,38 @@ class FilterScheduler(driver.Scheduler):
filter_properties['os_type'] = os_type
def _setup_instance_group(self, context, filter_properties):
update_group_hosts = False
"""Update filter_properties with server group info.
:returns: True if filter_properties has been updated, False if not.
"""
scheduler_hints = filter_properties.get('scheduler_hints') or {}
group_hint = scheduler_hints.get('group', None)
if group_hint:
group = objects.InstanceGroup.get_by_hint(context, group_hint)
policies = set(('anti-affinity', 'affinity'))
if any((policy in policies) for policy in group.policies):
if ('affinity' in group.policies and
not self._supports_affinity):
msg = _("ServerGroupAffinityFilter not configured")
LOG.error(msg)
raise exception.NoValidHost(reason=msg)
if ('anti-affinity' in group.policies and
not self._supports_anti_affinity):
msg = _("ServerGroupAntiAffinityFilter not configured")
LOG.error(msg)
raise exception.NoValidHost(reason=msg)
update_group_hosts = True
filter_properties.setdefault('group_hosts', set())
user_hosts = set(filter_properties['group_hosts'])
group_hosts = set(group.get_hosts(context))
filter_properties['group_hosts'] = user_hosts | group_hosts
filter_properties['group_policies'] = group.policies
return update_group_hosts
if not group_hint:
return False
group = objects.InstanceGroup.get_by_hint(context, group_hint)
policies = set(('anti-affinity', 'affinity'))
if not any((policy in policies) for policy in group.policies):
return False
if ('affinity' in group.policies and
not self._supports_affinity):
msg = _("ServerGroupAffinityFilter not configured")
LOG.error(msg)
raise exception.NoValidHost(reason=msg)
if ('anti-affinity' in group.policies and
not self._supports_anti_affinity):
msg = _("ServerGroupAntiAffinityFilter not configured")
LOG.error(msg)
raise exception.NoValidHost(reason=msg)
filter_properties.setdefault('group_hosts', set())
user_hosts = set(filter_properties['group_hosts'])
group_hosts = set(group.get_hosts(context))
filter_properties['group_hosts'] = user_hosts | group_hosts
filter_properties['group_policies'] = group.policies
return True
def _schedule(self, context, request_spec, filter_properties):
"""Returns a list of hosts that meet the required specs,