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 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 conf.set(HConstants.REGIONSERVER_INFO_PORT, "0");
148
149 this.masterClass = (Class<? extends HMaster>)
150 conf.getClass(HConstants.MASTER_IMPL, masterClass);
151
152 for (int i = 0; i < noMasters; i++) {
153 addMaster(new Configuration(conf), i);
154 }
155
156 this.regionServerClass =
157 (Class<? extends HRegionServer>)conf.getClass(HConstants.REGION_SERVER_IMPL,
158 regionServerClass);
159
160 for (int i = 0; i < noRegionServers; i++) {
161 addRegionServer(new Configuration(conf), i);
162 }
163 }
164
165 public JVMClusterUtil.RegionServerThread addRegionServer()
166 throws IOException {
167 return addRegionServer(new Configuration(conf), this.regionThreads.size());
168 }
169
170 public JVMClusterUtil.RegionServerThread addRegionServer(
171 Configuration config, final int index)
172 throws IOException {
173
174
175
176
177
178
179
180 CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(conf);
181
182 JVMClusterUtil.RegionServerThread rst =
183 JVMClusterUtil.createRegionServerThread(config, cp,
184 this.regionServerClass, index);
185 this.regionThreads.add(rst);
186 return rst;
187 }
188
189 public JVMClusterUtil.RegionServerThread addRegionServer(
190 final Configuration config, final int index, User user)
191 throws IOException, InterruptedException {
192 return user.runAs(
193 new PrivilegedExceptionAction<JVMClusterUtil.RegionServerThread>() {
194 public JVMClusterUtil.RegionServerThread run() throws Exception {
195 return addRegionServer(config, index);
196 }
197 });
198 }
199
200 public JVMClusterUtil.MasterThread addMaster() throws IOException {
201 return addMaster(new Configuration(conf), this.masterThreads.size());
202 }
203
204 public JVMClusterUtil.MasterThread addMaster(Configuration c, final int index)
205 throws IOException {
206
207
208
209
210
211
212
213 CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(conf);
214
215 JVMClusterUtil.MasterThread mt = JVMClusterUtil.createMasterThread(c, cp,
216 (Class<? extends HMaster>) conf.getClass(HConstants.MASTER_IMPL, this.masterClass), index);
217 this.masterThreads.add(mt);
218 return mt;
219 }
220
221 public JVMClusterUtil.MasterThread addMaster(
222 final Configuration c, final int index, User user)
223 throws IOException, InterruptedException {
224 return user.runAs(
225 new PrivilegedExceptionAction<JVMClusterUtil.MasterThread>() {
226 public JVMClusterUtil.MasterThread run() throws Exception {
227 return addMaster(c, index);
228 }
229 });
230 }
231
232
233
234
235
236 public HRegionServer getRegionServer(int serverNumber) {
237 return regionThreads.get(serverNumber).getRegionServer();
238 }
239
240
241
242
243 public List<JVMClusterUtil.RegionServerThread> getRegionServers() {
244 return Collections.unmodifiableList(this.regionThreads);
245 }
246
247
248
249
250
251
252 public List<JVMClusterUtil.RegionServerThread> getLiveRegionServers() {
253 List<JVMClusterUtil.RegionServerThread> liveServers =
254 new ArrayList<JVMClusterUtil.RegionServerThread>();
255 List<RegionServerThread> list = getRegionServers();
256 for (JVMClusterUtil.RegionServerThread rst: list) {
257 if (rst.isAlive()) liveServers.add(rst);
258 else LOG.info("Not alive " + rst.getName());
259 }
260 return liveServers;
261 }
262
263
264
265
266
267
268
269 public String waitOnRegionServer(int serverNumber) {
270 JVMClusterUtil.RegionServerThread regionServerThread =
271 this.regionThreads.remove(serverNumber);
272 while (regionServerThread.isAlive()) {
273 try {
274 LOG.info("Waiting on " +
275 regionServerThread.getRegionServer().toString());
276 regionServerThread.join();
277 } catch (InterruptedException e) {
278 e.printStackTrace();
279 }
280 }
281 return regionServerThread.getName();
282 }
283
284
285
286
287
288
289
290 public String waitOnRegionServer(JVMClusterUtil.RegionServerThread rst) {
291 while (rst.isAlive()) {
292 try {
293 LOG.info("Waiting on " +
294 rst.getRegionServer().toString());
295 rst.join();
296 } catch (InterruptedException e) {
297 e.printStackTrace();
298 }
299 }
300 for (int i=0;i<regionThreads.size();i++) {
301 if (regionThreads.get(i) == rst) {
302 regionThreads.remove(i);
303 break;
304 }
305 }
306 return rst.getName();
307 }
308
309
310
311
312
313 public HMaster getMaster(int serverNumber) {
314 return masterThreads.get(serverNumber).getMaster();
315 }
316
317
318
319
320
321
322 public HMaster getActiveMaster() {
323 for (JVMClusterUtil.MasterThread mt : masterThreads) {
324 if (mt.getMaster().isActiveMaster()) {
325
326
327 if (mt.getMaster().isActiveMaster() && !mt.getMaster().isStopped()) {
328 return mt.getMaster();
329 }
330 }
331 }
332 return null;
333 }
334
335
336
337
338 public List<JVMClusterUtil.MasterThread> getMasters() {
339 return Collections.unmodifiableList(this.masterThreads);
340 }
341
342
343
344
345
346
347 public List<JVMClusterUtil.MasterThread> getLiveMasters() {
348 List<JVMClusterUtil.MasterThread> liveServers =
349 new ArrayList<JVMClusterUtil.MasterThread>();
350 List<JVMClusterUtil.MasterThread> list = getMasters();
351 for (JVMClusterUtil.MasterThread mt: list) {
352 if (mt.isAlive()) {
353 liveServers.add(mt);
354 }
355 }
356 return liveServers;
357 }
358
359
360
361
362
363
364
365 public String waitOnMaster(int serverNumber) {
366 JVMClusterUtil.MasterThread masterThread = this.masterThreads.remove(serverNumber);
367 while (masterThread.isAlive()) {
368 try {
369 LOG.info("Waiting on " + masterThread.getMaster().getServerName().toString());
370 masterThread.join();
371 } catch (InterruptedException e) {
372 e.printStackTrace();
373 }
374 }
375 return masterThread.getName();
376 }
377
378
379
380
381
382
383
384 public String waitOnMaster(JVMClusterUtil.MasterThread masterThread) {
385 while (masterThread.isAlive()) {
386 try {
387 LOG.info("Waiting on " +
388 masterThread.getMaster().getServerName().toString());
389 masterThread.join();
390 } catch (InterruptedException e) {
391 e.printStackTrace();
392 }
393 }
394 for (int i=0;i<masterThreads.size();i++) {
395 if (masterThreads.get(i) == masterThread) {
396 masterThreads.remove(i);
397 break;
398 }
399 }
400 return masterThread.getName();
401 }
402
403
404
405
406
407 public void join() {
408 if (this.regionThreads != null) {
409 for(Thread t: this.regionThreads) {
410 if (t.isAlive()) {
411 try {
412 Threads.threadDumpingIsAlive(t);
413 } catch (InterruptedException e) {
414 LOG.debug("Interrupted", e);
415 }
416 }
417 }
418 }
419 if (this.masterThreads != null) {
420 for (Thread t : this.masterThreads) {
421 if (t.isAlive()) {
422 try {
423 Threads.threadDumpingIsAlive(t);
424 } catch (InterruptedException e) {
425 LOG.debug("Interrupted", e);
426 }
427 }
428 }
429 }
430 }
431
432
433
434
435 public void startup() throws IOException {
436 JVMClusterUtil.startup(this.masterThreads, this.regionThreads);
437 }
438
439
440
441
442 public void shutdown() {
443 JVMClusterUtil.shutdown(this.masterThreads, this.regionThreads);
444 }
445
446
447
448
449
450 public static boolean isLocal(final Configuration c) {
451 boolean mode = c.getBoolean(HConstants.CLUSTER_DISTRIBUTED, HConstants.DEFAULT_CLUSTER_DISTRIBUTED);
452 return(mode == HConstants.CLUSTER_IS_LOCAL);
453 }
454
455
456
457
458
459
460 public static void main(String[] args) throws IOException {
461 Configuration conf = HBaseConfiguration.create();
462 LocalHBaseCluster cluster = new LocalHBaseCluster(conf);
463 cluster.startup();
464 Admin admin = new HBaseAdmin(conf);
465 try {
466 HTableDescriptor htd =
467 new HTableDescriptor(TableName.valueOf(cluster.getClass().getName()));
468 admin.createTable(htd);
469 } finally {
470 admin.close();
471 }
472 cluster.shutdown();
473 }
474 }