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 org.apache.oozie.WorkflowActionBean;
021    import org.apache.oozie.WorkflowJobBean;
022    import org.apache.oozie.client.SLAEvent.SlaAppType;
023    import org.apache.oozie.client.SLAEvent.Status;
024    import org.apache.oozie.command.CommandException;
025    import org.apache.oozie.action.ActionExecutor;
026    import org.apache.oozie.action.ActionExecutorException;
027    import org.apache.oozie.service.ActionService;
028    import org.apache.oozie.service.UUIDService;
029    import org.apache.oozie.store.StoreException;
030    import org.apache.oozie.store.WorkflowStore;
031    import org.apache.oozie.service.Services;
032    import org.apache.oozie.util.XLog;
033    import org.apache.oozie.util.Instrumentation;
034    import org.apache.oozie.util.db.SLADbOperations;
035    
036    public class ActionKillCommand extends ActionCommand<Void> {
037        private String id;
038        private String jobId;
039    
040        public ActionKillCommand(String id, String type) {
041            super("action.kill", type, 0);
042            this.id = id;
043        }
044    
045        @Override
046        protected Void call(WorkflowStore store) throws StoreException, CommandException {
047            // String jobId = Services.get().get(UUIDService.class).getId(id);
048            WorkflowJobBean workflow = store.getWorkflow(jobId, false);
049            setLogInfo(workflow);
050            WorkflowActionBean action = store.getAction(id, false);
051            setLogInfo(action);
052            if (action.isPending() && (action.getStatus() == WorkflowActionBean.Status.KILLED)) {
053                ActionExecutor executor = Services.get().get(ActionService.class).getExecutor(action.getType());
054                if (executor != null) {
055                    try {
056                        boolean isRetry = false;
057                        ActionExecutorContext context = new ActionCommand.ActionExecutorContext(workflow, action, isRetry);
058                        incrActionCounter(action.getType(), 1);
059    
060                        Instrumentation.Cron cron = new Instrumentation.Cron();
061                        cron.start();
062                        executor.kill(context, action);
063                        cron.stop();
064                        addActionCron(action.getType(), cron);
065    
066                        action.resetPending();
067                        action.setStatus(WorkflowActionBean.Status.KILLED);
068    
069                        store.updateAction(action);
070                        store.updateWorkflow(workflow);
071                        // Add SLA status event (KILLED) for WF_ACTION
072                        SLADbOperations.writeStausEvent(action.getSlaXml(), action.getId(), store, Status.KILLED,
073                                                        SlaAppType.WORKFLOW_ACTION);
074                        queueCallable(new NotificationCommand(workflow, action));
075                    }
076                    catch (ActionExecutorException ex) {
077                        action.resetPending();
078                        action.setStatus(WorkflowActionBean.Status.FAILED);
079                        action.setErrorInfo(ex.getErrorCode().toString(),
080                                            "KILL COMMAND FAILED - exception while executing job kill");
081                        workflow.setStatus(WorkflowJobBean.Status.KILLED);
082                        store.updateAction(action);
083                        store.updateWorkflow(workflow);
084                        // What will happen to WF and COORD_ACTION, NOTIFICATION?
085                        SLADbOperations.writeStausEvent(action.getSlaXml(), action.getId(), store, Status.FAILED,
086                                                        SlaAppType.WORKFLOW_ACTION);
087                        XLog.getLog(getClass()).warn("Exception while executing kill(). Error Code [{0}], Message[{1}]",
088                                                     ex.getErrorCode(), ex.getMessage(), ex);
089                    }
090                }
091            }
092            return null;
093        }
094    
095        @Override
096        protected Void execute(WorkflowStore store) throws CommandException, StoreException {
097            XLog.getLog(getClass()).debug("STARTED ActionKillCommand for action " + id);
098            try {
099                jobId = Services.get().get(UUIDService.class).getId(id);
100                if (lock(jobId)) {
101                    call(store);
102                }
103                else {
104                    queueCallable(new ActionKillCommand(id, getType()), LOCK_FAILURE_REQUEUE_INTERVAL);
105                    XLog.getLog(getClass()).warn("ActionKill lock was not acquired - failed {0}", id);
106                }
107            }
108            catch (InterruptedException e) {
109                queueCallable(new ActionKillCommand(id, getType()), LOCK_FAILURE_REQUEUE_INTERVAL);
110                XLog.getLog(getClass()).warn("ActionKill lock was not acquired - interrupted exception failed {0}", id);
111            }
112            finally {
113                XLog.getLog(getClass()).debug("ENDED ActionKillCommand for action " + id);
114            }
115            return null;
116        }
117    }