From 9c2252ea88e0fb331d25198ba7286680d92d9ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harald=20Jens=C3=A5s?= Date: Tue, 7 Feb 2023 10:36:22 +0100 Subject: [PATCH] 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 --- metalsmith/_instance.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/metalsmith/_instance.py b/metalsmith/_instance.py index 4b12eec..c269a69 100644 --- a/metalsmith/_instance.py +++ b/metalsmith/_instance.py @@ -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)