def count(self, context, _plugin, tenant_id, resync_usage=False): """Return the current usage count for the resource.""" # Load current usage data usage_info = quota_api.get_quota_usage_by_resource_and_tenant( context, self.name, tenant_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 (tenant_id in self._dirty_tenants or not usage_info or usage_info.dirty): LOG.debug(("Usage tracker for resource:%(resource)s and tenant:" "%(tenant_id)s is out of sync, need to count used " "quota"), { 'resource': self.name, 'tenant_id': tenant_id }) in_use = context.session.query( self._model_class).filter_by(tenant_id=tenant_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 or not usage_info: usage_info = self._resync(context, tenant_id, in_use) else: usage_info = quota_api.QuotaUsageInfo(usage_info.resource, usage_info.tenant_id, in_use, usage_info.reserved, usage_info.dirty) return usage_info.total
def count(self, context, _plugin, tenant_id, resync_usage=True): """Return the current usage count for the resource. This method will fetch aggregate information for resource usage data, unless usage data are marked as "dirty". In the latter case resource usage will be calculated counting rows for tenant_id in the resource's database model. Active reserved amount are instead always calculated by summing amounts for matching records in the 'reservations' database model. The _plugin and _resource parameters are unused but kept for compatibility with the signature of the count method for CountableResource instances. """ # Load current usage data, setting a row-level lock on the DB usage_info = quota_api.get_quota_usage_by_resource_and_tenant( context, self.name, tenant_id, lock_for_update=True) # Always fetch reservations, as they are not tracked by usage counters reservations = quota_api.get_reservations_for_resources( context, tenant_id, [self.name]) reserved = reservations.get(self.name, 0) # 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 (tenant_id in self._dirty_tenants or not usage_info or usage_info.dirty): LOG.debug(("Usage tracker for resource:%(resource)s and tenant:" "%(tenant_id)s is out of sync, need to count used " "quota"), { 'resource': self.name, 'tenant_id': tenant_id }) in_use = context.session.query( self._model_class).filter_by(tenant_id=tenant_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, tenant_id, in_use) else: resource = usage_info.resource if usage_info else self.name tenant_id = usage_info.tenant_id if usage_info else tenant_id dirty = usage_info.dirty if usage_info else True usage_info = quota_api.QuotaUsageInfo(resource, tenant_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 + reserved
def count(self, context, _plugin, tenant_id, resync_usage=False): """Return the current usage count for the resource. This method will fetch the information from resource usage data, unless usage data are marked as "dirty", in which case both used and reserved resource are explicitly counted. The _plugin and _resource parameters are unused but kept for compatibility with the signature of the count method for CountableResource instances. """ # Load current usage data, setting a row-level lock on the DB usage_info = quota_api.get_quota_usage_by_resource_and_tenant( context, self.name, tenant_id, lock_for_update=True) # 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 (tenant_id in self._dirty_tenants or not usage_info or usage_info.dirty): LOG.debug(("Usage tracker for resource:%(resource)s and tenant:" "%(tenant_id)s is out of sync, need to count used " "quota"), { 'resource': self.name, 'tenant_id': tenant_id }) in_use = context.session.query( self._model_class).filter_by(tenant_id=tenant_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 or not usage_info: usage_info = self._resync(context, tenant_id, in_use, reserved=0) else: # NOTE(salv-orlando): Passing 0 for reserved amount as # reservations are currently not supported usage_info = quota_api.QuotaUsageInfo(usage_info.resource, usage_info.tenant_id, in_use, 0, usage_info.dirty) LOG.debug( ("Quota usage for %(resource)s was recalculated. " "Used quota:%(used)d; Reserved quota:%(reserved)d"), { 'resource': self.name, 'used': usage_info.used, 'reserved': usage_info.reserved }) return usage_info.total
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