@Override
  public RoleCommandOrder getRoleCommandOrder(Cluster cluster) {
    boolean hasGLUSTERFS = false;
    boolean isNameNodeHAEnabled = false;
    boolean isResourceManagerHAEnabled = false;

    try {
      if (cluster != null && cluster.getService("GLUSTERFS") != null) {
        hasGLUSTERFS = true;
      }
    } catch (AmbariException ignored) {
    }

    try {
      if (cluster != null &&
        cluster.getService("HDFS") != null &&
        cluster.getService("HDFS").getServiceComponent("JOURNALNODE") != null) {
        isNameNodeHAEnabled = true;
      }
    } catch (AmbariException ignored) {
    }

    try {
      if (cluster != null &&
        cluster.getService("YARN") != null &&
        cluster.getService("YARN").getServiceComponent("RESOURCEMANAGER").getServiceComponentHosts().size() > 1) {
        isResourceManagerHAEnabled = true;
      }
    } catch (AmbariException ignored) {
    }

    int clusterCacheId = new HashCodeBuilder()
      .append(cluster != null ? cluster.getClusterId() : -1)
      .append(hasGLUSTERFS)
      .append(isNameNodeHAEnabled)
      .append(isResourceManagerHAEnabled)
      .toHashCode();

    RoleCommandOrder rco = rcoMap.get(clusterCacheId);
    if (rco == null) {
      rco = injector.getInstance(RoleCommandOrder.class);

      LinkedHashSet<String> sectionKeys = new LinkedHashSet<>();

      if (hasGLUSTERFS) {
        sectionKeys.add(RoleCommandOrder.GLUSTERFS_DEPS_KEY);
      } else {
        sectionKeys.add(RoleCommandOrder.NO_GLUSTERFS_DEPS_KEY);
      }

      if (isNameNodeHAEnabled) {
        sectionKeys.add(RoleCommandOrder.NAMENODE_HA_DEPS_KEY);
      }

      if (isResourceManagerHAEnabled) {
        sectionKeys.add(RoleCommandOrder.RESOURCEMANAGER_HA_DEPS_KEY);
      }

      rco.initialize(cluster, sectionKeys);
      rcoMap.put(clusterCacheId, rco);
    }
    return rco;
  }
RoleCommandOrderProvider which caches RoleCommandOrder objects for a cluster to avoid the cost of construction of
RoleCommandOrder objects each time.
ambari-server/src/main/java/org/apache/ambari/server/metadata/CachedRoleCommandOrderProvider.java