class HDFSServiceCalculatedState extends DefaultServiceCalculatedState implements ServiceCalculatedState {

  @Override
  public State getState(String clusterName, String serviceName) {
    try {
      Cluster cluster = getCluster(clusterName);
      if (cluster != null && managementControllerProvider != null) {
        AmbariMetaInfo ambariMetaInfo = managementControllerProvider.get().getAmbariMetaInfo();
        Service service = cluster.getService(serviceName);
        StackId stackId = service.getDesiredStackId();

        ServiceComponentHostRequest request = new ServiceComponentHostRequest(clusterName,
          serviceName, null, null, null);

        Set<ServiceComponentHostResponse> hostComponentResponses =
          managementControllerProvider.get().getHostComponents(Collections.singleton(request), true);

        Set<String> startedOrDisabledNNHosts = new HashSet<>();

        int     nameNodeCount       = 0;
        int     nameNodeStartedOrDisabledCount = 0;
        boolean hasSecondary        = false;
        boolean hasJournal          = false;
        State   nonStartedState     = null;

        for (ServiceComponentHostResponse hostComponentResponse : hostComponentResponses ) {
          try {
            ComponentInfo componentInfo = ambariMetaInfo.getComponent(stackId.getStackName(),
              stackId.getStackVersion(), hostComponentResponse.getServiceName(),
              hostComponentResponse.getComponentName());

            if (componentInfo.isMaster()) {
              String componentName = hostComponentResponse.getComponentName();
              boolean isNameNode = false;

              switch (componentName) {
                case "NAMENODE":
                  ++nameNodeCount;
                  isNameNode = true;
                  break;
                case "SECONDARY_NAMENODE":
                  hasSecondary = true;
                  break;
                case "JOURNALNODE":
                  hasJournal = true;
                  break;
              }

              State state = getHostComponentState(hostComponentResponse);

              switch (state) {
                case STARTED:
                case DISABLED:
                  if (isNameNode) {
                    ++nameNodeStartedOrDisabledCount;
                    startedOrDisabledNNHosts.add(hostComponentResponse.getHostname());
                  }
                  break;
                default:
                  nonStartedState = state;
              }
            }
          } catch (ObjectNotFoundException e) {
            // component doesn't exist, nothing to do
          }
        }

        boolean multipleNameServices = nameNodeCount > 2;
        int nameServiceWithStartedOrDisabledNNCount = 0;
        List<NameService> nameServices = new ArrayList<>();

        // count name services that has at least 1 namenode in started or disabled state
        if (multipleNameServices) {
          ConfigHelper configHelper = managementControllerProvider.get().getConfigHelper();
          nameServices = NameService.fromConfig(configHelper, cluster);

          for (NameService nameService : nameServices) {
            boolean hasStartedOrDisabledNN = false;
            for (NameService.NameNode nameNode : nameService.getNameNodes()) {
              if (startedOrDisabledNNHosts.contains(nameNode.getHost())) {
                hasStartedOrDisabledNN = true;
                break;
              }
            }
            if (hasStartedOrDisabledNN) {
              nameServiceWithStartedOrDisabledNNCount++;
            }
          }
        }

        // all started OR at least one active namenode for single namespace AND at least one namenode for each namespace for multiple namespaces
        if (nonStartedState == null ||  // all started
            ((nameNodeCount > 0 && !hasSecondary || hasJournal) &&
                nameNodeStartedOrDisabledCount > 0 &&
                (!multipleNameServices || nameServiceWithStartedOrDisabledNNCount == nameServices.size()))) {
          return State.STARTED;
        }
        return nonStartedState;
      }
    } catch (AmbariException e) {
      LOG.error("Can't determine service state.", e);
    }
    return State.UNKNOWN;
  }

}