Repository: ambari
Updated Branches:
  refs/heads/branch-2.5 7cf5ee4b1 -> ac01c2773


AMBARI-21248 Exception needs to be handled properly for mail alert (dsen)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/ac01c277
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/ac01c277
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/ac01c277

Branch: refs/heads/branch-2.5
Commit: ac01c27737e09c10e7cb3c7e4d59e7e5e23be6e7
Parents: 7cf5ee4
Author: Dmytro Sen <[email protected]>
Authored: Mon Jun 19 19:24:58 2017 +0300
Committer: Dmytro Sen <[email protected]>
Committed: Mon Jun 19 19:24:58 2017 +0300

----------------------------------------------------------------------
 .../services/AlertNoticeDispatchService.java    | 87 ++++++++++----------
 .../AlertNoticeDispatchServiceTest.java         | 23 ++++++
 2 files changed, 68 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/ac01c277/ambari-server/src/main/java/org/apache/ambari/server/state/services/AlertNoticeDispatchService.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/state/services/AlertNoticeDispatchService.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/state/services/AlertNoticeDispatchService.java
index 174f31f..d4ffd4a 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/state/services/AlertNoticeDispatchService.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/state/services/AlertNoticeDispatchService.java
@@ -306,65 +306,68 @@ public class AlertNoticeDispatchService extends 
AbstractScheduledService {
       if (null == notices || notices.size() == 0) {
         continue;
       }
+      try {
+        String targetType = target.getNotificationType();
+        NotificationDispatcher dispatcher = 
m_dispatchFactory.getDispatcher(targetType);
 
-      String targetType = target.getNotificationType();
-      NotificationDispatcher dispatcher = 
m_dispatchFactory.getDispatcher(targetType);
-
-      // create a single digest notification if supported
-      if (dispatcher.isDigestSupported()) {
-        AlertNotification notification = buildNotificationFromTarget(target);
-        notification.CallbackIds = new ArrayList<String>(notices.size());
-        List<AlertHistoryEntity> histories = new ArrayList<AlertHistoryEntity>(
-            notices.size());
-
-        // add callback IDs so that the notices can be marked as DELIVERED or
-        // FAILED, and create a list of just the alert histories
-        for (AlertNoticeEntity notice : notices) {
-          AlertHistoryEntity history = notice.getAlertHistory();
-          histories.add(history);
+        // create a single digest notification if supported
+        if (dispatcher.isDigestSupported()) {
+          AlertNotification notification = buildNotificationFromTarget(target);
+          notification.CallbackIds = new ArrayList<String>(notices.size());
+          List<AlertHistoryEntity> histories = new 
ArrayList<AlertHistoryEntity>(
+                  notices.size());
 
-          notification.CallbackIds.add(notice.getUuid());
-        }
+          // add callback IDs so that the notices can be marked as DELIVERED or
+          // FAILED, and create a list of just the alert histories
+          for (AlertNoticeEntity notice : notices) {
+            AlertHistoryEntity history = notice.getAlertHistory();
+            histories.add(history);
 
-        // populate the subject and body fields; if there is a problem
-        // generating the content, then mark the notices as FAILED
-        try {
-          renderDigestNotificationContent(dispatcher, notification, histories, 
target);
-
-          // dispatch
-          DispatchRunnable runnable = new DispatchRunnable(dispatcher, 
notification);
-          m_executor.execute(runnable);
-        } catch (Exception exception) {
-          LOG.error("Unable to create notification for alerts", exception);
-
-          // there was a problem generating content for the target; mark all
-          // notices as FAILED and skip this target
-          // mark these as failed
-          notification.Callback.onFailure(notification.CallbackIds);
-        }
-      } else {
-        // the dispatcher does not support digest, each notice must have a 1:1
-        // notification created for it
-        for (AlertNoticeEntity notice : notices) {
-          AlertNotification notification = buildNotificationFromTarget(target);
-          AlertHistoryEntity history = notice.getAlertHistory();
-          notification.CallbackIds = 
Collections.singletonList(notice.getUuid());
+            notification.CallbackIds.add(notice.getUuid());
+          }
 
           // populate the subject and body fields; if there is a problem
           // generating the content, then mark the notices as FAILED
           try {
-            renderNotificationContent(dispatcher, notification, history, 
target);
+            renderDigestNotificationContent(dispatcher, notification, 
histories, target);
 
             // dispatch
             DispatchRunnable runnable = new DispatchRunnable(dispatcher, 
notification);
             m_executor.execute(runnable);
           } catch (Exception exception) {
-            LOG.error("Unable to create notification for alert", exception);
+            LOG.error("Unable to create notification for alerts", exception);
 
+            // there was a problem generating content for the target; mark all
+            // notices as FAILED and skip this target
             // mark these as failed
             notification.Callback.onFailure(notification.CallbackIds);
           }
+        } else {
+          // the dispatcher does not support digest, each notice must have a 
1:1
+          // notification created for it
+          for (AlertNoticeEntity notice : notices) {
+            AlertNotification notification = 
buildNotificationFromTarget(target);
+            AlertHistoryEntity history = notice.getAlertHistory();
+            notification.CallbackIds = 
Collections.singletonList(notice.getUuid());
+
+            // populate the subject and body fields; if there is a problem
+            // generating the content, then mark the notices as FAILED
+            try {
+              renderNotificationContent(dispatcher, notification, history, 
target);
+
+              // dispatch
+              DispatchRunnable runnable = new DispatchRunnable(dispatcher, 
notification);
+              m_executor.execute(runnable);
+            } catch (Exception exception) {
+              LOG.error("Unable to create notification for alert", exception);
+
+              // mark these as failed
+              notification.Callback.onFailure(notification.CallbackIds);
+            }
+          }
         }
+      } catch (Exception e) {
+        LOG.error("Caught exception during Alert Notice dispatching.", e);
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/ac01c277/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java
index 8423eaf..ee29762 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java
@@ -252,6 +252,29 @@ public class AlertNoticeDispatchServiceTest extends 
AlertNoticeDispatchService {
     assertTrue(notification.Body.contains(ALERT_UNIQUE_TEXT));
   }
 
+  @Test
+  public void testExceptionHandling() throws Exception {
+    List<AlertNoticeEntity> notices = getSingleMockNotice("EMAIL");
+    AlertNoticeEntity notice = notices.get(0);
+
+    EasyMock.expect(m_dao.findPendingNotices()).andReturn(notices).once();
+    
EasyMock.expect(m_dispatchFactory.getDispatcher("EMAIL")).andReturn(null).once();
+    EasyMock.expect(m_dao.merge(notice)).andReturn(notice).atLeastOnce();
+
+    EasyMock.replay(m_dao, m_dispatchFactory);
+
+    // "startup" the service so that its initialization is done
+    AlertNoticeDispatchService service = 
m_injector.getInstance(AlertNoticeDispatchService.class);
+    service.startUp();
+
+    // service trigger with mock executor that blocks
+    service.setExecutor(new MockExecutor());
+    // no exceptions should be thrown
+    service.runOneIteration();
+
+    EasyMock.verify(m_dao, m_dispatchFactory);
+  }
+
   /**
    * Tests a digest dispatch for SNMP.
    *

Reply via email to