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.coord; 019 020 import java.util.Date; 021 import java.util.List; 022 023 import org.apache.oozie.CoordinatorActionBean; 024 import org.apache.oozie.CoordinatorJobBean; 025 import org.apache.oozie.ErrorCode; 026 import org.apache.oozie.XException; 027 import org.apache.oozie.client.CoordinatorJob; 028 import org.apache.oozie.client.Job; 029 import org.apache.oozie.command.CommandException; 030 import org.apache.oozie.command.PreconditionException; 031 import org.apache.oozie.command.SuspendTransitionXCommand; 032 import org.apache.oozie.command.bundle.BundleStatusUpdateXCommand; 033 import org.apache.oozie.command.wf.SuspendXCommand; 034 import org.apache.oozie.executor.jpa.CoordActionUpdateJPAExecutor; 035 import org.apache.oozie.executor.jpa.CoordJobGetActionsJPAExecutor; 036 import org.apache.oozie.executor.jpa.CoordJobGetJPAExecutor; 037 import org.apache.oozie.executor.jpa.CoordJobUpdateJPAExecutor; 038 import org.apache.oozie.executor.jpa.JPAExecutorException; 039 import org.apache.oozie.service.JPAService; 040 import org.apache.oozie.service.Services; 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.StatusUtils; 045 046 /** 047 * Suspend coordinator job and actions. 048 * 049 */ 050 public class CoordSuspendXCommand extends SuspendTransitionXCommand { 051 private final String jobId; 052 private CoordinatorJobBean coordJob; 053 private JPAService jpaService; 054 private boolean exceptionOccured = false; 055 private CoordinatorJob.Status prevStatus = null; 056 057 public CoordSuspendXCommand(String id) { 058 super("coord_suspend", "coord_suspend", 1); 059 this.jobId = ParamChecker.notEmpty(id, "id"); 060 } 061 062 /* (non-Javadoc) 063 * @see org.apache.oozie.command.XCommand#getEntityKey() 064 */ 065 @Override 066 protected String getEntityKey() { 067 return jobId; 068 } 069 070 /* (non-Javadoc) 071 * @see org.apache.oozie.command.XCommand#isLockRequired() 072 */ 073 @Override 074 protected boolean isLockRequired() { 075 return true; 076 } 077 078 /* (non-Javadoc) 079 * @see org.apache.oozie.command.XCommand#loadState() 080 */ 081 @Override 082 protected void loadState() throws CommandException { 083 super.eagerLoadState(); 084 try { 085 jpaService = Services.get().get(JPAService.class); 086 if (jpaService != null) { 087 this.coordJob = jpaService.execute(new CoordJobGetJPAExecutor(this.jobId)); 088 prevStatus = coordJob.getStatus(); 089 } 090 else { 091 throw new CommandException(ErrorCode.E0610); 092 } 093 } 094 catch (Exception ex) { 095 throw new CommandException(ErrorCode.E0603, ex); 096 } 097 LogUtils.setLogInfo(this.coordJob, logInfo); 098 } 099 100 /* (non-Javadoc) 101 * @see org.apache.oozie.command.XCommand#verifyPrecondition() 102 */ 103 @Override 104 protected void verifyPrecondition() throws CommandException, PreconditionException { 105 super.eagerVerifyPrecondition(); 106 if (coordJob.getStatus() == CoordinatorJob.Status.SUCCEEDED 107 || coordJob.getStatus() == CoordinatorJob.Status.FAILED 108 || coordJob.getStatus() == CoordinatorJob.Status.KILLED) { 109 LOG.info("CoordSuspendXCommand is not going to execute because " 110 + "job finished or failed or killed, id = " + jobId + ", status = " + coordJob.getStatus()); 111 throw new PreconditionException(ErrorCode.E0728, jobId, coordJob.getStatus().toString()); 112 } 113 } 114 115 /* (non-Javadoc) 116 * @see org.apache.oozie.command.SuspendTransitionXCommand#suspendChildren() 117 */ 118 @Override 119 public void suspendChildren() throws CommandException { 120 try { 121 List<CoordinatorActionBean> actionList = jpaService.execute(new CoordJobGetActionsJPAExecutor(jobId)); 122 for (CoordinatorActionBean action : actionList) { 123 if (action.getStatus() == CoordinatorActionBean.Status.RUNNING) { 124 // queue a SuspendXCommand 125 if (action.getExternalId() != null) { 126 queue(new SuspendXCommand(action.getExternalId())); 127 updateCoordAction(action); 128 LOG.debug("Suspend coord action = [{0}], new status = [{1}], pending = [{2}] and queue SuspendXCommand for [{3}]", 129 action.getId(), action.getStatus(), action.getPending(), action.getExternalId()); 130 } else { 131 updateCoordAction(action); 132 LOG.debug("Suspend coord action = [{0}], new status = [{1}], pending = [{2}] and external id is null", 133 action.getId(), action.getStatus(), action.getPending()); 134 } 135 136 } 137 } 138 LOG.debug("Suspended coordinator actions for the coordinator=[{0}]", jobId); 139 } 140 catch (XException ex) { 141 exceptionOccured = true; 142 throw new CommandException(ex); 143 } 144 finally { 145 if (exceptionOccured) { 146 coordJob.setStatus(CoordinatorJob.Status.FAILED); 147 coordJob.resetPending(); 148 LOG.debug("Exception happened, fail coordinator job id = " + jobId + ", status = " + coordJob.getStatus()); 149 try { 150 jpaService.execute(new CoordJobUpdateJPAExecutor(coordJob)); 151 } 152 catch (JPAExecutorException je) { 153 LOG.error("Failed to update coordinator job : " + jobId, je); 154 } 155 } 156 } 157 } 158 159 /* (non-Javadoc) 160 * @see org.apache.oozie.command.TransitionXCommand#notifyParent() 161 */ 162 @Override 163 public void notifyParent() throws CommandException { 164 // update bundle action 165 if (this.coordJob.getBundleId() != null) { 166 BundleStatusUpdateXCommand bundleStatusUpdate = new BundleStatusUpdateXCommand(coordJob, prevStatus); 167 bundleStatusUpdate.call(); 168 } 169 } 170 171 /* (non-Javadoc) 172 * @see org.apache.oozie.command.TransitionXCommand#updateJob() 173 */ 174 @Override 175 public void updateJob() throws CommandException { 176 InstrumentUtils.incrJobCounter(getName(), 1, getInstrumentation()); 177 coordJob.setLastModifiedTime(new Date()); 178 coordJob.setSuspendedTime(new Date()); 179 LOG.debug("Suspend coordinator job id = " + jobId + ", status = " + coordJob.getStatus() + ", pending = " + coordJob.isPending()); 180 try { 181 jpaService.execute(new CoordJobUpdateJPAExecutor(coordJob)); 182 } 183 catch (JPAExecutorException e) { 184 throw new CommandException(e); 185 } 186 } 187 188 private void updateCoordAction(CoordinatorActionBean action) throws CommandException { 189 action.setStatus(CoordinatorActionBean.Status.SUSPENDED); 190 action.incrementAndGetPending(); 191 action.setLastModifiedTime(new Date()); 192 try { 193 jpaService.execute(new CoordActionUpdateJPAExecutor(action)); 194 } 195 catch (JPAExecutorException e) { 196 throw new CommandException(e); 197 } 198 } 199 200 /* (non-Javadoc) 201 * @see org.apache.oozie.command.TransitionXCommand#getJob() 202 */ 203 @Override 204 public Job getJob() { 205 return coordJob; 206 } 207 208 /** 209 * Transit job to suspended from running or to prepsuspended from prep. 210 * 211 * @see org.apache.oozie.command.TransitionXCommand#transitToNext() 212 */ 213 @Override 214 public void transitToNext() { 215 if (coordJob == null) { 216 coordJob = (CoordinatorJobBean) this.getJob(); 217 } 218 if (coordJob.getStatus() == Job.Status.PREP) { 219 coordJob.setStatus(Job.Status.PREPSUSPENDED); 220 coordJob.setStatus(StatusUtils.getStatus(coordJob)); 221 } 222 else if (coordJob.getStatus() == Job.Status.RUNNING) { 223 coordJob.setStatus(Job.Status.SUSPENDED); 224 } 225 coordJob.setPending(); 226 } 227 228 }