zhouyifan279 commented on code in PR #5868:
URL: https://github.com/apache/kyuubi/pull/5868#discussion_r1438085262


##########
kyuubi-common/src/main/scala/org/apache/kyuubi/engine/deploy/yarn/ApplicationMaster.scala:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.kyuubi.engine.deploy.yarn
+
+import java.io.{File, IOException}
+
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.hadoop.fs.Path
+import org.apache.hadoop.yarn.api.records.FinalApplicationStatus
+import org.apache.hadoop.yarn.client.api.AMRMClient
+import org.apache.hadoop.yarn.client.api.AMRMClient.ContainerRequest
+import org.apache.hadoop.yarn.conf.YarnConfiguration
+
+import org.apache.kyuubi.{Logging, Utils}
+import org.apache.kyuubi.config.KyuubiConf
+import org.apache.kyuubi.service.Serverable
+import org.apache.kyuubi.util.KyuubiHadoopUtils
+import org.apache.kyuubi.util.command.CommandLineUtils.confKeyValues
+import org.apache.kyuubi.util.reflect.{DynFields, DynMethods}
+
+object ApplicationMaster extends Logging {
+
+  private var amClient: AMRMClient[ContainerRequest] = _
+  private var yarnConf: YarnConfiguration = _
+
+  private val kyuubiConf = new KyuubiConf()
+
+  private var currentEngineMainClass: String = _
+
+  private var currentEngine: Serverable = _
+
+  private var finalMsg: String = _
+
+  @volatile private var registered: Boolean = false
+  @volatile private var unregistered: Boolean = false
+  @volatile private var finalStatus = FinalApplicationStatus.UNDEFINED
+
+  def main(args: Array[String]): Unit = {
+    try {
+      val amArgs = new ApplicationMasterArguments(args)
+      Utils.getPropertiesFromFile(Some(new 
File(amArgs.propertiesFile))).foreach { case (k, v) =>
+        kyuubiConf.set(k, v)
+      }
+      currentEngineMainClass = amArgs.engineMainClass
+      yarnConf = KyuubiHadoopUtils.newYarnConfiguration(kyuubiConf)
+      Utils.addShutdownHook(() => {
+        if (!unregistered) {
+          if (currentEngine != null && currentEngine.selfExist) {
+            finalMsg = "Kyuubi Application Master is shutting down."
+            finalStatus = FinalApplicationStatus.SUCCEEDED
+          } else {
+            finalMsg = "Kyuubi Application Master is shutting down with error."
+            finalStatus = FinalApplicationStatus.FAILED
+          }
+          cleanupStagingDir()
+          unregister(finalStatus, finalMsg)
+        }
+      })
+      runApplicationMaster()
+    } catch {
+      case t: Throwable =>
+        error("Error running ApplicationMaster", t)
+        finalStatus = FinalApplicationStatus.FAILED
+        finalMsg = t.getMessage
+        cleanupStagingDir()
+        unregister(finalStatus, finalMsg)
+        if (currentEngine != null) {
+          currentEngine.stop()
+        }
+    }
+  }
+
+  def runApplicationMaster(): Unit = {
+    initAmClient()
+
+    runEngine()
+
+    registerAM()
+  }
+
+  def runEngine(): Unit = {
+    val buffer = new ArrayBuffer[String]()
+    buffer ++= confKeyValues(kyuubiConf.getAll)
+
+    val instance = DynFields.builder()
+      .impl(currentEngineMainClass, "MODULE$")
+      .build[Object].get(null)
+    DynMethods.builder("main")
+      .hiddenImpl(currentEngineMainClass, classOf[Array[String]])
+      .buildChecked()
+      .invoke(instance, buffer.toArray)
+
+    currentEngine = DynFields.builder()
+      .hiddenImpl(currentEngineMainClass, "currentEngine")
+      .buildChecked[Option[Serverable]]()
+      .get(instance)
+      .get
+  }
+
+  def initAmClient(): Unit = {
+    amClient = AMRMClient.createAMRMClient()
+    amClient.init(yarnConf)
+    amClient.start()
+  }
+
+  def registerAM(): Unit = {
+    val frontendService = currentEngine.frontendServices.head
+    val trackingUrl = frontendService.connectionUrl
+    val (host, port) = resolveHostAndPort(trackingUrl)
+    info("Registering the HiveSQLEngine ApplicationMaster with tracking url " +
+      s"$trackingUrl, host = $host, port = $port")
+    synchronized {
+      amClient.registerApplicationMaster(host, port, trackingUrl)

Review Comment:
   Better to set trackingUrl to Hive Server2 Web UI address.



##########
kyuubi-common/src/main/scala/org/apache/kyuubi/engine/deploy/yarn/ApplicationMaster.scala:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.kyuubi.engine.deploy.yarn
+
+import java.io.{File, IOException}
+
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.hadoop.fs.Path
+import org.apache.hadoop.yarn.api.records.FinalApplicationStatus
+import org.apache.hadoop.yarn.client.api.AMRMClient
+import org.apache.hadoop.yarn.client.api.AMRMClient.ContainerRequest
+import org.apache.hadoop.yarn.conf.YarnConfiguration
+
+import org.apache.kyuubi.{Logging, Utils}
+import org.apache.kyuubi.config.KyuubiConf
+import org.apache.kyuubi.service.Serverable
+import org.apache.kyuubi.util.KyuubiHadoopUtils
+import org.apache.kyuubi.util.command.CommandLineUtils.confKeyValues
+import org.apache.kyuubi.util.reflect.{DynFields, DynMethods}
+
+object ApplicationMaster extends Logging {
+
+  private var amClient: AMRMClient[ContainerRequest] = _
+  private var yarnConf: YarnConfiguration = _
+
+  private val kyuubiConf = new KyuubiConf()
+
+  private var currentEngineMainClass: String = _
+
+  private var currentEngine: Serverable = _
+
+  private var finalMsg: String = _
+
+  @volatile private var registered: Boolean = false
+  @volatile private var unregistered: Boolean = false
+  @volatile private var finalStatus = FinalApplicationStatus.UNDEFINED
+
+  def main(args: Array[String]): Unit = {
+    try {
+      val amArgs = new ApplicationMasterArguments(args)
+      Utils.getPropertiesFromFile(Some(new 
File(amArgs.propertiesFile))).foreach { case (k, v) =>
+        kyuubiConf.set(k, v)
+      }
+      currentEngineMainClass = amArgs.engineMainClass
+      yarnConf = KyuubiHadoopUtils.newYarnConfiguration(kyuubiConf)
+      Utils.addShutdownHook(() => {
+        if (!unregistered) {
+          if (currentEngine != null && currentEngine.selfExist) {
+            finalMsg = "Kyuubi Application Master is shutting down."
+            finalStatus = FinalApplicationStatus.SUCCEEDED
+          } else {
+            finalMsg = "Kyuubi Application Master is shutting down with error."
+            finalStatus = FinalApplicationStatus.FAILED
+          }
+          cleanupStagingDir()
+          unregister(finalStatus, finalMsg)
+        }
+      })
+      runApplicationMaster()
+    } catch {
+      case t: Throwable =>
+        error("Error running ApplicationMaster", t)
+        finalStatus = FinalApplicationStatus.FAILED
+        finalMsg = t.getMessage
+        cleanupStagingDir()
+        unregister(finalStatus, finalMsg)
+        if (currentEngine != null) {
+          currentEngine.stop()
+        }
+    }
+  }
+
+  def runApplicationMaster(): Unit = {
+    initAmClient()
+
+    runEngine()
+
+    registerAM()
+  }
+
+  def runEngine(): Unit = {
+    val buffer = new ArrayBuffer[String]()
+    buffer ++= confKeyValues(kyuubiConf.getAll)
+
+    val instance = DynFields.builder()
+      .impl(currentEngineMainClass, "MODULE$")
+      .build[Object].get(null)
+    DynMethods.builder("main")
+      .hiddenImpl(currentEngineMainClass, classOf[Array[String]])
+      .buildChecked()
+      .invoke(instance, buffer.toArray)
+
+    currentEngine = DynFields.builder()
+      .hiddenImpl(currentEngineMainClass, "currentEngine")
+      .buildChecked[Option[Serverable]]()
+      .get(instance)
+      .get
+  }
+
+  def initAmClient(): Unit = {
+    amClient = AMRMClient.createAMRMClient()
+    amClient.init(yarnConf)
+    amClient.start()
+  }
+
+  def registerAM(): Unit = {
+    val frontendService = currentEngine.frontendServices.head
+    val trackingUrl = frontendService.connectionUrl
+    val (host, port) = resolveHostAndPort(trackingUrl)
+    info("Registering the HiveSQLEngine ApplicationMaster with tracking url " +
+      s"$trackingUrl, host = $host, port = $port")
+    synchronized {
+      amClient.registerApplicationMaster(host, port, trackingUrl)
+      registered = true
+    }
+  }
+
+  def unregister(status: FinalApplicationStatus, diagnostics: String): Unit = {
+    synchronized {
+      if (registered && !unregistered) {
+        info(s"Unregistering ApplicationMaster with $status" +
+          Option(diagnostics).map(msg => s" (diagnostics message: 
$msg)").getOrElse(""))
+        unregistered = true
+        amClient.unregisterApplicationMaster(status, diagnostics, "")

Review Comment:
   We can set trackingUrl to AM log address here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to