在openstack上测试批量创建thin lun时,发现部分创建失败,分析代码发现,根本原因是主机的能力是周期性刷新(默认是1分钟),而不是即时刷新。现在来详细分析openstack的资源刷新机制,主要以cinder为例。
在cinder源代码中, 可以在很多函数上看到@periodic_task
这样的修饰符, 我们根据_publish_service_capabilities这个函数知道这是cinder的定时任务,可以让这个函数周期性执行, 但是不了解这个修饰符产生作用的原理和用法, 这里将进行详细分析。
关于元类:就像对象是类的实例一样,类是它的元类的实例。
在manager.py中更新函数的定义如下:@periodic_task.periodic_task说明该函数被装饰器装饰。被此装饰器修饰后,函数新增了这个属性:f._periodic_task = True。
@periodic_task.periodic_task
def _publish_service_capabilities(self, context):
"""Pass data back to the scheduler at a periodic interval."""
if self.last_capabilities:
LOG.debug('Notifying Schedulers of capabilities ...')
self.scheduler_rpcapi.updat