Github user vanzin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/8744#discussion_r39584199
  
    --- Diff: 
yarn/src/main/scala/org/apache/spark/scheduler/cluster/YarnExtensionService.scala
 ---
    @@ -0,0 +1,198 @@
    +/*
    + * 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.spark.scheduler.cluster
    +
    +import java.io.Closeable
    +import java.util.concurrent.atomic.AtomicBoolean
    +
    +import org.apache.hadoop.service.AbstractService
    +import org.apache.hadoop.service.Service.STATE
    +import org.apache.hadoop.util.ShutdownHookManager
    +import org.apache.hadoop.yarn.api.records.{ApplicationAttemptId, 
ApplicationId}
    +
    +import org.apache.spark.util.Utils
    +import org.apache.spark.{Logging, SparkContext}
    +
    +/**
    + * An extension sservice that can be loaded into a Spark YARN application.
    + * A Service that can be started and closed (==stop).
    + *
    + * The `close()` operation MUST be idempotent, and succeed even if 
`start()` was
    + * never invoked.
    + */
    +trait YarnExtensionService extends Closeable {
    +
    +  /**
    +   * For Yarn services, SparkContext, and ApplicationId is the basic info 
required.
    +   * This operation must be called before `init`
    +   * @param sc spark context
    +   * @param appId YARN application ID
    +   * @param attemptId YARN attempt ID if known. Otherwise, `None`.
    +   */
    +  def start(sc: SparkContext,
    +      appId: ApplicationId,
    +      attemptId: Option[ApplicationAttemptId]): Boolean
    +}
    +
    +
    +/**
    + * Class which loads child Yarn extension Services from the configuration,
    + * and closes them all when closed/stopped itself.
    + *
    + * It extends YARN's `AbstractService` to pick up its state model, and 
adds an alternate entry
    + * point for actually configuring and starting the service.
    + *
    + * An instance may be configured to register a shutdown hook. This 
addresses the issue in
    + * which a spark context is not always closed in an application, 
especially a `yarn-client`
    + * application. The shutdown hook allows services an opportunity to 
perform any final
    + * actions their `Service.stop` invocations trigger.
    + */
    +private[spark] class YarnExtensionServices extends 
AbstractService("YarnExtensionServices")
    +    with Logging {
    +  private var services: List[YarnExtensionService] = Nil
    +  private var sparkContext: SparkContext = _
    +  private var appId: ApplicationId = _
    +  private var attemptId: Option[ApplicationAttemptId] = _
    +  private val started = new AtomicBoolean(false)
    +  private var registerShutdownHook = false
    +  private var shutdownHookPriority = 0
    +
    +  /**
    +   * This is the routine to optionally be registered as a shutdown hook.
    +   * It logs the invocation and then calls the `close()` method to stop the
    +   * service instance. It is public to aid in testing.
    +   */
    +  val shutdownHook = new Runnable {
    +    override def run(): Unit = {
    +      logInfo(s"In shutdown hook for $this")
    +      try {
    +        stop()
    +      }
    +      catch {
    +        case e: Exception =>
    +          logInfo(s"During shutdown: $e", e)
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Binding operation will load the named services and call bind on them 
too; the
    +   * entire set of services are then ready for `init()` and `start()` calls
    +   * @param context spark context
    +   * @param appId application ID
    +   * @param attemptId YARN attempt ID if known. Otherwise, `None`.
    +   * @param registerShutdownHook should the services register as a hadoop 
shutdown hook?
    +   * @param shutdownHookPriority and if so, what priority
    +   */
    +  def start(context: SparkContext,
    +      appId: ApplicationId,
    +      attemptId: Option[ApplicationAttemptId] = None,
    +      registerShutdownHook: Boolean = false,
    +      shutdownHookPriority: Int = 0): Unit = {
    +    if (isInState(STATE.STARTED)) {
    +      logWarning("Ignoring re-entrant start operation")
    +      return
    +    }
    +    require(context != null, "Null context parameter")
    +    require(appId != null, "Null appId parameter")
    +    require(!registerShutdownHook || shutdownHookPriority > 0,
    +           "if registerShutdownHook is set, shutdownHookPriority must be 
greater than zero")
    +    // this operation will trigger the state change checks
    +    super.init(context.hadoopConfiguration)
    +    this.sparkContext = context
    +    this.appId = appId
    +    this.attemptId = attemptId
    +    this.registerShutdownHook = registerShutdownHook
    +    this.shutdownHookPriority = shutdownHookPriority
    +    logInfo(s"Starting Yarn extension services with app $appId and 
attemptId $attemptId")
    +    super.start()
    +  }
    +
    +  /**
    +   * Loads a comma separated list of yarn services
    +   */
    +  override def serviceStart(): Unit = {
    +    val sNames = 
sparkContext.getConf.getOption(YarnExtensionServices.SPARK_YARN_SERVICES)
    --- End diff --
    
    I prefer the slightly less noisy approach:
    
        services = 
sparkContext.getConf.getOption(YarnExtensionServices.SPARK_YARN_SERVICES)
          .map(_.split(","))
          .flatMap { sClass => ...
          }
    
    You lose the "no services" debug message but it's not like it's useful.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to