@Override
  public void run() {
    try {
      // empty the map for each region
      Map<String, AtomicInteger> blockCountMap = new HashMap<>();

      // get table name
      String tableName = regionPath.getParent().getName();
      int totalBlkCount = 0;

      // ignore null
      FileStatus[] cfList = fs.listStatus(regionPath, new FSUtils.FamilyDirFilter(fs));
      if (null == cfList) {
        return;
      }

      // for each cf, get all the blocks information
      for (FileStatus cfStatus : cfList) {
        if (!cfStatus.isDirectory()) {
          // skip because this is not a CF directory
          continue;
        }

        FileStatus[] storeFileLists = fs.listStatus(cfStatus.getPath());
        if (null == storeFileLists) {
          continue;
        }

        for (FileStatus storeFile : storeFileLists) {
          BlockLocation[] blkLocations = fs.getFileBlockLocations(storeFile, 0, storeFile.getLen());
          if (null == blkLocations) {
            continue;
          }

          totalBlkCount += blkLocations.length;
          for (BlockLocation blk : blkLocations) {
            for (String host : blk.getHosts()) {
              AtomicInteger count = blockCountMap.get(host);
              if (count == null) {
                count = new AtomicInteger(0);
                blockCountMap.put(host, count);
              }
              count.incrementAndGet();
            }
          }
        }
      }

      if (regionToBestLocalityRSMapping != null) {
        int largestBlkCount = 0;
        String hostToRun = null;
        for (Map.Entry<String, AtomicInteger> entry : blockCountMap.entrySet()) {
          String host = entry.getKey();

          int tmp = entry.getValue().get();
          if (tmp > largestBlkCount) {
            largestBlkCount = tmp;
            hostToRun = host;
          }
        }

        // empty regions could make this null
        if (null == hostToRun) {
          return;
        }

        if (hostToRun.endsWith(".")) {
          hostToRun = hostToRun.substring(0, hostToRun.length() - 1);
        }
        String name = tableName + ":" + regionPath.getName();
        synchronized (regionToBestLocalityRSMapping) {
          regionToBestLocalityRSMapping.put(name, hostToRun);
        }
      }

      if (regionDegreeLocalityMapping != null && totalBlkCount > 0) {
        Map<String, Float> hostLocalityMap = new HashMap<>();
        for (Map.Entry<String, AtomicInteger> entry : blockCountMap.entrySet()) {
          String host = entry.getKey();
          if (host.endsWith(".")) {
            host = host.substring(0, host.length() - 1);
          }
          // Locality is fraction of blocks local to this host.
          float locality = ((float) entry.getValue().get()) / totalBlkCount;
          hostLocalityMap.put(host, locality);
        }
        // Put the locality map into the result map, keyed by the encoded name
        // of the region.
        regionDegreeLocalityMapping.put(regionPath.getName(), hostLocalityMap);
      }
    } catch (IOException e) {
      LOG.warn("Problem scanning file system", e);
    } catch (RuntimeException e) {
      LOG.warn("Problem scanning file system", e);
    }
  }
Maps region encoded names to maps of hostnames to fractional locality of that region on that
host.
hbase-server/src/main/java/org/apache/hadoop/hbase/util/FSRegionScanner.java