فهرست منبع

AMBARI-1250. Upgrade the posgres connector to 9.1. (mahadev)

git-svn-id: https://svn.apache.org/repos/asf/incubator/ambari/trunk@1437735 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar 12 سال پیش
والد
کامیت
9cc8651a51
22فایلهای تغییر یافته به همراه311 افزوده شده و 49 حذف شده
  1. 3 0
      CHANGES.txt
  2. 1 1
      ambari-server/pom.xml
  3. 12 4
      ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
  4. 5 0
      ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
  5. 1 1
      ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
  6. 1 1
      ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterServiceEntity.java
  7. 1 1
      ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostComponentStateEntity.java
  8. 5 1
      ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntity.java
  9. 2 0
      ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
  10. 3 3
      ambari-server/src/main/java/org/apache/ambari/server/state/ConfigImpl.java
  11. 2 1
      ambari-server/src/main/java/org/apache/ambari/server/state/Service.java
  12. 4 2
      ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponent.java
  13. 2 0
      ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentHost.java
  14. 33 5
      ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
  15. 34 8
      ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java
  16. 18 4
      ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
  17. 8 3
      ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
  18. 1 1
      ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
  19. 35 1
      ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
  20. 30 2
      ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java
  21. 25 0
      ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
  22. 85 10
      ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java

+ 3 - 0
CHANGES.txt

@@ -173,6 +173,9 @@ Trunk (unreleased changes):
  directories that we get from the server (only for hadoop and its eco system)
  and not all. (mahadev)
 
+ AMBARI-1250. Upgrade the posgres connector to 9.1.
+ (mahadev)
+
 AMBARI-1.2.0 branch:
 
  INCOMPATIBLE CHANGES

+ 1 - 1
ambari-server/pom.xml

@@ -531,7 +531,7 @@
     <dependency>
       <groupId>postgresql</groupId>
       <artifactId>postgresql</artifactId>
-      <version>8.3-603.jdbc4</version>
+      <version>9.1-901.jdbc4</version>
     </dependency>
   </dependencies>
   <!--<reporting>

+ 12 - 4
ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java

@@ -57,6 +57,7 @@ import org.apache.ambari.server.state.svccomphost.ServiceComponentHostOpInProgre
 import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStartEvent;
 import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStopEvent;
 import org.apache.ambari.server.utils.StageUtils;
+import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -2689,9 +2690,7 @@ public class AmbariManagementControllerImpl implements
   @Override
   public synchronized void deleteCluster(ClusterRequest request)
       throws AmbariException {
-    throw new AmbariException("Delete cluster not supported");
 
-    /*
     if (request.getClusterName() == null
         || request.getClusterName().isEmpty()) {
       // FIXME throw correct error
@@ -2705,13 +2704,22 @@ public class AmbariManagementControllerImpl implements
       // deleting whole cluster
       clusters.deleteCluster(request.getClusterName());
     }
-    */
   }
 
   @Override
   public RequestStatusResponse deleteServices(Set<ServiceRequest> request)
       throws AmbariException {
-    throw new AmbariException("Delete services not supported");
+
+    for (ServiceRequest serviceRequest : request) {
+      if (StringUtils.isEmpty(serviceRequest.getClusterName()) || StringUtils.isEmpty(serviceRequest.getServiceName())) {
+        // FIXME throw correct error
+        throw new AmbariException("invalid arguments");
+      } else {
+        clusters.getCluster(serviceRequest.getClusterName()).deleteService(serviceRequest.getServiceName());
+      }
+    }
+    return null;
+
   }
 
   @Override

+ 5 - 0
ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java

@@ -108,4 +108,9 @@ public class ClusterDAO {
     remove(findByName(clusterName));
   }
 
+  @Transactional
+  public void removeByPK(long id) {
+    remove(findById(id));
+  }
+
 }

+ 1 - 1
ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java

@@ -169,7 +169,7 @@ public class ClusterEntity {
   }
   
   private Collection<ClusterConfigEntity> configEntities;
-  @OneToMany(mappedBy = "clusterEntity")
+  @OneToMany(mappedBy = "clusterEntity", cascade = CascadeType.ALL)
   public Collection<ClusterConfigEntity> getClusterConfigEntities() {
     return configEntities;
   }

+ 1 - 1
ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterServiceEntity.java

@@ -105,7 +105,7 @@ public class ClusterServiceEntity {
 
   private ServiceDesiredStateEntity serviceDesiredStateEntity;
 
-  @OneToOne(mappedBy = "clusterServiceEntity")
+  @OneToOne(mappedBy = "clusterServiceEntity", cascade = CascadeType.ALL)
   public ServiceDesiredStateEntity getServiceDesiredStateEntity() {
     return serviceDesiredStateEntity;
   }

+ 1 - 1
ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostComponentStateEntity.java

@@ -158,7 +158,7 @@ public class HostComponentStateEntity {
 
   private Collection<HostComponentConfigMappingEntity> configMappingEntities;
   @OneToMany(mappedBy = "hostComponentStateEntity", cascade = CascadeType.ALL)
-  public Collection<HostComponentConfigMappingEntity> getHostComponentConfigMappingEntities() {
+   public Collection<HostComponentConfigMappingEntity> getHostComponentConfigMappingEntities() {
     return configMappingEntities;
   }
 

+ 5 - 1
ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntity.java

@@ -115,7 +115,11 @@ public class ServiceDesiredStateEntity {
   private ClusterServiceEntity clusterServiceEntity;
 
   @OneToOne
-  @javax.persistence.JoinColumns({@javax.persistence.JoinColumn(name = "cluster_id", referencedColumnName = "cluster_id", nullable = false), @javax.persistence.JoinColumn(name = "service_name", referencedColumnName = "service_name", nullable = false)})
+  @javax.persistence.JoinColumns(
+      {
+          @JoinColumn(name = "cluster_id", referencedColumnName = "cluster_id", nullable = false),
+          @JoinColumn(name = "service_name", referencedColumnName = "service_name", nullable = false)
+      })
   public ClusterServiceEntity getClusterServiceEntity() {
     return clusterServiceEntity;
   }

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

@@ -101,4 +101,6 @@ public interface Cluster {
   public void deleteService(String serviceName) throws AmbariException;
 
   public boolean canBeRemoved();
+
+  public void delete() throws AmbariException;
 }

+ 3 - 3
ambari-server/src/main/java/org/apache/ambari/server/state/ConfigImpl.java

@@ -42,7 +42,7 @@ public class ConfigImpl implements Config {
   private String versionTag;
   private Map<String, String> properties;
   private ClusterConfigEntity entity;
-  
+
   @Inject
   private ClusterDAO clusterDAO;
   @Inject
@@ -111,7 +111,7 @@ public class ConfigImpl implements Config {
   
   @Transactional
   @Override
-  public void persist() {
+  public synchronized void persist() {
     
     ClusterEntity clusterEntity = clusterDAO.findById(cluster.getClusterId());
     
@@ -128,7 +128,7 @@ public class ConfigImpl implements Config {
     clusterEntity.getClusterConfigEntities().add(entity);
     clusterDAO.merge(clusterEntity);
     cluster.refresh();
-    
+
   }
 
 

+ 2 - 1
ambari-server/src/main/java/org/apache/ambari/server/state/Service.java

@@ -76,11 +76,12 @@ public interface Service {
    */
   public boolean canBeRemoved();
 
-  public void removeAllComponents() throws AmbariException;
+  public void deleteAllComponents() throws AmbariException;
 
   public void deleteServiceComponent(String componentName)
       throws AmbariException;
 
   public boolean isClientOnlyService();
 
+  public void delete() throws AmbariException;
 }

+ 4 - 2
ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponent.java

@@ -75,11 +75,13 @@ public interface ServiceComponent {
 
   public boolean canBeRemoved();
 
-  public void removeAllServiceComponentHosts() throws AmbariException;
+  public void deleteAllServiceComponentHosts() throws AmbariException;
 
-  public void removeServiceComponentHosts(String hostname)
+  public void deleteServiceComponentHosts(String hostname)
       throws AmbariException;
 
   ServiceComponentHost addServiceComponentHost(
       String hostName) throws AmbariException;
+
+  public void delete() throws AmbariException;
 }

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

@@ -104,4 +104,6 @@ public interface ServiceComponentHost {
   public void debugDump(StringBuilder sb);
 
   public boolean canBeRemoved();
+
+  public void delete() throws AmbariException;
 }

+ 33 - 5
ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java

@@ -510,7 +510,8 @@ public class ServiceComponentImpl implements ServiceComponent {
   }
 
   @Override
-  public synchronized void removeAllServiceComponentHosts()
+  @Transactional
+  public synchronized void deleteAllServiceComponentHosts()
       throws AmbariException {
     LOG.info("Deleting all servicecomponenthosts for component"
         + ", clusterName=" + getClusterName()
@@ -527,12 +528,16 @@ public class ServiceComponentImpl implements ServiceComponent {
             + ", hostname=" + sch.getHostName());
       }
     }
+
+    for (ServiceComponentHost serviceComponentHost : hostComponents.values()) {
+      serviceComponentHost.delete();
+    }
+
     hostComponents.clear();
-    // FIXME update DB
   }
 
   @Override
-  public synchronized void removeServiceComponentHosts(String hostname)
+  public synchronized void deleteServiceComponentHosts(String hostname)
       throws AmbariException {
     ServiceComponentHost sch = getServiceComponentHost(hostname);
     LOG.info("Deleting servicecomponenthost for cluster"
@@ -547,16 +552,39 @@ public class ServiceComponentImpl implements ServiceComponent {
           + ", componentName=" + getName()
           + ", hostname=" + sch.getHostName());
     }
+    sch.delete();
     hostComponents.remove(hostname);
-    // FIXME update DB
   }
 
   @Override
   public synchronized void deleteDesiredConfigs(Set<String> configTypes) {
+    componentConfigMappingDAO.removeByType(configTypes);
     for (String configType : configTypes) {
       desiredConfigs.remove(configType);
     }
-    componentConfigMappingDAO.removeByType(configTypes);
+  }
+
+  @Override
+  @Transactional
+  public synchronized void delete() throws AmbariException {
+    deleteAllServiceComponentHosts();
+
+    if (persisted) {
+      removeEntities();
+      persisted = false;
+    }
+
+    desiredConfigs.clear();
+  }
+
+  @Transactional
+  protected void removeEntities() throws AmbariException {
+    ServiceComponentDesiredStateEntityPK pk = new ServiceComponentDesiredStateEntityPK();
+    pk.setClusterId(getClusterId());
+    pk.setComponentName(getName());
+    pk.setServiceName(getServiceName());
+
+    serviceComponentDesiredStateDAO.removeByPK(pk);
   }
 
 }

+ 34 - 8
ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java

@@ -414,8 +414,10 @@ public class ServiceImpl implements Service {
     serviceDesiredStateDAO.create(serviceDesiredStateEntity);
     clusterEntity.getClusterServiceEntities().add(serviceEntity);
     clusterDAO.merge(clusterEntity);
-    serviceEntity = clusterServiceDAO.merge(serviceEntity);
-    serviceDesiredStateEntity = serviceDesiredStateDAO.merge(serviceDesiredStateEntity);
+//    serviceEntity =
+        clusterServiceDAO.merge(serviceEntity);
+//    serviceDesiredStateEntity =
+        serviceDesiredStateDAO.merge(serviceDesiredStateEntity);
   }
 
   @Transactional
@@ -463,7 +465,8 @@ public class ServiceImpl implements Service {
   }
 
   @Override
-  public synchronized void removeAllComponents() throws AmbariException {
+  @Transactional
+  public synchronized void deleteAllComponents() throws AmbariException {
     LOG.info("Deleting all components for service"
         + ", clusterName=" + cluster.getClusterName()
         + ", serviceName=" + getName());
@@ -477,11 +480,12 @@ public class ServiceImpl implements Service {
             + ", componentName=" + component.getName());
       }
     }
-    for (ServiceComponent component : components.values()) {
-      component.removeAllServiceComponentHosts();
+
+    for (ServiceComponent serviceComponent : components.values()) {
+      serviceComponent.delete();
     }
+
     components.clear();
-    // FIXME update DB
   }
 
   @Override
@@ -499,9 +503,9 @@ public class ServiceImpl implements Service {
           + ", serviceName=" + getName()
           + ", componentName=" + componentName);
     }
-    component.removeAllServiceComponentHosts();
+
+    component.delete();
     components.remove(componentName);
-    // FIXME update DB
   }
 
   @Override
@@ -509,4 +513,26 @@ public class ServiceImpl implements Service {
     return isClientOnlyService;
   }
 
+  @Override
+  @Transactional
+  public synchronized void delete() throws AmbariException {
+    deleteAllComponents();
+
+    if (persisted) {
+      removeEntities();
+      persisted = false;
+    }
+
+    desiredConfigs.clear();
+  }
+
+  @Transactional
+  protected void removeEntities() throws AmbariException {
+    ClusterServiceEntityPK pk = new ClusterServiceEntityPK();
+    pk.setClusterId(getClusterId());
+    pk.setServiceName(getName());
+
+    clusterServiceDAO.removeByPK(pk);
+  }
+
 }

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

@@ -434,6 +434,7 @@ public class ClusterImpl implements Cluster {
   }
 
   @Override
+  @Transactional
   public synchronized void deleteAllServices() throws AmbariException {
     loadServices();
     LOG.info("Deleting all services for cluster"
@@ -446,11 +447,12 @@ public class ClusterImpl implements Cluster {
             + ", serviceName=" + service.getName());
       }
     }
+
     for (Service service : services.values()) {
-      service.removeAllComponents();
+      service.delete();
     }
+
     services.clear();
-    // FIXME update DB
   }
 
   @Override
@@ -467,9 +469,8 @@ public class ClusterImpl implements Cluster {
           + ", clusterName=" + getClusterName()
           + ", serviceName=" + service.getName());
     }
-    service.removeAllComponents();
+    service.delete();
     services.remove(serviceName);
-    // FIXME update DB
   }
 
   @Override
@@ -486,4 +487,17 @@ public class ClusterImpl implements Cluster {
     }
     return safeToRemove;
   }
+
+  @Override
+  @Transactional
+  public void delete() throws AmbariException {
+    deleteAllServices();
+    removeEntities();
+    configs.clear();
+  }
+
+  @Transactional
+  protected void removeEntities() throws AmbariException {
+    clusterDAO.removeByPK(getClusterId());
+  }
 }

+ 8 - 3
ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java

@@ -360,7 +360,6 @@ public class ClustersImpl implements Clusters {
   }
 
   @Override
-  @Transactional
   public synchronized void deleteCluster(String clusterName)
       throws AmbariException {
     Cluster cluster = getCluster(clusterName);
@@ -368,9 +367,15 @@ public class ClustersImpl implements Clusters {
       throw new AmbariException("Could not delete cluster"
           + ", clusterName=" + clusterName);
     }
-    cluster.deleteAllServices();
+    LOG.info("Deleting cluster "+ cluster.getClusterName());
+    cluster.delete();
+
+    //clear maps
+    for (Set<Cluster> clusterSet : hostClusterMap.values()) {
+      clusterSet.remove(cluster);
+    }
+    clusterHostMap.remove(cluster.getClusterName());
     clusters.remove(clusterName);
-    // FIXME update DB
   }
 
 }

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

@@ -213,7 +213,7 @@ public class HostImpl implements Host {
 
   }
 
-//  //TODO remove
+//  //TODO delete
 //  public HostImpl(String hostname) {
 //    this.stateMachine = stateMachineFactory.make(this);
 //    ReadWriteLock rwLock = new ReentrantReadWriteLock();

+ 35 - 1
ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java

@@ -1228,13 +1228,47 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
   public void deleteDesiredConfigs(Set<String> configTypes) {
     try {
       writeLock.lock();
+      hostComponentDesiredConfigMappingDAO.removeByType(configTypes);
       for (String configType : configTypes) {
         desiredConfigs.remove(configType);
       }
-      hostComponentDesiredConfigMappingDAO.removeByType(configTypes);
     } finally {
       writeLock.unlock();
     }
   }
 
+  @Override
+  public void delete() throws AmbariException {
+    try {
+      writeLock.lock();
+      if (persisted) {
+        removeEntities();
+        persisted = false;
+      }
+      desiredConfigs.clear();
+    } finally {
+      writeLock.unlock();
+    }
+
+  }
+
+  @Transactional
+  protected void removeEntities() {
+    HostComponentStateEntityPK pk = new HostComponentStateEntityPK();
+    pk.setClusterId(stateEntity.getClusterId());
+    pk.setComponentName(stateEntity.getComponentName());
+    pk.setServiceName(stateEntity.getServiceName());
+    pk.setHostName(stateEntity.getHostName());
+
+    hostComponentStateDAO.removeByPK(pk);
+
+    HostComponentDesiredStateEntityPK desiredPK = new HostComponentDesiredStateEntityPK();
+    desiredPK.setClusterId(desiredStateEntity.getClusterId());
+    desiredPK.setComponentName(desiredStateEntity.getComponentName());
+    desiredPK.setServiceName(desiredStateEntity.getServiceName());
+    desiredPK.setHostName(desiredStateEntity.getHostName());
+
+    hostComponentDesiredStateDAO.removeByPK(desiredPK);
+  }
+
 }

+ 30 - 2
ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java

@@ -18,8 +18,6 @@
 
 package org.apache.ambari.server.state;
 
-import static org.junit.Assert.fail;
-
 import java.util.HashMap;
 import java.util.Map;
 
@@ -38,6 +36,8 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import static org.junit.Assert.*;
+
 public class ServiceTest {
 
   private Clusters clusters;
@@ -224,4 +224,32 @@ public class ServiceTest {
 
   }
 
+  @Test
+  public void testDeleteServiceComponent() throws Exception {
+    Service hdfs = cluster.addService("HDFS");
+    Service mapReduce = cluster.addService("MAPREDUCE");
+
+    hdfs.persist();
+
+    ServiceComponent nameNode = hdfs.addServiceComponent("NAMENODE");
+    nameNode.persist();
+    ServiceComponent jobTracker = mapReduce.addServiceComponent("JOBTRACKER");
+
+    assertEquals(2, cluster.getServices().size());
+    assertEquals(1, hdfs.getServiceComponents().size());
+    assertEquals(1, mapReduce.getServiceComponents().size());
+    assertTrue(hdfs.isPersisted());
+    assertFalse(mapReduce.isPersisted());
+
+    hdfs.deleteServiceComponent("NAMENODE");
+
+    assertEquals(0, hdfs.getServiceComponents().size());
+    assertEquals(1, mapReduce.getServiceComponents().size());
+
+    mapReduce.deleteServiceComponent("JOBTRACKER");
+
+    assertEquals(0, hdfs.getServiceComponents().size());
+    assertEquals(0, mapReduce.getServiceComponents().size());
+
+  }
 }

+ 25 - 0
ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java

@@ -18,6 +18,8 @@
 
 package org.apache.ambari.server.state.cluster;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
@@ -36,6 +38,7 @@ import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.ClusterResponse;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.orm.dao.ClusterServiceDAO;
 import org.apache.ambari.server.state.AgentVersion;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
@@ -59,6 +62,8 @@ import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.persist.PersistService;
 
+import javax.persistence.EntityManager;
+
 public class ClusterTest {
 
   private Clusters clusters;
@@ -290,4 +295,24 @@ public class ClusterTest {
     c1.debugDump(sb);
   }
 
+  @Test
+  public void testDeleteService() throws Exception {
+    c1.addService("MAPREDUCE").persist();
+
+    Service hdfs = c1.addService("HDFS");
+    hdfs.persist();
+    ServiceComponent nameNode = hdfs.addServiceComponent("NAMENODE");
+    nameNode.persist();
+
+
+    assertEquals(2, c1.getServices().size());
+    assertEquals(2, injector.getProvider(EntityManager.class).get().
+        createQuery("SELECT service FROM ClusterServiceEntity service").getResultList().size());
+
+    c1.deleteService("HDFS");
+
+    assertEquals(1, c1.getServices().size());
+    assertEquals(1, injector.getProvider(EntityManager.class).get().
+        createQuery("SELECT service FROM ClusterServiceEntity service").getResultList().size());
+  }
 }

+ 85 - 10
ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java

@@ -18,12 +18,7 @@
 
 package org.apache.ambari.server.state.cluster;
 
-import static org.junit.Assert.fail;
-
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 import com.google.inject.Guice;
 import com.google.inject.Inject;
@@ -38,14 +33,18 @@ import org.apache.ambari.server.HostNotFoundException;
 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.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Host;
-import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.orm.dao.*;
+import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntityPK;
+import org.apache.ambari.server.orm.entities.HostComponentStateEntityPK;
+import org.apache.ambari.server.state.*;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import javax.persistence.EntityManager;
+
+import static org.junit.Assert.*;
+
 public class ClustersTest {
 
   private Clusters clusters;
@@ -270,4 +269,80 @@ public class ClustersTest {
     // TODO verify dump output?
   }
 
+  @Test
+  public void testDeleteCluster() throws Exception {
+    String c1 = "c1";
+    final String h1 = "h1";
+    final String h2 = "h2";
+
+    clusters.addCluster(c1);
+
+    Cluster cluster = clusters.getCluster(c1);
+    cluster.setDesiredStackVersion(new StackId("HDP-0.1"));
+
+    Config config = injector.getInstance(ConfigFactory.class).createNew(cluster, "t1", new HashMap<String, String>() {{
+      put("prop1", "val1");
+    }});
+    config.setVersionTag("1");
+    config.persist();
+
+    clusters.addHost(h1);
+    clusters.addHost(h2);
+
+    Host host1 = clusters.getHost(h1);
+    host1.setOsType("centos5");
+    Host host2 = clusters.getHost(h2);
+    host2.setOsType("centos5");
+    host1.persist();
+    host2.persist();
+
+    clusters.mapHostsToCluster(new HashSet<String>() {
+      {
+        addAll(Arrays.asList(h1, h2));
+      }
+    }, c1);
+
+
+    Service hdfs = cluster.addService("HDFS");
+    hdfs.persist();
+
+    assertNotNull(injector.getInstance(ClusterServiceDAO.class).findByClusterAndServiceNames(c1, "HDFS"));
+
+    ServiceComponent nameNode = hdfs.addServiceComponent("NAMENODE");
+    nameNode.persist();
+    ServiceComponent dataNode = hdfs.addServiceComponent("DATANODE");
+    dataNode.persist();
+
+    ServiceComponentHost nameNodeHost = nameNode.addServiceComponentHost(h1);
+    nameNodeHost.persist();
+
+    ServiceComponentHost dataNodeHost = dataNode.addServiceComponentHost(h2);
+    dataNodeHost.persist();
+
+    HostComponentStateEntityPK hkspk = new HostComponentStateEntityPK();
+    HostComponentDesiredStateEntityPK hkdspk = new HostComponentDesiredStateEntityPK();
+
+    hkspk.setClusterId(nameNodeHost.getClusterId());
+    hkspk.setHostName(nameNodeHost.getHostName());
+    hkspk.setServiceName(nameNodeHost.getServiceName());
+    hkspk.setComponentName(nameNodeHost.getServiceComponentName());
+
+    hkdspk.setClusterId(nameNodeHost.getClusterId());
+    hkdspk.setHostName(nameNodeHost.getHostName());
+    hkdspk.setServiceName(nameNodeHost.getServiceName());
+    hkdspk.setComponentName(nameNodeHost.getServiceComponentName());
+
+    assertNotNull(injector.getInstance(HostComponentStateDAO.class).findByPK(hkspk));
+    assertNotNull(injector.getInstance(HostComponentDesiredStateDAO.class).findByPK(hkdspk));
+    assertEquals(1, injector.getProvider(EntityManager.class).get().createQuery("SELECT config FROM ClusterConfigEntity config").getResultList().size());
+
+    clusters.deleteCluster(c1);
+
+    assertEquals(2, injector.getInstance(HostDAO.class).findAll().size());
+    assertNull(injector.getInstance(HostComponentStateDAO.class).findByPK(hkspk));
+    assertNull(injector.getInstance(HostComponentDesiredStateDAO.class).findByPK(hkdspk));
+    //configs are removed implicitly by cascade operation
+    assertEquals(0, injector.getProvider(EntityManager.class).get().createQuery("SELECT config FROM ClusterConfigEntity config").getResultList().size());
+
+  }
 }