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 collection.immutable
40 import gr.grnet.aquarium.computation.BillingMonthInfo
41 import gr.grnet.aquarium.converter.StdConverters
42 import gr.grnet.aquarium.logic.accounting.dsl.Timeslot
43 import gr.grnet.aquarium.message.MessageConstants
44 import gr.grnet.aquarium.message.avro.gen.{UserAgreementHistoryMsg, UserStateMsg, IMEventMsg, ResourceEventMsg, PolicyMsg}
45 import gr.grnet.aquarium.message.avro.{MessageHelpers, MessageFactory, OrderingHelpers, AvroHelpers}
46 import gr.grnet.aquarium.store._
47 import gr.grnet.aquarium.util._
48 import gr.grnet.aquarium.util.json.JsonSupport
49 import gr.grnet.aquarium.{Aquarium, AquariumException}
50 import org.apache.avro.specific.SpecificRecord
51 import org.bson.types.ObjectId
54 * Mongodb implementation of the various aquarium stores.
56 * @author Christos KK Loverdos <loverdos@gmail.com>
57 * @author Georgios Gousios <gousiosg@gmail.com>
58 * @author Prodromos Gerakios <pgerakio@grnet.gr>
61 val aquarium: Aquarium,
66 extends ResourceEventStore
72 private[store] lazy val resourceEvents = getCollection(MongoDBStore.ResourceEventCollection)
73 private[store] lazy val userStates = getCollection(MongoDBStore.UserStateCollection)
74 private[store] lazy val imEvents = getCollection(MongoDBStore.IMEventCollection)
75 private[store] lazy val policies = getCollection(MongoDBStore.PolicyCollection)
77 private[this] def doAuthenticate(db: DB) {
78 if(!db.isAuthenticated && !db.authenticate(username, password.toCharArray)) {
79 throw new AquariumException("Could not authenticate user %s".format(username))
83 private[this] def getCollection(name: String): DBCollection = {
84 val db = mongo.getDB(database)
86 db.getCollection(name)
90 def pingResourceEventStore(): Unit = synchronized {
91 getCollection(MongoDBStore.ResourceEventCollection)
92 MongoDBStore.ping(mongo)
95 def insertResourceEvent(event: ResourceEventMsg) = {
96 val mongoID = new ObjectId()
97 event.setInStoreID(mongoID.toStringMongod)
99 val dbObject = new BasicDBObjectBuilder().
100 add(MongoDBStore.JsonNames._id, mongoID).
101 add(MongoDBStore.JsonNames.payload, AvroHelpers.bytesOfSpecificRecord(event)).
102 add(MongoDBStore.JsonNames.userID, event.getUserID).
103 add(MongoDBStore.JsonNames.occurredMillis, event.getOccurredMillis).
104 add(MongoDBStore.JsonNames.receivedMillis, event.getReceivedMillis).
107 MongoDBStore.insertDBObject(dbObject, resourceEvents)
111 def findResourceEventByID(id: String): Option[ResourceEventMsg] = {
112 val dbObjectOpt = MongoDBStore.findOneByAttribute(resourceEvents, MongoDBStore.JsonNames.id, id)
114 dbObject ← dbObjectOpt
115 payload = dbObject.get(MongoDBStore.JsonNames.payload)
116 msg = AvroHelpers.specificRecordOfBytes(payload.asInstanceOf[Array[Byte]], new ResourceEventMsg)
120 def countOutOfSyncResourceEventsForBillingPeriod(userID: String, startMillis: Long, stopMillis: Long): Long = {
121 val query = new BasicDBObjectBuilder().
122 add(MongoDBStore.JsonNames.userID, userID).
123 // received within the period
124 add(MongoDBStore.JsonNames.receivedMillis, new BasicDBObject("$gte", startMillis)).
125 add(MongoDBStore.JsonNames.receivedMillis, new BasicDBObject("$lte", stopMillis)).
126 // occurred outside the period
128 val dbList = new BasicDBList()
129 dbList.add(0, new BasicDBObject(MongoDBStore.JsonNames.occurredMillis, new BasicDBObject("$lt", startMillis)))
130 dbList.add(1, new BasicDBObject(MongoDBStore.JsonNames.occurredMillis, new BasicDBObject("$gt", stopMillis)))
135 resourceEvents.count(query)
138 def foreachResourceEventOccurredInPeriod(
142 )(f: ResourceEventMsg ⇒ Unit): Long = {
144 val query = new BasicDBObjectBuilder().
145 add(MongoDBStore.JsonNames.userID, userID).
146 add(MongoDBStore.JsonNames.occurredMillis, new BasicDBObject("$gte", startMillis)).
147 add(MongoDBStore.JsonNames.occurredMillis, new BasicDBObject("$lte", stopMillis)).
150 val sorter = new BasicDBObject(MongoDBStore.JsonNames.occurredMillis, 1)
151 val cursor = resourceEvents.find(query).sort(sorter)
153 withCloseable(cursor) { cursor ⇒
154 while(cursor.hasNext) {
155 val nextDBObject = cursor.next()
156 val payload = nextDBObject.get(MongoDBStore.JsonNames.payload).asInstanceOf[Array[Byte]]
157 val nextEvent = AvroHelpers.specificRecordOfBytes(payload, new ResourceEventMsg)
166 //-ResourceEventStore
169 def findUserStateByUserID(userID: String) = {
170 val dbObjectOpt = MongoDBStore.findOneByAttribute(userStates, MongoDBStore.JsonNames.userID, userID)
172 dbObject <- dbObjectOpt
173 payload = dbObject.get(MongoDBStore.JsonNames.payload).asInstanceOf[Array[Byte]]
174 msg = AvroHelpers.specificRecordOfBytes(payload, new UserStateMsg)
180 def findLatestUserStateForFullMonthBilling(userID: String, bmi: BillingMonthInfo) = {
181 val query = new BasicDBObjectBuilder().
182 add(MongoDBStore.JsonNames.userID, userID).
183 add(MongoDBStore.JsonNames.isForFullMonth, true).
184 add(MongoDBStore.JsonNames.billingYear, bmi.year).
185 add(MongoDBStore.JsonNames.billingMonth, bmi.month).
188 // Descending order, so that the latest comes first
189 val sorter = new BasicDBObject(MongoDBStore.JsonNames.occurredMillis, -1)
191 val cursor = userStates.find(query).sort(sorter)
193 withCloseable(cursor) { cursor ⇒
194 MongoDBStore.findNextPayloadRecord(cursor, new UserStateMsg)
198 def findLatestUserState(userID: String) = {
199 val query = new BasicDBObjectBuilder().
200 add(MongoDBStore.JsonNames.userID, userID).
203 // Descending order, so that the latest comes first
204 val sorter = new BasicDBObject(MongoDBStore.JsonNames.occurredMillis, -1)
206 val cursor = userStates.find(query).sort(sorter)
208 withCloseable(cursor) { cursor ⇒
209 MongoDBStore.findNextPayloadRecord(cursor, new UserStateMsg)
214 * Stores a user state.
216 def insertUserState(event: UserStateMsg)= {
217 val mongoID = new ObjectId()
218 event.setInStoreID(mongoID.toStringMongod)
220 val dbObject = new BasicDBObjectBuilder().
221 add(MongoDBStore.JsonNames._id, mongoID).
222 add(MongoDBStore.JsonNames.payload, AvroHelpers.bytesOfSpecificRecord(event)).
223 add(MongoDBStore.JsonNames.userID, event.getUserID).
224 add(MongoDBStore.JsonNames.occurredMillis, event.getOccurredMillis).
225 add(MongoDBStore.JsonNames.isForFullMonth, event.getIsForFullMonth).
226 add(MongoDBStore.JsonNames.billingYear, event.getBillingYear).
227 add(MongoDBStore.JsonNames.billingMonth, event.getBillingMonth).
228 add(MongoDBStore.JsonNames.billingMonthDay, event.getBillingMonthDay).
231 MongoDBStore.insertDBObject(dbObject, userStates)
237 def pingIMEventStore(): Unit = {
238 getCollection(MongoDBStore.IMEventCollection)
239 MongoDBStore.ping(mongo)
242 def insertIMEvent(event: IMEventMsg) = {
243 val mongoID = new ObjectId()
244 event.setInStoreID(mongoID.toStringMongod)
246 val dbObject = new BasicDBObjectBuilder().
247 add(MongoDBStore.JsonNames._id, mongoID).
248 add(MongoDBStore.JsonNames.payload, AvroHelpers.bytesOfSpecificRecord(event)).
249 add(MongoDBStore.JsonNames.userID, event.getUserID).
250 add(MongoDBStore.JsonNames.eventType, event.getEventType().toLowerCase).
251 add(MongoDBStore.JsonNames.occurredMillis, event.getOccurredMillis).
252 add(MongoDBStore.JsonNames.receivedMillis, event.getReceivedMillis).
255 MongoDBStore.insertDBObject(dbObject, imEvents)
259 def findIMEventByID(id: String) = {
260 val dbObjectOpt = MongoDBStore.findOneByAttribute(imEvents, MongoDBStore.JsonNames.id, id)
262 dbObject ← dbObjectOpt
263 payload = dbObject.get(MongoDBStore.JsonNames.payload).asInstanceOf[Array[Byte]]
264 msg = AvroHelpers.specificRecordOfBytes(payload, new IMEventMsg)
272 * Find the `CREATE` even for the given user. Note that there must be only one such event.
274 def findCreateIMEventByUserID(userID: String) = {
275 val query = new BasicDBObjectBuilder().
276 add(MongoDBStore.JsonNames.userID, userID).
277 add(MongoDBStore.JsonNames.eventType, MessageConstants.IMEventMsg.EventTypes.create).get()
279 // Normally one such event is allowed ...
280 val cursor = imEvents.find(query).sort(new BasicDBObject(MongoDBStore.JsonNames.occurredMillis, 1))
282 val dbObjectOpt = withCloseable(cursor) { cursor ⇒
291 dbObject <- dbObjectOpt
292 payload = dbObject.get(MongoDBStore.JsonNames.payload).asInstanceOf[Array[Byte]]
293 msg = AvroHelpers.specificRecordOfBytes(payload, new IMEventMsg)
300 * Scans events for the given user, sorted by `occurredMillis` in ascending order and runs them through
301 * the given function `f`.
303 * Any exception is propagated to the caller. The underlying DB resources are properly disposed in any case.
305 def foreachIMEventInOccurrenceOrder(userID: String)(f: (IMEventMsg) ⇒ Boolean) = {
306 val query = new BasicDBObject(MongoDBStore.JsonNames.userID, userID)
307 val cursor = imEvents.find(query).sort(new BasicDBObject(MongoDBStore.JsonNames.occurredMillis, 1))
309 var _shouldContinue = true
310 withCloseable(cursor) { cursor ⇒
311 while(_shouldContinue && cursor.hasNext) {
312 val dbObject = cursor.next()
313 val payload = dbObject.get(MongoDBStore.JsonNames.payload).asInstanceOf[Array[Byte]]
314 val msg = AvroHelpers.specificRecordOfBytes(payload, new IMEventMsg)
316 _shouldContinue = f(msg)
325 def foreachPolicy[U](f: PolicyMsg ⇒ U) {
326 val cursor = policies.find()
327 withCloseable(cursor) { cursor ⇒
328 while(cursor.hasNext) {
329 val dbObject = cursor.next()
330 val payload = dbObject.get(MongoDBStore.JsonNames.payload).asInstanceOf[Array[Byte]]
331 val policy = AvroHelpers.specificRecordOfBytes(payload, new PolicyMsg)
337 def insertPolicy(policy: PolicyMsg): PolicyMsg = {
338 val mongoID = new ObjectId()
339 policy.setInStoreID(mongoID.toStringMongod)
340 val dbObject = new BasicDBObjectBuilder().
341 add(MongoDBStore.JsonNames._id, mongoID).
342 add(MongoDBStore.JsonNames.validFromMillis, policy.getValidFromMillis).
343 add(MongoDBStore.JsonNames.validToMillis, policy.getValidToMillis).
344 add(MongoDBStore.JsonNames.payload, AvroHelpers.bytesOfSpecificRecord(policy)).
347 MongoDBStore.insertDBObject(dbObject, policies)
351 def loadPolicyAt(atMillis: Long): Option[PolicyMsg] = {
353 var _policies = immutable.TreeSet[PolicyMsg]()(OrderingHelpers.DefaultPolicyMsgOrdering)
354 foreachPolicy(_policies += _)
355 _policies.to(MessageFactory.newDummyPolicyMsgAt(atMillis)).lastOption
358 def loadSortedPoliciesWithin(fromMillis: Long, toMillis: Long): immutable.SortedMap[Timeslot, PolicyMsg] = {
360 var _policies = immutable.TreeSet[PolicyMsg]()(OrderingHelpers.DefaultPolicyMsgOrdering)
361 foreachPolicy(_policies += _)
363 immutable.SortedMap(_policies.
364 from(MessageFactory.newDummyPolicyMsgAt(fromMillis)).
365 to(MessageFactory.newDummyPolicyMsgAt(toMillis)).toSeq.
366 map(p ⇒ (Timeslot(p.getValidFromMillis, p.getValidToMillis), p)): _*
372 object MongoDBStore {
373 final val JsonNames = gr.grnet.aquarium.util.json.JsonNames
375 final val ResourceEventCollection = "resevents"
377 final val UserStateCollection = "userstates"
379 final val UserAgreementHistoryCollection = "useragreementhistory"
381 final val IMEventCollection = "imevents"
383 final val PolicyCollection = "policies"
385 def firstResultIfExists[A](cursor: DBCursor, f: DBObject ⇒ A): Option[A] = {
386 withCloseable(cursor) { cursor ⇒
388 Some(f(cursor.next()))
395 def ping(mongo: Mongo): Unit = synchronized {
396 // This requires a network roundtrip
400 def findOneByAttribute(
401 collection: DBCollection,
402 attributeName: String,
403 attributeValue: String,
404 sortByOpt: Option[DBObject] = None
405 ): Option[DBObject] = {
406 val query = new BasicDBObject(attributeName, attributeValue)
407 val cursor = sortByOpt match {
408 case None ⇒ collection find query
409 case Some(sortBy) ⇒ collection find query sort sortBy
411 withCloseable(cursor) { cursor ⇒
412 if(cursor.hasNext) Some(cursor.next()) else None
416 def insertDBObject(dbObj: DBObject, collection: DBCollection) {
417 collection.insert(dbObj, WriteConcern.JOURNAL_SAFE)
420 def findNextPayloadRecord[R <: SpecificRecord](cursor: DBCursor, fresh: R): Option[R] = {
422 dbObject <- if(cursor.hasNext) Some(cursor.next()) else None
423 payload = dbObject.get(MongoDBStore.JsonNames.payload).asInstanceOf[Array[Byte]]
424 msg = AvroHelpers.specificRecordOfBytes(payload, fresh)
430 def jsonSupportToDBObject(jsonSupport: JsonSupport) = {
431 StdConverters.AllConverters.convertEx[DBObject](jsonSupport)