在filter_scheduler.py 的_schedule 中會調用get_weighed_hosts 來為host計算權重
weighed_hosts = self.host_manager.get_weighed_hosts(hosts,spec_obj)
具體實作在host_manager.py的HostManager 類中
def get_weighed_hosts(self, hosts, spec_obj):
"""Weigh the hosts."""
return self.weight_handler.get_weighed_objects(self.weighers,
hosts, spec_obj)
get_weighed_objects 的實作在nova/weights.py 中
class BaseWeightHandler(loadables.BaseLoader):
object_class = WeighedObject
def get_weighed_objects(self, weighers, obj_list, weighing_properties):
"""Return a sorted (descending), normalized list of WeighedObjects."""
weighed_objs = [self.object_class(obj, 0.0) for obj in obj_list]
if len(weighed_objs) <= 1:
return weighed_objs
for weigher in weighers:
weights = weigher.weigh_objects(weighed_objs, weighing_properties)
# Normalize the weights
weights = normalize(weights,
minval=weigher.minval,
maxval=weigher.maxval)
for i, weight in enumerate(weights):
obj = weighed_objs[i]
obj.weight += weigher.weight_multiplier() * weight
//将個個權重計算出來後,再進行排序,看起來還是逆序的
return sorted(weighed_objs, key=lambda x: x.weight, reverse=True)
最關鍵的一句是調用weigh_objects,這個同樣在nova/weights.py 中的BaseWeigher 類中
def weigh_objects(self, weighed_obj_list, weight_properties):
"""Weigh multiple objects.
Override in a subclass if you need access to all objects in order
to calculate weights. Do not modify the weight of an object here,
just return a list of weights.
"""
# Calculate the weights
weights = []
for obj in weighed_obj_list:
weight = self._weigh_object(obj.obj, weight_properties)
weights.append(weight)
return weights
在weigh_objects 中又調用_weigh_object,這就回到了schedule/weights 中的__init__.py中的
class BaseHostWeigher(weights.BaseWeigher):
"""Base class for host weights."""
pass
這個是個空函數,但是卻是weights.BaseWeigher的子類,是以要實作對host的權重的計算必須是BaseHostWeigher的子類且實作了_weigh_object。這裡以disk.py 為例看看如果計算權重
class DiskWeigher(weights.BaseHostWeigher):
minval = 0
def weight_multiplier(self):
"""Override the weight multiplier."""
return CONF.disk_weight_multiplier
def _weigh_object(self, host_state, weight_properties):
"""Higher weights win. We want spreading to be the default."""
return host_state.free_disk_mb
原來就是直接傳回系統剩餘的disk 啊host_state.free_disk_mb