最近看了下关于OpenStack如何统计更新CPU、内存、硬盘等硬件资源的部分,由于历史原因下面的代码来自newton
版。
简单说,OpenStack先通过定时任务进行资源统计,入口代码位于nova\compute\manager.py
中ComputeManager
类的update_available_resource
函数。默认情况下每分钟更新一次:
@periodic_task.periodic_task(spacing=CONF.update_resources_interval)
def update_available_resource(self, context):
"""See driver.get_available_resource()
Periodic process that keeps that the compute host's understanding of
resource availability and usage in sync with the underlying hypervisor.
:param context: security context
"""
compute_nodes_in_db = self._get_compute_nodes_in_db(context,
use_slave=True)
nodenames = set(self.driver.get_available_nodes())
for nodename in nodenames:
self.update_available_resource_for_node(context, nodename)
self._resource_tracker_dict = {
k: v for k, v in self._resource_tracker_dict.items()
if k in nodenames}
# Delete orphan compute node not reported by driver but still in db
for cn in compute_nodes_in_db:
if cn.hypervisor_hostname not in nodenames:
LOG.info(_LI("Deleting orphan compute node %s"), cn.id)
cn.destroy()
首先获取所有节点,然后维护了一个名为_resource_tracker_dict
的字典用来记录host和ResourceTracker
实例的对应关系,所有的资源更新行为都在ResourceTracker
中进行处理。