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