rabbah commented on a change in pull request #4129: Adding 
YARNContainerFactory. This allows OpenWhisk to run actions on Apache Hadoop 
clusters.
URL: 
https://github.com/apache/incubator-openwhisk/pull/4129#discussion_r257477401
 
 

 ##########
 File path: 
common/scala/src/main/scala/org/apache/openwhisk/core/yarn/YARNContainerFactory.scala
 ##########
 @@ -0,0 +1,243 @@
+/*
+ * 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.yarn
+
+import akka.actor.{ActorRef, ActorSystem, Props}
+import akka.http.scaladsl.model.{HttpMethods, StatusCodes}
+import akka.pattern.ask
+import akka.util.Timeout
+import org.apache.openwhisk.common.{Logging, TransactionId}
+import org.apache.openwhisk.core.containerpool._
+import org.apache.openwhisk.core.entity.ExecManifest.ImageName
+import org.apache.openwhisk.core.entity.{ByteSize, ExecManifest, 
InvokerInstanceId}
+import org.apache.openwhisk.core.yarn.YARNComponentActor.CreateContainerAsync
+import org.apache.openwhisk.core.{ConfigKeys, WhiskConfig}
+import pureconfig.loadConfigOrThrow
+import spray.json._
+
+import scala.collection.immutable.HashMap
+import scala.concurrent.{blocking, ExecutionContext, Future}
+import scala.concurrent.duration._
+import YARNJsonProtocol._
+import akka.stream.ActorMaterializer
+
+case class YARNConfig(masterUrl: String,
+                      yarnLinkLogMessage: Boolean,
+                      serviceName: String,
+                      authType: String,
+                      kerberosPrincipal: String,
+                      kerberosKeytab: String,
+                      queue: String,
+                      memory: String,
+                      cpus: Int)
+
+object YARNContainerFactoryProvider extends ContainerFactoryProvider {
+  override def instance(actorSystem: ActorSystem,
+                        logging: Logging,
+                        config: WhiskConfig,
+                        instance: InvokerInstanceId,
+                        parameters: Map[String, Set[String]]): 
ContainerFactory =
+    new YARNContainerFactory(actorSystem, logging, config, instance, 
parameters)
+}
+class YARNContainerFactory(actorSystem: ActorSystem,
+                           logging: Logging,
+                           config: WhiskConfig,
+                           instance: InvokerInstanceId,
+                           parameters: Map[String, Set[String]],
+                           containerArgs: ContainerArgsConfig =
+                             
loadConfigOrThrow[ContainerArgsConfig](ConfigKeys.containerArgs),
+                           yarnConfig: YARNConfig = 
loadConfigOrThrow[YARNConfig](ConfigKeys.yarn))
+    extends ContainerFactory {
+
+  val images: Set[ImageName] = 
ExecManifest.runtimesManifest.runtimes.flatMap(a => a.versions.map(b => 
b.image))
+
+  //One actor of each type per image for parallelism
+  private var yarnComponentActors: Map[ImageName, ActorRef] = 
HashMap[ImageName, ActorRef]()
+  private var YARNContainerInfoActors: Map[ImageName, ActorRef] = 
HashMap[ImageName, ActorRef]()
+
+  val serviceStartTimeoutMS = 60000
+  val retryWaitMS = 1000
+  val runCommand = ""
+  val version = "1.0.0"
+  val description = "OpenWhisk Action Service"
+
+  //Allows for invoker HA
+  val serviceName: String = yarnConfig.serviceName + "-" + instance.toInt
+
+  val containerStartTimeoutMS = 60000
+
+  implicit val as: ActorSystem = actorSystem
+  implicit val materializer: ActorMaterializer = ActorMaterializer()
+  implicit val ec: ExecutionContext = actorSystem.dispatcher
+
+  override def init(): Unit = {
+    yarnComponentActors = images
+      .map(
+        i =>
+          (
+            i,
+            actorSystem.actorOf(
+              Props(new YARNComponentActor(actorSystem, logging, yarnConfig, 
serviceName, i)),
+              name = s"YARNComponentActor-${i.name}")))
+      .toMap
+    YARNContainerInfoActors = images
+      .map(
+        i =>
+          (
+            i,
+            actorSystem.actorOf(
+              Props(new YARNContainerInfoActor(actorSystem, logging, 
yarnConfig, serviceName, i)),
+              name = s"YARNComponentInfoActor-${i.name}")))
+      .toMap
+    blocking {
+      implicit val timeout: Timeout = 
Timeout(serviceStartTimeoutMS.milliseconds)
+
+      //Remove service if it already exists
+      val serviceDef =
+        YARNRESTUtil.downloadServiceDefinition(yarnConfig.authType, 
serviceName, yarnConfig.masterUrl)(logging)
+
+      if (serviceDef != null)
+        removeService()
+
+      createService()
+    }
+  }
+  override def createContainer(
+    unusedtid: TransactionId,
+    unusedname: String,
+    actionImage: ExecManifest.ImageName,
+    unuseduserProvidedImage: Boolean,
+    unusedmemory: ByteSize,
+    unusedcpuShares: Int)(implicit config: WhiskConfig, logging: Logging): 
Future[Container] = {
+    implicit val timeout: Timeout = 
Timeout(containerStartTimeoutMS.milliseconds)
+
+    //First send the create command to YARN, then with a different actor, wait 
for the container to be ready
+    ask(yarnComponentActors(actionImage), CreateContainerAsync).flatMap(_ =>
+      ask(YARNContainerInfoActors(actionImage), 
GetContainerInfo(yarnComponentActors(actionImage))).mapTo[Container])
+  }
+  override def cleanup(): Unit = {
+    removeService()
+    yarnComponentActors.foreach({ case (k, v)     => actorSystem.stop(v) })
+    YARNContainerInfoActors.foreach({ case (k, v) => actorSystem.stop(v) })
+  }
+
 
 Review comment:
   spacing between method is not consistent... sorry for nit picky comments.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to