如何让nova-compute 可以访问数据库

在nova-compute和nova-conductor 中都会调用Instance.save() 方法更改数据库。该类的定义在nova/objects/instance.py 中。

例如:nova-compute调用Instance.save() 方法更改数据库

nova-conductor调用Instance.save() 方法更改数据库

但是我们说过只有nova-conductor能访问数据库为什么呢?
nova/objects/instance.py 中的save方法定义如下。注意这里有个装饰器base.remotable
    @base.remotable
    def save(self, expected_vm_state=None,
             expected_task_state=None, admin_state_reset=False):
        """Save updates to this instance
而nova-computer 不能访问数据库的原因就在这个装饰器中
nova/objects/base.py 
def remotable(fn):
    @functools.wraps(fn)
    def wrapper(self, *args, **kwargs):

        if NovaObject.indirection_api:
            updates, result = NovaObject.indirection_api.object_action(
                ctxt, self, fn.__name__, args, kwargs)
            return result
        else:
            return fn(self, ctxt, *args, **kwargs)

原来能否直接访问数据库要看NovaObject.indirection_api 是否为true
而从nova-computer的入口函数可以看到nova/cmd/compute.py
 def main():
     config.parse_args(sys.argv)
     logging.setup(CONF, 'nova')
     priv_context.init(root_helper=shlex.split(utils.get_root_helper()))
     utils.monkey_patch()
     objects.register_all()
 
     gmr.TextGuruMeditation.setup_autorun(version)
 //如果没有定义CONF.conductor.use_local的话,则给objects_base.NovaObject.indirection_api 赋值
     if not CONF.conductor.use_local:
         cmd_common.block_db_access('nova-compute')
         objects_base.NovaObject.indirection_api = \
             conductor_rpcapi.ConductorAPI()
     else:
         LOG.warning(_LW('Conductor local mode is deprecated and will '
                         'be removed in a subsequent release'))
 
     server = service.Service.create(binary='nova-compute',
                                     topic=CONF.compute_topic,
                                     db_allowed=CONF.conductor.use_local)

所以只有在nova-computer 给objects_base.NovaObject.indirection_api 赋值了。而nova-conductor 没有赋值,所以instance.save() 才可以访问数据库
同理可以知道,如果要让nova-computer 可以访问数据库,则需要CONF.conductor.use_local 为true


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值