Repository: ambari
Updated Branches:
  refs/heads/branch-2.5 b37d1ffb8 -> d9a03d43c


AMBARI-19164. Add PK to hostcomponentdesiredstate Table To Support FK 
Relationships. (stoader)


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

Branch: refs/heads/branch-2.5
Commit: d9a03d43c41e50bd19435328fb3e8cd71307fe21
Parents: b37d1ff
Author: Toader, Sebastian <stoa...@hortonworks.com>
Authored: Thu Dec 15 17:14:01 2016 +0100
Committer: Toader, Sebastian <stoa...@hortonworks.com>
Committed: Thu Dec 15 17:14:01 2016 +0100

----------------------------------------------------------------------
 .../apache/ambari/server/orm/DBAccessor.java    |  11 ++
 .../ambari/server/orm/DBAccessorImpl.java       |  20 ++-
 .../orm/dao/HostComponentDesiredStateDAO.java   |  32 ++++-
 .../HostComponentDesiredStateEntity.java        |  59 +++++---
 .../HostComponentDesiredStateEntityPK.java      |  95 -------------
 .../server/orm/helpers/dbms/DbmsHelper.java     |   3 +
 .../orm/helpers/dbms/GenericDbmsHelper.java     |  16 ++-
 .../server/state/ServiceComponentImpl.java      |  14 +-
 .../svccomphost/ServiceComponentHostImpl.java   |  23 +--
 .../server/upgrade/UpgradeCatalog250.java       | 124 +++++++++++++++--
 .../main/resources/Ambari-DDL-Derby-CREATE.sql  |   9 +-
 .../main/resources/Ambari-DDL-MySQL-CREATE.sql  |   8 +-
 .../main/resources/Ambari-DDL-Oracle-CREATE.sql |   5 +-
 .../resources/Ambari-DDL-Postgres-CREATE.sql    |   7 +-
 .../resources/Ambari-DDL-SQLAnywhere-CREATE.sql |   5 +-
 .../resources/Ambari-DDL-SQLServer-CREATE.sql   |   8 +-
 .../server/state/ServiceComponentTest.java      |  15 +-
 .../server/state/cluster/ClustersTest.java      |  20 +--
 .../svccomphost/ServiceComponentHostTest.java   |  34 ++---
 .../server/upgrade/UpgradeCatalog200Test.java   |  13 +-
 .../server/upgrade/UpgradeCatalog250Test.java   | 139 +++++++++++--------
 21 files changed, 391 insertions(+), 269 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java
index 488c01e..b8a760f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java
@@ -71,6 +71,17 @@ public interface DBAccessor {
                           String... columnNames) throws SQLException;
 
   /**
+   * Create new index
+   * @param indexName The name of the index to be created
+   * @param tableName The database table the index to be created on
+   * @param columnNames The columns included into the index
+   * @param isUnique Specifies whether unique index is to be created.
+   * @throws SQLException Exception in case the index creation fails.
+   */
+  void createIndex(String indexName, String tableName, boolean isUnique,
+                   String... columnNames) throws SQLException;
+
+  /**
    * Add foreign key for a relation
    * @param tableName
    * @param constraintName

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
index 123fa96..ff8f7b0 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
@@ -427,12 +427,18 @@ public class DBAccessorImpl implements DBAccessor {
   @Override
   public void createIndex(String indexName, String tableName,
           String... columnNames) throws SQLException {
-   if (!tableHasIndex(tableName, false, indexName)) {
-     String query = dbmsHelper.getCreateIndexStatement(indexName, tableName, 
columnNames);
-     executeQuery(query);
-   } else {
-     LOG.info("Index {} already exist, skipping creation, table = {}", 
indexName, tableName);
-   }
+    createIndex(indexName, tableName, false, columnNames);
+  }
+
+  @Override
+  public void createIndex(String indexName, String tableName, boolean isUnique,
+                          String... columnNames) throws SQLException {
+    if (!tableHasIndex(tableName, false, indexName)) {
+      String query = dbmsHelper.getCreateIndexStatement(indexName, tableName, 
isUnique, columnNames);
+      executeQuery(query);
+    } else {
+      LOG.info("Index {} already exist, skipping creation, table = {}", 
indexName, tableName);
+    }
   }
 
   @Override
@@ -1177,6 +1183,7 @@ public class DBAccessorImpl implements DBAccessor {
 
         break;
       }
+      case MYSQL:
       case POSTGRES: {
         String lookupPrimaryKeyNameSql = String.format(
             "SELECT constraint_name FROM information_schema.table_constraints 
AS tc WHERE tc.constraint_type = 'PRIMARY KEY' AND table_name = '%s'",
@@ -1195,6 +1202,7 @@ public class DBAccessorImpl implements DBAccessor {
 
         break;
       }
+
       default:
         break;
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAO.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAO.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAO.java
index 876b1cf..5540294 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAO.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAO.java
@@ -26,7 +26,6 @@ import javax.persistence.TypedQuery;
 
 import org.apache.ambari.server.orm.RequiresSession;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntityPK;
 import org.apache.ambari.server.orm.entities.HostEntity;
 
 import com.google.inject.Inject;
@@ -46,8 +45,8 @@ public class HostComponentDesiredStateDAO {
   DaoUtils daoUtils;
 
   @RequiresSession
-  public HostComponentDesiredStateEntity 
findByPK(HostComponentDesiredStateEntityPK primaryKey) {
-    return 
entityManagerProvider.get().find(HostComponentDesiredStateEntity.class, 
primaryKey);
+  public HostComponentDesiredStateEntity findById(long id) {
+    return 
entityManagerProvider.get().find(HostComponentDesiredStateEntity.class, id);
   }
 
   @RequiresSession
@@ -78,6 +77,29 @@ public class HostComponentDesiredStateDAO {
     return daoUtils.selectSingle(query);
   }
 
+  /**
+   * Retrieve the single Host Component Desired State for the given unique 
cluster, service, component, and host.
+   *
+   * @param clusterId Cluster ID
+   * @param serviceName Service Name
+   * @param componentName Component Name
+   * @param hostId Host ID
+   * @return Return the Host Component Desired State entity that match the 
criteria.
+   */
+  @RequiresSession
+  public HostComponentDesiredStateEntity findByIndex(Long clusterId, String 
serviceName,
+                                                     String componentName, 
Long hostId) {
+    final TypedQuery<HostComponentDesiredStateEntity> query = 
entityManagerProvider.get()
+      .createNamedQuery("HostComponentDesiredStateEntity.findByIndex", 
HostComponentDesiredStateEntity.class);
+
+    query.setParameter("clusterId", clusterId);
+    query.setParameter("serviceName", serviceName);
+    query.setParameter("componentName", componentName);
+    query.setParameter("hostId", hostId);
+
+    return daoUtils.selectSingle(query);
+  }
+
   @Transactional
   public void refresh(HostComponentDesiredStateEntity 
hostComponentDesiredStateEntity) {
     entityManagerProvider.get().refresh(hostComponentDesiredStateEntity);
@@ -109,8 +131,8 @@ public class HostComponentDesiredStateDAO {
   }
 
   @Transactional
-  public void removeByPK(HostComponentDesiredStateEntityPK primaryKey) {
-    remove(findByPK(primaryKey));
+  public void removeId(long id) {
+    remove(findById(id));
   }
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostComponentDesiredStateEntity.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostComponentDesiredStateEntity.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostComponentDesiredStateEntity.java
index 274a1e0..ea2938b 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostComponentDesiredStateEntity.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostComponentDesiredStateEntity.java
@@ -24,6 +24,8 @@ 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.JoinColumns;
@@ -31,6 +33,9 @@ import javax.persistence.ManyToOne;
 import javax.persistence.NamedQueries;
 import javax.persistence.NamedQuery;
 import javax.persistence.OneToOne;
+import javax.persistence.Table;
+import javax.persistence.TableGenerator;
+import javax.persistence.UniqueConstraint;
 
 import org.apache.ambari.server.state.HostComponentAdminState;
 import org.apache.ambari.server.state.MaintenanceState;
@@ -39,39 +44,49 @@ import org.apache.ambari.server.state.State;
 
 import com.google.common.base.Objects;
 
-@javax.persistence.IdClass(HostComponentDesiredStateEntityPK.class)
-@javax.persistence.Table(name = "hostcomponentdesiredstate")
+
 @Entity
+@Table(
+  name = "hostcomponentdesiredstate",
+  uniqueConstraints = @UniqueConstraint(
+    name = "UQ_hcdesiredstate_name",
+    columnNames = { "component_name", "service_name", "host_id", "cluster_id" 
}) )
+@TableGenerator(
+  name = "hostcomponentdesiredstate_id_generator",
+  table = "ambari_sequences",
+  pkColumnName = "sequence_name",
+  valueColumnName = "sequence_value",
+  pkColumnValue = "hostcomponentdesiredstate_id_seq",
+  initialValue = 0)
 @NamedQueries({
     @NamedQuery(name = "HostComponentDesiredStateEntity.findAll", query = 
"SELECT hcds from HostComponentDesiredStateEntity hcds"),
 
-    @NamedQuery(name = "HostComponentDesiredStateEntity.findByHost", query =
-        "SELECT hcds from HostComponentDesiredStateEntity hcds WHERE 
hcds.hostEntity.hostName=:hostName"),
-
-    @NamedQuery(name = "HostComponentDesiredStateEntity.findByService", query =
-        "SELECT hcds from HostComponentDesiredStateEntity hcds WHERE 
hcds.serviceName=:serviceName"),
-
     @NamedQuery(name = 
"HostComponentDesiredStateEntity.findByServiceAndComponent", query =
         "SELECT hcds from HostComponentDesiredStateEntity hcds WHERE 
hcds.serviceName=:serviceName AND hcds.componentName=:componentName"),
 
     @NamedQuery(name = 
"HostComponentDesiredStateEntity.findByServiceComponentAndHost", query =
         "SELECT hcds from HostComponentDesiredStateEntity hcds WHERE 
hcds.serviceName=:serviceName AND hcds.componentName=:componentName AND 
hcds.hostEntity.hostName=:hostName"),
+
+  @NamedQuery(name = "HostComponentDesiredStateEntity.findByIndex", query =
+    "SELECT hcds from HostComponentDesiredStateEntity hcds WHERE 
hcds.clusterId=:clusterId AND hcds.serviceName=:serviceName AND 
hcds.componentName=:componentName AND hcds.hostId=:hostId"),
 })
 public class HostComponentDesiredStateEntity {
 
   @Id
+  @GeneratedValue(strategy = GenerationType.TABLE, generator = 
"hostcomponentdesiredstate_id_generator")
+  @Column(name = "id", nullable = false, insertable = true, updatable = false)
+  private Long id;
+
+
   @Column(name = "cluster_id", nullable = false, insertable = false, updatable 
= false, length = 10)
   private Long clusterId;
 
-  @Id
   @Column(name = "service_name", nullable = false, insertable = false, 
updatable = false)
   private String serviceName;
 
-  @Id
   @Column(name = "host_id", nullable = false, insertable = false, updatable = 
false)
   private Long hostId;
 
-  @Id
   @Column(name = "component_name", insertable = false, updatable = false)
   private String componentName = "";
 
@@ -115,6 +130,8 @@ public class HostComponentDesiredStateEntity {
   @Column(name = "restart_required", insertable = true, updatable = true, 
nullable = false)
   private Integer restartRequired = 0;
 
+  public Long getId() { return id; }
+
   public Long getClusterId() {
     return clusterId;
   }
@@ -194,28 +211,31 @@ public class HostComponentDesiredStateEntity {
 
     HostComponentDesiredStateEntity that = (HostComponentDesiredStateEntity) o;
 
-    if (clusterId != null ? !clusterId.equals(that.clusterId) : that.clusterId 
!= null) {
+    if (!Objects.equal(id, that.id)) {
+      return false;
+    }
+
+    if (!Objects.equal(clusterId, that.clusterId)) {
       return false;
     }
 
-    if (componentName != null ? !componentName.equals(that.componentName) : 
that.componentName != null) {
+    if (!Objects.equal(componentName, that.componentName)) {
       return false;
     }
 
-    if (desiredStack != null ? !desiredStack.equals(that.desiredStack)
-        : that.desiredStack != null) {
+    if (!Objects.equal(desiredStack, that.desiredStack)) {
       return false;
     }
 
-    if (desiredState != null ? !desiredState.equals(that.desiredState) : 
that.desiredState != null) {
+    if (!Objects.equal(desiredState, that.desiredState)) {
       return false;
     }
 
-    if (hostEntity != null ? !hostEntity.equals(that.hostEntity) : 
that.hostEntity != null) {
+    if (!Objects.equal(hostEntity, that.hostEntity)) {
       return false;
     }
 
-    if (serviceName != null ? !serviceName.equals(that.serviceName) : 
that.serviceName != null) {
+    if (!Objects.equal(serviceName, that.serviceName)) {
       return false;
     }
 
@@ -224,7 +244,8 @@ public class HostComponentDesiredStateEntity {
 
   @Override
   public int hashCode() {
-    int result = clusterId != null ? clusterId.intValue() : 0;
+    int result = id != null ? id.hashCode() : 0;
+    result = 31 * result + (clusterId != null ? clusterId.hashCode() : 0);
     result = 31 * result + (hostEntity != null ? hostEntity.hashCode() : 0);
     result = 31 * result + (componentName != null ? componentName.hashCode() : 
0);
     result = 31 * result + (desiredState != null ? desiredState.hashCode() : 
0);

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostComponentDesiredStateEntityPK.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostComponentDesiredStateEntityPK.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostComponentDesiredStateEntityPK.java
deleted file mode 100644
index 1fd899d..0000000
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostComponentDesiredStateEntityPK.java
+++ /dev/null
@@ -1,95 +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 javax.persistence.Column;
-import javax.persistence.Id;
-import java.io.Serializable;
-
-@SuppressWarnings("serial")
-public class HostComponentDesiredStateEntityPK implements Serializable {
-  private Long clusterId;
-
-  @Id
-  @Column(name = "cluster_id", nullable = false, insertable = true, updatable 
= true, length = 10)
-  public Long getClusterId() {
-    return clusterId;
-  }
-
-  public void setClusterId(Long clusterId) {
-    this.clusterId = clusterId;
-  }
-
-  private String serviceName;
-
-  @Id
-  @Column(name = "service_name", nullable = false, insertable = true, 
updatable = true)
-  public String getServiceName() {
-    return serviceName;
-  }
-
-  public void setServiceName(String serviceName) {
-    this.serviceName = serviceName;
-  }
-
-  private Long hostId;
-
-  @Id
-  @Column(name = "host_id", nullable = false, insertable = true, updatable = 
true)
-  public Long getHostId() {
-    return hostId;
-  }
-
-  public void setHostId(Long hostId) {
-    this.hostId = hostId;
-  }
-
-  private String componentName;
-
-  @Id
-  @Column(name = "component_name", nullable = false, insertable = true, 
updatable = true)
-  public String getComponentName() {
-    return componentName;
-  }
-
-  public void setComponentName(String componentName) {
-    this.componentName = componentName;
-  }
-
-  @Override
-  public boolean equals(Object o) {
-    if (this == o) return true;
-    if (o == null || getClass() != o.getClass()) return false;
-
-    HostComponentDesiredStateEntityPK that = 
(HostComponentDesiredStateEntityPK) o;
-
-    if (clusterId != null ? !clusterId.equals(that.clusterId) : that.clusterId 
!= null) return false;
-    if (componentName != null ? !componentName.equals(that.componentName) : 
that.componentName != null) return false;
-    if (hostId != null ? !hostId.equals(that.hostId) : that.hostId != null) 
return false;
-
-    return true;
-  }
-
-  @Override
-  public int hashCode() {
-    int result = clusterId !=null ? clusterId.intValue() : 0;
-    result = 31 * result + (hostId != null ? hostId.hashCode() : 0);
-    result = 31 * result + (componentName != null ? componentName.hashCode() : 
0);
-    return result;
-  }
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java
index 1fe65db..c2778d3 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java
@@ -58,6 +58,9 @@ public interface DbmsHelper {
   String getCreateIndexStatement(String indexName, String tableName,
                                  String... columnNames);
 
+  String getCreateIndexStatement(String indexName, String tableName, boolean 
isUnique,
+                                 String... columnNames);
+
   /**
    * Gets DROP INDEX statement
    *

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java
index 042b4d2..36fab83 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java
@@ -243,8 +243,22 @@ public class GenericDbmsHelper implements DbmsHelper {
   @Override
   public String getCreateIndexStatement(String indexName, String tableName,
                                         String... columnNames) {
+    return getCreateIndexStatement(indexName, tableName, false, columnNames);
+  }
+
+  /**
+   * get create index statement
+   * @param indexName The name of the index to be created
+   * @param tableName The database table the index to be created on
+   * @param columnNames The columns included into the index
+   * @param  isUnique Specifies whether unique index is to be created.
+   * @return The sql statement for creating the index
+   */
+  @Override
+  public String getCreateIndexStatement(String indexName, String tableName, 
boolean isUnique,
+                                        String... columnNames) {
     //TODO validateColumnNames()
-    String createIndex = databasePlatform.buildCreateIndex(tableName, 
indexName, columnNames);
+    String createIndex = databasePlatform.buildCreateIndex(tableName, 
indexName, "", isUnique, columnNames);
     return createIndex;
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
index f9c0eb7..236091b 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
@@ -40,7 +40,6 @@ import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
 import org.apache.ambari.server.orm.entities.ClusterServiceEntityPK;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntityPK;
 import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
 import 
org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
@@ -165,13 +164,14 @@ public class ServiceComponentImpl implements 
ServiceComponent {
     updateComponentInfo();
 
     for (HostComponentStateEntity hostComponentStateEntity : 
serviceComponentDesiredStateEntity.getHostComponentStateEntities()) {
-      HostComponentDesiredStateEntityPK pk = new 
HostComponentDesiredStateEntityPK();
-      pk.setClusterId(hostComponentStateEntity.getClusterId());
-      pk.setServiceName(hostComponentStateEntity.getServiceName());
-      pk.setComponentName(hostComponentStateEntity.getComponentName());
-      pk.setHostId(hostComponentStateEntity.getHostId());
 
-      HostComponentDesiredStateEntity hostComponentDesiredStateEntity = 
hostComponentDesiredStateDAO.findByPK(pk);
+      HostComponentDesiredStateEntity hostComponentDesiredStateEntity = 
hostComponentDesiredStateDAO.findByIndex(
+        hostComponentStateEntity.getClusterId(),
+        hostComponentStateEntity.getServiceName(),
+        hostComponentStateEntity.getComponentName(),
+        hostComponentStateEntity.getHostId()
+      );
+
       try {
         hostComponents.put(hostComponentStateEntity.getHostName(),
           serviceComponentHostFactory.createExisting(this,

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
index 5225598..1bcffe4 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
@@ -46,7 +46,6 @@ import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
 import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntityPK;
 import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
@@ -140,9 +139,9 @@ public class ServiceComponentHostImpl implements 
ServiceComponentHost {
   private final StackDAO stackDAO;
 
   /**
-   * The desired component state entity PK.
+   * The desired component state entity id.
    */
-  private final HostComponentDesiredStateEntityPK desiredStateEntityPK;
+  private final Long desiredStateEntityId;
 
   /**
    * Cache the generated id for host component state for fast lookups.
@@ -799,8 +798,6 @@ public class ServiceComponentHostImpl implements 
ServiceComponentHost {
       desiredStateEntity.setAdminState(null);
     }
 
-    desiredStateEntityPK = 
getHostComponentDesiredStateEntityPK(desiredStateEntity);
-
     persistEntities(hostEntity, stateEntity, desiredStateEntity);
 
     // publish the service component installed event
@@ -810,6 +807,7 @@ public class ServiceComponentHostImpl implements 
ServiceComponentHost {
 
     eventPublisher.publish(event);
 
+    desiredStateEntityId = desiredStateEntity.getId();
     hostComponentStateId = stateEntity.getId();
 
     resetLastOpInfo();
@@ -836,7 +834,7 @@ public class ServiceComponentHostImpl implements 
ServiceComponentHost {
     this.hostComponentDesiredStateDAO = hostComponentDesiredStateDAO;
     this.eventPublisher = eventPublisher;
 
-    desiredStateEntityPK = 
getHostComponentDesiredStateEntityPK(desiredStateEntity);
+    desiredStateEntityId = desiredStateEntity.getId();
     hostComponentStateId = stateEntity.getId();
 
     //TODO implement State Machine init as now type choosing is hardcoded in 
above code
@@ -1580,7 +1578,7 @@ public class ServiceComponentHostImpl implements 
ServiceComponentHost {
    * @return
    */
   private HostComponentDesiredStateEntity getDesiredStateEntity() {
-    return hostComponentDesiredStateDAO.findByPK(desiredStateEntityPK);
+    return hostComponentDesiredStateDAO.findById(desiredStateEntityId);
   }
 
   /**
@@ -1593,15 +1591,4 @@ public class ServiceComponentHostImpl implements 
ServiceComponentHost {
     return hostComponentStateDAO.findById(hostComponentStateId);
   }
 
-  // create a PK object from the given desired component state entity.
-  private static HostComponentDesiredStateEntityPK 
getHostComponentDesiredStateEntityPK(
-      HostComponentDesiredStateEntity desiredStateEntity) {
-
-    HostComponentDesiredStateEntityPK dpk = new 
HostComponentDesiredStateEntityPK();
-    dpk.setClusterId(desiredStateEntity.getClusterId());
-    dpk.setComponentName(desiredStateEntity.getComponentName());
-    dpk.setServiceName(desiredStateEntity.getServiceName());
-    dpk.setHostId(desiredStateEntity.getHostId());
-    return dpk;
-  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java
index 3f35adc..95e5cd9 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java
@@ -17,10 +17,20 @@
  */
 package org.apache.ambari.server.upgrade;
 
-import com.google.inject.Inject;
-import com.google.inject.Injector;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicLong;
+
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.actionmanager.CommandExecutionType;
+import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.orm.DBAccessor;
 import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
@@ -31,14 +41,10 @@ import org.apache.ambari.server.state.Config;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.jdbc.support.JdbcUtils;
 
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
 
 /**
  * Upgrade catalog for version 2.5.0.
@@ -62,6 +68,10 @@ public class UpgradeCatalog250 extends 
AbstractUpgradeCatalog {
   protected static final String CREDENTIAL_STORE_SUPPORTED_COL = 
"credential_store_supported";
   protected static final String CREDENTIAL_STORE_ENABLED_COL = 
"credential_store_enabled";
 
+  protected static final String HOST_COMPONENT_DESIREDSTATE_TABLE = 
"hostcomponentdesiredstate";
+  protected static final String HOST_COMPONENT_DESIREDSTATE_ID_COL = "id";
+  protected static final String HOST_COMPONENT_DESIREDSTATE_INDEX = 
"UQ_hcdesiredstate_name";
+
   /**
    * Logger.
    */
@@ -116,6 +126,7 @@ public class UpgradeCatalog250 extends 
AbstractUpgradeCatalog {
       new DBAccessor.DBColumnInfo("command_execution_type", String.class, 32, 
CommandExecutionType.STAGE.toString(),
         false));
     updateServiceDesiredStateTable();
+    updateHostComponentDesiredStateTable();
   }
 
   /**
@@ -330,6 +341,101 @@ public class UpgradeCatalog250 extends 
AbstractUpgradeCatalog {
       new DBColumnInfo(CREDENTIAL_STORE_ENABLED_COL, Short.class, null, 0, 
false));
   }
 
+
+  /**
+   * Removes the compound PK from hostcomponentdesiredstate table
+   * and replaces it with a surrogate PK, but only if the table doesn't have 
it's new PK set.
+   * Create index and unqiue constraint on the columns that originally formed 
the compound PK.
+   *
+   * @throws SQLException
+   */
+  private void updateHostComponentDesiredStateTable() throws SQLException {
+    if (dbAccessor.tableHasPrimaryKey(HOST_COMPONENT_DESIREDSTATE_TABLE, 
HOST_COMPONENT_DESIREDSTATE_ID_COL)) {
+      LOG.info("Skipping {} table Primary Key modifications since the new {} 
column already exists",
+        HOST_COMPONENT_DESIREDSTATE_TABLE, HOST_COMPONENT_DESIREDSTATE_ID_COL);
+
+      return;
+    }
+    // add the new ID column as nullable until we populate
+    dbAccessor.addColumn(HOST_COMPONENT_DESIREDSTATE_TABLE,
+      new DBColumnInfo(HOST_COMPONENT_DESIREDSTATE_ID_COL, Long.class, null, 
null, true));
+
+    // insert sequence values
+    AtomicLong id = new AtomicLong(1);
+    Statement statement = null;
+    ResultSet resultSet = null;
+
+    try {
+      statement = dbAccessor.getConnection().createStatement();
+
+      if (statement != null) {
+        // Select records by old PK
+        String selectSQL = String.format(
+          "SELECT cluster_id, component_name, host_id, service_name FROM %s", 
HOST_COMPONENT_DESIREDSTATE_TABLE);
+
+        resultSet = statement.executeQuery(selectSQL);
+
+        while (resultSet.next()) {
+          final Long clusterId = resultSet.getLong("cluster_id");
+          final String componentName = resultSet.getString("component_name");
+          final Long hostId = resultSet.getLong("host_id");
+          final String serviceName = resultSet.getString("service_name");
+
+          String updateSQL = String.format(
+            "UPDATE %s SET %s = %s WHERE cluster_id = %d AND component_name = 
'%s' AND service_name = '%s' AND host_id = %d",
+            HOST_COMPONENT_DESIREDSTATE_TABLE, 
HOST_COMPONENT_DESIREDSTATE_ID_COL, id.getAndIncrement(),
+            clusterId, componentName, serviceName, hostId);
+
+          dbAccessor.executeQuery(updateSQL);
+        }
+
+        // Add sequence for hostcomponentdesiredstate table ids
+        addSequence("hostcomponentdesiredstate_id_seq", id.get(), false);
+      }
+
+    }
+    finally {
+      JdbcUtils.closeResultSet(resultSet);
+      JdbcUtils.closeStatement(statement);
+    }
+
+    // make the ID column NON NULL now
+    dbAccessor.alterColumn(HOST_COMPONENT_DESIREDSTATE_TABLE,
+      new DBColumnInfo(HOST_COMPONENT_DESIREDSTATE_ID_COL, Long.class, null, 
null, false));
+
+    // drop existing PK and create new one on ID column
+    String primaryKeyConstraintName = null;
+    Configuration.DatabaseType databaseType = configuration.getDatabaseType();
+
+    switch (databaseType) {
+      case POSTGRES:
+      case MYSQL:
+      case ORACLE:
+      case SQL_SERVER:
+        primaryKeyConstraintName = 
dbAccessor.getPrimaryKeyConstraintName(HOST_COMPONENT_DESIREDSTATE_TABLE);
+        break;
+
+      default:
+        throw new UnsupportedOperationException(String.format("Invalid 
database type '%s'", databaseType));
+
+    }
+
+    // warn if we can't find it
+    if (null == primaryKeyConstraintName) {
+      LOG.warn("Unable to determine the primary key constraint name for {}", 
HOST_COMPONENT_DESIREDSTATE_TABLE);
+    }
+    else {
+      dbAccessor.dropPKConstraint(HOST_COMPONENT_DESIREDSTATE_TABLE, 
primaryKeyConstraintName, true);
+    }
+
+    // create a new PK, matching the name of the constraint found in the SQL 
files
+    dbAccessor.addPKConstraint(HOST_COMPONENT_DESIREDSTATE_TABLE, 
"PK_hostcomponentdesiredstate", "id");
+
+    // create index, ensuring column order matches that of the SQL files
+    dbAccessor.addUniqueConstraint(HOST_COMPONENT_DESIREDSTATE_TABLE, 
HOST_COMPONENT_DESIREDSTATE_INDEX,
+      "component_name", "service_name", "host_id", "cluster_id");
+  }
+
   protected void updateAtlasConfigs() throws AmbariException {
     AmbariManagementController ambariManagementController = 
injector.getInstance(AmbariManagementController.class);
     Clusters clusters = ambariManagementController.getClusters();

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql 
b/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql
index 8cf2c0d..6d79cd4 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql
@@ -197,6 +197,7 @@ CREATE TABLE servicecomponentdesiredstate (
   CONSTRAINT srvccmponentdesiredstatesrvcnm FOREIGN KEY (service_name, 
cluster_id) REFERENCES clusterservices (service_name, cluster_id));
 
 CREATE TABLE hostcomponentdesiredstate (
+  id BIGINT NOT NULL,
   cluster_id BIGINT NOT NULL,
   component_name VARCHAR(255) NOT NULL,
   desired_stack_id BIGINT NOT NULL,
@@ -207,11 +208,13 @@ CREATE TABLE hostcomponentdesiredstate (
   maintenance_state VARCHAR(32) NOT NULL,
   security_state VARCHAR(32) NOT NULL DEFAULT 'UNSECURED',
   restart_required SMALLINT NOT NULL DEFAULT 0,
-  CONSTRAINT PK_hostcomponentdesiredstate PRIMARY KEY (cluster_id, 
component_name, host_id, service_name),
+  CONSTRAINT PK_hostcomponentdesiredstate PRIMARY KEY (id),
+  CONSTRAINT UQ_hcdesiredstate_name UNIQUE (component_name, service_name, 
host_id, cluster_id),
   CONSTRAINT FK_hcdesiredstate_host_id FOREIGN KEY (host_id) REFERENCES hosts 
(host_id),
   CONSTRAINT FK_hcds_desired_stack_id FOREIGN KEY (desired_stack_id) 
REFERENCES stack(stack_id),
   CONSTRAINT hstcmpnntdesiredstatecmpnntnme FOREIGN KEY (component_name, 
service_name, cluster_id) REFERENCES servicecomponentdesiredstate 
(component_name, service_name, cluster_id));
 
+
 CREATE TABLE hostcomponentstate (
   id BIGINT NOT NULL,
   cluster_id BIGINT NOT NULL,
@@ -1158,7 +1161,9 @@ INSERT INTO ambari_sequences (sequence_name, 
sequence_value)
   union all
   select 'remote_cluster_service_id_seq', 0 FROM SYSIBM.SYSDUMMY1
   union all
-  select 'servicecomponent_version_id_seq', 0 FROM SYSIBM.SYSDUMMY1;
+  select 'servicecomponent_version_id_seq', 0 FROM SYSIBM.SYSDUMMY1
+  union all
+  select 'hostcomponentdesiredstate_id_seq', 0 FROM SYSIBM.SYSDUMMY1;
 
 
 INSERT INTO adminresourcetype (resource_type_id, resource_type_name)

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql 
b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
index 82ce31e..b493d0a 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
@@ -207,6 +207,7 @@ CREATE TABLE servicecomponentdesiredstate (
   CONSTRAINT srvccmponentdesiredstatesrvcnm FOREIGN KEY (service_name, 
cluster_id) REFERENCES clusterservices (service_name, cluster_id));
 
 CREATE TABLE hostcomponentdesiredstate (
+  id BIGINT NOT NULL,
   cluster_id BIGINT NOT NULL,
   component_name VARCHAR(100) NOT NULL,
   desired_stack_id BIGINT NOT NULL,
@@ -217,11 +218,13 @@ CREATE TABLE hostcomponentdesiredstate (
   maintenance_state VARCHAR(32) NOT NULL DEFAULT 'ACTIVE',
   security_state VARCHAR(32) NOT NULL DEFAULT 'UNSECURED',
   restart_required TINYINT(1) NOT NULL DEFAULT 0,
-  CONSTRAINT PK_hostcomponentdesiredstate PRIMARY KEY (cluster_id, 
component_name, host_id, service_name),
+  CONSTRAINT PK_hostcomponentdesiredstate PRIMARY KEY (id),
+  CONSTRAINT UQ_hcdesiredstate_name UNIQUE (component_name, service_name, 
host_id, cluster_id),
   CONSTRAINT FK_hcdesiredstate_host_id FOREIGN KEY (host_id) REFERENCES hosts 
(host_id),
   CONSTRAINT FK_hcds_desired_stack_id FOREIGN KEY (desired_stack_id) 
REFERENCES stack(stack_id),
   CONSTRAINT hstcmpnntdesiredstatecmpnntnme FOREIGN KEY (component_name, 
service_name, cluster_id) REFERENCES servicecomponentdesiredstate 
(component_name, service_name, cluster_id));
 
+
 CREATE TABLE hostcomponentstate (
   id BIGINT NOT NULL,
   cluster_id BIGINT NOT NULL,
@@ -1123,7 +1126,8 @@ INSERT INTO ambari_sequences(sequence_name, 
sequence_value) VALUES
   ('ambari_operation_history_id_seq', 0),
   ('remote_cluster_id_seq', 0),
   ('remote_cluster_service_id_seq', 0),
-  ('servicecomponent_version_id_seq', 0);
+  ('servicecomponent_version_id_seq', 0),
+  ('hostcomponentdesiredstate_id_seq', 0);
 
 INSERT INTO adminresourcetype (resource_type_id, resource_type_name) VALUES
   (1, 'AMBARI'),

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql 
b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
index e2c2dd5..3e40103 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
@@ -188,6 +188,7 @@ CREATE TABLE servicecomponentdesiredstate (
   CONSTRAINT srvccmponentdesiredstatesrvcnm FOREIGN KEY (service_name, 
cluster_id) REFERENCES clusterservices (service_name, cluster_id));
 
 CREATE TABLE hostcomponentdesiredstate (
+  id NUMBER(19) NOT NULL,
   cluster_id NUMBER(19) NOT NULL,
   component_name VARCHAR2(255) NOT NULL,
   desired_stack_id NUMBER(19) NULL,
@@ -198,7 +199,8 @@ CREATE TABLE hostcomponentdesiredstate (
   maintenance_state VARCHAR2(32) NOT NULL,
   security_state VARCHAR2(32) DEFAULT 'UNSECURED' NOT NULL,
   restart_required NUMBER(1) DEFAULT 0 NOT NULL,
-  CONSTRAINT PK_hostcomponentdesiredstate PRIMARY KEY (cluster_id, 
component_name, host_id, service_name),
+  CONSTRAINT PK_hostcomponentdesiredstate PRIMARY KEY (id),
+  CONSTRAINT UQ_hcdesiredstate_name UNIQUE (component_name, service_name, 
host_id, cluster_id),
   CONSTRAINT FK_hcdesiredstate_host_id FOREIGN KEY (host_id) REFERENCES hosts 
(host_id),
   CONSTRAINT FK_hcds_desired_stack_id FOREIGN KEY (desired_stack_id) 
REFERENCES stack(stack_id),
   CONSTRAINT hstcmpnntdesiredstatecmpnntnme FOREIGN KEY (component_name, 
service_name, cluster_id) REFERENCES servicecomponentdesiredstate 
(component_name, service_name, cluster_id));
@@ -1104,6 +1106,7 @@ INSERT INTO ambari_sequences(sequence_name, 
sequence_value) values ('ambari_oper
 INSERT INTO ambari_sequences(sequence_name, sequence_value) values 
('remote_cluster_id_seq', 0);
 INSERT INTO ambari_sequences(sequence_name, sequence_value) values 
('remote_cluster_service_id_seq', 0);
 INSERT INTO ambari_sequences(sequence_name, sequence_value) values 
('servicecomponent_version_id_seq', 0);
+INSERT INTO ambari_sequences(sequence_name, sequence_value) values 
('hostcomponentdesiredstate_id_seq', 0);
 
 INSERT INTO metainfo("metainfo_key", "metainfo_value") values ('version', 
'${ambariSchemaVersion}');
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql 
b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
index 4e9a535..e072805 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
@@ -197,6 +197,7 @@ CREATE TABLE servicecomponentdesiredstate (
   CONSTRAINT srvccmponentdesiredstatesrvcnm FOREIGN KEY (service_name, 
cluster_id) REFERENCES clusterservices (service_name, cluster_id));
 
 CREATE TABLE hostcomponentdesiredstate (
+  id BIGINT NOT NULL,
   cluster_id BIGINT NOT NULL,
   component_name VARCHAR(255) NOT NULL,
   desired_stack_id BIGINT NOT NULL,
@@ -207,7 +208,8 @@ CREATE TABLE hostcomponentdesiredstate (
   maintenance_state VARCHAR(32) NOT NULL,
   security_state VARCHAR(32) NOT NULL DEFAULT 'UNSECURED',
   restart_required SMALLINT NOT NULL DEFAULT 0,
-  CONSTRAINT PK_hostcomponentdesiredstate PRIMARY KEY (cluster_id, 
component_name, host_id, service_name),
+  CONSTRAINT PK_hostcomponentdesiredstate PRIMARY KEY (id),
+  CONSTRAINT UQ_hcdesiredstate_name UNIQUE (component_name, service_name, 
host_id, cluster_id),
   CONSTRAINT FK_hcdesiredstate_host_id FOREIGN KEY (host_id) REFERENCES hosts 
(host_id),
   CONSTRAINT FK_hcds_desired_stack_id FOREIGN KEY (desired_stack_id) 
REFERENCES stack(stack_id),
   CONSTRAINT hstcmpnntdesiredstatecmpnntnme FOREIGN KEY (component_name, 
service_name, cluster_id) REFERENCES servicecomponentdesiredstate 
(component_name, service_name, cluster_id));
@@ -1104,7 +1106,8 @@ INSERT INTO ambari_sequences (sequence_name, 
sequence_value) VALUES
   ('ambari_operation_history_id_seq', 0),
   ('remote_cluster_id_seq', 0),
   ('remote_cluster_service_id_seq', 0),
-  ('servicecomponent_version_id_seq', 0);
+  ('servicecomponent_version_id_seq', 0),
+  ('hostcomponentdesiredstate_id_seq', 0);
 
 INSERT INTO adminresourcetype (resource_type_id, resource_type_name) VALUES
   (1, 'AMBARI'),

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql 
b/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql
index 0ba7df6..01d9be5 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql
@@ -186,6 +186,7 @@ CREATE TABLE servicecomponentdesiredstate (
   CONSTRAINT srvccmponentdesiredstatesrvcnm FOREIGN KEY (service_name, 
cluster_id) REFERENCES clusterservices (service_name, cluster_id));
 
 CREATE TABLE hostcomponentdesiredstate (
+  id NUMERIC(19) NOT NULL,
   cluster_id NUMERIC(19) NOT NULL,
   component_name VARCHAR(255) NOT NULL,
   desired_stack_id NUMERIC(19) NOT NULL,
@@ -196,7 +197,8 @@ CREATE TABLE hostcomponentdesiredstate (
   maintenance_state VARCHAR(32) NOT NULL DEFAULT 'ACTIVE',
   security_state VARCHAR(32) NOT NULL DEFAULT 'UNSECURED',
   restart_required BIT NOT NULL DEFAULT 0,
-  CONSTRAINT PK_hostcomponentdesiredstate PRIMARY KEY (cluster_id, 
component_name, host_id, service_name),
+  CONSTRAINT PK_hostcomponentdesiredstate PRIMARY KEY (id),
+  CONSTRAINT UQ_hcdesiredstate_name UNIQUE (component_name, service_name, 
host_id, cluster_id),
   CONSTRAINT FK_hcdesiredstate_host_id FOREIGN KEY (host_id) REFERENCES hosts 
(host_id),
   CONSTRAINT FK_hcds_desired_stack_id FOREIGN KEY (desired_stack_id) 
REFERENCES stack(stack_id),
   CONSTRAINT hstcmpnntdesiredstatecmpnntnme FOREIGN KEY (component_name, 
service_name, cluster_id) REFERENCES servicecomponentdesiredstate 
(component_name, service_name, cluster_id));
@@ -1103,6 +1105,7 @@ INSERT INTO ambari_sequences(sequence_name, 
sequence_value) values ('ambari_oper
 INSERT INTO ambari_sequences(sequence_name, sequence_value) values 
('remote_cluster_id_seq', 0);
 INSERT INTO ambari_sequences(sequence_name, sequence_value) values 
('remote_cluster_service_id_seq', 0);
 INSERT INTO ambari_sequences(sequence_name, sequence_value) values 
('servicecomponent_version_id_seq', 0);
+INSERT INTO ambari_sequences(sequence_name, sequence_value) values 
('hostcomponentdesiredstate_id_seq', 0);
 
 insert into adminresourcetype (resource_type_id, resource_type_name)
   select 1, 'AMBARI'

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql 
b/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql
index d8cad6f..dc03827 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql
@@ -210,6 +210,7 @@ CREATE TABLE servicecomponentdesiredstate (
   CONSTRAINT srvccmponentdesiredstatesrvcnm FOREIGN KEY (service_name, 
cluster_id) REFERENCES clusterservices (service_name, cluster_id));
 
 CREATE TABLE hostcomponentdesiredstate (
+  id BIGINT NOT NULL,
   cluster_id BIGINT NOT NULL,
   component_name VARCHAR(255) NOT NULL,
   desired_stack_id BIGINT NOT NULL,
@@ -220,11 +221,13 @@ CREATE TABLE hostcomponentdesiredstate (
   maintenance_state VARCHAR(32) NOT NULL,
   security_state VARCHAR(32) NOT NULL DEFAULT 'UNSECURED',
   restart_required BIT NOT NULL DEFAULT 0,
-  CONSTRAINT PK_hostcomponentdesiredstate PRIMARY KEY CLUSTERED (cluster_id, 
component_name, host_id, service_name),
+  CONSTRAINT PK_hostcomponentdesiredstate PRIMARY KEY CLUSTERED (id),
+  CONSTRAINT UQ_hcdesiredstate_name UNIQUE NONCLUSTERED (component_name, 
service_name, host_id, cluster_id),
   CONSTRAINT FK_hcds_desired_stack_id FOREIGN KEY (desired_stack_id) 
REFERENCES stack(stack_id),
   CONSTRAINT hstcmpnntdesiredstatecmpnntnme FOREIGN KEY (component_name, 
service_name, cluster_id) REFERENCES servicecomponentdesiredstate 
(component_name, service_name, cluster_id),
   CONSTRAINT hstcmponentdesiredstatehstid FOREIGN KEY (host_id) REFERENCES 
hosts (host_id));
 
+
 CREATE TABLE hostcomponentstate (
   id BIGINT NOT NULL,
   cluster_id BIGINT NOT NULL,
@@ -1128,7 +1131,8 @@ BEGIN TRANSACTION
     ('ambari_operation_history_id_seq', 0),
     ('remote_cluster_id_seq', 0),
     ('remote_cluster_service_id_seq', 0),
-    ('servicecomponent_version_id_seq', 0);
+    ('servicecomponent_version_id_seq', 0),
+    ('hostcomponentdesiredstate_id_seq', 0);
 
   insert into adminresourcetype (resource_type_id, resource_type_name)
   values

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
index 0cf7f09..0f615ee 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
@@ -39,7 +39,6 @@ import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
 import org.apache.ambari.server.orm.dao.UpgradeDAO;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntityPK;
 import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
@@ -245,16 +244,14 @@ public class ServiceComponentTest {
     HostComponentStateDAO liveStateDAO = injector.getInstance(
         HostComponentStateDAO.class);
 
-    HostComponentDesiredStateEntityPK dPK =
-        new HostComponentDesiredStateEntityPK();
-
-    dPK.setClusterId(cluster.getClusterId());
-    dPK.setComponentName(componentName);
-    dPK.setHostId(hostEntity1.getHostId());
-    dPK.setServiceName(serviceName);
 
     HostComponentDesiredStateEntity desiredStateEntity =
-        desiredStateDAO.findByPK(dPK);
+        desiredStateDAO.findByIndex(
+          cluster.getClusterId(),
+          serviceName,
+          componentName,
+          hostEntity1.getHostId()
+        );
 
     HostComponentStateEntity stateEntity = 
liveStateDAO.findByIndex(cluster.getClusterId(),
         serviceName, componentName, hostEntity1.getHostId());

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/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 d75d9d0..b190704 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
@@ -54,7 +54,6 @@ import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.TopologyRequestDAO;
 import org.apache.ambari.server.orm.entities.ClusterStateEntity;
-import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntityPK;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.state.AgentVersion;
 import org.apache.ambari.server.state.Cluster;
@@ -453,18 +452,16 @@ public class ClustersTest {
     ServiceComponentHost serviceCheckNodeHost = 
serviceCheckNode.addServiceComponentHost(h2);
     serviceCheckNodeHost.setState(State.UNKNOWN);
 
-    HostComponentDesiredStateEntityPK hkdspk = new 
HostComponentDesiredStateEntityPK();
-
-    hkdspk.setClusterId(nameNodeHost.getClusterId());
-    hkdspk.setHostId(nameNodeHostEntity.getHostId());
-    hkdspk.setServiceName(nameNodeHost.getServiceName());
-    hkdspk.setComponentName(nameNodeHost.getServiceComponentName());
-
     
Assert.assertNotNull(injector.getInstance(HostComponentStateDAO.class).findByIndex(
       nameNodeHost.getClusterId(), nameNodeHost.getServiceName(),
       nameNodeHost.getServiceComponentName(), nameNodeHostEntity.getHostId()));
 
-    
Assert.assertNotNull(injector.getInstance(HostComponentDesiredStateDAO.class).findByPK(hkdspk));
+    
Assert.assertNotNull(injector.getInstance(HostComponentDesiredStateDAO.class).findByIndex(
+      nameNodeHost.getClusterId(),
+      nameNodeHost.getServiceName(),
+      nameNodeHost.getServiceComponentName(),
+      nameNodeHostEntity.getHostId()
+    ));
     Assert.assertEquals(2, 
injector.getProvider(EntityManager.class).get().createQuery("SELECT config FROM 
ClusterConfigEntity config").getResultList().size());
     Assert.assertEquals(1, 
injector.getProvider(EntityManager.class).get().createQuery("SELECT state FROM 
ClusterStateEntity state").getResultList().size());
     Assert.assertEquals(1, 
injector.getProvider(EntityManager.class).get().createQuery("SELECT config FROM 
ClusterConfigMappingEntity config").getResultList().size());
@@ -502,7 +499,10 @@ public class ClustersTest {
       nameNodeHost.getClusterId(), nameNodeHost.getServiceName(),
       nameNodeHost.getServiceComponentName(), nameNodeHostEntity.getHostId()));
 
-    
Assert.assertNull(injector.getInstance(HostComponentDesiredStateDAO.class).findByPK(hkdspk));
+    
Assert.assertNull(injector.getInstance(HostComponentDesiredStateDAO.class).findByIndex(
+      nameNodeHost.getClusterId(), nameNodeHost.getServiceName(),
+      nameNodeHost.getServiceComponentName(), nameNodeHostEntity.getHostId()
+    ));
     Assert.assertEquals(0, 
injector.getProvider(EntityManager.class).get().createQuery("SELECT config FROM 
ClusterConfigEntity config").getResultList().size());
     Assert.assertEquals(0, 
injector.getProvider(EntityManager.class).get().createQuery("SELECT state FROM 
ClusterStateEntity state").getResultList().size());
     Assert.assertEquals(0, 
injector.getProvider(EntityManager.class).get().createQuery("SELECT config FROM 
ClusterConfigMappingEntity config").getResultList().size());

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/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 5987af3..de2b59d 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
@@ -39,7 +39,6 @@ import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntityPK;
 import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.state.Cluster;
@@ -1057,20 +1056,24 @@ public class ServiceComponentHostTest {
     //ServiceComponentHost sch2 = createNewServiceComponentHost(cluster, 
"HDFS", "DATANODE", hostName);
     //ServiceComponentHost sch3 = createNewServiceComponentHost(cluster, 
"MAPREDUCE2", "HISTORYSERVER", hostName);
 
-    HostComponentDesiredStateEntityPK pk = new 
HostComponentDesiredStateEntityPK();
-    pk.setClusterId(Long.valueOf(cluster.getClusterId()));
-    pk.setComponentName(sch1.getServiceComponentName());
-    pk.setServiceName(sch1.getServiceName());
-    pk.setHostId(hostEntity.getHostId());
-
-    HostComponentDesiredStateEntity entity = 
hostComponentDesiredStateDAO.findByPK(pk);
+    HostComponentDesiredStateEntity entity = 
hostComponentDesiredStateDAO.findByIndex(
+      cluster.getClusterId(),
+      sch1.getServiceName(),
+      sch1.getServiceComponentName(),
+      hostEntity.getHostId()
+    );
     Assert.assertEquals(MaintenanceState.OFF, entity.getMaintenanceState());
     Assert.assertEquals(MaintenanceState.OFF, sch1.getMaintenanceState());
 
     sch1.setMaintenanceState(MaintenanceState.ON);
     Assert.assertEquals(MaintenanceState.ON, sch1.getMaintenanceState());
 
-    entity = hostComponentDesiredStateDAO.findByPK(pk);
+    entity = hostComponentDesiredStateDAO.findByIndex(
+      cluster.getClusterId(),
+      sch1.getServiceName(),
+      sch1.getServiceComponentName(),
+      hostEntity.getHostId()
+    );
     Assert.assertEquals(MaintenanceState.ON, entity.getMaintenanceState());
   }
 
@@ -1097,12 +1100,6 @@ public class ServiceComponentHostTest {
     ServiceComponentHost sch1 = createNewServiceComponentHost(cluster, "HDFS", 
"NAMENODE", hostName);
 
     HostComponentDesiredStateEntity entityHostComponentDesiredState;
-    HostComponentDesiredStateEntityPK pkHostComponentDesiredState = new 
HostComponentDesiredStateEntityPK();
-    pkHostComponentDesiredState.setClusterId(cluster.getClusterId());
-    
pkHostComponentDesiredState.setComponentName(sch1.getServiceComponentName());
-    pkHostComponentDesiredState.setServiceName(sch1.getServiceName());
-    pkHostComponentDesiredState.setHostId(hostEntity.getHostId());
-
     HostComponentStateEntity entityHostComponentState;
 
     for(SecurityState state: SecurityState.values()) {
@@ -1116,7 +1113,12 @@ public class ServiceComponentHostTest {
       try {
         sch1.setDesiredSecurityState(state);
         Assert.assertTrue(state.isEndpoint());
-        entityHostComponentDesiredState = 
hostComponentDesiredStateDAO.findByPK(pkHostComponentDesiredState);
+        entityHostComponentDesiredState = 
hostComponentDesiredStateDAO.findByIndex(
+          cluster.getClusterId(),
+          sch1.getServiceName(),
+          sch1.getServiceComponentName(),
+          hostEntity.getHostId()
+        );
         Assert.assertNotNull(entityHostComponentDesiredState);
         Assert.assertEquals(state, 
entityHostComponentDesiredState.getSecurityState());
       } catch (AmbariException e) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/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 4c347ed..fa3e224 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
@@ -67,7 +67,6 @@ 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;
-import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntityPK;
 import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import 
org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
@@ -643,12 +642,12 @@ public class UpgradeCatalog200Test {
 
     assertNotNull(serviceComponentDesiredStateEntity);
 
-    HostComponentDesiredStateEntityPK hcDesiredStateEntityPk = new 
HostComponentDesiredStateEntityPK();
-    hcDesiredStateEntityPk.setServiceName("NAGIOS");
-    hcDesiredStateEntityPk.setClusterId(clusterEntity.getClusterId());
-    hcDesiredStateEntityPk.setComponentName("NAGIOS_SERVER");
-    hcDesiredStateEntityPk.setHostId(hostEntity.getHostId());
-    HostComponentDesiredStateEntity hcDesiredStateEntity = 
hostComponentDesiredStateDAO.findByPK(hcDesiredStateEntityPk);
+    HostComponentDesiredStateEntity hcDesiredStateEntity = 
hostComponentDesiredStateDAO.findByIndex(
+      clusterEntity.getClusterId(),
+      "NAGIOS",
+      "NAGIOS_SERVER",
+      hostEntity.getHostId()
+    );
     assertNotNull(hcDesiredStateEntity);
 
     HostComponentStateEntity hcStateEntity = hostComponentStateDAO.findByIndex(

http://git-wip-us.apache.org/repos/asf/ambari/blob/d9a03d43/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java
index 9eb4db9..627ef07 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java
@@ -40,10 +40,14 @@ import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.stack.OsFamily;
 import org.easymock.Capture;
 import org.easymock.EasyMock;
+import org.easymock.EasyMockRunner;
 import org.easymock.EasyMockSupport;
+import org.easymock.Mock;
+import org.easymock.MockType;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 
 import javax.persistence.EntityManager;
 import java.lang.reflect.Method;
@@ -58,8 +62,6 @@ import static org.easymock.EasyMock.anyObject;
 import static org.easymock.EasyMock.anyString;
 import static org.easymock.EasyMock.capture;
 import static org.easymock.EasyMock.createMockBuilder;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.createStrictMock;
 import static org.easymock.EasyMock.eq;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.expectLastCall;
@@ -72,17 +74,66 @@ import static org.junit.Assert.assertTrue;
 /**
  * {@link UpgradeCatalog250} unit tests.
  */
+@RunWith(EasyMockRunner.class)
 public class UpgradeCatalog250Test {
 
   //  private Injector injector;
-  private Provider<EntityManager> entityManagerProvider = 
createStrictMock(Provider.class);
-  private EntityManager entityManager = createNiceMock(EntityManager.class);
+  @Mock(type = MockType.STRICT)
+  private Provider<EntityManager> entityManagerProvider;
+
+  @Mock(type = MockType.NICE)
+  private EntityManager entityManager;
+
+  @Mock(type = MockType.NICE)
+  private DBAccessor dbAccessor;
+
+  @Mock(type = MockType.NICE)
+  private Configuration configuration;
+
+  @Mock(type = MockType.NICE)
+  private Connection connection;
+
+  @Mock(type = MockType.NICE)
+  private Statement statement;
+
+  @Mock(type = MockType.NICE)
+  private ResultSet resultSet;
+
+  @Mock(type = MockType.NICE)
+  private OsFamily osFamily;
+
+  @Mock(type = MockType.NICE)
+  private KerberosHelper kerberosHelper;
+
+  @Mock(type = MockType.NICE)
+  private ActionManager actionManager;
+
+  @Mock(type = MockType.NICE)
+  private Config config;
+
+  @Mock(type = MockType.STRICT)
+  private Service service;
+
+  @Mock(type = MockType.NICE)
+  private Clusters clusters;
+
+  @Mock(type = MockType.NICE)
+  private  Cluster cluster;
+
+  @Mock(type = MockType.NICE)
+  private Injector injector;
 
   @Before
   public void init() {
-    reset(entityManagerProvider);
+    reset(entityManagerProvider, injector);
+
     expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
-    replay(entityManagerProvider);
+
+    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
+    
expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
+    
expect(injector.getInstance(KerberosHelper.class)).andReturn(kerberosHelper).anyTimes();
+
+    replay(entityManagerProvider, injector);
   }
 
   @After
@@ -91,14 +142,6 @@ public class UpgradeCatalog250Test {
 
   @Test
   public void testExecuteDDLUpdates() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-
-    Configuration configuration = createNiceMock(Configuration.class);
-    Connection connection = createNiceMock(Connection.class);
-    Statement statement = createNiceMock(Statement.class);
-    ResultSet resultSet = createNiceMock(ResultSet.class);
-
-
     // !!! setup capture for host_version
     dbAccessor.addUniqueConstraint("host_version", "UQ_host_repo", 
"repo_version_id", "host_id");
 
@@ -129,9 +172,10 @@ public class UpgradeCatalog250Test {
     dbAccessor.addColumn(eq(UpgradeCatalog250.SERVICE_DESIRED_STATE_TABLE), 
capture(capturedCredentialStoreSupportedCol));
     dbAccessor.addColumn(eq(UpgradeCatalog250.SERVICE_DESIRED_STATE_TABLE), 
capture(capturedCredentialStoreEnabledCol));
 
-    expect(dbAccessor.getConnection()).andReturn(connection);
-    expect(connection.createStatement()).andReturn(statement);
-    
expect(statement.executeQuery(anyObject(String.class))).andReturn(resultSet);
+    expect(dbAccessor.getConnection()).andReturn(connection).anyTimes();
+    expect(connection.createStatement()).andReturn(statement).anyTimes();
+    
expect(statement.executeQuery(anyObject(String.class))).andReturn(resultSet).anyTimes();
+    
expect(configuration.getDatabaseType()).andReturn(Configuration.DatabaseType.POSTGRES).anyTimes();
 
     replay(dbAccessor, configuration, connection, statement, resultSet);
 
@@ -139,8 +183,9 @@ public class UpgradeCatalog250Test {
       @Override
       public void configure(Binder binder) {
         binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
+        binder.bind(OsFamily.class).toInstance(osFamily);
         binder.bind(EntityManager.class).toInstance(entityManager);
+        binder.bind(Configuration.class).toInstance(configuration);
       }
     };
 
@@ -292,28 +337,22 @@ public class UpgradeCatalog250Test {
     };
     EasyMockSupport easyMockSupport = new EasyMockSupport();
 
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
     Config mockAmsEnv = easyMockSupport.createNiceMock(Config.class);
 
+    reset(clusters, cluster);
     expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
       put("normal", cluster);
     }}).once();
     
expect(cluster.getDesiredConfigByType("ams-env")).andReturn(mockAmsEnv).atLeastOnce();
     
expect(mockAmsEnv.getProperties()).andReturn(oldPropertiesAmsEnv).anyTimes();
 
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    
expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    
expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockAmsEnv, cluster);
+    replay(clusters, mockAmsEnv, cluster);
 
-    AmbariManagementControllerImpl controller = 
createMockBuilder(AmbariManagementControllerImpl.class)
+    AmbariManagementControllerImpl controller = 
(AmbariManagementControllerImpl)createMockBuilder(AmbariManagementControllerImpl.class)
       .addMockedMethod("createConfiguration")
-      .addMockedMethod("getClusters", new Class[]{})
+      .addMockedMethod("getClusters", new Class[] { })
       .addMockedMethod("createConfig")
-      .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
+      .withConstructor(actionManager, clusters, injector)
       .createNiceMock();
 
     Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
@@ -322,7 +361,7 @@ public class UpgradeCatalog250Test {
     
expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
     expect(controller.getClusters()).andReturn(clusters).anyTimes();
     expect(controller.createConfig(anyObject(Cluster.class), anyString(), 
capture(propertiesCapture), anyString(),
-      anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
+      anyObject(Map.class))).andReturn(config).once();
 
     replay(controller, injector2);
     new UpgradeCatalog250(injector2).updateAMSConfigs();
@@ -332,8 +371,9 @@ public class UpgradeCatalog250Test {
     assertTrue(Maps.difference(newPropertiesAmsEnv, 
updatedProperties).areEqual());
   }
 
+
   @Test
-  public void testKafkaUpdateConfigs() throws Exception {
+  public void testKafkaUpdateConfigs() throws Exception{
 
     Map<String, String> oldProperties = new HashMap<String, String>() {
       {
@@ -347,29 +387,22 @@ public class UpgradeCatalog250Test {
       }
     };
     EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
     Config mockKafkaBroker = easyMockSupport.createNiceMock(Config.class);
 
+    reset(clusters, cluster);
     expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
       put("normal", cluster);
     }}).once();
     
expect(cluster.getDesiredConfigByType("kafka-broker")).andReturn(mockKafkaBroker).atLeastOnce();
     
expect(mockKafkaBroker.getProperties()).andReturn(oldProperties).anyTimes();
 
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    
expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    
expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
+    replay(clusters, mockKafkaBroker, cluster);
 
-    replay(injector, clusters, mockKafkaBroker, cluster);
-
-    AmbariManagementControllerImpl controller = 
createMockBuilder(AmbariManagementControllerImpl.class)
+    AmbariManagementControllerImpl controller = 
(AmbariManagementControllerImpl)createMockBuilder(AmbariManagementControllerImpl.class)
       .addMockedMethod("createConfiguration")
-      .addMockedMethod("getClusters", new Class[]{})
+      .addMockedMethod("getClusters", new Class[] { })
       .addMockedMethod("createConfig")
-      .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
+      .withConstructor(actionManager, clusters, injector)
       .createNiceMock();
 
     Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
@@ -378,7 +411,7 @@ public class UpgradeCatalog250Test {
     
expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
     expect(controller.getClusters()).andReturn(clusters).anyTimes();
     expect(controller.createConfig(anyObject(Cluster.class), anyString(), 
capture(propertiesCapture), anyString(),
-      anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
+      anyObject(Map.class))).andReturn(config).once();
 
     replay(controller, injector2);
     new UpgradeCatalog250(injector2).updateKafkaConfigs();
@@ -431,10 +464,7 @@ public class UpgradeCatalog250Test {
 
     EasyMockSupport easyMockSupport = new EasyMockSupport();
 
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    final Service service = createStrictMock(Service.class);
-
+    reset(clusters, cluster);
     expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
       put("normal", cluster);
     }}).once();
@@ -445,18 +475,13 @@ public class UpgradeCatalog250Test {
     
expect(cluster.getDesiredConfigByType(configType)).andReturn(mockAtlasConfig).atLeastOnce();
     
expect(mockAtlasConfig.getProperties()).andReturn(oldProperties).anyTimes();
 
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    
expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    
expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockAtlasConfig, cluster);
+    replay(clusters, mockAtlasConfig, cluster);
 
-    AmbariManagementControllerImpl controller = 
createMockBuilder(AmbariManagementControllerImpl.class)
+    AmbariManagementControllerImpl controller = 
(AmbariManagementControllerImpl)createMockBuilder(AmbariManagementControllerImpl.class)
       .addMockedMethod("createConfiguration")
       .addMockedMethod("getClusters", new Class[] { })
       .addMockedMethod("createConfig")
-      .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
+      .withConstructor(actionManager, clusters, injector)
       .createNiceMock();
 
     Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
@@ -465,7 +490,7 @@ public class UpgradeCatalog250Test {
     
expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
     expect(controller.getClusters()).andReturn(clusters).anyTimes();
     expect(controller.createConfig(anyObject(Cluster.class), anyString(), 
capture(propertiesCapture), anyString(),
-      anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
+      anyObject(Map.class))).andReturn(config).once();
 
     replay(controller, injector2);
     new UpgradeCatalog250(injector2).updateAtlasConfigs();

Reply via email to