001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.action.hadoop;
020
021import com.google.common.base.Strings;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.Path;
024import org.apache.oozie.action.ActionExecutorException;
025import org.apache.oozie.service.ConfigurationService;
026import org.apache.oozie.service.Services;
027import org.apache.oozie.service.SparkConfigurationService;
028import org.jdom.Element;
029import org.jdom.Namespace;
030
031import java.io.IOException;
032import java.io.StringWriter;
033import java.util.ArrayList;
034import java.util.List;
035import java.util.Map;
036import java.util.Properties;
037
038public class SparkActionExecutor extends JavaActionExecutor {
039    public static final String SPARK_MAIN_CLASS_NAME = "org.apache.oozie.action.hadoop.SparkMain";
040    public static final String SPARK_MASTER = "oozie.spark.master";
041    public static final String SPARK_MODE = "oozie.spark.mode";
042    public static final String SPARK_OPTS = "oozie.spark.spark-opts";
043    public static final String SPARK_DEFAULT_OPTS = "oozie.spark.spark-default-opts";
044    public static final String SPARK_JOB_NAME = "oozie.spark.name";
045    public static final String SPARK_CLASS = "oozie.spark.class";
046    public static final String SPARK_JAR = "oozie.spark.jar";
047    public static final String MAPRED_CHILD_ENV = "mapred.child.env";
048    private static final String CONF_OOZIE_SPARK_SETUP_HADOOP_CONF_DIR = "oozie.action.spark.setup.hadoop.conf.dir";
049
050    public SparkActionExecutor() {
051        super("spark");
052    }
053
054    @Override
055    Configuration setupActionConf(Configuration actionConf, Context context, Element actionXml, Path appPath)
056            throws ActionExecutorException {
057        actionConf = super.setupActionConf(actionConf, context, actionXml, appPath);
058        Namespace ns = actionXml.getNamespace();
059
060        String master = actionXml.getChildTextTrim("master", ns);
061        actionConf.set(SPARK_MASTER, master);
062
063        String mode = actionXml.getChildTextTrim("mode", ns);
064        if (mode != null) {
065            actionConf.set(SPARK_MODE, mode);
066        }
067
068        String jobName = actionXml.getChildTextTrim("name", ns);
069        actionConf.set(SPARK_JOB_NAME, jobName);
070
071        String sparkClass = actionXml.getChildTextTrim("class", ns);
072        if (sparkClass != null) {
073            actionConf.set(SPARK_CLASS, sparkClass);
074        }
075
076        String jarLocation = actionXml.getChildTextTrim("jar", ns);
077        actionConf.set(SPARK_JAR, jarLocation);
078
079        if (master.startsWith("yarn")) {
080            String resourceManager = actionConf.get(HADOOP_YARN_RM);
081            Properties sparkConfig =
082                    Services.get().get(SparkConfigurationService.class).getSparkConfig(resourceManager);
083            if (!sparkConfig.isEmpty()) {
084                try (final StringWriter sw = new StringWriter()) {
085                    sparkConfig.store(sw, "Generated by Oozie server SparkActionExecutor");
086                    actionConf.set(SPARK_DEFAULT_OPTS, sw.toString());
087                } catch (IOException e) {
088                    LOG.warn("Could not propagate Spark default configuration!", e);
089                }
090            }
091        }
092        String sparkOpts = actionXml.getChildTextTrim("spark-opts", ns);
093        if (!Strings.isNullOrEmpty(sparkOpts)) {
094            // CLOUDERA-BUILD: Tell Spark not to localize Hadoop Configs to fix regression on kerberized cluster (CDH-32176)
095            sparkOpts += master.startsWith("yarn") ? " --conf spark.yarn.localizeConfig=false" : "";
096            actionConf.set(SPARK_OPTS, sparkOpts.toString().trim());
097        } else if (master.startsWith("yarn")) {
098            // CLOUDERA-BUILD: Tell Spark not to localize Hadoop Configs to fix regression on kerberized cluster (CDH-32176)
099            actionConf.set(SPARK_OPTS, "--conf spark.yarn.localizeConfig=false");
100        }
101
102        // Setting if SparkMain should setup hadoop config *-site.xml
103        boolean setupHadoopConf = actionConf.getBoolean(CONF_OOZIE_SPARK_SETUP_HADOOP_CONF_DIR,
104                ConfigurationService.getBoolean(CONF_OOZIE_SPARK_SETUP_HADOOP_CONF_DIR));
105        actionConf.setBoolean(CONF_OOZIE_SPARK_SETUP_HADOOP_CONF_DIR, setupHadoopConf);
106        return actionConf;
107    }
108
109    @Override
110    Configuration setupLauncherConf(Configuration conf, Element actionXml, Path appPath, Context context)
111            throws ActionExecutorException {
112        super.setupLauncherConf(conf, actionXml, appPath, context);
113
114        // Set SPARK_HOME environment variable on launcher job
115        // It is needed since pyspark client checks for it.
116        String sparkHome = "SPARK_HOME=.";
117        String mapredChildEnv = conf.get("oozie.launcher." + MAPRED_CHILD_ENV);
118
119        if (mapredChildEnv == null) {
120            conf.set(MAPRED_CHILD_ENV, sparkHome);
121            conf.set("oozie.launcher." + MAPRED_CHILD_ENV, sparkHome);
122        } else if (!mapredChildEnv.contains("SPARK_HOME")) {
123            conf.set(MAPRED_CHILD_ENV, mapredChildEnv + "," + sparkHome);
124            conf.set("oozie.launcher." + MAPRED_CHILD_ENV, mapredChildEnv + "," + sparkHome);
125        }
126        return conf;
127    }
128
129    @Override
130    public List<Class<?>> getLauncherClasses() {
131        List<Class<?>> classes = new ArrayList<Class<?>>();
132        try {
133            classes.add(Class.forName(SPARK_MAIN_CLASS_NAME));
134        } catch (ClassNotFoundException e) {
135            throw new RuntimeException("Class not found", e);
136        }
137        return classes;
138    }
139
140
141    /**
142     * Return the sharelib name for the action.
143     *
144     * @param actionXml
145     * @return returns <code>spark</code>.
146     */
147    @Override
148    protected String getDefaultShareLibName(Element actionXml) {
149        return "spark";
150    }
151
152    @Override
153    protected void addActionSpecificEnvVars(Map<String, String> env) {
154        env.put("SPARK_HOME", ".");
155    }
156
157    @Override
158    protected String getLauncherMain(Configuration launcherConf, Element actionXml) {
159        return launcherConf.get(LauncherAMUtils.CONF_OOZIE_ACTION_MAIN_CLASS, SPARK_MAIN_CLASS_NAME);
160    }
161
162    @Override
163    public String[] getShareLibFilesForActionConf() {
164        return new String[] { "hive-site.xml" };
165    }
166
167}