This project has retired. For details please refer to its
Attic page.
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.util.db;
019
020 import java.util.Date;
021
022 import org.apache.oozie.ErrorCode;
023 import org.apache.oozie.SLAEventBean;
024 import org.apache.oozie.client.SLAEvent.SlaAppType;
025 import org.apache.oozie.client.SLAEvent.Status;
026 import org.apache.oozie.command.CommandException;
027 import org.apache.oozie.executor.jpa.SLAEventInsertJPAExecutor;
028 import org.apache.oozie.service.JPAService;
029 import org.apache.oozie.service.Services;
030 import org.apache.oozie.service.StoreService;
031 import org.apache.oozie.store.SLAStore;
032 import org.apache.oozie.store.Store;
033 import org.apache.oozie.util.DateUtils;
034 import org.apache.oozie.util.XLog;
035 import org.jdom.Element;
036
037 public class SLADbOperations {
038 public static final String CLIENT_ID_TAG = "oozie:sla:client-id";
039
040 public static void writeSlaRegistrationEvent(Element eSla, Store store, String slaId, SlaAppType appType, String user,
041 String groupName) throws Exception {
042 // System.out.println("BBBBB SLA added");
043 if (eSla == null) {
044 return;
045 }
046 // System.out.println("Writing REG AAAAA " + slaId);
047 SLAEventBean sla = new SLAEventBean();
048 // sla.setClientId(getTagElement( eSla, "client-id"));
049 // sla.setClientId(getClientId());
050 sla.setAppName(getTagElement(eSla, "app-name"));
051 sla.setParentClientId(getTagElement(eSla, "parent-child-id"));
052 sla.setParentSlaId(getTagElement(eSla, "parent-sla-id"));
053 String strNominalTime = getTagElement(eSla, "nominal-time");
054 // System.out.println("AAAAA SLA nominal time "+ strNominalTime);
055 if (strNominalTime == null || strNominalTime.length() == 0) {
056 throw new RuntimeException("Nominal time is required"); // TODO:
057 // change to
058 // CommandException
059 }
060 Date nominalTime = DateUtils.parseDateUTC(strNominalTime);
061 // Setting expected start time
062 String strRelExpectedStart = getTagElement(eSla, "should-start");
063 if (strRelExpectedStart == null || strRelExpectedStart.length() == 0) {
064 throw new RuntimeException("should-start can't be empty");
065 }
066 int relExpectedStart = Integer.parseInt(strRelExpectedStart);
067 if (relExpectedStart < 0) {
068 sla.setExpectedStart(null);
069 }
070 else {
071 Date expectedStart = new Date(nominalTime.getTime() + relExpectedStart * 60 * 1000);
072 sla.setExpectedStart(expectedStart);
073 // sla.setExpectedStart(nominalTime);
074 }
075
076 // Setting expected end time
077 String strRelExpectedEnd = getTagElement(eSla, "should-end");
078 if (strRelExpectedEnd == null || strRelExpectedEnd.length() == 0) {
079 throw new RuntimeException("should-end can't be empty");
080 }
081 int relExpectedEnd = Integer.parseInt(strRelExpectedEnd);
082 if (relExpectedEnd < 0) {
083 sla.setExpectedEnd(null);
084 }
085 else {
086 Date expectedEnd = new Date(nominalTime.getTime() + relExpectedEnd * 60 * 1000);
087 sla.setExpectedEnd(expectedEnd);
088 }
089
090 sla.setNotificationMsg(getTagElement(eSla, "notification-msg"));
091 sla.setAlertContact(getTagElement(eSla, "alert-contact"));
092 sla.setDevContact(getTagElement(eSla, "dev-contact"));
093 sla.setQaContact(getTagElement(eSla, "qa-contact"));
094 sla.setSeContact(getTagElement(eSla, "se-contact"));
095 sla.setAlertFrequency(getTagElement(eSla, "alert-frequency"));
096 sla.setAlertPercentage(getTagElement(eSla, "alert-percentage"));
097
098 sla.setUpstreamApps(getTagElement(eSla, "upstream-apps"));
099
100 // Oozie defined
101
102 sla.setSlaId(slaId);
103 sla.setAppType(appType);
104 sla.setUser(user);
105 sla.setGroupName(groupName);
106 sla.setJobStatus(Status.CREATED);
107 sla.setStatusTimestamp(new Date());
108
109 SLAStore slaStore = (SLAStore) Services.get().get(StoreService.class).getStore(SLAStore.class, store);
110 slaStore.insertSLAEvent(sla);
111 }
112
113 public static void writeSlaRegistrationEvent(Element eSla,
114 String slaId, SlaAppType appType, String user, String groupName, XLog log)
115 throws Exception {
116 // System.out.println("BBBBB SLA added");
117 if (eSla == null) {
118 return;
119 }
120 //System.out.println("Writing REG AAAAA " + slaId);
121 SLAEventBean sla = new SLAEventBean();
122 // sla.setClientId(getTagElement( eSla, "client-id"));
123 // sla.setClientId(getClientId());
124 sla.setAppName(getTagElement(eSla, "app-name"));
125 sla.setParentClientId(getTagElement(eSla, "parent-child-id"));
126 sla.setParentSlaId(getTagElement(eSla, "parent-sla-id"));
127 String strNominalTime = getTagElement(eSla, "nominal-time");
128 // System.out.println("AAAAA SLA nominal time "+ strNominalTime);
129 if (strNominalTime == null || strNominalTime.length() == 0) {
130 throw new RuntimeException("Nominal time is required"); // TODO:
131 // change to
132 // CommandException
133 }
134 Date nominalTime = DateUtils.parseDateUTC(strNominalTime);
135 // Setting expected start time
136 String strRelExpectedStart = getTagElement(eSla, "should-start");
137 if (strRelExpectedStart == null || strRelExpectedStart.length() == 0) {
138 throw new RuntimeException("should-start can't be empty");
139 }
140 int relExpectedStart = Integer.parseInt(strRelExpectedStart);
141 if (relExpectedStart < 0) {
142 sla.setExpectedStart(null);
143 }
144 else {
145 Date expectedStart = new Date(nominalTime.getTime()
146 + relExpectedStart * 60 * 1000);
147 sla.setExpectedStart(expectedStart);
148 // sla.setExpectedStart(nominalTime);
149 }
150
151 // Setting expected end time
152 String strRelExpectedEnd = getTagElement(eSla, "should-end");
153 if (strRelExpectedEnd == null || strRelExpectedEnd.length() == 0) {
154 throw new RuntimeException("should-end can't be empty");
155 }
156 int relExpectedEnd = Integer.parseInt(strRelExpectedEnd);
157 if (relExpectedEnd < 0) {
158 sla.setExpectedEnd(null);
159 }
160 else {
161 Date expectedEnd = new Date(nominalTime.getTime() + relExpectedEnd
162 * 60 * 1000);
163 sla.setExpectedEnd(expectedEnd);
164 }
165
166 sla.setNotificationMsg(getTagElement(eSla, "notification-msg"));
167 sla.setAlertContact(getTagElement(eSla, "alert-contact"));
168 sla.setDevContact(getTagElement(eSla, "dev-contact"));
169 sla.setQaContact(getTagElement(eSla, "qa-contact"));
170 sla.setSeContact(getTagElement(eSla, "se-contact"));
171 sla.setAlertFrequency(getTagElement(eSla, "alert-frequency"));
172 sla.setAlertPercentage(getTagElement(eSla, "alert-percentage"));
173
174 sla.setUpstreamApps(getTagElement(eSla, "upstream-apps"));
175
176 // Oozie defined
177
178 sla.setSlaId(slaId);
179 sla.setAppType(appType);
180 sla.setUser(user);
181 sla.setGroupName(groupName);
182 sla.setJobStatus(Status.CREATED);
183 sla.setStatusTimestamp(new Date());
184
185 //SLAStore slaStore = (SLAStore) Services.get().get(StoreService.class)
186 // .getStore(SLAStore.class, store);
187 //slaStore.insertSLAEvent(sla);
188
189 JPAService jpaService = Services.get().get(JPAService.class);
190 if (jpaService != null) {
191 jpaService.execute(new SLAEventInsertJPAExecutor(sla));
192 }
193 else {
194 log.error(ErrorCode.E0610);
195 }
196 }
197
198 public static void writeSlaStatusEvent(String id,
199 Status status, Store store, SlaAppType appType) throws Exception {
200 SLAEventBean sla = new SLAEventBean();
201 sla.setSlaId(id);
202 sla.setJobStatus(status);
203 sla.setAppType(appType);
204 sla.setStatusTimestamp(new Date());
205 //System.out.println("Writing STATUS AAAAA " + id);
206 SLAStore slaStore = (SLAStore) Services.get().get(StoreService.class)
207 .getStore(SLAStore.class, store);
208 slaStore.insertSLAEvent(sla);
209 }
210
211 public static void writeSlaStatusEvent(String id, Status status, SlaAppType appType, XLog log) throws Exception {
212 SLAEventBean sla = new SLAEventBean();
213 sla.setSlaId(id);
214 sla.setJobStatus(status);
215 sla.setAppType(appType);
216 sla.setStatusTimestamp(new Date());
217 // System.out.println("Writing STATUS AAAAA " + id);
218 //SLAStore slaStore = (SLAStore) Services.get().get(StoreService.class).getStore(SLAStore.class, store);
219 //slaStore.insertSLAEvent(sla);
220
221 JPAService jpaService = Services.get().get(JPAService.class);
222 if (jpaService != null) {
223 jpaService.execute(new SLAEventInsertJPAExecutor(sla));
224 }
225 else {
226 log.error(ErrorCode.E0610);
227 }
228 }
229
230 public static void writeStausEvent(String slaXml, String id, Store store,
231 Status stat, SlaAppType appType) throws CommandException {
232 if (slaXml == null || slaXml.length() == 0) {
233 return;
234 }
235 try {
236 writeSlaStatusEvent(id, stat, store, appType);
237 }
238 catch (Exception e) {
239 throw new CommandException(ErrorCode.E1007, " id " + id, e);
240 }
241 }
242
243 public static void writeStausEvent(String slaXml, String id, Status stat, SlaAppType appType, XLog log)
244 throws CommandException {
245 if (slaXml == null || slaXml.length() == 0) {
246 return;
247 }
248 try {
249 writeSlaStatusEvent(id, stat, appType, log);
250 }
251 catch (Exception e) {
252 throw new CommandException(ErrorCode.E1007, " id " + id, e);
253 }
254 }
255
256 public static String getClientId() {
257 Services services = Services.get();
258 if (services == null) {
259 throw new RuntimeException("Services is not initialized");
260 }
261 String clientId = services.getConf().get(CLIENT_ID_TAG,
262 "oozie-default-instance"); // TODO" remove default
263 if (clientId == null) {
264 //System.out.println("CONF "
265 // + XmlUtils.prettyPrint(services.getConf()));
266 throw new RuntimeException(
267 "No SLA_CLIENT_ID defined in oozie-site.xml with property name "
268 + CLIENT_ID_TAG);
269 }
270 return clientId;
271 }
272
273 private static String getTagElement(Element elem, String tagName) {
274 if (elem != null
275 && elem.getChild(tagName, elem.getNamespace("sla")) != null) {
276 return elem.getChild(tagName, elem.getNamespace("sla")).getText()
277 .trim();
278 }
279 else {
280 return null;
281 }
282 }
283
284 }