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