17

我正在尝试将带有 Spark 作业的 JAR 从 Java 代码提交到 YARN 集群中。我正在使用 SparkLauncher 提交 SparkPi 示例:

Process spark = new SparkLauncher()
    .setAppResource("C:\\spark-1.4.1-bin-hadoop2.6\\lib\\spark-examples-1.4.1-hadoop2.6.0.jar")
    .setMainClass("org.apache.spark.examples.SparkPi")
    .setMaster("yarn-cluster")
    .launch();
System.out.println("Waiting for finish...");
int exitCode = spark.waitFor();
System.out.println("Finished! Exit code:" + exitCode);

有两个问题:

  1. 在“yarn-cluster”模式下提交时,应用程序成功提交到 YARN 并成功执行(它在 YARN UI 中可见,报告为 SUCCESS 并在输出中打印 pi)。但是,提交应用程序永远不会收到处理完成的通知 - 在打印“Waiting to finish...”后它会无限挂起。容器的日志可以在这里找到
  2. 在“yarn-client”模式下提交时,应用程序没有出现在 YARN UI 中,提交应用程序在“Waiting to finish...”处挂起。当挂起代码被杀死时,应用程序显示在 YARN UI 中并报告为SUCCESS,但输出为空(pi 没有打印出来)。容器的日志可以在这里找到

我尝试使用 Oracle Java 7 和 8 执行提交应用程序。

4

3 回答 3

17

我在 Spark 邮件列表中获得了帮助。关键是在Process上读取/清除getInputStream和getErrorStream()。子进程可能会填满缓冲区并导致死锁 - 请参阅有关 Process 的 Oracle 文档。流应该在单独的线程中读取:

Process spark = new SparkLauncher()
    .setSparkHome("C:\\spark-1.4.1-bin-hadoop2.6")
    .setAppResource("C:\\spark-1.4.1-bin-hadoop2.6\\lib\\spark-examples-1.4.1-hadoop2.6.0.jar")
    .setMainClass("org.apache.spark.examples.SparkPi").setMaster("yarn-cluster").launch();

InputStreamReaderRunnable inputStreamReaderRunnable = new InputStreamReaderRunnable(spark.getInputStream(), "input");
Thread inputThread = new Thread(inputStreamReaderRunnable, "LogStreamReader input");
inputThread.start();

InputStreamReaderRunnable errorStreamReaderRunnable = new InputStreamReaderRunnable(spark.getErrorStream(), "error");
Thread errorThread = new Thread(errorStreamReaderRunnable, "LogStreamReader error");
errorThread.start();

System.out.println("Waiting for finish...");
int exitCode = spark.waitFor();
System.out.println("Finished! Exit code:" + exitCode);

其中 InputStreamReaderRunnable 类是:

public class InputStreamReaderRunnable implements Runnable {

    private BufferedReader reader;

    private String name;

    public InputStreamReaderRunnable(InputStream is, String name) {
        this.reader = new BufferedReader(new InputStreamReader(is));
        this.name = name;
    }

    public void run() {
        System.out.println("InputStream " + name + ":");
        try {
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
于 2015-08-03T08:17:44.500 回答
9

由于这是一篇旧帖子,我想添加一个更新,可能对后来读过这篇文章的人有所帮助。在 Spark 1.6.0 中,SparkLauncher 类中添加了一些功能。这是:

def startApplication(listeners: <repeated...>[Listener]): SparkAppHandle

http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.launcher.SparkLauncher

您可以运行应用程序而无需额外的线程来处理 stdout 和 stderr 处理毛绒有一个很好的应用程序运行状态报告。使用此代码:

  val env = Map(
      "HADOOP_CONF_DIR" -> hadoopConfDir,
      "YARN_CONF_DIR" -> yarnConfDir
    )
  val handler = new SparkLauncher(env.asJava)
      .setSparkHome(sparkHome)
      .setAppResource("Jar/location/.jar")
      .setMainClass("path.to.the.main.class")
      .setMaster("yarn-client")
      .setConf("spark.app.id", "AppID if you have one")
      .setConf("spark.driver.memory", "8g")
      .setConf("spark.akka.frameSize", "200")
      .setConf("spark.executor.memory", "2g")
      .setConf("spark.executor.instances", "32")
      .setConf("spark.executor.cores", "32")
      .setConf("spark.default.parallelism", "100")
      .setConf("spark.driver.allowMultipleContexts","true")
      .setVerbose(true)
      .startApplication()
println(handle.getAppId)
println(handle.getState)

如果 spark 应用程序成功,您可以继续查询状态。有关 Spark Launcher 服务器在 1.6.0 中如何工作的信息。看到这个链接: https ://github.com/apache/spark/blob/v1.6.0/launcher/src/main/java/org/apache/spark/launcher/LauncherServer.java

于 2016-03-24T11:12:46.817 回答
4

我使用 CountDownLatch 实现,它按预期工作。这适用于 SparkLauncher 版本 2.0.1,它也适用于 Yarn-cluster 模式。

    ...
final CountDownLatch countDownLatch = new CountDownLatch(1);
SparkAppListener sparkAppListener = new SparkAppListener(countDownLatch);
SparkAppHandle appHandle = sparkLauncher.startApplication(sparkAppListener);
Thread sparkAppListenerThread = new Thread(sparkAppListener);
sparkAppListenerThread.start();
long timeout = 120;
countDownLatch.await(timeout, TimeUnit.SECONDS);    
    ...

private static class SparkAppListener implements SparkAppHandle.Listener, Runnable {
    private static final Log log = LogFactory.getLog(SparkAppListener.class);
    private final CountDownLatch countDownLatch;
    public SparkAppListener(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }
    @Override
    public void stateChanged(SparkAppHandle handle) {
        String sparkAppId = handle.getAppId();
        State appState = handle.getState();
        if (sparkAppId != null) {
            log.info("Spark job with app id: " + sparkAppId + ",\t State changed to: " + appState + " - "
                    + SPARK_STATE_MSG.get(appState));
        } else {
            log.info("Spark job's state changed to: " + appState + " - " + SPARK_STATE_MSG.get(appState));
        }
        if (appState != null && appState.isFinal()) {
            countDownLatch.countDown();
        }
    }
    @Override
    public void infoChanged(SparkAppHandle handle) {}
    @Override
    public void run() {}
}
于 2016-10-20T20:36:03.623 回答