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.computation
39 import scala.collection.mutable
40 import gr.grnet.aquarium.logic.accounting.dsl.DSLResourcesMap
41 import gr.grnet.aquarium.util.ContextualLogger
42 import gr.grnet.aquarium.event.model.resource.ResourceEventModel
43 import gr.grnet.aquarium.computation.state.parts.{IgnoredFirstResourceEventsWorker, ImplicitlyIssuedResourceEventsWorker, LatestResourceEventsWorker}
46 * A helper object holding intermediate state/results during resource event processing.
48 * @author Christos KK Loverdos <loverdos@gmail.com>
50 case class UserStateWorker(
54 * This is a collection of all the latest resource events.
55 * We want these in order to correlate incoming resource events with their previous (in `occurredMillis` time)
56 * ones. Will be updated on processing the next resource event.
58 previousResourceEvents: LatestResourceEventsWorker,
61 * The implicitly issued resource events at the beginning of the billing period.
63 implicitlyIssuedStartEvents: ImplicitlyIssuedResourceEventsWorker,
66 * The resource events that were first (and unused) of their kind.
68 ignoredFirstResourceEvents: IgnoredFirstResourceEventsWorker,
69 resourcesMap: DSLResourcesMap
73 * Finds the previous resource event by checking two possible sources: a) The implicitly terminated resource
74 * events and b) the explicit previous resource events. If the event is found, it is removed from the
77 * If the event is not found, then this must be for a new resource instance.
78 * (and probably then some `zero` resource event must be implied as the previous one)
84 def findAndRemovePreviousResourceEvent(resource: String, instanceId: String): Option[ResourceEventModel] = {
85 // implicitly issued events are checked first
86 implicitlyIssuedStartEvents.findAndRemoveResourceEvent(resource, instanceId) match {
90 // explicit previous resource events are checked second
91 previousResourceEvents.findAndRemoveResourceEvent(resource, instanceId) match {
100 def updateIgnored(resourceEvent: ResourceEventModel): Unit = {
101 ignoredFirstResourceEvents.updateResourceEvent(resourceEvent)
104 def updatePrevious(resourceEvent: ResourceEventModel): Unit = {
105 previousResourceEvents.updateResourceEvent(resourceEvent)
108 def debugTheMaps(clog: ContextualLogger)(rcDebugInfo: ResourceEventModel ⇒ String): Unit = {
109 if(previousResourceEvents.size > 0) {
110 val map = previousResourceEvents.latestEventsMap.map {
111 case (k, v) => (k, rcDebugInfo(v))
113 clog.debugMap("previousResourceEvents", map, 0)
115 if(implicitlyIssuedStartEvents.size > 0) {
116 val map = implicitlyIssuedStartEvents.implicitlyIssuedEventsMap.map {
117 case (k, v) => (k, rcDebugInfo(v))
119 clog.debugMap("implicitlyTerminatedResourceEvents", map, 0)
121 if(ignoredFirstResourceEvents.size > 0) {
122 val map = ignoredFirstResourceEvents.ignoredFirstEventsMap.map {
123 case (k, v) => (k, rcDebugInfo(v))
125 clog.debugMap("ignoredFirstResourceEvents", map, 0)
130 // def allPreviousAndAllImplicitlyStarted: List[ResourceEvent] = {
131 // val buffer: FullMutableResourceTypeMap = scala.collection.mutable.Map[FullResourceType, ResourceEvent]()
133 // buffer ++= implicitlyIssuedStartEvents.implicitlyIssuedEventsMap
134 // buffer ++= previousResourceEvents.latestEventsMap
136 // buffer.valuesIterator.toList
140 * Find those events from `implicitlyIssuedStartEvents` and `previousResourceEvents` that will generate implicit
141 * end events along with those implicitly issued events. Before returning, remove the events that generated the
142 * implicit ends from the internal state of this instance.
144 * @see [[gr.grnet.aquarium.logic.accounting.dsl.DSLCostPolicy]]
146 def findAndRemoveGeneratorsOfImplicitEndEvents(
148 * The `occurredMillis` that will be recorded in the synthetic implicit OFFs.
149 * Normally, this will be the end of a billing month.
151 newOccuredMillis: Long
152 ): (List[ResourceEventModel], List[ResourceEventModel]) = {
154 val buffer = mutable.ListBuffer[(ResourceEventModel, ResourceEventModel)]()
155 val checkSet = mutable.Set[ResourceEventModel]()
157 def doItFor(map: ResourceEventModel.FullMutableResourceTypeMap): Unit = {
158 val resourceEvents = map.valuesIterator
160 resourceEvent ← resourceEvents
161 dslResource ← resourcesMap.findResource(resourceEvent.safeResource)
162 costPolicy = dslResource.costPolicy
164 if(costPolicy.supportsImplicitEvents) {
165 if(costPolicy.mustConstructImplicitEndEventFor(resourceEvent)) {
166 val implicitEnd = costPolicy.constructImplicitEndEventFor(resourceEvent, newOccuredMillis)
168 if(!checkSet.contains(resourceEvent)) {
169 checkSet.add(resourceEvent)
170 buffer append ((resourceEvent, implicitEnd))
174 map.remove((resourceEvent.safeResource, resourceEvent.safeInstanceId))
180 doItFor(previousResourceEvents.latestEventsMap) // we give priority for previous
181 doItFor(implicitlyIssuedStartEvents.implicitlyIssuedEventsMap) // ... over implicitly issued...
183 (buffer.view.map(_._1).toList, buffer.view.map(_._2).toList)
187 object UserStateWorker {
188 def fromUserState(userState: UserState, resourcesMap: DSLResourcesMap): UserStateWorker = {
191 userState.latestResourceEventsSnapshot.toMutableWorker,
192 userState.implicitlyIssuedSnapshot.toMutableWorker,
193 IgnoredFirstResourceEventsWorker.Empty,