1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.master.balancer;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Queue;
35 import java.util.TreeMap;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.hadoop.conf.Configuration;
40 import org.apache.hadoop.hbase.ClusterStatus;
41 import org.apache.hadoop.hbase.HBaseConfiguration;
42 import org.apache.hadoop.hbase.HRegionInfo;
43 import org.apache.hadoop.hbase.RegionLoad;
44 import org.apache.hadoop.hbase.ServerLoad;
45 import org.apache.hadoop.hbase.ServerName;
46 import org.apache.hadoop.hbase.client.RegionReplicaUtil;
47 import org.apache.hadoop.hbase.master.MockNoopMasterServices;
48 import org.apache.hadoop.hbase.master.RackManager;
49 import org.apache.hadoop.hbase.master.RegionPlan;
50 import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer.Cluster;
51 import org.apache.hadoop.hbase.master.balancer.StochasticLoadBalancer.ServerLocalityCostFunction;
52 import org.apache.hadoop.hbase.testclassification.FlakeyTests;
53 import org.apache.hadoop.hbase.testclassification.MediumTests;
54 import org.apache.hadoop.hbase.util.Bytes;
55 import org.junit.Ignore;
56 import org.junit.Test;
57 import org.junit.experimental.categories.Category;
58
59 @Category({FlakeyTests.class, MediumTests.class})
60 public class TestStochasticLoadBalancer extends BalancerTestBase {
61 public static final String REGION_KEY = "testRegion";
62 private static final Log LOG = LogFactory.getLog(TestStochasticLoadBalancer.class);
63
64
65 private float[] expectedLocalities = {1.0f, 0.0f, 0.50f, 0.25f, 1.0f};
66
67
68
69
70
71
72
73
74 private int[][][] clusterRegionLocationMocks = new int[][][]{
75
76
77 new int[][]{
78 new int[]{2, 1, 1},
79 new int[]{2, 0, 0, 100},
80 new int[]{0, 100, 0, 0},
81 new int[]{0, 100, 0, 0},
82 new int[]{1, 0, 100, 0},
83 },
84
85
86 new int[][]{
87 new int[]{1, 2, 1},
88 new int[]{0, 0, 0, 100},
89 new int[]{1, 100, 0, 0},
90 new int[]{1, 100, 0, 0},
91 new int[]{2, 0, 100, 0},
92 },
93
94
95 new int[][]{
96 new int[]{1, 2, 1},
97 new int[]{0, 25, 0, 50},
98 new int[]{1, 50, 25, 0},
99 new int[]{1, 50, 25, 0},
100 new int[]{2, 0, 50, 25},
101 },
102
103
104 new int[][]{
105 new int[]{1, 2, 1},
106 new int[]{0, 25, 0, 100},
107 new int[]{1, 100, 25, 0},
108 new int[]{1, 100, 25, 0},
109 new int[]{2, 0, 100, 25},
110 },
111
112
113 new int[][]{
114 new int[]{1, 2, 1},
115 new int[]{0, 75, 75, 75},
116 new int[]{1, 75, 75, 75},
117 new int[]{1, 75, 75, 75},
118 new int[]{2, 75, 75, 75},
119 },
120 };
121
122
123 @Test
124 public void testKeepRegionLoad() throws Exception {
125
126 ServerName sn = ServerName.valueOf("test:8080", 100);
127 int numClusterStatusToAdd = 20000;
128 for (int i = 0; i < numClusterStatusToAdd; i++) {
129 ServerLoad sl = mock(ServerLoad.class);
130
131 RegionLoad rl = mock(RegionLoad.class);
132 when(rl.getStores()).thenReturn(i);
133
134 Map<byte[], RegionLoad> regionLoadMap =
135 new TreeMap<byte[], RegionLoad>(Bytes.BYTES_COMPARATOR);
136 regionLoadMap.put(Bytes.toBytes(REGION_KEY), rl);
137 when(sl.getRegionsLoad()).thenReturn(regionLoadMap);
138
139 ClusterStatus clusterStatus = mock(ClusterStatus.class);
140 when(clusterStatus.getServers()).thenReturn(Arrays.asList(sn));
141 when(clusterStatus.getLoad(sn)).thenReturn(sl);
142
143 loadBalancer.setClusterStatus(clusterStatus);
144 }
145
146 String regionNameAsString = HRegionInfo.getRegionNameAsString(Bytes.toBytes(REGION_KEY));
147 assertTrue(loadBalancer.loads.get(regionNameAsString) != null);
148 assertTrue(loadBalancer.loads.get(regionNameAsString).size() == 15);
149
150 Queue<RegionLoad> loads = loadBalancer.loads.get(regionNameAsString);
151 int i = 0;
152 while(loads.size() > 0) {
153 RegionLoad rl = loads.remove();
154 assertEquals(i + (numClusterStatusToAdd - 15), rl.getStores());
155 i ++;
156 }
157 }
158
159 @Test
160 public void testNeedBalance() {
161 float minCost = conf.getFloat("hbase.master.balancer.stochastic.minCostNeedBalance", 0.05f);
162 conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", 1.0f);
163 try {
164 loadBalancer.setConf(conf);
165 for (int[] mockCluster : clusterStateMocks) {
166 Map<ServerName, List<HRegionInfo>> servers = mockClusterServers(mockCluster);
167 List<RegionPlan> plans = loadBalancer.balanceCluster(servers);
168 assertNull(plans);
169 }
170 } finally {
171
172 conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", minCost);
173 loadBalancer.setConf(conf);
174 }
175 }
176
177
178
179
180
181
182
183
184
185 @Test
186 public void testBalanceCluster() throws Exception {
187
188 for (int[] mockCluster : clusterStateMocks) {
189 Map<ServerName, List<HRegionInfo>> servers = mockClusterServers(mockCluster);
190 List<ServerAndLoad> list = convertToList(servers);
191 LOG.info("Mock Cluster : " + printMock(list) + " " + printStats(list));
192 List<RegionPlan> plans = loadBalancer.balanceCluster(servers);
193 List<ServerAndLoad> balancedCluster = reconcile(list, plans, servers);
194 LOG.info("Mock Balance : " + printMock(balancedCluster));
195 assertClusterAsBalanced(balancedCluster);
196 List<RegionPlan> secondPlans = loadBalancer.balanceCluster(servers);
197 assertNull(secondPlans);
198 for (Map.Entry<ServerName, List<HRegionInfo>> entry : servers.entrySet()) {
199 returnRegions(entry.getValue());
200 returnServer(entry.getKey());
201 }
202 }
203 }
204
205 @Test
206 public void testLocalityCost() throws Exception {
207 Configuration conf = HBaseConfiguration.create();
208 MockNoopMasterServices master = new MockNoopMasterServices();
209 StochasticLoadBalancer.CostFunction
210 costFunction = new ServerLocalityCostFunction(conf, master);
211
212 for (int test = 0; test < clusterRegionLocationMocks.length; test++) {
213 int[][] clusterRegionLocations = clusterRegionLocationMocks[test];
214 MockCluster cluster = new MockCluster(clusterRegionLocations);
215 costFunction.init(cluster);
216 double cost = costFunction.cost();
217 double expected = 1 - expectedLocalities[test];
218 assertEquals(expected, cost, 0.001);
219 }
220 }
221
222 @Test
223 public void testMoveCost() throws Exception {
224 Configuration conf = HBaseConfiguration.create();
225 StochasticLoadBalancer.CostFunction
226 costFunction = new StochasticLoadBalancer.MoveCostFunction(conf);
227 for (int[] mockCluster : clusterStateMocks) {
228 BaseLoadBalancer.Cluster cluster = mockCluster(mockCluster);
229 costFunction.init(cluster);
230 double cost = costFunction.cost();
231 assertEquals(0.0f, cost, 0.001);
232
233
234 cluster.setNumRegions(200);
235 cluster.setNumMovedRegions(10);
236 cost = costFunction.cost();
237 assertEquals(0.05f, cost, 0.001);
238 cluster.setNumMovedRegions(100);
239 cost = costFunction.cost();
240 assertEquals(0.5f, cost, 0.001);
241 cluster.setNumMovedRegions(200);
242 cost = costFunction.cost();
243 assertEquals(1.0f, cost, 0.001);
244
245
246
247 cluster.setNumRegions(10000);
248 cluster.setNumMovedRegions(250);
249 cost = costFunction.cost();
250 assertEquals(0.1f, cost, 0.001);
251 cluster.setNumMovedRegions(1250);
252 cost = costFunction.cost();
253 assertEquals(0.5f, cost, 0.001);
254 cluster.setNumMovedRegions(2500);
255 cost = costFunction.cost();
256 assertEquals(1.0f, cost, 0.01);
257 }
258 }
259
260 @Test
261 public void testSkewCost() {
262 Configuration conf = HBaseConfiguration.create();
263 StochasticLoadBalancer.CostFunction
264 costFunction = new StochasticLoadBalancer.RegionCountSkewCostFunction(conf);
265 for (int[] mockCluster : clusterStateMocks) {
266 costFunction.init(mockCluster(mockCluster));
267 double cost = costFunction.cost();
268 assertTrue(cost >= 0);
269 assertTrue(cost <= 1.01);
270 }
271
272 costFunction.init(mockCluster(new int[]{0, 0, 0, 0, 1}));
273 assertEquals(0,costFunction.cost(), 0.01);
274 costFunction.init(mockCluster(new int[]{0, 0, 0, 1, 1}));
275 assertEquals(0, costFunction.cost(), 0.01);
276 costFunction.init(mockCluster(new int[]{0, 0, 1, 1, 1}));
277 assertEquals(0, costFunction.cost(), 0.01);
278 costFunction.init(mockCluster(new int[]{0, 1, 1, 1, 1}));
279 assertEquals(0, costFunction.cost(), 0.01);
280 costFunction.init(mockCluster(new int[]{1, 1, 1, 1, 1}));
281 assertEquals(0, costFunction.cost(), 0.01);
282 costFunction.init(mockCluster(new int[]{10000, 0, 0, 0, 0}));
283 assertEquals(1, costFunction.cost(), 0.01);
284 }
285
286 @Test
287 public void testCostAfterUndoAction() {
288 final int runs = 10;
289 loadBalancer.setConf(conf);
290 for (int[] mockCluster : clusterStateMocks) {
291 BaseLoadBalancer.Cluster cluster = mockCluster(mockCluster);
292 loadBalancer.initCosts(cluster);
293 for (int i = 0; i != runs; ++i) {
294 final double expectedCost = loadBalancer.computeCost(cluster, Double.MAX_VALUE);
295 Cluster.Action action = loadBalancer.nextAction(cluster);
296 cluster.doAction(action);
297 loadBalancer.updateCostsWithAction(cluster, action);
298 Cluster.Action undoAction = action.undoAction();
299 cluster.doAction(undoAction);
300 loadBalancer.updateCostsWithAction(cluster, undoAction);
301 final double actualCost = loadBalancer.computeCost(cluster, Double.MAX_VALUE);
302 assertEquals(expectedCost, actualCost, 0);
303 }
304 }
305 }
306
307 @Test
308 public void testTableSkewCost() {
309 Configuration conf = HBaseConfiguration.create();
310 StochasticLoadBalancer.CostFunction
311 costFunction = new StochasticLoadBalancer.TableSkewCostFunction(conf);
312 for (int[] mockCluster : clusterStateMocks) {
313 BaseLoadBalancer.Cluster cluster = mockCluster(mockCluster);
314 costFunction.init(cluster);
315 double cost = costFunction.cost();
316 assertTrue(cost >= 0);
317 assertTrue(cost <= 1.01);
318 }
319 }
320
321 @Test
322 public void testRegionLoadCost() {
323 List<RegionLoad> regionLoads = new ArrayList<>();
324 for (int i = 1; i < 5; i++) {
325 RegionLoad regionLoad = mock(RegionLoad.class);
326 when(regionLoad.getReadRequestsCount()).thenReturn(new Long(i));
327 when(regionLoad.getStorefileSizeMB()).thenReturn(i);
328 regionLoads.add(regionLoad);
329 }
330
331 Configuration conf = HBaseConfiguration.create();
332 StochasticLoadBalancer.ReadRequestCostFunction readCostFunction =
333 new StochasticLoadBalancer.ReadRequestCostFunction(conf);
334 double rateResult = readCostFunction.getRegionLoadCost(regionLoads);
335
336 assertEquals(1, rateResult, 0.01);
337
338 StochasticLoadBalancer.StoreFileCostFunction storeFileCostFunction =
339 new StochasticLoadBalancer.StoreFileCostFunction(conf);
340 double result = storeFileCostFunction.getRegionLoadCost(regionLoads);
341
342 assertEquals(2.5, result, 0.01);
343 }
344
345 @Test
346 public void testCostFromArray() {
347 Configuration conf = HBaseConfiguration.create();
348 StochasticLoadBalancer.CostFromRegionLoadFunction
349 costFunction = new StochasticLoadBalancer.MemstoreSizeCostFunction(conf);
350 costFunction.init(mockCluster(new int[]{0, 0, 0, 0, 1}));
351
352 double[] statOne = new double[100];
353 for (int i =0; i < 100; i++) {
354 statOne[i] = 10;
355 }
356 assertEquals(0, costFunction.costFromArray(statOne), 0.01);
357
358 double[] statTwo= new double[101];
359 for (int i =0; i < 100; i++) {
360 statTwo[i] = 0;
361 }
362 statTwo[100] = 100;
363 assertEquals(1, costFunction.costFromArray(statTwo), 0.01);
364
365 double[] statThree = new double[200];
366 for (int i =0; i < 100; i++) {
367 statThree[i] = (0);
368 statThree[i+100] = 100;
369 }
370 assertEquals(0.5, costFunction.costFromArray(statThree), 0.01);
371 }
372
373 @Test(timeout = 60000)
374 public void testLosingRs() throws Exception {
375 int numNodes = 3;
376 int numRegions = 20;
377 int numRegionsPerServer = 3;
378 int replication = 1;
379 int numTables = 2;
380
381 Map<ServerName, List<HRegionInfo>> serverMap =
382 createServerMap(numNodes, numRegions, numRegionsPerServer, replication, numTables);
383 List<ServerAndLoad> list = convertToList(serverMap);
384
385
386 List<RegionPlan> plans = loadBalancer.balanceCluster(serverMap);
387 assertNotNull(plans);
388
389
390 List<ServerAndLoad> balancedCluster = reconcile(list, plans, serverMap);
391
392 assertClusterAsBalanced(balancedCluster);
393
394 ServerName sn = serverMap.keySet().toArray(new ServerName[serverMap.size()])[0];
395
396 ServerName deadSn = ServerName.valueOf(sn.getHostname(), sn.getPort(), sn.getStartcode() - 100);
397
398 serverMap.put(deadSn, new ArrayList<HRegionInfo>(0));
399
400 plans = loadBalancer.balanceCluster(serverMap);
401 assertNull(plans);
402 }
403
404 @Test
405 public void testReplicaCost() {
406 Configuration conf = HBaseConfiguration.create();
407 StochasticLoadBalancer.CostFunction
408 costFunction = new StochasticLoadBalancer.RegionReplicaHostCostFunction(conf);
409 for (int[] mockCluster : clusterStateMocks) {
410 BaseLoadBalancer.Cluster cluster = mockCluster(mockCluster);
411 costFunction.init(cluster);
412 double cost = costFunction.cost();
413 assertTrue(cost >= 0);
414 assertTrue(cost <= 1.01);
415 }
416 }
417
418 @Test
419 public void testReplicaCostForReplicas() {
420 Configuration conf = HBaseConfiguration.create();
421 StochasticLoadBalancer.CostFunction
422 costFunction = new StochasticLoadBalancer.RegionReplicaHostCostFunction(conf);
423
424 int [] servers = new int[] {3,3,3,3,3};
425 TreeMap<ServerName, List<HRegionInfo>> clusterState = mockClusterServers(servers);
426
427 BaseLoadBalancer.Cluster cluster;
428
429 cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null);
430 costFunction.init(cluster);
431 double costWithoutReplicas = costFunction.cost();
432 assertEquals(0, costWithoutReplicas, 0);
433
434
435 HRegionInfo replica1 = RegionReplicaUtil.getRegionInfoForReplica(
436 clusterState.firstEntry().getValue().get(0),1);
437 clusterState.lastEntry().getValue().add(replica1);
438
439 cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null);
440 costFunction.init(cluster);
441 double costWith1ReplicaDifferentServer = costFunction.cost();
442
443 assertEquals(0, costWith1ReplicaDifferentServer, 0);
444
445
446 HRegionInfo replica2 = RegionReplicaUtil.getRegionInfoForReplica(replica1, 2);
447 clusterState.lastEntry().getValue().add(replica2);
448
449 cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null);
450 costFunction.init(cluster);
451 double costWith1ReplicaSameServer = costFunction.cost();
452
453 assertTrue(costWith1ReplicaDifferentServer < costWith1ReplicaSameServer);
454
455
456
457 HRegionInfo replica3;
458 Iterator<Entry<ServerName, List<HRegionInfo>>> it;
459 Entry<ServerName, List<HRegionInfo>> entry;
460
461 clusterState = mockClusterServers(servers);
462 it = clusterState.entrySet().iterator();
463 entry = it.next();
464 HRegionInfo hri = entry.getValue().get(0);
465 replica1 = RegionReplicaUtil.getRegionInfoForReplica(hri, 1);
466 replica2 = RegionReplicaUtil.getRegionInfoForReplica(hri, 2);
467 replica3 = RegionReplicaUtil.getRegionInfoForReplica(hri, 3);
468 entry.getValue().add(replica1);
469 entry.getValue().add(replica2);
470 it.next().getValue().add(replica3);
471
472 cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null);
473 costFunction.init(cluster);
474 double costWith3ReplicasSameServer = costFunction.cost();
475
476 clusterState = mockClusterServers(servers);
477 hri = clusterState.firstEntry().getValue().get(0);
478 replica1 = RegionReplicaUtil.getRegionInfoForReplica(hri, 1);
479 replica2 = RegionReplicaUtil.getRegionInfoForReplica(hri, 2);
480 replica3 = RegionReplicaUtil.getRegionInfoForReplica(hri, 3);
481
482 clusterState.firstEntry().getValue().add(replica1);
483 clusterState.lastEntry().getValue().add(replica2);
484 clusterState.lastEntry().getValue().add(replica3);
485
486 cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null);
487 costFunction.init(cluster);
488 double costWith2ReplicasOnTwoServers = costFunction.cost();
489
490 assertTrue(costWith2ReplicasOnTwoServers < costWith3ReplicasSameServer);
491 }
492
493 @Test
494 public void testNeedsBalanceForColocatedReplicas() {
495
496
497 List<HRegionInfo> regions = randomRegions(1);
498 ServerName s1 = ServerName.valueOf("host1", 1000, 11111);
499 ServerName s2 = ServerName.valueOf("host11", 1000, 11111);
500 Map<ServerName, List<HRegionInfo>> map = new HashMap<ServerName, List<HRegionInfo>>();
501 map.put(s1, regions);
502 regions.add(RegionReplicaUtil.getRegionInfoForReplica(regions.get(0), 1));
503
504 regions = randomRegions(1);
505 map.put(s2, regions);
506 assertTrue(loadBalancer.needsBalance(new Cluster(map, null, null, null)));
507
508
509 map.clear();
510 regions = randomRegions(1);
511 List<HRegionInfo> regionsOnS2 = new ArrayList<HRegionInfo>(1);
512 regionsOnS2.add(RegionReplicaUtil.getRegionInfoForReplica(regions.get(0), 1));
513 map.put(s1, regions);
514 map.put(s2, regionsOnS2);
515
516 map.put(ServerName.valueOf("host2", 1000, 11111), randomRegions(1));
517 assertTrue(loadBalancer.needsBalance(new Cluster(map, null, null,
518 new ForTestRackManagerOne())));
519 }
520
521 @Test (timeout = 60000)
522 public void testSmallCluster() {
523 int numNodes = 10;
524 int numRegions = 1000;
525 int numRegionsPerServer = 40;
526 int replication = 1;
527 int numTables = 10;
528 testWithCluster(numNodes, numRegions, numRegionsPerServer, replication, numTables, true, true);
529 }
530
531 @Test (timeout = 60000)
532 public void testSmallCluster2() {
533 int numNodes = 20;
534 int numRegions = 2000;
535 int numRegionsPerServer = 40;
536 int replication = 1;
537 int numTables = 10;
538 testWithCluster(numNodes, numRegions, numRegionsPerServer, replication, numTables, true, true);
539 }
540
541 @Test (timeout = 60000)
542 public void testSmallCluster3() {
543 int numNodes = 20;
544 int numRegions = 2000;
545 int numRegionsPerServer = 1;
546 int replication = 1;
547 int numTables = 10;
548
549 testWithCluster(numNodes, numRegions, numRegionsPerServer, replication, numTables, false, false);
550 }
551
552 @Test (timeout = 800000)
553 public void testMidCluster() {
554 int numNodes = 100;
555 int numRegions = 10000;
556 int numRegionsPerServer = 60;
557 int replication = 1;
558 int numTables = 40;
559 testWithCluster(numNodes, numRegions, numRegionsPerServer, replication, numTables, true, true);
560 }
561
562 @Test (timeout = 800000)
563 public void testMidCluster2() {
564 int numNodes = 200;
565 int numRegions = 100000;
566 int numRegionsPerServer = 40;
567 int replication = 1;
568 int numTables = 400;
569 testWithCluster(numNodes,
570 numRegions,
571 numRegionsPerServer,
572 replication,
573 numTables,
574 false,
575 false);
576 }
577
578
579 @Test (timeout = 800000)
580 public void testMidCluster3() {
581 int numNodes = 100;
582 int numRegions = 2000;
583 int numRegionsPerServer = 9;
584 int replication = 1;
585 int numTables = 110;
586 testWithCluster(numNodes, numRegions, numRegionsPerServer, replication, numTables, true, true);
587
588 }
589
590 @Test
591 public void testLargeCluster() {
592 int numNodes = 1000;
593 int numRegions = 100000;
594 int numRegionsPerServer = 80;
595 int numTables = 100;
596 int replication = 1;
597 testWithCluster(numNodes, numRegions, numRegionsPerServer, replication, numTables, true, true);
598 }
599
600 @Test (timeout = 800000)
601 public void testRegionReplicasOnSmallCluster() {
602 int numNodes = 10;
603 int numRegions = 1000;
604 int replication = 3;
605 int numRegionsPerServer = 80;
606 int numTables = 10;
607 testWithCluster(numNodes, numRegions, numRegionsPerServer, replication, numTables, true, true);
608 }
609
610 @Ignore @Test (timeout = 800000)
611 public void testRegionReplicationOnMidClusterSameHosts() {
612 conf.setLong(StochasticLoadBalancer.MAX_STEPS_KEY, 2000000L);
613 conf.setLong("hbase.master.balancer.stochastic.maxRunningTime", 90 * 1000);
614 conf.setFloat("hbase.master.balancer.stochastic.maxMovePercent", 1.0f);
615 loadBalancer.setConf(conf);
616 int numHosts = 100;
617 int numRegions = 100 * 100;
618 int replication = 3;
619 int numRegionsPerServer = 5;
620 int numTables = 10;
621 Map<ServerName, List<HRegionInfo>> serverMap =
622 createServerMap(numHosts, numRegions, numRegionsPerServer, replication, numTables);
623 int numNodesPerHost = 4;
624
625
626 Map<ServerName, List<HRegionInfo>> newServerMap = new TreeMap<ServerName, List<HRegionInfo>>(serverMap);
627 for (Map.Entry<ServerName, List<HRegionInfo>> entry : serverMap.entrySet()) {
628 for (int i=1; i < numNodesPerHost; i++) {
629 ServerName s1 = entry.getKey();
630 ServerName s2 = ServerName.valueOf(s1.getHostname(), s1.getPort() + i, 1);
631 newServerMap.put(s2, new ArrayList<HRegionInfo>());
632 }
633 }
634
635 testWithCluster(newServerMap, null, true, true);
636 }
637
638 private static class ForTestRackManager extends RackManager {
639 int numRacks;
640 public ForTestRackManager(int numRacks) {
641 this.numRacks = numRacks;
642 }
643 @Override
644 public String getRack(ServerName server) {
645 return "rack_" + (server.hashCode() % numRacks);
646 }
647 }
648
649 private static class ForTestRackManagerOne extends RackManager {
650 @Override
651 public String getRack(ServerName server) {
652 return server.getHostname().endsWith("1") ? "rack1" : "rack2";
653 }
654 }
655
656 @Test (timeout = 800000)
657 public void testRegionReplicationOnMidClusterWithRacks() {
658 conf.setLong(StochasticLoadBalancer.MAX_STEPS_KEY, 10000000L);
659 conf.setFloat("hbase.master.balancer.stochastic.maxMovePercent", 1.0f);
660 conf.setLong("hbase.master.balancer.stochastic.maxRunningTime", 120 * 1000);
661 loadBalancer.setConf(conf);
662 int numNodes = 30;
663 int numRegions = numNodes * 30;
664 int replication = 3;
665 int numRegionsPerServer = 28;
666 int numTables = 10;
667 int numRacks = 4;
668 Map<ServerName, List<HRegionInfo>> serverMap =
669 createServerMap(numNodes, numRegions, numRegionsPerServer, replication, numTables);
670 RackManager rm = new ForTestRackManager(numRacks);
671
672 testWithCluster(serverMap, rm, false, true);
673 }
674
675
676 private class MockCluster extends BaseLoadBalancer.Cluster {
677
678 private int[][] localities = null;
679
680 public MockCluster(int[][] regions) {
681
682
683 super(mockClusterServers(regions[0], 1), null, null, null);
684
685 localities = new int[regions.length - 1][];
686 for (int i = 1; i < regions.length; i++) {
687 int regionIndex = i - 1;
688 localities[regionIndex] = new int[regions[i].length - 1];
689 regionIndexToServerIndex[regionIndex] = regions[i][0];
690 for (int j = 1; j < regions[i].length; j++) {
691 int serverIndex = j - 1;
692 localities[regionIndex][serverIndex] = regions[i][j] > 100 ? regions[i][j] % 100 : regions[i][j];
693 }
694 }
695 }
696
697 @Override
698 float getLocalityOfRegion(int region, int server) {
699
700 return localities[region][server] / 100.0f;
701 }
702
703 @Override
704 public int getRegionSizeMB(int region) {
705 return 1;
706 }
707
708 }
709
710 }