Create a single method to set the quota usage dirty bit

The method ``set_resources_quota_usage_dirty`` can be used now to set
the dirty bit for a single resource or multiple ones.

Trivial-Fix

Change-Id: I13ef43b71fe7a080d55a84119784433ad84380b6
This commit is contained in:
Rodolfo Alonso Hernandez 2023-07-06 02:10:32 +00:00
parent 4fdcc03118
commit 2c0e9cfa71
5 changed files with 39 additions and 51 deletions

View File

@ -112,41 +112,23 @@ def set_quota_usage(context, resource, project_id, in_use=None, delta=False):
usage_data.in_use, usage_data.dirty)
@db_api.retry_if_session_inactive()
@db_api.CONTEXT_WRITER
def set_quota_usage_dirty(context, resource, project_id, dirty=True):
"""Set quota usage dirty bit for a given resource and project.
:param resource: a resource for which quota usage if tracked
:param project_id: project identifier
:param dirty: the desired value for the dirty bit (defaults to True)
:returns: 1 if the quota usage data were updated, 0 otherwise.
"""
obj = quota_obj.QuotaUsage.get_object(
context, resource=resource, project_id=project_id)
if obj:
obj.dirty = dirty
obj.update()
return 1
return 0
@db_api.retry_if_session_inactive()
@db_api.CONTEXT_WRITER
def set_resources_quota_usage_dirty(context, resources, project_id,
dirty=True):
"""Set quota usage dirty bit for a given project and multiple resources.
"""Set quota usage dirty bit for a given project and one/multiple resources
:param resources: list of resource for which the dirty bit is going
to be set
:param resources: (list of strings, string) list of resources or one single
resource, for which the dirty bit is going to be set
:param project_id: project identifier
:param dirty: the desired value for the dirty bit (defaults to True)
:returns: the number of records for which the bit was actually set.
"""
filters = {'project_id': project_id}
filters = {}
if resources:
filters['resource'] = resources
objs = quota_obj.QuotaUsage.get_objects(context, **filters)
objs = quota_obj.QuotaUsage.get_objects(context, project_id=project_id,
**filters)
for obj in objs:
obj.dirty = dirty
obj.update()

View File

@ -222,7 +222,8 @@ class TrackedResource(BaseResource):
# won't be harmful.
dirty_projects_snap = self._dirty_projects.copy()
for project_id in dirty_projects_snap:
quota_api.set_quota_usage_dirty(context, self.name, project_id)
quota_api.set_resources_quota_usage_dirty(context, self.name,
project_id)
self._out_of_sync_projects |= dirty_projects_snap
self._dirty_projects -= dirty_projects_snap

View File

@ -21,6 +21,7 @@ from neutron_lib.plugins import directory
from oslo_config import cfg
from neutron.db.quota import api as quota_api
from neutron import policy # noqa
from neutron.tests.unit.db.quota import test_driver
from neutron.tests.unit import testlib_api
@ -94,17 +95,17 @@ class TestQuotaDbApi(testlib_api.SqlTestCaseLight):
self._verify_quota_usage(usage_info_1,
expected_used=28)
def test_set_quota_usage_dirty(self):
def test_set_resources_quota_usage_dirty_one_resource_only(self):
self._create_quota_usage('goals', 26)
# Higuain needs a shower after the match
self.assertEqual(1, quota_api.set_quota_usage_dirty(
self.assertEqual(1, quota_api.set_resources_quota_usage_dirty(
self.context, 'goals', self.project_id))
usage_info = quota_api.get_quota_usage_by_resource_and_project(
self.context, 'goals', self.project_id)
self._verify_quota_usage(usage_info,
expected_dirty=True)
# Higuain is clean now
self.assertEqual(1, quota_api.set_quota_usage_dirty(
self.assertEqual(1, quota_api.set_resources_quota_usage_dirty(
self.context, 'goals', self.project_id, dirty=False))
usage_info = quota_api.get_quota_usage_by_resource_and_project(
self.context, 'goals', self.project_id)
@ -112,8 +113,10 @@ class TestQuotaDbApi(testlib_api.SqlTestCaseLight):
expected_dirty=False)
def test_set_dirty_non_existing_quota_usage(self):
self.assertEqual(0, quota_api.set_quota_usage_dirty(
self.assertEqual(0, quota_api.set_resources_quota_usage_dirty(
self.context, 'meh', self.project_id))
self.assertEqual(0, quota_api.set_resources_quota_usage_dirty(
self.context, ['meh1', 'meh2'], self.project_id))
def test_set_resources_quota_usage_dirty(self):
self._create_quota_usage('goals', 26)
@ -151,7 +154,7 @@ class TestQuotaDbApi(testlib_api.SqlTestCaseLight):
self._verify_quota_usage(usage_info_bookings, expected_dirty=True)
# Higuain is clean now
self.assertEqual(1, quota_api.set_quota_usage_dirty(
self.assertEqual(1, quota_api.set_resources_quota_usage_dirty(
self.context, 'goals', self.project_id, dirty=False))
usage_info = quota_api.get_quota_usage_by_resource_and_project(
self.context, 'goals', self.project_id)

View File

@ -226,7 +226,7 @@ class TestTrackedResources(BaseTestTrackedResources):
net = self._make_network('json', 'meh', True)['network']
self._verify_dirty_bit('network')
# Clear the dirty bit
quota_db_api.set_quota_usage_dirty(
quota_db_api.set_resources_quota_usage_dirty(
self.ctx, 'network', self._project_id, dirty=False)
self._delete('networks', net['id'])
self._verify_dirty_bit('network')
@ -243,7 +243,7 @@ class TestTrackedResources(BaseTestTrackedResources):
port = self._make_port('json', net['id'])['port']
self._verify_dirty_bit('port')
# Clear the dirty bit
quota_db_api.set_quota_usage_dirty(
quota_db_api.set_resources_quota_usage_dirty(
self.ctx, 'port', self._project_id, dirty=False)
self._delete('ports', port['id'])
self._verify_dirty_bit('port')
@ -262,7 +262,7 @@ class TestTrackedResources(BaseTestTrackedResources):
'10.0.0.0/24')['subnet']
self._verify_dirty_bit('subnet')
# Clear the dirty bit
quota_db_api.set_quota_usage_dirty(
quota_db_api.set_resources_quota_usage_dirty(
self.ctx, 'subnet', self._project_id, dirty=False)
self._delete('subnets', subnet['id'])
self._verify_dirty_bit('subnet')
@ -275,7 +275,7 @@ class TestTrackedResources(BaseTestTrackedResources):
'10.0.0.0/24')['subnet']
self._verify_dirty_bit('subnet')
# Clear the dirty bit
quota_db_api.set_quota_usage_dirty(
quota_db_api.set_resources_quota_usage_dirty(
self.ctx, 'subnet', self._project_id, dirty=False)
self._delete('networks', net['network']['id'])
self._verify_dirty_bit('network')
@ -294,7 +294,7 @@ class TestTrackedResources(BaseTestTrackedResources):
name='meh')['subnetpool']
self._verify_dirty_bit('subnetpool')
# Clear the dirty bit
quota_db_api.set_quota_usage_dirty(
quota_db_api.set_resources_quota_usage_dirty(
self.ctx, 'subnetpool', self._project_id, dirty=False)
self._delete('subnetpools', pool['id'])
self._verify_dirty_bit('subnetpool')
@ -311,7 +311,7 @@ class TestTrackedResources(BaseTestTrackedResources):
'json', 'meh', 'meh')['security_group']
self._verify_dirty_bit('security_group')
# Clear the dirty bit
quota_db_api.set_quota_usage_dirty(
quota_db_api.set_resources_quota_usage_dirty(
self.ctx, 'security_group', self._project_id, dirty=False)
self._delete('security-groups', sec_group['id'])
self._verify_dirty_bit('security_group')
@ -333,7 +333,7 @@ class TestTrackedResources(BaseTestTrackedResources):
'json', rule_req)['security_group_rule']
self._verify_dirty_bit('security_group_rule')
# Clear the dirty bit
quota_db_api.set_quota_usage_dirty(
quota_db_api.set_resources_quota_usage_dirty(
self.ctx, 'security_group_rule', self._project_id, dirty=False)
self._delete('security-group-rules', sec_group_rule['id'])
self._verify_dirty_bit('security_group_rule')

View File

@ -214,9 +214,9 @@ class TestTrackedResource(testlib_api.SqlTestCase):
res = self._test_count()
set_quota_usage = 'neutron.db.quota.api.set_quota_usage'
with mock.patch(set_quota_usage) as mock_set_quota_usage:
quota_api.set_quota_usage_dirty(self.context,
self.resource,
self.project_id)
quota_api.set_resources_quota_usage_dirty(self.context,
self.resource,
self.project_id)
res.count(self.context, None, self.project_id,
resync_usage=True)
mock_set_quota_usage.assert_called_once_with(
@ -226,9 +226,9 @@ class TestTrackedResource(testlib_api.SqlTestCase):
res = self._test_count()
set_quota_usage = 'neutron.db.quota.api.set_quota_usage'
with mock.patch(set_quota_usage) as mock_set_quota_usage:
quota_api.set_quota_usage_dirty(self.context,
self.resource,
self.project_id)
quota_api.set_resources_quota_usage_dirty(self.context,
self.resource,
self.project_id)
res.count_used(self.context, self.project_id,
resync_usage=True)
mock_set_quota_usage.assert_called_once_with(
@ -253,9 +253,9 @@ class TestTrackedResource(testlib_api.SqlTestCase):
self._add_data()
set_quota_usage = 'neutron.db.quota.api.set_quota_usage'
with mock.patch(set_quota_usage) as mock_set_quota_usage:
quota_api.set_quota_usage_dirty(self.context,
self.resource,
self.project_id)
quota_api.set_resources_quota_usage_dirty(self.context,
self.resource,
self.project_id)
res.count(self.context, None, self.project_id, resync_usage=True)
mock_set_quota_usage.assert_called_once_with(
self.context, self.resource, self.project_id, in_use=2)
@ -266,9 +266,9 @@ class TestTrackedResource(testlib_api.SqlTestCase):
self._add_data()
set_quota_usage = 'neutron.db.quota.api.set_quota_usage'
with mock.patch(set_quota_usage) as mock_set_quota_usage:
quota_api.set_quota_usage_dirty(self.context,
self.resource,
self.project_id)
quota_api.set_resources_quota_usage_dirty(self.context,
self.resource,
self.project_id)
res.count_used(self.context, self.project_id, resync_usage=True)
mock_set_quota_usage.assert_called_once_with(
self.context, self.resource, self.project_id, in_use=2)
@ -311,7 +311,8 @@ class TestTrackedResource(testlib_api.SqlTestCase):
res = self._create_resource()
self._add_data()
self._add_data('someone_else')
set_quota_usage = 'neutron.db.quota.api.set_quota_usage_dirty'
set_quota_usage = (
'neutron.db.quota.api.set_resources_quota_usage_dirty')
with mock.patch(set_quota_usage) as mock_set_quota_usage:
res.mark_dirty(self.context)
self.assertEqual(2, mock_set_quota_usage.call_count)
@ -322,7 +323,8 @@ class TestTrackedResource(testlib_api.SqlTestCase):
def test_mark_dirty_no_dirty_project(self):
res = self._create_resource()
set_quota_usage = 'neutron.db.quota.api.set_quota_usage_dirty'
set_quota_usage = (
'neutron.db.quota.api.set_resources_quota_usage_dirty')
with mock.patch(set_quota_usage) as mock_set_quota_usage:
res.mark_dirty(self.context)
self.assertFalse(mock_set_quota_usage.call_count)