Optimize NovaClusterDataModelCollector.add_instance_node

This does two things:

1. Rather than make an API call per server on the host,
   get all of the servers in a single API call by
   filtering on the host. The os-hypervisors API results
   to use make this require a bit of refactoring since
   get_compute_node_by_name does not have the service
   entry in it and get_compute_node_by_id does not have the
   servers entry in it. A TODO is added to clean that up
   with a single call to os-hypervisors once we have the
   support in python-novaclient.

2. Pulls get_node_by_uuid() out of the loop.

A test is added for the nova_helper get_instance_list method
since one did not exist before.

The fake compute node mocks in test_nova_cdmc_execute are
also cleaned up since, as noted above, get_compute_node_by_name
and get_compute_node_by_id don't both return all the details.

Change-Id: Ifd9f83c2f399d4c1765b0c520f4d5a62ad0f5fbd
This commit is contained in:
Matt Riedemann
2019-05-16 19:25:56 -04:00
committed by Sergey Slabnov
parent 3b9364d4c7
commit fdea38fb06
4 changed files with 71 additions and 33 deletions

View File

@@ -258,17 +258,25 @@ class ModelBuilder(object):
[node.hypervisor_hostname for node in all_nodes])
LOG.debug("compute nodes: %s", compute_nodes)
for node_name in compute_nodes:
# TODO(mriedem): Change this to list hypervisors with details
# so we don't have to call get_compute_node_by_id. It requires
# changes to python-novaclient.
cnode = self.nova_helper.get_compute_node_by_name(node_name,
servers=True)
if cnode:
self.add_compute_node(cnode[0])
self.add_instance_node(cnode[0])
# Get the node details (like the service.host).
node_info = self.nova_helper.get_compute_node_by_id(
cnode[0].id)
self.add_compute_node(node_info)
# node.servers is a list of server objects
# New in nova version 2.53
instances = getattr(cnode[0], "servers", None)
self.add_instance_node(node_info, instances)
def add_compute_node(self, node):
# Build and add base node.
node_info = self.nova_helper.get_compute_node_by_id(node.id)
LOG.debug("node info: %s", node_info)
compute_node = self.build_compute_node(node_info)
LOG.debug("node info: %s", node)
compute_node = self.build_compute_node(node)
self.model.add_node(compute_node)
# NOTE(v-francoise): we can encapsulate capabilities of the node
@@ -314,26 +322,18 @@ class ModelBuilder(object):
# node_attributes)
return compute_node
def add_instance_node(self, node):
# node.servers is a list of server objects
# New in nova version 2.53
instances = getattr(node, "servers", None)
def add_instance_node(self, node, instances):
if instances is None:
# no instances on this node
return
instance_uuids = [s['uuid'] for s in instances]
for uuid in instance_uuids:
try:
inst = self.nova_helper.find_instance(uuid)
except Exception as exc:
LOG.exception(exc)
continue
host = node.service["host"]
compute_node = self.model.get_node_by_uuid(host)
# Get all servers on this compute host.
instances = self.nova_helper.get_instance_list({'host': host})
for inst in instances:
# Add Node
instance = self._build_instance_node(inst)
self.model.add_instance(instance)
cnode_uuid = getattr(inst, "OS-EXT-SRV-ATTR:host")
compute_node = self.model.get_node_by_uuid(
cnode_uuid)
# Connect the instance to its compute node
self.model.map_instance(instance, compute_node)