Optimize quota calculations when they are disabled

Currently glare calculates the amount of data and artifacts
even if the related quotas are disabled (value -1 is set).
It's not optimal, because additional requests to db are
required.

This code changes this and implements new behaviour when
requsts to bd are performed only if quotas are enabled.

Also it disables quotas by default.

Change-Id: Ie297aa1bc6f2be989bcc29353c8fec0101cf2e5b
This commit is contained in:
Mike Fedosin 2017-09-27 17:43:17 +03:00
parent a30cdaf6a9
commit 8796021ca6
3 changed files with 34 additions and 33 deletions

View File

@ -73,10 +73,10 @@ class Engine(object):
self.schemas[type_name] = registry.ArtifactRegistry.\
get_artifact_type(type_name).gen_schemas()
type_conf_section = getattr(CONF, 'artifact_type:' + type_name)
if type_conf_section.max_artifact_number is not None:
if type_conf_section.max_artifact_number != -1:
self.config_quotas['max_artifact_number:' + type_name] = \
type_conf_section.max_artifact_number
if type_conf_section.max_uploaded_data is not None:
if type_conf_section.max_uploaded_data != -1:
self.config_quotas['max_uploaded_data:' + type_name] = \
type_conf_section.max_uploaded_data

View File

@ -27,13 +27,13 @@ from glare.objects.meta import validators
from glare.objects.meta import wrappers
global_artifact_opts = [
cfg.IntOpt('max_uploaded_data', default=1099511627776, # 1 Terabyte
cfg.IntOpt('max_uploaded_data', default=-1, # disabled
min=-1,
help=_("Defines how many bytes of data user can upload to "
"storage. This parameter is global and doesn't take "
"into account data of what type was uploaded. "
"Value -1 means no limit.")),
cfg.IntOpt('max_artifact_number', default=100,
cfg.IntOpt('max_artifact_number', default=-1, # disabled
min=-1,
help=_("Defines how many artifacts user can have. This "
"parameter is global and doesn't take "
@ -137,10 +137,10 @@ class BaseArtifact(base.VersionedObject):
}
common_artifact_type_opts = [
cfg.IntOpt('max_uploaded_data', min=-1,
cfg.IntOpt('max_uploaded_data', min=-1, default=-1,
help=_("Defines how many bytes of data of this type user "
"can upload to storage. Value -1 means no limit.")),
cfg.IntOpt('max_artifact_number', min=-1,
cfg.IntOpt('max_artifact_number', min=-1, default=-1,
help=_("Defines how many artifacts of this type user can "
"have. Value -1 means no limit.")),
cfg.BoolOpt('delayed_delete',

View File

@ -40,22 +40,24 @@ def verify_artifact_count(context, type_name):
type_limit = quotas['max_artifact_number:' + type_name]
session = api.get_session()
# the whole amount of created artifacts
whole_number = api.count_artifact_number(context, session)
if global_limit != -1 and whole_number >= global_limit:
msg = _("Can't create artifact because of global quota "
"limit is %(global_limit)d artifacts. "
"You have %(whole_number)d artifact(s).") % {
'global_limit': global_limit, 'whole_number': whole_number}
raise exception.Forbidden(msg)
if global_limit != -1:
# the whole amount of created artifacts
whole_number = api.count_artifact_number(context, session)
if type_limit is not None:
if whole_number >= global_limit:
msg = _("Can't create artifact because of global quota "
"limit is %(global_limit)d artifacts. "
"You have %(whole_number)d artifact(s).") % {
'global_limit': global_limit, 'whole_number': whole_number}
raise exception.Forbidden(msg)
if type_limit != -1:
# the amount of artifacts for specific type
type_number = api.count_artifact_number(
context, session, type_name)
if type_limit != -1 and type_number >= type_limit:
if type_number >= type_limit:
msg = _("Can't create artifact because of quota limit for "
"artifact type '%(type_name)s' is %(type_limit)d "
"artifacts. You have %(type_number)d artifact(s) "
@ -88,11 +90,11 @@ def verify_uploaded_data_amount(context, type_name, data_amount=None):
type_limit = quotas['max_uploaded_data:' + type_name]
session = api.get_session()
# the whole amount of created artifacts
whole_number = api.calculate_uploaded_data(context, session)
res = -1
if global_limit != -1:
# the whole amount of created artifacts
whole_number = api.calculate_uploaded_data(context, session)
if data_amount is None:
res = global_limit - whole_number
elif whole_number + data_amount > global_limit:
@ -104,24 +106,23 @@ def verify_uploaded_data_amount(context, type_name, data_amount=None):
'whole_number': whole_number}
raise exception.RequestEntityTooLarge(msg)
if type_limit is not None:
if type_limit != -1:
# the amount of artifacts for specific type
type_number = api.calculate_uploaded_data(
context, session, type_name)
if type_limit != -1:
if data_amount is None:
available = type_limit - type_number
res = available if res == -1 else min(res, available)
elif type_number + data_amount > type_limit:
msg = _("Can't upload %(data_amount)d byte(s) because of "
"quota limit for artifact type '%(type_name)s': "
"%(type_limit)d. You have %(type_number)d bytes "
"uploaded for this type.") % {
'data_amount': data_amount,
'type_name': type_name,
'type_limit': type_limit,
'type_number': type_number}
raise exception.RequestEntityTooLarge(msg)
if data_amount is None:
available = type_limit - type_number
res = available if res == -1 else min(res, available)
elif type_number + data_amount > type_limit:
msg = _("Can't upload %(data_amount)d byte(s) because of "
"quota limit for artifact type '%(type_name)s': "
"%(type_limit)d. You have %(type_number)d bytes "
"uploaded for this type.") % {
'data_amount': data_amount,
'type_name': type_name,
'type_limit': type_limit,
'type_number': type_number}
raise exception.RequestEntityTooLarge(msg)
return res