Skip to content

Commit 0065bb6

Browse files
gibizerkk7ds
authored andcommitted
Cache [pci]alias parsing
For each lifecycle operation nova re-load, parses, and validates the [pci]alias config option. This is wasteful. So this patch adds functools.cache decorator on the function used to do this work. Change-Id: If2ffb25430749a22c923c0938221833e7b883873
1 parent ae064ca commit 0065bb6

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
@@ -62,6 +62,7 @@
6262
from nova import exception
6363
from nova import objects
6464
from nova.objects import base as objects_base
65+
from nova.pci import request
6566
from nova import quota
6667
from nova.scheduler.client import report
6768
from nova.scheduler import utils as scheduler_utils
@@ -190,6 +191,10 @@ def setUp(self):
190191
self.useFixture(
191192
nova_fixtures.PropagateTestCaseIdToChildEventlets(self.id()))
192193

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

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

430439
def reset_flags(self, *k, **kw):
431440
"""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)