Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.launcher;
0019 
0020 import java.io.IOException;
0021 import java.util.ArrayList;
0022 import java.util.List;
0023 import java.util.Map;
0024 
0025 import static org.apache.spark.launcher.CommandBuilderUtils.*;
0026 
0027 /**
0028  * Command builder for internal Spark classes.
0029  * <p>
0030  * This class handles building the command to launch all internal Spark classes except for
0031  * SparkSubmit (which is handled by {@link SparkSubmitCommandBuilder} class.
0032  */
0033 class SparkClassCommandBuilder extends AbstractCommandBuilder {
0034 
0035   private final String className;
0036   private final List<String> classArgs;
0037 
0038   SparkClassCommandBuilder(String className, List<String> classArgs) {
0039     this.className = className;
0040     this.classArgs = classArgs;
0041   }
0042 
0043   @Override
0044   public List<String> buildCommand(Map<String, String> env)
0045       throws IOException, IllegalArgumentException {
0046     List<String> javaOptsKeys = new ArrayList<>();
0047     String memKey = null;
0048     String extraClassPath = null;
0049 
0050     // Master, Worker, HistoryServer, ExternalShuffleService, MesosClusterDispatcher use
0051     // SPARK_DAEMON_JAVA_OPTS (and specific opts) + SPARK_DAEMON_MEMORY.
0052     switch (className) {
0053       case "org.apache.spark.deploy.master.Master":
0054         javaOptsKeys.add("SPARK_DAEMON_JAVA_OPTS");
0055         javaOptsKeys.add("SPARK_MASTER_OPTS");
0056         extraClassPath = getenv("SPARK_DAEMON_CLASSPATH");
0057         memKey = "SPARK_DAEMON_MEMORY";
0058         break;
0059       case "org.apache.spark.deploy.worker.Worker":
0060         javaOptsKeys.add("SPARK_DAEMON_JAVA_OPTS");
0061         javaOptsKeys.add("SPARK_WORKER_OPTS");
0062         extraClassPath = getenv("SPARK_DAEMON_CLASSPATH");
0063         memKey = "SPARK_DAEMON_MEMORY";
0064         break;
0065       case "org.apache.spark.deploy.history.HistoryServer":
0066         javaOptsKeys.add("SPARK_DAEMON_JAVA_OPTS");
0067         javaOptsKeys.add("SPARK_HISTORY_OPTS");
0068         extraClassPath = getenv("SPARK_DAEMON_CLASSPATH");
0069         memKey = "SPARK_DAEMON_MEMORY";
0070         break;
0071       case "org.apache.spark.executor.CoarseGrainedExecutorBackend":
0072         javaOptsKeys.add("SPARK_EXECUTOR_OPTS");
0073         memKey = "SPARK_EXECUTOR_MEMORY";
0074         extraClassPath = getenv("SPARK_EXECUTOR_CLASSPATH");
0075         break;
0076       case "org.apache.spark.executor.MesosExecutorBackend":
0077         javaOptsKeys.add("SPARK_EXECUTOR_OPTS");
0078         memKey = "SPARK_EXECUTOR_MEMORY";
0079         extraClassPath = getenv("SPARK_EXECUTOR_CLASSPATH");
0080         break;
0081       case "org.apache.spark.deploy.mesos.MesosClusterDispatcher":
0082         javaOptsKeys.add("SPARK_DAEMON_JAVA_OPTS");
0083         extraClassPath = getenv("SPARK_DAEMON_CLASSPATH");
0084         memKey = "SPARK_DAEMON_MEMORY";
0085         break;
0086       case "org.apache.spark.deploy.ExternalShuffleService":
0087       case "org.apache.spark.deploy.mesos.MesosExternalShuffleService":
0088         javaOptsKeys.add("SPARK_DAEMON_JAVA_OPTS");
0089         javaOptsKeys.add("SPARK_SHUFFLE_OPTS");
0090         extraClassPath = getenv("SPARK_DAEMON_CLASSPATH");
0091         memKey = "SPARK_DAEMON_MEMORY";
0092         break;
0093       default:
0094         memKey = "SPARK_DRIVER_MEMORY";
0095         break;
0096     }
0097 
0098     List<String> cmd = buildJavaCommand(extraClassPath);
0099 
0100     for (String key : javaOptsKeys) {
0101       String envValue = System.getenv(key);
0102       if (!isEmpty(envValue) && envValue.contains("Xmx")) {
0103         String msg = String.format("%s is not allowed to specify max heap(Xmx) memory settings " +
0104                 "(was %s). Use the corresponding configuration instead.", key, envValue);
0105         throw new IllegalArgumentException(msg);
0106       }
0107       addOptionString(cmd, envValue);
0108     }
0109 
0110     String mem = firstNonEmpty(memKey != null ? System.getenv(memKey) : null, DEFAULT_MEM);
0111     cmd.add("-Xmx" + mem);
0112     cmd.add(className);
0113     cmd.addAll(classArgs);
0114     return cmd;
0115   }
0116 
0117 }