瀏覽代碼

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)
  directories that we get from the server (only for hadoop and its eco system)
  and not all. (mahadev)
  and not all. (mahadev)
 
 
+ AMBARI-1250. Upgrade the posgres connector to 9.1.
+ (mahadev)
+
 AMBARI-1.2.0 branch:
 AMBARI-1.2.0 branch:
 
 
  INCOMPATIBLE CHANGES
  INCOMPATIBLE CHANGES

+ 1 - 1
ambari-server/pom.xml

@@ -531,7 +531,7 @@
     <dependency>
     <dependency>
       <groupId>postgresql</groupId>
       <groupId>postgresql</groupId>
       <artifactId>postgresql</artifactId>
       <artifactId>postgresql</artifactId>
-      <version>8.3-603.jdbc4</version>
+      <version>9.1-901.jdbc4</version>
     </dependency>
     </dependency>
   </dependencies>
   </dependencies>
   <!--<reporting>
   <!--<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.ServiceComponentHostStartEvent;
 import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStopEvent;
 import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStopEvent;
 import org.apache.ambari.server.utils.StageUtils;
 import org.apache.ambari.server.utils.StageUtils;
+import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 
 
@@ -2689,9 +2690,7 @@ public class AmbariManagementControllerImpl implements
   @Override
   @Override
   public synchronized void deleteCluster(ClusterRequest request)
   public synchronized void deleteCluster(ClusterRequest request)
       throws AmbariException {
       throws AmbariException {
-    throw new AmbariException("Delete cluster not supported");
 
 
-    /*
     if (request.getClusterName() == null
     if (request.getClusterName() == null
         || request.getClusterName().isEmpty()) {
         || request.getClusterName().isEmpty()) {
       // FIXME throw correct error
       // FIXME throw correct error
@@ -2705,13 +2704,22 @@ public class AmbariManagementControllerImpl implements
       // deleting whole cluster
       // deleting whole cluster
       clusters.deleteCluster(request.getClusterName());
       clusters.deleteCluster(request.getClusterName());
     }
     }
-    */
   }
   }
 
 
   @Override
   @Override
   public RequestStatusResponse deleteServices(Set<ServiceRequest> request)
   public RequestStatusResponse deleteServices(Set<ServiceRequest> request)
       throws AmbariException {
       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
   @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));
     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;
   private Collection<ClusterConfigEntity> configEntities;
-  @OneToMany(mappedBy = "clusterEntity")
+  @OneToMany(mappedBy = "clusterEntity", cascade = CascadeType.ALL)
   public Collection<ClusterConfigEntity> getClusterConfigEntities() {
   public Collection<ClusterConfigEntity> getClusterConfigEntities() {
     return configEntities;
     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;
   private ServiceDesiredStateEntity serviceDesiredStateEntity;
 
 
-  @OneToOne(mappedBy = "clusterServiceEntity")
+  @OneToOne(mappedBy = "clusterServiceEntity", cascade = CascadeType.ALL)
   public ServiceDesiredStateEntity getServiceDesiredStateEntity() {
   public ServiceDesiredStateEntity getServiceDesiredStateEntity() {
     return serviceDesiredStateEntity;
     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;
   private Collection<HostComponentConfigMappingEntity> configMappingEntities;
   @OneToMany(mappedBy = "hostComponentStateEntity", cascade = CascadeType.ALL)
   @OneToMany(mappedBy = "hostComponentStateEntity", cascade = CascadeType.ALL)
-  public Collection<HostComponentConfigMappingEntity> getHostComponentConfigMappingEntities() {
+   public Collection<HostComponentConfigMappingEntity> getHostComponentConfigMappingEntities() {
     return configMappingEntities;
     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;
   private ClusterServiceEntity clusterServiceEntity;
 
 
   @OneToOne
   @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() {
   public ClusterServiceEntity getClusterServiceEntity() {
     return clusterServiceEntity;
     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 void deleteService(String serviceName) throws AmbariException;
 
 
   public boolean canBeRemoved();
   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 String versionTag;
   private Map<String, String> properties;
   private Map<String, String> properties;
   private ClusterConfigEntity entity;
   private ClusterConfigEntity entity;
-  
+
   @Inject
   @Inject
   private ClusterDAO clusterDAO;
   private ClusterDAO clusterDAO;
   @Inject
   @Inject
@@ -111,7 +111,7 @@ public class ConfigImpl implements Config {
   
   
   @Transactional
   @Transactional
   @Override
   @Override
-  public void persist() {
+  public synchronized void persist() {
     
     
     ClusterEntity clusterEntity = clusterDAO.findById(cluster.getClusterId());
     ClusterEntity clusterEntity = clusterDAO.findById(cluster.getClusterId());
     
     
@@ -128,7 +128,7 @@ public class ConfigImpl implements Config {
     clusterEntity.getClusterConfigEntities().add(entity);
     clusterEntity.getClusterConfigEntities().add(entity);
     clusterDAO.merge(clusterEntity);
     clusterDAO.merge(clusterEntity);
     cluster.refresh();
     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 boolean canBeRemoved();
 
 
-  public void removeAllComponents() throws AmbariException;
+  public void deleteAllComponents() throws AmbariException;
 
 
   public void deleteServiceComponent(String componentName)
   public void deleteServiceComponent(String componentName)
       throws AmbariException;
       throws AmbariException;
 
 
   public boolean isClientOnlyService();
   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 boolean canBeRemoved();
 
 
-  public void removeAllServiceComponentHosts() throws AmbariException;
+  public void deleteAllServiceComponentHosts() throws AmbariException;
 
 
-  public void removeServiceComponentHosts(String hostname)
+  public void deleteServiceComponentHosts(String hostname)
       throws AmbariException;
       throws AmbariException;
 
 
   ServiceComponentHost addServiceComponentHost(
   ServiceComponentHost addServiceComponentHost(
       String hostName) throws AmbariException;
       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 void debugDump(StringBuilder sb);
 
 
   public boolean canBeRemoved();
   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
   @Override
-  public synchronized void removeAllServiceComponentHosts()
+  @Transactional
+  public synchronized void deleteAllServiceComponentHosts()
       throws AmbariException {
       throws AmbariException {
     LOG.info("Deleting all servicecomponenthosts for component"
     LOG.info("Deleting all servicecomponenthosts for component"
         + ", clusterName=" + getClusterName()
         + ", clusterName=" + getClusterName()
@@ -527,12 +528,16 @@ public class ServiceComponentImpl implements ServiceComponent {
             + ", hostname=" + sch.getHostName());
             + ", hostname=" + sch.getHostName());
       }
       }
     }
     }
+
+    for (ServiceComponentHost serviceComponentHost : hostComponents.values()) {
+      serviceComponentHost.delete();
+    }
+
     hostComponents.clear();
     hostComponents.clear();
-    // FIXME update DB
   }
   }
 
 
   @Override
   @Override
-  public synchronized void removeServiceComponentHosts(String hostname)
+  public synchronized void deleteServiceComponentHosts(String hostname)
       throws AmbariException {
       throws AmbariException {
     ServiceComponentHost sch = getServiceComponentHost(hostname);
     ServiceComponentHost sch = getServiceComponentHost(hostname);
     LOG.info("Deleting servicecomponenthost for cluster"
     LOG.info("Deleting servicecomponenthost for cluster"
@@ -547,16 +552,39 @@ public class ServiceComponentImpl implements ServiceComponent {
           + ", componentName=" + getName()
           + ", componentName=" + getName()
           + ", hostname=" + sch.getHostName());
           + ", hostname=" + sch.getHostName());
     }
     }
+    sch.delete();
     hostComponents.remove(hostname);
     hostComponents.remove(hostname);
-    // FIXME update DB
   }
   }
 
 
   @Override
   @Override
   public synchronized void deleteDesiredConfigs(Set<String> configTypes) {
   public synchronized void deleteDesiredConfigs(Set<String> configTypes) {
+    componentConfigMappingDAO.removeByType(configTypes);
     for (String configType : configTypes) {
     for (String configType : configTypes) {
       desiredConfigs.remove(configType);
       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);
     serviceDesiredStateDAO.create(serviceDesiredStateEntity);
     clusterEntity.getClusterServiceEntities().add(serviceEntity);
     clusterEntity.getClusterServiceEntities().add(serviceEntity);
     clusterDAO.merge(clusterEntity);
     clusterDAO.merge(clusterEntity);
-    serviceEntity = clusterServiceDAO.merge(serviceEntity);
-    serviceDesiredStateEntity = serviceDesiredStateDAO.merge(serviceDesiredStateEntity);
+//    serviceEntity =
+        clusterServiceDAO.merge(serviceEntity);
+//    serviceDesiredStateEntity =
+        serviceDesiredStateDAO.merge(serviceDesiredStateEntity);
   }
   }
 
 
   @Transactional
   @Transactional
@@ -463,7 +465,8 @@ public class ServiceImpl implements Service {
   }
   }
 
 
   @Override
   @Override
-  public synchronized void removeAllComponents() throws AmbariException {
+  @Transactional
+  public synchronized void deleteAllComponents() throws AmbariException {
     LOG.info("Deleting all components for service"
     LOG.info("Deleting all components for service"
         + ", clusterName=" + cluster.getClusterName()
         + ", clusterName=" + cluster.getClusterName()
         + ", serviceName=" + getName());
         + ", serviceName=" + getName());
@@ -477,11 +480,12 @@ public class ServiceImpl implements Service {
             + ", componentName=" + component.getName());
             + ", componentName=" + component.getName());
       }
       }
     }
     }
-    for (ServiceComponent component : components.values()) {
-      component.removeAllServiceComponentHosts();
+
+    for (ServiceComponent serviceComponent : components.values()) {
+      serviceComponent.delete();
     }
     }
+
     components.clear();
     components.clear();
-    // FIXME update DB
   }
   }
 
 
   @Override
   @Override
@@ -499,9 +503,9 @@ public class ServiceImpl implements Service {
           + ", serviceName=" + getName()
           + ", serviceName=" + getName()
           + ", componentName=" + componentName);
           + ", componentName=" + componentName);
     }
     }
-    component.removeAllServiceComponentHosts();
+
+    component.delete();
     components.remove(componentName);
     components.remove(componentName);
-    // FIXME update DB
   }
   }
 
 
   @Override
   @Override
@@ -509,4 +513,26 @@ public class ServiceImpl implements Service {
     return isClientOnlyService;
     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
   @Override
+  @Transactional
   public synchronized void deleteAllServices() throws AmbariException {
   public synchronized void deleteAllServices() throws AmbariException {
     loadServices();
     loadServices();
     LOG.info("Deleting all services for cluster"
     LOG.info("Deleting all services for cluster"
@@ -446,11 +447,12 @@ public class ClusterImpl implements Cluster {
             + ", serviceName=" + service.getName());
             + ", serviceName=" + service.getName());
       }
       }
     }
     }
+
     for (Service service : services.values()) {
     for (Service service : services.values()) {
-      service.removeAllComponents();
+      service.delete();
     }
     }
+
     services.clear();
     services.clear();
-    // FIXME update DB
   }
   }
 
 
   @Override
   @Override
@@ -467,9 +469,8 @@ public class ClusterImpl implements Cluster {
           + ", clusterName=" + getClusterName()
           + ", clusterName=" + getClusterName()
           + ", serviceName=" + service.getName());
           + ", serviceName=" + service.getName());
     }
     }
-    service.removeAllComponents();
+    service.delete();
     services.remove(serviceName);
     services.remove(serviceName);
-    // FIXME update DB
   }
   }
 
 
   @Override
   @Override
@@ -486,4 +487,17 @@ public class ClusterImpl implements Cluster {
     }
     }
     return safeToRemove;
     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
   @Override
-  @Transactional
   public synchronized void deleteCluster(String clusterName)
   public synchronized void deleteCluster(String clusterName)
       throws AmbariException {
       throws AmbariException {
     Cluster cluster = getCluster(clusterName);
     Cluster cluster = getCluster(clusterName);
@@ -368,9 +367,15 @@ public class ClustersImpl implements Clusters {
       throw new AmbariException("Could not delete cluster"
       throw new AmbariException("Could not delete cluster"
           + ", clusterName=" + clusterName);
           + ", 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);
     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) {
 //  public HostImpl(String hostname) {
 //    this.stateMachine = stateMachineFactory.make(this);
 //    this.stateMachine = stateMachineFactory.make(this);
 //    ReadWriteLock rwLock = new ReentrantReadWriteLock();
 //    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) {
   public void deleteDesiredConfigs(Set<String> configTypes) {
     try {
     try {
       writeLock.lock();
       writeLock.lock();
+      hostComponentDesiredConfigMappingDAO.removeByType(configTypes);
       for (String configType : configTypes) {
       for (String configType : configTypes) {
         desiredConfigs.remove(configType);
         desiredConfigs.remove(configType);
       }
       }
-      hostComponentDesiredConfigMappingDAO.removeByType(configTypes);
     } finally {
     } finally {
       writeLock.unlock();
       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;
 package org.apache.ambari.server.state;
 
 
-import static org.junit.Assert.fail;
-
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map;
 
 
@@ -38,6 +36,8 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.Test;
 
 
+import static org.junit.Assert.*;
+
 public class ServiceTest {
 public class ServiceTest {
 
 
   private Clusters clusters;
   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;
 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 static org.junit.Assert.fail;
 
 
 import java.util.ArrayList;
 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.controller.ClusterResponse;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 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.AgentVersion;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 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.Injector;
 import com.google.inject.persist.PersistService;
 import com.google.inject.persist.PersistService;
 
 
+import javax.persistence.EntityManager;
+
 public class ClusterTest {
 public class ClusterTest {
 
 
   private Clusters clusters;
   private Clusters clusters;
@@ -290,4 +295,24 @@ public class ClusterTest {
     c1.debugDump(sb);
     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;
 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.Guice;
 import com.google.inject.Inject;
 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.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 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.After;
 import org.junit.Before;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.Test;
 
 
+import javax.persistence.EntityManager;
+
+import static org.junit.Assert.*;
+
 public class ClustersTest {
 public class ClustersTest {
 
 
   private Clusters clusters;
   private Clusters clusters;
@@ -270,4 +269,80 @@ public class ClustersTest {
     // TODO verify dump output?
     // 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());
+
+  }
 }
 }