Commit 4fa32c33 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis Committed by Christos Stavrakakis
Browse files

quotaholder: Speed up set_quota with bulk_create

Instead of updating every single holding, delete all after loading them,
then reinsert them updated with bulk_create.
parent ecaabe17
......@@ -72,16 +72,24 @@ def get_quota(holders=None, sources=None, resources=None):
return quotas
def _get_holdings_for_update(holding_keys):
def _get_holdings_for_update(holding_keys, delete=False):
holders = set(holder for (holder, source, resource) in holding_keys)
objs = Holding.objects
hs = objs.filter(holder__in=holders).order_by('pk').select_for_update()
objs = Holding.objects.filter(holder__in=holders).order_by('pk')
hs = objs.select_for_update()
keys = set(holding_keys)
holdings = {}
put_back = []
for h in hs:
key = h.holder, h.source, h.resource
holdings[key] = h
if key in keys:
holdings[key] = h
else:
put_back.append(h)
if delete:
objs.delete()
Holding.objects.bulk_create(put_back)
return holdings
......@@ -96,19 +104,24 @@ def _mkProvision(key, quantity):
def set_quota(quotas):
holding_keys = [key for (key, limit) in quotas]
holdings = _get_holdings_for_update(holding_keys)
holdings = _get_holdings_for_update(holding_keys, delete=True)
new_holdings = {}
for key, limit in quotas:
holder, source, resource = key
h = Holding(holder=holder,
source=source,
resource=resource,
limit=limit)
try:
h = holdings[key]
h_old = holdings[key]
h.usage_min = h_old.usage_min
h.usage_max = h_old.usage_max
except KeyError:
holder, source, resource = key
h = Holding(holder=holder,
source=source,
resource=resource)
h.limit = limit
h.save()
holdings[key] = h
pass
new_holdings[key] = h
Holding.objects.bulk_create(new_holdings.values())
def add_resource_limit(holders=None, sources=None, resources=None, diff=0):
......
......@@ -34,6 +34,7 @@
from django.test import TestCase
from snf_django.utils.testing import assertGreater, assertIn, assertRaises
from astakos.quotaholder_app import models
import astakos.quotaholder_app.callpoint as qh
from astakos.quotaholder_app.exception import (
InvalidDataError,
......@@ -275,3 +276,25 @@ class QuotaholderTest(TestCase):
serial = self.issue_commission([])
r = qh.resolve_pending_commission(self.client, serial)
self.assertEqual(r, True)
def test_030_set(self):
holder = 'h0'
source = 'system'
resource1 = 'r1'
resource2 = 'r2'
limit1 = 10
limit2 = 20
models.Holding.objects.create(
holder=holder, source=source, resource=resource1,
usage_min=1, usage_max=1, limit=2)
models.Holding.objects.create(
holder=holder, source=source, resource=resource2,
usage_min=2, usage_max=2, limit=22)
qh.set_quota([((holder, source, resource1), limit1),
((holder, source, resource1), limit2)])
r = qh.get_quota(holders=[holder])
self.assertEqual(r, {(holder, source, resource1): (limit2, 1, 1),
(holder, source, resource2): (22, 2, 2)})
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