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 java.io.BufferedReader; 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.InputStreamReader; 025import java.math.BigInteger; 026import java.security.MessageDigest; 027import java.security.NoSuchAlgorithmException; 028import java.security.PrivilegedExceptionAction; 029import java.util.ArrayList; 030import java.util.Collection; 031import java.util.HashMap; 032import java.util.List; 033import java.util.Map; 034import java.util.Properties; 035 036import org.apache.hadoop.conf.Configuration; 037import org.apache.hadoop.fs.CommonConfigurationKeys; 038import org.apache.hadoop.fs.FileSystem; 039import org.apache.hadoop.fs.Path; 040import org.apache.hadoop.io.SequenceFile; 041import org.apache.hadoop.io.Text; 042import org.apache.hadoop.mapred.Counters; 043import org.apache.hadoop.io.serializer.WritableSerialization; 044import org.apache.hadoop.mapred.JobConf; 045import org.apache.hadoop.mapred.RunningJob; 046import org.apache.hadoop.security.UserGroupInformation; 047import org.apache.oozie.client.OozieClient; 048import org.apache.oozie.client.WorkflowAction; 049import org.apache.oozie.service.HadoopAccessorException; 050import org.apache.oozie.service.HadoopAccessorService; 051import org.apache.oozie.service.Services; 052import org.apache.oozie.service.URIHandlerService; 053import org.apache.oozie.service.UserGroupInformationService; 054import org.apache.oozie.util.IOUtils; 055import org.apache.oozie.util.PropertiesUtils; 056 057public class LauncherHelper { 058 059 public static final String OOZIE_ACTION_YARN_TAG = "oozie.action.yarn.tag"; 060 061 public static String getRecoveryId(Configuration launcherConf, Path actionDir, String recoveryId) 062 throws HadoopAccessorException, IOException { 063 String jobId = null; 064 Path recoveryFile = new Path(actionDir, recoveryId); 065 FileSystem fs = Services.get().get(HadoopAccessorService.class) 066 .createFileSystem(launcherConf.get("user.name"),recoveryFile.toUri(), launcherConf); 067 068 if (fs.exists(recoveryFile)) { 069 InputStream is = fs.open(recoveryFile); 070 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 071 jobId = reader.readLine(); 072 reader.close(); 073 } 074 return jobId; 075 076 } 077 078 public static void setupMainClass(Configuration launcherConf, String javaMainClass) { 079 // Only set the javaMainClass if its not null or empty string, this way the user can override the action's main class via 080 // <configuration> property 081 if (javaMainClass != null && !javaMainClass.equals("")) { 082 launcherConf.set(LauncherAMUtils.CONF_OOZIE_ACTION_MAIN_CLASS, javaMainClass); 083 } 084 } 085 086 public static void setupLauncherURIHandlerConf(Configuration launcherConf) { 087 for(Map.Entry<String, String> entry : Services.get().get(URIHandlerService.class).getLauncherConfig()) { 088 launcherConf.set(entry.getKey(), entry.getValue()); 089 } 090 } 091 092 public static void setupMainArguments(Configuration launcherConf, String[] args) { 093 launcherConf.setInt(LauncherAMUtils.CONF_OOZIE_ACTION_MAIN_ARG_COUNT, args.length); 094 for (int i = 0; i < args.length; i++) { 095 launcherConf.set(LauncherAMUtils.CONF_OOZIE_ACTION_MAIN_ARG_PREFIX + i, args[i]); 096 } 097 } 098 099 public static void setupMaxOutputData(Configuration launcherConf, int maxOutputData) { 100 launcherConf.setInt(LauncherAMUtils.CONF_OOZIE_ACTION_MAX_OUTPUT_DATA, maxOutputData); 101 } 102 103 /** 104 * Set the maximum value of stats data 105 * 106 * @param launcherConf the oozie launcher configuration 107 * @param maxStatsData the maximum allowed size of stats data 108 */ 109 public static void setupMaxExternalStatsSize(Configuration launcherConf, int maxStatsData){ 110 launcherConf.setInt(LauncherAMUtils.CONF_OOZIE_EXTERNAL_STATS_MAX_SIZE, maxStatsData); 111 } 112 113 /** 114 * Set the maximum number of globbed files/dirs 115 * 116 * @param launcherConf the oozie launcher configuration 117 * @param fsGlobMax the maximum number of files/dirs for FS operation 118 */ 119 public static void setupMaxFSGlob(Configuration launcherConf, int fsGlobMax){ 120 launcherConf.setInt(LauncherAMUtils.CONF_OOZIE_ACTION_FS_GLOB_MAX, fsGlobMax); 121 } 122 123 public static void setupLauncherInfo(Configuration launcherConf, String jobId, String actionId, Path actionDir, 124 String recoveryId, Configuration actionConf, String prepareXML) throws IOException, HadoopAccessorException { 125 126 launcherConf.set(LauncherAMUtils.OOZIE_JOB_ID, jobId); 127 launcherConf.set(LauncherAMUtils.OOZIE_ACTION_ID, actionId); 128 launcherConf.set(LauncherAMUtils.OOZIE_ACTION_DIR_PATH, actionDir.toString()); 129 launcherConf.set(LauncherAMUtils.OOZIE_ACTION_RECOVERY_ID, recoveryId); 130 launcherConf.set(LauncherAMUtils.ACTION_PREPARE_XML, prepareXML); 131 132 actionConf.set(LauncherAMUtils.OOZIE_JOB_ID, jobId); 133 actionConf.set(LauncherAMUtils.OOZIE_ACTION_ID, actionId); 134 135 if (Services.get().getConf().getBoolean("oozie.hadoop-2.0.2-alpha.workaround.for.distributed.cache", false)) { 136 List<String> purgedEntries = new ArrayList<String>(); 137 Collection<String> entries = actionConf.getStringCollection("mapreduce.job.cache.files"); 138 for (String entry : entries) { 139 if (entry.contains("#")) { 140 purgedEntries.add(entry); 141 } 142 } 143 actionConf.setStrings("mapreduce.job.cache.files", purgedEntries.toArray(new String[purgedEntries.size()])); 144 launcherConf.setBoolean("oozie.hadoop-2.0.2-alpha.workaround.for.distributed.cache", true); 145 } 146 } 147 148 public static void setupYarnRestartHandling(Configuration launcherJobConf, Configuration actionConf, String launcherTag, 149 long launcherTime) 150 throws NoSuchAlgorithmException { 151 launcherJobConf.setLong(LauncherMain.OOZIE_JOB_LAUNCH_TIME, launcherTime); 152 // Tags are limited to 100 chars so we need to hash them to make sure (the actionId otherwise doesn't have a max length) 153 String tag = getTag(launcherTag); 154 // keeping the oozie.child.mapreduce.job.tags instead of mapreduce.job.tags to avoid killing launcher itself. 155 // mapreduce.job.tags should only go to child job launch by launcher. 156 actionConf.set(LauncherMain.CHILD_MAPREDUCE_JOB_TAGS, tag); 157 } 158 159 public static String getTag(String launcherTag) throws NoSuchAlgorithmException { 160 MessageDigest digest = MessageDigest.getInstance("MD5"); 161 digest.update(launcherTag.getBytes(), 0, launcherTag.length()); 162 String md5 = "oozie-" + new BigInteger(1, digest.digest()).toString(16); 163 return md5; 164 } 165 166 public static boolean isMainDone(RunningJob runningJob) throws IOException { 167 return runningJob.isComplete(); 168 } 169 170 public static boolean isMainSuccessful(RunningJob runningJob) throws IOException { 171 boolean succeeded = runningJob.isSuccessful(); 172 if (succeeded) { 173 Counters counters = runningJob.getCounters(); 174 if (counters != null) { 175 Counters.Group group = counters.getGroup(LauncherAMUtils.COUNTER_GROUP); 176 if (group != null) { 177 succeeded = group.getCounter(LauncherAMUtils.COUNTER_LAUNCHER_ERROR) == 0; 178 } 179 } 180 } 181 return succeeded; 182 } 183 184 /** 185 * Determine whether action has external child jobs or not 186 * @param actionData 187 * @return true/false 188 * @throws IOException 189 */ 190 public static boolean hasExternalChildJobs(Map<String, String> actionData) throws IOException { 191 return actionData.containsKey(LauncherAMUtils.ACTION_DATA_EXTERNAL_CHILD_IDS); 192 } 193 194 /** 195 * Determine whether action has output data or not 196 * @param actionData 197 * @return true/false 198 * @throws IOException 199 */ 200 public static boolean hasOutputData(Map<String, String> actionData) throws IOException { 201 return actionData.containsKey(LauncherAMUtils.ACTION_DATA_OUTPUT_PROPS); 202 } 203 204 /** 205 * Determine whether action has external stats or not 206 * @param actionData 207 * @return true/false 208 * @throws IOException 209 */ 210 public static boolean hasStatsData(Map<String, String> actionData) throws IOException{ 211 return actionData.containsKey(LauncherAMUtils.ACTION_DATA_STATS); 212 } 213 214 /** 215 * Determine whether action has new id (id swap) or not 216 * @param actionData 217 * @return true/false 218 * @throws IOException 219 */ 220 public static boolean hasIdSwap(Map<String, String> actionData) throws IOException { 221 return actionData.containsKey(LauncherAMUtils.ACTION_DATA_NEW_ID); 222 } 223 224 /** 225 * Get the sequence file path storing all action data 226 * @param actionDir 227 * @return Path returns sequence file path storing all action data 228 */ 229 public static Path getActionDataSequenceFilePath(Path actionDir) { 230 return new Path(actionDir, LauncherAMUtils.ACTION_DATA_SEQUENCE_FILE); 231 } 232 233 /** 234 * Utility function to load the contents of action data sequence file into 235 * memory object 236 * 237 * @param fs Action Filesystem 238 * @param actionDir Path 239 * @param conf Configuration 240 * @return Map action data 241 * @throws IOException 242 * @throws InterruptedException 243 */ 244 public static Map<String, String> getActionData(final FileSystem fs, final Path actionDir, final Configuration conf) 245 throws IOException, InterruptedException { 246 UserGroupInformationService ugiService = Services.get().get(UserGroupInformationService.class); 247 UserGroupInformation ugi = ugiService.getProxyUser(conf.get(OozieClient.USER_NAME)); 248 249 return ugi.doAs(new PrivilegedExceptionAction<Map<String, String>>() { 250 @Override 251 public Map<String, String> run() throws IOException { 252 Map<String, String> ret = new HashMap<String, String>(); 253 Path seqFilePath = getActionDataSequenceFilePath(actionDir); 254 if (fs.exists(seqFilePath)) { 255 conf.set(CommonConfigurationKeys.IO_SERIALIZATIONS_KEY, WritableSerialization.class.getCanonicalName()); 256 SequenceFile.Reader seqFile = new SequenceFile.Reader(fs, seqFilePath, conf); 257 Text key = new Text(), value = new Text(); 258 while (seqFile.next(key, value)) { 259 ret.put(key.toString(), value.toString()); 260 } 261 seqFile.close(); 262 } 263 else { // maintain backward-compatibility. to be deprecated 264 org.apache.hadoop.fs.FileStatus[] files = fs.listStatus(actionDir); 265 InputStream is; 266 BufferedReader reader = null; 267 Properties props; 268 if (files != null && files.length > 0) { 269 for (int x = 0; x < files.length; x++) { 270 Path file = files[x].getPath(); 271 if (file.equals(new Path(actionDir, "externalChildIds.properties"))) { 272 is = fs.open(file); 273 reader = new BufferedReader(new InputStreamReader(is)); 274 ret.put(LauncherAMUtils.ACTION_DATA_EXTERNAL_CHILD_IDS, 275 IOUtils.getReaderAsString(reader, -1)); 276 } 277 else if (file.equals(new Path(actionDir, "newId.properties"))) { 278 is = fs.open(file); 279 reader = new BufferedReader(new InputStreamReader(is)); 280 props = PropertiesUtils.readProperties(reader, -1); 281 ret.put(LauncherAMUtils.ACTION_DATA_NEW_ID, props.getProperty("id")); 282 } 283 else if (file.equals(new Path(actionDir, LauncherAMUtils.ACTION_DATA_OUTPUT_PROPS))) { 284 int maxOutputData = conf.getInt(LauncherAMUtils.CONF_OOZIE_ACTION_MAX_OUTPUT_DATA, 285 2 * 1024); 286 is = fs.open(file); 287 reader = new BufferedReader(new InputStreamReader(is)); 288 ret.put(LauncherAMUtils.ACTION_DATA_OUTPUT_PROPS, PropertiesUtils 289 .propertiesToString(PropertiesUtils.readProperties(reader, maxOutputData))); 290 } 291 else if (file.equals(new Path(actionDir, LauncherAMUtils.ACTION_DATA_STATS))) { 292 int statsMaxOutputData = conf.getInt(LauncherAMUtils.CONF_OOZIE_EXTERNAL_STATS_MAX_SIZE, 293 Integer.MAX_VALUE); 294 is = fs.open(file); 295 reader = new BufferedReader(new InputStreamReader(is)); 296 ret.put(LauncherAMUtils.ACTION_DATA_STATS, PropertiesUtils 297 .propertiesToString(PropertiesUtils.readProperties(reader, statsMaxOutputData))); 298 } 299 else if (file.equals(new Path(actionDir, LauncherAMUtils.ACTION_DATA_ERROR_PROPS))) { 300 is = fs.open(file); 301 reader = new BufferedReader(new InputStreamReader(is)); 302 ret.put(LauncherAMUtils.ACTION_DATA_ERROR_PROPS, IOUtils.getReaderAsString(reader, -1)); 303 } 304 } 305 } 306 } 307 return ret; 308 } 309 }); 310 } 311 312 public static String getActionYarnTag(Configuration conf, String parentId, WorkflowAction wfAction) { 313 String tag; 314 if ( conf != null && conf.get(OOZIE_ACTION_YARN_TAG) != null) { 315 tag = conf.get(OOZIE_ACTION_YARN_TAG) + "@" + wfAction.getName(); 316 } else if (parentId != null) { 317 tag = parentId + "@" + wfAction.getName(); 318 } else { 319 tag = wfAction.getId(); 320 } 321 return tag; 322 } 323 324}