1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.master;
19
20
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
25 import java.net.InetAddress;
26 import java.net.UnknownHostException;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Set;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.hadoop.conf.Configuration;
34 import org.apache.hadoop.hbase.Abortable;
35 import org.apache.hadoop.hbase.CoordinatedStateException;
36 import org.apache.hadoop.hbase.CoordinatedStateManager;
37 import org.apache.hadoop.hbase.CoordinatedStateManagerFactory;
38 import org.apache.hadoop.hbase.HBaseTestingUtility;
39 import org.apache.hadoop.hbase.HConstants;
40 import org.apache.hadoop.hbase.HRegionInfo;
41 import org.apache.hadoop.hbase.testclassification.MediumTests;
42 import org.apache.hadoop.hbase.MetaMockingUtil;
43 import org.apache.hadoop.hbase.Server;
44 import org.apache.hadoop.hbase.ServerLoad;
45 import org.apache.hadoop.hbase.ServerName;
46 import org.apache.hadoop.hbase.TableName;
47 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
48 import org.apache.hadoop.hbase.client.ClusterConnection;
49 import org.apache.hadoop.hbase.client.HConnectionTestingUtility;
50 import org.apache.hadoop.hbase.client.Result;
51 import org.apache.hadoop.hbase.monitoring.MonitoredTask;
52 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
53 import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionServerReportRequest;
54 import org.apache.hadoop.hbase.util.FSUtils;
55 import org.apache.hadoop.hbase.util.Threads;
56 import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
57 import org.apache.hadoop.hbase.zookeeper.ZKAssign;
58 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
59 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
60 import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
61 import org.apache.zookeeper.KeeperException;
62 import org.junit.After;
63 import org.junit.AfterClass;
64 import org.junit.BeforeClass;
65 import org.junit.Test;
66 import org.junit.experimental.categories.Category;
67 import org.mockito.Mockito;
68
69 import com.google.protobuf.ServiceException;
70
71
72
73
74
75
76
77
78
79 @Category(MediumTests.class)
80 public class TestMasterNoCluster {
81 private static final Log LOG = LogFactory.getLog(TestMasterNoCluster.class);
82 private static final HBaseTestingUtility TESTUTIL = new HBaseTestingUtility();
83
84 @BeforeClass
85 public static void setUpBeforeClass() throws Exception {
86 Configuration c = TESTUTIL.getConfiguration();
87
88 FSUtils.setRootDir(c, TESTUTIL.getDataTestDir());
89 DefaultMetricsSystem.setMiniClusterMode(true);
90
91 TESTUTIL.startMiniZKCluster();
92 }
93
94 @AfterClass
95 public static void tearDownAfterClass() throws Exception {
96 TESTUTIL.shutdownMiniZKCluster();
97 }
98
99 @After
100 public void tearDown()
101 throws KeeperException, ZooKeeperConnectionException, IOException {
102
103 ZooKeeperWatcher zkw = new ZooKeeperWatcher(TESTUTIL.getConfiguration(),
104 "@Before", new Abortable() {
105 @Override
106 public void abort(String why, Throwable e) {
107 throw new RuntimeException(why, e);
108 }
109
110 @Override
111 public boolean isAborted() {
112 return false;
113 }
114 });
115 ZKUtil.deleteNodeRecursively(zkw, zkw.baseZNode);
116 zkw.close();
117 }
118
119
120
121
122
123
124
125 @Test (timeout=30000)
126 public void testStopDuringStart()
127 throws IOException, KeeperException, InterruptedException {
128 CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(
129 TESTUTIL.getConfiguration());
130 HMaster master = new HMaster(TESTUTIL.getConfiguration(), cp);
131 master.start();
132
133 master.stopMaster();
134 master.join();
135 }
136
137
138
139
140
141
142
143
144 @Test (timeout=30000)
145 public void testFailover()
146 throws IOException, KeeperException, InterruptedException, ServiceException {
147 final long now = System.currentTimeMillis();
148
149
150 final ServerName sn0 = ServerName.valueOf("0.example.org", 0, now);
151 final ServerName sn1 = ServerName.valueOf("1.example.org", 1, now);
152 final ServerName sn2 = ServerName.valueOf("2.example.org", 2, now);
153 final ServerName [] sns = new ServerName [] {sn0, sn1, sn2};
154
155 final Configuration conf = TESTUTIL.getConfiguration();
156 final MockRegionServer rs0 = new MockRegionServer(conf, sn0);
157 final MockRegionServer rs1 = new MockRegionServer(conf, sn1);
158 final MockRegionServer rs2 = new MockRegionServer(conf, sn2);
159
160
161 MetaTableLocator.setMetaLocation(rs0.getZooKeeper(),
162 rs0.getServerName(), RegionState.State.OPEN);
163 final TableName tableName = TableName.valueOf("t");
164 Result [] results = new Result [] {
165 MetaMockingUtil.getMetaTableRowResult(
166 new HRegionInfo(tableName, HConstants.EMPTY_START_ROW, HBaseTestingUtility.KEYS[1]),
167 rs2.getServerName()),
168 MetaMockingUtil.getMetaTableRowResult(
169 new HRegionInfo(tableName, HBaseTestingUtility.KEYS[1], HBaseTestingUtility.KEYS[2]),
170 rs2.getServerName()),
171 MetaMockingUtil.getMetaTableRowResult(new HRegionInfo(tableName, HBaseTestingUtility.KEYS[2],
172 HConstants.EMPTY_END_ROW),
173 rs2.getServerName())
174 };
175 rs1.setNextResults(HRegionInfo.FIRST_META_REGIONINFO.getRegionName(), results);
176
177
178
179
180
181 CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(
182 TESTUTIL.getConfiguration());
183
184
185
186 final ClusterConnection mockedConnection = HConnectionTestingUtility.getMockedConnectionAndDecorate(
187 TESTUTIL.getConfiguration(), rs0, rs0, rs0.getServerName(),
188 HRegionInfo.FIRST_META_REGIONINFO);
189 HMaster master = new HMaster(conf, cp) {
190 InetAddress getRemoteInetAddress(final int port, final long serverStartCode)
191 throws UnknownHostException {
192
193 if (port > sns.length) {
194 return super.getRemoteInetAddress(port, serverStartCode);
195 }
196 ServerName sn = sns[port];
197 return InetAddress.getByAddress(sn.getHostname(),
198 new byte [] {10, 0, 0, (byte)sn.getPort()});
199 }
200
201 @Override
202 ServerManager createServerManager(Server master, MasterServices services)
203 throws IOException {
204 ServerManager sm = super.createServerManager(master, services);
205
206 ServerManager spy = Mockito.spy(sm);
207
208 Mockito.doReturn(true).when(spy).
209 sendRegionClose((ServerName)Mockito.any(), (HRegionInfo)Mockito.any(),
210 Mockito.anyInt(), (ServerName)Mockito.any(), Mockito.anyBoolean());
211 return spy;
212 }
213
214 @Override
215 public ClusterConnection getConnection() {
216 return mockedConnection;
217 }
218
219 @Override
220 void initNamespace() {
221 }
222 };
223 master.start();
224
225 try {
226
227 while (!master.serviceStarted) Threads.sleep(10);
228
229 for (int i = 0; i < sns.length; i++) {
230 RegionServerReportRequest.Builder request = RegionServerReportRequest.newBuilder();;
231 ServerName sn = ServerName.parseVersionedServerName(sns[i].getVersionedBytes());
232 request.setServer(ProtobufUtil.toServerName(sn));
233 request.setLoad(ServerLoad.EMPTY_SERVERLOAD.obtainServerLoadPB());
234 master.getMasterRpcServices().regionServerReport(null, request.build());
235 }
236 ZooKeeperWatcher zkw = master.getZooKeeper();
237
238 while (!master.isInitialized()) {
239
240
241 ZKAssign.transitionNodeClosed(zkw,
242 HRegionInfo.FIRST_META_REGIONINFO, sn0, -1);
243 Threads.sleep(100);
244 }
245 assertTrue(master.isInitialized());
246 } finally {
247 rs0.stop("Test is done");
248 rs1.stop("Test is done");
249 rs2.stop("Test is done");
250 master.stopMaster();
251 master.join();
252 }
253 }
254
255 @Test
256 public void testNotPullingDeadRegionServerFromZK()
257 throws IOException, KeeperException, InterruptedException {
258 final Configuration conf = TESTUTIL.getConfiguration();
259 final ServerName newServer = ServerName.valueOf("test.sample", 1, 101);
260 final ServerName deadServer = ServerName.valueOf("test.sample", 1, 100);
261 final MockRegionServer rs0 = new MockRegionServer(conf, newServer);
262
263 CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(
264 TESTUTIL.getConfiguration());
265 HMaster master = new HMaster(conf, cp) {
266 @Override
267 void assignMeta(MonitoredTask status, Set<ServerName> previouslyFailedMeatRSs, int replicaId)
268 { }
269
270 @Override
271 void initializeZKBasedSystemTrackers() throws IOException,
272 InterruptedException, KeeperException, CoordinatedStateException {
273 super.initializeZKBasedSystemTrackers();
274
275 serverManager.recordNewServerWithLock(newServer, ServerLoad.EMPTY_SERVERLOAD);
276
277 List<ServerName> onlineServers = new ArrayList<ServerName>();
278 onlineServers.add(deadServer);
279 onlineServers.add(newServer);
280
281 regionServerTracker = Mockito.spy(regionServerTracker);
282 Mockito.doReturn(onlineServers).when(
283 regionServerTracker).getOnlineServers();
284 }
285
286 @Override
287 public ClusterConnection getConnection() {
288
289
290
291 try {
292 return HConnectionTestingUtility.getMockedConnectionAndDecorate(
293 TESTUTIL.getConfiguration(), rs0, rs0, rs0.getServerName(),
294 HRegionInfo.FIRST_META_REGIONINFO);
295 } catch (IOException e) {
296 return null;
297 }
298 }
299
300 @Override
301 void initNamespace() {
302 }
303 };
304 master.start();
305
306 try {
307
308 while (!master.isInitialized()) Threads.sleep(10);
309 LOG.info("Master is initialized");
310
311 assertFalse("The dead server should not be pulled in",
312 master.serverManager.isServerOnline(deadServer));
313 } finally {
314 master.stopMaster();
315 master.join();
316 }
317 }
318 }