Browse Source

Optimize get_ports with trunk extension

Use case: many trunk ports (300+) each with many subports.
Issue: get_ports takes much time.
Solution: apply trunk resource extend func for a full list of ports,
not for each port individually, thus avoiding a DB query for each
trunk port.

The approach will be extended for other resource extenders, like QoS,
and probably for other resources as well.
QoS: https://review.opendev.org/c/openstack/neutron/+/764454

Change-Id: Ic08e4049f6156c0700ca3c7aee251b6eb0eb97da
Closes-Bug: #1905268
changes/77/763777/3
Oleg Bondarev 9 months ago
committed by Oleg Bondarev
parent
commit
e007349d47
  1. 5
      neutron/db/db_base_plugin_common.py
  2. 4
      neutron/db/db_base_plugin_v2.py
  3. 37
      neutron/services/trunk/plugin.py

5
neutron/db/db_base_plugin_common.py

@ -223,7 +223,8 @@ class DbBasePluginCommon(object):
def _make_port_dict(self, port, fields=None,
process_extensions=True,
with_fixed_ips=True):
with_fixed_ips=True,
bulk=False):
if isinstance(port, port_obj.Port):
port_data = port.db_obj
standard_attr_id = port.db_obj.standard_attr.id
@ -252,8 +253,10 @@ class DbBasePluginCommon(object):
ip["ip_address"])} for ip in port["fixed_ips"]]
# Call auxiliary extend functions, if any
if process_extensions:
res['bulk'] = bulk
resource_extend.apply_funcs(
port_def.COLLECTION_NAME, res, port_data)
res.pop('bulk')
return db_utils.resource_fields(res, fields)
def _get_network(self, context, id):

4
neutron/db/db_base_plugin_v2.py

@ -1563,7 +1563,9 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
sorts=sorts, limit=limit,
marker_obj=marker_obj,
page_reverse=page_reverse)
items = [self._make_port_dict(c, fields) for c in query]
items = [self._make_port_dict(c, fields, bulk=True) for c in query]
# TODO(obondarev): use neutron_lib constant
resource_extend.apply_funcs('ports_bulk', items, None)
if limit and page_reverse:
items.reverse()
return items

37
neutron/services/trunk/plugin.py

@ -79,17 +79,44 @@ class TrunkPlugin(service_base.ServicePluginBase):
'port_id': x.port_id}
for x in port_db.trunk_port.sub_ports
}
core_plugin = directory.get_plugin()
ports = core_plugin.get_ports(
context.get_admin_context(), filters={'id': subports})
for port in ports:
subports[port['id']]['mac_address'] = port['mac_address']
if not port_res.get('bulk'):
core_plugin = directory.get_plugin()
ports = core_plugin.get_ports(
context.get_admin_context(), filters={'id': subports})
for port in ports:
subports[port['id']]['mac_address'] = port['mac_address']
trunk_details = {'trunk_id': port_db.trunk_port.id,
'sub_ports': list(subports.values())}
port_res['trunk_details'] = trunk_details
return port_res
@staticmethod
# TODO(obondarev): use neutron_lib constant
@resource_extend.extends(['ports_bulk'])
def _extend_port_trunk_details_bulk(ports_res, noop):
"""Add trunk subport details to a list of ports."""
subport_ids = []
trunk_ports = []
for p in ports_res:
if 'trunk_details' in p and 'subports' in p['trunk_details']:
trunk_ports.append(p)
for subp in p['trunk_details']['subports']:
subport_ids.append(subp['port_id'])
if not subport_ids:
return ports_res
core_plugin = directory.get_plugin()
subports = core_plugin.get_ports(
context.get_admin_context(), filters={'id': subport_ids})
subport_macs = {p['id']: p['mac_address'] for p in subports}
for tp in trunk_ports:
for subp in tp['trunk_details']['subports']:
subp['mac_address'] = subport_macs[subp['port_id']]
return ports_res
def check_compatibility(self):
"""Verify the plugin can load correctly and fail otherwise."""
self.check_driver_compatibility()

Loading…
Cancel
Save