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.DiscriminatorColumn;
026    import javax.persistence.DiscriminatorType;
027    import javax.persistence.Entity;
028    import javax.persistence.Id;
029    import javax.persistence.NamedQueries;
030    import javax.persistence.NamedQuery;
031    import javax.persistence.Table;
032    
033    import org.apache.hadoop.io.Writable;
034    import org.apache.oozie.client.Job.Status;
035    import org.apache.oozie.util.DateUtils;
036    import org.apache.oozie.util.WritableUtils;
037    import org.apache.openjpa.persistence.jdbc.Index;
038    
039    @Entity
040    @Table(name = "BUNDLE_ACTIONS")
041    @DiscriminatorColumn(name = "bean_type", discriminatorType = DiscriminatorType.STRING)
042    @NamedQueries( {
043            @NamedQuery(name = "DELETE_BUNDLE_ACTION", query = "delete from BundleActionBean w where w.bundleActionId = :bundleActionId"),
044    
045            @NamedQuery(name = "GET_BUNDLE_ACTIONS_FOR_BUNDLE", query = "select OBJECT(w) from BundleActionBean w where w.bundleId = :bundleId"),
046    
047            @NamedQuery(name = "GET_BUNDLE_ACTIONS", query = "select OBJECT(w) from BundleActionBean w"),
048    
049            @NamedQuery(name = "GET_BUNDLE_ACTIONS_BY_LAST_MODIFIED_TIME", query = "select OBJECT(w) from BundleActionBean w where w.lastModifiedTimestamp >= :lastModifiedTime"),
050    
051            @NamedQuery(name = "GET_BUNDLE_WAITING_ACTIONS_OLDER_THAN", query = "select OBJECT(a) from BundleActionBean a where a.pending > 0 AND a.lastModifiedTimestamp <= :lastModifiedTime"),
052    
053            @NamedQuery(name = "GET_BUNDLE_ACTION", query = "select OBJECT(w) from BundleActionBean w where w.bundleActionId = :bundleActionId"),
054    
055            @NamedQuery(name = "GET_BUNDLE_ACTIONS_COUNT", query = "select count(w) from BundleActionBean w"),
056    
057            @NamedQuery(name = "GET_BUNDLE_ACTIONS_COUNT_BY_JOB", query = "select count(w) from BundleActionBean w where w.bundleId = :bundleId"),
058    
059            @NamedQuery(name = "GET_BUNDLE_ACTIONS_PENDING_TRUE_COUNT", query = "select count(w) from BundleActionBean w where w.bundleId = :bundleId AND w.pending > 0"),
060    
061            @NamedQuery(name = "GET_BUNDLE_ACTIONS_NOT_EQUAL_STATUS_COUNT", query = "select count(w) from BundleActionBean w where w.bundleId = :bundleId AND w.status <> :status"),
062    
063            @NamedQuery(name = "GET_BUNDLE_ACTIONS_NOT_TERMINATE_STATUS_COUNT", query = "select count(w) from BundleActionBean w where w.bundleId = :bundleId AND (w.status = 'PERP' OR w.status = 'RUNNING' OR w.status = 'SUSPENDED' OR w.status = 'PREPSUSPENDED' OR w.status = 'PAUSED' OR w.status = 'PREPPAUSED')"),
064    
065            @NamedQuery(name = "GET_BUNDLE_ACTIONS_FAILED_NULL_COORD_COUNT", query = "select count(w) from BundleActionBean w where w.bundleId = :bundleId AND w.status = 'FAILED' AND w.coordId IS NULL"),
066    
067            @NamedQuery(name = "GET_BUNDLE_ACTIONS_OLDER_THAN", query = "select OBJECT(w) from BundleActionBean w order by w.lastModifiedTimestamp"),
068    
069            @NamedQuery(name = "DELETE_COMPLETED_ACTIONS_FOR_BUNDLE", query = "delete from BundleActionBean a where a.bundleId = :bundleId and (a.status = 'SUCCEEDED' OR a.status = 'FAILED' OR a.status= 'KILLED' OR a.status = 'DONEWITHERROR')")})
070    public class BundleActionBean implements Writable {
071    
072        @Id
073        @Column(name = "bundle_action_id")
074        private String bundleActionId = null;
075    
076        @Column(name = "bundle_id")
077        private String bundleId = null;
078    
079        @Column(name = "coord_name")
080        private String coordName = null;
081    
082        @Basic
083        @Column(name = "coord_id")
084        private String coordId = null;
085    
086        @Basic
087        @Column(name = "status")
088        private String status = null;
089    
090        @Basic
091        @Column(name = "critical")
092        private int critical = 0;
093    
094        @Basic
095        @Column(name = "pending")
096        private int pending = 0;
097    
098        @Basic
099        @Column(name = "last_modified_time")
100        private java.sql.Timestamp lastModifiedTimestamp = null;
101    
102        /**
103         * bundleActionId to set
104         *
105         * @param bundleActionId the bundleActionId to set
106         */
107        public void setBundleActionId(String bundleActionId) {
108            this.bundleActionId = bundleActionId;
109        }
110    
111        /**
112         * Get the Bundle Action Id.
113         *
114         * @return the bundleActionId
115         */
116        public String getBundleActionId() {
117            return bundleActionId;
118        }
119    
120        /**
121         * Get the BundleId
122         *
123         * @return bundleId
124         */
125        public String getBundleId() {
126            return bundleId;
127        }
128    
129        /**
130         * Set the Bundle Id.
131         *
132         * @param bundleId
133         */
134        public void setBundleId(String bundleId) {
135            this.bundleId = bundleId;
136        }
137    
138        /**
139         * Get the Coordinator name.
140         *
141         * @return coordName
142         */
143        public String getCoordName() {
144            return coordName;
145        }
146    
147        /**
148         * Set the Coordinator name.
149         *
150         * @param coordName
151         */
152        public void setCoordName(String coordName) {
153            this.coordName = coordName;
154        }
155    
156        /**
157         * Get the coordinator Id.
158         *
159         * @return the coordId
160         */
161        public String getCoordId() {
162            return coordId;
163        }
164    
165        /**
166         * Set the coordinator Id.
167         *
168         * @param coordId
169         */
170        public void setCoordId(String coordId) {
171            this.coordId = coordId;
172        }
173    
174        /**
175         * Get the Status of the Bundle Action
176         *
177         * @return status object
178         */
179        public Status getStatus() {
180            return Status.valueOf(this.status);
181        }
182    
183        /**
184         * Get the Status of the Bundle Action
185         *
186         * @return status string
187         */
188        public String getStatusStr() {
189            return status;
190        }
191    
192        /**
193         * Set the Status of the Bundle Action
194         *
195         * @param val
196         */
197        public void setStatus(Status val) {
198            this.status = val.toString();
199        }
200    
201        /**
202         * Set Whether this bundle action is critical or not.
203         *
204         * @param critical set critical to true
205         */
206        public void setCritical() {
207            this.critical = 1;
208        }
209    
210        /**
211         * Reseset Whether this bundle action is critical or not.
212         *
213         * @param critical set critical to false
214         */
215        public void resetCritical() {
216            this.critical = 0;
217        }
218    
219        /**
220         * Return if the action is critical.
221         *
222         * @return if the action is critical.
223         */
224        public boolean isCritical() {
225            return critical == 1 ? true : false;
226        }
227    
228        /**
229         * Set some actions are in progress for particular bundle action.
230         *
231         * @param pending set pending to true
232         */
233        public void setPending(int pending) {
234            this.pending = pending;
235        }
236    
237        /**
238         * increment pending and return it
239         *
240         * @return pending
241         */
242        public int incrementAndGetPending() {
243            this.pending++;
244            return pending;
245        }
246    
247        /**
248         * decrement pending and return it
249         *
250         * @return pending
251         */
252        public int decrementAndGetPending() {
253            this.pending = Math.max(this.pending-1, 0);
254            return pending;
255        }
256    
257        /**
258         * Get some actions are in progress for particular bundle action.
259         *
260         * @return pending
261         */
262        public int getPending() {
263            return this.pending;
264        }
265    
266        /**
267         * Return if the action is pending.
268         *
269         * @return if the action is pending.
270         */
271        public boolean isPending() {
272            return pending > 0 ? true : false;
273        }
274    
275        /**
276         * Set Last modified time.
277         *
278         * @param lastModifiedTimestamp the lastModifiedTimestamp to set
279         */
280        public void setLastModifiedTimestamp(java.sql.Timestamp lastModifiedTimestamp) {
281            this.lastModifiedTimestamp = lastModifiedTimestamp;
282        }
283    
284        /**
285         * Set Last modified time.
286         *
287         * @param lastModifiedTime the lastModifiedTime to set
288         */
289        public void setLastModifiedTime(Date lastModifiedTime) {
290            this.lastModifiedTimestamp = DateUtils.convertDateToTimestamp(lastModifiedTime);
291        }
292    
293        /**
294         * Get Last modified time.
295         *
296         * @return lastModifiedTime
297         */
298        public Date getLastModifiedTime() {
299            return DateUtils.toDate(lastModifiedTimestamp);
300        }
301    
302        /**
303         * Get Last modified time.
304         *
305         * @return lastModifiedTimestamp
306         */
307        public Timestamp getLastModifiedTimestamp() {
308            return lastModifiedTimestamp;
309        }
310    
311        /* (non-Javadoc)
312         * @see org.apache.hadoop.io.Writable#write(java.io.DataOutput)
313         */
314        @Override
315        public void write(DataOutput dataOutput) throws IOException {
316            WritableUtils.writeStr(dataOutput, getBundleActionId());
317            WritableUtils.writeStr(dataOutput, getBundleId());
318            WritableUtils.writeStr(dataOutput, getCoordName());
319            WritableUtils.writeStr(dataOutput, getCoordId());
320            WritableUtils.writeStr(dataOutput, getStatusStr());
321            dataOutput.writeInt(critical);
322            dataOutput.writeInt(pending);
323            dataOutput.writeLong((getLastModifiedTimestamp() != null) ? getLastModifiedTimestamp().getTime() : -1);
324        }
325    
326        /* (non-Javadoc)
327         * @see org.apache.hadoop.io.Writable#readFields(java.io.DataInput)
328         */
329        @Override
330        public void readFields(DataInput dataInput) throws IOException {
331            setBundleActionId(WritableUtils.readStr(dataInput));
332            setBundleId(WritableUtils.readStr(dataInput));
333            setCoordName(WritableUtils.readStr(dataInput));
334            setCoordId(WritableUtils.readStr(dataInput));
335            setStatus(Status.valueOf(WritableUtils.readStr(dataInput)));
336            critical = dataInput.readInt();
337            pending = dataInput.readInt();
338            long d = dataInput.readLong();
339            if (d != -1) {
340                setLastModifiedTime(new Date(d));
341            }
342        }
343    }