You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by nc...@apache.org on 2017/05/09 16:25:10 UTC

[5/6] ambari git commit: AMBARI-20957. Remove cluster_version use (ncole)

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java
index b4ddf09..c0a074f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java
@@ -20,10 +20,8 @@ package org.apache.ambari.server.events.listeners.upgrade;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.concurrent.locks.Lock;
 
 import org.apache.ambari.server.AmbariException;
@@ -38,7 +36,7 @@ import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 import org.apache.ambari.server.logging.LockFactory;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
+import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
@@ -87,6 +85,9 @@ public class HostVersionOutOfSyncListener {
   @Inject
   private Provider<AmbariMetaInfo> ami;
 
+  @Inject
+  private Provider<RepositoryVersionDAO> repositoryVersionDAO;
+
   /**
    * The publisher may be an asynchronous, multi-threaded one, so to avoid the (rare, but possible) case
    * of both an Install and Uninstall event occurring at the same time, we use a Lock.
@@ -140,7 +141,6 @@ public class HostVersionOutOfSyncListener {
           case NOT_REQUIRED:
             hostVersionEntity.setState(RepositoryVersionState.OUT_OF_SYNC);
             hostVersionDAO.get().merge(hostVersionEntity);
-            cluster.recalculateClusterVersionState(hostVersionEntity.getRepositoryVersion());
             break;
           default:
             break;
@@ -224,7 +224,7 @@ public class HostVersionOutOfSyncListener {
 
     try {
       Cluster cluster = clusters.get().getClusterById(event.getClusterId());
-      Set<RepositoryVersionEntity> changedRepositoryVersions = new HashSet<>();
+
       Map<String, ServiceComponent> serviceComponents = cluster.getService(event.getServiceName()).getServiceComponents();
       // Determine hosts that become OUT_OF_SYNC when adding components for new service
       Map<String, List<ServiceComponent>> affectedHosts =
@@ -262,18 +262,20 @@ public class HostVersionOutOfSyncListener {
           if (hostVersionEntity.getState().equals(RepositoryVersionState.INSTALLED)) {
             hostVersionEntity.setState(RepositoryVersionState.OUT_OF_SYNC);
             hostVersionDAO.get().merge(hostVersionEntity);
-            changedRepositoryVersions.add(repositoryVersion);
           }
         }
       }
-      for (RepositoryVersionEntity repositoryVersion : changedRepositoryVersions) {
-        cluster.recalculateClusterVersionState(repositoryVersion);
-      }
+
     } catch (AmbariException e) {
       LOG.error("Can not update hosts about out of sync", e);
     }
   }
 
+  /**
+   * When hosts are added, add a host_version record for every repo_version in the database.
+   *
+   * @param event the add event
+   */
   @Subscribe
   @Transactional
   public void onHostEvent(HostsAddedEvent event) {
@@ -281,37 +283,28 @@ public class HostVersionOutOfSyncListener {
       LOG.debug(event.toString());
     }
 
-    try {
-      Cluster cluster = clusters.get().getClusterById(event.getClusterId());
+    List<RepositoryVersionEntity> repos = repositoryVersionDAO.get().findAllDefinitions();
 
-      Collection<ClusterVersionEntity> allClusterVersions = cluster.getAllClusterVersions();
-      for (ClusterVersionEntity clusterVersion : allClusterVersions) {
-        if (clusterVersion.getState() != RepositoryVersionState.CURRENT) { // Current version is taken care of automatically
-          RepositoryVersionEntity repositoryVersion = clusterVersion.getRepositoryVersion();
-          for (String hostName : event.getHostNames()) {
-            HostEntity hostEntity = hostDAO.get().findByName(hostName);
-            HostVersionEntity missingHostVersion = new HostVersionEntity(hostEntity,
-              repositoryVersion, RepositoryVersionState.OUT_OF_SYNC);
-
-            LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
-              missingHostVersion.getHostName(), missingHostVersion.getState(),
-              missingHostVersion.getRepositoryVersion().getVersion(), missingHostVersion.getRepositoryVersion().getId());
-            hostVersionDAO.get().create(missingHostVersion);
-          }
-          cluster.recalculateClusterVersionState(repositoryVersion);
-        }
+    for (String hostName : event.getHostNames()) {
+      HostEntity hostEntity = hostDAO.get().findByName(hostName);
+
+      for (RepositoryVersionEntity repositoryVersion : repos) {
+
+        // we don't have the knowledge yet to know if we need the record
+        HostVersionEntity missingHostVersion = new HostVersionEntity(hostEntity,
+            repositoryVersion, RepositoryVersionState.NOT_REQUIRED);
+
+        LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
+          missingHostVersion.getHostName(), missingHostVersion.getState(),
+          missingHostVersion.getRepositoryVersion().getVersion(), missingHostVersion.getRepositoryVersion().getId());
+
+        hostVersionDAO.get().create(missingHostVersion);
       }
-    } catch (AmbariException e) {
-      LOG.error("Can not update hosts about out of sync", e);
     }
   }
 
   /**
-   * Recalculates the cluster repo version state when a host is removed. If
-   * hosts are removed during an upgrade, the remaining hosts will all be in the
-   * {@link RepositoryVersionState#INSTALLED} state, but the cluster will never
-   * transition into this state. This is because when the host is removed, a
-   * recalculation must happen.
+   * Host repo_version entities are removed via cascade.
    *
    * @param event
    *          the removal event.
@@ -322,36 +315,6 @@ public class HostVersionOutOfSyncListener {
     if (LOG.isDebugEnabled()) {
       LOG.debug(event.toString());
     }
-
-    try {
-      Set<Cluster> clusters = event.getClusters();
-      for (Cluster cluster : clusters) {
-        Collection<ClusterVersionEntity> allClusterVersions = cluster.getAllClusterVersions();
-
-        for (ClusterVersionEntity clusterVersion : allClusterVersions) {
-          RepositoryVersionState repositoryVersionState = clusterVersion.getState();
-
-          // the CURRENT/INSTALLED states should not be affected by a host
-          // removal - if it's already current then removing a host will never
-          // make it not CURRENT or not INSTALLED
-          switch (repositoryVersionState) {
-            case CURRENT:
-            case INSTALLED:
-              continue;
-            default:
-              break;
-          }
-
-          RepositoryVersionEntity repositoryVersion = clusterVersion.getRepositoryVersion();
-          cluster.recalculateClusterVersionState(repositoryVersion);
-        }
-      }
-
-    } catch (AmbariException ambariException) {
-      LOG.error(
-          "Unable to recalculate the cluster repository version state when a host was removed",
-          ambariException);
-    }
   }
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/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 33c622f..1cedea8 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
@@ -43,9 +43,7 @@ import com.google.inject.Singleton;
  * The {@link StackVersionListener} class handles the propagation of versions
  * advertised by the {@link org.apache.ambari.server.state.ServiceComponentHost}
  * that bubble up to the
- * {@link org.apache.ambari.server.orm.entities.HostVersionEntity} and
- * eventually the
- * {@link org.apache.ambari.server.orm.entities.ClusterVersionEntity}
+ * {@link org.apache.ambari.server.orm.entities.HostVersionEntity}
  */
 @Singleton
 @EagerSingleton
@@ -167,10 +165,7 @@ public class StackVersionListener {
    * @throws AmbariException
    */
   private void bootstrapVersion(Cluster cluster, ServiceComponentHost sch) throws AmbariException {
-    RepositoryVersionEntity repoVersion = sch.recalculateHostVersionState();
-    if (null != repoVersion) {
-      cluster.recalculateClusterVersionState(repoVersion);
-    }
+    sch.recalculateHostVersionState();
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterVersionDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterVersionDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterVersionDAO.java
deleted file mode 100644
index 1bcca60..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterVersionDAO.java
+++ /dev/null
@@ -1,233 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.ambari.server.orm.dao;
-
-import java.util.List;
-
-import javax.persistence.EntityManager;
-import javax.persistence.NoResultException;
-import javax.persistence.NonUniqueResultException;
-import javax.persistence.TypedQuery;
-
-import org.apache.ambari.server.orm.RequiresSession;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
-import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
-import org.apache.ambari.server.state.RepositoryVersionState;
-import org.apache.ambari.server.state.StackId;
-
-import com.google.inject.Singleton;
-import com.google.inject.persist.Transactional;
-
-/**
- * The {@link ClusterVersionDAO} class manages the {@link ClusterVersionEntity} instances associated with a cluster.
- * Each cluster can have multiple stack versions {@link org.apache.ambari.server.state.RepositoryVersionState#INSTALLED},
- * exactly one stack version that is {@link org.apache.ambari.server.state.RepositoryVersionState#CURRENT}, and at most one
- * stack version that is {@link org.apache.ambari.server.state.RepositoryVersionState#UPGRADING}.
- */
-@Singleton
-public class ClusterVersionDAO extends CrudDAO<ClusterVersionEntity, Long>{
-  /**
-   * Constructor.
-   */
-  public ClusterVersionDAO() {
-    super(ClusterVersionEntity.class);
-  }
-
-  /**
-   * Retrieve all of the cluster versions for the given stack and version.
-   *
-   * @param stackName
-   *          the stack name (for example "HDP")
-   * @param stackVersion
-   *          the stack version (for example "2.2")
-   * @param version
-   *          Repository version (e.g., 2.2.0.1-995)
-   * @return Return a list of cluster versions that match the stack and version.
-   */
-  @RequiresSession
-  public List<ClusterVersionEntity> findByStackAndVersion(String stackName,
-      String stackVersion, String version) {
-    final TypedQuery<ClusterVersionEntity> query = entityManagerProvider.get().createNamedQuery("clusterVersionByStackVersion", ClusterVersionEntity.class);
-    query.setParameter("stackName", stackName);
-    query.setParameter("stackVersion", stackVersion);
-    query.setParameter("version", version);
-
-    return daoUtils.selectList(query);
-  }
-
-  /**
-   * Get the cluster version for the given cluster name, stack name, and stack
-   * version.
-   *
-   * @param clusterName
-   *          Cluster name
-   * @param stackId
-   *          Stack id (e.g., HDP-2.2)
-   * @param version
-   *          Repository version (e.g., 2.2.0.1-995)
-   * @return Return all of the cluster versions associated with the given
-   *         cluster.
-   */
-  @RequiresSession
-  public ClusterVersionEntity findByClusterAndStackAndVersion(
-      String clusterName, StackId stackId, String version) {
-    final TypedQuery<ClusterVersionEntity> query = entityManagerProvider.get()
-        .createNamedQuery("clusterVersionByClusterAndStackAndVersion", ClusterVersionEntity.class);
-    query.setParameter("clusterName", clusterName);
-    query.setParameter("stackName", stackId.getStackName());
-    query.setParameter("stackVersion", stackId.getStackVersion());
-    query.setParameter("version", version);
-
-    return daoUtils.selectSingle(query);
-  }
-
-  /**
-   * Retrieve all of the cluster versions for the given cluster.
-   *
-   * @param clusterName Cluster name
-   * @return Return all of the cluster versions associated with the given cluster.
-   */
-  @RequiresSession
-  public List<ClusterVersionEntity> findByCluster(String clusterName) {
-    final TypedQuery<ClusterVersionEntity> query = entityManagerProvider.get()
-        .createNamedQuery("clusterVersionByCluster", ClusterVersionEntity.class);
-    query.setParameter("clusterName", clusterName);
-
-    return daoUtils.selectList(query);
-  }
-
-  /**
-   * Retrieve the single cluster version whose state is {@link org.apache.ambari.server.state.RepositoryVersionState#CURRENT}, of which there should be exactly one at all times
-   * for the given cluster.
-   *
-   * @param clusterName Cluster name
-   * @return Returns the single cluster version for this cluster whose state is {@link org.apache.ambari.server.state.RepositoryVersionState#CURRENT}, or {@code null} otherwise.
-   */
-  @RequiresSession
-  public ClusterVersionEntity findByClusterAndStateCurrent(String clusterName) {
-    final TypedQuery<ClusterVersionEntity> query = entityManagerProvider.get()
-        .createNamedQuery("clusterVersionByClusterAndState", ClusterVersionEntity.class);
-    query.setParameter("clusterName", clusterName);
-    query.setParameter("state", RepositoryVersionState.CURRENT);
-
-    try {
-      List results = query.getResultList();
-      if (results.isEmpty()) {
-        return null;
-      } else {
-        if (results.size() == 1) {
-          return (ClusterVersionEntity) results.get(0);
-        }
-      }
-      throw new NonUniqueResultException();
-    } catch (NoResultException ignored) {
-      return null;
-    }
-  }
-
-  /**
-   * Retrieve all of the cluster versions for the cluster with the given name and a state.
-   *
-   * @param clusterName Cluster name
-   * @param state Cluster version state
-   * @return Returns a list of cluster versions for the given cluster and a state.
-   */
-  @RequiresSession
-  public List<ClusterVersionEntity> findByClusterAndState(String clusterName, RepositoryVersionState state) {
-    final TypedQuery<ClusterVersionEntity> query = entityManagerProvider.get()
-        .createNamedQuery("clusterVersionByClusterAndState", ClusterVersionEntity.class);
-    query.setParameter("clusterName", clusterName);
-    query.setParameter("state", state);
-
-    return daoUtils.selectList(query);
-  }
-
-  /**
-   * Construct a Cluster Version. Additionally this will update parent connection relations without
-   * forcing refresh of parent entity
-   * @param entity entity to create
-   */
-  @Override
-  @Transactional
-  public void create(ClusterVersionEntity entity) throws IllegalArgumentException {
-    // check if repository version is not missing, to avoid NPE
-    if (entity.getRepositoryVersion() == null) {
-      throw new IllegalArgumentException("RepositoryVersion argument is not set for the entity");
-    }
-
-    super.create(entity);
-    entity.getRepositoryVersion().updateClusterVersionEntityRelation(entity);
-  }
-
-  /**
-   * Construct a Cluster Version and return it. This is primarily used to be able to construct the object and mock
-   * the function call.
-   * @param cluster Cluster
-   * @param repositoryVersion Repository Version
-   * @param state Initial State
-   * @param startTime Start Time
-   * @param endTime End Time
-   * @param userName Username, such as "admin"
-   * @return Return new ClusterVersion object.
-   */
-  @Transactional
-  public ClusterVersionEntity create(ClusterEntity cluster, RepositoryVersionEntity repositoryVersion,
-                                     RepositoryVersionState state, long startTime, long endTime, String userName) {
-    ClusterVersionEntity clusterVersionEntity = new ClusterVersionEntity(cluster,
-        repositoryVersion, state, startTime, endTime, userName);
-    this.create(clusterVersionEntity);
-    return clusterVersionEntity;
-  }
-
-  /**
-   * Updates the cluster version's existing CURRENT record to the INSTALLED, and the target
-   * becomes CURRENT.  This method invokes {@code clear()} on the entity manager to force entities to be refreshed.
-   * @param clusterId the cluster
-   * @param target    the repo version that will be marked as CURRENT
-   * @param current   the cluster's current record to be marked INSTALLED
-   */
-  @Transactional
-  public void updateVersions(Long clusterId, RepositoryVersionEntity target, RepositoryVersionEntity current) {
-    // !!! first update target to be current
-    StringBuilder sb = new StringBuilder("UPDATE ClusterVersionEntity cve");
-    sb.append(" SET cve.state = ?1 ");
-    sb.append(" WHERE cve.clusterId = ?2");
-    sb.append(" AND cve.repositoryVersion = ?3");
-
-    EntityManager em = entityManagerProvider.get();
-
-    TypedQuery<Long> query = em.createQuery(sb.toString(), Long.class);
-    daoUtils.executeUpdate(query, RepositoryVersionState.CURRENT, clusterId, target);
-
-    // !!! then move existing current to installed
-    sb = new StringBuilder("UPDATE ClusterVersionEntity cve");
-    sb.append(" SET cve.state = ?1 ");
-    sb.append(" WHERE cve.clusterId = ?2");
-    sb.append(" AND cve.repositoryVersion = ?3");
-    sb.append(" AND cve.state = ?4");
-
-    query = em.createQuery(sb.toString(), Long.class);
-    daoUtils.executeUpdate(query, RepositoryVersionState.INSTALLED, clusterId, current,
-        RepositoryVersionState.CURRENT);
-
-    em.clear();
-  }
-
-
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostVersionDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostVersionDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostVersionDAO.java
index 3871b67..d367aa0 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostVersionDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostVersionDAO.java
@@ -297,6 +297,28 @@ public class HostVersionDAO extends CrudDAO<HostVersionEntity, Long> {
     return daoUtils.selectList(query);
   }
 
+  /**
+   * Gets all host version entities that are of the given states
+   *
+   * @param repositoryVersion
+   *          the repository (not {@code null})
+   * @param states
+   *          the states
+   * @return the host versions
+   */
+  @RequiresSession
+  public List<HostVersionEntity> findByRepositoryAndStates(RepositoryVersionEntity repositoryVersion,
+      Collection<RepositoryVersionState> states) {
+
+    TypedQuery<HostVersionEntity> query = entityManagerProvider.get().createNamedQuery(
+        "hostVersionByRepositoryAndStates", HostVersionEntity.class);
+
+    query.setParameter("repositoryVersion", repositoryVersion);
+    query.setParameter("states", states);
+
+    return daoUtils.selectList(query);
+  }
+
   @Transactional
   public void removeByHostName(String hostName) {
     Collection<HostVersionEntity> hostVersions = findByHost(hostName);

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/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 527fd7a..0f8f336 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
@@ -20,7 +20,6 @@ package org.apache.ambari.server.orm.entities;
 
 import static org.apache.commons.lang.StringUtils.defaultString;
 
-import java.util.ArrayList;
 import java.util.Collection;
 
 import javax.persistence.Basic;
@@ -128,9 +127,6 @@ public class ClusterEntity {
   private Collection<AlertDefinitionEntity> alertDefinitionEntities;
 
   @OneToMany(mappedBy = "clusterEntity", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
-  private Collection<ClusterVersionEntity> clusterVersionEntities;
-
-  @OneToMany(mappedBy = "clusterEntity", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
   private Collection<WidgetEntity> widgetEntities;
 
   @OneToMany(mappedBy = "clusterEntity", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
@@ -326,19 +322,6 @@ public class ClusterEntity {
     return alertDefinitionEntities;
   }
 
-  public Collection<ClusterVersionEntity> getClusterVersionEntities() {
-    return clusterVersionEntities;
-  }
-
-  public void setClusterVersionEntities(Collection<ClusterVersionEntity> clusterVersionEntities) { this.clusterVersionEntities = clusterVersionEntities; }
-
-  public void addClusterVersionEntity(ClusterVersionEntity clusterVersionEntity) {
-    if (clusterVersionEntities == null) {
-      clusterVersionEntities = new ArrayList<>();
-    }
-    clusterVersionEntities.add(clusterVersionEntity);
-  }
-
   /**
    * Get the admin resource entity.
    *

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java
deleted file mode 100644
index f1867b4..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java
+++ /dev/null
@@ -1,217 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.orm.entities;
-
-import static org.apache.commons.lang.StringUtils.defaultString;
-
-import javax.persistence.Basic;
-import javax.persistence.Column;
-import javax.persistence.Entity;
-import javax.persistence.EnumType;
-import javax.persistence.Enumerated;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
-import javax.persistence.JoinColumn;
-import javax.persistence.ManyToOne;
-import javax.persistence.NamedQueries;
-import javax.persistence.NamedQuery;
-import javax.persistence.Table;
-import javax.persistence.TableGenerator;
-
-import org.apache.ambari.server.state.RepositoryVersionState;
-
-@Table(name = "cluster_version")
-@Entity
-@TableGenerator(name = "cluster_version_id_generator",
-    table = "ambari_sequences", pkColumnName = "sequence_name", valueColumnName = "sequence_value"
-    , pkColumnValue = "cluster_version_id_seq"
-    , initialValue = 0
-)
-@NamedQueries({
-    @NamedQuery(name = "clusterVersionByClusterAndStackAndVersion", query =
-        "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion JOIN clusterVersion.clusterEntity cluster " +
-        "WHERE cluster.clusterName=:clusterName AND clusterVersion.repositoryVersion.stack.stackName=:stackName AND clusterVersion.repositoryVersion.stack.stackVersion=:stackVersion AND clusterVersion.repositoryVersion.version=:version"),
-    @NamedQuery(name = "clusterVersionByClusterAndState", query =
-        "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion JOIN clusterVersion.clusterEntity cluster " +
-        "WHERE cluster.clusterName=:clusterName AND clusterVersion.state=:state"),
-    @NamedQuery(name = "clusterVersionByCluster", query =
-        "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion JOIN clusterVersion.clusterEntity cluster " +
-        "WHERE cluster.clusterName=:clusterName"),
-    @NamedQuery(name = "clusterVersionByStackVersion", query = "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion WHERE clusterVersion.repositoryVersion.stack.stackName=:stackName AND clusterVersion.repositoryVersion.stack.stackVersion=:stackVersion AND clusterVersion.repositoryVersion.version=:version"),
-})
-public class ClusterVersionEntity {
-
-  @Id
-  @Column(name = "id", nullable = false, insertable = true, updatable = false)
-  @GeneratedValue(strategy = GenerationType.TABLE, generator = "cluster_version_id_generator")
-  private Long id;
-
-  @Column(name = "cluster_id", nullable = false, insertable = false, updatable = false)
-  private Long clusterId;
-
-  @ManyToOne
-  @JoinColumn(name = "cluster_id", referencedColumnName = "cluster_id", nullable = false)
-  private ClusterEntity clusterEntity;
-
-  @ManyToOne
-  @JoinColumn(name = "repo_version_id", referencedColumnName = "repo_version_id", nullable = false)
-  private RepositoryVersionEntity repositoryVersion;
-
-  @Column(name = "state", nullable = false, insertable = true, updatable = true)
-  @Enumerated(value = EnumType.STRING)
-  private RepositoryVersionState state = RepositoryVersionState.CURRENT;
-
-  @Basic
-  @Column(name = "start_time", nullable = false, insertable = true, updatable = true)
-  private Long startTime = System.currentTimeMillis();
-
-  @Basic
-  @Column(name = "end_time", insertable = true, updatable = true)
-  private Long endTime;
-
-  @Basic
-  @Column(name = "user_name", insertable = true, updatable = true)
-  private String userName = "";
-
-  /**
-   * Empty constructor primarily used by unit tests.
-   */
-  public ClusterVersionEntity() {
-  }
-
-  /**
-   * Full constructor that doesn't have the endTime
-   * @param cluster Cluster entity
-   * @param repositoryVersion repository version
-   * @param state Cluster version state
-   * @param startTime Time the cluster version reached its first state
-   * @param userName User who performed the action
-   */
-  public ClusterVersionEntity(ClusterEntity cluster, RepositoryVersionEntity repositoryVersion, RepositoryVersionState state, long startTime, String userName) {
-    clusterId = cluster.getClusterId();
-    this.repositoryVersion = repositoryVersion;
-    clusterEntity = cluster;
-    this.state = state;
-    this.startTime = startTime;
-    this.userName = userName;
-  }
-
-  /**
-   * Full constructor that does have the endTime
-   * @param cluster Cluster entity
-   * @param repositoryVersion repository version
-   * @param state Cluster version state
-   * @param startTime Time the cluster version reached its first state
-   * @param endTime Time the cluster version finalized its state
-   * @param userName User who performed the action
-   */
-  public ClusterVersionEntity(ClusterEntity cluster, RepositoryVersionEntity repositoryVersion, RepositoryVersionState state, long startTime, long endTime, String userName) {
-    this(cluster, repositoryVersion, state, startTime, userName);
-    this.endTime = endTime;
-  }
-
-  public Long getId() {
-    return id;
-  }
-
-  public void setId(Long id) {
-    this.id = id;
-  }
-
-  public Long getClusterId() {
-    return clusterId;
-  }
-
-  public void setClusterId(Long clusterId) {
-    this.clusterId = clusterId;
-  }
-
-  public ClusterEntity getClusterEntity() {
-    return clusterEntity;
-  }
-
-  public void setClusterEntity(ClusterEntity clusterEntity) {
-    this.clusterEntity = clusterEntity;
-  }
-
-  public RepositoryVersionState getState() {
-    return state;
-  }
-
-  public void setState(RepositoryVersionState state) {
-    this.state = state;
-  }
-
-  public Long getStartTime() { return startTime; }
-
-  public void setStartTime(Long startTime) { this.startTime = startTime; }
-
-  public Long getEndTime() { return endTime; }
-
-  public void setEndTime(Long endTime) { this.endTime = endTime; }
-
-  public String getUserName() { return defaultString(userName); }
-
-  public void setUserName(String userName) { this.userName = userName; }
-
-  public void setRepositoryVersion(RepositoryVersionEntity repositoryVersion) {
-    this.repositoryVersion = repositoryVersion;
-  }
-
-  public RepositoryVersionEntity getRepositoryVersion() {
-    return repositoryVersion;
-  }
-
-  @Override
-  public boolean equals(Object o) {
-    if (this == o) {
-      return true;
-    }
-    if (o == null || getClass() != o.getClass()) {
-      return false;
-    }
-
-    ClusterVersionEntity that = (ClusterVersionEntity) o;
-
-    if (id != that.id
-        || clusterId != that.clusterId
-        || !repositoryVersion.equals(that.repositoryVersion)
-        || !state.equals(that.state)
-        || !startTime.equals(that.startTime)
-        || !endTime.equals(that.endTime)
-        || !userName.equals(that.userName)) {
-      return false;
-    }
-
-    return true;
-  }
-
-  @Override
-  public int hashCode() {
-    int result = id !=null ? id.intValue() : 0;
-    result = 31 * result + (clusterId != null ? clusterId.hashCode() : 0);
-    result = 31 * result + (repositoryVersion != null ? repositoryVersion.hashCode() : 0);
-    result = 31 * result + (state != null ? state.hashCode() : 0);
-    result = 31 * result + (startTime != null ? startTime.hashCode() : 0);
-    result = 31 * result + (endTime != null ? endTime.hashCode() : 0);
-    result = 31 * result + (userName != null ? userName.hashCode() : 0);
-    return result;
-  }
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostVersionEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostVersionEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostVersionEntity.java
index 9be30a3..4bd6e9d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostVersionEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostVersionEntity.java
@@ -76,7 +76,10 @@ import org.apache.ambari.server.state.RepositoryVersionState;
     @NamedQuery(
         name = "findHostVersionByClusterAndRepository",
         query = "SELECT hostVersion FROM HostVersionEntity hostVersion JOIN hostVersion.hostEntity host JOIN host.clusterEntities clusters "
-            + "WHERE clusters.clusterId = :clusterId AND hostVersion.repositoryVersion = :repositoryVersion") 
+            + "WHERE clusters.clusterId = :clusterId AND hostVersion.repositoryVersion = :repositoryVersion"),
+    @NamedQuery(
+        name = "hostVersionByRepositoryAndStates",
+        query = "SELECT hostVersion FROM HostVersionEntity hostVersion WHERE hostVersion.repositoryVersion = :repositoryVersion AND hostVersion.state IN :states")
 })
 public class HostVersionEntity {
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
index f5d669e..7d6db2c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
@@ -105,10 +105,6 @@ public class RepositoryVersionEntity {
   @Column(name = "repositories")
   private String operatingSystems;
 
-
-  @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "repositoryVersion")
-  private Set<ClusterVersionEntity> clusterVersionEntities;
-
   @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "repositoryVersion")
   private Set<HostVersionEntity> hostVersionEntities;
 
@@ -159,13 +155,6 @@ public class RepositoryVersionEntity {
       version = version.substring(stackName.length() + 1);
     }
   }
-  /**
-   * Update one-to-many relation without rebuilding the whole entity
-   * @param entity many-to-one entity
-   */
-  public void updateClusterVersionEntityRelation(ClusterVersionEntity entity){
-    clusterVersionEntities.add(entity);
-  }
 
   /**
    * Update one-to-many relation without rebuilding the whole entity

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
index 6b89c02..7576e00 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
@@ -98,7 +98,7 @@ public class ServiceComponentDesiredStateEntity {
   @JoinColumn(
       name = "desired_repo_version_id",
       unique = false,
-      nullable = true,
+      nullable = false,
       insertable = true,
       updatable = true)
   private RepositoryVersionEntity desiredRepositoryVersion;

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
index a4cc757..7a39dcd 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
@@ -255,9 +255,6 @@ public class FinalizeUpgradeAction extends AbstractUpgradeServerAction {
           String.format("Finalizing the version for cluster %s.", cluster.getClusterName())).append(
               System.lineSeparator());
 
-      cluster.transitionClusterVersion(clusterDesiredStackId, version,
-          RepositoryVersionState.CURRENT);
-
       outSB.append("Creating upgrade history...").append(System.lineSeparator());
       writeComponentHistory(upgradeContext);
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/ambari-server/src/main/java/org/apache/ambari/server/stack/RepoUtil.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/stack/RepoUtil.java b/ambari-server/src/main/java/org/apache/ambari/server/stack/RepoUtil.java
index 1380f93..1c73e88 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/stack/RepoUtil.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/stack/RepoUtil.java
@@ -32,6 +32,7 @@ import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
 import org.apache.ambari.server.orm.entities.RepositoryEntity;
 import org.apache.ambari.server.state.RepositoryInfo;
 import org.apache.ambari.server.state.stack.RepositoryXml;
+import org.apache.commons.collections.CollectionUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -114,8 +115,9 @@ public class RepoUtil {
    *    service repository and will be added.
    * @param operatingSystems - A list of OperatingSystemEntity objects extracted from a RepositoryVersionEntity
    * @param stackReposByOs - Stack repositories loaded from the disk (including service repositories), grouped by os.
+   * @return {@code true} if there were added repositories
    */
-  public static void addServiceReposToOperatingSystemEntities(List<OperatingSystemEntity> operatingSystems,
+  public static boolean addServiceReposToOperatingSystemEntities(List<OperatingSystemEntity> operatingSystems,
       ListMultimap<String, RepositoryInfo> stackReposByOs) {
     Set<String> addedRepos = new HashSet<>();
     for (OperatingSystemEntity os : operatingSystems) {
@@ -128,6 +130,8 @@ public class RepoUtil {
         }
     }
     LOG.info("Added {} service repos: {}", addedRepos.size(),Iterables.toString(addedRepos));
+
+    return CollectionUtils.isNotEmpty(addedRepos);
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/ambari-server/src/main/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartup.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartup.java b/ambari-server/src/main/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartup.java
index 29af6a8..89f16d1 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartup.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartup.java
@@ -20,25 +20,21 @@ package org.apache.ambari.server.stack;
 
 import java.util.List;
 
-import javax.annotation.Nullable;
-
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
+import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
 import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
-import org.apache.ambari.server.orm.entities.RepositoryEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.RepositoryInfo;
+import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.StackInfo;
 import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Function;
 import com.google.common.collect.ListMultimap;
 import com.google.inject.Inject;
 import com.google.inject.persist.Transactional;
@@ -55,26 +51,17 @@ public class UpdateActiveRepoVersionOnStartup {
 
   private static final Logger LOG = LoggerFactory.getLogger(UpdateActiveRepoVersionOnStartup.class);
 
-
   ClusterDAO clusterDao;
-  ClusterVersionDAO clusterVersionDao;
   RepositoryVersionDAO repositoryVersionDao;
   RepositoryVersionHelper repositoryVersionHelper;
   StackManager stackManager;
 
-
-  private static final Function<RepositoryEntity, String> REPO_TO_ID = new Function<RepositoryEntity, String>() {
-    @Override  public String apply(@Nullable RepositoryEntity input) { return input.getRepositoryId(); }
-  };
-
   @Inject
   public UpdateActiveRepoVersionOnStartup(ClusterDAO clusterDao,
-      ClusterVersionDAO clusterVersionDao,
       RepositoryVersionDAO repositoryVersionDao,
       RepositoryVersionHelper repositoryVersionHelper,
       AmbariMetaInfo metaInfo) {
     this.clusterDao = clusterDao;
-    this.clusterVersionDao = clusterVersionDao;
     this.repositoryVersionDao = repositoryVersionDao;
     this.repositoryVersionHelper = repositoryVersionHelper;
     this.stackManager = metaInfo.getStackManager();
@@ -87,21 +74,20 @@ public class UpdateActiveRepoVersionOnStartup {
   @Transactional
   public void process() throws AmbariException {
     LOG.info("Updating existing repo versions with service repos.");
+
     try {
+
       List<ClusterEntity> clusters = clusterDao.findAll();
       for (ClusterEntity cluster: clusters) {
-        StackInfo stack =
-            stackManager.getStack(cluster.getDesiredStack().getStackName(), cluster.getDesiredStack().getStackVersion());
-        LOG.info("Updating existing repo versions for cluster {} on stack {}-{}",
-            cluster.getClusterName(), stack.getName(), stack.getVersion());
-        ClusterVersionEntity clusterVersion = clusterVersionDao.findByClusterAndStateCurrent(cluster.getClusterName());
-        if (null != clusterVersion) {
-          RepositoryVersionEntity repoVersion = clusterVersion.getRepositoryVersion();
-          updateRepoVersion(stack, repoVersion);
-          repositoryVersionDao.merge(repoVersion);
-        }
-        else {
-          LOG.warn("Missing cluster version for cluster {}", cluster.getClusterName());
+        for (ClusterServiceEntity service : cluster.getClusterServiceEntities()) {
+          RepositoryVersionEntity repositoryVersion = service.getServiceDesiredStateEntity().getDesiredRepositoryVersion();
+
+          StackId stackId = repositoryVersion.getStackId();
+          StackInfo stack = stackManager.getStack(stackId.getStackName(), stackId.getStackVersion());
+
+          if (updateRepoVersion(stack, repositoryVersion)) {
+            repositoryVersionDao.merge(repositoryVersion);
+          }
         }
       }
     }
@@ -112,13 +98,16 @@ public class UpdateActiveRepoVersionOnStartup {
     }
   }
 
-  private void updateRepoVersion(StackInfo stackInfo, RepositoryVersionEntity repoVersion) throws Exception {
+  private boolean updateRepoVersion(StackInfo stackInfo, RepositoryVersionEntity repoVersion) throws Exception {
     ListMultimap<String, RepositoryInfo> serviceReposByOs = stackInfo.getRepositoriesByOs();
 
     // Update repos in the JSON representation
     List<OperatingSystemEntity> operatingSystems = repoVersion.getOperatingSystems();
-    RepoUtil.addServiceReposToOperatingSystemEntities(operatingSystems, serviceReposByOs);
-    repoVersion.setOperatingSystems(repositoryVersionHelper.serializeOperatingSystemEntities(operatingSystems));
+    boolean changed = RepoUtil.addServiceReposToOperatingSystemEntities(operatingSystems, serviceReposByOs);
+    if (changed) {
+      repoVersion.setOperatingSystems(repositoryVersionHelper.serializeOperatingSystemEntities(operatingSystems));
+    }
+    return changed;
   }
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/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 88c5a59..6cefd42 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
@@ -28,7 +28,6 @@ import org.apache.ambari.server.controller.ClusterResponse;
 import org.apache.ambari.server.controller.ServiceConfigVersionResponse;
 import org.apache.ambari.server.events.ClusterConfigChangedEvent;
 import org.apache.ambari.server.metadata.RoleCommandOrder;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.PrivilegeEntity;
@@ -144,45 +143,6 @@ public interface Cluster {
   void removeServiceComponentHost(ServiceComponentHost svcCompHost)
       throws AmbariException;
 
-
-  /**
-   * Get the ClusterVersionEntity object whose state is CURRENT.
-   * @return Cluster Version entity to whose state is CURRENT.
-   */
-  ClusterVersionEntity getCurrentClusterVersion();
-
-  /**
-   * Gets the current stack version associated with the cluster.
-   * <ul>
-   * <li>if there is no upgrade in progress then get the
-   * {@link ClusterVersionEntity} object whose state is
-   * {@link RepositoryVersionState#CURRENT}.
-   * <li>If an upgrade is in progress then based on the direction and the
-   * desired stack determine which version to use. Assuming upgrading from HDP
-   * 2.2.0.0-1 to 2.3.0.0-2:
-   * <ul>
-   * <li>RU Upgrade: 2.3.0.0-2 (desired stack id)
-   * <li>RU Downgrade: 2.2.0.0-1 (desired stack id)
-   * <li>EU Upgrade: while stopping services and before changing desired stack,
-   * use 2.2.0.0-1, after, use 2.3.0.0-2
-   * <li>EU Downgrade: while stopping services and before changing desired
-   * stack, use 2.3.0.0-2, after, use 2.2.0.0-1
-   * </ul>
-   * </ul>
-   *
-   * This method must take into account both a running and a suspended upgrade.
-   *
-   * @return the effective cluster stack version given the current upgrading
-   *         conditions of the cluster.
-   */
-  ClusterVersionEntity getEffectiveClusterVersion() throws AmbariException;
-
-  /**
-   * Get all of the ClusterVersionEntity objects for the cluster.
-   * @return
-   */
-  Collection<ClusterVersionEntity> getAllClusterVersions();
-
   /**
    * Get desired stack version
    * @return
@@ -222,10 +182,6 @@ public interface Cluster {
    * the version distributed to them will move into the
    * {@link RepositoryVersionState#NOT_REQUIRED} state.
    *
-   * @param sourceClusterVersion
-   *          cluster version to be queried for a stack name/version info and
-   *          desired RepositoryVersionState. The only valid state of a cluster
-   *          version is {@link RepositoryVersionState#INSTALLING}
    * @param repoVersionEntity
    *          the repository that the hosts are being transitioned for (not
    *          {@code null}).
@@ -241,9 +197,8 @@ public interface Cluster {
    * @return a list of hosts which need the repository installed.
    * @throws AmbariException
    */
-  List<Host> transitionHostsToInstalling(ClusterVersionEntity sourceClusterVersion,
-      RepositoryVersionEntity repoVersionEntity, VersionDefinitionXml versionDefinitionXml,
-      boolean forceInstalled) throws AmbariException;
+  List<Host> transitionHostsToInstalling(RepositoryVersionEntity repoVersionEntity,
+      VersionDefinitionXml versionDefinitionXml, boolean forceInstalled) throws AmbariException;
 
   /**
    * For a given host, will either either update an existing Host Version Entity for the given version, or create
@@ -259,40 +214,19 @@ public interface Cluster {
       final RepositoryVersionEntity repositoryVersion, final StackId stack)
       throws AmbariException;
 
+
   /**
    * Update state of a cluster stack version for cluster based on states of host versions and stackids.
    * @param repositoryVersion the repository version entity whose version is a value like 2.2.1.0-100)
    * @throws AmbariException
    */
-  void recalculateClusterVersionState(RepositoryVersionEntity repositoryVersion) throws AmbariException;
+//  void recalculateClusterVersionState(RepositoryVersionEntity repositoryVersion) throws AmbariException;
 
   /**
    * Update state of all cluster stack versions for cluster based on states of host versions.
    * @throws AmbariException
    */
-  void recalculateAllClusterVersionStates() throws AmbariException;
-
-  /**
-   * Create a cluster version for the given stack and version, whose initial
-   * state must either be either {@link RepositoryVersionState#UPGRADING} (if no
-   * other cluster version exists) or {@link RepositoryVersionState#INSTALLING}
-   * (if at exactly one CURRENT cluster version already exists) or
-   * {@link RepositoryVersionState#INIT} (if the cluster is being created using
-   * a specific repository version).
-   *
-   * @param stackId
-   *          Stack ID
-   * @param version
-   *          Stack version
-   * @param userName
-   *          User performing the operation
-   * @param state
-   *          Initial state
-   * @return the newly created and persisted {@link ClusterVersionEntity}.
-   * @throws AmbariException
-   */
-  ClusterVersionEntity createClusterVersion(StackId stackId, String version,
-      String userName, RepositoryVersionState state) throws AmbariException;
+//  void recalculateAllClusterVersionStates() throws AmbariException;
 
   /**
    * Transition an existing cluster version from one state to another.
@@ -305,8 +239,8 @@ public interface Cluster {
    *          Desired state
    * @throws AmbariException
    */
-  void transitionClusterVersion(StackId stackId, String version,
-      RepositoryVersionState state) throws AmbariException;
+//  void transitionClusterVersion(StackId stackId, String version,
+//      RepositoryVersionState state) throws AmbariException;
 
   /**
    * Gets whether the cluster is still initializing or has finished with its
@@ -515,7 +449,7 @@ public interface Cluster {
 
   /**
    * Add service to the cluster
-   * 
+   *
    * @param serviceName
    *          the name of the service to add (not {@code null}).
    * @param repositoryVersion

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/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 21c275b..7e162d7 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
@@ -23,7 +23,6 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
-import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -46,24 +45,20 @@ import org.apache.ambari.annotations.Experimental;
 import org.apache.ambari.annotations.ExperimentalFeature;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.ConfigGroupNotFoundException;
-import org.apache.ambari.server.DuplicateResourceException;
 import org.apache.ambari.server.ObjectNotFoundException;
 import org.apache.ambari.server.ParentObjectNotFoundException;
 import org.apache.ambari.server.RoleCommand;
 import org.apache.ambari.server.ServiceComponentHostNotFoundException;
 import org.apache.ambari.server.ServiceComponentNotFoundException;
 import org.apache.ambari.server.ServiceNotFoundException;
-import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.agent.ExecutionCommand.KeyNames;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.AmbariSessionManager;
 import org.apache.ambari.server.controller.ClusterResponse;
 import org.apache.ambari.server.controller.ConfigurationResponse;
 import org.apache.ambari.server.controller.MaintenanceStateHelper;
 import org.apache.ambari.server.controller.RootServiceResponseFactory.Services;
 import org.apache.ambari.server.controller.ServiceConfigVersionResponse;
-import org.apache.ambari.server.controller.internal.UpgradeResourceProvider;
 import org.apache.ambari.server.events.AmbariEvent.AmbariEventType;
 import org.apache.ambari.server.events.ClusterConfigChangedEvent;
 import org.apache.ambari.server.events.ClusterEvent;
@@ -80,12 +75,9 @@ import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
 import org.apache.ambari.server.orm.dao.AlertDispatchDAO;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.ClusterStateDAO;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.HostConfigMappingDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
-import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.ServiceConfigDAO;
 import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.dao.TopologyRequestDAO;
@@ -94,11 +86,8 @@ import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
 import org.apache.ambari.server.orm.entities.ClusterStateEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupEntity;
-import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
-import org.apache.ambari.server.orm.entities.HostRoleCommandEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.PermissionEntity;
 import org.apache.ambari.server.orm.entities.PrivilegeEntity;
@@ -109,21 +98,16 @@ import org.apache.ambari.server.orm.entities.ServiceConfigEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.orm.entities.TopologyRequestEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
-import org.apache.ambari.server.orm.entities.UpgradeGroupEntity;
-import org.apache.ambari.server.orm.entities.UpgradeItemEntity;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
-import org.apache.ambari.server.security.authorization.AuthorizationHelper;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.ClusterHealthReport;
 import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.ComponentInfo;
 import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.ConfigFactory;
 import org.apache.ambari.server.state.ConfigHelper;
 import org.apache.ambari.server.state.DesiredConfig;
 import org.apache.ambari.server.state.Host;
 import org.apache.ambari.server.state.HostHealthStatus;
-import org.apache.ambari.server.state.HostState;
 import org.apache.ambari.server.state.MaintenanceState;
 import org.apache.ambari.server.state.PropertyInfo;
 import org.apache.ambari.server.state.RepositoryType;
@@ -147,7 +131,6 @@ import org.apache.ambari.server.state.fsm.InvalidStateTransitionException;
 import org.apache.ambari.server.state.repository.VersionDefinitionXml;
 import org.apache.ambari.server.state.scheduler.RequestExecution;
 import org.apache.ambari.server.state.scheduler.RequestExecutionFactory;
-import org.apache.ambari.server.state.stack.upgrade.Direction;
 import org.apache.ambari.server.state.svccomphost.ServiceComponentHostSummary;
 import org.apache.ambari.server.topology.TopologyRequest;
 import org.apache.commons.collections.CollectionUtils;
@@ -178,9 +161,6 @@ public class ClusterImpl implements Cluster {
    * Prefix for cluster session attributes name.
    */
   private static final String CLUSTER_SESSION_ATTRIBUTES_PREFIX = "cluster_session_attributes:";
-  private static final Set<RepositoryVersionState> ALLOWED_REPOSITORY_STATES =
-      EnumSet.of(RepositoryVersionState.INIT, RepositoryVersionState.INSTALLING,
-          RepositoryVersionState.INSTALLED);
 
   @Inject
   private Clusters clusters;
@@ -233,12 +213,6 @@ public class ClusterImpl implements Cluster {
   private ClusterStateDAO clusterStateDAO;
 
   @Inject
-  private ClusterVersionDAO clusterVersionDAO;
-
-  @Inject
-  private HostRoleCommandDAO hostRoleCommandDAO;
-
-  @Inject
   private HostDAO hostDAO;
 
   @Inject
@@ -284,12 +258,6 @@ public class ClusterImpl implements Cluster {
   private UpgradeDAO upgradeDAO;
 
   @Inject
-  private RepositoryVersionDAO repositoryVersionDAO;
-
-  @Inject
-  private Configuration configuration;
-
-  @Inject
   private AmbariSessionManager sessionManager;
 
   @Inject
@@ -999,105 +967,6 @@ public class ClusterImpl implements Cluster {
   }
 
   /**
-   * Get the ClusterVersionEntity object whose state is CURRENT.
-   * @return
-   */
-  @Override
-  @Experimental(feature = ExperimentalFeature.PATCH_UPGRADES)
-  public ClusterVersionEntity getCurrentClusterVersion() {
-    Collection<ClusterVersionEntity> clusterVersionEntities = getClusterEntity().getClusterVersionEntities();
-    for (ClusterVersionEntity clusterVersionEntity : clusterVersionEntities) {
-      if (clusterVersionEntity.getState() == RepositoryVersionState.CURRENT) {
-        // TODO assuming there's only 1 current version, return 1st found, exception was expected in previous implementation
-        return clusterVersionEntity;
-      }
-    }
-
-    if( clusterVersionEntities.size() == 1 ) {
-      return clusterVersionEntities.iterator().next();
-    }
-
-    return null;
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public ClusterVersionEntity getEffectiveClusterVersion() throws AmbariException {
-    UpgradeEntity upgradeEntity = getUpgradeInProgress();
-    if (upgradeEntity == null) {
-      return getCurrentClusterVersion();
-    }
-
-    // see if this is in the cache first, and only walk the upgrade if it's not
-    Long upgradeId = upgradeEntity.getId();
-    String effectiveVersion = upgradeEffectiveVersionCache.get(upgradeId);
-    if (null == effectiveVersion) {
-      switch (upgradeEntity.getUpgradeType()) {
-        case NON_ROLLING:
-          if (upgradeEntity.getDirection() == Direction.UPGRADE) {
-            boolean pastChangingStack = isNonRollingUpgradePastUpgradingStack(upgradeEntity);
-            effectiveVersion = pastChangingStack ? upgradeEntity.getToVersion()
-                : upgradeEntity.getFromVersion();
-          } else {
-            // Should be the lower value during a Downgrade.
-            effectiveVersion = upgradeEntity.getToVersion();
-          }
-          break;
-        case ROLLING:
-        default:
-          // Version will be higher on upgrade and lower on downgrade
-          // directions.
-          effectiveVersion = upgradeEntity.getToVersion();
-          break;
-      }
-
-      // cache for later use
-      upgradeEffectiveVersionCache.put(upgradeId, effectiveVersion);
-    }
-
-    if (effectiveVersion == null) {
-      throw new AmbariException("Unable to determine which version to use during Stack Upgrade, effectiveVersion is null.");
-    }
-
-    // Find the first cluster version whose repo matches the expected version.
-    Collection<ClusterVersionEntity> clusterVersionEntities = getClusterEntity().getClusterVersionEntities();
-    for (ClusterVersionEntity clusterVersionEntity : clusterVersionEntities) {
-      if (clusterVersionEntity.getRepositoryVersion().getVersion().equals(effectiveVersion)) {
-        return clusterVersionEntity;
-      }
-    }
-
-    return null;
-  }
-
-  /**
-   * Given a NonRolling stack upgrade, determine if it has already crossed the point of using the newer version.
-   * @param upgrade Stack Upgrade
-   * @return Return true if should be using to_version, otherwise, false to mean the from_version.
-   */
-  private boolean isNonRollingUpgradePastUpgradingStack(UpgradeEntity upgrade) {
-    for (UpgradeGroupEntity group : upgrade.getUpgradeGroups()) {
-      if (group.getName().equalsIgnoreCase(UpgradeResourceProvider.CONST_UPGRADE_GROUP_NAME)) {
-        for (UpgradeItemEntity item : group.getItems()) {
-          List<Long> taskIds = hostRoleCommandDAO.findTaskIdsByStage(upgrade.getRequestId(), item.getStageId());
-          List<HostRoleCommandEntity> commands = hostRoleCommandDAO.findByPKs(taskIds);
-          for (HostRoleCommandEntity command : commands) {
-            if (command.getCustomCommandName() != null &&
-                command.getCustomCommandName().equalsIgnoreCase(UpgradeResourceProvider.CONST_CUSTOM_COMMAND_NAME) &&
-                command.getStatus() == HostRoleStatus.COMPLETED) {
-              return true;
-            }
-          }
-        }
-        return false;
-      }
-    }
-    return false;
-  }
-
-  /**
    * {@inheritDoc}
    */
   @Override
@@ -1106,32 +975,13 @@ public class ClusterImpl implements Cluster {
   }
 
   /**
-   * Get all of the ClusterVersionEntity objects for the cluster.
-   * @return
-   */
-  @Override
-  public Collection<ClusterVersionEntity> getAllClusterVersions() {
-    return clusterVersionDAO.findByCluster(getClusterName());
-  }
-
-  /**
    * {@inheritDoc}
    */
   @Override
   @Transactional
-  public List<Host> transitionHostsToInstalling(ClusterVersionEntity sourceClusterVersion,
-      RepositoryVersionEntity repoVersionEntity, VersionDefinitionXml versionDefinitionXml,
-      boolean forceInstalled) throws AmbariException {
-
-    if (sourceClusterVersion == null) {
-      throw new AmbariException("Could not find current stack version of cluster " + getClusterName());
-    }
+  public List<Host> transitionHostsToInstalling(RepositoryVersionEntity repoVersionEntity,
+      VersionDefinitionXml versionDefinitionXml, boolean forceInstalled) throws AmbariException {
 
-    if (RepositoryVersionState.INSTALLING != sourceClusterVersion.getState()) {
-      throw new AmbariException(
-          "Unable to transition cluster hosts into " + RepositoryVersionState.INSTALLING
-          + ". The only valid state is " + sourceClusterVersion.getState());
-    }
 
     // the hosts to return so that INSTALL commands can be generated for them
     final List<Host> hostsRequiringInstallation;
@@ -1148,6 +998,7 @@ public class ClusterImpl implements Cluster {
       Collection<HostEntity> hostEntities = getClusterEntity().getHostEntities();
 
       for (HostEntity hostEntity : hostEntities) {
+
         // start with INSTALLING
         RepositoryVersionState state = RepositoryVersionState.INSTALLING;
         if (forceInstalled) {
@@ -1230,253 +1081,6 @@ public class ClusterImpl implements Cluster {
   }
 
   /**
-   * Calculate the effective Cluster Version State based on the state of its hosts.
-   *
-   * CURRENT: all hosts are CURRENT
-   * INSTALLED: all hosts in INSTALLED
-   * INSTALL_FAILED: at least one host in INSTALL_FAILED
-   * INSTALLING: all hosts in INSTALLING -or- INSTALLING and NOT_REQUIRED. Notice that if one host is CURRENT and another is INSTALLING, then the
-   * effective version will be OUT_OF_SYNC.
-   * OUT_OF_SYNC: otherwise
-   * @param stateToHosts Map from state to the collection of hosts with that state
-   * @return Return the effective Cluster Version State
-   */
-  private RepositoryVersionState getEffectiveState(Map<RepositoryVersionState, Set<String>> stateToHosts) {
-    if (stateToHosts == null || stateToHosts.size() < 1) {
-      return null;
-    }
-
-    int totalHosts = 0;
-    for (Set<String> hosts : stateToHosts.values()) {
-      totalHosts += hosts.size();
-    }
-
-    if (stateToHosts.containsKey(RepositoryVersionState.CURRENT) && stateToHosts.get(RepositoryVersionState.CURRENT).size() == totalHosts) {
-      return RepositoryVersionState.CURRENT;
-    }
-    if (stateToHosts.containsKey(RepositoryVersionState.INSTALLED) && stateToHosts.get(RepositoryVersionState.INSTALLED).size() == totalHosts) {
-      return RepositoryVersionState.INSTALLED;
-    }
-    if (stateToHosts.containsKey(RepositoryVersionState.INSTALL_FAILED) &&
-      !stateToHosts.get(RepositoryVersionState.INSTALL_FAILED).isEmpty()) {
-      // Installation failed on some host(s). But
-      // cluster version state should transition to Install Failed only after
-      // all hosts have finished installation. Otherwise, UI will misbehave
-      // (hide progress dialog before installation is finished)
-      if (! stateToHosts.containsKey(RepositoryVersionState.INSTALLING) ||
-        stateToHosts.get(RepositoryVersionState.INSTALLING).isEmpty()) {
-        return RepositoryVersionState.INSTALL_FAILED;
-      }
-    }
-
-    int totalInstalling = stateToHosts.containsKey(RepositoryVersionState.INSTALLING) ? stateToHosts.get(RepositoryVersionState.INSTALLING).size() : 0;
-    int totalInstalled = stateToHosts.containsKey(RepositoryVersionState.INSTALLED) ? stateToHosts.get(RepositoryVersionState.INSTALLED).size() : 0;
-    int totalNotRequired = stateToHosts.containsKey(RepositoryVersionState.NOT_REQUIRED) ? stateToHosts.get(RepositoryVersionState.NOT_REQUIRED).size() : 0;
-    int totalInstallFailed = stateToHosts.containsKey(RepositoryVersionState.INSTALL_FAILED) ? stateToHosts.get(RepositoryVersionState.INSTALL_FAILED).size() : 0;
-
-    if (totalInstalling + totalInstalled + totalInstallFailed == totalHosts) {
-      return RepositoryVersionState.INSTALLING;
-    }
-
-    if (totalNotRequired > 0) {
-
-      // !!! if all we have is NOT_REQUIRED and something else, the return should be the something else
-      if (2 == stateToHosts.size()) {
-
-        Map<RepositoryVersionState, Set<String>> map = Maps.filterKeys(stateToHosts,
-            new com.google.common.base.Predicate<RepositoryVersionState>() {
-              @Override
-              public boolean apply(RepositoryVersionState repoState) {
-                return repoState != RepositoryVersionState.NOT_REQUIRED;
-              }
-            });
-
-        // !!! better be true
-        if (1 == map.size()) {
-          return map.keySet().iterator().next();
-        } else {
-          LOG.warn("The mix of NON_REQUIRED hosts is unexpected: {}", stateToHosts);
-          return RepositoryVersionState.OUT_OF_SYNC;
-        }
-      }
-
-      // if any hosts are still installing, then cluster is INSTALLING
-      if (totalInstalling > 0) {
-        return RepositoryVersionState.INSTALLING;
-      }
-
-      // if any hosts are install_failed, then cluster is INSTALL_FAILED
-      if (totalInstallFailed > 0) {
-        return RepositoryVersionState.INSTALL_FAILED;
-      }
-
-      // should be covered by the 2-state check above
-      if (totalInstalled > 0) {
-        return RepositoryVersionState.INSTALLED;
-      }
-
-      // rare
-      if (totalNotRequired == totalHosts) {
-        return RepositoryVersionState.NOT_REQUIRED;
-      }
-
-    }
-
-    // Also returns when have a mix of CURRENT and INSTALLING|INSTALLED
-    LOG.warn("Have a mix of CURRENT and INSTALLING|INSTALLED host versions, " +
-      "returning OUT_OF_SYNC as cluster version. Host version states: {}", stateToHosts);
-    return RepositoryVersionState.OUT_OF_SYNC;
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public void recalculateClusterVersionState(RepositoryVersionEntity repositoryVersion) throws AmbariException {
-    if (repositoryVersion == null) {
-      return;
-    }
-
-    StackId stackId = repositoryVersion.getStackId();
-    String version = repositoryVersion.getVersion();
-
-    Map<String, Host> hosts = clusters.getHostsForCluster(getClusterName());
-    clusterGlobalLock.writeLock().lock();
-
-    try {
-      // Part 1, bootstrap cluster version if necessary.
-
-      ClusterVersionEntity clusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(
-          getClusterName(), stackId, version);
-
-      boolean performingInitialBootstrap = false;
-      if (clusterVersion == null) {
-        if (clusterVersionDAO.findByCluster(getClusterName()).isEmpty()) {
-          // During an Ambari Upgrade from 1.7.0 -> 2.0.0, the Cluster Version
-          // will not exist, so bootstrap it.
-          // This can still fail if the Repository Version has not yet been created,
-          // which can happen if the first HostComponentState to trigger this method
-          // cannot advertise a version.
-          performingInitialBootstrap = true;
-          createClusterVersionInternal(
-              stackId,
-              version,
-              AuthorizationHelper.getAuthenticatedName(configuration.getAnonymousAuditName()),
-              RepositoryVersionState.INSTALLING);
-          clusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(
-              getClusterName(), stackId, version);
-
-          if (clusterVersion == null) {
-            LOG.warn(String.format(
-                "Could not create a cluster version for cluster %s and stack %s using repo version %s",
-                getClusterName(), stackId.getStackId(), repositoryVersion));
-            return;
-          }
-        } else {
-          LOG.warn(String.format(
-              "Repository version %s not found for cluster %s",
-              repositoryVersion, getClusterName()));
-          return;
-        }
-      }
-
-      // Ignore if cluster version is CURRENT or UPGRADE_FAILED
-      if (clusterVersion.getState() != RepositoryVersionState.INSTALL_FAILED &&
-              clusterVersion.getState() != RepositoryVersionState.OUT_OF_SYNC &&
-              clusterVersion.getState() != RepositoryVersionState.INSTALLING &&
-              clusterVersion.getState() != RepositoryVersionState.INSTALLED &&
-              clusterVersion.getState() != RepositoryVersionState.INIT) {
-        // anything else is not supported as of now
-        return;
-      }
-
-      // Part 2, check for transitions.
-      Set<String> hostsWithoutHostVersion = new HashSet<>();
-      Map<RepositoryVersionState, Set<String>> stateToHosts = new HashMap<>();
-
-      //hack until better hostversion integration into in-memory cluster structure
-
-      List<HostVersionEntity> hostVersionEntities =
-              hostVersionDAO.findByClusterStackAndVersion(getClusterName(), stackId, version);
-
-      Set<String> hostsWithState = new HashSet<>();
-      Set<String> hostsInMaintenanceState = new HashSet<>();
-      for (HostVersionEntity hostVersionEntity : hostVersionEntities) {
-        String hostname = hostVersionEntity.getHostEntity().getHostName();
-        Host host = hosts.get(hostname);
-        if(host != null && host.getMaintenanceState(getClusterId()) == MaintenanceState.ON) {
-          hostsInMaintenanceState.add(hostname);
-          continue;
-        }
-        hostsWithState.add(hostname);
-        RepositoryVersionState hostState = hostVersionEntity.getState();
-
-        if (stateToHosts.containsKey(hostState)) {
-          stateToHosts.get(hostState).add(hostname);
-        } else {
-          Set<String> hostsInState = new HashSet<>();
-          hostsInState.add(hostname);
-          stateToHosts.put(hostState, hostsInState);
-        }
-      }
-
-      hostsWithoutHostVersion.addAll(hosts.keySet());
-      hostsWithoutHostVersion.removeAll(hostsWithState);
-      hostsWithoutHostVersion.removeAll(hostsInMaintenanceState);
-
-      // Ensure that all of the hosts without a Host Version only have
-      // Components that do not advertise a version.
-      // Otherwise, operations are still in progress.
-      for (String hostname : hostsWithoutHostVersion) {
-        HostEntity hostEntity = hostDAO.findByName(hostname);
-
-        // During initial bootstrap, unhealthy hosts are ignored
-        // so we boostrap the CURRENT version anyway
-        if (performingInitialBootstrap &&
-                hostEntity.getHostStateEntity().getCurrentState() != HostState.HEALTHY) {
-          continue;
-        }
-
-        final Collection<HostComponentStateEntity> allHostComponents = hostEntity.getHostComponentStateEntities();
-
-        for (HostComponentStateEntity hostComponentStateEntity : allHostComponents) {
-          if (hostComponentStateEntity.getVersion().equalsIgnoreCase(
-              State.UNKNOWN.toString())) {
-            // Some Components cannot advertise a version. E.g., ZKF, AMBARI_METRICS,
-            // Kerberos
-            ComponentInfo compInfo = ambariMetaInfo.getComponent(
-                stackId.getStackName(), stackId.getStackVersion(),
-                hostComponentStateEntity.getServiceName(),
-                hostComponentStateEntity.getComponentName());
-
-            if (compInfo.isVersionAdvertised()) {
-              LOG.debug("Skipping transitioning the cluster version because host "
-                  + hostname + " does not have a version yet.");
-              return;
-            }
-          }
-        }
-      }
-
-      RepositoryVersionState effectiveClusterVersionState = getEffectiveState(stateToHosts);
-
-      if (effectiveClusterVersionState != null
-          && effectiveClusterVersionState != clusterVersion.getState()) {
-        // Any mismatch will be caught while transitioning, and raise an
-        // exception.
-        try {
-          transitionClusterVersion(stackId, version,
-              effectiveClusterVersionState);
-        } catch (AmbariException e) {
-          ;
-        }
-      }
-    } finally {
-      clusterGlobalLock.writeLock().unlock();
-    }
-  }
-
-  /**
    * Transition the Host Version across states.
    * @param host Host object
    * @param repositoryVersion Repository Version with stack and version information
@@ -1539,262 +1143,6 @@ public class ClusterImpl implements Cluster {
   }
 
   @Override
-  public void recalculateAllClusterVersionStates() throws AmbariException {
-    clusterGlobalLock.writeLock().lock();
-    try {
-      List<ClusterVersionEntity> clusterVersionEntities = clusterVersionDAO.findByCluster(getClusterName());
-      StackId currentStackId = getCurrentStackVersion();
-      for (ClusterVersionEntity clusterVersionEntity : clusterVersionEntities) {
-        RepositoryVersionEntity repositoryVersionEntity = clusterVersionEntity.getRepositoryVersion();
-        StackId repoVersionStackId = repositoryVersionEntity.getStackId();
-
-        if (repoVersionStackId.equals(currentStackId)
-            && clusterVersionEntity.getState() != RepositoryVersionState.CURRENT) {
-          recalculateClusterVersionState(clusterVersionEntity.getRepositoryVersion());
-        }
-      }
-    } finally {
-      clusterGlobalLock.writeLock().unlock();
-    }
-  }
-
-  @Override
-  public ClusterVersionEntity createClusterVersion(StackId stackId, String version,
-      String userName, RepositoryVersionState state) throws AmbariException {
-    clusterGlobalLock.writeLock().lock();
-    try {
-      return createClusterVersionInternal(stackId, version, userName, state);
-    } finally {
-      clusterGlobalLock.writeLock().unlock();
-    }
-  }
-
-  /**
-   * See {@link #createClusterVersion}
-   *
-   * This method is intended to be called only when cluster lock is already acquired.
-   */
-  private ClusterVersionEntity createClusterVersionInternal(StackId stackId, String version,
-      String userName, RepositoryVersionState state) throws AmbariException {
-    if (!ALLOWED_REPOSITORY_STATES.contains(state)) {
-      throw new AmbariException("The allowed state for a new cluster version must be within " + ALLOWED_REPOSITORY_STATES);
-    }
-
-    ClusterVersionEntity existing = clusterVersionDAO.findByClusterAndStackAndVersion(
-      getClusterName(), stackId, version);
-    if (existing != null) {
-      throw new DuplicateResourceException(
-          "Duplicate item, a cluster version with stack=" + stackId
-              + ", version=" +
-          version + " for cluster " + getClusterName() + " already exists");
-    }
-
-    RepositoryVersionEntity repositoryVersionEntity = repositoryVersionDAO.findByStackAndVersion(
-      stackId, version);
-    if (repositoryVersionEntity == null) {
-      throw new AmbariException(
-          "Unable to find repository version for stack " + stackId + " and version " + version);
-    }
-
-    ClusterEntity clusterEntity = getClusterEntity();
-    ClusterVersionEntity clusterVersionEntity = new ClusterVersionEntity(
-        clusterEntity, repositoryVersionEntity, state,
-      System.currentTimeMillis(), System.currentTimeMillis(), userName);
-    clusterVersionDAO.create(clusterVersionEntity);
-    clusterEntity.getClusterVersionEntities().add(clusterVersionEntity);
-    clusterEntity = clusterDAO.merge(clusterEntity);
-
-    return clusterVersionEntity;
-  }
-
-  /**
-   * Transition an existing cluster version from one state to another. The
-   * following are some of the steps that are taken when transitioning between
-   * specific states:
-   * <ul>
-   * <li>INSTALLING/INSTALLED --> CURRENT</lki>: Set the current stack to the
-   * desired stack, ensure all hosts with the desired stack are CURRENT as well.
-   * </ul>
-   * <li>INSTALLING/INSTALLED --> CURRENT</lki>: Set the current stack to the
-   * desired stack. </ul>
-   *
-   * @param stackId
-   *          Stack ID
-   * @param version
-   *          Stack version
-   * @param state
-   *          Desired state
-   * @throws AmbariException
-   */
-  @Override
-  @Transactional
-  public void transitionClusterVersion(StackId stackId, String version,
-      RepositoryVersionState state) throws AmbariException {
-    Set<RepositoryVersionState> allowedStates = new HashSet<>();
-    clusterGlobalLock.writeLock().lock();
-    try {
-      ClusterEntity clusterEntity = getClusterEntity();
-      ClusterVersionEntity existingClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(
-          getClusterName(), stackId, version);
-
-      if (existingClusterVersion == null) {
-        throw new AmbariException("Existing cluster version not found for cluster="
-            + getClusterName() + ", stack=" + stackId + ", version=" + version);
-      }
-
-      // NOOP
-      if (existingClusterVersion.getState() == state) {
-        return;
-      }
-
-      switch (existingClusterVersion.getState()) {
-        case CURRENT:
-          // If CURRENT state is changed here cluster will not have CURRENT
-          // state.
-          // CURRENT state will be changed to INSTALLED when another CURRENT
-          // state is added.
-          // allowedStates.add(RepositoryVersionState.INSTALLED);
-          break;
-        case INSTALLING:
-          allowedStates.add(RepositoryVersionState.INSTALLED);
-          allowedStates.add(RepositoryVersionState.INSTALL_FAILED);
-          allowedStates.add(RepositoryVersionState.OUT_OF_SYNC);
-          if (clusterVersionDAO.findByClusterAndStateCurrent(getClusterName()) == null) {
-            allowedStates.add(RepositoryVersionState.CURRENT);
-          }
-          break;
-        case INSTALL_FAILED:
-          allowedStates.add(RepositoryVersionState.INSTALLING);
-          break;
-        case INSTALLED:
-          allowedStates.add(RepositoryVersionState.INSTALLING);
-          allowedStates.add(RepositoryVersionState.OUT_OF_SYNC);
-          allowedStates.add(RepositoryVersionState.CURRENT);
-          break;
-        case OUT_OF_SYNC:
-          allowedStates.add(RepositoryVersionState.INSTALLING);
-          break;
-        case INIT:
-          allowedStates.add(RepositoryVersionState.CURRENT);
-          break;
-      }
-
-      if (!allowedStates.contains(state)) {
-        throw new AmbariException("Invalid cluster version transition from "
-            + existingClusterVersion.getState() + " to " + state);
-      }
-
-      // There must be at most one cluster version whose state is CURRENT at
-      // all times.
-      if (state == RepositoryVersionState.CURRENT) {
-        ClusterVersionEntity currentVersion = clusterVersionDAO.findByClusterAndStateCurrent(
-            getClusterName());
-        if (currentVersion != null) {
-          currentVersion.setState(RepositoryVersionState.INSTALLED);
-          currentVersion = clusterVersionDAO.merge(currentVersion);
-        }
-      }
-
-      existingClusterVersion.setState(state);
-      existingClusterVersion.setEndTime(System.currentTimeMillis());
-      existingClusterVersion = clusterVersionDAO.merge(existingClusterVersion);
-
-      if (state == RepositoryVersionState.CURRENT) {
-        for (HostEntity hostEntity : clusterEntity.getHostEntities()) {
-          if (hostHasReportables(existingClusterVersion.getRepositoryVersion(), hostEntity)) {
-            continue;
-          }
-
-          Collection<HostVersionEntity> versions = hostVersionDAO.findByHost(
-              hostEntity.getHostName());
-
-          HostVersionEntity target = null;
-          if (null != versions) {
-            // Set anything that was previously marked CURRENT as INSTALLED, and
-            // the matching version as CURRENT
-            for (HostVersionEntity entity : versions) {
-              if (entity.getRepositoryVersion().getId().equals(
-                  existingClusterVersion.getRepositoryVersion().getId())) {
-                target = entity;
-                target.setState(state);
-                target = hostVersionDAO.merge(target);
-              } else if (entity.getState() == RepositoryVersionState.CURRENT) {
-                entity.setState(RepositoryVersionState.INSTALLED);
-                entity = hostVersionDAO.merge(entity);
-              }
-            }
-          }
-
-          if (null == target) {
-            // If no matching version was found, create one with the desired
-            // state
-            HostVersionEntity hve = new HostVersionEntity(hostEntity,
-                existingClusterVersion.getRepositoryVersion(), state);
-
-            LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
-                hve.getHostName(), hve.getState(), hve.getRepositoryVersion().getVersion(),
-                hve.getRepositoryVersion().getId());
-
-            hostVersionDAO.create(hve);
-          }
-        }
-
-        // when setting the cluster's state to current, we must also
-        // bring the desired stack and current stack in line with each other
-        StackEntity desiredStackEntity = clusterEntity.getDesiredStack();
-        StackId desiredStackId = new StackId(desiredStackEntity);
-
-        // if the desired stack ID doesn't match the target when setting the
-        // cluster to CURRENT, then there's a problem
-        if (!desiredStackId.equals(stackId)) {
-          String message = MessageFormat.format(
-              "The desired stack ID {0} must match {1} when transitioning the cluster''s state to {2}",
-              desiredStackId, stackId, RepositoryVersionState.CURRENT);
-
-          throw new AmbariException(message);
-        }
-
-        setCurrentStackVersion(stackId);
-      }
-    } catch (RollbackException e) {
-      String message = MessageFormat.format(
-        "Unable to transition stack {0} at version {1} for cluster {2} to state {3}",
-        stackId, version, getClusterName(), state);
-
-      LOG.warn(message);
-      throw new AmbariException(message, e);
-
-    } finally {
-      clusterGlobalLock.writeLock().unlock();
-    }
-
-  }
-
-  /**
-   * Checks if the host has any components reporting version information.
-   * @param repoVersion the repo version
-   * @param host        the host entity
-   * @return {@code true} if the host has any component that report version
-   * @throws AmbariException
-   */
-  private boolean hostHasReportables(RepositoryVersionEntity repoVersion, HostEntity host)
-      throws AmbariException {
-
-    for (HostComponentStateEntity hcse : host.getHostComponentStateEntities()) {
-      ComponentInfo ci = ambariMetaInfo.getComponent(
-          repoVersion.getStackName(),
-          repoVersion.getStackVersion(),
-          hcse.getServiceName(),
-          hcse.getComponentName());
-
-      if (ci.isVersionAdvertised()) {
-        return true;
-      }
-    }
-    return false;
-  }
-
-  @Override
   @Transactional
   public void setCurrentStackVersion(StackId stackId) throws AmbariException {
     clusterGlobalLock.writeLock().lock();

http://git-wip-us.apache.org/repos/asf/ambari/blob/aaa821cc/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
index 99bc781..bdc4f90 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
@@ -43,10 +43,8 @@ import org.apache.ambari.server.events.HostsAddedEvent;
 import org.apache.ambari.server.events.HostsRemovedEvent;
 import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.HostConfigMappingDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
-import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
 import org.apache.ambari.server.orm.dao.HostStateDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.KerberosPrincipalHostDAO;
@@ -59,7 +57,6 @@ import org.apache.ambari.server.orm.dao.TopologyHostRequestDAO;
 import org.apache.ambari.server.orm.dao.TopologyLogicalTaskDAO;
 import org.apache.ambari.server.orm.dao.TopologyRequestDAO;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.HostRoleCommandEntity;
 import org.apache.ambari.server.orm.entities.PermissionEntity;
@@ -113,14 +110,10 @@ public class ClustersImpl implements Clusters {
   @Inject
   private HostDAO hostDAO;
   @Inject
-  private ClusterVersionDAO clusterVersionDAO;
-  @Inject
   private HostVersionDAO hostVersionDAO;
   @Inject
   private HostStateDAO hostStateDAO;
   @Inject
-  private HostRoleCommandDAO hostRoleCommandDAO;
-  @Inject
   private ResourceTypeDAO resourceTypeDAO;
   @Inject
   private RequestOperationLevelDAO requestOperationLevelDAO;
@@ -649,12 +642,6 @@ public class ClustersImpl implements Clusters {
       clusterSet.remove(cluster);
     }
     clusterHostMap.remove(cluster.getClusterName());
-
-    Collection<ClusterVersionEntity> clusterVersions = cluster.getAllClusterVersions();
-    for (ClusterVersionEntity clusterVersion : clusterVersions) {
-      clusterVersionDAO.remove(clusterVersion);
-    }
-
     clusters.remove(clusterName);
   }