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

    https://github.com/apache/spark/pull/5096#discussion_r26809399
  
    --- Diff: core/src/main/scala/org/apache/spark/deploy/RRunner.scala ---
    @@ -0,0 +1,117 @@
    +/*
    + * 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.deploy
    +
    +import java.io._
    +import java.util.concurrent.{Semaphore, TimeUnit}
    +
    +import scala.collection.JavaConversions._
    +
    +import org.apache.hadoop.fs.Path
    +
    +import org.apache.spark.api.r.RBackend
    +
    +/**
    + * Main class used to launch SparkR applications using spark-submit. It 
executes R as a
    + * subprocess and then has it connect back to the JVM to access system 
properties etc.
    + */
    +object RRunner {
    +  def main(args: Array[String]) {
    +    val rFile = PythonRunner.formatPath(args(0))
    +
    +    val otherArgs = args.slice(1, args.length)
    +
    +    // Time to wait for SparkR backend to initialize in seconds
    +    val backendTimeout = sys.env.getOrElse("SPARKR_BACKEND_TIMEOUT", 
"120").toInt
    +    val rCommand = "Rscript"
    +
    +    // Check if the file path exists.
    +    // If not, change directory to current working directory for YARN 
cluster mode
    +    val rF = new File(rFile)
    +    val rFileNormalized = if (!rF.exists()) {
    +      new Path(rFile).getName
    +    } else {
    +      rFile
    +    }
    +
    +    // Launch a SparkR backend server for the R process to connect to; 
this will let it see our
    +    // Java system properties etc.
    +    val sparkRBackend = new RBackend()
    +    @volatile var sparkRBackendPort = 0
    +    val initialized = new Semaphore(0)
    +    val sparkRBackendThread = new Thread("SparkR backend") {
    +      override def run() {
    +        sparkRBackendPort = sparkRBackend.init()
    +        initialized.release()
    +        sparkRBackend.run()
    +      }
    +    }
    +
    +    sparkRBackendThread.start()
    +    // Wait for RBackend initialization to finish
    +    if (initialized.tryAcquire(backendTimeout, TimeUnit.SECONDS)) {
    +      // Launch R
    +      val returnCode = try {
    +        val builder = new ProcessBuilder(Seq(rCommand, rFileNormalized) ++ 
otherArgs)
    +        val env = builder.environment()
    +        env.put("EXISTING_SPARKR_BACKEND_PORT", sparkRBackendPort.toString)
    +        val sparkHome = System.getenv("SPARK_HOME")
    +        env.put("R_PROFILE_USER",
    +          Seq(sparkHome, "R", "lib", "SparkR", "profile", 
"general.R").mkString(File.separator))
    +        builder.redirectErrorStream(true) // Ugly but needed for stdout 
and stderr to synchronize
    +        val process = builder.start()
    +
    +        new RedirectThread(process.getInputStream, System.out, "redirect 
output").start()
    +
    +        process.waitFor()
    +      } finally {
    +        sparkRBackend.close()
    +      }
    +      System.exit(returnCode)
    +    } else {
    +      System.err.println("SparkR backend did not initialize in " + 
backendTimeout + " seconds")
    +      System.exit(-1)
    +    }
    +  }
    +
    +  private class RedirectThread(
    --- End diff --
    
    This class is also now in `Utils`.


---
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