在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 就是去除不活躍的節點.