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.store.mongodb
38 import com.mongodb.util.JSON
39 import gr.grnet.aquarium.computation.UserState.{JsonNames ⇒ UserStateJsonNames}
40 import gr.grnet.aquarium.util.json.JsonSupport
41 import collection.mutable.ListBuffer
42 import gr.grnet.aquarium.event.model.im.IMEventModel
43 import gr.grnet.aquarium.event.model.im.IMEventModel.{Names ⇒ IMEventNames}
44 import gr.grnet.aquarium.event.model.resource.ResourceEventModel
45 import gr.grnet.aquarium.event.model.resource.ResourceEventModel.{Names ⇒ ResourceEventNames}
46 import gr.grnet.aquarium.store._
47 import gr.grnet.aquarium.event.model.WalletEntry.{JsonNames ⇒ WalletJsonNames}
48 import gr.grnet.aquarium.event.model.PolicyEntry.{JsonNames ⇒ PolicyJsonNames}
50 import gr.grnet.aquarium.logic.accounting.Policy
52 import org.bson.types.ObjectId
53 import com.ckkloverdos.maybe.Maybe
54 import gr.grnet.aquarium.util._
55 import gr.grnet.aquarium.converter.Conversions
56 import gr.grnet.aquarium.computation.UserState
57 import gr.grnet.aquarium.event.model.{ExternalEventModel, WalletEntry, PolicyEntry}
60 * Mongodb implementation of the various aquarium stores.
62 * @author Christos KK Loverdos <loverdos@gmail.com>
63 * @author Georgios Gousios <gousiosg@gmail.com>
70 extends ResourceEventStore
77 override type IMEvent = MongoDBIMEvent
78 override type ResourceEvent = MongoDBResourceEvent
80 private[store] lazy val resourceEvents = getCollection(MongoDBStore.RESOURCE_EVENTS_COLLECTION)
81 private[store] lazy val userStates = getCollection(MongoDBStore.USER_STATES_COLLECTION)
82 private[store] lazy val imEvents = getCollection(MongoDBStore.IM_EVENTS_COLLECTION)
83 private[store] lazy val unparsedIMEvents = getCollection(MongoDBStore.UNPARSED_IM_EVENTS_COLLECTION)
84 private[store] lazy val walletEntries = getCollection(MongoDBStore.WALLET_ENTRIES_COLLECTION)
85 private[store] lazy val policyEntries = getCollection(MongoDBStore.POLICY_ENTRIES_COLLECTION)
87 private[this] def getCollection(name: String): DBCollection = {
88 val db = mongo.getDB(database)
89 //logger.debug("Authenticating to mongo")
90 if(!db.isAuthenticated && !db.authenticate(username, password.toCharArray)) {
91 throw new StoreException("Could not authenticate user %s".format(username))
93 db.getCollection(name)
96 private[this] def _sortByTimestampAsc[A <: ExternalEventModel](one: A, two: A): Boolean = {
97 if (one.occurredMillis > two.occurredMillis) false
98 else if (one.occurredMillis < two.occurredMillis) true
102 //+ResourceEventStore
103 def createResourceEventFromOther(event: ResourceEventModel): ResourceEvent = {
104 MongoDBResourceEvent.fromOther(event, null)
107 def pingResourceEventStore(): Unit = {
108 MongoDBStore.ping(mongo)
111 def insertResourceEvent(event: ResourceEventModel) = {
112 val localEvent = MongoDBResourceEvent.fromOther(event, new ObjectId().toStringMongod)
113 MongoDBStore.insertObject(localEvent, resourceEvents, MongoDBStore.jsonSupportToDBObject)
117 def findResourceEventById(id: String): Option[ResourceEvent] = {
118 MongoDBStore.findBy(ResourceEventNames.id, id, resourceEvents, MongoDBResourceEvent.fromDBObject)
121 def findResourceEventsByUserId(userId: String)
122 (sortWith: Option[(ResourceEvent, ResourceEvent) => Boolean]): List[ResourceEvent] = {
123 val query = new BasicDBObject(ResourceEventNames.userID, userId)
125 MongoDBStore.runQuery(query, resourceEvents)(MongoDBResourceEvent.fromDBObject)(sortWith)
128 def findResourceEventsByUserIdAfterTimestamp(userId: String, timestamp: Long): List[ResourceEvent] = {
129 val query = new BasicDBObject()
130 query.put(ResourceEventNames.userID, userId)
131 query.put(ResourceEventNames.occurredMillis, new BasicDBObject("$gt", timestamp))
133 val sort = new BasicDBObject(ResourceEventNames.occurredMillis, 1)
135 val cursor = resourceEvents.find(query).sort(sort)
138 val buffer = new scala.collection.mutable.ListBuffer[ResourceEvent]
139 while(cursor.hasNext) {
140 buffer += MongoDBResourceEvent.fromDBObject(cursor.next())
142 buffer.toList.sortWith(_sortByTimestampAsc)
148 def findResourceEventHistory(userId: String, resName: String,
149 instid: Option[String], upTo: Long) : List[ResourceEvent] = {
150 val query = new BasicDBObject()
151 query.put(ResourceEventNames.userID, userId)
152 query.put(ResourceEventNames.occurredMillis, new BasicDBObject("$lt", upTo))
153 query.put(ResourceEventNames.resource, resName)
157 Policy.policy.findResource(resName) match {
158 case Some(y) => query.put(ResourceEventNames.details,
159 new BasicDBObject(y.descriminatorField, instid.get))
165 val sort = new BasicDBObject(ResourceEventNames.occurredMillis, 1)
166 val cursor = resourceEvents.find(query).sort(sort)
169 val buffer = new scala.collection.mutable.ListBuffer[ResourceEvent]
170 while(cursor.hasNext) {
171 buffer += MongoDBResourceEvent.fromDBObject(cursor.next())
173 buffer.toList.sortWith(_sortByTimestampAsc)
179 def findResourceEventsForReceivedPeriod(userId: String, startTimeMillis: Long, stopTimeMillis: Long): List[ResourceEvent] = {
180 val query = new BasicDBObject()
181 query.put(ResourceEventNames.userID, userId)
182 query.put(ResourceEventNames.receivedMillis, new BasicDBObject("$gte", startTimeMillis))
183 query.put(ResourceEventNames.receivedMillis, new BasicDBObject("$lte", stopTimeMillis))
185 // Sort them by increasing order for occurred time
186 val orderBy = new BasicDBObject(ResourceEventNames.occurredMillis, 1)
188 MongoDBStore.runQuery[ResourceEvent](query, resourceEvents, orderBy)(MongoDBResourceEvent.fromDBObject)(None)
191 def countOutOfSyncEventsForBillingPeriod(userId: String, startMillis: Long, stopMillis: Long): Long = {
196 def findAllRelevantResourceEventsForBillingPeriod(userId: String,
198 stopMillis: Long): List[ResourceEvent] = {
202 //-ResourceEventStore
205 def insertUserState(userState: UserState) = {
206 MongoDBStore.insertUserState(userState, userStates, MongoDBStore.jsonSupportToDBObject)
209 def findUserStateByUserID(userID: String): Option[UserState] = {
210 val query = new BasicDBObject(UserStateJsonNames.userID, userID)
211 val cursor = userStates find query
213 withCloseable(cursor) { cursor ⇒
215 Some(MongoDBStore.dbObjectToUserState(cursor.next()))
222 def findLatestUserStateByUserID(userID: String) = {
227 def findLatestUserStateForEndOfBillingMonth(userId: String,
228 yearOfBillingMonth: Int,
229 billingMonth: Int): Option[UserState] = {
230 None // FIXME: implement
233 def deleteUserState(userId: String) = {
234 val query = new BasicDBObject(UserStateJsonNames.userID, userId)
235 userStates.findAndRemove(query)
240 def storeWalletEntry(entry: WalletEntry): Maybe[RecordID] = {
242 MongoDBStore.storeAny[WalletEntry](
247 MongoDBStore.jsonSupportToDBObject)
251 def findWalletEntryById(id: String): Maybe[WalletEntry] = {
252 MongoDBStore.findBy(WalletJsonNames.id, id, walletEntries, MongoDBStore.dbObjectToWalletEntry): Maybe[WalletEntry]
255 def findUserWalletEntries(userId: String) = {
257 findUserWalletEntriesFromTo(userId, new Date(0), new Date(Int.MaxValue))
260 def findUserWalletEntriesFromTo(userId: String, from: Date, to: Date) : List[WalletEntry] = {
261 val q = new BasicDBObject()
262 // TODO: Is this the correct way for an AND query?
263 q.put(WalletJsonNames.occurredMillis, new BasicDBObject("$gt", from.getTime))
264 q.put(WalletJsonNames.occurredMillis, new BasicDBObject("$lt", to.getTime))
265 q.put(WalletJsonNames.userId, userId)
267 MongoDBStore.runQuery[WalletEntry](q, walletEntries)(MongoDBStore.dbObjectToWalletEntry)(Some(_sortByTimestampAsc))
270 def findWalletEntriesAfter(userId: String, from: Date) : List[WalletEntry] = {
271 val q = new BasicDBObject()
272 q.put(WalletJsonNames.occurredMillis, new BasicDBObject("$gt", from.getTime))
273 q.put(WalletJsonNames.userId, userId)
275 MongoDBStore.runQuery[WalletEntry](q, walletEntries)(MongoDBStore.dbObjectToWalletEntry)(Some(_sortByTimestampAsc))
278 def findLatestUserWalletEntries(userId: String) = {
280 val orderBy = new BasicDBObject(WalletJsonNames.occurredMillis, -1) // -1 is descending order
281 val cursor = walletEntries.find().sort(orderBy)
284 val buffer = new scala.collection.mutable.ListBuffer[WalletEntry]
286 val walletEntry = MongoDBStore.dbObjectToWalletEntry(cursor.next())
287 buffer += walletEntry
289 var _previousOccurredMillis = walletEntry.occurredMillis
292 while(cursor.hasNext && _ok) {
293 val walletEntry = MongoDBStore.dbObjectToWalletEntry(cursor.next())
294 var currentOccurredMillis = walletEntry.occurredMillis
295 _ok = currentOccurredMillis == _previousOccurredMillis
298 buffer += walletEntry
312 def findPreviousEntry(userId: String, resource: String,
314 finalized: Option[Boolean]): List[WalletEntry] = {
315 val q = new BasicDBObject()
316 q.put(WalletJsonNames.userId, userId)
317 q.put(WalletJsonNames.resource, resource)
318 q.put(WalletJsonNames.instanceId, instanceId)
320 case Some(x) => q.put(WalletJsonNames.finalized, x)
324 MongoDBStore.runQuery[WalletEntry](q, walletEntries)(MongoDBStore.dbObjectToWalletEntry)(Some(_sortByTimestampAsc))
329 def createIMEventFromJson(json: String) = {
330 MongoDBStore.createIMEventFromJson(json)
333 def createIMEventFromOther(event: IMEventModel) = {
334 MongoDBStore.createIMEventFromOther(event)
337 def pingIMEventStore(): Unit = {
338 MongoDBStore.ping(mongo)
341 def insertIMEvent(event: IMEventModel): IMEvent = {
342 val localEvent = MongoDBIMEvent.fromOther(event, new ObjectId().toStringMongod)
343 MongoDBStore.insertObject(localEvent, imEvents, MongoDBStore.jsonSupportToDBObject)
347 def findIMEventById(id: String): Option[IMEvent] = {
348 MongoDBStore.findBy(IMEventNames.id, id, imEvents, MongoDBIMEvent.fromDBObject)
353 def loadPolicyEntriesAfter(after: Long): List[PolicyEntry] = {
354 val query = new BasicDBObject(PolicyEntry.JsonNames.validFrom,
355 new BasicDBObject("$gt", after))
356 MongoDBStore.runQuery(query, policyEntries)(MongoDBStore.dbObjectToPolicyEntry)(Some(_sortByTimestampAsc))
359 def storePolicyEntry(policy: PolicyEntry): Maybe[RecordID] = MongoDBStore.storePolicyEntry(policy, policyEntries)
362 def updatePolicyEntry(policy: PolicyEntry) = {
364 val query = new BasicDBObject(PolicyEntry.JsonNames.id, policy.id)
365 val policyObject = MongoDBStore.jsonSupportToDBObject(policy)
366 policyEntries.update(query, policyObject, true, false)
369 def findPolicyEntry(id: String) = {
370 MongoDBStore.findBy(PolicyJsonNames.id, id, policyEntries, MongoDBStore.dbObjectToPolicyEntry)
376 object MongoDBStore {
378 final val _id = "_id"
382 * Collection holding the [[gr.grnet.aquarium.event.model.resource.ResourceEventModel]]s.
384 * Resource events are coming from all systems handling billable resources.
386 final val RESOURCE_EVENTS_COLLECTION = "resevents"
389 * Collection holding the snapshots of [[gr.grnet.aquarium.computation.UserState]].
391 * [[gr.grnet.aquarium.computation.UserState]] is held internally within [[gr.grnet.aquarium.actor.service.user.UserActor]]s.
393 final val USER_STATES_COLLECTION = "userstates"
396 * Collection holding [[gr.grnet.aquarium.event.model.im.IMEventModel]]s.
398 * User events are coming from the IM module (external).
400 final val IM_EVENTS_COLLECTION = "imevents"
403 * Collection holding [[gr.grnet.aquarium.event.model.im.IMEventModel]]s that could not be parsed to normal objects.
405 * We of course assume at least a valid JSON representation.
407 * User events are coming from the IM module (external).
409 final val UNPARSED_IM_EVENTS_COLLECTION = "unparsed_imevents"
412 * Collection holding [[gr.grnet.aquarium.event.model.WalletEntry]].
414 * Wallet entries are generated internally in Aquarium.
416 final val WALLET_ENTRIES_COLLECTION = "wallets"
419 * Collection holding [[gr.grnet.aquarium.logic.accounting.dsl.DSLPolicy]].
421 // final val POLICIES_COLLECTION = "policies"
424 * Collection holding [[gr.grnet.aquarium.event.model.PolicyEntry]].
426 final val POLICY_ENTRIES_COLLECTION = "policyEntries"
428 def dbObjectToUserState(dbObj: DBObject): UserState = {
429 UserState.fromJson(JSON.serialize(dbObj))
432 def dbObjectToWalletEntry(dbObj: DBObject): WalletEntry = {
433 WalletEntry.fromJson(JSON.serialize(dbObj))
436 def dbObjectToPolicyEntry(dbObj: DBObject): PolicyEntry = {
437 PolicyEntry.fromJson(JSON.serialize(dbObj))
440 def ping(mongo: Mongo): Unit = {
441 // This requires a network roundtrip
445 def findBy[A >: Null <: AnyRef](name: String,
447 collection: DBCollection,
448 deserializer: (DBObject) => A) : Option[A] = {
449 val query = new BasicDBObject(name, value)
450 val cursor = collection find query
452 withCloseable(cursor) { cursor ⇒
454 Some(deserializer apply cursor.next)
460 def runQuery[A <: ExternalEventModel](query: DBObject, collection: DBCollection, orderBy: DBObject = null)
461 (deserializer: (DBObject) => A)
462 (sortWith: Option[(A, A) => Boolean]): List[A] = {
463 val cursor0 = collection find query
464 val cursor = if(orderBy ne null) {
468 } // I really know that docs say that it is the same cursor.
470 if(!cursor.hasNext) {
474 val buff = new ListBuffer[A]()
476 while(cursor.hasNext) {
477 buff += deserializer apply cursor.next
483 case Some(sorter) => buff.toList.sortWith(sorter)
484 case None => buff.toList
489 def storeUserState(userState: UserState, collection: DBCollection) = {
490 storeAny[UserState](userState, collection, ResourceEventNames.userID, _.userID, MongoDBStore.jsonSupportToDBObject)
493 def storePolicyEntry(policyEntry: PolicyEntry, collection: DBCollection): Maybe[RecordID] = {
494 Maybe(storeAny[PolicyEntry](policyEntry, collection, PolicyJsonNames.id, _.id, MongoDBStore.jsonSupportToDBObject))
497 def storeAny[A](any: A,
498 collection: DBCollection,
500 idValueProvider: (A) => String,
501 serializer: (A) => DBObject) : RecordID = {
503 val dbObject = serializer apply any
504 val _id = new ObjectId()
505 dbObject.put("_id", _id)
506 val writeResult = collection.insert(dbObject, WriteConcern.JOURNAL_SAFE)
507 writeResult.getLastError().throwOnError()
509 RecordID(dbObject.get("_id").toString)
512 // FIXME: consolidate
513 def insertUserState[A <: UserState](obj: A, collection: DBCollection, serializer: A ⇒ DBObject) = {
514 val dbObject = serializer apply obj
515 val objectId = obj._id match {
517 val _id = new ObjectId()
518 dbObject.put("_id", _id)
525 dbObject.put(JsonNames._id, objectId)
527 collection.insert(dbObject, WriteConcern.JOURNAL_SAFE)
532 def insertObject[A <: MongoDBEventModel](obj: A, collection: DBCollection, serializer: (A) => DBObject) : Unit = {
533 val dbObject = serializer apply obj
534 val objectId = obj._id match {
536 val _id = new ObjectId()
537 dbObject.put("_id", _id)
544 dbObject.put(JsonNames._id, objectId)
546 collection.insert(dbObject, WriteConcern.JOURNAL_SAFE)
549 def jsonSupportToDBObject(jsonSupport: JsonSupport) = {
550 Conversions.jsonSupportToDBObject(jsonSupport)
553 final def isLocalIMEvent(event: IMEventModel) = event match {
554 case _: MongoDBIMEvent ⇒ true
558 final def createIMEventFromJson(json: String) = {
559 MongoDBIMEvent.fromJsonString(json)
562 final def createIMEventFromOther(event: IMEventModel) = {
563 MongoDBIMEvent.fromOther(event, null)
566 final def createIMEventFromJsonBytes(jsonBytes: Array[Byte]) = {
567 MongoDBIMEvent.fromJsonBytes(jsonBytes)