This project has retired. For details please refer to its Attic page.
Source code
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
019
020package org.apache.oozie.sla;
021
022import java.util.Date;
023import java.util.Map;
024
025import org.apache.oozie.AppType;
026import org.apache.oozie.client.OozieClient;
027import org.apache.oozie.client.event.SLAEvent;
028import org.apache.oozie.lock.LockToken;
029import org.apache.oozie.service.JobsConcurrencyService;
030import org.apache.oozie.service.MemoryLocksService;
031import org.apache.oozie.service.Services;
032import org.apache.oozie.sla.service.SLAService;
033import org.apache.oozie.util.LogUtils;
034import org.apache.oozie.util.XLog;
035
036/**
037 * Class used by SLAService to store SLA objects and perform calculations and
038 * sla decisions
039 */
040public class SLACalcStatus extends SLAEvent {
041
042    public static String SLA_ENTITYKEY_PREFIX = "sla-";
043    private SLARegistrationBean regBean;
044    private String jobStatus;
045    private SLAStatus slaStatus;
046    private EventStatus eventStatus;
047    private Date actualStart;
048    private Date actualEnd;
049    private long actualDuration = -1;
050    private Date lastModifiedTime;
051    private byte eventProcessed;
052    private LockToken lock;
053
054    private XLog LOG;
055
056    public SLACalcStatus(SLARegistrationBean reg) {
057        this();
058        setSLARegistrationBean(reg);
059        LOG = LogUtils.setLogPrefix(LOG, this);
060    }
061
062    public SLACalcStatus(SLASummaryBean summary, SLARegistrationBean regBean) {
063        this();
064        SLARegistrationBean reg = new SLARegistrationBean();
065        reg.setNotificationMsg(regBean.getNotificationMsg());
066        reg.setUpstreamApps(regBean.getUpstreamApps());
067        reg.setAlertContact(regBean.getAlertContact());
068        reg.setAlertEvents(regBean.getAlertEvents());
069        reg.setJobData(regBean.getJobData());
070        if (regBean.getSLAConfigMap().containsKey(OozieClient.SLA_DISABLE_ALERT)) {
071            reg.addToSLAConfigMap(OozieClient.SLA_DISABLE_ALERT,
072                    regBean.getSLAConfigMap().get(OozieClient.SLA_DISABLE_ALERT));
073        }
074        reg.setId(summary.getId());
075        reg.setAppType(summary.getAppType());
076        reg.setUser(summary.getUser());
077        reg.setAppName(summary.getAppName());
078        reg.setParentId(summary.getParentId());
079        reg.setNominalTime(summary.getNominalTime());
080        reg.setExpectedStart(summary.getExpectedStart());
081        reg.setExpectedEnd(summary.getExpectedEnd());
082        reg.setExpectedDuration(summary.getExpectedDuration());
083        setSLARegistrationBean(reg);
084        setActualStart(summary.getActualStart());
085        setActualEnd(summary.getActualEnd());
086        setActualDuration(summary.getActualDuration());
087        setSLAStatus(summary.getSLAStatus());
088        setJobStatus(summary.getJobStatus());
089        setEventStatus(summary.getEventStatus());
090        setLastModifiedTime(summary.getLastModifiedTime());
091        setEventProcessed(summary.getEventProcessed());
092        LOG = LogUtils.setLogPrefix(LOG, this);
093    }
094
095    /**
096     * copy constructor
097     * @return SLACalcStatus
098     */
099    public SLACalcStatus(SLACalcStatus a) {
100        this();
101        setSLARegistrationBean(a.getSLARegistrationBean());
102        setJobStatus(a.getJobStatus());
103        setSLAStatus(a.getSLAStatus());
104        setEventStatus(a.getEventStatus());
105        setActualStart(a.getActualStart());
106        setActualEnd(a.getActualEnd());
107        setActualDuration(a.getActualDuration());
108        setEventProcessed(a.getEventProcessed());
109        LOG = LogUtils.setLogPrefix(LOG, this);
110    }
111
112    public SLACalcStatus() {
113        setMsgType(MessageType.SLA);
114        setLastModifiedTime(new Date());
115        LOG = XLog.getLog(getClass());
116    }
117
118    public SLARegistrationBean getSLARegistrationBean() {
119        return regBean;
120    }
121
122    public void setSLARegistrationBean(SLARegistrationBean slaBean) {
123        this.regBean = slaBean;
124    }
125
126    @Override
127    public String getId() {
128        return regBean.getId();
129    }
130
131    public void setId(String id) {
132        regBean.setId(id);
133    }
134
135    @Override
136    public Date getActualStart() {
137        return actualStart;
138    }
139
140    public void setActualStart(Date actualStart) {
141        this.actualStart = actualStart;
142    }
143
144    @Override
145    public Date getActualEnd() {
146        return actualEnd;
147    }
148
149    public void setActualEnd(Date actualEnd) {
150        this.actualEnd = actualEnd;
151    }
152
153    @Override
154    public long getActualDuration() {
155        return actualDuration;
156    }
157
158    public void setActualDuration(long actualDuration) {
159        this.actualDuration = actualDuration;
160    }
161
162    @Override
163    public String getJobStatus() {
164        return jobStatus;
165    }
166
167    public void setJobStatus(String status) {
168        this.jobStatus = status;
169    }
170
171    @Override
172    public SLAStatus getSLAStatus() {
173        return slaStatus;
174    }
175
176    public void setSLAStatus(SLAStatus slaStatus) {
177        this.slaStatus = slaStatus;
178    }
179
180    @Override
181    public EventStatus getEventStatus() {
182        return eventStatus;
183    }
184
185    public void setEventStatus(EventStatus es) {
186        this.eventStatus = es;
187    }
188
189    public void setLastModifiedTime(Date lastModifiedTime) {
190        this.lastModifiedTime = lastModifiedTime;
191    }
192
193    /**
194     * Get which type of sla event has been processed needed when calculator
195     * periodically loops to update all jobs' sla
196     *
197     * @return byte 1st bit set (from LSB) = start processed
198     * 2nd bit set = duration processed
199     * 3rd bit set = end processed
200     * only 4th bit set = everything processed
201     */
202    public byte getEventProcessed() {
203        return eventProcessed;
204    }
205
206    public void setEventProcessed(int eventProcessed) {
207        this.eventProcessed = (byte) eventProcessed;
208    }
209
210    @Override
211    public String getParentId() {
212        return regBean.getParentId();
213    }
214
215    @Override
216    public AppType getAppType() {
217        return regBean.getAppType();
218    }
219
220    @Override
221    public String getAppName() {
222        return regBean.getAppName();
223    }
224
225    @Override
226    public Date getNominalTime() {
227        return regBean.getNominalTime();
228    }
229
230    @Override
231    public Date getExpectedStart() {
232        return regBean.getExpectedStart();
233    }
234
235    @Override
236    public Date getExpectedEnd() {
237        return regBean.getExpectedEnd();
238    }
239
240    @Override
241    public long getExpectedDuration() {
242        return regBean.getExpectedDuration();
243    }
244
245    @Override
246    public String getNotificationMsg() {
247        return regBean.getNotificationMsg();
248    }
249
250    @Override
251    public String getAlertEvents() {
252        return regBean.getAlertEvents();
253    }
254
255    @Override
256    public String getAlertContact() {
257        return regBean.getAlertContact();
258    }
259
260    @Override
261    public String getUpstreamApps() {
262        return regBean.getUpstreamApps();
263    }
264
265    @Override
266    public String getJobData() {
267        return regBean.getJobData();
268    }
269
270    @Override
271    public String getUser() {
272        return regBean.getUser();
273    }
274
275    @Override
276    public String getSLAConfig() {
277        return regBean.getSlaConfig();
278    }
279
280    public Map<String, String> getSLAConfigMap() {
281        return regBean.getSLAConfigMap();
282    }
283
284    @Override
285    public MessageType getMsgType() {
286        return regBean.getMsgType();
287    }
288
289    @Override
290    public Date getLastModifiedTime() {
291        return lastModifiedTime;
292    }
293
294    public String getEntityKey() {
295        return SLA_ENTITYKEY_PREFIX + this.getId();
296    }
297    /**
298     * Obtain an exclusive lock on the {link #getEntityKey}.
299     * <p/>
300     * A timeout of {link #getLockTimeOut} is used when trying to obtain the lock.
301     *
302     * @throws InterruptedException thrown if an interruption happened while trying to obtain the lock
303     */
304    public void acquireLock() throws InterruptedException {
305        // only get ZK lock when multiple servers running
306        if (Services.get().get(JobsConcurrencyService.class).isHighlyAvailableMode()) {
307            lock = Services.get().get(MemoryLocksService.class).getWriteLock(getEntityKey(), getLockTimeOut());
308            if (lock == null) {
309            LOG.debug("Could not aquire lock for [{0}]", getEntityKey());
310            }
311            else {
312                LOG.debug("Acquired lock for [{0}]", getEntityKey());
313            }
314        }
315        else {
316            lock = new DummyToken();
317        }
318    }
319
320    private static class DummyToken implements LockToken {
321        @Override
322        public void release() {
323        }
324    }
325
326    public boolean isLocked() {
327        boolean locked = false;
328        if(lock != null) {
329            locked = true;
330        }
331        return locked;
332    }
333
334    public void releaseLock(){
335        if (lock != null) {
336            lock.release();
337            lock = null;
338            LOG.debug("Released lock for [{0}]", getEntityKey());
339        }
340    }
341
342    public long getLockTimeOut() {
343        return Services.get().getConf().getLong(SLAService.CONF_SLA_CALC_LOCK_TIMEOUT, 5 * 1000);
344    }
345
346}