public boolean assertClusterOverallAsBalanced(List<ServerAndLoad> servers, int tablenum) {
    int numServers = servers.size();
    int numRegions = 0;
    int maxRegions = 0;
    int minRegions = Integer.MAX_VALUE;
    for (ServerAndLoad server : servers) {
      int nr = server.getLoad();
      if (nr > maxRegions) {
        maxRegions = nr;
      }
      if (nr < minRegions) {
        minRegions = nr;
      }
      numRegions += nr;
    }
    if (maxRegions - minRegions < 2) {
      // less than 2 between max and min, can't balance
      return true;
    }
    int min = numRegions / numServers;
    int max = numRegions % numServers == 0 ? min : min + 1;

    for (ServerAndLoad server : servers) {
      // The '5' in below is arbitrary.
      if (
        server.getLoad() < 0 || server.getLoad() > max + (tablenum / 2 + 5)
          || server.getLoad() < (min - tablenum / 2 - 5)
      ) {
        LOG.warn("server={}, load={}, max={}, tablenum={}, min={}", server.getServerName(),
          server.getLoad(), max, tablenum, min);
        return false;
      }
    }
    return true;
  }
Invariant is that all servers have between acceptable range number of regions.
hbase-balancer/src/test/java/org/apache/hadoop/hbase/master/balancer/BalancerTestBase.java