]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Use a joined relationship for AZ info on networks
authorKevin Benton <blak111@gmail.com>
Mon, 14 Dec 2015 01:56:57 +0000 (17:56 -0800)
committerKevin Benton <blak111@gmail.com>
Mon, 14 Dec 2015 22:45:30 +0000 (14:45 -0800)
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
neutron/db/availability_zone/network.py
neutron/db/models_v2.py
neutron/extensions/network_availability_zone.py

index 478159c423f1d9fab9cc5d82cd45dee3aea6f3e2..a1b6796d27e94311ed29bce9f58ce147dc58e0a9 100644 (file)
@@ -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.
index c08d0da6d2bdadc72f4842c461ed73f26ee43859..9888b1cae4c9677c813d646606639fac26fd3b07 100644 (file)
@@ -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'])
index 5b78341b37d777b114840f3fc391c4e14cf2ff2e..f4e75a5d2cd77e66b273dd44596df9a1f7b3382f 100644 (file)
@@ -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')
index 192a6c299a39fa891bf5ff12aad7c209dff3ed93..2e62efb716d36f8288a1342be8ab25d5945e5d65 100644 (file)
@@ -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"""