nova multi-show 用一次查询查出所给的所有虚拟机信息;即 nova multi-show 查询10个虚拟机和调用10次 nova show 查询的区别就是减小了对数据库的请求开销。如果同时有100个用户作出了查询功能,假设查询了10台,那么 nova multi-show 则减少了900次对数据库的请求操作。
defmulti_show(self, ctxt, uuids, want_objects=False): """Get multi instances with the given uuids.""" # NOTE(ameade): we still need to support integer ids for ec2 # 以下是查询数据库时需要额外查询的表 expected_attrs = ['metadata', 'system_metadata', 'security_groups', 'info_cache'] try: # 请注意!这里将进入multi_show的核心方法。。。 instance = instance_obj.Instance.get_by_uuids(ctxt, uuids, expected_attrs=expected_attrs)
return result except DataError: # NOTE(sdague): catch all in case the db engine chokes on the # id because it's too long of an int to store. msg = _("Invalid instance id %s in request") % str(uuids) LOG.warn(msg) raise exception.InvalidID(id=str(uuids))
做完修饰工作
nova 对 list 、 show 的返回值都是有再加工的操作,举个简单的例子:
当你查询某个虚拟机详情( nova show )的时候,数据库中的 vm_state 字段和 task_state 字段不是直接打印在终端,而是诸如 OS-EXT-STS:vm_state 、 OS-EXT-STS:task_state 这样的显示,“ OS-EXT-STS ”即为 nova 对返回数据字段的加工操作
def_extend_server(self, server, instance): for state in ['task_state', 'vm_state', 'power_state']: key = "%s:%s" % (Extended_status.alias, state) server[key] = instance[state]
@wsgi.extends defshow(self, req, resp_obj, id): context = req.environ['nova.context'] if authorize(context): # Attach our slave template to the response object resp_obj.attach(xml=ExtendedStatusTemplate()) server = resp_obj.obj['server'] db_instance = req.get_db_instance(server['id']) # server['id'] is guaranteed to be in the cache due to # the core API adding it in its 'show' method. self._extend_server(server, db_instance)
@wsgi.extends defmulti_show(self, req, resp_obj, id, body): context = req.environ['nova.context'] if authorize(context): # Attach our slave template to the response object resp_obj.attach(xml=ExtendedStatusTemplate()) servers = resp_obj.obj['server_info'] for i inrange(len(servers)): server = servers[i] db_instance = req.get_db_instance(server['id']) # server['id'] is guaranteed to be in the cache due to # the core API adding it in its 'show' method. self._extend_server(server, db_instance)