From 0d7b5392ab05aa8842b42bbbe3adc19773925e67 Mon Sep 17 00:00:00 2001 From: Salvatore Orlando Date: Thu, 4 Jun 2015 15:43:35 -0700 Subject: [PATCH] Enable resource usage tracking for reference plugins. Specify which resources should be tracked when initializing the ML2 and l3_router plugins. This will enable usage tracking for the following resources: - Networks - Ports - Subnets - Subnet pools - Security groups - Security group rules - Routers - Floating IPs Partially implements blueprint: bp/better-quotas Change-Id: I57287b15ffdadc30297651a01e9f05110e9ce6b6 --- neutron/plugins/ml2/plugin.py | 9 +++++++++ neutron/quota/resource.py | 15 +++++++-------- neutron/services/l3_router/l3_router_plugin.py | 3 +++ .../tests/unit/api/rpc/handlers/test_dhcp_rpc.py | 3 +++ .../tests/unit/extensions/test_securitygroup.py | 6 ++---- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/neutron/plugins/ml2/plugin.py b/neutron/plugins/ml2/plugin.py index 9a1d5a84e..77798c6b7 100644 --- a/neutron/plugins/ml2/plugin.py +++ b/neutron/plugins/ml2/plugin.py @@ -55,6 +55,7 @@ from neutron.db import extradhcpopt_db from neutron.db import models_v2 from neutron.db import netmtu_db from neutron.db.quota import driver # noqa +from neutron.db import securitygroups_db from neutron.db import securitygroups_rpc_base as sg_db_rpc from neutron.db import vlantransparent_db from neutron.extensions import allowedaddresspairs as addr_pair @@ -74,6 +75,7 @@ from neutron.plugins.ml2 import driver_context from neutron.plugins.ml2 import managers from neutron.plugins.ml2 import models from neutron.plugins.ml2 import rpc +from neutron.quota import resource_registry LOG = log.getLogger(__name__) @@ -126,6 +128,13 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2, self._aliases = aliases return self._aliases + @resource_registry.tracked_resources( + network=models_v2.Network, + port=models_v2.Port, + subnet=models_v2.Subnet, + subnetpool=models_v2.SubnetPool, + security_group=securitygroups_db.SecurityGroup, + security_group_rule=securitygroups_db.SecurityGroupRule) def __init__(self): # First load drivers, then initialize DB, then initialize drivers self.type_manager = managers.TypeManager() diff --git a/neutron/quota/resource.py b/neutron/quota/resource.py index d9a716a5e..eb0036859 100644 --- a/neutron/quota/resource.py +++ b/neutron/quota/resource.py @@ -17,6 +17,7 @@ from oslo_config import cfg from oslo_db import api as oslo_db_api from oslo_db import exception as oslo_db_exception from oslo_log import log +from oslo_utils import excutils from sqlalchemy import event from neutron.db import api as db_api @@ -191,14 +192,12 @@ class TrackedResource(BaseResource): @lockutils.synchronized('dirty_tenants') def _db_event_handler(self, mapper, _conn, target): - tenant_id = target.get('tenant_id') - if not tenant_id: - # NOTE: This is an unexpected error condition. Log anomaly but do - # not raise as this might have unexpected effects on other - # operations - LOG.error(_LE("Model class %s does not have tenant_id attribute"), - target) - return + try: + tenant_id = target['tenant_id'] + except AttributeError: + with excutils.save_and_reraise_exception(): + LOG.error(_LE("Model class %s does not have a tenant_id " + "attribute"), target) self._dirty_tenants.add(tenant_id) # Retry the operation if a duplicate entry exception is raised. This diff --git a/neutron/services/l3_router/l3_router_plugin.py b/neutron/services/l3_router/l3_router_plugin.py index 289f81d1b..4c4e96ae5 100644 --- a/neutron/services/l3_router/l3_router_plugin.py +++ b/neutron/services/l3_router/l3_router_plugin.py @@ -30,6 +30,7 @@ from neutron.db import l3_gwmode_db from neutron.db import l3_hamode_db from neutron.db import l3_hascheduler_db from neutron.plugins.common import constants +from neutron.quota import resource_registry class L3RouterPlugin(common_db_mixin.CommonDbMixin, @@ -52,6 +53,8 @@ class L3RouterPlugin(common_db_mixin.CommonDbMixin, "extraroute", "l3_agent_scheduler", "l3-ha"] + @resource_registry.tracked_resources(router=l3_db.Router, + floatingip=l3_db.FloatingIP) def __init__(self): self.setup_rpc() self.router_scheduler = importutils.import_object( diff --git a/neutron/tests/unit/api/rpc/handlers/test_dhcp_rpc.py b/neutron/tests/unit/api/rpc/handlers/test_dhcp_rpc.py index 78eb49308..3ad241d56 100644 --- a/neutron/tests/unit/api/rpc/handlers/test_dhcp_rpc.py +++ b/neutron/tests/unit/api/rpc/handlers/test_dhcp_rpc.py @@ -33,6 +33,9 @@ class TestDhcpRpcCallback(base.BaseTestCase): self.callbacks = dhcp_rpc.DhcpRpcCallback() self.log_p = mock.patch('neutron.api.rpc.handlers.dhcp_rpc.LOG') self.log = self.log_p.start() + set_dirty_p = mock.patch('neutron.quota.resource_registry.' + 'set_resources_dirty') + self.mock_set_dirty = set_dirty_p.start() def test_get_active_networks(self): plugin_retval = [dict(id='a'), dict(id='b')] diff --git a/neutron/tests/unit/extensions/test_securitygroup.py b/neutron/tests/unit/extensions/test_securitygroup.py index 31b191cf8..44e816ba0 100644 --- a/neutron/tests/unit/extensions/test_securitygroup.py +++ b/neutron/tests/unit/extensions/test_securitygroup.py @@ -452,8 +452,7 @@ class TestSecurityGroups(SecurityGroupDBTestCase): '22', '22', remote_ip_prefix, None, - None, - ethertype) + ethertype=ethertype) res = self._create_security_group_rule(self.fmt, rule) self.assertEqual(res.status_int, webob.exc.HTTPBadRequest.code) @@ -474,8 +473,7 @@ class TestSecurityGroups(SecurityGroupDBTestCase): '22', '22', remote_ip_prefix, None, - None, - ethertype) + ethertype=ethertype) res = self._create_security_group_rule(self.fmt, rule) self.assertEqual(res.status_int, 201) res_sg = self.deserialize(self.fmt, res) -- 2.45.2