ソースを参照

AMBARI-4130. Deleted host still appears in configuration groups. (swagle)

Siddharth Wagle 11 年 前
コミット
f963f667e3

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

@@ -43,6 +43,7 @@ import org.apache.ambari.server.state.HostHealthStatus.HealthStatus;
 import org.apache.ambari.server.state.HostState;
 import org.apache.ambari.server.state.RepositoryInfo;
 import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.state.configgroup.ConfigGroup;
 import org.apache.ambari.server.state.host.HostFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -609,6 +610,8 @@ public class ClustersImpl implements Clusters {
       hostClusterMap.get(hostname).remove(cluster);
       clusterHostMap.get(clusterName).remove(host);
 
+      deleteConfigGroupHostMapping(hostname);
+
     } finally {
       w.unlock();
     }
@@ -623,11 +626,19 @@ public class ClustersImpl implements Clusters {
     hostEntity.getClusterEntities().remove(clusterEntity);
     clusterEntity.getHostEntities().remove(hostEntity);
 
-    configGroupHostMappingDAO.removeAllByHost(hostName);
-    
     hostDAO.merge(hostEntity);
     clusterDAO.merge(clusterEntity);
   }
+
+  @Transactional
+  private void deleteConfigGroupHostMapping(String hostname) throws AmbariException {
+    // Remove Config group mapping
+    for (Cluster cluster : clusters.values()) {
+      for (ConfigGroup configGroup : cluster.getConfigGroups().values()) {
+        configGroup.removeHost(hostname);
+      }
+    }
+  }
   
   @Override
   public void deleteHost(String hostname) throws AmbariException {
@@ -643,8 +654,7 @@ public class ClustersImpl implements Clusters {
       hostDAO.refresh(entity);
       hostDAO.remove(entity);
       hosts.remove(hostname);
-      // Remove Config group mapping
-      configGroupHostMappingDAO.removeAllByHost(hostname);
+      deleteConfigGroupHostMapping(hostname);
     } catch (Exception e) {
       throw new AmbariException("Could not remove host", e);
     } finally {

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

@@ -147,4 +147,9 @@ public interface ConfigGroup {
    * @param configs
    */
   public void setConfigurations(Map<String, Config> configs);
+
+  /**
+   * Remove host mapping
+   */
+  public void removeHost(String hostname) throws AmbariException;
 }

+ 27 - 1
ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupImpl.java

@@ -40,6 +40,7 @@ import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupConfigMappingEntity;
 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.Clusters;
@@ -61,7 +62,6 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 public class ConfigGroupImpl implements ConfigGroup {
   private static final Logger LOG = LoggerFactory.getLogger(ConfigGroupImpl.class);
-  //private final ReadWriteLock clusterGlobalLock;
   private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
 
   private Cluster cluster;
@@ -261,7 +261,33 @@ public class ConfigGroupImpl implements ConfigGroup {
 
   @Override
   @Transactional
+  public void removeHost(String hostname) throws AmbariException {
+    readWriteLock.writeLock().lock();
+    try {
+      if (hosts.containsKey(hostname)) {
+        LOG.info("Removing host from config group, hostname = " + hostname);
+        hosts.remove(hostname);
+        try {
+          ConfigGroupHostMappingEntityPK hostMappingEntityPK = new
+            ConfigGroupHostMappingEntityPK();
+          hostMappingEntityPK.setHostname(hostname);
+          hostMappingEntityPK.setConfigGroupId(configGroupEntity.getGroupId());
+          configGroupHostMappingDAO.removeByPK(hostMappingEntityPK);
+        } catch (Exception e) {
+          LOG.error("Failed to delete config group host mapping"
+            + ", clusterName = " + getClusterName()
+            + ", id = " + getId()
+            + ", hostname = " + hostname, e);
+          throw new AmbariException(e.getMessage());
+        }
+      }
+    } finally {
+      readWriteLock.writeLock().unlock();
+    }
+  }
 
+  @Override
+  @Transactional
   public void persist() {
     readWriteLock.writeLock().lock();
     try {

+ 20 - 0
ambari-server/src/test/java/org/apache/ambari/server/state/ConfigGroupTest.java

@@ -27,6 +27,7 @@ import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.dao.ConfigGroupDAO;
+import org.apache.ambari.server.orm.dao.ConfigGroupHostMappingDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.entities.ConfigGroupConfigMappingEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupEntity;
@@ -53,6 +54,7 @@ public class ConfigGroupTest {
   private ConfigFactory configFactory;
   private HostDAO hostDAO;
   private ConfigGroupDAO configGroupDAO;
+  private ConfigGroupHostMappingDAO configGroupHostMappingDAO;
 
   @Before
   public void setup() throws Exception {
@@ -64,6 +66,8 @@ public class ConfigGroupTest {
     configGroupFactory = injector.getInstance(ConfigGroupFactory.class);
     hostDAO = injector.getInstance(HostDAO.class);
     configGroupDAO = injector.getInstance(ConfigGroupDAO.class);
+    configGroupHostMappingDAO = injector.getInstance
+      (ConfigGroupHostMappingDAO.class);
 
     metaInfo.init();
     clusterName = "foo";
@@ -187,6 +191,22 @@ public class ConfigGroupTest {
     Assert.assertNull(cluster.getConfigGroups().get(id));
   }
 
+  @Test
+  public void testRemoveHost() throws Exception {
+    ConfigGroup configGroup = createConfigGroup();
+    Assert.assertNotNull(configGroup);
+    Long id = configGroup.getId();
+
+    configGroup = cluster.getConfigGroups().get(id);
+    Assert.assertNotNull(configGroup);
+
+    clusters.unmapHostFromCluster("h1", clusterName);
+
+    Assert.assertNull(clusters.getHostsForCluster(clusterName).get("h1"));
+    Assert.assertTrue(configGroupHostMappingDAO.findByHost("h1").isEmpty());
+    Assert.assertNull(configGroup.getHosts().get("h1"));
+  }
+
   @Test
   public void testGetConfigGroup() throws Exception {
     ConfigGroup configGroup = createConfigGroup();