From 0b2fa9540655b27e5dc079dade29f232cadc0531 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Tue, 15 Dec 2015 22:49:50 -0800 Subject: [PATCH] Use a joined relationship for AZ info on routers This patch adds a joined relationship from the routers to the agent table to prevent excessive DB lookups of the agent availability zones during router listings. This fix is the same as the one for networks here: I908ceb1a68e0eed7c304e3ff82279ad6fa406167 A follow-up patch will add an AZ table so this doesn't have to join directly to the agents table. Change-Id: I808f8ff0c581478b2ea1144e297d83af599a32f2 Closes-Bug: #1526644 --- neutron/db/availability_zone/router.py | 2 +- neutron/db/l3_agentschedulers_db.py | 12 ++---------- neutron/db/l3_db.py | 2 ++ neutron/extensions/router_availability_zone.py | 2 +- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/neutron/db/availability_zone/router.py b/neutron/db/availability_zone/router.py index ccb7244f4..1f3aad493 100644 --- a/neutron/db/availability_zone/router.py +++ b/neutron/db/availability_zone/router.py @@ -29,7 +29,7 @@ class RouterAvailabilityZoneMixin(l3_attrs_db.ExtraAttributesMixin): router_res[az_ext.AZ_HINTS] = az_ext.convert_az_string_to_list( router_res[az_ext.AZ_HINTS]) router_res['availability_zones'] = ( - self.get_router_availability_zones(router_db['id'])) + self.get_router_availability_zones(router_db)) def _process_extra_attr_router_create( self, context, router_db, router_req): diff --git a/neutron/db/l3_agentschedulers_db.py b/neutron/db/l3_agentschedulers_db.py index b61de7b75..ff0010c8b 100644 --- a/neutron/db/l3_agentschedulers_db.py +++ b/neutron/db/l3_agentschedulers_db.py @@ -31,7 +31,6 @@ from neutron.common import utils as n_utils from neutron import context as n_ctx from neutron.db import agents_db from neutron.db import agentschedulers_db -from neutron.db import api as db_api from neutron.db import l3_attrs_db from neutron.db import model_base from neutron.extensions import l3agentscheduler @@ -563,12 +562,5 @@ class AZL3AgentSchedulerDbMixin(L3AgentSchedulerDbMixin, router_az.RouterAvailabilityZonePluginBase): """Mixin class to add availability_zone supported l3 agent scheduler.""" - def get_router_availability_zones(self, router_id): - session = db_api.get_session() - with session.begin(): - query = session.query(agents_db.Agent.availability_zone) - query = query.join(RouterL3AgentBinding) - query = query.filter( - RouterL3AgentBinding.router_id == router_id) - query = query.group_by(agents_db.Agent.availability_zone) - return [item[0] for item in query] + def get_router_availability_zones(self, router): + return list({agent.availability_zone for agent in router.l3_agents}) diff --git a/neutron/db/l3_db.py b/neutron/db/l3_db.py index d27a62dc5..4c9009987 100644 --- a/neutron/db/l3_db.py +++ b/neutron/db/l3_db.py @@ -91,6 +91,8 @@ class Router(model_base.HasStandardAttributes, model_base.BASEV2, RouterPort, backref='router', lazy='dynamic') + l3_agents = orm.relationship('Agent', lazy='joined', viewonly=True, + secondary='routerl3agentbindings') class FloatingIP(model_base.HasStandardAttributes, model_base.BASEV2, diff --git a/neutron/extensions/router_availability_zone.py b/neutron/extensions/router_availability_zone.py index f7d2d92bc..3e09bb302 100644 --- a/neutron/extensions/router_availability_zone.py +++ b/neutron/extensions/router_availability_zone.py @@ -64,5 +64,5 @@ class Router_availability_zone(extensions.ExtensionDescriptor): class RouterAvailabilityZonePluginBase(object): @abc.abstractmethod - def get_router_availability_zones(self, router_id): + def get_router_availability_zones(self, router): """Return availability zones which a router belongs to.""" -- 2.45.2