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.hadoop.fs.Path; 023import org.apache.hadoop.fs.FileSystem; 024import org.apache.oozie.AppType; 025import org.apache.oozie.SLAEventBean; 026import org.apache.oozie.command.wf.ActionXCommand.ActionExecutorContext; 027import org.apache.oozie.WorkflowJobBean; 028import org.apache.oozie.ErrorCode; 029import org.apache.oozie.action.oozie.SubWorkflowActionExecutor; 030import org.apache.oozie.service.HadoopAccessorException; 031import org.apache.oozie.service.JPAService; 032import org.apache.oozie.service.UUIDService; 033import org.apache.oozie.service.WorkflowStoreService; 034import org.apache.oozie.service.WorkflowAppService; 035import org.apache.oozie.service.HadoopAccessorService; 036import org.apache.oozie.service.Services; 037import org.apache.oozie.service.DagXLogInfoService; 038import org.apache.oozie.util.ELUtils; 039import org.apache.oozie.util.LogUtils; 040import org.apache.oozie.sla.SLAOperations; 041import org.apache.oozie.util.XLog; 042import org.apache.oozie.util.ParamChecker; 043import org.apache.oozie.util.XConfiguration; 044import org.apache.oozie.util.XmlUtils; 045import org.apache.oozie.command.CommandException; 046import org.apache.oozie.executor.jpa.BatchQueryExecutor; 047import org.apache.oozie.executor.jpa.JPAExecutorException; 048import org.apache.oozie.service.ELService; 049import org.apache.oozie.store.StoreException; 050import org.apache.oozie.workflow.WorkflowApp; 051import org.apache.oozie.workflow.WorkflowException; 052import org.apache.oozie.workflow.WorkflowInstance; 053import org.apache.oozie.workflow.WorkflowLib; 054import org.apache.oozie.util.ELEvaluator; 055import org.apache.oozie.util.InstrumentUtils; 056import org.apache.oozie.util.PropertiesUtils; 057import org.apache.oozie.util.db.SLADbOperations; 058import org.apache.oozie.service.SchemaService.SchemaName; 059import org.apache.oozie.client.OozieClient; 060import org.apache.oozie.client.WorkflowJob; 061import org.apache.oozie.client.SLAEvent.SlaAppType; 062import org.apache.oozie.client.rest.JsonBean; 063import org.jdom.Element; 064import org.jdom.filter.ElementFilter; 065 066import java.util.ArrayList; 067import java.util.Date; 068import java.util.Iterator; 069import java.util.List; 070import java.util.Map; 071import java.util.Set; 072import java.util.HashSet; 073import java.io.IOException; 074import java.net.URI; 075 076@SuppressWarnings("deprecation") 077public class SubmitXCommand extends WorkflowXCommand<String> { 078 public static final String CONFIG_DEFAULT = "config-default.xml"; 079 080 private Configuration conf; 081 private List<JsonBean> insertList = new ArrayList<JsonBean>(); 082 private String parentId; 083 084 /** 085 * Constructor to create the workflow Submit Command. 086 * 087 * @param conf : Configuration for workflow job 088 */ 089 public SubmitXCommand(Configuration conf) { 090 super("submit", "submit", 1); 091 this.conf = ParamChecker.notNull(conf, "conf"); 092 } 093 094 /** 095 * Constructor for submitting wf through coordinator 096 * 097 * @param conf : Configuration for workflow job 098 * @param parentId the coord action id 099 */ 100 public SubmitXCommand(Configuration conf, String parentId) { 101 this(conf); 102 this.parentId = parentId; 103 } 104 105 /** 106 * Constructor to create the workflow Submit Command. 107 * 108 * @param dryrun : if dryrun 109 * @param conf : Configuration for workflow job 110 */ 111 public SubmitXCommand(boolean dryrun, Configuration conf) { 112 this(conf); 113 this.dryrun = dryrun; 114 } 115 116 private static final Set<String> DISALLOWED_USER_PROPERTIES = new HashSet<String>(); 117 118 static { 119 String[] badUserProps = {PropertiesUtils.DAYS, PropertiesUtils.HOURS, PropertiesUtils.MINUTES, 120 PropertiesUtils.KB, PropertiesUtils.MB, PropertiesUtils.GB, PropertiesUtils.TB, PropertiesUtils.PB, 121 PropertiesUtils.RECORDS, PropertiesUtils.MAP_IN, PropertiesUtils.MAP_OUT, PropertiesUtils.REDUCE_IN, 122 PropertiesUtils.REDUCE_OUT, PropertiesUtils.GROUPS}; 123 PropertiesUtils.createPropertySet(badUserProps, DISALLOWED_USER_PROPERTIES); 124 } 125 126 @Override 127 protected String execute() throws CommandException { 128 InstrumentUtils.incrJobCounter(getName(), 1, getInstrumentation()); 129 WorkflowAppService wps = Services.get().get(WorkflowAppService.class); 130 try { 131 XLog.Info.get().setParameter(DagXLogInfoService.TOKEN, conf.get(OozieClient.LOG_TOKEN)); 132 String user = conf.get(OozieClient.USER_NAME); 133 URI uri = new URI(conf.get(OozieClient.APP_PATH)); 134 HadoopAccessorService has = Services.get().get(HadoopAccessorService.class); 135 Configuration fsConf = has.createConfiguration(uri.getAuthority()); 136 FileSystem fs = has.createFileSystem(user, uri, fsConf); 137 138 Path configDefault = null; 139 Configuration defaultConf = null; 140 // app path could be a directory 141 Path path = new Path(uri.getPath()); 142 if (!fs.isFile(path)) { 143 configDefault = new Path(path, CONFIG_DEFAULT); 144 } else { 145 configDefault = new Path(path.getParent(), CONFIG_DEFAULT); 146 } 147 148 if (fs.exists(configDefault)) { 149 try { 150 defaultConf = new XConfiguration(fs.open(configDefault)); 151 PropertiesUtils.checkDisallowedProperties(defaultConf, DISALLOWED_USER_PROPERTIES); 152 PropertiesUtils.checkDefaultDisallowedProperties(defaultConf); 153 XConfiguration.injectDefaults(defaultConf, conf); 154 } 155 catch (IOException ex) { 156 throw new IOException("default configuration file, " + ex.getMessage(), ex); 157 } 158 } 159 if (defaultConf != null) { 160 defaultConf = resolveDefaultConfVariables(defaultConf); 161 } 162 163 WorkflowApp app = wps.parseDef(conf, defaultConf); 164 XConfiguration protoActionConf = wps.createProtoActionConf(conf, true); 165 WorkflowLib workflowLib = Services.get().get(WorkflowStoreService.class).getWorkflowLibWithNoDB(); 166 167 PropertiesUtils.checkDisallowedProperties(conf, DISALLOWED_USER_PROPERTIES); 168 169 // Resolving all variables in the job properties. 170 // This ensures the Hadoop Configuration semantics is preserved. 171 XConfiguration resolvedVarsConf = new XConfiguration(); 172 for (Map.Entry<String, String> entry : conf) { 173 resolvedVarsConf.set(entry.getKey(), conf.get(entry.getKey())); 174 } 175 conf = resolvedVarsConf; 176 177 WorkflowInstance wfInstance; 178 try { 179 wfInstance = workflowLib.createInstance(app, conf); 180 } 181 catch (WorkflowException e) { 182 throw new StoreException(e); 183 } 184 185 Configuration conf = wfInstance.getConf(); 186 // System.out.println("WF INSTANCE CONF:"); 187 // System.out.println(XmlUtils.prettyPrint(conf).toString()); 188 189 WorkflowJobBean workflow = new WorkflowJobBean(); 190 workflow.setId(wfInstance.getId()); 191 workflow.setAppName(ELUtils.resolveAppName(app.getName(), conf)); 192 workflow.setAppPath(conf.get(OozieClient.APP_PATH)); 193 workflow.setConf(XmlUtils.prettyPrint(conf).toString()); 194 workflow.setProtoActionConf(protoActionConf.toXmlString()); 195 workflow.setCreatedTime(new Date()); 196 workflow.setLastModifiedTime(new Date()); 197 workflow.setLogToken(conf.get(OozieClient.LOG_TOKEN, "")); 198 workflow.setStatus(WorkflowJob.Status.PREP); 199 workflow.setRun(0); 200 workflow.setUser(conf.get(OozieClient.USER_NAME)); 201 workflow.setGroup(conf.get(OozieClient.GROUP_NAME)); 202 workflow.setWorkflowInstance(wfInstance); 203 workflow.setExternalId(conf.get(OozieClient.EXTERNAL_ID)); 204 // Set parent id if it doesn't already have one (for subworkflows) 205 if (workflow.getParentId() == null) { 206 workflow.setParentId(conf.get(SubWorkflowActionExecutor.PARENT_ID)); 207 } 208 // Set to coord action Id if workflow submitted through coordinator 209 if (workflow.getParentId() == null) { 210 workflow.setParentId(parentId); 211 } 212 213 LogUtils.setLogInfo(workflow); 214 LOG.debug("Workflow record created, Status [{0}]", workflow.getStatus()); 215 Element wfElem = XmlUtils.parseXml(app.getDefinition()); 216 ELEvaluator evalSla = createELEvaluatorForGroup(conf, "wf-sla-submit"); 217 String jobSlaXml = verifySlaElements(wfElem, evalSla); 218 if (!dryrun) { 219 writeSLARegistration(wfElem, jobSlaXml, workflow.getId(), workflow.getParentId(), workflow.getUser(), 220 workflow.getGroup(), workflow.getAppName(), LOG, evalSla); 221 workflow.setSlaXml(jobSlaXml); 222 // System.out.println("SlaXml :"+ slaXml); 223 224 //store.insertWorkflow(workflow); 225 insertList.add(workflow); 226 JPAService jpaService = Services.get().get(JPAService.class); 227 if (jpaService != null) { 228 try { 229 BatchQueryExecutor.getInstance().executeBatchInsertUpdateDelete(insertList, null, null); 230 } 231 catch (JPAExecutorException je) { 232 throw new CommandException(je); 233 } 234 } 235 else { 236 LOG.error(ErrorCode.E0610); 237 return null; 238 } 239 240 return workflow.getId(); 241 } 242 else { 243 // Checking variable substitution for dryrun 244 ActionExecutorContext context = new ActionXCommand.ActionExecutorContext(workflow, null, false, false); 245 Element workflowXml = XmlUtils.parseXml(app.getDefinition()); 246 removeSlaElements(workflowXml); 247 String workflowXmlString = XmlUtils.removeComments(XmlUtils.prettyPrint(workflowXml).toString()); 248 workflowXmlString = context.getELEvaluator().evaluate(workflowXmlString, String.class); 249 workflowXml = XmlUtils.parseXml(workflowXmlString); 250 251 Iterator<Element> it = workflowXml.getDescendants(new ElementFilter("job-xml")); 252 253 // Checking all variable substitutions in job-xml files 254 while (it.hasNext()) { 255 Element e = it.next(); 256 String jobXml = e.getTextTrim(); 257 Path xmlPath = new Path(workflow.getAppPath(), jobXml); 258 Configuration jobXmlConf = new XConfiguration(fs.open(xmlPath)); 259 260 261 String jobXmlConfString = XmlUtils.prettyPrint(jobXmlConf).toString(); 262 jobXmlConfString = XmlUtils.removeComments(jobXmlConfString); 263 context.getELEvaluator().evaluate(jobXmlConfString, String.class); 264 } 265 266 return "OK"; 267 } 268 } 269 catch (WorkflowException ex) { 270 throw new CommandException(ex); 271 } 272 catch (HadoopAccessorException ex) { 273 throw new CommandException(ex); 274 } 275 catch (Exception ex) { 276 throw new CommandException(ErrorCode.E0803, ex.getMessage(), ex); 277 } 278 } 279 280 /** 281 * Resolving variables from config-default, which might be referencing into conf/defaultConf 282 * @param defaultConf config-default.xml 283 * @return resolved config-default configuration. 284 */ 285 private Configuration resolveDefaultConfVariables(Configuration defaultConf) { 286 XConfiguration resolveDefaultConf = new XConfiguration(); 287 for (Map.Entry<String, String> entry : defaultConf) { 288 String defaultConfKey = entry.getKey(); 289 String defaultConfValue = entry.getValue(); 290 // if value is referencing some other key, first check within the default config to resolve, 291 // then job.properties (conf) 292 if (defaultConfValue.contains("$") && defaultConf.get(defaultConfKey).contains("$")) { 293 resolveDefaultConf.set(defaultConfKey, conf.get(defaultConfKey)); 294 } else { 295 resolveDefaultConf.set(defaultConfKey, defaultConf.get(defaultConfKey)); 296 } 297 } 298 return resolveDefaultConf; 299 } 300 301 private void removeSlaElements(Element eWfJob) { 302 Element sla = XmlUtils.getSLAElement(eWfJob); 303 if (sla != null) { 304 eWfJob.removeChildren(sla.getName(), sla.getNamespace()); 305 } 306 307 for (Element action : (List<Element>) eWfJob.getChildren("action", eWfJob.getNamespace())) { 308 sla = XmlUtils.getSLAElement(action); 309 if (sla != null) { 310 action.removeChildren(sla.getName(), sla.getNamespace()); 311 } 312 } 313 } 314 private String verifySlaElements(Element eWfJob, ELEvaluator evalSla) throws CommandException { 315 String jobSlaXml = ""; 316 // Validate WF job 317 Element eSla = XmlUtils.getSLAElement(eWfJob); 318 if (eSla != null) { 319 jobSlaXml = resolveSla(eSla, evalSla); 320 } 321 322 // Validate all actions 323 for (Element action : (List<Element>) eWfJob.getChildren("action", eWfJob.getNamespace())) { 324 eSla = XmlUtils.getSLAElement(action); 325 if (eSla != null) { 326 resolveSla(eSla, evalSla); 327 } 328 } 329 return jobSlaXml; 330 } 331 332 private void writeSLARegistration(Element eWfJob, String slaXml, String jobId, String parentId, String user, 333 String group, String appName, XLog log, ELEvaluator evalSla) throws CommandException { 334 try { 335 if (slaXml != null && slaXml.length() > 0) { 336 Element eSla = XmlUtils.parseXml(slaXml); 337 SLAEventBean slaEvent = SLADbOperations.createSlaRegistrationEvent(eSla, jobId, 338 SlaAppType.WORKFLOW_JOB, user, group, log); 339 if(slaEvent != null) { 340 insertList.add(slaEvent); 341 } 342 // insert into new table 343 SLAOperations.createSlaRegistrationEvent(eSla, jobId, parentId, AppType.WORKFLOW_JOB, user, appName, 344 log, false); 345 } 346 // Add sla for wf actions 347 for (Element action : (List<Element>) eWfJob.getChildren("action", eWfJob.getNamespace())) { 348 Element actionSla = XmlUtils.getSLAElement(action); 349 if (actionSla != null) { 350 String actionSlaXml = SubmitXCommand.resolveSla(actionSla, evalSla); 351 actionSla = XmlUtils.parseXml(actionSlaXml); 352 String actionId = Services.get().get(UUIDService.class) 353 .generateChildId(jobId, action.getAttributeValue("name") + ""); 354 SLAOperations.createSlaRegistrationEvent(actionSla, actionId, jobId, AppType.WORKFLOW_ACTION, 355 user, appName, log, false); 356 } 357 } 358 } 359 catch (Exception e) { 360 e.printStackTrace(); 361 throw new CommandException(ErrorCode.E1007, "workflow " + jobId, e.getMessage(), e); 362 } 363 } 364 365 /** 366 * Resolve variables in sla xml element. 367 * 368 * @param eSla sla xml element 369 * @param evalSla sla evaluator 370 * @return sla xml string after evaluation 371 * @throws CommandException 372 */ 373 public static String resolveSla(Element eSla, ELEvaluator evalSla) throws CommandException { 374 // EL evaluation 375 String slaXml = XmlUtils.prettyPrint(eSla).toString(); 376 try { 377 slaXml = XmlUtils.removeComments(slaXml); 378 slaXml = evalSla.evaluate(slaXml, String.class); 379 XmlUtils.validateData(slaXml, SchemaName.SLA_ORIGINAL); 380 return slaXml; 381 } 382 catch (Exception e) { 383 throw new CommandException(ErrorCode.E1004, "Validation error :" + e.getMessage(), e); 384 } 385 } 386 387 /** 388 * Create an EL evaluator for a given group. 389 * 390 * @param conf configuration variable 391 * @param group group variable 392 * @return the evaluator created for the group 393 */ 394 public static ELEvaluator createELEvaluatorForGroup(Configuration conf, String group) { 395 ELEvaluator eval = Services.get().get(ELService.class).createEvaluator(group); 396 for (Map.Entry<String, String> entry : conf) { 397 eval.setVariable(entry.getKey(), entry.getValue()); 398 } 399 return eval; 400 } 401 402 @Override 403 public String getEntityKey() { 404 return null; 405 } 406 407 @Override 408 protected boolean isLockRequired() { 409 return false; 410 } 411 412 @Override 413 protected void loadState() { 414 415 } 416 417 @Override 418 protected void verifyPrecondition() throws CommandException { 419 420 } 421 422}