소스 검색

AMBARI-4215. Enable caching in ConfigGroupHostMappingDAO. (Oleksandr Diachenko via swagle)

Siddharth Wagle 11 년 전
부모
커밋
54549b2d73

+ 35 - 0
ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMapping.java

@@ -0,0 +1,35 @@
+/**
+ * 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.cache;
+
+import org.apache.ambari.server.state.Host;
+import org.apache.ambari.server.state.configgroup.ConfigGroup;
+
+public interface ConfigGroupHostMapping {
+  
+  public Long getConfigGroupId();
+  public String getHostname();
+  public Host getHost();
+  public ConfigGroup getConfigGroup();
+  
+  public void setConfigGroupId(Long configGroupId);
+  public void setHostname(String hostname);
+  public void setHost(Host host);
+  public void setConfigGroup(ConfigGroup configGroup);
+}

+ 121 - 0
ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMappingImpl.java

@@ -0,0 +1,121 @@
+/**
+ * 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.cache;
+
+import org.apache.ambari.server.state.Host;
+import org.apache.ambari.server.state.configgroup.ConfigGroup;
+
+public class ConfigGroupHostMappingImpl implements ConfigGroupHostMapping {
+
+  private Long configGroupId;
+  private String hostname;
+  private Host host;
+  private ConfigGroup configGroup;
+
+  @Override
+  public Long getConfigGroupId() {
+    return configGroupId;
+  }
+
+  @Override
+  public String getHostname() {
+    return hostname;
+  }
+
+  @Override
+  public Host getHost() {
+    return host;
+  }
+
+  @Override
+  public ConfigGroup getConfigGroup() {
+    return configGroup;
+  }
+
+  @Override
+  public void setConfigGroupId(Long configGroupId) {
+    this.configGroupId = configGroupId;
+
+  }
+
+  @Override
+  public void setHostname(String hostname) {
+    this.hostname = hostname;
+
+  }
+
+  @Override
+  public void setHost(Host host) {
+    this.host = host;
+
+  }
+
+  @Override
+  public void setConfigGroup(ConfigGroup configGroup) {
+    this.configGroup = configGroup;
+
+  }
+
+  @Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result =
+        prime * result + ((configGroup == null) ? 0 : configGroup.hashCode());
+    result =
+        prime * result
+            + ((configGroupId == null) ? 0 : configGroupId.hashCode());
+    result = prime * result + ((host == null) ? 0 : host.hashCode());
+    result = prime * result + ((hostname == null) ? 0 : hostname.hashCode());
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (obj == null)
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    ConfigGroupHostMappingImpl other = (ConfigGroupHostMappingImpl) obj;
+    if (configGroup == null) {
+      if (other.configGroup != null)
+        return false;
+    } else if (!configGroup.equals(other.configGroup))
+      return false;
+    if (configGroupId == null) {
+      if (other.configGroupId != null)
+        return false;
+    } else if (!configGroupId.equals(other.configGroupId))
+      return false;
+    if (host == null) {
+      if (other.host != null)
+        return false;
+    } else if (!host.equals(other.host))
+      return false;
+    if (hostname == null) {
+      if (other.hostname != null)
+        return false;
+    } else if (!hostname.equals(other.hostname))
+      return false;
+    return true;
+  }
+
+}

+ 1 - 1
ambari-server/src/main/java/org/apache/ambari/server/state/HostConfigMapping.java → ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMapping.java

@@ -15,7 +15,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ambari.server.state;
+package org.apache.ambari.server.orm.cache;
 
 public interface HostConfigMapping {
   

+ 2 - 1
ambari-server/src/main/java/org/apache/ambari/server/state/HostConfigMappingImpl.java → ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMappingImpl.java

@@ -16,7 +16,8 @@
  * limitations under the License.
  */
 
-package org.apache.ambari.server.state;
+package org.apache.ambari.server.orm.cache;
+
 
 public class HostConfigMappingImpl implements HostConfigMapping {
   

+ 240 - 30
ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ConfigGroupHostMappingDAO.java

@@ -21,12 +21,33 @@ import com.google.inject.Inject;
 import com.google.inject.Provider;
 import com.google.inject.Singleton;
 import com.google.inject.persist.Transactional;
+
+import org.apache.ambari.server.orm.cache.ConfigGroupHostMapping;
+import org.apache.ambari.server.orm.cache.ConfigGroupHostMappingImpl;
+import org.apache.ambari.server.orm.entities.ConfigGroupEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupHostMappingEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupHostMappingEntityPK;
+import org.apache.ambari.server.orm.entities.HostEntity;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Host;
+import org.apache.ambari.server.state.cluster.ClusterFactory;
+import org.apache.ambari.server.state.configgroup.ConfigGroup;
+import org.apache.ambari.server.state.configgroup.ConfigGroupFactory;
+import org.apache.ambari.server.state.host.HostFactory;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.Predicate;
+
 import javax.persistence.EntityManager;
-import javax.persistence.NoResultException;
 import javax.persistence.TypedQuery;
+
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.WeakHashMap;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 @Singleton
 public class ConfigGroupHostMappingDAO {
@@ -34,75 +55,218 @@ public class ConfigGroupHostMappingDAO {
   Provider<EntityManager> entityManagerProvider;
   @Inject
   DaoUtils daoUtils;
+  @Inject
+  private ConfigGroupFactory configGroupFactory;
+  @Inject
+  private ClusterFactory clusterFactory;
+  @Inject
+  private HostFactory hostFactory;
+  
+  private final ReadWriteLock gl = new ReentrantReadWriteLock();
+  
+  private Map<String, Set<ConfigGroupHostMapping>> configGroupHostMappingByHost;
+  
+  private volatile boolean cacheLoaded;
+
+  
+  private void populateCache() {
+    
+    if (!cacheLoaded) {
+      gl.writeLock().lock();
+      try {
+        if (configGroupHostMappingByHost == null) {
+          configGroupHostMappingByHost = new WeakHashMap<String, Set<ConfigGroupHostMapping>>();
+          
+          TypedQuery<ConfigGroupHostMappingEntity> query = entityManagerProvider.get().createQuery(
+              "SELECT entity FROM ConfigGroupHostMappingEntity entity",
+              ConfigGroupHostMappingEntity.class);
 
+          List<ConfigGroupHostMappingEntity> configGroupHostMappingEntities = daoUtils.selectList(query);
+          
+          for (ConfigGroupHostMappingEntity configGroupHostMappingEntity : configGroupHostMappingEntities) {
+
+            Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get((configGroupHostMappingEntity.getHostname()));
+              
+            if (setByHost == null) {
+              setByHost = new HashSet<ConfigGroupHostMapping>();
+              configGroupHostMappingByHost.put(configGroupHostMappingEntity.getHostname(), setByHost);
+            }
+       
+            ConfigGroupHostMapping configGroupHostMapping = buildConfigGroupHostMapping(configGroupHostMappingEntity);
+            setByHost.add(configGroupHostMapping);
+          } 
+        }
+      } finally {
+        gl.writeLock().unlock();
+      }
+      
+      cacheLoaded = true;
+
+    }
+    
+  }
+  
   @Transactional
-  public ConfigGroupHostMappingEntity findByPK(ConfigGroupHostMappingEntityPK
+  public ConfigGroupHostMapping findByPK(final ConfigGroupHostMappingEntityPK
         configGroupHostMappingEntityPK) {
-    return entityManagerProvider.get().find(ConfigGroupHostMappingEntity
-      .class, configGroupHostMappingEntityPK);
+    
+    populateCache();
+    
+    if (!configGroupHostMappingByHost.containsKey(configGroupHostMappingEntityPK.getHostname()))
+      return null;
+    
+    Set<ConfigGroupHostMapping> set = 
+        new HashSet<ConfigGroupHostMapping>(configGroupHostMappingByHost.get(configGroupHostMappingEntityPK.getHostname()));
+    
+    ConfigGroupHostMapping itemByPk = (ConfigGroupHostMapping) CollectionUtils.find(set, new Predicate() {
+      
+      @Override
+      public boolean evaluate(Object arg0) {
+        return ((ConfigGroupHostMapping) arg0).getConfigGroupId().
+            equals(configGroupHostMappingEntityPK.getConfigGroupId());
+      }
+    });
+    
+    return itemByPk;
   }
 
   @Transactional
-  public List<ConfigGroupHostMappingEntity> findByHost(String hostname) {
-    TypedQuery<ConfigGroupHostMappingEntity> query = entityManagerProvider
-      .get().createNamedQuery("groupsByHost", ConfigGroupHostMappingEntity
-        .class);
-
-    query.setParameter("hostname", hostname);
-    try {
-      return query.getResultList();
-    } catch (NoResultException ignored) {
-    }
-    return null;
+  public Set<ConfigGroupHostMapping> findByHost(String hostname) {
+    
+    populateCache();
+    
+    if (!configGroupHostMappingByHost.containsKey(hostname))
+      return null;
+    
+    Set<ConfigGroupHostMapping> set = new HashSet<ConfigGroupHostMapping>(configGroupHostMappingByHost.get(hostname));
+    
+    return set;
+    
   }
 
   @Transactional
-  public List<ConfigGroupHostMappingEntity> findByGroup(Long groupId) {
-    TypedQuery<ConfigGroupHostMappingEntity> query = entityManagerProvider
-      .get().createNamedQuery("hostsByGroup", ConfigGroupHostMappingEntity
-        .class);
-
-    query.setParameter("groupId", groupId);
-    try {
-      return query.getResultList();
-    } catch (NoResultException ignored) {
+  public Set<ConfigGroupHostMapping> findByGroup(final Long groupId) {
+    
+    populateCache();
+    
+    Set<ConfigGroupHostMapping> result = new HashSet<ConfigGroupHostMapping>();
+    
+    for (Set<ConfigGroupHostMapping> item : configGroupHostMappingByHost.values()) {
+      
+      Set<ConfigGroupHostMapping> setByHost = new HashSet<ConfigGroupHostMapping>(item);
+      
+      CollectionUtils.filter(setByHost, new Predicate() {
+        
+        @Override
+        public boolean evaluate(Object arg0) {
+          return ((ConfigGroupHostMapping) arg0).getConfigGroupId().equals(groupId);
+        }
+      });
+      
+      result.addAll(setByHost);
+      
     }
-    return null;
+    
+    return result;
+    
   }
 
   @Transactional
   public void create(ConfigGroupHostMappingEntity
                          configGroupHostMappingEntity) {
+    
+    populateCache();
+
     entityManagerProvider.get().persist(configGroupHostMappingEntity);
+    
+    //create in cache
+    Set<ConfigGroupHostMapping> set = configGroupHostMappingByHost.get(configGroupHostMappingEntity.getHostname());
+    if (set == null){
+      set = new HashSet<ConfigGroupHostMapping>();
+      configGroupHostMappingByHost.put(configGroupHostMappingEntity.getHostname(), set);
+    }
+    
+    set.add(buildConfigGroupHostMapping(configGroupHostMappingEntity));
+    
   }
 
+
+
   @Transactional
-  public ConfigGroupHostMappingEntity merge(ConfigGroupHostMappingEntity
-                         configGroupHostMappingEntity) {
+  public ConfigGroupHostMappingEntity merge(ConfigGroupHostMappingEntity configGroupHostMappingEntity) {
+    
+    populateCache();
+    
+    Set<ConfigGroupHostMapping> set = configGroupHostMappingByHost.get(configGroupHostMappingEntity.getHostname());
+    if (set == null){
+      set = new HashSet<ConfigGroupHostMapping>();
+      configGroupHostMappingByHost.put(configGroupHostMappingEntity.getHostname(), set);
+    }
+    
+    //Update object in set
+    set.remove(buildConfigGroupHostMapping(configGroupHostMappingEntity));
+    set.add(buildConfigGroupHostMapping(configGroupHostMappingEntity));
+    
+    
     return entityManagerProvider.get().merge(configGroupHostMappingEntity);
   }
 
   @Transactional
   public void refresh(ConfigGroupHostMappingEntity
                          configGroupHostMappingEntity) {
+    cacheLoaded = false;
+    populateCache();
+    
     entityManagerProvider.get().refresh(configGroupHostMappingEntity);
   }
 
   @Transactional
-  public void remove(ConfigGroupHostMappingEntity
+  public void remove(final ConfigGroupHostMappingEntity
                          configGroupHostMappingEntity) {
+    
+    populateCache();
+    
     entityManagerProvider.get().remove(merge(configGroupHostMappingEntity));
+    
+    Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get(configGroupHostMappingEntity.getHostname());
+    
+    if (setByHost != null) {
+      CollectionUtils.filter(setByHost, new Predicate() {
+        
+        @Override
+        public boolean evaluate(Object arg0) {
+          return !((ConfigGroupHostMapping) arg0).getConfigGroupId().
+              equals(configGroupHostMappingEntity.getConfigGroupId());
+        }
+      });
+    }
   }
 
   @Transactional
-  public void removeByPK(ConfigGroupHostMappingEntityPK
+  public void removeByPK(final ConfigGroupHostMappingEntityPK
                          configGroupHostMappingEntityPK) {
+    populateCache();
+    
     entityManagerProvider.get().remove(findByPK
       (configGroupHostMappingEntityPK));
+    
+    Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get(configGroupHostMappingEntityPK.getHostname());
+    
+    if (setByHost != null) {
+      CollectionUtils.filter(setByHost, new Predicate() {
+        
+        @Override
+        public boolean evaluate(Object arg0) {
+          return !((ConfigGroupHostMapping) arg0).getConfigGroupId().
+              equals(configGroupHostMappingEntityPK.getConfigGroupId());
+        }
+      });
+    }
+    
   }
 
   @Transactional
-  public void removeAllByGroup(Long groupId) {
+  public void removeAllByGroup(final Long groupId) {
     TypedQuery<Long> query = entityManagerProvider.get().createQuery
       ("DELETE FROM ConfigGroupHostMappingEntity confighosts WHERE " +
         "confighosts.configGroupId = ?1", Long.class);
@@ -111,6 +275,18 @@ public class ConfigGroupHostMappingDAO {
     // Flush to current transaction required in order to avoid Eclipse link
     // from re-ordering delete
     entityManagerProvider.get().flush();
+    
+    for (Set<ConfigGroupHostMapping> setByHost : configGroupHostMappingByHost.values()) {
+      
+      CollectionUtils.filter(setByHost, new Predicate() {
+        
+        @Override
+        public boolean evaluate(Object arg0) {
+          return !((ConfigGroupHostMapping) arg0).getConfigGroupId().equals(groupId);
+        }
+      });
+    }
+    
   }
 
   @Transactional
@@ -120,5 +296,39 @@ public class ConfigGroupHostMappingDAO {
         "confighosts.hostname = ?1", String.class);
 
     daoUtils.executeUpdate(query, hostname);
+    
+    
+    Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get(hostname);
+    
+    setByHost.clear();
+    
+    
+  }
+  
+  private ConfigGroupHostMapping buildConfigGroupHostMapping(
+      ConfigGroupHostMappingEntity configGroupHostMappingEntity) {
+    
+    ConfigGroupHostMappingImpl configGroupHostMapping = new ConfigGroupHostMappingImpl();
+    configGroupHostMapping.setConfigGroup(buildConfigGroup(configGroupHostMappingEntity.getConfigGroupEntity()));
+    configGroupHostMapping.setConfigGroupId(configGroupHostMappingEntity.getConfigGroupId());
+    configGroupHostMapping.setHost(buildHost(configGroupHostMappingEntity.getHostEntity()));
+    configGroupHostMapping.setHostname(configGroupHostMappingEntity.getHostname());
+    
+    return configGroupHostMapping;
+  }
+
+  private ConfigGroup buildConfigGroup(ConfigGroupEntity configGroupEntity) {
+    
+    Cluster cluster = clusterFactory.create(configGroupEntity.getClusterEntity());
+    ConfigGroup configGroup = configGroupFactory.createExisting(cluster, configGroupEntity);
+    
+    return configGroup;
+  }
+
+  private Host buildHost(HostEntity hostEntity) {
+    
+    Host host = hostFactory.create(hostEntity, false);
+    
+    return host;
   }
 }

+ 4 - 4
ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAO.java

@@ -27,9 +27,9 @@ import javax.persistence.TypedQuery;
 
 import com.google.inject.Singleton;
 
+import org.apache.ambari.server.orm.cache.HostConfigMapping;
+import org.apache.ambari.server.orm.cache.HostConfigMappingImpl;
 import org.apache.ambari.server.orm.entities.HostConfigMappingEntity;
-import org.apache.ambari.server.state.HostConfigMapping;
-import org.apache.ambari.server.state.HostConfigMappingImpl;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.collections.Predicate;
 
@@ -61,7 +61,7 @@ public class HostConfigMappingDAO {
         gl.writeLock().lock();
         try {
           if (hostConfigMappingByHost == null) {
-            hostConfigMappingByHost = new ConcurrentHashMap<String, Set<HostConfigMapping>>();
+            hostConfigMappingByHost = new WeakHashMap<String, Set<HostConfigMapping>>();
             
             TypedQuery<HostConfigMappingEntity> query = entityManagerProvider.get().createQuery(
                 "SELECT entity FROM HostConfigMappingEntity entity",
@@ -71,7 +71,7 @@ public class HostConfigMappingDAO {
             
             for (HostConfigMappingEntity hostConfigMappingEntity : hostConfigMappingEntities) {
 
-              Set<HostConfigMapping> setByHost = hostConfigMappingByHost.get((hostConfigMappingEntity.getType()));
+              Set<HostConfigMapping> setByHost = hostConfigMappingByHost.get((hostConfigMappingEntity.getHostName()));
               
               if (setByHost == null) {
                 setByHost = new HashSet<HostConfigMapping>();

+ 4 - 4
ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java

@@ -27,6 +27,8 @@ import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.ServiceComponentHostNotFoundException;
 import org.apache.ambari.server.ServiceNotFoundException;
 import org.apache.ambari.server.controller.ClusterResponse;
+import org.apache.ambari.server.orm.cache.ConfigGroupHostMapping;
+import org.apache.ambari.server.orm.cache.HostConfigMapping;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.ClusterStateDAO;
 import org.apache.ambari.server.orm.dao.ConfigGroupHostMappingDAO;
@@ -38,14 +40,12 @@ import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
 import org.apache.ambari.server.orm.entities.ClusterStateEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupHostMappingEntity;
-import org.apache.ambari.server.state.HostConfigMapping;
 import org.apache.ambari.server.orm.entities.RequestScheduleEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.ConfigFactory;
 import org.apache.ambari.server.state.DesiredConfig;
-import org.apache.ambari.server.state.HostConfigMapping;
 import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentHost;
@@ -369,11 +369,11 @@ public class ClusterImpl implements Cluster {
     try {
       readLock.lock();
       try {
-        List<ConfigGroupHostMappingEntity> hostMappingEntities =
+        Set<ConfigGroupHostMapping> hostMappingEntities =
           configGroupHostMappingDAO.findByHost(hostname);
 
         if (hostMappingEntities != null && !hostMappingEntities.isEmpty()) {
-          for (ConfigGroupHostMappingEntity entity : hostMappingEntities) {
+          for (ConfigGroupHostMapping entity : hostMappingEntities) {
             ConfigGroup configGroup = configGroupMap.get(entity.getConfigGroupId());
             if (configGroup != null && !configGroups.containsKey(configGroup.getId())) {
               configGroups.put(configGroup.getId(), configGroup);

+ 0 - 3
ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroup.java

@@ -20,13 +20,10 @@ package org.apache.ambari.server.state.configgroup;
 
 import com.google.inject.persist.Transactional;
 import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.DuplicateResourceException;
 import org.apache.ambari.server.controller.ConfigGroupResponse;
-import org.apache.ambari.server.orm.entities.ConfigGroupEntity;
 import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.Host;
 
-import java.util.List;
 import java.util.Map;
 
 /**

+ 0 - 1
ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupFactory.java

@@ -24,7 +24,6 @@ import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.Host;
 import org.apache.ambari.server.state.configgroup.ConfigGroup;
 
-import java.util.List;
 import java.util.Map;
 
 public interface ConfigGroupFactory {

+ 2 - 0
ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupImpl.java

@@ -26,7 +26,9 @@ import com.google.inject.persist.Transactional;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.DuplicateResourceException;
 import org.apache.ambari.server.controller.ConfigGroupResponse;
+import org.apache.ambari.server.controller.internal.ConfigGroupResourceProvider;
 import org.apache.ambari.server.controller.internal.ConfigurationResourceProvider;
+import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.ConfigGroupConfigMappingDAO;
 import org.apache.ambari.server.orm.dao.ConfigGroupDAO;

+ 2 - 2
ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java

@@ -31,6 +31,8 @@ import org.apache.ambari.server.agent.AgentEnv;
 import org.apache.ambari.server.agent.DiskInfo;
 import org.apache.ambari.server.agent.HostInfo;
 import org.apache.ambari.server.controller.HostResponse;
+import org.apache.ambari.server.orm.cache.HostConfigMapping;
+import org.apache.ambari.server.orm.cache.HostConfigMappingImpl;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.ConfigGroupHostMappingDAO;
 import org.apache.ambari.server.orm.dao.HostConfigMappingDAO;
@@ -46,8 +48,6 @@ import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.DesiredConfig;
 import org.apache.ambari.server.state.Host;
 import org.apache.ambari.server.state.HostConfig;
-import org.apache.ambari.server.state.HostConfigMapping;
-import org.apache.ambari.server.state.HostConfigMappingImpl;
 import org.apache.ambari.server.state.HostEvent;
 import org.apache.ambari.server.state.HostEventType;
 import org.apache.ambari.server.state.HostHealthStatus;

+ 9 - 4
ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ConfigGroupDAOTest.java

@@ -24,6 +24,7 @@ import junit.framework.Assert;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.orm.cache.ConfigGroupHostMapping;
 import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupConfigMappingEntity;
@@ -36,6 +37,7 @@ import org.junit.Test;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 
 public class ConfigGroupDAOTest {
   private Injector injector;
@@ -191,13 +193,16 @@ public class ConfigGroupDAOTest {
     Assert.assertNotNull(configGroupEntity
       .getConfigGroupHostMappingEntities().iterator().next());
 
-    List<ConfigGroupHostMappingEntity> hostMappingEntities = configGroupHostMappingDAO
+    Set<ConfigGroupHostMapping> hostMappingEntities = configGroupHostMappingDAO
       .findByHost("h1");
 
     Assert.assertNotNull(hostMappingEntities);
-    Assert.assertEquals("h1", hostMappingEntities.get(0).getHostname());
-    Assert.assertEquals("centOS", hostMappingEntities.get(0).getHostEntity()
-      .getOsType());
+    
+    for (ConfigGroupHostMapping hostMappingEntity : hostMappingEntities) {
+    
+      Assert.assertEquals("h1", hostMappingEntity.getHostname());
+      Assert.assertEquals("centOS", hostMappingEntity.getHost().getOsType());
+    }
   }
 
   @Test

+ 2 - 2
ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAOTest.java

@@ -24,8 +24,8 @@ import junit.framework.Assert;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.state.HostConfigMapping;
-import org.apache.ambari.server.state.HostConfigMappingImpl;
+import org.apache.ambari.server.orm.cache.HostConfigMapping;
+import org.apache.ambari.server.orm.cache.HostConfigMappingImpl;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;