Use a network cache in Instance

When listing NICs for an instance populate a class global
cache of networks by network_id. When listing nodes with VIFs
on the same network this reduces the amount of calls to the
network service.

250 Nodes without patch: real    6m29.342s
250 Nodes with patch:    real    5m26.544s

Story: 2010571
Task: 47301
Change-Id: I99a2d8ecab657c8e4c852c73e816a5a8f2856471
This commit is contained in:
Harald Jensås 2023-02-07 10:36:22 +01:00
parent a5fffb9242
commit 9c2252ea88
1 changed files with 6 additions and 2 deletions

View File

@ -73,6 +73,8 @@ _DEPLOYED_STATES = frozenset([InstanceState.ACTIVE, InstanceState.MAINTENANCE])
class Instance(object):
"""Instance status in metalsmith."""
network_cache = dict()
def __init__(self, connection, node, allocation=None):
self._connection = connection
self._uuid = node.id
@ -124,8 +126,10 @@ class Instance(object):
for vif in vifs:
try:
port = self._connection.network.get_port(vif)
port.network = self._connection.network.get_network(
port.network_id)
if port.network_id not in Instance.network_cache:
Instance.network_cache[port.network_id] = (
self._connection.network.get_network(port.network_id))
port.network = Instance.network_cache[port.network_id]
result.append(port)
except os_exc.ResourceNotFound:
LOG.warning('vif has missing port: %s', vif)