@Override
  public void prepare(BalancerClusterState cluster) {
    // First, set the cluster state and allocate one DoubleArrayCost per table.
    this.cluster = cluster;
    costsPerTable = new DoubleArrayCost[cluster.numTables];
    for (int tableIdx = 0; tableIdx < cluster.numTables; tableIdx++) {
      costsPerTable[tableIdx] = new DoubleArrayCost();
      costsPerTable[tableIdx].prepare(cluster.numServers);
      final int tableIndex = tableIdx;
      costsPerTable[tableIdx].applyCostsChange(costs -> {
        // For each server, compute the aggregated storefile size for this table.
        for (int server = 0; server < cluster.numServers; server++) {
          double totalStorefileMB = 0;
          // Sum over all regions on this server that belong to the given table.
          for (int region : cluster.regionsPerServer[server]) {
            if (cluster.regionIndexToTableIndex[region] == tableIndex) {
              Collection<BalancerRegionLoad> loads = cluster.getRegionLoads()[region];
              double regionCost = 0;
              if (loads != null && !loads.isEmpty()) {
                // Average the storefile sizes if there are multiple measurements.
                for (BalancerRegionLoad rl : loads) {
                  regionCost += getCostFromRl(rl);
                }
                regionCost /= loads.size();
              }
              totalStorefileMB += regionCost;
            }
          }
          costs[server] = totalStorefileMB;
        }
      });
    }
  }
Lightweight cost function that mirrors TableSkewCostFunction but aggregates storefile sizes (in
MB) per table using the CostFromRegionLoadFunction framework. For each table, it computes a
per-server aggregated storefile size by summing the average storefile size for each region (if
there are multiple load metrics, it averages them). The imbalance cost (as computed by
DoubleArrayCost) is then used to drive the balancer to reduce differences between servers.
hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/StoreFileTableSkewCostFunction.java