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.client.WorkflowJob; 021 import org.apache.oozie.client.SLAEvent.SlaAppType; 022 import org.apache.oozie.client.SLAEvent.Status; 023 import org.apache.oozie.client.rest.JsonBean; 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.command.CommandException; 030 import org.apache.oozie.command.PreconditionException; 031 import org.apache.oozie.command.coord.CoordActionUpdateXCommand; 032 import org.apache.oozie.executor.jpa.BulkUpdateInsertJPAExecutor; 033 import org.apache.oozie.executor.jpa.JPAExecutorException; 034 import org.apache.oozie.executor.jpa.WorkflowActionsGetForJobJPAExecutor; 035 import org.apache.oozie.executor.jpa.WorkflowJobGetJPAExecutor; 036 import org.apache.oozie.service.JPAService; 037 import org.apache.oozie.service.Services; 038 import org.apache.oozie.workflow.WorkflowException; 039 import org.apache.oozie.workflow.WorkflowInstance; 040 import org.apache.oozie.workflow.lite.LiteWorkflowInstance; 041 import org.apache.oozie.util.InstrumentUtils; 042 import org.apache.oozie.util.LogUtils; 043 import org.apache.oozie.util.ParamChecker; 044 import org.apache.oozie.util.db.SLADbXOperations; 045 046 import java.util.ArrayList; 047 import java.util.Date; 048 import java.util.List; 049 050 /** 051 * Kill workflow job and its workflow instance and queue a {@link WorkflowActionKillXCommand} to kill the workflow 052 * actions. 053 */ 054 public class KillXCommand extends WorkflowXCommand<Void> { 055 056 private String wfId; 057 private WorkflowJobBean wfJob; 058 private List<WorkflowActionBean> actionList; 059 private JPAService jpaService = null; 060 private List<JsonBean> updateList = new ArrayList<JsonBean>(); 061 private List<JsonBean> insertList = new ArrayList<JsonBean>(); 062 063 public KillXCommand(String wfId) { 064 super("kill", "kill", 1); 065 this.wfId = ParamChecker.notEmpty(wfId, "wfId"); 066 } 067 068 @Override 069 protected boolean isLockRequired() { 070 return true; 071 } 072 073 @Override 074 public String getEntityKey() { 075 return this.wfId; 076 } 077 078 @Override 079 protected void loadState() throws CommandException { 080 try { 081 jpaService = Services.get().get(JPAService.class); 082 if (jpaService != null) { 083 this.wfJob = jpaService.execute(new WorkflowJobGetJPAExecutor(wfId)); 084 this.actionList = jpaService.execute(new WorkflowActionsGetForJobJPAExecutor(wfId)); 085 LogUtils.setLogInfo(wfJob, logInfo); 086 } 087 else { 088 throw new CommandException(ErrorCode.E0610); 089 } 090 } 091 catch (XException ex) { 092 throw new CommandException(ex); 093 } 094 } 095 096 @Override 097 protected void verifyPrecondition() throws CommandException, PreconditionException { 098 if (wfJob.getStatus() != WorkflowJob.Status.PREP && wfJob.getStatus() != WorkflowJob.Status.RUNNING 099 && wfJob.getStatus() != WorkflowJob.Status.SUSPENDED && wfJob.getStatus() != WorkflowJob.Status.FAILED) { 100 throw new PreconditionException(ErrorCode.E0725, wfJob.getId()); 101 } 102 } 103 104 @Override 105 protected Void execute() throws CommandException { 106 LOG.info("STARTED WorkflowKillXCommand for jobId=" + wfId); 107 108 wfJob.setEndTime(new Date()); 109 110 if (wfJob.getStatus() != WorkflowJob.Status.FAILED) { 111 InstrumentUtils.incrJobCounter(getName(), 1, getInstrumentation()); 112 wfJob.setStatus(WorkflowJob.Status.KILLED); 113 SLAEventBean slaEvent = SLADbXOperations.createStatusEvent(wfJob.getSlaXml(), wfJob.getId(), 114 Status.KILLED, SlaAppType.WORKFLOW_JOB); 115 if(slaEvent != null) { 116 insertList.add(slaEvent); 117 } 118 try { 119 wfJob.getWorkflowInstance().kill(); 120 } 121 catch (WorkflowException e) { 122 throw new CommandException(ErrorCode.E0725, e.getMessage(), e); 123 } 124 WorkflowInstance wfInstance = wfJob.getWorkflowInstance(); 125 ((LiteWorkflowInstance) wfInstance).setStatus(WorkflowInstance.Status.KILLED); 126 wfJob.setWorkflowInstance(wfInstance); 127 } 128 try { 129 for (WorkflowActionBean action : actionList) { 130 if (action.getStatus() == WorkflowActionBean.Status.RUNNING 131 || action.getStatus() == WorkflowActionBean.Status.DONE) { 132 action.setPending(); 133 action.setStatus(WorkflowActionBean.Status.KILLED); 134 135 updateList.add(action); 136 137 queue(new ActionKillXCommand(action.getId(), action.getType())); 138 } 139 if (action.getStatus() == WorkflowActionBean.Status.PREP 140 || action.getStatus() == WorkflowActionBean.Status.START_RETRY 141 || action.getStatus() == WorkflowActionBean.Status.START_MANUAL 142 || action.getStatus() == WorkflowActionBean.Status.END_RETRY 143 || action.getStatus() == WorkflowActionBean.Status.END_MANUAL) { 144 145 action.setStatus(WorkflowActionBean.Status.KILLED); 146 action.resetPending(); 147 SLAEventBean slaEvent = SLADbXOperations.createStatusEvent(action.getSlaXml(), action.getId(), 148 Status.KILLED, SlaAppType.WORKFLOW_ACTION); 149 if(slaEvent != null) { 150 insertList.add(slaEvent); 151 } 152 updateList.add(action); 153 } 154 } 155 wfJob.setLastModifiedTime(new Date()); 156 updateList.add(wfJob); 157 jpaService.execute(new BulkUpdateInsertJPAExecutor(updateList, insertList)); 158 queue(new NotificationXCommand(wfJob)); 159 } 160 catch (JPAExecutorException e) { 161 throw new CommandException(e); 162 } 163 finally { 164 if(wfJob.getStatus() == WorkflowJob.Status.KILLED) { 165 new WfEndXCommand(wfJob).call(); //To delete the WF temp dir 166 } 167 // update coordinator action 168 new CoordActionUpdateXCommand(wfJob).call(); 169 } 170 171 LOG.info("ENDED WorkflowKillXCommand for jobId=" + wfId); 172 return null; 173 } 174 175 }