http://git-wip-us.apache.org/repos/asf/hbase-site/blob/8e3b63ca/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.CostFromRegionLoadFunction.html
----------------------------------------------------------------------
diff --git
a/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.CostFromRegionLoadFunction.html
b/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.CostFromRegionLoadFunction.html
index c895448..545d4da 100644
---
a/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.CostFromRegionLoadFunction.html
+++
b/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.CostFromRegionLoadFunction.html
@@ -1294,425 +1294,426 @@
<span class="sourceLineNo">1286</span> }<a name="line.1286"></a>
<span class="sourceLineNo">1287</span><a name="line.1287"></a>
<span class="sourceLineNo">1288</span> // We normalize locality to be a
score between 0 and 1.0 representing how good it<a name="line.1288"></a>
-<span class="sourceLineNo">1289</span> // is compared to how good it
could be<a name="line.1289"></a>
-<span class="sourceLineNo">1290</span> locality /= bestLocality;<a
name="line.1290"></a>
-<span class="sourceLineNo">1291</span> }<a name="line.1291"></a>
-<span class="sourceLineNo">1292</span><a name="line.1292"></a>
-<span class="sourceLineNo">1293</span> @Override<a name="line.1293"></a>
-<span class="sourceLineNo">1294</span> protected void regionMoved(int
region, int oldServer, int newServer) {<a name="line.1294"></a>
-<span class="sourceLineNo">1295</span> int oldEntity = type ==
LocalityType.SERVER ? oldServer : cluster.serverIndexToRackIndex[oldServer];<a
name="line.1295"></a>
-<span class="sourceLineNo">1296</span> int newEntity = type ==
LocalityType.SERVER ? newServer : cluster.serverIndexToRackIndex[newServer];<a
name="line.1296"></a>
-<span class="sourceLineNo">1297</span> if (this.services == null) {<a
name="line.1297"></a>
-<span class="sourceLineNo">1298</span> return;<a name="line.1298"></a>
-<span class="sourceLineNo">1299</span> }<a name="line.1299"></a>
-<span class="sourceLineNo">1300</span> double localityDelta =
getWeightedLocality(region, newEntity) - getWeightedLocality(region,
oldEntity);<a name="line.1300"></a>
-<span class="sourceLineNo">1301</span> double normalizedDelta =
localityDelta / bestLocality;<a name="line.1301"></a>
-<span class="sourceLineNo">1302</span> locality += normalizedDelta;<a
name="line.1302"></a>
-<span class="sourceLineNo">1303</span> }<a name="line.1303"></a>
-<span class="sourceLineNo">1304</span><a name="line.1304"></a>
-<span class="sourceLineNo">1305</span> @Override<a name="line.1305"></a>
-<span class="sourceLineNo">1306</span> double cost() {<a
name="line.1306"></a>
-<span class="sourceLineNo">1307</span> return 1 - locality;<a
name="line.1307"></a>
-<span class="sourceLineNo">1308</span> }<a name="line.1308"></a>
-<span class="sourceLineNo">1309</span><a name="line.1309"></a>
-<span class="sourceLineNo">1310</span> private int
getMostLocalEntityForRegion(int region) {<a name="line.1310"></a>
-<span class="sourceLineNo">1311</span> return
cluster.getOrComputeRegionsToMostLocalEntities(type)[region];<a
name="line.1311"></a>
-<span class="sourceLineNo">1312</span> }<a name="line.1312"></a>
-<span class="sourceLineNo">1313</span><a name="line.1313"></a>
-<span class="sourceLineNo">1314</span> private double
getWeightedLocality(int region, int entity) {<a name="line.1314"></a>
-<span class="sourceLineNo">1315</span> return
cluster.getOrComputeWeightedLocality(region, entity, type);<a
name="line.1315"></a>
-<span class="sourceLineNo">1316</span> }<a name="line.1316"></a>
-<span class="sourceLineNo">1317</span><a name="line.1317"></a>
-<span class="sourceLineNo">1318</span> }<a name="line.1318"></a>
-<span class="sourceLineNo">1319</span><a name="line.1319"></a>
-<span class="sourceLineNo">1320</span> static class
ServerLocalityCostFunction extends LocalityBasedCostFunction {<a
name="line.1320"></a>
-<span class="sourceLineNo">1321</span><a name="line.1321"></a>
-<span class="sourceLineNo">1322</span> private static final String
LOCALITY_COST_KEY = "hbase.master.balancer.stochastic.localityCost";<a
name="line.1322"></a>
-<span class="sourceLineNo">1323</span> private static final float
DEFAULT_LOCALITY_COST = 25;<a name="line.1323"></a>
-<span class="sourceLineNo">1324</span><a name="line.1324"></a>
-<span class="sourceLineNo">1325</span>
ServerLocalityCostFunction(Configuration conf, MasterServices srv) {<a
name="line.1325"></a>
-<span class="sourceLineNo">1326</span> super(<a name="line.1326"></a>
-<span class="sourceLineNo">1327</span> conf,<a name="line.1327"></a>
-<span class="sourceLineNo">1328</span> srv,<a name="line.1328"></a>
-<span class="sourceLineNo">1329</span> LocalityType.SERVER,<a
name="line.1329"></a>
-<span class="sourceLineNo">1330</span> LOCALITY_COST_KEY,<a
name="line.1330"></a>
-<span class="sourceLineNo">1331</span> DEFAULT_LOCALITY_COST<a
name="line.1331"></a>
-<span class="sourceLineNo">1332</span> );<a name="line.1332"></a>
-<span class="sourceLineNo">1333</span> }<a name="line.1333"></a>
-<span class="sourceLineNo">1334</span><a name="line.1334"></a>
-<span class="sourceLineNo">1335</span> @Override<a name="line.1335"></a>
-<span class="sourceLineNo">1336</span> int regionIndexToEntityIndex(int
region) {<a name="line.1336"></a>
-<span class="sourceLineNo">1337</span> return
cluster.regionIndexToServerIndex[region];<a name="line.1337"></a>
-<span class="sourceLineNo">1338</span> }<a name="line.1338"></a>
-<span class="sourceLineNo">1339</span> }<a name="line.1339"></a>
-<span class="sourceLineNo">1340</span><a name="line.1340"></a>
-<span class="sourceLineNo">1341</span> static class RackLocalityCostFunction
extends LocalityBasedCostFunction {<a name="line.1341"></a>
-<span class="sourceLineNo">1342</span><a name="line.1342"></a>
-<span class="sourceLineNo">1343</span> private static final String
RACK_LOCALITY_COST_KEY = "hbase.master.balancer.stochastic.rackLocalityCost";<a
name="line.1343"></a>
-<span class="sourceLineNo">1344</span> private static final float
DEFAULT_RACK_LOCALITY_COST = 15;<a name="line.1344"></a>
-<span class="sourceLineNo">1345</span><a name="line.1345"></a>
-<span class="sourceLineNo">1346</span> public
RackLocalityCostFunction(Configuration conf, MasterServices services) {<a
name="line.1346"></a>
-<span class="sourceLineNo">1347</span> super(<a name="line.1347"></a>
-<span class="sourceLineNo">1348</span> conf,<a name="line.1348"></a>
-<span class="sourceLineNo">1349</span> services,<a
name="line.1349"></a>
-<span class="sourceLineNo">1350</span> LocalityType.RACK,<a
name="line.1350"></a>
-<span class="sourceLineNo">1351</span> RACK_LOCALITY_COST_KEY,<a
name="line.1351"></a>
-<span class="sourceLineNo">1352</span> DEFAULT_RACK_LOCALITY_COST<a
name="line.1352"></a>
-<span class="sourceLineNo">1353</span> );<a name="line.1353"></a>
-<span class="sourceLineNo">1354</span> }<a name="line.1354"></a>
-<span class="sourceLineNo">1355</span><a name="line.1355"></a>
-<span class="sourceLineNo">1356</span> @Override<a name="line.1356"></a>
-<span class="sourceLineNo">1357</span> int regionIndexToEntityIndex(int
region) {<a name="line.1357"></a>
-<span class="sourceLineNo">1358</span> return
cluster.getRackForRegion(region);<a name="line.1358"></a>
-<span class="sourceLineNo">1359</span> }<a name="line.1359"></a>
-<span class="sourceLineNo">1360</span> }<a name="line.1360"></a>
-<span class="sourceLineNo">1361</span><a name="line.1361"></a>
-<span class="sourceLineNo">1362</span> /**<a name="line.1362"></a>
-<span class="sourceLineNo">1363</span> * Base class the allows writing costs
functions from rolling average of some<a name="line.1363"></a>
-<span class="sourceLineNo">1364</span> * number from RegionLoad.<a
name="line.1364"></a>
-<span class="sourceLineNo">1365</span> */<a name="line.1365"></a>
-<span class="sourceLineNo">1366</span> abstract static class
CostFromRegionLoadFunction extends CostFunction {<a name="line.1366"></a>
-<span class="sourceLineNo">1367</span><a name="line.1367"></a>
-<span class="sourceLineNo">1368</span> private ClusterStatus clusterStatus
= null;<a name="line.1368"></a>
-<span class="sourceLineNo">1369</span> private Map<String,
Deque<BalancerRegionLoad>> loads = null;<a name="line.1369"></a>
-<span class="sourceLineNo">1370</span> private double[] stats = null;<a
name="line.1370"></a>
-<span class="sourceLineNo">1371</span>
CostFromRegionLoadFunction(Configuration conf) {<a name="line.1371"></a>
-<span class="sourceLineNo">1372</span> super(conf);<a
name="line.1372"></a>
-<span class="sourceLineNo">1373</span> }<a name="line.1373"></a>
-<span class="sourceLineNo">1374</span><a name="line.1374"></a>
-<span class="sourceLineNo">1375</span> void setClusterStatus(ClusterStatus
status) {<a name="line.1375"></a>
-<span class="sourceLineNo">1376</span> this.clusterStatus = status;<a
name="line.1376"></a>
-<span class="sourceLineNo">1377</span> }<a name="line.1377"></a>
-<span class="sourceLineNo">1378</span><a name="line.1378"></a>
-<span class="sourceLineNo">1379</span> void setLoads(Map<String,
Deque<BalancerRegionLoad>> l) {<a name="line.1379"></a>
-<span class="sourceLineNo">1380</span> this.loads = l;<a
name="line.1380"></a>
-<span class="sourceLineNo">1381</span> }<a name="line.1381"></a>
-<span class="sourceLineNo">1382</span><a name="line.1382"></a>
-<span class="sourceLineNo">1383</span> @Override<a name="line.1383"></a>
-<span class="sourceLineNo">1384</span> double cost() {<a
name="line.1384"></a>
-<span class="sourceLineNo">1385</span> if (clusterStatus == null || loads
== null) {<a name="line.1385"></a>
-<span class="sourceLineNo">1386</span> return 0;<a name="line.1386"></a>
-<span class="sourceLineNo">1387</span> }<a name="line.1387"></a>
-<span class="sourceLineNo">1388</span><a name="line.1388"></a>
-<span class="sourceLineNo">1389</span> if (stats == null || stats.length
!= cluster.numServers) {<a name="line.1389"></a>
-<span class="sourceLineNo">1390</span> stats = new
double[cluster.numServers];<a name="line.1390"></a>
-<span class="sourceLineNo">1391</span> }<a name="line.1391"></a>
-<span class="sourceLineNo">1392</span><a name="line.1392"></a>
-<span class="sourceLineNo">1393</span> for (int i =0; i <
stats.length; i++) {<a name="line.1393"></a>
-<span class="sourceLineNo">1394</span> //Cost this server has from
RegionLoad<a name="line.1394"></a>
-<span class="sourceLineNo">1395</span> long cost = 0;<a
name="line.1395"></a>
-<span class="sourceLineNo">1396</span><a name="line.1396"></a>
-<span class="sourceLineNo">1397</span> // for every region on this
server get the rl<a name="line.1397"></a>
-<span class="sourceLineNo">1398</span> for(int
regionIndex:cluster.regionsPerServer[i]) {<a name="line.1398"></a>
-<span class="sourceLineNo">1399</span>
Collection<BalancerRegionLoad> regionLoadList =
cluster.regionLoads[regionIndex];<a name="line.1399"></a>
-<span class="sourceLineNo">1400</span><a name="line.1400"></a>
-<span class="sourceLineNo">1401</span> // Now if we found a region
load get the type of cost that was requested.<a name="line.1401"></a>
-<span class="sourceLineNo">1402</span> if (regionLoadList != null)
{<a name="line.1402"></a>
-<span class="sourceLineNo">1403</span> cost +=
getRegionLoadCost(regionLoadList);<a name="line.1403"></a>
-<span class="sourceLineNo">1404</span> }<a name="line.1404"></a>
-<span class="sourceLineNo">1405</span> }<a name="line.1405"></a>
-<span class="sourceLineNo">1406</span><a name="line.1406"></a>
-<span class="sourceLineNo">1407</span> // Add the total cost to the
stats.<a name="line.1407"></a>
-<span class="sourceLineNo">1408</span> stats[i] = cost;<a
name="line.1408"></a>
-<span class="sourceLineNo">1409</span> }<a name="line.1409"></a>
-<span class="sourceLineNo">1410</span><a name="line.1410"></a>
-<span class="sourceLineNo">1411</span> // Now return the scaled cost from
data held in the stats object.<a name="line.1411"></a>
-<span class="sourceLineNo">1412</span> return costFromArray(stats);<a
name="line.1412"></a>
-<span class="sourceLineNo">1413</span> }<a name="line.1413"></a>
-<span class="sourceLineNo">1414</span><a name="line.1414"></a>
-<span class="sourceLineNo">1415</span> protected double
getRegionLoadCost(Collection<BalancerRegionLoad> regionLoadList) {<a
name="line.1415"></a>
-<span class="sourceLineNo">1416</span> double cost = 0;<a
name="line.1416"></a>
-<span class="sourceLineNo">1417</span> for (BalancerRegionLoad rl :
regionLoadList) {<a name="line.1417"></a>
-<span class="sourceLineNo">1418</span> cost += getCostFromRl(rl);<a
name="line.1418"></a>
-<span class="sourceLineNo">1419</span> }<a name="line.1419"></a>
-<span class="sourceLineNo">1420</span> return cost /
regionLoadList.size();<a name="line.1420"></a>
-<span class="sourceLineNo">1421</span> }<a name="line.1421"></a>
-<span class="sourceLineNo">1422</span><a name="line.1422"></a>
-<span class="sourceLineNo">1423</span> protected abstract double
getCostFromRl(BalancerRegionLoad rl);<a name="line.1423"></a>
-<span class="sourceLineNo">1424</span> }<a name="line.1424"></a>
-<span class="sourceLineNo">1425</span><a name="line.1425"></a>
-<span class="sourceLineNo">1426</span> /**<a name="line.1426"></a>
-<span class="sourceLineNo">1427</span> * Class to be used for the subset of
RegionLoad costs that should be treated as rates.<a name="line.1427"></a>
-<span class="sourceLineNo">1428</span> * We do not compare about the actual
rate in requests per second but rather the rate relative<a name="line.1428"></a>
-<span class="sourceLineNo">1429</span> * to the rest of the regions.<a
name="line.1429"></a>
-<span class="sourceLineNo">1430</span> */<a name="line.1430"></a>
-<span class="sourceLineNo">1431</span> abstract static class
CostFromRegionLoadAsRateFunction extends CostFromRegionLoadFunction {<a
name="line.1431"></a>
-<span class="sourceLineNo">1432</span><a name="line.1432"></a>
-<span class="sourceLineNo">1433</span>
CostFromRegionLoadAsRateFunction(Configuration conf) {<a name="line.1433"></a>
-<span class="sourceLineNo">1434</span> super(conf);<a
name="line.1434"></a>
-<span class="sourceLineNo">1435</span> }<a name="line.1435"></a>
-<span class="sourceLineNo">1436</span><a name="line.1436"></a>
-<span class="sourceLineNo">1437</span> @Override<a name="line.1437"></a>
-<span class="sourceLineNo">1438</span> protected double
getRegionLoadCost(Collection<BalancerRegionLoad> regionLoadList) {<a
name="line.1438"></a>
-<span class="sourceLineNo">1439</span> double cost = 0;<a
name="line.1439"></a>
-<span class="sourceLineNo">1440</span> double previous = 0;<a
name="line.1440"></a>
-<span class="sourceLineNo">1441</span> boolean isFirst = true;<a
name="line.1441"></a>
-<span class="sourceLineNo">1442</span> for (BalancerRegionLoad rl :
regionLoadList) {<a name="line.1442"></a>
-<span class="sourceLineNo">1443</span> double current =
getCostFromRl(rl);<a name="line.1443"></a>
-<span class="sourceLineNo">1444</span> if (isFirst) {<a
name="line.1444"></a>
-<span class="sourceLineNo">1445</span> isFirst = false;<a
name="line.1445"></a>
-<span class="sourceLineNo">1446</span> } else {<a name="line.1446"></a>
-<span class="sourceLineNo">1447</span> cost += current - previous;<a
name="line.1447"></a>
-<span class="sourceLineNo">1448</span> }<a name="line.1448"></a>
-<span class="sourceLineNo">1449</span> previous = current;<a
name="line.1449"></a>
-<span class="sourceLineNo">1450</span> }<a name="line.1450"></a>
-<span class="sourceLineNo">1451</span> return Math.max(0, cost /
(regionLoadList.size() - 1));<a name="line.1451"></a>
-<span class="sourceLineNo">1452</span> }<a name="line.1452"></a>
-<span class="sourceLineNo">1453</span> }<a name="line.1453"></a>
-<span class="sourceLineNo">1454</span><a name="line.1454"></a>
-<span class="sourceLineNo">1455</span> /**<a name="line.1455"></a>
-<span class="sourceLineNo">1456</span> * Compute the cost of total number of
read requests The more unbalanced the higher the<a name="line.1456"></a>
-<span class="sourceLineNo">1457</span> * computed cost will be. This uses a
rolling average of regionload.<a name="line.1457"></a>
-<span class="sourceLineNo">1458</span> */<a name="line.1458"></a>
-<span class="sourceLineNo">1459</span><a name="line.1459"></a>
-<span class="sourceLineNo">1460</span> static class ReadRequestCostFunction
extends CostFromRegionLoadAsRateFunction {<a name="line.1460"></a>
-<span class="sourceLineNo">1461</span><a name="line.1461"></a>
-<span class="sourceLineNo">1462</span> private static final String
READ_REQUEST_COST_KEY =<a name="line.1462"></a>
-<span class="sourceLineNo">1463</span>
"hbase.master.balancer.stochastic.readRequestCost";<a name="line.1463"></a>
-<span class="sourceLineNo">1464</span> private static final float
DEFAULT_READ_REQUEST_COST = 5;<a name="line.1464"></a>
-<span class="sourceLineNo">1465</span><a name="line.1465"></a>
-<span class="sourceLineNo">1466</span>
ReadRequestCostFunction(Configuration conf) {<a name="line.1466"></a>
-<span class="sourceLineNo">1467</span> super(conf);<a
name="line.1467"></a>
-<span class="sourceLineNo">1468</span>
this.setMultiplier(conf.getFloat(READ_REQUEST_COST_KEY,
DEFAULT_READ_REQUEST_COST));<a name="line.1468"></a>
-<span class="sourceLineNo">1469</span> }<a name="line.1469"></a>
-<span class="sourceLineNo">1470</span><a name="line.1470"></a>
-<span class="sourceLineNo">1471</span> @Override<a name="line.1471"></a>
-<span class="sourceLineNo">1472</span> protected double
getCostFromRl(BalancerRegionLoad rl) {<a name="line.1472"></a>
-<span class="sourceLineNo">1473</span> return
rl.getReadRequestsCount();<a name="line.1473"></a>
-<span class="sourceLineNo">1474</span> }<a name="line.1474"></a>
-<span class="sourceLineNo">1475</span> }<a name="line.1475"></a>
-<span class="sourceLineNo">1476</span><a name="line.1476"></a>
-<span class="sourceLineNo">1477</span> /**<a name="line.1477"></a>
-<span class="sourceLineNo">1478</span> * Compute the cost of total number of
write requests. The more unbalanced the higher the<a name="line.1478"></a>
-<span class="sourceLineNo">1479</span> * computed cost will be. This uses a
rolling average of regionload.<a name="line.1479"></a>
-<span class="sourceLineNo">1480</span> */<a name="line.1480"></a>
-<span class="sourceLineNo">1481</span> static class WriteRequestCostFunction
extends CostFromRegionLoadAsRateFunction {<a name="line.1481"></a>
-<span class="sourceLineNo">1482</span><a name="line.1482"></a>
-<span class="sourceLineNo">1483</span> private static final String
WRITE_REQUEST_COST_KEY =<a name="line.1483"></a>
-<span class="sourceLineNo">1484</span>
"hbase.master.balancer.stochastic.writeRequestCost";<a name="line.1484"></a>
-<span class="sourceLineNo">1485</span> private static final float
DEFAULT_WRITE_REQUEST_COST = 5;<a name="line.1485"></a>
-<span class="sourceLineNo">1486</span><a name="line.1486"></a>
-<span class="sourceLineNo">1487</span>
WriteRequestCostFunction(Configuration conf) {<a name="line.1487"></a>
-<span class="sourceLineNo">1488</span> super(conf);<a
name="line.1488"></a>
-<span class="sourceLineNo">1489</span>
this.setMultiplier(conf.getFloat(WRITE_REQUEST_COST_KEY,
DEFAULT_WRITE_REQUEST_COST));<a name="line.1489"></a>
-<span class="sourceLineNo">1490</span> }<a name="line.1490"></a>
-<span class="sourceLineNo">1491</span><a name="line.1491"></a>
-<span class="sourceLineNo">1492</span> @Override<a name="line.1492"></a>
-<span class="sourceLineNo">1493</span> protected double
getCostFromRl(BalancerRegionLoad rl) {<a name="line.1493"></a>
-<span class="sourceLineNo">1494</span> return
rl.getWriteRequestsCount();<a name="line.1494"></a>
-<span class="sourceLineNo">1495</span> }<a name="line.1495"></a>
-<span class="sourceLineNo">1496</span> }<a name="line.1496"></a>
-<span class="sourceLineNo">1497</span><a name="line.1497"></a>
-<span class="sourceLineNo">1498</span> /**<a name="line.1498"></a>
-<span class="sourceLineNo">1499</span> * A cost function for region
replicas. We give a very high cost to hosting<a name="line.1499"></a>
-<span class="sourceLineNo">1500</span> * replicas of the same region in the
same host. We do not prevent the case<a name="line.1500"></a>
-<span class="sourceLineNo">1501</span> * though, since if numReplicas >
numRegionServers, we still want to keep the<a name="line.1501"></a>
-<span class="sourceLineNo">1502</span> * replica open.<a
name="line.1502"></a>
-<span class="sourceLineNo">1503</span> */<a name="line.1503"></a>
-<span class="sourceLineNo">1504</span> static class
RegionReplicaHostCostFunction extends CostFunction {<a name="line.1504"></a>
-<span class="sourceLineNo">1505</span> private static final String
REGION_REPLICA_HOST_COST_KEY =<a name="line.1505"></a>
-<span class="sourceLineNo">1506</span>
"hbase.master.balancer.stochastic.regionReplicaHostCostKey";<a
name="line.1506"></a>
-<span class="sourceLineNo">1507</span> private static final float
DEFAULT_REGION_REPLICA_HOST_COST_KEY = 100000;<a name="line.1507"></a>
-<span class="sourceLineNo">1508</span><a name="line.1508"></a>
-<span class="sourceLineNo">1509</span> long maxCost = 0;<a
name="line.1509"></a>
-<span class="sourceLineNo">1510</span> long[] costsPerGroup; // group is
either server, host or rack<a name="line.1510"></a>
-<span class="sourceLineNo">1511</span> int[][]
primariesOfRegionsPerGroup;<a name="line.1511"></a>
-<span class="sourceLineNo">1512</span><a name="line.1512"></a>
-<span class="sourceLineNo">1513</span> public
RegionReplicaHostCostFunction(Configuration conf) {<a name="line.1513"></a>
-<span class="sourceLineNo">1514</span> super(conf);<a
name="line.1514"></a>
-<span class="sourceLineNo">1515</span>
this.setMultiplier(conf.getFloat(REGION_REPLICA_HOST_COST_KEY,<a
name="line.1515"></a>
-<span class="sourceLineNo">1516</span>
DEFAULT_REGION_REPLICA_HOST_COST_KEY));<a name="line.1516"></a>
-<span class="sourceLineNo">1517</span> }<a name="line.1517"></a>
-<span class="sourceLineNo">1518</span><a name="line.1518"></a>
-<span class="sourceLineNo">1519</span> @Override<a name="line.1519"></a>
-<span class="sourceLineNo">1520</span> void init(Cluster cluster) {<a
name="line.1520"></a>
-<span class="sourceLineNo">1521</span> super.init(cluster);<a
name="line.1521"></a>
-<span class="sourceLineNo">1522</span> // max cost is the case where
every region replica is hosted together regardless of host<a
name="line.1522"></a>
-<span class="sourceLineNo">1523</span> maxCost = cluster.numHosts > 1
? getMaxCost(cluster) : 0;<a name="line.1523"></a>
-<span class="sourceLineNo">1524</span> costsPerGroup = new
long[cluster.numHosts];<a name="line.1524"></a>
-<span class="sourceLineNo">1525</span> primariesOfRegionsPerGroup =
cluster.multiServersPerHost // either server based or host based<a
name="line.1525"></a>
-<span class="sourceLineNo">1526</span> ?
cluster.primariesOfRegionsPerHost<a name="line.1526"></a>
-<span class="sourceLineNo">1527</span> :
cluster.primariesOfRegionsPerServer;<a name="line.1527"></a>
-<span class="sourceLineNo">1528</span> for (int i = 0 ; i <
primariesOfRegionsPerGroup.length; i++) {<a name="line.1528"></a>
-<span class="sourceLineNo">1529</span> costsPerGroup[i] =
costPerGroup(primariesOfRegionsPerGroup[i]);<a name="line.1529"></a>
-<span class="sourceLineNo">1530</span> }<a name="line.1530"></a>
-<span class="sourceLineNo">1531</span> }<a name="line.1531"></a>
-<span class="sourceLineNo">1532</span><a name="line.1532"></a>
-<span class="sourceLineNo">1533</span> long getMaxCost(Cluster cluster) {<a
name="line.1533"></a>
-<span class="sourceLineNo">1534</span> if (!cluster.hasRegionReplicas)
{<a name="line.1534"></a>
-<span class="sourceLineNo">1535</span> return 0; // short circuit<a
name="line.1535"></a>
-<span class="sourceLineNo">1536</span> }<a name="line.1536"></a>
-<span class="sourceLineNo">1537</span> // max cost is the case where
every region replica is hosted together regardless of host<a
name="line.1537"></a>
-<span class="sourceLineNo">1538</span> int[] primariesOfRegions = new
int[cluster.numRegions];<a name="line.1538"></a>
-<span class="sourceLineNo">1539</span>
System.arraycopy(cluster.regionIndexToPrimaryIndex, 0, primariesOfRegions, 0,<a
name="line.1539"></a>
-<span class="sourceLineNo">1540</span> cluster.regions.length);<a
name="line.1540"></a>
-<span class="sourceLineNo">1541</span><a name="line.1541"></a>
-<span class="sourceLineNo">1542</span> Arrays.sort(primariesOfRegions);<a
name="line.1542"></a>
-<span class="sourceLineNo">1543</span><a name="line.1543"></a>
-<span class="sourceLineNo">1544</span> // compute numReplicas from the
sorted array<a name="line.1544"></a>
-<span class="sourceLineNo">1545</span> return
costPerGroup(primariesOfRegions);<a name="line.1545"></a>
-<span class="sourceLineNo">1546</span> }<a name="line.1546"></a>
-<span class="sourceLineNo">1547</span><a name="line.1547"></a>
-<span class="sourceLineNo">1548</span> @Override<a name="line.1548"></a>
-<span class="sourceLineNo">1549</span> boolean isNeeded() {<a
name="line.1549"></a>
-<span class="sourceLineNo">1550</span> return
cluster.hasRegionReplicas;<a name="line.1550"></a>
-<span class="sourceLineNo">1551</span> }<a name="line.1551"></a>
-<span class="sourceLineNo">1552</span><a name="line.1552"></a>
-<span class="sourceLineNo">1553</span> @Override<a name="line.1553"></a>
-<span class="sourceLineNo">1554</span> double cost() {<a
name="line.1554"></a>
-<span class="sourceLineNo">1555</span> if (maxCost <= 0) {<a
name="line.1555"></a>
-<span class="sourceLineNo">1556</span> return 0;<a name="line.1556"></a>
-<span class="sourceLineNo">1557</span> }<a name="line.1557"></a>
-<span class="sourceLineNo">1558</span><a name="line.1558"></a>
-<span class="sourceLineNo">1559</span> long totalCost = 0;<a
name="line.1559"></a>
-<span class="sourceLineNo">1560</span> for (int i = 0 ; i <
costsPerGroup.length; i++) {<a name="line.1560"></a>
-<span class="sourceLineNo">1561</span> totalCost += costsPerGroup[i];<a
name="line.1561"></a>
-<span class="sourceLineNo">1562</span> }<a name="line.1562"></a>
-<span class="sourceLineNo">1563</span> return scale(0, maxCost,
totalCost);<a name="line.1563"></a>
-<span class="sourceLineNo">1564</span> }<a name="line.1564"></a>
-<span class="sourceLineNo">1565</span><a name="line.1565"></a>
-<span class="sourceLineNo">1566</span> /**<a name="line.1566"></a>
-<span class="sourceLineNo">1567</span> * For each primary region, it
computes the total number of replicas in the array (numReplicas)<a
name="line.1567"></a>
-<span class="sourceLineNo">1568</span> * and returns a sum of
numReplicas-1 squared. For example, if the server hosts<a name="line.1568"></a>
-<span class="sourceLineNo">1569</span> * regions a, b, c, d, e, f where a
and b are same replicas, and c,d,e are same replicas, it<a name="line.1569"></a>
-<span class="sourceLineNo">1570</span> * returns (2-1) * (2-1) + (3-1) *
(3-1) + (1-1) * (1-1).<a name="line.1570"></a>
-<span class="sourceLineNo">1571</span> * @param primariesOfRegions a
sorted array of primary regions ids for the regions hosted<a
name="line.1571"></a>
-<span class="sourceLineNo">1572</span> * @return a sum of numReplicas-1
squared for each primary region in the group.<a name="line.1572"></a>
-<span class="sourceLineNo">1573</span> */<a name="line.1573"></a>
-<span class="sourceLineNo">1574</span> protected long costPerGroup(int[]
primariesOfRegions) {<a name="line.1574"></a>
-<span class="sourceLineNo">1575</span> long cost = 0;<a
name="line.1575"></a>
-<span class="sourceLineNo">1576</span> int currentPrimary = -1;<a
name="line.1576"></a>
-<span class="sourceLineNo">1577</span> int currentPrimaryIndex = -1;<a
name="line.1577"></a>
-<span class="sourceLineNo">1578</span> // primariesOfRegions is a sorted
array of primary ids of regions. Replicas of regions<a name="line.1578"></a>
-<span class="sourceLineNo">1579</span> // sharing the same primary will
have consecutive numbers in the array.<a name="line.1579"></a>
-<span class="sourceLineNo">1580</span> for (int j = 0 ; j <=
primariesOfRegions.length; j++) {<a name="line.1580"></a>
-<span class="sourceLineNo">1581</span> int primary = j <
primariesOfRegions.length ? primariesOfRegions[j] : -1;<a name="line.1581"></a>
-<span class="sourceLineNo">1582</span> if (primary != currentPrimary) {
// we see a new primary<a name="line.1582"></a>
-<span class="sourceLineNo">1583</span> int numReplicas = j -
currentPrimaryIndex;<a name="line.1583"></a>
-<span class="sourceLineNo">1584</span> // square the cost<a
name="line.1584"></a>
-<span class="sourceLineNo">1585</span> if (numReplicas > 1) { //
means consecutive primaries, indicating co-location<a name="line.1585"></a>
-<span class="sourceLineNo">1586</span> cost += (numReplicas - 1) *
(numReplicas - 1);<a name="line.1586"></a>
-<span class="sourceLineNo">1587</span> }<a name="line.1587"></a>
-<span class="sourceLineNo">1588</span> currentPrimary = primary;<a
name="line.1588"></a>
-<span class="sourceLineNo">1589</span> currentPrimaryIndex = j;<a
name="line.1589"></a>
-<span class="sourceLineNo">1590</span> }<a name="line.1590"></a>
-<span class="sourceLineNo">1591</span> }<a name="line.1591"></a>
-<span class="sourceLineNo">1592</span><a name="line.1592"></a>
-<span class="sourceLineNo">1593</span> return cost;<a
name="line.1593"></a>
-<span class="sourceLineNo">1594</span> }<a name="line.1594"></a>
-<span class="sourceLineNo">1595</span><a name="line.1595"></a>
-<span class="sourceLineNo">1596</span> @Override<a name="line.1596"></a>
-<span class="sourceLineNo">1597</span> protected void regionMoved(int
region, int oldServer, int newServer) {<a name="line.1597"></a>
-<span class="sourceLineNo">1598</span> if (maxCost <= 0) {<a
name="line.1598"></a>
-<span class="sourceLineNo">1599</span> return; // no need to compute<a
name="line.1599"></a>
-<span class="sourceLineNo">1600</span> }<a name="line.1600"></a>
-<span class="sourceLineNo">1601</span> if (cluster.multiServersPerHost)
{<a name="line.1601"></a>
-<span class="sourceLineNo">1602</span> int oldHost =
cluster.serverIndexToHostIndex[oldServer];<a name="line.1602"></a>
-<span class="sourceLineNo">1603</span> int newHost =
cluster.serverIndexToHostIndex[newServer];<a name="line.1603"></a>
-<span class="sourceLineNo">1604</span> if (newHost != oldHost) {<a
name="line.1604"></a>
-<span class="sourceLineNo">1605</span> costsPerGroup[oldHost] =
costPerGroup(cluster.primariesOfRegionsPerHost[oldHost]);<a
name="line.1605"></a>
-<span class="sourceLineNo">1606</span> costsPerGroup[newHost] =
costPerGroup(cluster.primariesOfRegionsPerHost[newHost]);<a
name="line.1606"></a>
-<span class="sourceLineNo">1607</span> }<a name="line.1607"></a>
-<span class="sourceLineNo">1608</span> } else {<a name="line.1608"></a>
-<span class="sourceLineNo">1609</span> costsPerGroup[oldServer] =
costPerGroup(cluster.primariesOfRegionsPerServer[oldServer]);<a
name="line.1609"></a>
-<span class="sourceLineNo">1610</span> costsPerGroup[newServer] =
costPerGroup(cluster.primariesOfRegionsPerServer[newServer]);<a
name="line.1610"></a>
-<span class="sourceLineNo">1611</span> }<a name="line.1611"></a>
-<span class="sourceLineNo">1612</span> }<a name="line.1612"></a>
-<span class="sourceLineNo">1613</span> }<a name="line.1613"></a>
-<span class="sourceLineNo">1614</span><a name="line.1614"></a>
-<span class="sourceLineNo">1615</span> /**<a name="line.1615"></a>
-<span class="sourceLineNo">1616</span> * A cost function for region replicas
for the rack distribution. We give a relatively high<a name="line.1616"></a>
-<span class="sourceLineNo">1617</span> * cost to hosting replicas of the
same region in the same rack. We do not prevent the case<a name="line.1617"></a>
-<span class="sourceLineNo">1618</span> * though.<a name="line.1618"></a>
-<span class="sourceLineNo">1619</span> */<a name="line.1619"></a>
-<span class="sourceLineNo">1620</span> static class
RegionReplicaRackCostFunction extends RegionReplicaHostCostFunction {<a
name="line.1620"></a>
-<span class="sourceLineNo">1621</span> private static final String
REGION_REPLICA_RACK_COST_KEY =<a name="line.1621"></a>
-<span class="sourceLineNo">1622</span>
"hbase.master.balancer.stochastic.regionReplicaRackCostKey";<a
name="line.1622"></a>
-<span class="sourceLineNo">1623</span> private static final float
DEFAULT_REGION_REPLICA_RACK_COST_KEY = 10000;<a name="line.1623"></a>
-<span class="sourceLineNo">1624</span><a name="line.1624"></a>
-<span class="sourceLineNo">1625</span> public
RegionReplicaRackCostFunction(Configuration conf) {<a name="line.1625"></a>
-<span class="sourceLineNo">1626</span> super(conf);<a
name="line.1626"></a>
-<span class="sourceLineNo">1627</span>
this.setMultiplier(conf.getFloat(REGION_REPLICA_RACK_COST_KEY,<a
name="line.1627"></a>
-<span class="sourceLineNo">1628</span>
DEFAULT_REGION_REPLICA_RACK_COST_KEY));<a name="line.1628"></a>
-<span class="sourceLineNo">1629</span> }<a name="line.1629"></a>
-<span class="sourceLineNo">1630</span><a name="line.1630"></a>
-<span class="sourceLineNo">1631</span> @Override<a name="line.1631"></a>
-<span class="sourceLineNo">1632</span> void init(Cluster cluster) {<a
name="line.1632"></a>
-<span class="sourceLineNo">1633</span> this.cluster = cluster;<a
name="line.1633"></a>
-<span class="sourceLineNo">1634</span> if (cluster.numRacks <= 1) {<a
name="line.1634"></a>
-<span class="sourceLineNo">1635</span> maxCost = 0;<a
name="line.1635"></a>
-<span class="sourceLineNo">1636</span> return; // disabled for 1 rack<a
name="line.1636"></a>
-<span class="sourceLineNo">1637</span> }<a name="line.1637"></a>
-<span class="sourceLineNo">1638</span> // max cost is the case where
every region replica is hosted together regardless of rack<a
name="line.1638"></a>
-<span class="sourceLineNo">1639</span> maxCost = getMaxCost(cluster);<a
name="line.1639"></a>
-<span class="sourceLineNo">1640</span> costsPerGroup = new
long[cluster.numRacks];<a name="line.1640"></a>
-<span class="sourceLineNo">1641</span> for (int i = 0 ; i <
cluster.primariesOfRegionsPerRack.length; i++) {<a name="line.1641"></a>
-<span class="sourceLineNo">1642</span> costsPerGroup[i] =
costPerGroup(cluster.primariesOfRegionsPerRack[i]);<a name="line.1642"></a>
-<span class="sourceLineNo">1643</span> }<a name="line.1643"></a>
-<span class="sourceLineNo">1644</span> }<a name="line.1644"></a>
-<span class="sourceLineNo">1645</span><a name="line.1645"></a>
-<span class="sourceLineNo">1646</span> @Override<a name="line.1646"></a>
-<span class="sourceLineNo">1647</span> protected void regionMoved(int
region, int oldServer, int newServer) {<a name="line.1647"></a>
-<span class="sourceLineNo">1648</span> if (maxCost <= 0) {<a
name="line.1648"></a>
-<span class="sourceLineNo">1649</span> return; // no need to compute<a
name="line.1649"></a>
-<span class="sourceLineNo">1650</span> }<a name="line.1650"></a>
-<span class="sourceLineNo">1651</span> int oldRack =
cluster.serverIndexToRackIndex[oldServer];<a name="line.1651"></a>
-<span class="sourceLineNo">1652</span> int newRack =
cluster.serverIndexToRackIndex[newServer];<a name="line.1652"></a>
-<span class="sourceLineNo">1653</span> if (newRack != oldRack) {<a
name="line.1653"></a>
-<span class="sourceLineNo">1654</span> costsPerGroup[oldRack] =
costPerGroup(cluster.primariesOfRegionsPerRack[oldRack]);<a
name="line.1654"></a>
-<span class="sourceLineNo">1655</span> costsPerGroup[newRack] =
costPerGroup(cluster.primariesOfRegionsPerRack[newRack]);<a
name="line.1655"></a>
-<span class="sourceLineNo">1656</span> }<a name="line.1656"></a>
-<span class="sourceLineNo">1657</span> }<a name="line.1657"></a>
-<span class="sourceLineNo">1658</span> }<a name="line.1658"></a>
-<span class="sourceLineNo">1659</span><a name="line.1659"></a>
-<span class="sourceLineNo">1660</span> /**<a name="line.1660"></a>
-<span class="sourceLineNo">1661</span> * Compute the cost of total memstore
size. The more unbalanced the higher the<a name="line.1661"></a>
-<span class="sourceLineNo">1662</span> * computed cost will be. This uses a
rolling average of regionload.<a name="line.1662"></a>
-<span class="sourceLineNo">1663</span> */<a name="line.1663"></a>
-<span class="sourceLineNo">1664</span> static class MemstoreSizeCostFunction
extends CostFromRegionLoadAsRateFunction {<a name="line.1664"></a>
-<span class="sourceLineNo">1665</span><a name="line.1665"></a>
-<span class="sourceLineNo">1666</span> private static final String
MEMSTORE_SIZE_COST_KEY =<a name="line.1666"></a>
-<span class="sourceLineNo">1667</span>
"hbase.master.balancer.stochastic.memstoreSizeCost";<a name="line.1667"></a>
-<span class="sourceLineNo">1668</span> private static final float
DEFAULT_MEMSTORE_SIZE_COST = 5;<a name="line.1668"></a>
-<span class="sourceLineNo">1669</span><a name="line.1669"></a>
-<span class="sourceLineNo">1670</span>
MemstoreSizeCostFunction(Configuration conf) {<a name="line.1670"></a>
-<span class="sourceLineNo">1671</span> super(conf);<a
name="line.1671"></a>
-<span class="sourceLineNo">1672</span>
this.setMultiplier(conf.getFloat(MEMSTORE_SIZE_COST_KEY,
DEFAULT_MEMSTORE_SIZE_COST));<a name="line.1672"></a>
-<span class="sourceLineNo">1673</span> }<a name="line.1673"></a>
-<span class="sourceLineNo">1674</span><a name="line.1674"></a>
-<span class="sourceLineNo">1675</span> @Override<a name="line.1675"></a>
-<span class="sourceLineNo">1676</span> protected double
getCostFromRl(BalancerRegionLoad rl) {<a name="line.1676"></a>
-<span class="sourceLineNo">1677</span> return rl.getMemStoreSizeMB();<a
name="line.1677"></a>
-<span class="sourceLineNo">1678</span> }<a name="line.1678"></a>
-<span class="sourceLineNo">1679</span> }<a name="line.1679"></a>
-<span class="sourceLineNo">1680</span> /**<a name="line.1680"></a>
-<span class="sourceLineNo">1681</span> * Compute the cost of total open
storefiles size. The more unbalanced the higher the<a name="line.1681"></a>
-<span class="sourceLineNo">1682</span> * computed cost will be. This uses a
rolling average of regionload.<a name="line.1682"></a>
-<span class="sourceLineNo">1683</span> */<a name="line.1683"></a>
-<span class="sourceLineNo">1684</span> static class StoreFileCostFunction
extends CostFromRegionLoadFunction {<a name="line.1684"></a>
-<span class="sourceLineNo">1685</span><a name="line.1685"></a>
-<span class="sourceLineNo">1686</span> private static final String
STOREFILE_SIZE_COST_KEY =<a name="line.1686"></a>
-<span class="sourceLineNo">1687</span>
"hbase.master.balancer.stochastic.storefileSizeCost";<a name="line.1687"></a>
-<span class="sourceLineNo">1688</span> private static final float
DEFAULT_STOREFILE_SIZE_COST = 5;<a name="line.1688"></a>
-<span class="sourceLineNo">1689</span><a name="line.1689"></a>
-<span class="sourceLineNo">1690</span> StoreFileCostFunction(Configuration
conf) {<a name="line.1690"></a>
-<span class="sourceLineNo">1691</span> super(conf);<a
name="line.1691"></a>
-<span class="sourceLineNo">1692</span>
this.setMultiplier(conf.getFloat(STOREFILE_SIZE_COST_KEY,
DEFAULT_STOREFILE_SIZE_COST));<a name="line.1692"></a>
-<span class="sourceLineNo">1693</span> }<a name="line.1693"></a>
-<span class="sourceLineNo">1694</span><a name="line.1694"></a>
-<span class="sourceLineNo">1695</span> @Override<a name="line.1695"></a>
-<span class="sourceLineNo">1696</span> protected double
getCostFromRl(BalancerRegionLoad rl) {<a name="line.1696"></a>
-<span class="sourceLineNo">1697</span> return rl.getStorefileSizeMB();<a
name="line.1697"></a>
-<span class="sourceLineNo">1698</span> }<a name="line.1698"></a>
-<span class="sourceLineNo">1699</span> }<a name="line.1699"></a>
-<span class="sourceLineNo">1700</span><a name="line.1700"></a>
-<span class="sourceLineNo">1701</span> /**<a name="line.1701"></a>
-<span class="sourceLineNo">1702</span> * A helper function to compose the
attribute name from tablename and costfunction name<a name="line.1702"></a>
-<span class="sourceLineNo">1703</span> */<a name="line.1703"></a>
-<span class="sourceLineNo">1704</span> public static String
composeAttributeName(String tableName, String costFunctionName) {<a
name="line.1704"></a>
-<span class="sourceLineNo">1705</span> return tableName +
TABLE_FUNCTION_SEP + costFunctionName;<a name="line.1705"></a>
-<span class="sourceLineNo">1706</span> }<a name="line.1706"></a>
-<span class="sourceLineNo">1707</span>}<a name="line.1707"></a>
+<span class="sourceLineNo">1289</span> // is compared to how good it
could be. If bestLocality is 0, assume locality is 100<a name="line.1289"></a>
+<span class="sourceLineNo">1290</span> // (and the cost is 0)<a
name="line.1290"></a>
+<span class="sourceLineNo">1291</span> locality = bestLocality == 0 ? 1 :
locality / bestLocality;<a name="line.1291"></a>
+<span class="sourceLineNo">1292</span> }<a name="line.1292"></a>
+<span class="sourceLineNo">1293</span><a name="line.1293"></a>
+<span class="sourceLineNo">1294</span> @Override<a name="line.1294"></a>
+<span class="sourceLineNo">1295</span> protected void regionMoved(int
region, int oldServer, int newServer) {<a name="line.1295"></a>
+<span class="sourceLineNo">1296</span> int oldEntity = type ==
LocalityType.SERVER ? oldServer : cluster.serverIndexToRackIndex[oldServer];<a
name="line.1296"></a>
+<span class="sourceLineNo">1297</span> int newEntity = type ==
LocalityType.SERVER ? newServer : cluster.serverIndexToRackIndex[newServer];<a
name="line.1297"></a>
+<span class="sourceLineNo">1298</span> if (this.services == null) {<a
name="line.1298"></a>
+<span class="sourceLineNo">1299</span> return;<a name="line.1299"></a>
+<span class="sourceLineNo">1300</span> }<a name="line.1300"></a>
+<span class="sourceLineNo">1301</span> double localityDelta =
getWeightedLocality(region, newEntity) - getWeightedLocality(region,
oldEntity);<a name="line.1301"></a>
+<span class="sourceLineNo">1302</span> double normalizedDelta =
bestLocality == 0 ? 0.0 : localityDelta / bestLocality;<a name="line.1302"></a>
+<span class="sourceLineNo">1303</span> locality += normalizedDelta;<a
name="line.1303"></a>
+<span class="sourceLineNo">1304</span> }<a name="line.1304"></a>
+<span class="sourceLineNo">1305</span><a name="line.1305"></a>
+<span class="sourceLineNo">1306</span> @Override<a name="line.1306"></a>
+<span class="sourceLineNo">1307</span> double cost() {<a
name="line.1307"></a>
+<span class="sourceLineNo">1308</span> return 1 - locality;<a
name="line.1308"></a>
+<span class="sourceLineNo">1309</span> }<a name="line.1309"></a>
+<span class="sourceLineNo">1310</span><a name="line.1310"></a>
+<span class="sourceLineNo">1311</span> private int
getMostLocalEntityForRegion(int region) {<a name="line.1311"></a>
+<span class="sourceLineNo">1312</span> return
cluster.getOrComputeRegionsToMostLocalEntities(type)[region];<a
name="line.1312"></a>
+<span class="sourceLineNo">1313</span> }<a name="line.1313"></a>
+<span class="sourceLineNo">1314</span><a name="line.1314"></a>
+<span class="sourceLineNo">1315</span> private double
getWeightedLocality(int region, int entity) {<a name="line.1315"></a>
+<span class="sourceLineNo">1316</span> return
cluster.getOrComputeWeightedLocality(region, entity, type);<a
name="line.1316"></a>
+<span class="sourceLineNo">1317</span> }<a name="line.1317"></a>
+<span class="sourceLineNo">1318</span><a name="line.1318"></a>
+<span class="sourceLineNo">1319</span> }<a name="line.1319"></a>
+<span class="sourceLineNo">1320</span><a name="line.1320"></a>
+<span class="sourceLineNo">1321</span> static class
ServerLocalityCostFunction extends LocalityBasedCostFunction {<a
name="line.1321"></a>
+<span class="sourceLineNo">1322</span><a name="line.1322"></a>
+<span class="sourceLineNo">1323</span> private static final String
LOCALITY_COST_KEY = "hbase.master.balancer.stochastic.localityCost";<a
name="line.1323"></a>
+<span class="sourceLineNo">1324</span> private static final float
DEFAULT_LOCALITY_COST = 25;<a name="line.1324"></a>
+<span class="sourceLineNo">1325</span><a name="line.1325"></a>
+<span class="sourceLineNo">1326</span>
ServerLocalityCostFunction(Configuration conf, MasterServices srv) {<a
name="line.1326"></a>
+<span class="sourceLineNo">1327</span> super(<a name="line.1327"></a>
+<span class="sourceLineNo">1328</span> conf,<a name="line.1328"></a>
+<span class="sourceLineNo">1329</span> srv,<a name="line.1329"></a>
+<span class="sourceLineNo">1330</span> LocalityType.SERVER,<a
name="line.1330"></a>
+<span class="sourceLineNo">1331</span> LOCALITY_COST_KEY,<a
name="line.1331"></a>
+<span class="sourceLineNo">1332</span> DEFAULT_LOCALITY_COST<a
name="line.1332"></a>
+<span class="sourceLineNo">1333</span> );<a name="line.1333"></a>
+<span class="sourceLineNo">1334</span> }<a name="line.1334"></a>
+<span class="sourceLineNo">1335</span><a name="line.1335"></a>
+<span class="sourceLineNo">1336</span> @Override<a name="line.1336"></a>
+<span class="sourceLineNo">1337</span> int regionIndexToEntityIndex(int
region) {<a name="line.1337"></a>
+<span class="sourceLineNo">1338</span> return
cluster.regionIndexToServerIndex[region];<a name="line.1338"></a>
+<span class="sourceLineNo">1339</span> }<a name="line.1339"></a>
+<span class="sourceLineNo">1340</span> }<a name="line.1340"></a>
+<span class="sourceLineNo">1341</span><a name="line.1341"></a>
+<span class="sourceLineNo">1342</span> static class RackLocalityCostFunction
extends LocalityBasedCostFunction {<a name="line.1342"></a>
+<span class="sourceLineNo">1343</span><a name="line.1343"></a>
+<span class="sourceLineNo">1344</span> private static final String
RACK_LOCALITY_COST_KEY = "hbase.master.balancer.stochastic.rackLocalityCost";<a
name="line.1344"></a>
+<span class="sourceLineNo">1345</span> private static final float
DEFAULT_RACK_LOCALITY_COST = 15;<a name="line.1345"></a>
+<span class="sourceLineNo">1346</span><a name="line.1346"></a>
+<span class="sourceLineNo">1347</span> public
RackLocalityCostFunction(Configuration conf, MasterServices services) {<a
name="line.1347"></a>
+<span class="sourceLineNo">1348</span> super(<a name="line.1348"></a>
+<span class="sourceLineNo">1349</span> conf,<a name="line.1349"></a>
+<span class="sourceLineNo">1350</span> services,<a
name="line.1350"></a>
+<span class="sourceLineNo">1351</span> LocalityType.RACK,<a
name="line.1351"></a>
+<span class="sourceLineNo">1352</span> RACK_LOCALITY_COST_KEY,<a
name="line.1352"></a>
+<span class="sourceLineNo">1353</span> DEFAULT_RACK_LOCALITY_COST<a
name="line.1353"></a>
+<span class="sourceLineNo">1354</span> );<a name="line.1354"></a>
+<span class="sourceLineNo">1355</span> }<a name="line.1355"></a>
+<span class="sourceLineNo">1356</span><a name="line.1356"></a>
+<span class="sourceLineNo">1357</span> @Override<a name="line.1357"></a>
+<span class="sourceLineNo">1358</span> int regionIndexToEntityIndex(int
region) {<a name="line.1358"></a>
+<span class="sourceLineNo">1359</span> return
cluster.getRackForRegion(region);<a name="line.1359"></a>
+<span class="sourceLineNo">1360</span> }<a name="line.1360"></a>
+<span class="sourceLineNo">1361</span> }<a name="line.1361"></a>
+<span class="sourceLineNo">1362</span><a name="line.1362"></a>
+<span class="sourceLineNo">1363</span> /**<a name="line.1363"></a>
+<span class="sourceLineNo">1364</span> * Base class the allows writing costs
functions from rolling average of some<a name="line.1364"></a>
+<span class="sourceLineNo">1365</span> * number from RegionLoad.<a
name="line.1365"></a>
+<span class="sourceLineNo">1366</span> */<a name="line.1366"></a>
+<span class="sourceLineNo">1367</span> abstract static class
CostFromRegionLoadFunction extends CostFunction {<a name="line.1367"></a>
+<span class="sourceLineNo">1368</span><a name="line.1368"></a>
+<span class="sourceLineNo">1369</span> private ClusterStatus clusterStatus
= null;<a name="line.1369"></a>
+<span class="sourceLineNo">1370</span> private Map<String,
Deque<BalancerRegionLoad>> loads = null;<a name="line.1370"></a>
+<span class="sourceLineNo">1371</span> private double[] stats = null;<a
name="line.1371"></a>
+<span class="sourceLineNo">1372</span>
CostFromRegionLoadFunction(Configuration conf) {<a name="line.1372"></a>
+<span class="sourceLineNo">1373</span> super(conf);<a
name="line.1373"></a>
+<span class="sourceLineNo">1374</span> }<a name="line.1374"></a>
+<span class="sourceLineNo">1375</span><a name="line.1375"></a>
+<span class="sourceLineNo">1376</span> void setClusterStatus(ClusterStatus
status) {<a name="line.1376"></a>
+<span class="sourceLineNo">1377</span> this.clusterStatus = status;<a
name="line.1377"></a>
+<span class="sourceLineNo">1378</span> }<a name="line.1378"></a>
+<span class="sourceLineNo">1379</span><a name="line.1379"></a>
+<span class="sourceLineNo">1380</span> void setLoads(Map<String,
Deque<BalancerRegionLoad>> l) {<a name="line.1380"></a>
+<span class="sourceLineNo">1381</span> this.loads = l;<a
name="line.1381"></a>
+<span class="sourceLineNo">1382</span> }<a name="line.1382"></a>
+<span class="sourceLineNo">1383</span><a name="line.1383"></a>
+<span class="sourceLineNo">1384</span> @Override<a name="line.1384"></a>
+<span class="sourceLineNo">1385</span> double cost() {<a
name="line.1385"></a>
+<span class="sourceLineNo">1386</span> if (clusterStatus == null || loads
== null) {<a name="line.1386"></a>
+<span class="sourceLineNo">1387</span> return 0;<a name="line.1387"></a>
+<span class="sourceLineNo">1388</span> }<a name="line.1388"></a>
+<span class="sourceLineNo">1389</span><a name="line.1389"></a>
+<span class="sourceLineNo">1390</span> if (stats == null || stats.length
!= cluster.numServers) {<a name="line.1390"></a>
+<span class="sourceLineNo">1391</span> stats = new
double[cluster.numServers];<a name="line.1391"></a>
+<span class="sourceLineNo">1392</span> }<a name="line.1392"></a>
+<span class="sourceLineNo">1393</span><a name="line.1393"></a>
+<span class="sourceLineNo">1394</span> for (int i =0; i <
stats.length; i++) {<a name="line.1394"></a>
+<span class="sourceLineNo">1395</span> //Cost this server has from
RegionLoad<a name="line.1395"></a>
+<span class="sourceLineNo">1396</span> long cost = 0;<a
name="line.1396"></a>
+<span class="sourceLineNo">1397</span><a name="line.1397"></a>
+<span class="sourceLineNo">1398</span> // for every region on this
server get the rl<a name="line.1398"></a>
+<span class="sourceLineNo">1399</span> for(int
regionIndex:cluster.regionsPerServer[i]) {<a name="line.1399"></a>
+<span class="sourceLineNo">1400</span>
Collection<BalancerRegionLoad> regionLoadList =
cluster.regionLoads[regionIndex];<a name="line.1400"></a>
+<span class="sourceLineNo">1401</span><a name="line.1401"></a>
+<span class="sourceLineNo">1402</span> // Now if we found a region
load get the type of cost that was requested.<a name="line.1402"></a>
+<span class="sourceLineNo">1403</span> if (regionLoadList != null)
{<a name="line.1403"></a>
+<span class="sourceLineNo">1404</span> cost +=
getRegionLoadCost(regionLoadList);<a name="line.1404"></a>
+<span class="sourceLineNo">1405</span> }<a name="line.1405"></a>
+<span class="sourceLineNo">1406</span> }<a name="line.1406"></a>
+<span class="sourceLineNo">1407</span><a name="line.1407"></a>
+<span class="sourceLineNo">1408</span> // Add the total cost to the
stats.<a name="line.1408"></a>
+<span class="sourceLineNo">1409</span> stats[i] = cost;<a
name="line.1409"></a>
+<span class="sourceLineNo">1410</span> }<a name="line.1410"></a>
+<span class="sourceLineNo">1411</span><a name="line.1411"></a>
+<span class="sourceLineNo">1412</span> // Now return the scaled cost from
data held in the stats object.<a name="line.1412"></a>
+<span class="sourceLineNo">1413</span> return costFromArray(stats);<a
name="line.1413"></a>
+<span class="sourceLineNo">1414</span> }<a name="line.1414"></a>
+<span class="sourceLineNo">1415</span><a name="line.1415"></a>
+<span class="sourceLineNo">1416</span> protected double
getRegionLoadCost(Collection<BalancerRegionLoad> regionLoadList) {<a
name="line.1416"></a>
+<span class="sourceLineNo">1417</span> double cost = 0;<a
name="line.1417"></a>
+<span class="sourceLineNo">1418</span> for (BalancerRegionLoad rl :
regionLoadList) {<a name="line.1418"></a>
+<span class="sourceLineNo">1419</span> cost += getCostFromRl(rl);<a
name="line.1419"></a>
+<span class="sourceLineNo">1420</span> }<a name="line.1420"></a>
+<span class="sourceLineNo">1421</span> return cost /
regionLoadList.size();<a name="line.1421"></a>
+<span class="sourceLineNo">1422</span> }<a name="line.1422"></a>
+<span class="sourceLineNo">1423</span><a name="line.1423"></a>
+<span class="sourceLineNo">1424</span> protected abstract double
getCostFromRl(BalancerRegionLoad rl);<a name="line.1424"></a>
+<span class="sourceLineNo">1425</span> }<a name="line.1425"></a>
+<span class="sourceLineNo">1426</span><a name="line.1426"></a>
+<span class="sourceLineNo">1427</span> /**<a name="line.1427"></a>
+<span class="sourceLineNo">1428</span> * Class to be used for the subset of
RegionLoad costs that should be treated as rates.<a name="line.1428"></a>
+<span class="sourceLineNo">1429</span> * We do not compare about the actual
rate in requests per second but rather the rate relative<a name="line.1429"></a>
+<span class="sourceLineNo">1430</span> * to the rest of the regions.<a
name="line.1430"></a>
+<span class="sourceLineNo">1431</span> */<a name="line.1431"></a>
+<span class="sourceLineNo">1432</span> abstract static class
CostFromRegionLoadAsRateFunction extends CostFromRegionLoadFunction {<a
name="line.1432"></a>
+<span class="sourceLineNo">1433</span><a name="line.1433"></a>
+<span class="sourceLineNo">1434</span>
CostFromRegionLoadAsRateFunction(Configuration conf) {<a name="line.1434"></a>
+<span class="sourceLineNo">1435</span> super(conf);<a
name="line.1435"></a>
+<span class="sourceLineNo">1436</span> }<a name="line.1436"></a>
+<span class="sourceLineNo">1437</span><a name="line.1437"></a>
+<span class="sourceLineNo">1438</span> @Override<a name="line.1438"></a>
+<span class="sourceLineNo">1439</span> protected double
getRegionLoadCost(Collection<BalancerRegionLoad> regionLoadList) {<a
name="line.1439"></a>
+<span class="sourceLineNo">1440</span> double cost = 0;<a
name="line.1440"></a>
+<span class="sourceLineNo">1441</span> double previous = 0;<a
name="line.1441"></a>
+<span class="sourceLineNo">1442</span> boolean isFirst = true;<a
name="line.1442"></a>
+<span class="sourceLineNo">1443</span> for (BalancerRegionLoad rl :
regionLoadList) {<a name="line.1443"></a>
+<span class="sourceLineNo">1444</span> double current =
getCostFromRl(rl);<a name="line.1444"></a>
+<span class="sourceLineNo">1445</span> if (isFirst) {<a
name="line.1445"></a>
+<span class="sourceLineNo">1446</span> isFirst = false;<a
name="line.1446"></a>
+<span class="sourceLineNo">1447</span> } else {<a name="line.1447"></a>
+<span class="sourceLineNo">1448</span> cost += current - previous;<a
name="line.1448"></a>
+<span class="sourceLineNo">1449</span> }<a name="line.1449"></a>
+<span class="sourceLineNo">1450</span> previous = current;<a
name="line.1450"></a>
+<span class="sourceLineNo">1451</span> }<a name="line.1451"></a>
+<span class="sourceLineNo">1452</span> return Math.max(0, cost /
(regionLoadList.size() - 1));<a name="line.1452"></a>
+<span class="sourceLineNo">1453</span> }<a name="line.1453"></a>
+<span class="sourceLineNo">1454</span> }<a name="line.1454"></a>
+<span class="sourceLineNo">1455</span><a name="line.1455"></a>
+<span class="sourceLineNo">1456</span> /**<a name="line.1456"></a>
+<span class="sourceLineNo">1457</span> * Compute the cost of total number of
read requests The more unbalanced the higher the<a name="line.1457"></a>
+<span class="sourceLineNo">1458</span> * computed cost will be. This uses a
rolling average of regionload.<a name="line.1458"></a>
+<span class="sourceLineNo">1459</span> */<a name="line.1459"></a>
+<span class="sourceLineNo">1460</span><a name="line.1460"></a>
+<span class="sourceLineNo">1461</span> static class ReadRequestCostFunction
extends CostFromRegionLoadAsRateFunction {<a name="line.1461"></a>
+<span class="sourceLineNo">1462</span><a name="line.1462"></a>
+<span class="sourceLineNo">1463</span> private static final String
READ_REQUEST_COST_KEY =<a name="line.1463"></a>
+<span class="sourceLineNo">1464</span>
"hbase.master.balancer.stochastic.readRequestCost";<a name="line.1464"></a>
+<span class="sourceLineNo">1465</span> private static final float
DEFAULT_READ_REQUEST_COST = 5;<a name="line.1465"></a>
+<span class="sourceLineNo">1466</span><a name="line.1466"></a>
+<span class="sourceLineNo">1467</span>
ReadRequestCostFunction(Configuration conf) {<a name="line.1467"></a>
+<span class="sourceLineNo">1468</span> super(conf);<a
name="line.1468"></a>
+<span class="sourceLineNo">1469</span>
this.setMultiplier(conf.getFloat(READ_REQUEST_COST_KEY,
DEFAULT_READ_REQUEST_COST));<a name="line.1469"></a>
+<span class="sourceLineNo">1470</span> }<a name="line.1470"></a>
+<span class="sourceLineNo">1471</span><a name="line.1471"></a>
+<span class="sourceLineNo">1472</span> @Override<a name="line.1472"></a>
+<span class="sourceLineNo">1473</span> protected double
getCostFromRl(BalancerRegionLoad rl) {<a name="line.1473"></a>
+<span class="sourceLineNo">1474</span> return
rl.getReadRequestsCount();<a name="line.1474"></a>
+<span class="sourceLineNo">1475</span> }<a name="line.1475"></a>
+<span class="sourceLineNo">1476</span> }<a name="line.1476"></a>
+<span class="sourceLineNo">1477</span><a name="line.1477"></a>
+<span class="sourceLineNo">1478</span> /**<a name="line.1478"></a>
+<span class="sourceLineNo">1479</span> * Compute the cost of total number of
write requests. The more unbalanced the higher the<a name="line.1479"></a>
+<span class="sourceLineNo">1480</span> * computed cost will be. This uses a
rolling average of regionload.<a name="line.1480"></a>
+<span class="sourceLineNo">1481</span> */<a name="line.1481"></a>
+<span class="sourceLineNo">1482</span> static class WriteRequestCostFunction
extends CostFromRegionLoadAsRateFunction {<a name="line.1482"></a>
+<span class="sourceLineNo">1483</span><a name="line.1483"></a>
+<span class="sourceLineNo">1484</span> private static final String
WRITE_REQUEST_COST_KEY =<a name="line.1484"></a>
+<span class="sourceLineNo">1485</span>
"hbase.master.balancer.stochastic.writeRequestCost";<a name="line.1485"></a>
+<span class="sourceLineNo">1486</span> private static final float
DEFAULT_WRITE_REQUEST_COST = 5;<a name="line.1486"></a>
+<span class="sourceLineNo">1487</span><a name="line.1487"></a>
+<span class="sourceLineNo">1488</span>
WriteRequestCostFunction(Configuration conf) {<a name="line.1488"></a>
+<span class="sourceLineNo">1489</span> super(conf);<a
name="line.1489"></a>
+<span class="sourceLineNo">1490</span>
this.setMultiplier(conf.getFloat(WRITE_REQUEST_COST_KEY,
DEFAULT_WRITE_REQUEST_COST));<a name="line.1490"></a>
+<span class="sourceLineNo">1491</span> }<a name="line.1491"></a>
+<span class="sourceLineNo">1492</span><a name="line.1492"></a>
+<span class="sourceLineNo">1493</span> @Override<a name="line.1493"></a>
+<span class="sourceLineNo">1494</span> protected double
getCostFromRl(BalancerRegionLoad rl) {<a name="line.1494"></a>
+<span class="sourceLineNo">1495</span> return
rl.getWriteRequestsCount();<a name="line.1495"></a>
+<span class="sourceLineNo">1496</span> }<a name="line.1496"></a>
+<span class="sourceLineNo">1497</span> }<a name="line.1497"></a>
+<span class="sourceLineNo">1498</span><a name="line.1498"></a>
+<span class="sourceLineNo">1499</span> /**<a name="line.1499"></a>
+<span class="sourceLineNo">1500</span> * A cost function for region
replicas. We give a very high cost to hosting<a name="line.1500"></a>
+<span class="sourceLineNo">1501</span> * replicas of the same region in the
same host. We do not prevent the case<a name="line.1501"></a>
+<span class="sourceLineNo">1502</span> * though, since if numReplicas >
numRegionServers, we still want to keep the<a name="line.1502"></a>
+<span class="sourceLineNo">1503</span> * replica open.<a
name="line.1503"></a>
+<span class="sourceLineNo">1504</span> */<a name="line.1504"></a>
+<span class="sourceLineNo">1505</span> static class
RegionReplicaHostCostFunction extends CostFunction {<a name="line.1505"></a>
+<span class="sourceLineNo">1506</span> private static final String
REGION_REPLICA_HOST_COST_KEY =<a name="line.1506"></a>
+<span class="sourceLineNo">1507</span>
"hbase.master.balancer.stochastic.regionReplicaHostCostKey";<a
name="line.1507"></a>
+<span class="sourceLineNo">1508</span> private static final float
DEFAULT_REGION_REPLICA_HOST_COST_KEY = 100000;<a name="line.1508"></a>
+<span class="sourceLineNo">1509</span><a name="line.1509"></a>
+<span class="sourceLineNo">1510</span> long maxCost = 0;<a
name="line.1510"></a>
+<span class="sourceLineNo">1511</span> long[] costsPerGroup; // group is
either server, host or rack<a name="line.1511"></a>
+<span class="sourceLineNo">1512</span> int[][]
primariesOfRegionsPerGroup;<a name="line.1512"></a>
+<span class="sourceLineNo">1513</span><a name="line.1513"></a>
+<span class="sourceLineNo">1514</span> public
RegionReplicaHostCostFunction(Configuration conf) {<a name="line.1514"></a>
+<span class="sourceLineNo">1515</span> super(conf);<a
name="line.1515"></a>
+<span class="sourceLineNo">1516</span>
this.setMultiplier(conf.getFloat(REGION_REPLICA_HOST_COST_KEY,<a
name="line.1516"></a>
+<span class="sourceLineNo">1517</span>
DEFAULT_REGION_REPLICA_HOST_COST_KEY));<a name="line.1517"></a>
+<span class="sourceLineNo">1518</span> }<a name="line.1518"></a>
+<span class="sourceLineNo">1519</span><a name="line.1519"></a>
+<span class="sourceLineNo">1520</span> @Override<a name="line.1520"></a>
+<span class="sourceLineNo">1521</span> void init(Cluster cluster) {<a
name="line.1521"></a>
+<span class="sourceLineNo">1522</span> super.init(cluster);<a
name="line.1522"></a>
+<span class="sourceLineNo">1523</span> // max cost is the case where
every region replica is hosted together regardless of host<a
name="line.1523"></a>
+<span class="sourceLineNo">1524</span> maxCost = cluster.numHosts > 1
? getMaxCost(cluster) : 0;<a name="line.1524"></a>
+<span class="sourceLineNo">1525</span> costsPerGroup = new
long[cluster.numHosts];<a name="line.1525"></a>
+<span class="sourceLineNo">1526</span> primariesOfRegionsPerGroup =
cluster.multiServersPerHost // either server based or host based<a
name="line.1526"></a>
+<span class="sourceLineNo">1527</span> ?
cluster.primariesOfRegionsPerHost<a name="line.1527"></a>
+<span class="sourceLineNo">1528</span> :
cluster.primariesOfRegionsPerServer;<a name="line.1528"></a>
+<span class="sourceLineNo">1529</span> for (int i = 0 ; i <
primariesOfRegionsPerGroup.length; i++) {<a name="line.1529"></a>
+<span class="sourceLineNo">1530</span> costsPerGroup[i] =
costPerGroup(primariesOfRegionsPerGroup[i]);<a name="line.1530"></a>
+<span class="sourceLineNo">1531</span> }<a name="line.1531"></a>
+<span class="sourceLineNo">1532</span> }<a name="line.1532"></a>
+<span class="sourceLineNo">1533</span><a name="line.1533"></a>
+<span class="sourceLineNo">1534</span> long getMaxCost(Cluster cluster) {<a
name="line.1534"></a>
+<span class="sourceLineNo">1535</span> if (!cluster.hasRegionReplicas)
{<a name="line.1535"></a>
+<span class="sourceLineNo">1536</span> return 0; // short circuit<a
name="line.1536"></a>
+<span class="sourceLineNo">1537</span> }<a name="line.1537"></a>
+<span class="sourceLineNo">1538</span> // max cost is the case where
every region replica is hosted together regardless of host<a
name="line.1538"></a>
+<span class="sourceLineNo">1539</span> int[] primariesOfRegions = new
int[cluster.numRegions];<a name="line.1539"></a>
+<span class="sourceLineNo">1540</span>
System.arraycopy(cluster.regionIndexToPrimaryIndex, 0, primariesOfRegions, 0,<a
name="line.1540"></a>
+<span class="sourceLineNo">1541</span> cluster.regions.length);<a
name="line.1541"></a>
+<span class="sourceLineNo">1542</span><a name="line.1542"></a>
+<span class="sourceLineNo">1543</span> Arrays.sort(primariesOfRegions);<a
name="line.1543"></a>
+<span class="sourceLineNo">1544</span><a name="line.1544"></a>
+<span class="sourceLineNo">1545</span> // compute numReplicas from the
sorted array<a name="line.1545"></a>
+<span class="sourceLineNo">1546</span> return
costPerGroup(primariesOfRegions);<a name="line.1546"></a>
+<span class="sourceLineNo">1547</span> }<a name="line.1547"></a>
+<span class="sourceLineNo">1548</span><a name="line.1548"></a>
+<span class="sourceLineNo">1549</span> @Override<a name="line.1549"></a>
+<span class="sourceLineNo">1550</span> boolean isNeeded() {<a
name="line.1550"></a>
+<span class="sourceLineNo">1551</span> return
cluster.hasRegionReplicas;<a name="line.1551"></a>
+<span class="sourceLineNo">1552</span> }<a name="line.1552"></a>
+<span class="sourceLineNo">1553</span><a name="line.1553"></a>
+<span class="sourceLineNo">1554</span> @Override<a name="line.1554"></a>
+<span class="sourceLineNo">1555</span> double cost() {<a
name="line.1555"></a>
+<span class="sourceLineNo">1556</span> if (maxCost <= 0) {<a
name="line.1556"></a>
+<span class="sourceLineNo">1557</span> return 0;<a name="line.1557"></a>
+<span class="sourceLineNo">1558</span> }<a name="line.1558"></a>
+<span class="sourceLineNo">1559</span><a name="line.1559"></a>
+<span class="sourceLineNo">1560</span> long totalCost = 0;<a
name="line.1560"></a>
+<span class="sourceLineNo">1561</span> for (int i = 0 ; i <
costsPerGroup.length; i++) {<a name="line.1561"></a>
+<span class="sourceLineNo">1562</span> totalCost += costsPerGroup[i];<a
name="line.1562"></a>
+<span class="sourceLineNo">1563</span> }<a name="line.1563"></a>
+<span class="sourceLineNo">1564</span> return scale(0, maxCost,
totalCost);<a name="line.1564"></a>
+<span class="sourceLineNo">1565</span> }<a name="line.1565"></a>
+<span class="sourceLineNo">1566</span><a name="line.1566"></a>
+<span class="sourceLineNo">1567</span> /**<a name="line.1567"></a>
+<span class="sourceLineNo">1568</span> * For each primary region, it
computes the total number of replicas in the array (numReplicas)<a
name="line.1568"></a>
+<span class="sourceLineNo">1569</span> * and returns a sum of
numReplicas-1 squared. For example, if the server hosts<a name="line.1569"></a>
+<span class="sourceLineNo">1570</span> * regions a, b, c, d, e, f where a
and b are same replicas, and c,d,e are same replicas, it<a name="line.1570"></a>
+<span class="sourceLineNo">1571</span> * returns (2-1) * (2-1) + (3-1) *
(3-1) + (1-1) * (1-1).<a name="line.1571"></a>
+<span class="sourceLineNo">1572</span> * @param primariesOfRegions a
sorted array of primary regions ids for the regions hosted<a
name="line.1572"></a>
+<span class="sourceLineNo">1573</span> * @return a sum of numReplicas-1
squared for each primary region in the group.<a name="line.1573"></a>
+<span class="sourceLineNo">1574</span> */<a name="line.1574"></a>
+<span class="sourceLineNo">1575</span> protected long costPerGroup(int[]
primariesOfRegions) {<a name="line.1575"></a>
+<span class="sourceLineNo">1576</span> long cost = 0;<a
name="line.1576"></a>
+<span class="sourceLineNo">1577</span> int currentPrimary = -1;<a
name="line.1577"></a>
+<span class="sourceLineNo">1578</span> int currentPrimaryIndex = -1;<a
name="line.1578"></a>
+<span class="sourceLineNo">1579</span> // primariesOfRegions is a sorted
array of primary ids of regions. Replicas of regions<a name="line.1579"></a>
+<span class="sourceLineNo">1580</span> // sharing the same primary will
have consecutive numbers in the array.<a name="line.1580"></a>
+<span class="sourceLineNo">1581</span> for (int j = 0 ; j <=
primariesOfRegions.length; j++) {<a name="line.1581"></a>
+<span class="sourceLineNo">1582</span> int primary = j <
primariesOfRegions.length ? primariesOfRegions[j] : -1;<a name="line.1582"></a>
+<span class="sourceLineNo">1583</span> if (primary != currentPrimary) {
// we see a new primary<a name="line.1583"></a>
+<span class="sourceLineNo">1584</span> int numReplicas = j -
currentPrimaryIndex;<a name="line.1584"></a>
+<span class="sourceLineNo">1585</span> // square the cost<a
name="line.1585"></a>
+<span class="sourceLineNo">1586</span> if (numReplicas > 1) { //
means consecutive primaries, indicating co-location<a name="line.1586"></a>
+<span class="sourceLineNo">1587</span> cost += (numReplicas - 1) *
(numReplicas - 1);<a name="line.1587"></a>
+<span class="sourceLineNo">1588</span> }<a name="line.1588"></a>
+<span class="sourceLineNo">1589</span> currentPrimary = primary;<a
name="line.1589"></a>
+<span class="sourceLineNo">1590</span> currentPrimaryIndex = j;<a
name="line.1590"></a>
+<span class="sourceLineNo">1591</span> }<a name="line.1591"></a>
+<span class="sourceLineNo">1592</span> }<a name="line.1592"></a>
+<span class="sourceLineNo">1593</span><a name="line.1593"></a>
+<span class="sourceLineNo">1594</span> return cost;<a
name="line.1594"></a>
+<span class="sourceLineNo">1595</span> }<a name="line.1595"></a>
+<span class="sourceLineNo">1596</span><a name="line.1596"></a>
+<span class="sourceLineNo">1597</span> @Override<a name="line.1597"></a>
+<span class="sourceLineNo">1598</span> protected void regionMoved(int
region, int oldServer, int newServer) {<a name="line.1598"></a>
+<span class="sourceLineNo">1599</span> if (maxCost <= 0) {<a
name="line.1599"></a>
+<span class="sourceLineNo">1600</span> return; // no need to compute<a
name="line.1600"></a>
+<span class="sourceLineNo">1601</span> }<a name="line.1601"></a>
+<span class="sourceLineNo">1602</span> if (cluster.multiServersPerHost)
{<a name="line.1602"></a>
+<span class="sourceLineNo">1603</span> int oldHost =
cluster.serverIndexToHostIndex[oldServer];<a name="line.1603"></a>
+<span class="sourceLineNo">1604</span> int newHost =
cluster.serverIndexToHostIndex[newServer];<a name="line.1604"></a>
+<span class="sourceLineNo">1605</span> if (newHost != oldHost) {<a
name="line.1605"></a>
+<span class="sourceLineNo">1606</span> costsPerGroup[oldHost] =
costPerGroup(cluster.primariesOfRegionsPerHost[oldHost]);<a
name="line.1606"></a>
+<span class="sourceLineNo">1607</span> costsPerGroup[newHost] =
costPerGroup(cluster.primariesOfRegionsPerHost[newHost]);<a
name="line.1607"></a>
+<span class="sourceLineNo">1608</span> }<a name="line.1608"></a>
+<span class="sourceLineNo">1609</span> } else {<a name="line.1609"></a>
+<span class="sourceLineNo">1610</span> costsPerGroup[oldServer] =
costPerGroup(cluster.primariesOfRegionsPerServer[oldServer]);<a
name="line.1610"></a>
+<span class="sourceLineNo">1611</span> costsPerGroup[newServer] =
costPerGroup(cluster.primariesOfRegionsPerServer[newServer]);<a
name="line.1611"></a>
+<span class="sourceLineNo">1612</span> }<a name="line.1612"></a>
+<span class="sourceLineNo">1613</span> }<a name="line.1613"></a>
+<span class="sourceLineNo">1614</span> }<a name="line.1614"></a>
+<span class="sourceLineNo">1615</span><a name="line.1615"></a>
+<span class="sourceLineNo">1616</span> /**<a name="line.1616"></a>
+<span class="sourceLineNo">1617</span> * A cost function for region replicas
for the rack distribution. We give a relatively high<a name="line.1617"></a>
+<span class="sourceLineNo">1618</span> * cost to hosting replicas of the
same region in the same rack. We do not prevent the case<a name="line.1618"></a>
+<span class="sourceLineNo">1619</span> * though.<a name="line.1619"></a>
+<span class="sourceLineNo">1620</span> */<a name="line.1620"></a>
+<span class="sourceLineNo">1621</span> static class
RegionReplicaRackCostFunction extends RegionReplicaHostCostFunction {<a
name="line.1621"></a>
+<span class="sourceLineNo">1622</span> private static final String
REGION_REPLICA_RACK_COST_KEY =<a name="line.1622"></a>
+<span class="sourceLineNo">1623</span>
"hbase.master.balancer.stochastic.regionReplicaRackCostKey";<a
name="line.1623"></a>
+<span class="sourceLineNo">1624</span> private static final float
DEFAULT_REGION_REPLICA_RACK_COST_KEY = 10000;<a name="line.1624"></a>
+<span class="sourceLineNo">1625</span><a name="line.1625"></a>
+<span class="sourceLineNo">1626</span> public
RegionReplicaRackCostFunction(Configuration conf) {<a name="line.1626"></a>
+<span class="sourceLineNo">1627</span> super(conf);<a
name="line.1627"></a>
+<span class="sourceLineNo">1628</span>
this.setMultiplier(conf.getFloat(REGION_REPLICA_RACK_COST_KEY,<a
name="line.1628"></a>
+<span class="sourceLineNo">1629</span>
DEFAULT_REGION_REPLICA_RACK_COST_KEY));<a name="line.1629"></a>
+<span class="sourceLineNo">1630</span> }<a name="line.1630"></a>
+<span class="sourceLineNo">1631</span><a name="line.1631"></a>
+<span class="sourceLineNo">1632</span> @Override<a name="line.1632"></a>
+<span class="sourceLineNo">1633</span> void init(Cluster cluster) {<a
name="line.1633"></a>
+<span class="sourceLineNo">1634</span> this.cluster = cluster;<a
name="line.1634"></a>
+<span class="sourceLineNo">1635</span> if (cluster.numRacks <= 1) {<a
name="line.1635"></a>
+<span class="sourceLineNo">1636</span> maxCost = 0;<a
name="line.1636"></a>
+<span class="sourceLineNo">1637</span> return; // disabled for 1 rack<a
name="line.1637"></a>
+<span class="sourceLineNo">1638</span> }<a name="line.1638"></a>
+<span class="sourceLineNo">1639</span> // max cost is the case where
every region replica is hosted together regardless of rack<a
name="line.1639"></a>
+<span class="sourceLineNo">1640</span> maxCost = getMaxCost(cluster);<a
name="line.1640"></a>
+<span class="sourceLineNo">1641</span> costsPerGroup = new
long[cluster.numRacks];<a name="line.1641"></a>
+<span class="sourceLineNo">1642</span> for (int i = 0 ; i <
cluster.primariesOfRegionsPerRack.length; i++) {<a name="line.1642"></a>
+<span class="sourceLineNo">1643</span> costsPerGroup[i] =
costPerGroup(cluster.primariesOfRegionsPerRack[i]);<a name="line.1643"></a>
+<span class="sourceLineNo">1644</span> }<a name="line.1644"></a>
+<span class="sourceLineNo">1645</span> }<a name="line.1645"></a>
+<span class="sourceLineNo">1646</span><a name="line.1646"></a>
+<span class="sourceLineNo">1647</span> @Override<a name="line.1647"></a>
+<span class="sourceLineNo">1648</span> protected void regionMoved(int
region, int oldServer, int newServer) {<a name="line.1648"></a>
+<span class="sourceLineNo">1649</span> if (maxCost <= 0) {<a
name="line.1649"></a>
+<span class="sourceLineNo">1650</span> return; // no need to compute<a
name="line.1650"></a>
+<span class="sourceLineNo">1651</span> }<a name="line.1651"></a>
+<span class="sourceLineNo">1652</span> int oldRack =
cluster.serverIndexToRackIndex[oldServer];<a name="line.1652"></a>
+<span class="sourceLineNo">1653</span> int newRack =
cluster.serverIndexToRackIndex[newServer];<a name="line.1653"></a>
+<span class="sourceLineNo">1654</span> if (newRack != oldRack) {<a
name="line.1654"></a>
+<span class="sourceLineNo">1655</span> costsPerGroup[oldRack] =
costPerGroup(cluster.primariesOfRegionsPerRack[oldRack]);<a
name="line.1655"></a>
+<span class="sourceLineNo">1656</span> costsPerGroup[newRack] =
costPerGroup(cluster.primariesOfRegionsPerRack[newRack]);<a
name="line.1656"></a>
+<span class="sourceLineNo">1657</span> }<a name="line.1657"></a>
+<span class="sourceLineNo">1658</span> }<a name="line.1658"></a>
+<span class="sourceLineNo">1659</span> }<a name="line.1659"></a>
+<span class="sourceLineNo">1660</span><a name="line.1660"></a>
+<span class="sourceLineNo">1661</span> /**<a name="line.1661"></a>
+<span class="sourceLineNo">1662</span> * Compute the cost of total memstore
size. The more unbalanced the higher the<a name="line.1662"></a>
+<span class="sourceLineNo">1663</span> * computed cost will be. This uses a
rolling average of regionload.<a name="line.1663"></a>
+<span class="sourceLineNo">1664</span> */<a name="line.1664"></a>
+<span class="sourceLineNo">1665</span> static class MemstoreSizeCostFunction
extends CostFromRegionLoadAsRateFunction {<a name="line.1665"></a>
+<span class="sourceLineNo">1666</span><a name="line.1666"></a>
+<span class="sourceLineNo">1667</span> private static final String
MEMSTORE_SIZE_COST_KEY =<a name="line.1667"></a>
+<span class="sourceLineNo">1668</span>
"hbase.master.balancer.stochastic.memstoreSizeCost";<a name="line.1668"></a>
+<span class="sourceLineNo">1669</span> private static final float
DEFAULT_MEMSTORE_SIZE_COST = 5;<a name="line.1669"></a>
+<span class="sourceLineNo">1670</span><a name="line.1670"></a>
+<span class="sourceLineNo">1671</span>
MemstoreSizeCostFunction(Configuration conf) {<a name="line.1671"></a>
+<span class="sourceLineNo">1672</span> super(conf);<a
name="line.1672"></a>
+<span class="sourceLineNo">1673</span>
this.setMultiplier(conf.getFloat(MEMSTORE_SIZE_COST_KEY,
DEFAULT_MEMSTORE_SIZE_COST));<a name="line.1673"></a>
+<span class="sourceLineNo">1674</span> }<a name="line.1674"></a>
+<span class="sourceLineNo">1675</span><a name="line.1675"></a>
+<span class="sourceLineNo">1676</span> @Override<a name="line.1676"></a>
+<span class="sourceLineNo">1677</span> protected double
getCostFromRl(BalancerRegionLoad rl) {<a name="line.1677"></a>
+<span class="sourceLineNo">1678</span> return rl.getMemStoreSizeMB();<a
name="line.1678"></a>
+<span class="sourceLineNo">1679</span> }<a name="line.1679"></a>
+<span class="sourceLineNo">1680</span> }<a name="line.1680"></a>
+<span class="sourceLineNo">1681</span> /**<a name="line.1681"></a>
+<span class="sourceLineNo">1682</span> * Compute the cost of total open
storefiles size. The more unbalanced the higher the<a name="line.1682"></a>
+<span class="sourceLineNo">1683</span> * computed cost will be. This uses a
rolling average of regionload.<a name="line.1683"></a>
+<span class="sourceLineNo">1684</span> */<a name="line.1684"></a>
+<span class="sourceLineNo">1685</span> static class StoreFileCostFunction
extends CostFromRegionLoadFunction {<a name="line.1685"></a>
+<span class="sourceLineNo">1686</span><a name="line.1686"></a>
+<span class="sourceLineNo">1687</span> private static final String
STOREFILE_SIZE_COST_KEY =<a name="line.1687"></a>
+<span class="sourceLineNo">1688</span>
"hbase.master.balancer.stochastic.storefileSizeCost";<a name="line.1688"></a>
+<span class="sourceLineNo">1689</span> private static final float
DEFAULT_STOREFILE_SIZE_COST = 5;<a name="line.1689"></a>
+<span class="sourceLineNo">1690</span><a name="line.1690"></a>
+<span class="sourceLineNo">1691</span> StoreFileCostFunction(Configuration
conf) {<a name="line.1691"></a>
+<span class="sourceLineNo">1692</span> super(conf);<a
name="line.1692"></a>
+<span class="sourceLineNo">1693</span>
this.setMultiplier(conf.getFloat(STOREFILE_SIZE_COST_KEY,
DEFAULT_STOREFILE_SIZE_COST));<a name="line.1693"></a>
+<span class="sourceLineNo">1694</span> }<a name="line.1694"></a>
+<span class="sourceLineNo">1695</span><a name="line.1695"></a>
+<span class="sourceLineNo">1696</span> @Override<a name="line.1696"></a>
+<span class="sourceLineNo">1697</span> protected double
getCostFromRl(BalancerRegionLoad rl) {<a name="line.1697"></a>
+<span class="sourceLineNo">1698</span> return rl.getStorefileSizeMB();<a
name="line.1698"></a>
+<span class="sourceLineNo">1699</span> }<a name="line.1699"></a>
+<span class="sourceLineNo">1700</span> }<a name="line.1700"></a>
+<span class="sourceLineNo">1701</span><a name="line.1701"></a>
+<span class="sourceLineNo">1702</span> /**<a name="line.1702"></a>
+<span class="sourceLineNo">1703</span> * A helper function to compose the
attribute name from tablename and costfunction name<a name="line.1703"></a>
+<span class="sourceLineNo">1704</span> */<a name="line.1704"></a>
+<span class="sourceLineNo">1705</span> public static String
composeAttributeName(String tableName, String costFunctionName) {<a
name="line.1705"></a>
+<span class="sourceLineNo">1706</span> return tableName +
TABLE_FUNCTION_SEP + costFunctionName;<a name="line.1706"></a>
+<span class="sourceLineNo">1707</span> }<a name="line.1707"></a>
+<span class="sourceLineNo">1708</span>}<a name="line.1708"></a>