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 org.apache.oozie.client.CoordinatorAction;
021    import org.apache.oozie.client.CoordinatorJob;
022    import org.apache.oozie.client.Job;
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.command.bundle.BundleStatusUpdateXCommand;
028    import org.apache.oozie.command.wf.KillXCommand;
029    import org.apache.oozie.command.CommandException;
030    import org.apache.oozie.command.KillTransitionXCommand;
031    import org.apache.oozie.command.PreconditionException;
032    import org.apache.oozie.dependency.DependencyChecker;
033    import org.apache.oozie.executor.jpa.BatchQueryExecutor;
034    import org.apache.oozie.executor.jpa.BatchQueryExecutor.UpdateEntry;
035    import org.apache.oozie.executor.jpa.CoordActionQueryExecutor.CoordActionQuery;
036    import org.apache.oozie.executor.jpa.CoordJobGetActionsNotCompletedJPAExecutor;
037    import org.apache.oozie.executor.jpa.CoordJobQueryExecutor;
038    import org.apache.oozie.executor.jpa.CoordJobQueryExecutor.CoordJobQuery;
039    import org.apache.oozie.executor.jpa.JPAExecutorException;
040    import org.apache.oozie.service.EventHandlerService;
041    import org.apache.oozie.service.JPAService;
042    import org.apache.oozie.service.Services;
043    import org.apache.oozie.util.LogUtils;
044    import org.apache.oozie.util.ParamChecker;
045    import org.apache.oozie.util.StatusUtils;
046    
047    import java.util.Arrays;
048    import java.util.Date;
049    import java.util.List;
050    
051    public class CoordKillXCommand extends KillTransitionXCommand {
052    
053        private final String jobId;
054        private CoordinatorJobBean coordJob;
055        private List<CoordinatorActionBean> actionList;
056        private JPAService jpaService = null;
057        private CoordinatorJob.Status prevStatus = null;
058    
059        public CoordKillXCommand(String id) {
060            super("coord_kill", "coord_kill", 2);
061            this.jobId = ParamChecker.notEmpty(id, "id");
062        }
063    
064        @Override
065        protected boolean isLockRequired() {
066            return true;
067        }
068    
069        @Override
070        public String getEntityKey() {
071            return this.jobId;
072        }
073    
074        @Override
075        public String getKey() {
076            return getName() + "_" + this.jobId;
077        }
078    
079        @Override
080        protected void loadState() throws CommandException {
081            try {
082                jpaService = Services.get().get(JPAService.class);
083    
084                if (jpaService != null) {
085                    this.coordJob = CoordJobQueryExecutor.getInstance().get(CoordJobQuery.GET_COORD_JOB_SUSPEND_KILL, jobId);
086                    //Get actions which are not succeeded, failed, timed out or killed
087                    this.actionList = jpaService.execute(new CoordJobGetActionsNotCompletedJPAExecutor(jobId));
088                    prevStatus = coordJob.getStatus();
089                    LogUtils.setLogInfo(coordJob, logInfo);
090                }
091                else {
092                    throw new CommandException(ErrorCode.E0610);
093                }
094            }
095            catch (XException ex) {
096                throw new CommandException(ex);
097            }
098        }
099    
100        @Override
101        protected void verifyPrecondition() throws CommandException, PreconditionException {
102            // if namespace 0.1 is used and backward support is true, SUCCEEDED coord job can be killed
103            if (StatusUtils.isV1CoordjobKillable(coordJob)) {
104                return;
105            }
106            if (coordJob.getStatus() == CoordinatorJob.Status.SUCCEEDED
107                    || coordJob.getStatus() == CoordinatorJob.Status.FAILED
108                    || coordJob.getStatus() == CoordinatorJob.Status.DONEWITHERROR
109                    || coordJob.getStatus() == CoordinatorJob.Status.KILLED
110                    || coordJob.getStatus() == CoordinatorJob.Status.IGNORED) {
111                LOG.info("CoordKillXCommand not killed - job either finished SUCCEEDED, FAILED, KILLED, DONEWITHERROR"
112                        + " or IGNORED,job id = " + jobId + ", status = " + coordJob.getStatus());
113                throw new PreconditionException(ErrorCode.E1020, jobId);
114            }
115        }
116    
117        private void updateCoordAction(CoordinatorActionBean action, boolean makePending) {
118            CoordinatorAction.Status prevStatus = action.getStatus();
119            action.setStatus(CoordinatorActionBean.Status.KILLED);
120            if (makePending) {
121                action.incrementAndGetPending();
122            } else {
123                // set pending to false
124                action.setPending(0);
125            }
126            if (EventHandlerService.isEnabled() && prevStatus != CoordinatorAction.Status.RUNNING
127                    && prevStatus != CoordinatorAction.Status.SUSPENDED) {
128                CoordinatorXCommand.generateEvent(action, coordJob.getUser(), coordJob.getAppName(), null);
129            }
130            action.setLastModifiedTime(new Date());
131            updateList.add(new UpdateEntry<CoordActionQuery>(CoordActionQuery.UPDATE_COORD_ACTION_STATUS_PENDING_TIME,action));
132        }
133    
134        @Override
135        public void killChildren() throws CommandException {
136            if (actionList != null) {
137                for (CoordinatorActionBean action : actionList) {
138                    // queue a WorkflowKillXCommand to delete the workflow job and actions
139                    if (action.getExternalId() != null) {
140                        queue(new KillXCommand(action.getExternalId()));
141                        // As the kill command for children is queued, set pending flag for coord action to be true
142                        updateCoordAction(action, true);
143                        LOG.debug(
144                                "Killed coord action = [{0}], new status = [{1}], pending = [{2}] and queue KillXCommand for [{3}]",
145                                action.getId(), action.getStatus(), action.getPending(), action.getExternalId());
146                    }
147                    else {
148                        // As killing children is not required, set pending flag for coord action to be false
149                        updateCoordAction(action, false);
150                        LOG.debug("Killed coord action = [{0}], current status = [{1}], pending = [{2}]",
151                                action.getId(), action.getStatus(), action.getPending());
152                    }
153                    String pushMissingDeps = action.getPushMissingDependencies();
154                    if (pushMissingDeps != null) {
155                        CoordPushDependencyCheckXCommand.unregisterMissingDependencies(
156                                Arrays.asList(DependencyChecker.dependenciesAsArray(pushMissingDeps)), action.getId());
157                    }
158                }
159            }
160            coordJob.setDoneMaterialization();
161            coordJob.setLastModifiedTime(new Date());
162            LOG.debug("Killed coord actions for the coordinator=[{0}]", jobId);
163        }
164    
165        @Override
166        public void notifyParent() throws CommandException {
167            // update bundle action
168            if (coordJob.getBundleId() != null) {
169                BundleStatusUpdateXCommand bundleStatusUpdate = new BundleStatusUpdateXCommand(coordJob, prevStatus);
170                bundleStatusUpdate.call();
171            }
172        }
173    
174        @Override
175        public void updateJob() throws CommandException {
176            updateList.add(new UpdateEntry<CoordJobQuery>(CoordJobQuery.UPDATE_COORD_JOB_STATUS_PENDING_TIME, coordJob));
177        }
178    
179        @Override
180        public void performWrites() throws CommandException {
181            try {
182                BatchQueryExecutor.getInstance().executeBatchInsertUpdateDelete(null, updateList, null);
183            }
184            catch (JPAExecutorException e) {
185                throw new CommandException(e);
186            }
187        }
188    
189        @Override
190        public Job getJob() {
191            return coordJob;
192        }
193    
194    }