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 2014/08/20 17:30:44 UTC

[43/50] [abbrv] git commit: AMBARI-6860 - Alerts: Add Ability to Invalidate Definition Hash For An Agent (jonathanhurley)

AMBARI-6860 - Alerts: Add Ability to Invalidate Definition Hash For An Agent (jonathanhurley)


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

Branch: refs/heads/branch-alerts-dev
Commit: 9b6c02f6e2f7fa6a34457a4ebb39e20b38630e2f
Parents: 93e61c0
Author: Jonathan Hurley <jh...@hortonworks.com>
Authored: Thu Aug 14 09:22:49 2014 -0400
Committer: Jonathan Hurley <jh...@hortonworks.com>
Committed: Wed Aug 20 10:46:38 2014 -0400

----------------------------------------------------------------------
 .../ambari/server/controller/AmbariServer.java  |   7 +-
 .../AlertDefinitionResourceProvider.java        |  20 +--
 .../server/state/alert/AlertDefinitionHash.java | 122 +++++++++++++++++++
 .../src/main/resources/properties.json          |   3 +-
 .../AlertDefinitionResourceProviderTest.java    |  54 ++++++--
 .../state/alerts/AlertDefinitionHashTest.java   | 112 ++++++++++++++---
 6 files changed, 280 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/9b6c02f6/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
index 7b93836..e9b0c9e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
@@ -81,17 +81,18 @@ import org.apache.ambari.server.scheduler.ExecutionScheduleManager;
 import org.apache.ambari.server.security.CertificateManager;
 import org.apache.ambari.server.security.SecurityFilter;
 import org.apache.ambari.server.security.SecurityHelper;
+import org.apache.ambari.server.security.authorization.AmbariAuthorizationFilter;
 import org.apache.ambari.server.security.authorization.AmbariLdapAuthenticationProvider;
 import org.apache.ambari.server.security.authorization.AmbariLdapDataPopulator;
 import org.apache.ambari.server.security.authorization.AmbariLocalUserDetailsService;
 import org.apache.ambari.server.security.authorization.Users;
-import org.apache.ambari.server.security.authorization.AmbariAuthorizationFilter;
 import org.apache.ambari.server.security.authorization.internal.AmbariInternalAuthenticationProvider;
 import org.apache.ambari.server.security.unsecured.rest.CertificateDownload;
 import org.apache.ambari.server.security.unsecured.rest.CertificateSign;
 import org.apache.ambari.server.security.unsecured.rest.ConnectionInfo;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ConfigHelper;
+import org.apache.ambari.server.state.alert.AlertDefinitionHash;
 import org.apache.ambari.server.utils.StageUtils;
 import org.apache.ambari.server.utils.VersionUtils;
 import org.apache.ambari.server.view.ViewRegistry;
@@ -533,7 +534,9 @@ public class AmbariServer {
         injector.getInstance(Gson.class), ambariMetaInfo);
     StackDependencyResourceProvider.init(ambariMetaInfo);
     ClusterResourceProvider.init(injector.getInstance(BlueprintDAO.class), ambariMetaInfo, injector.getInstance(ConfigHelper.class));
-    AlertDefinitionResourceProvider.init(injector.getInstance(AlertDefinitionDAO.class));
+    AlertDefinitionResourceProvider.init(
+        injector.getInstance(AlertDefinitionDAO.class),
+        injector.getInstance(AlertDefinitionHash.class));
     PermissionResourceProvider.init(injector.getInstance(PermissionDAO.class));
     ViewPermissionResourceProvider.init(injector.getInstance(PermissionDAO.class));
     PrivilegeResourceProvider.init(injector.getInstance(PrivilegeDAO.class), injector.getInstance(UserDAO.class),

http://git-wip-us.apache.org/repos/asf/ambari/blob/9b6c02f6/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProvider.java
index 5ea6d3b..83bd7b1 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProvider.java
@@ -42,6 +42,7 @@ import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
 import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
 import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.alert.AlertDefinitionHash;
 import org.apache.ambari.server.state.alert.Scope;
 import org.apache.ambari.server.state.alert.SourceType;
 
@@ -65,20 +66,23 @@ public class AlertDefinitionResourceProvider extends AbstractControllerResourceP
   protected static final String ALERT_DEF_COMPONENT_NAME = "AlertDefinition/component_name";
   protected static final String ALERT_DEF_ENABLED = "AlertDefinition/enabled";
   protected static final String ALERT_DEF_SCOPE = "AlertDefinition/scope";
-  protected static final String ALERT_DEF_UUID = "AlertDefinition/uuid";
 
   private static Set<String> pkPropertyIds = new HashSet<String>(
       Arrays.asList(ALERT_DEF_ID, ALERT_DEF_NAME));
+
   private static AlertDefinitionDAO alertDefinitionDAO = null;
 
   private static Gson gson = new Gson();
 
+  private static AlertDefinitionHash alertDefinitionHash;
+
   /**
    * @param instance
    */
   @Inject
-  public static void init(AlertDefinitionDAO instance) {
+  public static void init(AlertDefinitionDAO instance, AlertDefinitionHash adh) {
     alertDefinitionDAO = instance;
+    alertDefinitionHash = adh;
   }
 
   AlertDefinitionResourceProvider(Set<String> propertyIds,
@@ -120,6 +124,7 @@ public class AlertDefinitionResourceProvider extends AbstractControllerResourceP
     // !!! TODO multi-create in a transaction
     for (AlertDefinitionEntity entity : entities) {
       alertDefinitionDAO.create(entity);
+      alertDefinitionHash.invalidateHosts(entity);
     }
   }
 
@@ -219,7 +224,6 @@ public class AlertDefinitionResourceProvider extends AbstractControllerResourceP
           results.add(toResource(false, clusterName, entity, requestPropertyIds));
         }
       } else {
-
         Cluster cluster = null;
         try {
           cluster = getManagementController().getClusters().getCluster(clusterName);
@@ -297,6 +301,8 @@ public class AlertDefinitionResourceProvider extends AbstractControllerResourceP
         entity.setHash(UUID.randomUUID().toString());
 
         alertDefinitionDAO.merge(entity);
+
+        alertDefinitionHash.invalidateHosts(entity);
       }
     }
 
@@ -323,12 +329,13 @@ public class AlertDefinitionResourceProvider extends AbstractControllerResourceP
 
       LOG.info("Deleting alert definition {}", definitionId);
 
-      final AlertDefinitionEntity ad = alertDefinitionDAO.findById(definitionId.longValue());
+      final AlertDefinitionEntity entity = alertDefinitionDAO.findById(definitionId.longValue());
 
       modifyResources(new Command<Void>() {
         @Override
         public Void invoke() throws AmbariException {
-          alertDefinitionDAO.remove(ad);
+          alertDefinitionDAO.remove(entity);
+          alertDefinitionHash.invalidateHosts(entity);
           return null;
         }
       });
@@ -355,9 +362,6 @@ public class AlertDefinitionResourceProvider extends AbstractControllerResourceP
     setResourceProperty(resource, ALERT_DEF_LABEL, entity.getLabel(),
         requestedIds);
 
-    setResourceProperty(resource, ALERT_DEF_UUID, entity.getHash(),
-        requestedIds);
-
     if (!isCollection && null != resource.getPropertyValue(ALERT_DEF_SOURCE_TYPE)) {
 
       try {

http://git-wip-us.apache.org/repos/asf/ambari/blob/9b6c02f6/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertDefinitionHash.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertDefinitionHash.java b/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertDefinitionHash.java
index 1f31c35..7cbd4b3 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertDefinitionHash.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertDefinitionHash.java
@@ -31,10 +31,13 @@ import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.controller.RootServiceResponseFactory.Components;
+import org.apache.ambari.server.controller.RootServiceResponseFactory.Services;
 import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
 import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.Host;
 import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentHost;
@@ -138,6 +141,39 @@ public class AlertDefinitionHash {
   }
 
   /**
+   * Invalidate all cached hashes causing subsequent lookups to recalculate.
+   */
+  public void invalidateAll() {
+    m_hashes.clear();
+  }
+
+  /**
+   * Invalidates the cached hash for the specified agent host.
+   *
+   * @param hostName
+   *          the host to invalidate the cache for (not {@code null}).
+   */
+  public void invalidate(String hostName) {
+    m_hashes.remove(hostName);
+  }
+
+  /**
+   * Gets whether the alert definition has for the specified host has been
+   * calculated and cached.
+   *
+   * @param hostName
+   *          the host.
+   * @return {@code true} if the hash was calculated; {@code false} otherwise.
+   */
+  public boolean isHashCached(String hostName) {
+    if (null == hostName) {
+      return false;
+    }
+
+    return m_hashes.containsKey(hostName);
+  }
+
+  /**
    * Gets the alert definitions for the specified host. This will include the
    * following types of alert definitions:
    * <ul>
@@ -219,6 +255,92 @@ public class AlertDefinitionHash {
   }
 
   /**
+   * Invalidate the hashes of any host that would be affected by the specified
+   * definition.
+   *
+   * @param definition
+   *          the definition to use to find the hosts to invlidate (not
+   *          {@code null}).
+   */
+  public void invalidateHosts(AlertDefinitionEntity definition) {
+    long clusterId = definition.getClusterId();
+
+    // intercept host agent alerts; they affect all hosts
+    String definitionServiceName = definition.getServiceName();
+    String definitionComponentName = definition.getComponentName();
+    if (Services.AMBARI.equals(definitionServiceName)
+        && Components.AMBARI_AGENT.equals(definitionComponentName)) {
+
+      invalidateAll();
+      return;
+    }
+
+    Cluster cluster = null;
+    Map<String, Host> hosts = null;
+    try {
+      cluster = m_clusters.getClusterById(clusterId);
+      if (null != cluster) {
+        hosts = m_clusters.getHostsForCluster(cluster.getClusterName());
+      }
+
+      if (null == cluster) {
+        LOG.warn("Unable to lookup cluster with ID {}", clusterId);
+      }
+    } catch (Exception exception) {
+      LOG.error("Unable to lookup cluster with ID {}", clusterId, exception);
+    }
+
+    if (null == cluster) {
+      return;
+    }
+
+    // find all hosts that have the matching service and component
+    if (null != hosts) {
+      for (String hostName : hosts.keySet()) {
+        List<ServiceComponentHost> hostComponents = cluster.getServiceComponentHosts(hostName);
+        if (null == hostComponents || hostComponents.size() == 0) {
+          continue;
+        }
+
+        // if a host has a matching service/component, invalidate it
+        for (ServiceComponentHost component : hostComponents) {
+          String serviceName = component.getServiceName();
+          String componentName = component.getServiceComponentName();
+          if (serviceName.equals(definitionServiceName)
+              && componentName.equals(definitionComponentName)) {
+            invalidate(hostName);
+          }
+        }
+      }
+    }
+
+    // get the service that this alert definition is associated with
+    Map<String, Service> services = cluster.getServices();
+    Service service = services.get(definitionServiceName);
+    if (null == service) {
+      LOG.warn("The alert definition {} has an unknown service of {}",
+          definition.getDefinitionName(), definitionServiceName);
+      return;
+    }
+
+    // get all master components of the definition's service; any hosts that
+    // run the master should be invalidated as well
+    Map<String, ServiceComponent> components = service.getServiceComponents();
+    if (null != components) {
+      for (Entry<String, ServiceComponent> component : components.entrySet()) {
+        if (component.getValue().isMasterComponent()) {
+          Map<String, ServiceComponentHost> componentHosts = component.getValue().getServiceComponentHosts();
+          if (null != componentHosts) {
+            for (String componentHost : componentHosts.keySet()) {
+              invalidate(componentHost);
+            }
+          }
+        }
+      }
+    }
+  }
+
+  /**
    * Calculates a unique hash value representing all of the alert definitions
    * that should be scheduled to run on a given host. Alerts of type
    * {@link SourceType#AGGREGATE} are not included in the hash since they are

http://git-wip-us.apache.org/repos/asf/ambari/blob/9b6c02f6/ambari-server/src/main/resources/properties.json
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/properties.json b/ambari-server/src/main/resources/properties.json
index fec8aa2..6686f39 100644
--- a/ambari-server/src/main/resources/properties.json
+++ b/ambari-server/src/main/resources/properties.json
@@ -421,8 +421,7 @@
       "AlertDefinition/interval",
       "AlertDefinition/enabled",
       "AlertDefinition/scope",
-      "AlertDefinition/source",
-      "AlertDefinition/uuid"
+      "AlertDefinition/source"
     ],      
     "Controller":[
         "Controllers/name",

http://git-wip-us.apache.org/repos/asf/ambari/blob/9b6c02f6/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProviderTest.java
index cf6ff42..864eb08 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProviderTest.java
@@ -20,6 +20,7 @@ package org.apache.ambari.server.controller.internal;
 import static org.easymock.EasyMock.anyObject;
 import static org.easymock.EasyMock.capture;
 import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.createNiceMock;
 import static org.easymock.EasyMock.createStrictMock;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.expectLastCall;
@@ -46,7 +47,9 @@ import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
 import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.alert.AlertDefinitionHash;
 import org.easymock.Capture;
+import org.easymock.EasyMock;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -57,16 +60,21 @@ import org.junit.Test;
 public class AlertDefinitionResourceProviderTest {
 
   AlertDefinitionDAO dao = null;
+  AlertDefinitionHash definitionHash = null;
 
   private static String DEFINITION_UUID = UUID.randomUUID().toString();
 
   @Before
   public void before() {
     dao = createStrictMock(AlertDefinitionDAO.class);
+    definitionHash = createNiceMock(AlertDefinitionHash.class);
 
-    AlertDefinitionResourceProvider.init(dao);
+    AlertDefinitionResourceProvider.init(dao, definitionHash);
   }
 
+  /**
+   * @throws Exception
+   */
   @Test
   public void testGetResourcesNoPredicate() throws Exception {
     AlertDefinitionResourceProvider provider = createProvider(null);
@@ -79,13 +87,15 @@ public class AlertDefinitionResourceProviderTest {
     assertEquals(0, results.size());
   }
 
+  /**
+   * @throws Exception
+   */
   @Test
   public void testGetResourcesClusterPredicate() throws Exception {
     Request request = PropertyHelper.getReadRequest(
         AlertDefinitionResourceProvider.ALERT_DEF_CLUSTER_NAME,
         AlertDefinitionResourceProvider.ALERT_DEF_ID,
         AlertDefinitionResourceProvider.ALERT_DEF_NAME,
-        AlertDefinitionResourceProvider.ALERT_DEF_UUID,
         AlertDefinitionResourceProvider.ALERT_DEF_LABEL);
 
     AmbariManagementController amc = createMock(AmbariManagementController.class);
@@ -114,12 +124,12 @@ public class AlertDefinitionResourceProviderTest {
     Assert.assertEquals("Mock Label",
         r.getPropertyValue(AlertDefinitionResourceProvider.ALERT_DEF_LABEL));
 
-    Assert.assertEquals(DEFINITION_UUID,
-        r.getPropertyValue(AlertDefinitionResourceProvider.ALERT_DEF_UUID));
-
     verify(amc, clusters, cluster, dao);
   }
 
+  /**
+   * @throws Exception
+   */
   @Test
   public void testGetSingleResource() throws Exception {
     Request request = PropertyHelper.getReadRequest(
@@ -158,6 +168,9 @@ public class AlertDefinitionResourceProviderTest {
     Assert.assertNotNull(r.getPropertyValue("AlertDefinition/source/type"));
   }
 
+  /**
+   * @throws Exception
+   */
   @Test
   public void testCreateResources() throws Exception {
     AmbariManagementController amc = createMock(AmbariManagementController.class);
@@ -171,7 +184,11 @@ public class AlertDefinitionResourceProviderTest {
     dao.create(capture(entityCapture));
     expectLastCall();
 
-    replay(amc, clusters, cluster, dao);
+    // creating a single definition should invalidate hosts of the definition
+    definitionHash.invalidateHosts(EasyMock.anyObject(AlertDefinitionEntity.class));
+    expectLastCall().once();
+
+    replay(amc, clusters, cluster, dao, definitionHash);
 
     AlertDefinitionResourceProvider provider = createProvider(amc);
 
@@ -208,6 +225,9 @@ public class AlertDefinitionResourceProviderTest {
 
   }
 
+  /**
+   * @throws Exception
+   */
   @Test
   public void testUpdateResources() throws Exception {
     AmbariManagementController amc = createMock(AmbariManagementController.class);
@@ -221,7 +241,11 @@ public class AlertDefinitionResourceProviderTest {
     dao.create(capture(entityCapture));
     expectLastCall();
 
-    replay(amc, clusters, cluster, dao);
+    // updateing a single definition should invalidate hosts of the definition
+    definitionHash.invalidateHosts(EasyMock.anyObject(AlertDefinitionEntity.class));
+    expectLastCall().once();
+
+    replay(amc, clusters, cluster, dao, definitionHash);
 
     Map<String, Object> requestProps = new HashMap<String, Object>();
     requestProps.put(AlertDefinitionResourceProvider.ALERT_DEF_CLUSTER_NAME, "c1");
@@ -270,6 +294,9 @@ public class AlertDefinitionResourceProviderTest {
     verify(amc, clusters, cluster, dao);
   }
 
+  /**
+   * @throws Exception
+   */
   @Test
   public void testDeleteResources() throws Exception {
     AmbariManagementController amc = createMock(AmbariManagementController.class);
@@ -283,7 +310,11 @@ public class AlertDefinitionResourceProviderTest {
     dao.create(capture(entityCapture));
     expectLastCall();
 
-    replay(amc, clusters, cluster, dao);
+    // deleting a single definition should invalidate hosts of the definition
+    definitionHash.invalidateHosts(EasyMock.anyObject(AlertDefinitionEntity.class));
+    expectLastCall().once();
+
+    replay(amc, clusters, cluster, dao, definitionHash);
 
     AlertDefinitionResourceProvider provider = createProvider(amc);
 
@@ -323,6 +354,10 @@ public class AlertDefinitionResourceProviderTest {
 
   }
 
+  /**
+   * @param amc
+   * @return
+   */
   private AlertDefinitionResourceProvider createProvider(AmbariManagementController amc) {
     return new AlertDefinitionResourceProvider(
         PropertyHelper.getPropertyIds(Resource.Type.AlertDefinition),
@@ -330,6 +365,9 @@ public class AlertDefinitionResourceProviderTest {
         amc);
   }
 
+  /**
+   * @return
+   */
   private List<AlertDefinitionEntity> getMockEntities() {
     AlertDefinitionEntity entity = new AlertDefinitionEntity();
     entity.setClusterId(Long.valueOf(1L));

http://git-wip-us.apache.org/repos/asf/ambari/blob/9b6c02f6/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertDefinitionHashTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertDefinitionHashTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertDefinitionHashTest.java
index 937417a..c8fce49 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertDefinitionHashTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertDefinitionHashTest.java
@@ -66,6 +66,10 @@ public class AlertDefinitionHashTest extends TestCase {
   private static final String CLUSTERNAME = "cluster1";
   private static final String HOSTNAME = "c6401.ambari.apache.org";
 
+  private List<AlertDefinitionEntity> m_agentDefinitions;
+  private AlertDefinitionEntity m_hdfsService;
+  AlertDefinitionEntity m_hdfsHost;
+
   /**
    *
    */
@@ -124,6 +128,9 @@ public class AlertDefinitionHashTest extends TestCase {
     expect(m_mockClusters.getCluster((String) anyObject())).andReturn(
         m_mockCluster).atLeastOnce();
 
+    expect(m_mockClusters.getClusterById(EasyMock.anyInt())).andReturn(
+        m_mockCluster).atLeastOnce();
+
     // cluster mock
     expect(m_mockCluster.getClusterId()).andReturn(Long.valueOf(1)).anyTimes();
     expect(m_mockCluster.getClusterName()).andReturn(CLUSTERNAME).anyTimes();
@@ -132,21 +139,21 @@ public class AlertDefinitionHashTest extends TestCase {
         m_mockCluster.getServiceComponentHosts(EasyMock.anyObject(String.class))).andReturn(
         serviceComponentHosts).anyTimes();
 
-    AlertDefinitionEntity hdfsService = new AlertDefinitionEntity();
-    hdfsService.setDefinitionId(1L);
-    hdfsService.setClusterId(1L);
-    hdfsService.setHash(UUID.randomUUID().toString());
-    hdfsService.setServiceName("HDFS");
-    hdfsService.setComponentName("NAMENODE");
-    hdfsService.setScope(Scope.SERVICE);
-
-    AlertDefinitionEntity hdfsHost = new AlertDefinitionEntity();
-    hdfsHost.setDefinitionId(2L);
-    hdfsHost.setClusterId(1L);
-    hdfsHost.setHash(UUID.randomUUID().toString());
-    hdfsHost.setServiceName("HDFS");
-    hdfsHost.setComponentName("DATANODE");
-    hdfsHost.setScope(Scope.HOST);
+    m_hdfsService = new AlertDefinitionEntity();
+    m_hdfsService.setDefinitionId(1L);
+    m_hdfsService.setClusterId(1L);
+    m_hdfsService.setHash(UUID.randomUUID().toString());
+    m_hdfsService.setServiceName("HDFS");
+    m_hdfsService.setComponentName("NAMENODE");
+    m_hdfsService.setScope(Scope.SERVICE);
+
+    m_hdfsHost = new AlertDefinitionEntity();
+    m_hdfsHost.setDefinitionId(2L);
+    m_hdfsHost.setClusterId(1L);
+    m_hdfsHost.setHash(UUID.randomUUID().toString());
+    m_hdfsHost.setServiceName("HDFS");
+    m_hdfsHost.setComponentName("DATANODE");
+    m_hdfsHost.setScope(Scope.HOST);
 
     AlertDefinitionEntity agentScoped = new AlertDefinitionEntity();
     agentScoped.setDefinitionId(3L);
@@ -159,15 +166,17 @@ public class AlertDefinitionHashTest extends TestCase {
     EasyMock.expect(
         m_mockDao.findByServiceMaster(EasyMock.anyInt(),
             (Set<String>) EasyMock.anyObject())).andReturn(
-        Collections.singletonList(hdfsService)).anyTimes();
+        Collections.singletonList(m_hdfsService)).anyTimes();
 
     EasyMock.expect(
         m_mockDao.findByServiceComponent(EasyMock.anyInt(),
             EasyMock.anyObject(String.class), EasyMock.anyObject(String.class))).andReturn(
-        Collections.singletonList(hdfsHost)).anyTimes();
+        Collections.singletonList(m_hdfsHost)).anyTimes();
 
+    m_agentDefinitions = new ArrayList<AlertDefinitionEntity>();
+    m_agentDefinitions.add(agentScoped);
     EasyMock.expect(m_mockDao.findAgentScoped(EasyMock.anyInt())).andReturn(
-        Collections.singletonList(agentScoped)).anyTimes();
+        m_agentDefinitions).anyTimes();
 
     EasyMock.replay(m_mockClusters, m_mockCluster, m_mockDao);
     m_hash = m_injector.getInstance(AlertDefinitionHash.class);
@@ -205,6 +214,73 @@ public class AlertDefinitionHashTest extends TestCase {
   }
 
   /**
+   * Test {@link AlertDefinitionHash#invalidateAll()}.
+   */
+  @Test
+  public void testInvalidateAll() {
+    String hash = m_hash.getHash(CLUSTERNAME, HOSTNAME);
+    assertNotNull(hash);
+
+    m_hash.invalidateAll();
+
+    String newHash = m_hash.getHash(CLUSTERNAME, HOSTNAME);
+    assertEquals(hash, newHash);
+
+    m_hash.invalidateAll();
+
+    // add a new alert definition, forcing new hash
+    AlertDefinitionEntity agentScoped = new AlertDefinitionEntity();
+    agentScoped.setDefinitionId(System.currentTimeMillis());
+    agentScoped.setClusterId(1L);
+    agentScoped.setHash(UUID.randomUUID().toString());
+    agentScoped.setServiceName("AMBARI");
+    agentScoped.setComponentName("AMBARI_AGENT");
+    agentScoped.setScope(Scope.HOST);
+
+    m_agentDefinitions.add(agentScoped);
+
+    newHash = m_hash.getHash(CLUSTERNAME, HOSTNAME);
+    assertNotSame(hash, newHash);
+  }
+
+  /**
+   * Test {@link AlertDefinitionHash#isHashCached(String)}.
+   */
+  @Test
+  public void testIsHashCached() {
+    assertFalse(m_hash.isHashCached(HOSTNAME));
+    String hash = m_hash.getHash(CLUSTERNAME, HOSTNAME);
+    assertNotNull(hash);
+    assertTrue(m_hash.isHashCached(HOSTNAME));
+
+    m_hash.invalidate(HOSTNAME);
+    assertFalse(m_hash.isHashCached(HOSTNAME));
+    hash = m_hash.getHash(CLUSTERNAME, HOSTNAME);
+    assertNotNull(hash);
+    assertTrue(m_hash.isHashCached(HOSTNAME));
+
+    m_hash.invalidateAll();
+    assertFalse(m_hash.isHashCached(HOSTNAME));
+    hash = m_hash.getHash(CLUSTERNAME, HOSTNAME);
+    assertNotNull(hash);
+    assertTrue(m_hash.isHashCached(HOSTNAME));
+  }
+
+  /**
+   * Test {@link AlertDefinitionHash#invalidateHosts(AlertDefinitionEntity)}.
+   */
+  @Test
+  public void testInvalidateHosts() {
+    assertFalse(m_hash.isHashCached(HOSTNAME));
+    String hash = m_hash.getHash(CLUSTERNAME, HOSTNAME);
+    assertNotNull(hash);
+    assertTrue(m_hash.isHashCached(HOSTNAME));
+
+    m_hash.invalidateHosts(m_hdfsHost);
+    assertFalse(m_hash.isHashCached(HOSTNAME));
+  }
+
+  /**
    *
    */
   private class MockModule implements Module {