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 package org.apache.oozie; 019 020 import java.io.DataInput; 021 import java.io.DataOutput; 022 import java.io.IOException; 023 import java.sql.Timestamp; 024 import java.util.Date; 025 026 import javax.persistence.Basic; 027 import javax.persistence.Column; 028 import javax.persistence.ColumnResult; 029 import javax.persistence.Entity; 030 import javax.persistence.Lob; 031 import javax.persistence.NamedNativeQueries; 032 import javax.persistence.NamedNativeQuery; 033 import javax.persistence.NamedQueries; 034 import javax.persistence.NamedQuery; 035 import javax.persistence.SqlResultSetMapping; 036 037 import org.apache.hadoop.io.Writable; 038 import org.apache.oozie.client.CoordinatorAction; 039 import org.apache.oozie.client.rest.JsonCoordinatorAction; 040 import org.apache.oozie.util.DateUtils; 041 import org.apache.oozie.util.WritableUtils; 042 import org.apache.openjpa.persistence.jdbc.Index; 043 044 @SqlResultSetMapping( 045 name = "CoordActionJobIdLmt", 046 columns = {@ColumnResult(name = "job_id"), 047 @ColumnResult(name = "min_lmt")}) 048 049 @Entity 050 @NamedQueries({ 051 052 @NamedQuery(name = "UPDATE_COORD_ACTION", query = "update CoordinatorActionBean w set w.actionNumber = :actionNumber, w.actionXml = :actionXml, w.consoleUrl = :consoleUrl, w.createdConf = :createdConf, w.errorCode = :errorCode, w.errorMessage = :errorMessage, w.externalStatus = :externalStatus, w.missingDependencies = :missingDependencies, w.runConf = :runConf, w.timeOut = :timeOut, w.trackerUri = :trackerUri, w.type = :type, w.createdTimestamp = :createdTime, w.externalId = :externalId, w.jobId = :jobId, w.lastModifiedTimestamp = :lastModifiedTime, w.nominalTimestamp = :nominalTime, w.slaXml = :slaXml, w.status = :status where w.id = :id"), 053 054 @NamedQuery(name = "UPDATE_COORD_ACTION_MIN", query = "update CoordinatorActionBean w set w.actionXml = :actionXml, w.missingDependencies = :missingDependencies, w.lastModifiedTimestamp = :lastModifiedTime, w.status = :status where w.id = :id"), 055 // Query to update the action status, pending status and last modified time stamp of a Coordinator action 056 @NamedQuery(name = "UPDATE_COORD_ACTION_STATUS_PENDING_TIME", query = "update CoordinatorActionBean w set w.status =:status, w.pending =:pending, w.lastModifiedTimestamp = :lastModifiedTime where w.id = :id"), 057 // Update query for InputCheck 058 @NamedQuery(name = "UPDATE_COORD_ACTION_FOR_INPUTCHECK", query = "update CoordinatorActionBean w set w.status = :status, w.lastModifiedTimestamp = :lastModifiedTime, w.actionXml = :actionXml, w.missingDependencies = :missingDependencies where w.id = :id"), 059 // Update query for Start 060 @NamedQuery(name = "UPDATE_COORD_ACTION_FOR_START", query = "update CoordinatorActionBean w set w.status =:status, w.lastModifiedTimestamp = :lastModifiedTime, w.runConf = :runConf, w.externalId = :externalId, w.pending = :pending, w.errorCode = :errorCode, w.errorMessage = :errorMessage where w.id = :id"), 061 062 @NamedQuery(name = "UPDATE_COORD_ACTION_FOR_MODIFIED_DATE", query = "update CoordinatorActionBean w set w.lastModifiedTimestamp = :lastModifiedTime where w.id = :id"), 063 064 @NamedQuery(name = "DELETE_COMPLETED_ACTIONS_FOR_COORDINATOR", query = "delete from CoordinatorActionBean a where a.jobId = :jobId and (a.status = 'SUCCEEDED' OR a.status = 'FAILED' OR a.status= 'KILLED')"), 065 066 @NamedQuery(name = "DELETE_UNSCHEDULED_ACTION", query = "delete from CoordinatorActionBean a where a.id = :id and (a.status = 'WAITING' OR a.status = 'READY')"), 067 068 // Query used by XTestcase to setup tables 069 @NamedQuery(name = "GET_COORD_ACTIONS", query = "select OBJECT(w) from CoordinatorActionBean w"), 070 // Select query used only by test cases 071 @NamedQuery(name = "GET_COORD_ACTION", query = "select OBJECT(a) from CoordinatorActionBean a where a.id = :id"), 072 073 // Select query used by ActionInfo command 074 @NamedQuery(name = "GET_COORD_ACTION_FOR_INFO", query = "select a.id, a.jobId, a.actionNumber, a.consoleUrl, a.errorCode, a.errorMessage, a.externalId, a.externalStatus, a.trackerUri, a.createdTimestamp, a.nominalTimestamp, a.status, a.lastModifiedTimestamp, a.missingDependencies from CoordinatorActionBean a where a.id = :id"), 075 // Select Query used by Timeout command 076 @NamedQuery(name = "GET_COORD_ACTION_FOR_TIMEOUT", query = "select a.id, a.jobId, a.status, a.runConf, a.pending from CoordinatorActionBean a where a.id = :id"), 077 // Select query used by InputCheck command 078 @NamedQuery(name = "GET_COORD_ACTION_FOR_INPUTCHECK", query = "select a.id, a.jobId, a.status, a.runConf, a.nominalTimestamp, a.createdTimestamp, a.actionXml, a.missingDependencies, a.timeOut from CoordinatorActionBean a where a.id = :id"), 079 // Select query used by CoordActionUpdate command 080 @NamedQuery(name = "GET_COORD_ACTION_FOR_EXTERNALID", query = "select a.id, a.jobId, a.status, a.pending, a.externalId, a.lastModifiedTimestamp, a.slaXml from CoordinatorActionBean a where a.externalId = :externalId"), 081 // Select query used by Check command 082 @NamedQuery(name = "GET_COORD_ACTION_FOR_CHECK", query = "select a.id, a.jobId, a.status, a.pending, a.externalId, a.lastModifiedTimestamp, a.slaXml from CoordinatorActionBean a where a.id = :id"), 083 // Select query used by Start command 084 @NamedQuery(name = "GET_COORD_ACTION_FOR_START", query = "select a.id, a.jobId, a.status, a.pending, a.createdConf, a.slaXml, a.actionXml, a.externalId, a.errorMessage, a.errorCode from CoordinatorActionBean a where a.id = :id"), 085 086 @NamedQuery(name = "GET_COORD_ACTIONS_FOR_JOB_FIFO", query = "select a.id, a.jobId, a.status, a.pending from CoordinatorActionBean a where a.jobId = :jobId AND a.status = 'READY' order by a.nominalTimestamp"), 087 088 @NamedQuery(name = "GET_COORD_ACTIONS_FOR_JOB_LIFO", query = "select a.id, a.jobId, a.status, a.pending from CoordinatorActionBean a where a.jobId = :jobId AND a.status = 'READY' order by a.nominalTimestamp desc"), 089 090 @NamedQuery(name = "GET_COORD_RUNNING_ACTIONS_COUNT", query = "select count(a) from CoordinatorActionBean a where a.jobId = :jobId AND (a.status = 'RUNNING' OR a.status='SUBMITTED')"), 091 092 @NamedQuery(name = "GET_COORD_ACTIONS_COUNT_BY_JOBID", query = "select count(a) from CoordinatorActionBean a where a.jobId = :jobId"), 093 094 @NamedQuery(name = "GET_COORD_ACTIVE_ACTIONS_COUNT_BY_JOBID", query = "select count(a) from CoordinatorActionBean a where a.jobId = :jobId AND a.status = 'WAITING'"), 095 096 @NamedQuery(name = "GET_COORD_ACTIONS_PENDING_FALSE_COUNT", query = "select count(a) from CoordinatorActionBean a where a.jobId = :jobId AND a.pending = 0 AND (a.status = 'SUSPENDED' OR a.status = 'TIMEDOUT' OR a.status = 'SUCCEEDED' OR a.status = 'KILLED' OR a.status = 'FAILED')"), 097 098 @NamedQuery(name = "GET_COORD_ACTIONS_PENDING_FALSE_STATUS_COUNT", query = "select count(a) from CoordinatorActionBean a where a.jobId = :jobId AND a.pending = 0 AND a.status = :status"), 099 100 @NamedQuery(name = "GET_ACTIONS_FOR_COORD_JOB", query = "select count(a) from CoordinatorActionBean a where a.jobId = :jobId"), 101 // Query to retrieve Coordinator actions sorted by nominal time 102 @NamedQuery(name = "GET_ACTIONS_FOR_COORD_JOB_ORDER_BY_NOMINAL_TIME", query = "select a.id, a.actionNumber, a.consoleUrl, a.errorCode, a.errorMessage, a.externalId, a.externalStatus, a.jobId, a.trackerUri, a.createdTimestamp, a.nominalTimestamp, a.status, a.lastModifiedTimestamp, a.missingDependencies, a.timeOut from CoordinatorActionBean a where a.jobId = :jobId order by a.nominalTimestamp"), 103 // Query to maintain backward compatibility for coord job info command 104 @NamedQuery(name = "GET_ALL_COLS_FOR_ACTIONS_FOR_COORD_JOB_ORDER_BY_NOMINAL_TIME", query = "select OBJECT(a) from CoordinatorActionBean a where a.jobId = :jobId order by a.nominalTimestamp"), 105 // Query to retrieve action id, action status, pending status and external Id of not completed Coordinator actions 106 @NamedQuery(name = "GET_COORD_ACTIONS_NOT_COMPLETED", query = "select a.id, a.status, a.pending, a.externalId from CoordinatorActionBean a where a.jobId = :jobId AND a.status <> 'FAILED' AND a.status <> 'TIMEDOUT' AND a.status <> 'SUCCEEDED' AND a.status <> 'KILLED'"), 107 108 // Query to retrieve action id, action status, pending status and external Id of running Coordinator actions 109 @NamedQuery(name = "GET_COORD_ACTIONS_RUNNING", query = "select a.id, a.status, a.pending, a.externalId from CoordinatorActionBean a where a.jobId = :jobId and a.status = 'RUNNING'"), 110 111 // Query to retrieve action id, action status, pending status and external Id of suspended Coordinator actions 112 @NamedQuery(name = "GET_COORD_ACTIONS_SUSPENDED", query = "select a.id, a.status, a.pending, a.externalId from CoordinatorActionBean a where a.jobId = :jobId and a.status = 'SUSPENDED'"), 113 114 // Query to retrieve count of Coordinator actions which are pending 115 @NamedQuery(name = "GET_COORD_ACTIONS_PENDING_COUNT", query = "select count(a) from CoordinatorActionBean a where a.jobId = :jobId AND a.pending > 0"), 116 117 // Query to retrieve status of Coordinator actions 118 @NamedQuery(name = "GET_COORD_ACTIONS_STATUS", query = "select a.status from CoordinatorActionBean a where a.jobId = :jobId"), 119 120 @NamedQuery(name = "GET_COORD_ACTION_FOR_COORD_JOB_BY_ACTION_NUMBER", query = "select a.id from CoordinatorActionBean a where a.jobId = :jobId AND a.actionNumber = :actionNumber"), 121 122 @NamedQuery(name = "GET_COORD_ACTIONS_BY_LAST_MODIFIED_TIME", query = "select a.jobId from CoordinatorActionBean a where a.lastModifiedTimestamp >= :lastModifiedTime"), 123 124 //Used by coordinator store only 125 @NamedQuery(name = "GET_RUNNING_ACTIONS_FOR_COORD_JOB", query = "select OBJECT(a) from CoordinatorActionBean a where a.jobId = :jobId AND a.status = 'RUNNING'"), 126 127 @NamedQuery(name = "GET_RUNNING_ACTIONS_OLDER_THAN", query = "select a.id from CoordinatorActionBean a where a.status = 'RUNNING' AND a.lastModifiedTimestamp <= :lastModifiedTime"), 128 129 @NamedQuery(name = "GET_COORD_ACTIONS_WAITING_SUBMITTED_OLDER_THAN", query = "select a.id, a.jobId, a.status, a.externalId from CoordinatorActionBean a where (a.status = 'WAITING' OR a.status = 'SUBMITTED') AND a.lastModifiedTimestamp <= :lastModifiedTime"), 130 131 @NamedQuery(name = "GET_COORD_ACTIONS_FOR_RECOVERY_OLDER_THAN", query = "select a.id, a.jobId, a.status, a.externalId from CoordinatorActionBean a where a.pending > 0 AND (a.status = 'SUSPENDED' OR a.status = 'KILLED' OR a.status = 'RUNNING') AND a.lastModifiedTimestamp <= :lastModifiedTime"), 132 // Select query used by rerun, requires almost all columns so select * is used 133 @NamedQuery(name = "GET_ACTIONS_FOR_DATES", query = "select OBJECT(a) from CoordinatorActionBean a where a.jobId = :jobId AND (a.status = 'TIMEDOUT' OR a.status = 'SUCCEEDED' OR a.status = 'KILLED' OR a.status = 'FAILED') AND a.nominalTimestamp >= :startTime AND a.nominalTimestamp <= :endTime"), 134 // Select query used by log 135 @NamedQuery(name = "GET_ACTION_IDS_FOR_DATES", query = "select a.id from CoordinatorActionBean a where a.jobId = :jobId AND (a.status = 'TIMEDOUT' OR a.status = 'SUCCEEDED' OR a.status = 'KILLED' OR a.status = 'FAILED') AND a.nominalTimestamp >= :startTime AND a.nominalTimestamp <= :endTime"), 136 // Select query used by rerun, requires almost all columns so select * is used 137 @NamedQuery(name = "GET_ACTION_FOR_NOMINALTIME", query = "select OBJECT(a) from CoordinatorActionBean a where a.jobId = :jobId AND a.nominalTimestamp = :nominalTime"), 138 139 @NamedQuery(name = "GET_COORD_ACTIONS_COUNT", query = "select count(w) from CoordinatorActionBean w")}) 140 141 @NamedNativeQueries({ 142 143 @NamedNativeQuery(name = "GET_READY_ACTIONS_GROUP_BY_JOBID", query = "select a.job_id as job_id, MIN(a.last_modified_time) as min_lmt from COORD_ACTIONS a where a.status = 'READY' GROUP BY a.job_id HAVING MIN(a.last_modified_time) < ?", resultSetMapping = "CoordActionJobIdLmt") 144 }) 145 public class CoordinatorActionBean extends JsonCoordinatorAction implements 146 Writable { 147 @Basic 148 @Index 149 @Column(name = "job_id") 150 private String jobId; 151 152 @Basic 153 @Index 154 @Column(name = "status") 155 private String status = null; 156 157 @Basic 158 @Index 159 @Column(name = "nominal_time") 160 private java.sql.Timestamp nominalTimestamp = null; 161 162 @Basic 163 @Index 164 @Column(name = "last_modified_time") 165 private java.sql.Timestamp lastModifiedTimestamp = null; 166 167 @Basic 168 @Index 169 @Column(name = "created_time") 170 private java.sql.Timestamp createdTimestamp = null; 171 172 @Basic 173 @Index 174 @Column(name = "rerun_time") 175 private java.sql.Timestamp rerunTimestamp = null; 176 177 @Basic 178 @Index 179 @Column(name = "external_id") 180 private String externalId; 181 182 @Column(name = "sla_xml") 183 @Lob 184 private String slaXml = null; 185 186 @Basic 187 @Column(name = "pending") 188 private int pending = 0; 189 190 public CoordinatorActionBean() { 191 } 192 193 /** 194 * Serialize the coordinator bean to a data output. 195 * 196 * @param dataOutput data output. 197 * @throws IOException thrown if the coordinator bean could not be 198 * serialized. 199 */ 200 @Override 201 public void write(DataOutput dataOutput) throws IOException { 202 WritableUtils.writeStr(dataOutput, getJobId()); 203 WritableUtils.writeStr(dataOutput, getType()); 204 WritableUtils.writeStr(dataOutput, getId()); 205 WritableUtils.writeStr(dataOutput, getCreatedConf()); 206 WritableUtils.writeStr(dataOutput, getStatus().toString()); 207 dataOutput.writeInt(getActionNumber()); 208 WritableUtils.writeStr(dataOutput, getRunConf()); 209 WritableUtils.writeStr(dataOutput, getExternalStatus()); 210 WritableUtils.writeStr(dataOutput, getTrackerUri()); 211 WritableUtils.writeStr(dataOutput, getConsoleUrl()); 212 WritableUtils.writeStr(dataOutput, getErrorCode()); 213 WritableUtils.writeStr(dataOutput, getErrorMessage()); 214 dataOutput.writeLong((getCreatedTime() != null) ? getCreatedTime().getTime() : -1); 215 dataOutput.writeLong((getLastModifiedTime() != null) ? getLastModifiedTime().getTime() : -1); 216 } 217 218 /** 219 * Deserialize a coordinator bean from a data input. 220 * 221 * @param dataInput data input. 222 * @throws IOException thrown if the workflow bean could not be 223 * deserialized. 224 */ 225 @Override 226 public void readFields(DataInput dataInput) throws IOException { 227 setJobId(WritableUtils.readStr(dataInput)); 228 setType(WritableUtils.readStr(dataInput)); 229 setId(WritableUtils.readStr(dataInput)); 230 setCreatedConf(WritableUtils.readStr(dataInput)); 231 setStatus(CoordinatorAction.Status.valueOf(WritableUtils.readStr(dataInput))); 232 setActionNumber(dataInput.readInt()); 233 setRunConf(WritableUtils.readStr(dataInput)); 234 setExternalStatus(WritableUtils.readStr(dataInput)); 235 setTrackerUri(WritableUtils.readStr(dataInput)); 236 setConsoleUrl(WritableUtils.readStr(dataInput)); 237 setErrorCode(WritableUtils.readStr(dataInput)); 238 setErrorMessage(WritableUtils.readStr(dataInput)); 239 long d = dataInput.readLong(); 240 if (d != -1) { 241 setCreatedTime(new Date(d)); 242 } 243 d = dataInput.readLong(); 244 if (d != -1) { 245 setLastModifiedTime(new Date(d)); 246 } 247 } 248 249 @Override 250 public String getJobId() { 251 return this.jobId; 252 } 253 254 @Override 255 public void setJobId(String id) { 256 super.setJobId(id); 257 this.jobId = id; 258 } 259 260 @Override 261 public Status getStatus() { 262 return Status.valueOf(status); 263 } 264 265 @Override 266 public void setStatus(Status status) { 267 super.setStatus(status); 268 this.status = status.toString(); 269 } 270 271 @Override 272 public void setCreatedTime(Date createdTime) { 273 this.createdTimestamp = DateUtils.convertDateToTimestamp(createdTime); 274 super.setCreatedTime(createdTime); 275 } 276 277 public void setRerunTime(Date rerunTime) { 278 this.rerunTimestamp = DateUtils.convertDateToTimestamp(rerunTime); 279 } 280 281 @Override 282 public void setNominalTime(Date nominalTime) { 283 this.nominalTimestamp = DateUtils.convertDateToTimestamp(nominalTime); 284 super.setNominalTime(nominalTime); 285 } 286 287 @Override 288 public void setLastModifiedTime(Date lastModifiedTime) { 289 this.lastModifiedTimestamp = DateUtils.convertDateToTimestamp(lastModifiedTime); 290 super.setLastModifiedTime(lastModifiedTime); 291 } 292 293 @Override 294 public Date getCreatedTime() { 295 return DateUtils.toDate(createdTimestamp); 296 } 297 298 public Timestamp getCreatedTimestamp() { 299 return createdTimestamp; 300 } 301 302 public Date getRerunTime() { 303 return DateUtils.toDate(rerunTimestamp); 304 } 305 306 public Timestamp getRerunTimestamp() { 307 return rerunTimestamp; 308 } 309 310 @Override 311 public Date getLastModifiedTime() { 312 return DateUtils.toDate(lastModifiedTimestamp); 313 } 314 315 public Timestamp getLastModifiedTimestamp() { 316 return lastModifiedTimestamp; 317 } 318 319 @Override 320 public Date getNominalTime() { 321 return DateUtils.toDate(nominalTimestamp); 322 } 323 324 public Timestamp getNominalTimestamp() { 325 return nominalTimestamp; 326 } 327 328 @Override 329 public String getExternalId() { 330 return externalId; 331 } 332 333 @Override 334 public void setExternalId(String externalId) { 335 super.setExternalId(externalId); 336 this.externalId = externalId; 337 } 338 339 public String getSlaXml() { 340 return slaXml; 341 } 342 343 public void setSlaXml(String slaXml) { 344 this.slaXml = slaXml; 345 } 346 347 /** 348 * @return true if in terminal status 349 */ 350 public boolean isTerminalStatus() { 351 boolean isTerminal = true; 352 switch (getStatus()) { 353 case WAITING: 354 case READY: 355 case SUBMITTED: 356 case RUNNING: 357 case SUSPENDED: 358 isTerminal = false; 359 break; 360 default: 361 isTerminal = true; 362 break; 363 } 364 return isTerminal; 365 } 366 367 /** 368 * Set some actions are in progress for particular coordinator action. 369 * 370 * @param pending set pending to true 371 */ 372 public void setPending(int pending) { 373 this.pending = pending; 374 } 375 376 /** 377 * increment pending and return it 378 * 379 * @return pending 380 */ 381 public int incrementAndGetPending() { 382 this.pending++; 383 return pending; 384 } 385 386 /** 387 * decrement pending and return it 388 * 389 * @return pending 390 */ 391 public int decrementAndGetPending() { 392 this.pending = Math.max(this.pending - 1, 0); 393 return pending; 394 } 395 396 /** 397 * Get some actions are in progress for particular bundle action. 398 * 399 * @return pending 400 */ 401 public int getPending() { 402 return this.pending; 403 } 404 405 /** 406 * Return if the action is pending. 407 * 408 * @return if the action is pending. 409 */ 410 public boolean isPending() { 411 return pending > 0 ? true : false; 412 } 413 }