Updated Branches: refs/heads/trunk abb8020e2 -> f5b581343
AMBARI-3202. Implement DELETE on host_components endpoint. (ncole) Project: http://git-wip-us.apache.org/repos/asf/incubator-ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ambari/commit/f5b58134 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ambari/tree/f5b58134 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ambari/diff/f5b58134 Branch: refs/heads/trunk Commit: f5b58134337fceae8c7cfc85cea446a97b28ed34 Parents: abb8020 Author: Nate Cole <[email protected]> Authored: Thu Sep 12 10:59:33 2013 -0400 Committer: Nate Cole <[email protected]> Committed: Thu Sep 12 15:29:54 2013 -0400 ---------------------------------------------------------------------- .../api/services/HostComponentService.java | 17 ++++ .../AmbariManagementControllerImpl.java | 31 +++++-- .../AmbariManagementControllerTest.java | 94 +++++++++++++++++++- 3 files changed, 134 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/f5b58134/ambari-server/src/main/java/org/apache/ambari/server/api/services/HostComponentService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/HostComponentService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/HostComponentService.java index c7f6b5e..c0f8297 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/HostComponentService.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/HostComponentService.java @@ -190,6 +190,23 @@ public class HostComponentService extends BaseService { return handleRequest(headers, null, ui, Request.Type.DELETE, createHostComponentResource(m_clusterName, m_hostName, hostComponentName)); } + + /** + * Handles DELETE /clusters/{clusterID}/hosts/{hostID}/host_components + * Deletes multiple host_component resources. + * + * @param headers http headers + * @param ui uri info + * + * @return host_component resource representation + */ + @DELETE + @Produces("text/plain") + public Response deleteHostComponents(@Context HttpHeaders headers, @Context UriInfo ui) { + + return handleRequest(headers, null, ui, Request.Type.DELETE, + createHostComponentResource(m_clusterName, m_hostName, null)); + } /** * Create a host_component resource instance. http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/f5b58134/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 e670272..58fbef4 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 @@ -92,6 +92,7 @@ import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStopEvent; import org.apache.ambari.server.state.svccomphost.ServiceComponentHostUpgradeEvent; import org.apache.ambari.server.utils.StageUtils; import org.apache.commons.lang.StringUtils; +import org.apache.http.client.utils.URIBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -101,8 +102,6 @@ import com.google.inject.Injector; import com.google.inject.Singleton; import com.google.inject.persist.Transactional; -import org.apache.http.client.utils.URIBuilder; - @Singleton public class AmbariManagementControllerImpl implements AmbariManagementController { @@ -3662,14 +3661,36 @@ public class AmbariManagementControllerImpl implements } } } - + @Override public RequestStatusResponse deleteHostComponents( Set<ServiceComponentHostRequest> requests) throws AmbariException { - Map<ServiceComponent, Set<ServiceComponentHost>> safeToRemoveSCHs = new HashMap<ServiceComponent, Set<ServiceComponentHost>>(); - + Set<ServiceComponentHostRequest> expanded = new HashSet<ServiceComponentHostRequest>(); + + // if any request are for the whole host, they need to be expanded for (ServiceComponentHostRequest request : requests) { + if (null == request.getComponentName()) { + if (null == request.getClusterName() || request.getClusterName().isEmpty() || + null == request.getHostname() || request.getHostname().isEmpty()) { + throw new IllegalArgumentException("Cluster name and hostname must be specified."); + } + Cluster cluster = clusters.getCluster(request.getClusterName()); + + for (ServiceComponentHost sch : cluster.getServiceComponentHosts(request.getHostname())) { + ServiceComponentHostRequest schr = new ServiceComponentHostRequest(request.getClusterName(), + sch.getServiceName(), sch.getServiceComponentName(), sch.getHostName(), null, null); + expanded.add(schr); + } + } + else { + expanded.add(request); + } + } + + Map<ServiceComponent, Set<ServiceComponentHost>> safeToRemoveSCHs = new HashMap<ServiceComponent, Set<ServiceComponentHost>>(); + + for (ServiceComponentHostRequest request : expanded) { validateServiceComponentHostRequest(request); http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/f5b58134/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 5ffa24d..4502b93 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 @@ -36,9 +36,6 @@ import java.util.Set; import javax.persistence.EntityManager; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.persist.PersistService; import junit.framework.Assert; import org.apache.ambari.server.AmbariException; @@ -88,6 +85,9 @@ import org.apache.ambari.server.state.State; import org.apache.ambari.server.state.svccomphost.ServiceComponentHostInstallEvent; import org.apache.ambari.server.state.svccomphost.ServiceComponentHostOpSucceededEvent; import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStartEvent; +import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStartedEvent; +import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStopEvent; +import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStoppedEvent; import org.apache.ambari.server.utils.StageUtils; import org.junit.After; import org.junit.Before; @@ -97,6 +97,10 @@ import org.junit.rules.ExpectedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; + public class AmbariManagementControllerTest { private static final Logger LOG = @@ -7133,4 +7137,88 @@ public class AmbariManagementControllerTest { Assert.assertTrue(e instanceof ObjectNotFoundException); } } + + @Test + public void testDeleteComponentsOnHost() throws Exception { + String clusterName = "foo1"; + + createCluster(clusterName); + + Cluster cluster = clusters.getCluster(clusterName); + cluster.setDesiredStackVersion(new StackId("HDP-0.1")); + + String serviceName = "HDFS"; + createService(clusterName, serviceName, null); + String componentName1 = "NAMENODE"; + String componentName2 = "DATANODE"; + String componentName3 = "HDFS_CLIENT"; + + Map<String, String> mapRequestProps = new HashMap<String, String>(); + mapRequestProps.put("context", "Called from a test"); + + createServiceComponent(clusterName, serviceName, componentName1, State.INIT); + createServiceComponent(clusterName, serviceName, componentName2, State.INIT); + createServiceComponent(clusterName, serviceName, componentName3, State.INIT); + + String host1 = "h1"; + clusters.addHost(host1); + clusters.getHost("h1").setOsType("centos5"); + clusters.getHost("h1").persist(); + + clusters.mapHostToCluster(host1, clusterName); + + createServiceComponentHost(clusterName, null, componentName1, host1, null); + createServiceComponentHost(clusterName, serviceName, componentName2, host1, null); + createServiceComponentHost(clusterName, serviceName, componentName3, host1, null); + + // Install + installService(clusterName, serviceName, false, false); + + // make them believe they are up + Map<String, ServiceComponentHost> hostComponents = cluster.getService(serviceName).getServiceComponent(componentName1).getServiceComponentHosts(); + for (Map.Entry<String, ServiceComponentHost> entry : hostComponents.entrySet()) { + ServiceComponentHost cHost = entry.getValue(); + cHost.handleEvent(new ServiceComponentHostInstallEvent(cHost.getServiceComponentName(), cHost.getHostName(), System.currentTimeMillis(), cluster.getDesiredStackVersion().getStackId())); + cHost.handleEvent(new ServiceComponentHostOpSucceededEvent(cHost.getServiceComponentName(), cHost.getHostName(), System.currentTimeMillis())); + } + hostComponents = cluster.getService(serviceName).getServiceComponent(componentName2).getServiceComponentHosts(); + for (Map.Entry<String, ServiceComponentHost> entry : hostComponents.entrySet()) { + ServiceComponentHost cHost = entry.getValue(); + cHost.handleEvent(new ServiceComponentHostInstallEvent(cHost.getServiceComponentName(), cHost.getHostName(), System.currentTimeMillis(), cluster.getDesiredStackVersion().getStackId())); + cHost.handleEvent(new ServiceComponentHostOpSucceededEvent(cHost.getServiceComponentName(), cHost.getHostName(), System.currentTimeMillis())); + } + + + ServiceComponentHost sch = cluster.getService(serviceName).getServiceComponent(componentName2).getServiceComponentHost(host1); + Assert.assertNotNull(sch); + + sch.handleEvent(new ServiceComponentHostStartEvent(sch.getServiceComponentName(), sch.getHostName(), System.currentTimeMillis(), new HashMap<String, String>())); + sch.handleEvent(new ServiceComponentHostStartedEvent (sch.getServiceComponentName(), sch.getHostName(), System.currentTimeMillis())); + + Set<ServiceComponentHostRequest> schRequests = new HashSet<ServiceComponentHostRequest>(); + schRequests.add(new ServiceComponentHostRequest(clusterName, null, null, host1, null, null)); + try { + controller.deleteHostComponents(schRequests); + fail("Expected exception while deleting all host components."); + } catch (AmbariException e) { + } + Assert.assertEquals(3, cluster.getServiceComponentHosts(host1).size()); + + sch.handleEvent(new ServiceComponentHostStopEvent(sch.getServiceComponentName(), sch.getHostName(), System.currentTimeMillis())); + sch.handleEvent(new ServiceComponentHostStoppedEvent (sch.getServiceComponentName(), sch.getHostName(), System.currentTimeMillis())); + + schRequests.clear(); + // maintenance HC, DN was already stopped + schRequests.add(new ServiceComponentHostRequest(clusterName, serviceName, componentName1, host1, null, "MAINTENANCE")); + controller.updateHostComponents(schRequests, new HashMap<String,String>(), false); + + // delete HC + schRequests.clear(); + schRequests.add(new ServiceComponentHostRequest(clusterName, null, null, host1, null, null)); + controller.deleteHostComponents(schRequests); + + Assert.assertEquals(0, cluster.getServiceComponentHosts(host1).size()); + } } + + \ No newline at end of file
