Commit 44087f00 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis Committed by Christos Stavrakakis
Browse files

astakos: Recompute quota when updating a resource

Avoid stateful update of holdings when a resource limit changes; rather
recompute and set all affected users' quota.
parent 4fa32c33
......@@ -311,13 +311,12 @@ def qh_sync_project(project):
qh_sync_users(users)
def qh_add_resource_limit(resource, diff):
def qh_change_resource_limit(resource):
objs = AstakosUser.objects.filter(
Q(moderated=True, is_rejected=False) & ~Q(policy=resource))
users = objs.order_by('id').select_for_update()
uuids = [u.uuid for u in users]
qh.add_resource_limit(holders=uuids, sources=[SYSTEM],
resources=[resource.name], diff=diff)
quota = astakos_users_quotas(users)
_set_user_quota(quota)
def qh_sync_new_resource(resource, limit):
......
......@@ -32,8 +32,7 @@
# or implied, of GRNET S.A.
from astakos.im.models import Resource, Service, Endpoint, EndpointData
from astakos.im.quotas import qh_add_resource_limit, qh_sync_new_resource
from astakos.im import quotas
import logging
logger = logging.getLogger(__name__)
......@@ -95,7 +94,7 @@ def add_resource(resource_dict):
r.save()
if not exists:
qh_sync_new_resource(r, 0)
quotas.qh_sync_new_resource(r, 0)
if exists:
logger.info("Updated resource %s." % (name))
......@@ -106,14 +105,17 @@ def add_resource(resource_dict):
def update_resource(resource, uplimit):
old_uplimit = resource.uplimit
resource.uplimit = uplimit
resource.save()
logger.info("Updated resource %s with limit %s."
% (resource.name, uplimit))
diff = uplimit - old_uplimit
if diff != 0:
qh_add_resource_limit(resource, diff)
if uplimit == old_uplimit:
logger.info("Resource %s has limit %s; no need to update."
% (resource.name, uplimit))
return []
else:
resource.uplimit = uplimit
resource.save()
logger.info("Updated resource %s with limit %s."
% (resource.name, uplimit))
affected = quotas.qh_change_resource_limit(resource)
return affected
def get_resources(resources=None, services=None):
......
......@@ -124,21 +124,6 @@ def set_quota(quotas):
Holding.objects.bulk_create(new_holdings.values())
def add_resource_limit(holders=None, sources=None, resources=None, diff=0):
holdings = Holding.objects.all()
if holders is not None:
holdings = holdings.filter(holder__in=holders)
if sources is not None:
holdings = holdings.filter(source__in=sources)
if resources is not None:
holdings = holdings.filter(resource__in=resources)
holdings.update(limit=F('limit')+diff)
def issue_commission(clientkey, provisions, name="", force=False):
operations = Operations()
provisions_to_create = []
......
......@@ -254,24 +254,6 @@ class QuotaholderTest(TestCase):
}
self.assertEqual(r, quotas)
# add resource limit
qh.add_resource_limit(sources=[source], resources=[resource1], diff=1)
r = qh.get_quota(holders=[holder])
quotas = {
(holder, source, resource1): (limit1+1, limit1+1, limit1+2),
(holder, source, resource2): (limit2, limit2-2, limit2-1),
}
self.assertEqual(r, quotas)
qh.add_resource_limit(holders=[holder, "nonex"], diff=10)
r = qh.get_quota(holders=[holder, "nonex"])
quotas = {
(holder, source, resource1): (limit1+11, limit1+1, limit1+2),
(holder, source, resource2): (limit2+10, limit2-2, limit2-1),
}
self.assertEqual(r, quotas)
def test_020_empty_provisions(self):
serial = self.issue_commission([])
r = qh.resolve_pending_commission(self.client, serial)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment