This project has retired. For details please refer to its
Attic page.
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.sql.Timestamp;
021 import java.util.Date;
022
023 import org.apache.oozie.CoordinatorActionBean;
024 import org.apache.oozie.ErrorCode;
025 import org.apache.oozie.WorkflowJobBean;
026 import org.apache.oozie.XException;
027 import org.apache.oozie.service.JPAService;
028 import org.apache.oozie.service.Services;
029 import org.apache.oozie.util.InstrumentUtils;
030 import org.apache.oozie.util.LogUtils;
031 import org.apache.oozie.util.ParamChecker;
032 import org.apache.oozie.util.db.SLADbOperations;
033 import org.apache.oozie.client.CoordinatorAction;
034 import org.apache.oozie.client.WorkflowJob;
035 import org.apache.oozie.client.SLAEvent.SlaAppType;
036 import org.apache.oozie.client.SLAEvent.Status;
037 import org.apache.oozie.command.CommandException;
038 import org.apache.oozie.command.PreconditionException;
039 import org.apache.oozie.executor.jpa.CoordActionGetJPAExecutor;
040 import org.apache.oozie.executor.jpa.WorkflowJobGetJPAExecutor;
041
042 /**
043 * The command checks workflow status for coordinator action.
044 */
045 public class CoordActionCheckXCommand extends CoordinatorXCommand<Void> {
046 private String actionId;
047 private int actionCheckDelay;
048 private CoordinatorActionBean coordAction = null;
049 private JPAService jpaService = null;
050
051 public CoordActionCheckXCommand(String actionId, int actionCheckDelay) {
052 super("coord_action_check", "coord_action_check", 0);
053 this.actionId = ParamChecker.notEmpty(actionId, "actionId");
054 this.actionCheckDelay = actionCheckDelay;
055 }
056
057 /* (non-Javadoc)
058 * @see org.apache.oozie.command.XCommand#execute()
059 */
060 @Override
061 protected Void execute() throws CommandException {
062 try {
063 InstrumentUtils.incrJobCounter(getName(), 1, getInstrumentation());
064 WorkflowJobBean wf = jpaService.execute(new WorkflowJobGetJPAExecutor(coordAction.getExternalId()));
065
066 Status slaStatus = null;
067
068 if (wf.getStatus() == WorkflowJob.Status.SUCCEEDED) {
069 coordAction.setStatus(CoordinatorAction.Status.SUCCEEDED);
070 slaStatus = Status.SUCCEEDED;
071 }
072 else {
073 if (wf.getStatus() == WorkflowJob.Status.FAILED) {
074 coordAction.setStatus(CoordinatorAction.Status.FAILED);
075 slaStatus = Status.FAILED;
076 }
077 else {
078 if (wf.getStatus() == WorkflowJob.Status.KILLED) {
079 coordAction.setStatus(CoordinatorAction.Status.KILLED);
080 slaStatus = Status.KILLED;
081 }
082 else {
083 LOG.warn("Unexpected workflow " + wf.getId() + " STATUS " + wf.getStatus());
084 coordAction.setLastModifiedTime(new Date());
085 jpaService.execute(new org.apache.oozie.executor.jpa.CoordActionUpdateJPAExecutor(coordAction));
086 return null;
087 }
088 }
089 }
090
091 LOG.debug("Updating Coordintaor actionId :" + coordAction.getId() + "status to ="
092 + coordAction.getStatus());
093 coordAction.setLastModifiedTime(new Date());
094 jpaService.execute(new org.apache.oozie.executor.jpa.CoordActionUpdateJPAExecutor(coordAction));
095
096 if (slaStatus != null) {
097 SLADbOperations.writeStausEvent(coordAction.getSlaXml(), coordAction.getId(), slaStatus,
098 SlaAppType.COORDINATOR_ACTION, LOG);
099 }
100 }
101 catch (XException ex) {
102 LOG.warn("CoordActionCheckCommand Failed ", ex);
103 throw new CommandException(ex);
104 }
105 return null;
106 }
107
108 /* (non-Javadoc)
109 * @see org.apache.oozie.command.XCommand#getEntityKey()
110 */
111 @Override
112 protected String getEntityKey() {
113 return actionId;
114 }
115
116 /* (non-Javadoc)
117 * @see org.apache.oozie.command.XCommand#isLockRequired()
118 */
119 @Override
120 protected boolean isLockRequired() {
121 return true;
122 }
123
124 /* (non-Javadoc)
125 * @see org.apache.oozie.command.XCommand#loadState()
126 */
127 @Override
128 protected void loadState() throws CommandException {
129 try {
130 jpaService = Services.get().get(JPAService.class);
131
132 if (jpaService != null) {
133 coordAction = jpaService.execute(new CoordActionGetJPAExecutor(actionId));
134 LogUtils.setLogInfo(coordAction, logInfo);
135 }
136 else {
137 throw new CommandException(ErrorCode.E0610);
138 }
139 }
140 catch (XException ex) {
141 throw new CommandException(ex);
142 }
143 }
144
145 /* (non-Javadoc)
146 * @see org.apache.oozie.command.XCommand#verifyPrecondition()
147 */
148 @Override
149 protected void verifyPrecondition() throws CommandException, PreconditionException {
150 // if the action has been updated, quit this command
151 Timestamp actionCheckTs = new Timestamp(System.currentTimeMillis() - actionCheckDelay * 1000);
152 Timestamp cactionLmt = coordAction.getLastModifiedTimestamp();
153 if (cactionLmt.after(actionCheckTs)) {
154 throw new PreconditionException(ErrorCode.E1100, "The coord action :" + actionId
155 + " has been udated. Ignore CoordActionCheckCommand!");
156 }
157 if (coordAction.getStatus().equals(CoordinatorAction.Status.SUCCEEDED)
158 || coordAction.getStatus().equals(CoordinatorAction.Status.FAILED)
159 || coordAction.getStatus().equals(CoordinatorAction.Status.KILLED)) {
160 throw new PreconditionException(ErrorCode.E1100);
161 }
162 }
163 }