1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.master;
20
21 import org.apache.hadoop.hbase.ClusterStatus;
22 import org.apache.hadoop.hbase.HBaseTestingUtility;
23 import org.apache.hadoop.hbase.testclassification.LargeTests;
24 import org.apache.hadoop.hbase.MasterNotRunningException;
25 import org.apache.hadoop.hbase.MiniHBaseCluster;
26 import org.apache.hadoop.hbase.util.JVMClusterUtil;
27 import org.junit.Test;
28 import org.junit.experimental.categories.Category;
29
30 import java.io.IOException;
31 import java.util.List;
32
33 import static org.junit.Assert.assertFalse;
34 import static org.junit.Assert.assertTrue;
35
36 @Category(LargeTests.class)
37 public class TestMasterFailoverBalancerPersistence {
38
39
40
41
42
43
44
45 @Test(timeout = 240000)
46 public void testMasterFailoverBalancerPersistence() throws Exception {
47 final int NUM_MASTERS = 3;
48 final int NUM_RS = 1;
49
50
51 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
52
53 TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
54 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
55
56 assertTrue(cluster.waitForActiveAndReadyMaster());
57 HMaster active = cluster.getMaster();
58
59 ClusterStatus clusterStatus = active.getClusterStatus();
60 assertTrue(clusterStatus.isBalancerOn());
61
62 active = killActiveAndWaitForNewActive(cluster);
63
64
65 clusterStatus = active.getClusterStatus();
66 assertTrue(clusterStatus.isBalancerOn());
67
68
69 active.balanceSwitch(false);
70
71
72 active = killActiveAndWaitForNewActive(cluster);
73
74
75 clusterStatus = active.getClusterStatus();
76 assertFalse(clusterStatus.isBalancerOn());
77
78
79 TEST_UTIL.shutdownMiniCluster();
80 }
81
82
83
84
85
86
87
88
89
90 private HMaster killActiveAndWaitForNewActive(MiniHBaseCluster cluster)
91 throws InterruptedException, IOException {
92 int activeIndex = getActiveMasterIndex(cluster);
93 HMaster active = cluster.getMaster();
94 cluster.stopMaster(activeIndex);
95 cluster.waitOnMaster(activeIndex);
96 assertTrue(cluster.waitForActiveAndReadyMaster());
97
98 HMaster newActive = cluster.getMaster();
99 assertFalse(active == newActive);
100 return newActive;
101 }
102
103
104
105
106
107
108
109 private int getActiveMasterIndex(MiniHBaseCluster cluster) throws MasterNotRunningException {
110
111 List<JVMClusterUtil.MasterThread> masterThreads = cluster.getMasterThreads();
112
113 for (int i = 0; i < masterThreads.size(); i++) {
114 if (masterThreads.get(i).getMaster().isActiveMaster()) {
115 return i;
116 }
117 }
118 throw new MasterNotRunningException();
119 }
120
121 }