From 4bb3c92917cdf19b18c7481786050241218e8df7 Mon Sep 17 00:00:00 2001 From: Oleg Bondarev Date: Fri, 5 Mar 2021 15:02:23 +0400 Subject: [PATCH] Check for existence instead of fetching the whole net object There is no need to fetch the whole net object (which leads to several heavy DB requests according to OSProfiler stats) when only need to check net existence This speeds up port creation up to 15% Closes-Bug: #1917866 Change-Id: I75db7c713a17a0f1b1b5aecc6eec93e636a71827 --- neutron/db/db_base_plugin_common.py | 5 +++++ neutron/db/db_base_plugin_v2.py | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/neutron/db/db_base_plugin_common.py b/neutron/db/db_base_plugin_common.py index 655972f46f2..35c509bc2a4 100644 --- a/neutron/db/db_base_plugin_common.py +++ b/neutron/db/db_base_plugin_common.py @@ -266,6 +266,11 @@ class DbBasePluginCommon(object): raise exceptions.NetworkNotFound(net_id=id) return network + def _network_exists(self, context, network_id): + query = model_query.query_with_hooks( + context, models_v2.Network, field='id') + return query.filter(models_v2.Network.id == network_id).first() + def _get_subnet_object(self, context, id): subnet = subnet_obj.Subnet.get_object(context, id=id) if not subnet: diff --git a/neutron/db/db_base_plugin_v2.py b/neutron/db/db_base_plugin_v2.py index e0902ac1241..fb4cd519215 100644 --- a/neutron/db/db_base_plugin_v2.py +++ b/neutron/db/db_base_plugin_v2.py @@ -1309,7 +1309,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon, if not validators.is_attr_set(network_id): msg = _("network_id must be specified.") raise exc.InvalidInput(error_message=msg) - if not network_obj.Network.objects_exist(context, id=network_id): + if not self._network_exists(context, network_id): raise exc.NetworkNotFound(net_id=network_id) subnetpool = subnetpool_obj.SubnetPool.get_object(context, @@ -1419,7 +1419,8 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon, port_data['mac_address'] = p.get('mac_address') with db_api.CONTEXT_WRITER.using(context): # Ensure that the network exists. - self._get_network(context, network_id) + if not self._network_exists(context, network_id): + raise exc.NetworkNotFound(net_id=network_id) # Create the port db_port = self._create_db_port_obj(context, port_data)