public void testOnSLADurationMetEvent() throws Exception {
    JMSSLAEventListener slaListener = new JMSSLAEventListener();
    slaListener.init(conf);
    SLACalcStatus durationMet = new SLACalcStatus(new SLARegistrationBean());
    SLARegistrationBean durationMetBean = durationMet.getSLARegistrationBean();

    Date expectedStartDate = DateUtils.parseDateUTC("2013-01-01T00:00Z");
    Date actualStartDate = DateUtils.parseDateUTC("2013-01-01T01:00Z");
    Date expectedEndDate = DateUtils.parseDateUTC("2013-01-01T12:00Z");
    Date actualEndDate = DateUtils.parseDateUTC("2013-01-01T14:00Z");
    long expectedDuration = (expectedEndDate.getTime() - actualStartDate.getTime()) / (1000 * 60);
    durationMet.setId("0000000-000000000000001-oozie-wrkf-C@1");
    durationMetBean.setParentId("0000000-000000000000001-oozie-wrkf-C");
    durationMetBean.setAppName("Test-SLA-Duration-Met");
    durationMet.setEventStatus(EventStatus.DURATION_MET);
    durationMet.setSLAStatus(SLAStatus.MET);
    durationMetBean.setAppType(AppType.COORDINATOR_ACTION);
    durationMetBean.setUser("dummyuser");
    durationMetBean.setNotificationMsg("notification of duration met");
    durationMetBean.setExpectedStart(expectedStartDate);
    durationMet.setActualStart(actualStartDate);
    durationMetBean.setExpectedEnd(expectedEndDate);
    durationMet.setActualEnd(actualEndDate);
    durationMetBean.setExpectedDuration(expectedDuration);
    long actualDuration = actualEndDate.getTime() - actualStartDate.getTime();
    durationMet.setActualDuration(actualDuration);

    ConnectionContext jmsContext = getConnectionContext();

    Session session = jmsContext.createSession(Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer =
        jmsContext.createConsumer(session, slaListener.getTopic(durationMet));
    slaListener.onDurationMet(durationMet);
    TextMessage message = (TextMessage) consumer.receive(5000);
    SLAMessage durationMissMsg = (SLAMessage) JMSMessagingUtils.getEventMessage(message);
    // check msg header
    assertEquals(EventStatus.DURATION_MET, durationMissMsg.getEventStatus());
    assertEquals(SLAStatus.MET, durationMissMsg.getSLAStatus());
    assertEquals(AppType.COORDINATOR_ACTION, durationMissMsg.getAppType());
    assertEquals(MessageType.SLA, durationMissMsg.getMessageType());
    assertEquals("Test-SLA-Duration-Met", durationMissMsg.getAppName());
    assertEquals("dummyuser", durationMissMsg.getUser());
    // check msg body
    assertEquals("0000000-000000000000001-oozie-wrkf-C@1", durationMissMsg.getId());
    assertEquals("0000000-000000000000001-oozie-wrkf-C", durationMissMsg.getParentId());
    assertEquals(expectedStartDate, durationMissMsg.getExpectedStartTime());
    assertEquals(actualStartDate, durationMissMsg.getActualStartTime());
    assertEquals(expectedEndDate, durationMissMsg.getExpectedEndTime());
    assertEquals(actualEndDate, durationMissMsg.getActualEndTime());
    assertEquals(expectedDuration, durationMissMsg.getExpectedDuration());
    assertEquals(actualDuration, durationMissMsg.getActualDuration());
    assertEquals("notification of duration met", durationMissMsg.getNotificationMessage());
  }
  @Test
  public void testOnSLAStartMissEvent() throws Exception {
    JMSSLAEventListener slaListener = new JMSSLAEventListener();
    slaListener.init(conf);
    SLACalcStatus startMiss = new SLACalcStatus(new SLARegistrationBean());
    SLARegistrationBean startMissBean = startMiss.getSLARegistrationBean();
    Date startDate = DateUtils.parseDateUTC("2013-01-01T00:00Z");
    startMiss.setId("0000000-000000000000001-oozie-wrkf-C@1");
    startMissBean.setParentId("0000000-000000000000001-oozie-wrkf-C");
    startMissBean.setAppName("Test-SLA-Start-Miss");
    startMissBean.setUser("dummyuser");
    startMissBean.setExpectedStart(startDate);
    startMissBean.setNotificationMsg("notification of start miss");
    startMissBean.setJobData("random job data");
    startMiss.setEventStatus(EventStatus.START_MISS);
    startMiss.setSLAStatus(SLAStatus.NOT_STARTED);
    startMissBean.setAppType(AppType.COORDINATOR_ACTION);
    startMiss.setActualStart(DateUtils.parseDateUTC("2013-01-01T01:00Z"));

    ConnectionContext jmsContext = getConnectionContext();

    Session session = jmsContext.createSession(Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = jmsContext.createConsumer(session, slaListener.getTopic(startMiss));
    slaListener.onStartMiss(startMiss);
    TextMessage message = (TextMessage) consumer.receive(5000);
    SLAMessage slaStartMissMsg = (SLAMessage) JMSMessagingUtils.getEventMessage(message);
    // check msg header
    assertEquals(EventStatus.START_MISS, slaStartMissMsg.getEventStatus());
    assertEquals(SLAStatus.NOT_STARTED, slaStartMissMsg.getSLAStatus());
    assertEquals(AppType.COORDINATOR_ACTION, slaStartMissMsg.getAppType());
    assertEquals(MessageType.SLA, slaStartMissMsg.getMessageType());
    assertEquals("Test-SLA-Start-Miss", slaStartMissMsg.getAppName());
    assertEquals("dummyuser", slaStartMissMsg.getUser());
    // check msg body
    assertEquals("0000000-000000000000001-oozie-wrkf-C@1", slaStartMissMsg.getId());
    assertEquals("0000000-000000000000001-oozie-wrkf-C", slaStartMissMsg.getParentId());
    assertEquals(startDate, slaStartMissMsg.getExpectedStartTime());
    assertEquals("notification of start miss", slaStartMissMsg.getNotificationMessage());
  }
  public void testOnSLAEndMissEvent() throws Exception {
    JMSSLAEventListener slaListener = new JMSSLAEventListener();
    slaListener.init(conf);
    SLACalcStatus endMiss = new SLACalcStatus(new SLARegistrationBean());
    SLARegistrationBean endMissBean = endMiss.getSLARegistrationBean();
    Date expectedEndDate = DateUtils.parseDateUTC("2013-01-01T00:00Z");
    Date actualEndDate = DateUtils.parseDateUTC("2013-01-01T01:00Z");
    endMiss.setId("0000000-000000000000001-oozie-wrkf-C@1");
    endMissBean.setParentId("0000000-000000000000001-oozie-wrkf-C");
    endMissBean.setAppName("Test-SLA-End-Miss");
    endMiss.setEventStatus(EventStatus.END_MISS);
    endMiss.setSLAStatus(SLAStatus.IN_PROCESS);
    endMissBean.setAppType(AppType.COORDINATOR_ACTION);
    endMissBean.setUser("dummyuser");
    endMissBean.setNotificationMsg("notification of end miss");
    endMissBean.setExpectedEnd(expectedEndDate);
    endMiss.setActualEnd(actualEndDate);

    ConnectionContext jmsContext = getConnectionContext();

    Session session = jmsContext.createSession(Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = jmsContext.createConsumer(session, slaListener.getTopic(endMiss));
    slaListener.onEndMiss(endMiss);
    TextMessage message = (TextMessage) consumer.receive(5000);
    SLAMessage slaEndMissMsg = (SLAMessage) JMSMessagingUtils.getEventMessage(message);
    // check msg header
    assertEquals(EventStatus.END_MISS, slaEndMissMsg.getEventStatus());
    assertEquals(SLAStatus.IN_PROCESS, slaEndMissMsg.getSLAStatus());
    assertEquals(AppType.COORDINATOR_ACTION, slaEndMissMsg.getAppType());
    assertEquals(MessageType.SLA, slaEndMissMsg.getMessageType());
    assertEquals("Test-SLA-End-Miss", slaEndMissMsg.getAppName());
    assertEquals("dummyuser", slaEndMissMsg.getUser());
    // check msg body
    assertEquals("0000000-000000000000001-oozie-wrkf-C@1", slaEndMissMsg.getId());
    assertEquals("0000000-000000000000001-oozie-wrkf-C", slaEndMissMsg.getParentId());
    assertEquals(expectedEndDate, slaEndMissMsg.getExpectedEndTime());
    assertEquals(actualEndDate, slaEndMissMsg.getActualEndTime());
    assertEquals("notification of end miss", slaEndMissMsg.getNotificationMessage());
  }
Example #4
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;
  }