@Override
  void prepare(BalancerClusterState cluster) {
    super.prepare(cluster);
    locality = 0.0;
    bestLocality = 0.0;

    for (int region = 0; region < cluster.numRegions; region++) {
      locality += getWeightedLocality(region, regionIndexToEntityIndex(region));
      bestLocality += getWeightedLocality(region, getMostLocalEntityForRegion(region));
    }

    // We normalize locality to be a score between 0 and 1.0 representing how good it
    // is compared to how good it could be. If bestLocality is 0, assume locality is 100
    // (and the cost is 0)
    locality = bestLocality == 0 ? 1.0 : locality / bestLocality;
  }
Maps region to the current entity (server or rack) on which it is stored
hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/LocalityBasedCostFunction.java