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