Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize updated value of entity document in response #4646

Merged
merged 18 commits into from
Nov 29, 2019
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.apache.openwhisk.core.entity

import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
import java.nio.charset.StandardCharsets.UTF_8
import java.time.Instant
import java.util.Base64

import akka.http.scaladsl.model.ContentTypes
Expand Down Expand Up @@ -122,7 +123,8 @@ abstract class WhiskActionLikeMetaData(override val name: EntityName) extends Wh
* @param limits the limits to impose on the action
* @param version the semantic version
* @param publish true to share the action or false otherwise
* @param annotation the set of annotations to attribute to the action
* @param annotations the set of annotations to attribute to the action
* @param updated the timestamp when the action is updated
* @throws IllegalArgumentException if any argument is undefined
*/
@throws[IllegalArgumentException]
Expand All @@ -133,7 +135,8 @@ case class WhiskAction(namespace: EntityPath,
limits: ActionLimits = ActionLimits(),
version: SemVer = SemVer(),
publish: Boolean = false,
annotations: Parameters = Parameters())
annotations: Parameters = Parameters(),
override val updated: Instant = WhiskEntity.currentMillis())
rabbah marked this conversation as resolved.
Show resolved Hide resolved
extends WhiskActionLike(name) {

require(exec != null, "exec undefined")
Expand Down Expand Up @@ -200,6 +203,7 @@ case class WhiskActionMetaData(namespace: EntityPath,
version: SemVer = SemVer(),
publish: Boolean = false,
annotations: Parameters = Parameters(),
override val updated: Instant = WhiskEntity.currentMillis(),
binding: Option[EntityPath] = None)
extends WhiskActionLikeMetaData(name) {

Expand Down Expand Up @@ -264,7 +268,7 @@ case class WhiskActionMetaData(namespace: EntityPath,
* @param limits the limits to impose on the action
* @param version the semantic version
* @param publish true to share the action or false otherwise
* @param annotation the set of annotations to attribute to the action
* @param annotations the set of annotations to attribute to the action
* @param binding the path of the package binding if any
* @throws IllegalArgumentException if any argument is undefined
*/
Expand Down Expand Up @@ -330,7 +334,7 @@ case class ExecutableWhiskActionMetaData(namespace: EntityPath,
require(limits != null, "limits undefined")

def toWhiskAction =
WhiskActionMetaData(namespace, name, exec, parameters, limits, version, publish, annotations)
WhiskActionMetaData(namespace, name, exec, parameters, limits, version, publish, annotations, updated)
.revision[WhiskActionMetaData](rev)

/**
Expand All @@ -342,11 +346,13 @@ case class ExecutableWhiskActionMetaData(namespace: EntityPath,
}

object WhiskAction extends DocumentFactory[WhiskAction] with WhiskEntityQueries[WhiskAction] with DefaultJsonProtocol {
import WhiskActivation.instantSerdes

val execFieldName = "exec"
val requireWhiskAuthHeader = "x-require-whisk-auth"

override val collectionName = "actions"
override val cacheEnabled = true

override implicit val serdes = jsonFormat(
WhiskAction.apply,
Expand All @@ -357,9 +363,8 @@ object WhiskAction extends DocumentFactory[WhiskAction] with WhiskEntityQueries[
"limits",
"version",
"publish",
"annotations")

override val cacheEnabled = true
"annotations",
"updated")

// overriden to store attached code
override def put[A >: WhiskAction](db: ArtifactStore[A], doc: WhiskAction, old: Option[WhiskAction])(
Expand Down Expand Up @@ -547,7 +552,10 @@ object WhiskActionMetaData
with WhiskEntityQueries[WhiskActionMetaData]
with DefaultJsonProtocol {

import WhiskActivation.instantSerdes

override val collectionName = "actions"
override val cacheEnabled = true

override implicit val serdes = jsonFormat(
WhiskActionMetaData.apply,
Expand All @@ -559,10 +567,9 @@ object WhiskActionMetaData
"version",
"publish",
"annotations",
"updated",
"binding")

override val cacheEnabled = true

/**
* Resolves an action name if it is contained in a package.
* Look up the package to determine if it is a binding or the actual package.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ package org.apache.openwhisk.core.entity

import java.time.Clock
import java.time.Instant
import java.time.temporal.ChronoUnit

import scala.Stream
import scala.util.Try

import spray.json._
import org.apache.openwhisk.core.database.DocumentUnreadable
import org.apache.openwhisk.core.database.DocumentTypeMismatchException
Expand All @@ -37,7 +36,7 @@ import org.apache.openwhisk.http.Messages
* @param namespace the namespace for the entity as an abstract field
* @param version the semantic version as an abstract field
* @param publish true to share the entity and false to keep it private as an abstract field
* @param annotation the set of annotations to attribute to the entity
* @param annotations the set of annotations to attribute to the entity
*
* @throws IllegalArgumentException if any argument is undefined
*/
Expand All @@ -49,7 +48,7 @@ abstract class WhiskEntity protected[entity] (en: EntityName, val entityType: St
val version: SemVer
val publish: Boolean
val annotations: Parameters
val updated = Instant.now(Clock.systemUTC())
val updated = WhiskEntity.currentMillis()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since all the subtypes now assign this value in the constructors, can we make this an abstract value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can't make this an abstract value because the WhiskActivation entity type doesn't assign this value to the constructor.


/**
* The name of the entity qualified with its namespace and version for
Expand Down Expand Up @@ -112,6 +111,15 @@ object WhiskEntity {
def qualifiedName(namespace: EntityPath, activationId: ActivationId) = {
s"$namespace${EntityPath.PATHSEP}$activationId"
}

/**
* Get Instant object with a millisecond precision
* timestamp of whisk entity is stored in milliseconds in the db
*/
def currentMillis() = {
Instant.now(Clock.systemUTC()).truncatedTo(ChronoUnit.MILLIS)
}

}

object WhiskDocumentReader extends DocumentReader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

package org.apache.openwhisk.core.entity

import java.time.Instant

import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.language.postfixOps
import scala.util.Try

import spray.json.DefaultJsonProtocol
import spray.json.DefaultJsonProtocol._
import spray.json._
Expand Down Expand Up @@ -60,7 +61,8 @@ case class WhiskPackagePut(binding: Option[Binding] = None,
* @param parameters the set of parameters to bind to the action environment
* @param version the semantic version
* @param publish true to share the action or false otherwise
* @param annotation the set of annotations to attribute to the package
* @param annotations the set of annotations to attribute to the package
* @param updated the timestamp when the package is updated
* @throws IllegalArgumentException if any argument is undefined
*/
@throws[IllegalArgumentException]
Expand All @@ -70,7 +72,8 @@ case class WhiskPackage(namespace: EntityPath,
parameters: Parameters = Parameters(),
version: SemVer = SemVer(),
publish: Boolean = false,
annotations: Parameters = Parameters())
annotations: Parameters = Parameters(),
override val updated: Instant = WhiskEntity.currentMillis())
extends WhiskEntity(name, "package") {

require(binding != null || (binding map { _ != null } getOrElse true), "binding undefined")
Expand Down Expand Up @@ -159,6 +162,8 @@ object WhiskPackage
with WhiskEntityQueries[WhiskPackage]
with DefaultJsonProtocol {

import WhiskActivation.instantSerdes

val bindingFieldName = "binding"
override val collectionName = "packages"

Expand Down Expand Up @@ -197,7 +202,7 @@ object WhiskPackage
override def write(b: Option[Binding]) = Binding.optionalBindingSerializer.write(b)
override def read(js: JsValue) = Binding.optionalBindingDeserializer.read(js)
}
jsonFormat7(WhiskPackage.apply)
jsonFormat8(WhiskPackage.apply)
}

override val cacheEnabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@

package org.apache.openwhisk.core.entity

import java.time.Instant

import scala.util.Failure
import scala.util.Success
import scala.util.Try

import spray.json.DefaultJsonProtocol
import spray.json.DeserializationException
import spray.json.JsObject
Expand Down Expand Up @@ -65,7 +66,8 @@ case class WhiskRulePut(trigger: Option[FullyQualifiedEntityName] = None,
* @param action the action name to invoke invoke when trigger is fired
* @param version the semantic version
* @param publish true to share the action or false otherwise
* @param annotation the set of annotations to attribute to the rule
* @param annotations the set of annotations to attribute to the rule
* @param updated the timestamp when the rule is updated
* @throws IllegalArgumentException if any argument is undefined
*/
@throws[IllegalArgumentException]
Expand All @@ -75,10 +77,12 @@ case class WhiskRule(namespace: EntityPath,
action: FullyQualifiedEntityName,
version: SemVer = SemVer(),
publish: Boolean = false,
annotations: Parameters = Parameters())
annotations: Parameters = Parameters(),
override val updated: Instant = WhiskEntity.currentMillis())
extends WhiskEntity(name, "rule") {

def withStatus(s: Status) = WhiskRuleResponse(namespace, name, s, trigger, action, version, publish, annotations)
def withStatus(s: Status) =
WhiskRuleResponse(namespace, name, s, trigger, action, version, publish, annotations, updated)

def toJson = WhiskRule.serdes.write(this).asJsObject
}
Expand All @@ -95,7 +99,7 @@ case class WhiskRule(namespace: EntityPath,
* @param action the action name to invoke invoke when trigger is fired
* @param version the semantic version
* @param publish true to share the action or false otherwise
* @param annotation the set of annotations to attribute to the rule
* @param annotations the set of annotations to attribute to the rule
*/
case class WhiskRuleResponse(namespace: EntityPath,
name: EntityName,
Expand All @@ -104,7 +108,8 @@ case class WhiskRuleResponse(namespace: EntityPath,
action: FullyQualifiedEntityName,
version: SemVer = SemVer(),
publish: Boolean = false,
annotations: Parameters = Parameters()) {
annotations: Parameters = Parameters(),
updated: Instant) {

def toWhiskRule = WhiskRule(namespace, name, trigger, action, version, publish, annotations)
}
Expand Down Expand Up @@ -195,11 +200,12 @@ protected[core] object Status extends ArgNormalizer[Status] {
}

object WhiskRule extends DocumentFactory[WhiskRule] with WhiskEntityQueries[WhiskRule] with DefaultJsonProtocol {
import WhiskActivation.instantSerdes

override val collectionName = "rules"

private implicit val fqnSerdes = FullyQualifiedEntityName.serdes
private val caseClassSerdes = jsonFormat7(WhiskRule.apply)
private val caseClassSerdes = jsonFormat8(WhiskRule.apply)

override implicit val serdes = new RootJsonFormat[WhiskRule] {
def write(r: WhiskRule) = caseClassSerdes.write(r)
Expand Down Expand Up @@ -233,8 +239,9 @@ object WhiskRule extends DocumentFactory[WhiskRule] with WhiskEntityQueries[Whis
}

object WhiskRuleResponse extends DefaultJsonProtocol {
import WhiskActivation.instantSerdes
private implicit val fqnSerdes = FullyQualifiedEntityName.serdes
implicit val serdes = jsonFormat8(WhiskRuleResponse.apply)
implicit val serdes = jsonFormat9(WhiskRuleResponse.apply)
}

object WhiskRulePut extends DefaultJsonProtocol {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.openwhisk.core.entity

import java.time.Instant

import spray.json.DefaultJsonProtocol
import org.apache.openwhisk.core.database.DocumentFactory
import spray.json._
Expand Down Expand Up @@ -54,8 +56,9 @@ case class ReducedRule(action: FullyQualifiedEntityName, status: Status)
* @param limits the limits to impose on the trigger
* @param version the semantic version
* @param publish true to share the action or false otherwise
* @param annotation the set of annotations to attribute to the trigger
* @param annotations the set of annotations to attribute to the trigger
* @param rules the map of the rules that are associated with this trigger. Key is the rulename and value is the ReducedRule
* @param updated the timestamp when the trigger is updated
* @throws IllegalArgumentException if any argument is undefined
*/
@throws[IllegalArgumentException]
Expand All @@ -66,7 +69,8 @@ case class WhiskTrigger(namespace: EntityPath,
version: SemVer = SemVer(),
publish: Boolean = false,
annotations: Parameters = Parameters(),
rules: Option[Map[FullyQualifiedEntityName, ReducedRule]] = None)
rules: Option[Map[FullyQualifiedEntityName, ReducedRule]] = None,
override val updated: Instant = WhiskEntity.currentMillis())
extends WhiskEntity(name, "trigger") {

require(limits != null, "limits undefined")
Expand Down Expand Up @@ -108,11 +112,12 @@ object WhiskTrigger
extends DocumentFactory[WhiskTrigger]
with WhiskEntityQueries[WhiskTrigger]
with DefaultJsonProtocol {
import WhiskActivation.instantSerdes

override val collectionName = "triggers"

private implicit val fqnSerdesAsDocId = FullyQualifiedEntityName.serdesAsDocId
override implicit val serdes = jsonFormat8(WhiskTrigger.apply)
override implicit val serdes = jsonFormat9(WhiskTrigger.apply)

override val cacheEnabled = true
}
Expand Down
4 changes: 4 additions & 0 deletions core/controller/src/main/resources/apiv1swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1998,6 +1998,10 @@
"deactivating"
]
},
"updated": {
"type": "integer",
"description": "Time when the rule was updated"
},
"trigger": {
"$ref": "#/definitions/PathName"
},
Expand Down
Loading