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