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