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 019package org.apache.oozie.command.coord; 020 021import java.util.Date; 022import java.util.List; 023 024import org.apache.oozie.CoordinatorActionBean; 025import org.apache.oozie.CoordinatorJobBean; 026import org.apache.oozie.ErrorCode; 027import org.apache.oozie.XException; 028import org.apache.oozie.client.CoordinatorJob; 029import org.apache.oozie.client.Job; 030import org.apache.oozie.command.CommandException; 031import org.apache.oozie.command.PreconditionException; 032import org.apache.oozie.command.ResumeTransitionXCommand; 033import org.apache.oozie.command.bundle.BundleStatusUpdateXCommand; 034import org.apache.oozie.command.wf.ResumeXCommand; 035import org.apache.oozie.executor.jpa.BatchQueryExecutor; 036import org.apache.oozie.executor.jpa.BatchQueryExecutor.UpdateEntry; 037import org.apache.oozie.executor.jpa.CoordActionQueryExecutor.CoordActionQuery; 038import org.apache.oozie.executor.jpa.CoordJobGetActionsSuspendedJPAExecutor; 039import org.apache.oozie.executor.jpa.CoordJobGetJPAExecutor; 040import org.apache.oozie.executor.jpa.JPAExecutorException; 041import org.apache.oozie.executor.jpa.CoordJobQueryExecutor.CoordJobQuery; 042import org.apache.oozie.service.EventHandlerService; 043import org.apache.oozie.service.JPAService; 044import org.apache.oozie.service.Services; 045import org.apache.oozie.util.InstrumentUtils; 046import org.apache.oozie.util.LogUtils; 047import org.apache.oozie.util.ParamChecker; 048import org.apache.oozie.util.XLog; 049 050/** 051 * Resume coordinator job and actions. 052 * 053 */ 054public class CoordResumeXCommand extends ResumeTransitionXCommand { 055 private final String jobId; 056 private CoordinatorJobBean coordJob = null; 057 private JPAService jpaService = null; 058 private boolean exceptionOccured = false; 059 CoordinatorJob.Status prevStatus; 060 061 public CoordResumeXCommand(String id) { 062 super("coord_resume", "coord_resume", 1); 063 this.jobId = ParamChecker.notEmpty(id, "id"); 064 } 065 066 @Override 067 public String getEntityKey() { 068 return jobId; 069 } 070 071 @Override 072 public String getKey() { 073 return getName() + "_" + this.jobId; 074 } 075 076 @Override 077 protected boolean isLockRequired() { 078 return true; 079 } 080 081 @Override 082 protected void loadState() throws CommandException { 083 jpaService = Services.get().get(JPAService.class); 084 if (jpaService == null) { 085 throw new CommandException(ErrorCode.E0610); 086 } 087 try { 088 coordJob = jpaService.execute(new CoordJobGetJPAExecutor(jobId)); 089 } 090 catch (JPAExecutorException e) { 091 throw new CommandException(e); 092 } 093 setJob(coordJob); 094 prevStatus = coordJob.getStatus(); 095 LogUtils.setLogInfo(coordJob); 096 } 097 098 @Override 099 protected void verifyPrecondition() throws CommandException, PreconditionException { 100 if (coordJob.getStatus() != CoordinatorJob.Status.SUSPENDED && coordJob.getStatus() != CoordinatorJob.Status 101 .SUSPENDEDWITHERROR && coordJob.getStatus() != Job.Status.PREPSUSPENDED) { 102 throw new PreconditionException(ErrorCode.E1100, "CoordResumeXCommand not Resumed - " 103 + "job not in SUSPENDED/SUSPENDEDWITHERROR/PREPSUSPENDED state, job = " + jobId); 104 } 105 } 106 107 @Override 108 public void updateJob() { 109 InstrumentUtils.incrJobCounter(getName(), 1, getInstrumentation()); 110 coordJob.setSuspendedTime(null); 111 coordJob.setLastModifiedTime(new Date()); 112 LOG.debug("Resume coordinator job id = " + jobId + ", status = " + coordJob.getStatus() + ", pending = " 113 + coordJob.isPending()); 114 updateList.add(new UpdateEntry<CoordJobQuery>(CoordJobQuery.UPDATE_COORD_JOB_STATUS_PENDING_TIME, coordJob)); 115 } 116 117 @Override 118 public void resumeChildren() throws CommandException { 119 try { 120 // Get all suspended actions to resume them 121 List<CoordinatorActionBean> actionList = jpaService.execute(new CoordJobGetActionsSuspendedJPAExecutor( 122 jobId)); 123 124 for (CoordinatorActionBean action : actionList) { 125 if (action.getExternalId() != null) { 126 // queue a ResumeXCommand 127 queue(new ResumeXCommand(action.getExternalId())); 128 updateCoordAction(action); 129 LOG.debug( 130 "Resume coord action = [{0}], new status = [{1}], pending = [{2}] and queue ResumeXCommand for [{3}]", 131 action.getId(), action.getStatus(), action.getPending(), action.getExternalId()); 132 } 133 else { 134 updateCoordAction(action); 135 LOG.debug( 136 "Resume coord action = [{0}], new status = [{1}], pending = [{2}] and external id is null", 137 action.getId(), action.getStatus(), action.getPending()); 138 } 139 } 140 141 } 142 catch (XException ex) { 143 exceptionOccured = true; 144 throw new CommandException(ex); 145 } 146 finally { 147 if (exceptionOccured) { 148 coordJob.setStatus(CoordinatorJob.Status.FAILED); 149 coordJob.resetPending(); 150 LOG.warn("Resume children failed so fail coordinator, coordinator job id = " + jobId + ", status = " 151 + coordJob.getStatus()); 152 updateList.add(new UpdateEntry<CoordJobQuery>(CoordJobQuery.UPDATE_COORD_JOB_STATUS_PENDING_TIME, 153 coordJob)); 154 } 155 } 156 } 157 158 @Override 159 public void notifyParent() throws CommandException { 160 // update bundle action 161 if (this.coordJob.getBundleId() != null) { 162 BundleStatusUpdateXCommand bundleStatusUpdate = new BundleStatusUpdateXCommand(coordJob, prevStatus); 163 bundleStatusUpdate.call(); 164 } 165 } 166 167 @Override 168 public void performWrites() throws CommandException { 169 try { 170 BatchQueryExecutor.getInstance().executeBatchInsertUpdateDelete(null, updateList, null); 171 if (EventHandlerService.isEnabled()) { 172 // good enough to set event start time as coord's last modified time 173 // updated when set to running 174 generateEvents(coordJob, coordJob.getLastModifiedTime()); 175 } 176 } 177 catch (JPAExecutorException e) { 178 throw new CommandException(e); 179 } 180 } 181 182 private void updateCoordAction(CoordinatorActionBean action) { 183 action.setStatus(CoordinatorActionBean.Status.RUNNING); 184 action.incrementAndGetPending(); 185 action.setLastModifiedTime(new Date()); 186 updateList.add(new UpdateEntry<CoordActionQuery>(CoordActionQuery.UPDATE_COORD_ACTION_STATUS_PENDING_TIME, action)); 187 } 188 189 @Override 190 public Job getJob() { 191 return coordJob; 192 } 193}