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