style95 commented on a change in pull request #4963: URL: https://github.com/apache/openwhisk/pull/4963#discussion_r614431285
########## File path: ansible/roles/mongodb/tasks/clean.yml ########## @@ -0,0 +1,29 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +--- +# Remove MongoDB server + +- name: remove MongoDB + docker_container: + name: mongodb + state: absent + keep_volumes: False + +- name: remove MongoDB data volume Review comment: I suppose even if a user wants to clean up his container, he may still want to keep his data. I`d rather keep volumes or at least make it configurable to keep volumes when cleaning up the container. ########## File path: ansible/group_vars/all ########## @@ -322,6 +327,12 @@ elasticsearch_connect_string: "{% set ret = [] %}\ {{ ret.append( hostvars[host].ansible_host + ':' + ((db.elasticsearch.port+loop.index-1)|string) ) }}\ {% endfor %}\ {{ ret | join(',') }}" +mongodb: + version: 4.4.0 + commonEnv: + CONFIG_whisk_mongodb_uri: "{{ db.mongodb.connect_string }}" + CONFIG_whisk_mongodb_database: "{{ db.mongodb.database }}" + CONFIG_whisk_spi_ArtifactStoreProvider: "org.apache.openwhisk.core.database.mongodb.MongoDBArtifactStoreProvider" Review comment: Do we need to follow the convention just like the others? https://github.com/apache/openwhisk/blob/68120f2170dc9f9b53361ab0cb51c4e9458dbe29/ansible/group_vars/all#L224 ########## File path: common/scala/src/main/scala/org/apache/openwhisk/core/database/mongodb/MongoDBArtifactStore.scala ########## @@ -0,0 +1,661 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openwhisk.core.database.mongodb + +import java.security.MessageDigest + +import akka.actor.ActorSystem +import akka.event.Logging.ErrorLevel +import akka.http.scaladsl.model._ +import akka.stream.ActorMaterializer +import akka.stream.scaladsl._ +import akka.util.ByteString +import com.mongodb.client.gridfs.model.GridFSUploadOptions +import org.apache.openwhisk.common.{Logging, LoggingMarkers, TransactionId} +import org.apache.openwhisk.core.database._ +import org.apache.openwhisk.core.database.StoreUtils._ +import org.apache.openwhisk.core.entity.Attachments.Attached +import org.apache.openwhisk.core.entity.{DocId, DocInfo, DocRevision, DocumentReader, UUID} +import org.apache.openwhisk.http.Messages +import org.bson.json.{JsonMode, JsonWriterSettings} +import org.mongodb.scala.bson.BsonString +import org.mongodb.scala.bson.collection.immutable.Document +import org.mongodb.scala.gridfs.{GridFSBucket, GridFSFile, MongoGridFSException} +import org.mongodb.scala.model._ +import org.mongodb.scala.{MongoClient, MongoCollection, MongoException} +import spray.json._ + +import scala.concurrent.Future +import scala.util.Try + +object MongoDBArtifactStore { + val _computed = "_computed" +} + +/** + * Basic client to put and delete artifacts in a data store. + * + * @param client the mongodb client to access database + * @param dbName the name of the database to operate on + * @param collName the name of the collection to operate on + * @param documentHandler helper class help to simulate the designDoc of CouchDB + * @param viewMapper helper class help to simulate the designDoc of CouchDB + */ +class MongoDBArtifactStore[DocumentAbstraction <: DocumentSerializer](client: MongoClient, + dbName: String, + collName: String, + documentHandler: DocumentHandler, + viewMapper: MongoDBViewMapper, + val inliningConfig: InliningConfig, + val attachmentStore: Option[AttachmentStore])( + implicit system: ActorSystem, + val logging: Logging, + jsonFormat: RootJsonFormat[DocumentAbstraction], + val materializer: ActorMaterializer, + docReader: DocumentReader) + extends ArtifactStore[DocumentAbstraction] + with DocumentProvider + with DefaultJsonProtocol + with AttachmentSupport[DocumentAbstraction] { + + import MongoDBArtifactStore._ + + protected[core] implicit val executionContext = system.dispatcher + + private val mongodbScheme = "mongodb" + val attachmentScheme: String = attachmentStore.map(_.scheme).getOrElse(mongodbScheme) + + private val database = client.getDatabase(dbName) + private val collection = getCollectionAndCreateIndexes + private val gridFSBucket = GridFSBucket(database, collName) + + private val jsonWriteSettings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build + + // MongoDB doesn't support using `$` as the first char of field name, so below two fields needs to be encoded first + private val fieldsNeedEncode = Seq("annotations", "parameters") + + override protected[database] def put(d: DocumentAbstraction)(implicit transid: TransactionId): Future[DocInfo] = { + val asJson = d.toDocumentRecord + + val id: String = asJson.fields.getOrElse("_id", JsString.empty).convertTo[String].trim + require(!id.isEmpty, "document id must be defined") + + val (old_rev, rev) = revisionCalculate(asJson) + val docinfoStr = s"id: $id, rev: $rev" + val start = + transid.started(this, LoggingMarkers.DATABASE_SAVE, s"[PUT] '$collName' saving document: '$docinfoStr'") + + val encodedData = encodeFields(fieldsNeedEncode, asJson) + + val data = JsObject( + encodedData.fields + (_computed -> documentHandler.computedFields(asJson)) + ("_rev" -> rev.toJson)) + + val filters = + if (rev.startsWith("1-")) { + // for new document, we should get no matched document and insert new one + // if there is a matched document, that one with no _rev filed will be replaced + // if there is a document with the same id but has an _rev field, will return en E11000(conflict) error + Filters.and(Filters.eq("_id", id), Filters.not(Filters.exists("_rev"))) + } else { + // for old document, we should find a matched document and replace it + // if no matched document find and try to insert new document, mongodb will return an E11000 error + Filters.and(Filters.eq("_id", id), Filters.eq("_rev", old_rev)) + } + + val f = + collection + .findOneAndReplace( + filters, + Document(data.compactPrint), + FindOneAndReplaceOptions().upsert(true).returnDocument(ReturnDocument.AFTER)) + .toFuture() + .map { doc => + transid.finished(this, start, s"[PUT] '$collName' completed document: '$docinfoStr', document: '$doc'") + DocInfo(DocId(id), DocRevision(rev)) + } + .recover { + case t: MongoException if t.getCode == 11000 => Review comment: Would be great to add some comment to describe what 11000(conflict) stands for or suggesting to use a constant for this. ########## File path: common/scala/src/main/scala/org/apache/openwhisk/core/database/mongodb/MongoDBArtifactStore.scala ########## @@ -0,0 +1,661 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openwhisk.core.database.mongodb + +import java.security.MessageDigest + +import akka.actor.ActorSystem +import akka.event.Logging.ErrorLevel +import akka.http.scaladsl.model._ +import akka.stream.ActorMaterializer +import akka.stream.scaladsl._ +import akka.util.ByteString +import com.mongodb.client.gridfs.model.GridFSUploadOptions +import org.apache.openwhisk.common.{Logging, LoggingMarkers, TransactionId} +import org.apache.openwhisk.core.database._ +import org.apache.openwhisk.core.database.StoreUtils._ +import org.apache.openwhisk.core.entity.Attachments.Attached +import org.apache.openwhisk.core.entity.{DocId, DocInfo, DocRevision, DocumentReader, UUID} +import org.apache.openwhisk.http.Messages +import org.bson.json.{JsonMode, JsonWriterSettings} +import org.mongodb.scala.bson.BsonString +import org.mongodb.scala.bson.collection.immutable.Document +import org.mongodb.scala.gridfs.{GridFSBucket, GridFSFile, MongoGridFSException} +import org.mongodb.scala.model._ +import org.mongodb.scala.{MongoClient, MongoCollection, MongoException} +import spray.json._ + +import scala.concurrent.Future +import scala.util.Try + +object MongoDBArtifactStore { + val _computed = "_computed" +} + +/** + * Basic client to put and delete artifacts in a data store. + * + * @param client the mongodb client to access database + * @param dbName the name of the database to operate on + * @param collName the name of the collection to operate on + * @param documentHandler helper class help to simulate the designDoc of CouchDB + * @param viewMapper helper class help to simulate the designDoc of CouchDB + */ +class MongoDBArtifactStore[DocumentAbstraction <: DocumentSerializer](client: MongoClient, + dbName: String, + collName: String, + documentHandler: DocumentHandler, + viewMapper: MongoDBViewMapper, + val inliningConfig: InliningConfig, + val attachmentStore: Option[AttachmentStore])( + implicit system: ActorSystem, + val logging: Logging, + jsonFormat: RootJsonFormat[DocumentAbstraction], + val materializer: ActorMaterializer, + docReader: DocumentReader) + extends ArtifactStore[DocumentAbstraction] + with DocumentProvider + with DefaultJsonProtocol + with AttachmentSupport[DocumentAbstraction] { + + import MongoDBArtifactStore._ + + protected[core] implicit val executionContext = system.dispatcher + + private val mongodbScheme = "mongodb" + val attachmentScheme: String = attachmentStore.map(_.scheme).getOrElse(mongodbScheme) + + private val database = client.getDatabase(dbName) + private val collection = getCollectionAndCreateIndexes + private val gridFSBucket = GridFSBucket(database, collName) + + private val jsonWriteSettings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build + + // MongoDB doesn't support using `$` as the first char of field name, so below two fields needs to be encoded first + private val fieldsNeedEncode = Seq("annotations", "parameters") + + override protected[database] def put(d: DocumentAbstraction)(implicit transid: TransactionId): Future[DocInfo] = { + val asJson = d.toDocumentRecord + + val id: String = asJson.fields.getOrElse("_id", JsString.empty).convertTo[String].trim + require(!id.isEmpty, "document id must be defined") + + val (old_rev, rev) = revisionCalculate(asJson) + val docinfoStr = s"id: $id, rev: $rev" + val start = + transid.started(this, LoggingMarkers.DATABASE_SAVE, s"[PUT] '$collName' saving document: '$docinfoStr'") + + val encodedData = encodeFields(fieldsNeedEncode, asJson) + + val data = JsObject( + encodedData.fields + (_computed -> documentHandler.computedFields(asJson)) + ("_rev" -> rev.toJson)) + + val filters = + if (rev.startsWith("1-")) { + // for new document, we should get no matched document and insert new one + // if there is a matched document, that one with no _rev filed will be replaced + // if there is a document with the same id but has an _rev field, will return en E11000(conflict) error + Filters.and(Filters.eq("_id", id), Filters.not(Filters.exists("_rev"))) + } else { + // for old document, we should find a matched document and replace it + // if no matched document find and try to insert new document, mongodb will return an E11000 error + Filters.and(Filters.eq("_id", id), Filters.eq("_rev", old_rev)) + } + + val f = + collection + .findOneAndReplace( + filters, + Document(data.compactPrint), + FindOneAndReplaceOptions().upsert(true).returnDocument(ReturnDocument.AFTER)) + .toFuture() + .map { doc => + transid.finished(this, start, s"[PUT] '$collName' completed document: '$docinfoStr', document: '$doc'") + DocInfo(DocId(id), DocRevision(rev)) + } + .recover { + case t: MongoException if t.getCode == 11000 => + transid.finished(this, start, s"[PUT] '$dbName', document: '$docinfoStr'; conflict.") + throw DocumentConflictException("conflict on 'put'") + case t: MongoException => + transid.failed( + this, + start, + s"[PUT] '$dbName' failed to put document: '$docinfoStr'; return error code: '${t.getCode}'", + ErrorLevel) + throw new Exception("Unexpected mongodb server error: " + t.getMessage) + } + + reportFailure( + f, + failure => + transid + .failed(this, start, s"[PUT] '$collName' internal error, failure: '${failure.getMessage}'", ErrorLevel)) + } + + override protected[database] def del(doc: DocInfo)(implicit transid: TransactionId): Future[Boolean] = { + require(doc != null && doc.rev.asString != null, "doc revision required for delete") + + val start = + transid.started(this, LoggingMarkers.DATABASE_DELETE, s"[DEL] '$collName' deleting document: '$doc'") + + val f = collection + .deleteOne(Filters.and(Filters.eq("_id", doc.id.id), Filters.eq("_rev", doc.rev.rev))) + .toFuture() + .flatMap { result => + if (result.getDeletedCount == 1) { // the result can only be 1 or 0 + transid.finished(this, start, s"[DEL] '$collName' completed document: '$doc'") + Future(true) + } else { + collection.find(Filters.eq("_id", doc.id.id)).toFuture.map { result => + if (result.size == 1) { + // find the document according to _id, conflict + transid.finished(this, start, s"[DEL] '$collName', document: '$doc'; conflict.") + throw DocumentConflictException("conflict on 'delete'") + } else { + // doesn't find the document according to _id, not found + transid.finished(this, start, s"[DEL] '$collName', document: '$doc'; not found.") + throw NoDocumentException(s"$doc not found on 'delete'") + } + } + } + } + .recover { + case t: MongoException => + transid.failed( + this, + start, + s"[DEL] '$collName' failed to delete document: '$doc'; error code: '${t.getCode}'", + ErrorLevel) + throw new Exception("Unexpected mongodb server error: " + t.getMessage) + } + + reportFailure( + f, + failure => + transid.failed( + this, + start, + s"[DEL] '$collName' internal error, doc: '$doc', failure: '${failure.getMessage}'", + ErrorLevel)) + } + + override protected[database] def get[A <: DocumentAbstraction](doc: DocInfo, + attachmentHandler: Option[(A, Attached) => A] = None)( + implicit transid: TransactionId, + ma: Manifest[A]): Future[A] = { + + val start = transid.started(this, LoggingMarkers.DATABASE_GET, s"[GET] '$dbName' finding document: '$doc'") + + require(doc != null, "doc undefined") + + val f = collection + .find(Filters.eq("_id", doc.id.id)) // method deserialize will check whether the _rev matched + .toFuture() + .map(result => + if (result.isEmpty) { + transid.finished(this, start, s"[GET] '$collName', document: '$doc'; not found.") + throw NoDocumentException("not found on 'get'") + } else { + transid.finished(this, start, s"[GET] '$collName' completed: found document '$doc'") + val response = result.head.toJson(jsonWriteSettings).parseJson.asJsObject + val decodeData = decodeFields(fieldsNeedEncode, response) + + val deserializedDoc = deserialize[A, DocumentAbstraction](doc, decodeData) + attachmentHandler + .map(processAttachments(deserializedDoc, decodeData, doc.id.id, _)) + .getOrElse(deserializedDoc) + }) + .recoverWith { + case t: MongoException => + transid.finished(this, start, s"[GET] '$collName' failed to get document: '$doc'; error code: '${t.getCode}'") + throw new Exception("Unexpected mongodb server error: " + t.getMessage) + case _: DeserializationException => throw DocumentUnreadable(Messages.corruptedEntity) + } + + reportFailure( + f, + failure => + transid.failed( + this, + start, + s"[GET] '$collName' internal error, doc: '$doc', failure: '${failure.getMessage}'", + ErrorLevel)) + } + + override protected[database] def get(id: DocId)(implicit transid: TransactionId): Future[Option[JsObject]] = { + val start = transid.started(this, LoggingMarkers.DATABASE_GET, s"[GET] '$collName' finding document: '$id'") + val f = collection + .find(Filters.equal("_id", id.id)) + .head() + .map { + case d: Document => + transid.finished(this, start, s"[GET] '$dbName' completed: found document '$id'") + Some(decodeFields(fieldsNeedEncode, d.toJson(jsonWriteSettings).parseJson.asJsObject)) + case null => + transid.finished(this, start, s"[GET] '$dbName', document: '$id'; not found.") + None + } + .recover { + case t: MongoException => + transid.failed( + this, + start, + s"[GET] '$collName' failed to get document: '$id'; error code: '${t.getCode}'", + ErrorLevel) + throw new Exception("Unexpected mongodb server error: " + t.getMessage) + } + + reportFailure( + f, + failure => + transid.failed( + this, + start, + s"[GET] '$collName' internal error, doc: '$id', failure: '${failure.getMessage}'", + ErrorLevel)) + } + + override protected[core] def query(table: String, + startKey: List[Any], + endKey: List[Any], + skip: Int, + limit: Int, + includeDocs: Boolean, + descending: Boolean, + reduce: Boolean, + stale: StaleParameter)(implicit transid: TransactionId): Future[List[JsObject]] = { + require(!(reduce && includeDocs), "reduce and includeDocs cannot both be true") + require(!reduce, "Reduce scenario not supported") //TODO Investigate reduce + require(skip >= 0, "skip should be non negative") + require(limit >= 0, "limit should be non negative") + + val Array(ddoc, viewName) = table.split("/") + + val find = collection + .find(viewMapper.filter(ddoc, viewName, startKey, endKey)) + + viewMapper.sort(ddoc, viewName, descending).foreach(find.sort) + + find.skip(skip).limit(limit) + + val realIncludeDocs = includeDocs | documentHandler.shouldAlwaysIncludeDocs(ddoc, viewName) + val start = transid.started(this, LoggingMarkers.DATABASE_QUERY, s"[QUERY] '$collName' searching '$table") + + val f = find + .toFuture() + .map { docs => + transid.finished(this, start, s"[QUERY] '$dbName' completed: matched ${docs.size}") + docs.map { doc => + val js = decodeFields(fieldsNeedEncode, doc.toJson(jsonWriteSettings).parseJson.convertTo[JsObject]) + documentHandler.transformViewResult( + ddoc, + viewName, + startKey, + endKey, + realIncludeDocs, + JsObject(js.fields - _computed), + MongoDBArtifactStore.this) + } + } + .flatMap(Future.sequence(_)) + .map(_.flatten.toList) + .recover { + case t: MongoException => + transid.failed(this, start, s"[QUERY] '$collName' failed; error code: '${t.getCode}'", ErrorLevel) + throw new Exception("Unexpected mongodb server error: " + t.getMessage) + } + + reportFailure( + f, + failure => + transid + .failed(this, start, s"[QUERY] '$collName' internal error, failure: '${failure.getMessage}'", ErrorLevel)) + } + + protected[core] def count(table: String, startKey: List[Any], endKey: List[Any], skip: Int, stale: StaleParameter)( + implicit transid: TransactionId): Future[Long] = { + require(skip >= 0, "skip should be non negative") + + val Array(ddoc, viewName) = table.split("/") + val start = transid.started(this, LoggingMarkers.DATABASE_QUERY, s"[COUNT] '$dbName' searching '$table") + + val query = viewMapper.filter(ddoc, viewName, startKey, endKey) + + val option = CountOptions().skip(skip) + val f = + collection + .countDocuments(query, option) + .toFuture() + .map { result => + transid.finished(this, start, s"[COUNT] '$collName' completed: count $result") + result + } + .recover { + case t: MongoException => + transid.failed(this, start, s"[COUNT] '$collName' failed; error code: '${t.getCode}'", ErrorLevel) + throw new Exception("Unexpected mongodb server error: " + t.getMessage) + } + + reportFailure( + f, + failure => + transid + .failed(this, start, s"[COUNT] '$dbName' internal error, failure: '${failure.getMessage}'", ErrorLevel)) + } + + override protected[database] def putAndAttach[A <: DocumentAbstraction]( + doc: A, + update: (A, Attached) => A, + contentType: ContentType, + docStream: Source[ByteString, _], + oldAttachment: Option[Attached])(implicit transid: TransactionId): Future[(DocInfo, Attached)] = { + + attachmentStore match { + case Some(as) => + attachToExternalStore(doc, update, contentType, docStream, oldAttachment, as) + case None => + attachToMongo(doc, update, contentType, docStream, oldAttachment) + } + + } + + private def attachToMongo[A <: DocumentAbstraction]( + doc: A, + update: (A, Attached) => A, + contentType: ContentType, + docStream: Source[ByteString, _], + oldAttachment: Option[Attached])(implicit transid: TransactionId): Future[(DocInfo, Attached)] = { + + for { + bytesOrSource <- inlineOrAttach(docStream) + uri = uriOf(bytesOrSource, UUID().asString) + attached <- { + bytesOrSource match { + case Left(bytes) => + Future.successful(Attached(uri.toString, contentType, Some(bytes.size), Some(digest(bytes)))) + case Right(source) => + attach(doc, uri.path.toString, contentType, source).map { r => + Attached(uri.toString, contentType, Some(r.length), Some(r.digest)) + } + } + } + docInfo <- put(update(doc, attached)) + + //Remove old attachment if it was part of attachmentStore + _ <- oldAttachment + .map { old => + val oldUri = Uri(old.attachmentName) + if (oldUri.scheme == mongodbScheme) { + val name = oldUri.path.toString + gridFSBucket.delete(BsonString(s"${docInfo.id.id}/$name")).toFuture.map { _ => + true + } + } else { + Future.successful(true) + } + } + .getOrElse(Future.successful(true)) + } yield (docInfo, attached) + } + + private def attach(d: DocumentAbstraction, name: String, contentType: ContentType, docStream: Source[ByteString, _])( + implicit transid: TransactionId): Future[AttachResult] = { + + logging.info(this, s"Uploading attach $name") + val asJson = d.toDocumentRecord + val id: String = asJson.fields("_id").convertTo[String].trim + require(!id.isEmpty, "document id must be defined") + + val start = transid.started( + this, + LoggingMarkers.DATABASE_ATT_SAVE, + s"[ATT_PUT] '$collName' uploading attachment '$name' of document 'id: $id'") + + val document: org.bson.Document = new org.bson.Document("contentType", contentType.toString) + //add the document id to the metadata + document.append("belongsTo", id) + + val option = new GridFSUploadOptions().metadata(document) + + val uploadStream = gridFSBucket.openUploadStream(BsonString(s"$id/$name"), name, option) + val sink = MongoDBAsyncStreamSink(uploadStream) + + val f = docStream + .runWith(combinedSink(sink)) + .map { r => + transid + .finished(this, start, s"[ATT_PUT] '$collName' completed uploading attachment '$name' of document '$id'") + AttachResult(r.digest, r.length) + } + .recover { + case t: MongoException => + transid.failed( + this, + start, + s"[ATT_PUT] '$collName' failed to upload attachment '$name' of document '$id'; error code '${t.getCode}'", + ErrorLevel) + throw new Exception("Unexpected mongodb server error: " + t.getMessage) + } + + reportFailure( + f, + failure => + transid.failed( + this, + start, + s"[ATT_PUT] '$collName' internal error, name: '$name', doc: '$id', failure: '${failure.getMessage}'", + ErrorLevel)) + } + + override protected[core] def readAttachment[T](doc: DocInfo, attached: Attached, sink: Sink[ByteString, Future[T]])( + implicit transid: TransactionId): Future[T] = { + + val name = attached.attachmentName + val attachmentUri = Uri(name) + + attachmentUri.scheme match { + case AttachmentSupport.MemScheme => + memorySource(attachmentUri).runWith(sink) + case s if s == mongodbScheme || attachmentUri.isRelative => + //relative case is for compatibility with earlier naming approach where attachment name would be like 'jarfile' + //Compared to current approach of '<scheme>:<name>' + readAttachmentFromMongo(doc, attachmentUri, sink) + case s if attachmentStore.isDefined && attachmentStore.get.scheme == s => + attachmentStore.get.readAttachment(doc.id, attachmentUri.path.toString, sink) + case _ => + throw new IllegalArgumentException(s"Unknown attachment scheme in attachment uri $attachmentUri") + } + } + + private def readAttachmentFromMongo[T](doc: DocInfo, attachmentUri: Uri, sink: Sink[ByteString, Future[T]])( + implicit transid: TransactionId): Future[T] = { + + val attachmentName = attachmentUri.path.toString + val start = transid.started( + this, + LoggingMarkers.DATABASE_ATT_GET, + s"[ATT_GET] '$dbName' finding attachment '$attachmentName' of document '$doc'") + + require(doc != null, "doc undefined") + require(doc.rev.rev != null, "doc revision must be specified") + + val downloadStream = gridFSBucket.openDownloadStream(BsonString(s"${doc.id.id}/$attachmentName")) + + def readStream(file: GridFSFile) = { + val source = MongoDBAsyncStreamSource(downloadStream) + source + .runWith(sink) + .map { result => + transid + .finished( + this, + start, + s"[ATT_GET] '$collName' completed: found attachment '$attachmentName' of document '$doc'") + result + } + } + + def getGridFSFile = { + downloadStream + .gridFSFile() + .head() + .transform( + identity, { + case ex: MongoGridFSException if ex.getMessage.contains("File not found") => + transid.finished( + this, + start, + s"[ATT_GET] '$collName', retrieving attachment '$attachmentName' of document '$doc'; not found.") + NoDocumentException("Not found on 'readAttachment'.") + case ex: MongoGridFSException => + transid.failed( + this, + start, + s"[ATT_GET] '$collName' failed to get attachment '$attachmentName' of document '$doc'; error code: '${ex.getCode}'", + ErrorLevel) + throw new Exception("Unexpected mongodb server error: " + ex.getMessage) + case t => t + }) + } + + val f = for { + file <- getGridFSFile + result <- readStream(file) + } yield result + + reportFailure( + f, + failure => + transid.failed( + this, + start, + s"[ATT_GET] '$dbName' internal error, name: '$attachmentName', doc: '$doc', failure: '${failure.getMessage}'", + ErrorLevel)) + + } + + override protected[core] def deleteAttachments[T](doc: DocInfo)(implicit transid: TransactionId): Future[Boolean] = + attachmentStore + .map(as => as.deleteAttachments(doc.id)) + .getOrElse(Future.successful(true)) // For MongoDB it is expected that the entire document is deleted. + + override def shutdown(): Unit = { + // MongoClient maintains the connection pool internally, we don't need to manage it + attachmentStore.foreach(_.shutdown()) + } + + private def reportFailure[T, U](f: Future[T], onFailure: Throwable => U): Future[T] = { + f.failed.foreach { + case _: ArtifactStoreException => // These failures are intentional and shouldn't trigger the catcher. + case x => onFailure(x) + } + f + } + + // calculate the revision manually, to be compatible with couchdb's _rev field + private def revisionCalculate(doc: JsObject): (String, String) = { + val md: MessageDigest = MessageDigest.getInstance("MD5") Review comment: minor nit: I think we can use store util instead? https://github.com/apache/openwhisk/blob/master/common/scala/src/main/scala/org/apache/openwhisk/core/database/StoreUtils.scala#L101 Should the underlying algorithm be MD5? ########## File path: common/scala/src/main/scala/org/apache/openwhisk/core/database/mongodb/MongoDBViewMapper.scala ########## @@ -0,0 +1,224 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openwhisk.core.database.mongodb + +import org.apache.openwhisk.core.database._ +import org.apache.openwhisk.core.entity.WhiskQueries +import org.mongodb.scala.Document +import org.mongodb.scala.bson.conversions.Bson +import org.mongodb.scala.model.Filters._ +import org.mongodb.scala.model.Sorts + +trait MongoDBViewMapper { Review comment: Would be great to include some description about the role of this mapper. ########## File path: ansible/library/mongodb.py ########## @@ -0,0 +1,283 @@ +#!/usr/bin/python + +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +DOCUMENTATION = ''' +--- +module: mongodb +short_description: A module which support some simple operations on MongoDB. +description: + - Including add user/insert document/create indexes in MongoDB Review comment: If this tool does something similar to wskadmin, should we put this under tools? https://github.com/apache/openwhisk/blob/68120f2170dc9f9b53361ab0cb51c4e9458dbe29/tools/admin/wskadmin ########## File path: ansible/roles/mongodb/tasks/deploy.yml ########## @@ -0,0 +1,38 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +--- +# This role will run a MongoDB server on the db group, this is only for test, please use +# shared cluster for production env + +- name: (re)start mongodb Review comment: BTW I think this can be handled in the subsequent PR, but just asking, should we include some steps to set up a MongoDB cluster rather than a standalone one? ########## File path: common/scala/src/main/scala/org/apache/openwhisk/core/database/mongodb/MongoDBArtifactStoreProvider.scala ########## @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openwhisk.core.database.mongodb + +import akka.actor.ActorSystem +import akka.stream.ActorMaterializer +import org.apache.openwhisk.common.Logging +import org.apache.openwhisk.core.ConfigKeys +import org.apache.openwhisk.core.database._ +import org.apache.openwhisk.core.entity.size._ +import org.apache.openwhisk.core.entity.{DocumentReader, WhiskActivation, WhiskAuth, WhiskEntity} +import org.mongodb.scala.MongoClient +import pureconfig._ +import pureconfig.generic.auto._ +import spray.json.RootJsonFormat + +import scala.reflect.ClassTag + +case class MongoDBConfig(uri: String, database: String) { + assume(Set(database, uri).forall(_.nonEmpty), "At least one expected property is missing") + + def collectionFor[D](implicit tag: ClassTag[D]) = tag.runtimeClass.getSimpleName.toLowerCase +} + +object MongoDBClient { + private var _client: Option[MongoClient] = None + + def client(config: MongoDBConfig): MongoClient = { Review comment: Is this used by other than MongoDBArtifactStore? ########## File path: ansible/README.md ########## @@ -196,6 +196,42 @@ ansible-playbook -i environments/$ENVIRONMENT routemgmt.yml - To use the API Gateway, you'll need to run `apigateway.yml` and `routemgmt.yml`. - Use `ansible-playbook -i environments/$ENVIRONMENT openwhisk.yml` to avoid wiping the data store. This is useful to start OpenWhisk after restarting your Operating System. +### Deploying Using MongoDB + +You can choose MongoDB instead of CouchDB as the database backend to store entities and activations. Review comment: Seems this PR only includes ArtifactStore, should we remove "activations" from the sentence? ########## File path: common/scala/src/main/scala/org/apache/openwhisk/core/database/mongodb/MongoDBArtifactStoreProvider.scala ########## @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openwhisk.core.database.mongodb + +import akka.actor.ActorSystem +import akka.stream.ActorMaterializer +import org.apache.openwhisk.common.Logging +import org.apache.openwhisk.core.ConfigKeys +import org.apache.openwhisk.core.database._ +import org.apache.openwhisk.core.entity.size._ +import org.apache.openwhisk.core.entity.{DocumentReader, WhiskActivation, WhiskAuth, WhiskEntity} +import org.mongodb.scala.MongoClient +import pureconfig._ +import pureconfig.generic.auto._ +import spray.json.RootJsonFormat + +import scala.reflect.ClassTag + +case class MongoDBConfig(uri: String, database: String) { + assume(Set(database, uri).forall(_.nonEmpty), "At least one expected property is missing") Review comment: Just wondering if it's possible for one of these two parameters to be missing. I suppose even if an empty string is passed, we can't check that with this. ########## File path: common/scala/src/main/scala/org/apache/openwhisk/core/database/mongodb/MongoDBViewMapper.scala ########## @@ -0,0 +1,224 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openwhisk.core.database.mongodb + +import org.apache.openwhisk.core.database._ +import org.apache.openwhisk.core.entity.WhiskQueries +import org.mongodb.scala.Document +import org.mongodb.scala.bson.conversions.Bson +import org.mongodb.scala.model.Filters._ +import org.mongodb.scala.model.Sorts + +trait MongoDBViewMapper { + protected val _computed: String = "_computed" + protected val TOP: String = WhiskQueries.TOP + + val indexes: List[Document] + + def filter(ddoc: String, view: String, startKey: List[Any], endKey: List[Any]): Bson + + def sort(ddoc: String, view: String, descending: Boolean): Option[Bson] + + protected def checkKeys(startKey: List[Any], endKey: List[Any]): Unit = { + require(startKey.nonEmpty) + require(endKey.nonEmpty) + require(startKey.head == endKey.head, s"First key should be same => ($startKey) - ($endKey)") + } +} + +private object ActivationViewMapper extends MongoDBViewMapper { + private val NS = "namespace" + private val NS_WITH_PATH = s"${_computed}.${ActivationHandler.NS_PATH}" + private val START = "start" + override val indexes: List[Document] = + List( + Document(s"$START" -> -1), + Document(s"$START" -> -1, s"$NS" -> -1), + Document(s"$NS_WITH_PATH" -> -1, s"$START" -> -1)) + + override def filter(ddoc: String, view: String, startKey: List[Any], endKey: List[Any]): Bson = { + checkKeys(startKey, endKey) + view match { + //whisks-filters ddoc uses namespace + invoking action path as first key + case "activations" if ddoc.startsWith("whisks-filters") => createActivationFilter(NS_WITH_PATH, startKey, endKey) + //whisks ddoc uses namespace as first key + case "activations" if ddoc.startsWith("whisks") => createActivationFilter(NS, startKey, endKey) + case _ => throw UnsupportedView(s"$ddoc/$view") + } + } + + override def sort(ddoc: String, view: String, descending: Boolean): Option[Bson] = { + view match { + case "activations" if ddoc.startsWith("whisks-filters") => + val sort = if (descending) Sorts.descending(NS_WITH_PATH, START) else Sorts.ascending(NS_WITH_PATH, START) + Some(sort) + case "activations" if ddoc.startsWith("whisks") => + val sort = if (descending) Sorts.descending(NS, START) else Sorts.ascending(NS, START) + Some(sort) + case _ => throw UnsupportedView(s"$ddoc/$view") + } + } + + private def createActivationFilter(nsPropName: String, startKey: List[Any], endKey: List[Any]) = { + require(startKey.head.isInstanceOf[String]) + val matchNS = equal(nsPropName, startKey.head) + + val filter = (startKey, endKey) match { + case (_ :: Nil, _ :: `TOP` :: Nil) => + matchNS + case (_ :: since :: Nil, _ :: `TOP` :: `TOP` :: Nil) => + and(matchNS, gte(START, since)) + case (_ :: since :: Nil, _ :: upto :: `TOP` :: Nil) => + and(matchNS, gte(START, since), lte(START, upto)) + case _ => throw UnsupportedQueryKeys(s"$startKey, $endKey") + } + filter + } +} + +private object WhisksViewMapper extends MongoDBViewMapper { + private val NS = "namespace" + private val ROOT_NS = s"${_computed}.${WhisksHandler.ROOT_NS}" + private val TYPE = "entityType" + private val UPDATED = "updated" + private val PUBLISH = "publish" + private val BINDING = "binding" + override val indexes: List[Document] = + List(Document(s"$NS" -> -1, s"$UPDATED" -> -1), Document(s"$ROOT_NS" -> -1, s"$UPDATED" -> -1)) + + override def filter(ddoc: String, view: String, startKey: List[Any], endKey: List[Any]): Bson = { Review comment: Looks like it does list up data in DB, then where does this name(`filter`) come from? ########## File path: common/scala/src/main/scala/org/apache/openwhisk/core/database/mongodb/MongoDBArtifactStore.scala ########## @@ -0,0 +1,661 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openwhisk.core.database.mongodb + +import java.security.MessageDigest + +import akka.actor.ActorSystem +import akka.event.Logging.ErrorLevel +import akka.http.scaladsl.model._ +import akka.stream.ActorMaterializer +import akka.stream.scaladsl._ +import akka.util.ByteString +import com.mongodb.client.gridfs.model.GridFSUploadOptions +import org.apache.openwhisk.common.{Logging, LoggingMarkers, TransactionId} +import org.apache.openwhisk.core.database._ +import org.apache.openwhisk.core.database.StoreUtils._ +import org.apache.openwhisk.core.entity.Attachments.Attached +import org.apache.openwhisk.core.entity.{DocId, DocInfo, DocRevision, DocumentReader, UUID} +import org.apache.openwhisk.http.Messages +import org.bson.json.{JsonMode, JsonWriterSettings} +import org.mongodb.scala.bson.BsonString +import org.mongodb.scala.bson.collection.immutable.Document +import org.mongodb.scala.gridfs.{GridFSBucket, GridFSFile, MongoGridFSException} +import org.mongodb.scala.model._ +import org.mongodb.scala.{MongoClient, MongoCollection, MongoException} +import spray.json._ + +import scala.concurrent.Future +import scala.util.Try + +object MongoDBArtifactStore { + val _computed = "_computed" +} + +/** + * Basic client to put and delete artifacts in a data store. + * + * @param client the mongodb client to access database + * @param dbName the name of the database to operate on + * @param collName the name of the collection to operate on + * @param documentHandler helper class help to simulate the designDoc of CouchDB + * @param viewMapper helper class help to simulate the designDoc of CouchDB + */ +class MongoDBArtifactStore[DocumentAbstraction <: DocumentSerializer](client: MongoClient, + dbName: String, + collName: String, + documentHandler: DocumentHandler, + viewMapper: MongoDBViewMapper, + val inliningConfig: InliningConfig, + val attachmentStore: Option[AttachmentStore])( + implicit system: ActorSystem, + val logging: Logging, + jsonFormat: RootJsonFormat[DocumentAbstraction], + val materializer: ActorMaterializer, + docReader: DocumentReader) + extends ArtifactStore[DocumentAbstraction] + with DocumentProvider + with DefaultJsonProtocol + with AttachmentSupport[DocumentAbstraction] { + + import MongoDBArtifactStore._ + + protected[core] implicit val executionContext = system.dispatcher + + private val mongodbScheme = "mongodb" + val attachmentScheme: String = attachmentStore.map(_.scheme).getOrElse(mongodbScheme) + + private val database = client.getDatabase(dbName) + private val collection = getCollectionAndCreateIndexes + private val gridFSBucket = GridFSBucket(database, collName) + + private val jsonWriteSettings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build + + // MongoDB doesn't support using `$` as the first char of field name, so below two fields needs to be encoded first + private val fieldsNeedEncode = Seq("annotations", "parameters") + + override protected[database] def put(d: DocumentAbstraction)(implicit transid: TransactionId): Future[DocInfo] = { + val asJson = d.toDocumentRecord + + val id: String = asJson.fields.getOrElse("_id", JsString.empty).convertTo[String].trim + require(!id.isEmpty, "document id must be defined") + + val (old_rev, rev) = revisionCalculate(asJson) + val docinfoStr = s"id: $id, rev: $rev" + val start = + transid.started(this, LoggingMarkers.DATABASE_SAVE, s"[PUT] '$collName' saving document: '$docinfoStr'") + + val encodedData = encodeFields(fieldsNeedEncode, asJson) + + val data = JsObject( + encodedData.fields + (_computed -> documentHandler.computedFields(asJson)) + ("_rev" -> rev.toJson)) + + val filters = + if (rev.startsWith("1-")) { + // for new document, we should get no matched document and insert new one + // if there is a matched document, that one with no _rev filed will be replaced Review comment: filed -> field ########## File path: common/scala/src/main/scala/org/apache/openwhisk/core/database/mongodb/MongoDBArtifactStore.scala ########## @@ -0,0 +1,661 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openwhisk.core.database.mongodb + +import java.security.MessageDigest + +import akka.actor.ActorSystem +import akka.event.Logging.ErrorLevel +import akka.http.scaladsl.model._ +import akka.stream.ActorMaterializer +import akka.stream.scaladsl._ +import akka.util.ByteString +import com.mongodb.client.gridfs.model.GridFSUploadOptions +import org.apache.openwhisk.common.{Logging, LoggingMarkers, TransactionId} +import org.apache.openwhisk.core.database._ +import org.apache.openwhisk.core.database.StoreUtils._ +import org.apache.openwhisk.core.entity.Attachments.Attached +import org.apache.openwhisk.core.entity.{DocId, DocInfo, DocRevision, DocumentReader, UUID} +import org.apache.openwhisk.http.Messages +import org.bson.json.{JsonMode, JsonWriterSettings} +import org.mongodb.scala.bson.BsonString +import org.mongodb.scala.bson.collection.immutable.Document +import org.mongodb.scala.gridfs.{GridFSBucket, GridFSFile, MongoGridFSException} +import org.mongodb.scala.model._ +import org.mongodb.scala.{MongoClient, MongoCollection, MongoException} +import spray.json._ + +import scala.concurrent.Future +import scala.util.Try + +object MongoDBArtifactStore { + val _computed = "_computed" +} + +/** + * Basic client to put and delete artifacts in a data store. + * + * @param client the mongodb client to access database + * @param dbName the name of the database to operate on + * @param collName the name of the collection to operate on + * @param documentHandler helper class help to simulate the designDoc of CouchDB + * @param viewMapper helper class help to simulate the designDoc of CouchDB + */ +class MongoDBArtifactStore[DocumentAbstraction <: DocumentSerializer](client: MongoClient, Review comment: I think we need comprehensive documents describing the internals of this component and source/sink. That would be helpful for those who have less background in MongoDB like me. ########## File path: common/scala/src/main/scala/org/apache/openwhisk/core/database/mongodb/MongoDBArtifactStore.scala ########## @@ -0,0 +1,661 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openwhisk.core.database.mongodb + +import java.security.MessageDigest + +import akka.actor.ActorSystem +import akka.event.Logging.ErrorLevel +import akka.http.scaladsl.model._ +import akka.stream.ActorMaterializer +import akka.stream.scaladsl._ +import akka.util.ByteString +import com.mongodb.client.gridfs.model.GridFSUploadOptions +import org.apache.openwhisk.common.{Logging, LoggingMarkers, TransactionId} +import org.apache.openwhisk.core.database._ +import org.apache.openwhisk.core.database.StoreUtils._ +import org.apache.openwhisk.core.entity.Attachments.Attached +import org.apache.openwhisk.core.entity.{DocId, DocInfo, DocRevision, DocumentReader, UUID} +import org.apache.openwhisk.http.Messages +import org.bson.json.{JsonMode, JsonWriterSettings} +import org.mongodb.scala.bson.BsonString +import org.mongodb.scala.bson.collection.immutable.Document +import org.mongodb.scala.gridfs.{GridFSBucket, GridFSFile, MongoGridFSException} +import org.mongodb.scala.model._ +import org.mongodb.scala.{MongoClient, MongoCollection, MongoException} +import spray.json._ + +import scala.concurrent.Future +import scala.util.Try + +object MongoDBArtifactStore { + val _computed = "_computed" +} + +/** + * Basic client to put and delete artifacts in a data store. + * + * @param client the mongodb client to access database + * @param dbName the name of the database to operate on + * @param collName the name of the collection to operate on + * @param documentHandler helper class help to simulate the designDoc of CouchDB + * @param viewMapper helper class help to simulate the designDoc of CouchDB + */ +class MongoDBArtifactStore[DocumentAbstraction <: DocumentSerializer](client: MongoClient, + dbName: String, + collName: String, + documentHandler: DocumentHandler, + viewMapper: MongoDBViewMapper, + val inliningConfig: InliningConfig, + val attachmentStore: Option[AttachmentStore])( + implicit system: ActorSystem, + val logging: Logging, + jsonFormat: RootJsonFormat[DocumentAbstraction], + val materializer: ActorMaterializer, + docReader: DocumentReader) + extends ArtifactStore[DocumentAbstraction] + with DocumentProvider + with DefaultJsonProtocol + with AttachmentSupport[DocumentAbstraction] { + + import MongoDBArtifactStore._ + + protected[core] implicit val executionContext = system.dispatcher + + private val mongodbScheme = "mongodb" + val attachmentScheme: String = attachmentStore.map(_.scheme).getOrElse(mongodbScheme) + + private val database = client.getDatabase(dbName) + private val collection = getCollectionAndCreateIndexes + private val gridFSBucket = GridFSBucket(database, collName) + + private val jsonWriteSettings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build + + // MongoDB doesn't support using `$` as the first char of field name, so below two fields needs to be encoded first + private val fieldsNeedEncode = Seq("annotations", "parameters") + + override protected[database] def put(d: DocumentAbstraction)(implicit transid: TransactionId): Future[DocInfo] = { + val asJson = d.toDocumentRecord + + val id: String = asJson.fields.getOrElse("_id", JsString.empty).convertTo[String].trim + require(!id.isEmpty, "document id must be defined") + + val (old_rev, rev) = revisionCalculate(asJson) + val docinfoStr = s"id: $id, rev: $rev" + val start = + transid.started(this, LoggingMarkers.DATABASE_SAVE, s"[PUT] '$collName' saving document: '$docinfoStr'") + + val encodedData = encodeFields(fieldsNeedEncode, asJson) + + val data = JsObject( + encodedData.fields + (_computed -> documentHandler.computedFields(asJson)) + ("_rev" -> rev.toJson)) + + val filters = + if (rev.startsWith("1-")) { + // for new document, we should get no matched document and insert new one + // if there is a matched document, that one with no _rev filed will be replaced + // if there is a document with the same id but has an _rev field, will return en E11000(conflict) error Review comment: Is it possible for MongoDB to include `_rev` field? ########## File path: common/scala/src/main/scala/org/apache/openwhisk/core/database/mongodb/MongoDBArtifactStore.scala ########## @@ -0,0 +1,661 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openwhisk.core.database.mongodb + +import java.security.MessageDigest + +import akka.actor.ActorSystem +import akka.event.Logging.ErrorLevel +import akka.http.scaladsl.model._ +import akka.stream.ActorMaterializer +import akka.stream.scaladsl._ +import akka.util.ByteString +import com.mongodb.client.gridfs.model.GridFSUploadOptions +import org.apache.openwhisk.common.{Logging, LoggingMarkers, TransactionId} +import org.apache.openwhisk.core.database._ +import org.apache.openwhisk.core.database.StoreUtils._ +import org.apache.openwhisk.core.entity.Attachments.Attached +import org.apache.openwhisk.core.entity.{DocId, DocInfo, DocRevision, DocumentReader, UUID} +import org.apache.openwhisk.http.Messages +import org.bson.json.{JsonMode, JsonWriterSettings} +import org.mongodb.scala.bson.BsonString +import org.mongodb.scala.bson.collection.immutable.Document +import org.mongodb.scala.gridfs.{GridFSBucket, GridFSFile, MongoGridFSException} +import org.mongodb.scala.model._ +import org.mongodb.scala.{MongoClient, MongoCollection, MongoException} +import spray.json._ + +import scala.concurrent.Future +import scala.util.Try + +object MongoDBArtifactStore { + val _computed = "_computed" +} + +/** + * Basic client to put and delete artifacts in a data store. + * + * @param client the mongodb client to access database + * @param dbName the name of the database to operate on + * @param collName the name of the collection to operate on + * @param documentHandler helper class help to simulate the designDoc of CouchDB + * @param viewMapper helper class help to simulate the designDoc of CouchDB + */ +class MongoDBArtifactStore[DocumentAbstraction <: DocumentSerializer](client: MongoClient, + dbName: String, + collName: String, + documentHandler: DocumentHandler, + viewMapper: MongoDBViewMapper, + val inliningConfig: InliningConfig, + val attachmentStore: Option[AttachmentStore])( + implicit system: ActorSystem, + val logging: Logging, + jsonFormat: RootJsonFormat[DocumentAbstraction], + val materializer: ActorMaterializer, + docReader: DocumentReader) + extends ArtifactStore[DocumentAbstraction] + with DocumentProvider + with DefaultJsonProtocol + with AttachmentSupport[DocumentAbstraction] { + + import MongoDBArtifactStore._ + + protected[core] implicit val executionContext = system.dispatcher + + private val mongodbScheme = "mongodb" + val attachmentScheme: String = attachmentStore.map(_.scheme).getOrElse(mongodbScheme) + + private val database = client.getDatabase(dbName) + private val collection = getCollectionAndCreateIndexes Review comment: nit `getCollectionAndCreateIndexes` -> `getCollectionAndCreateIndexes()` to indicate there is a side effect according to the scala convention. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
