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 org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.Path;
023import org.apache.hadoop.mapreduce.MRJobConfig;
024import org.apache.oozie.action.ActionExecutorException;
025import org.apache.oozie.service.ConfigurationService;
026import org.apache.oozie.util.StringUtils;
027import org.apache.oozie.util.XLog;
028import org.jdom.Element;
029
030import java.util.ArrayList;
031import java.util.List;
032
033public class DistcpActionExecutor extends JavaActionExecutor{
034    public static final String CONF_OOZIE_DISTCP_ACTION_MAIN_CLASS = "org.apache.oozie.action.hadoop.DistcpMain";
035    private static final String DISTCP_MAIN_CLASS_NAME = "org.apache.hadoop.tools.DistCp";
036    public static final String CLASS_NAMES = "oozie.actions.main.classnames";
037    private static final XLog LOG = XLog.getLog(DistcpActionExecutor.class);
038    public static final String DISTCP_TYPE = "distcp";
039
040    /**
041     * Comma separated list of NameNode hosts to obtain delegation token(s) for.
042     */
043    private static final String OOZIE_LAUNCHER_MAPREDUCE_JOB_HDFS_SERVERS = "oozie.launcher.mapreduce.job.hdfs-servers";
044
045    /**
046     * Comma separated list to instruct ResourceManagers on either cluster to skip delegation token renewal for NameNode hosts.
047     */
048    private static final String OOZIE_LAUNCHER_MAPREDUCE_JOB_HDFS_SERVERS_TOKEN_RENEWAL_EXCLUDE =
049            "oozie.launcher.mapreduce.job.hdfs-servers.token-renewal.exclude";
050    private static final String JOB_NAMENODES_TOKEN_RENEWAL_EXCLUDE = "mapreduce.job.hdfs-servers.token-renewal.exclude";
051
052
053    public DistcpActionExecutor() {
054        super("distcp");
055    }
056
057    @Override
058    Configuration setupActionConf(Configuration actionConf, Context context, Element actionXml, Path appPath)
059            throws ActionExecutorException {
060        actionConf = super.setupActionConf(actionConf, context, actionXml, appPath);
061        actionConf.set(JavaMain.JAVA_MAIN_CLASS, DISTCP_MAIN_CLASS_NAME);
062        return actionConf;
063    }
064
065    @Override
066    public List<Class<?>> getLauncherClasses() {
067        List<Class<?>> classes = new ArrayList<>();
068        try {
069            classes.add(Class.forName(CONF_OOZIE_DISTCP_ACTION_MAIN_CLASS));
070        }
071        catch (ClassNotFoundException e) {
072            throw new RuntimeException("Class not found", e);
073        }
074        return classes;
075    }
076
077    /**
078     * This function returns the Action classes names from the configuration
079     *
080     * @param type This is type of the action classes
081     * @return Name of the class from the configuration
082     */
083    public static String getClassNamebyType(String type){
084        String classname = null;
085        for (String function : ConfigurationService.getStrings(CLASS_NAMES)) {
086            function = StringUtils.trim(function);
087            LOG.debug("class for Distcp Action: " + function);
088            String[] str = function.split("=");
089            if (str.length > 0) {
090                if(type.equalsIgnoreCase(str[0])){
091                    classname = new String(str[1]);
092                }
093            }
094        }
095        return classname;
096    }
097
098    /**
099     * Return the sharelib name for the action.
100     *
101     * @return returns <code>distcp</code>.
102     * @param actionXml
103     */
104    @Override
105    protected String getDefaultShareLibName(Element actionXml) {
106        return "distcp";
107    }
108
109    @Override
110    protected String getLauncherMain(Configuration launcherConf, Element actionXml) {
111        return launcherConf.get(LauncherAMUtils.CONF_OOZIE_ACTION_MAIN_CLASS, CONF_OOZIE_DISTCP_ACTION_MAIN_CLASS);
112    }
113
114    /**
115     * Extracts information required for DistCp action between secure clusters (in the same or distinct Kerberos realms)
116     *
117     * @param jobconf workflow action configuration
118     */
119    @Override
120    protected void setActionTokenProperties(final Configuration jobconf) {
121        final String hdfsServers = jobconf.get(OOZIE_LAUNCHER_MAPREDUCE_JOB_HDFS_SERVERS);
122        if (hdfsServers != null) {
123            jobconf.set(MRJobConfig.JOB_NAMENODES, hdfsServers);
124            final String tokenRenewalExclude = jobconf.get(OOZIE_LAUNCHER_MAPREDUCE_JOB_HDFS_SERVERS_TOKEN_RENEWAL_EXCLUDE);
125            if (tokenRenewalExclude != null) {
126                jobconf.set(JOB_NAMENODES_TOKEN_RENEWAL_EXCLUDE, tokenRenewalExclude);
127            }
128        }
129    }
130}