]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Re-use context session in ML2 DB get_port_binding_host
authorDane LeBlanc <leblancd@cisco.com>
Sat, 4 Apr 2015 22:50:36 +0000 (18:50 -0400)
committerDane LeBlanc <leblancd@cisco.com>
Wed, 8 Apr 2015 15:09:56 +0000 (11:09 -0400)
This patch modifies ML2 DB get_port_binding_host method so that it
reuses the existing context session to do the database query
rather than creating a new database session.

Note that there are other methods in ML2 DB that do not re-use
the caller's session (get_port_from_device_mac() and
get_sg_ids_grouped_by_port()). These will be modified using
a separate bug (https://bugs.launchpad.net/neutron/+bug/1441205).
Change-Id: I8aafb0a70f40f9306ccc366e5db6860c92c48cce
Closes-Bug: #1440183

neutron/db/l3_dvrscheduler_db.py
neutron/plugins/ml2/db.py
neutron/plugins/ml2/plugin.py
neutron/tests/unit/plugins/ml2/test_db.py

index daf58a11a15a4f251a5ba09593dedd48e2063bce..887abdea443fc5fdc0c3b05b001ca62188213807 100644 (file)
@@ -163,7 +163,7 @@ class L3_DVRsch_db_mixin(l3agent_sch_db.L3AgentSchedulerDbMixin):
     def dvr_deletens_if_no_port(self, context, port_id):
         """Delete the DVR namespace if no dvr serviced port exists."""
         router_ids = self.get_dvr_routers_by_portid(context, port_id)
-        port_host = ml2_db.get_port_binding_host(port_id)
+        port_host = ml2_db.get_port_binding_host(context.session, port_id)
         if not router_ids:
             LOG.debug('No namespaces available for this DVR port %(port)s '
                       'on host %(host)s', {'port': port_id,
index 73523ac74daff2de94f9b1eb6b3779417b42be8e..9e4f8ca14d4bda2a77f21d4a93b82451297bb624 100644 (file)
@@ -313,21 +313,20 @@ def make_port_dict_with_security_groups(port, sec_groups):
     return port_dict
 
 
-def get_port_binding_host(port_id):
-    session = db_api.get_session()
-    with session.begin(subtransactions=True):
-        try:
+def get_port_binding_host(session, port_id):
+    try:
+        with session.begin(subtransactions=True):
             query = (session.query(models.PortBinding).
                      filter(models.PortBinding.port_id.startswith(port_id)).
                      one())
-        except exc.NoResultFound:
-            LOG.debug("No binding found for port %(port_id)s",
-                      {'port_id': port_id})
-            return
-        except exc.MultipleResultsFound:
-            LOG.error(_LE("Multiple ports have port_id starting with %s"),
-                      port_id)
-            return
+    except exc.NoResultFound:
+        LOG.debug("No binding found for port %(port_id)s",
+                  {'port_id': port_id})
+        return
+    except exc.MultipleResultsFound:
+        LOG.error(_LE("Multiple ports have port_id starting with %s"),
+                  port_id)
+        return
     return query.host
 
 
index 7866103ef58e9eb7c8b69602cd7dc3899616802f..cfc50d2ad32f27b03690bae0b73d8f6336bfeab6 100644 (file)
@@ -1422,7 +1422,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
             LOG.debug("No binding found for DVR port %s", port['id'])
             return False
         else:
-            port_host = db.get_port_binding_host(port_id)
+            port_host = db.get_port_binding_host(context.session, port_id)
             return (port_host == host)
 
     def get_ports_from_devices(self, devices):
index 10042e8b9b048d23d60dc5404b43fac41382f294..c34b82abfdcbdbc1474e7dc8b447d392b863025a 100644 (file)
@@ -144,7 +144,7 @@ class Ml2DBTestCase(testlib_api.SqlTestCase):
         self._setup_neutron_port(network_id, port_id)
         self._setup_neutron_portbinding(port_id, vif_type, host)
 
-        port_host = ml2_db.get_port_binding_host(port_id)
+        port_host = ml2_db.get_port_binding_host(self.ctx.session, port_id)
         self.assertEqual(host, port_host)
 
     def test_get_port_binding_host_multiple_results_found(self):
@@ -160,13 +160,13 @@ class Ml2DBTestCase(testlib_api.SqlTestCase):
         self._setup_neutron_port(network_id, port_id_two)
         self._setup_neutron_portbinding(port_id_two, vif_type, host)
 
-        port_host = ml2_db.get_port_binding_host(port_id)
+        port_host = ml2_db.get_port_binding_host(self.ctx.session, port_id)
         self.assertIsNone(port_host)
 
     def test_get_port_binding_host_result_not_found(self):
         port_id = uuidutils.generate_uuid()
 
-        port_host = ml2_db.get_port_binding_host(port_id)
+        port_host = ml2_db.get_port_binding_host(self.ctx.session, port_id)
         self.assertIsNone(port_host)
 
     def test_get_port(self):