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=90000)
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 HMaster master = new HMaster(conf, cp) {
184 InetAddress getRemoteInetAddress(final int port, final long serverStartCode)
185 throws UnknownHostException {
186
187 if (port > sns.length) {
188 return super.getRemoteInetAddress(port, serverStartCode);
189 }
190 ServerName sn = sns[port];
191 return InetAddress.getByAddress(sn.getHostname(),
192 new byte [] {10, 0, 0, (byte)sn.getPort()});
193 }
194
195 @Override
196 ServerManager createServerManager(Server master, MasterServices services)
197 throws IOException {
198 ServerManager sm = super.createServerManager(master, services);
199
200 ServerManager spy = Mockito.spy(sm);
201
202 Mockito.doReturn(true).when(spy).
203 sendRegionClose((ServerName)Mockito.any(), (HRegionInfo)Mockito.any(),
204 Mockito.anyInt(), (ServerName)Mockito.any(), Mockito.anyBoolean());
205 return spy;
206 }
207
208 @Override
209 public ClusterConnection getConnection() {
210
211
212
213 try {
214 return HConnectionTestingUtility.getMockedConnectionAndDecorate(
215 TESTUTIL.getConfiguration(), rs0, rs0, rs0.getServerName(),
216 HRegionInfo.FIRST_META_REGIONINFO);
217 } catch (IOException e) {
218 return null;
219 }
220 }
221 };
222 master.start();
223
224 try {
225
226 while (!master.serviceStarted) Threads.sleep(10);
227
228 for (int i = 0; i < sns.length; i++) {
229 RegionServerReportRequest.Builder request = RegionServerReportRequest.newBuilder();;
230 ServerName sn = ServerName.parseVersionedServerName(sns[i].getVersionedBytes());
231 request.setServer(ProtobufUtil.toServerName(sn));
232 request.setLoad(ServerLoad.EMPTY_SERVERLOAD.obtainServerLoadPB());
233 master.getMasterRpcServices().regionServerReport(null, request.build());
234 }
235 ZooKeeperWatcher zkw = master.getZooKeeper();
236
237 while (!master.isInitialized()) {
238
239
240 ZKAssign.transitionNodeClosed(zkw,
241 HRegionInfo.FIRST_META_REGIONINFO, sn0, -1);
242 Threads.sleep(500);
243 }
244 assertTrue(master.isInitialized());
245 } finally {
246 rs0.stop("Test is done");
247 rs1.stop("Test is done");
248 rs2.stop("Test is done");
249 master.stopMaster();
250 master.join();
251 }
252 }
253
254 @Test
255 public void testNotPullingDeadRegionServerFromZK()
256 throws IOException, KeeperException, InterruptedException {
257 final Configuration conf = TESTUTIL.getConfiguration();
258 final ServerName newServer = ServerName.valueOf("test.sample", 1, 101);
259 final ServerName deadServer = ServerName.valueOf("test.sample", 1, 100);
260 final MockRegionServer rs0 = new MockRegionServer(conf, newServer);
261
262 CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(
263 TESTUTIL.getConfiguration());
264 HMaster master = new HMaster(conf, cp) {
265 @Override
266 void assignMeta(MonitoredTask status, Set<ServerName> previouslyFailedMeatRSs, int replicaId)
267 { }
268
269 @Override
270 void initializeZKBasedSystemTrackers() throws IOException,
271 InterruptedException, KeeperException, CoordinatedStateException {
272 super.initializeZKBasedSystemTrackers();
273
274 serverManager.recordNewServerWithLock(newServer, ServerLoad.EMPTY_SERVERLOAD);
275
276 List<ServerName> onlineServers = new ArrayList<ServerName>();
277 onlineServers.add(deadServer);
278 onlineServers.add(newServer);
279
280 regionServerTracker = Mockito.spy(regionServerTracker);
281 Mockito.doReturn(onlineServers).when(
282 regionServerTracker).getOnlineServers();
283 }
284
285 @Override
286 public ClusterConnection getConnection() {
287
288
289
290 try {
291 return HConnectionTestingUtility.getMockedConnectionAndDecorate(
292 TESTUTIL.getConfiguration(), rs0, rs0, rs0.getServerName(),
293 HRegionInfo.FIRST_META_REGIONINFO);
294 } catch (IOException e) {
295 return null;
296 }
297 }
298 };
299 master.start();
300
301 try {
302
303 while (!master.initialized) Threads.sleep(10);
304 LOG.info("Master is initialized");
305
306 assertFalse("The dead server should not be pulled in",
307 master.serverManager.isServerOnline(deadServer));
308 } finally {
309 master.stopMaster();
310 master.join();
311 }
312 }
313 }