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.Entity;
029    import javax.persistence.Lob;
030    import javax.persistence.NamedQueries;
031    import javax.persistence.NamedQuery;
032    
033    import org.apache.hadoop.io.Writable;
034    import org.apache.oozie.client.BundleJob;
035    import org.apache.oozie.client.Job;
036    import org.apache.oozie.client.rest.JsonBundleJob;
037    import org.apache.oozie.util.DateUtils;
038    import org.apache.oozie.util.WritableUtils;
039    import org.apache.openjpa.persistence.jdbc.Index;
040    
041    @Entity
042    @NamedQueries( {
043            @NamedQuery(name = "UPDATE_BUNDLE_JOB", query = "update BundleJobBean w set w.appName = :appName, w.appPath = :appPath, w.conf = :conf, w.externalId = :externalId, w.timeOut = :timeOut, w.authToken = :authToken, w.createdTimestamp = :createdTimestamp, w.endTimestamp = :endTimestamp, w.jobXml = :jobXml, w.lastModifiedTimestamp = :lastModifiedTimestamp, w.origJobXml = :origJobXml, w.startTimestamp = :startTimestamp, w.status = :status, w.timeUnitStr = :timeUnit, w.pending = :pending where w.id = :id"),
044    
045            @NamedQuery(name = "UPDATE_BUNDLE_JOB_STATUS", query = "update BundleJobBean w set w.status = :status, w.lastModifiedTimestamp = :lastModifiedTimestamp, w.pending = :pending where w.id = :id"),
046    
047            @NamedQuery(name = "DELETE_BUNDLE_JOB", query = "delete from BundleJobBean w where w.id = :id"),
048    
049            @NamedQuery(name = "GET_BUNDLE_JOBS", query = "select OBJECT(w) from BundleJobBean w"),
050    
051            @NamedQuery(name = "GET_BUNDLE_JOB", query = "select OBJECT(w) from BundleJobBean w where w.id = :id"),
052    
053            @NamedQuery(name = "GET_BUNDLE_JOBS_COUNT", query = "select count(w) from BundleJobBean w"),
054    
055            @NamedQuery(name = "GET_BUNDLE_JOBS_COLUMNS", query = "select w.id, w.appName, w.appPath, w.conf, w.status, w.kickoffTimestamp, w.startTimestamp, w.endTimestamp, w.pauseTimestamp, w.createdTimestamp, w.user, w.group, w.timeUnitStr, w.timeOut from BundleJobBean w order by w.createdTimestamp desc"),
056    
057            @NamedQuery(name = "GET_BUNDLE_JOBS_RUNNING_OR_PENDING", query = "select OBJECT(w) from BundleJobBean w where w.status = 'RUNNING' OR w.status = 'RUNNINGWITHERROR' OR w.pending = 1 order by w.lastModifiedTimestamp"),
058    
059            @NamedQuery(name = "GET_BUNDLE_JOBS_NEED_START", query = "select OBJECT(w) from BundleJobBean w where w.status = 'PREP' AND (w.kickoffTimestamp IS NULL OR (w.kickoffTimestamp IS NOT NULL AND w.kickoffTimestamp <= :currentTime)) order by w.lastModifiedTimestamp"),
060    
061            @NamedQuery(name = "GET_BUNDLE_JOBS_PAUSED", query = "select OBJECT(w) from BundleJobBean w where w.status = 'PAUSED' OR w.status = 'PAUSEDWITHERROR' OR w.status = 'PREPPAUSED' order by w.lastModifiedTimestamp"),
062    
063            @NamedQuery(name = "GET_BUNDLE_JOBS_UNPAUSED", query = "select OBJECT(w) from BundleJobBean w where w.status = 'RUNNING' OR w.status = 'RUNNINGWITHERROR' OR w.status = 'PREP' order by w.lastModifiedTimestamp"),
064    
065            @NamedQuery(name = "GET_BUNDLE_JOBS_OLDER_THAN", query = "select OBJECT(w) from BundleJobBean w where w.startTimestamp <= :matTime AND (w.status = 'PREP' OR w.status = 'RUNNING' or w.status = 'RUNNINGWITHERROR')  order by w.lastModifiedTimestamp"),
066    
067            @NamedQuery(name = "GET_BUNDLE_JOBS_OLDER_THAN_STATUS", query = "select OBJECT(w) from BundleJobBean w where w.status = :status AND w.lastModifiedTimestamp <= :lastModTime order by w.lastModifiedTimestamp"),
068    
069            @NamedQuery(name = "GET_COMPLETED_BUNDLE_JOBS_OLDER_THAN", query = "select OBJECT(w) from BundleJobBean w where ( w.status = 'SUCCEEDED' OR w.status = 'FAILED' OR w.status = 'KILLED' OR w.status = 'DONEWITHERROR') AND w.lastModifiedTimestamp <= :lastModTime order by w.lastModifiedTimestamp"),
070    
071            @NamedQuery(name = "BULK_MONITOR_BUNDLE_QUERY", query = "SELECT b.id, b.status FROM BundleJobBean b WHERE b.appName = :appName"),
072    
073            // Join query
074            @NamedQuery(name = "BULK_MONITOR_ACTIONS_QUERY", query = "SELECT a.id, a.actionNumber, a.errorCode, a.errorMessage, a.externalId, " +
075                    "a.externalStatus, a.status, a.createdTimestamp, a.nominalTimestamp, a.missingDependencies, " +
076                    "c.id, c.appName, c.status FROM CoordinatorActionBean a, CoordinatorJobBean c " +
077                    "WHERE a.jobId = c.id AND c.bundleId = :bundleId ORDER BY a.jobId, a.createdTimestamp"),
078    
079            @NamedQuery(name = "BULK_MONITOR_COUNT_QUERY", query = "SELECT COUNT(a) FROM CoordinatorActionBean a, CoordinatorJobBean c") })
080    public class BundleJobBean extends JsonBundleJob implements Writable {
081    
082        @Basic
083        @Index
084        @Column(name = "status")
085        private String status = Job.Status.PREP.toString();
086    
087        @Basic
088        @Column(name = "auth_token")
089        @Lob
090        private String authToken = null;
091    
092        @Basic
093        @Column(name = "kickoff_time")
094        private java.sql.Timestamp kickoffTimestamp = null;
095    
096        @Basic
097        @Column(name = "start_time")
098        private java.sql.Timestamp startTimestamp = null;
099    
100        @Basic
101        @Column(name = "end_time")
102        private java.sql.Timestamp endTimestamp = null;
103    
104        @Basic
105        @Column(name = "pause_time")
106        private java.sql.Timestamp pauseTimestamp = null;
107    
108        @Basic
109        @Index
110        @Column(name = "created_time")
111        private java.sql.Timestamp createdTimestamp = null;
112    
113        @Basic
114        @Column(name = "time_unit")
115        private String timeUnitStr = BundleJob.Timeunit.NONE.toString();
116    
117        @Basic
118        @Column(name = "pending")
119        private int pending = 0;
120    
121        @Basic
122        @Index
123        @Column(name = "last_modified_time")
124        private java.sql.Timestamp lastModifiedTimestamp = null;
125    
126        @Basic
127        @Index
128        @Column(name = "suspended_time")
129        private java.sql.Timestamp suspendedTimestamp = null;
130    
131        @Column(name = "job_xml")
132        @Lob
133        private String jobXml = null;
134    
135        @Column(name = "orig_job_xml")
136        @Lob
137        private String origJobXml = null;
138    
139        /**
140         * @return the authToken
141         */
142        public String getAuthToken() {
143            return authToken;
144        }
145    
146        /**
147         * @param authToken the authToken to set
148         */
149        public void setAuthToken(String authToken) {
150            this.authToken = authToken;
151        }
152    
153        /**
154         * @return the kickoffTimestamp
155         */
156        public java.sql.Timestamp getKickoffTimestamp() {
157            return kickoffTimestamp;
158        }
159    
160        /**
161         * @return the startTimestamp
162         */
163        public java.sql.Timestamp getstartTimestamp() {
164            return startTimestamp;
165        }
166    
167        /**
168         * @param kickoffTimestamp the kickoffTimestamp to set
169         */
170        public void setKickoffTimestamp(java.sql.Timestamp kickoffTimestamp) {
171            super.setKickoffTime(DateUtils.toDate(kickoffTimestamp));
172            this.kickoffTimestamp = kickoffTimestamp;
173        }
174    
175        /**
176         * @param startTimestamp the startTimestamp to set
177         */
178        public void setStartTimestamp(java.sql.Timestamp startTimestamp) {
179            super.setStartTime(DateUtils.toDate(startTimestamp));
180            this.startTimestamp = startTimestamp;
181        }
182    
183        /**
184         * Set startTime
185         *
186         * @param startTime the startTime to set
187         */
188        @Override
189        public void setStartTime(Date startTime) {
190            super.setStartTime(startTime);
191            this.startTimestamp = DateUtils.convertDateToTimestamp(startTime);
192        }
193    
194        /**
195         * @return the endTimestamp
196         */
197        public java.sql.Timestamp getEndTimestamp() {
198            return endTimestamp;
199        }
200    
201        /**
202         * @param endTimestamp the endTimestamp to set
203         */
204        public void setEndTimestamp(java.sql.Timestamp endTimestamp) {
205            super.setEndTime(DateUtils.toDate(endTimestamp));
206            this.endTimestamp = endTimestamp;
207        }
208    
209        /**
210         * @return the pauseTimestamp
211         */
212        public java.sql.Timestamp getPauseTimestamp() {
213            return pauseTimestamp;
214        }
215    
216        /**
217         * @param pauseTimestamp the pauseTimestamp to set
218         */
219        public void setPauseTimestamp(java.sql.Timestamp pauseTimestamp) {
220            super.setPauseTime(DateUtils.toDate(pauseTimestamp));
221            this.pauseTimestamp = pauseTimestamp;
222        }
223    
224        /**
225         * @return the createdTimestamp
226         */
227        public java.sql.Timestamp getCreatedTimestamp() {
228            return createdTimestamp;
229        }
230    
231        /**
232         * @return the createdTime
233         */
234        @Override
235        public Date getCreatedTime() {
236            return DateUtils.toDate(createdTimestamp);
237        }
238    
239        /**
240         * @return the timeUnitStr
241         */
242        public String getTimeUnitStr() {
243            return timeUnitStr;
244        }
245    
246        /**
247         * @return the pending
248         */
249        public int getPending() {
250            return pending;
251        }
252    
253        /**
254         * Set pending to true
255         *
256         * @param pending set pending to true
257         */
258        @Override
259        public void setPending() {
260            super.setPending();
261            this.pending = 1;
262        }
263    
264        /**
265         * Set pending to false
266         *
267         * @param pending set pending to false
268         */
269        @Override
270        public void resetPending() {
271            super.resetPending();
272            this.pending = 0;
273        }
274    
275        /**
276         * Return if the action is pending.
277         *
278         * @return if the action is pending.
279         */
280        public boolean isPending() {
281            return pending == 1 ? true : false;
282        }
283    
284        /**
285         * @return the lastModifiedTimestamp
286         */
287        public java.sql.Timestamp getLastModifiedTimestamp() {
288            return lastModifiedTimestamp;
289        }
290    
291        /**
292         * @param lastModifiedTimestamp the lastModifiedTimestamp to set
293         */
294        public void setLastModifiedTimestamp(java.sql.Timestamp lastModifiedTimestamp) {
295            this.lastModifiedTimestamp = lastModifiedTimestamp;
296        }
297    
298        /**
299         * @return the suspendedTimestamp
300         */
301        public Timestamp getSuspendedTimestamp() {
302            return suspendedTimestamp;
303        }
304    
305        /**
306         * @param suspendedTimestamp the suspendedTimestamp to set
307         */
308        public void setSuspendedTimestamp(Timestamp suspendedTimestamp) {
309            this.suspendedTimestamp = suspendedTimestamp;
310        }
311    
312        /**
313         * @return the jobXml
314         */
315        public String getJobXml() {
316            return jobXml;
317        }
318    
319        /**
320         * @param jobXml the jobXml to set
321         */
322        public void setJobXml(String jobXml) {
323            this.jobXml = jobXml;
324        }
325    
326        /**
327         * @return the origJobXml
328         */
329        public String getOrigJobXml() {
330            return origJobXml;
331        }
332    
333        /**
334         * @param origJobXml the origJobXml to set
335         */
336        public void setOrigJobXml(String origJobXml) {
337            this.origJobXml = origJobXml;
338        }
339    
340        /**
341         * @param createTime the createdTime to set
342         */
343        @Override
344        public void setCreatedTime(Date createTime) {
345            super.setCreatedTime(createTime);
346            this.createdTimestamp = DateUtils.convertDateToTimestamp(createTime);
347        }
348    
349        /**
350         * @param lastModifiedTime
351         */
352        public void setLastModifiedTime(Date lastModifiedTime) {
353            this.lastModifiedTimestamp = DateUtils.convertDateToTimestamp(lastModifiedTime);
354        }
355    
356        /* (non-Javadoc)
357         * @see org.apache.hadoop.io.Writable#write(java.io.DataOutput)
358         */
359        @Override
360        public void write(DataOutput dataOutput) throws IOException {
361            WritableUtils.writeStr(dataOutput, getAppPath());
362            WritableUtils.writeStr(dataOutput, getAppName());
363            WritableUtils.writeStr(dataOutput, getId());
364            WritableUtils.writeStr(dataOutput, getConf());
365            WritableUtils.writeStr(dataOutput, getStatusStr());
366            WritableUtils.writeStr(dataOutput, getTimeUnit().toString());
367            dataOutput.writeLong((getKickoffTime() != null) ? getKickoffTime().getTime() : -1);
368            dataOutput.writeLong((getStartTime() != null) ? getStartTime().getTime() : -1);
369            dataOutput.writeLong((getEndTime() != null) ? getEndTime().getTime() : -1);
370            WritableUtils.writeStr(dataOutput, getUser());
371            WritableUtils.writeStr(dataOutput, getGroup());
372            WritableUtils.writeStr(dataOutput, getExternalId());
373            dataOutput.writeInt(getTimeout());
374        }
375    
376        /* (non-Javadoc)
377         * @see org.apache.hadoop.io.Writable#readFields(java.io.DataInput)
378         */
379        @Override
380        public void readFields(DataInput dataInput) throws IOException {
381    
382            setAppPath(WritableUtils.readStr(dataInput));
383            setAppName(WritableUtils.readStr(dataInput));
384            setId(WritableUtils.readStr(dataInput));
385            setConf(WritableUtils.readStr(dataInput));
386            setStatus(BundleJob.Status.valueOf(WritableUtils.readStr(dataInput)));
387            setTimeUnit(BundleJob.Timeunit.valueOf(WritableUtils.readStr(dataInput)));
388    
389            long d = dataInput.readLong();
390            if (d != -1) {
391                setKickoffTime(new Date(d));
392            }
393            d = dataInput.readLong();
394            if (d != -1) {
395                setStartTime(new Date(d));
396            }
397            d = dataInput.readLong();
398            if (d != -1) {
399                setEndTime(new Date(d));
400            }
401            setUser(WritableUtils.readStr(dataInput));
402            setGroup(WritableUtils.readStr(dataInput));
403            setExternalId(WritableUtils.readStr(dataInput));
404            setTimeOut(dataInput.readInt());
405        }
406    
407        /* (non-Javadoc)
408         * @see org.apache.oozie.client.rest.JsonBundleJob#getStatus()
409         */
410        @Override
411        public Status getStatus() {
412            return Status.valueOf(this.status);
413        }
414    
415        /**
416         * @return status string
417         */
418        public String getStatusStr() {
419            return status;
420        }
421    
422        /* (non-Javadoc)
423         * @see org.apache.oozie.client.rest.JsonBundleJob#getEndTime()
424         */
425        @Override
426        public Date getEndTime() {
427            return DateUtils.toDate(endTimestamp);
428        }
429    
430        /* (non-Javadoc)
431         * @see org.apache.oozie.client.rest.JsonBundleJob#getKickoffTime()
432         */
433        @Override
434        public Date getKickoffTime() {
435            return DateUtils.toDate(kickoffTimestamp);
436        }
437    
438        /* (non-Javadoc)
439         * @see org.apache.oozie.client.rest.JsonBundleJob#getTimeUnit()
440         */
441        @Override
442        public Timeunit getTimeUnit() {
443            return Timeunit.valueOf(this.timeUnitStr);
444        }
445    
446        /* (non-Javadoc)
447         * @see org.apache.oozie.client.rest.JsonBundleJob#setEndTime(java.util.Date)
448         */
449        @Override
450        public void setEndTime(Date endTime) {
451            super.setEndTime(endTime);
452            this.endTimestamp = DateUtils.convertDateToTimestamp(endTime);
453        }
454    
455        /* (non-Javadoc)
456         * @see org.apache.oozie.client.rest.JsonBundleJob#setKickoffTime(java.util.Date)
457         */
458        @Override
459        public void setKickoffTime(Date kickoffTime) {
460            super.setKickoffTime(kickoffTime);
461            this.kickoffTimestamp = DateUtils.convertDateToTimestamp(kickoffTime);
462        }
463    
464        @Override
465        /* (non-Javadoc)
466         * @see org.apache.oozie.client.rest.JsonBundleJob#getPauseTime()
467         */
468        public Date getPauseTime() {
469            return DateUtils.toDate(pauseTimestamp);
470        }
471    
472        /* (non-Javadoc)
473         * @see org.apache.oozie.client.rest.JsonBundleJob#setPauseTime(java.util.Date)
474         */
475        @Override
476        public void setPauseTime(Date pauseTime) {
477            super.setPauseTime(pauseTime);
478            this.pauseTimestamp = DateUtils.convertDateToTimestamp(pauseTime);
479        }
480    
481        /* (non-Javadoc)
482         * @see org.apache.oozie.client.rest.JsonBundleJob#setStatus(org.apache.oozie.client.BundleJob.Status)
483         */
484        @Override
485        public void setStatus(org.apache.oozie.client.BundleJob.Status val) {
486            super.setStatus(val);
487            this.status = val.toString();
488        }
489    
490        /* (non-Javadoc)
491         * @see org.apache.oozie.client.rest.JsonBundleJob#setTimeUnit(org.apache.oozie.client.BundleJob.Timeunit)
492         */
493        @Override
494        public void setTimeUnit(Timeunit timeUnit) {
495            super.setTimeUnit(timeUnit);
496            this.timeUnitStr = timeUnit.toString();
497        }
498    
499        /**
500         * @param return the suspendTime
501         */
502        public void setSuspendedTime(Date suspendTime) {
503            this.suspendedTimestamp = DateUtils.convertDateToTimestamp(suspendTime);
504        }
505    
506    }