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;
016    
017    import java.io.IOException;
018    import java.io.Writer;
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.HashSet;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.Properties;
025    import java.util.Set;
026    import java.util.StringTokenizer;
027    
028    import org.apache.hadoop.conf.Configuration;
029    import org.apache.oozie.client.CoordinatorJob;
030    import org.apache.oozie.client.OozieClient;
031    import org.apache.oozie.client.WorkflowJob;
032    import org.apache.oozie.command.CommandException;
033    import org.apache.oozie.command.wf.CompletedActionCommand;
034    import org.apache.oozie.command.wf.DefinitionCommand;
035    import org.apache.oozie.command.wf.ExternalIdCommand;
036    import org.apache.oozie.command.wf.JobCommand;
037    import org.apache.oozie.command.wf.JobsCommand;
038    import org.apache.oozie.command.wf.KillCommand;
039    import org.apache.oozie.command.wf.ReRunCommand;
040    import org.apache.oozie.command.wf.ResumeCommand;
041    import org.apache.oozie.command.wf.StartCommand;
042    import org.apache.oozie.command.wf.SubmitCommand;
043    import org.apache.oozie.command.wf.SuspendCommand;
044    import org.apache.oozie.service.DagXLogInfoService;
045    import org.apache.oozie.service.Services;
046    import org.apache.oozie.service.XLogService;
047    import org.apache.oozie.util.ParamChecker;
048    import org.apache.oozie.util.XLog;
049    import org.apache.oozie.util.XLogStreamer;
050    
051    public abstract class BaseEngine {
052    
053        protected String user;
054        protected String authToken;
055    
056        /**
057         * Return the user name.
058         *
059         * @return the user name.
060         */
061        public String getUser() {
062            return user;
063        }
064    
065        /**
066         * Return the authentication token.
067         *
068         * @return the authentication token.
069         */
070        protected String getAuthToken() {
071            return authToken;
072        }
073    
074        /**
075         * Submit a job. <p/> It validates configuration properties.
076         *
077         * @param conf job configuration.
078         * @param startJob indicates if the job should be started or not.
079         * @return the job Id.
080         * @throws BaseEngineException thrown if the job could not be created.
081         */
082        public abstract String submitJob(Configuration conf, boolean startJob) throws BaseEngineException;
083    
084        /**
085         * Start a job.
086         *
087         * @param jobId job Id.
088         * @throws BaseEngineException thrown if the job could not be started.
089         */
090        public abstract void start(String jobId) throws BaseEngineException;
091    
092        /**
093         * Resume a job.
094         *
095         * @param jobId job Id.
096         * @throws BaseEngineException thrown if the job could not be resumed.
097         */
098        public abstract void resume(String jobId) throws BaseEngineException;
099    
100        /**
101         * Suspend a job.
102         *
103         * @param jobId job Id.
104         * @throws BaseEngineException thrown if the job could not be suspended.
105         */
106        public abstract void suspend(String jobId) throws BaseEngineException;
107    
108        /**
109         * Kill a job.
110         *
111         * @param jobId job Id.
112         * @throws BaseEngineException thrown if the job could not be killed.
113         */
114        public abstract void kill(String jobId) throws BaseEngineException;
115    
116        /**
117         * Change a coordinator job.
118         *
119         * @param jobId job Id.
120         * @param changeValue change value.
121         * @throws BaseEngineException thrown if the job could not be changed.
122         */
123        public abstract void change(String jobId, String changeValue) throws BaseEngineException;
124    
125        /**
126         * Rerun a job.
127         *
128         * @param jobId job Id to rerun.
129         * @param conf configuration information for the rerun.
130         * @throws BaseEngineException thrown if the job could not be rerun.
131         */
132        public abstract void reRun(String jobId, Configuration conf) throws BaseEngineException;
133    
134    
135        /**
136         * Return the info about a wf job.
137         *
138         * @param jobId job Id.
139         * @return the workflow job info.
140         * @throws DagEngineException thrown if the job info could not be obtained.
141         */
142        public abstract WorkflowJob getJob(String jobId) throws BaseEngineException;
143    
144        /**
145         * Return the info about a wf job with actions subset.
146         *
147         * @param jobId job Id
148         * @param start starting from this index in the list of actions belonging to the job
149         * @param length number of actions to be returned
150         * @return the workflow job info.
151         * @throws DagEngineException thrown if the job info could not be obtained.
152         */
153        public abstract WorkflowJob getJob(String jobId, int start, int length) throws BaseEngineException;
154    
155        /**
156         * Return the info about a coord job.
157         *
158         * @param jobId job Id.
159         * @return the coord job info.
160         * @throws BaseEngineException thrown if the job info could not be obtained.
161         */
162        public abstract CoordinatorJob getCoordJob(String jobId) throws BaseEngineException;
163    
164        /**
165         * Return the info about a coord job with actions subset.
166         *
167         * @param jobId job Id.
168         * @param start starting from this index in the list of actions belonging to the job
169         * @param length number of actions to be returned
170         * @return the coord job info.
171         * @throws BaseEngineException thrown if the job info could not be obtained.
172         */
173        public abstract CoordinatorJob getCoordJob(String jobId, int start, int length) throws BaseEngineException;
174    
175        /**
176         * Return the a job definition.
177         *
178         * @param jobId job Id.
179         * @return the job definition.
180         * @throws BaseEngineException thrown if the job definition could no be obtained.
181         */
182        public abstract String getDefinition(String jobId) throws BaseEngineException;
183    
184        /**
185         * Stream the log of a job.
186         *
187         * @param jobId job Id.
188         * @param writer writer to stream the log to.
189         * @throws IOException thrown if the log cannot be streamed.
190         * @throws BaseEngineException thrown if there is error in getting the Workflow/Coordinator Job Information for
191         * jobId.
192         */
193        public abstract void streamLog(String jobId, Writer writer) throws IOException, BaseEngineException;
194    
195        /**
196         * Return the workflow Job ID for an external ID. <p/> This is reverse lookup for recovery purposes.
197         *
198         * @param externalId external ID provided at job submission time.
199         * @return the associated workflow job ID if any, <code>null</code> if none.
200         * @throws BaseEngineException thrown if the lookup could not be done.
201         */
202        public abstract String getJobIdForExternalId(String externalId) throws BaseEngineException;
203    
204        public abstract String dryrunSubmit(Configuration conf, boolean startJob)
205                throws BaseEngineException;
206    
207    }