This project has retired. For details please refer to its
Attic page.
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 package org.apache.oozie.action.hadoop;
019
020 import java.io.IOException;
021 import java.io.StringReader;
022 import java.util.List;
023
024 import org.apache.hadoop.conf.Configuration;
025 import org.apache.hadoop.fs.Path;
026 import org.apache.hadoop.mapred.Counters;
027 import org.apache.hadoop.mapred.JobClient;
028 import org.apache.hadoop.mapred.JobConf;
029 import org.apache.hadoop.mapred.JobID;
030 import org.apache.hadoop.mapred.RunningJob;
031 import org.apache.oozie.action.ActionExecutorException;
032 import org.apache.oozie.client.WorkflowAction;
033 import org.apache.oozie.util.XConfiguration;
034 import org.apache.oozie.util.XLog;
035 import org.apache.oozie.util.XmlUtils;
036 import org.jdom.Element;
037 import org.jdom.Namespace;
038 import org.json.simple.JSONObject;
039
040 public class MapReduceActionExecutor extends JavaActionExecutor {
041
042 public static final String OOZIE_ACTION_EXTERNAL_STATS_WRITE = "oozie.action.external.stats.write";
043 public static final String HADOOP_COUNTERS = "hadoop.counters";
044 private XLog log = XLog.getLog(getClass());
045
046 public MapReduceActionExecutor() {
047 super("map-reduce");
048 }
049
050 @Override
051 protected List<Class> getLauncherClasses() {
052 List<Class> classes = super.getLauncherClasses();
053 classes.add(LauncherMain.class);
054 classes.add(MapReduceMain.class);
055 classes.add(StreamingMain.class);
056 classes.add(PipesMain.class);
057 return classes;
058 }
059
060 @Override
061 protected String getLauncherMain(Configuration launcherConf, Element actionXml) {
062 String mainClass;
063 Namespace ns = actionXml.getNamespace();
064 if (actionXml.getChild("streaming", ns) != null) {
065 mainClass = launcherConf.get(LauncherMapper.CONF_OOZIE_ACTION_MAIN_CLASS, StreamingMain.class.getName());
066 }
067 else {
068 if (actionXml.getChild("pipes", ns) != null) {
069 mainClass = launcherConf.get(LauncherMapper.CONF_OOZIE_ACTION_MAIN_CLASS, PipesMain.class.getName());
070 }
071 else {
072 mainClass = launcherConf.get(LauncherMapper.CONF_OOZIE_ACTION_MAIN_CLASS, MapReduceMain.class.getName());
073 }
074 }
075 return mainClass;
076 }
077
078 @Override
079 Configuration setupLauncherConf(Configuration conf, Element actionXml, Path appPath, Context context) throws ActionExecutorException {
080 super.setupLauncherConf(conf, actionXml, appPath, context);
081 conf.setBoolean("mapreduce.job.complete.cancel.delegation.tokens", true);
082 return conf;
083 }
084
085 @Override
086 @SuppressWarnings("unchecked")
087 Configuration setupActionConf(Configuration actionConf, Context context, Element actionXml, Path appPath)
088 throws ActionExecutorException {
089 Namespace ns = actionXml.getNamespace();
090 if (actionXml.getChild("streaming", ns) != null) {
091 Element streamingXml = actionXml.getChild("streaming", ns);
092 String mapper = streamingXml.getChildTextTrim("mapper", ns);
093 String reducer = streamingXml.getChildTextTrim("reducer", ns);
094 String recordReader = streamingXml.getChildTextTrim("record-reader", ns);
095 List<Element> list = (List<Element>) streamingXml.getChildren("record-reader-mapping", ns);
096 String[] recordReaderMapping = new String[list.size()];
097 for (int i = 0; i < list.size(); i++) {
098 recordReaderMapping[i] = list.get(i).getTextTrim();
099 }
100 list = (List<Element>) streamingXml.getChildren("env", ns);
101 String[] env = new String[list.size()];
102 for (int i = 0; i < list.size(); i++) {
103 env[i] = list.get(i).getTextTrim();
104 }
105 StreamingMain.setStreaming(actionConf, mapper, reducer, recordReader, recordReaderMapping, env);
106 }
107 else {
108 if (actionXml.getChild("pipes", ns) != null) {
109 Element pipesXml = actionXml.getChild("pipes", ns);
110 String map = pipesXml.getChildTextTrim("map", ns);
111 String reduce = pipesXml.getChildTextTrim("reduce", ns);
112 String inputFormat = pipesXml.getChildTextTrim("inputformat", ns);
113 String partitioner = pipesXml.getChildTextTrim("partitioner", ns);
114 String writer = pipesXml.getChildTextTrim("writer", ns);
115 String program = pipesXml.getChildTextTrim("program", ns);
116 PipesMain.setPipes(actionConf, map, reduce, inputFormat, partitioner, writer, program, appPath);
117 }
118 }
119 actionConf = super.setupActionConf(actionConf, context, actionXml, appPath);
120 return actionConf;
121 }
122
123 @Override
124 public void end(Context context, WorkflowAction action) throws ActionExecutorException {
125 super.end(context, action);
126 JobClient jobClient = null;
127 boolean exception = false;
128 try {
129 if (action.getStatus() == WorkflowAction.Status.OK) {
130 Element actionXml = XmlUtils.parseXml(action.getConf());
131 JobConf jobConf = createBaseHadoopConf(context, actionXml);
132 jobClient = createJobClient(context, jobConf);
133 RunningJob runningJob = jobClient.getJob(JobID.forName(action.getExternalId()));
134 if (runningJob == null) {
135 throw new ActionExecutorException(ActionExecutorException.ErrorType.FAILED, "MR002",
136 "Unknown hadoop job [{0}] associated with action [{1}]. Failing this action!", action
137 .getExternalId(), action.getId());
138 }
139
140 // TODO this has to be done in a better way
141 if (!runningJob.getJobName().startsWith("oozie:action:")) {
142 throw new ActionExecutorException(ActionExecutorException.ErrorType.FAILED, "MR001",
143 "ID swap should have happened in launcher job [{0}]", action.getExternalId());
144 }
145
146 Counters counters = runningJob.getCounters();
147 if (counters != null) {
148 ActionStats stats = new MRStats(counters);
149 String statsJsonString = stats.toJSON();
150 context.setVar(HADOOP_COUNTERS, statsJsonString);
151
152 // If action stats write property is set to false by user or
153 // size of stats is greater than the maximum allowed size,
154 // do not store the action stats
155 if (Boolean.parseBoolean(evaluateConfigurationProperty(actionXml,
156 OOZIE_ACTION_EXTERNAL_STATS_WRITE, "false"))
157 && (statsJsonString.getBytes().length <= getMaxExternalStatsSize())) {
158 context.setExecutionStats(statsJsonString);
159 log.debug(
160 "Printing stats for Map-Reduce action as a JSON string : [{0}]" + statsJsonString);
161 }
162 }
163 else {
164 context.setVar(HADOOP_COUNTERS, "");
165 XLog.getLog(getClass()).warn("Could not find Hadoop Counters for: [{0}]", action.getExternalId());
166 }
167 }
168 }
169 catch (Exception ex) {
170 exception = true;
171 throw convertException(ex);
172 }
173 finally {
174 if (jobClient != null) {
175 try {
176 jobClient.close();
177 }
178 catch (Exception e) {
179 if (exception) {
180 log.error("JobClient error: ", e);
181 }
182 else {
183 throw convertException(e);
184 }
185 }
186 }
187 }
188 }
189
190 // Return the value of the specified configuration property
191 private String evaluateConfigurationProperty(Element actionConf, String key, String defaultValue) throws ActionExecutorException {
192 try {
193 if (actionConf != null) {
194 Namespace ns = actionConf.getNamespace();
195 Element e = actionConf.getChild("configuration", ns);
196 String strConf = XmlUtils.prettyPrint(e).toString();
197 XConfiguration inlineConf = new XConfiguration(new StringReader(strConf));
198 return inlineConf.get(key, defaultValue);
199 }
200 return "";
201 }
202 catch (IOException ex) {
203 throw convertException(ex);
204 }
205 }
206
207 @SuppressWarnings("unchecked")
208 private JSONObject counterstoJson(Counters counters) {
209
210 if (counters == null) {
211 return null;
212 }
213
214 JSONObject groups = new JSONObject();
215 for (String gName : counters.getGroupNames()) {
216 JSONObject group = new JSONObject();
217 for (Counters.Counter counter : counters.getGroup(gName)) {
218 String cName = counter.getName();
219 Long cValue = counter.getCounter();
220 group.put(cName, cValue);
221 }
222 groups.put(gName, group);
223 }
224 return groups;
225 }
226
227 /**
228 * Return the sharelib name for the action.
229 *
230 * @return returns <code>streaming</code> if mapreduce-streaming action, <code>NULL</code> otherwise.
231 * @param actionXml
232 */
233 @Override
234 protected String getDefaultShareLibName(Element actionXml) {
235 Namespace ns = actionXml.getNamespace();
236 return (actionXml.getChild("streaming", ns) != null) ? "mapreduce-streaming" : null;
237 }
238
239 }