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.servlet; 020 021import java.io.IOException; 022import java.util.ArrayList; 023import java.util.HashSet; 024import java.util.List; 025import java.util.Set; 026 027import javax.servlet.http.HttpServletRequest; 028import javax.servlet.http.HttpServletResponse; 029 030import org.apache.hadoop.conf.Configuration; 031import org.apache.oozie.BaseEngineException; 032import org.apache.oozie.BulkResponseInfo; 033import org.apache.oozie.BundleEngine; 034import org.apache.oozie.BundleEngineException; 035import org.apache.oozie.BundleJobInfo; 036import org.apache.oozie.CoordinatorEngine; 037import org.apache.oozie.CoordinatorEngineException; 038import org.apache.oozie.CoordinatorJobInfo; 039import org.apache.oozie.DagEngine; 040import org.apache.oozie.DagEngineException; 041import org.apache.oozie.ErrorCode; 042import org.apache.oozie.OozieJsonFactory; 043import org.apache.oozie.WorkflowsInfo; 044import org.apache.oozie.cli.OozieCLI; 045import org.apache.oozie.client.OozieClient; 046import org.apache.oozie.client.rest.BulkResponseImpl; 047import org.apache.oozie.client.rest.JsonTags; 048import org.apache.oozie.client.rest.RestConstants; 049import org.apache.oozie.service.BundleEngineService; 050import org.apache.oozie.service.CoordinatorEngineService; 051import org.apache.oozie.service.DagEngineService; 052import org.apache.oozie.service.Services; 053import org.apache.oozie.util.ConfigUtils; 054import org.apache.oozie.util.IOUtils; 055import org.apache.oozie.util.XLog; 056import org.apache.oozie.util.XmlUtils; 057import org.json.simple.JSONArray; 058import org.json.simple.JSONObject; 059 060public class V1JobsServlet extends BaseJobsServlet { 061 062 private static final String INSTRUMENTATION_NAME = "v1jobs"; 063 private static final Set<String> httpJobType = new HashSet<String>(){{ 064 this.add(OozieCLI.HIVE_CMD); 065 this.add(OozieCLI.SQOOP_CMD); 066 this.add(OozieCLI.PIG_CMD); 067 this.add(OozieCLI.MR_CMD); 068 }}; 069 070 public V1JobsServlet() { 071 super(INSTRUMENTATION_NAME); 072 } 073 074 /** 075 * v1 service implementation to submit a job, either workflow or coordinator 076 */ 077 @Override 078 protected JSONObject submitJob(HttpServletRequest request, Configuration conf) throws XServletException, 079 IOException { 080 JSONObject json = null; 081 082 String jobType = request.getParameter(RestConstants.JOBTYPE_PARAM); 083 084 if (!getUser(request).equals(UNDEF)) { 085 ConfigUtils.checkAndSetDisallowedProperties(conf, 086 getUser(request), 087 new XServletException(HttpServletResponse.SC_BAD_REQUEST, 088 ErrorCode.E0303, 089 "configuration", 090 OozieClient.USER_NAME), 091 false); 092 } 093 094 if (jobType == null) { 095 String wfPath = conf.get(OozieClient.APP_PATH); 096 String coordPath = conf.get(OozieClient.COORDINATOR_APP_PATH); 097 String bundlePath = conf.get(OozieClient.BUNDLE_APP_PATH); 098 099 ServletUtilities.ValidateAppPath(wfPath, coordPath, bundlePath); 100 101 if (wfPath != null) { 102 json = submitWorkflowJob(request, conf); 103 } 104 else if (coordPath != null) { 105 json = submitCoordinatorJob(request, conf); 106 } 107 else { 108 json = submitBundleJob(request, conf); 109 } 110 } 111 else { // This is a http submission job 112 if (httpJobType.contains(jobType)) { 113 json = submitHttpJob(request, conf, jobType); 114 } 115 else { 116 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0303, 117 RestConstants.JOBTYPE_PARAM, jobType); 118 } 119 } 120 return json; 121 } 122 123 /** 124 * v1 service implementation to get a JSONObject representation of a job from its external ID 125 */ 126 @Override 127 protected JSONObject getJobIdForExternalId(HttpServletRequest request, String externalId) throws XServletException, 128 IOException { 129 JSONObject json = null; 130 /* 131 * Configuration conf = new XConfiguration(); String wfPath = 132 * conf.get(OozieClient.APP_PATH); String coordPath = 133 * conf.get(OozieClient.COORDINATOR_APP_PATH); 134 * 135 * ServletUtilities.ValidateAppPath(wfPath, coordPath); 136 */ 137 String jobtype = request.getParameter(RestConstants.JOBTYPE_PARAM); 138 jobtype = (jobtype != null) ? jobtype : "wf"; 139 if (jobtype.contains("wf")) { 140 json = getWorkflowJobIdForExternalId(request, externalId); 141 } 142 else { 143 json = getCoordinatorJobIdForExternalId(request, externalId); 144 } 145 return json; 146 } 147 148 /** 149 * v1 service implementation to get a list of workflows, coordinators, or bundles, with filtering or interested 150 * windows embedded in the request object 151 */ 152 @Override 153 protected JSONObject getJobs(HttpServletRequest request) throws XServletException, IOException { 154 JSONObject json = null; 155 String isBulk = request.getParameter(RestConstants.JOBS_BULK_PARAM); 156 if(isBulk != null) { 157 json = getBulkJobs(request); 158 } else { 159 String jobtype = request.getParameter(RestConstants.JOBTYPE_PARAM); 160 jobtype = (jobtype != null) ? jobtype : "wf"; 161 162 if (jobtype.contains("wf")) { 163 json = getWorkflowJobs(request); 164 } 165 else if (jobtype.contains("coord")) { 166 json = getCoordinatorJobs(request); 167 } 168 else if (jobtype.contains("bundle")) { 169 json = getBundleJobs(request); 170 } 171 } 172 return json; 173 } 174 175 /** 176 * v1 service implementation to submit a workflow job 177 */ 178 @SuppressWarnings("unchecked") 179 private JSONObject submitWorkflowJob(HttpServletRequest request, Configuration conf) throws XServletException { 180 181 JSONObject json = new JSONObject(); 182 183 try { 184 String action = request.getParameter(RestConstants.ACTION_PARAM); 185 if (action != null && !action.equals(RestConstants.JOB_ACTION_START) 186 && !action.equals(RestConstants.JOB_ACTION_DRYRUN)) { 187 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0303, 188 RestConstants.ACTION_PARAM, action); 189 } 190 boolean startJob = (action != null); 191 String user = conf.get(OozieClient.USER_NAME); 192 DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(user); 193 String id; 194 boolean dryrun = false; 195 if (action != null) { 196 dryrun = (action.equals(RestConstants.JOB_ACTION_DRYRUN)); 197 } 198 if (dryrun) { 199 id = dagEngine.dryRunSubmit(conf); 200 } 201 else { 202 id = dagEngine.submitJob(conf, startJob); 203 } 204 json.put(JsonTags.JOB_ID, id); 205 } 206 catch (BaseEngineException ex) { 207 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 208 } 209 210 return json; 211 } 212 213 /** 214 * v1 service implementation to submit a coordinator job 215 */ 216 @SuppressWarnings("unchecked") 217 private JSONObject submitCoordinatorJob(HttpServletRequest request, Configuration conf) throws XServletException { 218 219 JSONObject json = new JSONObject(); 220 XLog.getLog(getClass()).warn("submitCoordinatorJob " + XmlUtils.prettyPrint(conf).toString()); 221 try { 222 String action = request.getParameter(RestConstants.ACTION_PARAM); 223 if (action != null && !action.equals(RestConstants.JOB_ACTION_START) 224 && !action.equals(RestConstants.JOB_ACTION_DRYRUN)) { 225 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0303, 226 RestConstants.ACTION_PARAM, action); 227 } 228 boolean startJob = (action != null); 229 String user = conf.get(OozieClient.USER_NAME); 230 CoordinatorEngine coordEngine = Services.get().get(CoordinatorEngineService.class).getCoordinatorEngine( 231 user); 232 String id = null; 233 boolean dryrun = false; 234 if (action != null) { 235 dryrun = (action.equals(RestConstants.JOB_ACTION_DRYRUN)); 236 } 237 if (dryrun) { 238 id = coordEngine.dryRunSubmit(conf); 239 } 240 else { 241 id = coordEngine.submitJob(conf, startJob); 242 } 243 json.put(JsonTags.JOB_ID, id); 244 } 245 catch (CoordinatorEngineException ex) { 246 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 247 } 248 249 return json; 250 } 251 252 /** 253 * v1 service implementation to submit a bundle job 254 */ 255 @SuppressWarnings("unchecked") 256 private JSONObject submitBundleJob(HttpServletRequest request, Configuration conf) throws XServletException { 257 JSONObject json = new JSONObject(); 258 XLog.getLog(getClass()).warn("submitBundleJob " + XmlUtils.prettyPrint(conf).toString()); 259 try { 260 String action = request.getParameter(RestConstants.ACTION_PARAM); 261 if (action != null && !action.equals(RestConstants.JOB_ACTION_START) 262 && !action.equals(RestConstants.JOB_ACTION_DRYRUN)) { 263 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0303, 264 RestConstants.ACTION_PARAM, action); 265 } 266 boolean startJob = (action != null); 267 String user = conf.get(OozieClient.USER_NAME); 268 BundleEngine bundleEngine = Services.get().get(BundleEngineService.class).getBundleEngine(user); 269 String id = null; 270 boolean dryrun = false; 271 if (action != null) { 272 dryrun = (action.equals(RestConstants.JOB_ACTION_DRYRUN)); 273 } 274 if (dryrun) { 275 id = bundleEngine.dryRunSubmit(conf); 276 } 277 else { 278 id = bundleEngine.submitJob(conf, startJob); 279 } 280 json.put(JsonTags.JOB_ID, id); 281 } 282 catch (BundleEngineException ex) { 283 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 284 } 285 286 return json; 287 } 288 289 /** 290 * v1 service implementation to get a JSONObject representation of a job from its external ID 291 */ 292 @SuppressWarnings("unchecked") 293 private JSONObject getWorkflowJobIdForExternalId(HttpServletRequest request, String externalId) 294 throws XServletException { 295 JSONObject json = new JSONObject(); 296 try { 297 DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(getUser(request)); 298 String jobId = dagEngine.getJobIdForExternalId(externalId); 299 json.put(JsonTags.JOB_ID, jobId); 300 } 301 catch (DagEngineException ex) { 302 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 303 } 304 return json; 305 } 306 307 /** 308 * v1 service implementation to get a JSONObject representation of a job from its external ID 309 */ 310 private JSONObject getCoordinatorJobIdForExternalId(HttpServletRequest request, String externalId) 311 throws XServletException { 312 JSONObject json = new JSONObject(); 313 return json; 314 } 315 316 /** 317 * v1 service implementation to get a list of workflows, with filtering or interested windows embedded in the 318 * request object 319 */ 320 private JSONObject getWorkflowJobs(HttpServletRequest request) throws XServletException { 321 JSONObject json; 322 try { 323 String filter = request.getParameter(RestConstants.JOBS_FILTER_PARAM); 324 String startStr = request.getParameter(RestConstants.OFFSET_PARAM); 325 String lenStr = request.getParameter(RestConstants.LEN_PARAM); 326 String timeZoneId = request.getParameter(RestConstants.TIME_ZONE_PARAM) == null 327 ? "GMT" : request.getParameter(RestConstants.TIME_ZONE_PARAM); 328 int start = (startStr != null) ? Integer.parseInt(startStr) : 1; 329 start = (start < 1) ? 1 : start; 330 int len = (lenStr != null) ? Integer.parseInt(lenStr) : 50; 331 len = (len < 1) ? 50 : len; 332 DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(getUser(request)); 333 WorkflowsInfo jobs = dagEngine.getJobs(filter, start, len); 334 json = OozieJsonFactory.getWFJSONObject(jobs, timeZoneId); 335 } 336 catch (DagEngineException ex) { 337 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 338 } 339 340 return json; 341 } 342 343 /** 344 * v1 service implementation to get a list of workflows, with filtering or interested windows embedded in the 345 * request object 346 */ 347 @SuppressWarnings("unchecked") 348 private JSONObject getCoordinatorJobs(HttpServletRequest request) throws XServletException { 349 JSONObject json; 350 try { 351 String filter = request.getParameter(RestConstants.JOBS_FILTER_PARAM); 352 String startStr = request.getParameter(RestConstants.OFFSET_PARAM); 353 String lenStr = request.getParameter(RestConstants.LEN_PARAM); 354 String timeZoneId = request.getParameter(RestConstants.TIME_ZONE_PARAM) == null 355 ? "GMT" : request.getParameter(RestConstants.TIME_ZONE_PARAM); 356 int start = (startStr != null) ? Integer.parseInt(startStr) : 1; 357 start = (start < 1) ? 1 : start; 358 int len = (lenStr != null) ? Integer.parseInt(lenStr) : 50; 359 len = (len < 1) ? 50 : len; 360 CoordinatorEngine coordEngine = Services.get().get(CoordinatorEngineService.class).getCoordinatorEngine( 361 getUser(request)); 362 CoordinatorJobInfo jobs = coordEngine.getCoordJobs(filter, start, len); 363 json = OozieJsonFactory.getCoordJSONObject(jobs, timeZoneId); 364 } 365 catch (CoordinatorEngineException ex) { 366 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 367 } 368 return json; 369 } 370 371 @SuppressWarnings("unchecked") 372 private JSONObject getBundleJobs(HttpServletRequest request) throws XServletException { 373 JSONObject json; 374 try { 375 String filter = request.getParameter(RestConstants.JOBS_FILTER_PARAM); 376 String startStr = request.getParameter(RestConstants.OFFSET_PARAM); 377 String lenStr = request.getParameter(RestConstants.LEN_PARAM); 378 String timeZoneId = request.getParameter(RestConstants.TIME_ZONE_PARAM) == null 379 ? "GMT" : request.getParameter(RestConstants.TIME_ZONE_PARAM); 380 int start = (startStr != null) ? Integer.parseInt(startStr) : 1; 381 start = (start < 1) ? 1 : start; 382 int len = (lenStr != null) ? Integer.parseInt(lenStr) : 50; 383 len = (len < 1) ? 50 : len; 384 385 BundleEngine bundleEngine = Services.get().get(BundleEngineService.class).getBundleEngine(getUser(request)); 386 BundleJobInfo jobs = bundleEngine.getBundleJobs(filter, start, len); 387 json = OozieJsonFactory.getBundleJSONObject(jobs, timeZoneId); 388 } 389 catch (BundleEngineException ex) { 390 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 391 } 392 return json; 393 } 394 395 @SuppressWarnings("unchecked") 396 private JSONObject getBulkJobs(HttpServletRequest request) throws XServletException, IOException { 397 JSONObject json = new JSONObject(); 398 try { 399 String bulkFilter = request.getParameter(RestConstants.JOBS_BULK_PARAM); //REST API 400 String startStr = request.getParameter(RestConstants.OFFSET_PARAM); 401 String lenStr = request.getParameter(RestConstants.LEN_PARAM); 402 String timeZoneId = request.getParameter(RestConstants.TIME_ZONE_PARAM) == null 403 ? "GMT" : request.getParameter(RestConstants.TIME_ZONE_PARAM); 404 int start = (startStr != null) ? Integer.parseInt(startStr) : 1; 405 start = (start < 1) ? 1 : start; 406 int len = (lenStr != null) ? Integer.parseInt(lenStr) : 50; 407 len = (len < 1) ? 50 : len; 408 409 BundleEngine bundleEngine = Services.get().get(BundleEngineService.class).getBundleEngine(getUser(request)); 410 BulkResponseInfo bulkResponse = bundleEngine.getBulkJobs(bulkFilter, start, len); 411 List<BulkResponseImpl> responsesToJson = bulkResponse.getResponses(); 412 413 json.put(JsonTags.BULK_RESPONSES, BulkResponseImpl.toJSONArray(responsesToJson, timeZoneId)); 414 json.put(JsonTags.BULK_RESPONSE_TOTAL, bulkResponse.getTotal()); 415 json.put(JsonTags.BULK_RESPONSE_OFFSET, bulkResponse.getStart()); 416 json.put(JsonTags.BULK_RESPONSE_LEN, bulkResponse.getLen()); 417 418 } 419 catch (BaseEngineException ex) { 420 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 421 } 422 return json; 423 } 424 425 /** 426 * service implementation to submit a http job 427 */ 428 private JSONObject submitHttpJob(HttpServletRequest request, Configuration conf, String jobType) 429 throws XServletException { 430 JSONObject json = new JSONObject(); 431 432 try { 433 String user = conf.get(OozieClient.USER_NAME); 434 DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(user); 435 String id = dagEngine.submitHttpJob(conf, jobType); 436 json.put(JsonTags.JOB_ID, id); 437 } 438 catch (DagEngineException ex) { 439 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 440 } 441 442 return json; 443 } 444 445 /** 446 * service implementation to bulk kill jobs 447 * @param request 448 * @param response 449 * @return bulkModifyJobs implementation to bulk kill jobs 450 * @throws XServletException 451 * @throws IOException 452 */ 453 @Override 454 protected JSONObject killJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException, 455 IOException { 456 return bulkModifyJobs(request, response); 457 } 458 459 /** 460 * service implementation to bulk suspend jobs 461 * @param request 462 * @param response 463 * @return bulkModifyJobs implementation to bulk suspend jobs 464 * @throws XServletException 465 * @throws IOException 466 */ 467 @Override 468 protected JSONObject suspendJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException, 469 IOException { 470 return bulkModifyJobs(request, response); 471 } 472 473 /** 474 * service implementation to bulk resume jobs 475 * @param request 476 * @param response 477 * @return bulkModifyJobs implementation to bulk resume jobs 478 * @throws XServletException 479 * @throws IOException 480 */ 481 @Override 482 protected JSONObject resumeJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException, 483 IOException { 484 return bulkModifyJobs(request, response); 485 } 486 487 private JSONObject bulkModifyJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException, 488 IOException { 489 String action = request.getParameter(RestConstants.ACTION_PARAM); 490 String jobType = request.getParameter(RestConstants.JOBTYPE_PARAM); 491 String filter = request.getParameter(RestConstants.JOBS_FILTER_PARAM); 492 String startStr = request.getParameter(RestConstants.OFFSET_PARAM); 493 String lenStr = request.getParameter(RestConstants.LEN_PARAM); 494 String timeZoneId = request.getParameter(RestConstants.TIME_ZONE_PARAM) == null 495 ? "GMT" : request.getParameter(RestConstants.TIME_ZONE_PARAM); 496 497 int start = (startStr != null) ? Integer.parseInt(startStr) : 1; 498 start = (start < 1) ? 1 : start; 499 int len = (lenStr != null) ? Integer.parseInt(lenStr) : 50; 500 len = (len < 1) ? 50 : len; 501 502 JSONObject json; 503 List<String> ids = new ArrayList<String>(); 504 505 if (jobType.equals("wf")) { 506 WorkflowsInfo jobs; 507 DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(getUser(request)); 508 try { 509 switch (action) { 510 case RestConstants.JOB_ACTION_KILL: 511 jobs = dagEngine.killJobs(filter, start, len); 512 break; 513 case RestConstants.JOB_ACTION_SUSPEND: 514 jobs = dagEngine.suspendJobs(filter, start, len); 515 break; 516 case RestConstants.JOB_ACTION_RESUME: 517 jobs = dagEngine.resumeJobs(filter, start, len); 518 break; 519 default: 520 throw new DagEngineException(ErrorCode.E0301, action); 521 } 522 } catch (DagEngineException ex) { 523 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 524 } 525 json = OozieJsonFactory.getWFJSONObject(jobs, timeZoneId); 526 } 527 else if (jobType.equals("bundle")) { 528 BundleJobInfo jobs; 529 BundleEngine bundleEngine = Services.get().get(BundleEngineService.class).getBundleEngine(getUser(request)); 530 try { 531 switch (action) { 532 case RestConstants.JOB_ACTION_KILL: 533 jobs = bundleEngine.killJobs(filter, start, len); 534 break; 535 case RestConstants.JOB_ACTION_SUSPEND: 536 jobs = bundleEngine.suspendJobs(filter, start, len); 537 break; 538 case RestConstants.JOB_ACTION_RESUME: 539 jobs = bundleEngine.resumeJobs(filter, start, len); 540 break; 541 default: 542 throw new BundleEngineException(ErrorCode.E0301, action); 543 } 544 } catch (BundleEngineException ex) { 545 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 546 } 547 json = OozieJsonFactory.getBundleJSONObject(jobs, timeZoneId); 548 } 549 else { 550 CoordinatorJobInfo jobs; 551 CoordinatorEngine coordEngine = Services.get().get(CoordinatorEngineService.class). 552 getCoordinatorEngine(getUser(request)); 553 try { 554 switch (action) { 555 case RestConstants.JOB_ACTION_KILL: 556 jobs = coordEngine.killJobs(filter, start, len); 557 break; 558 case RestConstants.JOB_ACTION_SUSPEND: 559 jobs = coordEngine.suspendJobs(filter, start, len); 560 break; 561 case RestConstants.JOB_ACTION_RESUME: 562 jobs = coordEngine.resumeJobs(filter, start, len); 563 break; 564 default: 565 throw new CoordinatorEngineException(ErrorCode.E0301, action); 566 } 567 } catch (CoordinatorEngineException ex) { 568 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 569 } 570 json = OozieJsonFactory.getCoordJSONObject(jobs, timeZoneId); 571 } 572 json.put(JsonTags.JOB_IDS, toJSONArray(ids)); 573 return json; 574 } 575 576 private static JSONArray toJSONArray(List<String> ids) { 577 JSONArray array = new JSONArray(); 578 for (String id : ids) { 579 array.add(id); 580 } 581 return array; 582 } 583}