public List<Pair<Long, TConsensusGroupId>> getSortedRegionGroupSlotsCounter(
      final String database, final TConsensusGroupType type)
      throws NoAvailableRegionGroupException {
    // Collect static data
    final List<Pair<Long, TConsensusGroupId>> regionGroupSlotsCounter =
        partitionInfo.getRegionGroupSlotsCounter(database, type);

    // Filter RegionGroups that have Disabled status
    final List<Pair<Long, TConsensusGroupId>> result = new ArrayList<>();
    for (final Pair<Long, TConsensusGroupId> slotsCounter : regionGroupSlotsCounter) {
      final RegionGroupStatus status =
          getLoadManager().getRegionGroupStatus(slotsCounter.getRight());
      if (!RegionGroupStatus.Disabled.equals(status)) {
        result.add(slotsCounter);
      }
    }

    if (result.isEmpty()) {
      throw new NoAvailableRegionGroupException(type);
    }

    final Map<TConsensusGroupId, RegionGroupStatus> regionGroupStatusMap =
        getLoadManager()
            .getRegionGroupStatus(result.stream().map(Pair::getRight).collect(Collectors.toList()));
    result.sort(
        (o1, o2) -> {
          // Use the number of partitions as the first priority
          if (o1.getLeft() < o2.getLeft()) {
            return -1;
          } else if (o1.getLeft() > o2.getLeft()) {
            return 1;
          } else {
            // Use RegionGroup status as second priority, Running > Available > Discouraged
            return regionGroupStatusMap
                .get(o1.getRight())
                .compare(regionGroupStatusMap.get(o2.getRight()));
          }
        });
    return result;
  }
Only leader use this interface.

@param database DatabaseName
@param type SchemaRegion or DataRegion
@return The specific StorageGroup's Regions that sorted by the number of allocated slots
@throws NoAvailableRegionGroupException When all RegionGroups within the specified StorageGroup
    are unavailable currently
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/partition/PartitionManager.java