一、resource_tracker
resource_tracker.py文件中保存了各种与资源扣除相关的函数,原理都是一样的,更新计算节点的资源,保证资源的一致性,常见的有instance_claim、rebuild_claim、resize_claim等,本文以instance_claim为例进行讲解。每一个计算节点都运行nova-compute服务,因此每一个计算节点维持一个自己的rt用于更新自身的资源信息。
二、具体过程
1、instance_claim用于创建虚拟机的时候进行资源的计算、资源的扣除等操作,该函数中的操作具有事务性,即资源的计算、更新等操作必须一起完成,不能被中断,不是具有事务性则无法保证资源更新的正确性,因此该函数的执行会添加一个事务锁,保证事务性。@utils.synchronized(COMPUTE_RESOURCE_SEMAPHORE, fair=True)
def instance_claim(self, context, instance, nodename, allocations,
limits=None):
if self.disabled(nodename):
self._set_instance_host_and_node(instance, nodename)
return claims.NopClaim()
# sanity checks:
if instance.host:
LOG.warning("Host field should not be set on the instance "
"until resources have been claimed.",
instance=instance)
if instance.node:
LOG.warning("Node field should not be set on the instance "
"until resources have been claimed.",
instance=instance)
cn = self.compute_nodes[nodename]
pci_requests = instance.pci_requests
#创建一个Claim对象,用于判断虚拟机需要的资源计算节点能否满足
claim = claims.Claim(context, instance, nodename, self, cn,
pci_requests, limits=limits)
# self._set_instance_host_and_node() will save instance to the DB
# so set instance.numa_topology first. We need to make sure
# that numa_topology is saved whi