Reduce the query time of the instances when call get_instance_list()

The problem is that watcher is passing limit=-1 to novaclient when
listing servers which will always make at least two API calls to be
sure it's done paging:

https://github.com/openstack/python-novaclient/blob/13.0.1/novaclient/v2/servers.py#L896

If we can determine before we list servers that there are only a
certain number where the number of servers is less than 1000. For
example: 4, we should just pass the limit=len(servers) to novaclient
and avoid the second call for paging which takes extra time and
yields no results.

Change-Id: I797ad934a0f8496dbcbf65798e28b0443f238137
Closes-Bug: #1834679
This commit is contained in:
chenke
2019-06-28 14:43:57 +08:00
parent 1b41d92b9e
commit 1e8b17ac46
3 changed files with 43 additions and 5 deletions

View File

@@ -324,8 +324,18 @@ class ModelBuilder(object):
return
host = node.service["host"]
compute_node = self.model.get_node_by_uuid(host)
filters = {'host': host}
limit = len(instances) if len(instances) <= 1000 else -1
# Get all servers on this compute host.
instances = self.nova_helper.get_instance_list({'host': host})
# Note that the advantage of passing the limit parameter is
# that it can speed up the call time of novaclient. 1000 is
# the default maximum number of return servers provided by
# compute API. If we need to request more than 1000 servers,
# we can set limit=-1. For details, please see:
# https://bugs.launchpad.net/watcher/+bug/1834679
instances = self.nova_helper.get_instance_list(
filters=filters,
limit=limit)
for inst in instances:
# Add Node
instance = self._build_instance_node(inst)