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.DataInput; 018 import java.io.DataOutput; 019 import java.io.IOException; 020 import java.sql.Timestamp; 021 import java.util.Date; 022 023 import javax.persistence.Basic; 024 import javax.persistence.Column; 025 import javax.persistence.Entity; 026 import javax.persistence.Lob; 027 import javax.persistence.NamedQueries; 028 import javax.persistence.NamedQuery; 029 030 import org.apache.hadoop.io.Writable; 031 import org.apache.oozie.client.CoordinatorJob; 032 import org.apache.oozie.client.rest.JsonCoordinatorJob; 033 import org.apache.oozie.util.DateUtils; 034 import org.apache.oozie.util.WritableUtils; 035 import org.apache.openjpa.persistence.jdbc.Index; 036 037 @Entity 038 @NamedQueries( { 039 @NamedQuery(name = "UPDATE_COORD_JOB", query = "update CoordinatorJobBean w set w.appName = :appName, w.appPath = :appPath, w.concurrency = :concurrency, w.conf = :conf, w.externalId = :externalId, w.frequency = :frequency, w.lastActionNumber = :lastActionNumber, w.timeOut = :timeOut, w.timeZone = :timeZone, w.authToken = :authToken, w.createdTimestamp = :createdTime, w.endTimestamp = :endTime, w.execution = :execution, w.jobXml = :jobXml, w.lastActionTimestamp = :lastAction, w.lastModifiedTimestamp = :lastModifiedTime, w.nextMaterializedTimestamp = :nextMaterializedTime, w.origJobXml = :origJobXml, w.slaXml=:slaXml, w.startTimestamp = :startTime, w.status = :status, w.timeUnitStr = :timeUnit where w.id = :id"), 040 041 @NamedQuery(name = "UPDATE_COORD_JOB_STATUS", query = "update CoordinatorJobBean w set w.status = :status, w.lastModifiedTimestamp = :lastModifiedTime where w.id = :id"), 042 043 @NamedQuery(name = "DELETE_COORD_JOB", query = "delete from CoordinatorJobBean w where w.id = :id"), 044 045 @NamedQuery(name = "GET_COORD_JOBS", query = "select OBJECT(w) from CoordinatorJobBean w"), 046 047 @NamedQuery(name = "GET_COORD_JOB", query = "select OBJECT(w) from CoordinatorJobBean w where w.id = :id"), 048 049 @NamedQuery(name = "GET_COORD_JOBS_PENDING", query = "select OBJECT(w) from CoordinatorJobBean w where w.pending = 1 order by w.lastModifiedTimestamp"), 050 051 @NamedQuery(name = "GET_COORD_JOBS_COUNT", query = "select count(w) from CoordinatorJobBean w"), 052 053 @NamedQuery(name = "GET_COORD_JOBS_COLUMNS", query = "select w.id, w.appName, w.status, w.user, w.group, w.startTimestamp, w.endTimestamp, w.appPath, w.concurrency, w.frequency, w.lastActionTimestamp, w.nextMaterializedTimestamp, w.createdTimestamp, w.timeUnitStr, w.timeZone, w.timeOut from CoordinatorJobBean w order by w.createdTimestamp desc"), 054 055 @NamedQuery(name = "GET_COORD_JOBS_OLDER_THAN", query = "select OBJECT(w) from CoordinatorJobBean w where w.startTimestamp <= :matTime AND (w.status = 'PREP' OR w.status = 'RUNNING') AND (w.nextMaterializedTimestamp < :matTime OR w.nextMaterializedTimestamp IS NULL) AND (w.nextMaterializedTimestamp IS NULL OR (w.endTimestamp > w.nextMaterializedTimestamp AND (w.pauseTimestamp IS NULL OR w.pauseTimestamp > w.nextMaterializedTimestamp))) order by w.lastModifiedTimestamp"), 056 057 @NamedQuery(name = "GET_COORD_JOBS_OLDER_THAN_STATUS", query = "select OBJECT(w) from CoordinatorJobBean w where w.status = :status AND w.lastModifiedTimestamp <= :lastModTime order by w.lastModifiedTimestamp"), 058 059 @NamedQuery(name = "GET_COMPLETED_COORD_JOBS_OLDER_THAN_STATUS", query = "select OBJECT(w) from CoordinatorJobBean w where ( w.status = 'SUCCEEDED' OR w.status = 'FAILED' or w.status = 'KILLED') AND w.lastModifiedTimestamp <= :lastModTime order by w.lastModifiedTimestamp"), 060 061 @NamedQuery(name = "GET_COORD_JOBS_UNPAUSED", query = "select OBJECT(w) from CoordinatorJobBean w where w.status = 'RUNNING' OR w.status = 'RUNNINGWITHERROR' OR w.status = 'PREP' order by w.lastModifiedTimestamp"), 062 063 @NamedQuery(name = "GET_COORD_JOBS_PAUSED", query = "select OBJECT(w) from CoordinatorJobBean w where w.status = 'PAUSED' OR w.status = 'PAUSEDWITHERROR' OR w.status = 'PREPPAUSED' order by w.lastModifiedTimestamp"), 064 065 @NamedQuery(name = "GET_COORD_JOBS_FOR_BUNDLE", query = "select OBJECT(w) from CoordinatorJobBean w where w.bundleId = :bundleId order by w.lastModifiedTimestamp") }) 066 public class CoordinatorJobBean extends JsonCoordinatorJob implements Writable { 067 068 @Basic 069 @Index 070 @Column(name = "status") 071 private String status = CoordinatorJob.Status.PREP.toString(); 072 073 @Basic 074 @Column(name = "auth_token") 075 @Lob 076 private String authToken = null; 077 078 @Basic 079 @Column(name = "start_time") 080 private java.sql.Timestamp startTimestamp = null; 081 082 @Basic 083 @Column(name = "end_time") 084 private java.sql.Timestamp endTimestamp = null; 085 086 @Basic 087 @Column(name = "pause_time") 088 private java.sql.Timestamp pauseTimestamp = null; 089 090 @Basic 091 @Index 092 @Column(name = "created_time") 093 private java.sql.Timestamp createdTimestamp = null; 094 095 @Basic 096 @Column(name = "time_unit") 097 private String timeUnitStr = CoordinatorJob.Timeunit.NONE.toString(); 098 099 @Basic 100 @Column(name = "execution") 101 private String execution = CoordinatorJob.Execution.FIFO.toString(); 102 103 @Basic 104 @Column(name = "last_action") 105 private java.sql.Timestamp lastActionTimestamp = null; 106 107 @Basic 108 @Index 109 @Column(name = "next_matd_time") 110 private java.sql.Timestamp nextMaterializedTimestamp = null; 111 112 @Basic 113 @Index 114 @Column(name = "last_modified_time") 115 private java.sql.Timestamp lastModifiedTimestamp = null; 116 117 @Basic 118 @Index 119 @Column(name = "suspended_time") 120 private java.sql.Timestamp suspendedTimestamp = null; 121 122 @Column(name = "job_xml") 123 @Lob 124 private String jobXml = null; 125 126 @Column(name = "orig_job_xml") 127 @Lob 128 private String origJobXml = null; 129 130 @Column(name = "sla_xml") 131 @Lob 132 private String slaXml = null; 133 134 @Basic 135 @Column(name = "pending") 136 private int pending = 0; 137 138 @Basic 139 @Column(name = "done_materialization") 140 private int doneMaterialization = 0; 141 142 @Basic 143 @Column(name = "app_namespace") 144 private String appNamespace = null; 145 146 /** 147 * Get start timestamp 148 * 149 * @return start timestamp 150 */ 151 public java.sql.Timestamp getStartTimestamp() { 152 return startTimestamp; 153 } 154 155 /** 156 * Set start timestamp 157 * 158 * @param startTimestamp start timestamp 159 */ 160 public void setStartTimestamp(java.sql.Timestamp startTimestamp) { 161 super.setStartTime(DateUtils.toDate(startTimestamp)); 162 this.startTimestamp = startTimestamp; 163 } 164 165 /** 166 * Get end timestamp 167 * 168 * @return end timestamp 169 */ 170 public java.sql.Timestamp getEndTimestamp() { 171 return endTimestamp; 172 } 173 174 /** 175 * Set end timestamp 176 * 177 * @param endTimestamp end timestamp 178 */ 179 public void setEndTimestamp(java.sql.Timestamp endTimestamp) { 180 super.setEndTime(DateUtils.toDate(endTimestamp)); 181 this.endTimestamp = endTimestamp; 182 } 183 184 /** 185 * Get next materialized timestamp 186 * 187 * @return next materialized timestamp 188 */ 189 public Timestamp getNextMaterializedTimestamp() { 190 return nextMaterializedTimestamp; 191 } 192 193 /** 194 * Set next materialized timestamp 195 * 196 * @param nextMaterializedTimestamp next materialized timestamp 197 */ 198 public void setNextMaterializedTimestamp(java.sql.Timestamp nextMaterializedTimestamp) { 199 super.setNextMaterializedTime(DateUtils.toDate(nextMaterializedTimestamp)); 200 this.nextMaterializedTimestamp = nextMaterializedTimestamp; 201 } 202 203 /** 204 * Get last modified timestamp 205 * 206 * @return last modified timestamp 207 */ 208 public Timestamp getLastModifiedTimestamp() { 209 return lastModifiedTimestamp; 210 } 211 212 /** 213 * Set last modified timestamp 214 * 215 * @param lastModifiedTimestamp last modified timestamp 216 */ 217 public void setLastModifiedTimestamp(java.sql.Timestamp lastModifiedTimestamp) { 218 this.lastModifiedTimestamp = lastModifiedTimestamp; 219 } 220 221 /** 222 * Get suspended timestamp 223 * 224 * @return suspended timestamp 225 */ 226 public Timestamp getSuspendedTimestamp() { 227 return suspendedTimestamp; 228 } 229 230 /** 231 * Set suspended timestamp 232 * 233 * @param suspendedTimestamp suspended timestamp 234 */ 235 public void setSuspendedTimestamp(java.sql.Timestamp suspendedTimestamp) { 236 this.suspendedTimestamp = suspendedTimestamp; 237 } 238 239 /** 240 * Get job xml 241 * 242 * @return job xml 243 */ 244 public String getJobXml() { 245 return jobXml; 246 } 247 248 /** 249 * Set job xml 250 * 251 * @param jobXml job xml 252 */ 253 public void setJobXml(String jobXml) { 254 this.jobXml = jobXml; 255 } 256 257 /** 258 * Get original job xml 259 * 260 * @return original job xml 261 */ 262 public String getOrigJobXml() { 263 return origJobXml; 264 } 265 266 /** 267 * Set original job xml 268 * 269 * @param origJobXml 270 */ 271 public void setOrigJobXml(String origJobXml) { 272 this.origJobXml = origJobXml; 273 } 274 275 /** 276 * Get sla xml 277 * 278 * @return sla xml 279 */ 280 public String getSlaXml() { 281 return slaXml; 282 } 283 284 /** 285 * Set sla xml 286 * 287 * @param slaXml sla xml 288 */ 289 public void setSlaXml(String slaXml) { 290 this.slaXml = slaXml; 291 } 292 293 /* (non-Javadoc) 294 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#setTimeUnit(org.apache.oozie.client.CoordinatorJob.Timeunit) 295 */ 296 @Override 297 public void setTimeUnit(Timeunit timeUnit) { 298 super.setTimeUnit(timeUnit); 299 this.timeUnitStr = timeUnit.toString(); 300 } 301 302 /** 303 * Set last action timestamp 304 * 305 * @param lastActionTimestamp last action timestamp 306 */ 307 public void setLastActionTimestamp(java.sql.Timestamp lastActionTimestamp) { 308 super.setLastActionTime(DateUtils.toDate(lastActionTimestamp)); 309 this.lastActionTimestamp = lastActionTimestamp; 310 } 311 312 /** 313 * Set auth token 314 * 315 * @param authToken auth token 316 */ 317 public void setAuthToken(String authToken) { 318 this.authToken = authToken; 319 } 320 321 /** 322 * Set pending to true 323 */ 324 @Override 325 public void setPending() { 326 super.setPending(); 327 this.pending = 1; 328 } 329 330 /** 331 * Set pending to false 332 */ 333 @Override 334 public void resetPending() { 335 super.resetPending(); 336 this.pending = 0; 337 } 338 339 /** 340 * Return if the action is pending. 341 * 342 * @return if the action is pending. 343 */ 344 public boolean isPending() { 345 return pending == 1 ? true : false; 346 } 347 348 /** 349 * Set doneMaterialization to true 350 */ 351 public void setDoneMaterialization() { 352 this.doneMaterialization = 1; 353 } 354 355 /** 356 * Set doneMaterialization to false 357 */ 358 public void resetDoneMaterialization() { 359 this.doneMaterialization = 0; 360 } 361 362 /** 363 * Return if the action is done with materialization 364 * 365 * @return if the action is done with materialization 366 */ 367 public boolean isDoneMaterialization() { 368 return doneMaterialization == 1 ? true : false; 369 } 370 371 372 /** 373 * Get app namespce 374 * 375 * @return app namespce 376 */ 377 public String getAppNamespace() { 378 return appNamespace; 379 } 380 381 /** 382 * Set app namespce 383 * 384 * @param appNamespace the app namespce to set 385 */ 386 public void setAppNamespace(String appNamespace) { 387 this.appNamespace = appNamespace; 388 } 389 390 public CoordinatorJobBean() { 391 } 392 393 /* 394 * Serialize the coordinator bean to a data output. @param dataOutput data 395 * output. @throws IOException thrown if the coordinator bean could not be 396 * serialized. 397 */ 398 public void write(DataOutput dataOutput) throws IOException { 399 WritableUtils.writeStr(dataOutput, getAppPath()); 400 WritableUtils.writeStr(dataOutput, getAppName()); 401 WritableUtils.writeStr(dataOutput, getId()); 402 WritableUtils.writeStr(dataOutput, getConf()); 403 WritableUtils.writeStr(dataOutput, getStatusStr()); 404 dataOutput.writeInt(getFrequency()); 405 WritableUtils.writeStr(dataOutput, getTimeUnit().toString()); 406 WritableUtils.writeStr(dataOutput, getTimeZone()); 407 dataOutput.writeInt(getConcurrency()); 408 WritableUtils.writeStr(dataOutput, getExecutionOrder().toString()); 409 dataOutput.writeLong((getLastActionTime() != null) ? getLastActionTime().getTime() : -1); 410 dataOutput.writeLong((getNextMaterializedTime() != null) ? getNextMaterializedTime().getTime() : -1); 411 dataOutput.writeLong((getStartTime() != null) ? getStartTime().getTime() : -1); 412 dataOutput.writeLong((getEndTime() != null) ? getEndTime().getTime() : -1); 413 WritableUtils.writeStr(dataOutput, getUser()); 414 WritableUtils.writeStr(dataOutput, getGroup()); 415 WritableUtils.writeStr(dataOutput, getExternalId()); 416 dataOutput.writeInt(getTimeout()); 417 dataOutput.writeInt(getMatThrottling()); 418 if (isPending()) { 419 dataOutput.writeInt(1); 420 } else { 421 dataOutput.writeInt(0); 422 } 423 if (isDoneMaterialization()) { 424 dataOutput.writeInt(1); 425 } else { 426 dataOutput.writeInt(0); 427 } 428 WritableUtils.writeStr(dataOutput, getAppNamespace()); 429 } 430 431 /** 432 * Deserialize a coordinator bean from a data input. 433 * 434 * @param dataInput data input. 435 * @throws IOException thrown if the workflow bean could not be deserialized. 436 */ 437 public void readFields(DataInput dataInput) throws IOException { 438 setAppPath(WritableUtils.readStr(dataInput)); 439 setAppName(WritableUtils.readStr(dataInput)); 440 setId(WritableUtils.readStr(dataInput)); 441 setConf(WritableUtils.readStr(dataInput)); 442 setStatus(CoordinatorJob.Status.valueOf(WritableUtils.readStr(dataInput))); 443 setFrequency(dataInput.readInt()); 444 setTimeUnit(CoordinatorJob.Timeunit.valueOf(WritableUtils.readStr(dataInput))); 445 setTimeZone(WritableUtils.readStr(dataInput)); 446 setConcurrency(dataInput.readInt()); 447 setExecutionOrder(Execution.valueOf(WritableUtils.readStr(dataInput))); 448 449 long d = dataInput.readLong(); 450 if (d != -1) { 451 setLastActionTime(new Date(d)); 452 } 453 d = dataInput.readLong(); 454 if (d != -1) { 455 setNextMaterializedTime(new Date(d)); 456 } 457 d = dataInput.readLong(); 458 if (d != -1) { 459 setStartTime(new Date(d)); 460 } 461 462 d = dataInput.readLong(); 463 if (d != -1) { 464 setEndTime(new Date(d)); 465 } 466 setUser(WritableUtils.readStr(dataInput)); 467 setGroup(WritableUtils.readStr(dataInput)); 468 setExternalId(WritableUtils.readStr(dataInput)); 469 setTimeout(dataInput.readInt()); 470 setMatThrottling(dataInput.readInt()); 471 472 d = dataInput.readInt(); 473 if (d == 1) { 474 setPending(); 475 } 476 477 d = dataInput.readInt(); 478 if (d == 1) { 479 setDoneMaterialization(); 480 } 481 482 setAppNamespace(WritableUtils.readStr(dataInput)); 483 } 484 485 /* (non-Javadoc) 486 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#getStatus() 487 */ 488 @Override 489 public Status getStatus() { 490 return Status.valueOf(this.status); 491 } 492 493 /** 494 * Get status 495 * 496 * @return status 497 */ 498 public String getStatusStr() { 499 return status; 500 } 501 502 /* (non-Javadoc) 503 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#setStatus(org.apache.oozie.client.Job.Status) 504 */ 505 @Override 506 public void setStatus(Status val) { 507 super.setStatus(val); 508 this.status = val.toString(); 509 } 510 511 /** 512 * Get time unit 513 * 514 * @return time unit 515 */ 516 public String getTimeUnitStr() { 517 return timeUnitStr; 518 } 519 520 /* (non-Javadoc) 521 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#getTimeUnit() 522 */ 523 @Override 524 public Timeunit getTimeUnit() { 525 return Timeunit.valueOf(this.timeUnitStr); 526 } 527 528 /** 529 * Set order 530 * 531 * @param order 532 */ 533 public void setExecution(Execution order) { 534 this.execution = order.toString(); 535 super.setExecutionOrder(order); 536 } 537 538 /* (non-Javadoc) 539 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#getExecutionOrder() 540 */ 541 @Override 542 public Execution getExecutionOrder() { 543 return Execution.valueOf(this.execution); 544 } 545 546 /** 547 * Get execution 548 * 549 * @return execution 550 */ 551 public String getExecution() { 552 return execution; 553 } 554 555 /* (non-Javadoc) 556 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#setLastActionTime(java.util.Date) 557 */ 558 @Override 559 public void setLastActionTime(Date lastAction) { 560 this.lastActionTimestamp = DateUtils.convertDateToTimestamp(lastAction); 561 super.setLastActionTime(lastAction); 562 } 563 564 /* (non-Javadoc) 565 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#getLastActionTime() 566 */ 567 @Override 568 public Date getLastActionTime() { 569 return DateUtils.toDate(lastActionTimestamp); 570 } 571 572 /** 573 * Get last action timestamp 574 * 575 * @return last action timestamp 576 */ 577 public Timestamp getLastActionTimestamp() { 578 return lastActionTimestamp; 579 } 580 581 /* (non-Javadoc) 582 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#setNextMaterializedTime(java.util.Date) 583 */ 584 @Override 585 public void setNextMaterializedTime(Date nextMaterializedTime) { 586 super.setNextMaterializedTime(nextMaterializedTime); 587 this.nextMaterializedTimestamp = DateUtils.convertDateToTimestamp(nextMaterializedTime); 588 } 589 590 /* (non-Javadoc) 591 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#getNextMaterializedTime() 592 */ 593 @Override 594 public Date getNextMaterializedTime() { 595 return DateUtils.toDate(nextMaterializedTimestamp); 596 } 597 598 /** 599 * Set last modified time 600 * 601 * @param lastModifiedTime last modified time 602 */ 603 public void setLastModifiedTime(Date lastModifiedTime) { 604 this.lastModifiedTimestamp = DateUtils.convertDateToTimestamp(lastModifiedTime); 605 } 606 607 /** 608 * Get last modified time 609 * 610 * @return last modified time 611 */ 612 public Date getLastModifiedTime() { 613 return DateUtils.toDate(lastModifiedTimestamp); 614 } 615 616 /** 617 * Set suspended time 618 * 619 * @param suspendedTime suspended time 620 */ 621 public void setSuspendedTime(Date suspendedTime) { 622 this.suspendedTimestamp = DateUtils.convertDateToTimestamp(suspendedTime); 623 } 624 625 /** 626 * Get suspended time 627 * 628 * @return suspended time 629 */ 630 public Date getSuspendedTime() { 631 return DateUtils.toDate(suspendedTimestamp); 632 } 633 634 /* (non-Javadoc) 635 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#setStartTime(java.util.Date) 636 */ 637 @Override 638 public void setStartTime(Date startTime) { 639 super.setStartTime(startTime); 640 this.startTimestamp = DateUtils.convertDateToTimestamp(startTime); 641 } 642 643 /* (non-Javadoc) 644 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#getStartTime() 645 */ 646 @Override 647 public Date getStartTime() { 648 return DateUtils.toDate(startTimestamp); 649 } 650 651 /* (non-Javadoc) 652 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#setEndTime(java.util.Date) 653 */ 654 @Override 655 public void setEndTime(Date endTime) { 656 super.setEndTime(endTime); 657 this.endTimestamp = DateUtils.convertDateToTimestamp(endTime); 658 } 659 660 /* (non-Javadoc) 661 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#setPauseTime(java.util.Date) 662 */ 663 @Override 664 public void setPauseTime(Date pauseTime) { 665 super.setPauseTime(pauseTime); 666 this.pauseTimestamp = DateUtils.convertDateToTimestamp(pauseTime); 667 } 668 669 /* (non-Javadoc) 670 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#getEndTime() 671 */ 672 @Override 673 public Date getEndTime() { 674 return DateUtils.toDate(endTimestamp); 675 } 676 677 /* (non-Javadoc) 678 * @see org.apache.oozie.client.rest.JsonCoordinatorJob#getPauseTime() 679 */ 680 @Override 681 public Date getPauseTime() { 682 return DateUtils.toDate(pauseTimestamp); 683 } 684 685 /** 686 * Set created time 687 * 688 * @param createTime created time 689 */ 690 public void setCreatedTime(Date createTime) { 691 this.createdTimestamp = DateUtils.convertDateToTimestamp(createTime); 692 } 693 694 /** 695 * Get created time 696 * 697 * @return created time 698 */ 699 public Date getCreatedTime() { 700 return DateUtils.toDate(createdTimestamp); 701 } 702 703 /** 704 * Get created timestamp 705 * 706 * @return created timestamp 707 */ 708 public Timestamp getCreatedTimestamp() { 709 return createdTimestamp; 710 } 711 712 /** 713 * Get auth token 714 * 715 * @return auth token 716 */ 717 public String getAuthToken() { 718 return this.authToken; 719 } 720 721 }