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