1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.IOException;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.CoordinatedStateManager;
29 import org.apache.hadoop.hbase.HBaseTestingUtility;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.LocalHBaseCluster;
32 import org.apache.hadoop.hbase.ServerName;
33 import org.apache.hadoop.hbase.MiniHBaseCluster.MiniHBaseClusterRegionServer;
34 import org.apache.hadoop.hbase.master.HMaster;
35 import org.apache.hadoop.hbase.master.ServerManager;
36 import org.apache.hadoop.hbase.testclassification.MediumTests;
37 import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
38 import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
39 import org.apache.zookeeper.KeeperException;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.experimental.categories.Category;
44
45 @Category(MediumTests.class)
46 public class TestRegionServerReportForDuty {
47
48 private static final Log LOG = LogFactory.getLog(TestRegionServerReportForDuty.class);
49
50 private static final long SLEEP_INTERVAL = 500;
51
52 private HBaseTestingUtility testUtil;
53 private LocalHBaseCluster cluster;
54 private RegionServerThread rs;
55 private RegionServerThread rs2;
56 private MasterThread master;
57 private MasterThread backupMaster;
58
59 @Before
60 public void setUp() throws Exception {
61 testUtil = new HBaseTestingUtility();
62 testUtil.startMiniDFSCluster(1);
63 testUtil.startMiniZKCluster(1);
64 testUtil.createRootDir();
65 cluster = new LocalHBaseCluster(testUtil.getConfiguration(), 0, 0);
66 }
67
68 @After
69 public void tearDown() throws Exception {
70 cluster.shutdown();
71 cluster.join();
72 testUtil.shutdownMiniZKCluster();
73 testUtil.shutdownMiniDFSCluster();
74 }
75
76
77
78
79
80 @Test (timeout=180000)
81 public void testReportForDutyWithMasterChange() throws Exception {
82
83
84
85 cluster.getConfiguration().setInt(HConstants.MASTER_PORT, HBaseTestingUtility.randomFreePort());
86 cluster.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
87 cluster.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MAXTOSTART, 1);
88 master = cluster.addMaster();
89 rs = cluster.addRegionServer();
90 LOG.debug("Starting master: " + master.getMaster().getServerName());
91 master.start();
92 rs.start();
93
94
95 cluster.getConfiguration().set(HConstants.REGION_SERVER_IMPL, MyRegionServer.class.getName());
96 rs2 = cluster.addRegionServer();
97
98
99
100 LOG.debug("Starting 2nd region server: " + rs2.getRegionServer().getServerName());
101 rs2.start();
102
103 waitForClusterOnline(master);
104
105
106 master.getMaster().stop("Stopping master");
107
108
109
110 cluster.getConfiguration().setInt(HConstants.MASTER_PORT, HBaseTestingUtility.randomFreePort());
111 cluster.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 2);
112 cluster.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MAXTOSTART, 2);
113 backupMaster = cluster.addMaster();
114 LOG.debug("Starting new master: " + backupMaster.getMaster().getServerName());
115 backupMaster.start();
116
117 waitForClusterOnline(backupMaster);
118
119
120 assertTrue(backupMaster.getMaster().isActiveMaster());
121 assertTrue(backupMaster.getMaster().isInitialized());
122 assertEquals(backupMaster.getMaster().getServerManager().getOnlineServersList().size(), 2);
123
124 }
125
126 private void waitForClusterOnline(MasterThread master) throws InterruptedException {
127 while (true) {
128 if (master.getMaster().isInitialized()
129 && ((MyRegionServer) rs2.getRegionServer()).getRpcStubCreatedFlag() == true) {
130 break;
131 }
132 Thread.sleep(SLEEP_INTERVAL);
133 LOG.debug("Waiting for master to come online ...");
134 }
135 rs.waitForServerOnline();
136 }
137
138
139
140
141
142
143 public static class MyRegionServer extends MiniHBaseClusterRegionServer {
144
145 private ServerName sn;
146
147
148 private boolean rpcStubCreatedFlag = false;
149 private boolean masterChanged = false;
150
151 public MyRegionServer(Configuration conf, CoordinatedStateManager cp)
152 throws IOException, KeeperException,
153 InterruptedException {
154 super(conf, cp);
155 }
156
157 @Override
158 protected synchronized ServerName createRegionServerStatusStub() {
159 sn = super.createRegionServerStatusStub();
160 rpcStubCreatedFlag = true;
161
162
163 while (!masterChanged) {
164 ServerName newSn = super.getMasterAddressTracker().getMasterAddress(true);
165 if (newSn != null && !newSn.equals(sn)) {
166 masterChanged = true;
167 break;
168 }
169 try {
170 Thread.sleep(SLEEP_INTERVAL);
171 } catch (InterruptedException e) {
172 }
173 LOG.debug("Waiting for master switch over ... ");
174 }
175 return sn;
176 }
177
178 public boolean getRpcStubCreatedFlag() {
179 return rpcStubCreatedFlag;
180 }
181 }
182 }