Commit 7608c66c authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

wip Remove import_limit and export_limit from policy

If needed, they can be simulated by importing from special resources.
parent fceaf461
......@@ -35,7 +35,6 @@ from astakos.quotaholder.exception import (
QuotaholderError,
CorruptedError, InvalidDataError,
NoQuantityError, NoCapacityError,
ExportLimitError, ImportLimitError,
DuplicateError)
from astakos.quotaholder.utils.newname import newname
......@@ -62,15 +61,13 @@ class QuotaholderDjangoDBCallpoint(object):
except Policy.DoesNotExist:
continue
append((policy, p.quantity, p.capacity,
p.import_limit, p.export_limit))
append((policy, p.quantity, p.capacity))
return limits
def set_limits(self, context=None, set_limits=()):
for (policy, quantity, capacity,
import_limit, export_limit) in set_limits:
for (policy, quantity, capacity) in set_limits:
try:
policy = db_get_policy(policy=policy, for_update=True)
......@@ -78,13 +75,10 @@ class QuotaholderDjangoDBCallpoint(object):
Policy.objects.create(policy=policy,
quantity=quantity,
capacity=capacity,
import_limit=import_limit,
export_limit=export_limit)
)
else:
policy.quantity = quantity
policy.capacity = capacity
policy.export_limit = export_limit
policy.import_limit = import_limit
policy.save()
return ()
......@@ -287,7 +281,6 @@ class QuotaholderDjangoDBCallpoint(object):
p = h.policy
append((h.holder, h.resource, p.quantity, p.capacity,
p.import_limit, p.export_limit,
h.imported, h.exported,
h.returned, h.released,
h.flags))
......@@ -300,7 +293,7 @@ class QuotaholderDjangoDBCallpoint(object):
q_holdings = Q()
holders = []
for (holder, resource, _, _, _, _, _) in set_quota:
for (holder, resource, _, _, _) in set_quota:
holders.append(holder)
hs = Holding.objects.filter(holder__in=holders).select_for_update()
......@@ -312,14 +305,13 @@ class QuotaholderDjangoDBCallpoint(object):
for (holder, resource,
quantity, capacity,
import_limit, export_limit, flags) in set_quota:
flags) in set_quota:
policy = newname('policy_')
newp = Policy(policy=policy,
quantity=quantity,
capacity=capacity,
import_limit=import_limit,
export_limit=export_limit)
)
try:
h = holdings[(holder, resource)]
......@@ -353,7 +345,7 @@ class QuotaholderDjangoDBCallpoint(object):
sources = sub_quota + add_quota
q_holdings = Q()
holders = []
for (holder, resource, _, _, _, _) in sources:
for (holder, resource, _, _) in sources:
holders.append(holder)
hs = Holding.objects.filter(holder__in=holders).select_for_update()
......@@ -369,7 +361,7 @@ class QuotaholderDjangoDBCallpoint(object):
for removing, source in [(True, sub_quota), (False, add_quota)]:
for (holder, resource,
quantity, capacity,
import_limit, export_limit) in source:
) in source:
try:
h = holdings[(holder, resource)]
......@@ -393,14 +385,8 @@ class QuotaholderDjangoDBCallpoint(object):
invert=removing)
newp.capacity = _add(p.capacity if p else 0, capacity,
invert=removing)
newp.import_limit = _add(p.import_limit if p else 0,
import_limit, invert=removing)
newp.export_limit = _add(p.export_limit if p else 0,
export_limit, invert=removing)
new_values = [newp.capacity,
newp.import_limit, newp.export_limit]
if any(map(_isneg, new_values)):
if _isneg(newp.capacity):
append((holder, resource))
continue
......@@ -466,18 +452,6 @@ class QuotaholderDjangoDBCallpoint(object):
hp = h.policy
if not release:
current = h.exporting
limit = hp.export_limit
if current + quantity > limit:
m = ("Export limit reached for %s.%s" % (holder, resource))
raise ExportLimitError(m,
source=holder,
target=target,
resource=resource,
requested=quantity,
current=current,
limit=limit)
limit = hp.quantity + h.imported - h.releasing
unavailable = h.exporting - h.returned
available = limit - unavailable
......@@ -525,18 +499,6 @@ class QuotaholderDjangoDBCallpoint(object):
tp = th.policy
if not release:
limit = tp.import_limit
current = th.importing
if current + quantity > limit:
m = ("Import limit reached for %s.%s" % (target, resource))
raise ImportLimitError(m,
source=holder,
target=target,
resource=resource,
requested=quantity,
current=current,
limit=limit)
limit = tp.quantity + tp.capacity
current = (+ th.importing + th.returning + tp.quantity
- th.exported - th.released)
......@@ -600,16 +562,12 @@ class QuotaholderDjangoDBCallpoint(object):
'resource': provision.resource,
'source_quantity': s_policy.quantity,
'source_capacity': s_policy.capacity,
'source_import_limit': s_policy.import_limit,
'source_export_limit': s_policy.export_limit,
'source_imported': s_holding.imported,
'source_exported': s_holding.exported,
'source_returned': s_holding.returned,
'source_released': s_holding.released,
'target_quantity': t_policy.quantity,
'target_capacity': t_policy.capacity,
'target_import_limit': t_policy.import_limit,
'target_export_limit': t_policy.export_limit,
'target_imported': t_holding.imported,
'target_exported': t_holding.exported,
'target_returned': t_holding.returned,
......
......@@ -67,13 +67,5 @@ class NoCapacityError(CommissionValueException):
pass
class ExportLimitError(CommissionValueException):
pass
class ImportLimitError(CommissionValueException):
pass
class DuplicateError(CommissionException):
pass
......@@ -44,8 +44,6 @@ class Policy(Model):
policy = CharField(max_length=4096, primary_key=True)
quantity = intDecimalField()
capacity = intDecimalField()
import_limit = intDecimalField()
export_limit = intDecimalField()
objects = ForUpdateManager()
......@@ -111,16 +109,12 @@ class ProvisionLog(Model):
resource = CharField(max_length=4096)
source_quantity = intDecimalField()
source_capacity = intDecimalField()
source_import_limit = intDecimalField()
source_export_limit = intDecimalField()
source_imported = intDecimalField()
source_exported = intDecimalField()
source_returned = intDecimalField()
source_released = intDecimalField()
target_quantity = intDecimalField()
target_capacity = intDecimalField()
target_import_limit = intDecimalField()
target_export_limit = intDecimalField()
target_imported = intDecimalField()
target_exported = intDecimalField()
target_returned = intDecimalField()
......
......@@ -39,7 +39,6 @@ from astakos.quotaholder.exception import (
QuotaholderError,
InvalidDataError,
NoQuantityError, NoCapacityError,
ExportLimitError, ImportLimitError,
CommissionValueException,
DuplicateError)
......@@ -87,9 +86,7 @@ class QHAPITest(QHTestCase):
def rand_limits(self):
q = random_nat()
c = random_nat()
il = random_nat()
el = random_nat()
return q, c, il, el
return q, c,
def rand_policy_limits(self):
p = self.rand_policy()
......@@ -183,37 +180,37 @@ class QHAPITest(QHTestCase):
resource1 = self.rand_resource()
self.qh.set_quota(
set_quota=[(e0, resource0) + (5, QH_PRACTICALLY_INFINITE, 5, 6) + (0,),
(e1, resource0) + (5, 5, 5, 5) + (0,)])
set_quota=[(e0, resource0) + (5, QH_PRACTICALLY_INFINITE) + (0,),
(e1, resource0) + (5, 5) + (0,)])
self.qh.add_quota(sub_quota=[(e0, resource0,
0, QH_PRACTICALLY_INFINITE, 1, 1)],
0, QH_PRACTICALLY_INFINITE)],
add_quota=[(e0, resource0,
0, 3, QH_PRACTICALLY_INFINITE, 0),
0, 3),
# new holding
(e0, resource1,
0, QH_PRACTICALLY_INFINITE, 5, 5)])
0, QH_PRACTICALLY_INFINITE)])
r = self.qh.get_quota(get_quota=[(e0, resource0),
(e0, resource1)])
self.assertEqual(r, [(e0, resource0, 5, 3, QH_PRACTICALLY_INFINITE+4, 5)
self.assertEqual(r, [(e0, resource0, 5, 3)
+ DEFAULT_HOLDING + (0,),
(e0, resource1, 0, QH_PRACTICALLY_INFINITE, 5, 5)
(e0, resource1, 0, QH_PRACTICALLY_INFINITE)
+ DEFAULT_HOLDING + (0,)])
with self.assertRaises(QuotaholderError) as cm:
self.qh.add_quota(add_quota=[(e1, resource0,
0, (-10), QH_PRACTICALLY_INFINITE, 0),
(e0, resource1, 1, 0, 0, 0)])
0, (-10)),
(e0, resource1, 1, 0)])
err = cm.exception
self.assertEqual(err.message, [(e1, resource0)])
# r = self.qh.get_quota(get_quota=[(e1, resource0),
# (e0, resource1)])
# self.assertEqual(r, [(e1, resource0, 5, 5 , 5, 5)
# self.assertEqual(r, [(e1, resource0, 5, 5)
# + DEFAULT_HOLDING + (0,),
# (e0, resource1, 0, QH_PRACTICALLY_INFINITE, 5, 5)
# (e0, resource1, 0, QH_PRACTICALLY_INFINITE)
# + DEFAULT_HOLDING + (0,)])
@transaction.commit_on_success
......@@ -225,13 +222,13 @@ class QHAPITest(QHTestCase):
self.qh.set_quota(
set_quota=[(e0, resource0) +
(5, QH_PRACTICALLY_INFINITE, 5, 6) + (0,)])
(5, QH_PRACTICALLY_INFINITE) + (0,)])
self.qh.add_quota(add_quota=[(e0, resource0,
0, QH_PRACTICALLY_INFINITE, 0, 0)])
0, QH_PRACTICALLY_INFINITE)])
r = self.qh.get_quota(get_quota=[(e0, resource0)])
self.assertEqual(r, [(e0, resource0, 5, 2*QH_PRACTICALLY_INFINITE, 5, 6)
self.assertEqual(r, [(e0, resource0, 5, 2*QH_PRACTICALLY_INFINITE)
+ DEFAULT_HOLDING + (0,)])
@transaction.commit_on_success
......@@ -239,10 +236,10 @@ class QHAPITest(QHTestCase):
e0 = self.rand_holder()
e1 = self.rand_holder()
resource = self.rand_resource()
q0, c0, il0, el0 = self.new_quota(e0, resource)
q1, c1, il1, el1 = self.new_quota(e1, resource)
q0, c0 = self.new_quota(e0, resource)
q1, c1 = self.new_quota(e1, resource)
most = min(c0, il0, q1, el1)
most = min(c0, q1)
if most < 0:
raise AssertionError("%s <= 0" % most)
......@@ -278,10 +275,10 @@ class QHAPITest(QHTestCase):
et1 = self.rand_holder()
et2 = self.rand_holder()
resource = self.rand_resource()
self.new_quota(es1, resource, (10, 5, 5, 15))
self.new_quota(es2, resource, (10, 5, 5, 10))
self.new_quota(et1, resource, (0, 15, 3, 20))
self.new_quota(et2, resource, (0, 15, 20, 20))
self.new_quota(es1, resource, (10, 5))
self.new_quota(es2, resource, (10, 5))
self.new_quota(et1, resource, (0, 15))
self.new_quota(et2, resource, (0, 15))
with self.assertRaises(NoQuantityError) as cm:
self.qh.issue_commission(clientkey=self.client, target=et1,
......@@ -300,17 +297,17 @@ class QHAPITest(QHTestCase):
provisions=[(es1, resource, 2)])
self.assertGreater(r, 0)
with self.assertRaises(ImportLimitError) as cm:
self.qh.issue_commission(clientkey=self.client, target=et1,
name='something',
provisions=[(es1, resource, 2)])
e = cm.exception
self.assertEqual(e.source, es1)
self.assertEqual(e.target, et1)
self.assertEqual(e.resource, resource)
self.assertEqual(int(e.limit), 3)
self.assertEqual(int(e.requested), 2)
self.assertEqual(int(e.current), 2)
# with self.assertRaises(ImportLimitError) as cm:
# self.qh.issue_commission(clientkey=self.client, target=et1,
# name='something',
# provisions=[(es1, resource, 2)])
# e = cm.exception
# self.assertEqual(e.source, es1)
# self.assertEqual(e.target, et1)
# self.assertEqual(e.resource, resource)
# self.assertEqual(int(e.limit), 3)
# self.assertEqual(int(e.requested), 2)
# self.assertEqual(int(e.current), 2)
r = self.qh.issue_commission(clientkey=self.client, target=et2,
name='something',
......@@ -338,15 +335,9 @@ class QHAPITest(QHTestCase):
resource = 'list_holdings_resource'
sys = 'system'
self.qh.set_quota(set_quota=[(sys, resource, 10, 0,
QH_PRACTICALLY_INFINITE,
QH_PRACTICALLY_INFINITE, 0),
(e0, resource, 0, 10,
QH_PRACTICALLY_INFINITE,
QH_PRACTICALLY_INFINITE, 0),
(e1, resource, 0, 10,
QH_PRACTICALLY_INFINITE,
QH_PRACTICALLY_INFINITE, 0)])
self.qh.set_quota(set_quota=[(sys, resource, 10, 0, 0),
(e0, resource, 0, 10, 0),
(e1, resource, 0, 10, 0)])
s0 = self.qh.issue_commission(clientkey=self.client, target=e0,
name='a commission',
......@@ -370,7 +361,7 @@ class QHAPITest(QHTestCase):
def test_0130_release_holding(self):
e = self.rand_holder()
resource = self.rand_resource()
limits = self.new_quota(e, resource, (1, 2, 3, 4))
limits = self.new_quota(e, resource, (1, 2))
with self.assertRaises(QuotaholderError) as cm:
self.qh.release_holding(release_holding=[(e, resource)])
......@@ -382,7 +373,7 @@ class QHAPITest(QHTestCase):
def test_0131_release_holding(self):
e = self.rand_holder()
resource = self.rand_resource()
limits = self.new_quota(e, resource, (0, 2, 3, 4))
limits = self.new_quota(e, resource, (0, 2))
self.qh.release_holding(release_holding=[(e, resource)])
......@@ -391,9 +382,9 @@ class QHAPITest(QHTestCase):
resource = self.rand_resource()
es = self.rand_holder()
limits_s = self.new_quota(es, resource, (3, 3, 3, 3))
limits_s = self.new_quota(es, resource, (3, 3))
e = self.rand_holder()
limits = self.new_quota(e, resource, (0, 2, 3, 4))
limits = self.new_quota(e, resource, (0, 2))
r = self.qh.issue_commission(clientkey=self.client, target=e,
name='something',
......@@ -436,9 +427,9 @@ class QHAPITest(QHTestCase):
resource = "resource"
target = "test_015_release_nocapacity_target"
flags = 0
source_limits = [source, 6, 0, 1000, 1000]
source_limits = [source, 6, 0]
source_holding = [source, resource, source, flags]
target_limits = [target, 0, 5, 1000, 1000]
target_limits = [target, 0, 5]
target_holding = [target, resource, target, flags]
failed = AssertionError("Quotaholder call failed")
......@@ -472,7 +463,7 @@ class QHAPITest(QHTestCase):
name="something",
provisions=[(source, resource, -7)])
source_limits = [source, 6, 10, 1000, 1000]
source_limits = [source, 6, 10]
if qh.set_limits(set_limits=[source_limits]):
raise failed
......
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