Updated Branches:
  refs/heads/trunk 45a53ede1 -> 6fd7a3281

AMBARI-3118. MAINTENANCE should not be the only state at which a service 
component hosts can be deleted.


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

Branch: refs/heads/trunk
Commit: 6fd7a32815fc7bc87277b6836ebf97c4896657f3
Parents: 45a53ed
Author: Sumit Mohanty <[email protected]>
Authored: Thu Sep 5 16:53:39 2013 -0700
Committer: Sumit Mohanty <[email protected]>
Committed: Thu Sep 5 16:53:39 2013 -0700

----------------------------------------------------------------------
 .../AmbariManagementControllerImpl.java         |  9 +-
 .../org/apache/ambari/server/state/State.java   | 18 ++++
 .../svccomphost/ServiceComponentHostImpl.java   |  3 +-
 .../AmbariManagementControllerTest.java         | 87 +++++++++++++++++++-
 4 files changed, 110 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/6fd7a328/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
index 49d353f..019a400 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
@@ -3687,12 +3687,13 @@ public class AmbariManagementControllerImpl implements
             + ", request=" + request);
       }
 
-      // Only allow removing master/slave components in MAINTENANCE state 
without stages generation.
+      // Only allow removing master/slave components in 
MAINTENANCE/UNKNOWN/INSTALL_FAILED/INIT state without stages
+      // generation.
       // Clients may be removed without a state check.
       if (!component.isClientComponent() &&
-          componentHost.getState() != State.MAINTENANCE) {
-        throw new AmbariException("To remove master or slave components they 
must be in a " +
-            "MAINTENANCE state. Current=" + componentHost.getState() + ".");
+          !componentHost.getState().isHardDeletableState()) {
+        throw new AmbariException("To remove master or slave components they 
must be in " +
+            "MAINTENANCE/INIT/INSTALL_FAILED/UNKNOWN state. Current=" + 
componentHost.getState() + ".");
       }
 
       if (!safeToRemoveSCHs.containsKey(component)) {

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/6fd7a328/ambari-server/src/main/java/org/apache/ambari/server/state/State.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/state/State.java 
b/ambari-server/src/main/java/org/apache/ambari/server/state/State.java
index 034e7e8..b5099b8 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/State.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/State.java
@@ -150,4 +150,22 @@ public enum State {
         return false;
     }
   }
+
+  /**
+   * Indicates whether or not the resource with this state can be hard-deleted.
+   *
+   * @return true if this is a hard-deletable state
+   */
+  public boolean isHardDeletableState() {
+    switch (State.values()[this.state]) {
+      case INIT:
+      case INSTALLING:
+      case INSTALL_FAILED:
+      case UNKNOWN:
+      case MAINTENANCE:
+        return true;
+      default:
+        return false;
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/6fd7a328/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
index a6d351a..1e49808 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
@@ -1479,8 +1479,7 @@ public class ServiceComponentHostImpl implements 
ServiceComponentHost {
       readLock.lock();
       try {
 
-        return (getDesiredState().isRemovableState() &&
-            getState().isRemovableState());
+        return (getState().isRemovableState());
 
       } finally {
         readLock.unlock();

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/6fd7a328/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
index 7a63a85..84bc6ee 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
@@ -6852,7 +6852,92 @@ public class AmbariManagementControllerTest {
     assertEquals(original, repo.getBaseUrl());
     assertEquals(original, repo.getDefaultBaseUrl());
   }
-  
+
+  @Test
+  public void testDeleteHostComponentInVariousStates() throws Exception {
+    String clusterName = "foo1";
+    createCluster(clusterName);
+    clusters.getCluster(clusterName)
+        .setDesiredStackVersion(new StackId("HDP-1.3.1"));
+    String serviceName = "HDFS";
+    String mapred = "MAPREDUCE";
+    createService(clusterName, serviceName, null);
+    createService(clusterName, mapred, null);
+    String componentName1 = "NAMENODE";
+    String componentName2 = "DATANODE";
+    String componentName3 = "HDFS_CLIENT";
+    String componentName4 = "JOBTRACKER";
+    String componentName5 = "TASKTRACKER";
+    String componentName6 = "MAPREDUCE_CLIENT";
+
+    createServiceComponent(clusterName, serviceName, componentName1, 
State.INIT);
+    createServiceComponent(clusterName, serviceName, componentName2, 
State.INIT);
+    createServiceComponent(clusterName, serviceName, componentName3, 
State.INIT);
+    createServiceComponent(clusterName, mapred, componentName4, State.INIT);
+    createServiceComponent(clusterName, mapred, componentName5, State.INIT);
+    createServiceComponent(clusterName, mapred, componentName6, State.INIT);
+
+    String host1 = "h1";
+    clusters.addHost(host1);
+    clusters.getHost("h1").setOsType("centos5");
+    clusters.getHost("h1").setState(HostState.HEALTHY);
+    clusters.getHost("h1").persist();
+
+    clusters.mapHostToCluster(host1, clusterName);
+
+    createServiceComponentHost(clusterName, serviceName, componentName1, 
host1, null);
+    createServiceComponentHost(clusterName, serviceName, componentName2, 
host1, null);
+    createServiceComponentHost(clusterName, serviceName, componentName3, 
host1, null);
+    createServiceComponentHost(clusterName, mapred, componentName4, host1, 
null);
+    createServiceComponentHost(clusterName, mapred, componentName5, host1, 
null);
+    createServiceComponentHost(clusterName, mapred, componentName6, host1, 
null);
+
+    // Install
+    installService(clusterName, serviceName, false, false);
+    installService(clusterName, mapred, false, false);
+
+    Cluster cluster = clusters.getCluster(clusterName);
+    Service s1 = cluster.getService(serviceName);
+    Service s2 = cluster.getService(mapred);
+    ServiceComponent sc1 = s1.getServiceComponent(componentName1);
+    
sc1.getServiceComponentHosts().values().iterator().next().setState(State.INSTALLED);
+
+    Set<ServiceComponentHostRequest> schRequests = new 
HashSet<ServiceComponentHostRequest>();
+    // delete HC
+    schRequests.clear();
+    schRequests.add(new ServiceComponentHostRequest(clusterName, serviceName, 
componentName1, host1, null, null));
+    try {
+      controller.deleteHostComponents(schRequests);
+      Assert.fail("Expect failure while deleting.");
+    } catch (Exception ex) {
+      Assert.assertTrue(ex.getMessage().contains(
+          "remove master or slave components they must be in 
MAINTENANCE/INIT/INSTALL_FAILED/UNKNOWN"));
+    }
+
+    
sc1.getServiceComponentHosts().values().iterator().next().setDesiredState(State.STARTED);
+    
sc1.getServiceComponentHosts().values().iterator().next().setState(State.UNKNOWN);
+    ServiceComponent sc2 = s1.getServiceComponent(componentName2);
+    
sc2.getServiceComponentHosts().values().iterator().next().setState(State.INIT);
+    ServiceComponent sc3 = s1.getServiceComponent(componentName3);
+    
sc3.getServiceComponentHosts().values().iterator().next().setState(State.INSTALL_FAILED);
+    ServiceComponent sc4 = s2.getServiceComponent(componentName4);
+    
sc4.getServiceComponentHosts().values().iterator().next().setDesiredState(State.INSTALLED);
+    
sc4.getServiceComponentHosts().values().iterator().next().setState(State.MAINTENANCE);
+    ServiceComponent sc5 = s2.getServiceComponent(componentName5);
+    
sc5.getServiceComponentHosts().values().iterator().next().setState(State.INSTALL_FAILED);
+    ServiceComponent sc6 = s2.getServiceComponent(componentName6);
+    
sc6.getServiceComponentHosts().values().iterator().next().setState(State.INIT);
+
+    schRequests.clear();
+    schRequests.add(new ServiceComponentHostRequest(clusterName, serviceName, 
componentName1, host1, null, null));
+    schRequests.add(new ServiceComponentHostRequest(clusterName, serviceName, 
componentName2, host1, null, null));
+    schRequests.add(new ServiceComponentHostRequest(clusterName, serviceName, 
componentName3, host1, null, null));
+    schRequests.add(new ServiceComponentHostRequest(clusterName, mapred, 
componentName4, host1, null, null));
+    schRequests.add(new ServiceComponentHostRequest(clusterName, mapred, 
componentName5, host1, null, null));
+    schRequests.add(new ServiceComponentHostRequest(clusterName, mapred, 
componentName6, host1, null, null));
+    controller.deleteHostComponents(schRequests);
+  }
+
   @Test
   public void testDeleteHost() throws Exception {
     String clusterName = "foo1";

Reply via email to