AMBARI-20672 - Cluster Merge At End Of Upgrade Creation Cascades Unnecessarily 
(jonathanhurley)


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

Branch: refs/heads/ambari-rest-api-explorer
Commit: fe0aa448bd16a225ccb0bab8315fd2b8b34fba75
Parents: 3ca41f2
Author: Jonathan Hurley <jhur...@hortonworks.com>
Authored: Tue Apr 4 12:57:33 2017 -0400
Committer: Jonathan Hurley <jhur...@hortonworks.com>
Committed: Tue Apr 4 16:52:10 2017 -0400

----------------------------------------------------------------------
 .../internal/UpgradeResourceProvider.java       | 86 ++++++++++----------
 .../alerts/AlertStateChangedListener.java       |  2 +-
 .../listeners/upgrade/StackVersionListener.java |  2 +-
 .../ambari/server/orm/dao/UpgradeDAO.java       |  5 --
 .../server/orm/entities/ClusterEntity.java      | 13 ++-
 .../server/orm/entities/UpgradeEntity.java      | 27 ++++--
 .../org/apache/ambari/server/state/Cluster.java | 30 +++----
 .../server/state/cluster/ClusterImpl.java       | 48 ++---------
 .../services/RetryUpgradeActionService.java     |  2 +-
 .../internal/UpgradeResourceProviderTest.java   |  4 +-
 .../UpgradeSummaryResourceProviderTest.java     | 16 ++--
 .../upgrade/StackVersionListenerTest.java       |  2 +-
 .../ambari/server/orm/dao/UpgradeDAOTest.java   | 21 +++--
 .../upgrades/UpgradeActionTest.java             |  2 +-
 .../server/state/ServiceComponentTest.java      | 15 +++-
 .../alerts/AlertStateChangedEventTest.java      |  6 +-
 .../services/RetryUpgradeActionServiceTest.java |  2 +-
 17 files changed, 141 insertions(+), 142 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
index ca9ce07..709ca93 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
@@ -335,7 +335,7 @@ public class UpgradeResourceProvider extends 
AbstractControllerResourceProvider
     final Cluster cluster;
 
     try {
-      cluster = 
getManagementController().getClusters().getCluster(clusterName);
+      cluster = clusters.get().getCluster(clusterName);
     } catch (AmbariException e) {
       throw new NoSuchParentResourceException(
           String.format("Cluster %s could not be loaded", clusterName));
@@ -421,7 +421,7 @@ public class UpgradeResourceProvider extends 
AbstractControllerResourceProvider
 
       Cluster cluster;
       try {
-        cluster = 
getManagementController().getClusters().getCluster(clusterName);
+        cluster = clusters.get().getCluster(clusterName);
       } catch (AmbariException e) {
         throw new NoSuchResourceException(
             String.format("Cluster %s could not be loaded", clusterName));
@@ -484,7 +484,7 @@ public class UpgradeResourceProvider extends 
AbstractControllerResourceProvider
     final Cluster cluster;
 
     try {
-      cluster = 
getManagementController().getClusters().getCluster(clusterName);
+      cluster = clusters.get().getCluster(clusterName);
     } catch (AmbariException e) {
       throw new NoSuchParentResourceException(
           String.format("Cluster %s could not be loaded", clusterName));
@@ -496,14 +496,11 @@ public class UpgradeResourceProvider extends 
AbstractControllerResourceProvider
           "manage upgrade and downgrade");
     }
 
-
-
     String requestIdProperty = (String) propertyMap.get(UPGRADE_REQUEST_ID);
     if (null == requestIdProperty) {
       throw new IllegalArgumentException(String.format("%s is required", 
UPGRADE_REQUEST_ID));
     }
 
-    long clusterId = cluster.getClusterId();
     long requestId = Long.parseLong(requestIdProperty);
     UpgradeEntity upgradeEntity = 
s_upgradeDAO.findUpgradeByRequestId(requestId);
     if( null == upgradeEntity){
@@ -544,11 +541,11 @@ public class UpgradeResourceProvider extends 
AbstractControllerResourceProvider
         suspended = Boolean.valueOf((String) 
propertyMap.get(UPGRADE_SUSPENDED));
       }
 
-      setUpgradeRequestStatus(clusterId, requestId, status, propertyMap);
-
-      // When the status of the upgrade's request is changing, we also update 
the suspended flag.
-      upgradeEntity.setSuspended(suspended);
-      s_upgradeDAO.merge(upgradeEntity);
+      try {
+        setUpgradeRequestStatus(cluster, requestId, status, suspended, 
propertyMap);
+      } catch (AmbariException ambariException) {
+        throw new SystemException(ambariException.getMessage(), 
ambariException);
+      }
     }
 
     // if either of the skip failure settings are in the request, then we need
@@ -948,11 +945,12 @@ public class UpgradeResourceProvider extends 
AbstractControllerResourceProvider
       RequestStageContainer request,
       UpgradeEntity upgradeEntity) throws AmbariException {
 
-    upgradeEntity.setRequestId(request.getId());
-
     request.persist();
+    RequestEntity requestEntity = s_requestDAO.findByPK(request.getId());
 
+    upgradeEntity.setRequestEntity(requestEntity);
     s_upgradeDAO.create(upgradeEntity);
+
     cluster.setUpgradeEntity(upgradeEntity);
 
     return upgradeEntity;
@@ -1670,19 +1668,28 @@ public class UpgradeResourceProvider extends 
AbstractControllerResourceProvider
    * <li>{@link HostRoleStatus#ABORTED}</li>
    * <li>{@link HostRoleStatus#PENDING}</li>
    * </ul>
+   * This method will also adjust the cluster->upgrade association correctly
+   * based on the new status being supplied.
    *
-   * @param clusterId
-   *          the ID of the cluster
+   * @param cluster
+   *          the cluster
    * @param requestId
    *          the request to change the status for.
    * @param status
    *          the status to set on the associated request.
+   * @param suspended
+   *          if the value of the specified status is
+   *          {@link HostRoleStatus#ABORTED}, then this boolean will control
+   *          whether the upgrade is suspended (still associated with the
+   *          cluster) or aborted (no longer associated with the cluster).
    * @param propertyMap
    *          the map of request properties (needed for things like abort 
reason
    *          if present)
    */
-  private void setUpgradeRequestStatus(long clusterId, long requestId, 
HostRoleStatus status,
-      Map<String, Object> propertyMap) {
+  @Transactional
+  void setUpgradeRequestStatus(Cluster cluster, long requestId, HostRoleStatus 
status,
+      boolean suspended, Map<String, Object> propertyMap) throws 
AmbariException {
+    // these are the only two states we allow
     if (status != HostRoleStatus.ABORTED && status != HostRoleStatus.PENDING) {
       throw new IllegalArgumentException(String.format("Cannot set status %s, 
only %s is allowed",
           status, EnumSet.of(HostRoleStatus.ABORTED, HostRoleStatus.PENDING)));
@@ -1710,23 +1717,23 @@ public class UpgradeResourceProvider extends 
AbstractControllerResourceProvider
 
     ActionManager actionManager = getManagementController().getActionManager();
 
-    if (HostRoleStatus.ABORTED == status) {
-      if (!internalStatus.isCompletedState()) {
-        actionManager.cancelRequest(requestId, reason);
-        // Remove relevant upgrade entity
-        try {
-          Cluster cluster = clusters.get().getClusterById(clusterId);
-          UpgradeEntity upgradeEntity = 
s_upgradeDAO.findUpgradeByRequestId(requestId);
-          upgradeEntity.setSuspended(true);
-          s_upgradeDAO.merge(upgradeEntity);
-
-          cluster.setUpgradeEntity(null);
-        } catch (AmbariException e) {
-          LOG.warn("Could not clear upgrade entity for cluster with id {}", 
clusterId, e);
-        }
+    if (HostRoleStatus.ABORTED == status && 
!internalStatus.isCompletedState()) {
+      // cancel the request
+      actionManager.cancelRequest(requestId, reason);
+
+      // either suspend the upgrade or abort it outright
+      UpgradeEntity upgradeEntity = 
s_upgradeDAO.findUpgradeByRequestId(requestId);
+      if (suspended) {
+        // set the upgrade to suspended
+        upgradeEntity.setSuspended(suspended);
+        s_upgradeDAO.merge(upgradeEntity);
+      } else {
+        // otherwise remove the association with the cluster since it's being
+        // full aborted
+        cluster.setUpgradeEntity(null);
       }
-    } else {
-      // Status must be PENDING.
+
+    } else if (status == HostRoleStatus.PENDING) {
       List<Long> taskIds = new ArrayList<>();
       List<HostRoleCommandEntity> hrcEntities = 
s_hostRoleCommandDAO.findByRequestIdAndStatuses(
           requestId, Sets.newHashSet(HostRoleStatus.ABORTED, 
HostRoleStatus.TIMEDOUT));
@@ -1737,16 +1744,9 @@ public class UpgradeResourceProvider extends 
AbstractControllerResourceProvider
 
       actionManager.resubmitTasks(taskIds);
 
-      try {
-        Cluster cluster = clusters.get().getClusterById(clusterId);
-        UpgradeEntity lastUpgradeItemForCluster = 
s_upgradeDAO.findLastUpgradeOrDowngradeForCluster(cluster.getClusterId());
-        lastUpgradeItemForCluster.setSuspended(false);
-        s_upgradeDAO.merge(lastUpgradeItemForCluster);
-
-        cluster.setUpgradeEntity(lastUpgradeItemForCluster);
-      } catch (AmbariException e) {
-        LOG.warn("Could not clear upgrade entity for cluster with id {}", 
clusterId, e);
-      }
+      UpgradeEntity lastUpgradeItemForCluster = 
s_upgradeDAO.findLastUpgradeOrDowngradeForCluster(cluster.getClusterId());
+      lastUpgradeItemForCluster.setSuspended(false);
+      s_upgradeDAO.merge(lastUpgradeItemForCluster);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertStateChangedListener.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertStateChangedListener.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertStateChangedListener.java
index ce55203..a02a5d8 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertStateChangedListener.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertStateChangedListener.java
@@ -227,7 +227,7 @@ public class AlertStateChangedListener {
     Long clusterId = history.getClusterId();
     try {
       Cluster cluster = m_clusters.get().getClusterById(clusterId);
-      if (null != cluster.getUpgradeEntity() || cluster.isUpgradeSuspended()) {
+      if (null != cluster.getUpgradeInProgress()) {
         // only send AMBARI alerts if in an upgrade
         String serviceName = definition.getServiceName();
         if (!StringUtils.equals(serviceName, Services.AMBARI.name())) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
index bd7eb00..22d7f2e 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
@@ -223,7 +223,7 @@ public class StackVersionListener {
         sch.setUpgradeState(UpgradeState.VERSION_MISMATCH);
       }
     } else if (upgradeState == UpgradeState.VERSION_MISMATCH && 
desiredVersion.equals(newVersion)) {
-      if (cluster.getUpgradeEntity() != null) {
+      if (cluster.getUpgradeInProgress() != null) {
         sch.setUpgradeState(UpgradeState.COMPLETE);
       } else {
         sch.setUpgradeState(UpgradeState.NONE);

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/UpgradeDAO.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/UpgradeDAO.java 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/UpgradeDAO.java
index bf9c650..4666edf 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/UpgradeDAO.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/UpgradeDAO.java
@@ -105,11 +105,6 @@ public class UpgradeDAO {
   @Transactional
   public void create(UpgradeEntity entity) {
     EntityManager entityManager = entityManagerProvider.get();
-    // This is required because since none of the entities
-    // for the request are actually persisted yet,
-    // JPA ordering could allow foreign key entities
-    // to be created after this statement.
-    entityManager.flush();
     entityManager.persist(entity);
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
index 89b0646..527fd7a 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
@@ -142,8 +142,17 @@ public class ClusterEntity {
   })
   private ResourceEntity resource;
 
-  @OneToOne(cascade = CascadeType.ALL)
-  @JoinColumn(name = "upgrade_id", referencedColumnName = "upgrade_id")
+  @Basic
+  @Column(name = "upgrade_id", nullable = true, insertable = false, updatable 
= false)
+  private Long upgradeId;
+
+  @OneToOne(cascade = CascadeType.REMOVE)
+  @JoinColumn(
+      name = "upgrade_id",
+      referencedColumnName = "upgrade_id",
+      nullable = true,
+      insertable = false,
+      updatable = true)
   /**
    * {@code null} when there is no upgrade/downgrade in progress.
    */

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeEntity.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeEntity.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeEntity.java
index 7421ca1..89574bc 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeEntity.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeEntity.java
@@ -24,12 +24,15 @@ import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.EnumType;
 import javax.persistence.Enumerated;
+import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.JoinColumn;
 import javax.persistence.NamedQueries;
 import javax.persistence.NamedQuery;
 import javax.persistence.OneToMany;
+import javax.persistence.OneToOne;
 import javax.persistence.Table;
 import javax.persistence.TableGenerator;
 
@@ -42,8 +45,11 @@ import 
org.apache.ambari.server.state.stack.upgrade.UpgradeType;
  */
 @Entity
 @Table(name = "upgrade")
-@TableGenerator(name = "upgrade_id_generator",
-    table = "ambari_sequences", pkColumnName = "sequence_name", 
valueColumnName = "sequence_value",
+@TableGenerator(
+    name = "upgrade_id_generator",
+    table = "ambari_sequences",
+    pkColumnName = "sequence_name",
+    valueColumnName = "sequence_value",
     pkColumnValue = "upgrade_id_seq",
     initialValue = 0)
 @NamedQueries({
@@ -68,9 +74,19 @@ public class UpgradeEntity {
   @Column(name = "cluster_id", nullable = false, insertable = true, updatable 
= false)
   private Long clusterId;
 
-  @Column(name="request_id", nullable = false)
+  @Column(name = "request_id", nullable = false, insertable = false, updatable 
= false)
   private Long requestId;
 
+  /**
+   * The request entity associated with this upgrade. This relationship allows
+   * JPA to correctly order non-flushed commits during the transaction which
+   * creates the upgrade. Without it, JPA would not know the correct order and
+   * may try to create the upgrade before the request.
+   */
+  @OneToOne(optional = false, fetch = FetchType.LAZY)
+  @JoinColumn(name = "request_id", nullable = false, insertable = true, 
updatable = false)
+  private RequestEntity requestEntity = null;
+
   @Column(name="from_version", nullable = false)
   private String fromVersion = null;
 
@@ -161,8 +177,9 @@ public class UpgradeEntity {
   /**
    * @param id the request id
    */
-  public void setRequestId(Long id) {
-    requestId = id;
+  public void setRequestEntity(RequestEntity requestEntity) {
+    this.requestEntity = requestEntity;
+    requestId = requestEntity.getRequestId();
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java 
b/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
index 8074b31..9594803 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
@@ -672,15 +672,21 @@ public interface Cluster {
   boolean isBluePrintDeployed();
 
   /**
-   * @return upgrade that is in progress for a cluster. If no upgrade is going
-   * on, a null is returned.
+   * Gets an {@link UpgradeEntity} if there is an upgrade in progress or an
+   * upgrade that has been suspended. This will return the associated
+   * {@link UpgradeEntity} if it exists.
+   * 
+   * @return an upgrade which will either be in progress or suspended, or
+   *         {@code null} if none.
+   * 
    */
-  UpgradeEntity getUpgradeEntity();
+  UpgradeEntity getUpgradeInProgress();
 
   /**
-   * The value is explicitly set on the ClusterEntity when Creating,
-   * Aborting (switching to downgrade), Resuming, or Finalizing an upgrade.
-   * @param upgradeEntity the upgrade entity to set for cluster
+   * Sets or clears the associated upgrade with the cluster.
+   *
+   * @param upgradeEntity
+   *          the upgrade entity to set for cluster, or {@code null} for none.
    * @throws AmbariException
    */
   void setUpgradeEntity(UpgradeEntity upgradeEntity) throws AmbariException;
@@ -695,18 +701,6 @@ public interface Cluster {
   boolean isUpgradeSuspended();
 
   /**
-   * Gets an {@link UpgradeEntity} if there is an upgrade in progress or an
-   * upgrade that has been suspended. This will first check
-   * {@link #getUpgradeEntity()} and return that if it is not {@code null}.
-   * Otherwise, this will perform a search for the most recent 
upgrade/downgrade
-   * which has not been completed.
-   *
-   * @return an upgrade which will either be in progress or suspended, or
-   *         {@code null} if none.
-   */
-  UpgradeEntity getUpgradeInProgress();
-
-  /**
    * Returns the name of the service that the passed config type belongs to.
    *
    * @param configType

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
index 739fe23..b7cc4cd 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
@@ -1020,39 +1020,6 @@ public class ClusterImpl implements Cluster {
    * {@inheritDoc}
    */
   @Override
-  public UpgradeEntity getUpgradeInProgress() {
-    // first check for an upgrade that's actively running
-    UpgradeEntity upgradeInProgress = getUpgradeEntity();
-    if (null != upgradeInProgress) {
-      return upgradeInProgress;
-    }
-
-    // perform a search for any upgrade which should also return upgrades which
-    // are suspended
-    UpgradeEntity mostRecentUpgrade = 
upgradeDAO.findLastUpgradeOrDowngradeForCluster(getClusterId());
-    if (mostRecentUpgrade != null) {
-      if (mostRecentUpgrade.isSuspended()) {
-        return mostRecentUpgrade;
-      }
-
-      // look for any item from the prior upgrade which is still in progress
-      // (not failed, completed, or aborted)
-      List<HostRoleCommandEntity> commands = 
hostRoleCommandDAO.findByRequestIdAndStatuses(
-          mostRecentUpgrade.getRequestId(), 
HostRoleStatus.IN_PROGRESS_STATUSES);
-
-      if (!commands.isEmpty()) {
-        return mostRecentUpgrade;
-      }
-    }
-
-    return null;
-  }
-
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
   public ClusterVersionEntity getEffectiveClusterVersion() throws 
AmbariException {
     UpgradeEntity upgradeEntity = getUpgradeInProgress();
     if (upgradeEntity == null) {
@@ -3334,7 +3301,7 @@ public class ClusterImpl implements Cluster {
    * {@inheritDoc}
    */
   @Override
-  public UpgradeEntity getUpgradeEntity() {
+  public UpgradeEntity getUpgradeInProgress() {
     ClusterEntity clusterEntity = getClusterEntity();
     return clusterEntity.getUpgradeEntity();
   }
@@ -3350,10 +3317,7 @@ public class ClusterImpl implements Cluster {
       clusterEntity.setUpgradeEntity(upgradeEntity);
       clusterDAO.merge(clusterEntity);
     } catch (RollbackException e) {
-      String msg = "Unable to set upgrade entiry " + upgradeEntity + " for 
cluster "
-        + getClusterName();
-      LOG.warn(msg);
-      throw new AmbariException(msg, e);
+      throw new AmbariException("Unable to update the associated upgrade with 
the cluster", e);
     }
   }
 
@@ -3362,11 +3326,9 @@ public class ClusterImpl implements Cluster {
    */
   @Override
   public boolean isUpgradeSuspended() {
-    UpgradeEntity lastUpgradeItemForCluster = 
upgradeDAO.findLastUpgradeForCluster(clusterId,
-        Direction.UPGRADE);
-
-    if (null != lastUpgradeItemForCluster) {
-      return lastUpgradeItemForCluster.isSuspended();
+    UpgradeEntity upgrade = getUpgradeInProgress();
+    if (null != upgrade) {
+      return upgrade.isSuspended();
     }
 
     return false;

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/main/java/org/apache/ambari/server/state/services/RetryUpgradeActionService.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/state/services/RetryUpgradeActionService.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/state/services/RetryUpgradeActionService.java
index 6d960c3..584ce98 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/state/services/RetryUpgradeActionService.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/state/services/RetryUpgradeActionService.java
@@ -173,7 +173,7 @@ public class RetryUpgradeActionService extends 
AbstractScheduledService {
     }
 
     // May be null, and either upgrade or downgrade
-    UpgradeEntity currentUpgrade = cluster.getUpgradeEntity();
+    UpgradeEntity currentUpgrade = cluster.getUpgradeInProgress();
     if (currentUpgrade == null) {
       LOG.debug("There is no active stack upgrade in progress. Skip retrying 
failed tasks.");
       return null;

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
index 3e70989..999b7a7 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
@@ -618,7 +618,7 @@ public class UpgradeResourceProviderTest {
     upgradeEntity.setToVersion("2.2.2.2");
     upgradeEntity.setUpgradePackage("upgrade_test");
     upgradeEntity.setUpgradeType(UpgradeType.ROLLING);
-    upgradeEntity.setRequestId(2L);
+    upgradeEntity.setRequestEntity(requestEntity);
 
     upgradeDao.create(upgradeEntity);
     upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
@@ -1608,7 +1608,7 @@ public class UpgradeResourceProviderTest {
     Request request = 
PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
     upgradeResourceProvider.createResources(request);
 
-    List<StageEntity> stages = 
stageDao.findByRequestId(cluster.getUpgradeEntity().getRequestId());
+    List<StageEntity> stages = 
stageDao.findByRequestId(cluster.getUpgradeInProgress().getRequestId());
     Assert.assertEquals(3, stages.size());
 
     long expectedStageId = 1L;

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeSummaryResourceProviderTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeSummaryResourceProviderTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeSummaryResourceProviderTest.java
index 19dbb44..bee8983 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeSummaryResourceProviderTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeSummaryResourceProviderTest.java
@@ -202,10 +202,7 @@ public class UpgradeSummaryResourceProviderTest {
     ServiceComponentHostEvent event = new 
ServiceComponentHostOpInProgressEvent("ZOOKEEPER_SERVER", "h1", 1L);
     ServiceComponentHostEventWrapper eventWrapper = new 
ServiceComponentHostEventWrapper(event);
 
-    RequestEntity requestEntity = new RequestEntity();
-    requestEntity.setRequestId(upgradeRequestId);
-    requestEntity.setClusterId(cluster.getClusterId());
-    requestDAO.create(requestEntity);
+    RequestEntity requestEntity = requestDAO.findByPK(upgradeRequestId);
 
     // Create the stage and add it to the request
     StageEntity stageEntity = new StageEntity();
@@ -269,8 +266,15 @@ public class UpgradeSummaryResourceProviderTest {
     Set<Resource> resources = 
upgradeSummaryResourceProvider.getResources(requestResource, p1And2);
     assertEquals(0, resources.size());
 
+    RequestEntity requestEntity = new RequestEntity();
+    requestEntity.setRequestId(1L);
+    requestEntity.setClusterId(cluster.getClusterId());
+    requestEntity.setStatus(HostRoleStatus.PENDING);
+    requestEntity.setStages(new ArrayList<StageEntity>());
+    requestDAO.create(requestEntity);
+
     UpgradeEntity upgrade = new UpgradeEntity();
-    upgrade.setRequestId(upgradeRequestId);
+    upgrade.setRequestEntity(requestEntity);
     upgrade.setClusterId(cluster.getClusterId());
     upgrade.setId(1L);
     upgrade.setUpgradePackage("some-name");
@@ -298,7 +302,7 @@ public class UpgradeSummaryResourceProviderTest {
     
Assert.assertNull(r.getPropertyValue(UpgradeSummaryResourceProvider.UPGRADE_SUMMARY_FAIL_REASON));
 
     // Case 4: Append a failed task to the Upgrade. Resource should have a 
failed reason.
-    RequestEntity requestEntity = requestDAO.findByPK(upgradeRequestId);
+    requestEntity = requestDAO.findByPK(upgradeRequestId);
     HostEntity h1 = hostDAO.findByName("h1");
 
     StageEntity nextStage = new StageEntity();

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
index bd9a340..7b8b68a 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
@@ -248,7 +248,7 @@ public class StackVersionListenerTest extends 
EasyMockSupport {
   public void 
testSetUpgradeStateToCompleteWhenHostHasVersionMismatchAndNewVersionIsEqualToComponentDesiredVersionAndClusterUpgradeIsInProgress()
 {
     expect(sch.getVersion()).andReturn(VALID_PREVIOUS_VERSION);
     expect(sch.getUpgradeState()).andReturn(UpgradeState.VERSION_MISMATCH);
-    expect(cluster.getUpgradeEntity()).andReturn(DUMMY_UPGRADE_ENTITY);
+    expect(cluster.getUpgradeInProgress()).andReturn(DUMMY_UPGRADE_ENTITY);
     
expect(serviceComponent.getDesiredVersion()).andStubReturn(VALID_NEW_VERSION);
     expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
     sch.setUpgradeState(UpgradeState.COMPLETE);

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UpgradeDAOTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UpgradeDAOTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UpgradeDAOTest.java
index 636108e..9e47e4d 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UpgradeDAOTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UpgradeDAOTest.java
@@ -33,7 +33,6 @@ import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
 import org.apache.ambari.server.orm.entities.RequestEntity;
 import org.apache.ambari.server.orm.entities.StageEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
@@ -51,8 +50,7 @@ import com.google.inject.Guice;
 import com.google.inject.Injector;
 
 /**
- * Tests {@link AlertDefinitionDAO} for interacting with
- * {@link AlertDefinitionEntity}.
+ * Tests {@link UpgradeDAO} for interacting with {@link UpgradeEntity}.
  */
 public class UpgradeDAOTest {
 
@@ -78,10 +76,17 @@ public class UpgradeDAOTest {
     helper = injector.getInstance(OrmTestHelper.class);
     clusterId = helper.createCluster();
 
+    RequestEntity requestEntity = new RequestEntity();
+    requestEntity.setRequestId(99L);
+    requestEntity.setClusterId(clusterId.longValue());
+    requestEntity.setStatus(HostRoleStatus.PENDING);
+    requestEntity.setStages(new ArrayList<StageEntity>());
+    requestDAO.create(requestEntity);
+
     // create upgrade entities
     UpgradeEntity entity = new UpgradeEntity();
     entity.setClusterId(clusterId.longValue());
-    entity.setRequestId(Long.valueOf(1));
+    entity.setRequestEntity(requestEntity);
     entity.setFromVersion("");
     entity.setToVersion("");
     entity.setUpgradeType(UpgradeType.ROLLING);
@@ -159,7 +164,7 @@ public class UpgradeDAOTest {
     entity1.setId(11L);
     entity1.setClusterId(clusterId.longValue());
     entity1.setDirection(Direction.UPGRADE);
-    entity1.setRequestId(1L);
+    entity1.setRequestEntity(requestEntity);
     entity1.setFromVersion("2.2.0.0-1234");
     entity1.setToVersion("2.3.0.0-4567");
     entity1.setUpgradeType(UpgradeType.ROLLING);
@@ -170,7 +175,7 @@ public class UpgradeDAOTest {
     entity2.setId(22L);
     entity2.setClusterId(clusterId.longValue());
     entity2.setDirection(Direction.DOWNGRADE);
-    entity2.setRequestId(1L);
+    entity2.setRequestEntity(requestEntity);
     entity2.setFromVersion("2.3.0.0-4567");
     entity2.setToVersion("2.2.0.0-1234");
     entity2.setUpgradeType(UpgradeType.ROLLING);
@@ -181,7 +186,7 @@ public class UpgradeDAOTest {
     entity3.setId(33L);
     entity3.setClusterId(clusterId.longValue());
     entity3.setDirection(Direction.UPGRADE);
-    entity3.setRequestId(1L);
+    entity3.setRequestEntity(requestEntity);
     entity3.setFromVersion("2.2.0.0-1234");
     entity3.setToVersion("2.3.1.1-4567");
     entity3.setUpgradeType(UpgradeType.ROLLING);
@@ -211,7 +216,7 @@ public class UpgradeDAOTest {
     upgradeEntity.setId(11L);
     upgradeEntity.setClusterId(clusterId.longValue());
     upgradeEntity.setDirection(Direction.UPGRADE);
-    upgradeEntity.setRequestId(1L);
+    upgradeEntity.setRequestEntity(requestEntity);
     upgradeEntity.setFromVersion("2.2.0.0-1234");
     upgradeEntity.setToVersion("2.3.0.0-4567");
     upgradeEntity.setUpgradeType(UpgradeType.ROLLING);

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
index a17c40d..c6ee875 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
@@ -980,7 +980,7 @@ public class UpgradeActionTest {
     UpgradeEntity upgradeEntity = new UpgradeEntity();
     upgradeEntity.setId(1L);
     upgradeEntity.setClusterId(cluster.getClusterId());
-    upgradeEntity.setRequestId(requestEntity.getRequestId());
+    upgradeEntity.setRequestEntity(requestEntity);
     upgradeEntity.setUpgradePackage("");
     upgradeEntity.setFromVersion(sourceRepo);
     upgradeEntity.setToVersion(targetRepo);

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
index 5383113..8e8bed3 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
@@ -23,12 +23,14 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.H2DatabaseCleaner;
+import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.ServiceComponentResponse;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
@@ -38,15 +40,18 @@ import 
org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO;
 import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
+import org.apache.ambari.server.orm.dao.RequestDAO;
 import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
 import org.apache.ambari.server.orm.dao.UpgradeDAO;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
+import org.apache.ambari.server.orm.entities.RequestEntity;
 import 
org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentHistoryEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentVersionEntity;
+import org.apache.ambari.server.orm.entities.StageEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.state.stack.upgrade.Direction;
 import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
@@ -643,6 +648,14 @@ public class ServiceComponentTest {
    * @return
    */
   private UpgradeEntity createUpgradeEntity(String fromVersion, String 
toVersion) {
+    RequestDAO requestDAO = injector.getInstance(RequestDAO.class);
+    RequestEntity requestEntity = new RequestEntity();
+    requestEntity.setRequestId(99L);
+    requestEntity.setClusterId(cluster.getClusterId());
+    requestEntity.setStatus(HostRoleStatus.PENDING);
+    requestEntity.setStages(new ArrayList<StageEntity>());
+    requestDAO.create(requestEntity);
+
     UpgradeDAO upgradeDao = injector.getInstance(UpgradeDAO.class);
     UpgradeEntity upgradeEntity = new UpgradeEntity();
     upgradeEntity.setClusterId(cluster.getClusterId());
@@ -651,7 +664,7 @@ public class ServiceComponentTest {
     upgradeEntity.setToVersion(toVersion);
     upgradeEntity.setUpgradePackage("upgrade_test");
     upgradeEntity.setUpgradeType(UpgradeType.ROLLING);
-    upgradeEntity.setRequestId(1L);
+    upgradeEntity.setRequestEntity(requestEntity);
 
     upgradeDao.create(upgradeEntity);
     List<UpgradeEntity> upgrades = 
upgradeDao.findUpgrades(cluster.getClusterId());

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/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 222f2b0..d4dbdc8 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
@@ -540,7 +540,7 @@ public class AlertStateChangedEventTest extends 
EasyMockSupport {
     Cluster cluster = createMock(Cluster.class);
 
     
EasyMock.expect(clusters.getClusterById(EasyMock.anyLong())).andReturn(cluster).atLeastOnce();
-    EasyMock.expect(cluster.getUpgradeEntity()).andReturn(null).anyTimes();
+    EasyMock.expect(cluster.getUpgradeInProgress()).andReturn(null).anyTimes();
     EasyMock.expect(cluster.isUpgradeSuspended()).andReturn(false).anyTimes();
   }
 
@@ -556,7 +556,7 @@ public class AlertStateChangedEventTest extends 
EasyMockSupport {
     EasyMock.reset(clusters);
 
     
EasyMock.expect(clusters.getClusterById(EasyMock.anyLong())).andReturn(cluster).atLeastOnce();
-    EasyMock.expect(cluster.getUpgradeEntity()).andReturn(new 
UpgradeEntity()).anyTimes();
+    EasyMock.expect(cluster.getUpgradeInProgress()).andReturn(new 
UpgradeEntity()).anyTimes();
     EasyMock.expect(cluster.isUpgradeSuspended()).andReturn(false).anyTimes();
   }
 
@@ -572,7 +572,7 @@ public class AlertStateChangedEventTest extends 
EasyMockSupport {
     EasyMock.reset(clusters);
 
     
EasyMock.expect(clusters.getClusterById(EasyMock.anyLong())).andReturn(cluster).atLeastOnce();
-    EasyMock.expect(cluster.getUpgradeEntity()).andReturn(null).anyTimes();
+    EasyMock.expect(cluster.getUpgradeInProgress()).andReturn(null).anyTimes();
     EasyMock.expect(cluster.isUpgradeSuspended()).andReturn(true).anyTimes();
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/fe0aa448/ambari-server/src/test/java/org/apache/ambari/server/state/services/RetryUpgradeActionServiceTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/state/services/RetryUpgradeActionServiceTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/state/services/RetryUpgradeActionServiceTest.java
index 2c0b507..f52f007 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/state/services/RetryUpgradeActionServiceTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/state/services/RetryUpgradeActionServiceTest.java
@@ -285,7 +285,7 @@ public class RetryUpgradeActionServiceTest {
 
     UpgradeEntity upgrade = new UpgradeEntity();
     upgrade.setId(1L);
-    upgrade.setRequestId(upgradeRequestId);
+    upgrade.setRequestEntity(requestEntity);
     upgrade.setClusterId(cluster.getClusterId());
     upgrade.setUpgradePackage("some-name");
     upgrade.setUpgradeType(UpgradeType.ROLLING);

Reply via email to