From 8796021ca66dcbe31382253d38a78b874cc99dbe Mon Sep 17 00:00:00 2001 From: Mike Fedosin Date: Wed, 27 Sep 2017 17:43:17 +0300 Subject: [PATCH] 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 --- glare/engine.py | 4 ++-- glare/objects/base.py | 8 +++---- glare/quota.py | 55 ++++++++++++++++++++++--------------------- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/glare/engine.py b/glare/engine.py index 5bb885e..d3015f7 100644 --- a/glare/engine.py +++ b/glare/engine.py @@ -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 diff --git a/glare/objects/base.py b/glare/objects/base.py index 4c9942e..61ba8ff 100644 --- a/glare/objects/base.py +++ b/glare/objects/base.py @@ -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', diff --git a/glare/quota.py b/glare/quota.py index 6fc2c70..5e58277 100644 --- a/glare/quota.py +++ b/glare/quota.py @@ -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