Improve port delete performance

Pass existing port dict to IPAM delete and l3 callback handler
to save 2 port DB requests.
This gives ~10-15% improvement.

Change-Id: Iab5ed8a57aaf68f626e5b386f72d941ef7b22227
This commit is contained in:
Oleg Bondarev 2021-04-12 16:11:06 +03:00
parent 51cccd4b1a
commit b221a4af33
5 changed files with 24 additions and 24 deletions

View File

@ -1495,9 +1495,9 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
return self._make_port_dict(db_port)
@db_api.retry_if_session_inactive()
def delete_port(self, context, id):
def delete_port(self, context, id, port=None):
with db_api.CONTEXT_WRITER.using(context):
self.ipam.delete_port(context, id)
self.ipam.delete_port(context, id, port)
def delete_ports_by_device_id(self, context, device_id, network_id=None):
with db_api.CONTEXT_READER.using(context):

View File

@ -483,9 +483,9 @@ class IpamPluggableBackend(ipam_backend_mixin.IpamBackendMixin):
revert_on_fail=False)
return changes
def delete_port(self, context, id):
def delete_port(self, context, id, port=None):
# Get fixed_ips list before port deletion
port = self._get_port(context, id)
port = port or self._get_port(context, id)
ipam_driver = driver.Pool.get_instance(None, context)
super(IpamPluggableBackend, self).delete_port(context, id)

View File

@ -105,7 +105,8 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
l3plugin = directory.get_plugin(plugin_constants.L3)
if l3plugin and payload.metadata['port_check']:
l3plugin.prevent_l3_port_deletion(
payload.context, payload.resource_id)
payload.context, payload.resource_id,
port=payload.metadata.get('port'))
@staticmethod
def _validate_subnet_address_mode(subnet):
@ -1562,7 +1563,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
filters = filters or {}
return l3_obj.FloatingIP.count(context, **filters)
def prevent_l3_port_deletion(self, context, port_id):
def prevent_l3_port_deletion(self, context, port_id, port=None):
"""Checks to make sure a port is allowed to be deleted.
Raises an exception if this is not the case. This should be called by
@ -1572,7 +1573,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
deletion checks.
"""
try:
port = self._core_plugin.get_port(context, port_id)
port = port or self._core_plugin.get_port(context, port_id)
except n_exc.PortNotFound:
# non-existent ports don't need to be protected from deletion
return

View File

@ -1877,7 +1877,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
return
self._bind_port_if_needed(mech_context)
def _pre_delete_port(self, context, port_id, port_check):
def _pre_delete_port(self, context, port_id, port_check, port=None):
"""Do some preliminary operations before deleting the port."""
LOG.debug("Deleting port %s", port_id)
try:
@ -1886,7 +1886,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
registry.publish(
resources.PORT, events.BEFORE_DELETE, self,
payload=events.DBEventPayload(
context, metadata={'port_check': port_check},
context, metadata={'port_check': port_check, 'port': port},
resource_id=port_id))
except exceptions.CallbackFailure as e:
# NOTE(armax): preserve old check's behavior
@ -1897,21 +1897,22 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
@utils.transaction_guard
@db_api.retry_if_session_inactive()
def delete_port(self, context, id, l3_port_check=True):
self._pre_delete_port(context, id, l3_port_check)
try:
port_db = self._get_port(context, id)
port = self._make_port_dict(port_db)
except exc.PortNotFound:
LOG.debug("The port '%s' was deleted", id)
return
self._pre_delete_port(context, id, l3_port_check, port)
# TODO(armax): get rid of the l3 dependency in the with block
router_ids = []
l3plugin = directory.get_plugin(plugin_constants.L3)
with db_api.CONTEXT_WRITER.using(context):
try:
port_db = self._get_port(context, id)
binding = p_utils.get_port_binding_by_status_and_host(
port_db.port_bindings, const.ACTIVE,
raise_if_not_found=True, port_id=id)
except exc.PortNotFound:
LOG.debug("The port '%s' was deleted", id)
return
port = self._make_port_dict(port_db)
binding = p_utils.get_port_binding_by_status_and_host(
port_db.port_bindings, const.ACTIVE,
raise_if_not_found=True, port_id=id)
network = self.get_network(context, port['network_id'])
bound_mech_contexts = []
@ -1953,7 +1954,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
LOG.debug("Calling delete_port for %(port_id)s owned by %(owner)s",
{"port_id": id, "owner": device_owner})
super(Ml2Plugin, self).delete_port(context, id)
super(Ml2Plugin, self).delete_port(context, id, port)
self._post_delete_port(
context, port, router_ids, bound_mech_contexts)

View File

@ -1328,10 +1328,8 @@ class TestMl2PortsV2(test_plugin.TestPortsV2, Ml2PluginV2TestCase):
plugin = directory.get_plugin()
with mock.patch.object(ml2_plugin.LOG, 'debug') as log_debug:
plugin.delete_port(ctx, 'invalid-uuid', l3_port_check=False)
log_debug.assert_has_calls([
mock.call(_("Deleting port %s"), 'invalid-uuid'),
mock.call(_("The port '%s' was deleted"), 'invalid-uuid')
])
log_debug.assert_called_with(
_("The port '%s' was deleted"), 'invalid-uuid')
def test_l3_cleanup_on_net_delete(self):
l3plugin = directory.get_plugin(plugin_constants.L3)