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.util.ArrayList;
018 import java.util.Collections;
019 import java.util.Iterator;
020 import java.util.List;
021 import java.util.Properties;
022
023 import org.apache.oozie.client.CoordinatorAction;
024 import org.apache.oozie.client.CoordinatorJob;
025 import org.apache.oozie.client.OozieClient;
026 import org.apache.oozie.client.OozieClientException;
027 import org.apache.oozie.client.WorkflowJob;
028 import org.apache.oozie.client.rest.JsonCoordinatorAction;
029 import org.apache.oozie.util.XConfiguration;
030
031 /**
032 * Client API to submit and manage Oozie coordinator jobs against an Oozie
033 * intance.
034 * <p/>
035 * This class is thread safe.
036 * <p/>
037 * Syntax for filter for the {@link #getJobsInfo(String)}
038 * {@link #getJobsInfo(String, int, int)} methods:
039 * <code>[NAME=VALUE][;NAME=VALUE]*</code>.
040 * <p/>
041 * Valid filter names are:
042 * <p/>
043 * <ul/>
044 * <li>name: the coordinator application name from the coordinator definition.</li>
045 * <li>user: the user that submitted the job.</li>
046 * <li>group: the group for the job.</li>
047 * <li>status: the status of the job.</li>
048 * </ul>
049 * <p/>
050 * The query will do an AND among all the filter names. The query will do an OR
051 * among all the filter values for the same name. Multiple values must be
052 * specified as different name value pairs.
053 */
054 public class LocalOozieClientCoord extends OozieClient {
055
056 private CoordinatorEngine coordEngine;
057
058 /**
059 * Create a coordinator client for Oozie local use.
060 * <p/>
061 *
062 * @param coordEngine the engine instance to use.
063 */
064 public LocalOozieClientCoord(CoordinatorEngine coordEngine) {
065 this.coordEngine = coordEngine;
066 }
067
068 /**
069 * Return the Oozie URL of the coordinator client instance.
070 * <p/>
071 * This URL is the base URL fo the Oozie system, with not protocol
072 * versioning.
073 *
074 * @return the Oozie URL of the coordinator client instance.
075 */
076 @Override
077 public String getOozieUrl() {
078 return "localoozie";
079 }
080
081 /**
082 * Return the Oozie URL used by the client and server for WS communications.
083 * <p/>
084 * This URL is the original URL plus the versioning element path.
085 *
086 * @return the Oozie URL used by the client and server for communication.
087 * @throws org.apache.oozie.client.OozieClientException thrown in the client
088 * and the server are not protocol compatible.
089 */
090 @Override
091 public String getProtocolUrl() throws OozieClientException {
092 return "localoozie";
093 }
094
095 /**
096 * Validate that the Oozie client and server instances are protocol
097 * compatible.
098 *
099 * @throws org.apache.oozie.client.OozieClientException thrown in the client
100 * and the server are not protocol compatible.
101 */
102 @Override
103 public synchronized void validateWSVersion() throws OozieClientException {
104 }
105
106 /**
107 * Create an empty configuration with just the {@link #USER_NAME} set to the
108 * JVM user name and the {@link #GROUP_NAME} set to 'other'.
109 *
110 * @return an empty configuration.
111 */
112 @Override
113 public Properties createConfiguration() {
114 Properties conf = new Properties();
115 if (coordEngine != null) {
116 conf.setProperty(USER_NAME, coordEngine.getUser());
117 }
118 conf.setProperty(GROUP_NAME, "users");
119 return conf;
120 }
121
122 /**
123 * Set a HTTP header to be used in the WS requests by the coordinator
124 * instance.
125 *
126 * @param name header name.
127 * @param value header value.
128 */
129 @Override
130 public void setHeader(String name, String value) {
131 }
132
133 /**
134 * Get the value of a set HTTP header from the coordinator instance.
135 *
136 * @param name header name.
137 * @return header value, <code>null</code> if not set.
138 */
139 @Override
140 public String getHeader(String name) {
141 return null;
142 }
143
144 /**
145 * Remove a HTTP header from the coordinator client instance.
146 *
147 * @param name header name.
148 */
149 @Override
150 public void removeHeader(String name) {
151 }
152
153 /**
154 * Return an iterator with all the header names set in the coordinator
155 * instance.
156 *
157 * @return header names.
158 */
159 @Override
160 @SuppressWarnings("unchecked")
161 public Iterator<String> getHeaderNames() {
162 return Collections.EMPTY_SET.iterator();
163 }
164
165 /**
166 * Submit a coordinator job.
167 *
168 * @param conf job configuration.
169 * @return the job Id.
170 * @throws org.apache.oozie.client.OozieClientException thrown if the job
171 * could not be submitted.
172 */
173 @Override
174 public String submit(Properties conf) throws OozieClientException {
175 try {
176 return coordEngine.submitJob(new XConfiguration(conf), false);
177 }
178 catch (CoordinatorEngineException ex) {
179 throw new OozieClientException(ex.getErrorCode().toString(), ex);
180 }
181 }
182
183 /**
184 * Start a coordinator job.
185 *
186 * @param jobId job Id.
187 * @throws org.apache.oozie.client.OozieClientException thrown if the job
188 * could not be started.
189 */
190 @Override
191 @Deprecated
192 public void start(String jobId) throws OozieClientException {
193 try {
194 coordEngine.start(jobId);
195 }
196 catch (CoordinatorEngineException ex) {
197 throw new OozieClientException(ex.getErrorCode().toString(), ex);
198 }
199 catch (BaseEngineException bex) {
200 throw new OozieClientException(bex.getErrorCode().toString(), bex);
201 }
202 }
203
204 /**
205 * Submit and start a coordinator job.
206 *
207 * @param conf job configuration.
208 * @return the job Id.
209 * @throws org.apache.oozie.client.OozieClientException thrown if the job
210 * could not be submitted.
211 */
212 @Override
213 public String run(Properties conf) throws OozieClientException {
214 try {
215 return coordEngine.submitJob(new XConfiguration(conf), true);
216 }
217 catch (CoordinatorEngineException ex) {
218 throw new OozieClientException(ex.getErrorCode().toString(), ex);
219 }
220 }
221
222 /**
223 * Rerun a workflow job.
224 *
225 * @param jobId job Id to rerun.
226 * @param conf configuration information for the rerun.
227 * @throws org.apache.oozie.client.OozieClientException thrown if the job
228 * could not be started.
229 */
230 @Override
231 @Deprecated
232 public void reRun(String jobId, Properties conf) throws OozieClientException {
233 throw new OozieClientException(ErrorCode.E0301.toString(), "no-op");
234 }
235
236 /**
237 * Rerun coordinator actions.
238 *
239 * @param jobId coordinator jobId
240 * @param rerunType rerun type 'date' if -date is used, 'action-id' if
241 * -action is used
242 * @param scope rerun scope for date or actionIds
243 * @param refresh true if -refresh is given in command option
244 * @param noCleanup true if -nocleanup is given in command option
245 * @throws OozieClientException
246 */
247 @Override
248 public List<JsonCoordinatorAction> reRunCoord(String jobId, String rerunType, String scope, boolean refresh,
249 boolean noCleanup) throws OozieClientException {
250 try {
251 CoordinatorActionInfo coordInfo = coordEngine.reRun(jobId, rerunType, scope, Boolean.valueOf(refresh),
252 Boolean.valueOf(noCleanup));
253 List<CoordinatorActionBean> actionBeans = coordInfo.getCoordActions();
254 List<JsonCoordinatorAction> actions = new ArrayList<JsonCoordinatorAction>();
255 for (CoordinatorActionBean actionBean : actionBeans) {
256 actions.add(actionBean);
257 }
258 return actions;
259 }
260 catch (BaseEngineException ex) {
261 throw new OozieClientException(ex.getErrorCode().toString(), ex);
262 }
263 }
264
265 /**
266 * Suspend a coordinator job.
267 *
268 * @param jobId job Id.
269 * @throws org.apache.oozie.client.OozieClientException thrown if the job
270 * could not be suspended.
271 */
272 @Override
273 public void suspend(String jobId) throws OozieClientException {
274 try {
275 coordEngine.suspend(jobId);
276 }
277 catch (CoordinatorEngineException ex) {
278 throw new OozieClientException(ex.getErrorCode().toString(), ex);
279 }
280 }
281
282 /**
283 * Resume a coordinator job.
284 *
285 * @param jobId job Id.
286 * @throws org.apache.oozie.client.OozieClientException thrown if the job
287 * could not be resume.
288 */
289 @Override
290 public void resume(String jobId) throws OozieClientException {
291 try {
292 coordEngine.resume(jobId);
293 }
294 catch (CoordinatorEngineException ex) {
295 throw new OozieClientException(ex.getErrorCode().toString(), ex);
296 }
297 }
298
299 /**
300 * Kill a coordinator job.
301 *
302 * @param jobId job Id.
303 * @throws org.apache.oozie.client.OozieClientException thrown if the job
304 * could not be killed.
305 */
306 @Override
307 public void kill(String jobId) throws OozieClientException {
308 try {
309 coordEngine.kill(jobId);
310 }
311 catch (CoordinatorEngineException ex) {
312 throw new OozieClientException(ex.getErrorCode().toString(), ex);
313 }
314 }
315
316 /**
317 * Get the info of a workflow job.
318 *
319 * @param jobId job Id.
320 * @return the job info.
321 * @throws org.apache.oozie.client.OozieClientException thrown if the job
322 * info could not be retrieved.
323 */
324 @Override
325 @Deprecated
326 public WorkflowJob getJobInfo(String jobId) throws OozieClientException {
327 throw new OozieClientException(ErrorCode.E0301.toString(), "no-op");
328 }
329
330 /**
331 * Get the info of a coordinator job.
332 *
333 * @param jobId job Id.
334 * @return the job info.
335 * @throws org.apache.oozie.client.OozieClientException thrown if the job
336 * info could not be retrieved.
337 */
338 @Override
339 public CoordinatorJob getCoordJobInfo(String jobId) throws OozieClientException {
340 try {
341 return coordEngine.getCoordJob(jobId);
342 }
343 catch (CoordinatorEngineException ex) {
344 throw new OozieClientException(ex.getErrorCode().toString(), ex);
345 }
346 catch (BaseEngineException bex) {
347 throw new OozieClientException(bex.getErrorCode().toString(), bex);
348 }
349 }
350
351 /**
352 * Get the info of a coordinator action.
353 *
354 * @param actionId Id.
355 * @return the coordinator action info.
356 * @throws OozieClientException thrown if the job info could not be
357 * retrieved.
358 */
359 @Override
360 public CoordinatorAction getCoordActionInfo(String actionId) throws OozieClientException {
361 try {
362 return coordEngine.getCoordAction(actionId);
363 }
364 catch (CoordinatorEngineException ex) {
365 throw new OozieClientException(ex.getErrorCode().toString(), ex);
366 }
367 catch (BaseEngineException bex) {
368 throw new OozieClientException(bex.getErrorCode().toString(), bex);
369 }
370 }
371
372 /**
373 * Return the info of the workflow jobs that match the filter.
374 *
375 * @param filter job filter. Refer to the {@link OozieClient} for the filter
376 * syntax.
377 * @param start jobs offset, base 1.
378 * @param len number of jobs to return.
379 * @return a list with the workflow jobs info, without node details.
380 * @throws OozieClientException thrown if the jobs info could not be
381 * retrieved.
382 */
383 @Override
384 @Deprecated
385 public List<WorkflowJob> getJobsInfo(String filter, int start, int len) throws OozieClientException {
386 throw new OozieClientException(ErrorCode.E0301.toString(), "no-op");
387 }
388
389 /**
390 * Return the info of the coordinator jobs that match the filter.
391 *
392 * @param filter job filter. Refer to the {@link OozieClient} for the filter
393 * syntax.
394 * @param start jobs offset, base 1.
395 * @param len number of jobs to return.
396 * @return a list with the coordinator jobs info
397 * @throws OozieClientException thrown if the jobs info could not be
398 * retrieved.
399 */
400 @Override
401 public List<CoordinatorJob> getCoordJobsInfo(String filter, int start, int len) throws OozieClientException {
402 try {
403 CoordinatorJobInfo info = coordEngine.getCoordJobs(filter, start, len);
404 List<CoordinatorJob> jobs = new ArrayList<CoordinatorJob>();
405 List<CoordinatorJobBean> jobBeans = info.getCoordJobs();
406 for (CoordinatorJobBean jobBean : jobBeans) {
407 jobs.add(jobBean);
408 }
409 return jobs;
410
411 }
412 catch (CoordinatorEngineException ex) {
413 throw new OozieClientException(ex.getErrorCode().toString(), ex);
414 }
415 }
416
417 /**
418 * Return the info of the workflow jobs that match the filter.
419 * <p/>
420 * It returns the first 100 jobs that match the filter.
421 *
422 * @param filter job filter. Refer to the {@link LocalOozieClient} for the
423 * filter syntax.
424 * @return a list with the workflow jobs info, without node details.
425 * @throws org.apache.oozie.client.OozieClientException thrown if the jobs
426 * info could not be retrieved.
427 */
428 @Override
429 @Deprecated
430 public List<WorkflowJob> getJobsInfo(String filter) throws OozieClientException {
431 throw new OozieClientException(ErrorCode.E0301.toString(), "no-op");
432 }
433
434 }