openwhisk-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dube...@apache.org
Subject [incubator-openwhisk] branch master updated: Optional write activation results to file… (#4243)
Date Mon, 04 Feb 2019 19:55:21 GMT
This is an automated email from the ASF dual-hosted git repository.

dubeejw pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new abe2d33  Optional write activation results to file… (#4243)
abe2d33 is described below

commit abe2d33fc876df011687c9ed43571684b6eaefe5
Author: James Dubee <jwdubee@us.ibm.com>
AuthorDate: Mon Feb 4 14:55:15 2019 -0500

    Optional write activation results to file… (#4243)
    
    * ArtifactWithFileStorageActivationStore configuration for writing results to file
    
    * Use if statements instead of match
    
    * Review refactor
---
 .../core/database/ActivationFileStorage.scala      |  8 ++-
 .../ArtifactWithFileStorageActivationStore.scala   | 12 +++-
 ...tifactWithFileStorageActivationStoreTests.scala | 70 ++++++++++++++++++++--
 3 files changed, 81 insertions(+), 9 deletions(-)

diff --git a/common/scala/src/main/scala/org/apache/openwhisk/core/database/ActivationFileStorage.scala
b/common/scala/src/main/scala/org/apache/openwhisk/core/database/ActivationFileStorage.scala
index 7fd7741..23036ff 100644
--- a/common/scala/src/main/scala/org/apache/openwhisk/core/database/ActivationFileStorage.scala
+++ b/common/scala/src/main/scala/org/apache/openwhisk/core/database/ActivationFileStorage.scala
@@ -36,6 +36,7 @@ import scala.concurrent.duration._
 
 class ActivationFileStorage(logFilePrefix: String,
                             logPath: Path,
+                            writeResultToFile: Boolean,
                             actorMaterializer: ActorMaterializer,
                             logging: Logging) {
 
@@ -92,8 +93,11 @@ class ActivationFileStorage(logFilePrefix: String,
 
   private def transcribeActivation(activation: WhiskActivation, additionalFields: Map[String,
JsValue]) = {
     val transactionType = Map("type" -> "activation_record".toJson)
-    val message = Map(
-      "message" -> s"Activation record '${activation.activationId}' for entity '${activation.name}'".toJson)
+    val message = Map(if (writeResultToFile) {
+      "message" -> JsString(activation.response.result.getOrElse(JsNull).compactPrint)
+    } else {
+      "message" -> JsString(s"Activation record '${activation.activationId}' for entity
'${activation.name}'")
+    })
     val annotations = activation.annotations.toJsObject.fields
     val addFields = transactionType ++ annotations ++ message ++ additionalFields
     val removeFields = Seq("logs", "annotations")
diff --git a/common/scala/src/main/scala/org/apache/openwhisk/core/database/ArtifactWithFileStorageActivationStore.scala
b/common/scala/src/main/scala/org/apache/openwhisk/core/database/ArtifactWithFileStorageActivationStore.scala
index 597a1f9..c621cc6 100644
--- a/common/scala/src/main/scala/org/apache/openwhisk/core/database/ArtifactWithFileStorageActivationStore.scala
+++ b/common/scala/src/main/scala/org/apache/openwhisk/core/database/ArtifactWithFileStorageActivationStore.scala
@@ -29,7 +29,10 @@ import spray.json._
 
 import scala.concurrent.Future
 
-case class ArtifactWithFileStorageActivationStoreConfig(logFilePrefix: String, logPath: String,
userIdField: String)
+case class ArtifactWithFileStorageActivationStoreConfig(logFilePrefix: String,
+                                                        logPath: String,
+                                                        userIdField: String,
+                                                        writeResultToFile: Boolean)
 
 class ArtifactWithFileStorageActivationStore(
   actorSystem: ActorSystem,
@@ -40,7 +43,12 @@ class ArtifactWithFileStorageActivationStore(
     extends ArtifactActivationStore(actorSystem, actorMaterializer, logging) {
 
   private val activationFileStorage =
-    new ActivationFileStorage(config.logFilePrefix, Paths.get(config.logPath), actorMaterializer,
logging)
+    new ActivationFileStorage(
+      config.logFilePrefix,
+      Paths.get(config.logPath),
+      config.writeResultToFile,
+      actorMaterializer,
+      logging)
 
   def getLogFile = activationFileStorage.getLogFile
 
diff --git a/tests/src/test/scala/org/apache/openwhisk/core/database/ArtifactWithFileStorageActivationStoreTests.scala
b/tests/src/test/scala/org/apache/openwhisk/core/database/ArtifactWithFileStorageActivationStoreTests.scala
index 9253844..205634c 100644
--- a/tests/src/test/scala/org/apache/openwhisk/core/database/ArtifactWithFileStorageActivationStoreTests.scala
+++ b/tests/src/test/scala/org/apache/openwhisk/core/database/ArtifactWithFileStorageActivationStoreTests.scala
@@ -77,7 +77,7 @@ class ArtifactWithFileStorageActivationStoreTests()
           "2018-03-05T02:10:38.196754258Z stdout: second log line of multiple lines")))
   }
 
-  def expectedFileContent(activation: WhiskActivation) = {
+  def expectedFileContent(activation: WhiskActivation, includeResult: Boolean) = {
     val expectedLogs = activation.logs.logs.map { log =>
       JsObject(
         "type" -> "user_log".toJson,
@@ -85,6 +85,11 @@ class ArtifactWithFileStorageActivationStoreTests()
         "activationId" -> activation.activationId.toJson,
         "namespaceId" -> user.namespace.uuid.toJson)
     }
+    val expectedResult = if (includeResult) {
+      JsString(activation.response.result.getOrElse(JsNull).compactPrint)
+    } else {
+      JsString(s"Activation record '${activation.activationId}' for entity '${activation.name}'")
+    }
     val expectedActivation = JsObject(
       "type" -> "activation_record".toJson,
       "duration" -> activation.duration.toJson,
@@ -97,7 +102,7 @@ class ArtifactWithFileStorageActivationStoreTests()
       "version" -> activation.version.toJson,
       "response" -> activation.response.withoutResult.toExtendedJson,
       "end" -> activation.end.toEpochMilli.toJson,
-      "message" -> s"Activation record '${activation.activationId}' for entity '${activation.name}'".toJson,
+      "message" -> expectedResult,
       "kind" -> activation.annotations.get("kind").toJson.toJson,
       "start" -> activation.start.toEpochMilli.toJson,
       "limits" -> activation.annotations.get("limits").toJson.toJson,
@@ -107,8 +112,8 @@ class ArtifactWithFileStorageActivationStoreTests()
     expectedLogs ++ Seq(expectedActivation)
   }
 
-  it should "store activations in artifact store and to file" in {
-    val config = ArtifactWithFileStorageActivationStoreConfig("userlogs", "logs", "namespaceId")
+  it should "store activations in artifact store and to file without result" in {
+    val config = ArtifactWithFileStorageActivationStoreConfig("userlogs", "logs", "namespaceId",
false)
     val activationStore = new ArtifactWithFileStorageActivationStore(system, materializer,
logging, config)
     val logDir = new File(new File(".").getCanonicalPath, config.logPath)
 
@@ -147,7 +152,62 @@ class ArtifactWithFileStorageActivationStoreTests()
         .toList
         .map(_.parseJson)
         .toJson
-        .convertTo[JsArray] shouldBe activations.map(expectedFileContent).flatten.toJson.convertTo[JsArray]
+        .convertTo[JsArray] shouldBe activations
+        .map(a => expectedFileContent(a, false))
+        .flatten
+        .toJson
+        .convertTo[JsArray]
+    } finally {
+      activationStore.getLogFile.toFile.getAbsoluteFile.delete
+      logDir.delete
+    }
+  }
+
+  it should "store activations in artifact store and to file with result" in {
+    val config = ArtifactWithFileStorageActivationStoreConfig("userlogs", "logs", "namespaceId",
true)
+    val activationStore = new ArtifactWithFileStorageActivationStore(system, materializer,
logging, config)
+    val logDir = new File(new File(".").getCanonicalPath, config.logPath)
+
+    try {
+      logDir.mkdir
+
+      val activations = responsePermutations.map { response =>
+        logPermutations.map { logs =>
+          val activation = WhiskActivation(
+            namespace = EntityPath(subject.asString),
+            name = EntityName("name"),
+            subject = subject,
+            activationId = ActivationId.generate(),
+            start = Instant.now,
+            end = Instant.now,
+            response = response,
+            logs = logs,
+            duration = Some(101L),
+            annotations = Parameters("kind", "nodejs:6") ++ Parameters(
+              "limits",
+              ActionLimits(TimeLimit(60.second), MemoryLimit(256.MB), LogLimit(10.MB)).toJson)
++
+              Parameters("waitTime", 16.toJson) ++
+              Parameters("initTime", 44.toJson))
+          val docInfo = await(activationStore.store(activation, context))
+          val fullyQualifiedActivationId = ActivationId(docInfo.id.asString)
+
+          await(activationStore.get(fullyQualifiedActivationId, context)) shouldBe activation
+          await(activationStore.delete(fullyQualifiedActivationId, context))
+          activation
+        }
+      }.flatten
+
+      Source
+        .fromFile(activationStore.getLogFile.toFile.getAbsoluteFile)
+        .getLines
+        .toList
+        .map(_.parseJson)
+        .toJson
+        .convertTo[JsArray] shouldBe activations
+        .map(a => expectedFileContent(a, true))
+        .flatten
+        .toJson
+        .convertTo[JsArray]
     } finally {
       activationStore.getLogFile.toFile.getAbsoluteFile.delete
       logDir.delete


Mime
View raw message