/** Invariant is that all servers have between acceptable range number of regions. */ 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) { if (server.getLoad() < 0 || server.getLoad() > max + tablenum / 2 + 1 || server.getLoad() < min - tablenum / 2 - 1) return false; } return true; }
/** Invariant is that all servers have between floor(avg) and ceiling(avg) number of regions. */ public void assertClusterAsBalanced(List<ServerAndLoad> servers) { 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; } int min = numRegions / numServers; int max = numRegions % numServers == 0 ? min : min + 1; for (ServerAndLoad server : servers) { assertTrue(server.getLoad() >= 0); assertTrue(server.getLoad() <= max); assertTrue(server.getLoad() >= min); } }
protected void updateLoad( final Map<ServerName, ServerAndLoad> map, final ServerName sn, final int diff) { ServerAndLoad sal = map.get(sn); if (sal == null) sal = new ServerAndLoad(sn, 0); sal = new ServerAndLoad(sn, sal.getLoad() + diff); map.put(sn, sal); }
protected String printStats(List<ServerAndLoad> servers) { int numServers = servers.size(); int totalRegions = 0; for (ServerAndLoad server : servers) { totalRegions += server.getLoad(); } float average = (float) totalRegions / numServers; int max = (int) Math.ceil(average); int min = (int) Math.floor(average); return "[srvr=" + numServers + " rgns=" + totalRegions + " avg=" + average + " max=" + max + " min=" + min + "]"; }