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.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.conf.Configuration;
30 import org.apache.hadoop.hbase.client.HBaseAdmin;
31 import org.apache.hadoop.hbase.regionserver.HRegionServer;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import java.util.concurrent.CopyOnWriteArrayList;
34 import org.apache.hadoop.hbase.master.HMaster;
35 import org.apache.hadoop.hbase.util.JVMClusterUtil;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class LocalHBaseCluster {
58 static final Log LOG = LogFactory.getLog(LocalHBaseCluster.class);
59 private final HMaster master;
60 private final List<JVMClusterUtil.RegionServerThread> regionThreads;
61 private final static int DEFAULT_NO = 1;
62
63 public static final String LOCAL = "local";
64
65 public static final String LOCAL_COLON = LOCAL + ":";
66 private final Configuration conf;
67 private final Class<? extends HRegionServer> regionServerClass;
68
69
70
71
72
73
74 public LocalHBaseCluster(final Configuration conf)
75 throws IOException {
76 this(conf, DEFAULT_NO);
77 }
78
79
80
81
82
83
84
85
86 public LocalHBaseCluster(final Configuration conf, final int noRegionServers)
87 throws IOException {
88 this(conf, noRegionServers, HMaster.class, getRegionServerImplementation(conf));
89 }
90
91 @SuppressWarnings("unchecked")
92 private static Class<? extends HRegionServer> getRegionServerImplementation(final Configuration conf) {
93 return (Class<? extends HRegionServer>)conf.getClass(HConstants.REGION_SERVER_IMPL,
94 HRegionServer.class);
95 }
96
97
98
99
100
101
102
103
104
105 @SuppressWarnings("unchecked")
106 public LocalHBaseCluster(final Configuration conf,
107 final int noRegionServers, final Class<? extends HMaster> masterClass,
108 final Class<? extends HRegionServer> regionServerClass)
109 throws IOException {
110 this.conf = conf;
111
112 this.master = HMaster.constructMaster(masterClass, conf);
113
114
115
116 conf.set(HConstants.REGIONSERVER_PORT, "0");
117 this.regionThreads =
118 new CopyOnWriteArrayList<JVMClusterUtil.RegionServerThread>();
119 this.regionServerClass =
120 (Class<? extends HRegionServer>)conf.getClass(HConstants.REGION_SERVER_IMPL,
121 regionServerClass);
122 for (int i = 0; i < noRegionServers; i++) {
123 addRegionServer(i);
124 }
125 }
126
127 public JVMClusterUtil.RegionServerThread addRegionServer() throws IOException {
128 return addRegionServer(this.regionThreads.size());
129 }
130
131 public JVMClusterUtil.RegionServerThread addRegionServer(final int index)
132 throws IOException {
133 JVMClusterUtil.RegionServerThread rst = JVMClusterUtil.createRegionServerThread(this.conf,
134 this.regionServerClass, index);
135 this.regionThreads.add(rst);
136 return rst;
137 }
138
139
140
141
142
143 public HRegionServer getRegionServer(int serverNumber) {
144 return regionThreads.get(serverNumber).getRegionServer();
145 }
146
147
148
149
150 public HMaster getMaster() {
151 return this.master;
152 }
153
154
155
156
157 public List<JVMClusterUtil.RegionServerThread> getRegionServers() {
158 return Collections.unmodifiableList(this.regionThreads);
159 }
160
161
162
163
164
165
166 public List<JVMClusterUtil.RegionServerThread> getLiveRegionServers() {
167 List<JVMClusterUtil.RegionServerThread> liveServers =
168 new ArrayList<JVMClusterUtil.RegionServerThread>();
169 for (JVMClusterUtil.RegionServerThread rst: getRegionServers()) {
170 if (rst.isAlive()) liveServers.add(rst);
171 }
172 return liveServers;
173 }
174
175
176
177
178
179
180
181 public String waitOnRegionServer(int serverNumber) {
182 JVMClusterUtil.RegionServerThread regionServerThread =
183 this.regionThreads.remove(serverNumber);
184 while (regionServerThread.isAlive()) {
185 try {
186 LOG.info("Waiting on " +
187 regionServerThread.getRegionServer().getHServerInfo().toString());
188 regionServerThread.join();
189 } catch (InterruptedException e) {
190 e.printStackTrace();
191 } catch (IOException e) {
192 e.printStackTrace();
193 }
194 }
195 return regionServerThread.getName();
196 }
197
198
199
200
201
202 public void join() {
203 if (this.regionThreads != null) {
204 for(Thread t: this.regionThreads) {
205 if (t.isAlive()) {
206 try {
207 t.join();
208 } catch (InterruptedException e) {
209
210 }
211 }
212 }
213 }
214 if (this.master != null && this.master.isAlive()) {
215 try {
216 this.master.join();
217 } catch(InterruptedException e) {
218
219 }
220 }
221 }
222
223
224
225
226 public void startup() {
227 JVMClusterUtil.startup(this.master, this.regionThreads);
228 }
229
230
231
232
233 public void shutdown() {
234 JVMClusterUtil.shutdown(this.master, this.regionThreads);
235 }
236
237
238
239
240
241 public static boolean isLocal(final Configuration c) {
242 final String mode = c.get(HConstants.CLUSTER_DISTRIBUTED);
243 return mode == null || mode.equals(HConstants.CLUSTER_IS_LOCAL);
244 }
245
246
247
248
249
250
251 public static void main(String[] args) throws IOException {
252 Configuration conf = HBaseConfiguration.create();
253 LocalHBaseCluster cluster = new LocalHBaseCluster(conf);
254 cluster.startup();
255 HBaseAdmin admin = new HBaseAdmin(conf);
256 HTableDescriptor htd =
257 new HTableDescriptor(Bytes.toBytes(cluster.getClass().getName()));
258 admin.createTable(htd);
259 cluster.shutdown();
260 }
261 }