Merge "placement: Perform build list of standard classes once"
This commit is contained in:
@ -57,6 +57,11 @@ def _refresh_from_db(ctx, cache):
|
||||
class ResourceClassCache(object):
|
||||
"""A cache of integer and string lookup values for resource classes."""
|
||||
|
||||
# List of dict of all standard resource classes, where every list item
|
||||
# have a form {'id': <ID>, 'name': <NAME>}
|
||||
STANDARDS = [{'id': fields.ResourceClass.STANDARD.index(s), 'name': s}
|
||||
for s in fields.ResourceClass.STANDARD]
|
||||
|
||||
def __init__(self, ctx):
|
||||
"""Initialize the cache of resource class identifiers.
|
||||
|
||||
@ -72,13 +77,6 @@ class ResourceClassCache(object):
|
||||
self.id_cache = {}
|
||||
self.str_cache = {}
|
||||
|
||||
def get_standards(self):
|
||||
"""Return a list of {'id': <ID>, 'name': <NAME> for all standard
|
||||
resource classes.
|
||||
"""
|
||||
return [{'id': fields.ResourceClass.STANDARD.index(s), 'name': s}
|
||||
for s in fields.ResourceClass.STANDARD]
|
||||
|
||||
def id_from_string(self, rc_str):
|
||||
"""Given a string representation of a resource class -- e.g. "DISK_GB"
|
||||
or "IRON_SILVER" -- return the integer code for the resource class. For
|
||||
|
@ -1222,8 +1222,7 @@ class ResourceClass(base.NovaObject):
|
||||
# Never delete any standard resource class, since the standard resource
|
||||
# classes don't even exist in the database table anyway.
|
||||
_ensure_rc_cache(self._context)
|
||||
standards = _RC_CACHE.get_standards()
|
||||
if self.id in (rc['id'] for rc in standards):
|
||||
if self.id in (rc['id'] for rc in _RC_CACHE.STANDARDS):
|
||||
raise exception.ResourceClassCannotDeleteStandard(
|
||||
resource_class=self.name)
|
||||
|
||||
@ -1253,8 +1252,7 @@ class ResourceClass(base.NovaObject):
|
||||
# Never update any standard resource class, since the standard resource
|
||||
# classes don't even exist in the database table anyway.
|
||||
_ensure_rc_cache(self._context)
|
||||
standards = _RC_CACHE.get_standards()
|
||||
if self.id in (rc['id'] for rc in standards):
|
||||
if self.id in (rc['id'] for rc in _RC_CACHE.STANDARDS):
|
||||
raise exception.ResourceClassCannotUpdateStandard(
|
||||
resource_class=self.name)
|
||||
self._save(self._context, self.id, self.name, updates)
|
||||
@ -1285,9 +1283,8 @@ class ResourceClassList(base.ObjectListBase, base.NovaObject):
|
||||
@db_api.api_context_manager.reader
|
||||
def _get_all(context):
|
||||
_ensure_rc_cache(context)
|
||||
standards = _RC_CACHE.get_standards()
|
||||
customs = list(context.session.query(models.ResourceClass).all())
|
||||
return standards + customs
|
||||
return _RC_CACHE.STANDARDS + customs
|
||||
|
||||
@base.remotable_classmethod
|
||||
def get_all(cls, context):
|
||||
|
@ -44,14 +44,19 @@ class TestResourceClassCache(test.TestCase):
|
||||
|
||||
self.assertFalse(sel_mock.called)
|
||||
|
||||
def test_get_standards(self):
|
||||
def test_standards(self):
|
||||
cache = rc_cache.ResourceClassCache(self.context)
|
||||
standards = cache.get_standards()
|
||||
standards = cache.STANDARDS
|
||||
|
||||
self.assertEqual(len(standards), len(fields.ResourceClass.STANDARD))
|
||||
names = (rc['name'] for rc in standards)
|
||||
for name in fields.ResourceClass.STANDARD:
|
||||
self.assertIn(name, names)
|
||||
|
||||
cache = rc_cache.ResourceClassCache(self.context)
|
||||
standards2 = cache.STANDARDS
|
||||
self.assertEqual(id(standards), id(standards2))
|
||||
|
||||
def test_rc_cache_custom(self):
|
||||
"""Test that non-standard, custom resource classes hit the database and
|
||||
return appropriate results, caching the results after a single
|
||||
|
Reference in New Issue
Block a user