2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
36 package gr.grnet.aquarium.user
39 import scala.collection.mutable
40 import com.ckkloverdos.maybe.{Failed, NoVal, Just, Maybe}
41 import gr.grnet.aquarium.util.{ContextualLogger, Loggable, justForSure, failedForSure}
42 import gr.grnet.aquarium.util.date.{TimeHelpers, MutableDateCalc}
43 import gr.grnet.aquarium.logic.accounting.dsl.{DSLAgreement, DSLResourcesMap}
44 import gr.grnet.aquarium.store.{StoreProvider, PolicyStore}
45 import gr.grnet.aquarium.logic.accounting.Accounting
46 import gr.grnet.aquarium.logic.accounting.algorithm.CostPolicyAlgorithmCompiler
47 import gr.grnet.aquarium.AquariumException
48 import gr.grnet.aquarium.event.{NewWalletEntry, ResourceEvent}
52 * @author Christos KK Loverdos <loverdos@gmail.com>
54 class UserStateComputations extends Loggable {
55 def createInitialUserState(userId: String,
56 userCreationMillis: Long,
59 roleNames: List[String] = List(),
60 agreementName: String = DSLAgreement.DefaultAgreementName) = {
61 val now = userCreationMillis
69 ImplicitlyIssuedResourceEventsSnapshot(List(), now),
72 LatestResourceEventsSnapshot(List(), now),
75 ActiveStateSnapshot(isActive, now),
76 CreditSnapshot(credits, now),
77 AgreementSnapshot(List(Agreement(agreementName, userCreationMillis)), now),
78 RolesSnapshot(roleNames, now),
79 OwnedResourcesSnapshot(Nil, now),
81 UserStateChangeReasonCodes.InitialCalculationCode,
82 InitialUserStateCalculation
86 def createInitialUserStateFrom(us: UserState): UserState = {
87 createInitialUserState(
89 us.userCreationMillis,
90 us.activeStateSnapshot.isActive,
91 us.creditsSnapshot.creditAmount,
92 us.rolesSnapshot.roles,
93 us.agreementsSnapshot.agreementsByTimeslot.valuesIterator.toList.last
97 def findUserStateAtEndOfBillingMonth(userId: String,
98 billingMonthInfo: BillingMonthInfo,
99 storeProvider: StoreProvider,
100 currentUserState: UserState,
101 defaultResourcesMap: DSLResourcesMap,
102 accounting: Accounting,
103 algorithmCompiler: CostPolicyAlgorithmCompiler,
104 calculationReason: UserStateChangeReason,
105 contextualLogger: Maybe[ContextualLogger] = NoVal): Maybe[UserState] = {
107 val clog = ContextualLogger.fromOther(
110 "findUserStateAtEndOfBillingMonth(%s)", billingMonthInfo)
113 def doCompute: Maybe[UserState] = {
114 doFullMonthlyBilling(
126 val userStateStore = storeProvider.userStateStore
127 val resourceEventStore = storeProvider.resourceEventStore
129 val userCreationMillis = currentUserState.userCreationMillis
130 val userCreationDateCalc = new MutableDateCalc(userCreationMillis)
131 val billingMonthStartMillis = billingMonthInfo.startMillis
132 val billingMonthStopMillis = billingMonthInfo.stopMillis
134 if(billingMonthStopMillis < userCreationMillis) {
135 // If the user did not exist for this billing month, piece of cake
136 clog.debug("User did not exist before %s", userCreationDateCalc)
138 // NOTE: Reason here will be: InitialUserStateCalculation
139 val initialUserState0 = createInitialUserStateFrom(currentUserState)
140 val initialUserStateM = userStateStore.insertUserState2(initialUserState0)
142 clog.debug("Returning ZERO state [_idM=%s] %s".format(initialUserStateM.map(_._id), initialUserStateM))
147 // Ask DB cache for the latest known user state for this billing period
148 val latestUserStateM = Maybe { userStateStore.findLatestUserStateForEndOfBillingMonth(
150 billingMonthInfo.year,
151 billingMonthInfo.month) match {
153 case Some(latestUserState) ⇒
159 latestUserStateM match {
161 // Not found, must compute
162 clog.debug("No user state found from cache, will have to (re)compute")
163 val result = doCompute
167 case failed @ Failed(e) ⇒
168 clog.warn("Failure while quering cache for user state: %s", failed)
172 case Just(latestUserState) ⇒
173 // Found a "latest" user state but need to see if it is indeed the true and one latest.
174 // For this reason, we must count the events again.
175 val latestStateOOSEventsCounter = latestUserState.billingPeriodOutOfSyncResourceEventsCounter
176 val actualOOSEventsCounterM = resourceEventStore.countOutOfSyncEventsForBillingPeriod(
178 billingMonthStartMillis,
179 billingMonthStopMillis)
181 actualOOSEventsCounterM match {
183 val errMsg = "No counter computed for out of sync events. Should at least be zero."
185 val result = Failed(new AquariumException(errMsg))
189 case failed @ Failed(_) ⇒
190 clog.warn("Failure while querying for out of sync events: %s", failed)
194 case Just(actualOOSEventsCounter) ⇒
195 val counterDiff = actualOOSEventsCounter - latestStateOOSEventsCounter
199 // NOTE: Keep the caller's calculation reason
200 Just(latestUserState.copyForChangeReason(calculationReason))
202 // We had more, so must recompute
205 "Found %s out of sync events (%s more), will have to (re)compute user state", actualOOSEventsCounter, n)
206 val result = doCompute
212 val errMsg = "Found %s out of sync events (%s less). DB must be inconsistent".format(actualOOSEventsCounter, n)
214 val result = Failed(new AquariumException(errMsg))
224 def rcDebugInfo(rcEvent: ResourceEvent) = {
225 rcEvent.toDebugString(false)
229 def processResourceEvent(startingUserState: UserState,
230 userStateWorker: UserStateWorker,
231 currentResourceEvent: ResourceEvent,
232 policyStore: PolicyStore,
233 stateChangeReason: UserStateChangeReason,
234 billingMonthInfo: BillingMonthInfo,
235 walletEntriesBuffer: mutable.Buffer[NewWalletEntry],
236 algorithmCompiler: CostPolicyAlgorithmCompiler,
237 clogM: Maybe[ContextualLogger] = NoVal): UserState = {
239 val clog = ContextualLogger.fromOther(clogM, logger, "walletEntriesForResourceEvent(%s)", currentResourceEvent.id)
241 var _workingUserState = startingUserState
243 val theResource = currentResourceEvent.safeResource
244 val theInstanceId = currentResourceEvent.safeInstanceId
245 val theValue = currentResourceEvent.value
247 val accounting = userStateWorker.accounting
248 val resourcesMap = userStateWorker.resourcesMap
250 val currentResourceEventDebugInfo = rcDebugInfo(currentResourceEvent)
251 clog.begin(currentResourceEventDebugInfo)
253 userStateWorker.debugTheMaps(clog)(rcDebugInfo)
255 // Ignore the event if it is not billable (but still record it in the "previous" stuff).
256 // But to make this decision, first we need the resource definition (and its cost policy).
257 val dslResourceOpt = resourcesMap.findResource(theResource)
258 dslResourceOpt match {
259 // We have a resource (and thus a cost policy)
260 case Some(dslResource) ⇒
261 val costPolicy = dslResource.costPolicy
262 clog.debug("Cost policy %s for %s", costPolicy, dslResource)
263 val isBillable = costPolicy.isBillableEventBasedOnValue(theValue)
265 // The resource event is not billable
266 clog.debug("Ignoring not billable event %s", currentResourceEventDebugInfo)
268 // The resource event is billable
269 // Find the previous event.
270 // This is (potentially) needed to calculate new credit amount and new resource instance amount
271 val previousResourceEventM = userStateWorker.findAndRemovePreviousResourceEvent(theResource, theInstanceId)
272 clog.debug("PreviousM %s", previousResourceEventM.map(rcDebugInfo(_)))
274 val havePreviousResourceEvent = previousResourceEventM.isJust
275 val needPreviousResourceEvent = costPolicy.needsPreviousEventForCreditAndAmountCalculation
276 if(needPreviousResourceEvent && !havePreviousResourceEvent) {
277 // This must be the first resource event of its kind, ever.
278 // TODO: We should normally check the DB to verify the claim (?)
279 clog.info("Ignoring first event of its kind %s", currentResourceEventDebugInfo)
280 userStateWorker.updateIgnored(currentResourceEvent)
282 val defaultInitialAmount = costPolicy.getResourceInstanceInitialAmount
283 val oldAmount = _workingUserState.getResourceInstanceAmount(theResource, theInstanceId, defaultInitialAmount)
284 val oldCredits = _workingUserState.creditsSnapshot.creditAmount
286 // A. Compute new resource instance accumulating amount
287 val newAmount = costPolicy.computeNewAccumulatingAmount(oldAmount, theValue)
289 clog.debug("theValue = %s, oldAmount = %s, newAmount = %s, oldCredits = %s", theValue, oldAmount, newAmount, oldCredits)
291 // B. Compute new wallet entries
292 clog.debug("agreementsSnapshot = %s", _workingUserState.agreementsSnapshot)
293 val alltimeAgreements = _workingUserState.agreementsSnapshot.agreementsByTimeslot
295 // clog.debug("Computing full chargeslots")
296 val fullChargeslotsM = accounting.computeFullChargeslots(
297 previousResourceEventM,
298 currentResourceEvent,
310 // We have the chargeslots, let's associate them with the current event
311 fullChargeslotsM match {
312 case Just((referenceTimeslot, fullChargeslots)) ⇒
313 if(fullChargeslots.length == 0) {
314 // At least one chargeslot is required.
315 throw new AquariumException("No chargeslots computed for resource event %s".format(currentResourceEvent.id))
317 clog.debugSeq("fullChargeslots", fullChargeslots, 0)
319 // C. Compute new credit amount (based on the charge slots)
320 val newCreditsDiff = fullChargeslots.map(_.computedCredits.get).sum
321 val newCredits = oldCredits - newCreditsDiff
323 if(stateChangeReason.shouldStoreCalculatedWalletEntries) {
324 val newWalletEntry = NewWalletEntry(
325 userStateWorker.userId,
329 TimeHelpers.nowMillis(),
331 billingMonthInfo.year,
332 billingMonthInfo.month,
333 if(havePreviousResourceEvent)
334 List(currentResourceEvent, justForSure(previousResourceEventM).get)
336 List(currentResourceEvent),
339 currentResourceEvent.isSynthetic
341 clog.debug("New %s", newWalletEntry)
343 walletEntriesBuffer += newWalletEntry
345 clog.debug("newCreditsDiff = %s, newCredits = %s", newCreditsDiff, newCredits)
348 _workingUserState = _workingUserState.copy(
349 creditsSnapshot = CreditSnapshot(newCredits, TimeHelpers.nowMillis()),
350 stateChangeCounter = _workingUserState.stateChangeCounter + 1,
351 totalEventsProcessedCounter = _workingUserState.totalEventsProcessedCounter + 1
355 // At least one chargeslot is required.
356 throw new AquariumException("No chargeslots computed")
358 case failed@Failed(e) ⇒
359 throw new AquariumException(e, "Error computing chargeslots")
364 // After processing, all events billable or not update the previous state
365 userStateWorker.updatePrevious(currentResourceEvent)
367 _workingUserState = _workingUserState.copy(
368 latestResourceEventsSnapshot = userStateWorker.previousResourceEvents.toImmutableSnapshot(TimeHelpers.nowMillis())
371 // We do not have a resource (and thus, no cost policy)
373 // Now, this is a matter of politics: what do we do if no policy was found?
374 clog.warn("Unknown resource for %s", currentResourceEventDebugInfo)
375 } // dslResourceOpt match
377 clog.end(currentResourceEventDebugInfo)
382 def processResourceEvents(resourceEvents: Traversable[ResourceEvent],
383 startingUserState: UserState,
384 userStateWorker: UserStateWorker,
385 policyStore: PolicyStore,
386 stateChangeReason: UserStateChangeReason,
387 billingMonthInfo: BillingMonthInfo,
388 walletEntriesBuffer: mutable.Buffer[NewWalletEntry],
389 algorithmCompiler: CostPolicyAlgorithmCompiler,
390 clogM: Maybe[ContextualLogger] = NoVal): UserState = {
392 var _workingUserState = startingUserState
394 for(currentResourceEvent <- resourceEvents) {
396 _workingUserState = processResourceEvent(
399 currentResourceEvent,
413 def doFullMonthlyBilling(userId: String,
414 billingMonthInfo: BillingMonthInfo,
415 storeProvider: StoreProvider,
416 currentUserState: UserState,
417 defaultResourcesMap: DSLResourcesMap,
418 accounting: Accounting,
419 algorithmCompiler: CostPolicyAlgorithmCompiler,
420 calculationReason: UserStateChangeReason = NoSpecificChangeReason,
421 contextualLogger: Maybe[ContextualLogger] = NoVal): Maybe[UserState] = Maybe {
424 val clog = ContextualLogger.fromOther(
427 "doFullMonthlyBilling(%s)", billingMonthInfo)
430 val clogJ = Just(clog)
432 val previousBillingMonthUserStateM = findUserStateAtEndOfBillingMonth(
434 billingMonthInfo.previousMonth,
440 calculationReason.forPreviousBillingMonth,
444 if(previousBillingMonthUserStateM.isNoVal) {
445 throw new AquariumException("Could not calculate initial user state for billing %s".format(billingMonthInfo))
447 if(previousBillingMonthUserStateM.isFailed) {
448 throw failedForSure(previousBillingMonthUserStateM).exception
451 val startingUserState = justForSure(previousBillingMonthUserStateM).get
453 val userStateStore = storeProvider.userStateStore
454 val resourceEventStore = storeProvider.resourceEventStore
455 val policyStore = storeProvider.policyStore
457 val billingMonthStartMillis = billingMonthInfo.startMillis
458 val billingMonthEndMillis = billingMonthInfo.stopMillis
460 // Keep the working (current) user state. This will get updated as we proceed with billing for the month
461 // specified in the parameters.
462 // NOTE: The calculation reason is not the one we get from the previous user state but the one our caller specifies
463 var _workingUserState = startingUserState.copyForChangeReason(calculationReason)
465 val userStateWorker = UserStateWorker.fromUserState(_workingUserState, accounting, defaultResourcesMap)
467 userStateWorker.debugTheMaps(clog)(rcDebugInfo)
469 // First, find and process the actual resource events from DB
470 val allResourceEventsForMonth = resourceEventStore.findAllRelevantResourceEventsForBillingPeriod(
472 billingMonthStartMillis,
473 billingMonthEndMillis)
475 val newWalletEntries = scala.collection.mutable.ListBuffer[NewWalletEntry]()
477 _workingUserState = processResourceEvents(
478 allResourceEventsForMonth,
489 // Second, for the remaining events which must contribute an implicit OFF, we collect those OFFs
490 // ... in order to generate an implicit ON later
491 val (specialEvents, theirImplicitEnds) = userStateWorker.
492 findAndRemoveGeneratorsOfImplicitEndEvents(billingMonthEndMillis)
493 if(specialEvents.lengthCompare(1) >= 0 || theirImplicitEnds.lengthCompare(1) >= 0) {
495 clog.debug("Process implicitly issued events")
496 clog.debugSeq("specialEvents", specialEvents, 0)
497 clog.debugSeq("theirImplicitEnds", theirImplicitEnds, 0)
500 // Now, the previous and implicitly started must be our base for the following computation, so we create an
501 // appropriate worker
502 val specialUserStateWorker = UserStateWorker(
503 userStateWorker.userId,
504 LatestResourceEventsWorker.fromList(specialEvents),
505 ImplicitlyIssuedResourceEventsWorker.Empty,
506 IgnoredFirstResourceEventsWorker.Empty,
507 userStateWorker.accounting,
508 userStateWorker.resourcesMap
511 _workingUserState = processResourceEvents(
514 specialUserStateWorker,
523 val lastUpdateTime = TimeHelpers.nowMillis()
525 _workingUserState = _workingUserState.copy(
526 implicitlyIssuedSnapshot = userStateWorker.implicitlyIssuedStartEvents.toImmutableSnapshot(lastUpdateTime),
527 latestResourceEventsSnapshot = userStateWorker.previousResourceEvents.toImmutableSnapshot(lastUpdateTime),
528 stateChangeCounter = _workingUserState.stateChangeCounter + 1,
529 parentUserStateId = startingUserState.idOpt,
530 newWalletEntries = newWalletEntries.toList
533 clog.debug("calculationReason = %s", calculationReason)
535 if(calculationReason.shouldStoreUserState) {
536 val storedUserStateM = userStateStore.insertUserState2(_workingUserState)
537 storedUserStateM match {
538 case Just(storedUserState) ⇒
539 clog.info("Saved [_id=%s] %s", storedUserState._id, storedUserState)
540 _workingUserState = storedUserState
542 clog.warn("Could not store %s", _workingUserState)
543 case failed @ Failed(e) ⇒
544 clog.error(e, "Could not store %s", _workingUserState)
548 clog.debug("RETURN %s", _workingUserState)
555 * A helper object holding intermediate state/results during resource event processing.
557 * @param previousResourceEvents
558 * This is a collection of all the latest resource events.
559 * We want these in order to correlate incoming resource events with their previous (in `occurredMillis` time)
560 * ones. Will be updated on processing the next resource event.
562 * @param implicitlyIssuedStartEvents
563 * The implicitly issued resource events at the beginning of the billing period.
565 * @param ignoredFirstResourceEvents
566 * The resource events that were first (and unused) of their kind.
568 * @author Christos KK Loverdos <loverdos@gmail.com>
570 case class UserStateWorker(userId: String,
571 previousResourceEvents: LatestResourceEventsWorker,
572 implicitlyIssuedStartEvents: ImplicitlyIssuedResourceEventsWorker,
573 ignoredFirstResourceEvents: IgnoredFirstResourceEventsWorker,
574 accounting: Accounting,
575 resourcesMap: DSLResourcesMap) {
578 * Finds the previous resource event by checking two possible sources: a) The implicitly terminated resource
579 * events and b) the explicit previous resource events. If the event is found, it is removed from the
582 * If the event is not found, then this must be for a new resource instance.
583 * (and probably then some `zero` resource event must be implied as the previous one)
589 def findAndRemovePreviousResourceEvent(resource: String, instanceId: String): Maybe[ResourceEvent] = {
590 // implicitly issued events are checked first
591 implicitlyIssuedStartEvents.findAndRemoveResourceEvent(resource, instanceId) match {
592 case just @ Just(_) ⇒
595 // explicit previous resource events are checked second
596 previousResourceEvents.findAndRemoveResourceEvent(resource, instanceId) match {
597 case just @ Just(_) ⇒
607 def updateIgnored(resourceEvent: ResourceEvent): Unit = {
608 ignoredFirstResourceEvents.updateResourceEvent(resourceEvent)
611 def updatePrevious(resourceEvent: ResourceEvent): Unit = {
612 previousResourceEvents.updateResourceEvent(resourceEvent)
615 def debugTheMaps(clog: ContextualLogger)(rcDebugInfo: ResourceEvent ⇒ String): Unit = {
616 if(previousResourceEvents.size > 0) {
617 val map = previousResourceEvents.latestEventsMap.map { case (k, v) => (k, rcDebugInfo(v)) }
618 clog.debugMap("previousResourceEvents", map, 0)
620 if(implicitlyIssuedStartEvents.size > 0) {
621 val map = implicitlyIssuedStartEvents.implicitlyIssuedEventsMap.map { case (k, v) => (k, rcDebugInfo(v)) }
622 clog.debugMap("implicitlyTerminatedResourceEvents", map, 0)
624 if(ignoredFirstResourceEvents.size > 0) {
625 val map = ignoredFirstResourceEvents.ignoredFirstEventsMap.map { case (k, v) => (k, rcDebugInfo(v)) }
626 clog.debugMap("ignoredFirstResourceEvents", map, 0)
631 // def allPreviousAndAllImplicitlyStarted: List[ResourceEvent] = {
632 // val buffer: FullMutableResourceTypeMap = scala.collection.mutable.Map[FullResourceType, ResourceEvent]()
634 // buffer ++= implicitlyIssuedStartEvents.implicitlyIssuedEventsMap
635 // buffer ++= previousResourceEvents.latestEventsMap
637 // buffer.valuesIterator.toList
641 * Find those events from `implicitlyIssuedStartEvents` and `previousResourceEvents` that will generate implicit
642 * end events along with those implicitly issued events. Before returning, remove the events that generated the
643 * implicit ends from the internal state of this instance.
645 * @see [[gr.grnet.aquarium.logic.accounting.dsl.DSLCostPolicy]]
647 def findAndRemoveGeneratorsOfImplicitEndEvents(newOccuredMillis: Long
648 ): (List[ResourceEvent], List[ResourceEvent]) = {
649 val buffer = mutable.ListBuffer[(ResourceEvent, ResourceEvent)]()
650 val checkSet = mutable.Set[ResourceEvent]()
652 def doItFor(map: ResourceEvent.FullMutableResourceTypeMap): Unit = {
653 val resourceEvents = map.valuesIterator
655 resourceEvent <- resourceEvents
656 dslResource <- resourcesMap.findResource(resourceEvent.safeResource)
657 costPolicy = dslResource.costPolicy
659 if(costPolicy.supportsImplicitEvents) {
660 if(costPolicy.mustConstructImplicitEndEventFor(resourceEvent)) {
661 val implicitEnd = costPolicy.constructImplicitEndEventFor(resourceEvent, newOccuredMillis)
663 if(!checkSet.contains(resourceEvent)) {
664 checkSet.add(resourceEvent)
665 buffer append ((resourceEvent, implicitEnd))
669 map.remove((resourceEvent.safeResource, resourceEvent.safeInstanceId))
675 doItFor(previousResourceEvents.latestEventsMap) // we give priority for previous
676 doItFor(implicitlyIssuedStartEvents.implicitlyIssuedEventsMap) // ... over implicitly issued...
678 (buffer.view.map(_._1).toList, buffer.view.map(_._2).toList)
682 object UserStateWorker {
683 def fromUserState(userState: UserState, accounting: Accounting, resourcesMap: DSLResourcesMap): UserStateWorker = {
686 userState.latestResourceEventsSnapshot.toMutableWorker,
687 userState.implicitlyIssuedSnapshot.toMutableWorker,
688 IgnoredFirstResourceEventsWorker.Empty,