You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/12/17 12:09:03 UTC

[GitHub] markusthoemmes commented on a change in pull request #4183: Ensure cache gets properly updated with concurrent access for action with attachments

markusthoemmes commented on a change in pull request #4183: Ensure cache gets properly updated with concurrent access for action with attachments
URL: https://github.com/apache/incubator-openwhisk/pull/4183#discussion_r242122269
 
 

 ##########
 File path: common/scala/src/main/scala/org/apache/openwhisk/core/database/DocumentFactory.scala
 ##########
 @@ -198,23 +198,40 @@ trait DocumentFactory[W <: DocumentRevisionProvider] extends MultipleReadersSing
     doc: DocId,
     rev: DocRevision = DocRevision.empty,
     fromCache: Boolean = cacheEnabled)(implicit transid: TransactionId, mw: Manifest[W]): Future[W] = {
-    getWithAttachment(db, doc, rev, fromCache, None)
+    Try {
+      require(db != null, "db undefined")
+    } map {
+      implicit val logger = db.logging
+      implicit val ec = db.executionContext
+      val key = doc.asDocInfo(rev)
+      _ =>
+        cacheLookup(CacheKey(key), db.get[W](key, None), fromCache)
+    } match {
+      case Success(f) => f
+      case Failure(t) => Future.failed(t)
+    }
   }
 
+  /**
+   *  Fetches document along with attachment. `postProcess` would be used to process the fetched document
+   *  before adding it to cache. This ensures that for documents having attachment the cache is updated only
+   *  post fetch of the attachment
+   */
   protected def getWithAttachment[Wsuper >: W](
     db: ArtifactStore[Wsuper],
     doc: DocId,
     rev: DocRevision = DocRevision.empty,
     fromCache: Boolean,
-    attachmentHandler: Option[(W, Attached) => W])(implicit transid: TransactionId, mw: Manifest[W]): Future[W] = {
+    attachmentHandler: (W, Attached) => W,
+    postProcess: W => Future[W])(implicit transid: TransactionId, mw: Manifest[W]): Future[W] = {
     Try {
       require(db != null, "db undefined")
     } map {
       implicit val logger = db.logging
       implicit val ec = db.executionContext
       val key = doc.asDocInfo(rev)
       _ =>
-        cacheLookup(CacheKey(key), db.get[W](key, attachmentHandler), fromCache)
+        cacheLookup(CacheKey(key), db.get[W](key, Some(attachmentHandler)) flatMap { postProcess }, fromCache)
 
 Review comment:
   Nit: I find the combinators a bit hard to read
   
   ```scala
   db.get[W](key, Some(attachmentHandler)).flatMap(postProcess)
   ```
   
   Is imo a bit clearer.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services