Skip to content

Commit

Permalink
[scheduler] Consolidate host_info/passes steps in filter & weigher
Browse files Browse the repository at this point in the history
Both host_info_requiring_instance_ids as well as host_passes/_weigh_object
had duplicated code for extracting the instance-ids needed
By consolidating them we reduce the code duplication.

Change-Id: Icfc1d3e554ff0834dec35d52772996284dc0a5da
  • Loading branch information
fwiesel committed Oct 5, 2022
1 parent bf5fe10 commit fe75123
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
19 changes: 11 additions & 8 deletions nova/scheduler/filters/affinity_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DifferentHostFilter(filters.BaseHostFilter):
RUN_ON_REBUILD = False

def host_passes(self, host_state, spec_obj):
affinity_uuids = spec_obj.get_scheduler_hint('different_host')
affinity_uuids = self.host_info_requiring_instance_ids(spec_obj)
if affinity_uuids:
overlap = utils.instance_uuids_overlap(host_state, affinity_uuids)
return not overlap
Expand All @@ -53,7 +53,7 @@ class SameHostFilter(filters.BaseHostFilter):
RUN_ON_REBUILD = False

def host_passes(self, host_state, spec_obj):
affinity_uuids = spec_obj.get_scheduler_hint('same_host')
affinity_uuids = self.host_info_requiring_instance_ids(spec_obj)
if affinity_uuids:
overlap = utils.instance_uuids_overlap(host_state, affinity_uuids)
return overlap
Expand Down Expand Up @@ -93,24 +93,25 @@ class _GroupAntiAffinityFilter(filters.BaseHostFilter):
RUN_ON_REBUILD = False

def host_passes(self, host_state, spec_obj):
# Only invoke the filter if 'anti-affinity' is configured
instance_group = spec_obj.instance_group
policy = instance_group.policy if instance_group else None
if self.policy_name != policy:
members = self.host_info_requiring_instance_ids(spec_obj)
# Only invoke the filter if 'anti-affinity' is configured,
# and there are any instances to consider
if not members:
return True

# NOTE(hanrong): Move operations like resize can check the same source
# compute node where the instance is. That case, AntiAffinityFilter
# must not return the source as a non-possible destination.
if spec_obj.instance_uuid in host_state.instances.keys():
return True

# The list of instances UUIDs on the given host
instances = set(host_state.instances.keys())
# The list of instances UUIDs which are members of this group
members = set(spec_obj.instance_group.members)
# The set of instances on the host that are also members of this group
servers_on_host = instances.intersection(members)

instance_group = spec_obj.instance_group

rules = instance_group.rules
if rules and 'max_server_per_host' in rules:
max_server_per_host = rules['max_server_per_host']
Expand All @@ -137,9 +138,11 @@ def host_passes(self, host_state, spec_obj):
def host_info_requiring_instance_ids(self, spec_obj):
instance_group = spec_obj.instance_group
policy = instance_group.policy if instance_group else None

if self.policy_name != policy:
return set()

# The list of instances UUIDs which are members of this group
return set(spec_obj.instance_group.members)


Expand Down
9 changes: 2 additions & 7 deletions nova/scheduler/weights/affinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,11 @@ class _SoftAffinityWeigherBase(weights.BaseHostWeigher):

def _weigh_object(self, host_state, request_spec):
"""Higher weights win."""
if not request_spec.instance_group:
return 0

policy = request_spec.instance_group.policy

if self.policy_name != policy:
members = self.host_info_requiring_instance_ids(request_spec)
if not members:
return 0

instances = set(host_state.instances.keys())
members = set(request_spec.instance_group.members)
member_on_host = instances.intersection(members)

return len(member_on_host)
Expand Down

0 comments on commit fe75123

Please sign in to comment.