Merge "ml2: allow retry on retriabable db error by precommit" into stable/mitaka

This commit is contained in:
Jenkins 2016-09-23 14:46:27 +00:00 committed by Gerrit Code Review
commit 664a725142
2 changed files with 121 additions and 21 deletions

View File

@ -22,6 +22,7 @@ import stevedore
from neutron._i18n import _, _LE, _LI, _LW
from neutron.api.v2 import attributes
from neutron.common import exceptions as exc
from neutron.db import api as db_api
from neutron.extensions import external_net
from neutron.extensions import multiprovidernet as mpnet
from neutron.extensions import portbindings
@ -385,21 +386,32 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
raise vlantransparent.VlanTransparencyDriverError()
def _call_on_drivers(self, method_name, context,
continue_on_failure=False):
continue_on_failure=False, raise_db_retriable=False):
"""Helper method for calling a method across all mechanism drivers.
:param method_name: name of the method to call
:param context: context parameter to pass to each method call
:param continue_on_failure: whether or not to continue to call
all mechanism drivers once one has raised an exception
:param raise_db_retriable: whether or not to treat retriable db
exception by mechanism drivers to propagate up to upper layer so
that upper layer can handle it or error in ML2 player
:raises: neutron.plugins.ml2.common.MechanismDriverError
if any mechanism driver call fails.
if any mechanism driver call fails. or DB retriable error when
raise_db_retriable=False. See neutron.db.api.is_retriable for
what db exception is retriable
"""
error = False
for driver in self.ordered_mech_drivers:
try:
getattr(driver.obj, method_name)(context)
except Exception:
except Exception as e:
if raise_db_retriable and db_api.is_retriable(e):
with excutils.save_and_reraise_exception():
LOG.debug("DB exception raised by Mechanism driver "
"'%(name)s' in %(method)s",
{'name': driver.name, 'method': method_name},
exc_info=e)
LOG.exception(
_LE("Mechanism driver '%(name)s' failed in %(method)s"),
{'name': driver.name, 'method': method_name}
@ -415,7 +427,9 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
def create_network_precommit(self, context):
"""Notify all mechanism drivers during network creation.
:raises: neutron.plugins.ml2.common.MechanismDriverError
:raises: DB retriable error if create_network_precommit raises them
See neutron.db.api.is_retriable for what db exception is retriable
or neutron.plugins.ml2.common.MechanismDriverError
if any mechanism driver create_network_precommit call fails.
Called within the database transaction. If a mechanism driver
@ -424,7 +438,8 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
that all mechanism drivers are called in this case.
"""
self._check_vlan_transparency(context)
self._call_on_drivers("create_network_precommit", context)
self._call_on_drivers("create_network_precommit", context,
raise_db_retriable=True)
def create_network_postcommit(self, context):
"""Notify all mechanism drivers after network creation.
@ -443,7 +458,9 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
def update_network_precommit(self, context):
"""Notify all mechanism drivers during network update.
:raises: neutron.plugins.ml2.common.MechanismDriverError
:raises: DB retriable error if create_network_precommit raises them
See neutron.db.api.is_retriable for what db exception is retriable
or neutron.plugins.ml2.common.MechanismDriverError
if any mechanism driver update_network_precommit call fails.
Called within the database transaction. If a mechanism driver
@ -451,7 +468,8 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
to the caller, triggering a rollback. There is no guarantee
that all mechanism drivers are called in this case.
"""
self._call_on_drivers("update_network_precommit", context)
self._call_on_drivers("update_network_precommit", context,
raise_db_retriable=True)
def update_network_postcommit(self, context):
"""Notify all mechanism drivers after network update.
@ -470,7 +488,9 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
def delete_network_precommit(self, context):
"""Notify all mechanism drivers during network deletion.
:raises: neutron.plugins.ml2.common.MechanismDriverError
:raises: DB retriable error if create_network_precommit raises them
See neutron.db.api.is_retriable for what db exception is retriable
or neutron.plugins.ml2.common.MechanismDriverError
if any mechanism driver delete_network_precommit call fails.
Called within the database transaction. If a mechanism driver
@ -478,7 +498,8 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
to the caller, triggering a rollback. There is no guarantee
that all mechanism drivers are called in this case.
"""
self._call_on_drivers("delete_network_precommit", context)
self._call_on_drivers("delete_network_precommit", context,
raise_db_retriable=True)
def delete_network_postcommit(self, context):
"""Notify all mechanism drivers after network deletion.
@ -501,7 +522,9 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
def create_subnet_precommit(self, context):
"""Notify all mechanism drivers during subnet creation.
:raises: neutron.plugins.ml2.common.MechanismDriverError
:raises: DB retriable error if create_network_precommit raises them
See neutron.db.api.is_retriable for what db exception is retriable
or neutron.plugins.ml2.common.MechanismDriverError
if any mechanism driver create_subnet_precommit call fails.
Called within the database transaction. If a mechanism driver
@ -509,7 +532,8 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
to the caller, triggering a rollback. There is no guarantee
that all mechanism drivers are called in this case.
"""
self._call_on_drivers("create_subnet_precommit", context)
self._call_on_drivers("create_subnet_precommit", context,
raise_db_retriable=True)
def create_subnet_postcommit(self, context):
"""Notify all mechanism drivers after subnet creation.
@ -528,7 +552,9 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
def update_subnet_precommit(self, context):
"""Notify all mechanism drivers during subnet update.
:raises: neutron.plugins.ml2.common.MechanismDriverError
:raises: DB retriable error if create_network_precommit raises them
See neutron.db.api.is_retriable for what db exception is retriable
or neutron.plugins.ml2.common.MechanismDriverError
if any mechanism driver update_subnet_precommit call fails.
Called within the database transaction. If a mechanism driver
@ -536,7 +562,8 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
to the caller, triggering a rollback. There is no guarantee
that all mechanism drivers are called in this case.
"""
self._call_on_drivers("update_subnet_precommit", context)
self._call_on_drivers("update_subnet_precommit", context,
raise_db_retriable=True)
def update_subnet_postcommit(self, context):
"""Notify all mechanism drivers after subnet update.
@ -555,7 +582,9 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
def delete_subnet_precommit(self, context):
"""Notify all mechanism drivers during subnet deletion.
:raises: neutron.plugins.ml2.common.MechanismDriverError
:raises: DB retriable error if create_network_precommit raises them
See neutron.db.api.is_retriable for what db exception is retriable
or neutron.plugins.ml2.common.MechanismDriverError
if any mechanism driver delete_subnet_precommit call fails.
Called within the database transaction. If a mechanism driver
@ -563,7 +592,8 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
to the caller, triggering a rollback. There is no guarantee
that all mechanism drivers are called in this case.
"""
self._call_on_drivers("delete_subnet_precommit", context)
self._call_on_drivers("delete_subnet_precommit", context,
raise_db_retriable=True)
def delete_subnet_postcommit(self, context):
"""Notify all mechanism drivers after subnet deletion.
@ -586,7 +616,9 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
def create_port_precommit(self, context):
"""Notify all mechanism drivers during port creation.
:raises: neutron.plugins.ml2.common.MechanismDriverError
:raises: DB retriable error if create_network_precommit raises them
See neutron.db.api.is_retriable for what db exception is retriable
or neutron.plugins.ml2.common.MechanismDriverError
if any mechanism driver create_port_precommit call fails.
Called within the database transaction. If a mechanism driver
@ -594,7 +626,8 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
to the caller, triggering a rollback. There is no guarantee
that all mechanism drivers are called in this case.
"""
self._call_on_drivers("create_port_precommit", context)
self._call_on_drivers("create_port_precommit", context,
raise_db_retriable=True)
def create_port_postcommit(self, context):
"""Notify all mechanism drivers of port creation.
@ -613,7 +646,9 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
def update_port_precommit(self, context):
"""Notify all mechanism drivers during port update.
:raises: neutron.plugins.ml2.common.MechanismDriverError
:raises: DB retriable error if create_network_precommit raises them
See neutron.db.api.is_retriable for what db exception is retriable
or neutron.plugins.ml2.common.MechanismDriverError
if any mechanism driver update_port_precommit call fails.
Called within the database transaction. If a mechanism driver
@ -621,7 +656,8 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
to the caller, triggering a rollback. There is no guarantee
that all mechanism drivers are called in this case.
"""
self._call_on_drivers("update_port_precommit", context)
self._call_on_drivers("update_port_precommit", context,
raise_db_retriable=True)
def update_port_postcommit(self, context):
"""Notify all mechanism drivers after port update.
@ -640,7 +676,9 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
def delete_port_precommit(self, context):
"""Notify all mechanism drivers during port deletion.
:raises: neutron.plugins.ml2.common.MechanismDriverError
:raises:DB retriable error if create_network_precommit raises them
See neutron.db.api.is_retriable for what db exception is retriable
or neutron.plugins.ml2.common.MechanismDriverError
if any mechanism driver delete_port_precommit call fails.
Called within the database transaction. If a mechanism driver
@ -648,7 +686,8 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
to the caller, triggering a rollback. There is no guarantee
that all mechanism drivers are called in this case.
"""
self._call_on_drivers("delete_port_precommit", context)
self._call_on_drivers("delete_port_precommit", context,
raise_db_retriable=True)
def delete_port_postcommit(self, context):
"""Notify all mechanism drivers after port deletion.

View File

@ -0,0 +1,61 @@
# Copyright (c) 2016 IBM Corp.
#
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from oslo_db import exception as db_exc
from neutron.plugins.ml2.common import exceptions as ml2_exc
from neutron.plugins.ml2 import config as config
from neutron.plugins.ml2 import managers
from neutron.tests import base
from neutron.tests.unit.plugins.ml2.drivers import mechanism_test
class TestMechManager(base.BaseTestCase):
def setUp(self):
config.cfg.CONF.set_override('mechanism_drivers', ['test'],
group='ml2')
super(TestMechManager, self).setUp()
self._manager = managers.MechanismManager()
def _check_precommit(self, resource, operation):
meth_name = "%s_%s_precommit" % (operation, resource)
method = getattr(self._manager, meth_name)
fake_ctxt = mock.Mock()
fake_ctxt.current = {}
with mock.patch.object(mechanism_test.TestMechanismDriver, meth_name,
side_effect=db_exc.DBDeadlock()):
self.assertRaises(db_exc.DBDeadlock, method, fake_ctxt)
with mock.patch.object(mechanism_test.TestMechanismDriver, meth_name,
side_effect=RuntimeError()):
self.assertRaises(ml2_exc.MechanismDriverError, method, fake_ctxt)
def _check_resource(self, resource):
self._check_precommit(resource, 'create')
self._check_precommit(resource, 'update')
self._check_precommit(resource, 'delete')
def test_network_precommit(self):
self._check_resource('network')
def test_subnet_precommit(self):
self._check_resource('subnet')
def test_port_precommit(self):
self._check_resource('port')