Fixed data race in caching polict store. Restored some code from timeslot computations
authorProdromos Gerakios <pgerakios@grnet.gr>
Mon, 23 Jul 2012 12:59:36 +0000 (15:59 +0300)
committerProdromos Gerakios <pgerakios@grnet.gr>
Mon, 23 Jul 2012 12:59:36 +0000 (15:59 +0300)
src/main/scala/gr/grnet/aquarium/computation/TimeslotComputations.scala
src/main/scala/gr/grnet/aquarium/policy/CachingPolicyStore.scala

index 85d90ae..4c2070f 100644 (file)
@@ -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 = {
index b85de7f..4576a2f 100644 (file)
@@ -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
+    }
 
 
   /**