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