ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jonathanhur...@apache.org
Subject ambari git commit: AMBARI-9823 - Alert Notifications Are Not Received Without Restarting Ambari Server (jonathanhurley)
Date Fri, 27 Feb 2015 14:17:54 GMT
Repository: ambari
Updated Branches:
  refs/heads/branch-2.0.0 9a8903e83 -> 0f0ab2c04


AMBARI-9823 - Alert Notifications Are Not Received Without Restarting Ambari Server (jonathanhurley)


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

Branch: refs/heads/branch-2.0.0
Commit: 0f0ab2c04761142f5708f70d2f3354502a9f0f34
Parents: 9a8903e
Author: Jonathan Hurley <jhurley@hortonworks.com>
Authored: Thu Feb 26 21:31:37 2015 -0500
Committer: Jonathan Hurley <jhurley@hortonworks.com>
Committed: Fri Feb 27 09:17:45 2015 -0500

----------------------------------------------------------------------
 ambari-project/pom.xml                          |  2 +-
 .../server/orm/dao/AlertDefinitionDAO.java      |  5 ++-
 .../server/orm/entities/AlertNoticeEntity.java  |  4 ++-
 .../server/orm/entities/AlertTargetEntity.java  | 37 +++++++++++++++----
 .../server/orm/dao/AlertDefinitionDAOTest.java  | 19 ++++++++++
 .../server/orm/dao/AlertDispatchDAOTest.java    | 38 ++++++++++++++++++++
 .../ambari/server/orm/dao/AlertsDAOTest.java    |  9 +++++
 .../alerts/AlertStateChangedEventTest.java      |  2 +-
 8 files changed, 105 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/0f0ab2c0/ambari-project/pom.xml
----------------------------------------------------------------------
diff --git a/ambari-project/pom.xml b/ambari-project/pom.xml
index 92e3b5c..0577bee 100644
--- a/ambari-project/pom.xml
+++ b/ambari-project/pom.xml
@@ -274,7 +274,7 @@ instead of a SNAPSHOT. -->
       <dependency>
         <groupId>org.eclipse.persistence</groupId>
         <artifactId>eclipselink</artifactId>
-        <version>2.4.2</version>
+        <version>2.5.2</version>
       </dependency>
       <dependency>
         <groupId>org.postgresql</groupId>

http://git-wip-us.apache.org/repos/asf/ambari/blob/0f0ab2c0/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAO.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAO.java
index 2daf804..659fb21 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAO.java
@@ -291,7 +291,8 @@ public class AlertDefinitionDAO {
   @Transactional
   public void create(AlertDefinitionEntity alertDefinition)
       throws AmbariException {
-    entityManagerProvider.get().persist(alertDefinition);
+    EntityManager entityManager = entityManagerProvider.get();
+    entityManager.persist(alertDefinition);
 
     AlertGroupEntity group = dispatchDao.findDefaultServiceGroup(
         alertDefinition.getClusterId(), alertDefinition.getServiceName());
@@ -319,6 +320,8 @@ public class AlertDefinitionDAO {
       LOG.warn("Unable to broadcast alert registration event for {}",
           alertDefinition.getDefinitionName());
     }
+
+    entityManager.refresh(alertDefinition);
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/0f0ab2c0/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertNoticeEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertNoticeEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertNoticeEntity.java
index d0d4c9a..ae43dfe 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertNoticeEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertNoticeEntity.java
@@ -18,6 +18,7 @@
 package org.apache.ambari.server.orm.entities;
 
 import javax.persistence.Basic;
+import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.EnumType;
@@ -74,7 +75,7 @@ public class AlertNoticeEntity {
   /**
    * Bi-directional many-to-one association to {@link AlertTargetEntity}
    */
-  @ManyToOne
+  @ManyToOne(cascade = { CascadeType.REFRESH })
   @JoinColumn(name = "target_id", nullable = false)
   private AlertTargetEntity alertTarget;
 
@@ -181,6 +182,7 @@ public class AlertNoticeEntity {
    */
   public void setAlertTarget(AlertTargetEntity alertTarget) {
     this.alertTarget = alertTarget;
+    alertTarget.addAlertNotice(this);
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/0f0ab2c0/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
index e1248db..29f79d7 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
@@ -17,8 +17,11 @@
  */
 package org.apache.ambari.server.orm.entities;
 
+import java.util.ArrayList;
 import java.util.Collections;
+import java.util.EnumSet;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 import javax.persistence.Basic;
@@ -38,6 +41,7 @@ import javax.persistence.Lob;
 import javax.persistence.ManyToMany;
 import javax.persistence.NamedQueries;
 import javax.persistence.NamedQuery;
+import javax.persistence.OneToMany;
 import javax.persistence.PreRemove;
 import javax.persistence.Table;
 import javax.persistence.TableGenerator;
@@ -99,7 +103,13 @@ public class AlertTargetEntity {
   @ElementCollection(targetClass = AlertState.class)
   @CollectionTable(name = "alert_target_states", joinColumns = @JoinColumn(name = "target_id"))
   @Column(name = "alert_state")
-  private Set<AlertState> alertStates;
+  private Set<AlertState> alertStates = EnumSet.allOf(AlertState.class);
+
+  /**
+   * Bi-directional one-to-many association to {@link AlertNoticeEntity}.
+   */
+  @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "alertTarget")
+  private List<AlertNoticeEntity> alertNotices;
 
   /**
    * Gets the unique ID of this alert target.
@@ -296,18 +306,31 @@ public class AlertTargetEntity {
   }
 
   /**
+   * Adds the specified notice to the notices that have been sent out for this
+   * target.
+   *
+   * @param notice
+   *          the notice.
+   */
+  protected void addAlertNotice(AlertNoticeEntity notice) {
+    if (null == alertNotices) {
+      alertNotices = new ArrayList<AlertNoticeEntity>();
+    }
+
+    alertNotices.add(notice);
+  }
+
+  /**
    * Called before {@link EntityManager#remove(Object)} for this entity, removes
    * the non-owning relationship between targets and groups.
    */
   @PreRemove
   public void preRemove() {
     Set<AlertGroupEntity> groups = getAlertGroups();
-    if (groups.isEmpty()) {
-      return;
-    }
-
-    for (AlertGroupEntity group : groups) {
-      group.removeAlertTarget(this);
+    if (!groups.isEmpty()) {
+      for (AlertGroupEntity group : groups) {
+        group.removeAlertTarget(this);
+      }
     }
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/0f0ab2c0/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
index a1cd063..acf25d2 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
@@ -364,4 +364,23 @@ public class AlertDefinitionDAOTest {
 
     assertEquals(0, dispatchDao.findAllGroups(clusterId).size());
   }
+
+  @Test
+  public void testNestedClusterEntity() throws Exception {
+    AlertDefinitionEntity definition = new AlertDefinitionEntity();
+    definition.setDefinitionName("nested-cluster-entity-test");
+    definition.setServiceName("HDFS");
+    definition.setComponentName(null);
+    definition.setClusterId(clusterId);
+    definition.setHash(UUID.randomUUID().toString());
+    definition.setScheduleInterval(60);
+    definition.setScope(Scope.SERVICE);
+    definition.setSource("{\"type\" : \"SCRIPT\"}");
+    definition.setSourceType(SourceType.SCRIPT);
+    dao.create(definition);
+
+    definition = dao.findById(definition.getDefinitionId());
+    assertNotNull(definition.getCluster());
+    assertEquals(clusterId, definition.getCluster().getClusterId());
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/0f0ab2c0/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
index 44c40f2..a267043 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
@@ -444,6 +444,8 @@ public class AlertDispatchDAOTest {
   public void testDeleteTarget() throws Exception {
     AlertTargetEntity target = m_helper.createAlertTarget();
     target = m_dao.findTargetById(target.getTargetId());
+    assertTrue(target.getAlertStates().size() > 0);
+
     assertNotNull(target);
 
     m_dao.remove(target);
@@ -453,6 +455,42 @@ public class AlertDispatchDAOTest {
   }
 
   /**
+  *
+  */
+  @Test
+  public void testDeleteTargetWithNotices() throws Exception {
+    AlertTargetEntity target = m_helper.createAlertTarget();
+
+    List<AlertDefinitionEntity> definitions = createDefinitions();
+    AlertDefinitionEntity definition = definitions.get(0);
+
+    AlertHistoryEntity history = new AlertHistoryEntity();
+    history.setServiceName(definition.getServiceName());
+    history.setClusterId(m_cluster.getClusterId());
+    history.setAlertDefinition(definition);
+    history.setAlertLabel("Label");
+    history.setAlertState(AlertState.OK);
+    history.setAlertText("Alert Text");
+    history.setAlertTimestamp(System.currentTimeMillis());
+    m_alertsDao.create(history);
+
+    AlertNoticeEntity notice = new AlertNoticeEntity();
+    notice.setUuid(UUID.randomUUID().toString());
+    notice.setAlertTarget(target);
+    notice.setAlertHistory(history);
+    notice.setNotifyState(NotificationState.PENDING);
+    m_dao.create(notice);
+
+    notice = m_dao.findNoticeById(notice.getNotificationId());
+    assertEquals(target.getTargetId(), notice.getAlertTarget().getTargetId());
+
+    target = m_dao.findTargetById(target.getTargetId());
+    m_dao.remove(target);
+    notice = m_dao.findNoticeById(notice.getNotificationId());
+    assertNull(notice);
+  }
+
+  /**
    *
    */
   @Test

http://git-wip-us.apache.org/repos/asf/ambari/blob/0f0ab2c0/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
index fb3303c..9a2a6a4 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
@@ -835,6 +835,15 @@ public class AlertsDAOTest {
 
     assertEquals(newHistory.getAlertState(),
         newCurrent.getAlertHistory().getAlertState());
+
+    newCurrent = m_dao.findCurrentById(current.getAlertId());
+
+    assertEquals(newHistory.getAlertId(),
+        newCurrent.getAlertHistory().getAlertId());
+
+    assertEquals(newHistory.getAlertState(),
+        newCurrent.getAlertHistory().getAlertState());
+
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/0f0ab2c0/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertStateChangedEventTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertStateChangedEventTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertStateChangedEventTest.java
index acac1c0..961157c 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertStateChangedEventTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertStateChangedEventTest.java
@@ -103,7 +103,7 @@ public class AlertStateChangedEventTest {
    */
   @Test
   public void testAlertNoticeCreationFromEvent() throws Exception {
-    AlertTargetEntity alertTarget = EasyMock.createMock(AlertTargetEntity.class);
+    AlertTargetEntity alertTarget = EasyMock.createNiceMock(AlertTargetEntity.class);
     AlertGroupEntity alertGroup = EasyMock.createMock(AlertGroupEntity.class);
     List<AlertGroupEntity> groups = new ArrayList<AlertGroupEntity>();
     Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();


Mime
View raw message