From aeb5efe3fbeae82a2d65f6bb68710d14156c58bf Mon Sep 17 00:00:00 2001 From: Dane LeBlanc Date: Sat, 4 Apr 2015 18:50:36 -0400 Subject: [PATCH] Re-use context session in ML2 DB get_port_binding_host 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 | 2 +- neutron/plugins/ml2/db.py | 23 +++++++++++------------ neutron/plugins/ml2/plugin.py | 2 +- neutron/tests/unit/plugins/ml2/test_db.py | 6 +++--- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/neutron/db/l3_dvrscheduler_db.py b/neutron/db/l3_dvrscheduler_db.py index daf58a11a..887abdea4 100644 --- a/neutron/db/l3_dvrscheduler_db.py +++ b/neutron/db/l3_dvrscheduler_db.py @@ -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, diff --git a/neutron/plugins/ml2/db.py b/neutron/plugins/ml2/db.py index 73523ac74..9e4f8ca14 100644 --- a/neutron/plugins/ml2/db.py +++ b/neutron/plugins/ml2/db.py @@ -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 diff --git a/neutron/plugins/ml2/plugin.py b/neutron/plugins/ml2/plugin.py index 7866103ef..cfc50d2ad 100644 --- a/neutron/plugins/ml2/plugin.py +++ b/neutron/plugins/ml2/plugin.py @@ -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): diff --git a/neutron/tests/unit/plugins/ml2/test_db.py b/neutron/tests/unit/plugins/ml2/test_db.py index 10042e8b9..c34b82abf 100644 --- a/neutron/tests/unit/plugins/ml2/test_db.py +++ b/neutron/tests/unit/plugins/ml2/test_db.py @@ -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): -- 2.45.2