Raise correct error instead of class exist in Placement API

When create resource class using placement API, and at
the DB layer, we have a retry logic for 100 loop, and
error will raise when the retry limit exceed, currently
a log about limit exceed is generated but a
ResourceClassExists Error is raised, which is a little
bit weird and might confuse the users.

We'd better add a particular Exception for this kind of
scenario and it could be used in other places also.

Co-Authored-By: huangtianhua@huawei.com
Closes-Bug: #1658868

Change-Id: I89156be82ad520bdeacb12b31b44e7170b77ce3f
This commit is contained in:
Kevin_Zheng 2017-01-24 15:12:51 +08:00
parent 9a44d56f65
commit f2bb08ed6a
4 changed files with 16 additions and 3 deletions

View File

@ -83,6 +83,13 @@ def create_resource_class(req):
_('Conflicting resource class already exists: %(name)s') %
{'name': data['name']},
json_formatter=util.json_error_formatter)
except exception.MaxDBRetriesExceeded:
raise webob.exc.HTTPConflict(
_('Max retries of DB transaction exceeded attempting '
'to create resource class: %(name)s, please'
'try again.') %
{'name': data['name']},
json_formatter=util.json_error_formatter)
req.response.location = util.resource_class_url(req.environ, rc)
req.response.status = 201

View File

@ -2067,6 +2067,11 @@ class LibguestfsCannotReadKernel(Invalid):
msg_fmt = _("Libguestfs does not have permission to read host kernel.")
class MaxDBRetriesExceeded(NovaException):
msg_fmt = _("Max retries of DB transaction exceeded attempting to "
"perform %(action)s.")
class RealtimePolicyNotSupported(Invalid):
msg_fmt = _("Realtime policy not supported by hypervisor")

View File

@ -28,7 +28,7 @@ from nova.db.sqlalchemy import api as db_api
from nova.db.sqlalchemy import api_models as models
from nova.db.sqlalchemy import resource_class_cache as rc_cache
from nova import exception
from nova.i18n import _LW
from nova.i18n import _, _LW
from nova import objects
from nova.objects import base
from nova.objects import fields
@ -1366,7 +1366,8 @@ class ResourceClass(base.NovaObject):
LOG.warning(_LW("Exceeded retry limit on ID generation while "
"creating ResourceClass %(name)s"),
{'name': self.name})
raise exception.ResourceClassExists(resource_class=self.name)
msg = _("creating resource class %s") % self.name
raise exception.MaxDBRetriesExceeded(action=msg)
@staticmethod
@db_api.api_context_manager.writer

View File

@ -1402,7 +1402,7 @@ class ResourceClassTestCase(ResourceProviderBaseCase):
name='CUSTOM_TWO',
)
rc2.RESOURCE_CREATE_RETRY_COUNT = 3
self.assertRaises(exception.ResourceClassExists, rc2.create)
self.assertRaises(exception.MaxDBRetriesExceeded, rc2.create)
def test_create_duplicate_custom(self):
rc = objects.ResourceClass(