Esempio n. 1
0
  public static SLARegistrationBean createSlaRegistrationEvent(
      Element eSla,
      String jobId,
      String parentId,
      AppType appType,
      String user,
      String appName,
      XLog log,
      boolean rerun)
      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);
    if (strNominalTime == null || strNominalTime.length() == 0) {
      throw new CommandException(ErrorCode.E1101, NOMINAL_TIME);
    }
    Date nominalTime;
    try {
      nominalTime = DateUtils.parseDateOozieTZ(strNominalTime);
      sla.setNominalTime(nominalTime);
    } catch (ParseException pex) {
      throw new CommandException(ErrorCode.E0302, strNominalTime, pex);
    }

    // Setting expected start time
    String strExpectedStart = getTagElement(eSla, SHOULD_START);
    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);
      }
    }

    // Setting expected end time
    String strExpectedEnd = getTagElement(eSla, SHOULD_END);
    if (strExpectedEnd == null || strExpectedEnd.length() == 0) {
      throw new CommandException(ErrorCode.E1101, SHOULD_END);
    }
    float expectedEnd = Float.parseFloat(strExpectedEnd);
    if (expectedEnd < 0) {
      throw new CommandException(ErrorCode.E0302, strExpectedEnd, "for SLA Expected end time");
    } else {
      Date expectedEndTime = new Date(nominalTime.getTime() + (long) (expectedEnd * 60 * 1000));
      sla.setExpectedEnd(expectedEndTime);
    }

    // Setting expected duration in milliseconds
    String expectedDurationStr = getTagElement(eSla, MAX_DURATION);
    if (expectedDurationStr != null && expectedDurationStr.length() > 0) {
      float expectedDuration = Float.parseFloat(expectedDurationStr);
      if (expectedDuration > 0) {
        sla.setExpectedDuration((long) (expectedDuration * 60 * 1000));
      }
    } else if (sla.getExpectedStart() != null) {
      sla.setExpectedDuration(sla.getExpectedEnd().getTime() - sla.getExpectedStart().getTime());
    }

    // 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"));

    // 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;
  }