]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Get all interfaces for get_snat_sync_interfaces
authorKevin Benton <blak111@gmail.com>
Fri, 17 Apr 2015 12:10:26 +0000 (05:10 -0700)
committerKevin Benton <kevinbenton@buttewifi.com>
Sun, 3 May 2015 01:48:38 +0000 (01:48 +0000)
The get_snat_sync_interfaces method was being called for
each router individually during a sync, which resulted
in a new query to the database.

This patch eliminates that waste by querying for the snat
interfaces for all of the routers in the list at once.

Change-Id: I1e44a0cf15a70632e8b62ac89ce807a7a457747d
Partial-Bug: #1445412

neutron/db/l3_dvr_db.py

index 678dd39ac9a194f4999e5e949c3f8d63259c9fb7..3d15e969829304a00c5282ce77e5cd94bfe38446 100644 (file)
@@ -11,6 +11,7 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    under the License.
+import collections
 
 from oslo_config import cfg
 from oslo_log import log as logging
@@ -358,9 +359,10 @@ class L3_NAT_with_dvr_db_mixin(l3_db.L3_NAT_db_mixin,
             l3_db.RouterPort.router_id.in_(router_ids),
             l3_db.RouterPort.port_type == DEVICE_OWNER_DVR_SNAT
         )
-
-        interfaces = [self._core_plugin._make_port_dict(rp.port, None)
-                      for rp in qry]
+        interfaces = collections.defaultdict(list)
+        for rp in qry:
+            interfaces[rp.router_id].append(
+                self._core_plugin._make_port_dict(rp.port, None))
         LOG.debug("Return the SNAT ports: %s", interfaces)
         return interfaces
 
@@ -395,12 +397,12 @@ class L3_NAT_with_dvr_db_mixin(l3_db.L3_NAT_db_mixin,
 
     def _process_routers(self, context, routers):
         routers_dict = {}
+        snat_intfs_by_router_id = self._get_snat_sync_interfaces(
+            context, [r['id'] for r in routers])
         for router in routers:
             routers_dict[router['id']] = router
-            router_ids = [router['id']]
             if router['gw_port_id']:
-                snat_router_intfs = self._get_snat_sync_interfaces(context,
-                                                                  router_ids)
+                snat_router_intfs = snat_intfs_by_router_id[router['id']]
                 LOG.debug("SNAT ports returned: %s ", snat_router_intfs)
                 router[SNAT_ROUTER_INTF_KEY] = snat_router_intfs
         return routers_dict