Repository: ambari Updated Branches: refs/heads/trunk 927d5c834 -> 958776415
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/95877641 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/95877641 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/95877641 Branch: refs/heads/trunk Commit: 95877641588a50c20d8b97e8ce749addc37fc0a2 Parents: 927d5c8 Author: Dmytro Sen <[email protected]> Authored: Mon Jun 19 19:19:14 2017 +0300 Committer: Dmytro Sen <[email protected]> Committed: Mon Jun 19 19:19:14 2017 +0300 ---------------------------------------------------------------------- .../services/AlertNoticeDispatchService.java | 86 ++++++++++---------- .../AlertNoticeDispatchServiceTest.java | 23 ++++++ 2 files changed, 68 insertions(+), 41 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/95877641/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 2972f51..73a661d 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,69 @@ 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<>(notices.size()); - List<AlertHistoryEntity> histories = new ArrayList<>( - notices.size()); + // create a single digest notification if supported + if (dispatcher.isDigestSupported()) { + AlertNotification notification = buildNotificationFromTarget(target); + notification.CallbackIds = new ArrayList<>(notices.size()); + List<AlertHistoryEntity> histories = new ArrayList<>( + 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); + // 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); - notification.CallbackIds.add(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 { - 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()); // 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/95877641/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 42ee366..61b0c31 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. *
