From 57964df5c6e4d590516ae7eb1783f694fa24f501 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Sun, 13 Dec 2015 17:56:57 -0800 Subject: [PATCH] Use a joined relationship for AZ info on networks The previous code was doing a DB lookup for each network's availability zone which was significantly impacting the performance of network listings. This patch adjusts the network model to be automatically joined to the DHCP agents table that the AZ code uses to populate the AZs for the network. Change-Id: I908ceb1a68e0eed7c304e3ff82279ad6fa406167 Closes-Bug: #1525740 --- neutron/db/agentschedulers_db.py | 12 +++--------- neutron/db/availability_zone/network.py | 2 +- neutron/db/models_v2.py | 2 ++ neutron/extensions/network_availability_zone.py | 2 +- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/neutron/db/agentschedulers_db.py b/neutron/db/agentschedulers_db.py index 478159c42..a1b6796d2 100644 --- a/neutron/db/agentschedulers_db.py +++ b/neutron/db/agentschedulers_db.py @@ -464,15 +464,9 @@ class AZDhcpAgentSchedulerDbMixin(DhcpAgentSchedulerDbMixin, network_az.NetworkAvailabilityZoneMixin): """Mixin class to add availability_zone supported DHCP agent scheduler.""" - def get_network_availability_zones(self, network_id): - context = ncontext.get_admin_context() - with context.session.begin(): - query = context.session.query(agents_db.Agent.availability_zone) - query = query.join(NetworkDhcpAgentBinding) - query = query.filter( - NetworkDhcpAgentBinding.network_id == network_id) - query = query.group_by(agents_db.Agent.availability_zone) - return [item[0] for item in query] + def get_network_availability_zones(self, network): + zones = {agent.availability_zone for agent in network.dhcp_agents} + return list(zones) # helper functions for readability. diff --git a/neutron/db/availability_zone/network.py b/neutron/db/availability_zone/network.py index c08d0da6d..9888b1cae 100644 --- a/neutron/db/availability_zone/network.py +++ b/neutron/db/availability_zone/network.py @@ -29,7 +29,7 @@ class NetworkAvailabilityZoneMixin(net_az.NetworkAvailabilityZonePluginBase): net_res[az_ext.AZ_HINTS] = az_ext.convert_az_string_to_list( net_db[az_ext.AZ_HINTS]) net_res[az_ext.AVAILABILITY_ZONES] = ( - self.get_network_availability_zones(net_db['id'])) + self.get_network_availability_zones(net_db)) common_db_mixin.CommonDbMixin.register_dict_extend_funcs( attributes.NETWORKS, ['_extend_availability_zone']) diff --git a/neutron/db/models_v2.py b/neutron/db/models_v2.py index 5b78341b3..f4e75a5d2 100644 --- a/neutron/db/models_v2.py +++ b/neutron/db/models_v2.py @@ -267,3 +267,5 @@ class Network(model_base.HasStandardAttributes, model_base.BASEV2, backref='network', lazy='joined', cascade='all, delete, delete-orphan') availability_zone_hints = sa.Column(sa.String(255)) + dhcp_agents = orm.relationship('Agent', lazy='joined', viewonly=True, + secondary='networkdhcpagentbindings') diff --git a/neutron/extensions/network_availability_zone.py b/neutron/extensions/network_availability_zone.py index 192a6c299..2e62efb71 100644 --- a/neutron/extensions/network_availability_zone.py +++ b/neutron/extensions/network_availability_zone.py @@ -64,5 +64,5 @@ class Network_availability_zone(extensions.ExtensionDescriptor): class NetworkAvailabilityZonePluginBase(object): @abc.abstractmethod - def get_network_availability_zones(self, network_id): + def get_network_availability_zones(self, network): """Return availability zones which a network belongs to""" -- 2.45.2