def test_set_resources_quota_usage_dirty_with_empty_list(self): self._create_quota_usage('goals', 26) self._create_quota_usage('assists', 11) self._create_quota_usage('bookings', 3) # Expect all the resources for the project to be set dirty self.assertEqual( 3, quota_api.set_resources_quota_usage_dirty(self.context, [], self.project_id)) usage_info_goals = quota_api.get_quota_usage_by_resource_and_project( self.context, 'goals', self.project_id) usage_info_assists = quota_api.get_quota_usage_by_resource_and_project( self.context, 'assists', self.project_id) usage_info_bookings = ( quota_api.get_quota_usage_by_resource_and_project( self.context, 'bookings', self.project_id)) self._verify_quota_usage(usage_info_goals, expected_dirty=True) self._verify_quota_usage(usage_info_assists, expected_dirty=True) self._verify_quota_usage(usage_info_bookings, expected_dirty=True) # Higuain is clean now self.assertEqual( 1, quota_api.set_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) self._verify_quota_usage(usage_info, expected_dirty=False)
def test_get_quota_usage_by_project_and_resource(self): self._create_quota_usage('goals', 26) usage_info = quota_api.get_quota_usage_by_resource_and_project( self.context, 'goals', self.project_id) self._verify_quota_usage(usage_info, expected_resource='goals', expected_used=26)
def test_set_resources_quota_usage_dirty(self): self._create_quota_usage('goals', 26) self._create_quota_usage('assists', 11) self._create_quota_usage('bookings', 3) self.assertEqual( 2, quota_api.set_resources_quota_usage_dirty(self.context, ['goals', 'bookings'], self.project_id)) usage_info_goals = quota_api.get_quota_usage_by_resource_and_project( self.context, 'goals', self.project_id) usage_info_assists = quota_api.get_quota_usage_by_resource_and_project( self.context, 'assists', self.project_id) usage_info_bookings = ( quota_api.get_quota_usage_by_resource_and_project( self.context, 'bookings', self.project_id)) self._verify_quota_usage(usage_info_goals, expected_dirty=True) self._verify_quota_usage(usage_info_assists, expected_dirty=False) self._verify_quota_usage(usage_info_bookings, expected_dirty=True)
def test_set_quota_usage_dirty(self): self._create_quota_usage('goals', 26) # Higuain needs a shower after the match self.assertEqual( 1, quota_api.set_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.context, 'goals', self.project_id, dirty=False)) 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=False)
def count_used(self, context, project_id, resync_usage=True): """Returns the current usage count for the resource. :param context: The request context. :param project_id: The ID of the project :param resync_usage: Default value is set to True. Syncs with in_use usage. """ # Load current usage data, setting a row-level lock on the DB usage_info = quota_api.get_quota_usage_by_resource_and_project( context, self.name, project_id) # If dirty or missing, calculate actual resource usage querying # the database and set/create usage info data # NOTE: this routine "trusts" usage counters at service startup. This # assumption is generally valid, but if the database is tampered with, # or if data migrations do not take care of usage counters, the # assumption will not hold anymore if (project_id in self._dirty_projects or not usage_info or usage_info.dirty): LOG.debug(("Usage tracker for resource:%(resource)s and project:" "%(project_id)s is out of sync, need to count used " "quota"), { 'resource': self.name, 'project_id': project_id }) in_use = context.session.query( self._model_class.project_id).filter_by( project_id=project_id).count() # Update quota usage, if requested (by default do not do that, as # typically one counts before adding a record, and that would mark # the usage counter as dirty again) if resync_usage: usage_info = self._resync(context, project_id, in_use) else: resource = usage_info.resource if usage_info else self.name project_id = (usage_info.project_id if usage_info else project_id) dirty = usage_info.dirty if usage_info else True usage_info = quota_api.QuotaUsageInfo(resource, project_id, in_use, dirty) LOG.debug(("Quota usage for %(resource)s was recalculated. " "Used quota:%(used)d."), { 'resource': self.name, 'used': usage_info.used }) return usage_info.used
def test_get_non_existing_quota_usage_returns_none(self): self.assertIsNone( quota_api.get_quota_usage_by_resource_and_project( self.context, 'goals', self.project_id))
def _verify_dirty_bit(self, resource_name, expected_value=True): usage = quota_db_api.get_quota_usage_by_resource_and_project( self.ctx, resource_name, self._project_id) self.assertEqual(expected_value, usage.dirty)