markusthoemmes commented on a change in pull request #2228: Hook up reactive pool into invoker startup URL: https://github.com/apache/incubator-openwhisk/pull/2228#discussion_r116415560
########## File path: core/invoker/src/main/scala/whisk/core/invoker/InvokerReactive.scala ########## @@ -0,0 +1,181 @@ +/* + * Copyright 2015-2016 IBM Corporation + * + * Licensed 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 whisk.core.invoker + +import scala.concurrent.Await +import scala.concurrent.Future +import scala.concurrent.duration._ +import scala.util.Failure +import scala.util.Success + +import akka.actor.ActorRef +import akka.actor.ActorRefFactory +import akka.actor.ActorSystem +import spray.json._ +import spray.json.DefaultJsonProtocol._ +import whisk.common.Logging +import whisk.common.LoggingMarkers +import whisk.common.TransactionId +import whisk.core.WhiskConfig +import whisk.core.connector.ActivationMessage +import whisk.core.connector.CompletionMessage +import whisk.core.connector.MessageProducer +import whisk.core.container.{ ContainerPool => OldContainerPool } +import whisk.core.container.Interval +import whisk.core.containerpool.ContainerProxy +import whisk.core.containerpool.PrewarmingConfig +import whisk.core.containerpool.Run +import whisk.core.containerpool.docker.DockerClientWithFileAccess +import whisk.core.containerpool.docker.DockerContainer +import whisk.core.containerpool.docker.RuncClient +import whisk.core.dispatcher.MessageHandler +import whisk.core.entity._ +import whisk.core.entity.ExecManifest.ImageName +import whisk.core.entity.size._ + +class InvokerReactive( + config: WhiskConfig, + instance: Int, + activationFeed: ActorRef, + producer: MessageProducer)(implicit actorSystem: ActorSystem, logging: Logging) + extends MessageHandler(s"invoker$instance") { + + implicit val ec = actorSystem.dispatcher + + private val entityStore = WhiskEntityStore.datastore(config) + private val activationStore = WhiskActivationStore.datastore(config) + + implicit val docker = new DockerClientWithFileAccess()(ec) + implicit val runc = new RuncClient(ec) + + /** Cleans up all running wsk_ containers */ + def cleanup() = { + val cleaning = docker.ps(Seq("name" -> "wsk_"))(TransactionId.invokerNanny).flatMap { containers => + val removals = containers.map { id => + runc.resume(id)(TransactionId.invokerNanny).recoverWith { + case _ => Future.successful(()) + }.flatMap { + _ => docker.rm(id)(TransactionId.invokerNanny) + } + } + Future.sequence(removals) + } + + Await.ready(cleaning, 30.seconds) + } + cleanup() + sys.addShutdownHook(cleanup()) + + val containerFactory = (tid: TransactionId, name: String, actionImage: ImageName, userProvidedImage: Boolean, memory: ByteSize) => { + val image = if (userProvidedImage) { + actionImage.publicImageName + } else { + actionImage.localImageName(config.dockerRegistry, config.dockerImagePrefix, Some(config.dockerImageTag)) + } + + DockerContainer.create( + tid, + image = image, + userProvidedImage = userProvidedImage, + memory = memory, + cpuShares = OldContainerPool.cpuShare(config), + environment = Map("__OW_API_HOST" -> config.wskApiHost), + network = config.invokerContainerNetwork, + name = Some(name)) + } + + val ack = (tid: TransactionId, activation: WhiskActivation) => { + implicit val transid = tid + producer.send("completed", CompletionMessage(tid, activation, s"invoker$instance")).andThen { + case Success(_) => logging.info(this, s"posted completion of activation ${activation.activationId}") + } + } + + val store = (tid: TransactionId, activation: WhiskActivation) => { + implicit val transid = tid + logging.info(this, "recording the activation result to the data store") + WhiskActivation.put(activationStore, activation).andThen { + case Success(id) => logging.info(this, s"recorded activation") + case Failure(t) => logging.error(this, s"failed to record activation") + } + } + + val prewarmKind = "nodejs:6" + val prewarmExec = ExecManifest.runtimesManifest.resolveDefaultRuntime(prewarmKind).map { manifest => + new CodeExecAsString(manifest, "", None) + }.get + + val childFactory = (f: ActorRefFactory) => f.actorOf(ContainerProxy.props(containerFactory, ack, store)) + val pool = actorSystem.actorOf(whisk.core.containerpool.ContainerPool.props( + childFactory, + OldContainerPool.getDefaultMaxActive(config), + activationFeed, + Some(PrewarmingConfig(2, prewarmExec, 256.MB)))) + + override def onMessage(msg: ActivationMessage)(implicit transid: TransactionId): Future[Unit] = { + require(msg != null, "message undefined") + require(msg.action.version.isDefined, "action version undefined") + + val start = transid.started(this, LoggingMarkers.INVOKER_ACTIVATION) + val namespace = msg.action.path + val name = msg.action.name + val actionid = FullyQualifiedEntityName(namespace, name).toDocId.asDocInfo(msg.revision) + val tran = Transaction(msg) + val subject = msg.user.subject + + logging.info(this, s"${actionid.id} $subject ${msg.activationId}") + + // caching is enabled since actions have revision id and an updated + // action will not hit in the cache due to change in the revision id; + // if the doc revision is missing, then bypass cache + if (actionid.rev == DocRevision()) { + logging.error(this, s"revision was not provided for ${actionid.id}") + } + WhiskAction.get(entityStore, actionid.id, actionid.rev, fromCache = actionid.rev != DocRevision()).flatMap { action => Review comment: Hmm, we could. Not sure if it would be too beneficial though. I imagine the `Future` would then fail with different Exceptions, indicating a missing action or a failed deserialization? As we only need it in so little places, I'd leave it as is, at least for now. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
