001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.command.coord;
016    
017    import org.apache.oozie.CoordinatorActionBean;
018    import org.apache.oozie.WorkflowJobBean;
019    import org.apache.oozie.XException;
020    import org.apache.oozie.store.CoordinatorStore;
021    import org.apache.oozie.store.StoreException;
022    import org.apache.oozie.util.XLog;
023    import org.apache.oozie.util.db.SLADbOperations;
024    import org.apache.oozie.client.CoordinatorAction;
025    import org.apache.oozie.client.WorkflowJob;
026    import org.apache.oozie.client.SLAEvent.SlaAppType;
027    import org.apache.oozie.client.SLAEvent.Status;
028    import org.apache.oozie.command.CommandException;
029    
030    public class CoordActionUpdateCommand extends CoordinatorCommand<Void> {
031        private final XLog log = XLog.getLog(getClass());
032        private WorkflowJobBean workflow;
033        private CoordinatorActionBean caction = null;
034    
035        public CoordActionUpdateCommand(WorkflowJobBean workflow) {
036            super("coord-action-update", "coord-action-update", 1, XLog.OPS);
037            this.workflow = workflow;
038        }
039    
040        @Override
041        protected Void call(CoordinatorStore cstore) throws StoreException, CommandException {
042            try {
043                if (workflow.getStatus() == WorkflowJob.Status.RUNNING
044                        || workflow.getStatus() == WorkflowJob.Status.SUSPENDED) {
045                    //update lastModifiedTime
046                    cstore.updateCoordinatorAction(caction);
047                    return null;
048                }
049                // CoordinatorActionBean caction =
050                // cstore.getCoordinatorActionForExternalId(workflow.getId());
051                Status slaStatus = null;
052                if (caction != null) {
053                    if (workflow.getStatus() == WorkflowJob.Status.SUCCEEDED) {
054                        caction.setStatus(CoordinatorAction.Status.SUCCEEDED);
055                        slaStatus = Status.SUCCEEDED;
056                    }
057                    else {
058                        if (workflow.getStatus() == WorkflowJob.Status.FAILED) {
059                            caction.setStatus(CoordinatorAction.Status.FAILED);
060                            slaStatus = Status.FAILED;
061                        }
062                        else {
063                            if (workflow.getStatus() == WorkflowJob.Status.KILLED) {
064                                caction.setStatus(CoordinatorAction.Status.KILLED);
065                                slaStatus = Status.KILLED;
066                            }
067                            else {
068                                log.warn(
069                                        "Unexpected workflow " + workflow.getId() + " STATUS " + workflow.getStatus());
070                                //update lastModifiedTime
071                                cstore.updateCoordinatorAction(caction);
072                                return null;
073                            }
074                        }
075                    }
076    
077                    log.info(
078                            "Updating Coordintaor id :" + caction.getId() + "status to =" + caction.getStatus());
079                    cstore.updateCoordinatorAction(caction);
080                    if (slaStatus != null) {
081                        SLADbOperations.writeStausEvent(caction.getSlaXml(), caction.getId(), cstore, slaStatus,
082                                                        SlaAppType.COORDINATOR_ACTION);
083                    }
084                    queueCallable(new CoordActionReadyCommand(caction.getJobId()));
085                }
086            }
087            catch (XException ex) {
088                log.warn("CoordActionUpdate Failed ", ex.getMessage());
089                throw new CommandException(ex);
090            }
091            return null;
092        }
093    
094        @Override
095        protected Void execute(CoordinatorStore store) throws StoreException, CommandException {
096            log.info("STARTED CoordActionUpdateCommand for wfId=" + workflow.getId());
097            caction = store.getCoordinatorActionForExternalId(workflow.getId());
098            if (caction == null) {
099                log.info("ENDED CoordActionUpdateCommand for wfId=" + workflow.getId() + ", coord action is null");
100                return null;
101            }
102            setLogInfo(caction);
103            String jobId = caction.getJobId();
104            try {
105                if (lock(jobId)) {
106                    call(store);
107                }
108                else {
109                    queueCallable(new CoordActionUpdateCommand(workflow), LOCK_FAILURE_REQUEUE_INTERVAL);
110                    log.warn("CoordActionUpdateCommand lock was not acquired - failed JobId=" + jobId + ", wfId="
111                            + workflow.getId() + ". Requeing the same.");
112                }
113            }
114            catch (InterruptedException e) {
115                queueCallable(new CoordActionUpdateCommand(workflow), LOCK_FAILURE_REQUEUE_INTERVAL);
116                log.warn("CoordActionUpdateCommand lock acquiring failed with exception " + e.getMessage() + " for jobId="
117                        + jobId + ", wfId=" + workflow.getId() + ". Requeing the same.");
118            }
119            finally {
120                log.info("ENDED CoordActionUpdateCommand for wfId=" + workflow.getId() + ", jobId=" + jobId);
121            }
122            return null;
123        }
124    }