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
This commit is contained in:
Dane LeBlanc 2015-04-04 18:50:36 -04:00
parent 21bef562c2
commit aeb5efe3fb
4 changed files with 16 additions and 17 deletions

View 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,

View 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

View 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):

View 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):