From 7743a23e325c3286390c168024338b07c481dc55 Mon Sep 17 00:00:00 2001 From: Prodromos Gerakios Date: Mon, 23 Jul 2012 15:59:36 +0300 Subject: [PATCH] Fixed data race in caching polict store. Restored some code from timeslot computations --- .../computation/TimeslotComputations.scala | 13 +++++---- .../grnet/aquarium/policy/CachingPolicyStore.scala | 30 +++++++++----------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/main/scala/gr/grnet/aquarium/computation/TimeslotComputations.scala b/src/main/scala/gr/grnet/aquarium/computation/TimeslotComputations.scala index 85d90ae..4c2070f 100644 --- a/src/main/scala/gr/grnet/aquarium/computation/TimeslotComputations.scala +++ b/src/main/scala/gr/grnet/aquarium/computation/TimeslotComputations.scala @@ -122,7 +122,8 @@ object TimeslotComputations extends Loggable { } // 1. Round ONE: split time according to overlapping policies and agreements. - val alignedTimeslots = List(referenceTimeslot) //splitTimeslotByPoliciesAndAgreements(referenceTimeslot, policyTimeslots.toList, agreementTimeslots.toList, Just(clog)) + //val alignedTimeslots = List(referenceTimeslot) //splitTimeslotByPoliciesAndAgreements(referenceTimeslot, policyTimeslots.toList, agreementTimeslots.toList, Just(clog)) + val alignedTimeslots = splitTimeslotByPoliciesAndAgreements(referenceTimeslot, policyTimeslots.toList, agreementTimeslots.toList, Just(clog)) // 2. Round TWO: Use the aligned timeslots of Round ONE to produce even more // fine-grained timeslots according to applicable algorithms. @@ -131,9 +132,11 @@ object TimeslotComputations extends Loggable { val allChargeslots = for { alignedTimeslot <- alignedTimeslots } yield { - val policy = policyByTimeslot.valuesIterator.next()//getPolicyWithin(alignedTimeslot) + //val policy = policyByTimeslot.valuesIterator.next()//getPolicyWithin(alignedTimeslot) + val policy = getPolicyWithin(alignedTimeslot) // clog.debug("dslPolicy = %s", dslPolicy) - val userAgreement = agreementByTimeslot.valuesIterator.next()//getAgreementWithin(alignedTimeslot) + //val userAgreement = agreementByTimeslot.valuesIterator.next()//getAgreementWithin(alignedTimeslot) + val userAgreement = getAgreementWithin(alignedTimeslot) // TODO: Factor this out, just like we did with: // TODO: val alignedTimeslots = splitTimeslotByPoliciesAndAgreements @@ -238,8 +241,8 @@ object TimeslotComputations extends Loggable { effectivePriceTable } - //resolveEffective(alignedTimeslot, effectivePriceTable.priceOverrides) - immutable.SortedMap(alignedTimeslot -> effectivePriceTable.priceOverrides.head) + resolveEffective(alignedTimeslot, effectivePriceTable.priceOverrides) + //immutable.SortedMap(alignedTimeslot -> effectivePriceTable.priceOverrides.head) } private def printPriceList(p: PriceList) : Unit = { diff --git a/src/main/scala/gr/grnet/aquarium/policy/CachingPolicyStore.scala b/src/main/scala/gr/grnet/aquarium/policy/CachingPolicyStore.scala index b85de7f..4576a2f 100644 --- a/src/main/scala/gr/grnet/aquarium/policy/CachingPolicyStore.scala +++ b/src/main/scala/gr/grnet/aquarium/policy/CachingPolicyStore.scala @@ -74,18 +74,17 @@ class CachingPolicyStore(defaultPolicy: PolicyModel, policyStore: PolicyStore) e private[this] def policyAt(s:Long) : PolicyModel = new StdPolicy("", None, Timespan(s), Set(), Set(), Map()) - def loadAndSortPoliciesWithin(fromMillis: Long, toMillis: Long): immutable.SortedMap[Timeslot, Policy] = { - ensureLoaded() - - val range = Timeslot(fromMillis,toMillis) - /* ``to'' method: return the subset of all policies.from <= range.to */ - _policies.to(policyAt(range.to.getTime)).foldLeft (EmptyPolicyByTimeslotMap) { (map,p) => - if(p.validityTimespan.toTimeslot.to.getTime >= range.from.getTime) - map + ((p.validityTimespan.toTimeslot,p)) - else - map + def loadAndSortPoliciesWithin(fromMillis: Long, toMillis: Long): immutable.SortedMap[Timeslot, Policy] = + ensureLoaded { + val range = Timeslot(fromMillis,toMillis) + /* ``to'' method: return the subset of all policies.from <= range.to */ + _policies.to(policyAt(range.to.getTime)).foldLeft (EmptyPolicyByTimeslotMap) { (map,p) => + if(p.validityTimespan.toTimeslot.to.getTime >= range.from.getTime) + map + ((p.validityTimespan.toTimeslot,p)) + else + map + } } - } /** @@ -94,14 +93,13 @@ class CachingPolicyStore(defaultPolicy: PolicyModel, policyStore: PolicyStore) e * @param atMillis * @return */ - def loadValidPolicyAt(atMillis: Long): Option[Policy] = { - ensureLoaded() - + def loadValidPolicyAt(atMillis: Long): Option[Policy] = + ensureLoaded { // Take the subset of all ordered policies up to the one with less than or equal start time // and then return the last item. This should be the policy right before the given time. // TODO: optimize the creation of the fake StdPolicy - _policies.to(policyAt(atMillis)).lastOption - } + _policies.to(policyAt(atMillis)).lastOption + } /** -- 1.7.10.4