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;
020
021import java.io.IOException;
022import java.io.Writer;
023import java.util.Map;
024
025import org.apache.hadoop.conf.Configuration;
026import org.apache.oozie.client.CoordinatorJob;
027import org.apache.oozie.client.WorkflowJob;
028import org.apache.oozie.executor.jpa.JPAExecutorException;
029import org.apache.oozie.service.JMSTopicService;
030import org.apache.oozie.service.Services;
031
032public abstract class BaseEngine {
033    public static final String USE_XCOMMAND = "oozie.useXCommand";
034
035    protected String user;
036
037    /**
038     * Return the user name.
039     *
040     * @return the user name.
041     */
042    public String getUser() {
043        return user;
044    }
045
046    /**
047     * Submit a job.
048     * <p/>
049     * It validates configuration properties.
050     *
051     * @param conf job configuration.
052     * @param startJob indicates if the job should be started or not.
053     * @return the job Id.
054     * @throws BaseEngineException thrown if the job could not be created.
055     */
056    public abstract String submitJob(Configuration conf, boolean startJob) throws BaseEngineException;
057
058    /**
059     * Start a job.
060     *
061     * @param jobId job Id.
062     * @throws BaseEngineException thrown if the job could not be started.
063     */
064    public abstract void start(String jobId) throws BaseEngineException;
065
066    /**
067     * Resume a job.
068     *
069     * @param jobId job Id.
070     * @throws BaseEngineException thrown if the job could not be resumed.
071     */
072    public abstract void resume(String jobId) throws BaseEngineException;
073
074    /**
075     * Suspend a job.
076     *
077     * @param jobId job Id.
078     * @throws BaseEngineException thrown if the job could not be suspended.
079     */
080    public abstract void suspend(String jobId) throws BaseEngineException;
081
082    /**
083     * Kill a job.
084     *
085     * @param jobId job Id.
086     * @throws BaseEngineException thrown if the job could not be killed.
087     */
088    public abstract void kill(String jobId) throws BaseEngineException;
089
090    /**
091     * Change a coordinator job.
092     *
093     * @param jobId job Id.
094     * @param changeValue change value.
095     * @throws BaseEngineException thrown if the job could not be changed.
096     */
097    public abstract void change(String jobId, String changeValue) throws BaseEngineException;
098
099    /**
100     * Rerun a job.
101     *
102     * @param jobId job Id to rerun.
103     * @param conf configuration information for the rerun.
104     * @throws BaseEngineException thrown if the job could not be rerun.
105     */
106    public abstract void reRun(String jobId, Configuration conf) throws BaseEngineException;
107
108    /**
109     * Return the info about a wf job.
110     *
111     * @param jobId job Id.
112     * @return the workflow job info.
113     * @throws DagEngineException thrown if the job info could not be obtained.
114     */
115    public abstract WorkflowJob getJob(String jobId) throws BaseEngineException;
116
117    /**
118     * Return the info about a wf job with actions subset.
119     *
120     * @param jobId job Id
121     * @param start starting from this index in the list of actions belonging to the job
122     * @param length number of actions to be returned
123     * @return the workflow job info.
124     * @throws DagEngineException thrown if the job info could not be obtained.
125     */
126    public abstract WorkflowJob getJob(String jobId, int start, int length) throws BaseEngineException;
127
128    /**
129     * Return the info about a coord job.
130     *
131     * @param jobId job Id.
132     * @return the coord job info.
133     * @throws BaseEngineException thrown if the job info could not be obtained.
134     */
135    public abstract CoordinatorJob getCoordJob(String jobId) throws BaseEngineException;
136
137    /**
138     * Return the info about a coord job with actions subset.
139     *
140     * @param jobId job Id.
141     * @param filter the status filter
142     * @param start starting from this index in the list of actions belonging to the job
143     * @param length number of actions to be returned
144     * @param order true if actions are sorted in a descending order of nominal time, false if asc order
145     * @return the coord job info.
146     * @throws BaseEngineException thrown if the job info could not be obtained.
147     */
148    public abstract CoordinatorJob getCoordJob(String jobId, String filter, int start, int length, boolean desc)
149            throws BaseEngineException;
150
151    /**
152     * Return the a job definition.
153     *
154     * @param jobId job Id.
155     * @return the job definition.
156     * @throws BaseEngineException thrown if the job definition could no be obtained.
157     */
158    public abstract String getDefinition(String jobId) throws BaseEngineException;
159
160    /**
161     * Stream the log of a job.
162     *
163     * @param jobId job Id.
164     * @param writer writer to stream the log to.
165     * @param params additional parameters from the request
166     * @throws IOException thrown if the log cannot be streamed.
167     * @throws BaseEngineException thrown if there is error in getting the Workflow/Coordinator Job Information for
168     *         jobId.
169     */
170    public abstract void streamLog(String jobId, Writer writer, Map<String, String[]> params)
171            throws IOException, BaseEngineException;
172
173    /**
174     * Return the workflow Job ID for an external ID.
175     * <p/>
176     * This is reverse lookup for recovery purposes.
177     *
178     * @param externalId external ID provided at job submission time.
179     * @return the associated workflow job ID if any, <code>null</code> if none.
180     * @throws BaseEngineException thrown if the lookup could not be done.
181     */
182    public abstract String getJobIdForExternalId(String externalId) throws BaseEngineException;
183
184    /**
185     * Dry run a job; like {@link BaseEngine#submitJob(org.apache.hadoop.conf.Configuration, boolean) but doesn't actually execute
186     * the job.
187     * <p/>
188     * It validates configuration properties.
189     *
190     * @param conf job configuration.
191     * @return the result of the dryrun
192     * @throws BaseEngineException thrown if there was a problem doing the dryrun
193     */
194    public abstract String dryRunSubmit(Configuration conf) throws BaseEngineException;
195
196
197    /**
198     * Return the jms topic name for the job.
199     *
200     * @param jobId job Id.
201     * @return String the topic name
202     * @throws DagEngineException thrown if the jms info could not be obtained.
203     */
204    public String getJMSTopicName(String jobId) throws DagEngineException {
205        JMSTopicService jmsTopicService = Services.get().get(JMSTopicService.class);
206        if (jmsTopicService != null) {
207            try {
208                return jmsTopicService.getTopic(jobId);
209            }
210            catch (JPAExecutorException e) {
211               throw new DagEngineException(ErrorCode.E1602, e);
212            }
213        }
214        else {
215            throw new DagEngineException(ErrorCode.E1602,
216                    "JMSTopicService is not initialized. JMS notification"
217                            + "may not be enabled");
218        }
219    }
220
221    /**
222     * Return the status for a Job ID
223     *
224     * @param jobId job Id.
225     * @return the job's status
226     * @throws BaseEngineException thrown if the job's status could not be obtained
227     */
228    public abstract String getJobStatus(String jobId) throws BaseEngineException;
229}