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.command.wf;
020
021import org.apache.hadoop.conf.Configuration;
022import org.apache.oozie.service.WorkflowAppService;
023import org.jdom.Element;
024import org.jdom.Namespace;
025import org.apache.oozie.client.XOozieClient;
026import org.apache.oozie.command.CommandException;
027
028import java.util.HashMap;
029import java.util.HashSet;
030import java.util.Iterator;
031import java.util.Map;
032import java.util.Set;
033
034public class SubmitMRXCommand extends SubmitHttpXCommand {
035    private static final Set<String> SKIPPED_CONFS = new HashSet<String>();
036    private static final Map<String, String> DEPRECATE_MAP = new HashMap<String, String>();
037
038    public SubmitMRXCommand(Configuration conf) {
039        super("submitMR", "submitMR", conf);
040    }
041
042    static {
043        SKIPPED_CONFS.add(WorkflowAppService.HADOOP_USER);
044        SKIPPED_CONFS.add(XOozieClient.JT);
045        SKIPPED_CONFS.add(XOozieClient.NN);
046        // a brillant mind made a change in Configuration that 'fs.default.name' key gets converted to 'fs.defaultFS'
047        // in Hadoop 0.23, we need skip that one too, keeping the old one because of Hadoop 1
048        SKIPPED_CONFS.add(XOozieClient.NN_2);
049
050        DEPRECATE_MAP.put(XOozieClient.NN, XOozieClient.NN_2);
051        DEPRECATE_MAP.put(XOozieClient.JT, XOozieClient.JT_2);
052        DEPRECATE_MAP.put(WorkflowAppService.HADOOP_USER, "mapreduce.job.user.name");
053    }
054
055    @Override
056    protected Namespace getSectionNamespace(){
057        return Namespace.getNamespace("uri:oozie:workflow:0.2");
058    }
059
060    @Override
061    protected String getWorkflowName(){
062        return "mapreduce";
063    }
064
065    private Element generateConfigurationSection(Configuration conf, Namespace ns) {
066        Element configuration = null;
067        Iterator<Map.Entry<String, String>> iter = conf.iterator();
068        while (iter.hasNext()) {
069            Map.Entry<String, String> entry = iter.next();
070            String name = entry.getKey();
071            if (MANDATORY_OOZIE_CONFS.contains(name) || OPTIONAL_OOZIE_CONFS.contains(name)
072                    || SKIPPED_CONFS.contains(name)
073                    || DEPRECATE_MAP.containsValue(name)) {
074                continue;
075            }
076
077            if (configuration == null) {
078                configuration = new Element("configuration", ns);
079            }
080
081            String value = entry.getValue();
082            Element property = new Element("property", ns);
083            Element nameElement = new Element("name", ns);
084            nameElement.addContent(name != null ? name : "");
085            property.addContent(nameElement);
086            Element valueElement = new Element("value", ns);
087            valueElement.addContent(value != null ? value : "");
088            property.addContent(valueElement);
089            configuration.addContent(property);
090        }
091
092        return configuration;
093    }
094
095    @Override
096    protected Element generateSection(Configuration conf, Namespace ns) {
097        Element mapreduce = new Element("map-reduce", ns);
098        Element jt = new Element("job-tracker", ns);
099        String newJTVal = conf.get(DEPRECATE_MAP.get(XOozieClient.JT));
100        jt.addContent(newJTVal != null ? newJTVal : (conf.get(XOozieClient.JT)));
101        mapreduce.addContent(jt);
102        Element nn = new Element("name-node", ns);
103        String newNNVal = conf.get(DEPRECATE_MAP.get(XOozieClient.NN));
104        nn.addContent(newNNVal != null ? newNNVal : (conf.get(XOozieClient.NN)));
105        mapreduce.addContent(nn);
106
107        if (conf.size() > MANDATORY_OOZIE_CONFS.size()) { // excluding JT, NN,
108                                                          // LIBPATH
109            // configuration section
110            Element configuration = generateConfigurationSection(conf, ns);
111            if (configuration != null) {
112                mapreduce.addContent(configuration);
113            }
114
115            // file section
116            addFileSection(mapreduce, conf, ns);
117
118            // archive section
119            addArchiveSection(mapreduce, conf, ns);
120        }
121
122        return mapreduce;
123    }
124
125    @Override
126    protected void checkMandatoryConf(Configuration conf) {
127        for (String key : MANDATORY_OOZIE_CONFS) {
128            String value = conf.get(key);
129            if(value == null) {
130                if(DEPRECATE_MAP.containsKey(key)) {
131                    if(conf.get(DEPRECATE_MAP.get(key)) == null) {
132                        throw new RuntimeException(key + " or " + DEPRECATE_MAP.get(key) + " is not specified");
133                    }
134                }
135                else {
136                    throw new RuntimeException(key + " is not specified");
137                }
138            }
139        }
140    }
141
142    @Override
143    public String getEntityKey() {
144        return null;
145    }
146
147    @Override
148    protected boolean isLockRequired() {
149        return false;
150    }
151
152    @Override
153    protected void loadState() {
154
155    }
156
157    @Override
158    protected void verifyPrecondition() throws CommandException {
159
160    }
161}