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.coord; 020 021import org.apache.hadoop.conf.Configuration; 022import org.apache.oozie.action.hadoop.OozieJobInfo; 023import org.apache.oozie.client.CoordinatorAction; 024import org.apache.oozie.client.OozieClient; 025import org.apache.oozie.CoordinatorActionBean; 026import org.apache.oozie.DagEngineException; 027import org.apache.oozie.DagEngine; 028import org.apache.oozie.ErrorCode; 029import org.apache.oozie.SLAEventBean; 030import org.apache.oozie.WorkflowJobBean; 031import org.apache.oozie.command.CommandException; 032import org.apache.oozie.command.PreconditionException; 033import org.apache.oozie.service.DagEngineService; 034import org.apache.oozie.service.EventHandlerService; 035import org.apache.oozie.service.JPAService; 036import org.apache.oozie.service.Services; 037import org.apache.oozie.util.ConfigUtils; 038import org.apache.oozie.util.JobUtils; 039import org.apache.oozie.util.LogUtils; 040import org.apache.oozie.util.ParamChecker; 041import org.apache.oozie.util.XConfiguration; 042import org.apache.oozie.util.XLog; 043import org.apache.oozie.util.XmlUtils; 044import org.apache.oozie.util.db.SLADbOperations; 045import org.apache.oozie.client.SLAEvent.SlaAppType; 046import org.apache.oozie.client.SLAEvent.Status; 047import org.apache.oozie.client.rest.JsonBean; 048import org.apache.oozie.executor.jpa.BatchQueryExecutor.UpdateEntry; 049import org.apache.oozie.executor.jpa.BatchQueryExecutor; 050import org.apache.oozie.executor.jpa.CoordActionQueryExecutor.CoordActionQuery; 051import org.apache.oozie.executor.jpa.JPAExecutorException; 052import org.apache.oozie.executor.jpa.WorkflowJobQueryExecutor; 053import org.apache.oozie.executor.jpa.WorkflowJobQueryExecutor.WorkflowJobQuery; 054import org.jdom.Element; 055import org.jdom.JDOMException; 056 057import java.io.IOException; 058import java.io.StringReader; 059import java.util.ArrayList; 060import java.util.Date; 061import java.util.List; 062 063@SuppressWarnings("deprecation") 064public class CoordActionStartXCommand extends CoordinatorXCommand<Void> { 065 066 public static final String EL_ERROR = "EL_ERROR"; 067 public static final String EL_EVAL_ERROR = "EL_EVAL_ERROR"; 068 public static final String COULD_NOT_START = "COULD_NOT_START"; 069 public static final String START_DATA_MISSING = "START_DATA_MISSING"; 070 public static final String EXEC_DATA_MISSING = "EXEC_DATA_MISSING"; 071 public static final String OOZIE_COORD_ACTION_NOMINAL_TIME = "oozie.coord.action.nominal_time"; 072 073 private final XLog log = getLog(); 074 private String actionId = null; 075 private String user = null; 076 private String appName = null; 077 private CoordinatorActionBean coordAction = null; 078 private JPAService jpaService = null; 079 private String jobId = null; 080 private List<UpdateEntry> updateList = new ArrayList<UpdateEntry>(); 081 private List<JsonBean> insertList = new ArrayList<JsonBean>(); 082 083 public CoordActionStartXCommand(String id, String user, String appName, String jobId) { 084 //super("coord_action_start", "coord_action_start", 1, XLog.OPS); 085 super("coord_action_start", "coord_action_start", 1); 086 this.actionId = ParamChecker.notEmpty(id, "id"); 087 this.user = ParamChecker.notEmpty(user, "user"); 088 this.appName = ParamChecker.notEmpty(appName, "appName"); 089 this.jobId = jobId; 090 } 091 092 @Override 093 protected void setLogInfo() { 094 LogUtils.setLogInfo(actionId); 095 } 096 097 /** 098 * Create config to pass to WF Engine 1. Get createdConf from coord_actions table 2. Get actionXml from 099 * coord_actions table. Extract all 'property' tags and merge createdConf (overwrite duplicate keys). 3. Extract 100 * 'app-path' from actionXML. Create a new property called 'oozie.wf.application.path' and merge with createdConf 101 * (overwrite duplicate keys) 4. Read contents of config-default.xml in workflow directory. 5. Merge createdConf 102 * with config-default.xml (overwrite duplicate keys). 6. Results is runConf which is saved in coord_actions table. 103 * Merge Action createdConf with actionXml to create new runConf with replaced variables 104 * 105 * @param action CoordinatorActionBean 106 * @return Configuration 107 * @throws CommandException 108 */ 109 private Configuration mergeConfig(CoordinatorActionBean action) throws CommandException { 110 String createdConf = action.getCreatedConf(); 111 String actionXml = action.getActionXml(); 112 Element workflowProperties = null; 113 try { 114 workflowProperties = XmlUtils.parseXml(actionXml); 115 } 116 catch (JDOMException e1) { 117 log.warn("Configuration parse error in:" + actionXml); 118 throw new CommandException(ErrorCode.E1005, e1.getMessage(), e1); 119 } 120 // generate the 'runConf' for this action 121 // Step 1: runConf = createdConf 122 Configuration runConf = null; 123 try { 124 runConf = new XConfiguration(new StringReader(createdConf)); 125 126 } 127 catch (IOException e1) { 128 log.warn("Configuration parse error in:" + createdConf); 129 throw new CommandException(ErrorCode.E1005, e1.getMessage(), e1); 130 } 131 // Step 2: Merge local properties into runConf 132 // extract 'property' tags under 'configuration' block in the 133 // coordinator.xml (saved in actionxml column) 134 // convert Element to XConfiguration 135 Element configElement = workflowProperties.getChild("action", workflowProperties.getNamespace()) 136 .getChild("workflow", workflowProperties.getNamespace()).getChild("configuration", 137 workflowProperties.getNamespace()); 138 if (configElement != null) { 139 String strConfig = XmlUtils.prettyPrint(configElement).toString(); 140 Configuration localConf; 141 try { 142 localConf = new XConfiguration(new StringReader(strConfig)); 143 } 144 catch (IOException e1) { 145 log.warn("Configuration parse error in:" + strConfig); 146 throw new CommandException(ErrorCode.E1005, e1.getMessage(), e1); 147 } 148 149 // copy configuration properties in coordinator.xml to the runConf 150 XConfiguration.copy(localConf, runConf); 151 } 152 153 // Step 3: Extract value of 'app-path' in actionxml, and save it as a 154 // new property called 'oozie.wf.application.path' 155 // WF Engine requires the path to the workflow.xml to be saved under 156 // this property name 157 String appPath = workflowProperties.getChild("action", workflowProperties.getNamespace()) 158 .getChild("workflow", workflowProperties.getNamespace()).getChild("app-path", 159 workflowProperties.getNamespace()).getValue(); 160 161 // Copying application path in runconf. 162 runConf.set("oozie.wf.application.path", appPath); 163 164 // Step 4: Extract the runconf and copy the rerun config to runconf. 165 if (runConf.get(CoordRerunXCommand.RERUN_CONF) != null) { 166 Configuration rerunConf = null; 167 try { 168 rerunConf = new XConfiguration(new StringReader(runConf.get(CoordRerunXCommand.RERUN_CONF))); 169 XConfiguration.copy(rerunConf, runConf); 170 } catch (IOException e) { 171 log.warn("Configuration parse error in:" + rerunConf); 172 throw new CommandException(ErrorCode.E1005, e.getMessage(), e); 173 } 174 runConf.unset(CoordRerunXCommand.RERUN_CONF); 175 } 176 177 ConfigUtils.checkAndSetDisallowedProperties(runConf, 178 this.user, 179 new CommandException(ErrorCode.E1003, 180 String.format("%s=%s", OozieClient.USER_NAME, runConf.get(OozieClient.USER_NAME))), 181 true); 182 183 return runConf; 184 } 185 186 @Override 187 protected Void execute() throws CommandException { 188 boolean makeFail = true; 189 String errCode = ""; 190 String errMsg = ""; 191 ParamChecker.notEmpty(user, "user"); 192 193 log.debug("actionid=" + actionId + ", status=" + coordAction.getStatus()); 194 if (coordAction.getStatus() == CoordinatorAction.Status.SUBMITTED) { 195 try { 196 // log.debug("getting.. job id: " + coordAction.getJobId()); 197 // create merged runConf to pass to WF Engine 198 Configuration runConf = mergeConfig(coordAction); 199 coordAction.setRunConf(XmlUtils.prettyPrint(runConf).toString()); 200 // log.debug("%%% merged runconf=" + 201 // XmlUtils.prettyPrint(runConf).toString()); 202 DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(user); 203 204 Configuration conf = new XConfiguration(new StringReader(coordAction.getRunConf())); 205 SLAEventBean slaEvent = SLADbOperations.createStatusEvent(coordAction.getSlaXml(), coordAction.getId(), 206 Status.STARTED, 207 SlaAppType.COORDINATOR_ACTION, log); 208 if(slaEvent != null) { 209 insertList.add(slaEvent); 210 } 211 if (OozieJobInfo.isJobInfoEnabled()) { 212 conf.set(OozieJobInfo.COORD_ID, actionId); 213 conf.set(OozieJobInfo.COORD_NAME, appName); 214 conf.set(OozieJobInfo.COORD_NOMINAL_TIME, coordAction.getNominalTimestamp().toString()); 215 } 216 // Normalize workflow appPath here; 217 JobUtils.normalizeAppPath(conf.get(OozieClient.USER_NAME), conf.get(OozieClient.GROUP_NAME), conf); 218 if (coordAction.getExternalId() != null) { 219 conf.setBoolean(OozieClient.RERUN_FAIL_NODES, true); 220 dagEngine.reRun(coordAction.getExternalId(), conf); 221 } else { 222 // Pushing the nominal time in conf to use for launcher tag search 223 conf.set(OOZIE_COORD_ACTION_NOMINAL_TIME,String.valueOf(coordAction.getNominalTime().getTime())); 224 String wfId = dagEngine.submitJobFromCoordinator(conf, actionId); 225 coordAction.setExternalId(wfId); 226 } 227 coordAction.setStatus(CoordinatorAction.Status.RUNNING); 228 coordAction.incrementAndGetPending(); 229 230 //store.updateCoordinatorAction(coordAction); 231 JPAService jpaService = Services.get().get(JPAService.class); 232 if (jpaService != null) { 233 log.debug("Updating WF record for WFID :" + coordAction.getExternalId() + " with parent id: " + actionId); 234 WorkflowJobBean wfJob = WorkflowJobQueryExecutor.getInstance().get(WorkflowJobQuery.GET_WORKFLOW_STARTTIME, 235 coordAction.getExternalId()); 236 wfJob.setParentId(actionId); 237 wfJob.setLastModifiedTime(new Date()); 238 BatchQueryExecutor executor = BatchQueryExecutor.getInstance(); 239 updateList.add(new UpdateEntry<WorkflowJobQuery>( 240 WorkflowJobQuery.UPDATE_WORKFLOW_PARENT_MODIFIED, wfJob)); 241 updateList.add(new UpdateEntry<CoordActionQuery>( 242 CoordActionQuery.UPDATE_COORD_ACTION_FOR_START, coordAction)); 243 try { 244 executor.executeBatchInsertUpdateDelete(insertList, updateList, null); 245 queue(new CoordActionNotificationXCommand(coordAction), 100); 246 if (EventHandlerService.isEnabled()) { 247 generateEvent(coordAction, user, appName, wfJob.getStartTime()); 248 } 249 } 250 catch (JPAExecutorException je) { 251 throw new CommandException(je); 252 } 253 } 254 else { 255 log.error(ErrorCode.E0610); 256 } 257 258 makeFail = false; 259 } 260 catch (DagEngineException dee) { 261 errMsg = dee.getMessage(); 262 errCode = dee.getErrorCode().toString(); 263 log.warn("can not create DagEngine for submitting jobs", dee); 264 } 265 catch (CommandException ce) { 266 errMsg = ce.getMessage(); 267 errCode = ce.getErrorCode().toString(); 268 log.warn("command exception occurred ", ce); 269 } 270 catch (java.io.IOException ioe) { 271 errMsg = ioe.getMessage(); 272 errCode = "E1005"; 273 log.warn("Configuration parse error. read from DB :" + coordAction.getRunConf(), ioe); 274 } 275 catch (Exception ex) { 276 errMsg = ex.getMessage(); 277 errCode = "E1005"; 278 log.warn("can not create DagEngine for submitting jobs", ex); 279 } 280 finally { 281 if (makeFail == true) { // No DB exception occurs 282 log.error("Failing the action " + coordAction.getId() + ". Because " + errCode + " : " + errMsg); 283 coordAction.setStatus(CoordinatorAction.Status.FAILED); 284 if (errMsg.length() > 254) { // Because table column size is 255 285 errMsg = errMsg.substring(0, 255); 286 } 287 coordAction.setErrorMessage(errMsg); 288 coordAction.setErrorCode(errCode); 289 290 updateList = new ArrayList<UpdateEntry>(); 291 updateList.add(new UpdateEntry<CoordActionQuery>( 292 CoordActionQuery.UPDATE_COORD_ACTION_FOR_START, coordAction)); 293 insertList = new ArrayList<JsonBean>(); 294 295 SLAEventBean slaEvent = SLADbOperations.createStatusEvent(coordAction.getSlaXml(), coordAction.getId(), 296 Status.FAILED, 297 SlaAppType.COORDINATOR_ACTION, log); 298 if(slaEvent != null) { 299 insertList.add(slaEvent); //Update SLA events 300 } 301 try { 302 // call JPAExecutor to do the bulk writes 303 BatchQueryExecutor.getInstance().executeBatchInsertUpdateDelete(insertList, updateList, null); 304 if (EventHandlerService.isEnabled()) { 305 generateEvent(coordAction, user, appName, null); 306 } 307 } 308 catch (JPAExecutorException je) { 309 throw new CommandException(je); 310 } 311 queue(new CoordActionReadyXCommand(coordAction.getJobId())); 312 } 313 } 314 } 315 return null; 316 } 317 318 @Override 319 public String getEntityKey() { 320 return this.jobId; 321 } 322 323 @Override 324 protected boolean isLockRequired() { 325 return true; 326 } 327 328 @Override 329 protected void loadState() throws CommandException { 330 jpaService = Services.get().get(JPAService.class); 331 try { 332 coordAction = jpaService.execute(new org.apache.oozie.executor.jpa.CoordActionGetForStartJPAExecutor( 333 actionId)); 334 } 335 catch (JPAExecutorException je) { 336 throw new CommandException(je); 337 } 338 LogUtils.setLogInfo(coordAction); 339 } 340 341 @Override 342 protected void verifyPrecondition() throws PreconditionException { 343 if (coordAction.getStatus() != CoordinatorAction.Status.SUBMITTED) { 344 throw new PreconditionException(ErrorCode.E1100, "The coord action [" + actionId + "] must have status " 345 + CoordinatorAction.Status.SUBMITTED.name() + " but has status [" + coordAction.getStatus().name() + "]"); 346 } 347 } 348 349 @Override 350 public String getKey(){ 351 return getName() + "_" + actionId; 352 } 353}