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.bundle;
020
021import java.util.Date;
022import java.util.List;
023
024import org.apache.oozie.BundleActionBean;
025import org.apache.oozie.BundleJobBean;
026import org.apache.oozie.ErrorCode;
027import org.apache.oozie.client.Job;
028import org.apache.oozie.command.CommandException;
029import org.apache.oozie.command.PreconditionException;
030import org.apache.oozie.command.ResumeTransitionXCommand;
031import org.apache.oozie.command.coord.CoordResumeXCommand;
032import org.apache.oozie.executor.jpa.BatchQueryExecutor;
033import org.apache.oozie.executor.jpa.BundleActionQueryExecutor;
034import org.apache.oozie.executor.jpa.BundleJobQueryExecutor;
035import org.apache.oozie.executor.jpa.JPAExecutorException;
036import org.apache.oozie.executor.jpa.BatchQueryExecutor.UpdateEntry;
037import org.apache.oozie.executor.jpa.BundleActionQueryExecutor.BundleActionQuery;
038import org.apache.oozie.executor.jpa.BundleJobQueryExecutor.BundleJobQuery;
039import org.apache.oozie.service.JPAService;
040import org.apache.oozie.service.Services;
041import org.apache.oozie.util.InstrumentUtils;
042import org.apache.oozie.util.LogUtils;
043import org.apache.oozie.util.ParamChecker;
044
045public class BundleJobResumeXCommand extends ResumeTransitionXCommand {
046
047    private final String bundleId;
048    private JPAService jpaService = null;
049    private BundleJobBean bundleJob;
050    private List<BundleActionBean> bundleActions;
051
052    /**
053     * Constructor to create the Bundle Resume Command.
054     *
055     * @param jobId : Bundle Id
056     */
057    public BundleJobResumeXCommand(String jobId) {
058        super("bundle_resume", "bundle_resume", 1);
059        this.bundleId = ParamChecker.notNull(jobId, "BundleId");
060    }
061
062    /* (non-Javadoc)
063     * @see org.apache.oozie.command.ResumeTransitionXCommand#resumeChildren()
064     */
065    @Override
066    public void resumeChildren() {
067        for (BundleActionBean action : bundleActions) {
068            if (action.getStatus() == Job.Status.SUSPENDED
069                    || action.getStatus() == Job.Status.SUSPENDEDWITHERROR
070                    || action.getStatus() == Job.Status.PREPSUSPENDED) {
071                // queue a CoordResumeXCommand
072                if (action.getCoordId() != null) {
073                    queue(new CoordResumeXCommand(action.getCoordId()));
074                    updateBundleAction(action);
075                    LOG.debug("Resume bundle action = [{0}], new status = [{1}], "
076                            + "pending = [{2}] and queue CoordResumeXCommand for [{3}]",
077                            action.getBundleActionId(), action.getStatus(), action.getPending(), action
078                                            .getCoordId());
079                }
080                else {
081                    updateBundleAction(action);
082                    LOG.debug("Resume bundle action = [{0}], new status = [{1}], "
083                            + "pending = [{2}] and coord id is null",
084                            action.getBundleActionId(), action.getStatus(), action.getPending());
085                }
086            }
087        }
088        LOG.debug("Resume bundle actions for the bundle=[{0}]", bundleId);
089    }
090
091    private void updateBundleAction(BundleActionBean action) {
092        if (action.getStatus() == Job.Status.PREPSUSPENDED) {
093            action.setStatus(Job.Status.PREP);
094        }
095        else if (action.getStatus() == Job.Status.SUSPENDED) {
096            action.setStatus(Job.Status.RUNNING);
097        }
098        else if (action.getStatus() == Job.Status.SUSPENDEDWITHERROR) {
099            action.setStatus(Job.Status.RUNNINGWITHERROR);
100        }
101        action.incrementAndGetPending();
102        action.setLastModifiedTime(new Date());
103        updateList.add(new UpdateEntry<BundleActionQuery>(
104                BundleActionQuery.UPDATE_BUNDLE_ACTION_STATUS_PENDING_MODTIME, action));
105    }
106
107    /* (non-Javadoc)
108     * @see org.apache.oozie.command.TransitionXCommand#notifyParent()
109     */
110    @Override
111    public void notifyParent() throws CommandException {
112
113    }
114
115    /* (non-Javadoc)
116     * @see org.apache.oozie.command.TransitionXCommand#updateJob()
117     */
118    @Override
119    public void updateJob() {
120        InstrumentUtils.incrJobCounter("bundle_resume", 1, null);
121        bundleJob.setSuspendedTime(null);
122        bundleJob.setLastModifiedTime(new Date());
123        LOG.debug("Resume bundle job id = " + bundleId + ", "
124                + "status = " + bundleJob.getStatus() + ", pending = " + bundleJob.isPending());
125        updateList.add(new UpdateEntry<BundleJobQuery>(
126                BundleJobQuery.UPDATE_BUNDLE_JOB_STATUS_PENDING_SUSP_MOD_TIME, bundleJob));
127    }
128
129    /* (non-Javadoc)
130     * @see org.apache.oozie.command.ResumeTransitionXCommand#performWrites()
131     */
132    @Override
133    public void performWrites() throws CommandException {
134        try {
135            BatchQueryExecutor.getInstance().executeBatchInsertUpdateDelete(null, updateList, null);
136        }
137        catch (JPAExecutorException e) {
138            throw new CommandException(e);
139        }
140    }
141
142    /* (non-Javadoc)
143     * @see org.apache.oozie.command.XCommand#getEntityKey()
144     */
145    @Override
146    public String getEntityKey() {
147        return bundleId;
148    }
149
150    /* (non-Javadoc)
151     * @see org.apache.oozie.command.XCommand#isLockRequired()
152     */
153    @Override
154    protected boolean isLockRequired() {
155        return true;
156    }
157
158    /* (non-Javadoc)
159     * @see org.apache.oozie.command.XCommand#loadState()
160     */
161    @Override
162    protected void loadState() throws CommandException {
163        jpaService = Services.get().get(JPAService.class);
164        if (jpaService == null) {
165            throw new CommandException(ErrorCode.E0610);
166        }
167
168        try {
169            bundleJob = BundleJobQueryExecutor.getInstance().get(BundleJobQuery.GET_BUNDLE_JOB, bundleId);
170            bundleActions = BundleActionQueryExecutor.getInstance().getList(
171                    BundleActionQuery.GET_BUNDLE_ACTIONS_STATUS_UNIGNORED_FOR_BUNDLE, bundleId);
172        }
173        catch (Exception Ex) {
174            throw new CommandException(ErrorCode.E0604, bundleId);
175        }
176
177        LogUtils.setLogInfo(bundleJob);
178    }
179
180    /* (non-Javadoc)
181     * @see org.apache.oozie.command.XCommand#verifyPrecondition()
182     */
183    @Override
184    protected void verifyPrecondition() throws CommandException, PreconditionException {
185        if (bundleJob.getStatus() != Job.Status.SUSPENDED && bundleJob.getStatus() != Job.Status.SUSPENDEDWITHERROR
186                && bundleJob.getStatus() != Job.Status.PREPSUSPENDED) {
187            throw new PreconditionException(ErrorCode.E1100, "BundleResumeCommand not Resumed - "
188                    + "job not in SUSPENDED/SUSPENDEDWITHERROR/PREPSUSPENDED state " + bundleId);
189        }
190    }
191
192    /* (non-Javadoc)
193     * @see org.apache.oozie.command.TransitionXCommand#getJob()
194     */
195    @Override
196    public Job getJob() {
197        return bundleJob;
198    }
199}