001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.command.coord;
016    
017    import org.apache.oozie.CoordinatorJobBean;
018    import org.apache.oozie.client.CoordinatorJob;
019    import org.apache.oozie.command.CommandException;
020    import org.apache.oozie.store.CoordinatorStore;
021    import org.apache.oozie.store.StoreException;
022    import org.apache.oozie.util.XLog;
023    
024    public class CoordRecoveryCommand extends CoordinatorCommand<Void> {
025        private final XLog log = XLog.getLog(getClass());
026        private String jobId;
027    
028        public CoordRecoveryCommand(String id) {
029            super("coord_recovery", "coord_recovery", 1, XLog.STD);
030            this.jobId = id;
031        }
032    
033        @Override
034        protected Void call(CoordinatorStore store) throws StoreException {
035            //CoordinatorJobBean coordJob = store.getCoordinatorJob(jobId, true);
036            CoordinatorJobBean coordJob = store.getEntityManager().find(CoordinatorJobBean.class, jobId);
037            setLogInfo(coordJob);
038            if (coordJob.getStatus() == CoordinatorJob.Status.PREMATER) {
039                // update status of job from PREMATER to RUNNING in coordJob
040                coordJob.setStatus(CoordinatorJob.Status.RUNNING);
041                store.updateCoordinatorJob(coordJob);
042                log.debug("[" + jobId + "]: Recover status from PREMATER to RUNNING");
043            }
044            else {
045                log.debug("[" + jobId + "]: already in non-PREMATER status");
046            }
047            return null;
048        }
049    
050        @Override
051        protected Void execute(CoordinatorStore store) throws StoreException, CommandException {
052            log.info("STARTED CoordRecoveryCommand for jobId=" + jobId);
053            try {
054                if (lock(jobId)) {
055                    call(store);
056                }
057                else {
058                    queueCallable(new CoordRecoveryCommand(jobId), LOCK_FAILURE_REQUEUE_INTERVAL);
059                    log.warn("CoordRecoveryCommand lock was not acquired - failed jobId=" + jobId
060                            + ". Requeing the same.");
061                }
062            }
063            catch (InterruptedException e) {
064                queueCallable(new CoordRecoveryCommand(jobId), LOCK_FAILURE_REQUEUE_INTERVAL);
065                log.warn("CoordRecoveryCommand lock acquiring failed with exception " + e.getMessage()
066                        + " for jobId=" + jobId + " Requeing the same.");
067            }
068            finally {
069                log.info("ENDED CoordRecoveryCommand for jobId=" + jobId);
070            }
071            return null;
072        }
073    
074    }