Skip to content

Commit 04f29e4

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Cache [pci]alias parsing" into stable/2025.1
2 parents 896db4e + 456a57c commit 04f29e4

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

nova/pci/request.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
These two aliases define a device request meaning: vendor_id is "8086" and
3838
product_id is "0442" or "0443".
3939
"""
40-
40+
import functools
4141
import typing as ty
4242

4343
import jsonschema
@@ -173,6 +173,7 @@ def _validate_aliases(aliases):
173173
_validate_required_ids(aliases)
174174

175175

176+
@functools.cache
176177
def get_alias_from_config() -> Alias:
177178
"""Parse and validate PCI aliases from the nova config.
178179

nova/test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
from nova import exception
6262
from nova import objects
6363
from nova.objects import base as objects_base
64+
from nova.pci import request
6465
from nova import quota
6566
from nova.scheduler.client import report
6667
from nova.scheduler import utils as scheduler_utils
@@ -189,6 +190,10 @@ def setUp(self):
189190
self.useFixture(
190191
nova_fixtures.PropagateTestCaseIdToChildEventlets(self.id()))
191192

193+
# Ensure that the pci alias is reset between test cases running in
194+
# the same process
195+
request.get_alias_from_config.cache_clear()
196+
192197
# How many of which service we've started. {$service-name: $count}
193198
self._service_fixture_count = collections.defaultdict(int)
194199

@@ -425,6 +430,10 @@ def flags(self, **kw):
425430
group = kw.pop('group', None)
426431
for k, v in kw.items():
427432
CONF.set_override(k, v, group)
433+
# loading and validating alias is cached so if it is reconfigured
434+
# we need to reset the cache
435+
if k == 'alias' and group == 'pci':
436+
request.get_alias_from_config.cache_clear()
428437

429438
def reset_flags(self, *k, **kw):
430439
"""Reset flag variables for a test."""

nova/tests/unit/pci/test_request.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,23 @@ def test_get_alias_from_config_missing_ids_or_rc_pci_in_placement(self):
342342
"product_id fields set or resource_class field set.",
343343
str(ex))
344344

345+
def test_get_alias_from_config_cached(self):
346+
alias = jsonutils.dumps({
347+
"name": "a5",
348+
"vendor_id": "4444",
349+
"product_id": "4444",
350+
})
351+
self.flags(alias=[alias], group='pci')
352+
353+
origi_loads = jsonutils.loads
354+
355+
with mock.patch('oslo_serialization.jsonutils.loads') as mock_loads:
356+
mock_loads.side_effect = origi_loads
357+
request.get_alias_from_config()
358+
request.get_alias_from_config()
359+
360+
mock_loads.assert_called_once()
361+
345362
def _verify_result(self, expected, real):
346363
exp_real = zip(expected, real)
347364
for exp, real in exp_real:

0 commit comments

Comments
 (0)