天天看点

nova schedule 调度器之FilterSchedulerz中的 _get_all_host_states

在filter_scheduler.py 的_schedule 中首先会调用_get_all_host_states来获得所有host的状态。

    def _get_all_host_states(self, context):

        """Template method, so a subclass can implement caching."""

        return self.host_manager.get_all_host_states(context)

在_get_all_host_states 中调用host_manager的get_all_host_states

    def get_all_host_states(self, context):

        """Returns a list of HostStates that represents all the hosts

        the HostManager knows about. Also, each of the consumable resources

        in HostState are pre-populated and adjusted based on data in the db.

        """

//得到service list

        service_refs = {service.host: service

                        for service in objects.ServiceList.get_by_binary(

                            context, 'nova-compute', include_disabled=True)}

        # Get resource usage across the available compute nodes:

//获得所有的计算节点

        compute_nodes = objects.ComputeNodeList.get_all(context)

        seen_nodes = set()

        for compute in compute_nodes:

            service = service_refs.get(compute.host)

//如果计算节点中没有service 则继续下一次查找

            if not service:

                LOG.warning(_LW(

                    "No compute service record found for host %(host)s"),

                    {'host': compute.host})

                continue

            host = compute.host

            node = compute.hypervisor_hostname

//将从compute_nodes 得到的host和node组成一个tuble

            state_key = (host, node)

            host_state = self.host_state_map.get(state_key)

//如果host_state 为NULL的话,就用host和node 组成一个host_state 然后添加到host_state_map 中。其中

//    def host_state_cls(self, host, node, **kwargs):

 //       return HostState(host, node) 可见HostState,就是用host和node 来构建的.

            if not host_state:

                host_state = self.host_state_cls(host, node, compute=compute)

                self.host_state_map[state_key] = host_state

            # We force to update the aggregates info each time a new request

            # comes in, because some changes on the aggregates could have been

            # happening after setting this field for the first time

//更新host_state

            host_state.update(compute,

                              dict(service),

                              self._get_aggregates_info(host),

                              self._get_instance_info(context, compute))

            seen_nodes.add(state_key)

//去掉不活跃的节点

        # remove compute nodes from host_state_map if they are not active

        dead_nodes = set(self.host_state_map.keys()) - seen_nodes

        for state_key in dead_nodes:

            host, node = state_key

            LOG.info(_LI("Removing dead compute node %(host)s:%(node)s "

                         "from scheduler"), {'host': host, 'node': node})

            del self.host_state_map[state_key]

        return six.itervalues(self.host_state_map)

而 compute_nodes = objects.ComputeNodeList.get_all(context) 这句话是从数据库中查询信息

具体是在objects/compute_node.py 中的ComputeNodeList

    @base.remotable_classmethod

    def get_all(cls, context):

        db_computes = db.compute_node_get_all(context)

        return base.obj_make_list(context, cls(context), objects.ComputeNode,

                                  db_computes)

这也证明了schedule 不能直接访问数据库.

总结一下,_get_all_host_states 就是去除不活跃的节点.

继续阅读