Search in sources :

Example 46 with CommandException

use of org.apache.oozie.command.CommandException 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);
    }
}
Also used : SLARegistrationBean(org.apache.oozie.sla.SLARegistrationBean) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CommandException(org.apache.oozie.command.CommandException) SLASummaryFilter(org.apache.oozie.executor.jpa.sla.SLASummaryGetForFilterJPAExecutor.SLASummaryFilter) XException(org.apache.oozie.XException) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(java.text.ParseException) JPAService(org.apache.oozie.service.JPAService) SLASummaryGetForFilterJPAExecutor(org.apache.oozie.executor.jpa.sla.SLASummaryGetForFilterJPAExecutor) HashMap(java.util.HashMap) Map(java.util.Map) SLASummaryBean(org.apache.oozie.sla.SLASummaryBean)

Example 47 with CommandException

use of org.apache.oozie.command.CommandException in project oozie by apache.

the class SLAOperations method setExpectedStart.

public static void setExpectedStart(String strExpectedStart, Date nominalTime, SLARegistrationBean sla) throws CommandException {
    if (strExpectedStart != null) {
        float expectedStart = Float.parseFloat(strExpectedStart);
        if (expectedStart < 0) {
            throw new CommandException(ErrorCode.E0302, strExpectedStart, "for SLA Expected start time");
        } else {
            Date expectedStartTime = new Date(nominalTime.getTime() + (long) (expectedStart * 60 * 1000));
            sla.setExpectedStart(expectedStartTime);
            LOG.debug("Setting expected start to " + expectedStartTime + " for job " + sla.getId());
        }
    }
}
Also used : CommandException(org.apache.oozie.command.CommandException) Date(java.util.Date)

Example 48 with CommandException

use of org.apache.oozie.command.CommandException in project oozie by apache.

the class SLAOperations method setNominalTime.

public static Date setNominalTime(String strNominalTime, SLARegistrationBean sla) throws CommandException {
    if (strNominalTime == null || strNominalTime.length() == 0) {
        return sla.getNominalTime();
    }
    Date nominalTime;
    try {
        nominalTime = DateUtils.parseDateOozieTZ(strNominalTime);
        sla.setNominalTime(nominalTime);
    } catch (ParseException pex) {
        throw new CommandException(ErrorCode.E0302, strNominalTime, pex);
    }
    return nominalTime;
}
Also used : ParseException(java.text.ParseException) CommandException(org.apache.oozie.command.CommandException) Date(java.util.Date)

Example 49 with CommandException

use of org.apache.oozie.command.CommandException in project oozie by apache.

the class SLAOperations method createSlaRegistrationEvent.

public static SLARegistrationBean createSlaRegistrationEvent(Element eSla, String jobId, String parentId, AppType appType, String user, String appName, XLog log, boolean rerun, boolean disableAlert) throws CommandException {
    if (eSla == null || !SLAService.isEnabled()) {
        log.debug("Not registering SLA for job [{0}]. Sla-Xml null OR SLAService not enabled", jobId);
        return null;
    }
    SLARegistrationBean sla = new SLARegistrationBean();
    // Setting nominal time
    String strNominalTime = getTagElement(eSla, NOMINAL_TIME);
    Date nominalTime = setNominalTime(strNominalTime, sla);
    // Setting expected start time
    String strExpectedStart = getTagElement(eSla, SHOULD_START);
    setExpectedStart(strExpectedStart, nominalTime, sla);
    // Setting expected end time
    String strExpectedEnd = getTagElement(eSla, SHOULD_END);
    setExpectedEnd(strExpectedEnd, nominalTime, sla);
    // Setting expected duration in milliseconds
    String expectedDurationStr = getTagElement(eSla, MAX_DURATION);
    setExpectedDuration(expectedDurationStr, sla);
    // Parse desired alert-types i.e. start-miss, end-miss, start-met etc..
    String alertEvents = getTagElement(eSla, ALERT_EVENTS);
    if (alertEvents != null) {
        String[] events = alertEvents.split(",");
        StringBuilder alertsStr = new StringBuilder();
        for (int i = 0; i < events.length; i++) {
            String event = events[i].trim().toUpperCase();
            try {
                EventStatus.valueOf(event);
            } catch (IllegalArgumentException iae) {
                XLog.getLog(SLAService.class).warn("Invalid value: [" + event + "]" + " for SLA Alert-event. Should be one of " + EventStatus.values() + ". Setting it to default [" + EventStatus.END_MISS.name() + "]");
                event = EventStatus.END_MISS.name();
            }
            alertsStr.append(event).append(",");
        }
        sla.setAlertEvents(alertsStr.toString().substring(0, alertsStr.lastIndexOf(",")));
    }
    // Other sla config
    sla.setNotificationMsg(getTagElement(eSla, "notification-msg"));
    sla.setAlertContact(getTagElement(eSla, "alert-contact"));
    sla.setUpstreamApps(getTagElement(eSla, "upstream-apps"));
    // disable Alert flag in slaConfig
    if (disableAlert) {
        sla.addToSLAConfigMap(OozieClient.SLA_DISABLE_ALERT, Boolean.toString(disableAlert));
    }
    // Oozie defined
    sla.setId(jobId);
    sla.setAppType(appType);
    sla.setAppName(appName);
    sla.setUser(user);
    sla.setParentId(parentId);
    SLAService slaService = Services.get().get(SLAService.class);
    try {
        if (!rerun) {
            slaService.addRegistrationEvent(sla);
        } else {
            slaService.updateRegistrationEvent(sla);
        }
    } catch (ServiceException e) {
        throw new CommandException(ErrorCode.E1007, " id " + jobId, e.getMessage(), e);
    }
    log.debug("Job [{0}] reg for SLA. Size of Sla Xml = [{1}]", jobId, XmlUtils.prettyPrint(eSla).toString().length());
    return sla;
}
Also used : SLAService(org.apache.oozie.sla.service.SLAService) ServiceException(org.apache.oozie.service.ServiceException) CommandException(org.apache.oozie.command.CommandException) Date(java.util.Date)

Example 50 with CommandException

use of org.apache.oozie.command.CommandException in project oozie by apache.

the class JobUtils method parseChangeValue.

/**
 * This Function will parse the value of the changed values in key value manner. the change value would be
 * key1=value1;key2=value2
 *
 * @param changeValue change value.
 * @return This returns the hash with hash&lt;[key1,value1],[key2,value2]&gt;
 * @throws CommandException thrown if changeValue cannot be parsed properly.
 */
public static Map<String, String> parseChangeValue(String changeValue) throws CommandException {
    if (changeValue == null || changeValue.trim().equalsIgnoreCase("")) {
        throw new CommandException(ErrorCode.E1015, "change value can not be empty string or null");
    }
    Map<String, String> map = new HashMap<String, String>();
    String[] tokens = changeValue.split(";");
    for (String token : tokens) {
        if (!token.contains("=")) {
            throw new CommandException(ErrorCode.E1015, changeValue, "change value must be name=value pair or name=(empty string)");
        }
        String[] pair = token.split("=");
        String key = pair[0];
        if (map.containsKey(key)) {
            throw new CommandException(ErrorCode.E1015, changeValue, "can not specify repeated change values on " + key);
        }
        if (pair.length == 2) {
            map.put(key, pair[1]);
        } else if (pair.length == 1) {
            map.put(key, "");
        } else {
            throw new CommandException(ErrorCode.E1015, changeValue, "elements on " + key + " must be name=value pair or name=(empty string)");
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) CommandException(org.apache.oozie.command.CommandException)

Aggregations

CommandException (org.apache.oozie.command.CommandException)225 JPAExecutorException (org.apache.oozie.executor.jpa.JPAExecutorException)85 XConfiguration (org.apache.oozie.util.XConfiguration)62 Date (java.util.Date)59 IOException (java.io.IOException)57 Configuration (org.apache.hadoop.conf.Configuration)56 JPAService (org.apache.oozie.service.JPAService)56 XException (org.apache.oozie.XException)42 PreconditionException (org.apache.oozie.command.PreconditionException)35 ArrayList (java.util.ArrayList)26 StringReader (java.io.StringReader)25 CoordinatorJobBean (org.apache.oozie.CoordinatorJobBean)24 List (java.util.List)23 CoordinatorActionBean (org.apache.oozie.CoordinatorActionBean)23 Element (org.jdom.Element)23 JDOMException (org.jdom.JDOMException)20 File (java.io.File)16 HadoopAccessorException (org.apache.oozie.service.HadoopAccessorException)16 WorkflowException (org.apache.oozie.workflow.WorkflowException)16 URISyntaxException (java.net.URISyntaxException)14