在filter_scheduler.py 的_schedule 中会调用get_filtered_hosts 来对host 进行过滤。
hosts = self.host_manager.get_filtered_hosts(hosts,
spec_obj, index=num)
可以看到get_filtered_hosts是在host_manager中实现的
def get_filtered_hosts(self, hosts, spec_obj, index=0):
"""Filter hosts and return only ones passing all filters."""
return self.filter_handler.get_filtered_objects(self.default_filters,
hosts, spec_obj, index)
最终调用get_filtered_objects其是在nova/filers.py中的BaseFilterHandler
def get_filtered_objects(self, filters, objs, spec_obj, index=0):
list_objs = list(objs)
LOG.debug("Starting with %d host(s)", len(list_objs))
part_filter_results = []
full_filter_results = []
log_msg = "%(cls_name)s: (start: %(start)s, end: %(end)s)"
for filter_ in filters:
if filter_.run_filter_for_index(index):
cls_name = filter_.__class__.__name__
start_count = len(list_objs)
objs = filter_.filter_all(list_objs, spec_obj)
if objs is None:
LOG.debug("Filter %s says to stop filtering", cls_name)
return
list_objs = list(objs)
end_count = len(list_objs)
part_filter_results.append(log_msg % {"cls_name": cls_name,
"start": start_count, "end": end_count})
if list_objs:
remaining = [(getattr(obj, "host", obj),
getattr(obj, "nodename", ""))
for obj in list_objs]
full_filter_results.append((cls_name, remaining))
else:
LOG.info(_LI("Filter %s returned 0 hosts"), cls_name)
full_filter_results.append((cls_name, None))
break
LOG.debug("Filter %(cls_name)s returned "
"%(obj_len)d host(s)",
{'cls_name': cls_name, 'obj_len': len(list_objs)})
if not list_objs:
# Log the filtration history
# NOTE(sbauza): Since the Cells scheduler still provides a legacy
# dictionary for filter_props, and since we agreed on not modifying
# the Cells scheduler to support that because of Cells v2, we
# prefer to define a compatible way to address both types
if isinstance(spec_obj, dict):
rspec = spec_obj.get("request_spec", {})
inst_props = rspec.get("instance_properties", {})
inst_uuid = inst_props.get("uuid", "")
else:
inst_uuid = spec_obj.instance_uuid
msg_dict = {"inst_uuid": inst_uuid,
"str_results": str(full_filter_results),
}
full_msg = ("Filtering removed all hosts for the request with "
"instance ID "
"'%(inst_uuid)s'. Filter results: %(str_results)s"
) % msg_dict
msg_dict["str_results"] = str(part_filter_results)
part_msg = _LI("Filtering removed all hosts for the request with "
"instance ID "
"'%(inst_uuid)s'. Filter results: %(str_results)s"
) % msg_dict
LOG.debug(full_msg)
LOG.info(part_msg)
return list_objs
关键是调用 objs = filter_.filter_all(list_objs, spec_obj),而filter_all 也在这这个文件中的BaseFilter类中
class BaseFilter(object):
"""Base class for all filter classes."""
def _filter_one(self, obj, spec_obj):
"""Return True if it passes the filter, False otherwise.
Override this in a subclass.
"""
return True
def filter_all(self, filter_obj_list, spec_obj):
"""Yield objects that pass the filter.
Can be overridden in a subclass, if you need to base filtering
decisions on all objects. Otherwise, one can just override
_filter_one() to filter a single object.
"""
for obj in filter_obj_list:
if self._filter_one(obj, spec_obj):
yield obj
而在./scheduler/filters/__init__.py 中BaseHostFilter继承了filters.BaseFilter,因此会调用BaseHostFilter中的_filter_one,这个_filter_one 又调用host_passes
class BaseHostFilter(filters.BaseFilter):
"""Base class for host filters."""
def _filter_one(self, obj, filter_properties):
"""Return True if the object passes the filter, otherwise False."""
return self.host_passes(obj, filter_properties)
def host_passes(self, host_state, filter_properties):
"""Return True if the HostState passes the filter, otherwise False.
Override this in a subclass.
"""
raise NotImplementedError()
因此后面的filter都会继承BaseHostFilter,且必须实现host_passes。以core_filter.py为例
class BaseCoreFilter(filters.BaseHostFilter):
def _get_cpu_allocation_ratio(self, host_state, spec_obj):
raise NotImplementedError
def host_passes(self, host_state, spec_obj):
"""Return True if host has sufficient CPU cores.
:param host_state: nova.scheduler.host_manager.HostState
:param spec_obj: filter options
:return: boolean
"""
if not host_state.vcpus_total:
# Fail safe
LOG.warning(_LW("VCPUs not set; assuming CPU collection broken"))
return True
instance_vcpus = spec_obj.vcpus
cpu_allocation_ratio = self._get_cpu_allocation_ratio(host_state,
spec_obj)
vcpus_total = host_state.vcpus_total * cpu_allocation_ratio
# Only provide a VCPU limit to compute if the virt driver is reporting
# an accurate count of installed VCPUs. (XenServer driver does not)
if vcpus_total > 0:
host_state.limits['vcpu'] = vcpus_total
# Do not allow an instance to overcommit against itself, only
# against other instances.
if instance_vcpus > host_state.vcpus_total:
LOG.debug("%(host_state)s does not have %(instance_vcpus)d "
"total cpus before overcommit, it only has %(cpus)d",
{'host_state': host_state,
'instance_vcpus': instance_vcpus,
'cpus': host_state.vcpus_total})
return False
free_vcpus = vcpus_total - host_state.vcpus_used
if free_vcpus < instance_vcpus:
LOG.debug("%(host_state)s does not have %(instance_vcpus)d "
"usable vcpus, it only has %(free_vcpus)d usable "
"vcpus",
{'host_state': host_state,
'instance_vcpus': instance_vcpus,
'free_vcpus': free_vcpus})
return False
return True
可见返回false的case就是instance_vcpus > host_state.vcpus_total:也就是需要的cpu大于总的可以提供的cpu,free_vcpus < instance_vcpus即空闲的cpu小于虚拟机要求的cpu.可见filter并没有什么神秘的,就是简单的数量比较而已