You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by jo...@apache.org on 2017/05/12 14:36:43 UTC

[04/26] ambari git commit: AMBARI-20726 - Aggregate alert does not show status properly at Maintenance Mode change (Qin Liu via jonathanhurley)

AMBARI-20726 - Aggregate alert does not show status properly at Maintenance Mode change (Qin Liu via jonathanhurley)


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

Branch: refs/heads/branch-feature-AMBARI-12556
Commit: a60f1328bfcf3cfa8ae90ecc59bdc8b51e6daa65
Parents: c12c320
Author: Jonathan Hurley <jh...@hortonworks.com>
Authored: Wed May 10 17:04:33 2017 -0400
Committer: Jonathan Hurley <jh...@hortonworks.com>
Committed: Wed May 10 17:04:33 2017 -0400

----------------------------------------------------------------------
 .../alerts/AlertMaintenanceModeListener.java    | 60 +++++++++++++++++---
 .../apache/ambari/server/state/AlertState.java  |  4 ++
 .../AlertMaintenanceModeListenerTest.java       |  4 ++
 3 files changed, 60 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/a60f1328/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertMaintenanceModeListener.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertMaintenanceModeListener.java b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertMaintenanceModeListener.java
index 847a207..1197589 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertMaintenanceModeListener.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertMaintenanceModeListener.java
@@ -20,17 +20,22 @@ package org.apache.ambari.server.events.listeners.alerts;
 import java.util.List;
 
 import org.apache.ambari.server.EagerSingleton;
+import org.apache.ambari.server.events.AggregateAlertRecalculateEvent;
+import org.apache.ambari.server.events.AlertEvent;
 import org.apache.ambari.server.events.MaintenanceModeEvent;
+import org.apache.ambari.server.events.publishers.AlertEventPublisher;
 import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 import org.apache.ambari.server.orm.dao.AlertsDAO;
 import org.apache.ambari.server.orm.entities.AlertCurrentEntity;
 import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
 import org.apache.ambari.server.orm.entities.AlertHistoryEntity;
 import org.apache.ambari.server.orm.entities.AlertNoticeEntity;
+import org.apache.ambari.server.state.AlertState;
 import org.apache.ambari.server.state.Host;
 import org.apache.ambari.server.state.MaintenanceState;
 import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponentHost;
+import org.apache.ambari.server.state.alert.AggregateDefinitionMapping;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,12 +57,26 @@ public class AlertMaintenanceModeListener {
   private static Logger LOG = LoggerFactory.getLogger(AlertMaintenanceModeListener.class);
 
   /**
+   * Publishes {@link AlertEvent} instances.
+   */
+  @Inject
+  private AlertEventPublisher m_alertEventPublisher;
+
+  /**
    * Used for updating the MM of current alerts.
    */
   @Inject
   private AlertsDAO m_alertsDao = null;
 
   /**
+   * Used for quick lookups of aggregate alerts.
+   */
+  @Inject
+  private AggregateDefinitionMapping m_aggregateMapping;
+
+  private long clusterId = -1;
+
+  /**
    * Constructor.
    *
    * @param publisher
@@ -81,6 +100,7 @@ public class AlertMaintenanceModeListener {
   public void onEvent(MaintenanceModeEvent event) {
     LOG.debug("Received event {}", event);
 
+    boolean recalculateAggregateAlert = false;
     List<AlertCurrentEntity> currentAlerts = m_alertsDao.findCurrent();
 
     MaintenanceState newMaintenanceState = MaintenanceState.OFF;
@@ -104,14 +124,16 @@ public class AlertMaintenanceModeListener {
         if( null != host ){
           String hostName = host.getHostName();
           if( hostName.equals( alertHostName ) ){
-            updateMaintenanceState(currentAlert, newMaintenanceState);
+            if (updateMaintenanceStateAndRecalculateAggregateAlert(history, currentAlert, newMaintenanceState))
+              recalculateAggregateAlert = true;
             continue;
           }
         } else if( null != service ){
           // service level maintenance
           String serviceName = service.getName();
           if( serviceName.equals(alertServiceName)){
-            updateMaintenanceState(currentAlert, newMaintenanceState);
+            if (updateMaintenanceStateAndRecalculateAggregateAlert(history, currentAlert, newMaintenanceState))
+              recalculateAggregateAlert = true;
             continue;
           }
         } else if( null != serviceComponentHost ){
@@ -123,7 +145,8 @@ public class AlertMaintenanceModeListener {
           // match on all 3 for a service component
           if (hostName.equals(alertHostName) && serviceName.equals(alertServiceName)
               && componentName.equals(alertComponentName)) {
-            updateMaintenanceState(currentAlert, newMaintenanceState);
+            if (updateMaintenanceStateAndRecalculateAggregateAlert(history, currentAlert, newMaintenanceState))
+              recalculateAggregateAlert = true;
             continue;
           }
         }
@@ -133,35 +156,56 @@ public class AlertMaintenanceModeListener {
             definition.getDefinitionName(), alertHostName, exception);
       }
     }
+
+    if (recalculateAggregateAlert) {
+      // publish the event to recalculate aggregates
+      m_alertEventPublisher.publish(new AggregateAlertRecalculateEvent(clusterId));
+    }
   }
 
   /**
    * Updates the maintenance state of the specified alert if different than the
-   * supplied maintenance state.
+   * supplied maintenance state. And recalcualte aggregates if the maintenance state
+   * is changed and there is an aggregate alert for the specified alert if it is
+   * in CRITICAL or WARNING state.
    *
+   * @param historyAlert
+   *          the alert to check if having an aggregate alert associated with it.
    * @param currentAlert
    *          the alert to update (not {@code null}).
    * @param maintenanceState
    *          the maintenance state to change to, either
    *          {@link MaintenanceState#OFF} or {@link MaintenanceState#ON}.
    */
-  private void updateMaintenanceState(AlertCurrentEntity currentAlert,
-      MaintenanceState maintenanceState) {
+  private boolean updateMaintenanceStateAndRecalculateAggregateAlert (AlertHistoryEntity historyAlert,
+      AlertCurrentEntity currentAlert, MaintenanceState maintenanceState) {
 
     // alerts only care about OFF or ON
     if (maintenanceState != MaintenanceState.OFF && maintenanceState != MaintenanceState.ON) {
       LOG.warn("Unable to set invalid maintenance state of {} on the alert {}", maintenanceState,
           currentAlert.getAlertHistory().getAlertDefinition().getDefinitionName());
 
-      return;
+      return false;
     }
 
     MaintenanceState currentState = currentAlert.getMaintenanceState();
     if (currentState == maintenanceState) {
-      return;
+      return false;
     }
 
     currentAlert.setMaintenanceState(maintenanceState);
     m_alertsDao.merge(currentAlert);
+
+    AlertState alertState = historyAlert.getAlertState();
+
+    if (AlertState.RECALCULATE_AGGREGATE_ALERT_STATES.contains(alertState)){
+      clusterId = historyAlert.getClusterId();
+      String alertName = historyAlert.getAlertDefinition().getDefinitionName();
+
+      if (m_aggregateMapping.getAggregateDefinition(clusterId, alertName) != null){
+        return true;
+      }
+    }
+    return false;
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/a60f1328/ambari-server/src/main/java/org/apache/ambari/server/state/AlertState.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/AlertState.java b/ambari-server/src/main/java/org/apache/ambari/server/state/AlertState.java
index 8e04eb1..3834472 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/AlertState.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/AlertState.java
@@ -17,6 +17,8 @@
  */
 package org.apache.ambari.server.state;
 
+import java.util.EnumSet;
+
 /**
  * Represents the state of an alert.
  */
@@ -48,6 +50,8 @@ public enum AlertState {
    */
   SKIPPED(4);
 
+  public static EnumSet<AlertState> RECALCULATE_AGGREGATE_ALERT_STATES = EnumSet.of(CRITICAL, WARNING);
+
   private final int intValue;
 
   public int getIntValue() {

http://git-wip-us.apache.org/repos/asf/ambari/blob/a60f1328/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/AlertMaintenanceModeListenerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/AlertMaintenanceModeListenerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/AlertMaintenanceModeListenerTest.java
index bdc662a..f1d774b 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/AlertMaintenanceModeListenerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/AlertMaintenanceModeListenerTest.java
@@ -32,6 +32,7 @@ import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
 import org.apache.ambari.server.orm.dao.AlertsDAO;
 import org.apache.ambari.server.orm.entities.AlertCurrentEntity;
 import org.apache.ambari.server.orm.entities.AlertHistoryEntity;
+import org.apache.ambari.server.state.AlertState;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Host;
@@ -185,10 +186,12 @@ public class AlertMaintenanceModeListenerTest {
     EasyMock.expect(hostHistory.getHostName()).andReturn(HOSTNAME).atLeastOnce();
     EasyMock.expect(hostHistory.getServiceName()).andReturn(null).atLeastOnce();
     EasyMock.expect(hostHistory.getComponentName()).andReturn(null).atLeastOnce();
+    EasyMock.expect(hostHistory.getAlertState()).andReturn(AlertState.OK).atLeastOnce();
 
     EasyMock.expect(serviceHistory.getHostName()).andReturn(null).atLeastOnce();
     EasyMock.expect(serviceHistory.getServiceName()).andReturn(SERVICE).atLeastOnce();
     EasyMock.expect(serviceHistory.getComponentName()).andReturn(null).atLeastOnce();
+    EasyMock.expect(serviceHistory.getAlertState()).andReturn(AlertState.OK).atLeastOnce();
 
     if (testType.equals("SCH")) {
       EasyMock.expect(componentHistory.getHostName()).andReturn(HOSTNAME).atLeastOnce();
@@ -199,6 +202,7 @@ public class AlertMaintenanceModeListenerTest {
       EasyMock.expect(componentHistory.getServiceName()).andReturn(null).atLeastOnce();
       EasyMock.expect(componentHistory.getComponentName()).andReturn(COMPONENT).atLeastOnce();
     }
+    EasyMock.expect(componentHistory.getAlertState()).andReturn(AlertState.OK).atLeastOnce();
 
     List<AlertCurrentEntity> currentAlerts = new ArrayList<>();
     currentAlerts.add(hostAlert);