1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import java.io.IOException;
22 import java.security.PrivilegedExceptionAction;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.hbase.classification.InterfaceAudience;
30 import org.apache.hadoop.hbase.classification.InterfaceStability;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.hbase.client.Admin;
33 import org.apache.hadoop.hbase.client.HBaseAdmin;
34 import org.apache.hadoop.hbase.regionserver.HRegionServer;
35 import org.apache.hadoop.hbase.security.User;
36 import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
37 import org.apache.hadoop.hbase.util.Threads;
38
39 import java.util.concurrent.CopyOnWriteArrayList;
40 import org.apache.hadoop.hbase.master.HMaster;
41 import org.apache.hadoop.hbase.util.JVMClusterUtil;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 @InterfaceAudience.Public
60 @InterfaceStability.Evolving
61 public class LocalHBaseCluster {
62 private static final Log LOG = LogFactory.getLog(LocalHBaseCluster.class);
63 private final List<JVMClusterUtil.MasterThread> masterThreads =
64 new CopyOnWriteArrayList<JVMClusterUtil.MasterThread>();
65 private final List<JVMClusterUtil.RegionServerThread> regionThreads =
66 new CopyOnWriteArrayList<JVMClusterUtil.RegionServerThread>();
67 private final static int DEFAULT_NO = 1;
68
69 public static final String LOCAL = "local";
70
71 public static final String LOCAL_COLON = LOCAL + ":";
72 private final Configuration conf;
73 private final Class<? extends HMaster> masterClass;
74 private final Class<? extends HRegionServer> regionServerClass;
75
76
77
78
79
80
81 public LocalHBaseCluster(final Configuration conf)
82 throws IOException {
83 this(conf, DEFAULT_NO);
84 }
85
86
87
88
89
90
91
92
93 public LocalHBaseCluster(final Configuration conf, final int noRegionServers)
94 throws IOException {
95 this(conf, 1, noRegionServers, getMasterImplementation(conf),
96 getRegionServerImplementation(conf));
97 }
98
99
100
101
102
103
104
105
106
107 public LocalHBaseCluster(final Configuration conf, final int noMasters,
108 final int noRegionServers)
109 throws IOException {
110 this(conf, noMasters, noRegionServers, getMasterImplementation(conf),
111 getRegionServerImplementation(conf));
112 }
113
114 @SuppressWarnings("unchecked")
115 private static Class<? extends HRegionServer> getRegionServerImplementation(final Configuration conf) {
116 return (Class<? extends HRegionServer>)conf.getClass(HConstants.REGION_SERVER_IMPL,
117 HRegionServer.class);
118 }
119
120 @SuppressWarnings("unchecked")
121 private static Class<? extends HMaster> getMasterImplementation(final Configuration conf) {
122 return (Class<? extends HMaster>)conf.getClass(HConstants.MASTER_IMPL,
123 HMaster.class);
124 }
125
126
127
128
129
130
131
132
133
134
135
136 @SuppressWarnings("unchecked")
137 public LocalHBaseCluster(final Configuration conf, final int noMasters,
138 final int noRegionServers, final Class<? extends HMaster> masterClass,
139 final Class<? extends HRegionServer> regionServerClass)
140 throws IOException {
141 this.conf = conf;
142
143
144
145 conf.set(HConstants.MASTER_PORT, "0");
146 conf.set(HConstants.REGIONSERVER_PORT, "0");
147 if (conf.getInt(HConstants.REGIONSERVER_INFO_PORT, 0) != -1) {
148 conf.set(HConstants.REGIONSERVER_INFO_PORT, "0");
149 }
150
151 this.masterClass = (Class<? extends HMaster>)
152 conf.getClass(HConstants.MASTER_IMPL, masterClass);
153
154 for (int i = 0; i < noMasters; i++) {
155 addMaster(new Configuration(conf), i);
156 }
157
158 this.regionServerClass =
159 (Class<? extends HRegionServer>)conf.getClass(HConstants.REGION_SERVER_IMPL,
160 regionServerClass);
161
162 for (int i = 0; i < noRegionServers; i++) {
163 addRegionServer(new Configuration(conf), i);
164 }
165 }
166
167 public JVMClusterUtil.RegionServerThread addRegionServer()
168 throws IOException {
169 return addRegionServer(new Configuration(conf), this.regionThreads.size());
170 }
171
172 @SuppressWarnings("unchecked")
173 public JVMClusterUtil.RegionServerThread addRegionServer(
174 Configuration config, final int index)
175 throws IOException {
176
177
178
179
180
181
182
183 CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(conf);
184
185 JVMClusterUtil.RegionServerThread rst =
186 JVMClusterUtil.createRegionServerThread(config, cp, (Class<? extends HRegionServer>) conf
187 .getClass(HConstants.REGION_SERVER_IMPL, this.regionServerClass), index);
188
189 this.regionThreads.add(rst);
190 return rst;
191 }
192
193 public JVMClusterUtil.RegionServerThread addRegionServer(
194 final Configuration config, final int index, User user)
195 throws IOException, InterruptedException {
196 return user.runAs(
197 new PrivilegedExceptionAction<JVMClusterUtil.RegionServerThread>() {
198 @Override
199 public JVMClusterUtil.RegionServerThread run() throws Exception {
200 return addRegionServer(config, index);
201 }
202 });
203 }
204
205 public JVMClusterUtil.MasterThread addMaster() throws IOException {
206 return addMaster(new Configuration(conf), this.masterThreads.size());
207 }
208
209 public JVMClusterUtil.MasterThread addMaster(Configuration c, final int index)
210 throws IOException {
211
212
213
214
215
216
217
218 CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(conf);
219
220 JVMClusterUtil.MasterThread mt = JVMClusterUtil.createMasterThread(c, cp,
221 (Class<? extends HMaster>) conf.getClass(HConstants.MASTER_IMPL, this.masterClass), index);
222 this.masterThreads.add(mt);
223 return mt;
224 }
225
226 public JVMClusterUtil.MasterThread addMaster(
227 final Configuration c, final int index, User user)
228 throws IOException, InterruptedException {
229 return user.runAs(
230 new PrivilegedExceptionAction<JVMClusterUtil.MasterThread>() {
231 @Override
232 public JVMClusterUtil.MasterThread run() throws Exception {
233 return addMaster(c, index);
234 }
235 });
236 }
237
238
239
240
241
242 public HRegionServer getRegionServer(int serverNumber) {
243 return regionThreads.get(serverNumber).getRegionServer();
244 }
245
246
247
248
249 public List<JVMClusterUtil.RegionServerThread> getRegionServers() {
250 return Collections.unmodifiableList(this.regionThreads);
251 }
252
253
254
255
256
257
258 public List<JVMClusterUtil.RegionServerThread> getLiveRegionServers() {
259 List<JVMClusterUtil.RegionServerThread> liveServers =
260 new ArrayList<JVMClusterUtil.RegionServerThread>();
261 List<RegionServerThread> list = getRegionServers();
262 for (JVMClusterUtil.RegionServerThread rst: list) {
263 if (rst.isAlive()) liveServers.add(rst);
264 else LOG.info("Not alive " + rst.getName());
265 }
266 return liveServers;
267 }
268
269
270
271
272 public Configuration getConfiguration() {
273 return this.conf;
274 }
275
276
277
278
279
280 public String waitOnRegionServer(int serverNumber) {
281 JVMClusterUtil.RegionServerThread regionServerThread = this.regionThreads.get(serverNumber);
282 return waitOnRegionServer(regionServerThread);
283 }
284
285
286
287
288
289 public String waitOnRegionServer(JVMClusterUtil.RegionServerThread rst) {
290 while (rst.isAlive()) {
291 try {
292 LOG.info("Waiting on " + rst.getRegionServer().toString());
293 rst.join();
294 } catch (InterruptedException e) {
295 e.printStackTrace();
296 }
297 }
298 regionThreads.remove(rst);
299 return rst.getName();
300 }
301
302
303
304
305 public HMaster getMaster(int serverNumber) {
306 return masterThreads.get(serverNumber).getMaster();
307 }
308
309
310
311
312
313
314 public HMaster getActiveMaster() {
315 for (JVMClusterUtil.MasterThread mt : masterThreads) {
316 if (mt.getMaster().isActiveMaster()) {
317
318
319 if (mt.getMaster().isActiveMaster() && !mt.getMaster().isStopped()) {
320 return mt.getMaster();
321 }
322 }
323 }
324 return null;
325 }
326
327
328
329
330 public List<JVMClusterUtil.MasterThread> getMasters() {
331 return Collections.unmodifiableList(this.masterThreads);
332 }
333
334
335
336
337
338
339 public List<JVMClusterUtil.MasterThread> getLiveMasters() {
340 List<JVMClusterUtil.MasterThread> liveServers = new ArrayList<>();
341 List<JVMClusterUtil.MasterThread> list = getMasters();
342 for (JVMClusterUtil.MasterThread mt: list) {
343 if (mt.isAlive()) {
344 liveServers.add(mt);
345 }
346 }
347 return liveServers;
348 }
349
350
351
352
353
354 public String waitOnMaster(int serverNumber) {
355 JVMClusterUtil.MasterThread masterThread = this.masterThreads.get(serverNumber);
356 return waitOnMaster(masterThread);
357 }
358
359
360
361
362
363 public String waitOnMaster(JVMClusterUtil.MasterThread masterThread) {
364 while (masterThread.isAlive()) {
365 try {
366 LOG.info("Waiting on " + masterThread.getMaster().getServerName().toString());
367 masterThread.join();
368 } catch (InterruptedException e) {
369 e.printStackTrace();
370 }
371 }
372 masterThreads.remove(masterThread);
373 return masterThread.getName();
374 }
375
376
377
378
379
380 public void join() {
381 if (this.regionThreads != null) {
382 for(Thread t: this.regionThreads) {
383 if (t.isAlive()) {
384 try {
385 Threads.threadDumpingIsAlive(t);
386 } catch (InterruptedException e) {
387 LOG.debug("Interrupted", e);
388 }
389 }
390 }
391 }
392 if (this.masterThreads != null) {
393 for (Thread t : this.masterThreads) {
394 if (t.isAlive()) {
395 try {
396 Threads.threadDumpingIsAlive(t);
397 } catch (InterruptedException e) {
398 LOG.debug("Interrupted", e);
399 }
400 }
401 }
402 }
403 }
404
405
406
407
408 public void startup() throws IOException {
409 JVMClusterUtil.startup(this.masterThreads, this.regionThreads);
410 }
411
412
413
414
415 public void shutdown() {
416 JVMClusterUtil.shutdown(this.masterThreads, this.regionThreads);
417 }
418
419
420
421
422
423 public static boolean isLocal(final Configuration c) {
424 boolean mode = c.getBoolean(HConstants.CLUSTER_DISTRIBUTED, HConstants.DEFAULT_CLUSTER_DISTRIBUTED);
425 return(mode == HConstants.CLUSTER_IS_LOCAL);
426 }
427
428
429
430
431
432
433 public static void main(String[] args) throws IOException {
434 Configuration conf = HBaseConfiguration.create();
435 LocalHBaseCluster cluster = new LocalHBaseCluster(conf);
436 cluster.startup();
437 Admin admin = new HBaseAdmin(conf);
438 try {
439 HTableDescriptor htd =
440 new HTableDescriptor(TableName.valueOf(cluster.getClass().getName()));
441 admin.createTable(htd);
442 } finally {
443 admin.close();
444 }
445 cluster.shutdown();
446 }
447 }