use of org.apache.oozie.XException in project oozie by apache.
the class V2SLAServlet method getSLASummaryList.
private JSONObject getSLASummaryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, CommandException {
String timeZoneId = request.getParameter(RestConstants.TIME_ZONE_PARAM) == null ? null : request.getParameter(RestConstants.TIME_ZONE_PARAM);
String filterString = request.getParameter(RestConstants.JOBS_FILTER_PARAM);
String maxResults = request.getParameter(RestConstants.LEN_PARAM);
// Default
int numMaxResults = 1000;
if (maxResults != null) {
numMaxResults = Integer.parseInt(maxResults);
}
if (filterString == null || filterString.equals("")) {
throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0305, RestConstants.JOBS_FILTER_PARAM);
}
try {
Map<String, List<String>> filterList = parseFilter(URLDecoder.decode(filterString, "UTF-8"), SLA_FILTER_NAMES);
SLASummaryFilter filter = new SLASummaryFilter();
if (!filterList.containsKey(OozieClient.FILTER_SLA_APPNAME) && !filterList.containsKey(OozieClient.FILTER_SLA_ID) && !filterList.containsKey(OozieClient.FILTER_SLA_PARENT_ID) && !filterList.containsKey(OozieClient.FILTER_BUNDLE) && !filterList.containsKey(OozieClient.FILTER_SLA_NOMINAL_START) && !filterList.containsKey(OozieClient.FILTER_SLA_NOMINAL_END)) {
StringBuffer st = new StringBuffer();
st.append("At least one of the filter parameters - ").append(OozieClient.FILTER_SLA_APPNAME).append(",").append(OozieClient.FILTER_SLA_ID).append(",").append(OozieClient.FILTER_SLA_PARENT_ID).append(",").append(OozieClient.FILTER_BUNDLE).append(",").append(OozieClient.FILTER_SLA_NOMINAL_START).append(" or ").append(OozieClient.FILTER_SLA_NOMINAL_END).append(" should be specified in the filter query parameter");
throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0305, st.toString());
}
if (filterList.containsKey(OozieClient.FILTER_SLA_ID)) {
filter.setJobId(filterList.get(OozieClient.FILTER_SLA_ID).get(0));
}
if (filterList.containsKey(OozieClient.FILTER_SLA_PARENT_ID)) {
filter.setParentId(filterList.get(OozieClient.FILTER_SLA_PARENT_ID).get(0));
}
if (filterList.containsKey(OozieClient.FILTER_BUNDLE)) {
String bundle = filterList.get(OozieClient.FILTER_BUNDLE).get(0);
if (isBundleId(bundle)) {
filter.setBundleId(bundle);
} else {
filter.setBundleName(bundle);
}
}
if (filterList.containsKey(OozieClient.FILTER_SLA_EVENT_STATUS)) {
filter.setEventStatus(filterList.get(OozieClient.FILTER_SLA_EVENT_STATUS).get(0));
}
if (filterList.containsKey(OozieClient.FILTER_SLA_STATUS)) {
filter.setSLAStatus(filterList.get(OozieClient.FILTER_SLA_STATUS).get(0));
}
if (filterList.containsKey(OozieClient.FILTER_SLA_APPNAME)) {
filter.setAppName(filterList.get(OozieClient.FILTER_SLA_APPNAME).get(0));
}
if (filterList.containsKey(OozieClient.FILTER_SLA_NOMINAL_START)) {
filter.setNominalStart(DateUtils.parseDateUTC(filterList.get(OozieClient.FILTER_SLA_NOMINAL_START).get(0)));
}
if (filterList.containsKey(OozieClient.FILTER_SLA_NOMINAL_END)) {
filter.setNominalEnd(DateUtils.parseDateUTC(filterList.get(OozieClient.FILTER_SLA_NOMINAL_END).get(0)));
}
JPAService jpaService = Services.get().get(JPAService.class);
List<SLASummaryBean> slaSummaryList = null;
if (jpaService != null) {
slaSummaryList = jpaService.execute(new SLASummaryGetForFilterJPAExecutor(filter, numMaxResults));
} else {
XLog.getLog(getClass()).error(ErrorCode.E0610);
}
List<String> jobIds = new ArrayList<String>();
for (SLASummaryBean summaryBean : slaSummaryList) {
jobIds.add(summaryBean.getId());
}
List<SLARegistrationBean> SLARegistrationList = SLARegistrationQueryExecutor.getInstance().getList(SLARegQuery.GET_SLA_CONFIGS, jobIds);
Map<String, Map<String, String>> jobIdSLAConfigMap = new HashMap<String, Map<String, String>>();
for (SLARegistrationBean registrationBean : SLARegistrationList) {
jobIdSLAConfigMap.put(registrationBean.getId(), registrationBean.getSLAConfigMap());
}
return SLASummaryBean.toJSONObject(slaSummaryList, jobIdSLAConfigMap, timeZoneId);
} catch (XException ex) {
throw new CommandException(ex);
} catch (UnsupportedEncodingException e) {
throw new XServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ErrorCode.E0307, "Unsupported Encoding", e);
} catch (ParseException e) {
throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0303, filterString, e);
}
}
use of org.apache.oozie.XException in project oozie by apache.
the class SLACalculatorMemory method addJobStatus.
/**
* Triggered after receiving Job status change event, update SLA status
* accordingly
*/
@Override
public boolean addJobStatus(String jobId, String jobStatus, JobEvent.EventStatus jobEventStatus, Date startTime, Date endTime) throws JPAExecutorException, ServiceException {
LOG.debug("Received addJobStatus request for job [{0}] jobStatus = [{1}], jobEventStatus = [{2}], startTime = [{3}], " + "endTime = [{4}] ", jobId, jobStatus, jobEventStatus, startTime, endTime);
SLACalcStatus slaCalc = slaMap.get(jobId);
boolean firstCheckAfterRetstart = checkAndUpdateSLACalcAfterRestart(slaCalc);
if (slaCalc == null) {
SLARegistrationBean slaRegBean = SLARegistrationQueryExecutor.getInstance().get(SLARegQuery.GET_SLA_REG_ALL, jobId);
if (slaRegBean != null) {
// filter out jobs picked by SLA job event listener
// but not actually configured for SLA
SLASummaryBean slaSummaryBean = SLASummaryQueryExecutor.getInstance().get(SLASummaryQuery.GET_SLA_SUMMARY, jobId);
slaCalc = new SLACalcStatus(slaSummaryBean, slaRegBean);
putAndIncrement(jobId, slaCalc);
}
} else {
SLASummaryBean summaryBean = ((SLASummaryQueryExecutor) SLASummaryQueryExecutor.getInstance()).get(SLASummaryQuery.GET_SLA_SUMMARY_EVENTPROCESSED_LAST_MODIFIED, jobId);
byte eventProc = summaryBean.getEventProcessed();
if (!slaCalc.getLastModifiedTime().equals(summaryBean.getLastModifiedTime())) {
// Update last modified time.
slaCalc.setLastModifiedTime(summaryBean.getLastModifiedTime());
reloadExpectedTimeAndConfig(slaCalc);
LOG.debug("Last modified time has changed for job " + jobId + " reloading config from DB");
}
slaCalc.setEventProcessed(eventProc);
}
if (slaCalc != null) {
try {
SLAXCommandFactory.getSLAEventXCommand(slaCalc, ConfigurationService.getLong(SLAService.CONF_SLA_CALC_LOCK_TIMEOUT, 20 * 1000)).call();
checkEventProc(slaCalc);
} catch (XException e) {
if (firstCheckAfterRetstart) {
slaCalc.setSLARegistrationBean(null);
}
LOG.error(e);
throw new ServiceException(e);
}
return true;
} else {
return false;
}
}
use of org.apache.oozie.XException in project oozie by apache.
the class CoordActionsInDateRange method getCoordActionsFromDateRange.
/**
* Get the coordinator actions for a given date range
* @param jobId the coordinator job id
* @param range the date range separated by '::'
* @param active to list only active (non-terminated) actions
* @return the list of Coordinator actions for the date range
* @throws XException if range is not well formatted or invalid
*/
public static List<CoordinatorActionBean> getCoordActionsFromDateRange(String jobId, String range, boolean active) throws XException {
String[] dateRange = range.split("::");
// This block checks for errors in the format of specifying date range
if (dateRange.length != 2) {
throw new XException(ErrorCode.E0308, "'" + range + "'. Date value expected on both sides of the scope resolution operator '::' to signify start and end of range");
}
Date start;
Date end;
try {
// Get the start and end dates for the range
start = DateUtils.parseDateOozieTZ(dateRange[0].trim());
end = DateUtils.parseDateOozieTZ(dateRange[1].trim());
} catch (ParseException dx) {
throw new XException(ErrorCode.E0308, "Error in parsing start or end date. " + dx);
}
if (start.after(end)) {
throw new XException(ErrorCode.E0308, "'" + range + "'. Start date '" + start + "' is older than end date: '" + end + "'");
}
List<CoordinatorActionBean> listOfActions = getActionsFromDateRange(jobId, start, end, active);
return listOfActions;
}
use of org.apache.oozie.XException in project oozie by apache.
the class TestCoordActionsInDateRange method testCoordActionsInDateRange.
/**
* This is unit test case for the 'getCoordActionsFromDates()' method. The method is supposed to retrieve the list of
* coordinator actions running between a range of start and end date. The following test case tests its accuracy and fails
* otherwise.
*/
public void testCoordActionsInDateRange() {
try {
int actionNum = 1;
CoordinatorJobBean job = addRecordToCoordJobTable(CoordinatorJob.Status.RUNNING, false, false);
CoordinatorActionBean actionId1 = addRecordToCoordActionTable(job.getId(), actionNum, CoordinatorAction.Status.SUCCEEDED, "coord-action-get.xml", 0);
Date nominalTime = actionId1.getNominalTime();
long nominalTimeMilliseconds = nominalTime.getTime();
long noOfMillisecondsinOneHour = 3600000;
String date1 = DateUtils.formatDateOozieTZ(new Date(nominalTimeMilliseconds - (noOfMillisecondsinOneHour / 2)));
String date2 = DateUtils.formatDateOozieTZ(new Date(nominalTimeMilliseconds + noOfMillisecondsinOneHour));
// Test a bad date format.
try {
String badDate = "bad" + date1;
CoordActionsInDateRange.getCoordActionIdsFromDates(job.getId().toString(), badDate + "::" + date2);
fail("Accepted badly formatted date: " + badDate);
} catch (XException e) {
// Pass
assertEquals(ErrorCode.E0308, e.getErrorCode());
}
// Test a bad scope.
try {
String badScope = date1 + "0xbad5c09e" + date2;
CoordActionsInDateRange.getCoordActionIdsFromDates(job.getId().toString(), badScope);
fail("Accepted bad range scope: " + badScope);
} catch (XException e) {
// Pass
assertEquals(ErrorCode.E0308, e.getErrorCode());
}
// Test inverted start and end dates.
try {
CoordActionsInDateRange.getCoordActionIdsFromDates(job.getId().toString(), date2 + "::" + date1);
fail("Accepted inverted dates: [Start::End] = " + date2 + "::" + date1);
} catch (XException e) {
// Pass
assertEquals(ErrorCode.E0308, e.getErrorCode());
}
// Test a lenient date
try {
// Like date2 but with the 50th day of the month
String lenientDate = date2.replaceAll("[^-]*T", "50T");
CoordActionsInDateRange.getCoordActionIdsFromDates(job.getId().toString(), date1 + "::" + lenientDate);
fail("Accepted lenient date: " + lenientDate);
} catch (XException e) {
// Pass
assertEquals(ErrorCode.E0308, e.getErrorCode());
}
// Testing for the number of coordinator actions in a date range that spans from half an hour prior to the nominal
// time to 1 hour after the nominal time
int noOfActions = CoordActionsInDateRange.getCoordActionIdsFromDates(job.getId().toString(), date1 + "::" + date2).size();
assertEquals(1, noOfActions);
// Testing for the number of coordinator actions in a date range that spans from half an hour after the nominal
// time to 1 hour after the nominal time
date1 = DateUtils.formatDateOozieTZ(new Date(nominalTimeMilliseconds + (noOfMillisecondsinOneHour / 2)));
noOfActions = CoordActionsInDateRange.getCoordActionIdsFromDates(job.getId().toString(), date1 + "::" + date2).size();
assertEquals(0, noOfActions);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of org.apache.oozie.XException in project oozie by apache.
the class CoordSuspendXCommand method suspendChildren.
@Override
public void suspendChildren() throws CommandException {
try {
// Get all running actions of a job to suspend them
List<CoordinatorActionBean> actionList = jpaService.execute(new CoordJobGetActionsRunningJPAExecutor(jobId));
for (CoordinatorActionBean action : actionList) {
// queue a SuspendXCommand
if (action.getExternalId() != null) {
queue(new SuspendXCommand(action.getExternalId()));
updateCoordAction(action);
LOG.debug("Suspend coord action = [{0}], new status = [{1}], pending = [{2}] and queue SuspendXCommand for [{3}]", action.getId(), action.getStatus(), action.getPending(), action.getExternalId());
} else {
updateCoordAction(action);
LOG.debug("Suspend coord action = [{0}], new status = [{1}], pending = [{2}] and external id is null", action.getId(), action.getStatus(), action.getPending());
}
}
LOG.debug("Suspended coordinator actions for the coordinator=[{0}]", jobId);
} catch (XException ex) {
exceptionOccured = true;
throw new CommandException(ex);
} finally {
if (exceptionOccured) {
coordJob.setStatus(CoordinatorJob.Status.FAILED);
coordJob.resetPending();
LOG.debug("Exception happened, fail coordinator job id = " + jobId + ", status = " + coordJob.getStatus());
updateList.add(new UpdateEntry<CoordJobQuery>(CoordJobQuery.UPDATE_COORD_JOB_STATUS_PENDING_TIME, coordJob));
}
}
}
Aggregations