Merge "use exc_to_retry from lib"

This commit is contained in:
Zuul 2018-05-21 22:29:18 +00:00 committed by Gerrit Code Review
commit 19b7aa7c40
3 changed files with 3 additions and 50 deletions

View File

@ -175,17 +175,6 @@ def _is_nested_instance(e, etypes):
return False
@contextlib.contextmanager
def exc_to_retry(etypes):
try:
yield
except Exception as e:
with excutils.save_and_reraise_exception() as ctx:
if _is_nested_instance(e, etypes):
ctx.reraise = False
raise db_exc.RetryRequest(e)
def get_reader_session():
"""Helper to get reader session"""
return context_manager.reader.get_sessionmaker()()

View File

@ -26,6 +26,7 @@ from neutron_lib.callbacks import registry
from neutron_lib.callbacks import resources
from neutron_lib import constants
from neutron_lib import context as ctx
from neutron_lib.db import api as lib_db_api
from neutron_lib import exceptions as exc
from neutron_lib.exceptions import l3 as l3_exc
from neutron_lib.plugins import constants as plugin_constants
@ -471,7 +472,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
"The port has already been deleted.", port_id)
# clean up subnets
subnets = self._get_subnets_by_network(context, id)
with db_api.exc_to_retry(os_db_exc.DBReferenceError):
with lib_db_api.exc_to_retry(os_db_exc.DBReferenceError):
# retry reference errors so we can check the port type and
# cleanup if a network-owned port snuck in without failing
for subnet in subnets:
@ -1032,7 +1033,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
self._delete_subnet(context, subnet)
def _delete_subnet(self, context, subnet):
with db_api.exc_to_retry(sql_exc.IntegrityError), \
with lib_db_api.exc_to_retry(sql_exc.IntegrityError), \
db_api.context_manager.writer.using(context):
registry.notify(resources.SUBNET, events.PRECOMMIT_DELETE,
self, context=context, subnet_id=subnet.id)

View File

@ -24,43 +24,6 @@ from neutron.db import api as db_api
from neutron.tests import base
class TestExceptionToRetryContextManager(base.BaseTestCase):
def test_translates_single_exception(self):
with testtools.ExpectedException(db_exc.RetryRequest):
with db_api.exc_to_retry(ValueError):
raise ValueError()
def test_translates_multiple_exception_types(self):
with testtools.ExpectedException(db_exc.RetryRequest):
with db_api.exc_to_retry((ValueError, TypeError)):
raise TypeError()
def test_translates_DBerror_inner_exception(self):
with testtools.ExpectedException(db_exc.RetryRequest):
with db_api.exc_to_retry(ValueError):
raise db_exc.DBError(ValueError())
def test_passes_other_exceptions(self):
with testtools.ExpectedException(ValueError):
with db_api.exc_to_retry(TypeError):
raise ValueError()
def test_inner_exception_preserved_in_retryrequest(self):
try:
exc = ValueError('test')
with db_api.exc_to_retry(ValueError):
raise exc
except db_exc.RetryRequest as e:
self.assertEqual(exc, e.inner_exc)
def test_retries_on_multi_exception_containing_target(self):
with testtools.ExpectedException(db_exc.RetryRequest):
with db_api.exc_to_retry(ValueError):
e = exceptions.MultipleExceptions([ValueError(), TypeError()])
raise e
class TestDeadLockDecorator(base.BaseTestCase):
@db_api.retry_db_errors