int selectCoHostedRegionPerGroup(Int2IntCounterMap colocatedReplicaCountsPerGroup,
    int[] regionsPerGroup, int[] regionIndexToPrimaryIndex) {
    final IntArrayList colocated = new IntArrayList(colocatedReplicaCountsPerGroup.size(), -1);
    colocatedReplicaCountsPerGroup.forEach((primary, count) -> {
      if (count > 1) { // means consecutive primaries, indicating co-location
        colocated.add(primary);
      }
    });

    if (!colocated.isEmpty()) {
      int rand = ThreadLocalRandom.current().nextInt(colocated.size());
      int selectedPrimaryIndex = colocated.get(rand);
      // we have found the primary id for the region to move. Now find the actual regionIndex
      // with the given primary, prefer to move the secondary region.
      for (int regionIndex : regionsPerGroup) {
        if (selectedPrimaryIndex == regionIndexToPrimaryIndex[regionIndex]) {
          // always move the secondary, not the primary
          if (selectedPrimaryIndex != regionIndex) {
            return regionIndex;
          }
        }
      }
    }
    return -1;
  }
Randomly select one regionIndex out of all region replicas co-hosted in the same group (a group
is a server, host or rack)
@param colocatedReplicaCountsPerGroup either Cluster.colocatedReplicaCountsPerServer,
                                      colocatedReplicaCountsPerHost or
                                      colocatedReplicaCountsPerRack
@param regionsPerGroup                either Cluster.regionsPerServer, regionsPerHost or
                                      regionsPerRack
@param regionIndexToPrimaryIndex      Cluster.regionsIndexToPrimaryIndex
@return a regionIndex for the selected primary or -1 if there is no co-locating
hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/RegionReplicaCandidateGenerator.java