Return-Path: X-Original-To: apmail-ambari-commits-archive@www.apache.org Delivered-To: apmail-ambari-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C57FD10A80 for ; Mon, 27 Jan 2014 18:26:06 +0000 (UTC) Received: (qmail 12905 invoked by uid 500); 27 Jan 2014 18:26:06 -0000 Delivered-To: apmail-ambari-commits-archive@ambari.apache.org Received: (qmail 12863 invoked by uid 500); 27 Jan 2014 18:26:05 -0000 Mailing-List: contact commits-help@ambari.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ambari-dev@ambari.apache.org Delivered-To: mailing list commits@ambari.apache.org Received: (qmail 12855 invoked by uid 99); 27 Jan 2014 18:26:05 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Jan 2014 18:26:05 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 315A3906D12; Mon, 27 Jan 2014 18:26:05 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ncole@apache.org To: commits@ambari.apache.org Date: Mon, 27 Jan 2014 18:26:05 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/2] AMBARI-4419. Add Passive state for Host, Service, HostComponent (ncole) Updated Branches: refs/heads/trunk bc102b327 -> 95f0159ac http://git-wip-us.apache.org/repos/asf/ambari/blob/95f0159a/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 bc2039e..f36471f 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 @@ -18,14 +18,37 @@ package org.apache.ambari.server.controller; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import com.google.inject.AbstractModule; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.persist.PersistService; -import com.google.inject.persist.Transactional; +import static org.easymock.EasyMock.capture; +import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.lang.reflect.Type; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import javax.persistence.EntityManager; + import junit.framework.Assert; + import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.ClusterNotFoundException; import org.apache.ambari.server.DuplicateResourceException; @@ -47,7 +70,6 @@ import org.apache.ambari.server.actionmanager.TargetHostType; import org.apache.ambari.server.agent.ExecutionCommand; import org.apache.ambari.server.api.services.AmbariMetaInfo; import org.apache.ambari.server.configuration.Configuration; -import org.apache.ambari.server.controller.internal.ActionResourceProviderTest; import org.apache.ambari.server.controller.internal.ComponentResourceProviderTest; import org.apache.ambari.server.controller.internal.HostResourceProviderTest; import org.apache.ambari.server.controller.internal.ServiceResourceProviderTest; @@ -72,6 +94,7 @@ import org.apache.ambari.server.state.ConfigImpl; import org.apache.ambari.server.state.Host; import org.apache.ambari.server.state.HostComponentAdminState; import org.apache.ambari.server.state.HostState; +import org.apache.ambari.server.state.PassiveState; import org.apache.ambari.server.state.RepositoryInfo; import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.ServiceComponent; @@ -102,33 +125,13 @@ import org.junit.rules.ExpectedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.persistence.EntityManager; -import java.io.File; -import java.lang.reflect.Type; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; - -import static org.easymock.EasyMock.capture; -import static org.easymock.EasyMock.createNiceMock; -import static org.easymock.EasyMock.createStrictMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; +import com.google.inject.persist.Transactional; public class AmbariManagementControllerTest { @@ -146,7 +149,6 @@ public class AmbariManagementControllerTest { private static final int STACK_VERSIONS_CNT = 10; private static final int REPOS_CNT = 3; private static final int STACKS_CNT = 1; - private static final int STACK_SERVICES_CNT = 5 ; private static final int STACK_PROPERTIES_CNT = 81; private static final int STACK_COMPONENTS_CNT = 3; private static final int OS_CNT = 2; @@ -8867,6 +8869,260 @@ public class AmbariManagementControllerTest { } } + @Test + public void testPassiveStates() throws Exception { + String clusterName = "c1"; + createCluster(clusterName); + clusters.getCluster(clusterName) + .setDesiredStackVersion(new StackId("HDP-0.1")); + String serviceName = "HDFS"; + createService(clusterName, serviceName, null); + String componentName1 = "NAMENODE"; + String componentName2 = "DATANODE"; + String componentName3 = "HDFS_CLIENT"; + 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").setState(HostState.HEALTHY); + clusters.getHost("h1").persist(); + String host2 = "h2"; + clusters.addHost(host2); + clusters.getHost("h2").setOsType("centos5"); + clusters.getHost("h2").setState(HostState.HEALTHY); + clusters.getHost("h2").persist(); + + clusters.mapHostToCluster(host1, clusterName); + clusters.mapHostToCluster(host2, clusterName); + + createServiceComponentHost(clusterName, serviceName, componentName1, host1, null); + createServiceComponentHost(clusterName, serviceName, componentName2, host1, null); + createServiceComponentHost(clusterName, serviceName, componentName2, host2, null); + + Map requestProperties = new HashMap(); + requestProperties.put("context", "Called from a test"); + + + Cluster cluster = clusters.getCluster(clusterName); + Service service = cluster.getService(serviceName); + Map hosts = clusters.getHostsForCluster(clusterName); + + // test updating a service + ServiceRequest sr = new ServiceRequest(clusterName, serviceName, null); + sr.setPassiveState(PassiveState.PASSIVE.name()); + ServiceResourceProviderTest.updateServices(controller, Collections.singleton(sr), + requestProperties, false, false); + Assert.assertEquals(PassiveState.PASSIVE, service.getPassiveState()); + + // check the host components implied state vs desired state + for (ServiceComponent sc : service.getServiceComponents().values()) { + for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) { + Assert.assertEquals(PassiveState.IMPLIED, + controller.getEffectivePassiveState(cluster, service, sch)); + Assert.assertEquals(PassiveState.ACTIVE, sch.getPassiveState()); + } + } + + // reset + sr.setPassiveState(PassiveState.ACTIVE.name()); + ServiceResourceProviderTest.updateServices(controller, Collections.singleton(sr), + requestProperties, false, false); + Assert.assertEquals(PassiveState.ACTIVE, service.getPassiveState()); + + // check the host components implied state vs desired state + for (ServiceComponent sc : service.getServiceComponents().values()) { + for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) { + Assert.assertEquals(PassiveState.ACTIVE, + controller.getEffectivePassiveState(cluster, service, sch)); + Assert.assertEquals(PassiveState.ACTIVE, sch.getPassiveState()); + } + } + + // passivate a host + HostRequest hr = new HostRequest(host1, clusterName, requestProperties); + hr.setPassiveState(PassiveState.PASSIVE.name()); + HostResourceProviderTest.updateHosts(controller, Collections.singleton(hr)); + + Host host = hosts.get(host1); + Assert.assertEquals(PassiveState.PASSIVE, host.getPassiveState(cluster.getClusterId())); + + // check the host components implied state vs desired state, only for affected hosts + for (ServiceComponent sc : service.getServiceComponents().values()) { + for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) { + PassiveState implied = controller.getEffectivePassiveState(cluster, service, sch); + if (sch.getHostName().equals(host1)) { + Assert.assertEquals(PassiveState.IMPLIED, implied); + } else { + Assert.assertEquals(PassiveState.ACTIVE, implied); + } + Assert.assertEquals(PassiveState.ACTIVE, sch.getPassiveState()); + } + } + + // reset + hr.setPassiveState(PassiveState.ACTIVE.name()); + HostResourceProviderTest.updateHosts(controller, Collections.singleton(hr)); + + host = hosts.get(host1); + Assert.assertEquals(PassiveState.ACTIVE, host.getPassiveState(cluster.getClusterId())); + + // check the host components active state vs desired state + for (ServiceComponent sc : service.getServiceComponents().values()) { + for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) { + Assert.assertEquals(PassiveState.ACTIVE, + controller.getEffectivePassiveState(cluster, service, sch)); + Assert.assertEquals(PassiveState.ACTIVE, sch.getPassiveState()); + } + } + + // only do one SCH + ServiceComponentHost targetSch = service.getServiceComponent( + componentName2).getServiceComponentHosts().get(host2); + Assert.assertNotNull(targetSch); + targetSch.setPassiveState(PassiveState.PASSIVE); + + // check the host components active state vs desired state + Assert.assertEquals(PassiveState.PASSIVE, controller.getEffectivePassiveState(cluster, service, targetSch)); + + // update the service + service.setPassiveState(PassiveState.PASSIVE); + Assert.assertEquals(PassiveState.PASSIVE, controller.getEffectivePassiveState(cluster, service, targetSch)); + + // make SCH active + targetSch.setPassiveState(PassiveState.ACTIVE); + Assert.assertEquals(PassiveState.IMPLIED, controller.getEffectivePassiveState(cluster, service, targetSch)); + + // update the service + service.setPassiveState(PassiveState.ACTIVE); + Assert.assertEquals(PassiveState.ACTIVE, controller.getEffectivePassiveState(cluster, service, targetSch)); + + host = hosts.get(host2); + // update host + host.setPassiveState(cluster.getClusterId(), PassiveState.PASSIVE); + Assert.assertEquals(PassiveState.IMPLIED, controller.getEffectivePassiveState(cluster, service, targetSch)); + + targetSch.setPassiveState(PassiveState.PASSIVE); + Assert.assertEquals(PassiveState.PASSIVE, controller.getEffectivePassiveState(cluster, service, targetSch)); + + } + + @Test + public void testPassiveSkipServices() throws Exception { + String clusterName = "c1"; + createCluster(clusterName); + clusters.getCluster(clusterName) + .setDesiredStackVersion(new StackId("HDP-0.1")); + + String serviceName1 = "HDFS"; + String serviceName2 = "MAPREDUCE"; + createService(clusterName, serviceName1, null); + createService(clusterName, serviceName2, null); + + String componentName1_1 = "NAMENODE"; + String componentName1_2 = "DATANODE"; + String componentName1_3 = "HDFS_CLIENT"; + createServiceComponent(clusterName, serviceName1, componentName1_1, + State.INIT); + createServiceComponent(clusterName, serviceName1, componentName1_2, + State.INIT); + createServiceComponent(clusterName, serviceName1, componentName1_3, + State.INIT); + + String componentName2_1 = "JOBTRACKER"; + String componentName2_2 = "TASKTRACKER"; + createServiceComponent(clusterName, serviceName2, componentName2_1, + State.INIT); + createServiceComponent(clusterName, serviceName2, componentName2_2, + State.INIT); + + + String host1 = "h1"; + clusters.addHost(host1); + clusters.getHost("h1").setOsType("centos5"); + clusters.getHost("h1").setState(HostState.HEALTHY); + clusters.getHost("h1").persist(); + String host2 = "h2"; + clusters.addHost(host2); + clusters.getHost("h2").setOsType("centos5"); + clusters.getHost("h2").setState(HostState.HEALTHY); + clusters.getHost("h2").persist(); + + clusters.mapHostToCluster(host1, clusterName); + clusters.mapHostToCluster(host2, clusterName); + + createServiceComponentHost(clusterName, serviceName1, componentName1_1, host1, null); + createServiceComponentHost(clusterName, serviceName1, componentName1_2, host1, null); + createServiceComponentHost(clusterName, serviceName1, componentName1_2, host2, null); + + createServiceComponentHost(clusterName, serviceName2, componentName2_1, host1, null); + createServiceComponentHost(clusterName, serviceName2, componentName2_2, host2, null); + + installService(clusterName, serviceName1, false, false); + installService(clusterName, serviceName2, false, false); + + startService(clusterName, serviceName1, false, false); + startService(clusterName, serviceName2, false, false); + + Map requestProperties = new HashMap(); + requestProperties.put("context", "Called from a test"); + + Cluster cluster = clusters.getCluster(clusterName); + + for (Service service : cluster.getServices().values()) { + Assert.assertEquals(State.STARTED, service.getDesiredState()); + } + + Service service2 = cluster.getService(serviceName2); + service2.setPassiveState(PassiveState.PASSIVE); + + Set srs = new HashSet(); + srs.add(new ServiceRequest(clusterName, serviceName1, State.INSTALLED.name())); + srs.add(new ServiceRequest(clusterName, serviceName2, State.INSTALLED.name())); + RequestStatusResponse rsr = ServiceResourceProviderTest.updateServices(controller, srs, requestProperties, false, false); + + for (ShortTaskStatus sts : rsr.getTasks()) { + String role = sts.getRole(); + Assert.assertFalse(role.equals(componentName2_1)); + Assert.assertFalse(role.equals(componentName2_2)); + } + + for (Service service : cluster.getServices().values()) { + if (service.getName().equals(serviceName2)) + Assert.assertEquals(State.STARTED, service.getDesiredState()); + else + Assert.assertEquals(State.INSTALLED, service.getDesiredState()); + } + + service2.setPassiveState(PassiveState.ACTIVE); + ServiceResourceProviderTest.updateServices(controller, srs, requestProperties, false, false); + for (Service service : cluster.getServices().values()) { + Assert.assertEquals(State.INSTALLED, service.getDesiredState()); + } + + startService(clusterName, serviceName1, false, false); + startService(clusterName, serviceName2, false, false); + + // test host + Host h1 = clusters.getHost(host1); + h1.setPassiveState(cluster.getClusterId(), PassiveState.PASSIVE); + + srs = new HashSet(); + srs.add(new ServiceRequest(clusterName, serviceName1, State.INSTALLED.name())); + srs.add(new ServiceRequest(clusterName, serviceName2, State.INSTALLED.name())); + + rsr = ServiceResourceProviderTest.updateServices(controller, srs, requestProperties, false, false); + + for (ShortTaskStatus sts : rsr.getTasks()) { + Assert.assertFalse(sts.getHostName().equals(host1)); + } + + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/95f0159a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostResourceProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostResourceProviderTest.java index 9bf0c94..951c4ad 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostResourceProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostResourceProviderTest.java @@ -1142,4 +1142,11 @@ public class HostResourceProviderTest { HostResourceProvider provider = getHostProvider(controller); provider.deleteHosts(requests); } + + public static void updateHosts(AmbariManagementController controller, Set requests) + throws AmbariException { + HostResourceProvider provider = getHostProvider(controller); + provider.updateHosts(requests); + } + } http://git-wip-us.apache.org/repos/asf/ambari/blob/95f0159a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java index f9658fa..066f4c4 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java @@ -18,13 +18,14 @@ package org.apache.ambari.server.state; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.util.HashMap; import java.util.Map; -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; @@ -32,11 +33,15 @@ import org.apache.ambari.server.api.services.AmbariMetaInfo; import org.apache.ambari.server.controller.ServiceResponse; 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.orm.entities.ClusterServiceEntity; import org.junit.After; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; public class ServiceTest { @@ -279,4 +284,31 @@ public class ServiceTest { } } + @Test + public void testServicePassive() throws Exception { + String serviceName = "HDFS"; + Service s = serviceFactory.createNew(cluster, serviceName); + cluster.addService(s); + s.persist(); + + Service service = cluster.getService(serviceName); + Assert.assertNotNull(service); + + ClusterServiceDAO dao = injector.getInstance(ClusterServiceDAO.class); + ClusterServiceEntity entity = dao.findByClusterAndServiceNames(clusterName, serviceName); + Assert.assertNotNull(entity); + Assert.assertEquals(PassiveState.ACTIVE, entity.getServiceDesiredStateEntity().getPassiveState()); + Assert.assertEquals(PassiveState.ACTIVE, service.getPassiveState()); + + service.setPassiveState(PassiveState.PASSIVE); + Assert.assertEquals(PassiveState.PASSIVE, service.getPassiveState()); + + entity = dao.findByClusterAndServiceNames(clusterName, serviceName); + Assert.assertNotNull(entity); + Assert.assertEquals(PassiveState.PASSIVE, entity.getServiceDesiredStateEntity().getPassiveState()); + + + } + + } http://git-wip-us.apache.org/repos/asf/ambari/blob/95f0159a/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java index 272a448..50ef1ad 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java @@ -40,6 +40,7 @@ import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.orm.dao.HostDAO; import org.apache.ambari.server.orm.entities.HostEntity; +import org.apache.ambari.server.orm.entities.HostStateEntity; import org.apache.ambari.server.state.AgentVersion; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; @@ -50,6 +51,7 @@ import org.apache.ambari.server.state.Host; import org.apache.ambari.server.state.HostHealthStatus; import org.apache.ambari.server.state.HostHealthStatus.HealthStatus; import org.apache.ambari.server.state.HostState; +import org.apache.ambari.server.state.PassiveState; import org.apache.ambari.server.state.StackId; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -410,4 +412,35 @@ public class HostTest { Assert.assertEquals("Expect no mapping configs", 0, map.size()); } + + @Test + public void testHostPassive() throws Exception { + AmbariMetaInfo metaInfo = injector.getInstance(AmbariMetaInfo.class); + metaInfo.init(); + + clusters.addCluster("c1"); + Cluster c1 = clusters.getCluster("c1"); + Assert.assertEquals("c1", c1.getClusterName()); + Assert.assertEquals(1, c1.getClusterId()); + clusters.addHost("h1"); + Host host = clusters.getHost("h1"); + host.setIPv4("ipv4"); + host.setIPv6("ipv6"); + host.setOsType("centos5"); + host.persist(); + c1.setDesiredStackVersion(new StackId("HDP-0.1")); + clusters.mapHostToCluster("h1", "c1"); + + HostEntity entity = hostDAO.findByName("h1"); + HostStateEntity stateEntity = entity.getHostStateEntity(); + Assert.assertNull(stateEntity.getPassiveState()); + Assert.assertEquals(PassiveState.ACTIVE, host.getPassiveState(c1.getClusterId())); + + host.setPassiveState(c1.getClusterId(), PassiveState.PASSIVE); + + entity = hostDAO.findByName("h1"); + stateEntity = entity.getHostStateEntity(); + Assert.assertNotNull(stateEntity.getPassiveState()); + Assert.assertEquals(PassiveState.PASSIVE, host.getPassiveState(c1.getClusterId())); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/95f0159a/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java index da3e3e1..6046bb8 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java @@ -30,12 +30,16 @@ import org.apache.ambari.server.api.services.AmbariMetaInfo; import org.apache.ambari.server.controller.ServiceComponentHostResponse; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO; +import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity; +import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntityPK; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Config; import org.apache.ambari.server.state.ConfigFactory; import org.apache.ambari.server.state.Host; import org.apache.ambari.server.state.HostConfig; +import org.apache.ambari.server.state.PassiveState; import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.ServiceComponent; import org.apache.ambari.server.state.ServiceComponentFactory; @@ -796,6 +800,46 @@ public class ServiceComponentHostTest { cluster.addConfig(config); cluster.addDesiredConfig("user", config); } + + @Test + public void testPassive() throws Exception { + String stackVersion="HDP-2.0.6"; + String clusterName = "c2"; + String hostName = "h3"; + + clusters.addCluster(clusterName); + clusters.addHost(hostName); + clusters.getHost(hostName).setOsType("centos5"); + clusters.getHost(hostName).persist(); + clusters.getCluster(clusterName).setDesiredStackVersion( + new StackId(stackVersion)); + metaInfo.init(); + clusters.mapHostToCluster(hostName, clusterName); + + Cluster cluster = clusters.getCluster(clusterName); + + ServiceComponentHost sch1 = createNewServiceComponentHost(cluster, "HDFS", "NAMENODE", hostName); + ServiceComponentHost sch2 = createNewServiceComponentHost(cluster, "HDFS", "DATANODE", hostName); + ServiceComponentHost sch3 = createNewServiceComponentHost(cluster, "MAPREDUCE2", "HISTORYSERVER", hostName); + + HostComponentDesiredStateEntityPK pk = new HostComponentDesiredStateEntityPK(); + pk.setClusterId(Long.valueOf(cluster.getClusterId())); + pk.setComponentName(sch1.getServiceComponentName()); + pk.setServiceName(sch1.getServiceName()); + pk.setHostName(hostName); + + HostComponentDesiredStateDAO dao = injector.getInstance(HostComponentDesiredStateDAO.class); + HostComponentDesiredStateEntity entity = dao.findByPK(pk); + Assert.assertEquals(PassiveState.ACTIVE, entity.getPassiveState()); + Assert.assertEquals(PassiveState.ACTIVE, sch1.getPassiveState()); + + sch1.setPassiveState(PassiveState.PASSIVE); + Assert.assertEquals(PassiveState.PASSIVE, sch1.getPassiveState()); + + entity = dao.findByPK(pk); + Assert.assertEquals(PassiveState.PASSIVE, entity.getPassiveState()); + + }