private void updatePartitionResourceMappingFromIdealStates(Collection<IdealState> idealStates, String dcName) {
      if (dcName != null) {
        // Rebuild the entire partition-to-resource map in current dc
        ConcurrentHashMap<String, Set<String>> partitionToResourceMap = new ConcurrentHashMap<>();
        ConcurrentHashMap<String, Set<String>> resourceToPartitionMap = new ConcurrentHashMap<>();
        ConcurrentHashMap<String, String> partitionToDuplicateResourceMap = new ConcurrentHashMap<>();
        ConcurrentHashMap<String, Set<String>> partitionResourceToInstanceSetMap = new ConcurrentHashMap<>();
        ConcurrentHashMap<String, ResourceProperty> tagToProperty = new ConcurrentHashMap<>();
        ConcurrentHashMap<String, String> resourceNameToTag = new ConcurrentHashMap<>();
        for (IdealState state : idealStates) {
          String resourceName = state.getResourceName();
          String tag = state.getInstanceGroupTag();
          int numPartitions = state.getNumPartitions();
          int replicationFactor = state.getReplicaCount(DEFAULT_NUM_REPLICAS);
          if (replicationFactor == 0) {
            replicationFactor = DEFAULT_NUM_REPLICAS;
          }
          int minActiveReplicas = state.getMinActiveReplicas();

          // In case MIN_ACTIVE_REPLICAS is not set
          if (minActiveReplicas == -1) {
            minActiveReplicas = replicationFactor - 1;
          }

          if (!Strings.isEmpty(tag)) {
            resourceNameToTag.put(resourceName, tag);
            ResourceProperty resourceProperty =
                new ResourceProperty(resourceName, numPartitions, replicationFactor, state.getRebalanceMode(), minActiveReplicas);
            tagToProperty.put(tag, resourceProperty);
          }
          resourceToPartitionMap.put(resourceName, new HashSet<>(state.getPartitionSet()));
          // Build the partition to resource, partition to duplicate resource and partition-resource to instance set map
          for (String partitionName : state.getPartitionSet()) {
            Set<String> instanceSet = state.getInstanceSet(partitionName);
            Set<String> resources = partitionToResourceMap.computeIfAbsent(partitionName, k -> new HashSet<>());
            if (resources.isEmpty()) {
              // there is no resource mapped to partition, just add this partition/resource in the map.
              resources.add(resourceName);
              partitionResourceToInstanceSetMap.put(partitionName + "-" + resourceName, instanceSet);
            } else {
              boolean foundDuplicate = false;
              for (String prevResource : resources) {
                Set<String> instanceSetInMap =
                    partitionResourceToInstanceSetMap.get(partitionName + "-" + prevResource);
                if (instanceSet.equals(instanceSetInMap)) {
                  foundDuplicate = true;
                  // we found the duplicate resource that has the same instance set, remove the smaller resource id
                  int currentResourceId = Integer.valueOf(resourceName);
                  int previousResourceId = Integer.valueOf(prevResource);
                  if (currentResourceId > previousResourceId) {
                    resources.remove(prevResource);
                    resources.add(resourceName);
                    partitionResourceToInstanceSetMap.remove(partitionName + "-" + prevResource);
                    partitionResourceToInstanceSetMap.put(partitionName + "-" + resourceName, instanceSet);
                    partitionToDuplicateResourceMap.put(partitionName, prevResource);
                  } else {
                    partitionToDuplicateResourceMap.put(partitionName, resourceName);
                  }
                  break;
                }
              }
              if (!foundDuplicate) {
                resources.add(resourceName);
                partitionResourceToInstanceSetMap.put(partitionName + "-" + resourceName, instanceSet);
              }
            }
          }
        }
        dcToTagToResourceProperty.put(dcName, tagToProperty);
        dcToResourceNameToTag.put(dcName, resourceNameToTag);
        partitionToResourceNameByDc.put(dcName, partitionToResourceMap);
        partitionToDuplicateResourceNameByDc.put(dcName, partitionToDuplicateResourceMap);
        resourceNameToPartitionByDc.put(dcName, resourceToPartitionMap);

        // Ideal state has changed, we need to check if the data node is now on FULL AUTO or not.
        // This call might come from frontend node, make sure we don't register any FULL AUTO metrics in frontend.
        DataNodeId currentDataNodeId = instanceNameToAmbryDataNode.get(selfInstanceName);
        if (currentDataNodeId == null || !currentDataNodeId.getDatacenterName().equals(dcName)
            || isDataNodeInFullAutoMode(currentDataNodeId) == localDataNodeInFullAuto.get()) {
          return;
        }
        localDataNodeInFullAuto.set(!localDataNodeInFullAuto.get());
        if (localDataNodeInFullAuto.get()) {
          dataNodeOnBecomingFullAuto();
        } else {
          dataNodeOnBecomingSemiAuto();
        }
      } else {
        logger.warn("Partition to resource mapping for aggregated cluster view would be built from external view");
      }
    }
Update partition to resource mapping from Ideal states.
@param idealStates list of ideal states for various resources.
@param dcName data center for which this mapping corresponds to.
ambry-clustermap/src/main/java/com/github/ambry/clustermap/HelixClusterManager.java