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    package org.apache.oozie.command.wf;
019    
020    import java.util.ArrayList;
021    import java.util.Date;
022    import java.util.List;
023    
024    import org.apache.oozie.ErrorCode;
025    import org.apache.oozie.SLAEventBean;
026    import org.apache.oozie.WorkflowActionBean;
027    import org.apache.oozie.WorkflowJobBean;
028    import org.apache.oozie.XException;
029    import org.apache.oozie.client.SLAEvent.SlaAppType;
030    import org.apache.oozie.client.SLAEvent.Status;
031    import org.apache.oozie.client.rest.JsonBean;
032    import org.apache.oozie.command.CommandException;
033    import org.apache.oozie.command.PreconditionException;
034    import org.apache.oozie.executor.jpa.BatchQueryExecutor.UpdateEntry;
035    import org.apache.oozie.executor.jpa.BatchQueryExecutor;
036    import org.apache.oozie.executor.jpa.JPAExecutorException;
037    import org.apache.oozie.executor.jpa.WorkflowActionQueryExecutor;
038    import org.apache.oozie.executor.jpa.WorkflowJobQueryExecutor;
039    import org.apache.oozie.executor.jpa.WorkflowActionQueryExecutor.WorkflowActionQuery;
040    import org.apache.oozie.executor.jpa.WorkflowJobQueryExecutor.WorkflowJobQuery;
041    import org.apache.oozie.action.ActionExecutor;
042    import org.apache.oozie.action.ActionExecutorException;
043    import org.apache.oozie.action.control.ControlNodeActionExecutor;
044    import org.apache.oozie.service.ActionService;
045    import org.apache.oozie.service.EventHandlerService;
046    import org.apache.oozie.service.JPAService;
047    import org.apache.oozie.service.UUIDService;
048    import org.apache.oozie.service.Services;
049    import org.apache.oozie.util.LogUtils;
050    import org.apache.oozie.util.Instrumentation;
051    import org.apache.oozie.util.db.SLADbXOperations;
052    
053    /**
054     * Kill workflow action and invoke action executor to kill the underlying context.
055     *
056     */
057    @SuppressWarnings("deprecation")
058    public class ActionKillXCommand extends ActionXCommand<Void> {
059        private String actionId;
060        private String jobId;
061        private WorkflowJobBean wfJob;
062        private WorkflowActionBean wfAction;
063        private JPAService jpaService = null;
064        private List<UpdateEntry> updateList = new ArrayList<UpdateEntry>();
065        private List<JsonBean> insertList = new ArrayList<JsonBean>();
066    
067        public ActionKillXCommand(String actionId, String type) {
068            super("action.kill", type, 0);
069            this.actionId = actionId;
070            this.jobId = Services.get().get(UUIDService.class).getId(actionId);
071        }
072    
073        public ActionKillXCommand(String actionId) {
074            this(actionId, "action.kill");
075        }
076    
077        @Override
078        protected boolean isLockRequired() {
079            return true;
080        }
081    
082        @Override
083        public String getEntityKey() {
084            return this.jobId;
085        }
086    
087        @Override
088        public String getKey() {
089            return getName() + "_" + this.actionId;
090        }
091    
092        @Override
093        protected void loadState() throws CommandException {
094            try {
095                jpaService = Services.get().get(JPAService.class);
096    
097                if (jpaService != null) {
098                    this.wfJob = WorkflowJobQueryExecutor.getInstance().get(WorkflowJobQuery.GET_WORKFLOW_ACTION_OP, jobId);
099                    this.wfAction = WorkflowActionQueryExecutor.getInstance().get(WorkflowActionQuery.GET_ACTION, actionId);
100                    LogUtils.setLogInfo(wfJob, logInfo);
101                    LogUtils.setLogInfo(wfAction, logInfo);
102                }
103                else {
104                    throw new CommandException(ErrorCode.E0610);
105                }
106            }
107            catch (XException ex) {
108                throw new CommandException(ex);
109            }
110        }
111    
112        @Override
113        protected void verifyPrecondition() throws CommandException, PreconditionException {
114            if (wfAction.getStatus() != WorkflowActionBean.Status.KILLED) {
115                throw new PreconditionException(ErrorCode.E0726, wfAction.getId());
116            }
117        }
118    
119        @Override
120        protected Void execute() throws CommandException {
121            LOG.debug("STARTED WorkflowActionKillXCommand for action " + actionId);
122    
123            if (wfAction.isPending()) {
124                ActionExecutor executor = Services.get().get(ActionService.class).getExecutor(wfAction.getType());
125                if (executor != null) {
126                    try {
127                        boolean isRetry = false;
128                        boolean isUserRetry = false;
129                        ActionExecutorContext context = new ActionXCommand.ActionExecutorContext(wfJob, wfAction,
130                                isRetry, isUserRetry);
131                        incrActionCounter(wfAction.getType(), 1);
132    
133                        Instrumentation.Cron cron = new Instrumentation.Cron();
134                        cron.start();
135                        executor.kill(context, wfAction);
136                        cron.stop();
137                        addActionCron(wfAction.getType(), cron);
138    
139                        wfAction.resetPending();
140                        wfAction.setStatus(WorkflowActionBean.Status.KILLED);
141                        wfAction.setEndTime(new Date());
142    
143                        updateList.add(new UpdateEntry<WorkflowActionQuery>(WorkflowActionQuery.UPDATE_ACTION_END, wfAction));
144                        wfJob.setLastModifiedTime(new Date());
145                        updateList.add(new UpdateEntry<WorkflowJobQuery>(WorkflowJobQuery.UPDATE_WORKFLOW_MODTIME, wfJob));
146                        // Add SLA status event (KILLED) for WF_ACTION
147                        SLAEventBean slaEvent = SLADbXOperations.createStatusEvent(wfAction.getSlaXml(), wfAction.getId(), Status.KILLED,
148                                SlaAppType.WORKFLOW_ACTION);
149                        if(slaEvent != null) {
150                            insertList.add(slaEvent);
151                        }
152                        queue(new NotificationXCommand(wfJob, wfAction));
153                    }
154                    catch (ActionExecutorException ex) {
155                        wfAction.resetPending();
156                        wfAction.setStatus(WorkflowActionBean.Status.FAILED);
157                        wfAction.setErrorInfo(ex.getErrorCode().toString(),
158                                "KILL COMMAND FAILED - exception while executing job kill");
159                        wfAction.setEndTime(new Date());
160    
161                        wfJob.setStatus(WorkflowJobBean.Status.KILLED);
162                        updateList.add(new UpdateEntry<WorkflowActionQuery>(WorkflowActionQuery.UPDATE_ACTION_END, wfAction));
163                        wfJob.setLastModifiedTime(new Date());
164                        updateList.add(new UpdateEntry<WorkflowJobQuery>(WorkflowJobQuery.UPDATE_WORKFLOW_STATUS_MODTIME, wfJob));
165                        // What will happen to WF and COORD_ACTION, NOTIFICATION?
166                        SLAEventBean slaEvent = SLADbXOperations.createStatusEvent(wfAction.getSlaXml(), wfAction.getId(), Status.FAILED,
167                                SlaAppType.WORKFLOW_ACTION);
168                        if(slaEvent != null) {
169                            insertList.add(slaEvent);
170                        }
171                        LOG.warn("Exception while executing kill(). Error Code [{0}], Message[{1}]",
172                                ex.getErrorCode(), ex.getMessage(), ex);
173                    }
174                    finally {
175                        try {
176                            BatchQueryExecutor.getInstance().executeBatchInsertUpdateDelete(insertList, updateList, null);
177                            if (!(executor instanceof ControlNodeActionExecutor) && EventHandlerService.isEnabled()) {
178                                generateEvent(wfAction, wfJob.getUser());
179                            }
180                        }
181                        catch (JPAExecutorException e) {
182                            throw new CommandException(e);
183                        }
184                    }
185                }
186            }
187            LOG.debug("ENDED WorkflowActionKillXCommand for action " + actionId);
188            return null;
189        }
190    
191    }