ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jonathanhur...@apache.org
Subject [2/8] ambari git commit: AMBARI-10511 - Use Stack Table For Entity Relationships (jonathanhurley)
Date Thu, 16 Apr 2015 14:37:25 GMT
http://git-wip-us.apache.org/repos/asf/ambari/blob/746df034/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
index 66a4ade..bb1cb46 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
@@ -70,6 +70,7 @@ import org.apache.ambari.server.orm.entities.HostStateEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.ServiceDesiredStateEntity;
+import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.state.AgentVersion;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
@@ -100,7 +101,6 @@ import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 
-import com.google.gson.Gson;
 import com.google.inject.AbstractModule;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
@@ -187,7 +187,8 @@ public class ClusterTest {
   }
 
   private void createDefaultCluster() throws Exception {
-    clusters.addCluster("c1");
+    StackId stackId = new StackId("HDP-0.1");
+    clusters.addCluster("c1", stackId);
     c1 = clusters.getCluster("c1");
     Assert.assertEquals("c1", c1.getClusterName());
     Assert.assertEquals(1, c1.getClusterId());
@@ -211,21 +212,26 @@ public class ClusterTest {
     host1.persist();
     host2.persist();
 
-    StackId stackId = new StackId("HDP-0.1");
-    c1.setDesiredStackVersion(stackId);
-    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
-    c1.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
-    c1.transitionClusterVersion(stackId.getStackName(), stackId.getStackVersion(), RepositoryVersionState.CURRENT);
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    c1.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
+    c1.transitionClusterVersion(stackId, stackId.getStackVersion(),
+        RepositoryVersionState.CURRENT);
     clusters.mapHostToCluster("h1", "c1");
     clusters.mapHostToCluster("h2", "c1");
     ClusterVersionDAOMock.failOnCurrentVersionState = false;
   }
 
   public ClusterEntity createDummyData() {
+    StackEntity stackEntity = new StackEntity();
+    stackEntity.setStackName("HDP");
+    stackEntity.setStackVersion("0.1");
+
     ClusterEntity clusterEntity = new ClusterEntity();
     clusterEntity.setClusterId(1L);
     clusterEntity.setClusterName("test_cluster1");
     clusterEntity.setClusterInfo("test_cluster_info1");
+    clusterEntity.setDesiredStack(stackEntity);
 
     HostEntity host1 = new HostEntity();
     HostEntity host2 = new HostEntity();
@@ -262,10 +268,11 @@ public class ClusterTest {
     clusterServiceEntity.setClusterEntity(clusterEntity);
     clusterServiceEntity.setServiceComponentDesiredStateEntities(
         Collections.EMPTY_LIST);
+
     ServiceDesiredStateEntity stateEntity = mock(ServiceDesiredStateEntity.class);
-    Gson gson = new Gson();
-    when(stateEntity.getDesiredStackVersion()).thenReturn(gson.toJson(new StackId("HDP-0.1"),
-        StackId.class));
+
+    when(stateEntity.getDesiredStack()).thenReturn(stackEntity);
+
     clusterServiceEntity.setServiceDesiredStateEntity(stateEntity);
     List<ClusterServiceEntity> clusterServiceEntities = new ArrayList<ClusterServiceEntity>();
     clusterServiceEntities.add(clusterServiceEntity);
@@ -273,23 +280,25 @@ public class ClusterTest {
     return clusterEntity;
   }
 
-  private void checkStackVersionState(String stack, String version, RepositoryVersionState state) {
+  private void checkStackVersionState(StackId stackId, String version, RepositoryVersionState state) {
     Collection<ClusterVersionEntity> allClusterVersions = c1.getAllClusterVersions();
     for (ClusterVersionEntity entity : allClusterVersions) {
-      if (entity.getRepositoryVersion().getStack().equals(stack)
-          && entity.getRepositoryVersion().getVersion().equals(version)) {
+      StackId repoVersionStackId = new StackId(entity.getRepositoryVersion().getStack());
+      if (repoVersionStackId.equals(stackId)
+          && repoVersionStackId.getStackVersion().equals(version)) {
         assertEquals(state, entity.getState());
       }
     }
   }
 
-  private void assertStateException(String stack, String version, RepositoryVersionState transitionState,
+  private void assertStateException(StackId stackId, String version,
+      RepositoryVersionState transitionState,
                                     RepositoryVersionState stateAfter) {
     try {
-      c1.transitionClusterVersion(stack, version, transitionState);
+      c1.transitionClusterVersion(stackId, version, transitionState);
       Assert.fail();
     } catch (AmbariException e) {}
-    checkStackVersionState(stack, version, stateAfter);
+    checkStackVersionState(stackId, version, stateAfter);
     assertNotNull(c1.getCurrentClusterVersion());
   }
 
@@ -308,7 +317,7 @@ public class ClusterTest {
    * @return Cluster that was created
    */
   private Cluster createClusterForRU(String clusterName, StackId stackId, Map<String, String> hostAttributes) throws Exception {
-    clusters.addCluster(clusterName);
+    clusters.addCluster(clusterName, stackId);
     Cluster cluster = clusters.getCluster(clusterName);
     Assert.assertEquals(clusterName, cluster.getClusterName());
     Assert.assertEquals(1, cluster.getClusterId());
@@ -511,14 +520,14 @@ public class ClusterTest {
   @Test
   public void testGetHostState() throws Exception {
     createDefaultCluster();
-    
+
     Assert.assertEquals(HostState.INIT, clusters.getHost("h1").getState());
   }
 
   @Test
   public void testSetHostState() throws Exception {
     createDefaultCluster();
-    
+
     clusters.getHost("h1").setState(HostState.HEARTBEAT_LOST);
     Assert.assertEquals(HostState.HEARTBEAT_LOST,
         clusters.getHost("h1").getState());
@@ -528,7 +537,7 @@ public class ClusterTest {
   public void testHostEvent() throws Exception,
       InvalidStateTransitionException {
     createDefaultCluster();
-    
+
     HostInfo hostInfo = new HostInfo();
     hostInfo.setHostName("h1");
     hostInfo.setInterfaces("fip_4");
@@ -576,6 +585,8 @@ public class ClusterTest {
 
   @Test
   public void testBasicClusterSetup() throws Exception {
+    StackId stackVersion = new StackId("HDP-1.2.0");
+
     createDefaultCluster();
 
     String clusterName = "c2";
@@ -587,7 +598,7 @@ public class ClusterTest {
       // Expected
     }
 
-    clusters.addCluster(clusterName);
+    clusters.addCluster(clusterName, stackVersion);
     Cluster c2 = clusters.getCluster(clusterName);
 
     Assert.assertNotNull(c2);
@@ -598,11 +609,7 @@ public class ClusterTest {
     Assert.assertEquals("foo2", c2.getClusterName());
 
     Assert.assertNotNull(c2.getDesiredStackVersion());
-    Assert.assertEquals("", c2.getDesiredStackVersion().getStackId());
-
-    StackId stackVersion = new StackId("HDP-1.0");
-    c2.setDesiredStackVersion(stackVersion);
-    Assert.assertEquals("HDP-1.0", c2.getDesiredStackVersion().getStackId());
+    Assert.assertEquals("HDP-1.2.0", c2.getDesiredStackVersion().getStackId());
   }
 
   @Test
@@ -798,7 +805,7 @@ public class ClusterTest {
   public void testClusterRecovery() throws AmbariException {
     ClusterEntity entity = createDummyData();
     ClusterStateEntity clusterStateEntity = new ClusterStateEntity();
-    clusterStateEntity.setCurrentStackVersion("{\"stackName\":\"HDP\",\"stackVersion\":\"0.1\"}");
+    clusterStateEntity.setCurrentStack(entity.getDesiredStack());
     entity.setClusterStateEntity(clusterStateEntity);
     ClusterImpl cluster = new ClusterImpl(entity, injector);
     Service service = cluster.getService("HDFS");
@@ -1128,99 +1135,161 @@ public class ClusterTest {
     String stack = "HDP";
     String version = "0.2";
 
-    helper.getOrCreateRepositoryVersion(stack, version);
-    c1.createClusterVersion(stack, version, "admin", RepositoryVersionState.INSTALLING);
-
-    assertStateException(stack, version, RepositoryVersionState.CURRENT, RepositoryVersionState.INSTALLING);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADING, RepositoryVersionState.INSTALLING);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADED, RepositoryVersionState.INSTALLING);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADE_FAILED, RepositoryVersionState.INSTALLING);
-
-    c1.transitionClusterVersion(stack, version, RepositoryVersionState.INSTALL_FAILED);
-    checkStackVersionState(stack, version, RepositoryVersionState.INSTALL_FAILED);
-
-    assertStateException(stack, version, RepositoryVersionState.CURRENT, RepositoryVersionState.INSTALL_FAILED);
-    assertStateException(stack, version, RepositoryVersionState.INSTALLED, RepositoryVersionState.INSTALL_FAILED);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADING, RepositoryVersionState.INSTALL_FAILED);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADED, RepositoryVersionState.INSTALL_FAILED);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADE_FAILED, RepositoryVersionState.INSTALL_FAILED);
-    assertStateException(stack, version, RepositoryVersionState.OUT_OF_SYNC, RepositoryVersionState.INSTALL_FAILED);
-
-    c1.transitionClusterVersion(stack, version, RepositoryVersionState.INSTALLING);
-    checkStackVersionState(stack, version, RepositoryVersionState.INSTALLING);
-
-    c1.transitionClusterVersion(stack, version, RepositoryVersionState.INSTALLED);
-    checkStackVersionState(stack, version, RepositoryVersionState.INSTALLED);
-
-    assertStateException(stack, version, RepositoryVersionState.CURRENT, RepositoryVersionState.INSTALLED);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADE_FAILED, RepositoryVersionState.INSTALLED);
-    assertStateException(stack, version, RepositoryVersionState.INSTALL_FAILED, RepositoryVersionState.INSTALLED);
-
-    c1.transitionClusterVersion(stack, version, RepositoryVersionState.OUT_OF_SYNC);
-    checkStackVersionState(stack, version, RepositoryVersionState.OUT_OF_SYNC);
-
-    assertStateException(stack, version, RepositoryVersionState.CURRENT, RepositoryVersionState.OUT_OF_SYNC);
-    assertStateException(stack, version, RepositoryVersionState.INSTALLED, RepositoryVersionState.OUT_OF_SYNC);
-    assertStateException(stack, version, RepositoryVersionState.INSTALL_FAILED, RepositoryVersionState.OUT_OF_SYNC);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADING, RepositoryVersionState.OUT_OF_SYNC);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADED, RepositoryVersionState.OUT_OF_SYNC);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADE_FAILED, RepositoryVersionState.OUT_OF_SYNC);
-
-    c1.transitionClusterVersion(stack, version, RepositoryVersionState.INSTALLING);
-    checkStackVersionState(stack, version, RepositoryVersionState.INSTALLING);
-
-    c1.transitionClusterVersion(stack, version, RepositoryVersionState.INSTALLED);
-    checkStackVersionState(stack, version, RepositoryVersionState.INSTALLED);
-
-    c1.transitionClusterVersion(stack, version, RepositoryVersionState.UPGRADING);
-    checkStackVersionState(stack, version, RepositoryVersionState.UPGRADING);
-
-    assertStateException(stack, version, RepositoryVersionState.CURRENT, RepositoryVersionState.UPGRADING);
-    assertStateException(stack, version, RepositoryVersionState.INSTALLED, RepositoryVersionState.UPGRADING);
-    assertStateException(stack, version, RepositoryVersionState.INSTALL_FAILED, RepositoryVersionState.UPGRADING);
-    assertStateException(stack, version, RepositoryVersionState.OUT_OF_SYNC, RepositoryVersionState.UPGRADING);
-
-    c1.transitionClusterVersion(stack, version, RepositoryVersionState.UPGRADE_FAILED);
-    checkStackVersionState(stack, version, RepositoryVersionState.UPGRADE_FAILED);
-
-    assertStateException(stack, version, RepositoryVersionState.CURRENT, RepositoryVersionState.UPGRADE_FAILED);
-    assertStateException(stack, version, RepositoryVersionState.INSTALLED, RepositoryVersionState.UPGRADE_FAILED);
-    assertStateException(stack, version, RepositoryVersionState.INSTALL_FAILED, RepositoryVersionState.UPGRADE_FAILED);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADED, RepositoryVersionState.UPGRADE_FAILED);
-    assertStateException(stack, version, RepositoryVersionState.OUT_OF_SYNC, RepositoryVersionState.UPGRADE_FAILED);
-
-    c1.transitionClusterVersion(stack, version, RepositoryVersionState.UPGRADING);
-    checkStackVersionState(stack, version, RepositoryVersionState.UPGRADING);
-
-    c1.transitionClusterVersion(stack, version, RepositoryVersionState.UPGRADED);
-    checkStackVersionState(stack, version, RepositoryVersionState.UPGRADED);
-
-    assertStateException(stack, version, RepositoryVersionState.INSTALLED, RepositoryVersionState.UPGRADED);
-    assertStateException(stack, version, RepositoryVersionState.INSTALL_FAILED, RepositoryVersionState.UPGRADED);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADING, RepositoryVersionState.UPGRADED);
-    assertStateException(stack, version, RepositoryVersionState.UPGRADE_FAILED, RepositoryVersionState.UPGRADED);
-    assertStateException(stack, version, RepositoryVersionState.OUT_OF_SYNC, RepositoryVersionState.UPGRADED);
-
-    c1.transitionClusterVersion(stack, version, RepositoryVersionState.CURRENT);
-    checkStackVersionState(stack, version, RepositoryVersionState.CURRENT);
-    checkStackVersionState("HDP", "0.1", RepositoryVersionState.INSTALLED);
+    StackId stackId = new StackId(stack, version);
+
+    helper.getOrCreateRepositoryVersion(stackId, version);
+    c1.createClusterVersion(stackId, version, "admin",
+        RepositoryVersionState.INSTALLING);
+
+    assertStateException(stackId, version, RepositoryVersionState.CURRENT,
+        RepositoryVersionState.INSTALLING);
+    assertStateException(stackId, version, RepositoryVersionState.UPGRADING,
+        RepositoryVersionState.INSTALLING);
+    assertStateException(stackId, version, RepositoryVersionState.UPGRADED,
+        RepositoryVersionState.INSTALLING);
+    assertStateException(stackId, version,
+        RepositoryVersionState.UPGRADE_FAILED,
+        RepositoryVersionState.INSTALLING);
+
+    c1.transitionClusterVersion(stackId, version,
+        RepositoryVersionState.INSTALL_FAILED);
+    checkStackVersionState(stackId, version,
+        RepositoryVersionState.INSTALL_FAILED);
+
+    assertStateException(stackId, version, RepositoryVersionState.CURRENT,
+        RepositoryVersionState.INSTALL_FAILED);
+    assertStateException(stackId, version, RepositoryVersionState.INSTALLED,
+        RepositoryVersionState.INSTALL_FAILED);
+    assertStateException(stackId, version, RepositoryVersionState.UPGRADING,
+        RepositoryVersionState.INSTALL_FAILED);
+    assertStateException(stackId, version, RepositoryVersionState.UPGRADED,
+        RepositoryVersionState.INSTALL_FAILED);
+    assertStateException(stackId, version,
+        RepositoryVersionState.UPGRADE_FAILED,
+        RepositoryVersionState.INSTALL_FAILED);
+    assertStateException(stackId, version, RepositoryVersionState.OUT_OF_SYNC,
+        RepositoryVersionState.INSTALL_FAILED);
+
+    c1.transitionClusterVersion(stackId, version,
+        RepositoryVersionState.INSTALLING);
+    checkStackVersionState(stackId, version, RepositoryVersionState.INSTALLING);
+
+    c1.transitionClusterVersion(stackId, version,
+        RepositoryVersionState.INSTALLED);
+    checkStackVersionState(stackId, version, RepositoryVersionState.INSTALLED);
+
+    assertStateException(stackId, version, RepositoryVersionState.CURRENT,
+        RepositoryVersionState.INSTALLED);
+    assertStateException(stackId, version,
+        RepositoryVersionState.UPGRADE_FAILED, RepositoryVersionState.INSTALLED);
+    assertStateException(stackId, version,
+        RepositoryVersionState.INSTALL_FAILED, RepositoryVersionState.INSTALLED);
+
+    c1.transitionClusterVersion(stackId, version,
+        RepositoryVersionState.OUT_OF_SYNC);
+    checkStackVersionState(stackId, version, RepositoryVersionState.OUT_OF_SYNC);
+
+    assertStateException(stackId, version, RepositoryVersionState.CURRENT,
+        RepositoryVersionState.OUT_OF_SYNC);
+    assertStateException(stackId, version, RepositoryVersionState.INSTALLED,
+        RepositoryVersionState.OUT_OF_SYNC);
+    assertStateException(stackId, version,
+        RepositoryVersionState.INSTALL_FAILED,
+        RepositoryVersionState.OUT_OF_SYNC);
+    assertStateException(stackId, version, RepositoryVersionState.UPGRADING,
+        RepositoryVersionState.OUT_OF_SYNC);
+    assertStateException(stackId, version, RepositoryVersionState.UPGRADED,
+        RepositoryVersionState.OUT_OF_SYNC);
+    assertStateException(stackId, version,
+        RepositoryVersionState.UPGRADE_FAILED,
+        RepositoryVersionState.OUT_OF_SYNC);
+
+    c1.transitionClusterVersion(stackId, version,
+        RepositoryVersionState.INSTALLING);
+    checkStackVersionState(stackId, version, RepositoryVersionState.INSTALLING);
+
+    c1.transitionClusterVersion(stackId, version,
+        RepositoryVersionState.INSTALLED);
+    checkStackVersionState(stackId, version, RepositoryVersionState.INSTALLED);
+
+    c1.transitionClusterVersion(stackId, version,
+        RepositoryVersionState.UPGRADING);
+    checkStackVersionState(stackId, version, RepositoryVersionState.UPGRADING);
+
+    assertStateException(stackId, version, RepositoryVersionState.CURRENT,
+        RepositoryVersionState.UPGRADING);
+    assertStateException(stackId, version, RepositoryVersionState.INSTALLED,
+        RepositoryVersionState.UPGRADING);
+    assertStateException(stackId, version,
+        RepositoryVersionState.INSTALL_FAILED, RepositoryVersionState.UPGRADING);
+    assertStateException(stackId, version, RepositoryVersionState.OUT_OF_SYNC,
+        RepositoryVersionState.UPGRADING);
+
+    c1.transitionClusterVersion(stackId, version,
+        RepositoryVersionState.UPGRADE_FAILED);
+    checkStackVersionState(stackId, version,
+        RepositoryVersionState.UPGRADE_FAILED);
+
+    assertStateException(stackId, version, RepositoryVersionState.CURRENT,
+        RepositoryVersionState.UPGRADE_FAILED);
+    assertStateException(stackId, version, RepositoryVersionState.INSTALLED,
+        RepositoryVersionState.UPGRADE_FAILED);
+    assertStateException(stackId, version,
+        RepositoryVersionState.INSTALL_FAILED,
+        RepositoryVersionState.UPGRADE_FAILED);
+    assertStateException(stackId, version, RepositoryVersionState.UPGRADED,
+        RepositoryVersionState.UPGRADE_FAILED);
+    assertStateException(stackId, version, RepositoryVersionState.OUT_OF_SYNC,
+        RepositoryVersionState.UPGRADE_FAILED);
+
+    c1.transitionClusterVersion(stackId, version,
+        RepositoryVersionState.UPGRADING);
+    checkStackVersionState(stackId, version, RepositoryVersionState.UPGRADING);
+
+    c1.transitionClusterVersion(stackId, version,
+        RepositoryVersionState.UPGRADED);
+    checkStackVersionState(stackId, version, RepositoryVersionState.UPGRADED);
+
+    assertStateException(stackId, version, RepositoryVersionState.INSTALLED,
+        RepositoryVersionState.UPGRADED);
+    assertStateException(stackId, version,
+        RepositoryVersionState.INSTALL_FAILED, RepositoryVersionState.UPGRADED);
+    assertStateException(stackId, version, RepositoryVersionState.UPGRADING,
+        RepositoryVersionState.UPGRADED);
+    assertStateException(stackId, version,
+        RepositoryVersionState.UPGRADE_FAILED, RepositoryVersionState.UPGRADED);
+    assertStateException(stackId, version, RepositoryVersionState.OUT_OF_SYNC,
+        RepositoryVersionState.UPGRADED);
+
+    c1.transitionClusterVersion(stackId, version,
+        RepositoryVersionState.CURRENT);
+    checkStackVersionState(stackId, version, RepositoryVersionState.CURRENT);
+
+    checkStackVersionState(new StackId("HDP", "0.1"), "0.1",
+        RepositoryVersionState.INSTALLED);
 
     // The only CURRENT state should not be changed
-    assertStateException(stack, version, RepositoryVersionState.INSTALLED, RepositoryVersionState.CURRENT);
+    assertStateException(stackId, version, RepositoryVersionState.INSTALLED,
+        RepositoryVersionState.CURRENT);
   }
 
   @Test
   public void testTransitionClusterVersionTransactionFail() throws Exception {
     createDefaultCluster();
 
-    helper.getOrCreateRepositoryVersion("HDP", "0.2");
-    c1.createClusterVersion("HDP", "0.2", "admin", RepositoryVersionState.INSTALLING);
-    c1.transitionClusterVersion("HDP", "0.2", RepositoryVersionState.INSTALLED);
-    c1.transitionClusterVersion("HDP", "0.2", RepositoryVersionState.UPGRADING);
-    c1.transitionClusterVersion("HDP", "0.2", RepositoryVersionState.UPGRADED);
+    StackId stackId = new StackId("HDP", "0.2");
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    c1.createClusterVersion(stackId, "0.2", "admin",
+        RepositoryVersionState.INSTALLING);
+    c1.transitionClusterVersion(stackId, "0.2",
+        RepositoryVersionState.INSTALLED);
+    c1.transitionClusterVersion(stackId, "0.2",
+        RepositoryVersionState.UPGRADING);
+    c1.transitionClusterVersion(stackId, "0.2", RepositoryVersionState.UPGRADED);
     try {
       ClusterVersionDAOMock.failOnCurrentVersionState = true;
-      c1.transitionClusterVersion("HDP", "0.2", RepositoryVersionState.CURRENT);
+      c1.transitionClusterVersion(stackId, "0.2",
+          RepositoryVersionState.CURRENT);
       Assert.fail();
     } catch (AmbariException e) {
 
@@ -1236,12 +1305,17 @@ public class ClusterTest {
   public void testInferHostVersions() throws Exception {
     createDefaultCluster();
 
-    helper.getOrCreateRepositoryVersion("HDP", "0.2");
-    c1.createClusterVersion("HDP", "0.2", "admin", RepositoryVersionState.INSTALLING);
+    StackId stackId = new StackId("HDP", "0.2");
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    c1.createClusterVersion(stackId, "0.2", "admin",
+        RepositoryVersionState.INSTALLING);
     ClusterVersionEntity entityHDP2 = null;
     for (ClusterVersionEntity entity : c1.getAllClusterVersions()) {
-      if (entity.getRepositoryVersion().getStack().equals("HDP")
-          && entity.getRepositoryVersion().getVersion().equals("0.2")) {
+      StackEntity repoVersionStackEntity = entity.getRepositoryVersion().getStack();
+      StackId repoVersionStackId = new StackId(repoVersionStackEntity);
+
+      if (repoVersionStackId.getStackName().equals("HDP")
+          && repoVersionStackId.getStackVersion().equals("0.2")) {
         entityHDP2 = entity;
         break;
       }
@@ -1258,8 +1332,9 @@ public class ClusterTest {
 
     boolean checked = false;
     for (HostVersionEntity entity : hostVersionsH1After) {
-      if (entity.getRepositoryVersion().getStack().equals("HDP")
-          && entity.getRepositoryVersion().getVersion().equals("0.2")) {
+      StackEntity repoVersionStackEntity = entity.getRepositoryVersion().getStack();
+      if (repoVersionStackEntity.getStackName().equals("HDP")
+          && repoVersionStackEntity.getStackVersion().equals("0.2")) {
         assertEquals(RepositoryVersionState.INSTALLING, entity.getState());
         checked = true;
         break;
@@ -1275,8 +1350,9 @@ public class ClusterTest {
 
     checked = false;
     for (HostVersionEntity entity : hostVersionsH1After) {
-      if (entity.getRepositoryVersion().getStack().equals("HDP")
-          && entity.getRepositoryVersion().getVersion().equals("0.2")) {
+      StackEntity repoVersionStackEntity = entity.getRepositoryVersion().getStack();
+      if (repoVersionStackEntity.getStackName().equals("HDP")
+          && repoVersionStackEntity.getStackVersion().equals("0.2")) {
         assertEquals(RepositoryVersionState.INSTALLING, entity.getState());
         checked = true;
         break;
@@ -1298,10 +1374,12 @@ public class ClusterTest {
     // Phase 1: Install bits during distribution
     StackId stackId = new StackId("HDP-0.1");
     final String stackVersion = "1.0-1000";
-    RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(stackId.getStackId(),
+    RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(
+        stackId,
         stackVersion);
     // Because the cluster already has a Cluster Version, an additional stack must init with INSTALLING
-    c1.createClusterVersion(stackId.getStackId(), stackVersion, "admin", RepositoryVersionState.INSTALLING);
+    c1.createClusterVersion(stackId, stackVersion, "admin",
+        RepositoryVersionState.INSTALLING);
     c1.setCurrentStackVersion(stackId);
 
     HostVersionEntity hv1 = helper.createHostVersion("h1", repositoryVersionEntity, RepositoryVersionState.INSTALLING);
@@ -1309,64 +1387,77 @@ public class ClusterTest {
 
     c1.recalculateClusterVersionState(stackVersion);
     //Should remain in its current state
-    checkStackVersionState(stackId.getStackId(), stackVersion, RepositoryVersionState.INSTALLING);
+    checkStackVersionState(stackId, stackVersion,
+        RepositoryVersionState.INSTALLING);
 
     h2.setState(HostState.UNHEALTHY);
     c1.recalculateClusterVersionState(stackVersion);
     // In order for the states to be accurately reflected, the host health status should not impact the status
     // of the host_version.
-    checkStackVersionState(stackId.getStackId(), stackVersion, RepositoryVersionState.INSTALLING);
+    checkStackVersionState(stackId, stackVersion,
+        RepositoryVersionState.INSTALLING);
     // Retry by going back to INSTALLING
-    c1.transitionClusterVersion(stackId.getStackId(), stackVersion, RepositoryVersionState.INSTALLING);
+    c1.transitionClusterVersion(stackId, stackVersion,
+        RepositoryVersionState.INSTALLING);
 
     h2.setState(HostState.HEALTHY);
     hv2.setState(RepositoryVersionState.INSTALLED);
     hostVersionDAO.merge(hv2);
     c1.recalculateClusterVersionState(stackVersion);
-    checkStackVersionState(stackId.getStackId(), stackVersion, RepositoryVersionState.INSTALLING);
+    checkStackVersionState(stackId, stackVersion,
+        RepositoryVersionState.INSTALLING);
 
     // Make one host fail
     hv1.setState(RepositoryVersionState.INSTALL_FAILED);
     hostVersionDAO.merge(hv1);
     c1.recalculateClusterVersionState(stackVersion);
-    checkStackVersionState(stackId.getStackId(), stackVersion, RepositoryVersionState.INSTALL_FAILED);
+    checkStackVersionState(stackId, stackVersion,
+        RepositoryVersionState.INSTALL_FAILED);
     // Retry by going back to INSTALLING
-    c1.transitionClusterVersion(stackId.getStackId(), stackVersion, RepositoryVersionState.INSTALLING);
+    c1.transitionClusterVersion(stackId, stackVersion,
+        RepositoryVersionState.INSTALLING);
 
     // Now, all hosts are in INSTALLED
     hv1.setState(RepositoryVersionState.INSTALLED);
     hostVersionDAO.merge(hv1);
     c1.recalculateClusterVersionState(stackVersion);
-    checkStackVersionState(stackId.getStackId(), stackVersion, RepositoryVersionState.INSTALLED);
+    checkStackVersionState(stackId, stackVersion,
+        RepositoryVersionState.INSTALLED);
 
     // Phase 2: Upgrade stack
     hv1.setState(RepositoryVersionState.UPGRADING);
     hostVersionDAO.merge(hv1);
     c1.recalculateClusterVersionState(stackVersion);
-    checkStackVersionState(stackId.getStackId(), stackVersion, RepositoryVersionState.UPGRADING);
+    checkStackVersionState(stackId, stackVersion,
+        RepositoryVersionState.UPGRADING);
 
     hv2.setState(RepositoryVersionState.UPGRADING);
     hostVersionDAO.merge(hv2);
     c1.recalculateClusterVersionState(stackVersion);
-    checkStackVersionState(stackId.getStackId(), stackVersion, RepositoryVersionState.UPGRADING);
+    checkStackVersionState(stackId, stackVersion,
+        RepositoryVersionState.UPGRADING);
 
     hv2.setState(RepositoryVersionState.UPGRADE_FAILED);
     hostVersionDAO.merge(hv2);
     c1.recalculateClusterVersionState(stackVersion);
-    checkStackVersionState(stackId.getStackId(), stackVersion, RepositoryVersionState.UPGRADE_FAILED);
+    checkStackVersionState(stackId, stackVersion,
+        RepositoryVersionState.UPGRADE_FAILED);
     // Retry by going back to UPGRADING
-    c1.transitionClusterVersion(stackId.getStackId(), stackVersion, RepositoryVersionState.UPGRADING);
+    c1.transitionClusterVersion(stackId, stackVersion,
+        RepositoryVersionState.UPGRADING);
 
     hv2.setState(RepositoryVersionState.UPGRADED);
     hostVersionDAO.merge(hv2);
     c1.recalculateClusterVersionState(stackVersion);
-    checkStackVersionState(stackId.getStackId(), stackVersion, RepositoryVersionState.UPGRADING);
+    checkStackVersionState(stackId, stackVersion,
+        RepositoryVersionState.UPGRADING);
 
     // Now both hosts are UPGRADED
     hv1.setState(RepositoryVersionState.UPGRADED);
     hostVersionDAO.merge(hv1);
     c1.recalculateClusterVersionState(stackVersion);
-    checkStackVersionState(stackId.getStackId(), stackVersion, RepositoryVersionState.UPGRADED);
+    checkStackVersionState(stackId, stackVersion,
+        RepositoryVersionState.UPGRADED);
 
     // Set both hosts to CURRENT
     hv1.setState(RepositoryVersionState.CURRENT);
@@ -1374,7 +1465,8 @@ public class ClusterTest {
     hv2.setState(RepositoryVersionState.CURRENT);
     hostVersionDAO.merge(hv2);
     c1.recalculateClusterVersionState(stackVersion);
-    checkStackVersionState(stackId.getStackId(), stackVersion, RepositoryVersionState.CURRENT);
+    checkStackVersionState(stackId, stackVersion,
+        RepositoryVersionState.CURRENT);
   }
 
   @Test
@@ -1388,34 +1480,41 @@ public class ClusterTest {
     h2.setState(HostState.HEALTHY);
 
     StackId stackId = new StackId("HDP-0.1");
-    RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(stackId.getStackId(),
+    RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(
+        stackId,
         "1.0-1000");
-    c1.createClusterVersion(stackId.getStackId(), "1.0-1000", "admin", RepositoryVersionState.INSTALLING);
+    c1.createClusterVersion(stackId, "1.0-1000", "admin",
+        RepositoryVersionState.INSTALLING);
     c1.setCurrentStackVersion(stackId);
     c1.recalculateAllClusterVersionStates();
-    checkStackVersionState(stackId.getStackId(), "1.0-1000", RepositoryVersionState.INSTALLING);
-    checkStackVersionState(stackId.getStackId(), "1.0-2086", RepositoryVersionState.CURRENT);
+    checkStackVersionState(stackId, "1.0-1000",
+        RepositoryVersionState.INSTALLING);
+    checkStackVersionState(stackId, "1.0-2086", RepositoryVersionState.CURRENT);
 
     HostVersionEntity hv1 = helper.createHostVersion("h1", repositoryVersionEntity, RepositoryVersionState.INSTALLING);
     HostVersionEntity hv2 = helper.createHostVersion("h2", repositoryVersionEntity, RepositoryVersionState.INSTALLING);
 
     c1.recalculateAllClusterVersionStates();
-    checkStackVersionState(stackId.getStackId(), "1.0-1000", RepositoryVersionState.INSTALLING);
-    checkStackVersionState(stackId.getStackId(), "1.0-2086", RepositoryVersionState.CURRENT);
+    checkStackVersionState(stackId, "1.0-1000",
+        RepositoryVersionState.INSTALLING);
+    checkStackVersionState(stackId, "1.0-2086", RepositoryVersionState.CURRENT);
 
     hv1.setState(RepositoryVersionState.INSTALL_FAILED);
     hostVersionDAO.merge(hv1);
     c1.recalculateAllClusterVersionStates();
-    checkStackVersionState(stackId.getStackId(), "1.0-1000", RepositoryVersionState.INSTALL_FAILED);
-    checkStackVersionState(stackId.getStackId(), "1.0-2086", RepositoryVersionState.CURRENT);
+    checkStackVersionState(stackId, "1.0-1000",
+        RepositoryVersionState.INSTALL_FAILED);
+    checkStackVersionState(stackId, "1.0-2086", RepositoryVersionState.CURRENT);
     // Retry by going back to INSTALLING
-    c1.transitionClusterVersion(stackId.getStackId(), "1.0-1000", RepositoryVersionState.INSTALLING);
+    c1.transitionClusterVersion(stackId, "1.0-1000",
+        RepositoryVersionState.INSTALLING);
 
     hv1.setState(RepositoryVersionState.CURRENT);
     hostVersionDAO.merge(hv1);
     c1.recalculateAllClusterVersionStates();
-    checkStackVersionState(stackId.getStackId(), "1.0-1000", RepositoryVersionState.OUT_OF_SYNC);
-    checkStackVersionState(stackId.getStackId(), "1.0-2086", RepositoryVersionState.CURRENT);
+    checkStackVersionState(stackId, "1.0-1000",
+        RepositoryVersionState.OUT_OF_SYNC);
+    checkStackVersionState(stackId, "1.0-2086", RepositoryVersionState.CURRENT);
   }
 
   /**
@@ -1470,7 +1569,7 @@ public class ClusterTest {
 
       if (versionedComponentCount > 0) {
         // On the first component with a version, a RepoVersion should have been created
-        RepositoryVersionEntity repositoryVersion = repositoryVersionDAO.findByStackAndVersion(stackId.getStackId(), v1);
+        RepositoryVersionEntity repositoryVersion = repositoryVersionDAO.findByStackAndVersion(stackId, v1);
         Assert.assertNotNull(repositoryVersion);
         Assert.assertTrue(clusterVersions != null && clusterVersions.size() == 1);
 
@@ -1512,23 +1611,26 @@ public class ClusterTest {
 
     Collection<HostVersionEntity> hostVersions = hostVersionDAO.findAll();
     Assert.assertEquals(hostVersions.size(), clusters.getHosts().size());
-    HostVersionEntity h4Version1 = hostVersionDAO.findByClusterStackVersionAndHost(clusterName, stackId.getStackId(), v1, "h-4");
+    HostVersionEntity h4Version1 = hostVersionDAO.findByClusterStackVersionAndHost(clusterName, stackId, v1, "h-4");
     Assert.assertNotNull(h4Version1);
     Assert.assertEquals(h4Version1.getState(), RepositoryVersionState.CURRENT);
 
     // Distribute bits for a new repo
     String v2 = "2.2.0-456";
-    RepositoryVersionEntity rv2 = helper.getOrCreateRepositoryVersion(stackId.getStackId(), v2);
+    RepositoryVersionEntity rv2 = helper.getOrCreateRepositoryVersion(stackId,
+        v2);
     for(String hostName : clusters.getHostsForCluster(clusterName).keySet()) {
       HostEntity host = hostDAO.findByName(hostName);
       HostVersionEntity hve = new HostVersionEntity(hostName, rv2, RepositoryVersionState.INSTALLED);
       hve.setHostEntity(host);
       hostVersionDAO.create(hve);
     }
-    cluster.createClusterVersion(stackId.getStackId(), v2, "admin", RepositoryVersionState.INSTALLING);
-    cluster.transitionClusterVersion(stackId.getStackId(), v2, RepositoryVersionState.INSTALLED);
+    cluster.createClusterVersion(stackId, v2, "admin",
+        RepositoryVersionState.INSTALLING);
+    cluster.transitionClusterVersion(stackId, v2,
+        RepositoryVersionState.INSTALLED);
 
-    ClusterVersionEntity cv2 = clusterVersionDAO.findByClusterAndStackAndVersion(clusterName, stackId.getStackId(), v2);
+    ClusterVersionEntity cv2 = clusterVersionDAO.findByClusterAndStackAndVersion(clusterName, stackId, v2);
     Assert.assertNotNull(cv2);
     Assert.assertEquals(cv2.getState(), RepositoryVersionState.INSTALLED);
 
@@ -1540,7 +1642,7 @@ public class ClusterTest {
     schHost5Serv3CompB.persist();
 
     // Host 5 will be in OUT_OF_SYNC, so redistribute bits to it so that it reaches a state of INSTALLED
-    HostVersionEntity h5Version2 = hostVersionDAO.findByClusterStackVersionAndHost(clusterName, stackId.getStackId(), v2, "h-5");
+    HostVersionEntity h5Version2 = hostVersionDAO.findByClusterStackVersionAndHost(clusterName, stackId, v2, "h-5");
     Assert.assertNotNull(h5Version2);
     Assert.assertEquals(h5Version2.getState(), RepositoryVersionState.OUT_OF_SYNC);
 
@@ -1578,13 +1680,13 @@ public class ClusterTest {
 
       if (versionedComponentCount > 0) {
         // On the first component with a version, a RepoVersion should have been created
-        RepositoryVersionEntity repositoryVersion = repositoryVersionDAO.findByStackAndVersion(stackId.getStackId(), v2);
+        RepositoryVersionEntity repositoryVersion = repositoryVersionDAO.findByStackAndVersion(stackId, v2);
         Assert.assertNotNull(repositoryVersion);
         Assert.assertTrue(clusterVersions != null && clusterVersions.size() == 2);
 
         // First component to report a version should cause the ClusterVersion to go to UPGRADING
         if (versionedComponentCount == 1 && i < (hostComponentStates.size() - 1)) {
-          cv2 = clusterVersionDAO.findByClusterAndStackAndVersion(clusterName, stackId.getStackId(), v2);
+          cv2 = clusterVersionDAO.findByClusterAndStackAndVersion(clusterName, stackId, v2);
           Assert.assertEquals(cv2.getState(), RepositoryVersionState.UPGRADING);
         }
       }
@@ -1592,10 +1694,10 @@ public class ClusterTest {
 
     // Last component to report a version should still keep the ClusterVersion in UPGRADING because
     // hosts 3 and 5 only have Ganglia and the HostVersion will remain in INSTALLED
-    cv2 = clusterVersionDAO.findByClusterAndStackAndVersion(clusterName, stackId.getStackId(), v2);
+    cv2 = clusterVersionDAO.findByClusterAndStackAndVersion(clusterName, stackId, v2);
     Assert.assertEquals(cv2.getState(), RepositoryVersionState.UPGRADING);
 
-    Collection<HostVersionEntity> v2HostVersions = hostVersionDAO.findByClusterStackAndVersion(clusterName, stackId.getStackId(), v2);
+    Collection<HostVersionEntity> v2HostVersions = hostVersionDAO.findByClusterStackAndVersion(clusterName, stackId, v2);
     Assert.assertEquals(v2HostVersions.size(), clusters.getHostsForCluster(clusterName).size());
     for (HostVersionEntity hve : v2HostVersions) {
       if (hve.getHostName().equals("h-3") || hve.getHostName().equals("h-5")) {
@@ -1608,8 +1710,10 @@ public class ClusterTest {
 
   @Test
   public void testTransitionNonReportableHost() throws Exception {
+    StackId stackId = new StackId("HDP-2.0.5");
+
     String clusterName = "c1";
-    clusters.addCluster(clusterName);
+    clusters.addCluster(clusterName, stackId);
     Cluster c1 = clusters.getCluster(clusterName);
     Assert.assertEquals(clusterName, c1.getClusterName());
     Assert.assertEquals(1, c1.getClusterId());
@@ -1632,14 +1736,16 @@ public class ClusterTest {
 
     String v1 = "2.0.5-1";
     String v2 = "2.0.5-2";
-    StackId stackId = new StackId("HDP-2.0.5");
     c1.setDesiredStackVersion(stackId);
-    RepositoryVersionEntity rve1 = helper.getOrCreateRepositoryVersion(stackId.getStackName(), v1);
-    RepositoryVersionEntity rve2 = helper.getOrCreateRepositoryVersion(stackId.getStackName(), v2);
+    RepositoryVersionEntity rve1 = helper.getOrCreateRepositoryVersion(stackId,
+        v1);
+    RepositoryVersionEntity rve2 = helper.getOrCreateRepositoryVersion(stackId,
+        v2);
 
     c1.setCurrentStackVersion(stackId);
-    c1.createClusterVersion(stackId.getStackName(), v1, "admin", RepositoryVersionState.UPGRADING);
-    c1.transitionClusterVersion(stackId.getStackName(), v1, RepositoryVersionState.CURRENT);
+    c1.createClusterVersion(stackId, v1, "admin",
+        RepositoryVersionState.UPGRADING);
+    c1.transitionClusterVersion(stackId, v1, RepositoryVersionState.CURRENT);
 
     clusters.mapHostToCluster("h-1", clusterName);
     clusters.mapHostToCluster("h-2", clusterName);
@@ -1658,12 +1764,13 @@ public class ClusterTest {
     List<HostVersionEntity> entities = hostVersionDAO.findByClusterAndHost(clusterName, "h-3");
     assertTrue("Expected no host versions", null == entities || 0 == entities.size());
 
-    c1.createClusterVersion(stackId.getStackName(), v2, "admin", RepositoryVersionState.INSTALLING);
-    c1.transitionClusterVersion(stackId.getStackName(), v2, RepositoryVersionState.INSTALLED);
-    c1.transitionClusterVersion(stackId.getStackName(), v2, RepositoryVersionState.UPGRADING);
-    c1.transitionClusterVersion(stackId.getStackName(), v2, RepositoryVersionState.UPGRADED);
+    c1.createClusterVersion(stackId, v2, "admin",
+        RepositoryVersionState.INSTALLING);
+    c1.transitionClusterVersion(stackId, v2, RepositoryVersionState.INSTALLED);
+    c1.transitionClusterVersion(stackId, v2, RepositoryVersionState.UPGRADING);
+    c1.transitionClusterVersion(stackId, v2, RepositoryVersionState.UPGRADED);
 
-    c1.transitionClusterVersion(stackId.getStackName(), v2, RepositoryVersionState.CURRENT);
+    c1.transitionClusterVersion(stackId, v2, RepositoryVersionState.CURRENT);
 
     entities = hostVersionDAO.findByClusterAndHost(clusterName, "h-3");
 
@@ -1677,8 +1784,9 @@ public class ClusterTest {
      * and we add a new host to cluster. On a new host, both CURRENT and OUT_OF_SYNC host
      * versions should be present
      */
+    StackId stackId = new StackId("HDP-2.0.5");
     String clusterName = "c1";
-    clusters.addCluster(clusterName);
+    clusters.addCluster(clusterName, stackId);
     final Cluster c1 = clusters.getCluster(clusterName);
     Assert.assertEquals(clusterName, c1.getClusterName());
     Assert.assertEquals(1, c1.getClusterId());
@@ -1702,15 +1810,16 @@ public class ClusterTest {
 
     String v1 = "2.0.5-1";
     String v2 = "2.0.5-2";
-    StackId stackId = new StackId("HDP-2.0.5");
     c1.setDesiredStackVersion(stackId);
-    RepositoryVersionEntity rve1 = helper.getOrCreateRepositoryVersion(stackId.getStackId()
-            , v1);
-    RepositoryVersionEntity rve2 = helper.getOrCreateRepositoryVersion(stackId.getStackId(), v2);
+    RepositoryVersionEntity rve1 = helper.getOrCreateRepositoryVersion(stackId,
+        v1);
+    RepositoryVersionEntity rve2 = helper.getOrCreateRepositoryVersion(stackId,
+        v2);
 
     c1.setCurrentStackVersion(stackId);
-    c1.createClusterVersion(stackId.getStackId(), v1, "admin", RepositoryVersionState.UPGRADING);
-    c1.transitionClusterVersion(stackId.getStackId(), v1, RepositoryVersionState.CURRENT);
+    c1.createClusterVersion(stackId, v1, "admin",
+        RepositoryVersionState.UPGRADING);
+    c1.transitionClusterVersion(stackId, v1, RepositoryVersionState.CURRENT);
 
     clusters.mapHostToCluster("h-1", clusterName);
     clusters.mapHostToCluster("h-2", clusterName);
@@ -1722,9 +1831,10 @@ public class ClusterTest {
     sc.addServiceComponentHost("h-1");
     sc.addServiceComponentHost("h-2");
 
-    c1.createClusterVersion(stackId.getStackId(), v2, "admin", RepositoryVersionState.INSTALLING);
-    c1.transitionClusterVersion(stackId.getStackId(), v2, RepositoryVersionState.INSTALLED);
-    c1.transitionClusterVersion(stackId.getStackId(), v2, RepositoryVersionState.OUT_OF_SYNC);
+    c1.createClusterVersion(stackId, v2, "admin",
+        RepositoryVersionState.INSTALLING);
+    c1.transitionClusterVersion(stackId, v2, RepositoryVersionState.INSTALLED);
+    c1.transitionClusterVersion(stackId, v2, RepositoryVersionState.OUT_OF_SYNC);
 
     clusters.mapHostToCluster(h3, clusterName);
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/746df034/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersDeadlockTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersDeadlockTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersDeadlockTest.java
index d771eba..7cb7679 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersDeadlockTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersDeadlockTest.java
@@ -29,6 +29,7 @@ import junit.framework.Assert;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.ServiceComponentNotFoundException;
 import org.apache.ambari.server.ServiceNotFoundException;
+import org.apache.ambari.server.events.listeners.upgrade.HostVersionOutOfSyncListener;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
@@ -44,14 +45,18 @@ import org.apache.ambari.server.state.ServiceComponentHostFactory;
 import org.apache.ambari.server.state.ServiceFactory;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.State;
+import org.easymock.EasyMock;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.inject.Binder;
 import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
+import com.google.inject.Module;
 import com.google.inject.persist.PersistService;
+import com.google.inject.util.Modules;
 
 /**
  * Tests AMBARI-9738 which produced a deadlock during read and writes between
@@ -88,16 +93,19 @@ public class ClustersDeadlockTest {
 
   @Before
   public void setup() throws Exception {
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    injector = Guice.createInjector(Modules.override(
+        new InMemoryDefaultTestModule()).with(new MockModule()));
+
     injector.getInstance(GuiceJpaInitializer.class);
     injector.injectMembers(this);
-    clusters.addCluster(CLUSTER_NAME);
 
     StackId stackId = new StackId("HDP-0.1");
+    clusters.addCluster(CLUSTER_NAME, stackId);
+
     cluster = clusters.getCluster(CLUSTER_NAME);
-    cluster.setDesiredStackVersion(stackId);
-    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
-    cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    cluster.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
 
     // install HDFS
     installService("HDFS");
@@ -114,7 +122,7 @@ public class ClustersDeadlockTest {
    *
    * @throws Exception
    */
-  @Test(timeout = 35000)
+  @Test(timeout = 40000)
   public void testDeadlockWhileMappingHosts() throws Exception {
     List<Thread> threads = new ArrayList<Thread>();
     for (int i = 0; i < NUMBER_OF_THREADS; i++) {
@@ -143,7 +151,7 @@ public class ClustersDeadlockTest {
    *
    * @throws Exception
    */
-  @Test(timeout = 35000)
+  @Test(timeout = 40000)
   public void testDeadlockWhileMappingHostsWithExistingServices()
       throws Exception {
     List<Thread> threads = new ArrayList<Thread>();
@@ -169,7 +177,7 @@ public class ClustersDeadlockTest {
    *
    * @throws Exception
    */
-  @Test(timeout = 35000)
+  @Test(timeout = 40000)
   public void testDeadlockWhileUnmappingHosts() throws Exception {
     List<Thread> threads = new ArrayList<Thread>();
     for (int i = 0; i < NUMBER_OF_THREADS; i++) {
@@ -363,4 +371,20 @@ public class ClustersDeadlockTest {
     sch.persist();
     return sch;
   }
+
+  /**
+  *
+  */
+  private class MockModule implements Module {
+    /**
+    *
+    */
+    @Override
+    public void configure(Binder binder) {
+      // this listener gets in the way of actually testing the concurrency
+      // between the threads; it slows them down too much, so mock it out
+      binder.bind(HostVersionOutOfSyncListener.class).toInstance(
+          EasyMock.createNiceMock(HostVersionOutOfSyncListener.class));
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/746df034/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java
index 20eafe0..ce1fd34 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java
@@ -116,12 +116,14 @@ public class ClustersTest {
   @Test
   public void testAddAndGetCluster() throws AmbariException {
 
+    StackId stackId = new StackId("HDP-2.1.1");
+
     String c1 = "foo";
     String c2 = "foo";
-    clusters.addCluster(c1);
+    clusters.addCluster(c1, stackId);
 
     try {
-      clusters.addCluster(c1);
+      clusters.addCluster(c1, stackId);
       fail("Exception should be thrown on invalid add");
     }
     catch (AmbariException e) {
@@ -129,7 +131,7 @@ public class ClustersTest {
     }
 
     try {
-      clusters.addCluster(c2);
+      clusters.addCluster(c2, stackId);
       fail("Exception should be thrown on invalid add");
     }
     catch (AmbariException e) {
@@ -137,7 +139,7 @@ public class ClustersTest {
     }
 
     c2 = "foo2";
-    clusters.addCluster(c2);
+    clusters.addCluster(c2, stackId);
 
     Assert.assertNotNull(clusters.getCluster(c1));
     Assert.assertNotNull(clusters.getCluster(c2));
@@ -218,21 +220,27 @@ public class ClustersTest {
       // Expected
     }
 
-    clusters.addCluster(c1);
-    clusters.addCluster(c2);
+    StackId stackId = new StackId("HDP-0.1");
+
+    clusters.addCluster(c1, stackId);
+    clusters.addCluster(c2, stackId);
 
     Cluster cluster1 = clusters.getCluster(c1);
     Cluster cluster2 = clusters.getCluster(c2);
     Assert.assertNotNull(clusters.getCluster(c1));
     Assert.assertNotNull(clusters.getCluster(c2));
-    StackId stackId = new StackId("HDP-0.1");
+
     cluster1.setDesiredStackVersion(stackId);
-    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
-    cluster1.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
-    cluster1.transitionClusterVersion(stackId.getStackName(), stackId.getStackVersion(), RepositoryVersionState.CURRENT);
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    cluster1.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
+    cluster1.transitionClusterVersion(stackId, stackId.getStackVersion(),
+        RepositoryVersionState.CURRENT);
     cluster2.setDesiredStackVersion(stackId);
-    cluster2.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
-    cluster2.transitionClusterVersion(stackId.getStackName(), stackId.getStackVersion(), RepositoryVersionState.CURRENT);
+    cluster2.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
+    cluster2.transitionClusterVersion(stackId, stackId.getStackVersion(),
+        RepositoryVersionState.CURRENT);
 
     try {
       clusters.mapHostToCluster(h1, c1);
@@ -307,20 +315,26 @@ public class ClustersTest {
     String h1 = "h1";
     String h2 = "h2";
     String h3 = "h3";
-    clusters.addCluster(c1);
-    clusters.addCluster(c2);
+
+    StackId stackId = new StackId("HDP-0.1");
+
+    clusters.addCluster(c1, stackId);
+    clusters.addCluster(c2, stackId);
     Cluster cluster1 = clusters.getCluster(c1);
     Cluster cluster2 = clusters.getCluster(c2);
     Assert.assertNotNull(clusters.getCluster(c1));
     Assert.assertNotNull(clusters.getCluster(c2));
-    StackId stackId = new StackId("HDP-0.1");
-    cluster1.setDesiredStackVersion(stackId);
-    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
-    cluster1.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
-    cluster1.transitionClusterVersion(stackId.getStackName(), stackId.getStackVersion(), RepositoryVersionState.CURRENT);
+
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    cluster1.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
+    cluster1.transitionClusterVersion(stackId, stackId.getStackVersion(),
+        RepositoryVersionState.CURRENT);
     cluster2.setDesiredStackVersion(stackId);
-    cluster2.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
-    cluster2.transitionClusterVersion(stackId.getStackName(), stackId.getStackVersion(), RepositoryVersionState.CURRENT);
+    cluster2.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
+    cluster2.transitionClusterVersion(stackId, stackId.getStackVersion(),
+        RepositoryVersionState.CURRENT);
     clusters.addHost(h1);
     clusters.addHost(h2);
     clusters.addHost(h3);
@@ -344,15 +358,18 @@ public class ClustersTest {
     final String h1 = "h1";
     final String h2 = "h2";
 
-    clusters.addCluster(c1);
+    StackId stackId = new StackId("HDP-0.1");
+    clusters.addCluster(c1, stackId);
 
     Cluster cluster = clusters.getCluster(c1);
-    StackId stackId = new StackId("HDP-0.1");
+
     cluster.setDesiredStackVersion(stackId);
     cluster.setCurrentStackVersion(stackId);
-    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
-    cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
-    cluster.transitionClusterVersion(stackId.getStackName(), stackId.getStackVersion(), RepositoryVersionState.CURRENT);
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    cluster.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
+    cluster.transitionClusterVersion(stackId, stackId.getStackVersion(),
+        RepositoryVersionState.CURRENT);
 
     final Config config1 = injector.getInstance(ConfigFactory.class).createNew(cluster, "t1",
         new HashMap<String, String>() {{
@@ -479,14 +496,18 @@ public class ClustersTest {
       Assert.assertTrue(e.getMessage().contains("Cluster not found"));
     }
 
-    clusters.addCluster(c1);
+    clusters.addCluster(c1, stackId);
     clusters.setCurrentStackVersion(c1, stackId);
 
     Assert.assertNotNull(clusters.getCluster(c1));
     ClusterStateEntity entity = injector.getInstance(ClusterStateDAO.class).findByPK(clusters.getCluster(c1).getClusterId());
     Assert.assertNotNull(entity);
-    Assert.assertTrue(entity.getCurrentStackVersion().contains(stackId.getStackName()) &&
-        entity.getCurrentStackVersion().contains(stackId.getStackVersion()));
+
+    Assert.assertTrue(entity.getCurrentStack().getStackName().equals(
+        stackId.getStackName())
+        && entity.getCurrentStack().getStackVersion().equals(
+            stackId.getStackVersion()));
+
     Assert.assertTrue(clusters.getCluster(c1).getCurrentStackVersion().getStackName().equals(stackId.getStackName()));
     Assert.assertTrue(
         clusters.getCluster(c1).getCurrentStackVersion().getStackVersion().equals(stackId.getStackVersion()));

http://git-wip-us.apache.org/repos/asf/ambari/blob/746df034/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java
index 27b2a0e..4248d13 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java
@@ -359,11 +359,13 @@ public class HostTest {
   public void testHostDesiredConfig() throws Exception {
     AmbariMetaInfo metaInfo = injector.getInstance(AmbariMetaInfo.class);
 
-    clusters.addCluster("c1");
-    Cluster c1 = clusters.getCluster("c1");
     StackId stackId = new StackId("HDP-0.1");
-    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
-    c1.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
+    clusters.addCluster("c1", stackId);
+    Cluster c1 = clusters.getCluster("c1");
+
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    c1.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
     Assert.assertEquals("c1", c1.getClusterName());
     Assert.assertEquals(1, c1.getClusterId());
     clusters.addHost("h1");
@@ -420,7 +422,8 @@ public class HostTest {
   public void testHostMaintenance() throws Exception {
     AmbariMetaInfo metaInfo = injector.getInstance(AmbariMetaInfo.class);
 
-    clusters.addCluster("c1");
+    StackId stackId = new StackId("HDP-0.1");
+    clusters.addCluster("c1", stackId);
     Cluster c1 = clusters.getCluster("c1");
     Assert.assertEquals("c1", c1.getClusterName());
     Assert.assertEquals(1, c1.getClusterId());
@@ -435,9 +438,10 @@ public class HostTest {
     host.setHostAttributes(hostAttributes);
 
     host.persist();
-    StackId stackId = new StackId("HDP-0.1");
-    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
-    c1.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
+
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    c1.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
     c1.setDesiredStackVersion(stackId);
     clusters.mapHostToCluster("h1", "c1");
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/746df034/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java
index 137575e..fde1945 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java
@@ -109,15 +109,16 @@ public class ServiceComponentHostTest {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
     injector.injectMembers(this);
-    clusters.addCluster("C1");
+    StackId stackId = new StackId("HDP-0.1");
+    clusters.addCluster("C1", stackId);
     clusters.addHost("h1");
     setOsFamily(clusters.getHost("h1"), "redhat", "5.9");
     clusters.getHost("h1").persist();
-    StackId stackId = new StackId("HDP-0.1");
+
     Cluster c1 = clusters.getCluster("C1");
-    c1.setDesiredStackVersion(stackId);
-    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
-    c1.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    c1.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
     clusters.mapHostToCluster("h1","C1");
   }
 
@@ -190,7 +191,7 @@ public class ServiceComponentHostTest {
     Assert.assertFalse(
         impl.getDesiredStackVersion().getStackId().isEmpty());
 
-    Assert.assertTrue(impl.getStackVersion().getStackId().isEmpty());
+    Assert.assertFalse(impl.getStackVersion().getStackId().isEmpty());
 
     return impl;
   }
@@ -263,7 +264,6 @@ public class ServiceComponentHostTest {
     boolean checkStack = false;
     if (startEventType == ServiceComponentHostEventType.HOST_SVCCOMP_INSTALL) {
       checkStack = true;
-      impl.setStackVersion(null);
     }
 
     Assert.assertEquals(startState,
@@ -515,14 +515,14 @@ public class ServiceComponentHostTest {
         createNewServiceComponentHost("HDFS", "NAMENODE", "h1", false);
     sch.setDesiredState(State.INSTALLED);
     sch.setState(State.INSTALLING);
-    sch.setStackVersion(new StackId("HDP-1.0.0"));
-    sch.setDesiredStackVersion(new StackId("HDP-1.1.0"));
+    sch.setStackVersion(new StackId("HDP-1.2.0"));
+    sch.setDesiredStackVersion(new StackId("HDP-1.2.0"));
 
     Assert.assertEquals(State.INSTALLING, sch.getState());
     Assert.assertEquals(State.INSTALLED, sch.getDesiredState());
-    Assert.assertEquals("HDP-1.0.0",
+    Assert.assertEquals("HDP-1.2.0",
         sch.getStackVersion().getStackId());
-    Assert.assertEquals("HDP-1.1.0",
+    Assert.assertEquals("HDP-1.2.0",
         sch.getDesiredStackVersion().getStackId());
   }
 
@@ -532,8 +532,8 @@ public class ServiceComponentHostTest {
         createNewServiceComponentHost("HDFS", "NAMENODE", "h1", false);
     sch.setDesiredState(State.INSTALLED);
     sch.setState(State.INSTALLING);
-    sch.setStackVersion(new StackId("HDP-1.0.0"));
-    sch.setDesiredStackVersion(new StackId("HDP-1.1.0"));
+    sch.setStackVersion(new StackId("HDP-1.2.0"));
+    sch.setDesiredStackVersion(new StackId("HDP-1.2.0"));
 
     Cluster cluster = clusters.getCluster("C1");
 
@@ -567,7 +567,7 @@ public class ServiceComponentHostTest {
         createNewServiceComponentHost("HDFS", "DATANODE", "h1", false);
     sch.setDesiredState(State.INSTALLED);
     sch.setState(State.INSTALLING);
-    sch.setStackVersion(new StackId("HDP-1.0.0"));
+    sch.setStackVersion(new StackId("HDP-1.2.0"));
     ServiceComponentHostResponse r =
         sch.convertToResponse();
     Assert.assertEquals("HDFS", r.getServiceName());
@@ -576,7 +576,7 @@ public class ServiceComponentHostTest {
     Assert.assertEquals("C1", r.getClusterName());
     Assert.assertEquals(State.INSTALLED.toString(), r.getDesiredState());
     Assert.assertEquals(State.INSTALLING.toString(), r.getLiveState());
-    Assert.assertEquals("HDP-1.0.0", r.getStackVersion());
+    Assert.assertEquals("HDP-1.2.0", r.getStackVersion());
 
     Assert.assertFalse(r.isStaleConfig());
 
@@ -715,16 +715,17 @@ public class ServiceComponentHostTest {
     String stackVersion="HDP-2.0.6";
     String clusterName = "c2";
     String hostName = "h3";
+    StackId stackId = new StackId(stackVersion);
 
-    clusters.addCluster(clusterName);
+    clusters.addCluster(clusterName, stackId);
     clusters.addHost(hostName);
     setOsFamily(clusters.getHost(hostName), "redhat", "5.9");
     clusters.getHost(hostName).persist();
     Cluster c2 = clusters.getCluster(clusterName);
-    StackId stackId = new StackId(stackVersion);
-    c2.setDesiredStackVersion(stackId);
-    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
-    c2.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
+
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    c2.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
     clusters.mapHostToCluster(hostName, clusterName);
 
     Cluster cluster = clusters.getCluster(clusterName);
@@ -942,15 +943,17 @@ public class ServiceComponentHostTest {
     String clusterName = "c2";
     String hostName = "h3";
 
-    clusters.addCluster(clusterName);
+    StackId stackId = new StackId(stackVersion);
+
+    clusters.addCluster(clusterName, stackId);
     clusters.addHost(hostName);
     setOsFamily(clusters.getHost(hostName), "redhat", "5.9");
     clusters.getHost(hostName).persist();
     Cluster c2 = clusters.getCluster(clusterName);
-    StackId stackId = new StackId(stackVersion);
-    c2.setDesiredStackVersion(stackId);
-    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
-    c2.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
+
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    c2.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
 
     clusters.mapHostToCluster(hostName, clusterName);
 
@@ -1076,15 +1079,17 @@ public class ServiceComponentHostTest {
     String clusterName = "c2";
     String hostName = "h3";
 
-    clusters.addCluster(clusterName);
+    StackId stackId = new StackId(stackVersion);
+
+    clusters.addCluster(clusterName, stackId);
     clusters.addHost(hostName);
     setOsFamily(clusters.getHost(hostName), "redhat", "5.9");
     clusters.getHost(hostName).persist();
     Cluster c2 = clusters.getCluster(clusterName);
-    StackId stackId = new StackId(stackVersion);
-    c2.setDesiredStackVersion(stackId);
-    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
-    c2.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
+
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    c2.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
     clusters.mapHostToCluster(hostName, clusterName);
     HostEntity hostEntity = hostDAO.findByName(hostName);
     Assert.assertNotNull(hostEntity);
@@ -1119,8 +1124,9 @@ public class ServiceComponentHostTest {
     String stackVersion="HDP-2.0.6";
     String clusterName = "c2";
     String hostName = "h3";
+    StackId stackId = new StackId(stackVersion);
 
-    clusters.addCluster(clusterName);
+    clusters.addCluster(clusterName, stackId);
     clusters.addHost(hostName);
     setOsFamily(clusters.getHost(hostName), "redhat", "5.9");
     clusters.getHost(hostName).persist();
@@ -1128,10 +1134,11 @@ public class ServiceComponentHostTest {
     Assert.assertNotNull(hostEntity);
 
     Cluster c2 = clusters.getCluster(clusterName);
-    StackId stackId = new StackId(stackVersion);
+
     c2.setDesiredStackVersion(stackId);
-    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
-    c2.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING);
+    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+    c2.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
     clusters.mapHostToCluster(hostName, clusterName);
 
     Cluster cluster = clusters.getCluster(clusterName);

http://git-wip-us.apache.org/repos/asf/ambari/blob/746df034/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog150Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog150Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog150Test.java
index 37328e8..39dd815 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog150Test.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog150Test.java
@@ -22,11 +22,13 @@ import javax.persistence.EntityManager;
 import junit.framework.Assert;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO;
 import org.apache.ambari.server.orm.dao.KeyValueDAO;
+import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
 import org.apache.ambari.server.orm.entities.ClusterConfigMappingEntity;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
@@ -35,7 +37,9 @@ import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.KeyValueEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
+import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.state.HostComponentAdminState;
+import org.apache.ambari.server.state.StackId;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -48,15 +52,28 @@ public class UpgradeCatalog150Test {
   private Injector injector;
   private final String CLUSTER_NAME = "c1";
   private final String HOST_NAME = "h1";
-  private final String DESIRED_STACK_VERSION = "{\"stackName\":\"HDP\",\"stackVersion\":\"1.3.4\"}";
+
+  public static final StackId DESIRED_STACK = new StackId("HDP", "1.3.4");
 
   private UpgradeCatalogHelper upgradeCatalogHelper;
+  private StackEntity desiredStackEntity;
 
   @Before
   public void setup() throws Exception {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
     upgradeCatalogHelper = injector.getInstance(UpgradeCatalogHelper.class);
+
+    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
+    injector.getInstance(AmbariMetaInfo.class);
+
+    // load the stack entity
+    StackDAO stackDAO = injector.getInstance(StackDAO.class);
+
+    desiredStackEntity = stackDAO.find(DESIRED_STACK.getStackName(),
+        DESIRED_STACK.getStackVersion());
+
+    Assert.assertNotNull(desiredStackEntity);
   }
 
   @After
@@ -87,9 +104,9 @@ public class UpgradeCatalog150Test {
   @Test
   public void testAddHistoryServer() throws AmbariException {
     final ClusterEntity clusterEntity = upgradeCatalogHelper.createCluster(
-        injector, CLUSTER_NAME, DESIRED_STACK_VERSION);
+        injector, CLUSTER_NAME, desiredStackEntity);
     final ClusterServiceEntity clusterServiceEntityMR = upgradeCatalogHelper.addService(
-        injector, clusterEntity, "MAPREDUCE", DESIRED_STACK_VERSION);
+        injector, clusterEntity, "MAPREDUCE", desiredStackEntity);
     final HostEntity hostEntity = upgradeCatalogHelper.createHost(injector,
         clusterEntity, HOST_NAME);
 
@@ -98,7 +115,7 @@ public class UpgradeCatalog150Test {
       public void run() {
         upgradeCatalogHelper.addComponent(injector, clusterEntity,
             clusterServiceEntityMR, hostEntity, "JOBTRACKER",
-            DESIRED_STACK_VERSION);
+            desiredStackEntity);
       }
     });
 
@@ -109,7 +126,7 @@ public class UpgradeCatalog150Test {
   @Test
   public void testProcessDecommissionedDatanodes() throws Exception {
     ClusterEntity clusterEntity = upgradeCatalogHelper.createCluster(injector,
-        CLUSTER_NAME, DESIRED_STACK_VERSION);
+        CLUSTER_NAME, desiredStackEntity);
     ClusterServiceEntity clusterServiceEntity = upgradeCatalogHelper.createService(
         injector, clusterEntity, "HDFS");
     HostEntity hostEntity = upgradeCatalogHelper.createHost(injector,
@@ -121,6 +138,7 @@ public class UpgradeCatalog150Test {
     componentDesiredStateEntity.setServiceName(clusterServiceEntity.getServiceName());
     componentDesiredStateEntity.setClusterServiceEntity(clusterServiceEntity);
     componentDesiredStateEntity.setComponentName("DATANODE");
+    componentDesiredStateEntity.setDesiredStack(desiredStackEntity);
 
     //componentDesiredStateDAO.create(componentDesiredStateEntity);
 
@@ -136,6 +154,7 @@ public class UpgradeCatalog150Test {
     hostComponentDesiredStateEntity.setServiceName(clusterServiceEntity.getServiceName());
     hostComponentDesiredStateEntity.setServiceComponentDesiredStateEntity(componentDesiredStateEntity);
     hostComponentDesiredStateEntity.setHostEntity(hostEntity);
+    hostComponentDesiredStateEntity.setDesiredStack(desiredStackEntity);
 
     hostComponentDesiredStateDAO.create(hostComponentDesiredStateEntity);
 
@@ -157,6 +176,9 @@ public class UpgradeCatalog150Test {
     configEntity.setTag("1394147791230");
     configEntity.setData("{\"datanodes\":\"" + HOST_NAME + "\"}");
     configEntity.setTimestamp(System.currentTimeMillis());
+    configEntity.setStack(desiredStackEntity);
+    configEntity.setStack(clusterEntity.getDesiredStack());
+
     clusterDAO.createConfig(configEntity);
 
     UpgradeCatalog150 upgradeCatalog150 = injector.getInstance(UpgradeCatalog150.class);
@@ -179,10 +201,10 @@ public class UpgradeCatalog150Test {
     ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
 
     ClusterEntity clusterEntity = upgradeCatalogHelper.createCluster(injector,
-        CLUSTER_NAME, DESIRED_STACK_VERSION);
+        CLUSTER_NAME, desiredStackEntity);
 
     ClusterServiceEntity clusterServiceEntityMR = upgradeCatalogHelper.addService(
-        injector, clusterEntity, "HDFS", DESIRED_STACK_VERSION);
+        injector, clusterEntity, "HDFS", desiredStackEntity);
 
     Long clusterId = clusterEntity.getClusterId();
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/746df034/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog170Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog170Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog170Test.java
index 2bf6d96..377c0f7 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog170Test.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog170Test.java
@@ -69,6 +69,7 @@ import javax.persistence.criteria.Root;
 import javax.persistence.metamodel.SingularAttribute;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.orm.DBAccessor;
@@ -86,6 +87,7 @@ import org.apache.ambari.server.orm.dao.PrivilegeDAO;
 import org.apache.ambari.server.orm.dao.ResourceDAO;
 import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
 import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
+import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.dao.UserDAO;
 import org.apache.ambari.server.orm.dao.ViewDAO;
 import org.apache.ambari.server.orm.dao.ViewInstanceDAO;
@@ -108,6 +110,7 @@ import org.apache.ambari.server.orm.entities.PrivilegeEntity;
 import org.apache.ambari.server.orm.entities.ResourceEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntityPK;
+import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.orm.entities.UserEntity;
 import org.apache.ambari.server.orm.entities.ViewEntity;
 import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
@@ -141,7 +144,8 @@ public class UpgradeCatalog170Test {
   private final String CLUSTER_NAME = "c1";
   private final String SERVICE_NAME = "HDFS";
   private final String HOST_NAME = "h1";
-  private final String DESIRED_STACK_VERSION = "{\"stackName\":\"HDP\",\"stackVersion\":\"2.0.6\"}";
+
+  public static final StackId DESIRED_STACK = new StackId("HDP", "2.0.6");
 
   Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
   EntityManager entityManager = createNiceMock(EntityManager.class);
@@ -480,10 +484,17 @@ public class UpgradeCatalog170Test {
     ClusterConfigMappingEntity configMappingEntity = createNiceMock(ClusterConfigMappingEntity.class);
     ClusterStateEntity clusterStateEntity = createNiceMock(ClusterStateEntity.class);
 
+    StackEntity stackEntity = createNiceMock(StackEntity.class);
+    expect(stackEntity.getStackName()).andReturn(
+        CLUSTER_STATE_STACK_HDP_2_1.getStackName());
+
+    expect(stackEntity.getStackVersion()).andReturn(
+        CLUSTER_STATE_STACK_HDP_2_1.getStackVersion());
+
     expect(clusterEntity.getClusterId()).andReturn(1L).anyTimes();
     expect(clusterEntity.getConfigMappingEntities()).andReturn(Collections.singleton(configMappingEntity)).times(2);
     expect(clusterEntity.getClusterStateEntity()).andReturn(clusterStateEntity).anyTimes();
-    expect(clusterStateEntity.getCurrentStackVersion()).andReturn(CLUSTER_STATE_STACK_HDP_2_1);
+    expect(clusterStateEntity.getCurrentStack()).andReturn(stackEntity);
     expect(configMappingEntity.getType()).andReturn(YARN_SITE).anyTimes();
     expect(configMappingEntity.isSelected()).andReturn(1).anyTimes();
     expect(configMappingEntity.getTag()).andReturn("version1");
@@ -518,7 +529,7 @@ public class UpgradeCatalog170Test {
     replay(jobsView, showJobsKeyValue, user);
     replay(userEntity1, userEntity2, userPrincipal1, userPrincipal2, userPrivileges1, userPrivileges2);
     replay(viewRegistry, viewUsePermission, adminPermission);
-    replay(clusterEntity, configEntity, configMappingEntity, clusterStateEntity);
+    replay(clusterEntity, configEntity, configMappingEntity, clusterStateEntity, stackEntity);
 
     Class<?> c = AbstractUpgradeCatalog.class;
     Field f = c.getDeclaredField("configuration");
@@ -545,29 +556,47 @@ public class UpgradeCatalog170Test {
     ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO = injector.getInstance(ServiceComponentDesiredStateDAO.class);
     HostComponentDesiredStateDAO hostComponentDesiredStateDAO = injector.getInstance(HostComponentDesiredStateDAO.class);
     HostComponentStateDAO hostComponentStateDAO = injector.getInstance(HostComponentStateDAO.class);
+    StackDAO stackDAO = injector.getInstance(StackDAO.class);
+
+    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
+    injector.getInstance(AmbariMetaInfo.class);
+
+    StackEntity desiredStackEntity = stackDAO.find(
+        DESIRED_STACK.getStackName(),
+        DESIRED_STACK.getStackVersion());
+
+    assertNotNull(desiredStackEntity);
 
     final ClusterEntity clusterEntity = upgradeCatalogHelper.createCluster(
-        injector, CLUSTER_NAME, DESIRED_STACK_VERSION);
+        injector, CLUSTER_NAME, desiredStackEntity);
+
     final ClusterServiceEntity clusterServiceEntityHDFS = upgradeCatalogHelper.addService(
-        injector, clusterEntity, "HDFS", DESIRED_STACK_VERSION);
+        injector, clusterEntity, "HDFS", desiredStackEntity);
+
     final ClusterServiceEntity clusterServiceEntityHIVE = upgradeCatalogHelper.addService(
-        injector, clusterEntity, "HIVE", DESIRED_STACK_VERSION);
+        injector, clusterEntity, "HIVE", desiredStackEntity);
+
     final ClusterServiceEntity clusterServiceEntityHCATALOG = upgradeCatalogHelper.addService(
-        injector, clusterEntity, "HCATALOG", DESIRED_STACK_VERSION);
+        injector, clusterEntity, "HCATALOG", desiredStackEntity);
+
     final ClusterServiceEntity clusterServiceEntityWEBHCAT = upgradeCatalogHelper.addService(
-        injector, clusterEntity, "WEBHCAT", DESIRED_STACK_VERSION);
+        injector, clusterEntity, "WEBHCAT", desiredStackEntity);
+
     final HostEntity hostEntity = upgradeCatalogHelper.createHost(injector,
         clusterEntity, HOST_NAME);
+
     upgradeCatalogHelper.addComponent(injector, clusterEntity,
-        clusterServiceEntityHDFS, hostEntity, "NAMENODE", DESIRED_STACK_VERSION);
+        clusterServiceEntityHDFS, hostEntity, "NAMENODE", desiredStackEntity);
+
     upgradeCatalogHelper.addComponent(injector, clusterEntity,
-        clusterServiceEntityHIVE, hostEntity, "HIVE_SERVER",
-        DESIRED_STACK_VERSION);
+        clusterServiceEntityHIVE, hostEntity, "HIVE_SERVER", desiredStackEntity);
+
     upgradeCatalogHelper.addComponent(injector, clusterEntity,
-        clusterServiceEntityHCATALOG, hostEntity, "HCAT", DESIRED_STACK_VERSION);
+        clusterServiceEntityHCATALOG, hostEntity, "HCAT", desiredStackEntity);
+
     upgradeCatalogHelper.addComponent(injector, clusterEntity,
         clusterServiceEntityWEBHCAT, hostEntity, "WEBHCAT_SERVER",
-        DESIRED_STACK_VERSION);
+        desiredStackEntity);
 
     upgradeCatalog170.moveHcatalogIntoHiveService();
 
@@ -597,8 +626,19 @@ public class UpgradeCatalog170Test {
 
   @Test
   public void updateClusterProvisionState()  throws AmbariException {
+    StackDAO stackDAO = injector.getInstance(StackDAO.class);
+
+    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
+    injector.getInstance(AmbariMetaInfo.class);
+
+    StackEntity desiredStackEntity = stackDAO.find(
+        DESIRED_STACK.getStackName(), DESIRED_STACK.getStackVersion());
+
+    assertNotNull(desiredStackEntity);
+
     ClusterEntity clusterEntity = upgradeCatalogHelper.createCluster(injector,
-        CLUSTER_NAME, DESIRED_STACK_VERSION);
+        CLUSTER_NAME, desiredStackEntity);
+
     UpgradeCatalog170 upgradeCatalog170 = injector.getInstance(UpgradeCatalog170.class);
     upgradeCatalog170.updateClusterProvisionState();    //action
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/746df034/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java
index 96d5134..804aa60 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java
@@ -58,6 +58,7 @@ import org.apache.ambari.server.orm.dao.ClusterServiceDAO;
 import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO;
 import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
 import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
+import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
@@ -67,6 +68,7 @@ import org.apache.ambari.server.orm.entities.HostComponentStateEntityPK;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntityPK;
+import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Config;
@@ -99,7 +101,8 @@ import com.google.inject.persist.PersistService;
 public class UpgradeCatalog200Test {
   private final String CLUSTER_NAME = "c1";
   private final String HOST_NAME = "h1";
-  private final String DESIRED_STACK_VERSION = "{\"stackName\":\"HDP\",\"stackVersion\":\"2.0.6\"}";
+
+  private final StackId DESIRED_STACK = new StackId("HDP", "2.0.6");
 
   private Injector injector;
   private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
@@ -536,19 +539,27 @@ public class UpgradeCatalog200Test {
     HostComponentDesiredStateDAO hostComponentDesiredStateDAO = injector.getInstance(HostComponentDesiredStateDAO.class);
     HostComponentStateDAO hostComponentStateDAO = injector.getInstance(HostComponentStateDAO.class);
     ClusterServiceDAO clusterServiceDao = injector.getInstance(ClusterServiceDAO.class);
+    StackDAO stackDAO = injector.getInstance(StackDAO.class);
+
+    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
+    injector.getInstance(AmbariMetaInfo.class);
+
+    StackEntity stackEntity = stackDAO.find(DESIRED_STACK.getStackName(),
+        DESIRED_STACK.getStackVersion());
+
+    assertNotNull(stackEntity);
 
     final ClusterEntity clusterEntity = upgradeCatalogHelper.createCluster(
-        injector, CLUSTER_NAME, DESIRED_STACK_VERSION);
+        injector, CLUSTER_NAME, stackEntity);
 
     final ClusterServiceEntity clusterServiceEntityNagios = upgradeCatalogHelper.addService(
-        injector, clusterEntity, "NAGIOS", DESIRED_STACK_VERSION);
+        injector, clusterEntity, "NAGIOS", stackEntity);
 
     final HostEntity hostEntity = upgradeCatalogHelper.createHost(injector,
         clusterEntity, HOST_NAME);
 
     upgradeCatalogHelper.addComponent(injector, clusterEntity,
-        clusterServiceEntityNagios, hostEntity, "NAGIOS_SERVER",
-        DESIRED_STACK_VERSION);
+        clusterServiceEntityNagios, hostEntity, "NAGIOS_SERVER", stackEntity);
 
     ServiceComponentDesiredStateEntityPK pkNagiosServer = new ServiceComponentDesiredStateEntityPK();
     pkNagiosServer.setComponentName("NAGIOS_SERVER");


Mime
View raw message