]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Call target plugin out of DB transaction in the Metaplugin
authorItsuro Oda <oda@valinux.co.jp>
Wed, 5 Feb 2014 01:12:26 +0000 (10:12 +0900)
committerItsuro Oda <oda@valinux.co.jp>
Tue, 11 Mar 2014 04:04:15 +0000 (13:04 +0900)
Previously, there are cases that a target plugin is called
within a DB transaction in the metaplugin. This possibly causes
"DB lock timeout" error since a target plugin may take a long
time (ex. communicate to a controller).

Change-Id: Ie2971bfae1fecc97086b58cd943c321feb0aba04
Closes-Bug: #1267291

neutron/plugins/metaplugin/meta_neutron_plugin.py
neutron/tests/unit/metaplugin/test_metaplugin.py

index 8a6307dbf9e8b9abc0eca2ed98f02f79c69df862..c1c5df6beb4125161add9c090d982a306789f32f 100644 (file)
@@ -192,17 +192,16 @@ class MetaPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
         if str(flavor) not in self.plugins:
             flavor = self.default_flavor
         plugin = self._get_plugin(flavor)
-        with context.session.begin(subtransactions=True):
-            net = plugin.create_network(context, network)
-            LOG.debug(_("Created network: %(net_id)s with flavor "
-                        "%(flavor)s"), {'net_id': net['id'], 'flavor': flavor})
-            try:
-                meta_db_v2.add_network_flavor_binding(context.session,
-                                                      flavor, str(net['id']))
-            except Exception:
-                LOG.exception(_('Failed to add flavor bindings'))
-                plugin.delete_network(context, net['id'])
-                raise FaildToAddFlavorBinding()
+        net = plugin.create_network(context, network)
+        LOG.debug(_("Created network: %(net_id)s with flavor "
+                    "%(flavor)s"), {'net_id': net['id'], 'flavor': flavor})
+        try:
+            meta_db_v2.add_network_flavor_binding(context.session,
+                                                  flavor, str(net['id']))
+        except Exception:
+            LOG.exception(_('Failed to add flavor bindings'))
+            plugin.delete_network(context, net['id'])
+            raise FaildToAddFlavorBinding()
 
         LOG.debug(_("Created network: %s"), net['id'])
         self._extend_network_dict(context, net)
@@ -336,13 +335,17 @@ class MetaPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
         if str(flavor) not in self.l3_plugins:
             flavor = self.default_l3_flavor
         plugin = self._get_l3_plugin(flavor)
-        with context.session.begin(subtransactions=True):
-            r_in_db = plugin.create_router(context, router)
-            LOG.debug(_("Created router: %(router_id)s with flavor "
-                        "%(flavor)s"),
-                      {'router_id': r_in_db['id'], 'flavor': flavor})
+        r_in_db = plugin.create_router(context, router)
+        LOG.debug(_("Created router: %(router_id)s with flavor "
+                    "%(flavor)s"),
+                  {'router_id': r_in_db['id'], 'flavor': flavor})
+        try:
             meta_db_v2.add_router_flavor_binding(context.session,
                                                  flavor, str(r_in_db['id']))
+        except Exception:
+            LOG.exception(_('Failed to add flavor bindings'))
+            plugin.delete_router(context, r_in_db['id'])
+            raise FaildToAddFlavorBinding()
 
         LOG.debug(_("Created router: %s"), r_in_db['id'])
         self._extend_router_dict(context, r_in_db)
index 3d76656e92a6982c5fd10ca02632a9b3ef3b2957..7d6f88948388adceafbd521febccee269124f228 100644 (file)
@@ -27,6 +27,8 @@ from neutron.db import db_base_plugin_v2
 from neutron.db import models_v2
 from neutron.extensions.flavor import (FLAVOR_NETWORK, FLAVOR_ROUTER)
 from neutron.openstack.common import uuidutils
+from neutron.plugins.metaplugin.meta_neutron_plugin import (
+    FaildToAddFlavorBinding)
 from neutron.plugins.metaplugin.meta_neutron_plugin import FlavorNotFound
 from neutron.plugins.metaplugin.meta_neutron_plugin import MetaPluginV2
 from neutron.tests import base
@@ -331,6 +333,30 @@ class MetaNeutronPluginV2Test(base.BaseTestCase):
 
         self.fail("No Error is not raised")
 
+    def test_create_network_flavor_fail(self):
+        with mock.patch('neutron.plugins.metaplugin.meta_db_v2.'
+                        'add_network_flavor_binding',
+                        side_effect=Exception):
+            network = self._fake_network('fake1')
+            self.assertRaises(FaildToAddFlavorBinding,
+                              self.plugin.create_network,
+                              self.context,
+                              network)
+            count = self.plugin.get_networks_count(self.context)
+            self.assertEqual(count, 0)
+
+    def test_create_router_flavor_fail(self):
+        with mock.patch('neutron.plugins.metaplugin.meta_db_v2.'
+                        'add_router_flavor_binding',
+                        side_effect=Exception):
+            router = self._fake_router('fake1')
+            self.assertRaises(FaildToAddFlavorBinding,
+                              self.plugin.create_router,
+                              self.context,
+                              router)
+            count = self.plugin.get_routers_count(self.context)
+            self.assertEqual(count, 0)
+
 
 class MetaNeutronPluginV2TestWithoutL3(MetaNeutronPluginV2Test):
     """Tests without l3_plugin_list configration."""
@@ -343,3 +369,6 @@ class MetaNeutronPluginV2TestWithoutL3(MetaNeutronPluginV2Test):
 
     def test_create_delete_router(self):
         self.skipTest("Test case without router")
+
+    def test_create_router_flavor_fail(self):
+        self.skipTest("Test case without router")