Commit 6a3f57af authored by Giorgos Korfiatis's avatar Giorgos Korfiatis Committed by Christos Stavrakakis
Browse files

astakos: Speed up setting quota for one resource

When registering a new resource or updating base quota for a single
resource, compute and set holdings just for this resource.
parent 4ab23c89
......@@ -116,9 +116,9 @@ def _level_quota_dict(quotas):
return lst
def _set_user_quota(quotas):
def _set_user_quota(quotas, resource=None):
q = _level_quota_dict(quotas)
qh.set_quota(q)
qh.set_quota(q, resource=resource)
SYSTEM = 'system'
......@@ -148,7 +148,7 @@ def update_base_quota(users, resource, value):
AstakosUserQuota.objects.\
filter(resource__name=resource, user__pk__in=userids).\
update(capacity=value)
qh_sync_locked_users(users)
qh_sync_locked_users(users, resource=resource)
def initial_quotas(users, flt=None):
......@@ -179,9 +179,10 @@ def add_limits(x, y):
return min(x+y, units.PRACTICALLY_INFINITE)
def astakos_users_quotas(users):
def astakos_users_quotas(users, resource=None):
users = list(users)
quotas = initial_quotas(users)
flt = Q(resource__name=resource) if resource is not None else Q()
quotas = initial_quotas(users, flt=flt)
userids = [user.pk for user in users]
ACTUALLY_ACCEPTED = ProjectMembership.ACTUALLY_ACCEPTED
......@@ -196,7 +197,7 @@ def astakos_users_quotas(users):
apps = set(m.project.application_id for m in memberships)
objs = ProjectResourceGrant.objects.select_related()
grants = objs.filter(project_application__in=apps)
grants = objs.filter(project_application__in=apps).filter(flt)
for membership in memberships:
uuid = membership.person.uuid
......@@ -239,15 +240,15 @@ def get_user_for_update(user_id):
return get_users_for_update([user_id])[0]
def qh_sync_locked_users(users):
astakos_quotas = astakos_users_quotas(users)
_set_user_quota(astakos_quotas)
def qh_sync_locked_users(users, resource=None):
astakos_quotas = astakos_users_quotas(users, resource=resource)
_set_user_quota(astakos_quotas, resource=resource)
def qh_sync_users(users):
def qh_sync_users(users, resource=None):
uids = [user.id for user in users]
users = get_users_for_update(uids)
qh_sync_locked_users(users)
qh_sync_locked_users(users, resource=resource)
def qh_sync_users_diffs(users, sync=True):
......@@ -313,4 +314,4 @@ def qh_sync_new_resource(resource):
AstakosUserQuota(user=user, resource=resource,
capacity=resource.uplimit))
AstakosUserQuota.objects.bulk_create(entries)
qh_sync_users(users)
qh_sync_users(users, resource=resource.name)
......@@ -75,9 +75,10 @@ def get_quota(holders=None, sources=None, resources=None, flt=None):
return quotas
def _get_holdings_for_update(holding_keys, delete=False):
def _get_holdings_for_update(holding_keys, resource=None, delete=False):
flt = Q(resource=resource) if resource is not None else Q()
holders = set(holder for (holder, source, resource) in holding_keys)
objs = Holding.objects.filter(holder__in=holders).order_by('pk')
objs = Holding.objects.filter(flt, holder__in=holders).order_by('pk')
hs = objs.select_for_update()
keys = set(holding_keys)
......@@ -105,16 +106,19 @@ def _mkProvision(key, quantity):
}
def set_quota(quotas):
def set_quota(quotas, resource=None):
holding_keys = [key for (key, limit) in quotas]
holdings = _get_holdings_for_update(holding_keys, delete=True)
holdings = _get_holdings_for_update(
holding_keys, resource=resource, delete=True)
new_holdings = {}
for key, limit in quotas:
holder, source, resource = key
holder, source, res = key
if resource is not None and resource != res:
continue
h = Holding(holder=holder,
source=source,
resource=resource,
resource=res,
limit=limit)
try:
h_old = holdings[key]
......
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