天天看点

nova schedule 调度器之FilterSchedulerz中的 get_weighed_hosts

在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

继续阅读