incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tbeerbo...@apache.org
Subject git commit: AMBARI-3677 - Service state should accurately reflect state of host-components
Date Tue, 05 Nov 2013 23:01:05 GMT
Updated Branches:
  refs/heads/trunk f2c9e1a9a -> 49f10119a


AMBARI-3677 - Service state should accurately reflect state of host-components


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

Branch: refs/heads/trunk
Commit: 49f10119a1d9d5133bf3393873380dc7e4d31a35
Parents: f2c9e1a
Author: tbeerbower <tbeerbower@hortonworks.com>
Authored: Tue Nov 5 16:17:19 2013 -0500
Committer: tbeerbower <tbeerbower@hortonworks.com>
Committed: Tue Nov 5 18:00:46 2013 -0500

----------------------------------------------------------------------
 .../internal/ServiceResourceProvider.java       | 237 ++++++++++-
 .../internal/ServiceResourceProviderTest.java   | 388 +++++++++++++++++++
 2 files changed, 624 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/49f10119/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
index e0b9989..30928cf 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
@@ -26,12 +26,15 @@ import org.apache.ambari.server.ServiceNotFoundException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.RequestStatusResponse;
+import org.apache.ambari.server.controller.ServiceComponentHostRequest;
+import org.apache.ambari.server.controller.ServiceComponentHostResponse;
 import org.apache.ambari.server.controller.ServiceRequest;
 import org.apache.ambari.server.controller.ServiceResponse;
 import org.apache.ambari.server.controller.spi.*;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.ComponentInfo;
 import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponent;
@@ -44,6 +47,7 @@ import org.apache.commons.lang.StringUtils;
 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.Iterator;
@@ -77,6 +81,16 @@ class ServiceResourceProvider extends AbstractControllerResourceProvider
{
           SERVICE_CLUSTER_NAME_PROPERTY_ID,
           SERVICE_SERVICE_NAME_PROPERTY_ID}));
 
+  // Service state calculation
+  private static final Map<String, ServiceState> serviceStateMap = new HashMap<String,
ServiceState>();
+  static {
+    serviceStateMap.put("HDFS", new HDFSServiceState());
+    serviceStateMap.put("HBASE", new HBaseServiceState());
+  }
+
+  private static final ServiceState DEFAULT_SERVICE_STATE = new DefaultServiceState();
+
+
   // ----- Constructors ----------------------------------------------------
 
   /**
@@ -146,7 +160,8 @@ class ServiceResourceProvider extends AbstractControllerResourceProvider
{
       setResourceProperty(resource, SERVICE_DESIRED_CONFIGS_PROPERTY_ID,
           response.getConfigVersions(), requestedIds);
       setResourceProperty(resource, SERVICE_SERVICE_STATE_PROPERTY_ID,
-          response.getDesiredState(), requestedIds);
+          calculateServiceState(response.getClusterName(), response.getServiceName()),
+          requestedIds);
       resources.add(resource);
     }
     return resources;
@@ -759,4 +774,224 @@ class ServiceResourceProvider extends AbstractControllerResourceProvider
{
     }
     return null;
   }
+
+  // Get the State of a host component
+  private static State getHostComponentState(ServiceComponentHostResponse hostComponent)
{
+    return State.valueOf(hostComponent.getLiveState());
+  }
+
+  // calculate the service state, accounting for the state of the host components
+  private String calculateServiceState(String clusterName, String serviceName) {
+
+    ServiceState serviceState = serviceStateMap.get(serviceName);
+    if (serviceState == null) {
+      serviceState = DEFAULT_SERVICE_STATE;
+    }
+    State state = serviceState.getState(getManagementController(), clusterName, serviceName);
+
+    return state.toString();
+  }
+
+
+  // ----- inner class ServiceState ------------------------------------------
+
+  /**
+   * Interface to allow for different state calculations for different services.
+   * TODO : see if this functionality can be moved to service definitions.
+   */
+  protected interface ServiceState {
+    public State getState(AmbariManagementController controller, String clusterName, String
serviceName);
+  }
+
+  /**
+   * Default calculator of service state.
+   */
+  protected static class DefaultServiceState implements ServiceState {
+
+    @Override
+    public State getState(AmbariManagementController controller, String clusterName, String
serviceName) {
+      AmbariMetaInfo ambariMetaInfo = controller.getAmbariMetaInfo();
+      Clusters       clusters       = controller.getClusters();
+
+      if (clusterName != null && clusterName.length() > 0) {
+        try {
+          Cluster cluster = clusters.getCluster(clusterName);
+          if (cluster != null) {
+            StackId stackId = cluster.getDesiredStackVersion();
+
+            ServiceComponentHostRequest request = new ServiceComponentHostRequest(clusterName,
+                serviceName, null, null, null, null);
+
+            Set<ServiceComponentHostResponse> hostComponentResponses =
+                controller.getHostComponents(Collections.singleton(request));
+
+            for (ServiceComponentHostResponse hostComponentResponse : hostComponentResponses
) {
+              ComponentInfo componentInfo = ambariMetaInfo.getComponentCategory(stackId.getStackName(),
+                  stackId.getStackVersion(), hostComponentResponse.getServiceName(),
+                  hostComponentResponse.getComponentName());
+
+              if (componentInfo != null) {
+                if (componentInfo.isMaster()) {
+                  State state = getHostComponentState(hostComponentResponse);
+                  switch (state) {
+                    case STARTED:
+                    case MAINTENANCE:
+                      break;
+                    default:
+                      return state;
+                  }
+                }
+              }
+            }
+            return State.STARTED;
+          }
+        } catch (AmbariException e) {
+          LOG.error("Can't determine service state.", e);
+        }
+      }
+      return State.UNKNOWN;
+    }
+  }
+
+  /**
+   * Calculator of HDFS service state.
+   */
+  protected static class HDFSServiceState implements ServiceState {
+
+    @Override
+    public State getState(AmbariManagementController controller,String clusterName, String
serviceName) {
+      AmbariMetaInfo ambariMetaInfo = controller.getAmbariMetaInfo();
+      Clusters       clusters       = controller.getClusters();
+
+      if (clusterName != null && clusterName.length() > 0) {
+        try {
+          Cluster cluster = clusters.getCluster(clusterName);
+          if (cluster != null) {
+            StackId stackId = cluster.getDesiredStackVersion();
+
+            ServiceComponentHostRequest request = new ServiceComponentHostRequest(clusterName,
+                serviceName, "NAMENODE", null, null, null);
+
+            Set<ServiceComponentHostResponse> hostComponentResponses =
+                controller.getHostComponents(Collections.singleton(request));
+
+            int     nameNodeCount       = 0;
+            int     nameNodeActiveCount = 0;
+            boolean hasSecondary        = false;
+            boolean hasJournal          = false;
+            State   nonStartedState     = null;
+
+            for (ServiceComponentHostResponse hostComponentResponse : hostComponentResponses
) {
+              ComponentInfo componentInfo = ambariMetaInfo.getComponentCategory(stackId.getStackName(),
+                  stackId.getStackVersion(), hostComponentResponse.getServiceName(),
+                  hostComponentResponse.getComponentName());
+
+              if (componentInfo != null) {
+                if (componentInfo.isMaster()) {
+
+                  String componentName = hostComponentResponse.getComponentName();
+                  boolean isNameNode = false;
+
+                  if (componentName.equals("NAMENODE")) {
+                    ++nameNodeCount;
+                    isNameNode = true;
+                  } else if (componentName.equals("SECONDARY_NAMENODE")) {
+                    hasSecondary = true;
+                  } else if (componentName.equals("JOURNALNODE")) {
+                    hasJournal = true;
+                  }
+
+                  State state = getHostComponentState(hostComponentResponse);
+
+                  switch (state) {
+                    case STARTED:
+                    case MAINTENANCE:
+                      if (isNameNode) {
+                        ++nameNodeActiveCount;
+                      }
+                      break;
+                    default:
+                      nonStartedState = state;
+                  }
+                }
+              }
+            }
+
+            if ( nonStartedState == null ||  // all started
+                ((nameNodeCount > 0 && !hasSecondary || hasJournal) &&
+                    nameNodeActiveCount > 0)) {  // at least one active namenode
+              return State.STARTED;
+            }
+            return nonStartedState;
+          }
+        } catch (AmbariException e) {
+          LOG.error("Can't determine service state.", e);
+        }
+      }
+      return State.UNKNOWN;
+    }
+  }
+
+  /**
+   * Calculator of HBase service state.
+   */
+  protected static class HBaseServiceState implements ServiceState {
+
+    @Override
+    public State getState(AmbariManagementController controller,String clusterName, String
serviceName) {
+      AmbariMetaInfo ambariMetaInfo = controller.getAmbariMetaInfo();
+      Clusters       clusters       = controller.getClusters();
+
+      if (clusterName != null && clusterName.length() > 0) {
+        try {
+          Cluster cluster = clusters.getCluster(clusterName);
+          if (cluster != null) {
+            StackId stackId = cluster.getDesiredStackVersion();
+
+            ServiceComponentHostRequest request = new ServiceComponentHostRequest(clusterName,
+                serviceName, "NAMENODE", null, null, null);
+
+            Set<ServiceComponentHostResponse> hostComponentResponses =
+                controller.getHostComponents(Collections.singleton(request));
+
+            int     hBaseMasterActiveCount = 0;
+            State   nonStartedState        = null;
+
+            for (ServiceComponentHostResponse hostComponentResponse : hostComponentResponses
) {
+              ComponentInfo componentInfo = ambariMetaInfo.getComponentCategory(stackId.getStackName(),
+                  stackId.getStackVersion(), hostComponentResponse.getServiceName(),
+                  hostComponentResponse.getComponentName());
+
+              if (componentInfo != null) {
+                if (componentInfo.isMaster()) {
+
+                  State state = getHostComponentState(hostComponentResponse);
+
+                  switch (state) {
+                    case STARTED:
+                    case MAINTENANCE:
+                      String componentName = hostComponentResponse.getComponentName();
+                      if (componentName.equals("HBASE_MASTER")) {
+                        ++hBaseMasterActiveCount;
+                      }
+                      break;
+                    default:
+                      nonStartedState = state;
+                  }
+                }
+              }
+            }
+
+            if ( nonStartedState == null ||     // all started
+                hBaseMasterActiveCount > 0) {   // at least one active hbase_master
+              return State.STARTED;
+            }
+            return nonStartedState;
+          }
+        } catch (AmbariException e) {
+          LOG.error("Can't determine service state.", e);
+        }
+      }
+      return State.UNKNOWN;    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/49f10119/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
index b7bcbeb..cc134ab 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
@@ -22,6 +22,8 @@ import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.RequestStatusResponse;
+import org.apache.ambari.server.controller.ServiceComponentHostRequest;
+import org.apache.ambari.server.controller.ServiceComponentHostResponse;
 import org.apache.ambari.server.controller.ServiceRequest;
 import org.apache.ambari.server.controller.ServiceResponse;
 import org.apache.ambari.server.controller.spi.Predicate;
@@ -32,6 +34,7 @@ import org.apache.ambari.server.controller.utilities.PredicateBuilder;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.ComponentInfo;
 import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentHost;
@@ -147,6 +150,8 @@ public class ServiceResourceProviderTest {
     expect(managementController.getClusters()).andReturn(clusters).anyTimes();
     expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
     expect(managementController.getServiceFactory()).andReturn(serviceFactory).anyTimes();
+    expect(managementController.getHostComponents((Set<ServiceComponentHostRequest>)
anyObject())).
+        andReturn(Collections.<ServiceComponentHostResponse>emptySet()).anyTimes();
 
     expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes();
 
@@ -321,6 +326,11 @@ public class ServiceResourceProviderTest {
     mapRequestProps.put("context", "Called from a test");
 
     // set expectations
+    expect(managementController1.getHostComponents((Set<ServiceComponentHostRequest>)
anyObject())).
+        andReturn(Collections.<ServiceComponentHostResponse>emptySet()).anyTimes();
+    expect(managementController2.getHostComponents((Set<ServiceComponentHostRequest>)
anyObject())).
+        andReturn(Collections.<ServiceComponentHostResponse>emptySet()).anyTimes();
+
     expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes();
 
     expect(managementController1.getClusters()).andReturn(clusters).anyTimes();
@@ -473,6 +483,384 @@ public class ServiceResourceProviderTest {
     Assert.assertTrue(unsupported.isEmpty());
   }
 
+  @Test
+  public void testDefaultServiceState_STARTED() throws Exception{
+    AmbariManagementController managementController = createMock(AmbariManagementController.class);
+    Clusters clusters = createNiceMock(Clusters.class);
+    Cluster cluster = createNiceMock(Cluster.class);
+    AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
+    StackId stackId = createNiceMock(StackId.class);
+    ComponentInfo componentInfo = createNiceMock(ComponentInfo.class);
+
+    ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"JOBTRACKER", "Host100", null, null, "STARTED", "", null, null);
+    ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"MAPREDUCE_CLIENT", "Host100", null, null, "STARTED", "", null, null);
+    ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"TASKTRACKER", "Host100", null, null, "STARTED", "", null, null);
+
+    Set<ServiceComponentHostResponse> responses = new LinkedHashSet<ServiceComponentHostResponse>();
+    responses.add(shr1);
+    responses.add(shr2);
+    responses.add(shr3);
+
+    // set expectations
+    expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
+    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getCluster("C1")).andReturn(cluster).anyTimes();
+    expect(managementController.getHostComponents((Set<ServiceComponentHostRequest>)
anyObject())).andReturn(responses).anyTimes();
+    expect(cluster.getDesiredStackVersion()).andReturn(stackId);
+
+    expect(stackId.getStackName()).andReturn("S1").anyTimes();
+    expect(stackId.getStackVersion()).andReturn("V1").anyTimes();
+
+
+    expect(ambariMetaInfo.getComponentCategory((String) anyObject(), (String) anyObject(),
+        (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes();
+
+    expect(componentInfo.isMaster()).andReturn(true);
+    expect(componentInfo.isMaster()).andReturn(false);
+    expect(componentInfo.isMaster()).andReturn(false);
+
+    // replay
+    replay(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+
+    ServiceResourceProvider.ServiceState serviceState = new ServiceResourceProvider.DefaultServiceState();
+
+    State state = serviceState.getState(managementController, "C1", "MAPREDUCE");
+    Assert.assertEquals(State.STARTED, state);
+
+    // verify
+    verify(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+  }
+
+  @Test
+  public void testDefaultServiceState_UNKNOWN() throws Exception{
+    AmbariManagementController managementController = createMock(AmbariManagementController.class);
+    Clusters clusters = createNiceMock(Clusters.class);
+    Cluster cluster = createNiceMock(Cluster.class);
+    AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
+    StackId stackId = createNiceMock(StackId.class);
+    ComponentInfo componentInfo = createNiceMock(ComponentInfo.class);
+
+    ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"JOBTRACKER", "Host100", null, null, "UNKNOWN", "", null, null);
+    ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"MAPREDUCE_CLIENT", "Host100", null, null, "STARTED", "", null, null);
+    ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"TASKTRACKER", "Host100", null, null, "STARTED", "", null, null);
+
+    Set<ServiceComponentHostResponse> responses = new LinkedHashSet<ServiceComponentHostResponse>();
+    responses.add(shr1);
+    responses.add(shr2);
+    responses.add(shr3);
+
+    // set expectations
+    expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
+    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getCluster("C1")).andReturn(cluster).anyTimes();
+    expect(managementController.getHostComponents((Set<ServiceComponentHostRequest>)
anyObject())).andReturn(responses).anyTimes();
+    expect(cluster.getDesiredStackVersion()).andReturn(stackId);
+
+    expect(stackId.getStackName()).andReturn("S1").anyTimes();
+    expect(stackId.getStackVersion()).andReturn("V1").anyTimes();
+
+
+    expect(ambariMetaInfo.getComponentCategory((String) anyObject(), (String) anyObject(),
+        (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes();
+
+    expect(componentInfo.isMaster()).andReturn(true);
+
+    // replay
+    replay(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+
+    ServiceResourceProvider.ServiceState serviceState = new ServiceResourceProvider.DefaultServiceState();
+
+    State state = serviceState.getState(managementController, "C1", "MAPREDUCE");
+    Assert.assertEquals(State.UNKNOWN, state);
+
+    // verify
+    verify(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+  }
+
+  @Test
+  public void testDefaultServiceState_STARTING() throws Exception{
+    AmbariManagementController managementController = createMock(AmbariManagementController.class);
+    Clusters clusters = createNiceMock(Clusters.class);
+    Cluster cluster = createNiceMock(Cluster.class);
+    AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
+    StackId stackId = createNiceMock(StackId.class);
+    ComponentInfo componentInfo = createNiceMock(ComponentInfo.class);
+
+    ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"JOBTRACKER", "Host100", null, null, "STARTING", "", null, null);
+    ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"MAPREDUCE_CLIENT", "Host100", null, null, "STARTED", "", null, null);
+    ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"TASKTRACKER", "Host100", null, null, "STARTED", "", null, null);
+
+    Set<ServiceComponentHostResponse> responses = new LinkedHashSet<ServiceComponentHostResponse>();
+    responses.add(shr1);
+    responses.add(shr2);
+    responses.add(shr3);
+
+    // set expectations
+    expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
+    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getCluster("C1")).andReturn(cluster).anyTimes();
+    expect(managementController.getHostComponents((Set<ServiceComponentHostRequest>)
anyObject())).andReturn(responses).anyTimes();
+    expect(cluster.getDesiredStackVersion()).andReturn(stackId);
+
+    expect(stackId.getStackName()).andReturn("S1").anyTimes();
+    expect(stackId.getStackVersion()).andReturn("V1").anyTimes();
+
+
+    expect(ambariMetaInfo.getComponentCategory((String) anyObject(), (String) anyObject(),
+        (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes();
+
+    expect(componentInfo.isMaster()).andReturn(true);
+
+    // replay
+    replay(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+
+    ServiceResourceProvider.ServiceState serviceState = new ServiceResourceProvider.DefaultServiceState();
+
+    State state = serviceState.getState(managementController, "C1", "MAPREDUCE");
+    Assert.assertEquals(State.STARTING, state);
+
+    // verify
+    verify(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+  }
+
+  @Test
+  public void testDefaultServiceState_STOPPED() throws Exception{
+    AmbariManagementController managementController = createMock(AmbariManagementController.class);
+    Clusters clusters = createNiceMock(Clusters.class);
+    Cluster cluster = createNiceMock(Cluster.class);
+    AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
+    StackId stackId = createNiceMock(StackId.class);
+    ComponentInfo componentInfo = createNiceMock(ComponentInfo.class);
+
+    ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"JOBTRACKER", "Host100", null, null, "INSTALLED", "", null, null);
+    ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"MAPREDUCE_CLIENT", "Host100", null, null, "STARTED", "", null, null);
+    ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"TASKTRACKER", "Host100", null, null, "STARTED", "", null, null);
+
+    Set<ServiceComponentHostResponse> responses = new LinkedHashSet<ServiceComponentHostResponse>();
+    responses.add(shr1);
+    responses.add(shr2);
+    responses.add(shr3);
+
+    // set expectations
+    expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
+    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getCluster("C1")).andReturn(cluster).anyTimes();
+    expect(managementController.getHostComponents((Set<ServiceComponentHostRequest>)
anyObject())).andReturn(responses).anyTimes();
+    expect(cluster.getDesiredStackVersion()).andReturn(stackId);
+
+    expect(stackId.getStackName()).andReturn("S1").anyTimes();
+    expect(stackId.getStackVersion()).andReturn("V1").anyTimes();
+
+
+    expect(ambariMetaInfo.getComponentCategory((String) anyObject(), (String) anyObject(),
+        (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes();
+
+    expect(componentInfo.isMaster()).andReturn(true);
+
+    // replay
+    replay(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+
+    ServiceResourceProvider.ServiceState serviceState = new ServiceResourceProvider.DefaultServiceState();
+
+    State state = serviceState.getState(managementController, "C1", "MAPREDUCE");
+    Assert.assertEquals(State.INSTALLED, state);
+
+    // verify
+    verify(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+  }
+
+  @Test
+  public void testDefaultServiceState_MAINTENANCE() throws Exception{
+    AmbariManagementController managementController = createMock(AmbariManagementController.class);
+    Clusters clusters = createNiceMock(Clusters.class);
+    Cluster cluster = createNiceMock(Cluster.class);
+    AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
+    StackId stackId = createNiceMock(StackId.class);
+    ComponentInfo componentInfo = createNiceMock(ComponentInfo.class);
+
+    ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"JOBTRACKER", "Host100", null, null, "MAINTENANCE", "", null, null);
+    ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"MAPREDUCE_CLIENT", "Host100", null, null, "STARTED", "", null, null);
+    ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("C1", "MAPREDUCE",
"TASKTRACKER", "Host100", null, null, "STARTED", "", null, null);
+
+    Set<ServiceComponentHostResponse> responses = new LinkedHashSet<ServiceComponentHostResponse>();
+    responses.add(shr1);
+    responses.add(shr2);
+    responses.add(shr3);
+
+    // set expectations
+    expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
+    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getCluster("C1")).andReturn(cluster).anyTimes();
+    expect(managementController.getHostComponents((Set<ServiceComponentHostRequest>)
anyObject())).andReturn(responses).anyTimes();
+    expect(cluster.getDesiredStackVersion()).andReturn(stackId);
+
+    expect(stackId.getStackName()).andReturn("S1").anyTimes();
+    expect(stackId.getStackVersion()).andReturn("V1").anyTimes();
+
+
+    expect(ambariMetaInfo.getComponentCategory((String) anyObject(), (String) anyObject(),
+        (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes();
+
+    expect(componentInfo.isMaster()).andReturn(true);
+    expect(componentInfo.isMaster()).andReturn(false);
+    expect(componentInfo.isMaster()).andReturn(false);
+
+    // replay
+    replay(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+
+    ServiceResourceProvider.ServiceState serviceState = new ServiceResourceProvider.DefaultServiceState();
+
+    State state = serviceState.getState(managementController, "C1", "MAPREDUCE");
+    Assert.assertEquals(State.STARTED, state);
+
+    // verify
+    verify(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+  }
+
+  @Test
+  public void testHDFSServiceState_STARTED() throws Exception{
+    AmbariManagementController managementController = createMock(AmbariManagementController.class);
+    Clusters clusters = createNiceMock(Clusters.class);
+    Cluster cluster = createNiceMock(Cluster.class);
+    AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
+    StackId stackId = createNiceMock(StackId.class);
+    ComponentInfo componentInfo = createNiceMock(ComponentInfo.class);
+
+    ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("C1", "HDFS", "NAMENODE",
"Host100", null, null, "STARTED", "", null, null);
+    ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("C1", "HDFS", "SECONDARY_NAMENODE",
"Host100", null, null, "STARTED", "", null, null);
+    ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("C1", "HDFS", "JOURNALNODE",
"Host100", null, null, "STARTED", "", null, null);
+
+    Set<ServiceComponentHostResponse> responses = new LinkedHashSet<ServiceComponentHostResponse>();
+    responses.add(shr1);
+    responses.add(shr2);
+    responses.add(shr3);
+
+    // set expectations
+    expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
+    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getCluster("C1")).andReturn(cluster).anyTimes();
+    expect(managementController.getHostComponents((Set<ServiceComponentHostRequest>)
anyObject())).andReturn(responses).anyTimes();
+    expect(cluster.getDesiredStackVersion()).andReturn(stackId);
+
+    expect(stackId.getStackName()).andReturn("S1").anyTimes();
+    expect(stackId.getStackVersion()).andReturn("V1").anyTimes();
+
+
+    expect(ambariMetaInfo.getComponentCategory((String) anyObject(), (String) anyObject(),
+        (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes();
+
+    expect(componentInfo.isMaster()).andReturn(true);
+    expect(componentInfo.isMaster()).andReturn(true);
+    expect(componentInfo.isMaster()).andReturn(true);
+
+    // replay
+    replay(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+
+    ServiceResourceProvider.ServiceState serviceState = new ServiceResourceProvider.HDFSServiceState();
+
+    State state = serviceState.getState(managementController, "C1", "MAPREDUCE");
+    Assert.assertEquals(State.STARTED, state);
+
+    // verify
+    verify(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+  }
+
+  @Test
+  public void testHDFSServiceState_STARTED2() throws Exception{
+    AmbariManagementController managementController = createMock(AmbariManagementController.class);
+    Clusters clusters = createNiceMock(Clusters.class);
+    Cluster cluster = createNiceMock(Cluster.class);
+    AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
+    StackId stackId = createNiceMock(StackId.class);
+    ComponentInfo componentInfo = createNiceMock(ComponentInfo.class);
+
+    ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("C1", "HDFS", "NAMENODE",
"Host100", null, null, "INSTALLED", "", null, null);
+    ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("C1", "HDFS", "NAMENODE",
"Host101", null, null, "STARTED", "", null, null);
+    ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("C1", "HDFS", "JOURNALNODE",
"Host100", null, null, "STARTED", "", null, null);
+
+    Set<ServiceComponentHostResponse> responses = new LinkedHashSet<ServiceComponentHostResponse>();
+    responses.add(shr1);
+    responses.add(shr2);
+    responses.add(shr3);
+
+    // set expectations
+    expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
+    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getCluster("C1")).andReturn(cluster).anyTimes();
+    expect(managementController.getHostComponents((Set<ServiceComponentHostRequest>)
anyObject())).andReturn(responses).anyTimes();
+    expect(cluster.getDesiredStackVersion()).andReturn(stackId);
+
+    expect(stackId.getStackName()).andReturn("S1").anyTimes();
+    expect(stackId.getStackVersion()).andReturn("V1").anyTimes();
+
+
+    expect(ambariMetaInfo.getComponentCategory((String) anyObject(), (String) anyObject(),
+        (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes();
+
+    expect(componentInfo.isMaster()).andReturn(true);
+    expect(componentInfo.isMaster()).andReturn(true);
+    expect(componentInfo.isMaster()).andReturn(true);
+
+    // replay
+    replay(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+
+    ServiceResourceProvider.ServiceState serviceState = new ServiceResourceProvider.HDFSServiceState();
+
+    State state = serviceState.getState(managementController, "C1", "MAPREDUCE");
+    Assert.assertEquals(State.STARTED, state);
+
+    // verify
+    verify(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+  }
+
+  @Test
+  public void testHBaseServiceState_STARTED() throws Exception{
+    AmbariManagementController managementController = createMock(AmbariManagementController.class);
+    Clusters clusters = createNiceMock(Clusters.class);
+    Cluster cluster = createNiceMock(Cluster.class);
+    AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
+    StackId stackId = createNiceMock(StackId.class);
+    ComponentInfo componentInfo = createNiceMock(ComponentInfo.class);
+
+    ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("C1", "HDFS", "HBASE_MASTER",
"Host100", null, null, "STARTED", "", null, null);
+    ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("C1", "HDFS", "HBASE_MASTER",
"Host101", null, null, "INSTALLED", "", null, null);
+    ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("C1", "HDFS", "HBASE_REGIONSERVER",
"Host100", null, null, "STARTED", "", null, null);
+
+    Set<ServiceComponentHostResponse> responses = new LinkedHashSet<ServiceComponentHostResponse>();
+    responses.add(shr1);
+    responses.add(shr2);
+    responses.add(shr3);
+
+    // set expectations
+    expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
+    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getCluster("C1")).andReturn(cluster).anyTimes();
+    expect(managementController.getHostComponents((Set<ServiceComponentHostRequest>)
anyObject())).andReturn(responses).anyTimes();
+    expect(cluster.getDesiredStackVersion()).andReturn(stackId);
+
+    expect(stackId.getStackName()).andReturn("S1").anyTimes();
+    expect(stackId.getStackVersion()).andReturn("V1").anyTimes();
+
+
+    expect(ambariMetaInfo.getComponentCategory((String) anyObject(), (String) anyObject(),
+        (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes();
+
+    expect(componentInfo.isMaster()).andReturn(true);
+    expect(componentInfo.isMaster()).andReturn(true);
+    expect(componentInfo.isMaster()).andReturn(false);
+
+    // replay
+    replay(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+
+    ServiceResourceProvider.ServiceState serviceState = new ServiceResourceProvider.HBaseServiceState();
+
+    State state = serviceState.getState(managementController, "C1", "MAPREDUCE");
+    Assert.assertEquals(State.STARTED, state);
+
+    // verify
+    verify(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo);
+  }
+
   public static ServiceResourceProvider getServiceProvider(AmbariManagementController managementController)
{
     Resource.Type type = Resource.Type.Service;
 


Mime
View raw message